Example usage for java.util.concurrent CompletableFuture completeExceptionally

List of usage examples for java.util.concurrent CompletableFuture completeExceptionally

Introduction

In this page you can find the example usage for java.util.concurrent CompletableFuture completeExceptionally.

Prototype

public boolean completeExceptionally(Throwable ex) 

Source Link

Document

If not already completed, causes invocations of #get() and related methods to throw the given exception.

Usage

From source file:org.apache.hadoop.hbase.AsyncMetaTableAccessor.java

public static CompletableFuture<Pair<HRegionInfo, ServerName>> getRegion(RawAsyncTable metaTable,
        byte[] regionName) {
    CompletableFuture<Pair<HRegionInfo, ServerName>> future = new CompletableFuture<>();
    byte[] row = regionName;
    HRegionInfo parsedInfo = null;/*  w w w .j  a  v a  2  s .  c om*/
    try {
        parsedInfo = MetaTableAccessor.parseRegionInfoFromRegionName(regionName);
        row = MetaTableAccessor.getMetaKeyForRegion(parsedInfo);
    } catch (Exception parseEx) {
        // Ignore if regionName is a encoded region name.
    }

    final HRegionInfo finalHRI = parsedInfo;
    metaTable.get(new Get(row).addFamily(HConstants.CATALOG_FAMILY)).whenComplete((r, err) -> {
        if (err != null) {
            future.completeExceptionally(err);
            return;
        }
        RegionLocations locations = MetaTableAccessor.getRegionLocations(r);
        HRegionLocation hrl = locations == null ? null
                : locations.getRegionLocation(finalHRI == null ? 0 : finalHRI.getReplicaId());
        if (hrl == null) {
            future.complete(null);
        } else {
            future.complete(new Pair<>(hrl.getRegionInfo(), hrl.getServerName()));
        }
    });

    return future;
}

From source file:org.apache.hadoop.hbase.client.AsyncBatchRpcRetryingCaller.java

private void failOne(Action action, int tries, Throwable error, long currentTime, String extras) {
    CompletableFuture<T> future = action2Future.get(action);
    if (future.isDone()) {
        return;//from   www.  j a  va  2s  .  com
    }
    ThrowableWithExtraContext errorWithCtx = new ThrowableWithExtraContext(error, currentTime, extras);
    List<ThrowableWithExtraContext> errors = removeErrors(action);
    if (errors == null) {
        errors = Collections.singletonList(errorWithCtx);
    } else {
        errors.add(errorWithCtx);
    }
    future.completeExceptionally(new RetriesExhaustedException(tries - 1, errors));
}

From source file:org.apache.hadoop.hbase.client.AsyncBatchRpcRetryingCaller.java

private void failAll(Stream<Action> actions, int tries) {
    actions.forEach(action -> {/*from  w w  w.  j  a  v a  2s.  c o m*/
        CompletableFuture<T> future = action2Future.get(action);
        if (future.isDone()) {
            return;
        }
        future.completeExceptionally(new RetriesExhaustedException(tries,
                Optional.ofNullable(removeErrors(action)).orElse(Collections.emptyList())));
    });
}

From source file:org.apache.hadoop.hbase.client.AsyncHBaseAdmin.java

private <PREQ, PRESP, RESP> CompletableFuture<RESP> call(HBaseRpcController controller,
        MasterService.Interface stub, PREQ preq, MasterRpcCall<PRESP, PREQ> rpcCall,
        Converter<RESP, PRESP> respConverter) {
    CompletableFuture<RESP> future = new CompletableFuture<>();
    rpcCall.call(stub, controller, preq, new RpcCallback<PRESP>() {

        @Override/*w  w w  .  java  2 s.co m*/
        public void run(PRESP resp) {
            if (controller.failed()) {
                future.completeExceptionally(controller.getFailed());
            } else {
                try {
                    future.complete(respConverter.convert(resp));
                } catch (IOException e) {
                    future.completeExceptionally(e);
                }
            }
        }
    });
    return future;
}

From source file:org.apache.hadoop.hbase.client.AsyncHBaseAdmin.java

private <PREQ, PRESP, RESP> CompletableFuture<RESP> adminCall(HBaseRpcController controller,
        AdminService.Interface stub, PREQ preq, AdminRpcCall<PRESP, PREQ> rpcCall,
        Converter<RESP, PRESP> respConverter) {

    CompletableFuture<RESP> future = new CompletableFuture<>();
    rpcCall.call(stub, controller, preq, new RpcCallback<PRESP>() {

        @Override/*from   w  w w . j  a  va2 s  .c o m*/
        public void run(PRESP resp) {
            if (controller.failed()) {
                future.completeExceptionally(new IOException(controller.errorText()));
            } else {
                try {
                    future.complete(respConverter.convert(resp));
                } catch (IOException e) {
                    future.completeExceptionally(e);
                }
            }
        }
    });
    return future;
}

From source file:org.apache.hadoop.hbase.client.AsyncHBaseAdmin.java

