Skip to content
Open
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
126 changes: 122 additions & 4 deletions crates/metadb-raft/src/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use metadb_core::{
};
use metadb_storage::sstable::{sst_file_name, sync_dir};
use metadb_storage::{
DurableShardCheckpoint, LifecycleStorageEngine, LsmEngineConfig, PreFlushHook,
DurableShardCheckpoint, FlushIdentity, LifecycleStorageEngine, LsmEngineConfig, PreFlushHook,
ShardedBlockCache,
};

Expand Down Expand Up @@ -141,6 +141,57 @@ struct PersistedSnapshotMeta {
file_specs: Vec<(String, u64, u32)>,
}

struct CommandFlush<'a> {
engine: &'a LifecycleStorageEngine,
open: bool,
}

impl CommandFlush<'_> {
fn commit(mut self) -> Result<()> {
self.open = false;
self.engine.end_command(true)
}
}

impl Drop for CommandFlush<'_> {
fn drop(&mut self) {
if self.open {
let _ = self.engine.end_command(false);
}
}
}

fn attach_flush_hook(engine: &LifecycleStorageEngine, handler: &Arc<dyn ShardApplyHandler>) {
engine.set_pre_flush_hook(Arc::new(HandlerFlushHook {
handler: handler.clone(),
}));
}

struct HandlerFlushHook {
handler: Arc<dyn ShardApplyHandler>,
}

impl PreFlushHook for HandlerFlushHook {
fn on_seal_memtable(
&self,
sealed_raft_index: u64,
sealed_max_hlc: HlcTimestamp,
view_at_m: &metadb_storage::ViewAtRaftIndex<'_>,
) -> Result<Vec<(Vec<u8>, HlcTimestamp, metadb_storage::MvccValue)>> {
match self.handler.pre_flush_hook() {
Some(hook) => hook.on_seal_memtable(sealed_raft_index, sealed_max_hlc, view_at_m),
None => Ok(Vec::new()),
}
}

fn on_flush_durable(&self, durable_raft_index: u64, durable_max_hlc: HlcTimestamp) -> u64 {
match self.handler.pre_flush_hook() {
Some(hook) => hook.on_flush_durable(durable_raft_index, durable_max_hlc),
None => 0,
}
}
}

fn checkpoint_log_id(checkpoint: &DurableShardCheckpoint) -> Option<LogIdOf<MetaDbTypeConfig>> {
if (
checkpoint.flushed_raft_term,
Expand Down Expand Up @@ -223,6 +274,41 @@ impl StateMachineShared {
Ok((state_flushed, updated_cp))
}

fn refresh_flushed_checkpoint(
&self,
engine: &LifecycleStorageEngine,
log_id: &LogIdOf<MetaDbTypeConfig>,
applied_hlc: HlcTimestamp,
) -> Result<()> {
let current = engine.durable_checkpoint();
// Only the apply that flushed this index may stamp its term and descriptor.
if current.flushed_raft_index != log_id.index {
return Ok(());
}
let membership = self.last_membership.read().unwrap().clone();
let membership_bytes = codec::serialize_serde(&membership).unwrap_or_default();
let descriptor = self.handler.encode_descriptor_bytes();
let corrected = DurableShardCheckpoint {
flushed_raft_term: log_id.committed_leader_id().term,
flushed_raft_leader: log_id.committed_leader_id().node_id,
flushed_raft_index: current.flushed_raft_index,
flushed_max_hlc: current.flushed_max_hlc.max(applied_hlc),
contiguous_aux_acked_index: current.contiguous_aux_acked_index,
membership_bytes,
descriptor_bytes: descriptor,
};
if corrected != current {
engine.persist_checkpoint(corrected.clone())?;
}
self.watermarks
.advance_state_flushed(corrected.flushed_raft_index);
if self.handler.pre_flush_hook().is_some() {
self.watermarks
.advance_aux_outbox_durable(engine.last_aux_frontier());
}
Ok(())
}

fn build_snapshot_sync(&self) -> io::Result<SnapshotOf<MetaDbTypeConfig, SnapshotArchive>> {
let _operation = self.operation_lock.lock().unwrap();
let current_idx = self
Expand Down Expand Up @@ -454,6 +540,7 @@ impl ShardStateMachine {
block_cache.clone(),
lsm_config.clone(),
)?);
attach_flush_hook(&engine, &handler);
let cp = engine.durable_checkpoint();

// Report `MANIFEST`'s `flushed_raft_log_id` (`F`) as `applied_state` on startup so OpenRaft
Expand Down Expand Up @@ -632,9 +719,39 @@ impl RaftStateMachine<MetaDbTypeConfig> for ShardStateMachine {
actual: cur_epoch,
})
} else {
self.shared
.handler
.apply_command(log_id.index, commit_hlc, &req, &engine)
let membership = self.shared.last_membership.read().unwrap().clone();
engine.set_flush_identity(FlushIdentity {
term: log_id.committed_leader_id().term,
leader: log_id.committed_leader_id().node_id,
membership: codec::serialize_serde(&membership).unwrap_or_default(),
descriptor: self.shared.handler.encode_descriptor_bytes(),
aux_index: self.shared.watermarks.snapshot_values().1,
});
// Seal only after every key and side effect of this command is in the memtable.
engine.begin_command();
let command = CommandFlush {
engine: &engine,
open: true,
};
match self.shared.handler.apply_command(
log_id.index,
commit_hlc,
&req,
&engine,
) {
Ok(bytes) => command.commit().map(|()| bytes),
Err(error) => {
drop(command);
Err(error)
}
}
};
let apply_res = match apply_res {
Ok(bytes) => self
.shared
.refresh_flushed_checkpoint(&engine, &log_id, commit_hlc)
.map(|()| bytes),
Err(error) => Err(error),
};

