Java Code Examples for org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver#onError()
The following examples show how to use
org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver#onError() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GrpcUtil.java From incubator-ratis with Apache License 2.0 | 6 votes |
static <REPLY extends RaftClientReply, REPLY_PROTO> void asyncCall( StreamObserver<REPLY_PROTO> responseObserver, CheckedSupplier<CompletableFuture<REPLY>, IOException> supplier, Function<REPLY, REPLY_PROTO> toProto) { try { supplier.get().whenCompleteAsync((reply, exception) -> { if (exception != null) { responseObserver.onError(GrpcUtil.wrapException(exception)); } else { responseObserver.onNext(toProto.apply(reply)); responseObserver.onCompleted(); } }); } catch (Exception e) { responseObserver.onError(GrpcUtil.wrapException(e)); } }
Example 2
Source File: GrpcUtil.java From ratis with Apache License 2.0 | 6 votes |
static <REPLY extends RaftClientReply, REPLY_PROTO> void asyncCall( StreamObserver<REPLY_PROTO> responseObserver, CheckedSupplier<CompletableFuture<REPLY>, IOException> supplier, Function<REPLY, REPLY_PROTO> toProto) { try { supplier.get().whenCompleteAsync((reply, exception) -> { if (exception != null) { responseObserver.onError(GrpcUtil.wrapException(exception)); } else { responseObserver.onNext(toProto.apply(reply)); responseObserver.onCompleted(); } }); } catch (Exception e) { responseObserver.onError(GrpcUtil.wrapException(e)); } }
Example 3
Source File: GrpcReplicationService.java From hadoop-ozone with Apache License 2.0 | 5 votes |
@Override public void download(CopyContainerRequestProto request, StreamObserver<CopyContainerResponseProto> responseObserver) { long containerID = request.getContainerID(); LOG.info("Streaming container data ({}) to other datanode", containerID); try { GrpcOutputStream outputStream = new GrpcOutputStream(responseObserver, containerID, BUFFER_SIZE); source.copyData(containerID, outputStream); } catch (IOException e) { LOG.error("Error streaming container {}", containerID, e); responseObserver.onError(e); } }
Example 4
Source File: GrpcXceiverService.java From hadoop-ozone with Apache License 2.0 | 5 votes |
@Override public StreamObserver<ContainerCommandRequestProto> send( StreamObserver<ContainerCommandResponseProto> responseObserver) { return new StreamObserver<ContainerCommandRequestProto>() { private final AtomicBoolean isClosed = new AtomicBoolean(false); @Override public void onNext(ContainerCommandRequestProto request) { try { ContainerCommandResponseProto resp = dispatcher.dispatch(request, null); responseObserver.onNext(resp); } catch (Throwable e) { LOG.error("Got exception when processing" + " ContainerCommandRequestProto {}", request, e); responseObserver.onError(e); } } @Override public void onError(Throwable t) { // for now we just log a msg LOG.error("ContainerCommand send on error. Exception: ", t); } @Override public void onCompleted() { if (isClosed.compareAndSet(false, true)) { LOG.debug("ContainerCommand send completed"); responseObserver.onCompleted(); } } }; }
Example 5
Source File: GrpcServerProtocolService.java From incubator-ratis with Apache License 2.0 | 5 votes |
@Override public void requestVote(RequestVoteRequestProto request, StreamObserver<RequestVoteReplyProto> responseObserver) { try { final RequestVoteReplyProto reply = server.requestVote(request); responseObserver.onNext(reply); responseObserver.onCompleted(); } catch (Throwable e) { GrpcUtil.warn(LOG, () -> getId() + ": Failed requestVote " + ProtoUtils.toString(request.getServerRequest()), e); responseObserver.onError(GrpcUtil.wrapException(e)); } }
Example 6
Source File: GrpcLogAppender.java From incubator-ratis with Apache License 2.0 | 5 votes |
/** * Send installSnapshot request to Follower with only a notification that a snapshot needs to be installed. * @param firstAvailableLogTermIndex the first available log's index on the Leader */ private void installSnapshot(TermIndex firstAvailableLogTermIndex) { LOG.info("{}: followerNextIndex = {} but logStartIndex = {}, notify follower to install snapshot-{}", this, getFollower().getNextIndex(), getRaftLog().getStartIndex(), firstAvailableLogTermIndex); final InstallSnapshotResponseHandler responseHandler = new InstallSnapshotResponseHandler(true); StreamObserver<InstallSnapshotRequestProto> snapshotRequestObserver = null; // prepare and enqueue the notify install snapshot request. final InstallSnapshotRequestProto request = createInstallSnapshotNotificationRequest(firstAvailableLogTermIndex); if (LOG.isInfoEnabled()) { LOG.info("{}: send {}", this, ServerProtoUtils.toString(request)); } try { snapshotRequestObserver = getClient().installSnapshot(responseHandler); snapshotRequestObserver.onNext(request); getFollower().updateLastRpcSendTime(); responseHandler.addPending(request); snapshotRequestObserver.onCompleted(); } catch (Exception e) { GrpcUtil.warn(LOG, () -> this + ": Failed to notify follower to install snapshot.", e); if (snapshotRequestObserver != null) { snapshotRequestObserver.onError(e); } return; } synchronized (this) { if (isAppenderRunning() && !responseHandler.isDone()) { try { wait(); } catch (InterruptedException ignored) { } } } }
Example 7
Source File: GrpcServerProtocolService.java From ratis with Apache License 2.0 | 5 votes |
@Override public void requestVote(RequestVoteRequestProto request, StreamObserver<RequestVoteReplyProto> responseObserver) { try { final RequestVoteReplyProto reply = server.requestVote(request); responseObserver.onNext(reply); responseObserver.onCompleted(); } catch (Throwable e) { GrpcUtil.warn(LOG, () -> getId() + ": Failed requestVote " + ProtoUtils.toString(request.getServerRequest()), e); responseObserver.onError(GrpcUtil.wrapException(e)); } }
Example 8
Source File: GrpcServerProtocolService.java From ratis with Apache License 2.0 | 5 votes |
@Override public StreamObserver<InstallSnapshotRequestProto> installSnapshot( StreamObserver<InstallSnapshotReplyProto> responseObserver) { return new StreamObserver<InstallSnapshotRequestProto>() { @Override public void onNext(InstallSnapshotRequestProto request) { try { final InstallSnapshotReplyProto reply = server.installSnapshot(request); responseObserver.onNext(reply); } catch (Throwable e) { GrpcUtil.warn(LOG, () -> getId() + ": Failed installSnapshot " + ProtoUtils.toString(request.getServerRequest()), e); responseObserver.onError(GrpcUtil.wrapException(e)); } } @Override public void onError(Throwable t) { GrpcUtil.warn(LOG, () -> getId() + ": installSnapshot onError", t); } @Override public void onCompleted() { LOG.info("{}: installSnapshot completed", getId()); responseObserver.onCompleted(); } }; }
Example 9
Source File: GrpcLogAppender.java From incubator-ratis with Apache License 2.0 | 4 votes |
/** * Send installSnapshot request to Follower with a snapshot. * @param snapshot the snapshot to be sent to Follower */ private void installSnapshot(SnapshotInfo snapshot) { LOG.info("{}: followerNextIndex = {} but logStartIndex = {}, send snapshot {} to follower", this, getFollower().getNextIndex(), getRaftLog().getStartIndex(), snapshot); final InstallSnapshotResponseHandler responseHandler = new InstallSnapshotResponseHandler(); StreamObserver<InstallSnapshotRequestProto> snapshotRequestObserver = null; final String requestId = UUID.randomUUID().toString(); try { snapshotRequestObserver = getClient().installSnapshot(responseHandler); for (InstallSnapshotRequestProto request : new SnapshotRequestIter(snapshot, requestId)) { if (isAppenderRunning()) { snapshotRequestObserver.onNext(request); getFollower().updateLastRpcSendTime(); responseHandler.addPending(request); } else { break; } } snapshotRequestObserver.onCompleted(); grpcServerMetrics.onInstallSnapshot(); } catch (Exception e) { LOG.warn("{}: failed to install snapshot {}: {}", this, snapshot.getFiles(), e); if (snapshotRequestObserver != null) { snapshotRequestObserver.onError(e); } return; } synchronized (this) { while (isAppenderRunning() && !responseHandler.isDone()) { try { wait(); } catch (InterruptedException ignored) { } } } if (responseHandler.hasAllResponse()) { getFollower().setSnapshotIndex(snapshot.getTermIndex().getIndex()); LOG.info("{}: installed snapshot {} successfully", this, snapshot); } }
Example 10
Source File: GrpcServerProtocolService.java From ratis with Apache License 2.0 | 4 votes |
@Override public StreamObserver<AppendEntriesRequestProto> appendEntries( StreamObserver<AppendEntriesReplyProto> responseObserver) { return new StreamObserver<AppendEntriesRequestProto>() { private final AtomicReference<CompletableFuture<Void>> previousOnNext = new AtomicReference<>(CompletableFuture.completedFuture(null)); private final AtomicBoolean isClosed = new AtomicBoolean(false); @Override public void onNext(AppendEntriesRequestProto request) { final CompletableFuture<Void> current = new CompletableFuture<>(); final CompletableFuture<Void> previous = previousOnNext.getAndSet(current); try { server.appendEntriesAsync(request).thenCombine(previous, (reply, v) -> { if (!isClosed.get()) { if (LOG.isDebugEnabled()) { LOG.debug(server.getId() + ": reply " + ServerProtoUtils.toString(reply)); } responseObserver.onNext(reply); } current.complete(null); return null; }); } catch (Throwable e) { GrpcUtil.warn(LOG, () -> getId() + ": Failed appendEntries " + ProtoUtils.toString(request.getServerRequest()), e); responseObserver.onError(GrpcUtil.wrapException(e, request.getServerRequest().getCallId())); current.completeExceptionally(e); } } @Override public void onError(Throwable t) { // for now we just log a msg GrpcUtil.warn(LOG, () -> getId() + ": appendEntries onError", t); } @Override public void onCompleted() { if (isClosed.compareAndSet(false, true)) { LOG.info("{}: appendEntries completed", getId()); responseObserver.onCompleted(); } } }; }
Example 11
Source File: GrpcLogAppender.java From ratis with Apache License 2.0 | 4 votes |
private void installSnapshot(SnapshotInfo snapshot) { LOG.info("{}: follower {}'s next index is {}," + " log's start index is {}, need to install snapshot", server.getId(), follower.getPeer(), follower.getNextIndex(), raftLog.getStartIndex()); final InstallSnapshotResponseHandler responseHandler = new InstallSnapshotResponseHandler(); StreamObserver<InstallSnapshotRequestProto> snapshotRequestObserver = null; final String requestId = UUID.randomUUID().toString(); try { snapshotRequestObserver = getClient().installSnapshot(responseHandler); for (InstallSnapshotRequestProto request : new SnapshotRequestIter(snapshot, requestId)) { if (isAppenderRunning()) { snapshotRequestObserver.onNext(request); follower.updateLastRpcSendTime(); responseHandler.addPending(request); } else { break; } } snapshotRequestObserver.onCompleted(); } catch (Exception e) { LOG.warn("{} failed to install snapshot {}. Exception: {}", this, snapshot.getFiles(), e); if (snapshotRequestObserver != null) { snapshotRequestObserver.onError(e); } return; } synchronized (this) { while (isAppenderRunning() && !responseHandler.isDone()) { try { wait(); } catch (InterruptedException ignored) { } } } if (responseHandler.hasAllResponse()) { follower.setSnapshotIndex(snapshot.getTermIndex().getIndex()); LOG.info("{}: install snapshot-{} successfully on follower {}", server.getId(), snapshot.getTermIndex().getIndex(), follower.getPeer()); } }