private CompletableFuture<HTableDescriptor[]> batchTableOperations(Pattern pattern, TableOperator operator,
        String operationType) {//from  ww w.j  a v  a2s  .c  o  m
    CompletableFuture<HTableDescriptor[]> future = new CompletableFuture<>();
    List<HTableDescriptor> failed = new LinkedList<>();
    listTables(pattern, false).whenComplete((tables, error) -> {
        if (error != null) {
            future.completeExceptionally(error);
            return;
        }
        CompletableFuture[] futures = Arrays.stream(tables)
                .map((table) -> operator.operate(table.getTableName()).whenComplete((v, ex) -> {
                    if (ex != null) {
                        LOG.info("Failed to " + operationType + " table " + table.getTableName(), ex);
                        failed.add(table);
                    }
                })).toArray(size -> new CompletableFuture[size]);
        CompletableFuture.allOf(futures).thenAccept((v) -> {
            future.complete(failed.toArray(new HTableDescriptor[failed.size()]));
        });
    });
    return future;
}

From source file:org.apache.hadoop.hbase.client.AsyncHBaseAdmin.java

@Override
public CompletableFuture<HTableDescriptor> getTableDescriptor(TableName tableName) {
    CompletableFuture<HTableDescriptor> future = new CompletableFuture<>();
    this.<List<TableSchema>>newMasterCaller()
            .action((controller, stub) -> this
                    .<GetTableDescriptorsRequest, GetTableDescriptorsResponse, List<TableSchema>>call(
                            controller, stub, RequestConverter.buildGetTableDescriptorsRequest(tableName),
                            (s, c, req, done) -> s.getTableDescriptors(c, req, done),
                            (resp) -> resp.getTableSchemaList()))
            .call().whenComplete((tableSchemas, error) -> {
                if (error != null) {
                    future.completeExceptionally(error);
                    return;
                }//from ww  w. j  ava  2 s.c om
                if (!tableSchemas.isEmpty()) {
                    future.complete(ProtobufUtil.convertToHTableDesc(tableSchemas.get(0)));
                } else {
                    future.completeExceptionally(new TableNotFoundException(tableName.getNameAsString()));
                }
            });
    return future;
}

From source file:org.apache.hadoop.hbase.client.AsyncHBaseAdmin.java

@Override
public CompletableFuture<Void> closeRegion(byte[] regionName, String serverName) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    getRegion(regionName).whenComplete((p, err) -> {
        if (err != null) {
            future.completeExceptionally(err);
            return;
        }/*from ww w . j  av a2s .c o m*/
        if (p == null || p.getFirst() == null) {
            future.completeExceptionally(new UnknownRegionException(Bytes.toStringBinary(regionName)));
            return;
        }
        if (serverName != null) {
            closeRegion(ServerName.valueOf(serverName), p.getFirst()).whenComplete((p2, err2) -> {
                if (err2 != null) {
                    future.completeExceptionally(err2);
                } else {
                    future.complete(null);
                }
            });
        } else {
            if (p.getSecond() == null) {
                future.completeExceptionally(new NotServingRegionException(regionName));
            } else {
                closeRegion(p.getSecond(), p.getFirst()).whenComplete((p2, err2) -> {
                    if (err2 != null) {
                        future.completeExceptionally(err2);
                    } else {
                        future.complete(null);
                    }
                });
            }
        }
    });
    return future;
}

From source file:org.apache.hadoop.hbase.client.AsyncHBaseAdmin.java

CompletableFuture<Pair<HRegionInfo, ServerName>> getRegion(byte[] regionName) {
    if (regionName == null) {
        return failedFuture(new IllegalArgumentException("Pass region name"));
    }// w ww  .  ja v  a 2s . com
    CompletableFuture<Pair<HRegionInfo, ServerName>> future = new CompletableFuture<>();
    AsyncMetaTableAccessor.getRegion(metaTable, regionName).whenComplete((p, err) -> {
        if (err != null) {
            future.completeExceptionally(err);
        } else if (p != null) {
            future.complete(p);
        } else {
            metaTable.scanAll(new Scan().setReadType(ReadType.PREAD).addFamily(HConstants.CATALOG_FAMILY))
                    .whenComplete((results, err2) -> {
                        if (err2 != null) {
                            future.completeExceptionally(err2);
                            return;
                        }
                        String encodedName = Bytes.toString(regionName);
                        if (results != null && !results.isEmpty()) {
                            for (Result r : results) {
                                if (r.isEmpty() || MetaTableAccessor.getHRegionInfo(r) == null)
                                    continue;
                                RegionLocations rl = MetaTableAccessor.getRegionLocations(r);
                                if (rl != null) {
                                    for (HRegionLocation h : rl.getRegionLocations()) {
                                        if (h != null
                                                && encodedName.equals(h.getRegionInfo().getEncodedName())) {
                                            future.complete(new Pair<>(h.getRegionInfo(), h.getServerName()));
                                            return;
                                        }
                                    }
                                }
                            }
                        }
                        future.complete(null);
                    });
        }
    });
    return future;
}

From source file:org.apache.hadoop.hbase.client.AsyncHBaseAdmin.java

private CompletableFuture<Void> waitProcedureResult(CompletableFuture<Long> procFuture) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    procFuture.whenComplete((procId, error) -> {
        if (error != null) {
            future.completeExceptionally(error);
            return;
        }/*from  w w  w. ja  v  a  2 s.  c o  m*/
        getProcedureResult(procId, future);
    });
    return future;
}