-
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathgc.rs
More file actions
307 lines (266 loc) · 8.99 KB
/
Copy pathgc.rs
File metadata and controls
307 lines (266 loc) · 8.99 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Copyright (c) 2024-present, fjall-rs
// This source code is licensed under both the Apache 2.0 and MIT License
// (found in the LICENSE-* files in the repository)
use crate::{
blob_tree::handle::BlobIndirection, coding::Decode, compaction::stream::ExpiredKvCallback,
fs::FileSystem, version::BlobFileList, vlog::BlobFileId,
};
/// Tracks fragmentation information in a blob file
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct FragmentationEntry {
/// Number of unreferenced (garbage) blobs
pub(crate) len: usize,
/// Unreferenced (garbage) blob bytes that could be freed (compressed)
pub(crate) bytes: u64,
/// Unreferenced (garbage) blob bytes that could be freed from disk
pub(crate) on_disk_bytes: u64,
}
impl FragmentationEntry {
#[must_use]
pub fn new(len: usize, bytes: u64, on_disk_bytes: u64) -> Self {
Self {
len,
bytes,
on_disk_bytes,
}
}
}
/// Tracks fragmentation information in a value log (list of blob files)
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct FragmentationMap(crate::HashMap<BlobFileId, FragmentationEntry>);
impl std::ops::Deref for FragmentationMap {
type Target = crate::HashMap<BlobFileId, FragmentationEntry>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::ops::DerefMut for FragmentationMap {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl FragmentationMap {
/// Returns the number of bytes that could be freed from disk.
#[must_use]
pub fn stale_bytes(&self) -> u64 {
self.0.values().map(|x| x.on_disk_bytes).sum()
}
/// Removes blob file entries that are not part of the value log (anymore)
/// to reduce linear memory growth.
pub fn prune<F: FileSystem>(&mut self, value_log: &BlobFileList<F>) {
self.0.retain(|&k, _| value_log.contains_key(k));
}
/// Merges a fragmentation map into another.
///
/// This is used after a compaction stream is summed up (using the expiration callback), to apply
/// the diff to the tree's fragmentation stats.
pub fn merge_into(self, other: &mut Self) {
for (blob_file_id, diff) in self.0 {
other
.0
.entry(blob_file_id)
.and_modify(|counter| {
counter.bytes += diff.bytes;
counter.len += diff.len;
counter.on_disk_bytes += diff.on_disk_bytes;
})
.or_insert(diff);
}
}
}
impl crate::coding::Encode for FragmentationMap {
fn encode_into<W: std::io::Write>(&self, writer: &mut W) -> Result<(), crate::Error> {
use byteorder::{WriteBytesExt, LE};
#[expect(
clippy::cast_possible_truncation,
reason = "there are always less than 4 billion blob files"
)]
writer.write_u32::<LE>(self.len() as u32)?;
for (blob_file_id, item) in self.iter() {
writer.write_u64::<LE>(*blob_file_id)?;
#[expect(
clippy::cast_possible_truncation,
reason = "there are always less than 4 billion blobs in a blob file"
)]
writer.write_u32::<LE>(item.len as u32)?;
writer.write_u64::<LE>(item.bytes)?;
writer.write_u64::<LE>(item.on_disk_bytes)?;
}
Ok(())
}
}
impl crate::coding::Decode for FragmentationMap {
fn decode_from<R: std::io::Read>(reader: &mut R) -> Result<Self, crate::Error>
where
Self: Sized,
{
use byteorder::{ReadBytesExt, LE};
let len = reader.read_u32::<LE>()?;
let mut map =
crate::HashMap::with_capacity_and_hasher(len as usize, rustc_hash::FxBuildHasher);
for _ in 0..len {
let id = reader.read_u64::<LE>()?;
let len = reader.read_u32::<LE>()? as usize;
let bytes = reader.read_u64::<LE>()?;
let on_disk_bytes = reader.read_u64::<LE>()?;
map.insert(id, FragmentationEntry::new(len, bytes, on_disk_bytes));
}
Ok(Self(map))
}
}
impl ExpiredKvCallback for FragmentationMap {
fn on_expired(&mut self, kv: &crate::InternalValue) {
if kv.key.value_type.is_indirection() {
let mut reader = &kv.value[..];
#[expect(
clippy::expect_used,
reason = "data is read and checked for corruption, so we expect to be able to deserialize BlobIndirection fine"
)]
let vptr =
BlobIndirection::decode_from(&mut reader).expect("should parse BlobIndirection");
let size = u64::from(vptr.size);
let on_disk_size = u64::from(vptr.vhandle.on_disk_size);
self.0
.entry(vptr.vhandle.blob_file_id)
.and_modify(|counter| {
counter.len += 1;
counter.bytes += size;
counter.on_disk_bytes += on_disk_size;
})
.or_insert_with(|| FragmentationEntry {
bytes: size,
on_disk_bytes: on_disk_size,
len: 1,
});
}
}
}
#[cfg(test)]
#[expect(clippy::expect_used, clippy::indexing_slicing)]
mod tests {
use super::*;
use crate::{
coding::{Decode, Encode},
compaction::stream::CompactionStream,
value::InternalValue,
vlog::ValueHandle,
ValueType,
};
use std::collections::HashMap;
use test_log::test;
#[test]
fn frag_map_merge_into() {
let mut map = FragmentationMap(HashMap::default());
map.0.insert(
0,
FragmentationEntry {
len: 1,
bytes: 1_000,
on_disk_bytes: 500,
},
);
map.0.insert(
1,
FragmentationEntry {
len: 2,
bytes: 2_000,
on_disk_bytes: 1_000,
},
);
// test merge_into
let mut diff = FragmentationMap(HashMap::default());
diff.0.insert(
0,
FragmentationEntry {
len: 3,
bytes: 3_000,
on_disk_bytes: 1_500,
},
);
diff.0.insert(
3,
FragmentationEntry {
len: 4,
bytes: 4_000,
on_disk_bytes: 2_000,
},
);
diff.merge_into(&mut map);
assert_eq!(map.0.len(), 3);
assert_eq!(map.0[&0].len, 4);
assert_eq!(map.0[&0].bytes, 4_000);
assert_eq!(map.0[&0].on_disk_bytes, 2_000);
assert_eq!(map.0[&1].len, 2);
assert_eq!(map.0[&1].bytes, 2_000);
assert_eq!(map.0[&1].on_disk_bytes, 1_000);
assert_eq!(map.0[&3].len, 4);
assert_eq!(map.0[&3].bytes, 4_000);
assert_eq!(map.0[&3].on_disk_bytes, 2_000);
}
#[test]
fn frag_map_roundtrip() {
let map = FragmentationMap({
let mut map = HashMap::default();
map.insert(
0,
FragmentationEntry {
len: 1,
bytes: 1_000,
on_disk_bytes: 500,
},
);
map.insert(
1,
FragmentationEntry {
len: 2,
bytes: 2_000,
on_disk_bytes: 1_000,
},
);
map
});
let encoded = map.encode_into_vec();
let decoded = FragmentationMap::decode_from(&mut &encoded[..]).expect("should decode map");
assert_eq!(map, decoded);
}
#[test]
#[expect(clippy::unwrap_used)]
fn compaction_stream_gc_count_drops() -> crate::Result<()> {
#[rustfmt::skip]
let vec = &[
InternalValue::from_components("a", b"abc", 1, ValueType::Value),
InternalValue::from_components("a", BlobIndirection {
size: 1000,
vhandle: ValueHandle {
blob_file_id: 0,
on_disk_size: 500,
offset: 0,
}
}.encode_into_vec(), 0, ValueType::Indirection),
];
let mut my_watcher = FragmentationMap::default();
let iter = vec.iter().cloned().map(Ok);
let mut iter = CompactionStream::new(iter, 1_000).with_expiration_callback(&mut my_watcher);
assert_eq!(
// TODO: Seqno is normally reset to 0
InternalValue::from_components(*b"a", b"abc", 1, ValueType::Value),
iter.next().unwrap()?,
);
assert_eq!(
{
let mut map = HashMap::default();
map.insert(
0,
FragmentationEntry {
len: 1,
bytes: 1_000,
on_disk_bytes: 500,
},
);
map
},
my_watcher.0,
);
Ok(())
}
}