Keep subtag failover until the Alarmmgr query completes
This commit is contained in:
@@ -12,7 +12,9 @@ use std::time::{Duration, Instant, SystemTime};
|
||||
use tokio::sync::oneshot;
|
||||
use uuid::Uuid;
|
||||
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 crate::mxaccess::{CommandError, CommandResult, Outcome, Proxy};
|
||||
@@ -95,6 +97,7 @@ struct Primary {
|
||||
source: Option<SnapshotProcess>,
|
||||
local_manager: Option<LocalAlarmmgr>,
|
||||
pending_until: Option<Instant>,
|
||||
require_live_query: bool,
|
||||
records: BTreeMap<Uuid, Record>,
|
||||
truncated: bool,
|
||||
}
|
||||
@@ -110,7 +113,9 @@ impl Primary {
|
||||
) -> Option<Result<(), HRESULT>> {
|
||||
let manager = self.local_manager.as_mut().map(LocalAlarmmgr::observe);
|
||||
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();
|
||||
self.reset();
|
||||
return (may_request || lost_source)
|
||||
@@ -127,12 +132,17 @@ impl Primary {
|
||||
return match reply {
|
||||
Ok(reply) => {
|
||||
self.pending_until = None;
|
||||
let result = reply.and_then(|xml| {
|
||||
if manager_missing {
|
||||
return Err((0x8007_0000 | ERROR_SERVICE_NOT_ACTIVE) as HRESULT);
|
||||
let result = match reply {
|
||||
Ok(None) if !manager_missing => {
|
||||
// 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() {
|
||||
self.reset();
|
||||
}
|
||||
@@ -165,7 +175,7 @@ impl Primary {
|
||||
.source
|
||||
.as_mut()
|
||||
.expect("started helper")
|
||||
.request()
|
||||
.request(!emit && self.require_live_query)
|
||||
.is_err()
|
||||
{
|
||||
self.reset();
|
||||
@@ -336,6 +346,7 @@ impl AlarmRuntime {
|
||||
if subscription.stable_probes >= subscription.settings.failback_stable_probes {
|
||||
subscription.mode = Mode::Alarmmgr;
|
||||
subscription.failures = 0;
|
||||
primary.require_live_query = false;
|
||||
publish_mode(&proxy.publisher, Mode::Alarmmgr, "recovered", 0);
|
||||
}
|
||||
} else {
|
||||
@@ -394,6 +405,7 @@ impl AlarmRuntime {
|
||||
expression: request.subscription_expression,
|
||||
source: None,
|
||||
pending_until: None,
|
||||
require_live_query: false,
|
||||
records: BTreeMap::new(),
|
||||
truncated: false,
|
||||
}),
|
||||
|
||||
@@ -23,13 +23,18 @@ use crate::mxaccess::{pump_messages, Apartment};
|
||||
|
||||
pub const ARGUMENT: &str = "--wnwrap-snapshot-worker";
|
||||
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 {
|
||||
// Closing the job terminates the child even during an uninterruptible COM call.
|
||||
job: OwnedHandle,
|
||||
child: Child,
|
||||
requests: ChildStdin,
|
||||
pub replies: Receiver<Result<String, HRESULT>>,
|
||||
pub replies: Receiver<SnapshotReply>,
|
||||
}
|
||||
|
||||
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.
|
||||
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 subscription = None;
|
||||
let mut request = [0];
|
||||
while input.read_exact(&mut request).is_ok() && request == [1] && pump_messages() {
|
||||
let reply = match &subscription {
|
||||
Some(consumer) => WnWrap::snapshot(consumer, limit),
|
||||
None => WnWrap::subscribe(&expression).and_then(|consumer| {
|
||||
let result = consumer.snapshot(limit);
|
||||
subscription = Some(consumer);
|
||||
result
|
||||
}),
|
||||
};
|
||||
while input.read_exact(&mut request).is_ok()
|
||||
&& matches!(request[0], SNAPSHOT | RECOVERY_PROBE)
|
||||
&& pump_messages()
|
||||
{
|
||||
let reply = (|| {
|
||||
if subscription.is_none() {
|
||||
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();
|
||||
if write_reply(&mut output, reply).is_err() || failed {
|
||||
break;
|
||||
@@ -153,15 +165,16 @@ pub fn run() -> ExitCode {
|
||||
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 {
|
||||
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(&(xml.len() as u32).to_le_bytes())?;
|
||||
output.write_all(xml.as_bytes())?;
|
||||
}
|
||||
Ok(_) => {
|
||||
Ok(Some(_)) => {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
"WnWrap snapshot is too large",
|
||||
@@ -171,10 +184,13 @@ fn write_reply(mut output: impl Write, reply: Result<String, HRESULT>) -> io::Re
|
||||
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];
|
||||
input.read_exact(&mut field)?;
|
||||
let hr = i32::from_le_bytes(field);
|
||||
if hr == QUERY_PENDING {
|
||||
return Ok(Ok(None));
|
||||
}
|
||||
if hr != 0 {
|
||||
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];
|
||||
input.read_exact(&mut bytes)?;
|
||||
String::from_utf8(bytes)
|
||||
.map(Ok)
|
||||
.map(|xml| Ok(Some(xml)))
|
||||
.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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use std::ptr::null_mut;
|
||||
|
||||
use windows_sys::core::{IUnknown_Vtbl, BSTR, GUID, HRESULT, PCWSTR};
|
||||
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 crate::automation::AutomationString;
|
||||
@@ -42,6 +42,19 @@ struct Vtable {
|
||||
i32,
|
||||
*mut i32,
|
||||
) -> 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 {
|
||||
@@ -148,6 +161,40 @@ impl WnWrap {
|
||||
_ => 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 {
|
||||
|
||||
Reference in New Issue
Block a user