Skip to content

Commit 675a0a0

Browse files
authored
feat(cubestore): Cache - auto trigger for full compaction (#7120)
1 parent 224b777 commit 675a0a0

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

rust/cubestore/cubestore/src/cachestore/cache_eviction_manager.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ pub struct CacheEvictionManager {
150150
// if ttl of a key is less then this value, key will be evicted
151151
// this help to delete upcoming keys for deleting
152152
eviction_min_ttl_threshold: u32,
153+
compaction_trigger_size: u64,
153154
// background listener to track events
154155
_ttl_tl_loop_join_handle: Arc<AbortingJoinHandle<()>>,
155156
}
@@ -307,6 +308,7 @@ impl CacheEvictionManager {
307308
eviction_batch_size: config.cachestore_cache_eviction_batch_size(),
308309
eviction_below_threshold: config.cachestore_cache_eviction_below_threshold(),
309310
eviction_min_ttl_threshold: config.cachestore_cache_eviction_min_ttl_threshold(),
311+
compaction_trigger_size: config.cachestore_cache_compaction_trigger_size(),
310312
//
311313
_ttl_tl_loop_join_handle: Arc::new(AbortingJoinHandle::new(join_handle)),
312314
}
@@ -506,6 +508,8 @@ impl CacheEvictionManager {
506508
} else {
507509
log::trace!("Nothing to evict");
508510

511+
self.check_compaction_trigger(&store);
512+
509513
return Ok(EvictionResult::Finished(EvictionFinishedResult {
510514
total_keys_removed: 0,
511515
total_size_removed: 0,
@@ -518,6 +522,8 @@ impl CacheEvictionManager {
518522

519523
let result = eviction_fut.await?;
520524

525+
self.check_compaction_trigger(&store);
526+
521527
log::debug!(
522528
"Eviction finished, total_keys: {}, total_size: {}",
523529
self.get_stats_total_keys(),
@@ -1094,6 +1100,28 @@ impl CacheEvictionManager {
10941100
Ok((weight, CACHE_ITEM_SIZE_WITHOUT_VALUE))
10951101
}
10961102
}
1103+
1104+
fn check_compaction_trigger(&self, store: &Arc<RocksStore>) {
1105+
let default_cf_metadata = store.db.get_column_family_metadata();
1106+
1107+
log::trace!(
1108+
"Compaction auto trigger, CF default size: {}",
1109+
humansize::format_size(default_cf_metadata.size, humansize::DECIMAL)
1110+
);
1111+
1112+
if default_cf_metadata.size > self.compaction_trigger_size {
1113+
let start: Option<&[u8]> = None;
1114+
let end: Option<&[u8]> = None;
1115+
1116+
log::debug!(
1117+
"Triggering compaction, CF default size: {} > {}",
1118+
humansize::format_size(default_cf_metadata.size, humansize::DECIMAL),
1119+
humansize::format_size(self.compaction_trigger_size, humansize::DECIMAL)
1120+
);
1121+
1122+
store.db.compact_range(start, end)
1123+
}
1124+
}
10971125
}
10981126

10991127
#[derive(Debug)]

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

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,8 @@ pub trait ConfigObj: DIService {
437437

438438
fn cachestore_cache_max_size(&self) -> u64;
439439

440+
fn cachestore_cache_compaction_trigger_size(&self) -> u64;
441+
440442
fn cachestore_cache_max_keys(&self) -> u32;
441443

442444
fn cachestore_cache_eviction_policy(&self) -> CacheEvictionPolicy;
@@ -573,6 +575,7 @@ pub struct ConfigObjImpl {
573575
pub cachestore_cache_eviction_loop_interval: u64,
574576
pub cachestore_cache_ttl_persist_loop_interval: u64,
575577
pub cachestore_cache_max_size: u64,
578+
pub cachestore_cache_compaction_trigger_size: u64,
576579
pub cachestore_cache_threshold_to_force_eviction: u8,
577580
pub cachestore_queue_results_expire: u64,
578581
pub cachestore_metrics_interval: u64,
@@ -780,6 +783,10 @@ impl ConfigObj for ConfigObjImpl {
780783
self.cachestore_cache_max_size
781784
}
782785

786+
fn cachestore_cache_compaction_trigger_size(&self) -> u64 {
787+
self.cachestore_cache_compaction_trigger_size
788+
}
789+
783790
fn cachestore_cache_threshold_to_force_eviction(&self) -> u8 {
784791
self.cachestore_cache_threshold_to_force_eviction
785792
}
@@ -1098,6 +1105,20 @@ where
10981105
}
10991106

11001107
impl Config {
1108+
fn calculate_cache_compaction_trigger_size(cache_max_size: usize) -> usize {
1109+
match cache_max_size >> 20 {
1110+
// TODO: Enable this limits after moving to separate CF for cache
1111+
// d if d < 32 => 32 * 9,
1112+
// d if d < 64 => 64 * 8,
1113+
// d if d < 128 => 128 * 7,
1114+
// d if d < 256 => 256 * 6,
1115+
d if d < 512 => 512 * 5,
1116+
d if d < 1024 => cache_max_size * 4,
1117+
d if d < 4096 => cache_max_size * 3,
1118+
_ => cache_max_size * 2,
1119+
}
1120+
}
1121+
11011122
pub fn default() -> Config {
11021123
let query_timeout = env_parse("CUBESTORE_QUERY_TIMEOUT", 120);
11031124
let query_cache_time_to_idle_secs = env_parse(
@@ -1106,6 +1127,23 @@ impl Config {
11061127
60 * 60,
11071128
);
11081129

1130+
let cachestore_cache_max_size = env_parse_size(
1131+
"CUBESTORE_CACHE_MAX_SIZE",
1132+
4096 << 20,
1133+
// 16384 mb
1134+
Some(16_384 << 20),
1135+
// 32 mb
1136+
Some(32 << 20),
1137+
);
1138+
1139+
let cachestore_cache_compaction_trigger_size = env_parse_size(
1140+
"CUBESTORE_CACHE_COMPACTION_TRIGGER_SIZE",
1141+
Self::calculate_cache_compaction_trigger_size(cachestore_cache_max_size),
1142+
None,
1143+
// 256 mb
1144+
Some(256 << 20),
1145+
) as u64;
1146+
11091147
Config {
11101148
injector: Injector::new(),
11111149
config_obj: Arc::new(ConfigObjImpl {
@@ -1256,14 +1294,8 @@ impl Config {
12561294
// 0 to disable
12571295
Some(0),
12581296
),
1259-
cachestore_cache_max_size: env_parse_size(
1260-
"CUBESTORE_CACHE_MAX_SIZE",
1261-
4096 << 20,
1262-
// 16384 mb
1263-
Some(16384 << 20),
1264-
// 32 mb
1265-
Some(32 << 20),
1266-
) as u64,
1297+
cachestore_cache_max_size: cachestore_cache_max_size as u64,
1298+
cachestore_cache_compaction_trigger_size,
12671299
cachestore_cache_threshold_to_force_eviction: 25,
12681300
cachestore_queue_results_expire: env_parse_duration(
12691301
"CUBESTORE_QUEUE_RESULTS_EXPIRE",
@@ -1474,7 +1506,8 @@ impl Config {
14741506
cachestore_gc_loop_interval: 30,
14751507
cachestore_cache_eviction_loop_interval: 60,
14761508
cachestore_cache_ttl_persist_loop_interval: 15,
1477-
cachestore_cache_max_size: 4096 << 32,
1509+
cachestore_cache_max_size: 4096 << 20,
1510+
cachestore_cache_compaction_trigger_size: 4096 * 2 << 20,
14781511
cachestore_cache_threshold_to_force_eviction: 25,
14791512
cachestore_queue_results_expire: 90,
14801513
cachestore_metrics_interval: 15,

0 commit comments

Comments
 (0)