forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_job_control.cpp
More file actions
336 lines (315 loc) · 9.16 KB
/
Copy pathob_job_control.cpp
File metadata and controls
336 lines (315 loc) · 9.16 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
/**
* 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.
*/
#define USING_LOG_PREFIX SQL_EXE
#include "sql/executor/ob_job_control.h"
#include "sql/executor/ob_task_event.h"
#include "lib/utility/ob_tracepoint.h"
#include "sql/engine/ob_exec_context.h"
#include "sql/engine/px/exchange/ob_transmit_op.h"
namespace oceanbase
{
namespace sql
{
using namespace oceanbase::common;
volatile uint64_t ObJobControl::global_job_id_ = 0;
ObJobControl::ObJobControl() : jobs_()
{
}
ObJobControl::~ObJobControl()
{
for (int64_t i = 0; i < jobs_.count(); ++i) {
ObJob *job = jobs_.at(i);
if (NULL != job) {
job->~ObJob();
}
}
}
void ObJobControl::reset()
{
for (int64_t i = 0; i < jobs_.count(); ++i) {
ObJob *job = jobs_.at(i);
if (NULL != job) {
job->reset();
}
}
jobs_.reset();
}
int ObJobControl::all_jobs_finished(bool &is_finished) const
{
int ret = OB_SUCCESS;
bool finished = true;
for (int64_t i = 0; OB_SUCC(ret) && true == finished
&& i < jobs_.count(); ++i) {
ObJob *job = jobs_.at(i);
if (OB_I(t1) OB_ISNULL(job)) {
ret = OB_ERR_UNEXPECTED;
finished = false;
LOG_ERROR("job is NULL", K(i), K(ret));
} else if (OB_JOB_STATE_FINISHED != job->get_state()) {
finished = false;
} else {
//empty
}
}
is_finished = finished;
return ret;
}
int ObJobControl::all_jobs_finished_except_root_job(bool &is_finished) const
{
int ret = OB_SUCCESS;
bool finished = true;
if (jobs_.count() < 1) {
ret = OB_ERR_UNEXPECTED;
finished = false;
LOG_ERROR("count of jobs is less than 1",
K(ret), "job_count", jobs_.count());
} else {
for (int64_t i = 0; OB_SUCC(ret) && true == finished
&& i < jobs_.count() - 1; ++i) {
ObJob *job = jobs_.at(i);
if (OB_I(t1) OB_ISNULL(job)) {
ret = OB_ERR_UNEXPECTED;
finished = false;
LOG_ERROR("job is NULL", K(i), K(ret));
} else if (OB_JOB_STATE_FINISHED != job->get_state()) {
finished = false;
} else {
//empty
}
}
}
is_finished = finished;
return ret;
}
int ObJobControl::create_job(ObIAllocator &allocator,
const ObExecutionID &ob_execution_id,
uint64_t root_op_id,
ObJob *&job) const
{
int ret = OB_SUCCESS;
void *tmp = NULL;
job = NULL;
if (OB_I(t1) OB_ISNULL(tmp = allocator.alloc(sizeof(ObJob)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_ERROR("fail to alloc ObJob", K(ret), K(ob_execution_id));
} else if (OB_I(t2) OB_ISNULL(job = new(tmp) ObJob)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("fail to new ObJob", K(ret), K(ob_execution_id));
} else {
// job_id之所以全局递增,是因为execution_id是从ObIDMap的assign函数中获取的,
// 有可能会重复,所以job_id在本进程内必须不能重复。
ObJobID ob_job_id;
uint64_t job_id = ATOMIC_FAA(&global_job_id_, 1);
ob_job_id.set_ob_execution_id(ob_execution_id);
ob_job_id.set_job_id(job_id);
ob_job_id.set_root_op_id(root_op_id);
job->set_ob_job_id(ob_job_id);
}
return ret;
}
int ObJobControl::find_job_by_job_id(uint64_t job_id, ObJob *&job) const
{
int ret = OB_ENTRY_NOT_EXIST;
for (int64_t i = 0; OB_ENTRY_NOT_EXIST == ret
&& i < jobs_.count(); ++i) {
ObJob *tmp_job = jobs_.at(i);
if (OB_I(t1) OB_ISNULL(tmp_job)) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("job is NULL", K(ret));
} else if (tmp_job->get_job_id() == job_id) {
job = tmp_job;
ret = OB_SUCCESS;
}
}
return ret;
}
int ObJobControl::find_job_by_root_op_id(uint64_t root_op_id, ObJob *&job) const
{
int ret = OB_ENTRY_NOT_EXIST;
for (int64_t i = 0; OB_ENTRY_NOT_EXIST == ret
&& i < jobs_.count(); ++i) {
ObJob *tmp_job = jobs_.at(i);
if (OB_I(t1) OB_ISNULL(tmp_job)) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("job is NULL", K(ret));
} else if (tmp_job->get_root_op_id() == root_op_id) {
job = tmp_job;
ret = OB_SUCCESS;
}
}
return ret;
}
int ObJobControl::get_running_jobs(ObIArray<ObJob *> &jobs) const
{
int ret = OB_SUCCESS;
jobs.reset();
for (int64_t i = 0; OB_SUCC(ret) && i < jobs_.count(); ++i) {
ObJob *job = jobs_.at(i);
if (OB_I(t1) OB_ISNULL(job)) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("job is NULL", K(ret));
} else if (OB_JOB_STATE_RUNNING == job->get_state() && OB_FAIL(jobs.push_back(job))) {
LOG_WARN("fail to push back job", K(ret), K(*job));
}
}
return ret;
}
int ObJobControl::get_all_jobs(ObIArray<ObJob *> &jobs) const
{
int ret = OB_SUCCESS;
jobs.reset();
for (int64_t i = 0; OB_SUCC(ret) && i < jobs_.count(); ++i) {
ObJob *job = jobs_.at(i);
if (OB_I(t1) OB_ISNULL(job)) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("job is NULL", K(ret));
} else if (OB_FAIL(jobs.push_back(job))) {
LOG_WARN("fail to push back job", K(ret), K(*job));
}
}
return ret;
}
int ObJobControl::get_all_jobs_except_root_job(ObIArray<ObJob *> &jobs) const
{
int ret = OB_SUCCESS;
jobs.reset();
for (int64_t i = 0; OB_SUCC(ret) && i < jobs_.count(); ++i) {
ObJob *job = jobs_.at(i);
if (OB_I(t1) OB_ISNULL(job)) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("job is NULL", K(ret));
} else if (!job->is_root_job() && OB_FAIL(jobs.push_back(job))) {
LOG_WARN("fail to push back job", K(ret), K(*job));
}
}
return ret;
}
int ObJobControl::sort_job_scan_part_locs(ObExecContext &ctx)
{
UNUSED(ctx);
return OB_SUCCESS;
}
int ObJobControl::init_job_finish_queue(ObExecContext &ctx)
{
UNUSED(ctx);
return OB_SUCCESS;
}
//int ObJobControl::arrange_jobs()
//{
// return jobs_quick_sort(jobs_, 0, jobs_.count() - 1);
//}
//
//int ObJobControl::jobs_quick_sort(ObIArray<ObJob *> &jobs,
// int64_t low,
// int64_t high)
//{
// int ret = OB_SUCCESS;
// if (low < high) {
// int64_t i = low;
// int64_t j = high;
// ObJob *temp = jobs.at(i);
// if (OB_ISNULL(temp)) {
// ret = OB_ERR_UNEXPECTED;
// LOG_WARN("job is NULL", K(ret), K(i), K(low), K(high));
// }
// while (OB_SUCC(ret) && i < j) {
// while ((jobs.at(j)->get_priority() <= temp->get_priority()) && (i < j)) {
// j--;
// }
// jobs.at(i) = jobs.at(j);
// while ((jobs.at(i)->get_priority() >= temp->get_priority()) && (i < j)) {
// i++;
// }
// jobs.at(j) = jobs.at(i);
// }
// if (OB_SUCC(ret)) {
// jobs.at(i) = temp;
// if (OB_FAIL(jobs_quick_sort(jobs, low, i - 1))) {
// LOG_WARN("fail to jobs quick sort left", K(ret),
// K(low), K(high), K(i), K(j));
// } else if (OB_FAIL(jobs_quick_sort(jobs, j + 1, high))) {
// LOG_WARN("fail to jobs quick sort right", K(ret),
// K(low), K(high), K(i), K(j));
// }
// }
// }
// return ret;
//}
// 如果超过buf_len则会被截断
int ObJobControl::print_status(char *buf, int64_t buf_len,
bool ignore_normal_state/* = false*/) const
{
int ret = OB_SUCCESS;
int64_t pos = 0;
if (OB_FAIL(J_OBJ_START())) {
LOG_WARN("fail to print obj start", K(ret));
} else {
J_KV(N_JOB_COUNT, jobs_.count());
if (OB_FAIL(J_COMMA())) {
LOG_WARN("fail to print comma", K(ret));
}
}
for (int64_t i = 0; OB_SUCC(ret) && i < jobs_.count(); ++i) {
ObJob *job = jobs_.at(i);
if (OB_ISNULL(job)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("job is NULL", K(ret), K(i));
} else if (OB_FAIL(job->print_status(buf, buf_len, pos, ignore_normal_state))) {
LOG_WARN("fail to print job status", K(ret), K(i), K(*job));
} else if (i < jobs_.count() - 1 && OB_FAIL(J_COMMA())) {
LOG_WARN("fail to print comma", K(ret), K(i), K(*job));
}
}
if (OB_FAIL(ret)) {
} else if (OB_FAIL(J_OBJ_END())) {
LOG_WARN("fail to print obj end", K(ret));
}
if (OB_SIZE_OVERFLOW == ret) {
LOG_WARN("buf overflow, truncate it", K(ret), K(buf_len), K(pos));
ret = OB_SUCCESS;
}
return ret;
}
DEF_TO_STRING(ObJobControl)
{
int64_t pos = 0;
J_OBJ_START();
J_NAME(N_JOB_TREE);
J_COLON();
print_job_tree(buf, buf_len, pos, jobs_.at(0));
J_OBJ_END();
return pos;
}
void ObJobControl::print_job_tree(char *buf, const int64_t buf_len, int64_t &pos, ObJob *job) const
{
J_OBJ_START();
J_KV(N_JOB, job);
int64_t child_count = job->get_child_count();
if (child_count > 0) {
J_COMMA();
J_NAME(N_CHILD_JOB);
J_COLON();
J_ARRAY_START();
ObJob *child_job = NULL;
for (int64_t i = 0; i < child_count; i++) {
if (i > 0) {
J_COMMA();
}
(void)job->get_child_job(i, child_job);
print_job_tree(buf, buf_len, pos, child_job);
}
J_ARRAY_END();
}
J_OBJ_END();
}
}
}