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 CompletableFuture<Void> flush(TableName tableName) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    addListener(tableExists(tableName), (exists, err) -> {
        if (err != null) {
            future.completeExceptionally(err);
        } else if (!exists) {
            future.completeExceptionally(new TableNotFoundException(tableName));
        } else {/*from   w ww .  ja v  a 2s. c  o m*/
            addListener(isTableEnabled(tableName), (tableEnabled, err2) -> {
                if (err2 != null) {
                    future.completeExceptionally(err2);
                } else if (!tableEnabled) {
                    future.completeExceptionally(new TableNotEnabledException(tableName));
                } else {
                    addListener(execProcedure(FLUSH_TABLE_PROCEDURE_SIGNATURE, tableName.getNameAsString(),
                            new HashMap<>()), (ret, err3) -> {
                                if (err3 != null) {
                                    future.completeExceptionally(err3);
                                } else {
                                    future.complete(ret);
                                }
                            });
                }
            });
        }
    });
    return future;
}

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

@Override
public CompletableFuture<Void> flushRegion(byte[] regionName) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    addListener(getRegionLocation(regionName), (location, err) -> {
        if (err != null) {
            future.completeExceptionally(err);
            return;
        }//  www .  java2s . co  m
        ServerName serverName = location.getServerName();
        if (serverName == null) {
            future.completeExceptionally(new NoServerForRegionException(Bytes.toStringBinary(regionName)));
            return;
        }
        addListener(flush(serverName, location.getRegion()), (ret, err2) -> {
            if (err2 != null) {
                future.completeExceptionally(err2);
            } else {
                future.complete(ret);
            }
        });
    });
    return future;
}

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

@Override
public CompletableFuture<Void> flushRegionServer(ServerName sn) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    addListener(getRegions(sn), (hRegionInfos, err) -> {
        if (err != null) {
            future.completeExceptionally(err);
            return;
        }//w  w  w.j  a v  a2  s  .  c o  m
        List<CompletableFuture<Void>> compactFutures = new ArrayList<>();
        if (hRegionInfos != null) {
            hRegionInfos.forEach(region -> compactFutures.add(flush(sn, region)));
        }
        addListener(
                CompletableFuture
                        .allOf(compactFutures.toArray(new CompletableFuture<?>[compactFutures.size()])),
                (ret, err2) -> {
                    if (err2 != null) {
                        future.completeExceptionally(err2);
                    } else {
                        future.complete(ret);
                    }
                });
    });
    return future;
}

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

private CompletableFuture<Void> compactRegionServer(ServerName sn, boolean major) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    addListener(getRegions(sn), (hRegionInfos, err) -> {
        if (err != null) {
            future.completeExceptionally(err);
            return;
        }/*  www.  j  ava  2  s.  co  m*/
        List<CompletableFuture<Void>> compactFutures = new ArrayList<>();
        if (hRegionInfos != null) {
            hRegionInfos.forEach(region -> compactFutures.add(compact(sn, region, major, null)));
        }
        addListener(
                CompletableFuture
                        .allOf(compactFutures.toArray(new CompletableFuture<?>[compactFutures.size()])),
                (ret, err2) -> {
                    if (err2 != null) {
                        future.completeExceptionally(err2);
                    } else {
                        future.complete(ret);
                    }
                });
    });
    return future;
}

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

private CompletableFuture<Void> compactRegion(byte[] regionName, byte[] columnFamily, boolean major) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    addListener(getRegionLocation(regionName), (location, err) -> {
        if (err != null) {
            future.completeExceptionally(err);
            return;
        }/*from   w w  w  .  ja v  a 2  s  . c  o  m*/
        ServerName serverName = location.getServerName();
        if (serverName == null) {
            future.completeExceptionally(new NoServerForRegionException(Bytes.toStringBinary(regionName)));
            return;
        }
        addListener(compact(location.getServerName(), location.getRegion(), major, columnFamily),
                (ret, err2) -> {
                    if (err2 != null) {
                        future.completeExceptionally(err2);
                    } else {
                        future.complete(ret);
                    }
                });
    });
    return future;
}

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

