@Override public CompletableFuture<Transaction.Status> checkTransactionStatus(final Stream stream, final UUID txId) { Exceptions.checkNotClosed(closed.get(), this); Preconditions.checkNotNull(stream, "stream"); Preconditions.checkNotNull(txId, "txId"); long traceId = LoggerHelpers.traceEnter(log, "checkTransactionStatus", stream, txId); final CompletableFuture<TxnState> result = this.retryConfig.runAsync(() -> { RPCAsyncCallback<TxnState> callback = new RPCAsyncCallback<>(traceId, "checkTransactionStatus"); client.checkTransactionState(TxnRequest.newBuilder() .setStreamInfo(ModelHelper.createStreamInfo(stream.getScope(), stream.getStreamName())) .setTxnId(ModelHelper.decode(txId)) .build(), callback); return callback.getFuture(); }, this.executor); return result.thenApply(status -> ModelHelper.encode(status.getState(), stream + " " + txId)) .whenComplete((x, e) -> { if (e != null) { log.warn("checkTransactionStatus failed: ", e); } LoggerHelpers.traceLeave(log, "checkTransactionStatus", traceId); }); }
@Override public CompletableFuture<Void> commitTransaction(final Stream stream, final UUID txId) { Exceptions.checkNotClosed(closed.get(), this); Preconditions.checkNotNull(stream, "stream"); Preconditions.checkNotNull(txId, "txId"); long traceId = LoggerHelpers.traceEnter(log, "commitTransaction", stream, txId); final CompletableFuture<TxnStatus> result = this.retryConfig.runAsync(() -> { RPCAsyncCallback<TxnStatus> callback = new RPCAsyncCallback<>(traceId, "commitTransaction"); client.commitTransaction(TxnRequest.newBuilder() .setStreamInfo(ModelHelper.createStreamInfo(stream.getScope(), stream.getStreamName())) .setTxnId(ModelHelper.decode(txId)) .build(), callback); return callback.getFuture(); }, this.executor); return Futures.toVoidExpecting(result, TxnStatus.newBuilder().setStatus(TxnStatus.Status.SUCCESS).build(), TxnFailedException::new) .whenComplete((x, e) -> { if (e != null) { log.warn("commitTransaction failed: ", e); } LoggerHelpers.traceLeave(log, "commitTransaction", traceId); }); }
@Override public CompletableFuture<Void> abortTransaction(final Stream stream, final UUID txId) { Exceptions.checkNotClosed(closed.get(), this); Preconditions.checkNotNull(stream, "stream"); Preconditions.checkNotNull(txId, "txId"); long traceId = LoggerHelpers.traceEnter(log, "abortTransaction", stream, txId); final CompletableFuture<TxnStatus> result = this.retryConfig.runAsync(() -> { RPCAsyncCallback<TxnStatus> callback = new RPCAsyncCallback<>(traceId, "abortTransaction"); client.abortTransaction(TxnRequest.newBuilder() .setStreamInfo(ModelHelper.createStreamInfo(stream.getScope(), stream.getStreamName())) .setTxnId(ModelHelper.decode(txId)) .build(), callback); return callback.getFuture(); }, this.executor); return Futures.toVoidExpecting(result, TxnStatus.newBuilder().setStatus(TxnStatus.Status.SUCCESS).build(), TxnFailedException::new) .whenComplete((x, e) -> { if (e != null) { log.warn("abortTransaction failed: ", e); } LoggerHelpers.traceLeave(log, "abortTransaction", traceId); }); }
private Controller.TxnStatus closeTransaction(final String scope, final String stream, final Controller.TxnId txnId, final boolean abort) { Controller.StreamInfo streamInfo = ModelHelper.createStreamInfo(scope, stream); Controller.TxnRequest request = Controller.TxnRequest.newBuilder() .setStreamInfo(streamInfo) .setTxnId(txnId) .build(); ResultObserver<Controller.TxnStatus> resultObserver = new ResultObserver<>(); if (abort) { this.controllerService.abortTransaction(request, resultObserver); } else { this.controllerService.commitTransaction(request, resultObserver); } Controller.TxnStatus status = resultObserver.get(); Assert.assertNotNull(status); return resultObserver.get(); }