mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-11 07:36:53 +00:00
feat(rio): rio_v2 is compatible with minio for storing data. (#3115)
* Set up a compatibility layer for replacing old Rio components with new ones. * fix(rio). compress range * feat(rio). Add the experimental feature rio_v2 to support minio data at the binary level. * feat(rio_v2): add sse-c test * test compression component * simple fix * fix minlz encode * fix metadata * fix kms key cache error * Update launch.json * ci: set nix crate download user agent * fix: gate obs pyroscope backend * ignore minio test * fix encrypt check * fix * fix * fix * Update object_usecase.rs * Update ci.yml * fix * ci add rio-v2 test * fix * ci fix * fix * Reconstructed into a more reasonable compatibility mode * fix * fix --------- Signed-off-by: houseme <housemecn@gmail.com> Signed-off-by: 唐小鸭 <tangtang1251@qq.com> Co-authored-by: houseme <housemecn@gmail.com> Co-authored-by: cxymds <Cxymds@qq.com> Co-authored-by: 安正超 <anzhengchao@gmail.com>
This commit is contained in:
+8
-88
@@ -14,22 +14,13 @@
|
||||
|
||||
//! Caching layer for KMS operations to improve performance
|
||||
|
||||
use crate::types::{KeyMetadata, KeySpec};
|
||||
use crate::types::KeyMetadata;
|
||||
use moka::future::Cache;
|
||||
use std::time::Duration;
|
||||
|
||||
/// Cached data key entry
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct CachedDataKey {
|
||||
pub plaintext: Vec<u8>,
|
||||
pub ciphertext: Vec<u8>,
|
||||
pub key_spec: KeySpec,
|
||||
}
|
||||
|
||||
/// KMS cache for storing frequently accessed keys and metadata
|
||||
pub struct KmsCache {
|
||||
key_metadata_cache: Cache<String, KeyMetadata>,
|
||||
data_key_cache: Cache<String, CachedDataKey>,
|
||||
}
|
||||
|
||||
impl KmsCache {
|
||||
@@ -44,13 +35,9 @@ impl KmsCache {
|
||||
pub fn new(capacity: u64) -> Self {
|
||||
Self {
|
||||
key_metadata_cache: Cache::builder()
|
||||
.max_capacity(capacity / 2)
|
||||
.max_capacity(capacity)
|
||||
.time_to_live(Duration::from_secs(300)) // 5 minutes default TTL
|
||||
.build(),
|
||||
data_key_cache: Cache::builder()
|
||||
.max_capacity(capacity / 2)
|
||||
.time_to_live(Duration::from_secs(60)) // 1 minute for data keys (shorter for security)
|
||||
.build(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,35 +64,6 @@ impl KmsCache {
|
||||
self.key_metadata_cache.run_pending_tasks().await;
|
||||
}
|
||||
|
||||
/// Get data key from cache
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `key_id` - The ID of the key to retrieve the data key for
|
||||
///
|
||||
/// # Returns
|
||||
/// An `Option` containing the `CachedDataKey` if found, or `None` if not found
|
||||
///
|
||||
pub async fn get_data_key(&self, key_id: &str) -> Option<CachedDataKey> {
|
||||
self.data_key_cache.get(key_id).await
|
||||
}
|
||||
|
||||
/// Put data key into cache
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `key_id` - The ID of the key to store the data key for
|
||||
/// * `plaintext` - The plaintext data key bytes
|
||||
/// * `ciphertext` - The ciphertext data key bytes
|
||||
///
|
||||
pub async fn put_data_key(&mut self, key_id: &str, plaintext: &[u8], ciphertext: &[u8]) {
|
||||
let cached_key = CachedDataKey {
|
||||
plaintext: plaintext.to_vec(),
|
||||
ciphertext: ciphertext.to_vec(),
|
||||
key_spec: KeySpec::Aes256, // Default to AES-256
|
||||
};
|
||||
self.data_key_cache.insert(key_id.to_string(), cached_key).await;
|
||||
self.data_key_cache.run_pending_tasks().await;
|
||||
}
|
||||
|
||||
/// Remove key metadata from cache
|
||||
///
|
||||
/// # Arguments
|
||||
@@ -115,23 +73,12 @@ impl KmsCache {
|
||||
self.key_metadata_cache.remove(key_id).await;
|
||||
}
|
||||
|
||||
/// Remove data key from cache
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `key_id` - The ID of the key to remove the data key for
|
||||
///
|
||||
pub async fn remove_data_key(&mut self, key_id: &str) {
|
||||
self.data_key_cache.remove(key_id).await;
|
||||
}
|
||||
|
||||
/// Clear all cached entries
|
||||
pub async fn clear(&mut self) {
|
||||
self.key_metadata_cache.invalidate_all();
|
||||
self.data_key_cache.invalidate_all();
|
||||
|
||||
// Wait for invalidation to complete
|
||||
self.key_metadata_cache.run_pending_tasks().await;
|
||||
self.data_key_cache.run_pending_tasks().await;
|
||||
}
|
||||
|
||||
/// Get cache statistics (hit count, miss count)
|
||||
@@ -140,13 +87,10 @@ impl KmsCache {
|
||||
/// A tuple containing total entries and total misses
|
||||
///
|
||||
pub fn stats(&self) -> (u64, u64) {
|
||||
let metadata_stats = (
|
||||
(
|
||||
self.key_metadata_cache.entry_count(),
|
||||
0u64, // moka doesn't provide miss count directly
|
||||
);
|
||||
let data_key_stats = (self.data_key_cache.entry_count(), 0u64);
|
||||
|
||||
(metadata_stats.0 + data_key_stats.0, metadata_stats.1 + data_key_stats.1)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,37 +104,30 @@ mod tests {
|
||||
#[derive(Debug, Clone)]
|
||||
struct CacheInfo {
|
||||
key_metadata_count: u64,
|
||||
data_key_count: u64,
|
||||
}
|
||||
|
||||
impl CacheInfo {
|
||||
fn total_entries(&self) -> u64 {
|
||||
self.key_metadata_count + self.data_key_count
|
||||
self.key_metadata_count
|
||||
}
|
||||
}
|
||||
|
||||
impl KmsCache {
|
||||
fn with_ttl_for_tests(capacity: u64, metadata_ttl: Duration, data_key_ttl: Duration) -> Self {
|
||||
fn with_ttl_for_tests(capacity: u64, metadata_ttl: Duration) -> Self {
|
||||
Self {
|
||||
key_metadata_cache: Cache::builder().max_capacity(capacity / 2).time_to_live(metadata_ttl).build(),
|
||||
data_key_cache: Cache::builder().max_capacity(capacity / 2).time_to_live(data_key_ttl).build(),
|
||||
key_metadata_cache: Cache::builder().max_capacity(capacity).time_to_live(metadata_ttl).build(),
|
||||
}
|
||||
}
|
||||
|
||||
fn info_for_tests(&self) -> CacheInfo {
|
||||
CacheInfo {
|
||||
key_metadata_count: self.key_metadata_cache.entry_count(),
|
||||
data_key_count: self.data_key_cache.entry_count(),
|
||||
}
|
||||
}
|
||||
|
||||
fn contains_key_metadata_for_tests(&self, key_id: &str) -> bool {
|
||||
self.key_metadata_cache.contains_key(key_id)
|
||||
}
|
||||
|
||||
fn contains_data_key_for_tests(&self, key_id: &str) -> bool {
|
||||
self.data_key_cache.contains_key(key_id)
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -216,23 +153,10 @@ mod tests {
|
||||
assert!(retrieved.is_some());
|
||||
assert_eq!(retrieved.expect("metadata should be cached").key_id, "test-key-1");
|
||||
|
||||
// Test data key caching
|
||||
let plaintext = vec![1, 2, 3, 4];
|
||||
let ciphertext = vec![5, 6, 7, 8];
|
||||
cache.put_data_key("test-key-1", &plaintext, &ciphertext).await;
|
||||
|
||||
let cached_data_key = cache.get_data_key("test-key-1").await;
|
||||
assert!(cached_data_key.is_some());
|
||||
let cached_data_key = cached_data_key.expect("data key should be cached");
|
||||
assert_eq!(cached_data_key.plaintext, plaintext);
|
||||
assert_eq!(cached_data_key.ciphertext, ciphertext);
|
||||
assert_eq!(cached_data_key.key_spec, KeySpec::Aes256);
|
||||
|
||||
// Test cache info
|
||||
let info = cache.info_for_tests();
|
||||
assert_eq!(info.key_metadata_count, 1);
|
||||
assert_eq!(info.data_key_count, 1);
|
||||
assert_eq!(info.total_entries(), 2);
|
||||
assert_eq!(info.total_entries(), 1);
|
||||
|
||||
// Test cache clearing
|
||||
cache.clear().await;
|
||||
@@ -245,7 +169,6 @@ mod tests {
|
||||
let mut cache = KmsCache::with_ttl_for_tests(
|
||||
100,
|
||||
Duration::from_millis(100), // Short TTL for testing
|
||||
Duration::from_millis(50),
|
||||
);
|
||||
|
||||
let metadata = KeyMetadata {
|
||||
@@ -277,7 +200,6 @@ mod tests {
|
||||
let mut cache = KmsCache::new(100);
|
||||
|
||||
assert!(!cache.contains_key_metadata_for_tests("nonexistent"));
|
||||
assert!(!cache.contains_data_key_for_tests("nonexistent"));
|
||||
|
||||
let metadata = KeyMetadata {
|
||||
key_id: "contains-test".to_string(),
|
||||
@@ -292,9 +214,7 @@ mod tests {
|
||||
};
|
||||
|
||||
cache.put_key_metadata("contains-test", &metadata).await;
|
||||
cache.put_data_key("contains-test", &[1, 2, 3], &[4, 5, 6]).await;
|
||||
|
||||
assert!(cache.contains_key_metadata_for_tests("contains-test"));
|
||||
assert!(cache.contains_data_key_for_tests("contains-test"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user