forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_task_executor_ctx.cpp
More file actions
295 lines (278 loc) · 9.55 KB
/
Copy pathob_task_executor_ctx.cpp
File metadata and controls
295 lines (278 loc) · 9.55 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
/**
* 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 "lib/net/ob_addr.h"
#include "lib/hash/ob_hashmap.h"
#include "sql/executor/ob_task_executor_ctx.h"
#include "sql/ob_sql_trans_control.h"
#include "sql/executor/ob_job_control.h"
#include "sql/engine/ob_exec_context.h"
#include "sql/engine/ob_physical_plan_ctx.h"
#include "sql/ob_phy_table_location.h"
#include "share/ob_common_rpc_proxy.h"
#include "sql/executor/ob_executor_rpc_impl.h"
#include "observer/ob_server.h"
using namespace oceanbase::common;
using namespace oceanbase::share;
using namespace oceanbase::share::schema;
namespace oceanbase
{
namespace sql
{
int ObTaskExecutorCtx::CalcVirtualPartitionIdParams::init(uint64_t ref_table_id)
{
int ret = common::OB_SUCCESS;
if (true == inited_) {
ret = common::OB_INIT_TWICE;
LOG_ERROR("init twice", K(ret), K(inited_), K(ref_table_id));
} else {
inited_ = true;
ref_table_id_ = ref_table_id;
}
return ret;
}
OB_SERIALIZE_MEMBER(ObTaskExecutorCtx,
table_locations_,
retry_times_,
min_cluster_version_,
expected_worker_cnt_,
admited_worker_cnt_,
query_tenant_begin_schema_version_,
query_sys_begin_schema_version_,
minimal_worker_cnt_);
ObTaskExecutorCtx::ObTaskExecutorCtx(ObExecContext &exec_context)
: task_resp_handler_(NULL),
virtual_part_servers_(exec_context.get_allocator()),
exec_ctx_(&exec_context),
expected_worker_cnt_(0),
minimal_worker_cnt_(0),
admited_worker_cnt_(0),
retry_times_(0),
min_cluster_version_(ObExecutorRpcCtx::INVALID_CLUSTER_VERSION),
sys_job_id_(-1),
rs_rpc_proxy_(nullptr),
query_tenant_begin_schema_version_(-1),
query_sys_begin_schema_version_(-1),
schema_service_(GCTX.schema_service_)
{
}
ObTaskExecutorCtx::~ObTaskExecutorCtx()
{
if (NULL != task_resp_handler_) {
task_resp_handler_->~RemoteExecuteStreamHandle();
task_resp_handler_ = NULL;
}
if (rs_rpc_proxy_ != nullptr) {
rs_rpc_proxy_->~ObCommonRpcProxy();
rs_rpc_proxy_ = nullptr;
}
}
int ObTaskExecutorCtx::get_addr_by_virtual_partition_id(int64_t partition_id, ObAddr &addr)
{
int ret = OB_SUCCESS;
if (partition_id < 0 || partition_id >= virtual_part_servers_.size()) {
ret = OB_ENTRY_NOT_EXIST;
LOG_WARN("addr not exist", K(ret), K(partition_id), K(virtual_part_servers_.size()));
} else {
ObList<ObAddr, ObIAllocator>::iterator it = virtual_part_servers_.begin();
int64_t idx = 0;
for (; idx != partition_id; ++it) {
++idx;
}
addr = *it;
}
return ret;
}
int ObTaskExecutorCtx::set_table_locations(const ObTablePartitionInfoArray &table_partition_infos)
{
int ret = OB_SUCCESS;
//table_locations_在这里必须先reset,确保table partition infos没有被重复添加
table_locations_.reset();
ObPhyTableLocation phy_table_loc;
int64_t N = table_partition_infos.count();
if (OB_FAIL(table_locations_.reserve(N))) {
LOG_WARN("fail reserve locations", K(ret), K(N));
}
for (int64_t i = 0; OB_SUCC(ret) && i < N; ++i) {
phy_table_loc.reset();
ObTablePartitionInfo *partition_info = table_partition_infos.at(i);
if (OB_ISNULL(partition_info)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected error. table partition info is null", K(ret), K(i));
} else if (partition_info->get_table_location().use_das()) {
//do nothing,DAS的location由自己维护和计算
} else if (OB_FAIL(phy_table_loc.assign_from_phy_table_loc_info(partition_info->get_phy_tbl_location_info()))) {
LOG_WARN("fail to assign_from_phy_table_loc_info", K(ret), K(i), K(partition_info->get_phy_tbl_location_info()), K(N));
} else if (OB_FAIL(table_locations_.push_back(phy_table_loc))) {
LOG_WARN("fail to push back into table locations", K(ret), K(i), K(phy_table_loc), K(N));
}
}
return ret;
}
int ObTaskExecutorCtx::append_table_location(const ObCandiTableLoc &phy_location_info)
{
int ret = OB_SUCCESS;
ObPhyTableLocation phy_table_loc;
if (OB_FAIL(phy_table_loc.assign_from_phy_table_loc_info(phy_location_info))) {
LOG_WARN("assign from physical table location info failed", K(ret));
} else if (OB_FAIL(table_locations_.push_back(phy_table_loc))) {
LOG_WARN("store table location failed", K(ret));
}
return ret;
}
//
//
// Utility
//
int ObTaskExecutorCtxUtil::get_stream_handler(
ObExecContext &ctx,
RemoteExecuteStreamHandle *&handler)
{
int ret = OB_SUCCESS;
ObTaskExecutorCtx *executor_ctx = NULL;
if (OB_ISNULL(executor_ctx = ctx.get_task_executor_ctx())) {
ret = OB_NOT_INIT;
LOG_WARN("fail get executor ctx");
} else if (OB_ISNULL(handler = executor_ctx->get_stream_handler())) {
ret = OB_NOT_INIT;
LOG_WARN("stream handler is not inited", K(ret));
}
return ret;
}
int ObTaskExecutorCtxUtil::get_task_executor_rpc(
ObExecContext &ctx,
ObExecutorRpcImpl *&rpc)
{
int ret = OB_SUCCESS;
ObTaskExecutorCtx *executor_ctx = NULL;
if (OB_ISNULL(executor_ctx = ctx.get_task_executor_ctx())) {
ret = OB_NOT_INIT;
LOG_WARN("ObTaskExecutorCtx is null", K(ret));
} else if (OB_ISNULL(rpc = executor_ctx->get_task_executor_rpc())) {
ret = OB_NOT_INIT;
LOG_WARN("rpc is null", K(ret));
}
return ret;
}
obrpc::ObCommonRpcProxy *ObTaskExecutorCtx::get_common_rpc()
{
int ret = OB_SUCCESS;
obrpc::ObCommonRpcProxy *ret_pointer = NULL;
if (OB_FAIL(get_common_rpc(ret_pointer))) {
LOG_WARN("get common rpc problem ", K(ret));
}
return ret_pointer;
}
int ObTaskExecutorCtx::get_common_rpc(obrpc::ObCommonRpcProxy *&common_rpc_proxy)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(exec_ctx_)) {
ret = OB_ERR_UNEXPECTED;
LOG_ERROR("null pointer", K_(exec_ctx), K(ret));
} else if (OB_ISNULL(exec_ctx_->get_physical_plan_ctx())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("physical plan ctx is null", K(ret));
} else {
const int64_t timeout = exec_ctx_->get_physical_plan_ctx()->get_timeout_timestamp() -
ObTimeUtility::current_time();
if (rs_rpc_proxy_ == nullptr) {
void *buf = nullptr;
if (OB_ISNULL(buf = exec_ctx_->get_allocator().alloc(sizeof(ObCommonRpcProxy)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("allocate rpc proxy memory failed", K(ret));
} else {
rs_rpc_proxy_ = new(buf) ObCommonRpcProxy();
*rs_rpc_proxy_ = *GCTX.rs_rpc_proxy_;
}
}
if (OB_SUCC(ret)) {
if (timeout <= 0) {
ret = OB_TIMEOUT;
LOG_WARN("execute task timeout", K(timeout), K(ret));
} else {
rs_rpc_proxy_->set_timeout(timeout);
common_rpc_proxy = rs_rpc_proxy_;
}
}
}
return ret;
}
int ObTaskExecutorCtx::reset_and_init_stream_handler()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(exec_ctx_)) {
ret = OB_NOT_INIT;
LOG_ERROR("unexpected error. exec ctx is not inited", K(ret));
} else {
if (NULL != task_resp_handler_) {
// 有可能是transaction_set_violation_and_retry引起的执行器层面的重试,
// ObTaskExecutorCtx析构之前多次调用本函数,
// 所以这里要先析构掉之前的内存
task_resp_handler_->~RemoteExecuteStreamHandle();
task_resp_handler_ = NULL;
}
RemoteExecuteStreamHandle *buffer = NULL;
if (OB_ISNULL(buffer = static_cast<RemoteExecuteStreamHandle*>(exec_ctx_->get_allocator().//is this allocator ok ?
alloc(sizeof(RemoteExecuteStreamHandle))))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("fail to alloc memory for RemoteExecuteStreamHandle", K(ret));
} else {
task_resp_handler_ = new (buffer) RemoteExecuteStreamHandle("RemoteExecStream", MTL_ID());
}
}
return ret;
}
int ObTaskExecutorCtxUtil::nonblock_renew(
ObExecContext *exec_ctx,
const ObTabletID &tablet_id,
const int64_t expire_renew_time,
const int64_t cluster_id)
{
int ret = OB_SUCCESS;
UNUSED(cluster_id);
UNUSED(expire_renew_time);
if (NULL == GCTX.location_service_) {
ret = OB_NOT_INIT;
LOG_WARN("loc_cache is NULL", K(ret));
} else if (OB_FAIL(GCTX.location_service_->nonblock_renew(GET_MY_SESSION(*exec_ctx)->get_effective_tenant_id(),
tablet_id))) {
LOG_WARN("nonblock_renew failed", K(tablet_id), K(ret));
}
return ret;
}
int ObTaskExecutorCtx::nonblock_renew_with_limiter(
const ObTabletID &tablet_id,
const int64_t expire_renew_time,
bool &is_limited)
{
int ret = OB_SUCCESS;
UNUSED(expire_renew_time);
is_limited = false;
if (NULL == GCTX.location_service_) {
ret = OB_NOT_INIT;
LOG_WARN("tmp_loc_cache is NULL", K(ret));
} else if (OB_FAIL(GCTX.location_service_->nonblock_renew(GET_MY_SESSION(*exec_ctx_)->get_effective_tenant_id(),
tablet_id))) {
LOG_WARN("nonblock_renew failed", K(tablet_id), K(ret));
}
return ret;
}
void ObTaskExecutorCtx::set_self_addr(const common::ObAddr &self_addr)
{
UNUSED(self_addr);
}
const common::ObAddr ObTaskExecutorCtx::get_self_addr() const
{
return MYADDR;
}
}/* ns sql*/
}/* ns oceanbase */