forked from apache/horaedb
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.rs
399 lines (354 loc) · 11.8 KB
/
mod.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
// Copyright 2023 The HoraeDB 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.
//! A table engine instance
//!
//! The root mod only contains common functions of instance, other logics are
//! divided into the sub crates
pub(crate) mod alter;
mod close;
mod create;
mod drop;
pub mod engine;
pub mod flush_compaction;
pub(crate) mod mem_collector;
pub mod open;
mod read;
mod reorder_memtable;
pub(crate) mod serial_executor;
pub mod wal_replayer;
pub(crate) mod write;
use std::sync::Arc;
use common_types::{projected_schema::RowProjectorBuilder, table::TableId};
use generic_error::{BoxError, GenericError};
use logger::{error, info};
use macros::define_result;
use mem_collector::MemUsageCollector;
use runtime::{PriorityRuntime, Runtime};
use snafu::{ResultExt, Snafu};
use table_engine::{engine::EngineRuntimes, predicate::PredicateRef, table::FlushRequest};
use time_ext::ReadableDuration;
use tokio::sync::oneshot::{self, error::RecvError};
use wal::manager::{WalLocation, WalManagerRef};
use self::flush_compaction::{Flusher, TableFlushOptions};
use crate::{
compaction::{scheduler::CompactionSchedulerRef, TableCompactionRequest},
manifest::ManifestRef,
row_iter::IterOptions,
space::{SpaceId, SpaceRef, SpacesRef},
sst::{
factory::{
FactoryRef as SstFactoryRef, ObjectStorePickerRef, ReadFrequency, ScanOptions,
SstReadOptions,
},
file::FilePurgerRef,
meta_data::cache::MetaCacheRef,
metrics::MaybeTableLevelMetrics,
},
table::data::{TableDataRef, TableShardInfo},
RecoverMode, TableOptions, WalEncodeConfig,
};
#[allow(clippy::enum_variant_names)]
#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("Failed to stop file purger, err:{}", source))]
StopFilePurger { source: crate::sst::file::Error },
#[snafu(display("Failed to stop compaction scheduler, err:{}", source))]
StopScheduler {
source: crate::compaction::scheduler::Error,
},
#[snafu(display("Failed to {} table manually, table:{}, err:{}", op, table, source))]
ManualOp {
op: String,
table: String,
source: GenericError,
},
#[snafu(display("Failed to receive {} result, table:{}, err:{}", op, table, source))]
RecvManualOpResult {
op: String,
table: String,
source: RecvError,
},
}
define_result!(Error);
pub struct SpaceStore {
/// All spaces of the engine.
spaces: SpacesRef,
/// Manifest (or meta) stores meta data of the engine instance.
manifest: ManifestRef,
/// Wal of all tables
wal_manager: WalManagerRef,
/// Object store picker for persisting data.
store_picker: ObjectStorePickerRef,
/// Sst factory.
sst_factory: SstFactoryRef,
meta_cache: Option<MetaCacheRef>,
}
pub type SpaceStoreRef = Arc<SpaceStore>;
impl Drop for SpaceStore {
fn drop(&mut self) {
info!("SpaceStore dropped");
}
}
impl SpaceStore {
async fn close(&self) -> Result<()> {
// TODO: close all background jobs.
Ok(())
}
}
impl SpaceStore {
fn store_picker(&self) -> &ObjectStorePickerRef {
&self.store_picker
}
/// List all tables of all spaces
pub fn list_all_tables(&self, tables: &mut Vec<TableDataRef>) {
let spaces = self.spaces.read().unwrap();
spaces.list_all_tables(tables);
}
/// Find the space which it's all memtables consumes maximum memory.
#[inline]
fn find_maximum_memory_usage_space(&self) -> Option<SpaceRef> {
let spaces = self.spaces.read().unwrap().list_all_spaces();
spaces.into_iter().max_by_key(|t| t.memtable_memory_usage())
}
/// The memory space used by all tables in the space.
#[inline]
fn total_memory_usage_space(&self) -> usize {
let spaces = self.spaces.read().unwrap().list_all_spaces();
spaces.into_iter().map(|t| t.memtable_memory_usage()).sum()
}
}
/// Table engine instance
///
/// Manages all spaces, also contains needed resources shared across all table
pub struct Instance {
/// Space storage
space_store: SpaceStoreRef,
/// Runtime to execute async tasks.
runtimes: Arc<EngineRuntimes>,
/// Global table options, overwrite mutable options in each table's
/// TableOptions.
table_opts: TableOptions,
// End of write group options.
file_purger: FilePurgerRef,
compaction_scheduler: CompactionSchedulerRef,
meta_cache: Option<MetaCacheRef>,
/// Engine memtable memory usage collector
mem_usage_collector: Arc<MemUsageCollector>,
pub(crate) max_rows_in_write_queue: usize,
/// Engine write buffer size
pub(crate) db_write_buffer_size: usize,
/// Space write buffer size
pub(crate) space_write_buffer_size: usize,
/// Replay wal batch size
pub(crate) replay_batch_size: usize,
/// Write sst max buffer size
pub(crate) write_sst_max_buffer_size: usize,
/// The min interval between flushes
pub(crate) min_flush_interval: ReadableDuration,
/// Max retry limit to flush memtables
pub(crate) max_retry_flush_limit: usize,
/// Max bytes per write batch
pub(crate) max_bytes_per_write_batch: Option<usize>,
/// The interval for sampling the mem size
pub(crate) mem_usage_sampling_interval: ReadableDuration,
/// Options for scanning sst
pub(crate) scan_options: ScanOptions,
pub(crate) iter_options: Option<IterOptions>,
pub(crate) recover_mode: RecoverMode,
pub(crate) wal_encode: WalEncodeConfig,
pub(crate) disable_wal: bool,
}
impl Instance {
/// Close the instance gracefully.
pub async fn close(&self) -> Result<()> {
self.file_purger.stop().await.context(StopFilePurger)?;
self.space_store.close().await?;
self.compaction_scheduler
.stop_scheduler()
.await
.context(StopScheduler)
}
pub async fn manual_flush_table(
&self,
table_data: &TableDataRef,
request: FlushRequest,
) -> Result<()> {
let mut rx_opt = None;
let flush_opts = TableFlushOptions {
res_sender: if request.sync {
let (tx, rx) = oneshot::channel();
rx_opt = Some(rx);
Some(tx)
} else {
None
},
max_retry_flush_limit: 0,
};
let flusher = self.make_flusher();
let mut serial_exec = table_data.serial_exec.lock().await;
let flush_scheduler = serial_exec.flush_scheduler();
flusher
.schedule_flush(flush_scheduler, table_data, flush_opts)
.await
.box_err()
.context(ManualOp {
op: "flush",
table: &table_data.name,
})?;
if let Some(rx) = rx_opt {
rx.await
.context(RecvManualOpResult {
op: "flush",
table: &table_data.name,
})?
.box_err()
.context(ManualOp {
op: "flush",
table: &table_data.name,
})?;
}
Ok(())
}
// This method will wait until compaction finished.
pub async fn manual_compact_table(&self, table_data: &TableDataRef) -> Result<()> {
let (request, rx) = TableCompactionRequest::new(table_data.clone());
let succeed = self
.compaction_scheduler
.schedule_table_compaction(request)
.await;
if !succeed {
error!("Failed to schedule compaction, table:{}", table_data.name);
}
rx.await
.context(RecvManualOpResult {
op: "compact",
table: &table_data.name,
})?
.box_err()
.context(ManualOp {
op: "compact",
table: &table_data.name,
})
}
}
// TODO(yingwen): Instance builder
impl Instance {
/// Find space using read lock
fn get_space_by_read_lock(&self, space: SpaceId) -> Option<SpaceRef> {
let spaces = self.space_store.spaces.read().unwrap();
spaces.get_by_id(space).cloned()
}
/// Returns true when engine instance's total memtable memory usage reaches
/// db_write_buffer_size limit.
#[inline]
fn should_flush_instance(&self) -> bool {
self.db_write_buffer_size > 0
&& self.space_store.total_memory_usage_space() >= self.db_write_buffer_size
}
#[inline]
fn read_runtime(&self) -> &PriorityRuntime {
&self.runtimes.read_runtime
}
#[inline]
pub fn write_runtime(&self) -> &Arc<Runtime> {
&self.runtimes.write_runtime
}
#[inline]
fn make_flusher(&self) -> Flusher {
Flusher {
space_store: self.space_store.clone(),
// Do flush in write runtime
runtime: self.runtimes.write_runtime.clone(),
write_sst_max_buffer_size: self.write_sst_max_buffer_size,
min_flush_interval_ms: None,
}
}
#[inline]
fn make_flusher_with_min_interval(&self) -> Flusher {
Flusher {
space_store: self.space_store.clone(),
// Do flush in write runtime
runtime: self.runtimes.write_runtime.clone(),
write_sst_max_buffer_size: self.write_sst_max_buffer_size,
min_flush_interval_ms: Some(self.min_flush_interval.as_millis()),
}
}
#[inline]
fn max_retry_flush_limit(&self) -> usize {
self.max_retry_flush_limit
}
}
#[derive(Debug, Clone)]
pub struct SstReadOptionsBuilder {
scan_type: ScanType,
scan_options: ScanOptions,
maybe_table_level_metrics: Arc<MaybeTableLevelMetrics>,
num_rows_per_row_group: usize,
predicate: PredicateRef,
meta_cache: Option<MetaCacheRef>,
runtime: Arc<Runtime>,
}
impl SstReadOptionsBuilder {
pub fn new(
scan_type: ScanType,
scan_options: ScanOptions,
maybe_table_level_metrics: Arc<MaybeTableLevelMetrics>,
num_rows_per_row_group: usize,
predicate: PredicateRef,
meta_cache: Option<MetaCacheRef>,
runtime: Arc<Runtime>,
) -> Self {
Self {
scan_type,
scan_options,
maybe_table_level_metrics,
num_rows_per_row_group,
predicate,
meta_cache,
runtime,
}
}
pub fn build(self, row_projector_builder: RowProjectorBuilder) -> SstReadOptions {
SstReadOptions {
maybe_table_level_metrics: self.maybe_table_level_metrics,
num_rows_per_row_group: self.num_rows_per_row_group,
frequency: self.scan_type.into(),
row_projector_builder,
predicate: self.predicate,
meta_cache: self.meta_cache,
scan_options: self.scan_options,
runtime: self.runtime,
}
}
}
/// Scan type which mapped to the low level `ReadFrequency` in sst reader.
#[derive(Debug, Clone, Copy)]
pub enum ScanType {
Query,
Compaction,
}
impl From<ScanType> for ReadFrequency {
fn from(value: ScanType) -> Self {
match value {
ScanType::Query => ReadFrequency::Frequent,
ScanType::Compaction => ReadFrequency::Once,
}
}
}
/// Instance reference
pub type InstanceRef = Arc<Instance>;
#[inline]
pub(crate) fn create_wal_location(table_id: TableId, shard_info: TableShardInfo) -> WalLocation {
WalLocation::new(shard_info.shard_id as u64, table_id)
}