let updated_epoch = self.shared.handler.current_epoch();
Expand Down Expand Up @@ -852,6 +969,7 @@ impl RaftStateMachine<MetaDbTypeConfig> for ShardStateMachine {
)
.map_err(|e| io::Error::other(e.to_string()))?,
);
attach_flush_hook(&reopened, &self.shared.handler);

*self.shared.engine.write().unwrap() = reopened.clone();
*self.shared.last_applied_log_id.write().unwrap() = meta.last_log_id;
Expand Down
10 changes: 10 additions & 0 deletions crates/metadb-server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ pub struct StorageSection {
pub bloom_bits_per_key: usize,
pub locality_tombstone_threshold: usize,
pub locality_tombstone_ratio: f64,
/// Queued immutable memtables allowed before writers wait for a flush.
/// The storage design documents a backpressure limit of four.
pub max_imm_memtables: usize,
}

impl Default for StorageSection {
Expand All @@ -86,6 +89,7 @@ impl Default for StorageSection {
bloom_bits_per_key: 10,
locality_tombstone_threshold: 128,
locality_tombstone_ratio: 0.50,
max_imm_memtables: 4,
}
}
}
Expand Down Expand Up @@ -170,6 +174,8 @@ impl ServerTomlConfig {
tombstone_min_ratio_permille: ratio_permille,
bloom_bits_per_key: self.storage.bloom_bits_per_key,
auto_locality_compaction: true,
memtable_size_bytes: self.storage.memtable_size_bytes.max(1),
max_imm_memtables: self.storage.max_imm_memtables.max(1),
})
}
}
Expand Down Expand Up @@ -212,5 +218,9 @@ mod tests {
.unwrap();
assert_eq!(cfg.storage.memtable_size_bytes, 1024);
assert_eq!(cfg.storage.block_cache_bytes, 65536);
assert_eq!(cfg.storage.max_imm_memtables, 4);
let lsm = cfg.to_lsm_config().unwrap();
assert_eq!(lsm.memtable_size_bytes, 1024);
assert_eq!(lsm.max_imm_memtables, 4);
}
}
31 changes: 20 additions & 11 deletions crates/metadb-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,21 @@ impl MetaDbEngine {

/// Lists all live child entries in directory `pid`.
pub fn listdir(&self, pid: InodeId) -> Result<Vec<MetaEntry>> {
self.listdir_limited(pid, None, None, usize::MAX)
}

/// Pinned directory page. `limit == usize::MAX` reads the whole directory.
pub fn listdir_limited(
&self,
pid: InodeId,
after_name: Option<&str>,
name_prefix: Option<&str>,
limit: usize,
) -> Result<Vec<MetaEntry>> {
let hlc = self.clock.now();
let (_, _, storage) = self.global_manager.route_pid(pid)?;
self.pushdown.listdir(&storage, pid, hlc)
self.pushdown
.listdir_limited(&storage, pid, after_name, name_prefix, limit, hlc)
}

/// Executes a lightweight metadata SQL template statement directly without full SQL optimizer overhead.
Expand Down Expand Up @@ -411,16 +423,13 @@ impl MetaDbEngine {
name_prefix,
limit,
} => {
let mut rows = self.listdir(pid)?;
if let Some(after) = after_name {
rows.retain(|e| e.key.name > after);
}
if let Some(pfx) = name_prefix {
rows.retain(|e| e.key.name.starts_with(&pfx));
}
if limit > 0 && rows.len() > limit {
rows.truncate(limit);
}
let page_limit = if limit == 0 { usize::MAX } else { limit };
let rows = self.listdir_limited(
pid,
after_name.as_deref(),
name_prefix.as_deref(),
page_limit,
)?;
Ok(SqlQueryResult::Entries(rows))
}
MetadataSqlCommand::Rename {
Expand Down
79 changes: 33 additions & 46 deletions crates/metadb-storage/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::block::BlockEntryValue;
use crate::cache::ShardedBlockCache;
use crate::manifest::{DurableShardCheckpoint, ManifestLog, VersionEdit};
use crate::sstable::{sst_file_name, sync_dir, SstFileMeta, SstableBuilder, SstableReader};
use crate::version_set::{Version, NUM_LSM_LEVELS};
use crate::version_set::{MergedCursor, Version, NUM_LSM_LEVELS};

pub const DEFAULT_L0_COMPACTION_TRIGGER: usize = 4;
pub const TARGET_L1_SST_BYTES: usize = 64 * 1024 * 1024;
Expand Down Expand Up @@ -469,63 +469,36 @@ impl StorageBackend for LsmStorageBackend {
lvl.sort_by(|a, b| a.meta().min_key.cmp(&b.meta().min_key));
}

// Step 3: Merge 100% of keys from all participating SSTables in deterministic precedence order!
let mut merged: BTreeMap<Vec<u8>, BlockEntryValue> = BTreeMap::new();
// Step 3: Stream every key from the participating SSTables. Earlier readers
// win (`L0` newest flush generation, then `L1..L3`). One data block per
// input file is resident; the output is flushed in `TARGET_L1_SST_BYTES` chunks.
let mut max_raft_idx = 0u64;
let mut max_hlc = metadb_core::HlcTimestamp::ZERO;
let mut input_tombstones_in_range = 0usize;

let mut readers = Vec::new();
for sst in &participating[0] {
max_raft_idx = max_raft_idx.max(sst.meta().max_raft_index);
max_hlc = max_hlc.max(sst.meta().max_hlc);
for (k, v) in sst.read_all_entries()? {
merged.entry(k).or_insert(v);
}
readers.push(sst.clone());
}
for lvl in &participating[1..] {
for sst in lvl {
max_raft_idx = max_raft_idx.max(sst.meta().max_raft_index);
max_hlc = max_hlc.max(sst.meta().max_hlc);
for (k, v) in sst.read_all_entries()? {
merged.entry(k).or_insert(v);
}
readers.push(sst.clone());
}
}
let mut cursor = MergedCursor::open(readers, &[], None)?;

// Step 4: Bottommost Tombstone Drop Invariant (`Resolution B4`):
// Drop `PhysicalTombstone` iff zero non-participating SSTables in levels `> target_level`
// overlap `K`. For `K ∈ [start_key, end_key)`, `target_level` already absorbed the deepest
// overlapping SSTable so `has_overlap_above` is guaranteed `false`; for outer boundary keys,
// `base_version.has_overlap_above(target_level, &k)` preserves tombstones whenever needed!
let mut output_entries: Vec<(Vec<u8>, BlockEntryValue)> = Vec::with_capacity(merged.len());
let mut purged_tombstones = 0usize;

for (k, v) in merged {
match v {
BlockEntryValue::Value(bytes) => {
output_entries.push((k, BlockEntryValue::Value(bytes)));
}
BlockEntryValue::PhysicalTombstone => {
let in_target_range = k.as_slice() >= start_key && k.as_slice() < end_key;
if in_target_range {
input_tombstones_in_range += 1;
}
if !base_version.has_overlap_above(target_level, &k) {
purged_tombstones += 1;
} else {
output_entries.push((k, BlockEntryValue::PhysicalTombstone));
}
}
}
}
let _ = input_tombstones_in_range;

// Partition output into <= TARGET_L1_SST_BYTES non-overlapping SSTables at `target_level`.
let mut new_metas: Vec<SstFileMeta> = Vec::new();
let mut new_readers: Vec<Arc<SstableReader>> = Vec::new();

let mut chunk: Vec<(Vec<u8>, BlockEntryValue)> = Vec::new();
let mut chunk_bytes = 0usize;
let mut purged_tombstones = 0usize;

let write_chunk =
|items: &[(Vec<u8>, BlockEntryValue)]| -> Result<(SstFileMeta, Arc<SstableReader>)> {
Expand All @@ -549,27 +522,41 @@ impl StorageBackend for LsmStorageBackend {
Ok((meta, reader))
};

for (k, v) in output_entries {
while let Some((k, v)) = cursor.next_owned()? {
let emitted = match v {
BlockEntryValue::Value(bytes) => Some(BlockEntryValue::Value(bytes)),
BlockEntryValue::PhysicalTombstone => {
if !base_version.has_overlap_above(target_level, &k) {
purged_tombstones += 1;
None
} else {
Some(BlockEntryValue::PhysicalTombstone)
}
}
};
let Some(value) = emitted else {
continue;
};
let sz = k.len()
+ match &v {
BlockEntryValue::Value(b) => b.len(),
+ match &value {
BlockEntryValue::Value(bytes) => bytes.len(),
BlockEntryValue::PhysicalTombstone => 0,
}
+ 16;
chunk.push((k, v));
chunk.push((k, value));
chunk_bytes += sz;
if chunk_bytes >= TARGET_L1_SST_BYTES {
let (m, r) = write_chunk(&chunk)?;
new_metas.push(m);
new_readers.push(r);
let (meta, reader) = write_chunk(&chunk)?;
new_metas.push(meta);
new_readers.push(reader);
chunk.clear();
chunk_bytes = 0;
}
}
if !chunk.is_empty() {
let (m, r) = write_chunk(&chunk)?;
new_metas.push(m);
new_readers.push(r);
let (meta, reader) = write_chunk(&chunk)?;
new_metas.push(meta);
new_readers.push(reader);
}

// Step 5: Atomically publish `VersionEdit` (taking care to retain any newer L0 files flushed
Expand Down
Loading
Loading