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:io.pravega.controller.server.SegmentHelper.java

public CompletableFuture<TxnStatus> abortTransaction(final String scope, final String stream,
        final int segmentNumber, final UUID txId, final HostControllerStore hostControllerStore,
        final ConnectionFactory clientCF) {
    final Controller.NodeUri uri = getSegmentUri(scope, stream, segmentNumber, hostControllerStore);
    final CompletableFuture<TxnStatus> result = new CompletableFuture<>();
    final WireCommandType type = WireCommandType.ABORT_TRANSACTION;
    final FailingReplyProcessor replyProcessor = new FailingReplyProcessor() {

        @Override//from w w w  .  j av a  2s  .c o  m
        public void connectionDropped() {
            result.completeExceptionally(
                    new WireCommandFailedException(type, WireCommandFailedException.Reason.ConnectionDropped));
        }

        @Override
        public void wrongHost(WireCommands.WrongHost wrongHost) {
            result.completeExceptionally(
                    new WireCommandFailedException(type, WireCommandFailedException.Reason.UnknownHost));
        }

        @Override
        public void transactionCommitted(WireCommands.TransactionCommitted transactionCommitted) {
            result.completeExceptionally(
                    new WireCommandFailedException(type, WireCommandFailedException.Reason.PreconditionFailed));
        }

        @Override
        public void transactionAborted(WireCommands.TransactionAborted transactionDropped) {
            result.complete(TxnStatus.newBuilder().setStatus(TxnStatus.Status.SUCCESS).build());
        }

        @Override
        public void processingFailure(Exception error) {
            result.completeExceptionally(error);
        }
    };

    WireCommands.AbortTransaction request = new WireCommands.AbortTransaction(idGenerator.get(),
            Segment.getScopedName(scope, stream, segmentNumber), txId);
    sendRequestAsync(request, replyProcessor, result, clientCF, ModelHelper.encode(uri));
    return result;
}

From source file:io.pravega.controller.server.SegmentHelper.java

public CompletableFuture<TxnStatus> commitTransaction(final String scope, final String stream,
        final int segmentNumber, final UUID txId, final HostControllerStore hostControllerStore,
        final ConnectionFactory clientCF) {
    final Controller.NodeUri uri = getSegmentUri(scope, stream, segmentNumber, hostControllerStore);

    final CompletableFuture<TxnStatus> result = new CompletableFuture<>();
    final WireCommandType type = WireCommandType.COMMIT_TRANSACTION;
    final FailingReplyProcessor replyProcessor = new FailingReplyProcessor() {

        @Override//from w  w  w  .  j a va  2 s.c o  m
        public void connectionDropped() {
            result.completeExceptionally(
                    new WireCommandFailedException(type, WireCommandFailedException.Reason.ConnectionDropped));
        }

        @Override
        public void wrongHost(WireCommands.WrongHost wrongHost) {
            result.completeExceptionally(
                    new WireCommandFailedException(type, WireCommandFailedException.Reason.UnknownHost));
        }

        @Override
        public void transactionCommitted(WireCommands.TransactionCommitted transactionCommitted) {
            result.complete(TxnStatus.newBuilder().setStatus(TxnStatus.Status.SUCCESS).build());
        }

        @Override
        public void transactionAborted(WireCommands.TransactionAborted transactionAborted) {
            result.completeExceptionally(
                    new WireCommandFailedException(type, WireCommandFailedException.Reason.PreconditionFailed));
        }

        @Override
        public void processingFailure(Exception error) {
            result.completeExceptionally(error);
        }
    };

    WireCommands.CommitTransaction request = new WireCommands.CommitTransaction(idGenerator.get(),
            Segment.getScopedName(scope, stream, segmentNumber), txId);
    sendRequestAsync(request, replyProcessor, result, clientCF, ModelHelper.encode(uri));
    return result;
}

From source file:io.pravega.controller.store.stream.InMemoryStream.java

@Override
CompletableFuture<Void> setSegmentTable(Data<Integer> data) {
    Preconditions.checkNotNull(data);/*from   w w w.  j  a  v a 2  s  .  com*/
    Preconditions.checkNotNull(data.getData());

    CompletableFuture<Void> result = new CompletableFuture<>();
    synchronized (lock) {
        if (segmentTable == null) {
            result.completeExceptionally(StoreException.create(StoreException.Type.DATA_NOT_FOUND,
                    "Segment table for stream: " + getName()));
        } else if (segmentTable.getVersion().equals(data.getVersion())) {
            segmentTable = new Data<>(Arrays.copyOf(data.getData(), data.getData().length),
                    data.getVersion() + 1);
            result.complete(null);
        } else {
            result.completeExceptionally(StoreException.create(StoreException.Type.WRITE_CONFLICT,
                    "Segment table for stream: " + getName()));
        }
    }
    return result;
}

From source file:io.pravega.controller.store.stream.InMemoryStream.java

@Override
CompletableFuture<Void> setConfigurationData(StreamConfiguration configuration) {
    Preconditions.checkNotNull(configuration);

    CompletableFuture<Void> result = new CompletableFuture<>();

    synchronized (lock) {
        if (this.configuration == null) {
            result.completeExceptionally(StoreException.create(StoreException.Type.DATA_NOT_FOUND, getName()));
        } else {//from  w w w  .  ja  v  a2  s  .c  o m
            this.configuration = configuration;
            result.complete(null);
        }
    }
    return result;
}

From source file:io.pravega.controller.store.stream.InMemoryStream.java

