-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathopen.rs
476 lines (420 loc) · 16.2 KB
/
open.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
// Copyright 2022 CeresDB Project Authors. Licensed under Apache-2.0.
//! Open logic of instance
use std::{
collections::VecDeque,
sync::{Arc, RwLock},
};
use common_types::schema::IndexInWriterSchema;
use log::{debug, error, info, trace, warn};
use snafu::ResultExt;
use table_engine::{engine::OpenTableRequest, remote::RemoteEngineRef};
use tokio::sync::oneshot;
use wal::{
log_batch::LogEntry,
manager::{ReadBoundary, ReadContext, ReadRequest, WalManagerRef},
};
use crate::{
compaction::scheduler::SchedulerImpl,
context::OpenContext,
engine,
instance::{
self,
engine::{
ApplyMemTable, FlushTable, OperateByWriteWorker, ReadMetaUpdate, ReadWal,
RecoverTableData, Result,
},
flush_compaction::TableFlushOptions,
mem_collector::MemUsageCollector,
write_worker,
write_worker::{RecoverTableCommand, WorkerLocal, WriteGroup},
Instance, SpaceStore, Spaces,
},
manifest::{meta_data::TableManifestData, LoadRequest, ManifestRef},
payload::{ReadPayload, WalDecoder},
row_iter::IterOptions,
space::{Space, SpaceContext, SpaceId, SpaceRef},
sst::{
factory::{FactoryRef as SstFactoryRef, ObjectStorePickerRef, ScanOptions},
file::FilePurger,
},
table::data::{TableData, TableDataRef},
};
const MAX_RECORD_BATCHES_IN_FLIGHT_WHEN_COMPACTION_READ: usize = 64;
impl Instance {
/// Open a new instance
pub async fn open(
ctx: OpenContext,
manifest: ManifestRef,
wal_manager: WalManagerRef,
store_picker: ObjectStorePickerRef,
sst_factory: SstFactoryRef,
remote_engine_ref: Option<RemoteEngineRef>,
) -> Result<Arc<Self>> {
let space_store = Arc::new(SpaceStore {
spaces: RwLock::new(Spaces::default()),
manifest,
wal_manager: wal_manager.clone(),
store_picker: store_picker.clone(),
sst_factory,
meta_cache: ctx.meta_cache.clone(),
});
let scheduler_config = ctx.config.compaction_config.clone();
let bg_runtime = ctx.runtimes.bg_runtime.clone();
let scan_options_for_compaction = ScanOptions {
background_read_parallelism: 1,
max_record_batches_in_flight: MAX_RECORD_BATCHES_IN_FLIGHT_WHEN_COMPACTION_READ,
};
let compaction_scheduler = Arc::new(SchedulerImpl::new(
space_store.clone(),
bg_runtime.clone(),
scheduler_config,
ctx.config.write_sst_max_buffer_size.as_byte() as usize,
scan_options_for_compaction,
));
let file_purger = FilePurger::start(&bg_runtime, store_picker.default_store().clone());
let scan_options = ScanOptions {
background_read_parallelism: ctx.config.sst_background_read_parallelism,
max_record_batches_in_flight: ctx.config.scan_max_record_batches_in_flight,
};
let iter_options = ctx
.config
.scan_batch_size
.map(|batch_size| IterOptions { batch_size });
let instance = Arc::new(Instance {
space_store,
runtimes: ctx.runtimes.clone(),
table_opts: ctx.config.table_opts.clone(),
write_group_worker_num: ctx.config.write_group_worker_num,
write_group_command_channel_cap: ctx.config.write_group_command_channel_cap,
compaction_scheduler,
file_purger,
meta_cache: ctx.meta_cache.clone(),
mem_usage_collector: Arc::new(MemUsageCollector::default()),
db_write_buffer_size: ctx.config.db_write_buffer_size,
space_write_buffer_size: ctx.config.space_write_buffer_size,
replay_batch_size: ctx.config.replay_batch_size,
write_sst_max_buffer_size: ctx.config.write_sst_max_buffer_size.as_byte() as usize,
max_bytes_per_write_batch: ctx
.config
.max_bytes_per_write_batch
.map(|v| v.as_byte() as usize),
iter_options,
scan_options,
remote_engine: remote_engine_ref,
});
Ok(instance)
}
/// Open the space if it is not opened before.
async fn open_space(
self: &Arc<Self>,
space_id: SpaceId,
context: SpaceContext,
) -> Result<SpaceRef> {
{
let spaces = self.space_store.spaces.read().unwrap();
if let Some(space) = spaces.get_by_id(space_id) {
return Ok(space.clone());
}
}
// double check whether the space exists.
let mut spaces = self.space_store.spaces.write().unwrap();
if let Some(space) = spaces.get_by_id(space_id) {
return Ok(space.clone());
}
// space is not opened yet and try to open it
let write_group_opts = self.write_group_options(space_id);
let write_group = WriteGroup::new(write_group_opts, self.clone());
// Add this space to instance.
let space = Arc::new(Space::new(
space_id,
context,
self.space_write_buffer_size,
write_group,
self.mem_usage_collector.clone(),
));
spaces.insert(space.clone());
Ok(space)
}
/// Open the table.
pub async fn do_open_table(
self: &Arc<Self>,
space: SpaceRef,
request: &OpenTableRequest,
) -> Result<Option<TableDataRef>> {
if let Some(table_data) = space.find_table_by_id(request.table_id) {
return Ok(Some(table_data));
}
let table_data = match self.recover_table_meta_data(request).await? {
Some(v) => v,
None => return Ok(None),
};
let (tx, rx) = oneshot::channel();
let cmd = RecoverTableCommand {
space,
table_data: table_data.clone(),
tx,
replay_batch_size: self.replay_batch_size,
};
// Send recover request to write worker, actual works done in
// Self::recover_table_from_wal()
write_worker::process_command_in_write_worker(cmd.into_command(), &table_data, rx)
.await
.context(OperateByWriteWorker {
space_id: table_data.space_id,
table: &table_data.name,
table_id: table_data.id,
})
}
/// Recover the table data.
///
/// Return None if the table data does not exist.
pub async fn process_recover_table_command(
self: &Arc<Self>,
worker_local: &mut WorkerLocal,
space: SpaceRef,
table_data: TableDataRef,
replay_batch_size: usize,
) -> Result<Option<TableDataRef>> {
if let Some(exist_table_data) = space.find_table_by_id(table_data.id) {
warn!("Open a opened table, table:{}", table_data.name);
return Ok(Some(exist_table_data));
}
let read_ctx = ReadContext {
batch_size: replay_batch_size,
..Default::default()
};
self.recover_table_from_wal(
worker_local,
table_data.clone(),
replay_batch_size,
&read_ctx,
)
.await
.map_err(|e| {
error!("Recovery table from wal failed, table_data:{table_data:?}, err:{e}");
space.insert_open_failed_table(table_data.name.to_string());
e
})?;
space.insert_table(table_data.clone());
Ok(Some(table_data))
}
/// Recover meta data from manifest
///
/// Return None if no meta data is found for the table.
async fn recover_table_meta_data(
self: &Arc<Self>,
request: &OpenTableRequest,
) -> Result<Option<TableDataRef>> {
info!("Instance recover table:{} meta begin", request.table_id);
// Load manifest, also create a new snapshot at startup.
let table_id = request.table_id;
let space_id = engine::build_space_id(request.schema_id);
let load_req = LoadRequest {
space_id,
table_id,
shard_id: request.shard_id,
};
let manifest_data = self
.space_store
.manifest
.load_data(&load_req)
.await
.context(ReadMetaUpdate {
table_id: request.table_id,
})?;
let table_data = if let Some(manifest_data) = manifest_data {
Some(self.recover_table_data(manifest_data, request).await?)
} else {
None
};
info!("Instance recover table:{} meta end", request.table_id);
Ok(table_data)
}
/// Recover `TableData` by applying manifest data to instance
async fn recover_table_data(
self: &Arc<Self>,
manifest_data: TableManifestData,
request: &OpenTableRequest,
) -> Result<TableDataRef> {
let TableManifestData {
table_meta,
version_meta,
} = manifest_data;
let context = SpaceContext {
catalog_name: request.catalog_name.clone(),
schema_name: request.schema_name.clone(),
};
let space = self.open_space(table_meta.space_id, context).await?;
let (table_id, table_name) = (table_meta.table_id, table_meta.table_name.clone());
// Choose write worker for this table
let write_handle = space.write_group.choose_worker(table_id);
debug!("Instance apply add table, meta :{:?}", table_meta);
let table_data = Arc::new(
TableData::recover_from_add(
table_meta.clone(),
write_handle,
&self.file_purger,
space.mem_usage_collector.clone(),
request.shard_id,
request.cluster_version,
)
.context(RecoverTableData {
space_id: table_meta.space_id,
table: &table_name,
})?,
);
// Apply version meta to the table.
if let Some(version_meta) = version_meta {
let max_file_id = version_meta.max_file_id_to_add();
table_data.current_version().apply_meta(version_meta);
// In recovery case, we need to maintain last file id of the table manually.
if table_data.last_file_id() < max_file_id {
table_data.set_last_file_id(max_file_id);
}
}
Ok(table_data)
}
/// Recover table data from wal
///
/// Called by write worker
pub(crate) async fn recover_table_from_wal(
self: &Arc<Self>,
worker_local: &mut WorkerLocal,
table_data: TableDataRef,
replay_batch_size: usize,
read_ctx: &ReadContext,
) -> Result<()> {
debug!(
"Instance recover table from wal, replay batch size:{}, table id:{}, shard info:{:?}",
replay_batch_size, table_data.id, table_data.shard_info
);
let table_location = table_data.table_location();
let wal_location =
instance::create_wal_location(table_location.id, table_location.shard_info);
let read_req = ReadRequest {
location: wal_location,
start: ReadBoundary::Excluded(table_data.current_version().flushed_sequence()),
end: ReadBoundary::Max,
};
// Read all wal of current table.
let mut log_iter = self
.space_store
.wal_manager
.read_batch(read_ctx, &read_req)
.await
.context(ReadWal)?;
let mut log_entry_buf = VecDeque::with_capacity(replay_batch_size);
loop {
// fetch entries to log_entry_buf
let decoder = WalDecoder::default();
log_entry_buf = log_iter
.next_log_entries(decoder, log_entry_buf)
.await
.context(ReadWal)?;
// Replay all log entries of current table
self.replay_table_log_entries(worker_local, &table_data, &log_entry_buf)
.await?;
// No more entries.
if log_entry_buf.is_empty() {
break;
}
}
Ok(())
}
/// Replay all log entries into memtable and flush if necessary.
async fn replay_table_log_entries(
self: &Arc<Self>,
worker_local: &mut WorkerLocal,
table_data: &TableDataRef,
log_entries: &VecDeque<LogEntry<ReadPayload>>,
) -> Result<()> {
if log_entries.is_empty() {
info!(
"Instance replay an empty table log entries, table:{}, table_id:{:?}",
table_data.name, table_data.id
);
// No data in wal
return Ok(());
}
let last_sequence = log_entries.back().unwrap().sequence;
info!(
"Instance replay table log entries begin, table:{}, table_id:{:?}, sequence:{}",
table_data.name, table_data.id, last_sequence
);
for log_entry in log_entries {
let (sequence, payload) = (log_entry.sequence, &log_entry.payload);
// Apply to memtable
match payload {
ReadPayload::Write { row_group } => {
trace!(
"Instance replay row_group, table:{}, row_group:{:?}",
table_data.name,
row_group
);
let table_schema_version = table_data.schema_version();
if table_schema_version != row_group.schema().version() {
// Data with old schema should already been flushed, but we avoid panic
// here.
error!(
"Ignore data with mismatch schema version during replaying, \
table:{}, \
table_id:{:?}, \
expect:{}, \
actual:{}, \
last_sequence:{}, \
sequence:{}",
table_data.name,
table_data.id,
table_schema_version,
row_group.schema().version(),
last_sequence,
sequence,
);
continue;
}
let index_in_writer =
IndexInWriterSchema::for_same_schema(row_group.schema().num_columns());
Self::write_to_memtable(
worker_local,
table_data,
sequence,
&row_group.into(),
index_in_writer,
)
.context(ApplyMemTable {
space_id: table_data.space_id,
table: &table_data.name,
table_id: table_data.id,
})?;
// Flush the table if necessary.
if table_data.should_flush_table(worker_local) {
let opts = TableFlushOptions {
res_sender: None,
compact_after_flush: false,
block_on_write_thread: false,
};
self.flush_table_in_worker(worker_local, table_data, opts)
.await
.context(FlushTable {
space_id: table_data.space_id,
table: &table_data.name,
table_id: table_data.id,
})?;
}
}
ReadPayload::AlterSchema { .. } | ReadPayload::AlterOptions { .. } => {
// Ignore records except Data.
//
// - DDL (AlterSchema and AlterOptions) should be recovered
// from Manifest on start.
}
}
}
info!(
"Instance replay table log entries end, table:{}, table_id:{:?}, last_sequence:{}",
table_data.name, table_data.id, last_sequence
);
table_data.set_last_sequence(last_sequence);
Ok(())
}
}