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
19 changes: 19 additions & 0 deletions crates/metadb-cluster/src/cell_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,25 @@ impl DurableCellManager {
Ok(())
}

/// Routes for cluster status. A local shard that cannot rebuild its snapshot
/// is not returned as a live voter; the caller gets an error instead.
pub fn routes_for_live_status(&self) -> Result<Vec<CompositeShardRoute>> {
let routes = self.list_routes();
for route in &routes {
if let Some(group) = self.host.get_group(route.shard_id) {
// Probe the snapshot directory. The in-memory latch is still
// empty until something attempts repair.
group.ensure_published_snapshot_sync().map_err(|error| {
MetaDbError::StorageIoError(format!(
"shard {} storage is unrecoverable, so it is not a live voter: {error}",
route.shard_id
))
})?;
}
}
Ok(routes)
}

pub fn list_routes(&self) -> Vec<CompositeShardRoute> {
let mut routes: Vec<CompositeShardRoute> = {
let guard = self.state.lock().unwrap();
Expand Down
62 changes: 62 additions & 0 deletions crates/metadb-raft/src/group_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::time::Duration;

use openraft::async_runtime::watch::WatchReceiver;
use openraft::errors::RaftError;
use openraft::storage::RaftStateMachine;
use openraft::{BasicNode, Config};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -241,6 +242,12 @@ impl ShardRaftGroup {
let _ = raft.wait_for_recovery(Some(Duration::from_secs(5))).await;
}

if let Some(reason) = state_machine.storage_unrecoverable() {
return Err(MetaDbError::StorageIoError(format!(
"snapshot unrecoverable: {reason}"
)));
}

router.register_group(shard_id, raft.clone());
let read_batcher = Arc::new(ReadIndexBatcher::new(shard_id, raft.clone(), frontier_rx));

Expand Down Expand Up @@ -389,6 +396,57 @@ impl ShardRaftGroup {
&self.state_machine
}

/// `Some` when this process must not be reported as a live voter.
pub fn storage_unrecoverable(&self) -> Option<String> {
self.state_machine.storage_unrecoverable()
}

/// Repair a missing published snapshot before cluster status returns.
///
/// Status must not trust the in-memory latch alone. The files can disappear
/// before any `get_current_snapshot` call.
pub fn ensure_published_snapshot_sync(&self) -> Result<()> {
if let Some(reason) = self.storage_unrecoverable() {
return Err(MetaDbError::StorageIoError(format!(
"snapshot unrecoverable: {reason}"
)));
}
if !self.state_machine.published_snapshot_missing() {
return Ok(());
}
self.state_machine
.ensure_durable_snapshot()
.map_err(|error| MetaDbError::StorageIoError(error.to_string()))?;
if let Some(reason) = self.storage_unrecoverable() {
return Err(MetaDbError::StorageIoError(format!(
"snapshot unrecoverable: {reason}"
)));
}
Ok(())
}

/// Rewrite a published snapshot that disappeared, or refuse to serve.
///
/// A missing in-use snapshot used to stop RaftCore while cluster status
/// still answered from the cached voter set. Repair from the LSM when it
/// can cover the purged log. Otherwise leave an explicit unrecoverable
/// error so status and later reads do not pretend the voter is healthy.
pub async fn repair_published_snapshot(&self) -> Result<()> {
if let Some(reason) = self.storage_unrecoverable() {
return Err(MetaDbError::StorageIoError(format!(
"snapshot unrecoverable: {reason}"
)));
}
if !self.state_machine.published_snapshot_missing() {
return Ok(());
}
let mut state_machine = self.state_machine.clone();
match state_machine.get_current_snapshot().await {
Ok(_) => Ok(()),
Err(error) => Err(MetaDbError::StorageIoError(error.to_string())),
}
}

pub fn engine(&self) -> Arc<LifecycleStorageEngine> {
self.state_machine.engine()
}
Expand Down Expand Up @@ -647,6 +705,8 @@ impl ShardRaftGroup {
caller_hlc: HlcTimestamp,
payload: Vec<u8>,
) -> Result<MetaDbRaftResponse> {
// Forwarded ClientWrite RPCs enter here and skip `propose_until`.
self.repair_published_snapshot().await?;
let local_epoch = self.current_epoch();
if expected_epoch != local_epoch {
return Err(MetaDbError::EpochMismatch {
Expand Down Expand Up @@ -750,6 +810,7 @@ impl ShardRaftGroup {
&self,
expected_epoch: ShardEpoch,
) -> Result<(ReadToken, SnapshotGuard)> {
self.repair_published_snapshot().await?;
if self
.testing_read_quorum
.load(std::sync::atomic::Ordering::Relaxed)
Expand Down Expand Up @@ -778,6 +839,7 @@ impl ShardRaftGroup {
expected_epoch: ShardEpoch,
barrier_hlc: HlcTimestamp,
) -> Result<(ReadToken, SnapshotGuard)> {
self.repair_published_snapshot().await?;
let _ = self.clock.update_checked(barrier_hlc)?;
let token = self.read_batcher.linearizable_token(expected_epoch).await?;
let snap = self.state_machine.acquire_snapshot(
Expand Down
Loading
Loading