@Override
CompletableFuture<Void> updateHistoryTable(Data<Integer> updated) {
    Preconditions.checkNotNull(updated);
    Preconditions.checkNotNull(updated.getData());

    CompletableFuture<Void> result = new CompletableFuture<>();
    synchronized (lock) {
        if (historyTable == null) {
            result.completeExceptionally(StoreException.create(StoreException.Type.DATA_NOT_FOUND,
                    "Historytable for stream: " + getName()));
        } else {//  w w w.  ja va2s . c  om
            if (historyTable.getVersion().equals(updated.getVersion())) {
                historyTable = new Data<>(Arrays.copyOf(updated.getData(), updated.getData().length),
                        updated.getVersion() + 1);
                result.complete(null);
            } else {
                result.completeExceptionally(StoreException.create(StoreException.Type.WRITE_CONFLICT,
                        "Historytable for stream: " + getName()));
            }
        }
    }
    return result;
}

From source file:io.pravega.controller.store.stream.AbstractStreamMetadataStore.java

private <T> CompletableFuture<T> withCompletion(CompletableFuture<T> future, final Executor executor) {

    // Following makes sure that the result future given out to caller is actually completed on
    // caller's executor. So any chaining, if done without specifying an executor, will either happen on
    // caller's executor or fork join pool but never on someone else's executor.

    CompletableFuture<T> result = new CompletableFuture<>();

    future.whenCompleteAsync((r, e) -> {
        if (e != null) {
            result.completeExceptionally(e);
        } else {/*w w  w. j a v a2 s .c om*/
            result.complete(r);
        }
    }, executor);

    return result;
}

From source file:io.pravega.controller.store.stream.InMemoryStream.java

@Override
CompletableFuture<Void> updateIndexTable(Data<Integer> updated) {
    Preconditions.checkNotNull(updated);
    Preconditions.checkNotNull(updated.getData());

    final CompletableFuture<Void> result = new CompletableFuture<>();
    synchronized (lock) {
        if (indexTable == null) {
            result.completeExceptionally(StoreException.create(StoreException.Type.DATA_NOT_FOUND,
                    "Indextable for stream: " + getName()));
        } else if (indexTable.getVersion().equals(updated.getVersion())) {
            indexTable = new Data<>(Arrays.copyOf(updated.getData(), updated.getData().length),
                    updated.getVersion() + 1);
            result.complete(null);//  w w  w  .  j  av a  2  s.  c  om
        } else {
            result.completeExceptionally(StoreException.create(StoreException.Type.WRITE_CONFLICT,
                    "Indextable for stream: " + getName()));
        }
    }
    return result;
}

From source file:io.pravega.controller.store.stream.InMemoryStream.java

@Override
CompletableFuture<Void> updateActiveTx(int epoch, UUID txId, Data<Integer> data) {
    Preconditions.checkNotNull(data);/* w  w w  . j  av a2s  .com*/

    CompletableFuture<Void> result = new CompletableFuture<>();
    synchronized (txnsLock) {
        if (!activeTxns.containsKey(txId.toString())) {
            result.completeExceptionally(StoreException.create(StoreException.Type.DATA_NOT_FOUND,
                    "Stream: " + getName() + " Transaction: " + txId.toString()));
        } else {
            activeTxns.compute(txId.toString(), (x, y) -> new Data<>(data.getData(), y.getVersion() + 1));
            result.complete(null);
        }
    }

    return result;
}

From source file:io.pravega.controller.store.stream.InMemoryStream.java

@Override
CompletableFuture<Void> updateMarkerData(int segmentNumber, Data<Integer> data) {
    CompletableFuture<Void> result = new CompletableFuture<>();
    synchronized (markersLock) {
        if (!markers.containsKey(segmentNumber)) {
            result.completeExceptionally(StoreException.create(StoreException.Type.DATA_NOT_FOUND,
                    "Stream: " + getName() + " Segment number: " + segmentNumber));
        } else {/*from w  ww  . ja va2  s  .  c o m*/
            markers.compute(segmentNumber, (x, y) -> {
                if (y.getVersion().equals(data.getVersion())) {
                    result.complete(null);
                    return new Data<>(Arrays.copyOf(data.getData(), data.getData().length),
                            data.getVersion() + 1);
                } else {
                    result.completeExceptionally(StoreException.create(StoreException.Type.WRITE_CONFLICT,
                            "Stream: " + getName() + " Segment number: " + segmentNumber));
                    return y;
                }
            });
        }
    }
    return result;
}

From source file:io.pravega.controller.store.stream.InMemoryStream.java

@Override
CompletableFuture<Void> sealActiveTx(int epoch, UUID txId, boolean commit, ActiveTxnRecord txnRecord,
        int version) {
    Preconditions.checkNotNull(txId);/* w ww. jav  a 2s.  c  om*/

    CompletableFuture<Void> result = new CompletableFuture<>();
    synchronized (txnsLock) {
        if (!activeTxns.containsKey(txId.toString())) {
            result.completeExceptionally(StoreException.create(StoreException.Type.DATA_NOT_FOUND,
                    "Stream: " + getName() + " Transaction: " + txId.toString()));
        } else {
            activeTxns.compute(txId.toString(), (x, y) -> {
                if (version != y.getVersion()) {
                    result.completeExceptionally(StoreException.create(StoreException.Type.WRITE_CONFLICT,
                            "Stream: " + getName() + " Transaction: " + txId.toString()));
                    return y;
                } else {
                    ActiveTxnRecord previous = ActiveTxnRecord.parse(y.getData());
                    ActiveTxnRecord updated = new ActiveTxnRecord(previous.getTxCreationTimestamp(),
                            previous.getLeaseExpiryTime(), previous.getMaxExecutionExpiryTime(),
                            previous.getScaleGracePeriod(), commit ? TxnStatus.COMMITTING : TxnStatus.ABORTING);
                    result.complete(null);
                    return new Data<>(updated.toByteArray(), y.getVersion() + 1);
                }
            });
        }
    }
    return result;
}