66
77use super :: { CF_INDEXES , CF_LOGS , CfHandle , RocksDbStore , into_error} ;
88use crate :: {
9- Deserialize , IndexKey , Key , LogKey , SUBSPACE_COUNTER , SUBSPACE_IN_MEMORY_COUNTER ,
10- SUBSPACE_QUOTA ,
9+ Deserialize , IndexKey , Key , LogKey , SUBSPACE_BLOBS , SUBSPACE_COUNTER ,
10+ SUBSPACE_IN_MEMORY_COUNTER , SUBSPACE_QUOTA ,
1111 backend:: deserialize_i64_le,
1212 write:: {
1313 AssignedIds , Batch , MAX_COMMIT_ATTEMPTS , MAX_COMMIT_TIME , MergeResult , Operation ,
@@ -16,15 +16,18 @@ use crate::{
1616} ;
1717use rand:: RngExt ;
1818use rocksdb:: {
19- BoundColumnFamily , ErrorKind , IteratorMode , OptimisticTransactionDB ,
20- OptimisticTransactionOptions , WriteOptions ,
19+ BottommostLevelCompaction , BoundColumnFamily , CompactOptions , ErrorKind , IteratorMode ,
20+ OptimisticTransactionDB , OptimisticTransactionOptions , WriteOptions ,
2121} ;
2222use std:: {
2323 sync:: Arc ,
2424 thread:: sleep,
2525 time:: { Duration , Instant } ,
2626} ;
2727
28+ const BLOB_GC_MIN_GARBAGE : u64 = 256 * 1024 * 1024 ;
29+ const BLOB_GC_GARBAGE_RATIO : f64 = 0.25 ;
30+
2831impl RocksDbStore {
2932 pub ( crate ) async fn write ( & self , mut batch : Batch < ' _ > ) -> trc:: Result < AssignedIds > {
3033 let db = self . db . clone ( ) ;
@@ -114,6 +117,28 @@ impl RocksDbStore {
114117 }
115118 }
116119
120+ let cf = db. subspace_handle ( SUBSPACE_BLOBS ) ;
121+ let total = db
122+ . property_int_value_cf ( & cf, "rocksdb.live-blob-file-size" )
123+ . map_err ( into_error) ?
124+ . unwrap_or_default ( ) ;
125+ let garbage = db
126+ . property_int_value_cf ( & cf, "rocksdb.live-blob-file-garbage-size" )
127+ . map_err ( into_error) ?
128+ . unwrap_or_default ( ) ;
129+
130+ if garbage >= BLOB_GC_MIN_GARBAGE
131+ && garbage as f64 >= total as f64 * BLOB_GC_GARBAGE_RATIO
132+ {
133+ db. set_options_cf ( & cf, & [ ( "enable_blob_garbage_collection" , "true" ) ] )
134+ . map_err ( into_error) ?;
135+ let mut opts = CompactOptions :: default ( ) ;
136+ opts. set_bottommost_level_compaction ( BottommostLevelCompaction :: Force ) ;
137+ db. compact_range_cf_opt ( & cf, None :: < & [ u8 ] > , None :: < & [ u8 ] > , & opts) ;
138+ db. set_options_cf ( & cf, & [ ( "enable_blob_garbage_collection" , "false" ) ] )
139+ . map_err ( into_error) ?;
140+ }
141+
117142 Ok ( ( ) )
118143 } )
119144 . await
0 commit comments