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
35 changes: 32 additions & 3 deletions crates/ragfs/src/multibackend/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use rand::Rng;
use tokio::sync::watch;
use tracing::warn;
use tracing::{info, warn};

use crate::core::context::{FsContextInner, FS_CTX};
use crate::core::errors::{Error, Result};
Expand Down Expand Up @@ -52,8 +53,7 @@ impl CheckpointWorker {

/// Run checkpoint rounds at the configured interval until cancellation.
pub async fn run(&self, interval: Duration, mut cancellation: watch::Receiver<bool>) {
let mut ticker = tokio::time::interval(interval);
ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
let mut ticker = checkpoint_ticker(interval);
loop {
tokio::select! {
_ = ticker.tick() => if let Err(error) = self.run_once(interval).await {
Expand Down Expand Up @@ -82,6 +82,7 @@ impl CheckpointWorker {
if self.store.protocol_status().await? != ProtocolStatus::Stable {
return Ok(());
}
info!("multi-write checkpoint round started");
let now = now_ns()?;
let interval_ns = u64::try_from(interval.as_nanos()).unwrap_or(u64::MAX);
for account in self.store.initialized_accounts().await? {
Expand Down Expand Up @@ -157,10 +158,30 @@ impl CheckpointWorker {
}
}
}
info!("multi-write checkpoint round completed");
Ok(())
}
}

/// Create the periodic timer for checkpoint discovery rounds.
fn checkpoint_ticker(interval: Duration) -> tokio::time::Interval {
let jitter_bound_secs = interval.as_secs().saturating_div(60).min(i64::MAX as u64) as i64;
let jitter_secs = if jitter_bound_secs == 0 {
0
} else {
rand::thread_rng().gen_range(-jitter_bound_secs..=jitter_bound_secs)
};
let delay = if jitter_secs.is_negative() {
interval.saturating_sub(Duration::from_secs((-jitter_secs) as u64))
} else {
interval.saturating_add(Duration::from_secs(jitter_secs as u64))
};
let start = tokio::time::Instant::now() + delay;
let mut ticker = tokio::time::interval_at(start, interval);
ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
ticker
}

/// Result of one bounded checkpoint build attempt.
pub enum CheckpointBuildResult {
/// No new sealed sequence exists for this scope.
Expand Down Expand Up @@ -372,6 +393,14 @@ impl CheckpointBuilder {
CheckpointsManifest::validate,
)
.await?;
info!(
account = %scope.account_id,
partition = scope.partition_id,
epoch = scope.epoch,
checkpoint_to_seq = latest.checkpoint_to_seq,
checkpoint_path = %latest.path,
"multi-write checkpoint published"
);
Ok(CheckpointBuildResult::Published(latest))
}
.await;
Expand Down
18 changes: 16 additions & 2 deletions crates/ragfs/src/multibackend/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::multibackend::constants::SEGMENT_FILE_EXTENSION;
use crate::multibackend::meta::{MetadataStore, MultiWriteWorker};
use crate::multibackend::model::{is_checkpoint_directory_name, CheckpointsManifest, DirectoryEvent, LatestCheckpoint,PartitionState, PartitionsManifest, ProtocolStatus, ScopeKey, SegmentManifest,};
use crate::multibackend::provider::MultiWriteProvider;
use tracing::info;

struct ScopeSnapshot { latest: Option<LatestCheckpoint>, segments: SegmentManifest}

Expand All @@ -26,13 +27,26 @@ impl MetadataGc {

/// Collect one scope while treating concurrent account deletion as complete.
pub async fn collect_scope(&self, scope: &ScopeKey) -> Result<()> {
info!(
account = %scope.account_id,
partition = scope.partition_id,
epoch = scope.epoch,
"multi-write metadata gc started"
);
match self.collect_scope_inner(scope).await {
Err(Error::NotFound(_)) => Ok(()),
Ok(()) | Err(Error::NotFound(_)) => {
info!(
account = %scope.account_id,
partition = scope.partition_id,
epoch = scope.epoch,
"multi-write metadata gc completed"
);
Ok(())
}
Err(error) => {
self.store.record_worker_error(MultiWriteWorker::Gc);
Err(error)
}
result => result,
}
}

Expand Down
49 changes: 26 additions & 23 deletions crates/ragfs/src/multibackend/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ pub struct FlushWorker {
router: AccountRouter,
initial_partitions: u32,
backup_names: Vec<String>,
initialized_accounts: HashSet<String>,
}

impl FlushWorker {
Expand All @@ -298,6 +299,7 @@ impl FlushWorker {
primary,
initial_partitions,
backup_names,
initialized_accounts: HashSet::new(),
}
}

Expand All @@ -324,6 +326,7 @@ impl FlushWorker {
let mut accounts = HashMap::<String, Vec<PendingEvent>>::new();
for event in batch {
if matches!(event.kind, PendingEventKind::DeleteAccount) {
self.initialized_accounts.remove(&event.account_id);
accounts
.entry(SYSTEM_DIR.to_string())
.or_default()
Expand All @@ -347,35 +350,35 @@ impl FlushWorker {
}
Ok(true) => {}
}
match self
.store
.initialize_account(&account, self.initial_partitions, &self.backup_names)
.await
{
Ok(partitions) => {
if let Err(error) = bootstrap_account(
self.provider.as_ref(),
self.store.as_ref(),
&account,
&partitions,
)
if !self.initialized_accounts.contains(&account) {
match self
.store
.initialize_account(&account, self.initial_partitions, &self.backup_names)
.await
{
{
Ok(partitions) => {
if let Err(error) = bootstrap_account(
self.provider.as_ref(),
self.store.as_ref(),
&account,
&partitions,
)
.await
{
failed = true;
error!(account = %account, error = %error,
"multi-write provider bootstrap failed");
continue;
}
self.initialized_accounts.insert(account.clone());
}
Err(error) => {
failed = true;
error!(account = %account, error = %error,
"multi-write provider bootstrap failed");
"multi-write account initialization failed");
continue;
}
}
Err(error) => {
failed = true;
error!(
account = %account,
error = %error,
"multi-write account initialization failed"
);
continue;
}
}

let mut groups = HashMap::<ScopeKey, (PartitionContext, Vec<PendingEvent>)>::new();
Expand Down