forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_executor_rpc_impl.h
More file actions
351 lines (330 loc) · 10.8 KB
/
Copy pathob_executor_rpc_impl.h
File metadata and controls
351 lines (330 loc) · 10.8 KB
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
/**
* Copyright (c) 2021 OceanBase
* OceanBase CE is licensed under Mulan PubL v2.
* You can use this software according to the terms and conditions of the Mulan PubL v2.
* You may obtain a copy of Mulan PubL v2 at:
* http://license.coscl.org.cn/MulanPubL-2.0
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PubL v2 for more details.
*/
#ifndef OCEANBASE_SQL_EXECUTOR_RPC_IMPL_
#define OCEANBASE_SQL_EXECUTOR_RPC_IMPL_
#include "share/ob_define.h"
#include "lib/container/ob_array.h"
#include "lib/allocator/ob_allocator.h"
#include "rpc/obrpc/ob_rpc_proxy.h"
#include "share/ob_scanner.h"
#include "share/rpc/ob_batch_rpc.h"
#include "sql/executor/ob_task.h"
#include "sql/executor/ob_task_info.h"
#include "sql/executor/ob_slice_id.h"
#include "sql/executor/ob_executor_rpc_proxy.h"
#include "lib/ob_define.h"
namespace oceanbase
{
namespace obrpc
{
class ObExecutorRpcProxy;
}
namespace sql
{
class ObQueryRetryInfo;
/* MySteamHandler生命周期为整个SQL语句, 与ObResultSet相同,本质为栈上变量。参考:obmp_query.cpp */
template <obrpc::ObRpcPacketCode pcode>
class MyStreamHandle
{
public:
typedef typename obrpc::ObExecutorRpcProxy::SSHandle<pcode> MyHandle;
typedef common::ObScanner MyResult;
explicit MyStreamHandle(const char *label, uint64_t tenant_id)
: result_(label, NULL, common::ObScanner::DEFAULT_MAX_SERIALIZE_SIZE, tenant_id),
rc_(common::OB_SUCCESS)
{
}
virtual ~MyStreamHandle() {}
void reset()
{
result_.reset();
rc_ = common::OB_SUCCESS;
}
const common::ObAddr &get_dst_addr() const
{
return handle_.get_dst_addr();
}
MyHandle &get_handle() { return handle_; }
MyResult *get_result()
{
MyResult *ret_result = NULL;
if (!result_.is_inited()) {
SQL_EXE_LOG_RET(ERROR, common::OB_NOT_INIT, "result_ is not inited");
} else {
ret_result = &result_;
}
return ret_result;
}
int reset_and_init_result()
{
int ret = common::OB_SUCCESS;
result_.reset();
if (!result_.is_inited() && OB_FAIL(result_.init())) {
SQL_EXE_LOG(WARN, "fail to init result", K(ret));
}
return ret;
}
void set_result_code(int code) { rc_ = code; }
int get_result_code() { return rc_; }
void set_task_id(const ObTaskID &task_id) { task_id_ = task_id; }
const ObTaskID &get_task_id() const { return task_id_; }
private:
ObTaskID task_id_;
MyHandle handle_;
MyResult result_;
int rc_;
};
typedef MyStreamHandle<obrpc::OB_REMOTE_EXECUTE> RemoteStreamHandle;
typedef MyStreamHandle<obrpc::OB_REMOTE_SYNC_EXECUTE> RemoteStreamHandleV2;
typedef MyStreamHandle<obrpc::OB_TASK_FETCH_RESULT> FetchResultStreamHandle;
class RemoteExecuteStreamHandle
{
public:
RemoteExecuteStreamHandle(const char *label, uint64_t tenant_id) :
use_remote_protocol_v2_(false),
sync_stream_handle_(label, tenant_id),
sync_stream_handle_v2_(label, tenant_id)
{
}
~RemoteExecuteStreamHandle() = default;
void set_use_remote_protocol_v2() { use_remote_protocol_v2_ = true; }
void reset()
{
if (use_remote_protocol_v2_) {
sync_stream_handle_v2_.reset();
} else {
sync_stream_handle_.reset();
}
}
const common::ObAddr &get_dst_addr() const
{
const common::ObAddr *dst_addr = nullptr;
if (use_remote_protocol_v2_) {
dst_addr = &sync_stream_handle_v2_.get_dst_addr();
} else {
dst_addr = &sync_stream_handle_.get_dst_addr();
}
return *dst_addr;
}
int reset_and_init_result()
{
int ret = common::OB_SUCCESS;
if (use_remote_protocol_v2_) {
ret = sync_stream_handle_v2_.reset_and_init_result();
} else {
ret = sync_stream_handle_.reset_and_init_result();
}
return ret;
}
void set_result_code(int code)
{
if (use_remote_protocol_v2_) {
sync_stream_handle_v2_.set_result_code(code);
} else {
sync_stream_handle_.set_result_code(code);
}
}
int get_result_code()
{
int ret = common::OB_SUCCESS;
if (use_remote_protocol_v2_) {
ret = sync_stream_handle_v2_.get_result_code();
} else {
ret = sync_stream_handle_.get_result_code();
}
return ret;
}
void set_task_id(const ObTaskID &task_id)
{
if (use_remote_protocol_v2_) {
sync_stream_handle_v2_.set_task_id(task_id);
} else {
sync_stream_handle_.set_task_id(task_id);
}
}
const ObTaskID &get_task_id() const
{
const ObTaskID *task_id = nullptr;
if (use_remote_protocol_v2_) {
task_id = &(sync_stream_handle_v2_.get_task_id());
} else {
task_id = &(sync_stream_handle_.get_task_id());
}
return *task_id;
}
common::ObScanner *get_result()
{
common::ObScanner *result = nullptr;
if (use_remote_protocol_v2_) {
result = sync_stream_handle_v2_.get_result();
} else {
result = sync_stream_handle_.get_result();
}
return result;
}
bool has_more()
{
bool bret = false;
if (use_remote_protocol_v2_) {
bret = sync_stream_handle_v2_.get_handle().has_more();
} else {
bret = sync_stream_handle_.get_handle().has_more();
}
return bret;
}
int abort()
{
int ret = common::OB_SUCCESS;
if (use_remote_protocol_v2_) {
ret = sync_stream_handle_v2_.get_handle().abort();
} else {
ret = sync_stream_handle_.get_handle().abort();
}
return ret;
}
int get_more(common::ObScanner &result)
{
int ret = common::OB_SUCCESS;
if (use_remote_protocol_v2_) {
ret = sync_stream_handle_v2_.get_handle().get_more(result);
} else {
ret = sync_stream_handle_.get_handle().get_more(result);
}
return ret;
}
RemoteStreamHandle &get_remote_stream_handle() { return sync_stream_handle_; }
RemoteStreamHandleV2 &get_remote_stream_handle_v2() { return sync_stream_handle_v2_; }
private:
bool use_remote_protocol_v2_;
RemoteStreamHandle sync_stream_handle_;
RemoteStreamHandleV2 sync_stream_handle_v2_;
};
class ObExecutorRpcCtx
{
public:
//FIXME qianfu 仅用于兼容,1.4.0之后去掉
static const uint64_t INVALID_CLUSTER_VERSION = 0;
public:
ObExecutorRpcCtx(uint64_t rpc_tenant_id,
int64_t timeout_timestamp,
uint64_t min_cluster_version,
ObQueryRetryInfo *retry_info,
ObSQLSessionInfo *session,
bool is_plain_select,
int32_t group_id)
: rpc_tenant_id_(rpc_tenant_id),
timeout_timestamp_(timeout_timestamp),
min_cluster_version_(min_cluster_version),
retry_info_(retry_info),
session_(session),
is_plain_select_(is_plain_select),
group_id_(group_id)
{
}
~ObExecutorRpcCtx() {}
uint64_t get_rpc_tenant_id() const { return rpc_tenant_id_; }
inline int64_t get_timeout_timestamp() const { return timeout_timestamp_; }
// The timeout provided to the storage layer will be reduced by 100ms
// The timeout here needs to be aligned.
inline int64_t get_ps_timeout_timestamp() const { return timeout_timestamp_ - ESTIMATE_PS_RESERVE_TIME; }
// 等于INVALID_CLUSTER_VERSION说明是从远端的旧observer上序列化过来的
inline bool min_cluster_version_is_valid() const
{
return INVALID_CLUSTER_VERSION != min_cluster_version_;
}
inline uint64_t get_min_cluster_version() const { return min_cluster_version_; }
inline const ObQueryRetryInfo *get_retry_info() const { return retry_info_; }
inline ObQueryRetryInfo *get_retry_info_for_update() const { return retry_info_; }
bool is_retry_for_rpc_timeout() const { return is_plain_select_; }
int check_status() const;
int32_t get_group_id() const { return group_id_; }
TO_STRING_KV(K_(rpc_tenant_id),
K_(timeout_timestamp),
K_(min_cluster_version),
K_(retry_info),
K_(is_plain_select),
K_(group_id));
private:
uint64_t rpc_tenant_id_;
int64_t timeout_timestamp_;
uint64_t min_cluster_version_;
// retry_info_ == NULL表示本次rpc不用给重试模块反馈信息
ObQueryRetryInfo *retry_info_;
const ObSQLSessionInfo *session_;//该类中的变量会并发访问,注意session成功并发访问是否正确
bool is_plain_select_;//stmt_type == T_SELECT && not select...for update
int32_t group_id_;
private:
DISALLOW_COPY_AND_ASSIGN(ObExecutorRpcCtx);
};
#define OB_SQL_REMOTE_TASK_TYPE 1
#define OB_SQL_REMOTE_RESULT_TYPE 2
// 所有调用rpc的地方都要使用to函数,以便支持并发调用
class ObExecutorRpcImpl
{
public:
ObExecutorRpcImpl() : proxy_(NULL), batch_rpc_(nullptr) { }
virtual ~ObExecutorRpcImpl() {}
/*
* 设置rpc proxy
* */
int init(obrpc::ObExecutorRpcProxy *rpc_proxy, obrpc::ObBatchRpc *batch_rpc);
/*
* 发送一个task并阻塞等待,直到对端返回执行状态
* 将执行句柄保存在handler中, 随后可以通过handler收取数据
* */
virtual int task_execute(ObExecutorRpcCtx &rpc_ctx,
ObTask &task,
const common::ObAddr &svr,
RemoteExecuteStreamHandle &handler,
bool &has_sent_task,
bool &has_transfer_err);
virtual int task_execute_v2(ObExecutorRpcCtx &rpc_ctx,
ObRemoteTask &task,
const common::ObAddr &svr,
RemoteExecuteStreamHandle &handler,
bool &has_sent_task,
bool &has_transfer_err);
int remote_task_batch_submit(const uint64_t tenant_id,
const common::ObAddr &server,
const int64_t cluster_id,
const ObRemoteTask &task,
bool &has_sent_task);
int remote_batch_post_result(const uint64_t tenant_id,
const common::ObAddr &server,
const int64_t cluster_id,
const ObRemoteResult &result,
bool &has_sent_result);
/*
* used to terminate remote streaming
* */
virtual int task_kill(
ObExecutorRpcCtx &rpc_ctx,
const ObTaskID &task_id,
const common::ObAddr &svr);
obrpc::ObExecutorRpcProxy *get_proxy() { return proxy_; }
private:
void deal_with_rpc_timeout_err(ObExecutorRpcCtx &rpc_ctx,
int &err,
const common::ObAddr &dist_server) const;
int get_sql_batch_req_type(int64_t execution_id) const;
private:
/* functions */
/* variables */
obrpc::ObExecutorRpcProxy *proxy_;
obrpc::ObBatchRpc *batch_rpc_;
/* other */
DISALLOW_COPY_AND_ASSIGN(ObExecutorRpcImpl);
};
}
}
#endif /* OCEANBASE_SQL_EXECUTOR_RPC_IMPL_ */
//// end of header file