Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ mod replica_service;
pub use replica_service::*;
use replica_service::{
select_bounded_chunk_discard_ranges_from, select_bounded_chunk_write_ranges_from,
trim_btree_map_to_retention, trim_vec_front_to_retention, validate_page_route_qos_config,
trim_btree_map_to_retention, trim_deque_front_to_retention, validate_page_route_qos_config,
validate_chunk_mutation_qos_config, validate_meta_retention_limit,
validate_metadata_admission_policy, validate_metadata_qos_config,
validate_metadata_route_cache_config, validate_storage_receipt_retention,
Expand Down
29 changes: 29 additions & 0 deletions src/object_store_regression_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3092,6 +3092,35 @@ fn a_page_filled_by_small_appends_reallocates_rarely_and_holds_bounded_slack() {
);
}

#[test]
fn the_metadata_change_log_is_a_deque_because_it_drops_one_record_per_append() {
// THE LOG IS ALWAYS AT ITS BOUND once it has filled, and every append then drops its oldest
// record. Held as a `Vec`, that drop shifts every record the log keeps, once per put, for
// ever: 27.7% of the instructions of an 8,000-put profile, 50,000 a put on a log of
// 1,024 records, with the per-put cost 45% above what it was at 1,000 puts. It is a
// deque so the drop moves nothing. The object change feed had the same defect and the same
// cure; this is the second place it was found, so the declaration is pinned.
//
// Comments are excluded and the count is asserted, so a declaration that moved or was
// reworded cannot leave this checking nothing.
const STATE: &str = include_str!("services/meta/service_state.inc.rs");
let declared: Vec<&str> = STATE
.lines()
.map(str::trim)
.filter(|line| !line.starts_with("//") && line.starts_with("changes:") && line.ends_with(','))
.collect();
assert_eq!(
declared.len(),
1,
"the metadata service should declare its change log exactly once: {declared:?}"
);
assert!(
declared[0].contains("VecDeque<MetadataChange>"),
"the metadata change log must be a deque so trimming it moves no records: {}",
declared[0]
);
}

#[test]
fn a_chunk_checksum_is_the_hash_of_its_payload_however_the_write_arrived() {
// `write_chunk` resumes the FNV-1a fold over the appended bytes instead of re-hashing the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ pub(crate) fn trim_btree_map_to_retention<K: Clone + Ord, V>(
overflow as u64
}

pub(crate) fn trim_vec_front_to_retention<T>(items: &mut Vec<T>, retention: usize) -> u64 {
/// Drop the oldest records past `retention`. A deque, because a `Vec` shifts every record it
/// keeps down by one place -- once per append, for ever, on a log that is always at its bound.
pub(crate) fn trim_deque_front_to_retention<T>(
items: &mut std::collections::VecDeque<T>,
retention: usize,
) -> u64 {
let overflow = items.len().saturating_sub(retention);
if overflow > 0 {
items.drain(0..overflow);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ impl MatrixMetaService {
));
}
let first_retained_change_sequence =
self.changes.first().map_or(0, |change| change.sequence);
let last_retained_change_sequence = self.changes.last().map_or(0, |change| change.sequence);
self.changes.front().map_or(0, |change| change.sequence);
let last_retained_change_sequence = self.changes.back().map_or(0, |change| change.sequence);
let first_retained_commit_index =
self.changes.first().map_or(0, |change| change.commit_index);
self.changes.front().map_or(0, |change| change.commit_index);
let last_retained_commit_index =
self.changes.last().map_or(0, |change| change.commit_index);
self.changes.back().map_or(0, |change| change.commit_index);
let retained_changes_compacted = if first_retained_change_sequence > 0 {
after_sequence.saturating_add(1) < first_retained_change_sequence
} else {
Expand Down Expand Up @@ -166,7 +166,7 @@ impl MatrixMetaService {
}
}
report.remaining_changes = retained.len() as u64;
self.changes = retained;
self.changes = retained.into();
if report.compacted_changes > 0 {
self.next_commit_index = self.next_commit_index.saturating_add(1);
}
Expand Down Expand Up @@ -320,7 +320,7 @@ impl MatrixMetaService {
"prepared metadata changefeed compaction compacted set was incomplete".to_string(),
));
}
self.changes = retained;
self.changes = retained.into();
self.next_commit_index = self.next_commit_index.saturating_add(1);
Ok(prepared.report)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ impl MatrixMetaService {
}
}
}
self.changes.push(change);
self.changes.push_back(change);
}

