diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ec51dd97..a8e5b222 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -605,6 +605,7 @@ jobs: compact_owned_append_keeps_publication_bounded_and_recovers_all_replicas \ owned_append_keeps_data_writes_bounded_and_recovers_all_replicas \ star_corpus_scale::star_corpus_workload_verifies_durable_reads_writes_and_appends \ + star_corpus_scale::checkpoint_corpus_measurement::checkpoint_corpus_measurement_drains_both_layouts_and_recovers \ star_corpus_scale::checkpoint_interference::checkpoint_interference_preserves_three_durable_append_copies \ star_recovery_profiles::star_repair_and_append_work_across_local_recovery_profiles \ segmented_star_replicas_keep_objects_through_metadata_failover_and_full_restart @@ -612,6 +613,26 @@ jobs: grep -Fxq "test star_append_amplification::${test_name} ... ok" /tmp/star-append-cost.log done + - name: checkpoint buffering preserves bytes and propagates final flush failures + run: | + set -o pipefail + cargo test --locked --all-features --lib journal_checkpoint_buffer_ -- --test-threads=1 | tee /tmp/checkpoint-buffer.log + grep -q '^test result: ok. 3 passed; 0 failed;' /tmp/checkpoint-buffer.log + for test_name in journal_checkpoint_buffer_preserves_fragmented_bytes_and_checksum journal_checkpoint_buffer_final_write_and_flush_errors_propagate journal_checkpoint_buffer_rejects_a_changed_stream_length + do + grep -Fq "${test_name} ... ok" /tmp/checkpoint-buffer.log + done + + - name: standalone journal control validates bounded progress and publication + run: | + set -o pipefail + cargo test --locked --all-features --lib journal_control_ -- --test-threads=1 | tee /tmp/journal-control.log + grep -q '^test result: ok. 3 passed; 0 failed;' /tmp/journal-control.log + for test_name in journal_control_codecs_are_canonical_bounded_and_versioned journal_control_tcp_checkpoint_reports_published_epoch_and_recovers_tail journal_control_memory_refusal_does_not_fence_ordinary_requests + do + grep -Fq "${test_name} ... ok" /tmp/journal-control.log + done + - name: journaled append decisions preserve binary outcomes and recovery run: | set -o pipefail diff --git a/docs/replica-segmented-journal.md b/docs/replica-segmented-journal.md index 79bf9915..c45494d7 100644 --- a/docs/replica-segmented-journal.md +++ b/docs/replica-segmented-journal.md @@ -94,3 +94,84 @@ The process test starts three metadata nodes and three chunk servers, confirms six distinct live PIDs with their own storage paths and established sockets, writes objects through Star replication, changes metadata leadership, appends data, kills and restarts all six processes, and reads every object from every data replica. + +## Standalone checkpoint control + +`RemoteReplicaClient::journal_status()` reports bounded journal progress without +walking pages or reading payloads. `checkpoint_journal()` synchronizes preceding +mutations and starts a checkpoint. Both commands address the connected standalone +replica directly; they are refused on memory-only nodes and Raft groups. A busy +checkpoint is refused without fencing an otherwise healthy node. I/O failures +fence the node, including failures observed while polling background publication. + +The canonical binary protobuf control request is bounded to 32 bytes and its +status reply to 256 bytes. Unknown actions, versions, duplicate fields, unknown +fields, and noncanonical encodings are refused. Status includes a 16-byte journal +identity, layout, started and published epochs, in-flight state, queued record +count, uncheckpointed logical bytes, committed journal bytes, and segment count. +Committed journal bytes exclude preallocated space and ancillary files; they are +not a measurement of storage I/O. Legacy epochs and identity last until reopen; +segmented identity and committed epochs survive restart. + +Quiesce writers, wait for any current checkpoint, request a checkpoint, then poll +`status.checkpoint_is_published(&requested)` and `!status.checkpoint_in_flight`. +The helper refuses a changed journal identity or layout. A quiescent completed +barrier must also report zero pending records and zero checkpoint debt. A status +observation can reap a completed worker but does not append journal records. + +## Corpus I/O measurement + +The paired local WSL measurement uses three metadata processes and three data +processes, with three acknowledged copies and one-copy reads. Its timed requests +use the data replica API directly; metadata publication QPS is outside that scope. +Every phase drains warmup checkpoints before sampling data-node `/proc/PID/io`. +Writes and appends then include completed automatic checkpoints plus a final +explicit checkpoint. Request QPS and latency describe acknowledgment; cycle QPS +and CPU include checkpoint drain. Read phases do not create extra checkpoints. + +The output includes kernel write bytes, cancelled write bytes, their signed +difference, kernel read bytes, and all-file-descriptor write characters/calls. +The latter include sockets. These Linux process counters are not Windows device +I/O or allocation counts. All live payloads remain cached by the service. The +1 MiB checkpoint threshold is a stress configuration, not a default change. +One paired run is diagnostic evidence, not a sustained throughput guarantee. + +```sh +cargo test --locked --all-features --lib -j1 journal_control_ -- --test-threads=1 +cargo test --locked --all-features --test network_metadata_consensus -j1 checkpoint_corpus_measurement_drains_both_layouts_and_recovers -- --test-threads=1 +cargo test --locked --release --all-features --test network_metadata_consensus -j1 measure_checkpoint_io_at_growing_corpora -- --ignored --nocapture --test-threads=1 +``` + +The functional case covers both layouts and full process restart. The explicit +release measurement pairs 1, 16, and 64 MiB background payloads per data node, +16 clients, and 512 requests per phase, using the same foreground workload and +rotating layout order. Every read checks its complete payload; retained first +and last written pages and cold pages are checked on every replica after restart. + +One release run at commit `aae965b1` in local WSL produced these append results. Each phase appends +2 MiB of logical data, with three acknowledged copies. Kernel writes sum all +three data processes through the completed final checkpoint. + +| Background payload per replica | Layout | Append p99 | Cycle QPS | Kernel write bytes / logical byte | +| --- | --- | ---: | ---: | ---: | +| 1 MiB | legacy | 105 ms | 320 | 27.68 | +| 1 MiB | segmented | 58 ms | 396 | 5.95 | +| 16 MiB | legacy | 688 ms | 116 | 95.77 | +| 16 MiB | segmented | 67 ms | 373 | 8.40 | +| 64 MiB | legacy | 1919 ms | 58 | 313.57 | +| 64 MiB | segmented | 157 ms | 193 | 16.47 | + +These are short stress phases on one machine, with layout order rotated by corpus +size and no cross-host validation. Segmented latency and amplification still grow; +this run does not establish flat latency or a production throughput guarantee. +All cached read phases recorded zero kernel write bytes. Safe reclamation and an +incremental metadata checkpoint remain necessary before default activation. + +## Legacy checkpoint stream buffering + +The legacy checkpoint writer coalesces fragmented encoder reads in a fixed 64 KiB +output buffer. It flushes that buffer before rewriting the checkpoint header, +synchronizing the file, and atomically publishing it. Flush errors propagate to +the replica's existing failure fence. This changes neither stored bytes nor the +full-corpus payload rewrite; it targets small-write syscall overhead. The table +above predates this buffering change. diff --git a/src/cluster/chunk_consensus.rs b/src/cluster/chunk_consensus.rs index 6c9019c6..2a9d2ef2 100644 --- a/src/cluster/chunk_consensus.rs +++ b/src/cluster/chunk_consensus.rs @@ -392,6 +392,12 @@ impl ChunkConsensus { self.failed.load(Ordering::SeqCst) } pub(super) fn request(&self, request: ReplicaRequest) -> Result { + if matches!(request, ReplicaRequest::JournalControl(_)) { + return Err(ObjectError::PreconditionFailed( + "journal control requires standalone replica storage".into(), + )); + } + let request = match request { ReplicaRequest::WriteChunkBatchIfSequence { writes } => { return self.request_batch(writes) @@ -1003,7 +1009,7 @@ fn restore_snapshot( fn read_only(request: &ReplicaRequest) -> bool { match request { ReplicaRequest::ReadNamespaceFence { .. } => true, - ReplicaRequest::FenceNamespace(_) => false, + ReplicaRequest::FenceNamespace(_) | ReplicaRequest::JournalControl(_) => false, ReplicaRequest::Membership(command) => command.is_read(), ReplicaRequest::ReadChunk { .. } | ReplicaRequest::ReadChunkBatch { .. } diff --git a/src/cluster/mod.rs b/src/cluster/mod.rs index 0d626cc8..6343ba04 100644 --- a/src/cluster/mod.rs +++ b/src/cluster/mod.rs @@ -236,6 +236,7 @@ mod replica_image; mod replica_journal; #[cfg(feature = "replica-server")] mod replica_journal_backend; +pub mod replica_journal_control; #[cfg(feature = "replica-server")] mod replica_segmented_journal; diff --git a/src/cluster/replica_checkpoint_stream_tests.rs b/src/cluster/replica_checkpoint_stream_tests.rs new file mode 100644 index 00000000..d830a07e --- /dev/null +++ b/src/cluster/replica_checkpoint_stream_tests.rs @@ -0,0 +1,110 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2026 MatrixArkAI +use super::*; + +struct Fragmented<'a> { + bytes: &'a [u8], +} +impl Read for Fragmented<'_> { + fn read(&mut self, output: &mut [u8]) -> io::Result { + let count = output.len().min(31).min(self.bytes.len()); + output[..count].copy_from_slice(&self.bytes[..count]); + self.bytes = &self.bytes[count..]; + Ok(count) + } +} +#[derive(Default)] +struct Sink { + bytes: Vec, + calls: usize, + short: bool, + fail_write: bool, + fail_flush: bool, + interrupted: bool, +} +impl Write for Sink { + fn write(&mut self, bytes: &[u8]) -> io::Result { + self.calls += 1; + if self.fail_write { + return Err(io::Error::other("checkpoint write failure")); + } + if self.interrupted { + self.interrupted = false; + return Err(io::ErrorKind::Interrupted.into()); + } + let count = if self.short { + bytes.len().min(7) + } else { + bytes.len() + }; + self.bytes.extend_from_slice(&bytes[..count]); + Ok(count) + } + fn flush(&mut self) -> io::Result<()> { + if self.fail_flush { + Err(io::Error::other("checkpoint flush failure")) + } else { + Ok(()) + } + } +} +#[test] +fn journal_checkpoint_buffer_preserves_fragmented_bytes_and_checksum() { + let bytes = (0..1024 * 1024 + 17) + .map(|n| (n % 251) as u8) + .collect::>(); + for short in [false, true] { + let mut sink = Sink { + short, + interrupted: true, + ..Default::default() + }; + let checksum = write_checkpoint_stream( + &mut Fragmented { bytes: &bytes }, + bytes.len() as u64, + &mut sink, + ) + .unwrap(); + assert_eq!(sink.bytes, bytes); + assert_eq!(checksum, fnv1a64(&bytes)); + if !short { + assert!( + sink.calls < 32, + "fragmented writes were not coalesced: {}", + sink.calls + ); + } + } +} +#[test] +fn journal_checkpoint_buffer_final_write_and_flush_errors_propagate() { + // Everything fits in the buffer: only an explicit final flush exposes these errors. + let bytes = [0x5a; 1024]; + for fail_write in [false, true] { + let mut sink = Sink { + fail_write, + fail_flush: !fail_write, + ..Default::default() + }; + let error = write_checkpoint_stream(&mut bytes.as_slice(), bytes.len() as u64, &mut sink) + .unwrap_err(); + assert_eq!(error.kind(), io::ErrorKind::Other); + assert_eq!( + error.to_string(), + if fail_write { + "checkpoint write failure" + } else { + "checkpoint flush failure" + } + ); + } +} +#[test] +fn journal_checkpoint_buffer_rejects_a_changed_stream_length() { + for expected in [3, 5] { + let mut sink = Sink::default(); + let error = + write_checkpoint_stream(&mut b"data".as_slice(), expected, &mut sink).unwrap_err(); + assert_eq!(error.kind(), io::ErrorKind::InvalidData); + } +} diff --git a/src/cluster/replica_journal.rs b/src/cluster/replica_journal.rs index 0ae2b714..7e8d2ce4 100644 --- a/src/cluster/replica_journal.rs +++ b/src/cluster/replica_journal.rs @@ -20,6 +20,8 @@ const MAGIC_ZERO_TAIL: &[u8; 4] = b"MOZ2"; const FRAME_END: u8 = 0xA5; pub(super) struct ReplicaJournal { + journal_id: [u8; 16], + checkpoint_epoch: u64, file: File, // Stable across atomic journal replacement; retain the journal lock too. _lock: Option, @@ -107,7 +109,25 @@ fn outcome_header(payload: &[u8], chain: bool) -> io::Result<[u8; HEADER_LEN]> { Ok(header) } +fn new_journal_id() -> io::Result<[u8; 16]> { + let mut id = [0; 16]; + ring::rand::SecureRandom::fill(&ring::rand::SystemRandom::new(), &mut id) + .map_err(|_| invalid("journal identity generation failed"))?; + Ok(id) +} + impl ReplicaJournal { + pub(super) fn journal_id(&self) -> &[u8] { + &self.journal_id + } + pub(super) fn checkpoint_epoch(&self) -> u64 { + self.checkpoint_epoch + } + + pub(super) fn checkpoint_bytes(&self) -> u64 { + self.bytes_since_checkpoint + } + #[cfg(test)] pub(super) fn open(path: &Path, service: &mut MatrixReplicaService) -> io::Result { Self::open_with_limits(path, service, usize::MAX) @@ -318,6 +338,8 @@ impl ReplicaJournal { file.seek(SeekFrom::End(0))?; } let mut journal = Self { + journal_id: new_journal_id()?, + checkpoint_epoch: 0, file, _lock: Some(lock), path, @@ -607,6 +629,11 @@ impl ReplicaJournal { #[cfg(unix)] pub(super) fn checkpoint(&mut self, service: &MatrixReplicaService) -> io::Result<()> { + let epoch = self + .checkpoint_epoch + .checked_add(1) + .ok_or_else(|| invalid("checkpoint epoch overflow"))?; + let mut stream = super::replica_image::capture(service.export_snapshot(), (1, 1)) .map_err(invalid)? .ok_or_else(|| invalid("replica checkpoint exceeds stream bounds"))?; @@ -620,11 +647,7 @@ impl ReplicaJournal { .open(&temporary)?; fs2::FileExt::try_lock_exclusive(&replacement)?; replacement.write_all(&[0; BASE_HEADER_LEN])?; - let mut output = Digest::new(&mut replacement); - if io::copy(&mut stream, &mut output)? != length { - return Err(invalid("replica checkpoint stream length changed")); - } - let checksum = output.hash; + let checksum = write_checkpoint_stream(&mut stream, length, &mut replacement)?; replacement.seek(SeekFrom::Start(0))?; replacement.write_all(&base_header(length, checksum))?; replacement.sync_all()?; @@ -639,6 +662,7 @@ impl ReplicaJournal { self.bytes_since_checkpoint = 0; File::open(self.path.parent().unwrap())?.sync_all()?; self.mark_initialized()?; + self.checkpoint_epoch = epoch; Ok(()) } @@ -651,6 +675,31 @@ impl ReplicaJournal { } } +// The encoder may yield individual metadata fields as short reads. Coalesce +// those fragments without collecting the checkpoint, and flush before its header +// is rewritten or its file is synchronized and published. +#[cfg(unix)] +fn write_checkpoint_stream( + stream: &mut impl Read, + length: u64, + target: &mut impl Write, +) -> io::Result { + let mut buffered = std::io::BufWriter::with_capacity(64 * 1024, target); + let checksum = { + let mut output = Digest::new(&mut buffered); + if io::copy(stream, &mut output)? != length { + return Err(invalid("replica checkpoint stream length changed")); + } + output.hash + }; + buffered.flush()?; + Ok(checksum) +} + +#[cfg(all(test, unix))] +#[path = "replica_checkpoint_stream_tests.rs"] +mod checkpoint_stream_tests; + const BASE_HEADER_LEN: usize = 32; fn sibling(path: &Path, suffix: &str) -> PathBuf { let mut name = path.as_os_str().to_owned(); @@ -754,6 +803,8 @@ impl Write for Digest { impl ReplicaJournal { pub(super) fn read_only_for_test(path: &Path) -> Self { Self { + journal_id: new_journal_id().unwrap(), + checkpoint_epoch: 0, file: File::open(path).unwrap(), _lock: None, path: path.to_path_buf(), diff --git a/src/cluster/replica_journal_backend.rs b/src/cluster/replica_journal_backend.rs index 6c43ae2e..f4071e96 100644 --- a/src/cluster/replica_journal_backend.rs +++ b/src/cluster/replica_journal_backend.rs @@ -1,12 +1,14 @@ // SPDX-License-Identifier: Apache-2.0 // Copyright 2026 MatrixArkAI use super::replica_journal::ReplicaJournal; +use super::replica_journal_control::ReplicaJournalStatus; use crate::{ chunk_namespace_fence::NamespaceFenceOutcome, outcome::PageOutcome, MatrixReplicaService, ReplicaServiceSnapshot, }; use std::io; pub(super) trait ReplicaJournalBackend: Send { + fn status(&self) -> io::Result; fn requires_context(&self) -> bool { false } @@ -40,6 +42,20 @@ pub(super) trait ReplicaJournalBackend: Send { fn sync_count(&self) -> u64; } impl ReplicaJournalBackend for ReplicaJournal { + fn status(&self) -> io::Result { + Ok(ReplicaJournalStatus { + version: 1, + journal_id: self.journal_id().to_vec(), + layout: ReplicaJournalStatus::LEGACY, + started_epoch: self.checkpoint_epoch(), + published_epoch: self.checkpoint_epoch(), + checkpoint_in_flight: false, + pending_records: 0, + bytes_since_checkpoint: self.checkpoint_bytes(), + journal_bytes: self.logical_length(), + segments: 1, + }) + } fn append_owned( &mut self, outcome: PageOutcome, diff --git a/src/cluster/replica_journal_control.rs b/src/cluster/replica_journal_control.rs new file mode 100644 index 00000000..82359003 --- /dev/null +++ b/src/cluster/replica_journal_control.rs @@ -0,0 +1,180 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2026 MatrixArkAI +//! Bounded standalone journal maintenance and progress. No payloads or paths. +use crate::ObjectError; +use prost::Message; +fn invalid() -> ObjectError { + ObjectError::Corruption("invalid replica journal control message".into()) +} +#[derive(Clone, PartialEq, Eq, Message)] +pub struct ReplicaJournalControl { + #[prost(uint32, tag = "1")] + pub version: u32, + #[prost(uint32, tag = "2")] + pub action: u32, +} +impl ReplicaJournalControl { + pub const STATUS: u32 = 1; + pub const CHECKPOINT: u32 = 2; + pub fn validate(&self) -> Result<(), ObjectError> { + if self.version != 1 || !matches!(self.action, Self::STATUS | Self::CHECKPOINT) { + return Err(invalid()); + } + Ok(()) + } + pub fn from_payload(bytes: &[u8]) -> Result { + if bytes.len() > 32 { + return Err(invalid()); + } + let value = Self::decode(bytes).map_err(|_| invalid())?; + value.validate()?; + if value.encode_to_vec() != bytes { + return Err(invalid()); + } + Ok(value) + } +} +#[derive(Clone, PartialEq, Eq, Message)] +pub struct ReplicaJournalStatus { + #[prost(bytes = "vec", tag = "10")] + pub journal_id: Vec, + #[prost(uint32, tag = "1")] + pub version: u32, + #[prost(uint32, tag = "2")] + pub layout: u32, + #[prost(uint64, tag = "3")] + pub started_epoch: u64, + #[prost(uint64, tag = "4")] + pub published_epoch: u64, + #[prost(bool, tag = "5")] + pub checkpoint_in_flight: bool, + #[prost(uint32, tag = "6")] + pub pending_records: u32, + #[prost(uint64, tag = "7")] + pub bytes_since_checkpoint: u64, + #[prost(uint64, tag = "8")] + pub journal_bytes: u64, + #[prost(uint64, tag = "9")] + pub segments: u64, +} +impl ReplicaJournalStatus { + /// Compare a checkpoint reply with a later observation of the same journal. + /// A legacy journal gets a new identity after reopen; segmented identity persists. + pub fn checkpoint_is_published(&self, requested: &Self) -> Result { + self.validate()?; + requested.validate()?; + if self.journal_id != requested.journal_id || self.layout != requested.layout { + return Err(ObjectError::PreconditionFailed( + "replica journal changed while waiting for checkpoint".into(), + )); + } + Ok(requested.started_epoch > 0 && self.published_epoch >= requested.started_epoch) + } + + pub const LEGACY: u32 = 1; + pub const SEGMENTED: u32 = 2; + pub fn validate(&self) -> Result<(), ObjectError> { + if self.version != 1 + || self.journal_id.len() != 16 + || !matches!(self.layout, Self::LEGACY | Self::SEGMENTED) + || self.published_epoch > self.started_epoch + || self.pending_records > 64 + || self.segments == 0 + || (self.layout == Self::SEGMENTED + && (self.published_epoch == 0 || self.journal_bytes == 0)) + || (!self.checkpoint_in_flight && self.published_epoch != self.started_epoch) + || (self.checkpoint_in_flight && self.started_epoch <= self.published_epoch) + || (self.layout == Self::LEGACY + && (self.checkpoint_in_flight || self.pending_records != 0 || self.segments != 1)) + { + return Err(invalid()); + } + Ok(()) + } + pub fn from_payload(bytes: &[u8]) -> Result { + if bytes.len() > 256 { + return Err(invalid()); + } + let value = Self::decode(bytes).map_err(|_| invalid())?; + value.validate()?; + if value.encode_to_vec() != bytes { + return Err(invalid()); + } + Ok(value) + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn journal_control_codecs_are_canonical_bounded_and_versioned() { + for (action, bytes) in [ + (ReplicaJournalControl::STATUS, vec![8, 1, 16, 1]), + (ReplicaJournalControl::CHECKPOINT, vec![8, 1, 16, 2]), + ] { + let value = ReplicaJournalControl { version: 1, action }; + assert_eq!(value.encode_to_vec(), bytes); + assert_eq!(ReplicaJournalControl::from_payload(&bytes).unwrap(), value); + let mut duplicate = bytes.clone(); + duplicate.extend([8, 1]); + assert!(ReplicaJournalControl::from_payload(&duplicate).is_err()); + let mut unknown = bytes.clone(); + unknown.extend([24, 1]); + assert!(ReplicaJournalControl::from_payload(&unknown).is_err()); + } + for bytes in [ + vec![], + vec![8, 2, 16, 1], + vec![8, 1, 16, 3], + vec![8, 0x81, 0, 16, 1], + vec![0; 33], + b"{}".to_vec(), + ] { + assert!(ReplicaJournalControl::from_payload(&bytes).is_err()); + } + let status = ReplicaJournalStatus { + journal_id: vec![7; 16], + version: 1, + layout: 2, + started_epoch: 2, + published_epoch: 1, + checkpoint_in_flight: true, + pending_records: 3, + bytes_since_checkpoint: 8192, + journal_bytes: 65536, + segments: 2, + }; + let bytes = status.encode_to_vec(); + assert!(bytes.len() < 256); + assert_eq!(ReplicaJournalStatus::from_payload(&bytes).unwrap(), status); + for change in 0..7 { + let mut bad = status.clone(); + match change { + 0 => bad.version = 2, + 1 => bad.layout = 3, + 2 => bad.published_epoch = 3, + 3 => bad.pending_records = 65, + 4 => bad.checkpoint_in_flight = false, + 5 => bad.layout = 1, + _ => bad.published_epoch = 0, + }; + assert!(ReplicaJournalStatus::from_payload(&bad.encode_to_vec()).is_err()); + } + let mut replaced = status.clone(); + replaced.journal_id[0] ^= 1; + assert!(replaced.checkpoint_is_published(&status).is_err()); + let mut complete = status.clone(); + complete.published_epoch = complete.started_epoch; + complete.checkpoint_in_flight = false; + assert!(complete.checkpoint_is_published(&status).unwrap()); + assert!(!status.checkpoint_is_published(&complete).unwrap()); + let mut unknown = bytes; + unknown.extend([80, 1]); + assert!(ReplicaJournalStatus::from_payload(&unknown).is_err()); + assert!(ReplicaJournalStatus::from_payload(&vec![0; 257]).is_err()); + } +} +#[cfg(all(test, unix, feature = "replica-server"))] +#[path = "replica_journal_control_tests.rs"] +mod server_tests; diff --git a/src/cluster/replica_journal_control_tests.rs b/src/cluster/replica_journal_control_tests.rs new file mode 100644 index 00000000..0a65eb68 --- /dev/null +++ b/src/cluster/replica_journal_control_tests.rs @@ -0,0 +1,125 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2026 MatrixArkAI +use crate::cluster::{ + RemoteReplicaClient, ReplicaClientOptions, ReplicaNodeOptions, ReplicaNodeServer, +}; +use crate::{ChunkId, PageId}; +use std::{ + path::PathBuf, + sync::atomic::{AtomicU64, Ordering}, + thread, + time::{Duration, Instant}, +}; +static NEXT: AtomicU64 = AtomicU64::new(0); +struct Directory(PathBuf); +impl Directory { + fn new() -> Self { + let path = std::env::temp_dir().join(format!( + "replica-journal-control-{}-{}", + std::process::id(), + NEXT.fetch_add(1, Ordering::Relaxed) + )); + std::fs::create_dir(&path).unwrap(); + Self(path) + } +} +impl Drop for Directory { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } +} +struct Running { + stop: crate::cluster::ReplicaNodeShutdown, + worker: Option>>, +} +impl Drop for Running { + fn drop(&mut self) { + self.stop.stop(); + if let Some(worker) = self.worker.take() { + worker.join().unwrap().unwrap(); + } + } +} +fn run(server: ReplicaNodeServer) -> (Running, RemoteReplicaClient) { + let endpoint = server.local_addr().unwrap().to_string(); + let stop = server.shutdown_handle(); + let worker = thread::spawn(move || server.serve()); + let client = RemoteReplicaClient::connect(&endpoint, &ReplicaClientOptions::default()).unwrap(); + ( + Running { + stop, + worker: Some(worker), + }, + client, + ) +} +#[test] +fn journal_control_tcp_checkpoint_reports_published_epoch_and_recovers_tail() { + for segmented in [false, true] { + let dir = Directory::new(); + let path = dir.0.join("journal"); + let options = ReplicaNodeOptions { + local_recovery_sidecar: false, + ..Default::default() + }; + let server = if segmented { + ReplicaNodeServer::bind_with_segmented_journal("127.0.0.1:0", options.clone(), &path) + } else { + ReplicaNodeServer::bind_with_journal("127.0.0.1:0", options.clone(), &path) + } + .unwrap(); + let (running, mut client) = run(server); + let page = PageId { + chunk_id: ChunkId::new(1, 1).unwrap(), + page_index: 0, + }; + client.write_chunk(page.clone(), 0, b"base").unwrap(); + let before = client.journal_status().unwrap(); + assert!(before.bytes_since_checkpoint > 0); + assert_eq!(before.layout, if segmented { 2 } else { 1 }); + let requested = client.checkpoint_journal().unwrap(); + if segmented { + assert!(requested.started_epoch > before.published_epoch); + } + let until = Instant::now() + Duration::from_secs(10); + loop { + let status = client.journal_status().unwrap(); + if !status.checkpoint_in_flight && status.checkpoint_is_published(&requested).unwrap() { + assert_eq!(status.bytes_since_checkpoint, 0); + break; + } + assert!(Instant::now() < until, "checkpoint failed to publish"); + thread::sleep(Duration::from_millis(1)); + } + assert_eq!(client.read_chunk(&page, 0, 4).unwrap().data, b"base"); + client.write_chunk(page.clone(), 4, b"tail").unwrap(); + assert!(client.journal_status().unwrap().bytes_since_checkpoint > 0); + drop(client); + drop(running); + let server = if segmented { + ReplicaNodeServer::bind_with_segmented_journal("127.0.0.1:0", options, &path) + } else { + ReplicaNodeServer::bind_with_journal("127.0.0.1:0", options, &path) + } + .unwrap(); + let (_running, mut client) = run(server); + assert_eq!(client.read_chunk(&page, 0, 8).unwrap().data, b"basetail"); + let restored_status = client.journal_status().unwrap(); + if segmented { + assert_eq!(restored_status.journal_id, requested.journal_id); + assert!(restored_status.checkpoint_is_published(&requested).unwrap()); + assert!(restored_status.bytes_since_checkpoint > 0); + } else { + assert_ne!(restored_status.journal_id, requested.journal_id); + assert!(restored_status.checkpoint_is_published(&requested).is_err()); + } + } +} +#[test] +fn journal_control_memory_refusal_does_not_fence_ordinary_requests() { + let (_running, mut client) = + run(ReplicaNodeServer::bind("127.0.0.1:0", ReplicaNodeOptions::default()).unwrap()); + assert!(client.journal_status().is_err()); + assert!(client.checkpoint_journal().is_err()); + assert_eq!(client.ping().unwrap(), 0); +} diff --git a/src/cluster/replica_rpc_client.rs b/src/cluster/replica_rpc_client.rs index d56ac1bf..2207b89a 100644 --- a/src/cluster/replica_rpc_client.rs +++ b/src/cluster/replica_rpc_client.rs @@ -1463,6 +1463,37 @@ impl RemoteReplicaClient { } } + /// Inspect bounded standalone journal progress without reading payloads. + pub fn journal_status( + &mut self, + ) -> Result { + self.journal_control(super::replica_journal_control::ReplicaJournalControl::STATUS) + } + /// Start a checkpoint after prior mutations have synchronized. A busy checkpoint + /// is refused; after success, poll status until the returned epoch is published. + /// Use `checkpoint_is_published` to verify both epoch and journal identity. + /// Quiesce writers when a checkpoint must cover an entire measured interval. + pub fn checkpoint_journal( + &mut self, + ) -> Result { + self.journal_control(super::replica_journal_control::ReplicaJournalControl::CHECKPOINT) + } + fn journal_control( + &mut self, + action: u32, + ) -> Result { + let request = + ReplicaRequest::JournalControl(super::replica_journal_control::ReplicaJournalControl { + version: 1, + action, + }); + // Maintenance is local to the connected replica; never route it to a voter. + match self.call_one(&request)? { + ReplicaReply::JournalStatus(status) => Ok(status), + other => Err(unexpected_reply("standalone journal status", &other)), + } + } + /// Close a namespace or retire a subset of its pinned immutable pages. /// Inspect the fence after an uncertain reply before attempting another transition. pub fn fence_namespace(&mut self, fence: crate::chunk_namespace_fence::NamespaceFence) diff --git a/src/cluster/replica_rpc_messages.rs b/src/cluster/replica_rpc_messages.rs index 3b72e407..166baab7 100644 --- a/src/cluster/replica_rpc_messages.rs +++ b/src/cluster/replica_rpc_messages.rs @@ -213,6 +213,7 @@ pub(crate) fn encode_conditional_write_batch_frame<'a>( #[derive(Clone, Debug, Eq, PartialEq)] pub enum ReplicaRequest { + JournalControl(super::replica_journal_control::ReplicaJournalControl), FenceNamespace(crate::chunk_namespace_fence::NamespaceFence), ReadNamespaceFence { namespace: crate::ChunkId }, Membership(super::MembershipRequest), @@ -301,6 +302,7 @@ pub enum ReplicaRequest { impl ReplicaRequest { pub fn code(&self) -> RequestCode { match self { + Self::JournalControl(_) => RequestCode::JournalControl, Self::FenceNamespace(_) => RequestCode::FenceNamespace, Self::ReadNamespaceFence { .. } => RequestCode::ReadNamespaceFence, ReplicaRequest::Membership(_) => RequestCode::Membership, @@ -340,6 +342,7 @@ impl ReplicaRequest { pub fn encode_payload(&self) -> Vec { let mut out = Vec::new(); match self { + Self::JournalControl(value) => return value.encode_to_vec(), Self::FenceNamespace(fence) => return fence.encode_to_vec(), Self::ReadNamespaceFence { namespace } => return crate::chunk_namespace_fence::NamespaceFenceQuery { version: 1, volume_id: namespace.volume_id(), chunk_id: namespace.id() @@ -516,6 +519,12 @@ impl ReplicaRequest { /// the wrong place -- a page address read one field short still parses into a valid /// `PageId`, naming a page nobody asked about. pub fn decode(code: RequestCode, payload: &[u8]) -> Result { + if code == RequestCode::JournalControl { + return Ok(Self::JournalControl( + super::replica_journal_control::ReplicaJournalControl::from_payload(payload)?, + )); + } + if code == RequestCode::FenceNamespace { return Ok(Self::FenceNamespace(crate::chunk_namespace_fence::NamespaceFence::from_payload(payload)?)); } @@ -766,7 +775,9 @@ impl ReplicaRequest { } let mut cursor = Cursor::new(payload); let request = match code { - RequestCode::FenceNamespace | RequestCode::ReadNamespaceFence => unreachable!(), + RequestCode::JournalControl + | RequestCode::FenceNamespace + | RequestCode::ReadNamespaceFence => unreachable!(), RequestCode::Membership | RequestCode::StateInstall => unreachable!(), RequestCode::ReadChunkStateRange => unreachable!(), RequestCode::ReadChunkBatch => unreachable!(), @@ -863,6 +874,7 @@ impl ReplicaRequest { /// A decoded reply, including a typed pre-admission refusal. #[derive(Clone, Debug, Eq, PartialEq)] pub enum ReplicaReply { + JournalStatus(super::replica_journal_control::ReplicaJournalStatus), NamespaceFence(crate::chunk_namespace_fence::NamespaceFence), Membership(super::MembershipStatus), StateRange(super::state_transfer::ChunkStateRange), @@ -902,6 +914,7 @@ pub enum ReplicaReply { impl ReplicaReply { pub(crate) fn payload_len(&self) -> usize { match self { + Self::JournalStatus(value) => value.encoded_len(), Self::NamespaceFence(fence) => fence.encoded_len(), Self::StateRange(value) => value.encoded_len(), Self::Applied | Self::NotLeader => 0, @@ -961,6 +974,7 @@ impl ReplicaReply { pub fn encode_payload(&self) -> Vec { let mut out = Vec::new(); match self { + Self::JournalStatus(value) => return value.encode_to_vec(), ReplicaReply::NamespaceFence(value) => return value.encode_to_vec(), ReplicaReply::StateRange(value) => return value.encode_to_vec(), ReplicaReply::Applied | ReplicaReply::NotLeader => {} @@ -1054,6 +1068,12 @@ impl ReplicaReply { /// whose shape is read from the reply itself can be read as the wrong shape. The caller /// knows what it asked. pub fn decode(code: RequestCode, payload: &[u8]) -> Result { + if code == RequestCode::JournalControl { + return Ok(Self::JournalStatus( + super::replica_journal_control::ReplicaJournalStatus::from_payload(payload)?, + )); + } + if matches!(code, RequestCode::FenceNamespace | RequestCode::ReadNamespaceFence) { return Ok(Self::NamespaceFence(crate::chunk_namespace_fence::NamespaceFence::from_payload(payload)?)); } @@ -1228,7 +1248,9 @@ impl ReplicaReply { } let mut cursor = Cursor::new(payload); let reply = match code { - RequestCode::FenceNamespace | RequestCode::ReadNamespaceFence => unreachable!(), + RequestCode::JournalControl + | RequestCode::FenceNamespace + | RequestCode::ReadNamespaceFence => unreachable!(), RequestCode::Membership | RequestCode::StateInstall => unreachable!(), RequestCode::ReadChunkStateRange => unreachable!(), RequestCode::GroupIdentity => unreachable!(), diff --git a/src/cluster/replica_rpc_server.rs b/src/cluster/replica_rpc_server.rs index 2f6697c1..57ee48ef 100644 --- a/src/cluster/replica_rpc_server.rs +++ b/src/cluster/replica_rpc_server.rs @@ -854,6 +854,47 @@ fn apply_locked( return Err(ObjectError::Corruption(error.to_string())); } } + if let ReplicaRequest::JournalControl(command) = request { + use super::replica_journal_control::{ReplicaJournalControl, ReplicaJournalStatus}; + command.validate()?; + let NodeReplica { + journal, service, .. + } = guard; + let journal = journal.as_mut().ok_or_else(|| { + ObjectError::PreconditionFailed("replica has no standalone journal".into()) + })?; + let status = match journal.status() { + Ok(status) => status, + Err(error) => { + guard.failed = true; + return Err(ObjectError::Corruption(error.to_string())); + } + }; + if command.action == ReplicaJournalControl::CHECKPOINT && status.checkpoint_in_flight { + return Err(ObjectError::PreconditionFailed( + "replica checkpoint is already in flight".into(), + )); + } + let result = (|| -> std::io::Result { + if command.action == ReplicaJournalControl::STATUS { + return Ok(status); + } + journal.sync_deferred()?; + journal.checkpoint(service)?; + journal.check_health()?; + journal.status() + })(); + match result { + Ok(status) => { + status.validate()?; + return Ok(ReplicaReply::JournalStatus(status)); + } + Err(error) => { + guard.failed = true; + return Err(ObjectError::Corruption(error.to_string())); + } + } + } if guard.staged.as_ref().is_some_and(|stage| stage.expired()) { guard.staged = None; } @@ -1204,6 +1245,9 @@ pub(super) fn apply_to_replica( max_pages: usize, ) -> Result { match request { + ReplicaRequest::JournalControl(_) => Err(ObjectError::PreconditionFailed( + "journal control requires standalone replica storage".into(), + )), ReplicaRequest::FenceNamespace(fence) => { let outcome = crate::chunk_namespace_fence::NamespaceFenceOutcome::decide(guard, fence)?; diff --git a/src/cluster/replica_segmented_journal.rs b/src/cluster/replica_segmented_journal.rs index 383d21d7..ad73be60 100644 --- a/src/cluster/replica_segmented_journal.rs +++ b/src/cluster/replica_segmented_journal.rs @@ -52,6 +52,7 @@ impl Pending { } } struct Completed { + epoch: u64, state: CheckpointState, covered_bytes: u64, } @@ -65,6 +66,8 @@ pub(super) struct SegmentedReplicaJournal { pending_bytes: usize, context: Option, revision: u64, + started_epoch: u64, + published_epoch: u64, bytes_since_checkpoint: u64, publication: Arc>, failure: Arc>>, @@ -115,6 +118,8 @@ impl SegmentedReplicaJournal { pending_bytes: 0, context: None, revision: 0, + started_epoch: epoch, + published_epoch: epoch, bytes_since_checkpoint, publication: Arc::new(Mutex::new(epoch)), failure: Arc::new(Mutex::new(None)), @@ -147,6 +152,7 @@ impl SegmentedReplicaJournal { .map_err(|_| invalid("checkpoint worker panicked"))?? { Some(done) => { + self.published_epoch = done.epoch; self.state = Some(done.state); self.bytes_since_checkpoint = self .bytes_since_checkpoint @@ -246,6 +252,7 @@ impl SegmentedReplicaJournal { .ok_or_else(|| invalid("checkpoint epoch overflow"))?; *epoch }; + self.started_epoch = epoch; self.retire_job(); let mut state = CheckpointState::new(service.export_snapshot_unsealed_without_pages())?; let snapshot = service.export_snapshot_unsealed(); @@ -305,6 +312,7 @@ impl SegmentedReplicaJournal { } let commit = prepared.publish(&self.log)?; drop(publication); + self.published_epoch = epoch; self.cut = commit.through.clone(); state.cut = commit.through; self.state = Some(state); @@ -335,6 +343,7 @@ impl SegmentedReplicaJournal { .ok_or_else(|| invalid("checkpoint epoch overflow"))?; *epoch }; + self.started_epoch = epoch; self.job = Some( thread::Builder::new() .name("replica-checkpoint".into()) @@ -374,6 +383,7 @@ impl SegmentedReplicaJournal { prepared.publish(&log)?; drop(guard); Ok(Some(Completed { + epoch, state, covered_bytes, })) @@ -394,6 +404,21 @@ impl SegmentedReplicaJournal { } } impl ReplicaJournalBackend for SegmentedReplicaJournal { + fn status(&self) -> io::Result { + use super::replica_journal_control::ReplicaJournalStatus; + Ok(ReplicaJournalStatus { + version: 1, + layout: ReplicaJournalStatus::SEGMENTED, + journal_id: self.log.store_id().to_vec(), + started_epoch: self.started_epoch, + published_epoch: self.published_epoch, + checkpoint_in_flight: self.job.is_some(), + pending_records: self.pending.len() as u32, + bytes_since_checkpoint: self.bytes_since_checkpoint, + journal_bytes: self.log.logical_length()?, + segments: self.log.segment_count()? as u64, + }) + } fn requires_context(&self) -> bool { true } diff --git a/src/cluster/replica_wire_frames.rs b/src/cluster/replica_wire_frames.rs index 56de1283..fa93fb10 100644 --- a/src/cluster/replica_wire_frames.rs +++ b/src/cluster/replica_wire_frames.rs @@ -109,6 +109,8 @@ pub enum RequestCode { DigestChunkBatch = 24, FenceNamespace = 25, ReadNamespaceFence = 26, + /// Inspect or explicitly checkpoint a standalone journal. Bounded protobuf. + JournalControl = 27, } impl RequestCode { @@ -145,6 +147,7 @@ impl RequestCode { 24 => Some(RequestCode::DigestChunkBatch), 25 => Some(RequestCode::FenceNamespace), 26 => Some(RequestCode::ReadNamespaceFence), + 27 => Some(RequestCode::JournalControl), _ => None, } } diff --git a/tests/node_regime/checkpoint_corpus_measurement.rs b/tests/node_regime/checkpoint_corpus_measurement.rs new file mode 100644 index 00000000..6e6435f4 --- /dev/null +++ b/tests/node_regime/checkpoint_corpus_measurement.rs @@ -0,0 +1,132 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2026 MatrixArkAI +//! Data replica I/O measured through completed checkpoints, with quiescent writers. +use super::*; + +pub(super) fn barrier(fixture: &Fixture) { + for endpoint in &fixture.chunk_endpoints { + let mut client = + RemoteReplicaClient::connect(endpoint, &ReplicaClientOptions::default()).unwrap(); + let deadline = Instant::now() + Duration::from_secs(120); + while client.journal_status().unwrap().checkpoint_in_flight { + assert!(Instant::now() < deadline, "checkpoint did not finish"); + std::thread::sleep(Duration::from_millis(5)); + } + let requested = client.checkpoint_journal().unwrap(); + loop { + let status = client.journal_status().unwrap(); + if status.checkpoint_is_published(&requested).unwrap() && !status.checkpoint_in_flight { + assert_eq!(status.pending_records, 0); + assert_eq!(status.bytes_since_checkpoint, 0); + break; + } + assert!( + Instant::now() < deadline, + "checkpoint publication did not finish" + ); + std::thread::sleep(Duration::from_millis(5)); + } + } +} + +#[derive(Clone, Copy, Default)] +pub(super) struct IoCounts { + writes: u64, + cancelled: u64, + reads: u64, + chars: u64, + calls: u64, +} +pub(super) fn io_counts(fixture: &Fixture) -> IoCounts { + let mut total = IoCounts::default(); + for node in &fixture.chunks { + let text = std::fs::read_to_string(format!("/proc/{}/io", node.pid)).unwrap(); + let value = |name: &str| { + text.lines() + .find_map(|line| line.strip_prefix(name)) + .unwrap() + .trim() + .parse::() + .unwrap() + }; + total.writes += value("write_bytes:"); + total.cancelled += value("cancelled_write_bytes:"); + total.reads += value("read_bytes:"); + total.chars += value("wchar:"); + total.calls += value("syscw:"); + } + total +} + +#[allow(clippy::too_many_arguments)] +pub(super) fn report( + fixture: &Fixture, + corpus: usize, + clients: usize, + operation: Operation, + samples: &[u128], + elapsed: Duration, + cycle: Duration, + ticks: u64, + before: IoCounts, + after: IoCounts, + load: &str, +) { + let writes = after.writes.checked_sub(before.writes).unwrap(); + let cancelled = after.cancelled.checked_sub(before.cancelled).unwrap(); + // Cancellation may account for earlier dirty pages. Preserve the signed result. + let net = i128::from(writes) - i128::from(cancelled); + let reads = after.reads.checked_sub(before.reads).unwrap(); + let chars = after.chars.checked_sub(before.chars).unwrap(); + let calls = after.calls.checked_sub(before.calls).unwrap(); + let logical = if matches!(operation, Operation::Read) { + 0 + } else { + samples.len() * BYTES + }; + if matches!(operation, Operation::Read) { + assert_eq!(writes, 0); + } else { + assert!(writes > 0); + } + println!("STAR_CHECKPOINT_IO operation={operation:?} segmented={} group_commit={} background_pages={corpus} background_payload_bytes={} clients={clients} operations={} payload_bytes={BYTES} n=3 w=3 r=1 checkpoint_threshold_bytes=1048576 logical_write_bytes={logical} request_us={} cycle_us={} drain_us={} request_qps={:.2} cycle_qps={:.2} p50_us={} p95_us={} p99_us={} max_us={} data_cpu_ticks={ticks} data_kernel_write_bytes={writes} data_cancelled_write_bytes={cancelled} data_net_write_bytes={net} data_kernel_read_bytes={reads} data_all_fd_wchar={chars} data_all_fd_syscw={calls} host_load1_before={load} host_load1_after={}", + fixture.segmented, fixture.group_commit, corpus * BYTES, samples.len(), elapsed.as_micros(), cycle.as_micros(), cycle.saturating_sub(elapsed).as_micros(), + samples.len() as f64 / elapsed.as_secs_f64(), samples.len() as f64 / cycle.as_secs_f64(), + percentile(samples, 50), percentile(samples, 95), percentile(samples, 99), samples.last().unwrap(), host_load()); +} + +#[test] +fn checkpoint_corpus_measurement_drains_both_layouts_and_recovers() { + for segmented in [false, true] { + run_storage_workload( + &[8, 32], + &[2], + 2, + true, + &[Operation::Read, Operation::Write, Operation::Append], + Some(segmented), + ); + } +} + +#[test] +#[ignore = "explicit local paired corpus checkpoint I/O measurement"] +fn measure_checkpoint_io_at_growing_corpora() { + for (case, corpus) in [256, 4096, 16384].into_iter().enumerate() { + let layouts = if case % 2 == 0 { + [false, true] + } else { + [true, false] + }; + for segmented in layouts { + run_storage_workload( + &[corpus], + &[16], + 32, + true, + &[Operation::Read, Operation::Write, Operation::Append], + Some(segmented), + ); + } + } +} diff --git a/tests/node_regime/star_append_amplification.rs b/tests/node_regime/star_append_amplification.rs index b4db870f..a1a15d14 100644 --- a/tests/node_regime/star_append_amplification.rs +++ b/tests/node_regime/star_append_amplification.rs @@ -929,6 +929,37 @@ fn segmented_star_replicas_keep_objects_through_metadata_failover_and_full_resta expected.extend(suffix); } verify_replicas(&fixture, &manifest, &expected); + let mut journal_ids = std::collections::BTreeSet::new(); + for endpoint in &fixture.chunk_endpoints { + let mut node = RemoteReplicaClient::connect(endpoint, &options.replica).unwrap(); + let until = Instant::now() + Duration::from_secs(30); + while node.journal_status().unwrap().checkpoint_in_flight { + assert!(Instant::now() < until, "automatic checkpoint did not drain"); + std::thread::sleep(Duration::from_millis(2)); + } + let checkpoint = node.checkpoint_journal().unwrap(); + assert!(journal_ids.insert(checkpoint.journal_id.clone())); + loop { + let status = node.journal_status().unwrap(); + if status.checkpoint_is_published(&checkpoint).unwrap() && !status.checkpoint_in_flight + { + assert_eq!(status.pending_records, 0); + assert_eq!(status.bytes_since_checkpoint, 0); + // Observation itself must not append maintenance records. + assert_eq!( + node.journal_status().unwrap().journal_bytes, + status.journal_bytes + ); + break; + } + assert!( + Instant::now() < until, + "requested checkpoint did not publish" + ); + std::thread::sleep(Duration::from_millis(2)); + } + } + assert_eq!(journal_ids.len(), 3); fixture.metadata.start(leader); drop(client); drop(keep_alive); diff --git a/tests/node_regime/star_corpus_scale.rs b/tests/node_regime/star_corpus_scale.rs index ed8b44a0..de6c8437 100644 --- a/tests/node_regime/star_corpus_scale.rs +++ b/tests/node_regime/star_corpus_scale.rs @@ -67,6 +67,18 @@ fn phase( operations: usize, id: u64, operation: Operation, +) -> Vec<(PageId, usize)> { + measured_phase(fixture, corpus, clients, operations, id, operation, false) +} + +fn measured_phase( + fixture: &Fixture, + corpus: usize, + clients: usize, + operations: usize, + id: u64, + operation: Operation, + checkpoint_io: bool, ) -> Vec<(PageId, usize)> { // Each appender owns one page. Its starting size and operation count are the // same at every corpus size; unrelated pages are the independent variable. @@ -164,7 +176,15 @@ fn phase( }) .collect::>(); assert!(counts.windows(2).all(|pair| pair[0] == pair[1])); - let before = fixture.data_journals().iter().sum::(); + if checkpoint_io { + checkpoint_corpus_measurement::barrier(fixture); + } + let io_before = checkpoint_io.then(|| checkpoint_corpus_measurement::io_counts(fixture)); + let before = if checkpoint_io { + 0 + } else { + fixture.data_journals().iter().sum::() + }; let cpu = cpu_ticks(fixture); let load = host_load(); let began = Instant::now(); @@ -175,13 +195,22 @@ fn phase( done_rx.recv_timeout(Duration::from_secs(120)).unwrap(); } let elapsed = began.elapsed(); + if checkpoint_io && !matches!(operation, Operation::Read) { + checkpoint_corpus_measurement::barrier(fixture); + } + let cycle = began.elapsed(); let ticks = cpu_ticks(fixture).checked_sub(cpu).unwrap(); - let journal = fixture - .data_journals() - .iter() - .sum::() - .checked_sub(before) - .unwrap(); + let io_after = checkpoint_io.then(|| checkpoint_corpus_measurement::io_counts(fixture)); + let journal = if checkpoint_io { + 0 + } else { + fixture + .data_journals() + .iter() + .sum::() + .checked_sub(before) + .unwrap() + }; for finish in finishers { finish.send(()).unwrap(); } @@ -194,6 +223,13 @@ fn phase( } samples.sort_unstable(); assert_eq!(samples.len(), clients * operations); + if let (Some(before), Some(after)) = (io_before, io_after) { + checkpoint_corpus_measurement::report( + fixture, corpus, clients, operation, &samples, elapsed, cycle, ticks, before, after, + &load, + ); + return written; + } if matches!(operation, Operation::Read) { assert_eq!(journal, 0); } else { @@ -238,12 +274,33 @@ fn run_workload( operations: usize, group_commit: bool, operations_to_measure: &[Operation], +) { + run_storage_workload( + corpora, + concurrency, + operations, + group_commit, + operations_to_measure, + None, + ); +} + +fn run_storage_workload( + corpora: &[usize], + concurrency: &[usize], + operations: usize, + group_commit: bool, + operations_to_measure: &[Operation], + segmented: Option, ) { for &corpus in corpora { assert!((1..=32768).contains(&corpus)); // Matching phases have exactly the same prior benchmark traffic at // every background size. Earlier corpus cases cannot contaminate them. - let mut fixture = Fixture::with_group_commit(group_commit); + let mut fixture = match segmented { + Some(enabled) => Fixture::with_storage(group_commit, enabled, Some(1024 * 1024)), + None => Fixture::with_group_commit(group_commit), + }; let mut seeded = 0; let mut id = 10; let mut verify = Vec::new(); @@ -266,7 +323,11 @@ fn run_workload( assert!((1..=8192).contains(&operations)); for &operation in operations_to_measure { assert!(matches!(operation, Operation::Read) || operations <= 128); - let written = phase(&fixture, corpus, clients, operations, id, operation); + let written = if segmented.is_some() { + measured_phase(&fixture, corpus, clients, operations, id, operation, true) + } else { + phase(&fixture, corpus, clients, operations, id, operation) + }; // Bound retained verification metadata to first/last acknowledged // pages per phase; all foreground reads verify their full payload. if let Some(first) = written.first() { @@ -385,3 +446,6 @@ fn measure_star_sustained_reads_with_shared_sync() { #[path = "star_checkpoint_interference.rs"] mod checkpoint_interference; + +#[path = "checkpoint_corpus_measurement.rs"] +mod checkpoint_corpus_measurement;