Skip to content

Commit

Permalink
chore: bring back function set_cache_capacity (#17196)
Browse files Browse the repository at this point in the history
* chore:bring back func `set_cache_capacity`

* test cases

* revert config file

* tweak test case

* fix: use usize as bytes capacity

* tweak lock scope

* rename vars/methods of cache mgr
  • Loading branch information
dantengsky authored Jan 8, 2025
1 parent dceaa71 commit fa209f4
Show file tree
Hide file tree
Showing 11 changed files with 363 additions and 69 deletions.
4 changes: 4 additions & 0 deletions src/common/cache/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ pub trait Cache<K: Eq + Hash + MemSized, V: MemSized> {

fn items_capacity(&self) -> u64;

fn set_bytes_capacity(&mut self, capacity: usize);

fn set_items_capacity(&mut self, capacity: usize);

/// Returns the bytes size of all the key-value pairs in the cache.
fn bytes_size(&self) -> u64;

Expand Down
14 changes: 14 additions & 0 deletions src/common/cache/src/cache/lru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,20 @@ impl<K: Eq + Hash + MemSized, V: MemSized> Cache<K, V> for LruCache<K, V> {
self.max_items as u64
}

fn set_bytes_capacity(&mut self, max_bytes: usize) {
while self.bytes > max_bytes || self.map.len() > self.max_items {
self.pop_by_policy();
}
self.max_bytes = max_bytes;
}

fn set_items_capacity(&mut self, max_items: usize) {
while self.bytes > self.max_bytes || self.map.len() > max_items {
self.pop_by_policy();
}
self.max_items = max_items;
}

/// Returns the bytes size of all the key-value pairs in the cache.
fn bytes_size(&self) -> u64 {
self.bytes as u64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ enum ObjectId {
}

// table functions that need `Super` privilege
const SYSTEM_TABLE_FUNCTIONS: [&str; 1] = ["fuse_amend"];
const SYSTEM_TABLE_FUNCTIONS: [&str; 2] = ["fuse_amend", "set_cache_capacity"];

impl PrivilegeAccess {
pub fn create(ctx: Arc<QueryContext>) -> Box<dyn AccessChecker> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use databend_common_storages_fuse::table_functions::FuseEncodingFunc;
use databend_common_storages_fuse::table_functions::FuseStatisticsFunc;
use databend_common_storages_fuse::table_functions::FuseTimeTravelSizeFunc;
use databend_common_storages_fuse::table_functions::FuseVacuumTemporaryTable;
use databend_common_storages_fuse::table_functions::SetCacheCapacity;
use databend_common_storages_fuse::table_functions::TableFunctionTemplate;
use databend_common_storages_stream::stream_status_table_func::StreamStatusTable;
use databend_storages_common_table_meta::table_id_ranges::SYS_TBL_FUC_ID_END;
Expand Down Expand Up @@ -138,6 +139,14 @@ impl TableFunctionFactory {
),
);

creators.insert(
"set_cache_capacity".to_string(),
(
next_id(),
Arc::new(TableFunctionTemplate::<SetCacheCapacity>::create),
),
);

creators.insert(
"fuse_segment".to_string(),
(
Expand Down
Loading

0 comments on commit fa209f4

Please sign in to comment.