Example usage for java.util.concurrent CompletableFuture CompletableFuture

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

Introduction

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

Prototype

public CompletableFuture() 

Source Link

Document

Creates a new incomplete CompletableFuture.

Usage

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

@Override
public CompletableFuture<Void> snapshot(SnapshotDescription snapshotDesc) {
    SnapshotProtos.SnapshotDescription snapshot = ProtobufUtil.createHBaseProtosSnapshotDesc(snapshotDesc);
    try {/*  w  ww  .  j  a  v  a 2 s.co  m*/
        ClientSnapshotDescriptionUtils.assertSnapshotRequestIsValid(snapshot);
    } catch (IllegalArgumentException e) {
        return failedFuture(e);
    }
    CompletableFuture<Void> future = new CompletableFuture<>();
    final SnapshotRequest request = SnapshotRequest.newBuilder().setSnapshot(snapshot).build();
    addListener(this.<Long>newMasterCaller()
            .action((controller, stub) -> this.<SnapshotRequest, SnapshotResponse, Long>call(controller, stub,
                    request, (s, c, req, done) -> s.snapshot(c, req, done), resp -> resp.getExpectedTimeout()))
            .call(), (expectedTimeout, err) -> {
                if (err != null) {
                    future.completeExceptionally(err);
                    return;
                }
                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(isSnapshotFinished(snapshotDesc), (done, err2) -> {
                                if (err2 != null) {
                                    future.completeExceptionally(err2);
                                } else 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.MILLISECONDS);
                                }
                            });
                        } else {
                            future.completeExceptionally(new SnapshotCreationException(
                                    "Snapshot '" + snapshot.getName() + "' wasn't completed in expectedTime:"
                                            + expectedTimeout + " ms",
                                    snapshotDesc));
                        }
                    }
                };
                AsyncConnectionImpl.RETRY_TIMER.newTimeout(pollingTask, 1, TimeUnit.MILLISECONDS);
            });
    return future;
}

From source file:org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.java

@Override
public Position offloadPrefix(Position pos) throws InterruptedException, ManagedLedgerException {
    CompletableFuture<Position> promise = new CompletableFuture<>();

    asyncOffloadPrefix(pos, new OffloadCallback() {
        @Override// ww  w  . j  ava2s  .c o m
        public void offloadComplete(Position offloadedTo, Object ctx) {
            promise.complete(offloadedTo);
        }

        @Override
        public void offloadFailed(ManagedLedgerException e, Object ctx) {
            promise.completeExceptionally(e);
        }
    }, null);

    try {
        return promise.get(AsyncOperationTimeoutSeconds, TimeUnit.SECONDS);
    } catch (TimeoutException te) {
        throw new ManagedLedgerException("Timeout during managed ledger offload operation");
    } catch (ExecutionException e) {
        log.error("[{}] Error offloading. pos = {}", name, pos, e.getCause());
        throw ManagedLedgerException.getManagedLedgerException(e.getCause());
    }
}

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

@Override
public CompletableFuture<Void> restoreSnapshot(String snapshotName, boolean takeFailSafeSnapshot,
        boolean restoreAcl) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    addListener(listSnapshots(Pattern.compile(snapshotName)), (snapshotDescriptions, err) -> {
        if (err != null) {
            future.completeExceptionally(err);
            return;
        }//from ww  w .j a  v  a 2s  .  c  o m
        TableName tableName = null;
        if (snapshotDescriptions != null && !snapshotDescriptions.isEmpty()) {
            for (SnapshotDescription snap : snapshotDescriptions) {
                if (snap.getName().equals(snapshotName)) {
                    tableName = snap.getTableName();
                    break;
                }
            }
        }
        if (tableName == null) {
            future.completeExceptionally(
                    new RestoreSnapshotException("Unable to find the table name for snapshot=" + snapshotName));
            return;
        }
        final TableName finalTableName = tableName;
        addListener(tableExists(finalTableName), (exists, err2) -> {
            if (err2 != null) {
                future.completeExceptionally(err2);
            } else if (!exists) {
                // if table does not exist, then just clone snapshot into new table.
                completeConditionalOnFuture(future,
                        internalRestoreSnapshot(snapshotName, finalTableName, restoreAcl));
            } else {
                addListener(isTableDisabled(finalTableName), (disabled, err4) -> {
                    if (err4 != null) {
                        future.completeExceptionally(err4);
                    } else if (!disabled) {
                        future.completeExceptionally(new TableNotDisabledException(finalTableName));
                    } else {
                        completeConditionalOnFuture(future, restoreSnapshot(snapshotName, finalTableName,
                                takeFailSafeSnapshot, restoreAcl));
                    }
                });
            }
        });
    });
    return future;
}

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

