|
54 | 54 | import org.apache.hadoop.hbase.TableName;
|
55 | 55 | import org.apache.hadoop.hbase.client.MultiResponse.RegionResult;
|
56 | 56 | import org.apache.hadoop.hbase.client.RetriesExhaustedException.ThrowableWithExtraContext;
|
| 57 | +import org.apache.hadoop.hbase.client.backoff.ClientBackoffPolicy; |
| 58 | +import org.apache.hadoop.hbase.client.backoff.ServerStatistics; |
57 | 59 | import org.apache.hadoop.hbase.ipc.HBaseRpcController;
|
58 | 60 | import org.apache.hadoop.hbase.util.Bytes;
|
59 | 61 | import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
|
@@ -134,6 +136,10 @@ public void addAction(HRegionLocation loc, Action action) {
|
134 | 136 | () -> new RegionRequest(loc)).actions.add(action);
|
135 | 137 | }
|
136 | 138 |
|
| 139 | + public void setRegionRequest(byte[] regionName, RegionRequest regionReq) { |
| 140 | + actionsByRegion.put(regionName, regionReq); |
| 141 | + } |
| 142 | + |
137 | 143 | public int getPriority() {
|
138 | 144 | return actionsByRegion.values().stream().flatMap(rr -> rr.actions.stream())
|
139 | 145 | .mapToInt(Action::getPriority).max().orElse(HConstants.PRIORITY_UNSET);
|
@@ -298,6 +304,8 @@ private void onComplete(Action action, RegionRequest regionReq, int tries, Serve
|
298 | 304 |
|
299 | 305 | private void onComplete(Map<byte[], RegionRequest> actionsByRegion, int tries,
|
300 | 306 | ServerName serverName, MultiResponse resp) {
|
| 307 | + ConnectionUtils.updateStats(conn.getStatisticsTracker(), conn.getConnectionMetrics(), |
| 308 | + serverName, resp); |
301 | 309 | List<Action> failedActions = new ArrayList<>();
|
302 | 310 | MutableBoolean retryImmediately = new MutableBoolean(false);
|
303 | 311 | actionsByRegion.forEach((rn, regionReq) -> {
|
@@ -333,55 +341,88 @@ private void onComplete(Map<byte[], RegionRequest> actionsByRegion, int tries,
|
333 | 341 | }
|
334 | 342 | }
|
335 | 343 |
|
336 |
| - private void send(Map<ServerName, ServerRequest> actionsByServer, int tries) { |
| 344 | + private void sendToServer(ServerName serverName, ServerRequest serverReq, int tries) { |
337 | 345 | long remainingNs;
|
338 | 346 | if (operationTimeoutNs > 0) {
|
339 | 347 | remainingNs = remainingTimeNs();
|
340 | 348 | if (remainingNs <= 0) {
|
341 |
| - failAll(actionsByServer.values().stream().flatMap(m -> m.actionsByRegion.values().stream()) |
342 |
| - .flatMap(r -> r.actions.stream()), tries); |
| 349 | + failAll(serverReq.actionsByRegion.values().stream().flatMap(r -> r.actions.stream()), |
| 350 | + tries); |
343 | 351 | return;
|
344 | 352 | }
|
345 | 353 | } else {
|
346 | 354 | remainingNs = Long.MAX_VALUE;
|
347 | 355 | }
|
348 |
| - actionsByServer.forEach((sn, serverReq) -> { |
349 |
| - ClientService.Interface stub; |
350 |
| - try { |
351 |
| - stub = conn.getRegionServerStub(sn); |
352 |
| - } catch (IOException e) { |
353 |
| - onError(serverReq.actionsByRegion, tries, e, sn); |
354 |
| - return; |
355 |
| - } |
356 |
| - ClientProtos.MultiRequest req; |
357 |
| - List<CellScannable> cells = new ArrayList<>(); |
358 |
| - // Map from a created RegionAction to the original index for a RowMutations within |
359 |
| - // the original list of actions. This will be used to process the results when there |
360 |
| - // is RowMutations in the action list. |
361 |
| - Map<Integer, Integer> rowMutationsIndexMap = new HashMap<>(); |
362 |
| - try { |
363 |
| - req = buildReq(serverReq.actionsByRegion, cells, rowMutationsIndexMap); |
364 |
| - } catch (IOException e) { |
365 |
| - onError(serverReq.actionsByRegion, tries, e, sn); |
366 |
| - return; |
367 |
| - } |
368 |
| - HBaseRpcController controller = conn.rpcControllerFactory.newController(); |
369 |
| - resetController(controller, Math.min(rpcTimeoutNs, remainingNs), |
370 |
| - calcPriority(serverReq.getPriority(), tableName)); |
371 |
| - if (!cells.isEmpty()) { |
372 |
| - controller.setCellScanner(createCellScanner(cells)); |
| 356 | + ClientService.Interface stub; |
| 357 | + try { |
| 358 | + stub = conn.getRegionServerStub(serverName); |
| 359 | + } catch (IOException e) { |
| 360 | + onError(serverReq.actionsByRegion, tries, e, serverName); |
| 361 | + return; |
| 362 | + } |
| 363 | + ClientProtos.MultiRequest req; |
| 364 | + List<CellScannable> cells = new ArrayList<>(); |
| 365 | + // Map from a created RegionAction to the original index for a RowMutations within |
| 366 | + // the original list of actions. This will be used to process the results when there |
| 367 | + // is RowMutations in the action list. |
| 368 | + Map<Integer, Integer> rowMutationsIndexMap = new HashMap<>(); |
| 369 | + try { |
| 370 | + req = buildReq(serverReq.actionsByRegion, cells, rowMutationsIndexMap); |
| 371 | + } catch (IOException e) { |
| 372 | + onError(serverReq.actionsByRegion, tries, e, serverName); |
| 373 | + return; |
| 374 | + } |
| 375 | + HBaseRpcController controller = conn.rpcControllerFactory.newController(); |
| 376 | + resetController(controller, Math.min(rpcTimeoutNs, remainingNs), |
| 377 | + calcPriority(serverReq.getPriority(), tableName)); |
| 378 | + if (!cells.isEmpty()) { |
| 379 | + controller.setCellScanner(createCellScanner(cells)); |
| 380 | + } |
| 381 | + stub.multi(controller, req, resp -> { |
| 382 | + if (controller.failed()) { |
| 383 | + onError(serverReq.actionsByRegion, tries, controller.getFailed(), serverName); |
| 384 | + } else { |
| 385 | + try { |
| 386 | + onComplete(serverReq.actionsByRegion, tries, serverName, ResponseConverter.getResults(req, |
| 387 | + rowMutationsIndexMap, resp, controller.cellScanner())); |
| 388 | + } catch (Exception e) { |
| 389 | + onError(serverReq.actionsByRegion, tries, e, serverName); |
| 390 | + return; |
| 391 | + } |
373 | 392 | }
|
374 |
| - stub.multi(controller, req, resp -> { |
375 |
| - if (controller.failed()) { |
376 |
| - onError(serverReq.actionsByRegion, tries, controller.getFailed(), sn); |
| 393 | + }); |
| 394 | + } |
| 395 | + |
| 396 | + // We will make use of the ServerStatisticTracker to determine whether we need to delay a bit, |
| 397 | + // based on the load of the region server and the region. |
| 398 | + private void sendOrDelay(Map<ServerName, ServerRequest> actionsByServer, int tries) { |
| 399 | + Optional<MetricsConnection> metrics = conn.getConnectionMetrics(); |
| 400 | + Optional<ServerStatisticTracker> optStats = conn.getStatisticsTracker(); |
| 401 | + if (!optStats.isPresent()) { |
| 402 | + actionsByServer.forEach((serverName, serverReq) -> { |
| 403 | + metrics.ifPresent(MetricsConnection::incrNormalRunners); |
| 404 | + sendToServer(serverName, serverReq, tries); |
| 405 | + }); |
| 406 | + return; |
| 407 | + } |
| 408 | + ServerStatisticTracker stats = optStats.get(); |
| 409 | + ClientBackoffPolicy backoffPolicy = conn.getBackoffPolicy(); |
| 410 | + actionsByServer.forEach((serverName, serverReq) -> { |
| 411 | + ServerStatistics serverStats = stats.getStats(serverName); |
| 412 | + Map<Long, ServerRequest> groupByBackoff = new HashMap<>(); |
| 413 | + serverReq.actionsByRegion.forEach((regionName, regionReq) -> { |
| 414 | + long backoff = backoffPolicy.getBackoffTime(serverName, regionName, serverStats); |
| 415 | + groupByBackoff.computeIfAbsent(backoff, k -> new ServerRequest()) |
| 416 | + .setRegionRequest(regionName, regionReq); |
| 417 | + }); |
| 418 | + groupByBackoff.forEach((backoff, sr) -> { |
| 419 | + if (backoff > 0) { |
| 420 | + metrics.ifPresent(m -> m.incrDelayRunnersAndUpdateDelayInterval(backoff)); |
| 421 | + retryTimer.newTimeout(timer -> sendToServer(serverName, sr, tries), backoff, |
| 422 | + TimeUnit.MILLISECONDS); |
377 | 423 | } else {
|
378 |
| - try { |
379 |
| - onComplete(serverReq.actionsByRegion, tries, sn, ResponseConverter.getResults(req, |
380 |
| - rowMutationsIndexMap, resp, controller.cellScanner())); |
381 |
| - } catch (Exception e) { |
382 |
| - onError(serverReq.actionsByRegion, tries, e, sn); |
383 |
| - return; |
384 |
| - } |
| 424 | + metrics.ifPresent(MetricsConnection::incrNormalRunners); |
| 425 | + sendToServer(serverName, sr, tries); |
385 | 426 | }
|
386 | 427 | });
|
387 | 428 | });
|
@@ -454,7 +495,7 @@ private void groupAndSend(Stream<Action> actions, int tries) {
|
454 | 495 | }))
|
455 | 496 | .toArray(CompletableFuture[]::new)), (v, r) -> {
|
456 | 497 | if (!actionsByServer.isEmpty()) {
|
457 |
| - send(actionsByServer, tries); |
| 498 | + sendOrDelay(actionsByServer, tries); |
458 | 499 | }
|
459 | 500 | if (!locateFailed.isEmpty()) {
|
460 | 501 | tryResubmit(locateFailed.stream(), tries, false);
|
|
0 commit comments