1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::ffi::CString;
use crate::server::metrics::TTL_CHECKER_ACTIONS_COUNTER_VEC;
use crate::storage::raw::ttl::current_ts;
use engine_rocks::raw::{
new_compaction_filter_raw, CompactionFilter, CompactionFilterContext, CompactionFilterDecision,
CompactionFilterFactory, CompactionFilterValueType, DBCompactionFilter,
};
use engine_rocks::{RocksTtlProperties, RocksUserCollectedPropertiesNoRc};
use engine_traits::util::get_expire_ts;
pub struct TTLCompactionFilterFactory;
impl CompactionFilterFactory for TTLCompactionFilterFactory {
fn create_compaction_filter(
&self,
context: &CompactionFilterContext,
) -> *mut DBCompactionFilter {
let current = current_ts();
let mut min_expire_ts = u64::MAX;
for i in 0..context.file_numbers().len() {
let table_props = context.table_properties(i);
let user_props = unsafe {
&*(table_props.user_collected_properties() as *const _
as *const RocksUserCollectedPropertiesNoRc)
};
if let Ok(props) = RocksTtlProperties::decode(user_props) {
if props.min_expire_ts != 0 {
min_expire_ts = std::cmp::min(min_expire_ts, props.min_expire_ts);
}
}
}
if min_expire_ts > current {
return std::ptr::null_mut();
}
let name = CString::new("ttl_compaction_filter").unwrap();
let filter = Box::new(TTLCompactionFilter { ts: current });
unsafe { new_compaction_filter_raw(name, filter) }
}
}
struct TTLCompactionFilter {
ts: u64,
}
impl CompactionFilter for TTLCompactionFilter {
fn featured_filter(
&mut self,
_level: usize,
key: &[u8],
_sequence: u64,
value: &[u8],
value_type: CompactionFilterValueType,
) -> CompactionFilterDecision {
if value_type != CompactionFilterValueType::Value {
return CompactionFilterDecision::Keep;
}
if !key.starts_with(keys::DATA_PREFIX_KEY) {
return CompactionFilterDecision::Keep;
}
let expire_ts = get_expire_ts(&value).unwrap_or_else(|_| {
TTL_CHECKER_ACTIONS_COUNTER_VEC
.with_label_values(&["ts_error"])
.inc();
error!("unexpected ttl key:{:?}, value:{:?}", key, value);
0
});
if expire_ts == 0 {
return CompactionFilterDecision::Keep;
}
if expire_ts <= self.ts {
CompactionFilterDecision::Remove
} else {
CompactionFilterDecision::Keep
}
}
}
#[cfg(test)]
mod tests {
use engine_traits::util::append_expire_ts;
use crate::config::DbConfig;
use crate::storage::kv::TestEngineBuilder;
use crate::storage::raw::ttl::TEST_CURRENT_TS;
use engine_rocks::raw::CompactOptions;
use engine_rocks::util::get_cf_handle;
use engine_traits::{MiscExt, Peekable, SyncMutable, CF_DEFAULT};
#[test]
fn test_ttl_compaction_filter() {
let mut cfg = DbConfig::default();
cfg.writecf.disable_auto_compactions = true;
let dir = tempfile::TempDir::new().unwrap();
let builder = TestEngineBuilder::new().path(dir.path()).ttl(true);
let engine = builder.build_with_cfg(&cfg).unwrap();
let kvdb = engine.get_rocksdb();
let key1 = b"zkey1";
let mut value1 = vec![0; 10];
append_expire_ts(&mut value1, 10);
kvdb.put_cf(CF_DEFAULT, key1, &value1).unwrap();
kvdb.flush_cf(CF_DEFAULT, true).unwrap();
let db = kvdb.as_inner();
let handle = get_cf_handle(db, CF_DEFAULT).unwrap();
db.compact_range_cf_opt(handle, &CompactOptions::new(), None, None);
assert!(kvdb.get_value_cf(CF_DEFAULT, key1).unwrap().is_none());
let key2 = b"zkey2";
let mut value2 = vec![0; 10];
append_expire_ts(&mut value2, TEST_CURRENT_TS + 20);
kvdb.put_cf(CF_DEFAULT, key2, &value2).unwrap();
let key3 = b"zkey3";
let mut value3 = vec![0; 10];
append_expire_ts(&mut value3, 20);
kvdb.put_cf(CF_DEFAULT, key3, &value3).unwrap();
kvdb.flush_cf(CF_DEFAULT, true).unwrap();
let key4 = b"zkey4";
let mut value4 = vec![0; 10];
append_expire_ts(&mut value4, 0);
kvdb.put_cf(CF_DEFAULT, key4, &value4).unwrap();
kvdb.flush_cf(CF_DEFAULT, true).unwrap();
db.compact_range_cf_opt(handle, &CompactOptions::new(), None, None);
assert!(kvdb.get_value_cf(CF_DEFAULT, key2).unwrap().is_some());
assert!(kvdb.get_value_cf(CF_DEFAULT, key3).unwrap().is_none());
assert!(kvdb.get_value_cf(CF_DEFAULT, key4).unwrap().is_some());
}
}