forked from oceanbase/obkv-table-client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObTableClientQueryImpl.java
More file actions
437 lines (385 loc) · 17.9 KB
/
ObTableClientQueryImpl.java
File metadata and controls
437 lines (385 loc) · 17.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*-
* #%L
* OBKV Table Client Framework
* %%
* Copyright (C) 2021 OceanBase
* %%
* OBKV Table Client Framework is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* 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 PSL v2 for more details.
* #L%
*/
package com.alipay.oceanbase.rpc.table;
import com.alipay.oceanbase.rpc.ObGlobal;
import com.alipay.oceanbase.rpc.ObTableClient;
import com.alipay.oceanbase.rpc.exception.FeatureNotSupportedException;
import com.alipay.oceanbase.rpc.exception.ObTableException;
import com.alipay.oceanbase.rpc.location.model.partition.ObPair;
import com.alipay.oceanbase.rpc.mutation.Row;
import com.alipay.oceanbase.rpc.protocol.payload.ObPayload;
import com.alipay.oceanbase.rpc.protocol.payload.ResultCodes;
import com.alipay.oceanbase.rpc.protocol.payload.impl.ObRowKey;
import com.alipay.oceanbase.rpc.protocol.payload.impl.execute.ObTableEntityType;
import com.alipay.oceanbase.rpc.protocol.payload.impl.execute.aggregation.ObTableAggregationType;
import com.alipay.oceanbase.rpc.protocol.payload.impl.execute.query.*;
import com.alipay.oceanbase.rpc.stream.ObTableClientQueryAsyncStreamResult;
import com.alipay.oceanbase.rpc.stream.ObTableClientQueryStreamResult;
import com.alipay.oceanbase.rpc.stream.QueryResultSet;
import com.alipay.oceanbase.rpc.table.api.TableQuery;
import com.alipay.oceanbase.rpc.util.MonitorUtil;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static com.alipay.oceanbase.rpc.protocol.payload.Constants.INVALID_TABLET_ID;
import static com.alipay.oceanbase.rpc.protocol.payload.Constants.OB_INVALID_ID;
public class ObTableClientQueryImpl extends AbstractTableQueryImpl {
private String tableName;
private final ObTableClient obTableClient;
private Map<Long, ObPair<Long, ObTableParam>> partitionObTables;
private Row rowKey; // only used by BatchOperation
private boolean allowDistributeScan;
/*
* Add aggregation.
*/
public void addAggregation(ObTableAggregationType aggType, String aggColumn) {
this.tableQuery.addAggregation(aggType, aggColumn);
}
/*
* Ob table client query impl construct only with tableName
*/
public ObTableClientQueryImpl() {
this.tableName = null;
this.indexTableName = null;
this.obTableClient = null;
this.tableQuery = new ObTableQuery();
this.rowKey = null;
}
/*
* Ob table client query impl.
*/
public ObTableClientQueryImpl(String tableName, ObTableClient client) {
this.tableName = tableName;
this.indexTableName = tableName;
this.obTableClient = client;
this.tableQuery = new ObTableQuery();
this.rowKey = null;
}
/*
* Ob table client query impl.
*/
public ObTableClientQueryImpl(String tableName, ObTableQuery tableQuery, ObTableClient client) {
this.tableName = tableName;
this.indexTableName = tableName;
this.obTableClient = client;
this.tableQuery = tableQuery;
this.rowKey = null;
}
/*
* Execute.
*/
@Override
public QueryResultSet execute() throws Exception {
return new QueryResultSet(executeInternal());
}
/*
* Execute.
*/
@Override
public QueryResultSet asyncExecute() throws Exception {
return new QueryResultSet(asyncExecuteInternal());
}
/**
* 只有 limit query 需要,其他不需要
* @param keys keys want to set
* @return table query
*/
@Override
public TableQuery setKeys(String... keys) {
throw new IllegalArgumentException("Not needed");
}
/*
* set row key of query (only used by BatchOperation)
*/
public TableQuery setRowKey(Row rowKey) throws Exception {
this.rowKey = rowKey;
return this;
}
/*
* check argument before execution
*/
public void checkArgumentBeforeExec() throws Exception {
if (null == obTableClient) {
throw new ObTableException("table client is null");
} else if (tableQuery.getLimit() < 0 && tableQuery.getOffset() > 0) {
throw new ObTableException("offset can not be use without limit");
} else if (tableName == null || tableName.isEmpty()) {
throw new IllegalArgumentException("table name is null");
} else if (tableQuery.isFTSQuery()) {
if (!ObGlobal.isFtsQuerySupport()) {
throw new FeatureNotSupportedException("full text query is not supported in "
+ ObGlobal.obVsnString());
}
if (tableQuery.getIndexName() == null || tableQuery.getIndexName().isEmpty()
|| tableQuery.getIndexName().equalsIgnoreCase("primary")) {
throw new IllegalArgumentException(
"use fulltext search but specified index name is not fulltext index");
}
}
}
/*
* Set parameter into request
*/
private void setCommonParams2Result(AbstractQueryStreamResult result) throws Exception {
result.setTableQuery(tableQuery);
result.setEntityType(entityType);
result.setTableName(tableName);
result.setIndexTableName(indexTableName);
result.setExpectant(partitionObTables);
result.setOperationTimeout(operationTimeout);
result.setReadConsistency(obTableClient.getReadConsistency());
}
private abstract static class InitQueryResultCallback<T> {
abstract T execute() throws Exception;
}
private AbstractQueryStreamResult commonExecute(InitQueryResultCallback<AbstractQueryStreamResult> callable)
throws Exception {
checkArgumentBeforeExec();
final long startTime = System.currentTimeMillis();
this.partitionObTables = new LinkedHashMap<Long, ObPair<Long, ObTableParam>>(); // partitionObTables -> Map<logicId, Pair<logicId, param>>
// fill a whole range if no range is added explicitly.
if (tableQuery.getKeyRanges().isEmpty()) {
tableQuery.addKeyRange(ObNewRange.getWholeRange());
}
// init partitionObTables
if (obTableClient.isOdpMode()) {
if (tableQuery.getScanRangeColumns().isEmpty()) {
if (tableQuery.getIndexName() != null
&& !tableQuery.getIndexName().equalsIgnoreCase("primary")
&& !tableQuery.isFTSQuery()) {
throw new ObTableException("key range columns must be specified when use index");
}
}
if (getPartId() != null && tableQuery.getIndexName() == null) {
String realTableName = tableName;
try {
if (this.entityType == ObTableEntityType.HKV
&& obTableClient.isTableGroupName(tableName)) {
indexTableName = obTableClient.tryGetTableNameFromTableGroupCache(
tableName, false);
realTableName = indexTableName;
}
ObPair<Long, ObTableParam> odpTable = obTableClient.getODPTableWithPartId(
realTableName, getPartId(), false);
partitionObTables.put(odpTable.getLeft(), odpTable);
} catch (Exception e) {
if (e instanceof ObTableException) {
if (((ObTableException) e).getErrorCode() == ResultCodes.OB_NOT_SUPPORTED.errorCode) {
// current ODP version does not support get partition meta information
throw new FeatureNotSupportedException(
"current ODP version does not support query with part id", e);
} else if (((ObTableException) e).getErrorCode() == ResultCodes.OB_ERR_KV_ROUTE_ENTRY_EXPIRE.errorCode) {
// retry one time with force-renew flag
ObPair<Long, ObTableParam> odpTable = obTableClient
.getODPTableWithPartId(realTableName, getPartId(), true);
partitionObTables.put(odpTable.getLeft(), odpTable);
} else {
throw e;
}
} else {
throw e;
}
}
} else {
partitionObTables.put(0L, new ObPair<Long, ObTableParam>(0L, new ObTableParam(
obTableClient.getOdpTable())));
}
} else {
if (getPartId() == null) {
initPartitions();
} else { // directly get table from table entry by logic partId
if (this.entityType != ObTableEntityType.HKV) {
indexTableName = obTableClient.getIndexTableName(tableName,
tableQuery.getIndexName(), tableQuery.getScanRangeColumns(), false);
} else if (obTableClient.isTableGroupName(tableName)) {
indexTableName = obTableClient.tryGetTableNameFromTableGroupCache(tableName,
false);
}
ObPair<Long, ObTableParam> table = obTableClient.getTableWithPartId(indexTableName,
getPartId(), false, false, false, obTableClient.getRoute(false));
partitionObTables.put(table.getLeft(), table);
}
}
StringBuilder stringBuilder = new StringBuilder();
for (Map.Entry<Long, ObPair<Long, ObTableParam>> entry : this.partitionObTables.entrySet()) {
stringBuilder.append("#").append(entry.getValue().getRight().getObTable().getIp())
.append(":").append(entry.getValue().getRight().getObTable().getPort());
}
String endpoint = stringBuilder.toString();
long getTableTime = System.currentTimeMillis();
// defend aggregation of multiple partitions.
if (tableQuery.isAggregation()) {
if (this.partitionObTables.size() > 1) {
throw new ObTableException(
"Not supported aggregate of multiple partitions, the partition size is: "
+ this.partitionObTables.size(), ResultCodes.OB_NOT_SUPPORTED.errorCode);
}
}
// init query stream result
AbstractQueryStreamResult streamResult = callable.execute();
MonitorUtil
.info((ObPayload) streamResult, obTableClient.getDatabase(), tableName, "QUERY",
endpoint, tableQuery, streamResult, getTableTime - startTime,
System.currentTimeMillis() - getTableTime,
obTableClient.getslowQueryMonitorThreshold());
return streamResult;
}
/*
* Execute internal.
*/
public ObTableClientQueryStreamResult executeInternal() throws Exception {
return (ObTableClientQueryStreamResult) commonExecute(new InitQueryResultCallback<AbstractQueryStreamResult>() {
@Override
ObTableClientQueryStreamResult execute() throws Exception {
ObTableClientQueryStreamResult obTableClientQueryStreamResult = new ObTableClientQueryStreamResult();
setCommonParams2Result(obTableClientQueryStreamResult);
obTableClientQueryStreamResult.setClient(obTableClient);
obTableClientQueryStreamResult.init();
return obTableClientQueryStreamResult;
}
});
}
public ObTableClientQueryAsyncStreamResult asyncExecuteInternal() throws Exception {
return (ObTableClientQueryAsyncStreamResult) commonExecute(new InitQueryResultCallback<AbstractQueryStreamResult>() {
@Override
ObTableClientQueryAsyncStreamResult execute() throws Exception {
ObTableClientQueryAsyncStreamResult obTableClientQueryAsyncStreamResult = new ObTableClientQueryAsyncStreamResult();
obTableClientQueryAsyncStreamResult.setAllowDistributeScan(allowDistributeScan);
setCommonParams2Result(obTableClientQueryAsyncStreamResult);
obTableClientQueryAsyncStreamResult.setClient(obTableClient);
obTableClientQueryAsyncStreamResult.init();
return obTableClientQueryAsyncStreamResult;
}
});
}
public Map<Long, ObPair<Long, ObTableParam>> initFirstPartition(ObTableQuery tableQuery, String tableName) throws Exception {
Map<Long, ObPair<Long, ObTableParam>> partitionObTables = new LinkedHashMap<>();
String indexName = tableQuery.getIndexName();
if (!this.obTableClient.isOdpMode()) {
indexTableName = obTableClient.getIndexTableName(tableName, indexName, tableQuery.getScanRangeColumns(), false);
}
if (tableQuery.getKeyRanges().isEmpty()) {
throw new IllegalArgumentException("query ranges is empty");
} else {
ObNewRange range = tableQuery.getKeyRanges().get(0);
ObRowKey startKey = range.getStartKey();
int startKeySize = startKey.getObjs().size();
Object[] start = new Object[startKeySize];
for (int i = 0; i < startKeySize; i++) {
start[i] = startKey.getObj(i).isMinObj() || startKey.getObj(i).isMaxObj() ?
startKey.getObj(i) : startKey.getObj(i).getValue();
}
if (this.entityType == ObTableEntityType.HKV && obTableClient.isTableGroupName(tableName)) {
indexTableName = obTableClient.tryGetTableNameFromTableGroupCache(tableName, false);
}
ObBorderFlag borderFlag = range.getBorderFlag();
// pairs -> List<Pair<logicId, param>>
List<ObPair<Long, ObTableParam>> pairs = this.obTableClient.getTables(indexTableName, tableQuery, start,
borderFlag.isInclusiveStart(), start, borderFlag.isInclusiveEnd(), false, false);
partitionObTables.put(INVALID_TABLET_ID, pairs.get(0));
}
return partitionObTables;
}
public Map<Long, ObPair<Long, ObTableParam>> initPartitions(ObTableQuery tableQuery, String tableName) throws Exception {
Map<Long, ObPair<Long, ObTableParam>> partitionObTables = new LinkedHashMap<>();
String indexName = tableQuery.getIndexName();
if (!this.obTableClient.isOdpMode()) {
indexTableName = obTableClient.getIndexTableName(tableName, indexName, tableQuery.getScanRangeColumns(), false);
}
for (ObNewRange range : tableQuery.getKeyRanges()) {
ObRowKey startKey = range.getStartKey();
int startKeySize = startKey.getObjs().size();
ObRowKey endKey = range.getEndKey();
int endKeySize = endKey.getObjs().size();
Object[] start = new Object[startKeySize];
Object[] end = new Object[endKeySize];
for (int i = 0; i < startKeySize; i++) {
start[i] = startKey.getObj(i).isMinObj() || startKey.getObj(i).isMaxObj() ?
startKey.getObj(i) : startKey.getObj(i).getValue();
}
for (int i = 0; i < endKeySize; i++) {
end[i] = endKey.getObj(i).isMinObj() || endKey.getObj(i).isMaxObj() ?
endKey.getObj(i) : endKey.getObj(i).getValue();
}
if (this.entityType == ObTableEntityType.HKV && obTableClient.isTableGroupName(tableName)) {
indexTableName = obTableClient.tryGetTableNameFromTableGroupCache(tableName, false);
}
ObBorderFlag borderFlag = range.getBorderFlag();
// pairs -> List<Pair<logicId, param>>
List<ObPair<Long, ObTableParam>> pairs = this.obTableClient.getTables(indexTableName, tableQuery, start,
borderFlag.isInclusiveStart(), end, borderFlag.isInclusiveEnd(), false, false);
if (tableQuery.getScanOrder() == ObScanOrder.Reverse) {
for (int i = pairs.size() - 1; i >= 0; i--) {
partitionObTables.put(pairs.get(i).getLeft(), pairs.get(i));
}
} else {
for (ObPair<Long, ObTableParam> pair : pairs) {
partitionObTables.put(pair.getLeft(), pair);
}
}
}
return partitionObTables;
}
/*
* Init partition tables involved in this query
*/
public void initPartitions() throws Exception {
if (obTableClient.getServerCapacity().isSupportDistributedExecute()) {
this.partitionObTables = initFirstPartition(tableQuery, tableName);
} else {
this.partitionObTables = initPartitions(tableQuery, tableName);
}
}
/*
* Clear.
*/
@Override
public void clear() {
this.tableQuery = new ObTableQuery();
}
/*
* Get ob table query.
*/
@Override
public ObTableQuery getObTableQuery() {
return tableQuery;
}
/*
* Get table name.
*/
public String getTableName() {
return tableName;
}
public void setTableName(String tabName) {
this.tableName = tabName;
}
/*
* Get row key (used by BatchOperation)
*/
public Row getRowKey() {
return this.rowKey;
}
public void setPartId(Long partId) {
getObTableQuery().setPartId(partId);
}
public Long getPartId() {
return getObTableQuery().getPartId();
}
public void setAllowDistributeScan(boolean allowDistributeScan) {
this.allowDistributeScan = allowDistributeScan;
}
}