org.apache.ratis.protocol.RaftClientRequest Java Examples
The following examples show how to use
org.apache.ratis.protocol.RaftClientRequest.
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: OrderedAsync.java From incubator-ratis with Apache License 2.0 | 6 votes |
CompletableFuture<RaftClientReply> send(RaftClientRequest.Type type, Message message, RaftPeerId server) { if (!type.is(TypeCase.WATCH) && !type.is(TypeCase.STREAM)) { Objects.requireNonNull(message, "message == null"); } try { requestSemaphore.acquire(); } catch (InterruptedException e) { return JavaUtils.completeExceptionally(IOUtils.toInterruptedIOException( "Interrupted when sending " + type + ", message=" + message, e)); } final long callId = RaftClientImpl.nextCallId(); final LongFunction<PendingOrderedRequest> constructor = seqNum -> new PendingOrderedRequest(callId, seqNum, slidingWindowEntry -> client.newRaftClientRequest(server, callId, message, type, slidingWindowEntry)); return getSlidingWindow(server).submitNewRequest(constructor, this::sendRequestWithRetry ).getReplyFuture( ).thenApply(reply -> RaftClientImpl.handleRaftException(reply, CompletionException::new) ).whenComplete((r, e) -> requestSemaphore.release()); }
Example #2
Source File: WatchRequests.java From ratis with Apache License 2.0 | 6 votes |
PendingWatch add(RaftClientRequest request) { final long currentTime = Timestamp.currentTimeNanos(); final long roundUp = watchTimeoutDenominationNanos.roundUpNanos(currentTime); final PendingWatch pending = new PendingWatch(request.getType().getWatch(), Timestamp.valueOf(roundUp)); synchronized (this) { if (pending.getIndex() > getIndex()) { // compare again synchronized final PendingWatch previous = q.putIfAbsent(pending, pending); if (previous != null) { return previous; } } else { return null; } } final TimeDuration timeout = watchTimeoutNanos.apply(duration -> duration + roundUp - currentTime); scheduler.onTimeout(timeout, () -> handleTimeout(request, pending), LOG, () -> name + ": Failed to timeout " + request); return pending; }
Example #3
Source File: OzoneManagerStateMachine.java From hadoop-ozone with Apache License 2.0 | 6 votes |
/** * Validate/pre-process the incoming update request in the state machine. * @return the content to be written to the log entry. Null means the request * should be rejected. * @throws IOException thrown by the state machine while validating */ @Override public TransactionContext startTransaction( RaftClientRequest raftClientRequest) throws IOException { ByteString messageContent = raftClientRequest.getMessage().getContent(); OMRequest omRequest = OMRatisHelper.convertByteStringToOMRequest( messageContent); Preconditions.checkArgument(raftClientRequest.getRaftGroupId().equals( raftGroupId)); try { handler.validateRequest(omRequest); } catch (IOException ioe) { TransactionContext ctxt = TransactionContext.newBuilder() .setClientRequest(raftClientRequest) .setStateMachine(this) .setServerRole(RaftProtos.RaftPeerRole.LEADER) .build(); ctxt.setException(ioe); return ctxt; } return handleStartTransactionRequests(raftClientRequest, omRequest); }
Example #4
Source File: RetryCacheTests.java From ratis with Apache License 2.0 | 6 votes |
void runTestBasicRetry(CLUSTER cluster) throws Exception { RaftTestUtil.waitForLeader(cluster); final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage(false).getId(); long oldLastApplied = cluster.getLeader().getState().getLastAppliedIndex(); final RaftClient client = cluster.createClient(leaderId); final RaftClientRpc rpc = client.getClientRpc(); final long callId = 999; final long seqNum = 111; RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), leaderId, callId, seqNum, new SimpleMessage("message")); assertReply(rpc.sendRequest(r), client, callId); // retry with the same callId for (int i = 0; i < 5; i++) { assertReply(rpc.sendRequest(r), client, callId); } assertServer(cluster, client.getId(), callId, oldLastApplied); client.close(); }
Example #5
Source File: GrpcClientProtocolClient.java From ratis with Apache License 2.0 | 6 votes |
CompletableFuture<RaftClientReply> onNext(RaftClientRequest request) { final Map<Long, CompletableFuture<RaftClientReply>> map = replies.get(); if (map == null) { return JavaUtils.completeExceptionally(new AlreadyClosedException(getName() + " is closed.")); } final CompletableFuture<RaftClientReply> f = new CompletableFuture<>(); CollectionUtils.putNew(request.getCallId(), f, map, () -> getName() + ":" + getClass().getSimpleName()); try { requestStreamObserver.onNext(ClientProtoUtils.toRaftClientRequestProto(request)); scheduler.onTimeout(requestTimeoutDuration, () -> timeoutCheck(request), LOG, () -> "Timeout check failed for client request: " + request); } catch(Throwable t) { handleReplyFuture(request.getCallId(), future -> future.completeExceptionally(t)); } return f; }
Example #6
Source File: CombinedClientProtocolClientSideTranslatorPB.java From ratis with Apache License 2.0 | 6 votes |
static <REQUEST extends RaftClientRequest, REPLY extends RaftClientReply, PROTO_REQ, PROTO_REP> REPLY handleRequest( REQUEST request, Function<REQUEST, PROTO_REQ> reqToProto, Function<PROTO_REP, REPLY> repToProto, CheckedFunction<PROTO_REQ, PROTO_REP, ServiceException> handler) throws IOException { final PROTO_REQ proto = reqToProto.apply(request); try { final PROTO_REP reply = handler.apply(proto); return repToProto.apply(reply); } catch (ServiceException se) { LOG.trace("Failed to handle " + request, se); throw ProtoUtils.toIOException(se); } }
Example #7
Source File: FileStoreStateMachine.java From incubator-ratis with Apache License 2.0 | 6 votes |
@Override public TransactionContext startTransaction(RaftClientRequest request) throws IOException { final ByteString content = request.getMessage().getContent(); final FileStoreRequestProto proto = FileStoreRequestProto.parseFrom(content); final TransactionContext.Builder b = TransactionContext.newBuilder() .setStateMachine(this) .setClientRequest(request); if (proto.getRequestCase() == FileStoreRequestProto.RequestCase.WRITE) { final WriteRequestProto write = proto.getWrite(); final FileStoreRequestProto newProto = FileStoreRequestProto.newBuilder() .setWriteHeader(write.getHeader()).build(); b.setLogData(newProto.toByteString()).setStateMachineData(write.getData()); } else { b.setLogData(content); } return b.build(); }
Example #8
Source File: FileStoreStateMachine.java From ratis with Apache License 2.0 | 6 votes |
@Override public TransactionContext startTransaction(RaftClientRequest request) throws IOException { final ByteString content = request.getMessage().getContent(); final FileStoreRequestProto proto = FileStoreRequestProto.parseFrom(content); final TransactionContext.Builder b = TransactionContext.newBuilder() .setStateMachine(this) .setClientRequest(request); if (proto.getRequestCase() == FileStoreRequestProto.RequestCase.WRITE) { final WriteRequestProto write = proto.getWrite(); final FileStoreRequestProto newProto = FileStoreRequestProto.newBuilder() .setWriteHeader(write.getHeader()).build(); b.setLogData(newProto.toByteString()).setStateMachineData(write.getData()); } else { b.setLogData(content); } return b.build(); }
Example #9
Source File: CombinedClientProtocolClientSideTranslatorPB.java From incubator-ratis with Apache License 2.0 | 6 votes |
static <REQUEST extends RaftClientRequest, REPLY extends RaftClientReply, PROTO_REQ, PROTO_REP> REPLY handleRequest( REQUEST request, Function<REQUEST, PROTO_REQ> reqToProto, Function<PROTO_REP, REPLY> repToProto, CheckedFunction<PROTO_REQ, PROTO_REP, ServiceException> handler) throws IOException { final PROTO_REQ proto = reqToProto.apply(request); try { final PROTO_REP reply = handler.apply(proto); return repToProto.apply(reply); } catch (ServiceException se) { LOG.trace("Failed to handle " + request, se); throw ProtoUtils.toIOException(se); } }
Example #10
Source File: GrpcClientProtocolClient.java From incubator-ratis with Apache License 2.0 | 6 votes |
CompletableFuture<RaftClientReply> onNext(RaftClientRequest request) { final long callId = request.getCallId(); final CompletableFuture<RaftClientReply> f = replies.putNew(callId); if (f == null) { return JavaUtils.completeExceptionally(new AlreadyClosedException(getName() + " is closed.")); } try { if (!requestStreamer.onNext(ClientProtoUtils.toRaftClientRequestProto(request))) { return JavaUtils.completeExceptionally(new AlreadyClosedException(getName() + ": the stream is closed.")); } } catch(Throwable t) { handleReplyFuture(request.getCallId(), future -> future.completeExceptionally(t)); return f; } if (RaftClientRequestProto.TypeCase.WATCH.equals(request.getType().getTypeCase())) { scheduler.onTimeout(watchRequestTimeoutDuration, () -> timeoutCheck(callId, watchRequestTimeoutDuration), LOG, () -> "Timeout check failed for client request #" + callId); } else { scheduler.onTimeout(requestTimeoutDuration, () -> timeoutCheck(callId, requestTimeoutDuration), LOG, () -> "Timeout check failed for client request #" + callId); } return f; }
Example #11
Source File: RetryCacheTests.java From incubator-ratis with Apache License 2.0 | 6 votes |
void runTestBasicRetry(CLUSTER cluster) throws Exception { RaftTestUtil.waitForLeader(cluster); final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage(false).getId(); long oldLastApplied = cluster.getLeader().getState().getLastAppliedIndex(); try (final RaftClient client = cluster.createClient(leaderId)) { final RaftClientRpc rpc = client.getClientRpc(); final long callId = 999; RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), leaderId, callId, new SimpleMessage("message")); assertReply(rpc.sendRequest(r), client, callId); // retry with the same callId for (int i = 0; i < 5; i++) { assertReply(rpc.sendRequest(r), client, callId); } assertServer(cluster, client.getId(), callId, oldLastApplied); } }
Example #12
Source File: XceiverServerRatis.java From hadoop-ozone with Apache License 2.0 | 6 votes |
@Override public void submitRequest(ContainerCommandRequestProto request, HddsProtos.PipelineID pipelineID) throws IOException { RaftClientReply reply; Span span = TracingUtil .importAndCreateSpan( "XceiverServerRatis." + request.getCmdType().name(), request.getTraceID()); try (Scope scope = GlobalTracer.get().activateSpan(span)) { RaftClientRequest raftClientRequest = createRaftClientRequest(request, pipelineID, RaftClientRequest.writeRequestType()); try { reply = server.submitClientRequestAsync(raftClientRequest) .get(requestTimeout, TimeUnit.MILLISECONDS); } catch (Exception e) { throw new IOException(e.getMessage(), e); } processReply(reply); } finally { span.finish(); } }
Example #13
Source File: WatchRequests.java From incubator-ratis with Apache License 2.0 | 5 votes |
void handleTimeout(RaftClientRequest request, PendingWatch pending) { if (removeExisting(pending)) { pending.getFuture().completeExceptionally( new NotReplicatedException(request.getCallId(), replication, pending.getIndex())); LOG.debug("{}: timeout {}, {}", name, pending, request); } }
Example #14
Source File: RaftServerMetrics.java From incubator-ratis with Apache License 2.0 | 5 votes |
public Timer getClientRequestTimer(RaftClientRequest request) { if (request.is(TypeCase.READ)) { return getTimer(RAFT_CLIENT_READ_REQUEST); } else if (request.is(TypeCase.STALEREAD)) { return getTimer(RAFT_CLIENT_STALE_READ_REQUEST); } else if (request.is(TypeCase.WATCH)) { String watchType = RaftClientRequest.Type.toString(request.getType().getWatch().getReplication()); return getTimer(String.format(RAFT_CLIENT_WATCH_REQUEST, watchType)); } else if (request.is(TypeCase.WRITE)) { return getTimer(RAFT_CLIENT_WRITE_REQUEST); } return null; }
Example #15
Source File: WatchRequests.java From incubator-ratis with Apache License 2.0 | 5 votes |
CompletableFuture<Void> add(RaftClientRequest request) { final WatchRequestTypeProto watch = request.getType().getWatch(); final WatchQueue queue = queues.get(watch.getReplication()); if (watch.getIndex() > queue.getIndex()) { // compare without synchronization final CompletableFuture<Void> future = queue.add(request); if (future != null) { return future; } } return CompletableFuture.completedFuture(null); }
Example #16
Source File: SimpleStateMachine4Testing.java From incubator-ratis with Apache License 2.0 | 5 votes |
@Override public TransactionContext startTransaction(RaftClientRequest request) { blocking.await(Blocking.Type.START_TRANSACTION); return TransactionContext.newBuilder() .setStateMachine(this) .setClientRequest(request) .setStateMachineData(STATE_MACHINE_DATA) .build(); }
Example #17
Source File: TestStateMachine.java From ratis with Apache License 2.0 | 5 votes |
@Override public TransactionContext startTransaction(RaftClientRequest request) { // only leader will get this call isLeader.set(true); // send the next transaction id as the "context" from SM return TransactionContext.newBuilder() .setStateMachine(this) .setClientRequest(request) .setStateMachineContext(transactions.incrementAndGet()) .build(); }
Example #18
Source File: TransactionContextImpl.java From ratis with Apache License 2.0 | 5 votes |
/** * Construct a {@link TransactionContext} from a client request. * Used by the state machine to start a transaction * and send the Log entry representing the transaction data * to be applied to the raft log. */ public TransactionContextImpl( StateMachine stateMachine, RaftClientRequest clientRequest, StateMachineLogEntryProto smLogEntryProto, Object stateMachineContext) { this(RaftPeerRole.LEADER, stateMachine); this.clientRequest = clientRequest; this.smLogEntryProto = smLogEntryProto != null? smLogEntryProto : ServerProtoUtils.toStateMachineLogEntryProto(clientRequest, null, null); this.stateMachineContext = stateMachineContext; }
Example #19
Source File: SimpleStateMachine4Testing.java From ratis with Apache License 2.0 | 5 votes |
@Override public TransactionContext startTransaction(RaftClientRequest request) { blocking.await(Blocking.Type.START_TRANSACTION); return TransactionContext.newBuilder() .setStateMachine(this) .setClientRequest(request) .setStateMachineData(STATE_MACHINE_DATA) .build(); }
Example #20
Source File: CombinedClientProtocolClientSideTranslatorPB.java From ratis with Apache License 2.0 | 5 votes |
@Override public RaftClientReply submitClientRequest(RaftClientRequest request) throws IOException { return handleRequest(request, ClientProtoUtils::toRaftClientRequestProto, ClientProtoUtils::toRaftClientReply, p -> getProtocol().submitClientRequest(null, p)); }
Example #21
Source File: ServerProtoUtils.java From ratis with Apache License 2.0 | 5 votes |
static StateMachineLogEntryProto toStateMachineLogEntryProto( RaftClientRequest request, ByteString logData, ByteString stateMachineData) { if (logData == null) { logData = request.getMessage().getContent(); } return toStateMachineLogEntryProto(request.getClientId(), request.getCallId(), logData, stateMachineData); }
Example #22
Source File: WatchRequests.java From incubator-ratis with Apache License 2.0 | 5 votes |
CompletableFuture<Void> add(RaftClientRequest request) { final long currentTime = Timestamp.currentTimeNanos(); final long roundUp = watchTimeoutDenominationNanos.roundUpNanos(currentTime); final PendingWatch pending = new PendingWatch(request.getType().getWatch(), Timestamp.valueOf(roundUp)); final PendingWatch computed; synchronized (this) { if (pending.getIndex() <= getIndex()) { // compare again synchronized // watch condition already satisfied return null; } computed = q.compute(pending, (key, old) -> old != null? old: resource.tryAcquire()? pending: null); } if (computed == null) { // failed to acquire return JavaUtils.completeExceptionally(new ResourceUnavailableException( "Failed to acquire a pending watch request in " + name + " for " + request)); } if (computed != pending) { // already exists in q return computed.getFuture(); } // newly added to q final TimeDuration timeout = watchTimeoutNanos.apply(duration -> duration + roundUp - currentTime); scheduler.onTimeout(timeout, () -> handleTimeout(request, pending), LOG, () -> name + ": Failed to timeout " + request); return pending.getFuture(); }
Example #23
Source File: StreamRequests.java From incubator-ratis with Apache License 2.0 | 5 votes |
CompletableFuture<ByteString> streamEndOfRequestAsync(RaftClientRequest request) { final StreamRequestTypeProto stream = request.getType().getStream(); Preconditions.assertTrue(stream.getEndOfRequest()); final Key key = new Key(request.getClientId(), stream.getStreamId()); final PendingStream pending = streams.remove(key); if (pending == null) { return JavaUtils.completeExceptionally(new StreamException(name + ": " + key + " not found")); } return pending.getBytes(stream.getMessageId(), request.getMessage()); }
Example #24
Source File: StreamRequests.java From incubator-ratis with Apache License 2.0 | 5 votes |
CompletableFuture<?> streamAsync(RaftClientRequest request) { final StreamRequestTypeProto stream = request.getType().getStream(); Preconditions.assertTrue(!stream.getEndOfRequest()); final Key key = new Key(request.getClientId(), stream.getStreamId()); final PendingStream pending = streams.computeIfAbsent(key); return pending.append(stream.getMessageId(), request.getMessage()); }
Example #25
Source File: WatchRequests.java From ratis with Apache License 2.0 | 5 votes |
void handleTimeout(RaftClientRequest request, PendingWatch pending) { if (removeExisting(pending)) { pending.getFuture().completeExceptionally( new NotReplicatedException(request.getCallId(), replication, pending.getIndex())); LOG.debug("{}: timeout {}, {}", name, pending, request); } }
Example #26
Source File: PendingRequests.java From incubator-ratis with Apache License 2.0 | 5 votes |
void replySetConfiguration(Supplier<Collection<CommitInfoProto>> getCommitInfos) { // we allow the pendingRequest to be null in case that the new leader // commits the new configuration while it has not received the retry // request from the client if (pendingSetConf != null) { final RaftClientRequest request = pendingSetConf.getRequest(); LOG.debug("{}: sends success for {}", name, request); // for setConfiguration we do not need to wait for statemachine. send back // reply after it's committed. pendingSetConf.setReply(new RaftClientReply(request, getCommitInfos.get())); pendingSetConf = null; } }
Example #27
Source File: PendingRequests.java From incubator-ratis with Apache License 2.0 | 5 votes |
PendingRequest add(Permit permit, RaftClientRequest request, TransactionContext entry) { // externally synced for now Preconditions.assertTrue(request.is(RaftClientRequestProto.TypeCase.WRITE)); final long index = entry.getLogEntry().getIndex(); LOG.debug("{}: addPendingRequest at index={}, request={}", name, index, request); final PendingRequest pending = new PendingRequest(index, request, entry); return pendingRequests.put(permit, index, pending); }
Example #28
Source File: ServerProtoUtils.java From incubator-ratis with Apache License 2.0 | 5 votes |
static StateMachineLogEntryProto toStateMachineLogEntryProto( RaftClientRequest request, ByteString logData, ByteString stateMachineData) { if (logData == null) { logData = request.getMessage().getContent(); } return toStateMachineLogEntryProto(request.getClientId(), request.getCallId(), logData, stateMachineData); }
Example #29
Source File: TransactionContextImpl.java From incubator-ratis with Apache License 2.0 | 5 votes |
/** * Construct a {@link TransactionContext} from a client request. * Used by the state machine to start a transaction * and send the Log entry representing the transaction data * to be applied to the raft log. */ public TransactionContextImpl( StateMachine stateMachine, RaftClientRequest clientRequest, StateMachineLogEntryProto smLogEntryProto, Object stateMachineContext) { this(RaftPeerRole.LEADER, stateMachine); this.clientRequest = clientRequest; this.smLogEntryProto = smLogEntryProto != null? smLogEntryProto : ServerProtoUtils.toStateMachineLogEntryProto(clientRequest, null, null); this.stateMachineContext = stateMachineContext; }
Example #30
Source File: BaseStateMachine.java From incubator-ratis with Apache License 2.0 | 5 votes |
@Override public TransactionContext startTransaction(RaftClientRequest request) throws IOException { return TransactionContext.newBuilder() .setStateMachine(this) .setClientRequest(request) .build(); }