mirror of
https://github.com/rustfs/rustfs.git
synced 2026-11-30 20:57:35 +00:00
perf(scanner): stream checkpoint body comparisons
Share an exact bounded comparison for ambiguous writes and reused backup pages. Stop at the first mismatch and require EOF before using a read-back to confirm a committed revision. Co-Authored-By: heihutu <heihutu@gmail.com> Co-Authored-By: zhi22915 <qiuzgang@gmail.com> Co-Authored-By: qunjiazhu <qunjiazhu@gmail.com>
This commit is contained in:
@@ -18,8 +18,26 @@ mod scan_load;
|
||||
use super::*;
|
||||
use bytes::Bytes;
|
||||
use std::sync::Arc;
|
||||
use tokio::io::{AsyncRead, AsyncReadExt};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
/// Compare the stored body exactly without allocating a second copy of the cache.
|
||||
pub(super) async fn cache_body_matches(reader: &mut (impl AsyncRead + Unpin), mut expected: &[u8]) -> std::io::Result<bool> {
|
||||
let mut buffer = vec![0; expected.len().clamp(1, 16 * 1024)];
|
||||
loop {
|
||||
// When the expected body ends, read at most one extra byte to verify EOF.
|
||||
let limit = buffer.len().min(expected.len().saturating_add(1));
|
||||
let read = reader.read(&mut buffer[..limit]).await?;
|
||||
if read == 0 {
|
||||
return Ok(expected.is_empty());
|
||||
}
|
||||
let Some(remaining) = expected.strip_prefix(&buffer[..read]) else {
|
||||
return Ok(false);
|
||||
};
|
||||
expected = remaining;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct CachePublicationAdmissionUnavailable;
|
||||
|
||||
@@ -695,7 +713,7 @@ impl DataUsageCache {
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
let matches = reader.read_all().await?.as_slice() == buf.as_ref();
|
||||
let matches = cache_body_matches(&mut reader, buf).await?;
|
||||
Ok::<_, StorageError>((matches, DataUsageCacheRevision::from_etag(reader.object_info.etag)))
|
||||
})
|
||||
.await;
|
||||
|
||||
@@ -66,12 +66,8 @@ async fn backup_revision<S: ScannerObjectIO>(
|
||||
Err(StorageError::FileNotFound | StorageError::ObjectNotFound(..)) => return Ok(Some(DataUsageCacheRevision::Missing)),
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
if usize::try_from(reader.object_info.size).ok() == Some(expected.len()) {
|
||||
let mut bytes = Vec::with_capacity(expected.len());
|
||||
(&mut reader).take(PAGE_MAX as u64 + 1).read_to_end(&mut bytes).await?;
|
||||
if bytes == expected.as_ref() {
|
||||
return Ok(None);
|
||||
}
|
||||
if usize::try_from(reader.object_info.size).ok() == Some(expected.len()) && cache_body_matches(&mut reader, expected).await? {
|
||||
return Ok(None);
|
||||
}
|
||||
let etag = reader
|
||||
.object_info
|
||||
|
||||
@@ -226,6 +226,50 @@ impl AsyncRead for PrefixThenErrorReader {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn cache_body_comparison_requires_exact_bytes_and_verified_eof() {
|
||||
use super::persistence::cache_body_matches;
|
||||
|
||||
let payload = vec![b'x'; 128 * 1024];
|
||||
for (actual, expected, matches) in [
|
||||
(payload.as_slice(), payload.as_slice(), true),
|
||||
(&payload[..payload.len() - 1], payload.as_slice(), false),
|
||||
(payload.as_slice(), &payload[..payload.len() - 1], false),
|
||||
(b"".as_slice(), b"".as_slice(), true),
|
||||
(b"x".as_slice(), b"".as_slice(), false),
|
||||
] {
|
||||
assert_eq!(
|
||||
cache_body_matches(&mut Cursor::new(actual), expected)
|
||||
.await
|
||||
.expect("compare cache body"),
|
||||
matches,
|
||||
"stored length {}, expected length {}",
|
||||
actual.len(),
|
||||
expected.len()
|
||||
);
|
||||
}
|
||||
|
||||
let mut reader = PrefixThenErrorReader {
|
||||
prefix: Cursor::new(vec![b'y']),
|
||||
failed: false,
|
||||
};
|
||||
assert!(
|
||||
!cache_body_matches(&mut reader, &payload)
|
||||
.await
|
||||
.expect("an already divergent prefix needs no further reads")
|
||||
);
|
||||
assert!(!reader.failed, "the comparison must stop before polling the unread suffix");
|
||||
|
||||
let mut reader = PrefixThenErrorReader {
|
||||
prefix: Cursor::new(payload.clone()),
|
||||
failed: false,
|
||||
};
|
||||
assert!(
|
||||
cache_body_matches(&mut reader, &payload).await.is_err(),
|
||||
"matching bytes cannot establish equality until EOF succeeds"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct CacheReadStore {
|
||||
main: CacheReadBody,
|
||||
|
||||
Reference in New Issue
Block a user