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.client.RawAsyncHBaseAdmin.java

@Override
public <S, R> CompletableFuture<R> coprocessorService(Function<RpcChannel, S> stubMaker,
        ServiceCaller<S, R> callable) {
    MasterCoprocessorRpcChannelImpl channel = new MasterCoprocessorRpcChannelImpl(
            this.<Message>newMasterCaller());
    S stub = stubMaker.apply(channel);//from   w  ww  .jav  a  2s . com
    CompletableFuture<R> future = new CompletableFuture<>();
    ClientCoprocessorRpcController controller = new ClientCoprocessorRpcController();
    callable.call(stub, controller, resp -> {
        if (controller.failed()) {
            future.completeExceptionally(controller.getFailed());
        } else {
            future.complete(resp);
        }
    });
    return future;
}

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

@Override
public <S, R> CompletableFuture<R> coprocessorService(Function<RpcChannel, S> stubMaker,
        ServiceCaller<S, R> callable, ServerName serverName) {
    RegionServerCoprocessorRpcChannelImpl channel = new RegionServerCoprocessorRpcChannelImpl(
            this.<Message>newServerCaller().serverName(serverName));
    S stub = stubMaker.apply(channel);// ww w  . j a  v a2  s  .c  o  m
    CompletableFuture<R> future = new CompletableFuture<>();
    ClientCoprocessorRpcController controller = new ClientCoprocessorRpcController();
    callable.call(stub, controller, resp -> {
        if (controller.failed()) {
            future.completeExceptionally(controller.getFailed());
        } else {
            future.complete(resp);
        }
    });
    return future;
}

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

@Override
public CompletableFuture<Void> enableTableReplication(TableName tableName) {
    if (tableName == null) {
        return failedFuture(new IllegalArgumentException("Table name is null"));
    }//from w w w .j a va  2s.  c o  m
    CompletableFuture<Void> future = new CompletableFuture<>();
    addListener(tableExists(tableName), (exist, err) -> {
        if (err != null) {
            future.completeExceptionally(err);
            return;
        }
        if (!exist) {
            future.completeExceptionally(
                    new TableNotFoundException("Table '" + tableName.getNameAsString() + "' does not exists."));
            return;
        }
        addListener(getTableSplits(tableName), (splits, err1) -> {
            if (err1 != null) {
                future.completeExceptionally(err1);
            } else {
                addListener(checkAndSyncTableToPeerClusters(tableName, splits), (result, err2) -> {
                    if (err2 != null) {
                        future.completeExceptionally(err2);
                    } else {
                        addListener(setTableReplication(tableName, true), (result3, err3) -> {
                            if (err3 != null) {
                                future.completeExceptionally(err3);
                            } else {
                                future.complete(result3);
                            }
                        });
                    }
                });
            }
        });
    });
    return future;
}

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

@Override
public CompletableFuture<Void> disableTableReplication(TableName tableName) {
    if (tableName == null) {
        return failedFuture(new IllegalArgumentException("Table name is null"));
    }//from w  ww  . ja  va2s. co m
    CompletableFuture<Void> future = new CompletableFuture<>();
    addListener(tableExists(tableName), (exist, err) -> {
        if (err != null) {
            future.completeExceptionally(err);
            return;
        }
        if (!exist) {
            future.completeExceptionally(
                    new TableNotFoundException("Table '" + tableName.getNameAsString() + "' does not exists."));
            return;
        }
        addListener(setTableReplication(tableName, false), (result, err2) -> {
            if (err2 != null) {
                future.completeExceptionally(err2);
            } else {
                future.complete(result);
            }
        });
    });
    return future;
}

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

private CompletableFuture<byte[][]> getTableSplits(TableName tableName) {
    CompletableFuture<byte[][]> future = new CompletableFuture<>();
    addListener(//  ww  w .  j av a2 s .c om
            getRegions(tableName).thenApply(regions -> regions.stream()
                    .filter(RegionReplicaUtil::isDefaultReplica).collect(Collectors.toList())),
            (regions, err2) -> {
                if (err2 != null) {
                    future.completeExceptionally(err2);
                    return;
                }
                if (regions.size() == 1) {
                    future.complete(null);
                } else {
                    byte[][] splits = new byte[regions.size() - 1][];
                    for (int i = 1; i < regions.size(); i++) {
                        splits[i - 1] = regions.get(i).getStartKey();
                    }
                    future.complete(splits);
                }
            });
    return future;
}

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

/**
 * Connect to peer and check the table descriptor on peer:
 * <ol>/*from   ww  w .j ava  2 s .  c o m*/
 * <li>Create the same table on peer when not exist.</li>
 * <li>Throw an exception if the table already has replication enabled on any of the column
 * families.</li>
 * <li>Throw an exception if the table exists on peer cluster but descriptors are not same.</li>
 * </ol>
 * @param tableName name of the table to sync to the peer
 * @param splits table split keys
 */
