Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
feat(multiwrite): 多写 IOPS 优化 (#6646)
baojun-zhangf9bc07fad1b3
diff --git a/crates/ragfs/src/multibackend/checkpoint.rs b/crates/ragfs/src/multibackend/checkpoint.rsindex 85f0987b3..48f72db77 100644--- a/crates/ragfs/src/multibackend/checkpoint.rs+++ b/crates/ragfs/src/multibackend/checkpoint.rs@@ -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};@@ -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 {@@ -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? {@@ -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.@@ -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;diff --git a/crates/ragfs/src/multibackend/gc.rs b/crates/ragfs/src/multibackend/gc.rsindex 64507ca22..d42b27e54 100644--- a/crates/ragfs/src/multibackend/gc.rs+++ b/crates/ragfs/src/multibackend/gc.rs@@ -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}@@ -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,}}diff --git a/crates/ragfs/src/multibackend/runtime.rs b/crates/ragfs/src/multibackend/runtime.rsindex c9f2e16b8..4a5c285b5 100644--- a/crates/ragfs/src/multibackend/runtime.rs+++ b/crates/ragfs/src/multibackend/runtime.rs@@ -280,6 +280,7 @@ pub struct FlushWorker {router: AccountRouter,initial_partitions: u32,backup_names: Vec<String>,+ initialized_accounts: HashSet<String>,}impl FlushWorker {@@ -298,6 +299,7 @@ impl FlushWorker {primary,initial_partitions,backup_names,+ initialized_accounts: HashSet::new(),}}@@ -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()@@ -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();