forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_job_control.cpp
More file actions
216 lines (196 loc) · 5.85 KB
/
Copy pathob_job_control.cpp
File metadata and controls
216 lines (196 loc) · 5.85 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
/*
* Copyright (c) 2025 OceanBase.
*
* 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.
*/
#define USING_LOG_PREFIX SQL_EXE
#include "sql/executor/ob_job_control.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();
}
}
}
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 is globally incremented because execution_id is obtained from the assign function of ObIDMap,
// There might be duplicates, so job_id must not be duplicated within this process.
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::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::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;
//}
// If it exceeds buf_len it will be truncated
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();
}
}
}