mirror of
https://github.com/rustfs/rustfs.git
synced 2026-12-02 22:26:21 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7509bce774 |
@@ -556,6 +556,15 @@ struct ProofCache {
|
||||
window: Option<ProofWindow>,
|
||||
}
|
||||
|
||||
// An index that passed a full check is a better starting point for repair.
|
||||
// Among equally checked replicas, avoid an internode read when possible.
|
||||
fn preferred_proof_replica<'a>(sources: impl Iterator<Item = (&'a DiskStore, bool)>) -> usize {
|
||||
sources
|
||||
.enumerate()
|
||||
.min_by_key(|(_, (disk, verified))| (!verified, !disk.is_local()))
|
||||
.map_or(0, |(index, _)| index)
|
||||
}
|
||||
|
||||
// The root is checked against the complete descriptor during authentication.
|
||||
// A cache hit must carry the same geometry, part number, size and stripe count.
|
||||
type VerifiedStripeKey = (PartIntegrity, u32);
|
||||
@@ -619,16 +628,16 @@ impl PartProofReader {
|
||||
read_end: Option<u32>,
|
||||
) -> io::Result<Arc<Self>> {
|
||||
part.validate().map_err(|_| corrupt())?;
|
||||
let mut sources = Vec::with_capacity(disks.len());
|
||||
let part_number = usize::try_from(part.number).map_err(|_| corrupt())?;
|
||||
let mut sources = Vec::new();
|
||||
let mut file_name = None;
|
||||
let mut inline = None;
|
||||
for (file, disk) in files.iter().zip(disks) {
|
||||
let part_index = usize::try_from(part.number)
|
||||
.ok()
|
||||
.and_then(|number| file.parts.binary_search_by_key(&number, |p| p.number).ok());
|
||||
let part_index = file.parts.binary_search_by_key(&part_number, |p| p.number).ok();
|
||||
if part_index.and_then(|index| file.parts[index].integrity.as_ref()) != Some(&part) {
|
||||
continue;
|
||||
}
|
||||
if inline.is_none() && contains_key_str(&file.metadata, SUFFIX_INLINE_INTEGRITY) {
|
||||
if contains_key_str(&file.metadata, SUFFIX_INLINE_INTEGRITY) {
|
||||
let candidate = (|| {
|
||||
let value = get_consistent_str(&file.metadata, SUFFIX_INLINE_INTEGRITY)?;
|
||||
if value.len() > MAX_INLINE_PROOF_BYTES.div_ceil(3) * 4 {
|
||||
@@ -647,19 +656,29 @@ impl PartProofReader {
|
||||
})();
|
||||
if let Some(candidate) = candidate {
|
||||
inline = Some(candidate);
|
||||
sources = Vec::new();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if let (Some(disk), Some(directory)) = (disk, file.data_dir.filter(|id| !id.is_nil())) {
|
||||
sources.push((disk.clone(), format!("{object}/{directory}/{}", part.file_name())));
|
||||
if sources.is_empty() {
|
||||
sources.reserve_exact(files.len().min(disks.len()));
|
||||
}
|
||||
let file_name = file_name.get_or_insert_with(|| part.file_name());
|
||||
sources.push((disk.clone(), format!("{object}/{directory}/{file_name}")));
|
||||
}
|
||||
}
|
||||
let preferred = preferred_proof_replica(sources.iter().map(|(disk, _)| (disk, false)));
|
||||
Ok(Arc::new(Self {
|
||||
part,
|
||||
sources,
|
||||
volume: volume.to_owned(),
|
||||
inline,
|
||||
read_end,
|
||||
cached: Mutex::new(ProofCache::default()),
|
||||
cached: Mutex::new(ProofCache {
|
||||
preferred,
|
||||
..Default::default()
|
||||
}),
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -729,7 +748,7 @@ impl PartProofReader {
|
||||
let count = usize::try_from(end - stripe).map_err(|_| corrupt())?;
|
||||
let length = size.checked_mul(count).ok_or_else(corrupt)?;
|
||||
#[cfg(test)]
|
||||
crate::set_disk::disk_call_counters::record_proof_stream_open(path, index);
|
||||
crate::set_disk::disk_call_counters::record_proof_stream_open(path, index, index == preferred);
|
||||
let reader = disk
|
||||
.read_file_stream(&self.volume, path, offset, length)
|
||||
.await
|
||||
@@ -1044,13 +1063,21 @@ pub(crate) async fn restore_proof_replicas(
|
||||
if checks.iter().all(|valid| *valid) {
|
||||
continue;
|
||||
}
|
||||
let candidates = disks
|
||||
.iter()
|
||||
.zip(&checks)
|
||||
.filter_map(|(disk, &valid)| disk.as_ref().map(|disk| (disk, valid)));
|
||||
let preferred = preferred_proof_replica(candidates.clone());
|
||||
let proof = PartProofReader {
|
||||
part: part.clone(),
|
||||
sources: disks.iter().flatten().map(|disk| (disk.clone(), path.clone())).collect(),
|
||||
sources: candidates.map(|(disk, _)| (disk.clone(), path.clone())).collect(),
|
||||
volume: volume.to_owned(),
|
||||
inline: None,
|
||||
read_end: None,
|
||||
cached: Mutex::new(ProofCache::default()),
|
||||
cached: Mutex::new(ProofCache {
|
||||
preferred,
|
||||
..Default::default()
|
||||
}),
|
||||
};
|
||||
let prepared = PreparedIntegrity::copy_from(&proof).await.map_err(DiskError::from)?;
|
||||
let temporary = format!("integrity-{}", uuid::Uuid::new_v4());
|
||||
@@ -1387,6 +1414,54 @@ mod tests {
|
||||
assert_eq!(degraded.cached.lock().await.preferred, 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn proof_reader_prefers_local_replica_with_matching_metadata() {
|
||||
use crate::cluster::rpc::{RemoteDisk, TcpHttpInternodeDataTransport};
|
||||
use crate::disk::{Disk, DiskOption};
|
||||
use crate::layout::endpoint::Endpoint;
|
||||
|
||||
let (_dirs, store) = crate::services::rebalance::test_store_with_persisted_rebalance_meta(Default::default()).await;
|
||||
let locals = store.pools[0].disk_set[0].get_local_disks().await;
|
||||
let local = locals.into_iter().flatten().next().expect("local proof replica");
|
||||
let mut endpoint = Endpoint::try_from("http://remote-server:9000/proof").expect("fixture remote endpoint");
|
||||
endpoint.set_pool_index(0);
|
||||
endpoint.set_set_index(0);
|
||||
endpoint.set_disk_index(0);
|
||||
// The remote candidate only participates in source selection here.
|
||||
let remote = RemoteDisk::new(
|
||||
&endpoint,
|
||||
&DiskOption {
|
||||
cleanup: false,
|
||||
health_check: false,
|
||||
},
|
||||
Arc::new(TcpHttpInternodeDataTransport),
|
||||
)
|
||||
.await
|
||||
.expect("construct remote candidate");
|
||||
let disks = [Some(Arc::new(Disk::Remote(Box::new(remote)))), Some(local)];
|
||||
let part = PartIntegrity::new(IntegrityLayout::new(1, 1, 8, false).expect("layout"), 1).expect("commitment");
|
||||
let candidate = FileInfo {
|
||||
data_dir: Some(uuid::Uuid::new_v4()),
|
||||
parts: vec![rustfs_filemeta::ObjectPartInfo {
|
||||
number: 1,
|
||||
integrity: Some(part.clone()),
|
||||
..Default::default()
|
||||
}],
|
||||
..Default::default()
|
||||
};
|
||||
let matching =
|
||||
PartProofReader::new(part.clone(), &[candidate.clone(), candidate.clone()], &disks, "bucket", "object", None)
|
||||
.expect("both replicas carry the same descriptor");
|
||||
assert!(matching.sources[matching.cached.lock().await.preferred].0.is_local());
|
||||
|
||||
let mut mismatched = candidate.clone();
|
||||
mismatched.parts[0].integrity = None;
|
||||
let remote_only = PartProofReader::new(part, &[candidate, mismatched], &disks, "bucket", "object", None)
|
||||
.expect("select the matching replica");
|
||||
assert_eq!(remote_only.sources.len(), 1, "locality cannot override the selected part descriptor");
|
||||
assert!(!remote_only.sources[0].0.is_local());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn spilled_index_write_verify_and_restore_proof_replicas_round_trip() {
|
||||
let (_dirs, store) = crate::services::rebalance::test_store_with_persisted_rebalance_meta(Default::default()).await;
|
||||
@@ -1451,14 +1526,41 @@ mod tests {
|
||||
..Default::default()
|
||||
};
|
||||
fi.persist_shard_integrity().expect("persist shard integrity");
|
||||
let opens = crate::set_disk::disk_call_counters::observe(&rel_path);
|
||||
let repaired = restore_proof_replicas(&fi, &disk_slots, volume, object)
|
||||
.await
|
||||
.expect("restore proof replica 0");
|
||||
assert_eq!(repaired, 1);
|
||||
assert_eq!(
|
||||
opens.for_disk(crate::set_disk::disk_call_counters::KIND_PRIMARY_PROOF_STREAM_OPEN, 0),
|
||||
0,
|
||||
"repair must start from the fully verified proof replica"
|
||||
);
|
||||
assert!(opens.for_disk(crate::set_disk::disk_call_counters::KIND_PRIMARY_PROOF_STREAM_OPEN, 1) > 0);
|
||||
drop(opens);
|
||||
verify_index_file(&disks[0], volume, &rel_path, &part)
|
||||
.await
|
||||
.expect("repaired replica 0 must verify");
|
||||
|
||||
let original = disks[0].read_all(volume, &rel_path).await.expect("read verified proof index");
|
||||
for (slot, disk) in disks.iter().enumerate() {
|
||||
let stripe = u32::try_from(slot).expect("fixture stripe");
|
||||
let mut damaged = original.to_vec();
|
||||
damaged[part.record_offset(stripe).expect("stripe offset")] ^= 1;
|
||||
disk.write_all(volume, &rel_path, Bytes::from(damaged))
|
||||
.await
|
||||
.expect("damage different records");
|
||||
}
|
||||
assert_eq!(
|
||||
restore_proof_replicas(&fi, &disk_slots, volume, object)
|
||||
.await
|
||||
.expect("recover from complementary replicas"),
|
||||
2
|
||||
);
|
||||
for disk in &disks {
|
||||
assert_eq!(disk.read_all(volume, &rel_path).await.expect("read repaired index"), original);
|
||||
}
|
||||
|
||||
// Negative durability boundary: when all replicas are corrupted, restore_proof_replicas must fail closed.
|
||||
for disk in &disks {
|
||||
disk.write_all(volume, &rel_path, bytes::Bytes::from_static(b"all-corrupted"))
|
||||
|
||||
@@ -8384,6 +8384,7 @@ pub(crate) mod disk_call_counters {
|
||||
pub const KIND_INLINE_SHARD_OPEN: &str = "inline_shard_open";
|
||||
pub const KIND_DISK_SHARD_OPEN: &str = "disk_shard_open";
|
||||
pub const KIND_PROOF_STREAM_OPEN: &str = "proof_stream_open";
|
||||
pub const KIND_PRIMARY_PROOF_STREAM_OPEN: &str = "primary_proof_stream_open";
|
||||
/// Body verification, direct shard assembly, and erasure decoder entry points.
|
||||
pub const KIND_BODY_DECODE: &str = "body_decode";
|
||||
|
||||
@@ -8457,8 +8458,11 @@ pub(crate) mod disk_call_counters {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn record_proof_stream_open(path: &str, replica: usize) {
|
||||
pub(crate) fn record_proof_stream_open(path: &str, replica: usize, primary: bool) {
|
||||
record(path, KIND_PROOF_STREAM_OPEN, replica);
|
||||
if primary {
|
||||
record(path, KIND_PRIMARY_PROOF_STREAM_OPEN, replica);
|
||||
}
|
||||
}
|
||||
|
||||
/// RAII scope that observes call counts for a single `object`. Counts recorded
|
||||
|
||||
Reference in New Issue
Block a user