/**
 * List all region locations for the specific table.
 *///  ww  w  .j  a va  2s  . co m
private CompletableFuture<List<HRegionLocation>> getTableHRegionLocations(TableName tableName) {
    if (TableName.META_TABLE_NAME.equals(tableName)) {
        CompletableFuture<List<HRegionLocation>> future = new CompletableFuture<>();
        // For meta table, we use zk to fetch all locations.
        AsyncRegistry registry = AsyncRegistryFactory.getRegistry(connection.getConfiguration());
        addListener(registry.getMetaRegionLocation(), (metaRegions, err) -> {
            if (err != null) {
                future.completeExceptionally(err);
            } else if (metaRegions == null || metaRegions.isEmpty()
                    || metaRegions.getDefaultRegionLocation() == null) {
                future.completeExceptionally(new IOException("meta region does not found"));
            } else {
                future.complete(Collections.singletonList(metaRegions.getDefaultRegionLocation()));
            }
            // close the registry.
            IOUtils.closeQuietly(registry);
        });
        return future;
    } else {
        // For non-meta table, we fetch all locations by scanning hbase:meta table
        return AsyncMetaTableAccessor.getTableHRegionLocations(metaTable, Optional.of(tableName));
    }
}

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

/**
 * Compact column family of a table, Asynchronous operation even if CompletableFuture.get()
 */// w ww  .j a  v  a  2s  .c o  m
private CompletableFuture<Void> compact(TableName tableName, byte[] columnFamily, boolean major,
        CompactType compactType) {
    CompletableFuture<Void> future = new CompletableFuture<>();

    switch (compactType) {
    case MOB:
        addListener(connection.registry.getMasterAddress(), (serverName, err) -> {
            if (err != null) {
                future.completeExceptionally(err);
                return;
            }
            RegionInfo regionInfo = RegionInfo.createMobRegionInfo(tableName);
            addListener(compact(serverName, regionInfo, major, columnFamily), (ret, err2) -> {
                if (err2 != null) {
                    future.completeExceptionally(err2);
                } else {
                    future.complete(ret);
                }
            });
        });
        break;
    case NORMAL:
        addListener(getTableHRegionLocations(tableName), (locations, err) -> {
            if (err != null) {
                future.completeExceptionally(err);
                return;
            }
            if (locations == null || locations.isEmpty()) {
                future.completeExceptionally(new TableNotFoundException(tableName));
            }
            CompletableFuture<?>[] compactFutures = locations.stream().filter(l -> l.getRegion() != null)
                    .filter(l -> !l.getRegion().isOffline()).filter(l -> l.getServerName() != null)
                    .map(l -> compact(l.getServerName(), l.getRegion(), major, columnFamily))
                    .toArray(CompletableFuture<?>[]::new);
            // future complete unless all of the compact futures are completed.
            addListener(CompletableFuture.allOf(compactFutures), (ret, err2) -> {
                if (err2 != null) {
                    future.completeExceptionally(err2);
                } else {
                    future.complete(ret);
                }
            });
        });
        break;
    default:
        throw new IllegalArgumentException("Unknown compactType: " + compactType);
    }
    return future;
}

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

private void checkAndGetTableName(byte[] encodeRegionName, AtomicReference<TableName> tableName,
        CompletableFuture<TableName> result) {
    addListener(getRegionLocation(encodeRegionName), (location, err) -> {
        if (err != null) {
            result.completeExceptionally(err);
            return;
        }/*from   w  ww  . j a  v a2  s  .c  om*/
        RegionInfo regionInfo = location.getRegion();
        if (regionInfo.getReplicaId() != RegionInfo.DEFAULT_REPLICA_ID) {
            result.completeExceptionally(
                    new IllegalArgumentException("Can't invoke merge on non-default regions directly"));
            return;
        }
        if (!tableName.compareAndSet(null, regionInfo.getTable())) {
            if (!tableName.get().equals(regionInfo.getTable())) {
                // tables of this two region should be same.
                result.completeExceptionally(
                        new IllegalArgumentException("Cannot merge regions from two different tables "
                                + tableName.get() + " and " + regionInfo.getTable()));
            } else {
                result.complete(tableName.get());
            }
        }
    });
}

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

