List of usage examples for java.util.concurrent CompletableFuture CompletableFuture
public CompletableFuture()
From source file:org.apache.hadoop.hbase.client.RawAsyncHBaseAdmin.java
@Override public CompletableFuture<Void> execProcedure(String signature, String instance, Map<String, String> props) { CompletableFuture<Void> future = new CompletableFuture<>(); ProcedureDescription procDesc = ProtobufUtil.buildProcedureDescription(signature, instance, props); addListener(this.<Long>newMasterCaller() .action((controller, stub) -> this.<ExecProcedureRequest, ExecProcedureResponse, Long>call( controller, stub, ExecProcedureRequest.newBuilder().setProcedure(procDesc).build(), (s, c, req, done) -> s.execProcedure(c, req, done), resp -> resp.getExpectedTimeout())) .call(), (expectedTimeout, err) -> { if (err != null) { future.completeExceptionally(err); return; }//from w w w. j ava 2s .c o m TimerTask pollingTask = new TimerTask() { int tries = 0; long startTime = EnvironmentEdgeManager.currentTime(); long endTime = startTime + expectedTimeout; long maxPauseTime = expectedTimeout / maxAttempts; @Override public void run(Timeout timeout) throws Exception { if (EnvironmentEdgeManager.currentTime() < endTime) { addListener(isProcedureFinished(signature, instance, props), (done, err2) -> { if (err2 != null) { future.completeExceptionally(err2); return; } if (done) { future.complete(null); } else { // retry again after pauseTime. long pauseTime = ConnectionUtils .getPauseTime(TimeUnit.NANOSECONDS.toMillis(pauseNs), ++tries); pauseTime = Math.min(pauseTime, maxPauseTime); AsyncConnectionImpl.RETRY_TIMER.newTimeout(this, pauseTime, TimeUnit.MICROSECONDS); } }); } else { future.completeExceptionally( new IOException("Procedure '" + signature + " : " + instance + "' wasn't completed in expectedTime:" + expectedTimeout + " ms")); } } }; // Queue the polling task into RETRY_TIMER to poll procedure state asynchronously. AsyncConnectionImpl.RETRY_TIMER.newTimeout(pollingTask, 1, TimeUnit.MILLISECONDS); }); return future; }
From source file:org.apache.hadoop.hbase.client.RawAsyncHBaseAdmin.java
/** * Get the region location for the passed region name. The region name may be a full region name * or encoded region name. If the region does not found, then it'll throw an * UnknownRegionException wrapped by a {@link CompletableFuture} * @param regionNameOrEncodedRegionName region name or encoded region name * @return region location, wrapped by a {@link CompletableFuture} */// w w w . j a v a 2 s .co m @VisibleForTesting CompletableFuture<HRegionLocation> getRegionLocation(byte[] regionNameOrEncodedRegionName) { if (regionNameOrEncodedRegionName == null) { return failedFuture(new IllegalArgumentException("Passed region name can't be null")); } try { CompletableFuture<Optional<HRegionLocation>> future; if (RegionInfo.isEncodedRegionName(regionNameOrEncodedRegionName)) { String encodedName = Bytes.toString(regionNameOrEncodedRegionName); if (encodedName.length() < RegionInfo.MD5_HEX_LENGTH) { // old format encodedName, should be meta region future = connection.registry.getMetaRegionLocation() .thenApply(locs -> Stream.of(locs.getRegionLocations()) .filter(loc -> loc.getRegion().getEncodedName().equals(encodedName)) .findFirst()); } else { future = AsyncMetaTableAccessor.getRegionLocationWithEncodedName(metaTable, regionNameOrEncodedRegionName); } } else { RegionInfo regionInfo = MetaTableAccessor .parseRegionInfoFromRegionName(regionNameOrEncodedRegionName); if (regionInfo.isMetaRegion()) { future = connection.registry.getMetaRegionLocation() .thenApply(locs -> Stream.of(locs.getRegionLocations()) .filter(loc -> loc.getRegion().getReplicaId() == regionInfo.getReplicaId()) .findFirst()); } else { future = AsyncMetaTableAccessor.getRegionLocation(metaTable, regionNameOrEncodedRegionName); } } CompletableFuture<HRegionLocation> returnedFuture = new CompletableFuture<>(); addListener(future, (location, err) -> { if (err != null) { returnedFuture.completeExceptionally(err); return; } if (!location.isPresent() || location.get().getRegion() == null) { returnedFuture.completeExceptionally( new UnknownRegionException("Invalid region name or encoded region name: " + Bytes.toStringBinary(regionNameOrEncodedRegionName))); } else { returnedFuture.complete(location.get()); } }); return returnedFuture; } catch (IOException e) { return failedFuture(e); } }
From source file:org.apache.hadoop.hbase.client.RawAsyncHBaseAdmin.java
/** * Get the region info for the passed region name. The region name may be a full region name or * encoded region name. If the region does not found, then it'll throw an UnknownRegionException * wrapped by a {@link CompletableFuture} * @param regionNameOrEncodedRegionName//from w w w.j a va 2 s . c o m * @return region info, wrapped by a {@link CompletableFuture} */ private CompletableFuture<RegionInfo> getRegionInfo(byte[] regionNameOrEncodedRegionName) { if (regionNameOrEncodedRegionName == null) { return failedFuture(new IllegalArgumentException("Passed region name can't be null")); } if (Bytes.equals(regionNameOrEncodedRegionName, RegionInfoBuilder.FIRST_META_REGIONINFO.getRegionName()) || Bytes.equals(regionNameOrEncodedRegionName, RegionInfoBuilder.FIRST_META_REGIONINFO.getEncodedNameAsBytes())) { return CompletableFuture.completedFuture(RegionInfoBuilder.FIRST_META_REGIONINFO); } CompletableFuture<RegionInfo> future = new CompletableFuture<>(); addListener(getRegionLocation(regionNameOrEncodedRegionName), (location, err) -> { if (err != null) { future.completeExceptionally(err); } else { future.complete(location.getRegion()); } }); return future; }
From source file:org.apache.hadoop.hbase.client.RawAsyncHBaseAdmin.java
private CompletableFuture<Void> waitProcedureResult(CompletableFuture<Long> procFuture) { CompletableFuture<Void> future = new CompletableFuture<>(); addListener(procFuture, (procId, error) -> { if (error != null) { future.completeExceptionally(error); return; }/*from w ww .ja v a 2s . co m*/ getProcedureResult(procId, future, 0); }); return future; }
From source file:org.apache.hadoop.hbase.client.RawAsyncHBaseAdmin.java
@Override public CompletableFuture<Void> updateConfiguration() { CompletableFuture<Void> future = new CompletableFuture<Void>(); addListener(getClusterMetrics(EnumSet.of(Option.LIVE_SERVERS, Option.MASTER, Option.BACKUP_MASTERS)), (status, err) -> {/*from w w w.j a v a 2s . c o m*/ if (err != null) { future.completeExceptionally(err); } else { List<CompletableFuture<Void>> futures = new ArrayList<>(); status.getLiveServerMetrics().keySet() .forEach(server -> futures.add(updateConfiguration(server))); futures.add(updateConfiguration(status.getMasterName())); status.getBackupMasterNames().forEach(master -> futures.add(updateConfiguration(master))); addListener( CompletableFuture.allOf(futures.toArray(new CompletableFuture<?>[futures.size()])), (result, err2) -> { if (err2 != null) { future.completeExceptionally(err2); } else { future.complete(result); } }); } }); return future; }
From source file:org.apache.hadoop.hbase.client.RawAsyncHBaseAdmin.java
@Override public CompletableFuture<CompactionState> getCompactionState(TableName tableName, CompactType compactType) { CompletableFuture<CompactionState> future = new CompletableFuture<>(); switch (compactType) { case MOB://from w w w . j a va2s . c om addListener(connection.registry.getMasterAddress(), (serverName, err) -> { if (err != null) { future.completeExceptionally(err); return; } RegionInfo regionInfo = RegionInfo.createMobRegionInfo(tableName); addListener(this.<GetRegionInfoResponse>newAdminCaller().serverName(serverName) .action((controller, stub) -> this .<GetRegionInfoRequest, GetRegionInfoResponse, GetRegionInfoResponse>adminCall( controller, stub, RequestConverter.buildGetRegionInfoRequest(regionInfo.getRegionName(), true), (s, c, req, done) -> s.getRegionInfo(controller, req, done), resp -> resp)) .call(), (resp2, err2) -> { if (err2 != null) { future.completeExceptionally(err2); } else { if (resp2.hasCompactionState()) { future.complete(ProtobufUtil.createCompactionState(resp2.getCompactionState())); } else { future.complete(CompactionState.NONE); } } }); }); break; case NORMAL: addListener(getTableHRegionLocations(tableName), (locations, err) -> { if (err != null) { future.completeExceptionally(err); return; } ConcurrentLinkedQueue<CompactionState> regionStates = new ConcurrentLinkedQueue<>(); List<CompletableFuture<CompactionState>> futures = new ArrayList<>(); locations.stream().filter(loc -> loc.getServerName() != null).filter(loc -> loc.getRegion() != null) .filter(loc -> !loc.getRegion().isOffline()).map(loc -> loc.getRegion().getRegionName()) .forEach(region -> { futures.add(getCompactionStateForRegion(region).whenComplete((regionState, err2) -> { // If any region compaction state is MAJOR_AND_MINOR // the table compaction state is MAJOR_AND_MINOR, too. if (err2 != null) { future.completeExceptionally(unwrapCompletionException(err2)); } else if (regionState == CompactionState.MAJOR_AND_MINOR) { future.complete(regionState); } else { regionStates.add(regionState); } })); }); addListener(CompletableFuture.allOf(futures.toArray(new CompletableFuture<?>[futures.size()])), (ret, err3) -> { // If future not completed, check all regions's compaction state if (!future.isCompletedExceptionally() && !future.isDone()) { CompactionState state = CompactionState.NONE; for (CompactionState regionState : regionStates) { switch (regionState) { case MAJOR: if (state == CompactionState.MINOR) { future.complete(CompactionState.MAJOR_AND_MINOR); } else { state = CompactionState.MAJOR; } break; case MINOR: if (state == CompactionState.MAJOR) { future.complete(CompactionState.MAJOR_AND_MINOR); } else { state = CompactionState.MINOR; } break; case NONE: default: } } if (!future.isDone()) { future.complete(state); } } }); }); break; default: throw new IllegalArgumentException("Unknown compactType: " + compactType); } return future; }
From source file:org.apache.hadoop.hbase.client.RawAsyncHBaseAdmin.java
@Override public CompletableFuture<CompactionState> getCompactionStateForRegion(byte[] regionName) { CompletableFuture<CompactionState> future = new CompletableFuture<>(); addListener(getRegionLocation(regionName), (location, err) -> { if (err != null) { future.completeExceptionally(err); return; }/*from w w w . j a v a2s .c om*/ ServerName serverName = location.getServerName(); if (serverName == null) { future.completeExceptionally(new NoServerForRegionException(Bytes.toStringBinary(regionName))); return; } addListener(this.<GetRegionInfoResponse>newAdminCaller().action((controller, stub) -> this .<GetRegionInfoRequest, GetRegionInfoResponse, GetRegionInfoResponse>adminCall(controller, stub, RequestConverter.buildGetRegionInfoRequest(location.getRegion().getRegionName(), true), (s, c, req, done) -> s.getRegionInfo(controller, req, done), resp -> resp)) .serverName(serverName).call(), (resp2, err2) -> { if (err2 != null) { future.completeExceptionally(err2); } else { if (resp2.hasCompactionState()) { future.complete(ProtobufUtil.createCompactionState(resp2.getCompactionState())); } else { future.complete(CompactionState.NONE); } } }); }); return future; }
From source file:org.apache.hadoop.hbase.client.RawAsyncHBaseAdmin.java
@Override public CompletableFuture<Optional<Long>> getLastMajorCompactionTimestampForRegion(byte[] regionName) { CompletableFuture<Optional<Long>> future = new CompletableFuture<>(); // regionName may be a full region name or encoded region name, so getRegionInfo(byte[]) first addListener(getRegionInfo(regionName), (region, err) -> { if (err != null) { future.completeExceptionally(err); return; }//w w w . j a v a 2 s . c o m MajorCompactionTimestampForRegionRequest.Builder builder = MajorCompactionTimestampForRegionRequest .newBuilder(); builder.setRegion(RequestConverter.buildRegionSpecifier(RegionSpecifierType.REGION_NAME, regionName)); addListener(this.<Optional<Long>>newMasterCaller().action((controller, stub) -> this .<MajorCompactionTimestampForRegionRequest, MajorCompactionTimestampResponse, Optional<Long>>call( controller, stub, builder.build(), (s, c, req, done) -> s.getLastMajorCompactionTimestampForRegion(c, req, done), ProtobufUtil::toOptionalTimestamp)) .call(), (timestamp, err2) -> { if (err2 != null) { future.completeExceptionally(err2); } else { future.complete(timestamp); } }); }); return future; }
From source file:org.apache.hadoop.hbase.client.RawAsyncHBaseAdmin.java
@Override public CompletableFuture<Map<ServerName, Boolean>> compactionSwitch(boolean switchState, List<String> serverNamesList) { CompletableFuture<Map<ServerName, Boolean>> future = new CompletableFuture<>(); addListener(getRegionServerList(serverNamesList), (serverNames, err) -> { if (err != null) { future.completeExceptionally(err); return; }//w w w . j a v a 2 s.com // Accessed by multiple threads. Map<ServerName, Boolean> serverStates = new ConcurrentHashMap<>(serverNames.size()); List<CompletableFuture<Boolean>> futures = new ArrayList<>(serverNames.size()); serverNames.stream().forEach(serverName -> { futures.add(switchCompact(serverName, switchState).whenComplete((serverState, err2) -> { if (err2 != null) { future.completeExceptionally(unwrapCompletionException(err2)); } else { serverStates.put(serverName, serverState); } })); }); addListener(CompletableFuture.allOf(futures.toArray(new CompletableFuture<?>[futures.size()])), (ret, err3) -> { if (!future.isCompletedExceptionally()) { if (err3 != null) { future.completeExceptionally(err3); } else { future.complete(serverStates); } } }); }); return future; }
From source file:org.apache.hadoop.hbase.client.RawAsyncHBaseAdmin.java
private CompletableFuture<List<ServerName>> getRegionServerList(List<String> serverNamesList) { CompletableFuture<List<ServerName>> future = new CompletableFuture<>(); if (serverNamesList.isEmpty()) { CompletableFuture<ClusterMetrics> clusterMetricsCompletableFuture = getClusterMetrics( EnumSet.of(Option.LIVE_SERVERS)); addListener(clusterMetricsCompletableFuture, (clusterMetrics, err) -> { if (err != null) { future.completeExceptionally(err); } else { future.complete(new ArrayList<>(clusterMetrics.getLiveServerMetrics().keySet())); }/* w w w . ja v a2s . co m*/ }); return future; } else { List<ServerName> serverList = new ArrayList<>(); for (String regionServerName : serverNamesList) { ServerName serverName = null; try { serverName = ServerName.valueOf(regionServerName); } catch (Exception e) { future.completeExceptionally( new IllegalArgumentException(String.format("ServerName format: %s", regionServerName))); } if (serverName == null) { future.completeExceptionally( new IllegalArgumentException(String.format("Null ServerName: %s", regionServerName))); } } future.complete(serverList); } return future; }