-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathObTableClientBatchOpsImpl.java
More file actions
708 lines (654 loc) · 33.2 KB
/
ObTableClientBatchOpsImpl.java
File metadata and controls
708 lines (654 loc) · 33.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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/*-
* #%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.ObTableClient;
import com.alipay.oceanbase.rpc.bolt.transport.TransportCodes;
import com.alipay.oceanbase.rpc.exception.*;
import com.alipay.oceanbase.rpc.location.model.ObServerRoute;
import com.alipay.oceanbase.rpc.location.model.TableEntry;
import com.alipay.oceanbase.rpc.location.model.partition.ObPair;
import com.alipay.oceanbase.rpc.mutation.Row;
import com.alipay.oceanbase.rpc.mutation.result.*;
import com.alipay.oceanbase.rpc.protocol.payload.ObPayload;
import com.alipay.oceanbase.rpc.protocol.payload.Pcodes;
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.*;
import com.alipay.oceanbase.rpc.threadlocal.ThreadLocalMap;
import com.alipay.oceanbase.rpc.util.MonitorUtil;
import com.alipay.oceanbase.rpc.util.TableClientLoggerFactory;
import org.slf4j.Logger;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import static com.alipay.oceanbase.rpc.util.TableClientLoggerFactory.*;
public class ObTableClientBatchOpsImpl extends AbstractTableBatchOps {
private static final Logger logger = TableClientLoggerFactory
.getLogger(ObTableClientBatchOpsImpl.class);
private final ObTableClient obTableClient;
private ExecutorService executorService;
private boolean returningAffectedEntity = false;
private ObTableBatchOperation batchOperation;
/*
* Ob table client batch ops impl.
*/
public ObTableClientBatchOpsImpl(String tableName, ObTableClient obTableClient) {
this.tableName = tableName;
this.obTableClient = obTableClient;
this.batchOperation = new ObTableBatchOperation();
}
/*
* Ob table client batch ops impl.
*/
public ObTableClientBatchOpsImpl(String tableName, ObTableBatchOperation batchOperation,
ObTableClient obTableClient) {
this.tableName = tableName;
this.obTableClient = obTableClient;
this.batchOperation = batchOperation;
}
/*
* Get ob table batch operation.
*/
@Override
public ObTableBatchOperation getObTableBatchOperation() {
return batchOperation;
}
/*
* Get.
*/
@Override
public void get(Object[] rowkeys, String[] columns) {
addObTableClientOperation(ObTableOperationType.GET, rowkeys, columns, null);
}
/*
* Update.
*/
@Override
public void update(Object[] rowkeys, String[] columns, Object[] values) {
addObTableClientOperation(ObTableOperationType.UPDATE, rowkeys, columns, values);
}
/*
* Delete.
*/
@Override
public void delete(Object[] rowkeys) {
addObTableClientOperation(ObTableOperationType.DEL, rowkeys, null, null);
}
/*
* Insert.
*/
@Override
public void insert(Object[] rowkeys, String[] columns, Object[] values) {
addObTableClientOperation(ObTableOperationType.INSERT, rowkeys, columns, values);
}
/*
* Replace.
*/
@Override
public void replace(Object[] rowkeys, String[] columns, Object[] values) {
addObTableClientOperation(ObTableOperationType.REPLACE, rowkeys, columns, values);
}
/*
* Insert or update.
*/
@Override
public void insertOrUpdate(Object[] rowkeys, String[] columns, Object[] values) {
addObTableClientOperation(ObTableOperationType.INSERT_OR_UPDATE, rowkeys, columns, values);
}
/*
* Increment.
*/
@Override
public void increment(Object[] rowkeys, String[] columns, Object[] values, boolean withResult) {
returningAffectedEntity = withResult;
addObTableClientOperation(ObTableOperationType.INCREMENT, rowkeys, columns, values);
}
/*
* Append.
*/
@Override
public void append(Object[] rowkeys, String[] columns, Object[] values, boolean withResult) {
returningAffectedEntity = withResult;
addObTableClientOperation(ObTableOperationType.APPEND, rowkeys, columns, values);
}
/*
* Put.
*/
@Override
public void put(Object[] rowkeys, String[] columns, Object[] values) {
addObTableClientOperation(ObTableOperationType.PUT, rowkeys, columns, values);
}
private void addObTableClientOperation(ObTableOperationType type, Object[] rowkeys,
String[] columns, Object[] values) {
ObTableOperation instance = ObTableOperation.getInstance(type, rowkeys, columns, values);
batchOperation.addTableOperation((instance));
}
/*
* Execute.
*/
public List<Object> execute() throws Exception {
// consistent can not be sure
List<Object> results = new ArrayList<Object>(batchOperation.getTableOperations().size());
for (ObTableOperationResult result : executeInternal().getResults()) {
int errCode = result.getHeader().getErrno();
if (errCode == ResultCodes.OB_SUCCESS.errorCode) {
switch (result.getOperationType()) {
case GET:
case INCREMENT:
case APPEND:
results.add(result.getEntity().getSimpleProperties());
break;
default:
results.add(result.getAffectedRows());
}
} else {
results.add(ExceptionUtil.convertToObTableException(result.getExecuteHost(),
result.getExecutePort(), result.getSequence(), result.getUniqueId(), errCode,
result.getHeader().getErrMsg()));
}
}
return results;
}
/*
* Execute with result
*/
public List<Object> executeWithResult() throws Exception {
// consistent can not be sure
List<Object> results = new ArrayList<Object>(batchOperation.getTableOperations().size());
for (ObTableOperationResult result : executeInternal().getResults()) {
int errCode = result.getHeader().getErrno();
if (errCode == ResultCodes.OB_SUCCESS.errorCode) {
switch (result.getOperationType()) {
case GET:
case INSERT:
case DEL:
case UPDATE:
case INSERT_OR_UPDATE:
case REPLACE:
case INCREMENT:
case APPEND:
case PUT:
results.add(new MutationResult(result));
break;
default:
throw new ObTableException("unknown operation type "
+ result.getOperationType());
}
} else {
results.add(ExceptionUtil.convertToObTableException(result.getExecuteHost(),
result.getExecutePort(), result.getSequence(), result.getUniqueId(), errCode,
result.getHeader().getErrMsg()));
}
}
return results;
}
// Helper method to calculate RowKey from ObTableOperation
private Object[] calculateRowKey(ObTableOperation operation) {
ObRowKey rowKeyObject = operation.getEntity().getRowKey();
int rowKeySize = rowKeyObject.getObjs().size();
Object[] rowKey = new Object[rowKeySize];
for (int j = 0; j < rowKeySize; j++) {
rowKey[j] = rowKeyObject.getObj(j).getValue();
}
return rowKey;
}
public List<ObTableOperation> extractOperations(List<ObPair<Integer, ObTableOperation>> operationsPairs) {
List<ObTableOperation> operations = new ArrayList<>(operationsPairs.size());
for (ObPair<Integer, ObTableOperation> pair : operationsPairs) {
operations.add(pair.getRight());
}
return operations;
}
public Map<Long, ObPair<ObTableParam, List<ObPair<Integer, ObTableOperation>>>> prepareOperations(List<ObTableOperation> operations) throws Exception {
Map<Long, ObPair<ObTableParam, List<ObPair<Integer, ObTableOperation>>>> partitionOperationsMap = new HashMap<>();
if (obTableClient.isOdpMode()) {
ObPair<ObTableParam, List<ObPair<Integer, ObTableOperation>>> obTableOperations = new ObPair<>(
new ObTableParam(obTableClient.getOdpTable()),
new ArrayList<>());
for (int i = 0; i < operations.size(); i++) {
ObTableOperation operation = operations.get(i);
obTableOperations.getRight().add(
new ObPair<>(i, operation));
}
partitionOperationsMap.put(0L, obTableOperations);
return partitionOperationsMap;
}
for (int i = 0; i < operations.size(); i++) {
ObTableOperation operation = operations.get(i);
ObRowKey rowKeyObject = operation.getEntity().getRowKey();
int rowKeySize = rowKeyObject.getObjs().size();
Object[] rowKey = new Object[rowKeySize];
for (int j = 0; j < rowKeySize; j++) {
rowKey[j] = rowKeyObject.getObj(j).getValue();
}
Row row = obTableClient.transformToRow(tableName, rowKey);
ObTableParam tableParam = obTableClient.getTableParamWithRoute(
tableName, row, obTableClient.getRoute(batchOperation.isReadOnly()));
ObPair<ObTableParam, List<ObPair<Integer, ObTableOperation>>> obTableOperations = partitionOperationsMap
.computeIfAbsent(tableParam.getPartId(), k -> new ObPair<>(
tableParam, new ArrayList<>()));
obTableOperations.getRight().add(new ObPair<>(i, operation));
}
return partitionOperationsMap;
}
public Map<Long, ObPair<ObTableParam, List<ObPair<Integer, ObTableOperation>>>> partitionPrepare()
throws Exception {
// consistent can not be sure
List<ObTableOperation> operations = batchOperation.getTableOperations();
return prepareOperations(operations);
}
/*
* Partition execute.
*/
public void partitionExecute(ObTableOperationResult[] results,
Map.Entry<Long, ObPair<ObTableParam, List<ObPair<Integer, ObTableOperation>>>> partitionOperation)
throws Exception {
ObTableParam tableParam = partitionOperation.getValue().getLeft();
long tableId = tableParam.getTableId();
long partId = tableParam.getPartitionId();
long originPartId = tableParam.getPartId();
ObTable subObTable = tableParam.getObTable();
List<ObPair<Integer, ObTableOperation>> subOperationWithIndexList = partitionOperation
.getValue().getRight();
ObTableBatchOperationRequest subRequest = new ObTableBatchOperationRequest();
ObTableBatchOperation subOperations = new ObTableBatchOperation();
for (ObPair<Integer, ObTableOperation> operationWithIndex : subOperationWithIndexList) {
subOperations.addTableOperation(operationWithIndex.getRight());
}
subOperations.setSameType(batchOperation.isSameType());
subOperations.setReadOnly(batchOperation.isReadOnly());
subOperations.setSamePropertiesNames(batchOperation.isSamePropertiesNames());
subRequest.setBatchOperation(subOperations);
subRequest.setTableName(tableName);
subRequest.setReturningAffectedEntity(returningAffectedEntity);
subRequest.setReturningAffectedRows(true);
subRequest.setTableId(tableId);
subRequest.setPartitionId(partId);
subRequest.setEntityType(entityType);
subRequest.setTimeout(subObTable.getObTableOperationTimeout());
if (batchOperation.isReadOnly()) {
subRequest.setConsistencyLevel(obTableClient.getReadConsistency()
.toObTableConsistencyLevel());
}
subRequest.setBatchOperationAsAtomic(isAtomicOperation());
subRequest.setBatchOpReturnOneResult(isReturnOneResult());
ObTableBatchOperationResult subObTableBatchOperationResult;
int tryTimes = 0;
boolean needRefreshPartitionLocation = false;
long startExecute = System.currentTimeMillis();
Set<String> failedServerList = null;
ObServerRoute route = null;
while (true) {
obTableClient.checkStatus();
long currentExecute = System.currentTimeMillis();
long costMillis = currentExecute - startExecute;
if (costMillis > obTableClient.getRuntimeMaxWait()) {
logger.error(
"tablename:{} partition id:{} it has tried " + tryTimes
+ " times and it has waited " + costMillis
+ "/ms which exceeds response timeout "
+ obTableClient.getRuntimeMaxWait() + "/ms", tableName, partId);
throw new ObTableTimeoutExcetion("it has tried " + tryTimes
+ " times and it has waited " + costMillis
+ "/ms which exceeds response timeout "
+ obTableClient.getRuntimeMaxWait() + "/ms");
}
tryTimes++;
try {
if (obTableClient.isOdpMode()) {
subObTable = obTableClient.getOdpTable();
} else {
// getTable() when we need retry
// we should use partIdx to get table
if (tryTimes > 1) {
if (route == null) {
route = obTableClient.getRoute(batchOperation.isReadOnly());
}
if (failedServerList != null) {
route.setBlackList(failedServerList);
}
if (needRefreshPartitionLocation) {
// refresh partition location
TableEntry entry = obTableClient.getOrRefreshTableEntry(tableName,
false);
obTableClient.refreshTableLocationByTabletId(tableName,
obTableClient.getTabletIdByPartId(entry, originPartId));
ObTableParam newParam = obTableClient.getTableParamWithPartId(
tableName, originPartId, route);
subObTable = newParam.getObTable();
subRequest.setPartitionId(newParam.getPartitionId());
}
}
}
ObPayload result = subObTable.execute(subRequest);
if (result != null && result.getPcode() == Pcodes.OB_TABLE_API_MOVE) {
ObTableApiMove moveResponse = (ObTableApiMove) result;
subObTable = obTableClient.getTable(moveResponse);
result = subObTable.execute(subRequest);
if (result instanceof ObTableApiMove) {
ObTableApiMove move = (ObTableApiMove) result;
logger
.warn(
"The server has not yet completed the master switch, and returned an incorrect leader with an IP address of {}. "
+ "Rerouting return IP is {}", moveResponse.getReplica()
.getServer().ipToString(), move.getReplica().getServer()
.ipToString());
throw new ObTableRoutingWrongException();
}
}
subObTableBatchOperationResult = (ObTableBatchOperationResult) result;
obTableClient.resetExecuteContinuousFailureCount(tableName);
break;
} catch (Exception ex) {
needRefreshPartitionLocation = true;
if (obTableClient.isOdpMode()) {
needRefreshPartitionLocation = false;
// if exceptions need to retry, retry to timeout
if (ex instanceof ObTableException
&& ((ObTableException) ex).isNeedRetryError()) {
logger.warn(
"meet need retry exception when execute normal batch in odp mode."
+ "tableName: {}, errMsg: {}", tableName, ex.getMessage());
} else {
logger.warn("meet exception when execute normal batch in odp mode."
+ "tablename: {}, errMsg: {}", tableName, ex.getMessage());
// odp mode do not retry any other exceptions
throw new ObTableException(ex);
}
} else if (ex instanceof ObTableReplicaNotReadableException) {
if (System.currentTimeMillis() - startExecute < obTableClient
.getRuntimeMaxWait()) {
logger.warn(
"tablename:{} partition id:{} retry when replica not readable: {}",
tableName, partId, ex.getMessage());
if (failedServerList == null) {
failedServerList = new HashSet<String>();
}
failedServerList.add(subObTable.getIp());
} else {
logger.warn("retry to timeout when replica not readable: {}",
ex.getMessage());
throw ex;
}
} else if (ex instanceof ObTableException) {
if (((ObTableException) ex).isNeedRefreshTableEntry()) {
if (obTableClient.isRetryOnChangeMasterTimes()) {
if (ex instanceof ObTableNeedFetchMetaException) {
// refresh table info
TableEntry entry = obTableClient.getOrRefreshTableEntry(tableName, true);
if (((ObTableNeedFetchMetaException) ex).isNeedRefreshMetaAndLocation()) {
long tabletId = obTableClient.getTabletIdByPartId(entry, originPartId);
obTableClient.refreshTableLocationByTabletId(tableName, tabletId);
}
throw ex;
}
} else {
String logMessage = String
.format(
"retry is disabled while meet NeedRefresh Exception, table name: %s, batch ops refresh table, retry times: %d, errorCode: %d",
tableName, tryTimes, ((ObTableException) ex).getErrorCode());
logger.warn(logMessage, ex);
obTableClient.calculateContinuousFailure(tableName, ex.getMessage());
throw new ObTableRetryExhaustedException(logMessage, ex);
}
} else if (((ObTableException) ex).isNeedRetryError()) {
// retry server errors, no need to refresh partition location
needRefreshPartitionLocation = false;
if (obTableClient.isRetryOnChangeMasterTimes()) {
logger
.warn(
"execute while meet server error, need to retry, errorCode: {}, errorMsg: {}, try times {}",
((ObTableException) ex).getErrorCode(), ex.getMessage(),
tryTimes);
} else {
String logMessage = String
.format(
"retry is disabled while meet NeedRefresh Exception, table name: %s, batch ops refresh table, retry times: %d, errorCode: %d",
tableName, tryTimes, ((ObTableException) ex).getErrorCode());
logger.warn(logMessage, ex);
obTableClient.calculateContinuousFailure(tableName, ex.getMessage());
throw new ObTableRetryExhaustedException(logMessage, ex);
}
} else {
if (ex instanceof ObTableTransportException
&& ((ObTableTransportException) ex).getErrorCode() == TransportCodes.BOLT_TIMEOUT) {
logger.debug("normal batch meet transport timeout, obTable ip:port is {}:{}",
subObTable.getIp(), subObTable.getPort());
obTableClient.syncRefreshMetadata(true);
obTableClient.refreshTableLocationByTabletId(tableName, partId);
subObTable.setDirty();
}
obTableClient.calculateContinuousFailure(tableName, ex.getMessage());
throw ex;
}
} else {
obTableClient.calculateContinuousFailure(tableName, ex.getMessage());
throw ex;
}
}
Thread.sleep(obTableClient.getRuntimeRetryInterval());
}
long endExecute = System.currentTimeMillis();
if (subObTableBatchOperationResult == null) {
RUNTIME
.error(
"tablename:{} partition id:{} check batch operation result error: client get unexpected NULL result",
tableName, partId);
throw new ObTableUnexpectedException(
"check batch operation result error: client get unexpected NULL result");
}
List<ObTableOperationResult> subObTableOperationResults = subObTableBatchOperationResult
.getResults();
if (returnOneResult) {
ObTableOperationResult subObTableOperationResult = subObTableOperationResults.get(0);
if (results[0] == null) {
results[0] = new ObTableOperationResult();
subObTableOperationResult.setExecuteHost(subObTable.getIp());
subObTableOperationResult.setExecutePort(subObTable.getPort());
results[0] = subObTableOperationResult;
} else {
results[0].setAffectedRows(results[0].getAffectedRows()
+ subObTableOperationResult.getAffectedRows());
}
} else {
if (subObTableOperationResults.size() < subOperations.getTableOperations().size()) {
// only one result when it across failed
// only one result when hkv puts
if (subObTableOperationResults.size() == 1) {
ObTableOperationResult subObTableOperationResult = subObTableOperationResults
.get(0);
subObTableOperationResult.setExecuteHost(subObTable.getIp());
subObTableOperationResult.setExecutePort(subObTable.getPort());
for (ObPair<Integer, ObTableOperation> aSubOperationWithIndexList : subOperationWithIndexList) {
results[aSubOperationWithIndexList.getLeft()] = subObTableOperationResult;
}
} else {
// unexpected result found
throw new IllegalArgumentException(
"check batch operation result size error: operation size ["
+ subOperations.getTableOperations().size() + "] result size ["
+ subObTableOperationResults.size() + "]");
}
} else {
if (subOperationWithIndexList.size() != subObTableOperationResults.size()) {
throw new ObTableUnexpectedException("check batch result error: partition "
+ partId + " expect result size "
+ subOperationWithIndexList.size()
+ " actual result size "
+ subObTableOperationResults.size());
}
for (int i = 0; i < subOperationWithIndexList.size(); i++) {
ObTableOperationResult subObTableOperationResult = subObTableOperationResults
.get(i);
subObTableOperationResult.setExecuteHost(subObTable.getIp());
subObTableOperationResult.setExecutePort(subObTable.getPort());
results[subOperationWithIndexList.get(i).getLeft()] = subObTableOperationResult;
}
}
}
String endpoint = subObTable.getIp() + ":" + subObTable.getPort();
MonitorUtil.info(subRequest, subObTable.getDatabase(), tableName,
"BATCH-partitionExecute-", endpoint, subOperations, partId,
subObTableOperationResults.size(), endExecute - startExecute,
obTableClient.getslowQueryMonitorThreshold());
}
private boolean shouldRetry(Throwable throwable) {
return throwable instanceof ObTableNeedFetchMetaException;
}
private void executeWithRetries(ObTableOperationResult[] results, Map.Entry<Long, ObPair<ObTableParam, List<ObPair<Integer, ObTableOperation>>>> entry) throws Exception {
int retryCount = 0;
Map<Long, ObPair<ObTableParam, List<ObPair<Integer, ObTableOperation>>>> currentPartitions = new HashMap<>();
currentPartitions.put(entry.getKey(), entry.getValue());
int errCode = ResultCodes.OB_SUCCESS.errorCode;
String errMsg = null;
long runTimeMaxWait = obTableClient.getRuntimeMaxWait();
long startExecute = System.currentTimeMillis();
while (true) {
long costMillis = System.currentTimeMillis() - startExecute;
if (costMillis > runTimeMaxWait) {
errMsg = tableName + " failed to execute operation after retrying " + retryCount
+ " times and it has waited" + costMillis + " ms"
+ " which exceeds runtime max wait timeout " + runTimeMaxWait
+ " ms. Last error Msg:" + "[errCode=" + errCode + "] " + errMsg;
logger.error(errMsg);
throw new ObTableUnexpectedException(errMsg);
}
boolean allPartitionsSuccess = true;
for (Map.Entry<Long, ObPair<ObTableParam, List<ObPair<Integer, ObTableOperation>>>> currentEntry : currentPartitions.entrySet()) {
try {
partitionExecute(results, currentEntry);
} catch (Exception e) {
if (shouldRetry(e)) {
retryCount++;
errCode = ((ObTableNeedFetchMetaException)e).getErrorCode();
errMsg = e.getMessage();
List<ObTableOperation> failedOperations = extractOperations(currentEntry.getValue().getRight());
currentPartitions = prepareOperations(failedOperations);
allPartitionsSuccess = false;
break;
} else {
throw e;
}
}
}
if (allPartitionsSuccess) {
break;
}
}
}
public ObTableBatchOperationResult executeInternal() throws Exception {
if (tableName == null || tableName.isEmpty()) {
throw new IllegalArgumentException("table name is null");
}
long start = System.currentTimeMillis();
List<ObTableOperation> operations = batchOperation.getTableOperations();
ObTableOperationResult[] obTableOperationResults = returnOneResult ? new ObTableOperationResult[1]
: new ObTableOperationResult[operations.size()];
Map<Long, ObPair<ObTableParam, List<ObPair<Integer, ObTableOperation>>>> partitions = partitionPrepare();
long getTableTime = System.currentTimeMillis();
final Map<Object, Object> context = ThreadLocalMap.getContextMap();
ConcurrentTaskExecutor executor = null;
if (executorService != null && !executorService.isShutdown() && partitions.size() > 1) {
executor = new ConcurrentTaskExecutor(executorService, partitions.size());
}
for (final Map.Entry<Long, ObPair<ObTableParam, List<ObPair<Integer, ObTableOperation>>>> entry : partitions
.entrySet()) {
try {
if (executor != null) {
// Concurrent execution
ConcurrentTaskExecutor finalExecutor = executor;
executor.execute(new ConcurrentTask() {
@Override
public void doTask() {
try {
ThreadLocalMap.transmitContextMap(context);
executeWithRetries(obTableOperationResults, entry);
} catch (Exception e) {
logger.error(LCD.convert("01-00026"), e);
finalExecutor.collectExceptions(e);
} finally {
ThreadLocalMap.reset();
}
}
});
} else {
// Sequential execution
executeWithRetries(obTableOperationResults, entry);
}
} catch (Exception e) {
logger.error("Error executing retry: {}", entry.getKey(), e);
throw e;
}
}
if (executor != null) {
long estimate = obTableClient.getRuntimeBatchMaxWait() * 1000L * 1000L;
try {
while (estimate > 0) {
long nanos = System.nanoTime();
try {
executor.waitComplete(1, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
throw new ObTableUnexpectedException(
"Batch Concurrent Execute interrupted", e);
}
if (!executor.getThrowableList().isEmpty()) {
throw new ObTableUnexpectedException("Batch Concurrent Execute Error",
executor.getThrowableList().get(0));
}
if (executor.isComplete()) {
break;
}
estimate = estimate - (System.nanoTime() - nanos);
}
} finally {
executor.stop();
}
if (!executor.isComplete()) {
throw new ObTableUnexpectedException("Batch Concurrent Execute Error ["
+ obTableClient.getRpcExecuteTimeout()
+ "]/ms");
}
}
ObTableBatchOperationResult batchOperationResult = new ObTableBatchOperationResult();
for (ObTableOperationResult obTableOperationResult : obTableOperationResults) {
batchOperationResult.addResult(obTableOperationResult);
}
MonitorUtil.info(batchOperationResult, obTableClient.getDatabase(), tableName, "BATCH", "",
obTableOperationResults.length, getTableTime - start, System.currentTimeMillis()
- getTableTime,
obTableClient.getslowQueryMonitorThreshold());
return batchOperationResult;
}
/*
* clear batch operations1
*/
public void clear() {
batchOperation = new ObTableBatchOperation();
}
/*
* Set executor service.
*/
public void setExecutorService(ExecutorService executorService) {
this.executorService = executorService;
}
public boolean isReturningAffectedEntity() {
return returningAffectedEntity;
}
public void setReturningAffectedEntity(boolean returningAffectedEntity) {
this.returningAffectedEntity = returningAffectedEntity;
}
}