forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_phy_table_location.cpp
More file actions
387 lines (362 loc) · 15.2 KB
/
Copy pathob_phy_table_location.cpp
File metadata and controls
387 lines (362 loc) · 15.2 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
* 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
#include "sql/ob_phy_table_location.h"
#include "sql/optimizer/ob_phy_table_location_info.h"
#include "observer/ob_server_struct.h"
using namespace oceanbase::common;
using namespace oceanbase::share;
namespace oceanbase
{
namespace sql
{
bool ObPhyTableLocation::compare_phy_part_loc_info_asc(const ObCandiTabletLoc *&left,
const ObCandiTabletLoc *&right)
{
bool is_less_than = false;
if (OB_ISNULL(left) || OB_ISNULL(right)) {
LOG_ERROR_RET(OB_INVALID_ARGUMENT, "phy part loc info ptr is NULL", K(left), K(right));
} else {
is_less_than = left->get_partition_location().get_partition_id()
< right->get_partition_location().get_partition_id();
}
return is_less_than;
}
bool ObPhyTableLocation::compare_phy_part_loc_info_desc(const ObCandiTabletLoc *&left,
const ObCandiTabletLoc *&right)
{
bool is_larger_than = false;
if (OB_ISNULL(left) || OB_ISNULL(right)) {
LOG_ERROR_RET(OB_INVALID_ARGUMENT, "phy part loc info ptr is NULL", K(left), K(right));
} else {
is_larger_than = left->get_partition_location().get_partition_id()
> right->get_partition_location().get_partition_id();
}
return is_larger_than;
}
//OB_SERIALIZE_MEMBER(ObPhyTableLocation, table_location_key_,
// ref_table_id_, part_loc_list_, partition_location_list_);
OB_DEF_SERIALIZE(ObPhyTableLocation)
{
int ret = OB_SUCCESS;
static ObPartitionLocationSEArray unused_part_loc_list;
LST_DO_CODE(OB_UNIS_ENCODE,
table_location_key_,
ref_table_id_,
unused_part_loc_list,
partition_location_list_,
duplicate_type_);
return ret;
}
OB_DEF_DESERIALIZE(ObPhyTableLocation)
{
int ret = OB_SUCCESS;
ObPartitionLocationSEArray unused_part_loc_list;
LST_DO_CODE(OB_UNIS_DECODE,
table_location_key_,
ref_table_id_,
unused_part_loc_list,
partition_location_list_,
duplicate_type_);
// build local index cache
if (OB_SUCC(ret)) {
if (OB_FAIL(try_build_location_idx_map())) {
LOG_WARN("fail build location idx map", K(ret));
}
}
return ret;
}
OB_DEF_SERIALIZE_SIZE(ObPhyTableLocation)
{
int64_t len = 0;
static ObPartitionLocationSEArray unused_part_loc_list;
LST_DO_CODE(OB_UNIS_ADD_LEN,
table_location_key_,
ref_table_id_,
unused_part_loc_list,
partition_location_list_,
duplicate_type_);
return len;
}
ObPhyTableLocation::ObPhyTableLocation()
: table_location_key_(OB_INVALID_ID),
ref_table_id_(OB_INVALID_ID),
partition_location_list_(),
duplicate_type_(ObDuplicateType::NOT_DUPLICATE)
{
}
void ObPhyTableLocation::reset()
{
table_location_key_ = OB_INVALID_ID;
ref_table_id_ = OB_INVALID_ID;
duplicate_type_ = ObDuplicateType::NOT_DUPLICATE;
partition_location_list_.reset();
}
int ObPhyTableLocation::assign(const ObPhyTableLocation &other)
{
int ret = OB_SUCCESS;
table_location_key_ = other.table_location_key_;
ref_table_id_ = other.ref_table_id_;
duplicate_type_ = other.duplicate_type_;
if (OB_FAIL(partition_location_list_.assign(other.partition_location_list_))) {
LOG_WARN("Failed to assign partition location list", K(ret));
}
return ret;
}
int ObPhyTableLocation::append(const ObPhyTableLocation &other)
{
int ret = OB_SUCCESS;
if (table_location_key_ != other.table_location_key_
|| ref_table_id_ != other.ref_table_id_
|| duplicate_type_ != other.duplicate_type_) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(table_location_key_), K(other.table_location_key_),
K(ref_table_id_), K(other.ref_table_id_), K(other.duplicate_type_));
} else if (OB_FAIL(append_array_no_dup(partition_location_list_, other.partition_location_list_))) {
LOG_WARN("append array no dup failed", K(ret));
}
return ret;
}
int ObPhyTableLocation::add_partition_locations(const ObCandiTableLoc &phy_location_info)
{
int ret = OB_SUCCESS;
ObPartitionReplicaLocation part_replica_loc;
ObReplicaLocation replica_loc;
const ObCandiTabletLocIArray &phy_part_loc_info_list = phy_location_info.get_phy_part_loc_info_list();
// for (int64_t i = 0; OB_SUCC(ret) && i < phy_part_loc_info_list.count(); ++i) {
// part_replica_loc.reset();
// replica_loc.reset();
// const ObCandiTabletLoc &phy_part_loc_info = phy_part_loc_info_list.at(i);
// const ObOptTabletLoc &part_loc = phy_part_loc_info.get_partition_location();
// if (OB_FAIL(phy_part_loc_info.get_selected_replica(replica_loc))) {
// LOG_WARN("fail to get selected replica", K(ret), K(phy_part_loc_info));
// } else if (OB_FAIL(part_replica_loc.assign(part_loc.get_table_id(),
// part_loc.get_partition_id(),
// part_loc.get_partition_cnt(),
// replica_loc,
// part_loc.get_renew_time(),
// part_loc.get_pg_key()))) {
// LOG_WARN("fail to assin part replica loc", K(ret), K(part_loc), K(replica_loc));
// } else if (OB_FAIL(partition_location_list_.push_back(part_replica_loc))) {
// LOG_WARN("fail to push back part replica loc", K(ret), K(part_replica_loc));
// }
// }
return ret;
}
int ObPhyTableLocation::assign_from_phy_table_loc_info(const ObCandiTableLoc &other)
{
int ret = OB_SUCCESS;
ObSEArray<const ObCandiTabletLoc*, 2> phy_part_loc_info_sorted_list;
ObPartitionReplicaLocation part_replica_loc;
ObReplicaLocation replica_loc;
table_location_key_ = other.get_table_location_key();
ref_table_id_ = other.get_ref_table_id();
const ObCandiTabletLocIArray &phy_part_loc_info_list = other.get_phy_part_loc_info_list();
// if (other.get_direction() == UNORDERED) {
// if (OB_FAIL(partition_location_list_.reserve(phy_part_loc_info_list.count()))) {
// LOG_WARN("fail reserve memory", K(ret), K(phy_part_loc_info_list.count()));
// }
// for (int64_t i = 0; OB_SUCC(ret) && i < phy_part_loc_info_list.count(); ++i) {
// part_replica_loc.reset();
// replica_loc.reset();
// const ObCandiTabletLoc &phy_part_loc_info = phy_part_loc_info_list.at(i);
// const ObOptTabletLoc &part_loc = phy_part_loc_info.get_partition_location();
// if (OB_FAIL(phy_part_loc_info.get_selected_replica(replica_loc))) {
// LOG_WARN("fail to get selected replica", K(ret), K(phy_part_loc_info));
// } else if (OB_FAIL(part_replica_loc.assign(part_loc.get_table_id(),
// part_loc.get_partition_id(),
// part_loc.get_partition_cnt(),
// replica_loc,
// part_loc.get_renew_time(),
// part_loc.get_pg_key()))) {
// LOG_WARN("fail to assin part replica loc", K(ret), K(part_loc), K(replica_loc));
// } else if (OB_FAIL(partition_location_list_.push_back(part_replica_loc))) {
// LOG_WARN("fail to push back part replica loc", K(ret), K(part_replica_loc));
// }
// }
// } else {
// if (OB_FAIL(phy_part_loc_info_sorted_list.reserve(phy_part_loc_info_list.count()))) {
// LOG_WARN("fail reserve memory", K(ret), K(phy_part_loc_info_list.count()));
// } else if (OB_FAIL(partition_location_list_.reserve(phy_part_loc_info_list.count()))) {
// LOG_WARN("fail reserve memory", K(ret), K(phy_part_loc_info_list.count()));
// }
// for (int64_t i = 0; OB_SUCC(ret) && i < phy_part_loc_info_list.count(); ++i) {
// if (OB_FAIL(phy_part_loc_info_sorted_list.push_back(&phy_part_loc_info_list.at(i)))) {
// LOG_WARN("fail to push back phy part loc info", K(ret), K(phy_part_loc_info_list.at(i)), K(i));
// }
// }
// if (OB_SUCC(ret) && phy_part_loc_info_sorted_list.count() > 0) {
// //按照指定的direction顺序输出partition顺序.
// //目前依赖partition序代表range序.以后做分裂等复杂情况后,就需要依赖part_mgr(schema)来排序
// if (is_ascending_direction(other.get_direction())) {
// std::sort(&phy_part_loc_info_sorted_list.at(0),
// &phy_part_loc_info_sorted_list.at(0) + phy_part_loc_info_sorted_list.count(),
// ObPhyTableLocation::compare_phy_part_loc_info_asc);
// } else if (is_descending_direction(other.get_direction())) {
// std::sort(&phy_part_loc_info_sorted_list.at(0),
// &phy_part_loc_info_sorted_list.at(0) + phy_part_loc_info_sorted_list.count(),
// ObPhyTableLocation::compare_phy_part_loc_info_desc);
// } else {
// ret = OB_ERR_UNEXPECTED;
// LOG_ERROR("unexpected order", K(ret), K(other.get_direction()), K(other));
// }
// }
// for (int64_t i = 0; OB_SUCC(ret) && i < phy_part_loc_info_sorted_list.count(); ++i) {
// part_replica_loc.reset();
// replica_loc.reset();
// const ObCandiTabletLoc *phy_part_loc_info = phy_part_loc_info_sorted_list.at(i);
// if (OB_ISNULL(phy_part_loc_info)) {
// ret = OB_ERR_UNEXPECTED;
// LOG_ERROR("phy part loc info is NULL", K(ret), K(i));
// } else {
// const ObOptTabletLoc &part_loc = phy_part_loc_info->get_partition_location();
// if (OB_FAIL(phy_part_loc_info->get_selected_replica(replica_loc))) {
// LOG_WARN("fail to get selected replica", K(ret), K(*phy_part_loc_info));
// } else if (OB_FAIL(part_replica_loc.assign(part_loc.get_table_id(),
// part_loc.get_partition_id(),
// part_loc.get_partition_cnt(),
// replica_loc,
// part_loc.get_renew_time(),
// part_loc.get_pg_key()))) {
// LOG_WARN("fail to assin part replica loc", K(ret), K(part_loc), K(replica_loc));
// } else if (OB_FAIL(partition_location_list_.push_back(part_replica_loc))) {
// LOG_WARN("fail to push back part replica loc", K(ret), K(part_replica_loc));
// }
// }
// }
// }
if (OB_SUCC(ret)) {
set_duplicate_type(other.get_duplicate_type());
}
return ret;
}
int ObPhyTableLocation::add_partition_location(
const ObIArray<ObPartitionReplicaLocation> &partition_locations)
{
int ret = OB_SUCCESS;
int64_t N = partition_locations.count();
for (int64_t i = 0; OB_SUCC(ret) && i < N; ++i) {
if (OB_FAIL(add_partition_location(partition_locations.at(i)))) {
LOG_WARN("Fail to add partition_location to list");
}
}
return ret;
}
int ObPhyTableLocation::add_partition_location(
const ObPartitionReplicaLocation &partition_location)
{
int ret = OB_SUCCESS;
bool duplicated = false;
for (int64_t i = 0; OB_SUCC(ret) && !duplicated
&& i < partition_location_list_.count(); ++i) {
if (partition_location.get_table_id()
== partition_location_list_.at(i).get_table_id()
&& partition_location.get_partition_id()
== partition_location_list_.at(i).get_partition_id()) {
duplicated = true;
}
}
if (!duplicated) {
if (OB_FAIL(partition_location_list_.push_back(partition_location))) {
LOG_WARN("Fail to add partition location to list", K(ret), K(partition_location));
}
}
return ret;
}
const ObPartitionReplicaLocation *ObPhyTableLocation::get_part_replic_by_part_id(int64_t part_id) const
{
const ObPartitionReplicaLocation *ret = NULL;
int64_t i = 0;
if (OB_SUCCESS == get_location_idx_by_part_id(part_id, i)) {
ret = &partition_location_list_.at(i);
}
return ret;
}
const share::ObPartitionReplicaLocation *ObPhyTableLocation::get_part_replic_by_index(int64_t part_idx) const
{
const ObPartitionReplicaLocation *ret = NULL;
if (part_idx >= 0 && part_idx < partition_location_list_.count()) {
ret = &partition_location_list_.at(part_idx);
}
return ret;
}
int ObPhyTableLocation::try_build_location_idx_map()
{
int ret = OB_SUCCESS;
// if more than FAST_LOOKUP_LOC_IDX_SIZE_THRES partitions in a 'Task/SQC/Worker',
// build a hash map to accelerate lookup speed
if (OB_UNLIKELY(partition_location_list_.count() > FAST_LOOKUP_LOC_IDX_SIZE_THRES)) {
if (OB_FAIL(location_idx_map_.create(
partition_location_list_.count(), "LocIdxPerfMap", "LocIdxPerfMap"))) {
LOG_WARN("fail init location idx map", K(ret));
} else {
ARRAY_FOREACH(partition_location_list_, idx) {
// ObPartitionReplicaLocation
auto &item = partition_location_list_.at(idx);
if (OB_FAIL(location_idx_map_.set_refactored(item.get_partition_id(), idx))) {
LOG_WARN("fail set value to map", K(ret), K(item));
}
}
}
}
return ret;
}
int ObPhyTableLocation::get_location_idx_by_part_id(int64_t part_id, int64_t &loc_idx) const
{
int ret = OB_ENTRY_NOT_EXIST;
if (OB_UNLIKELY(location_idx_map_.size() > 0)) {
ret = location_idx_map_.get_refactored(part_id, loc_idx);
LOG_DEBUG("lookup location index from map", K(partition_location_list_.count()),
K(part_id), K(loc_idx), K(table_location_key_), K(ref_table_id_));
} else {
// single part table scan
for (int64_t idx = 0; idx < partition_location_list_.count(); ++idx) {
if (partition_location_list_.at(idx).get_partition_id() == part_id) {
loc_idx = idx;
ret = OB_SUCCESS;
break;
}
}
}
return ret;
}
// The reason for not using the remove interface is that
// the remove interface will not free the memory of the removed node.
// If the interface is called multiple times for anti-corruption,
// the problem of memory bloat will occur.
int ObPhyTableLocation::erase_partition_location(int64_t partition_id)
{
int ret = OB_SUCCESS;
ObPartitionReplicaLocationSEArray tmp_location_list;
int64_t N = partition_location_list_.count();
for (int64_t idx = N - 1; OB_SUCC(ret) && idx >= 0; --idx) {
if (partition_location_list_.at(idx).get_partition_id() != partition_id) {
if (OB_FAIL(tmp_location_list.push_back(partition_location_list_.at(idx)))) {
LOG_WARN("add partition location from list failed", K(ret), K(idx));
}
} else {
LOG_DEBUG("remove partition succ", K(idx), K(partition_id));
}
}
if (OB_SUCC(ret)) {
partition_location_list_.reset();
if (OB_FAIL(partition_location_list_.assign(tmp_location_list))) {
LOG_WARN("fail to assign partition location list", K(ret));
}
}
if (OB_SUCC(ret) && location_idx_map_.size() > 0) {
location_idx_map_.clear();
}
return ret;
}
}/* ns sql*/
}/* ns oceanbase */