@Override public void clearRegionLocationCache() { conn.getLocator().clearCache(tableName); } }
@Override public CompletableFuture<HRegionLocation> getRegionLocation(byte[] row, int replicaId, boolean reload) { return conn.getLocator().getRegionLocation(tableName, row, replicaId, RegionLocateType.CURRENT, reload, -1L); }
@Override public CompletableFuture<List<HRegionLocation>> getRegionLocations(byte[] row, boolean reload) { return conn.getLocator() .getRegionLocations(tableName, row, RegionLocateType.CURRENT, reload, -1L) .thenApply(locs -> Arrays.asList(locs.getRegionLocations())); }
@Override public void execute() { addListener(conn.getLocator().getRegionLocation(tableName, startKey, startKeyInclusive ? RegionLocateType.CURRENT : RegionLocateType.AFTER, operationTimeoutNs), (loc, error) -> onLocateComplete(stubMaker, callable, callback, new ArrayList<>(), endKey, endKeyInclusive, new AtomicBoolean(false), new AtomicInteger(0), loc, error)); } }
@Override public CompletableFuture<List<RegionInfo>> getRegions(TableName tableName) { if (tableName.equals(META_TABLE_NAME)) { return connection.getLocator().getRegionLocation(tableName, null, null, operationTimeoutNs) .thenApply(loc -> Collections.singletonList(loc.getRegion())); } else { return AsyncMetaTableAccessor.getTableHRegionLocations(metaTable, Optional.of(tableName)) .thenApply( locs -> locs.stream().map(loc -> loc.getRegion()).collect(Collectors.toList())); } }
@Override void onFinished() { connection.getLocator().clearCache(this.tableName); super.onFinished(); } }
private void onError(Map<byte[], RegionRequest> actionsByRegion, int tries, Throwable t, ServerName serverName) { Throwable error = translateException(t); logException(tries, () -> actionsByRegion.values().stream(), error, serverName); actionsByRegion.forEach( (rn, regionReq) -> conn.getLocator().updateCachedLocationOnError(regionReq.loc, error)); if (error instanceof DoNotRetryIOException || tries >= maxAttempts) { failAll(actionsByRegion.values().stream().flatMap(r -> r.actions.stream()), tries, error, serverName); return; } List<Action> copiedActions = actionsByRegion.values().stream().flatMap(r -> r.actions.stream()) .collect(Collectors.toList()); addError(copiedActions, error, serverName); tryResubmit(copiedActions.stream(), tries); }
@Override public CompletableFuture<Result> get(Get get) { return timelineConsistentRead(conn.getLocator(), tableName, get, get.getRow(), RegionLocateType.CURRENT, replicaId -> get(get, replicaId), readRpcTimeoutNs, conn.connConf.getPrimaryCallTimeoutNs(), retryTimer); }
conn.getLocator().updateCachedLocationOnError(regionReq.loc, error); if (error instanceof DoNotRetryIOException || tries >= maxAttempts) { failAll(regionReq.actions.stream(), tries, error, serverName);
@SuppressWarnings("unchecked") private void onComplete(Action action, RegionRequest regionReq, int tries, ServerName serverName, RegionResult regionResult, List<Action> failedActions, Throwable regionException) { Object result = regionResult.result.getOrDefault(action.getOriginalIndex(), regionException); if (result == null) { LOG.error("Server " + serverName + " sent us neither result nor exception for row '" + Bytes.toStringBinary(action.getAction().getRow()) + "' of " + regionReq.loc.getRegion().getRegionNameAsString()); addError(action, new RuntimeException("Invalid response"), serverName); failedActions.add(action); } else if (result instanceof Throwable) { Throwable error = translateException((Throwable) result); logException(tries, () -> Stream.of(regionReq), error, serverName); conn.getLocator().updateCachedLocationOnError(regionReq.loc, error); if (error instanceof DoNotRetryIOException || tries >= maxAttempts) { failOne(action, tries, error, EnvironmentEdgeManager.currentTime(), getExtraContextForError(serverName)); } else { failedActions.add(action); } } else { action2Future.get(action).complete((T) result); } }
private void openScanner() { incRegionCountMetrics(scanMetrics); openScannerTries.set(1); addListener( timelineConsistentRead(conn.getLocator(), tableName, scan, scan.getStartRow(), getLocateType(scan), this::openScanner, rpcTimeoutNs, getPrimaryTimeoutNs(), retryTimer), (resp, error) -> { if (error != null) { consumer.onError(error); return; } startScan(resp); }); }
ConcurrentLinkedQueue<Action> locateFailed = new ConcurrentLinkedQueue<>(); addListener(CompletableFuture.allOf(actions .map(action -> conn.getLocator().getRegionLocation(tableName, action.getAction().getRow(), RegionLocateType.CURRENT, locateTimeoutNs).whenComplete((loc, error) -> { if (error != null) {
private <S, R> void onLocateComplete(Function<RpcChannel, S> stubMaker, ServiceCaller<S, R> callable, CoprocessorCallback<R> callback, List<HRegionLocation> locs, byte[] endKey, boolean endKeyInclusive, AtomicBoolean locateFinished, AtomicInteger unfinishedRequest, HRegionLocation loc, Throwable error) { if (error != null) { callback.onError(error); return; } unfinishedRequest.incrementAndGet(); RegionInfo region = loc.getRegion(); if (locateFinished(region, endKey, endKeyInclusive)) { locateFinished.set(true); } else { addListener( conn.getLocator().getRegionLocation(tableName, region.getEndKey(), RegionLocateType.CURRENT, operationTimeoutNs), (l, e) -> onLocateComplete(stubMaker, callable, callback, locs, endKey, endKeyInclusive, locateFinished, unfinishedRequest, l, e)); } coprocessorService(stubMaker, callable, region, region.getStartKey()).whenComplete((r, e) -> { if (e != null) { callback.onRegionError(region, e); } else { callback.onRegionComplete(region, r); } if (unfinishedRequest.decrementAndGet() == 0 && locateFinished.get()) { callback.onComplete(); } }); }
private void call(HRegionLocation loc) { ClientService.Interface stub; try { stub = conn.getRegionServerStub(loc.getServerName()); } catch (IOException e) { onError(e, () -> "Get async stub to " + loc.getServerName() + " for '" + Bytes.toStringBinary(row) + "' in " + loc.getRegion().getEncodedName() + " of " + tableName + " failed", err -> conn.getLocator().updateCachedLocationOnError(loc, err)); return; } resetCallTimeout(); callable.call(controller, loc, stub).whenComplete((result, error) -> { if (error != null) { onError(error, () -> "Call to " + loc.getServerName() + " for '" + Bytes.toStringBinary(row) + "' in " + loc.getRegion().getEncodedName() + " of " + tableName + " failed", err -> conn.getLocator().updateCachedLocationOnError(loc, err)); return; } future.complete(result); }); }
@Override protected void doCall() { long locateTimeoutNs; if (operationTimeoutNs > 0) { locateTimeoutNs = remainingTimeNs(); if (locateTimeoutNs <= 0) { completeExceptionally(); return; } } else { locateTimeoutNs = -1L; } addListener( conn.getLocator().getRegionLocation(tableName, row, replicaId, locateType, locateTimeoutNs), (loc, error) -> { if (error != null) { onError(error, () -> "Locate '" + Bytes.toStringBinary(row) + "' in " + tableName + " failed", err -> { }); return; } call(loc); }); }
@BeforeClass public static void setUp() throws Exception { Configuration conf = TEST_UTIL.getConfiguration(); conf.set(REGION_COPROCESSOR_CONF_KEY, SleepRegionObserver.class.getName()); conf.setLong(HBASE_CLIENT_META_OPERATION_TIMEOUT, 2000); TEST_UTIL.startMiniCluster(1); TEST_UTIL.createTable(TABLE_NAME, FAMILY); TEST_UTIL.waitTableAvailable(TABLE_NAME); AsyncRegistry registry = AsyncRegistryFactory.getRegistry(TEST_UTIL.getConfiguration()); CONN = new AsyncConnectionImpl(TEST_UTIL.getConfiguration(), registry, registry.getClusterId().get(), User.getCurrent()); LOCATOR = CONN.getLocator(); }
@Override public void execute() { conn.getLocator().getRegionLocation(tableName, startKey, startKeyInclusive ? RegionLocateType.CURRENT : RegionLocateType.AFTER, operationTimeoutNs) .whenComplete( (loc, error) -> onLocateComplete(stubMaker, callable, callback, new ArrayList<>(), endKey, endKeyInclusive, new AtomicBoolean(false), new AtomicInteger(0), loc, error)); } }
@Override public CompletableFuture<List<RegionInfo>> getRegions(TableName tableName) { if (tableName.equals(META_TABLE_NAME)) { return connection.getLocator().getRegionLocation(tableName, null, null, operationTimeoutNs) .thenApply(loc -> Collections.singletonList(loc.getRegion())); } else { return AsyncMetaTableAccessor.getTableHRegionLocations(metaTable, Optional.of(tableName)) .thenApply( locs -> locs.stream().map(loc -> loc.getRegion()).collect(Collectors.toList())); } }
@Override void onFinished() { connection.getLocator().clearCache(this.tableName); super.onFinished(); } }
private void onError(Map<byte[], RegionRequest> actionsByRegion, int tries, Throwable t, ServerName serverName) { Throwable error = translateException(t); logException(tries, () -> actionsByRegion.values().stream(), error, serverName); actionsByRegion .forEach((rn, regionReq) -> conn.getLocator().updateCachedLocation(regionReq.loc, error)); if (error instanceof DoNotRetryIOException || tries >= maxAttempts) { failAll(actionsByRegion.values().stream().flatMap(r -> r.actions.stream()), tries, error, serverName); return; } List<Action> copiedActions = actionsByRegion.values().stream().flatMap(r -> r.actions.stream()) .collect(Collectors.toList()); addError(copiedActions, error, serverName); tryResubmit(copiedActions.stream(), tries); }