Keep subtag failover until the Alarmmgr query completes

This commit is contained in:
Joseph Doherty
2026-11-21 14:23:39 -05:00
parent c102f881b7
commit f2720d4e56
3 changed files with 120 additions and 26 deletions
+20 -8
View File
@@ -12,7 +12,9 @@ use std::time::{Duration, Instant, SystemTime};
use tokio::sync::oneshot; use tokio::sync::oneshot;
use uuid::Uuid; use uuid::Uuid;
use windows_sys::core::HRESULT; use windows_sys::core::HRESULT;
use windows_sys::Win32::Foundation::{ERROR_SERVICE_NOT_ACTIVE, ERROR_TIMEOUT, E_FAIL}; use windows_sys::Win32::Foundation::{
ERROR_NOT_READY, ERROR_SERVICE_NOT_ACTIVE, ERROR_TIMEOUT, E_FAIL,
};
use windows_sys::Win32::System::Threading::INFINITE; use windows_sys::Win32::System::Threading::INFINITE;
use crate::mxaccess::{CommandError, CommandResult, Outcome, Proxy}; use crate::mxaccess::{CommandError, CommandResult, Outcome, Proxy};
@@ -95,6 +97,7 @@ struct Primary {
source: Option<SnapshotProcess>, source: Option<SnapshotProcess>,
local_manager: Option<LocalAlarmmgr>, local_manager: Option<LocalAlarmmgr>,
pending_until: Option<Instant>, pending_until: Option<Instant>,
require_live_query: bool,
records: BTreeMap<Uuid, Record>, records: BTreeMap<Uuid, Record>,
truncated: bool, truncated: bool,
} }
@@ -110,7 +113,9 @@ impl Primary {
) -> Option<Result<(), HRESULT>> { ) -> Option<Result<(), HRESULT>> {
let manager = self.local_manager.as_mut().map(LocalAlarmmgr::observe); let manager = self.local_manager.as_mut().map(LocalAlarmmgr::observe);
let manager_missing = manager == Some(ManagerStatus::Missing); let manager_missing = manager == Some(ManagerStatus::Missing);
if manager == Some(ManagerStatus::Exited) || (emit && manager_missing) { let manager_exited = manager == Some(ManagerStatus::Exited);
self.require_live_query |= manager_exited;
if manager_exited || (emit && manager_missing) {
let lost_source = self.source.is_some(); let lost_source = self.source.is_some();
self.reset(); self.reset();
return (may_request || lost_source) return (may_request || lost_source)
@@ -127,12 +132,17 @@ impl Primary {
return match reply { return match reply {
Ok(reply) => { Ok(reply) => {
self.pending_until = None; self.pending_until = None;
let result = reply.and_then(|xml| { let result = match reply {
if manager_missing { Ok(None) if !manager_missing => {
return Err((0x8007_0000 | ERROR_SERVICE_NOT_ACTIVE) as HRESULT); // Keep the registration while the replacement connects to Galaxy.
return Some(Err((0x8007_0000 | ERROR_NOT_READY) as HRESULT));
} }
self.apply_snapshot(proxy, limit, emit, &xml) Ok(Some(xml)) if !manager_missing => {
}); self.apply_snapshot(proxy, limit, emit, &xml)
}
Ok(_) => Err((0x8007_0000 | ERROR_SERVICE_NOT_ACTIVE) as HRESULT),
Err(hr) => Err(hr),
};
if result.is_err() { if result.is_err() {
self.reset(); self.reset();
} }
@@ -165,7 +175,7 @@ impl Primary {
.source .source
.as_mut() .as_mut()
.expect("started helper") .expect("started helper")
.request() .request(!emit && self.require_live_query)
.is_err() .is_err()
{ {
self.reset(); self.reset();
@@ -336,6 +346,7 @@ impl AlarmRuntime {
if subscription.stable_probes >= subscription.settings.failback_stable_probes { if subscription.stable_probes >= subscription.settings.failback_stable_probes {
subscription.mode = Mode::Alarmmgr; subscription.mode = Mode::Alarmmgr;
subscription.failures = 0; subscription.failures = 0;
primary.require_live_query = false;
publish_mode(&proxy.publisher, Mode::Alarmmgr, "recovered", 0); publish_mode(&proxy.publisher, Mode::Alarmmgr, "recovered", 0);
} }
} else { } else {
@@ -394,6 +405,7 @@ impl AlarmRuntime {
expression: request.subscription_expression, expression: request.subscription_expression,
source: None, source: None,
pending_until: None, pending_until: None,
require_live_query: false,
records: BTreeMap::new(), records: BTreeMap::new(),
truncated: false, truncated: false,
}), }),
@@ -23,13 +23,18 @@ use crate::mxaccess::{pump_messages, Apartment};
pub const ARGUMENT: &str = "--wnwrap-snapshot-worker"; pub const ARGUMENT: &str = "--wnwrap-snapshot-worker";
const MAX_SNAPSHOT_BYTES: usize = 128 * 1024 * 1024; const MAX_SNAPSHOT_BYTES: usize = 128 * 1024 * 1024;
const SNAPSHOT: u8 = 1;
const RECOVERY_PROBE: u8 = 2;
const QUERY_PENDING: i32 = 1;
type SnapshotReply = Result<Option<String>, HRESULT>;
pub struct SnapshotProcess { pub struct SnapshotProcess {
// Closing the job terminates the child even during an uninterruptible COM call. // Closing the job terminates the child even during an uninterruptible COM call.
job: OwnedHandle, job: OwnedHandle,
child: Child, child: Child,
requests: ChildStdin, requests: ChildStdin,
pub replies: Receiver<Result<String, HRESULT>>, pub replies: Receiver<SnapshotReply>,
} }
impl SnapshotProcess { impl SnapshotProcess {
@@ -88,9 +93,10 @@ impl SnapshotProcess {
}); });
} }
pub fn request(&mut self) -> io::Result<()> { pub fn request(&mut self, recovering: bool) -> io::Result<()> {
// The parent allows only one outstanding byte on the request pipe. // The parent allows only one outstanding byte on the request pipe.
self.requests.write_all(&[1]) self.requests
.write_all(&[if recovering { RECOVERY_PROBE } else { SNAPSHOT }])
} }
} }
@@ -136,15 +142,21 @@ pub fn run() -> ExitCode {
let mut output = io::stdout().lock(); let mut output = io::stdout().lock();
let mut subscription = None; let mut subscription = None;
let mut request = [0]; let mut request = [0];
while input.read_exact(&mut request).is_ok() && request == [1] && pump_messages() { while input.read_exact(&mut request).is_ok()
let reply = match &subscription { && matches!(request[0], SNAPSHOT | RECOVERY_PROBE)
Some(consumer) => WnWrap::snapshot(consumer, limit), && pump_messages()
None => WnWrap::subscribe(&expression).and_then(|consumer| { {
let result = consumer.snapshot(limit); let reply = (|| {
subscription = Some(consumer); if subscription.is_none() {
result subscription = Some(WnWrap::subscribe(&expression)?);
}), }
}; let consumer = subscription.as_ref().expect("subscribed");
let xml = consumer.snapshot(limit)?;
if request[0] == RECOVERY_PROBE && !consumer.query_complete()? {
return Ok(None);
}
Ok(Some(xml))
})();
let failed = reply.is_err(); let failed = reply.is_err();
if write_reply(&mut output, reply).is_err() || failed { if write_reply(&mut output, reply).is_err() || failed {
break; break;
@@ -153,15 +165,16 @@ pub fn run() -> ExitCode {
ExitCode::SUCCESS ExitCode::SUCCESS
} }
fn write_reply(mut output: impl Write, reply: Result<String, HRESULT>) -> io::Result<()> { fn write_reply(mut output: impl Write, reply: SnapshotReply) -> io::Result<()> {
match reply { match reply {
Err(hr) => output.write_all(&hr.to_le_bytes())?, Err(hr) => output.write_all(&hr.to_le_bytes())?,
Ok(xml) if xml.len() <= MAX_SNAPSHOT_BYTES => { Ok(None) => output.write_all(&QUERY_PENDING.to_le_bytes())?,
Ok(Some(xml)) if xml.len() <= MAX_SNAPSHOT_BYTES => {
output.write_all(&0_i32.to_le_bytes())?; output.write_all(&0_i32.to_le_bytes())?;
output.write_all(&(xml.len() as u32).to_le_bytes())?; output.write_all(&(xml.len() as u32).to_le_bytes())?;
output.write_all(xml.as_bytes())?; output.write_all(xml.as_bytes())?;
} }
Ok(_) => { Ok(Some(_)) => {
return Err(io::Error::new( return Err(io::Error::new(
io::ErrorKind::InvalidData, io::ErrorKind::InvalidData,
"WnWrap snapshot is too large", "WnWrap snapshot is too large",
@@ -171,10 +184,13 @@ fn write_reply(mut output: impl Write, reply: Result<String, HRESULT>) -> io::Re
output.flush() output.flush()
} }
fn read_reply(mut input: impl Read) -> io::Result<Result<String, HRESULT>> { fn read_reply(mut input: impl Read) -> io::Result<SnapshotReply> {
let mut field = [0; 4]; let mut field = [0; 4];
input.read_exact(&mut field)?; input.read_exact(&mut field)?;
let hr = i32::from_le_bytes(field); let hr = i32::from_le_bytes(field);
if hr == QUERY_PENDING {
return Ok(Ok(None));
}
if hr != 0 { if hr != 0 {
return Ok(Err(hr)); return Ok(Err(hr));
} }
@@ -189,6 +205,25 @@ fn read_reply(mut input: impl Read) -> io::Result<Result<String, HRESULT>> {
let mut bytes = vec![0; length]; let mut bytes = vec![0; length];
input.read_exact(&mut bytes)?; input.read_exact(&mut bytes)?;
String::from_utf8(bytes) String::from_utf8(bytes)
.map(Ok) .map(|xml| Ok(Some(xml)))
.map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error)) .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pending_probe_keeps_the_following_snapshot_aligned() {
let mut frames = Vec::new();
write_reply(&mut frames, Ok(None)).unwrap();
write_reply(&mut frames, Ok(Some("<ALARM_RECORDS/>".into()))).unwrap();
let mut frames = frames.as_slice();
assert_eq!(read_reply(&mut frames).unwrap(), Ok(None));
assert_eq!(
read_reply(&mut frames).unwrap(),
Ok(Some("<ALARM_RECORDS/>".into()))
);
assert!(frames.is_empty());
}
}
+48 -1
View File
@@ -6,7 +6,7 @@ use std::ptr::null_mut;
use windows_sys::core::{IUnknown_Vtbl, BSTR, GUID, HRESULT, PCWSTR}; use windows_sys::core::{IUnknown_Vtbl, BSTR, GUID, HRESULT, PCWSTR};
use windows_sys::Win32::Foundation::E_FAIL; use windows_sys::Win32::Foundation::E_FAIL;
use windows_sys::Win32::System::Com::{CoCreateInstance, CLSCTX_INPROC_SERVER}; use windows_sys::Win32::System::Com::{CoCreateInstance, CoTaskMemFree, CLSCTX_INPROC_SERVER};
use windows_sys::Win32::System::Variant::{VariantClear, VARIANT, VT_BSTR}; use windows_sys::Win32::System::Variant::{VariantClear, VARIANT, VT_BSTR};
use crate::automation::AutomationString; use crate::automation::AutomationString;
@@ -42,6 +42,19 @@ struct Vtable {
i32, i32,
*mut i32, *mut i32,
) -> HRESULT, ) -> HRESULT,
ack_and_select: [usize; 3],
get_statistics: unsafe extern "system" fn(
*mut c_void,
*mut i32,
*mut i32,
*mut i32,
*mut i32,
*mut i32,
*mut *mut c_void,
*mut *mut c_void,
*mut *mut c_void,
*mut i32,
) -> HRESULT,
} }
pub struct WnWrap { pub struct WnWrap {
@@ -148,6 +161,40 @@ impl WnWrap {
_ => Err(E_FAIL), _ => Err(E_FAIL),
} }
} }
pub fn query_complete(&self) -> Result<bool, HRESULT> {
let mut percent = 0;
let mut total = 0;
let mut active = 0;
let mut new_alarms = 0;
let mut change_count = 0;
let mut codes = null_mut();
let mut positions = null_mut();
let mut handles = null_mut();
let mut result = 0;
// SAFETY: the v2 method takes five LONG outputs and three output pointers.
// WnWrap allocates those arrays with CoTaskMemAlloc, including on native error.
let hr = unsafe {
let hr = (table(&self.v2).get_statistics)(
self.v2.as_ptr(),
&mut percent,
&mut total,
&mut active,
&mut new_alarms,
&mut change_count,
&mut codes,
&mut positions,
&mut handles,
&mut result,
);
CoTaskMemFree(codes);
CoTaskMemFree(positions);
CoTaskMemFree(handles);
hr
};
native_result(hr, result)?;
Ok(percent >= 100)
}
} }
impl Drop for WnWrap { impl Drop for WnWrap {