forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_phy_table_location.cpp
More file actions
279 lines (256 loc) · 10.7 KB
/
Copy pathob_phy_table_location.cpp
File metadata and controls
279 lines (256 loc) · 10.7 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
/*
* 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
#include "ob_phy_table_location.h"
#include "sql/optimizer/ob_phy_table_location_info.h"
using namespace oceanbase::common;
using namespace oceanbase::share;
namespace oceanbase
{
namespace sql
{
//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::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) {
// //output partition order according to the specified direction order.
// //Currently dependency on partition order represents range order. In the future, after operations like split, etc., we will need to rely on part_mgr(schema) for sorting
// if (is_ascending_direction(other.get_direction())) {
// lib::ob_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())) {
// lib::ob_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 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;
}
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.
}/* ns sql*/
}/* ns oceanbase */