private CompletableFuture<Void> checkAndSyncTableToPeerClusters(TableName tableName, byte[][] splits) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    addListener(listReplicationPeers(), (peers, err) -> {
        if (err != null) {
            future.completeExceptionally(err);
            return;
        }
        if (peers == null || peers.size() <= 0) {
            future.completeExceptionally(
                    new IllegalArgumentException("Found no peer cluster for replication."));
            return;
        }
        List<CompletableFuture<Void>> futures = new ArrayList<>();
        peers.stream().filter(peer -> peer.getPeerConfig().needToReplicate(tableName)).forEach(peer -> {
            futures.add(trySyncTableToPeerCluster(tableName, splits, peer));
        });
        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

private CompletableFuture<Void> trySyncTableToPeerCluster(TableName tableName, byte[][] splits,
        ReplicationPeerDescription peer) {
    Configuration peerConf = null;
    try {//from www .  j  a  v  a 2s  .c  o m
        peerConf = ReplicationPeerConfigUtil.getPeerClusterConfiguration(connection.getConfiguration(), peer);
    } catch (IOException e) {
        return failedFuture(e);
    }
    CompletableFuture<Void> future = new CompletableFuture<>();
    addListener(ConnectionFactory.createAsyncConnection(peerConf), (conn, err) -> {
        if (err != null) {
            future.completeExceptionally(err);
            return;
        }
        addListener(getDescriptor(tableName), (tableDesc, err1) -> {
            if (err1 != null) {
                future.completeExceptionally(err1);
                return;
            }
            AsyncAdmin peerAdmin = conn.getAdmin();
            addListener(peerAdmin.tableExists(tableName), (exist, err2) -> {
                if (err2 != null) {
                    future.completeExceptionally(err2);
                    return;
                }
                if (!exist) {
                    CompletableFuture<Void> createTableFuture = null;
                    if (splits == null) {
                        createTableFuture = peerAdmin.createTable(tableDesc);
                    } else {
                        createTableFuture = peerAdmin.createTable(tableDesc, splits);
                    }
                    addListener(createTableFuture, (result, err3) -> {
                        if (err3 != null) {
                            future.completeExceptionally(err3);
                        } else {
                            future.complete(result);
                        }
                    });
                } else {
                    addListener(compareTableWithPeerCluster(tableName, tableDesc, peer, peerAdmin),
                            (result, err4) -> {
                                if (err4 != null) {
                                    future.completeExceptionally(err4);
                                } else {
                                    future.complete(result);
                                }
                            });
                }
            });
        });
    });
    return future;
}

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

private CompletableFuture<Void> compareTableWithPeerCluster(TableName tableName, TableDescriptor tableDesc,
        ReplicationPeerDescription peer, AsyncAdmin peerAdmin) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    addListener(peerAdmin.getDescriptor(tableName), (peerTableDesc, err) -> {
        if (err != null) {
            future.completeExceptionally(err);
            return;
        }/*from w w  w  .  j  a  va  2s. com*/
        if (peerTableDesc == null) {
            future.completeExceptionally(
                    new IllegalArgumentException("Failed to get table descriptor for table "
                            + tableName.getNameAsString() + " from peer cluster " + peer.getPeerId()));
            return;
        }
        if (TableDescriptor.COMPARATOR_IGNORE_REPLICATION.compare(peerTableDesc, tableDesc) != 0) {
            future.completeExceptionally(new IllegalArgumentException(
                    "Table " + tableName.getNameAsString() + " exists in peer cluster " + peer.getPeerId()
                            + ", but the table descriptors are not same when compared with source cluster."
                            + " Thus can not enable the table's replication switch."));
            return;
        }
        future.complete(null);
    });
    return future;
}

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

/**
 * Set the table's replication switch if the table's replication switch is already not set.
 * @param tableName name of the table//ww  w.j a  va  2 s.  c  o m
 * @param enableRep is replication switch enable or disable
 */
private CompletableFuture<Void> setTableReplication(TableName tableName, boolean enableRep) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    addListener(getDescriptor(tableName), (tableDesc, err) -> {
        if (err != null) {
            future.completeExceptionally(err);
            return;
        }
        if (!tableDesc.matchReplicationScope(enableRep)) {
            int scope = enableRep ? HConstants.REPLICATION_SCOPE_GLOBAL : HConstants.REPLICATION_SCOPE_LOCAL;
            TableDescriptor newTableDesc = TableDescriptorBuilder.newBuilder(tableDesc)
                    .setReplicationScope(scope).build();
            addListener(modifyTable(newTableDesc), (result, err2) -> {
                if (err2 != null) {
                    future.completeExceptionally(err2);
                } else {
                    future.complete(result);
                }
            });
        } else {
            future.complete(null);
        }
    });
    return future;
}

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

@Override
public CompletableFuture<CacheEvictionStats> clearBlockCache(TableName tableName) {
    CompletableFuture<CacheEvictionStats> future = new CompletableFuture<>();
    addListener(getTableHRegionLocations(tableName), (locations, err) -> {
        if (err != null) {
            future.completeExceptionally(err);
            return;
        }//from   w  ww .ja v  a  2 s .c om
        Map<ServerName, List<RegionInfo>> regionInfoByServerName = locations.stream()
                .filter(l -> l.getRegion() != null).filter(l -> !l.getRegion().isOffline())
                .filter(l -> l.getServerName() != null).collect(Collectors.groupingBy(l -> l.getServerName(),
                        Collectors.mapping(l -> l.getRegion(), Collectors.toList())));
        List<CompletableFuture<CacheEvictionStats>> futures = new ArrayList<>();
        CacheEvictionStatsAggregator aggregator = new CacheEvictionStatsAggregator();
        for (Map.Entry<ServerName, List<RegionInfo>> entry : regionInfoByServerName.entrySet()) {
            futures.add(clearBlockCache(entry.getKey(), entry.getValue()).whenComplete((stats, err2) -> {
                if (err2 != null) {
                    future.completeExceptionally(unwrapCompletionException(err2));
                } else {
                    aggregator.append(stats);
                }
            }));
        }
        addListener(CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])),
                (ret, err3) -> {
                    if (err3 != null) {
                        future.completeExceptionally(unwrapCompletionException(err3));
                    } else {
                        future.complete(aggregator.sum());
                    }
                });
    });
    return future;
}