@Override
public CompletableFuture<Void> mergeRegions(List<byte[]> nameOfRegionsToMerge, boolean forcible) {
    if (nameOfRegionsToMerge.size() < 2) {
        return failedFuture(
                new IllegalArgumentException("Can not merge only " + nameOfRegionsToMerge.size() + " region"));
    }/*  w  w  w. j a  v  a2s. c  om*/
    CompletableFuture<Void> future = new CompletableFuture<>();
    byte[][] encodedNameOfRegionsToMerge = nameOfRegionsToMerge.stream().map(this::toEncodeRegionName)
            .toArray(byte[][]::new);

    addListener(checkRegionsAndGetTableName(encodedNameOfRegionsToMerge), (tableName, err) -> {
        if (err != null) {
            future.completeExceptionally(err);
            return;
        }

        MergeTableRegionsRequest request = null;
        try {
            request = RequestConverter.buildMergeTableRegionsRequest(encodedNameOfRegionsToMerge, forcible,
                    ng.getNonceGroup(), ng.newNonce());
        } catch (DeserializationException e) {
            future.completeExceptionally(e);
            return;
        }

        addListener(this.<MergeTableRegionsRequest, MergeTableRegionsResponse>procedureCall(tableName, request,
                (s, c, req, done) -> s.mergeTableRegions(c, req, done), (resp) -> resp.getProcId(),
                new MergeTableRegionProcedureBiConsumer(tableName)), (ret, err2) -> {
                    if (err2 != null) {
                        future.completeExceptionally(err2);
                    } else {
                        future.complete(ret);
                    }
                });
    });
    return future;
}

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

@Override
public CompletableFuture<Void> split(TableName tableName) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    addListener(tableExists(tableName), (exist, error) -> {
        if (error != null) {
            future.completeExceptionally(error);
            return;
        }/*from  w  w  w .j  ava2s  .c  o  m*/
        if (!exist) {
            future.completeExceptionally(new TableNotFoundException(tableName));
            return;
        }
        addListener(
                metaTable.scanAll(new Scan().setReadType(ReadType.PREAD).addFamily(HConstants.CATALOG_FAMILY)
                        .withStartRow(MetaTableAccessor.getTableStartRowForMeta(tableName, QueryType.REGION))
                        .withStopRow(MetaTableAccessor.getTableStopRowForMeta(tableName, QueryType.REGION))),
                (results, err2) -> {
                    if (err2 != null) {
                        future.completeExceptionally(err2);
                        return;
                    }
                    if (results != null && !results.isEmpty()) {
                        List<CompletableFuture<Void>> splitFutures = new ArrayList<>();
                        for (Result r : results) {
                            if (r.isEmpty() || MetaTableAccessor.getRegionInfo(r) == null) {
                                continue;
                            }
                            RegionLocations rl = MetaTableAccessor.getRegionLocations(r);
                            if (rl != null) {
                                for (HRegionLocation h : rl.getRegionLocations()) {
                                    if (h != null && h.getServerName() != null) {
                                        RegionInfo hri = h.getRegion();
                                        if (hri == null || hri.isSplitParent()
                                                || hri.getReplicaId() != RegionInfo.DEFAULT_REPLICA_ID) {
                                            continue;
                                        }
                                        splitFutures.add(split(hri, null));
                                    }
                                }
                            }
                        }
                        addListener(
                                CompletableFuture.allOf(
                                        splitFutures.toArray(new CompletableFuture<?>[splitFutures.size()])),
                                (ret, exception) -> {
                                    if (exception != null) {
                                        future.completeExceptionally(exception);
                                        return;
                                    }
                                    future.complete(ret);
                                });
                    } else {
                        future.complete(null);
                    }
                });
    });
    return future;
}