From bd2a69d38bad4fd54820001f23e92a787bee1b6c Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 19 Nov 2026 13:40:04 +0000 Subject: [PATCH] Finish CI tests that hang on shutdown or reject a restored node Backup artifacts now include CLUSTER_IDENTITY so a restore reopens the same node instance instead of minting one the replicated binding rejects. Shutdown cancels an in-flight maintenance tick and bounds the auxiliary drain, so a departed voter cannot pin the process on a proposal that can no longer commit. Co-authored-by: RustFS --- crates/metadb-cluster/src/cell_manager.rs | 29 ++++++++++++------- crates/metadb-raft/src/backup.rs | 23 +++++++++++++++ crates/metadb-server/src/lib.rs | 34 +++++++++++++++++++---- 3 files changed, 70 insertions(+), 16 deletions(-) diff --git a/crates/metadb-cluster/src/cell_manager.rs b/crates/metadb-cluster/src/cell_manager.rs index fd76af3..f7d8f86 100644 --- a/crates/metadb-cluster/src/cell_manager.rs +++ b/crates/metadb-cluster/src/cell_manager.rs @@ -282,11 +282,28 @@ impl DurableCellManager { let _ = mgr.host.shutdown_all().await; return Err(error); } + if mgr.cluster_id() == 0 && mgr.replicated_membership() { + let _ = mgr.host.shutdown_all().await; + return Err(MetaDbError::IdentityRejected( + "legacy replicated cell directory has no shared CLUSTER_IDENTITY; refusing to invent a per-member cluster id" + .to_string(), + )); + } + if let Err(error) = mgr.recover_incomplete_splits().await { + let _ = mgr.host.shutdown_all().await; + return Err(error); + } + if let Err(error) = mgr.ingest_engine_bindings() { + let _ = mgr.host.shutdown_all().await; + return Err(error); + } if mgr.cluster_id() == 0 { - if mgr.replicated_membership() { + // A restored log already names this node's instance. Minting a new id + // would be rejected as a reused node identity. + if mgr.bound_instance(mgr.host.node_id()).is_some() { let _ = mgr.host.shutdown_all().await; return Err(MetaDbError::IdentityRejected( - "legacy replicated cell directory has no shared CLUSTER_IDENTITY; refusing to invent a per-member cluster id" + "CLUSTER_IDENTITY is missing but this node is already bound; refusing to mint a new instance id" .to_string(), )); } @@ -299,14 +316,6 @@ impl DurableCellManager { } } } - if let Err(error) = mgr.recover_incomplete_splits().await { - let _ = mgr.host.shutdown_all().await; - return Err(error); - } - if let Err(error) = mgr.ingest_engine_bindings() { - let _ = mgr.host.shutdown_all().await; - return Err(error); - } if let Err(error) = mgr.bind_local_instance().await { let _ = mgr.host.shutdown_all().await; return Err(error); diff --git a/crates/metadb-raft/src/backup.rs b/crates/metadb-raft/src/backup.rs index 8786bab..9253acc 100644 --- a/crates/metadb-raft/src/backup.rs +++ b/crates/metadb-raft/src/backup.rs @@ -1095,6 +1095,18 @@ fn copy_node_metadata( files, )?; } + let identity = entry.path().join("CLUSTER_IDENTITY"); + if !identity.is_file() { + return Err(backup_error(format!( + "{name}/CLUSTER_IDENTITY is missing; refusing a backup that cannot reopen this node's instance id" + ))); + } + copy_verified_frame( + &identity, + &tree.join(&name).join("CLUSTER_IDENTITY"), + &format!("{name}/CLUSTER_IDENTITY"), + files, + )?; cells += 1; } if cells == 0 { @@ -1268,6 +1280,17 @@ fn validate_manifest_tree(tree: &Path, manifest: &BackupManifest) -> Result<()> { return Err(restore_error("CELL_MANAGER_STATE is missing")); } + for file in &manifest.files { + if !(file.path.starts_with("cell_") && file.path.ends_with("/CELL_MANAGER_STATE")) { + continue; + } + let identity = file.path.replace("CELL_MANAGER_STATE", "CLUSTER_IDENTITY"); + if !listed.contains(identity.as_str()) { + return Err(restore_error(format!( + "{identity} is missing; refusing a restore that would mint a new node instance id" + ))); + } + } for shard in &manifest.shards { let prefix = format!("be/shards/{:020}", shard.shard_id); let raft = tree diff --git a/crates/metadb-server/src/lib.rs b/crates/metadb-server/src/lib.rs index 273d52f..834d5a2 100644 --- a/crates/metadb-server/src/lib.rs +++ b/crates/metadb-server/src/lib.rs @@ -705,11 +705,22 @@ impl PersistentMetaDbCluster { let bg_handle = tokio::spawn(async move { loop { tokio::select! { + biased; _ = bg_rx.changed() => break, - _ = tokio::time::sleep(Duration::from_millis(250)) => { - let Some(c) = bg_cluster.upgrade() else { break; }; - let _ = c.run_maintenance_tick_inner(false).await; - } + _ = tokio::time::sleep(Duration::from_millis(250)) => {} + } + if *bg_rx.borrow() { + break; + } + let Some(c) = bg_cluster.upgrade() else { + break; + }; + // Drop an in-flight tick when shutdown is signaled. The tick proposes + // through Raft, and a missing voter can leave that proposal unable to commit. + tokio::select! { + biased; + _ = bg_rx.changed() => break, + _ = c.run_maintenance_tick_inner(false) => {} } } }); @@ -886,10 +897,21 @@ impl PersistentMetaDbCluster { self.metrics_addr.get().map(String::as_str) } - /// Gracefully stops gRPC/background loops, flushes all shard state to disk, and stops OpenRaft groups. + /// Stops gRPC and maintenance, drains auxiliary work, flushes memtables, and stops OpenRaft. + /// + /// The drain proposes through Raft. After a peer is gone that proposal cannot commit, + /// so a stuck drain cannot pin process exit. Committed entries that are not in the + /// memtable flush stay in the Raft log and replay on the next open. pub async fn shutdown(&self) -> Result<()> { self.stop_tasks().await; - self.flush_all().await?; + match tokio::time::timeout(Duration::from_secs(10), self.flush_all()).await { + Ok(result) => result?, + Err(_) => { + for group in self.host.all_groups() { + group.state_machine().flush_to_lsm()?; + } + } + } let result = self.host.shutdown_all().await; backup::unregister_runtime(&self.config.node.data_dir); result