forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_job_control.cpp
More file actions
385 lines (350 loc) · 13.9 KB
/
Copy pathtest_job_control.cpp
File metadata and controls
385 lines (350 loc) · 13.9 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
/**
* 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_root_transmit.h"
#include "sql/executor/ob_distributed_transmit.h"
#include "sql/executor/ob_direct_receive.h"
#include "sql/executor/ob_fifo_receive.h"
#include "sql/executor/ob_distributed_job_control.h"
#include "sql/executor/ob_job_parser.h"
#include "sql/executor/ob_task_spliter_factory.h"
#include "sql/engine/ob_physical_plan_ctx.h"
#include "sql/engine/ob_physical_plan.h"
#include "sql/executor/ob_local_job_control.h"
#include "sql/executor/ob_remote_job_control.h"
#include "sql/executor/ob_distributed_job_control.h"
#include "sql/executor/ob_job.h"
#include "sql/engine/table/ob_table_scan.h"
#include "sql/ob_sql_init.h"
#include "lib/utility/ob_tracepoint.h"
#include <gtest/gtest.h>
using namespace oceanbase;
using namespace oceanbase::common;
using namespace oceanbase::sql;
using namespace oceanbase::storage;
using namespace oceanbase::share;
class ObJobControlTest : public ::testing::Test
{
public:
ObJobControlTest();
virtual ~ObJobControlTest();
virtual void SetUp();
virtual void TearDown();
ObPhysicalPlan *phy_plan_;
int create_plan_tree(ObExecContext &ctx);
void build_input_test(int exepected_ret, ObJobControl &jc);
void exception_test(int expected_ret, ObJobControl &jc);
void valid_test(ObJobControl &jc);
private:
// disallow copy
ObJobControlTest(const ObJobControlTest &other);
ObJobControlTest& operator=(const ObJobControlTest &other);
private:
// data members
};
ObJobControlTest::ObJobControlTest()
{
}
ObJobControlTest::~ObJobControlTest()
{
}
void ObJobControlTest::SetUp()
{
}
void ObJobControlTest::TearDown()
{
}
int ObJobControlTest::create_plan_tree(ObExecContext &ctx)
{
UNUSED(ctx);
const static int64_t TEST_PARA_DEGREE = 2;
int ret = OB_SUCCESS;
phy_plan_ = ObPhysicalPlan::alloc();
ObPhyOperator *cur_op = NULL;
ObPhyOperator *tmp_op = NULL;
int err_code = OB_SUCCESS;
/*
* calculate c0 % TEST_PARA_DEGREE
* */
ObSqlExpression *hash_expr = NULL;
EXPECT_EQ(OB_SUCCESS, ObSqlExpressionUtil::make_sql_expr(phy_plan_, hash_expr));
EXPECT_FALSE(NULL == hash_expr);
ObPostExprItem expr_item;
expr_item.set_column(0);
EXPECT_EQ(OB_SUCCESS, hash_expr->add_expr_item(expr_item));
expr_item.set_int(TEST_PARA_DEGREE);
expr_item.set_item_type(T_INT);
EXPECT_EQ(OB_SUCCESS, hash_expr->add_expr_item(expr_item));
expr_item.set_op("%", 2);
EXPECT_EQ(OB_SUCCESS, hash_expr->add_expr_item(expr_item));
/*
* calculate c0 % 1
* */
ObSqlExpression *iden_hash_expr = NULL;
EXPECT_EQ(OB_SUCCESS, ObSqlExpressionUtil::make_sql_expr(phy_plan_, iden_hash_expr));
EXPECT_FALSE(NULL == iden_hash_expr);
ObPostExprItem iden_expr_item;
iden_expr_item.set_column(0);
EXPECT_EQ(OB_SUCCESS, iden_hash_expr->add_expr_item(iden_expr_item));
iden_expr_item.set_int(1);
iden_expr_item.set_item_type(T_INT);
EXPECT_EQ(OB_SUCCESS, iden_hash_expr->add_expr_item(iden_expr_item));
iden_expr_item.set_op("%", 2);
EXPECT_EQ(OB_SUCCESS, iden_hash_expr->add_expr_item(iden_expr_item));
TEST_CREATE_PHY_OPERATOR(tmp_op, ObTableScan, PHY_TABLE_SCAN, phy_plan_, err_code);
tmp_op->set_column_count(TEST_MOCK_COL_NUM);
cur_op = tmp_op;
SQL_EXE_LOG(INFO, "op info", "op_id", cur_op->get_id(), "op_type", cur_op->get_type());
TEST_CREATE_PHY_OPERATOR(tmp_op, ObDistributedTransmit, PHY_DISTRIBUTED_TRANSMIT, phy_plan_, err_code);
tmp_op->set_column_count(TEST_MOCK_COL_NUM);
tmp_op->set_child(0, *cur_op);
static_cast<ObTransmit*>(tmp_op)->get_job_conf().set_task_split_type(ObTaskSpliter::IDENTITY_SPLIT);
//static_cast<ObTransmit*>(tmp_op)->set_parallel_degree(TEST_PARA_DEGREE);
static_cast<ObTransmit*>(tmp_op)->set_shuffle_func(hash_expr);
cur_op = tmp_op;
SQL_EXE_LOG(INFO, "op info", "op_id", cur_op->get_id(), "op_type", cur_op->get_type());
TEST_CREATE_PHY_OPERATOR(tmp_op, ObFifoReceive, PHY_FIFO_RECEIVE, phy_plan_, err_code);
tmp_op->set_column_count(TEST_MOCK_COL_NUM);
tmp_op->set_child(0, *cur_op);
cur_op = tmp_op;
SQL_EXE_LOG(INFO, "op info", "op_id", cur_op->get_id(), "op_type", cur_op->get_type());
TEST_CREATE_PHY_OPERATOR(tmp_op, ObDistributedTransmit, PHY_DISTRIBUTED_TRANSMIT, phy_plan_, err_code);
tmp_op->set_column_count(TEST_MOCK_COL_NUM);
tmp_op->set_child(0, *cur_op);
static_cast<ObTransmit*>(tmp_op)->get_job_conf().set_task_split_type(ObTaskSpliter::INTERM_SPLIT);
//static_cast<ObTransmit*>(tmp_op)->set_parallel_degree(TEST_PARA_DEGREE);
static_cast<ObTransmit*>(tmp_op)->set_shuffle_func(iden_hash_expr);
cur_op = tmp_op;
SQL_EXE_LOG(INFO, "op info", "op_id", cur_op->get_id(), "op_type", cur_op->get_type());
TEST_CREATE_PHY_OPERATOR(tmp_op, ObFifoReceive, PHY_FIFO_RECEIVE, phy_plan_, err_code);
tmp_op->set_column_count(TEST_MOCK_COL_NUM);
tmp_op->set_child(0, *cur_op);
cur_op = tmp_op;
SQL_EXE_LOG(INFO, "op info", "op_id", cur_op->get_id(), "op_type", cur_op->get_type());
TEST_CREATE_PHY_OPERATOR(tmp_op, ObRootTransmit, PHY_ROOT_TRANSMIT, phy_plan_, err_code);
tmp_op->set_column_count(TEST_MOCK_COL_NUM);
tmp_op->set_child(0, *cur_op);
static_cast<ObTransmit*>(tmp_op)->get_job_conf().set_task_split_type(ObTaskSpliter::IDENTITY_SPLIT);
//static_cast<ObTransmit*>(tmp_op)->set_parallel_degree(TEST_PARA_DEGREE);
static_cast<ObTransmit*>(tmp_op)->set_shuffle_func(hash_expr);
cur_op = tmp_op;
SQL_EXE_LOG(INFO, "op info", "op_id", cur_op->get_id(), "op_type", cur_op->get_type());
phy_plan_->set_main_query(cur_op);
return ret;
}
void ObJobControlTest::build_input_test(int expected_ret, ObJobControl &jc) {
int ret = OB_SUCCESS;
ObAddrsProviderFactory spf;
ObTaskSpliterFactory tsf;
ObJobParser parser;
ObExecContext exec_ctx;
exec_ctx.init_phy_op(100);
exec_ctx.create_physical_plan_ctx();
create_plan_tree(exec_ctx);
if (OB_FAIL(jc.build_jobs_ctx(exec_ctx))) {
//empty
} else if (OB_FAIL(parser.parse_job(exec_ctx, phy_plan_, 1, tsf, spf, jc))) {
//empty
}
ASSERT_EQ(expected_ret, ret);
}
void ObJobControlTest::exception_test(int expected_ret, ObJobControl &jc)
{
int ret = OB_SUCCESS;
ObJob *job1 = NULL;
ObJob *job2 = NULL;
ObJob *job3 = NULL;
ObJob *tmp_job = NULL;
ObArray<ObJob *> jobs;
ObArray<ObJob *> jobs1;
ObArray<ObJob *> jobs2;
ObArenaAllocator allocator(ObModIds::OB_SQL_EXEC_CONTEXT);
int64_t query_id = OB_INVALID_ID;
if (OB_FAIL(jc.create_job(allocator, query_id, job1))) {
//empty
} else if (OB_FAIL(jc.create_job(allocator, query_id, job2))) {
//empty
} else if (OB_FAIL(jc.create_job(allocator, query_id, job3))) {
//empty
} else {
job1->set_priority(1);
job2->set_priority(2);
job3->set_priority(3);
if (OB_FAIL(jc.add_job(job1))) {
//empty
} else if (OB_FAIL(jc.add_job(job2))) {
//empty
} else if (OB_FAIL(jc.add_job(job3))) {
//empty
} else if (OB_FAIL(jc.find_job(job1->get_job_id(), tmp_job))) {
//empty
} else if (OB_FAIL(jc.get_all_jobs(jobs))) {
//empty
} else {
OB_ASSERT(3 == jobs.count());
OB_ASSERT(1 == jobs.at(0)->get_priority());
OB_ASSERT(2 == jobs.at(1)->get_priority());
OB_ASSERT(3 == jobs.at(2)->get_priority());
if (OB_FAIL(jc.arrange_jobs())) {
//empty
} else if (OB_FAIL(jc.get_all_jobs(jobs1))) {
//empty
} else {
bool b1 = true;
bool b2 = true;
OB_ASSERT(3 == jobs1.count());
OB_ASSERT(3 == jobs1.at(0)->get_priority());
OB_ASSERT(2 == jobs1.at(1)->get_priority());
OB_ASSERT(1 == jobs1.at(2)->get_priority());
if(OB_FAIL(jc.all_jobs_finished(b1))) {
//empty
} else if (OB_FAIL(jc.all_jobs_finished_except_root_job(b2))) {
//empty
} else {
OB_ASSERT(false == b1);
OB_ASSERT(false == b2);
if (OB_FAIL(jc.get_running_jobs(jobs2))) {
//empty
} else {
OB_ASSERT(0 == jobs2.count());
jobs1.at(0)->set_state(OB_JOB_STATE_RUNNING);
OB_ASSERT(OB_SUCCESS == jc.all_jobs_finished(b1));
OB_ASSERT(OB_SUCCESS == jc.all_jobs_finished_except_root_job(b2));
OB_ASSERT(false == b1);
OB_ASSERT(false == b2);
jobs2.reset();
if (OB_FAIL(jc.get_running_jobs(jobs2))) {
//empty
} else {
OB_ASSERT(1 == jobs2.count());
OB_ASSERT(3 == jobs2.at(0)->get_priority());
jobs1.at(1)->set_state(OB_JOB_STATE_RUNNING);
OB_ASSERT(OB_SUCCESS == jc.all_jobs_finished(b1));
OB_ASSERT(OB_SUCCESS == jc.all_jobs_finished_except_root_job(b2));
OB_ASSERT(false == b1);
OB_ASSERT(false == b2);
jobs2.reset();
if (OB_FAIL(jc.get_running_jobs(jobs2))) {
//empty
} else {
OB_ASSERT(2 == jobs2.count());
OB_ASSERT(3 == jobs2.at(0)->get_priority());
OB_ASSERT(2 == jobs2.at(1)->get_priority());
jobs1.at(2)->set_state(OB_JOB_STATE_RUNNING);
OB_ASSERT(OB_SUCCESS == jc.all_jobs_finished(b1));
OB_ASSERT(OB_SUCCESS == jc.all_jobs_finished_except_root_job(b2));
OB_ASSERT(false == b1);
OB_ASSERT(false == b2);
jobs2.reset();
if (OB_FAIL(jc.get_running_jobs(jobs2))) {
//empty
} else {
OB_ASSERT(3 == jobs2.count());
OB_ASSERT(3 == jobs2.at(0)->get_priority());
OB_ASSERT(2 == jobs2.at(1)->get_priority());
OB_ASSERT(1 == jobs2.at(2)->get_priority());
jobs1.at(0)->set_state(OB_JOB_STATE_FINISHED);
jobs1.at(1)->set_state(OB_JOB_STATE_FINISHED);
OB_ASSERT(OB_SUCCESS == jc.all_jobs_finished(b1));
OB_ASSERT(OB_SUCCESS == jc.all_jobs_finished_except_root_job(b2));
OB_ASSERT(false == b1);
OB_ASSERT(true == b2);
jobs1.at(2)->set_state(OB_JOB_STATE_FINISHED);
OB_ASSERT(OB_SUCCESS == jc.all_jobs_finished(b1));
OB_ASSERT(OB_SUCCESS == jc.all_jobs_finished_except_root_job(b2));
OB_ASSERT(true == b1);
OB_ASSERT(true == b2);
}
}
}
}
}
}
}
}
ASSERT_EQ(expected_ret, ret);
}
void ObJobControlTest::valid_test(ObJobControl &jc)
{
ObJob *job = NULL;
ObJob *job2 = NULL;
bool b = true;
ObArenaAllocator allocator(ObModIds::OB_SQL_EXEC_CONTEXT);
int64_t query_id = OB_INVALID_ID;
OB_ASSERT(OB_SUCCESS == jc.create_job(allocator, query_id, job));
job->set_root_job();
OB_ASSERT(true == job->is_root_job());
OB_ASSERT(OB_ENTRY_NOT_EXIST == jc.find_job(100, job2));
OB_ASSERT(NULL == job2);
OB_ASSERT(OB_ENTRY_NOT_EXIST == jc.find_job(job->get_job_id(), job2));
OB_ASSERT(OB_ERR_UNEXPECTED == jc.all_jobs_finished_except_root_job(b));
OB_ASSERT(OB_SUCCESS == jc.add_job(job));
OB_ASSERT(OB_SUCCESS == jc.find_job(job->get_job_id(), job2));
OB_ASSERT(job2->get_job_id() == job->get_job_id());
OB_ASSERT(true == job2->is_root_job());
OB_ASSERT(OB_SUCCESS == jc.all_jobs_finished_except_root_job(b));
}
TEST_F(ObJobControlTest, valid_test)
{
ObLocalJobControl local_jc;
ObLocalJobControl remote_jc;
ObLocalJobControl distributed_jc;
valid_test(local_jc);
valid_test(remote_jc);
valid_test(distributed_jc);
}
TEST_F(ObJobControlTest, build_input_test)
{
ObLocalJobControl local_jc;
ObLocalJobControl remote_jc;
ObLocalJobControl distributed_jc;
build_input_test(OB_SUCCESS, local_jc);
build_input_test(OB_SUCCESS, remote_jc);
build_input_test(OB_SUCCESS, distributed_jc);
}
TEST_F(ObJobControlTest, basic_test)
{
ObLocalJobControl local_jc;
ObLocalJobControl remote_jc;
ObLocalJobControl distributed_jc;
exception_test(OB_SUCCESS, local_jc);
exception_test(OB_SUCCESS, remote_jc);
exception_test(OB_SUCCESS, distributed_jc);
}
#define EXCEPTION_TEST(test_func, test_name, func, key, err, expect_ret) \
TEST_F(ObJobControlTest, test_name) \
{\
ObLocalJobControl local_jc; \
ObLocalJobControl remote_jc; \
ObLocalJobControl distributed_jc; \
TP_SET_ERROR("executor/ob_job_control.cpp", func, key, err); \
test_func(expect_ret, local_jc); \
test_func(expect_ret, remote_jc); \
test_func(expect_ret, distributed_jc); \
TP_SET_ERROR("executor/ob_job_control.cpp", func, key, NULL); \
}\
EXCEPTION_TEST(exception_test, et1, "all_jobs_finished", "t1", 1, OB_ERR_UNEXPECTED);
EXCEPTION_TEST(exception_test, et2, "all_jobs_finished_except_root_job", "t1", 1, OB_ERR_UNEXPECTED);
EXCEPTION_TEST(exception_test, et3, "find_job", "t1", 1, OB_ERR_UNEXPECTED);
EXCEPTION_TEST(exception_test, et4, "get_running_jobs", "t1", 1, OB_ERR_UNEXPECTED);
EXCEPTION_TEST(exception_test, et5, "get_all_jobs", "t1", 1, OB_ERR_UNEXPECTED);
EXCEPTION_TEST(exception_test, et6, "create_job", "t1", 1, OB_ALLOCATE_MEMORY_FAILED);
EXCEPTION_TEST(exception_test, et7, "create_job", "t2", 1, OB_ERR_UNEXPECTED);
//EXCEPTION_TEST(build_input_test, et8, "build_job_ctx", "t1", 1, OB_ERR_UNEXPECTED);
EXCEPTION_TEST(build_input_test, et9, "build_job_ctx", "t2", 1, OB_ERR_UNEXPECTED);
int main(int argc, char **argv)
{
init_sql_factories();
oceanbase::common::ObLogger::get_logger().set_log_level("INFO");
::testing::InitGoogleTest(&argc,argv);
return RUN_ALL_TESTS();
}