forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_event_executor.cpp
More file actions
245 lines (229 loc) · 11.8 KB
/
Copy pathob_event_executor.cpp
File metadata and controls
245 lines (229 loc) · 11.8 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
/*
* 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_ENG
#include "sql/engine/cmd/ob_event_executor.h"
#include <chrono>
#include <ctime>
#include <iomanip>
#include "lib/mysqlclient/ob_mysql_transaction.h"
#include "lib/string/ob_sql_string.h"
#include "sql/resolver/cmd/ob_event_stmt.h"
#include "sql/engine/ob_exec_context.h"
#include "observer/dbms_scheduler/ob_dbms_sched_table_operator.h"
namespace oceanbase
{
using namespace common;
using namespace obrpc;
using namespace share::schema;
namespace sql
{
int ObCreateEventExecutor::execute(ObExecContext &ctx, ObCreateEventStmt &stmt)
{
int ret = OB_SUCCESS;
ObTaskExecutorCtx *task_exec_ctx = NULL;
if (OB_ISNULL(task_exec_ctx = GET_TASK_EXECUTOR_CTX(ctx))) {
ret = OB_NOT_INIT;
LOG_WARN("get task executor context failed");
} else {
int64_t job_id = OB_INVALID_ID;
if (OB_INVALID_TENANT_ID == stmt.get_event_info().get_tenant_id()) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid tenant id", KR(ret), K(stmt.get_event_info().get_tenant_id()));
} else if (OB_FAIL(dbms_scheduler::ObDBMSSchedJobUtils::generate_job_id(stmt.get_event_info().get_tenant_id(), job_id))) {
LOG_WARN("generate_job_id failed", KR(ret), K(stmt.get_event_info().get_tenant_id()));
} else {
int64_t start_date_us = stmt.get_event_info().get_start_time() == OB_INVALID_TIMESTAMP ? ObTimeUtility::current_time() : stmt.get_event_info().get_start_time();
int64_t end_date_us = stmt.get_event_info().get_end_time() == OB_INVALID_TIMESTAMP ? dbms_scheduler::ObDBMSSchedJobInfo::DEFAULT_MAX_END_DATE : stmt.get_event_info().get_end_time(); // 4000-01-01
HEAP_VAR(dbms_scheduler::ObDBMSSchedJobInfo, job_info) {
job_info.func_type_ = dbms_scheduler::ObDBMSSchedFuncType::MYSQL_EVENT_JOB;
job_info.tenant_id_ = stmt.get_event_info().get_tenant_id();
job_info.user_id_ = stmt.get_event_info().get_user_id();
job_info.database_id_ = stmt.get_event_info().get_database_id();
job_info.job_ = job_id;
job_info.job_name_ = stmt.get_event_info().get_event_name();
job_info.job_action_ = stmt.get_event_info().get_event_body();
job_info.lowner_ = stmt.get_event_info().get_event_definer();
job_info.powner_ = stmt.get_event_info().get_event_definer();
job_info.cowner_ = stmt.get_event_info().get_event_database();
job_info.job_style_ = ObString("REGULAR");
job_info.job_type_ = ObString("PLSQL_BLOCK");
job_info.job_class_ = ObString("MYSQL_EVENT_JOB_CLASS");
job_info.what_ = stmt.get_event_info().get_event_body();
job_info.start_date_ = start_date_us;
job_info.end_date_ = end_date_us;
job_info.interval_ = job_info.repeat_interval_;
job_info.repeat_interval_ = stmt.get_event_info().get_repeat_interval();
job_info.enabled_ = stmt.get_event_info().get_is_enable() == ObEventInfo::ObEventBoolType::SET_FALSE ? false : true; //Not_Set = true
job_info.auto_drop_ = stmt.get_event_info().get_auto_drop() == ObEventInfo::ObEventBoolType::SET_FALSE ? false : true; //Not_Set = true
job_info.max_run_duration_ = stmt.get_event_info().get_max_run_duration() > 0 ? stmt.get_event_info().get_max_run_duration() : 24 * 60 * 60;
job_info.exec_env_ = stmt.get_event_info().get_exec_env();
job_info.comments_ = stmt.get_event_info().get_event_comment();
ObMySQLTransaction trans;
if (OB_FAIL(trans.start(GCTX.sql_proxy_, stmt.get_event_info().get_tenant_id()))) {
LOG_WARN("failed to start trans", KR(ret), K(stmt.get_event_info().get_tenant_id()));
} else if (OB_FAIL(dbms_scheduler::ObDBMSSchedJobUtils::create_dbms_sched_job(
trans, stmt.get_event_info().get_tenant_id(), job_id, job_info))) {
LOG_WARN("failed to create dbms scheduler job", KR(ret), K(stmt.get_event_info().get_if_exist_or_if_not_exist()));
}
if (trans.is_started()) {
int tmp_ret = OB_SUCCESS;
if (OB_SUCCESS != (tmp_ret = trans.end(OB_SUCC(ret)))) {
LOG_WARN("failed to commit trans", KR(ret), KR(tmp_ret));
ret = OB_SUCC(ret) ? tmp_ret : ret;
}
}
if (ret == OB_ERR_PRIMARY_KEY_DUPLICATE) {
if (stmt.get_event_info().get_if_exist_or_if_not_exist()) {
ret = OB_SUCCESS;
} else {
ret = OB_ERR_EVENT_EXIST;
}
}
}
}
}
return ret;
}
int ObAlterEventExecutor::execute(ObExecContext &ctx, ObAlterEventStmt &stmt)
{
int ret = OB_SUCCESS;
int64_t tenant_id = stmt.get_event_info().get_tenant_id();
ObArenaAllocator allocator;
dbms_scheduler::ObDBMSSchedJobInfo job_info;
ObMySQLTransaction trans;
if (OB_FAIL(trans.start(GCTX.sql_proxy_, tenant_id))) {
LOG_WARN("failed to start trans", KR(ret), K(stmt.get_event_info().get_tenant_id()));
} else if (OB_FAIL(dbms_scheduler::ObDBMSSchedJobUtils::get_dbms_sched_job_info(*GCTX.sql_proxy_,
tenant_id,
false, // is_oracle_tenant
stmt.get_event_info().get_event_name(),
allocator,
job_info))) {
LOG_WARN("get job failed", K(ret));
if (ret == OB_ENTRY_NOT_EXIST) {
ret = OB_ERR_EVENT_NOT_EXIST;
}
} else {
const uint64_t exec_tenant_id = ObSchemaUtils::get_exec_tenant_id(tenant_id);
if (OB_SUCC(ret) && !stmt.get_event_info().get_event_definer().empty()) {
ObObj powner_obj, user_id_obj;
powner_obj.set_char(stmt.get_event_info().get_event_definer());
user_id_obj.set_int(stmt.get_event_info().get_user_id());
OZ (dbms_scheduler::ObDBMSSchedJobUtils::update_dbms_sched_job_info(trans, job_info, ObString("powner"), powner_obj));
OZ (dbms_scheduler::ObDBMSSchedJobUtils::update_dbms_sched_job_info(trans, job_info, ObString("user_id"), user_id_obj));
}
if (OB_SUCC(ret) && ObEventInfo::ObEventBoolType::NOT_SET != stmt.get_event_info().get_auto_drop()) {
ObObj obj;
if (ObEventInfo::ObEventBoolType::SET_TRUE == stmt.get_event_info().get_auto_drop()) {
obj.set_bool(true);
} else {
obj.set_bool(false);
}
OZ (dbms_scheduler::ObDBMSSchedJobUtils::update_dbms_sched_job_info(trans, job_info, ObString("auto_drop"), obj));
}
if (OB_SUCC(ret) && ObEventInfo::ObEventBoolType::NOT_SET != stmt.get_event_info().get_is_enable()) {
ObObj obj;
if (ObEventInfo::ObEventBoolType::SET_TRUE == stmt.get_event_info().get_is_enable()) {
obj.set_bool(true);
} else {
obj.set_bool(false);
}
OZ (dbms_scheduler::ObDBMSSchedJobUtils::update_dbms_sched_job_info(trans, job_info, ObString("enabled"), obj));
}
if (OB_SUCC(ret) && !stmt.get_event_info().get_event_comment().empty()) {
ObObj obj;
obj.set_char(stmt.get_event_info().get_event_comment());
OZ (dbms_scheduler::ObDBMSSchedJobUtils::update_dbms_sched_job_info(trans, job_info, ObString("comments"), obj));
}
if (OB_SUCC(ret) && !stmt.get_event_info().get_event_body().empty()) {
ObObj obj;
obj.set_char(stmt.get_event_info().get_event_body());
OZ (dbms_scheduler::ObDBMSSchedJobUtils::update_dbms_sched_job_info(trans, job_info, ObString("job_action"), obj));
}
if (OB_SUCC(ret) && OB_INVALID_TIMESTAMP != stmt.get_event_info().get_start_time()) {
int64_t start_date_us = stmt.get_event_info().get_start_time();
int64_t end_date_us = stmt.get_event_info().get_end_time() == OB_INVALID_TIMESTAMP ? dbms_scheduler::ObDBMSSchedJobInfo::DEFAULT_MAX_END_DATE : stmt.get_event_info().get_end_time();
if (start_date_us > end_date_us) {
ret = OB_ERR_EVENT_ENDS_BEFORE_STARTS;
LOG_WARN("end time invalid", K(ret), K(start_date_us), K(end_date_us));
} else {
ObObj start_date_obj, end_date_obj, repeat_interval_obj, max_run_duration_obj;
start_date_obj.set_time(start_date_us);
end_date_obj.set_time(end_date_us);
repeat_interval_obj.set_char(stmt.get_event_info().get_repeat_interval());
max_run_duration_obj.set_int(stmt.get_event_info().get_max_run_duration() > 0 ? stmt.get_event_info().get_max_run_duration() : 24 * 60 * 60);
OZ (dbms_scheduler::ObDBMSSchedJobUtils::update_dbms_sched_job_info(trans, job_info, ObString("start_date"), start_date_obj));
OX (job_info.start_date_ = start_date_us); // update local job_info to prevent calculation errors
OZ (dbms_scheduler::ObDBMSSchedJobUtils::update_dbms_sched_job_info(trans, job_info, ObString("end_date"), end_date_obj));
OX (job_info.end_date_ = end_date_us); // update local job_info to prevent calculation errors
OZ (dbms_scheduler::ObDBMSSchedJobUtils::update_dbms_sched_job_info(trans, job_info, ObString("repeat_interval"), repeat_interval_obj));
OZ (dbms_scheduler::ObDBMSSchedJobUtils::update_dbms_sched_job_info(trans, job_info, ObString("max_run_duration"), max_run_duration_obj));
}
}
// Updating job_name first will cause subsequent updates to execute incorrectly, the update of job name should be placed last.
if (OB_SUCC(ret) && !stmt.get_event_info().get_event_rename().empty()) {
if (job_info.get_start_date() > ObTimeUtility::current_time()) { // job cannot be modified once it starts running
ObObj obj;
obj.set_char(stmt.get_event_info().get_event_rename());
OZ (dbms_scheduler::ObDBMSSchedJobUtils::update_dbms_sched_job_info(trans, job_info, ObString("job_name"), obj));
if (OB_ERR_PRIMARY_KEY_DUPLICATE == ret) {
ret = OB_ERR_EVENT_EXIST;
}
} else {
ret = OB_NOT_SUPPORTED;
LOG_USER_ERROR(OB_NOT_SUPPORTED, "modify EVENT NAME that has already started");
}
}
if (trans.is_started()) {
int tmp_ret = OB_SUCCESS;
if (OB_SUCCESS != (tmp_ret = trans.end(OB_SUCC(ret)))) {
LOG_WARN("failed to commit trans", KR(ret), KR(tmp_ret));
ret = OB_SUCC(ret) ? tmp_ret : ret;
}
}
}
return ret;
}
int ObDropEventExecutor::execute(ObExecContext &ctx, ObDropEventStmt &stmt)
{
int ret = OB_SUCCESS;
ObTaskExecutorCtx *task_exec_ctx = NULL;
obrpc::ObCommonRpcProxy *common_rpc_proxy = NULL;
if (OB_ISNULL(task_exec_ctx = GET_TASK_EXECUTOR_CTX(ctx))) {
ret = OB_NOT_INIT;
LOG_WARN("get task executor context failed");
} else if (OB_ISNULL(common_rpc_proxy = task_exec_ctx->get_common_rpc())) {
ret = OB_NOT_INIT;
LOG_WARN("get common rpc proxy failed");
} else {
uint64_t tenant_id = stmt.get_event_info().get_tenant_id();
const uint64_t exec_tenant_id = ObSchemaUtils::get_exec_tenant_id(tenant_id);
if (OB_FAIL(dbms_scheduler::ObDBMSSchedJobUtils::remove_dbms_sched_job(
*GCTX.sql_proxy_,
stmt.get_event_info().get_tenant_id(),
stmt.get_event_info().get_event_name(),
stmt.get_event_info().get_if_exist_or_if_not_exist())
)) {
if (ret == OB_INVALID_ARGUMENT) {
ret = OB_ERR_EVENT_NOT_EXIST;
}
LOG_WARN("failed to remove dbms scheduler job", KR(ret));
}
}
return ret;
}
}
}