forked from apache/horaedb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtable_operator.rs
266 lines (233 loc) · 9.09 KB
/
table_operator.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
// Copyright 2023 CeresDB Project Authors. Licensed under Apache-2.0.
use std::time::Instant;
use common_util::{error::BoxError, time::InstantExt};
use log::{info, warn};
use snafu::{OptionExt, ResultExt};
use table_engine::{engine, table::TableRef};
use crate::{
manager::ManagerRef,
schema::{
CloseOptions, CloseShardRequest, CloseTableRequest, CreateOptions, CreateTableRequest,
DropOptions, DropTableRequest, OpenOptions, OpenShardRequest, OpenTableRequest, SchemaRef,
},
Result, TableOperatorNoCause, TableOperatorWithCause,
};
/// Table operator
///
/// Encapsulate all operations about tables, including create/drop, open/close
/// and etc.
#[derive(Clone)]
pub struct TableOperator {
catalog_manager: ManagerRef,
}
impl TableOperator {
pub fn new(catalog_manager: ManagerRef) -> Self {
Self { catalog_manager }
}
pub async fn open_shard(&self, request: OpenShardRequest, opts: OpenOptions) -> Result<()> {
let instant = Instant::now();
let table_engine = opts.table_engine;
let shard_id = request.shard_id;
// Generate open requests.
let mut schemas = Vec::with_capacity(request.table_defs.len());
let mut engine_table_defs = Vec::with_capacity(request.table_defs.len());
for open_ctx in request.table_defs {
let schema = self.schema_by_name(&open_ctx.catalog_name, &open_ctx.schema_name)?;
engine_table_defs.push(open_ctx.into_engine_table_def(schema.id()));
schemas.push(schema);
}
// Open tables by table engine.
// TODO: add the `open_shard` method into table engine.
let engine_open_shard_req = engine::OpenShardRequest {
shard_id: request.shard_id,
table_defs: engine_table_defs,
engine: request.engine,
};
let open_results = table_engine.open_shard(engine_open_shard_req).await;
// Check and register successful opened table into schema.
let mut success_count = 0_u32;
let mut no_table_count = 0_u32;
let mut open_err_count = 0_u32;
for (schema, open_result) in schemas.into_iter().zip(open_results.into_iter()) {
match open_result {
Ok(Some(table)) => {
schema.register_table(table);
success_count += 1;
}
Ok(None) => {
no_table_count += 1;
}
// Has printed error log for it.
Err(_) => {
open_err_count += 1;
}
}
}
info!(
"Open shard finish, shard id:{}, cost:{}ms, successful count:{}, no table is opened count:{}, open error count:{}",
shard_id,
instant.saturating_elapsed().as_millis(),
success_count,
no_table_count,
open_err_count
);
if no_table_count == 0 && open_err_count == 0 {
Ok(())
} else {
TableOperatorNoCause {
msg: format!(
"Failed to open shard, some tables open failed, shard id:{shard_id}, no table is opened count:{no_table_count}, open error count:{open_err_count}"),
}.fail()
}
}
pub async fn close_shard(&self, request: CloseShardRequest, opts: CloseOptions) -> Result<()> {
let instant = Instant::now();
let table_engine = opts.table_engine;
let shard_id = request.shard_id;
// Generate open requests.
let mut schemas = Vec::with_capacity(request.table_defs.len());
let mut engine_table_defs = Vec::with_capacity(request.table_defs.len());
for table_def in request.table_defs {
let schema = self.schema_by_name(&table_def.catalog_name, &table_def.schema_name)?;
engine_table_defs.push(table_def.into_engine_table_def(schema.id()));
schemas.push(schema);
}
// Close tables by table engine.
// TODO: add the `close_shard` method into table engine.
let engine_close_shard_req = engine::CloseShardRequest {
shard_id: request.shard_id,
table_defs: engine_table_defs,
engine: request.engine,
};
let close_results = table_engine.close_shard(engine_close_shard_req).await;
// Check and unregister successful closed table from schema.
let mut success_count = 0_u32;
let mut close_err_count = 0_u32;
for (schema, close_result) in schemas.into_iter().zip(close_results.into_iter()) {
match close_result {
Ok(table_name) => {
schema.unregister_table(&table_name);
success_count += 1;
}
Err(_) => {
close_err_count += 1;
}
}
}
info!(
"Close shard finished, shard id:{}, cost:{}ms, success_count:{}, close_err_count:{}",
shard_id,
instant.saturating_elapsed().as_millis(),
success_count,
close_err_count
);
if close_err_count == 0 {
Ok(())
} else {
TableOperatorNoCause {
msg: format!(
"Failed to close shard, shard id:{shard_id}, success_count:{success_count}, close_err_count:{close_err_count}",
),
}
.fail()
}
}
pub async fn open_table_on_shard(
&self,
request: OpenTableRequest,
opts: OpenOptions,
) -> Result<()> {
let table_engine = opts.table_engine;
let schema = self.schema_by_name(&request.catalog_name, &request.schema_name)?;
let table = table_engine
.open_table(request.clone().into_engine_open_request(schema.id()))
.await
.box_err()
.context(TableOperatorWithCause {
msg: format!("failed to open table on shard, request:{request:?}"),
})?
.context(TableOperatorNoCause {
msg: format!("table engine returns none when opening table, request:{request:?}"),
})?;
schema.register_table(table);
Ok(())
}
pub async fn close_table_on_shard(
&self,
request: CloseTableRequest,
opts: CloseOptions,
) -> Result<()> {
let table_engine = opts.table_engine;
let schema = self.schema_by_name(&request.catalog_name, &request.schema_name)?;
let table_name = request.table_name.clone();
table_engine
.close_table(request.clone().into_engine_close_request(schema.id()))
.await
.box_err()
.context(TableOperatorWithCause {
msg: format!("failed to close table on shard, request:{request:?}"),
})?;
schema.unregister_table(&table_name);
Ok(())
}
pub async fn create_table_on_shard(
&self,
request: CreateTableRequest,
opts: CreateOptions,
) -> Result<TableRef> {
let schema = self.schema_by_name(&request.catalog_name, &request.schema_name)?;
// TODO: we should create table directly by table engine, and register table
// into schema like opening.
schema
.create_table(request.clone(), opts)
.await
.box_err()
.context(TableOperatorWithCause {
msg: format!("failed to create table on shard, request:{request:?}"),
})
}
pub async fn drop_table_on_shard(
&self,
request: DropTableRequest,
opts: DropOptions,
) -> Result<()> {
let schema = self.schema_by_name(&request.catalog_name, &request.schema_name)?;
// TODO: we should drop table directly by table engine, and unregister table
// from schema like closing.
let has_dropped = schema
.drop_table(request.clone(), opts)
.await
.box_err()
.context(TableOperatorWithCause {
msg: format!("failed to create table on shard, request:{request:?}"),
})?;
if has_dropped {
warn!(
"Table has been dropped already, table_name:{}",
request.table_name
);
}
Ok(())
}
fn schema_by_name(&self, catalog_name: &str, schema_name: &str) -> Result<SchemaRef> {
let catalog = self
.catalog_manager
.catalog_by_name(catalog_name)
.box_err()
.context(TableOperatorWithCause {
msg: format!("failed to find catalog, catalog_name:{catalog_name}"),
})?
.context(TableOperatorNoCause {
msg: format!("catalog not found, catalog_name:{catalog_name}"),
})?;
catalog
.schema_by_name(schema_name)
.box_err()
.context(TableOperatorWithCause {
msg: format!("failed to find schema, schema_name:{schema_name}"),
})?
.context(TableOperatorNoCause {
msg: format!("schema not found, schema_name:{schema_name}"),
})
}
}