forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_mock_utils.cpp
More file actions
415 lines (384 loc) · 13.1 KB
/
Copy pathob_mock_utils.cpp
File metadata and controls
415 lines (384 loc) · 13.1 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
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
/**
* 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.
*/
#include "ob_mock_utils.h"
#include "sql/executor/ob_distributed_scheduler.h"
#include "sql/executor/ob_task_event.h"
#include "sql/executor/ob_interm_result_manager.h"
#include "sql/executor/ob_local_job_executor.h"
#include "sql/executor/ob_local_task_executor.h"
#include "sql/executor/ob_distributed_task_runner.h"
#include "sql/executor/ob_distributed_transmit.h"
#include "sql/engine/ob_physical_plan_ctx.h"
#include "rpc/obrpc/ob_rpc_packet.h"
using namespace oceanbase::common;
using namespace oceanbase::obrpc;
using namespace oceanbase::storage;
using namespace oceanbase::share;
namespace oceanbase
{
namespace sql
{
ObMockSqlExecutorRpc::ObMockSqlExecutorRpc()
{
int ret = OB_SUCCESS;
for (int64_t i = 1; OB_SUCC(ret) && i <= 5; ++i) {
for (int64_t j = 1; OB_SUCC(ret) && j <= 10; ++j) {
ObFakePartitionKey key;
key.table_id_ = i;
key.partition_id_ = j;
ObPartitionLocation location;
ObReplicaLocation replica_loc;
replica_loc.server_.set_ip_addr("127.0.0.1", (int32_t)j);
replica_loc.role_ = LEADER;
if (OB_SUCCESS != (ret = location.add(replica_loc))) {
SQL_ENG_LOG(WARN, "fail to add replica location", K(ret), K(i), K(j));
}
}
}
partition_service_.set_col_num(TEST_MOCK_COL_NUM);
for (int64_t i = 0; OB_SUCC(ret) && i < 100; ++i) {
ObNewRow row;
ObObj objs[TEST_MOCK_COL_NUM];
row.count_ = TEST_MOCK_COL_NUM;
row.cells_ = objs;
for (int64_t j = 0; j < TEST_MOCK_COL_NUM; ++j) {
row.cells_[j].set_int(i);
}
partition_service_.add_row(row);
}
}
ObMockSqlExecutorRpc::~ObMockSqlExecutorRpc()
{
}
/*
* 提交一个异步task执行请求, 在OB_TASK_NOTIFY_FETCH消息的驱动下收取结果数据
*/
int ObMockSqlExecutorRpc::task_submit(
ObExecContext &ctx,
ObTask &task,
const common::ObAddr &svr)
{
UNUSED(ctx);
UNUSED(task);
UNUSED(svr);
int ret = OB_SUCCESS;
const static int64_t S_BUF_SIZE = 100000;
char s_buf[S_BUF_SIZE];
int64_t s_buf_pos = 0;
if (OB_SUCCESS != (ret = task.serialize(s_buf, S_BUF_SIZE, s_buf_pos))) {
SQL_EXE_LOG(WARN, "fail to serialize task", K(ret));
} else {
ObExecContext exec_ctx;
ObMockSqlExecutorRpc rpc;
ObArenaAllocator allocator(ObModIds::OB_SQL_EXEC_CONTEXT);
ObMockRemoteExecuteStreamHandle resp_handler(allocator);
ObTaskExecutorCtx *executor_ctx = exec_ctx.get_task_executor_ctx();
executor_ctx->set_task_executor_rpc(&rpc);
executor_ctx->set_task_response_handler(resp_handler);
executor_ctx->set_server(svr);
executor_ctx->set_partition_service(&partition_service_);
//exec_ctx.set_query_ip_port(0);
ObPhysicalPlan *phy_plan = ObPhysicalPlan::alloc();
//ObPhyOperatorFactory *phy_op_factory = new ObPhyOperatorFactory();
//phy_plan->set_operator_factory(phy_op_factory);
ObTask new_task;
new_task.set_deserialize_param(exec_ctx, *phy_plan);
int64_t new_pos = 0;
if (OB_SUCCESS != (ret = new_task.deserialize(s_buf, s_buf_pos, new_pos))) {
SQL_EXE_LOG(WARN, "fail to deserialize", K(ret));
} else {
//执行plan
ObPhyOperator *root_op = phy_plan->get_main_query();
ObDistributedTransmitInput *trans_input = dynamic_cast<ObDistributedTransmitInput *>(ctx.get_phy_op_input(root_op->get_id()));
OB_ASSERT(NULL != trans_input);
ObSliceID sid = trans_input->get_ob_slice_id();
ObDistributedTaskRunner task_runner;
if (OB_SUCCESS != (ret = task_runner.execute(exec_ctx, *phy_plan))) {
SQL_EXE_LOG(WARN, "fail execute task", K(sid));
}
//返回task event
ObTaskEvent *task_event = new ObTaskEvent();
ObTaskLocation task_loc;
//ObAddr remote_server;
//remote_server.set_ip_addr("127.0.0.1", 9999);
task_loc.set_server(task.remote_server_);
task_loc.set_ob_task_id(sid.get_ob_task_id());
task_event->init(task_loc, ret);
if (OB_SUCCESS != (ret = ObMockPacketQueueThread::get_instance()->packet_queue_.push(task_event))) {
SQL_EXE_LOG(WARN, "fail to push packet into queue", K(ret));
} else {
SQL_EXE_LOG(INFO, "succeed to push packet into queue", "task_event", to_cstring(*task_event));
}
}
}
return ret;
}
/*
* 发送一个task并阻塞等待,直到对端返回执行状态
* 将执行句柄保存在handler中, 随后可以通过handler收取数据
* */
int ObMockSqlExecutorRpc::task_execute(
ObExecContext &ctx,
ObTask &task,
const common::ObAddr &svr,
RemoteExecuteStreamHandle &handler)
{
UNUSED(task);
UNUSED(svr);
UNUSED(handler);
ObMockSqlExecutorRpc rpc;
ObArenaAllocator allocator(ObModIds::OB_SQL_EXEC_CONTEXT);
ObMockRemoteExecuteStreamHandle resp_handler(allocator);
ObTaskExecutorCtx *executor_ctx = ctx.get_task_executor_ctx();
executor_ctx->set_task_executor_rpc(&rpc);
executor_ctx->set_task_response_handler(resp_handler);
executor_ctx->set_server(svr);
executor_ctx->set_partition_service(&partition_service_);
SQL_EXE_LOG(INFO, "task_execute");
return OB_SUCCESS;
}
/*
* 发送杀死一个task的命令并阻塞等待对端返回执行状态
* */
int ObMockSqlExecutorRpc::task_kill(
ObTaskInfo &task,
const common::ObAddr &svr)
{
UNUSED(task);
UNUSED(svr);
return OB_SUCCESS;
}
/*
* Task在Worker端执行完成,通知Scheduler启动Task读取结果
* */
int ObMockSqlExecutorRpc::task_complete(
ObTaskEvent &task_event,
const common::ObAddr &svr)
{
UNUSED(task_event);
UNUSED(svr);
return OB_SUCCESS;
}
/*
* 发送一个task的执行结果,不等待返回
* */
int ObMockSqlExecutorRpc::task_notify_fetch(
ObTaskEvent &task_event,
const common::ObAddr &svr)
{
UNUSED(task_event);
UNUSED(svr);
return OB_SUCCESS;
}
/*
* 获取一个task的中间结果的所有scanner,阻塞等待直到所有的scanner都返回
* */
int ObMockSqlExecutorRpc::task_fetch_result(
const ObSliceID &ob_slice_id,
const common::ObAddr &svr,
FetchResultStreamHandle &handler)
{
int ret = OB_SUCCESS;
ObScanner &scanner = *handler.get_result();
ObObj tmp_objs[TEST_MOCK_COL_NUM];
oceanbase::common::ObNewRow tmp_row;
tmp_row.count_ = TEST_MOCK_COL_NUM;
tmp_row.cells_ = tmp_objs;
ObIntermResultIterator iter;
ObIntermResultManager *ir_mgr = ObIntermResultManager::get_instance();
assert(NULL != ir_mgr);
ObTaskLocation task_loc;
task_loc.set_server(svr);
task_loc.set_ob_task_id(ob_slice_id.get_ob_task_id());
ObIntermResultInfo ir_info;
ir_info.init(ob_slice_id);
if (OB_SUCCESS != (ret = ir_mgr->get_result(ir_info, iter))) {
SQL_EXE_LOG(WARN, "fail to get interm result iterator", K(ret),
"ob_slice_id", to_cstring(ob_slice_id));
} else if (task_location_exist(task_loc)) {
//FIXME 暂时做成这样,第二次访问同一个location时返回OB_ITER_END
ret = OB_ITER_END;
} else {
//FIXME 暂时只弄一个scanner,第二次访问同一个location返回OB_ITER_END
while (OB_SUCC(ret)) {
if (OB_SUCCESS != (ret = iter.get_next_row(tmp_row))) {
if (OB_ITER_END != ret) {
SQL_EXE_LOG(WARN, "fail to get next row", K(ret));
} else {
SQL_EXE_LOG(DEBUG, "iter has no more rows");
}
} else if (OB_SUCCESS != (ret = scanner.add_row(tmp_row))) {
SQL_EXE_LOG(WARN, "fail to add row to scanner", K(ret));
} else {
SQL_EXE_LOG(DEBUG, "success to add row to scanner");
}
}
if (OB_ITER_END == ret) {
if (OB_SUCCESS != (ret = task_loc_array_.push_back(task_loc))) {
SQL_EXE_LOG(WARN, "fail to push back to task location array", K(ret));
} else{
ret = OB_SUCCESS;
SQL_EXE_LOG(DEBUG, "get a scanner",
"job_id", task_loc.get_job_id(),
"task_id", task_loc.get_task_id(),
"row_count", scanner.get_row_count());
}
}
}
SQL_EXE_LOG(INFO, "get interm result", K(ret), K(svr), K(ob_slice_id));
return ret;
}
bool ObMockSqlExecutorRpc::task_location_exist(ObTaskLocation task_loc)
{
bool loc_exist = false;
for (int64_t i = 0; i < task_loc_array_.count(); ++i) {
if (task_loc_array_.at(i).get_job_id() == task_loc.get_job_id()
&& task_loc_array_.at(i).get_task_id() == task_loc.get_task_id()) {
loc_exist = true;
break;
}
}
return loc_exist;
}
bool ObMockFetchResultStreamHandle::has_more()
{
return false;
}
int ObMockRemoteExecuteStreamHandle::get_more(ObScanner &scanner)
{
int ret = OB_SUCCESS;
static const int64_t col_num = TEST_MOCK_COL_NUM;
for (int64_t i = 0; i < 1000; ++i) {
ObNewRow row;
ObObj objs[col_num];
row.count_ = col_num;
row.cells_ = objs;
for (int64_t j = 0; j < col_num; ++j) {
row.cells_[j].set_int(i);
}
if (OB_SUCCESS != (ret = scanner.add_row(row))) {
SQL_EXE_LOG(ERROR, "fail to add row", K(i), K(ret));
}
}
return ret;
}
bool ObMockRemoteExecuteStreamHandle::has_more()
{
return false;
}
int ObMockFetchResultStreamHandle::get_more(ObScanner &scanner)
{
int ret = OB_SUCCESS;
ObObj tmp_objs[TEST_MOCK_COL_NUM];
oceanbase::common::ObNewRow tmp_row;
tmp_row.count_ = TEST_MOCK_COL_NUM;
tmp_row.cells_ = tmp_objs;
ObIntermResultIterator iter;
ObIntermResultManager *ir_mgr = ObIntermResultManager::get_instance();
assert(NULL != ir_mgr);
ObTaskLocation task_loc;
task_loc.set_server(server_);
task_loc.set_ob_task_id(ob_slice_id_.get_ob_task_id());
ObIntermResultInfo ir_info;
ir_info.init(ob_slice_id_);
if (OB_SUCCESS != (ret = ir_mgr->get_result(ir_info, iter))) {
SQL_EXE_LOG(WARN, "fail to get interm result iterator", K(ret),
"ob_slice_id", to_cstring(ob_slice_id_));
} else if (task_location_exist(task_loc)) {
//FIXME 暂时做成这样,第二次访问同一个location时返回OB_ITER_END
ret = OB_ITER_END;
} else {
//FIXME 暂时只弄一个scanner,第二次访问同一个location返回OB_ITER_END
while (OB_SUCC(ret)) {
if (OB_SUCCESS != (ret = iter.get_next_row(tmp_row))) {
if (OB_ITER_END != ret) {
SQL_EXE_LOG(WARN, "fail to get next row", K(ret));
} else {
SQL_EXE_LOG(DEBUG, "iter has no more rows");
}
} else if (OB_SUCCESS != (ret = scanner.add_row(tmp_row))) {
SQL_EXE_LOG(WARN, "fail to add row to scanner", K(ret));
} else {
SQL_EXE_LOG(DEBUG, "success to add row to scanner");
}
}
if (OB_ITER_END == ret) {
if (OB_SUCCESS != (ret = task_loc_array_.push_back(task_loc))) {
SQL_EXE_LOG(WARN, "fail to push back to task location array", K(ret));
} else{
ret = OB_SUCCESS;
SQL_EXE_LOG(DEBUG, "get a scanner",
"job_id", task_loc.get_job_id(),
"task_id", task_loc.get_task_id(),
"row_count", scanner.get_row_count());
}
}
}
SQL_EXE_LOG(INFO, "get interm result", K(ret), K_(server), K_(ob_slice_id));
return ret;
}
bool ObMockFetchResultStreamHandle::task_location_exist(ObTaskLocation task_loc)
{
bool loc_exist = false;
for (int64_t i = 0; i < task_loc_array_.count(); ++i) {
if (task_loc_array_.at(i).get_job_id() == task_loc.get_job_id()
&& task_loc_array_.at(i).get_task_id() == task_loc.get_task_id()) {
loc_exist = true;
break;
}
}
return loc_exist;
}
ObMockPacketQueueThread *ObMockPacketQueueThread::instance_ = NULL;
obutil::Mutex ObMockPacketQueueThread::locker_;
ObMockPacketQueueThread::ObMockPacketQueueThread()
{
packet_queue_.init(8192, common::ObModIds::OB_SQL_EXECUTOR_TASK_EVENT);
set_thread_count(THREAD_COUNT);
}
ObMockPacketQueueThread *ObMockPacketQueueThread::get_instance()
{
if (NULL == instance_) {
locker_.lock();
if (NULL == instance_) {
instance_ = new ObMockPacketQueueThread();
if (NULL == instance_) {
SQL_EXE_LOG(ERROR, "instance is NULL, unexpected");
} else {
//empty
}
}
locker_.unlock();
}
return instance_;
}
void ObMockPacketQueueThread::run(obsys::CThread *thread, void *arg)
{
SQL_EXE_LOG(INFO, "mock packet queue is running...");
UNUSED(arg);
int ret = OB_SUCCESS;
int64_t timeout_us = INT64_MAX;
void *msg = NULL;
ObDistributedSchedulerManager *sc_manager = ObDistributedSchedulerManager::get_instance();
while(OB_SUCCESS == (ret = ObMockPacketQueueThread::get_instance()->packet_queue_.pop(timeout_us, msg))) {
ObTaskEvent *packet = reinterpret_cast<ObTaskEvent*>(msg);
if (NULL == packet) {
SQL_EXE_LOG(ERROR, "packet is NULL");
} else if (OB_SUCCESS != (ret = sc_manager->signal_scheduler(*packet))) {
SQL_EXE_LOG(WARN, "fail to signal scheduler", K(ret));
} else {
//empty
}
}
}
}/* ns sql*/
}/* ns oceanbase */