forked from apache/horaedb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile.rs
633 lines (531 loc) · 16.1 KB
/
file.rs
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
// Copyright 2023 The CeresDB Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Sst file and storage info
use std::{
borrow::Borrow,
collections::{BTreeMap, HashSet},
fmt,
fmt::Debug,
hash::{Hash, Hasher},
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
time::Duration,
};
use common_types::{
time::{TimeRange, Timestamp},
SequenceNumber,
};
use future_ext::{retry_async, RetryConfig};
use log::{error, info, warn};
use macros::define_result;
use metric_ext::Meter;
use object_store::{ObjectStoreRef, Path};
use runtime::{JoinHandle, Runtime};
use snafu::{ResultExt, Snafu};
use table_engine::table::TableId;
use tokio::sync::{
mpsc::{self, UnboundedReceiver, UnboundedSender},
Mutex,
};
use crate::{space::SpaceId, sst::manager::FileId, table::sst_util, table_options::StorageFormat};
/// Error of sst file.
#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("Failed to join purger, err:{}", source))]
StopPurger { source: runtime::Error },
}
define_result!(Error);
pub const SST_LEVEL_NUM: usize = 2;
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct Level(u16);
impl Level {
// Currently there are only two levels: 0, 1.
pub const MAX: Self = Self(1);
pub const MIN: Self = Self(0);
pub fn next(&self) -> Self {
Self::MAX.0.min(self.0 + 1).into()
}
pub fn is_min(&self) -> bool {
self == &Self::MIN
}
pub fn as_usize(&self) -> usize {
self.0 as usize
}
pub fn as_u32(&self) -> u32 {
self.0 as u32
}
pub fn as_u16(&self) -> u16 {
self.0
}
}
impl From<u16> for Level {
fn from(value: u16) -> Self {
Self(value)
}
}
impl fmt::Display for Level {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
// TODO(yingwen): Order or split file by time range to speed up filter (even in
// level 0).
/// Manage files of single level
pub struct LevelHandler {
pub level: Level,
/// All files in current level.
files: FileHandleSet,
}
impl LevelHandler {
pub fn new(level: Level) -> Self {
Self {
level,
files: FileHandleSet::default(),
}
}
#[inline]
pub fn insert(&mut self, file: FileHandle) {
self.files.insert(file);
}
pub fn latest_sst(&self) -> Option<FileHandle> {
self.files.latest()
}
pub fn pick_ssts(&self, time_range: TimeRange) -> Vec<FileHandle> {
self.files.files_by_time_range(time_range)
}
#[inline]
pub fn remove_ssts(&mut self, file_ids: &[FileId]) {
self.files.remove_by_ids(file_ids);
}
pub fn iter_ssts(&self) -> Iter {
let iter = self.files.file_map.values();
Iter(iter)
}
#[inline]
pub fn collect_expired(
&self,
expire_time: Option<Timestamp>,
expired_files: &mut Vec<FileHandle>,
) {
self.files.collect_expired(expire_time, expired_files);
}
#[inline]
pub fn has_expired_sst(&self, expire_time: Option<Timestamp>) -> bool {
self.files.has_expired_sst(expire_time)
}
}
pub struct Iter<'a>(std::collections::btree_map::Values<'a, FileOrdKey, FileHandle>);
impl<'a> Iterator for Iter<'a> {
type Item = &'a FileHandle;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
#[derive(Clone)]
pub struct FileHandle {
inner: Arc<FileHandleInner>,
}
impl PartialEq for FileHandle {
fn eq(&self, other: &Self) -> bool {
self.id() == other.id()
}
}
impl Eq for FileHandle {}
impl Hash for FileHandle {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id().hash(state);
}
}
impl FileHandle {
pub fn new(meta: FileMeta, purge_queue: FilePurgeQueue) -> Self {
Self {
inner: Arc::new(FileHandleInner {
meta,
purge_queue,
being_compacted: AtomicBool::new(false),
metrics: SstMetrics::default(),
}),
}
}
#[inline]
pub fn read_meter(&self) -> Arc<Meter> {
self.inner.metrics.read_meter.clone()
}
#[inline]
pub fn row_num(&self) -> u64 {
self.inner.meta.row_num
}
#[inline]
pub fn id(&self) -> FileId {
self.inner.meta.id
}
#[inline]
pub fn id_ref(&self) -> &FileId {
&self.inner.meta.id
}
#[inline]
pub fn intersect_with_time_range(&self, time_range: TimeRange) -> bool {
self.inner.meta.intersect_with_time_range(time_range)
}
#[inline]
pub fn time_range(&self) -> TimeRange {
self.inner.meta.time_range
}
#[inline]
pub fn time_range_ref(&self) -> &TimeRange {
&self.inner.meta.time_range
}
#[inline]
pub fn max_sequence(&self) -> SequenceNumber {
self.inner.meta.max_seq
}
#[inline]
pub fn being_compacted(&self) -> bool {
self.inner.being_compacted.load(Ordering::Relaxed)
}
#[inline]
pub fn size(&self) -> u64 {
self.inner.meta.size
}
#[inline]
pub fn set_being_compacted(&self, value: bool) {
self.inner.being_compacted.store(value, Ordering::Relaxed);
}
#[inline]
pub fn storage_format(&self) -> StorageFormat {
self.inner.meta.storage_format
}
#[inline]
pub fn meta(&self) -> FileMeta {
self.inner.meta.clone()
}
}
impl fmt::Debug for FileHandle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FileHandle")
.field("meta", &self.inner.meta)
.field("being_compacted", &self.being_compacted())
.finish()
}
}
struct SstMetrics {
pub read_meter: Arc<Meter>,
pub key_num: usize,
}
impl Default for SstMetrics {
fn default() -> Self {
SstMetrics {
read_meter: Arc::new(Meter::new()),
key_num: 0,
}
}
}
impl fmt::Debug for SstMetrics {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SstMetrics")
.field("read_meter", &self.read_meter.h2_rate())
.field("key_num", &self.key_num)
.finish()
}
}
struct FileHandleInner {
meta: FileMeta,
purge_queue: FilePurgeQueue,
/// The file is being compacting.
being_compacted: AtomicBool,
metrics: SstMetrics,
}
impl Drop for FileHandleInner {
fn drop(&mut self) {
info!("FileHandle is dropped, meta:{:?}", self.meta);
// Push file cannot block or be async because we are in drop().
self.purge_queue.push_file(&self.meta);
}
}
/// Used to order [FileHandle] by (end_time, start_time, file_id)
#[derive(PartialEq, Eq, PartialOrd, Ord)]
struct FileOrdKey {
exclusive_end: Timestamp,
inclusive_start: Timestamp,
file_id: FileId,
}
impl FileOrdKey {
fn for_seek(exclusive_end: Timestamp) -> Self {
Self {
exclusive_end,
inclusive_start: Timestamp::MIN,
file_id: 0,
}
}
fn key_of(file: &FileHandle) -> Self {
Self {
exclusive_end: file.time_range().exclusive_end(),
inclusive_start: file.time_range().inclusive_start(),
file_id: file.id(),
}
}
}
/// Used to index [FileHandle] by file_id
struct FileHandleHash(FileHandle);
impl PartialEq for FileHandleHash {
fn eq(&self, other: &Self) -> bool {
self.0.id() == other.0.id()
}
}
impl Eq for FileHandleHash {}
impl Hash for FileHandleHash {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.id().hash(state);
}
}
impl Borrow<FileId> for FileHandleHash {
#[inline]
fn borrow(&self) -> &FileId {
self.0.id_ref()
}
}
#[derive(Default)]
struct FileHandleSet {
/// Files ordered by time range and id.
file_map: BTreeMap<FileOrdKey, FileHandle>,
/// Files indexed by file id, used to speed up removal.
id_to_files: HashSet<FileHandleHash>,
}
impl FileHandleSet {
fn latest(&self) -> Option<FileHandle> {
if let Some(file) = self.file_map.values().rev().next() {
return Some(file.clone());
}
None
}
fn files_by_time_range(&self, time_range: TimeRange) -> Vec<FileHandle> {
// Seek to first sst whose end time >= time_range.inclusive_start().
let seek_key = FileOrdKey::for_seek(time_range.inclusive_start());
self.file_map
.range(seek_key..)
.filter_map(|(_key, file)| {
if file.intersect_with_time_range(time_range) {
Some(file.clone())
} else {
None
}
})
.collect()
}
fn insert(&mut self, file: FileHandle) {
self.file_map
.insert(FileOrdKey::key_of(&file), file.clone());
self.id_to_files.insert(FileHandleHash(file));
}
fn remove_by_ids(&mut self, file_ids: &[FileId]) {
for file_id in file_ids {
if let Some(file) = self.id_to_files.take(file_id) {
let key = FileOrdKey::key_of(&file.0);
self.file_map.remove(&key);
}
}
}
/// Collect ssts with time range is expired.
fn collect_expired(&self, expire_time: Option<Timestamp>, expired_files: &mut Vec<FileHandle>) {
for file in self.file_map.values() {
if file.time_range().is_expired(expire_time) {
expired_files.push(file.clone());
} else {
// Files are sorted by end time first, so there is no more file whose end time
// is less than `expire_time`.
break;
}
}
}
fn has_expired_sst(&self, expire_time: Option<Timestamp>) -> bool {
// Files are sorted by end time first, so check first file is enough.
if let Some(file) = self.file_map.values().next() {
return file.time_range().is_expired(expire_time);
}
false
}
}
/// Meta of a sst file, immutable once created
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileMeta {
/// Id of the sst file
pub id: FileId,
/// File size in bytes
pub size: u64,
/// Total row number
pub row_num: u64,
/// The time range of the file.
pub time_range: TimeRange,
/// The max sequence number of the file.
pub max_seq: u64,
/// The format of the file.
pub storage_format: StorageFormat,
/// Associated files, such as: meta_path
pub associated_files: Vec<String>,
}
impl FileMeta {
pub fn intersect_with_time_range(&self, time_range: TimeRange) -> bool {
self.time_range.intersect_with(time_range)
}
}
// Queue to store files to be deleted for a table.
#[derive(Clone)]
pub struct FilePurgeQueue {
// Wrap a inner struct to avoid storing space/table ids for each file.
inner: Arc<FilePurgeQueueInner>,
}
impl FilePurgeQueue {
pub fn new(space_id: SpaceId, table_id: TableId, sender: UnboundedSender<Request>) -> Self {
Self {
inner: Arc::new(FilePurgeQueueInner {
space_id,
table_id,
sender,
closed: AtomicBool::new(false),
}),
}
}
/// Close the purge queue, then all request pushed to this queue will be
/// ignored. This is mainly used to avoid files being deleted after the
/// db is closed.
pub fn close(&self) {
self.inner.closed.store(true, Ordering::SeqCst);
}
fn push_file(&self, file_meta: &FileMeta) {
if self.inner.closed.load(Ordering::SeqCst) {
warn!("Purger closed, ignore file_id:{}", file_meta.id);
return;
}
// Send the file id via a channel to file purger and delete the file from sst
// store in background.
let request = FilePurgeRequest {
space_id: self.inner.space_id,
table_id: self.inner.table_id,
file_id: file_meta.id,
associated_files: file_meta.associated_files.clone(),
};
if let Err(send_res) = self.inner.sender.send(Request::Purge(request)) {
error!(
"Failed to send delete file request, request:{:?}",
send_res.0
);
}
}
}
struct FilePurgeQueueInner {
space_id: SpaceId,
table_id: TableId,
closed: AtomicBool,
sender: UnboundedSender<Request>,
}
#[derive(Debug)]
pub struct FilePurgeRequest {
space_id: SpaceId,
table_id: TableId,
file_id: FileId,
associated_files: Vec<String>,
}
#[derive(Debug)]
pub enum Request {
Purge(FilePurgeRequest),
Exit,
}
/// Background file purger.
pub struct FilePurger {
sender: UnboundedSender<Request>,
handle: Mutex<Option<JoinHandle<()>>>,
}
impl FilePurger {
const RETRY_CONFIG: RetryConfig = RetryConfig {
max_retries: 3,
interval: Duration::from_millis(500),
};
pub fn start(runtime: &Runtime, store: ObjectStoreRef) -> Self {
// We must use unbound channel, so the sender wont block when the handle is
// dropped.
let (tx, rx) = mpsc::unbounded_channel();
// Spawn a background job to purge files.
let handle = runtime.spawn(async {
Self::purge_file_loop(store, rx).await;
});
Self {
sender: tx,
handle: Mutex::new(Some(handle)),
}
}
pub async fn stop(&self) -> Result<()> {
info!("Try to stop file purger");
if self.sender.send(Request::Exit).is_err() {
error!("File purge task already exited");
}
let mut handle = self.handle.lock().await;
// Also clear the handle to avoid await a ready future.
if let Some(h) = handle.take() {
h.await.context(StopPurger)?;
}
Ok(())
}
pub fn create_purge_queue(&self, space_id: SpaceId, table_id: TableId) -> FilePurgeQueue {
FilePurgeQueue::new(space_id, table_id, self.sender.clone())
}
// TODO: currently we ignore errors when delete.
async fn delete_file(store: &ObjectStoreRef, path: &Path) {
if let Err(e) = retry_async(|| store.delete(path), &Self::RETRY_CONFIG).await {
error!("File purger failed to delete file, path:{path}, err:{e}");
}
}
async fn purge_file_loop(store: ObjectStoreRef, mut receiver: UnboundedReceiver<Request>) {
info!("File purger start");
while let Some(request) = receiver.recv().await {
match request {
Request::Purge(purge_request) => {
let sst_file_path = sst_util::new_sst_file_path(
purge_request.space_id,
purge_request.table_id,
purge_request.file_id,
);
info!(
"File purger delete file, purge_request:{:?}, sst_file_path:{}",
purge_request,
sst_file_path.to_string()
);
for path in purge_request.associated_files {
let path = Path::from(path);
Self::delete_file(&store, &path).await;
}
Self::delete_file(&store, &sst_file_path).await;
}
Request::Exit => break,
}
}
info!("File purger exit");
}
}
pub type FilePurgerRef = Arc<FilePurger>;
#[cfg(test)]
pub mod tests {
use super::*;
pub struct FilePurgerMocker;
impl FilePurgerMocker {
pub fn mock() -> FilePurger {
let (sender, _receiver) = mpsc::unbounded_channel();
FilePurger {
sender,
handle: Mutex::new(None),
}
}
}
}