// Buffer changes during snapshot export and release the change feed only at a matching snapshot fence. use std::cli; pub fn snapshot_fence_start() -> vector { [0, 0, 0] } pub fn snapshot_fence_step(snapshot: vector, event: str, value: f64) -> vector { if len(snapshot) != 3 { return []; } let phase = at(snapshot, 0); let saved = at(snapshot, 1); let previous = at(snapshot, 2); if !(phase >= 0.0 && phase < 4.0 && phase % 1.0 == 0.0 && previous >= 0.0 && previous < 7.0 && previous % 1.0 == 0.0 && saved >= 0.0 && saved <= 9007199254740991.0 && saved % 1.0 == 0.0) { return []; } if !(value >= 0.0 && value <= 1000000000.0 && value % 1.0 == 0.0) { return [phase, saved, 0]; } // idle if phase == 0.0 { if event == "capture" && value>0.0 { return [1, value, 1]; // record_fence } } // snapshot if phase == 1.0 { if event == "change" && value>saved { return [1, saved, 2]; // buffer_change } if event == "change" && value<=saved { return [1, saved, 3]; // covered_by_snapshot } if event == "sealed" && value==saved { return [2, saved, 4]; // drain_buffer } } // catchup if phase == 2.0 { if event == "applied" && value>=saved { return [3, value, 5]; // resume_tail } } // live if phase == 3.0 { if event == "applied" && value>saved { return [3, value, 6]; // advance_offset } } [phase, saved, 0] } pub fn snapshot_fence_command(snapshot: vector) -> str { if len(snapshot) != 3 { return "invalid"; } let action = at(snapshot, 2); if action == 0.0 { return "reject"; } if action == 1.0 { return "record_fence"; } if action == 2.0 { return "buffer_change"; } if action == 3.0 { return "covered_by_snapshot"; } if action == 4.0 { return "drain_buffer"; } if action == 5.0 { return "resume_tail"; } if action == 6.0 { return "advance_offset"; } "invalid" } // Stop at the first rejected event. No host effects run during replay. pub fn snapshot_fence_replay(events: arglist, values: vector) -> vector { if args_len(events) != len(values) { return []; } let mut state = snapshot_fence_start(); let mut i = 0; while i < len(values) { state = snapshot_fence_step(state, args_at(events, i), at(values, i)); if at(state, 2) == 0.0 { return []; } i += 1; } state }