forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_ddl_task_executor.cpp
More file actions
344 lines (318 loc) · 9.9 KB
/
Copy pathob_ddl_task_executor.cpp
File metadata and controls
344 lines (318 loc) · 9.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
/**
* 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 <sys/prctl.h>
#include "lib/thread/ob_async_task_queue.h"
#include "ob_ddl_task_executor.h"
#include "share/ob_thread_mgr.h"
#define USING_LOG_PREFIX STORAGE
using namespace oceanbase::share;
using namespace oceanbase::common;
namespace oceanbase
{
namespace share
{
ObDDLTaskQueue::ObDDLTaskQueue()
: task_list_(), task_set_(), lock_(ObLatchIds::DDL_LOCK), is_inited_(false), allocator_()
{
allocator_.set_label(common::ObModIds::OB_BUILD_INDEX_SCHEDULER);
}
ObDDLTaskQueue::~ObDDLTaskQueue()
{
}
void ObDDLTaskQueue::destroy()
{
int ret = OB_SUCCESS;
ObIDDLTask *task = NULL;
common::ObSpinLockGuard guard(lock_);
while (OB_SUCC(ret) && task_list_.get_size() > 0) {
if (OB_ISNULL(task = task_list_.remove_first())) {
ret = common::OB_ERR_UNEXPECTED;
STORAGE_LOG(WARN, "fail to remove first task", K(ret));
} else {
allocator_.free(task);
task = NULL;
}
}
task_set_.destroy();
allocator_.reset();
is_inited_ = false;
}
int ObDDLTaskQueue::init(const int64_t bucket_num, const int64_t total_mem_limit,
const int64_t hold_mem_limit, const int64_t page_size)
{
int ret = OB_SUCCESS;
common::ObSpinLockGuard guard(lock_);
if (bucket_num <= 0 || total_mem_limit <= 0
|| hold_mem_limit <= 0 || page_size <= 0) {
ret = common::OB_INVALID_ARGUMENT;
STORAGE_LOG(WARN, "invalid argument", K(ret), K(bucket_num), K(total_mem_limit),
K(hold_mem_limit), K(page_size));
} else if (OB_FAIL(task_set_.create(bucket_num))) {
STORAGE_LOG(WARN, "fail to create task set", K(ret), K(bucket_num));
} else if (OB_FAIL(allocator_.init(total_mem_limit, hold_mem_limit, page_size))) {
STORAGE_LOG(WARN, "fail to init allocator", K(ret));
} else {
is_inited_ = true;
}
return ret;
}
int ObDDLTaskQueue::push_task(const ObIDDLTask &task)
{
int ret = OB_SUCCESS;
ObIDDLTask *task_copy = NULL;
char *buf = NULL;
bool task_add_to_list = false;
common::ObSpinLockGuard guard(lock_);
const int64_t deep_copy_size = task.get_deep_copy_size();
if (OB_UNLIKELY(!is_inited_)) {
ret = common::OB_NOT_INIT;
STORAGE_LOG(WARN, "ObBuildIndexTaskQueue has not been inited", K(ret));
} else if (OB_ISNULL(buf = static_cast<char *>(allocator_.alloc(deep_copy_size)))) {
ret = common::OB_ALLOCATE_MEMORY_FAILED;
STORAGE_LOG(WARN, "fail to allocate memory for ObBuildIndexTask", K(ret));
} else if (OB_ISNULL(task_copy = task.deep_copy(buf, deep_copy_size))) {
ret = common::OB_ALLOCATE_MEMORY_FAILED;
STORAGE_LOG(WARN, "fail to deep copy task", K(ret));
} else if (!task_list_.add_last(task_copy)) {
ret = common::OB_ERR_UNEXPECTED;
STORAGE_LOG(ERROR, "unexpected error, add build index task failed", K(ret));
} else {
int is_overwrite = 0; // do not overwrite
task_add_to_list = true;
if (OB_FAIL(task_set_.set_refactored(task_copy, is_overwrite))) {
if (common::OB_HASH_EXIST == ret) {
ret = common::OB_ENTRY_EXIST;
} else {
STORAGE_LOG(WARN, "fail to set task to task set", K(ret));
}
} else {
STORAGE_LOG(INFO, "add task", K(*task_copy), KP(task_copy), K(common::lbt()));
}
}
if (OB_FAIL(ret) && NULL != buf) {
if (task_add_to_list) {
int tmp_ret = OB_SUCCESS;
if (!task_list_.remove(task_copy)) {
tmp_ret = common::OB_ERR_UNEXPECTED;
STORAGE_LOG(WARN, "fail to remove task", K(tmp_ret), K(*task_copy));
}
}
allocator_.free(buf);
buf = NULL;
task_copy = NULL;
}
return ret;
}
int ObDDLTaskQueue::get_next_task(ObIDDLTask *&task)
{
int ret = OB_SUCCESS;
common::ObSpinLockGuard guard(lock_);
if (OB_UNLIKELY(!is_inited_)) {
ret = common::OB_NOT_INIT;
STORAGE_LOG(WARN, "ObBuildIndexTaskQueue has not been inited", K(ret));
} else if (0 == task_list_.get_size()) {
ret = common::OB_EAGAIN;
} else if (OB_ISNULL(task = task_list_.remove_first())) {
ret = common::OB_ERR_UNEXPECTED;
STORAGE_LOG(WARN, "error unexpected, task must not be NULL", K(ret));
}
return ret;
}
int ObDDLTaskQueue::remove_task(ObIDDLTask *task)
{
int ret = OB_SUCCESS;
common::ObSpinLockGuard guard(lock_);
if (OB_UNLIKELY(!is_inited_)) {
ret = common::OB_NOT_INIT;
STORAGE_LOG(WARN, "ObBuildIndexTaskQueue has not been inited", K(ret));
} else if (OB_ISNULL(task)) {
ret = common::OB_INVALID_ARGUMENT;
STORAGE_LOG(WARN, "invalid argument", K(ret), KP(task));
} else if (OB_FAIL(task_set_.erase_refactored(task))) {
STORAGE_LOG(WARN, "fail to erase from task set", K(ret));
} else {
STORAGE_LOG(INFO, "succ to remove task", K(*task), KP(task));
}
if (NULL != task) {
allocator_.free(task);
task = NULL;
}
return ret;
}
int ObDDLTaskQueue::add_task_to_last(ObIDDLTask *task)
{
int ret = OB_SUCCESS;
common::ObSpinLockGuard guard(lock_);
if (OB_UNLIKELY(!is_inited_)) {
ret = common::OB_NOT_INIT;
STORAGE_LOG(WARN, "ObBuildIndexTaskQueue has not been inited", K(ret));
} else if (OB_ISNULL(task)) {
ret = common::OB_INVALID_ARGUMENT;
STORAGE_LOG(WARN, "invalid argument", K(ret), KP(task));
} else if (!task_list_.add_last(task)) {
ret = common::OB_ERR_UNEXPECTED;
STORAGE_LOG(ERROR, "error unexpected, fail to move task to last", K(ret));
}
return ret;
}
ObDDLTaskExecutor::ObDDLTaskExecutor()
: is_inited_(false), task_queue_(), cond_(), tg_id_(-1)
{
}
ObDDLTaskExecutor::~ObDDLTaskExecutor()
{
}
void ObDDLTaskExecutor::stop()
{
if (tg_id_ >= 0) {
TG_STOP(tg_id_);
}
}
void ObDDLTaskExecutor::wait()
{
if (tg_id_ >= 0) {
TG_WAIT(tg_id_);
}
}
void ObDDLTaskExecutor::destroy()
{
if (tg_id_ >= 0) {
TG_DESTROY(tg_id_);
tg_id_ = -1;
}
task_queue_.destroy();
is_inited_ = false;
cond_.destroy();
}
int ObDDLTaskExecutor::init(const int64_t task_num, int tg_id)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(is_inited_)) {
ret = common::OB_NOT_INIT;
STORAGE_LOG(WARN, "ObDDLTaskExecutor has already been inited", K(ret));
} else if (OB_UNLIKELY(task_num <= 0)) {
ret = common::OB_INVALID_ARGUMENT;
STORAGE_LOG(WARN, "invalid argument", K(ret), K(task_num));
} else if (OB_FAIL(task_queue_.init(task_num, TOTAL_LIMIT, HOLD_LIMIT, PAGE_SIZE))) {
STORAGE_LOG(WARN, "fail to init task queue", K(ret));
} else if (OB_FAIL(cond_.init(common::ObWaitEventIds::BUILD_INDEX_SCHEDULER_COND_WAIT))) {
STORAGE_LOG(WARN, "fail to init thread cond", K(ret));
} else {
tg_id_ = tg_id;
if (OB_FAIL(TG_SET_RUNNABLE_AND_START(tg_id_, *this))) {
STORAGE_LOG(WARN, "fail to set tg id", K(ret));
} else {
is_inited_ = true;
}
}
return ret;
}
void ObDDLTaskExecutor::run1()
{
int ret = OB_SUCCESS;
int64_t executed_task_count = 0;
ObIDDLTask *task = NULL;
ObIDDLTask *first_retry_task = NULL;
lib::set_thread_name("DDLTaskExecutor");
while (!has_set_stop()) {
while (!has_set_stop() && executed_task_count < BATCH_EXECUTE_COUNT) {
if (OB_FAIL(task_queue_.get_next_task(task))) {
if (common::OB_EAGAIN == ret) {
break;
} else {
STORAGE_LOG(WARN, "fail to get next task", K(ret));
break;
}
} else if (OB_ISNULL(task)) {
STORAGE_LOG(WARN, "error unexpected, task must not be NULL", K(ret));
} else if (task == first_retry_task) {
// add the task back to the queue
if (OB_FAIL(task_queue_.add_task_to_last(task))) {
STORAGE_LOG(ERROR, "fail to add task to last, which should not happen", K(ret), K(*task));
}
break;
} else {
task->process();
++executed_task_count;
if (task->need_retry()) {
if (OB_FAIL(task_queue_.add_task_to_last(task))) {
STORAGE_LOG(ERROR, "fail to add task to last, which should not happen", K(ret), K(*task));
}
first_retry_task = task;
} else {
if (OB_FAIL(task_queue_.remove_task(task))) {
STORAGE_LOG(WARN, "fail to remove task, which should not happen", K(ret), K(*task), KP(task));
}
}
}
}
cond_.lock();
cond_.wait(CHECK_TASK_INTERVAL);
cond_.unlock();
executed_task_count = 0;
first_retry_task = NULL;
}
}
ObDDLReplicaBuilder::ObDDLReplicaBuilder()
: is_thread_started_(false),
is_stopped_(false)
{
}
ObDDLReplicaBuilder::~ObDDLReplicaBuilder()
{
destroy();
}
int ObDDLReplicaBuilder::start()
{
int ret = OB_SUCCESS;
if (is_thread_started_) {
// do nothing
} else if (OB_FAIL(TG_START(lib::TGDefIDs::DdlBuild))) {
LOG_WARN("index build thread start failed", KR(ret));
} else {
is_thread_started_ = true;
}
if (OB_SUCC(ret)) {
is_stopped_ = false;
}
return ret;
}
void ObDDLReplicaBuilder::stop()
{
is_stopped_ = true;
}
void ObDDLReplicaBuilder::destroy()
{
is_stopped_ = true;
if (is_thread_started_) {
TG_STOP(lib::TGDefIDs::DdlBuild);
TG_WAIT(lib::TGDefIDs::DdlBuild);
TG_DESTROY(lib::TGDefIDs::DdlBuild);
is_thread_started_ = false;
}
}
int ObDDLReplicaBuilder::push_task(ObAsyncTask &task)
{
int ret = OB_SUCCESS;
if (OB_UNLIKELY(!is_thread_started_)) {
ret = OB_ERR_SYS;
LOG_WARN("ddl builder thread not started", K(ret), K(is_thread_started_));
} else if (is_stopped_) {
ret = OB_STATE_NOT_MATCH;
LOG_WARN("ddl builder has stopped", K(ret), K(is_stopped_));
} else if (OB_FAIL(TG_PUSH_TASK(lib::TGDefIDs::DdlBuild, task))) {
LOG_WARN("add task to queue failed", K(ret));
}
return ret;
}
} // end namespace share
} // end namespace oceanbase