private CompletableFuture<Void> restoreSnapshot(String snapshotName, TableName tableName,
        boolean takeFailSafeSnapshot, boolean restoreAcl) {
    if (takeFailSafeSnapshot) {
        CompletableFuture<Void> future = new CompletableFuture<>();
        // Step.1 Take a snapshot of the current state
        String failSafeSnapshotSnapshotNameFormat = this.connection.getConfiguration().get(
                HConstants.SNAPSHOT_RESTORE_FAILSAFE_NAME, HConstants.DEFAULT_SNAPSHOT_RESTORE_FAILSAFE_NAME);
        final String failSafeSnapshotSnapshotName = failSafeSnapshotSnapshotNameFormat
                .replace("{snapshot.name}", snapshotName)
                .replace("{table.name}", tableName.toString().replace(TableName.NAMESPACE_DELIM, '.'))
                .replace("{restore.timestamp}", String.valueOf(EnvironmentEdgeManager.currentTime()));
        LOG.info("Taking restore-failsafe snapshot: " + failSafeSnapshotSnapshotName);
        addListener(snapshot(failSafeSnapshotSnapshotName, tableName), (ret, err) -> {
            if (err != null) {
                future.completeExceptionally(err);
            } else {
                // Step.2 Restore snapshot
                addListener(internalRestoreSnapshot(snapshotName, tableName, restoreAcl), (void2, err2) -> {
                    if (err2 != null) {
                        // Step.3.a Something went wrong during the restore and try to rollback.
                        addListener(//from  w  ww.j  ava2  s . c  o m
                                internalRestoreSnapshot(failSafeSnapshotSnapshotName, tableName, restoreAcl),
                                (void3, err3) -> {
                                    if (err3 != null) {
                                        future.completeExceptionally(err3);
                                    } else {
                                        String msg = "Restore snapshot=" + snapshotName
                                                + " failed. Rollback to snapshot="
                                                + failSafeSnapshotSnapshotName + " succeeded.";
                                        future.completeExceptionally(new RestoreSnapshotException(msg, err2));
                                    }
                                });
                    } else {
                        // Step.3.b If the restore is succeeded, delete the pre-restore snapshot.
                        LOG.info("Deleting restore-failsafe snapshot: " + failSafeSnapshotSnapshotName);
                        addListener(deleteSnapshot(failSafeSnapshotSnapshotName), (ret3, err3) -> {
                            if (err3 != null) {
                                LOG.error("Unable to remove the failsafe snapshot: "
                                        + failSafeSnapshotSnapshotName, err3);
                                future.completeExceptionally(err3);
                            } else {
                                future.complete(ret3);
                            }
                        });
                    }
                });
            }
        });
        return future;
    } else {
        return internalRestoreSnapshot(snapshotName, tableName, restoreAcl);
    }
}

From source file:org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.java

