forked from apache/horaedb
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread.rs
339 lines (309 loc) · 11.7 KB
/
read.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
// 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.
//! Contains common methods used by the read process.
use std::{sync::Arc, time::Duration};
use ceresdbproto::storage::{
storage_service_client::StorageServiceClient, RequestContext, SqlQueryRequest, SqlQueryResponse,
};
use futures::FutureExt;
use generic_error::BoxError;
use http::StatusCode;
use interpreters::interpreter::Output;
use logger::{error, info, warn, SlowTimer};
use notifier::notifier::{ExecutionGuard, RequestNotifiers, RequestResult};
use query_frontend::{
frontend,
frontend::{Context as SqlContext, Frontend},
plan::{Plan, PriorityContext},
provider::CatalogMetaProvider,
};
use router::endpoint::Endpoint;
use snafu::{ensure, ResultExt};
use tokio::sync::mpsc::{self, Sender};
use tonic::{transport::Channel, IntoRequest};
use crate::{
error::{ErrNoCause, ErrWithCause, Error, Internal, InternalNoCause, Result},
forward::{ForwardRequest, ForwardResult},
metrics::GRPC_HANDLER_COUNTER_VEC,
Context, Proxy,
};
const DEDUP_READ_CHANNEL_LEN: usize = 1;
pub type ReadRequestNotifiers = Arc<RequestNotifiers<String, Sender<Result<SqlResponse>>>>;
pub enum SqlResponse {
Forwarded(SqlQueryResponse),
Local(Output),
}
impl Proxy {
pub(crate) async fn handle_sql(
&self,
ctx: &Context,
schema: &str,
sql: &str,
enable_partition_table_access: bool,
enable_block_query: bool, // true for grpc, false for http
) -> Result<SqlResponse> {
if let Some(resp) = self
.maybe_forward_sql_query(ctx.clone(), schema, sql)
.await?
{
match resp {
ForwardResult::Forwarded(resp) => return Ok(SqlResponse::Forwarded(resp?)),
ForwardResult::Local => (),
}
};
let output = self
.fetch_sql_query_output(
ctx,
schema,
sql,
enable_partition_table_access,
enable_block_query,
)
.await?;
Ok(SqlResponse::Local(output))
}
pub(crate) async fn dedup_handle_sql(
&self,
ctx: &Context,
schema: &str,
sql: &str,
request_notifiers: ReadRequestNotifiers,
enable_partition_table_access: bool,
) -> Result<SqlResponse> {
let (tx, mut rx) = mpsc::channel(DEDUP_READ_CHANNEL_LEN);
let mut guard = match request_notifiers.insert_notifier(sql.to_string(), tx) {
RequestResult::First => ExecutionGuard::new(|| {
request_notifiers.take_notifiers(&sql.to_string());
}),
RequestResult::Wait => {
match rx.recv().await {
Some(v) => return v,
None => {
return InternalNoCause {
msg: format!("notifier by query dedup has been dropped, sql:{sql}"),
}
.fail()
}
};
}
};
if let Some(resp) = self
.maybe_forward_sql_query(ctx.clone(), schema, sql)
.await?
{
match resp {
ForwardResult::Forwarded(resp) => {
let resp = resp?;
guard.cancel();
let notifiers = request_notifiers.take_notifiers(&sql.to_string()).unwrap();
for notifier in ¬ifiers {
if let Err(e) = notifier
.send(Ok(SqlResponse::Forwarded(resp.clone())))
.await
{
error!("Failed to send handler result, err:{}.", e);
}
}
GRPC_HANDLER_COUNTER_VEC
.dedupped_stream_query
.inc_by((notifiers.len() - 1) as u64);
return Ok(SqlResponse::Forwarded(resp));
}
ForwardResult::Local => (),
}
};
let result = self
.fetch_sql_query_output(ctx, schema, sql, enable_partition_table_access, true)
.await;
guard.cancel();
let notifiers = request_notifiers.take_notifiers(&sql.to_string()).unwrap();
for notifier in ¬ifiers {
let item = match &result {
Ok(v) => Ok(SqlResponse::Local(v.clone())),
Err(e) => ErrNoCause {
code: e.code(),
msg: e.to_string(),
}
.fail(),
};
if let Err(e) = notifier.send(item).await {
error!("Failed to send handler result, err:{}.", e);
}
}
GRPC_HANDLER_COUNTER_VEC
.dedupped_stream_query
.inc_by((notifiers.len() - 1) as u64);
Ok(SqlResponse::Local(result?))
}
#[inline]
pub(crate) async fn fetch_sql_query_output(
&self,
ctx: &Context,
// TODO: maybe we can put params below input a new ReadRequest struct.
schema: &str,
sql: &str,
enable_partition_table_access: bool,
enable_block_query: bool,
) -> Result<Output> {
let request_id = &ctx.request_id;
let slow_threshold_secs = self
.instance()
.dyn_config
.slow_threshold
.load(std::sync::atomic::Ordering::Relaxed);
let slow_threshold = Duration::from_secs(slow_threshold_secs);
let mut slow_timer = SlowTimer::new(request_id.as_str(), sql, slow_threshold);
let deadline = ctx.timeout.map(|t| slow_timer.start_time() + t);
let catalog = self.instance.catalog_manager.default_catalog_name();
info!("Handle sql query begin, request_id:{request_id}, catalog:{catalog}, schema:{schema}, ctx:{ctx:?}, sql:{sql}");
let instance = &self.instance;
// TODO(yingwen): Privilege check, cannot access data of other tenant
// TODO(yingwen): Maybe move MetaProvider to instance
let provider = CatalogMetaProvider {
manager: instance.catalog_manager.clone(),
default_catalog: catalog,
default_schema: schema,
function_registry: &*instance.function_registry,
};
let frontend = Frontend::new(provider, instance.dyn_config.fronted.clone());
let mut sql_ctx = SqlContext::new(request_id.clone(), deadline);
// Parse sql, frontend error of invalid sql already contains sql
// TODO(yingwen): Maybe move sql from frontend error to outer error
let mut stmts = frontend
.parse_sql(&mut sql_ctx, sql)
.box_err()
.context(ErrWithCause {
code: StatusCode::BAD_REQUEST,
msg: "Failed to parse sql",
})?;
// TODO: For simplicity, we only support executing one statement
let stmts_len = stmts.len();
ensure!(
stmts_len == 1,
ErrNoCause {
code: StatusCode::BAD_REQUEST,
msg: format!("Only support execute one statement now, current num:{stmts_len}"),
}
);
// Open partition table if needed.
let table_name = frontend::parse_table_name(&stmts);
if let Some(table_name) = &table_name {
self.maybe_open_partition_table_if_not_exist(catalog, schema, table_name)
.await?;
}
// Create logical plan
// Note: Remember to store sql in error when creating logical plan
let plan = frontend
// TODO(yingwen): Check error, some error may indicate that the sql is invalid. Now we
// return internal server error in those cases
.statement_to_plan(&sql_ctx, stmts.remove(0))
.box_err()
.with_context(|| ErrWithCause {
code: StatusCode::INTERNAL_SERVER_ERROR,
msg: "Failed to create plan",
})?;
if enable_block_query {
self.instance
.limiter
.try_limit(&plan)
.box_err()
.context(Internal {
msg: format!("Request is blocked, table_name:{table_name:?}"),
})?;
}
if let Plan::Query(plan) = &plan {
if let Some(priority) = plan.decide_query_priority(PriorityContext {
time_range_threshold: self.expensive_query_threshold,
}) {
slow_timer.priority(priority);
}
}
let output = if enable_partition_table_access {
self.execute_plan_involving_partition_table(
request_id.clone(),
catalog,
schema,
plan,
deadline,
)
.await
} else {
self.execute_plan(request_id.clone(), catalog, schema, plan, deadline)
.await
};
let output = output.box_err().with_context(|| ErrWithCause {
code: StatusCode::INTERNAL_SERVER_ERROR,
msg: "Failed to execute plan",
})?;
let cost = slow_timer.elapsed();
info!(
"Handle sql query finished, sql:{sql}, elapsed:{cost:?}, catalog:{catalog}, schema:{schema}, ctx:{ctx:?}",
);
Ok(output)
}
async fn maybe_forward_sql_query(
&self,
ctx: Context,
schema: &str,
sql: &str,
) -> Result<Option<ForwardResult<SqlQueryResponse, Error>>> {
let table_name = frontend::parse_table_name_with_sql(sql)
.box_err()
.with_context(|| Internal {
msg: "parse table name",
})?;
if table_name.is_none() {
warn!("Unable to forward sql query without table name, sql:{sql}",);
return Ok(None);
}
let sql_request = SqlQueryRequest {
context: Some(RequestContext {
database: schema.to_string(),
}),
tables: vec![],
sql: sql.to_string(),
};
let forward_req = ForwardRequest {
schema: schema.to_string(),
table: table_name.unwrap(),
req: sql_request.into_request(),
forwarded_from: ctx.forwarded_from,
};
let do_query = |mut client: StorageServiceClient<Channel>,
request: tonic::Request<SqlQueryRequest>,
_: &Endpoint| {
let query = async move {
client
.sql_query(request)
.await
.map(|resp| resp.into_inner())
.box_err()
.context(ErrWithCause {
code: StatusCode::INTERNAL_SERVER_ERROR,
msg: "Forwarded sql query failed",
})
}
.boxed();
Box::new(query) as _
};
let forward_result = self.forwarder.forward(forward_req, do_query).await;
Ok(match forward_result {
Ok(forward_res) => Some(forward_res),
Err(e) => {
error!("Failed to forward sql req but the error is ignored, err:{e}");
None
}
})
}
}