forked from oceanbase/obkv-table-client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObTableClientQueryAsyncStreamResult.java
More file actions
465 lines (420 loc) · 19.7 KB
/
ObTableClientQueryAsyncStreamResult.java
File metadata and controls
465 lines (420 loc) · 19.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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*-
* #%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.stream;
import com.alipay.oceanbase.rpc.bolt.transport.ObTableConnection;
import com.alipay.oceanbase.rpc.exception.*;
import com.alipay.oceanbase.rpc.location.model.TableEntry;
import com.alipay.oceanbase.rpc.location.model.partition.ObPair;
import com.alipay.oceanbase.rpc.location.model.partition.ObPartitionLevel;
import com.alipay.oceanbase.rpc.protocol.payload.Constants;
import com.alipay.oceanbase.rpc.protocol.payload.ObPayload;
import com.alipay.oceanbase.rpc.protocol.payload.impl.execute.query.*;
import com.alipay.oceanbase.rpc.protocol.payload.impl.execute.syncquery.ObQueryOperationType;
import com.alipay.oceanbase.rpc.protocol.payload.impl.execute.syncquery.ObTableQueryAsyncRequest;
import com.alipay.oceanbase.rpc.protocol.payload.impl.execute.syncquery.ObTableQueryAsyncResult;
import com.alipay.oceanbase.rpc.table.ObTableParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import static com.alipay.oceanbase.rpc.constant.Constants.INVALID_TABLET_ID;
import static com.alipay.oceanbase.rpc.util.TableClientLoggerFactory.RUNTIME;
public class ObTableClientQueryAsyncStreamResult extends AbstractQueryStreamResult {
private static final Logger logger = LoggerFactory
.getLogger(ObTableClientQueryStreamResult.class);
private boolean isEnd = true;
private long sessionId = Constants.OB_INVALID_ID;
private ObTableQueryAsyncRequest asyncRequest = new ObTableQueryAsyncRequest();
private ObTableConnection prevConnection = null;
private boolean allowDistributeScan = true; // false when partition scan
private boolean hasDoneRpc = false; // this flag is for obkv-hbase scan
@Override
public void init() throws Exception {
if (initialized) {
return;
}
// init request
ObTableQueryRequest request = new ObTableQueryRequest();
request.setTableName(tableName);
request.setTableQuery(tableQuery);
request.setEntityType(entityType);
request.setConsistencyLevel(getReadConsistency());
request.setHbaseOpType(hbaseOpType);
// construct async query request
asyncRequest.setObTableQueryRequest(request);
asyncRequest.setQueryType(ObQueryOperationType.QUERY_START);
asyncRequest.setQuerySessionId(Constants.OB_INVALID_ID);
// send to first tablet
if (!expectant.isEmpty()) {
Iterator<Map.Entry<Long, ObPair<Long, ObTableParam>>> it = expectant.entrySet()
.iterator();
int retryTimes = 0;
long startExecute = System.currentTimeMillis();
while (it.hasNext()) {
Map.Entry<Long, ObPair<Long, ObTableParam>> firstEntry = it.next();
try {
hasDoneRpc = true;
// try access new partition, async will not remove useless expectant
referToNewPartition(firstEntry.getValue());
break;
} catch (Exception e) {
if (shouldRetry(e)) {
setExpectant(refreshPartition(this.asyncRequest.getObTableQueryRequest()
.getTableQuery(), client.getPhyTableNameFromTableGroup(entityType,
tableName)));
it = expectant.entrySet().iterator();
retryTimes++;
long costMillis = System.currentTimeMillis() - startExecute;
if (costMillis > client.getRuntimeMaxWait()) {
throw new ObTableTimeoutExcetion(
"Fail to get refresh table entry response after " + retryTimes
+ "errorCode:"
+ ((ObTableNeedFetchMetaException) e).getErrorCode());
}
} else {
throw e;
}
}
}
if (isEnd())
it.remove();
}
initialized = true;
}
protected void cacheResultRows(ObTableQueryAsyncResult tableQueryResult) {
cacheRows.clear();
cacheRows.addAll(tableQueryResult.getAffectedEntity().getPropertiesRows());
cacheProperties = tableQueryResult.getAffectedEntity().getPropertiesNames();
}
protected ObTableQueryAsyncResult referToNewPartition(ObPair<Long, ObTableParam> partIdWithObTable)
throws Exception {
ObTableParam obTableParam = partIdWithObTable.getRight();
ObTableQueryRequest queryRequest = asyncRequest.getObTableQueryRequest();
long partitionId = needTabletId(queryRequest) ? obTableParam.getPartitionId()
: INVALID_TABLET_ID;
// refresh request info
queryRequest.setPartitionId(partitionId);
queryRequest.setTableId(obTableParam.getTableId());
if (operationTimeout > 0) {
asyncRequest.setTimeout(operationTimeout);
} else {
asyncRequest.setTimeout(obTableParam.getObTable().getObTableOperationTimeout());
}
// refresh async query request
// since we try to access a new server, we set corresponding status
asyncRequest.setQueryType(ObQueryOperationType.QUERY_START);
asyncRequest.setQuerySessionId(Constants.OB_INVALID_ID);
// async execute
ObTableQueryAsyncResult ret = executeAsync(partIdWithObTable, asyncRequest);
return ret;
}
protected ObTableQueryAsyncResult referToLastStreamResult(ObPair<Long, ObTableParam> partIdWithObTable)
throws Exception {
ObTableParam obTableParam = partIdWithObTable.getRight();
ObTableQueryRequest queryRequest = asyncRequest.getObTableQueryRequest();
// refresh request info
long partitionId = needTabletId(queryRequest) ? obTableParam.getPartitionId()
: INVALID_TABLET_ID;
queryRequest.setPartitionId(partitionId);
queryRequest.setTableId(obTableParam.getTableId());
// refresh async query request
asyncRequest.setQueryType(ObQueryOperationType.QUERY_NEXT);
asyncRequest.setQuerySessionId(sessionId);
// async execute
ObTableQueryAsyncResult ret = executeAsync(partIdWithObTable, asyncRequest);
return ret;
}
protected void closeLastStreamResult(ObPair<Long, ObTableParam> partIdWithObTable)
throws Exception {
ObTableParam obTableParam = partIdWithObTable.getRight();
ObTableQueryRequest queryRequest = asyncRequest.getObTableQueryRequest();
// refresh request info
long partitionId = needTabletId(queryRequest) ? obTableParam.getPartitionId()
: INVALID_TABLET_ID;
queryRequest.setPartitionId(partitionId);
queryRequest.setTableId(obTableParam.getTableId());
// set end async query
asyncRequest.setQueryType(ObQueryOperationType.QUERY_END);
asyncRequest.setQuerySessionId(sessionId);
// async execute
ObTableQueryAsyncResult ret = executeAsync(partIdWithObTable, asyncRequest);
if (!isEnd()) {
throw new ObTableException("failed to close last stream result");
}
}
public boolean queryLastStreamResultInNext() throws Exception {
Iterator<Map.Entry<Long, ObPair<Long, ObTableParam>>> it = expectant.entrySet().iterator();
Map.Entry<Long, ObPair<Long, ObTableParam>> lastEntry = it.next();
try {
hasDoneRpc = true;
// try access new partition, async will not remove useless expectant
referToLastStreamResult(lastEntry.getValue());
} catch (Exception e) {
if (shouldRetry(e)) {
String realTableName = client.getPhyTableNameFromTableGroup(entityType, tableName);
TableEntry entry = client.getOrRefreshTableEntry(realTableName, false);
// Calculate the next partition only when the range partition is affected by a split, based on the keys already scanned.
if (entry.isPartitionTable()
&& entry.getPartitionInfo().getLevel() == ObPartitionLevel.LEVEL_ONE
&& entry.getPartitionInfo().getFirstPartDesc().getPartFuncType().isRangePart()) {
this.asyncRequest.getObTableQueryRequest().getTableQuery()
.adjustStartKey(currentStartKey);
setExpectant(refreshPartition(this.asyncRequest.getObTableQueryRequest()
.getTableQuery(), realTableName));
setEnd(true);
} else {
// non one-level-range partitioned table does not retry, inform user to rescan
throw e;
}
} else {
throw e;
}
}
// remove useless expectant if it is end
if (isEnd()) {
it.remove();
}
if (!cacheRows.isEmpty()) {
nextRow();
return true;
}
return false;
}
public boolean queryNewStreamResultInNext() throws Exception {
boolean hasNext = false;
Iterator<Map.Entry<Long, ObPair<Long, ObTableParam>>> it = expectant.entrySet().iterator();
int retryTimes = 0;
long startExecute = System.currentTimeMillis();
while (it.hasNext()) {
hasDoneRpc = true;
Map.Entry<Long, ObPair<Long, ObTableParam>> entry = it.next();
try {
// try access new partition, async will not remove useless expectant
referToNewPartition(entry.getValue());
} catch (Exception e) {
if (shouldRetry(e)) {
String realTableName = client.getPhyTableNameFromTableGroup(entityType,
tableName);
TableEntry tableEntry = client.getOrRefreshTableEntry(realTableName, false);
if (tableEntry.isPartitionTable()
&& tableEntry.getPartitionInfo().getLevel() == ObPartitionLevel.LEVEL_ONE
&& tableEntry.getPartitionInfo().getFirstPartDesc().getPartFuncType()
.isRangePart()) {
this.asyncRequest.getObTableQueryRequest().getTableQuery()
.adjustStartKey(currentStartKey);
setExpectant(refreshPartition(this.asyncRequest.getObTableQueryRequest()
.getTableQuery(), realTableName));
} else {
// non one-level-range partitioned table does not retry, inform user to rescan
throw e;
}
it = expectant.entrySet().iterator();
retryTimes++;
long costMillis = System.currentTimeMillis() - startExecute;
if (costMillis > client.getRuntimeMaxWait()) {
RUNTIME.error("Fail to get refresh table entry response after {}",
retryTimes);
throw new ObTableTimeoutExcetion(
"Fail to get refresh table entry response after " + retryTimes);
}
continue;
} else {
throw e;
}
}
// remove useless expectant if it is end
if (isEnd()) {
it.remove();
}
if (!cacheRows.isEmpty()) {
hasNext = true;
nextRow();
break;
}
}
return hasNext;
}
@Override
protected Map<Long, ObPair<Long, ObTableParam>> refreshPartition(ObTableQuery tableQuery,
String tableName)
throws Exception {
if (isDistributeScan()) {
return buildFirstPartitions(client, tableQuery, tableName);
} else {
return buildAllPartitions(client, tableQuery, tableName);
}
}
// This function is designed for HBase-type requests.
// It is used to extend the session duration of a scan
@Override
public void renewLease() throws Exception {
if (!isEnd() && !expectant.isEmpty()) {
Iterator<Map.Entry<Long, ObPair<Long, ObTableParam>>> it = expectant.entrySet()
.iterator();
Map.Entry<Long, ObPair<Long, ObTableParam>> lastEntry = it.next();
ObPair<Long, ObTableParam> partIdWithObTable = lastEntry.getValue();
// try access new partition, async will not remove useless expectant
ObTableParam obTableParam = partIdWithObTable.getRight();
ObTableQueryRequest queryRequest = asyncRequest.getObTableQueryRequest();
// refresh request info
long partitionId = isDistributeScan() ? INVALID_TABLET_ID : obTableParam
.getPartitionId();
queryRequest.setPartitionId(partitionId);
queryRequest.setTableId(obTableParam.getTableId());
// refresh async query request
asyncRequest.setQueryType(ObQueryOperationType.QUERY_RENEW);
asyncRequest.setQuerySessionId(sessionId);
executeAsync(partIdWithObTable, asyncRequest);
} else {
throw new ObTableException("query end or expectant is null");
}
}
@Override
public boolean next() throws Exception {
checkStatus();
lock.lock();
try {
hasDoneRpc = false;
// firstly, refer to the cache
if (!cacheRows.isEmpty()) {
nextRow();
return true;
}
boolean hasNext = false;
// secondly, refer to the last stream result
if (!isEnd() && !expectant.isEmpty()) {
try {
hasNext = queryLastStreamResultInNext();
} catch (ObTableSessionNotExistException e) {
// session_id missing because the tablet has been transferred to new server
// new server does not store the current session_id
// only support range-partitioned table, check in server
this.asyncRequest.getObTableQueryRequest().getTableQuery()
.adjustStartKey(currentStartKey);
// just need to asjust startKey to anchor the correct position
// no need to refresh partition id for session_id missing
hasNext = queryNewStreamResultInNext();
}
}
// lastly, refer to the new partition
if (!hasNext) {
hasNext = queryNewStreamResultInNext();
}
return hasNext;
} finally {
lock.unlock();
}
}
@Override
protected ObTableQueryResult execute(ObPair<Long, ObTableParam> partIdWithObTable,
ObPayload streamRequest) throws Exception {
throw new IllegalArgumentException("not support this execute");
}
/*
* Attention: executeAsync will remove useless expectant
*/
@Override
protected ObTableQueryAsyncResult executeAsync(ObPair<Long, ObTableParam> partIdWithObTable,
ObPayload streamRequest) throws Exception {
// Construct connection reference
AtomicReference<ObTableConnection> connectionRef = new AtomicReference<>();
if (client.isOdpMode() && !isEnd && prevConnection != null) {
connectionRef.set(prevConnection);
}
// execute request
ObTableQueryAsyncResult result = null;
int tryTimes = 0;
long startExecute = System.currentTimeMillis();
while (true) {
long costMillis = System.currentTimeMillis() - startExecute;
if (costMillis > client.getRuntimeMaxWait()) {
throw new ObTableTimeoutExcetion("it has tried to execute async query after "
+ tryTimes + " times and it has waited " + costMillis + " ms which exceeds response timeout "
+ client.getRuntimeMaxWait() + " ms" + " for table name: " + tableName);
}
tryTimes++;
try {
result = (ObTableQueryAsyncResult) commonExecute(this.client,
logger, partIdWithObTable, streamRequest, connectionRef);
break;
} catch (ObTableServerCacheExpiredException e) {
client.syncRefreshMetadata(false);
} catch (ObTableEntryRefreshException e) {
if (e.isConnectInactive()) {
client.syncRefreshMetadata(true);
} else {
throw e;
}
} catch (Throwable t) {
throw t;
}
}
if (result == null) {
throw new ObTableRetryExhaustedException("query timeout and retried " + tryTimes + " times");
}
// cache result
cacheResultRows(result);
// refresh async query status
if (result.isEnd()) {
isEnd = true;
} else {
isEnd = false;
prevConnection = connectionRef.get();
}
sessionId = result.getSessionId();
return result;
}
@Override
public void close() throws Exception {
// set status
if (closed) {
return;
}
closed = true;
// send end packet to last tablet
if (!isEnd() && !expectant.isEmpty()) {
Iterator<Map.Entry<Long, ObPair<Long, ObTableParam>>> it = expectant.entrySet()
.iterator();
// get the last tablet
Map.Entry<Long, ObPair<Long, ObTableParam>> lastEntry = it.next();
// try access new partition, async will not remove useless expectant
closeLastStreamResult(lastEntry.getValue());
}
}
public boolean isEnd() {
return isEnd;
}
public void setEnd(boolean end) {
isEnd = end;
}
private boolean isDistributeScan() {
return allowDistributeScan && client.getServerCapacity().isSupportDistributedExecute();
}
private boolean needTabletId(ObTableQueryRequest queryRequest) {
if (isDistributeScan()) {
return queryRequest.getNeedTabletId();
} else {
return true;
}
}
public void setAllowDistributeScan(boolean allowDistributeScan) {
this.allowDistributeScan = allowDistributeScan;
}
public boolean hasDoneRpc() {
return hasDoneRpc;
}
}