@Override
public void asyncOffloadPrefix(Position pos, OffloadCallback callback, Object ctx) {
    PositionImpl requestOffloadTo = (PositionImpl) pos;
    if (!isValidPosition(requestOffloadTo)) {
        callback.offloadFailed(new InvalidCursorPositionException("Invalid position for offload"), ctx);
        return;//from  ww  w  . j  ava2  s.  c  o m
    }

    PositionImpl firstUnoffloaded;

    Queue<LedgerInfo> ledgersToOffload = new ConcurrentLinkedQueue<>();
    synchronized (this) {
        log.info("[{}] Start ledgersOffload. ledgers={} totalSize={}", name, ledgers.keySet(),
                TOTAL_SIZE_UPDATER.get(this));

        if (STATE_UPDATER.get(this) == State.Closed) {
            log.info("[{}] Ignoring offload request since the managed ledger was already closed", name);
            callback.offloadFailed(new ManagedLedgerAlreadyClosedException(
                    "Can't offload closed managed ledger (" + name + ")"), ctx);
            return;
        }

        if (ledgers.isEmpty()) {
            log.info("[{}] Tried to offload a managed ledger with no ledgers, giving up", name);
            callback.offloadFailed(new ManagedLedgerAlreadyClosedException(
                    "Can't offload managed ledger (" + name + ") with no ledgers"), ctx);
            return;
        }

        long current = ledgers.lastKey();

        // the first ledger which will not be offloaded. Defaults to current,
        // in the case that the whole headmap is offloaded. Otherwise it will
        // be set as we iterate through the headmap values
        long firstLedgerRetained = current;
        for (LedgerInfo ls : ledgers.headMap(current).values()) {
            if (requestOffloadTo.getLedgerId() > ls.getLedgerId()) {
                // don't offload if ledger has already been offloaded, or is empty
                if (!ls.getOffloadContext().getComplete() && ls.getSize() > 0) {
                    ledgersToOffload.add(ls);
                }
            } else {
                firstLedgerRetained = ls.getLedgerId();
                break;
            }
        }
        firstUnoffloaded = PositionImpl.get(firstLedgerRetained, 0);
    }

    if (ledgersToOffload.isEmpty()) {
        log.info("[{}] No ledgers to offload", name);
        callback.offloadComplete(firstUnoffloaded, ctx);
        return;
    }

    if (offloadMutex.tryLock()) {
        log.info("[{}] Going to offload ledgers {}", name,
                ledgersToOffload.stream().map(l -> l.getLedgerId()).collect(Collectors.toList()));

        CompletableFuture<PositionImpl> promise = new CompletableFuture<>();
        promise.whenComplete((result, exception) -> {
            offloadMutex.unlock();
            if (exception != null) {
                callback.offloadFailed(new ManagedLedgerException(exception), ctx);
            } else {
                callback.offloadComplete(result, ctx);
            }
        });
        offloadLoop(promise, ledgersToOffload, firstUnoffloaded, Optional.empty());
    } else {
        callback.offloadFailed(
                new ManagedLedgerException.OffloadInProgressException("Offload operation already running"),
                ctx);
    }
}

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

@Override
public CompletableFuture<Void> cloneSnapshot(String snapshotName, TableName tableName, boolean restoreAcl) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    addListener(tableExists(tableName), (exists, err) -> {
        if (err != null) {
            future.completeExceptionally(err);
        } else if (exists) {
            future.completeExceptionally(new TableExistsException(tableName));
        } else {//  w w w . ja va2 s  . c  o  m
            completeConditionalOnFuture(future, internalRestoreSnapshot(snapshotName, tableName, restoreAcl));
        }
    });
    return future;
}

From source file:org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.java

private CompletableFuture<Void> transformLedgerInfo(long ledgerId, LedgerInfoTransformation transformation) {
    CompletableFuture<Void> promise = new CompletableFuture<Void>();

    tryTransformLedgerInfo(ledgerId, transformation, promise);

    return promise;
}

From source file:org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.java

