Skip to content

Commit 2295fe0

Browse files
authored
feat(cubestore): Introduce limit for cache_max_entry_size (#7363)
1 parent 5dbe0ab commit 2295fe0

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

rust/cubestore/cubestore/src/cachestore/cache_rocksstore.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,15 @@ impl CacheStore for RocksCacheStore {
715715
item: CacheItem,
716716
update_if_not_exists: bool,
717717
) -> Result<bool, CubeError> {
718+
if item.get_value().len() >= self.store.config.cachestore_cache_max_entry_size() {
719+
return Err(CubeError::user(format!(
720+
"Unable to SET cache with '{}' key, exceeds maximum allowed size for payload: {}, max allowed: {}",
721+
item.key,
722+
humansize::format_size(item.get_value().len(), humansize::DECIMAL),
723+
humansize::format_size(self.store.config.cachestore_cache_max_entry_size(), humansize::DECIMAL),
724+
)));
725+
}
726+
718727
self.cache_eviction_manager
719728
.before_insert(item.get_value().len() as u64)
720729
.await?;
@@ -1521,6 +1530,38 @@ mod tests {
15211530
Ok(())
15221531
}
15231532

1533+
#[tokio::test]
1534+
async fn test_cache_set_max_entry_size() -> Result<(), CubeError> {
1535+
init_test_logger().await;
1536+
1537+
let config = Config::test("cachestore_set").update_config(|mut c| {
1538+
c.cachestore_cache_max_entry_size = 1 << 20;
1539+
c
1540+
});
1541+
1542+
let (_, cachestore) =
1543+
RocksCacheStore::prepare_test_cachestore("cache_set_max_entry_size", config);
1544+
1545+
let err = cachestore
1546+
.cache_set(
1547+
CacheItem::new(
1548+
"prefix:key-with-wrong-size".to_string(),
1549+
Some(60),
1550+
"a".repeat(2 << 20),
1551+
),
1552+
false,
1553+
)
1554+
.await;
1555+
1556+
assert_eq!(err, Err(CubeError::user(
1557+
"Unable to SET cache with 'key-with-wrong-size' key, exceeds maximum allowed size for payload: 2.10 MB, max allowed: 1.05 MB".to_string()
1558+
)));
1559+
1560+
RocksCacheStore::cleanup_test_cachestore("cache_set_max_entry_size");
1561+
1562+
Ok(())
1563+
}
1564+
15241565
async fn test_cachestore_force_eviction(
15251566
name: &str,
15261567
update_config: impl FnOnce(ConfigObjImpl) -> ConfigObjImpl,

rust/cubestore/cubestore/src/config/mod.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@ pub trait ConfigObj: DIService {
442442

443443
fn cachestore_cache_max_size(&self) -> u64;
444444

445+
fn cachestore_cache_max_entry_size(&self) -> usize;
446+
445447
fn cachestore_cache_compaction_trigger_size(&self) -> u64;
446448

447449
fn cachestore_cache_max_keys(&self) -> u32;
@@ -582,6 +584,7 @@ pub struct ConfigObjImpl {
582584
pub cachestore_cache_eviction_loop_interval: u64,
583585
pub cachestore_cache_ttl_persist_loop_interval: u64,
584586
pub cachestore_cache_max_size: u64,
587+
pub cachestore_cache_max_entry_size: usize,
585588
pub cachestore_cache_compaction_trigger_size: u64,
586589
pub cachestore_cache_threshold_to_force_eviction: u8,
587590
pub cachestore_queue_results_expire: u64,
@@ -798,6 +801,10 @@ impl ConfigObj for ConfigObjImpl {
798801
self.cachestore_cache_max_size
799802
}
800803

804+
fn cachestore_cache_max_entry_size(&self) -> usize {
805+
self.cachestore_cache_max_entry_size
806+
}
807+
801808
fn cachestore_cache_compaction_trigger_size(&self) -> u64 {
802809
self.cachestore_cache_compaction_trigger_size
803810
}
@@ -1151,6 +1158,20 @@ impl Config {
11511158
Some(32 << 20),
11521159
);
11531160

1161+
let transport_max_message_size = env_parse_size(
1162+
"CUBESTORE_TRANSPORT_MAX_MESSAGE_SIZE",
1163+
64 << 20,
1164+
Some(256 << 20),
1165+
Some(16 << 20),
1166+
);
1167+
1168+
let cachestore_cache_max_entry_size = env_parse_size(
1169+
"CUBESTORE_CACHE_MAX_ENTRY_SIZE",
1170+
(64 << 20) - (1024 << 10),
1171+
Some(transport_max_message_size - (1024 << 10)),
1172+
None,
1173+
);
1174+
11541175
let cachestore_cache_compaction_trigger_size = env_parse_size(
11551176
"CUBESTORE_CACHE_COMPACTION_TRIGGER_SIZE",
11561177
Self::calculate_cache_compaction_trigger_size(cachestore_cache_max_size),
@@ -1322,6 +1343,7 @@ impl Config {
13221343
Some(0),
13231344
),
13241345
cachestore_cache_max_size: cachestore_cache_max_size as u64,
1346+
cachestore_cache_max_entry_size,
13251347
cachestore_cache_compaction_trigger_size,
13261348
cachestore_cache_threshold_to_force_eviction: 25,
13271349
cachestore_queue_results_expire: env_parse_duration(
@@ -1441,15 +1463,10 @@ impl Config {
14411463
* 1024
14421464
* 1024,
14431465
disk_space_cache_duration_secs: 300,
1444-
transport_max_message_size: env_parse_size(
1445-
"CUBESTORE_TRANSPORT_MAX_MESSAGE_SIZE",
1446-
64 << 20,
1447-
Some(256 << 20),
1448-
Some(16 << 20),
1449-
),
1466+
transport_max_message_size,
14501467
transport_max_frame_size: env_parse_size(
14511468
"CUBESTORE_TRANSPORT_MAX_FRAME_SIZE",
1452-
32 << 20,
1469+
64 << 20,
14531470
Some(256 << 20),
14541471
Some(4 << 20),
14551472
),
@@ -1535,6 +1552,7 @@ impl Config {
15351552
cachestore_cache_eviction_loop_interval: 60,
15361553
cachestore_cache_ttl_persist_loop_interval: 15,
15371554
cachestore_cache_max_size: 4096 << 20,
1555+
cachestore_cache_max_entry_size: 64 << 20,
15381556
cachestore_cache_compaction_trigger_size: 4096 * 2 << 20,
15391557
cachestore_cache_threshold_to_force_eviction: 25,
15401558
cachestore_queue_results_expire: 90,

0 commit comments

Comments
 (0)