fn apply_mutation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ impl MatrixMetaService {
target_commit_index.unwrap_or_else(|| self.next_commit_index.saturating_sub(1));
let first_retained_change_sequence = self
.changes
.first()
.front()
.map_or(self.next_change_sequence, |change| change.sequence);
let last_retained_change_sequence = self.changes.last().map_or(0, |change| change.sequence);
let last_retained_change_sequence = self.changes.back().map_or(0, |change| change.sequence);
let first_retained_commit_index = self
.changes
.first()
.front()
.map_or(target_commit_index, |change| change.commit_index);
let last_retained_commit_index = self
.changes
.last()
.back()
.map_or(target_commit_index, |change| change.commit_index);

let mut plan = MetadataChangeCheckpointLagPlan {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl MatrixMetaService {
transactions: self.transactions.clone(),
transaction_receipts: self.transaction_receipts.clone(),
change_checkpoints: self.change_checkpoints.clone(),
changes: self.changes.clone(),
changes: self.changes.iter().cloned().collect(),
next_commit_index: self.next_commit_index,
// THE ROSTER, FROM THE SAME SERVICE STATE THE PLACEMENT EXPORT READS. Two export
// functions reading one set of fields is what makes the agreement rule in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl MatrixMetaService {
self.transactions = snapshot.transactions;
self.transaction_receipts = snapshot.transaction_receipts;
self.change_checkpoints = snapshot.change_checkpoints;
self.changes = snapshot.changes;
self.changes = snapshot.changes.into();
self.next_commit_index = snapshot.next_commit_index;
self.next_change_sequence = snapshot.next_change_sequence;
self.compacted_tombstone_count = snapshot.compacted_tombstone_count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl MatrixMetaService {
retained_changes.push(change);
}
}
self.changes = retained_changes;
self.changes = retained_changes.into();

let mut compacted_batch_receipts = 0u64;
if !self.batch_request_receipts.is_empty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl MatrixMetaService {
transactions: BTreeMap::new(),
transaction_receipts: BTreeMap::new(),
change_checkpoints: BTreeMap::new(),
changes: Vec::new(),
changes: std::collections::VecDeque::new(),
next_commit_index: 1,
next_change_sequence: 1,
compacted_tombstone_count: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ impl MatrixMetaService {

pub fn last_applied_metadata_mutation_commit_index(&self) -> u64 {
let mut last_commit = self.next_commit_index.saturating_sub(1);
last_commit = last_commit.max(self.changes.last().map_or(0, |change| change.commit_index));
last_commit = last_commit.max(self.changes.back().map_or(0, |change| change.commit_index));
for receipt in self.batch_request_receipts.values() {
last_commit = last_commit.max(receipt.last_commit_index);
}
Expand Down Expand Up @@ -36,7 +36,7 @@ impl MatrixMetaService {
pub fn last_applied_metadata_mutation_time_ms(&self) -> u64 {
let mut last_update = self
.changes
.last()
.back()
.map_or(0, |change| change.update_time_ms);
for receipt in self.batch_request_receipts.values() {
last_update = last_update.max(receipt.now_ms);
Expand Down Expand Up @@ -79,19 +79,19 @@ impl MatrixMetaService {
};
let max_retained_metadata_commit_index = self
.changes
.last()
.back()
.map(|change| change.commit_index)
.unwrap_or(0)
.max(self.raft_snapshot_floor_commit_index);
let first_retained_change_sequence =
self.changes.first().map_or(0, |change| change.sequence);
let last_retained_change_sequence = self.changes.last().map_or(0, |change| change.sequence);
self.changes.front().map_or(0, |change| change.sequence);
let last_retained_change_sequence = self.changes.back().map_or(0, |change| change.sequence);
let first_retained_metadata_commit_index =
self.changes.first().map_or(0, |change| change.commit_index);
self.changes.front().map_or(0, |change| change.commit_index);
let last_retained_metadata_commit_index =
self.changes.last().map_or(0, |change| change.commit_index);
self.changes.back().map_or(0, |change| change.commit_index);
let retained_mutation_receipts = self.retained_mutation_receipt_count();
let last_change_sequence = self.changes.last().map_or(0, |change| change.sequence);
let last_change_sequence = self.changes.back().map_or(0, |change| change.sequence);
let max_change_checkpoint_lag = self
.change_checkpoints
.values()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl MatrixMetaService {
}
fn trim_meta_retention(&mut self) -> MetadataRetentionConfigReport {
let trimmed_change_records =
trim_vec_front_to_retention(&mut self.changes, self.change_log_retention_limit);
trim_deque_front_to_retention(&mut self.changes, self.change_log_retention_limit);
let trimmed_batch_receipts = trim_btree_map_to_retention(
&mut self.batch_request_receipts,
self.mutation_receipt_retention_limit,
Expand Down
2 changes: 1 addition & 1 deletion src/services/meta/service_state.inc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub struct MatrixMetaService {
pub(crate) transactions: BTreeMap<String, MetadataTransaction>,
transaction_receipts: BTreeMap<String, MetadataTransactionReceipt>,
change_checkpoints: BTreeMap<String, MetadataChangeCheckpoint>,
changes: Vec<MetadataChange>,
changes: std::collections::VecDeque<MetadataChange>,
pub(crate) next_commit_index: u64,
next_change_sequence: u64,
compacted_tombstone_count: u64,
Expand Down
Loading