private void tryTransformLedgerInfo(long ledgerId, LedgerInfoTransformation transformation,
        CompletableFuture<Void> finalPromise) {
    synchronized (this) {
        if (!ledgersListMutex.tryLock()) {
            // retry in 100 milliseconds
            scheduledExecutor.schedule(//  w  w  w. j  a  va 2  s .  co m
                    safeRun(() -> tryTransformLedgerInfo(ledgerId, transformation, finalPromise)), 100,
                    TimeUnit.MILLISECONDS);
        } else { // lock acquired
            CompletableFuture<Void> unlockingPromise = new CompletableFuture<>();
            unlockingPromise.whenComplete((res, ex) -> {
                ledgersListMutex.unlock();
                if (ex != null) {
                    finalPromise.completeExceptionally(ex);
                } else {
                    finalPromise.complete(res);
                }
            });

            LedgerInfo oldInfo = ledgers.get(ledgerId);
            if (oldInfo == null) {
                unlockingPromise.completeExceptionally(new OffloadConflict(
                        "Ledger " + ledgerId + " no longer exists in ManagedLedger, likely trimmed"));
            } else {
                try {
                    LedgerInfo newInfo = transformation.transform(oldInfo);
                    ledgers.put(ledgerId, newInfo);
                    store.asyncUpdateLedgerIds(name, getManagedLedgerInfo(), ledgersStat,
                            new MetaStoreCallback<Void>() {
                                @Override
                                public void operationComplete(Void result, Stat stat) {
                                    ledgersStat = stat;
                                    unlockingPromise.complete(null);
                                }

                                @Override
                                public void operationFailed(MetaStoreException e) {
                                    unlockingPromise.completeExceptionally(e);
                                }
                            });
                } catch (ManagedLedgerException mle) {
                    unlockingPromise.completeExceptionally(mle);
                }
            }
        }
    }
}

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

private CompletableFuture<List<SnapshotDescription>> getCompletedSnapshots(Pattern tableNamePattern,
        Pattern snapshotNamePattern) {
    CompletableFuture<List<SnapshotDescription>> future = new CompletableFuture<>();
    addListener(listTableNames(tableNamePattern, false), (tableNames, err) -> {
        if (err != null) {
            future.completeExceptionally(err);
            return;
        }//from   ww  w. j a  v  a  2 s  .co  m
        if (tableNames == null || tableNames.size() <= 0) {
            future.complete(Collections.emptyList());
            return;
        }
        addListener(getCompletedSnapshots(snapshotNamePattern), (snapshotDescList, err2) -> {
            if (err2 != null) {
                future.completeExceptionally(err2);
                return;
            }
            if (snapshotDescList == null || snapshotDescList.isEmpty()) {
                future.complete(Collections.emptyList());
                return;
            }
            future.complete(snapshotDescList.stream()
                    .filter(snap -> (snap != null && tableNames.contains(snap.getTableName())))
                    .collect(Collectors.toList()));
        });
    });
    return future;
}

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

private CompletableFuture<Void> internalDeleteSnapshots(Pattern tableNamePattern, Pattern snapshotNamePattern) {
    CompletableFuture<List<SnapshotDescription>> listSnapshotsFuture;
    if (tableNamePattern == null) {
        listSnapshotsFuture = getCompletedSnapshots(snapshotNamePattern);
    } else {/*from w w w. j a v  a 2s.c  om*/
        listSnapshotsFuture = getCompletedSnapshots(tableNamePattern, snapshotNamePattern);
    }
    CompletableFuture<Void> future = new CompletableFuture<>();
    addListener(listSnapshotsFuture, ((snapshotDescriptions, err) -> {
        if (err != null) {
            future.completeExceptionally(err);
            return;
        }
        if (snapshotDescriptions == null || snapshotDescriptions.isEmpty()) {
            future.complete(null);
            return;
        }
        addListener(CompletableFuture.allOf(snapshotDescriptions.stream().map(this::internalDeleteSnapshot)
                .toArray(CompletableFuture[]::new)), (v, e) -> {
                    if (e != null) {
                        future.completeExceptionally(e);
                    } else {
                        future.complete(v);
                    }
                });
    }));
    return future;
}