org.apache.ratis.statemachine.TransactionContext Java Examples
The following examples show how to use
org.apache.ratis.statemachine.TransactionContext.
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: 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 #2
Source File: LeaderState.java From incubator-ratis with Apache License 2.0 | 6 votes |
void stop() { this.running = false; // do not interrupt event processor since it may be in the middle of logSync senders.forEach(LogAppender::stopAppender); final NotLeaderException nle = server.generateNotLeaderException(); final Collection<CommitInfoProto> commitInfos = server.getCommitInfos(); try { final Collection<TransactionContext> transactions = pendingRequests.sendNotLeaderResponses(nle, commitInfos); server.getStateMachine().notifyNotLeader(transactions); watchRequests.failWatches(nle); } catch (IOException e) { LOG.warn("{}: Caught exception in sendNotLeaderResponses", this, e); } streamRequests.clear(); server.getServerRpc().notifyNotLeader(server.getMemberId().getGroupId()); logAppenderMetrics.unregister(); raftServerMetrics.unregister(); if (pendingRequests != null) { pendingRequests.close(); } }
Example #3
Source File: PendingRequests.java From incubator-ratis with Apache License 2.0 | 6 votes |
Collection<TransactionContext> setNotLeaderException(NotLeaderException nle, Collection<CommitInfoProto> commitInfos) { synchronized (this) { resource.close(); permits.clear(); } LOG.debug("{}: PendingRequests.setNotLeaderException", name); final List<TransactionContext> transactions = new ArrayList<>(map.size()); for(;;) { final Iterator<Long> i = map.keySet().iterator(); if (!i.hasNext()) { // the map is empty return transactions; } final PendingRequest pending = map.remove(i.next()); if (pending != null) { transactions.add(pending.setNotLeaderException(nle, commitInfos)); } } }
Example #4
Source File: StateMachineShutdownTests.java From incubator-ratis with Apache License 2.0 | 6 votes |
@Override public CompletableFuture<Message> applyTransaction(TransactionContext trx) { CompletableFuture<Message> future = new CompletableFuture<Message>(); if (blockOnApply) { synchronized (objectToWait) { try { objectToWait.wait(); } catch (InterruptedException e) { throw new RuntimeException(); } } } RaftProtos.LogEntryProto entry = trx.getLogEntry(); updateLastAppliedTermIndex(entry.getTerm(), entry.getIndex()); future.complete(new RaftTestUtil.SimpleMessage("done")); return future; }
Example #5
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 #6
Source File: ArithmeticStateMachine.java From ratis with Apache License 2.0 | 6 votes |
@Override public CompletableFuture<Message> applyTransaction(TransactionContext trx) { final LogEntryProto entry = trx.getLogEntry(); final AssignmentMessage assignment = new AssignmentMessage(entry.getStateMachineLogEntry().getLogData()); final long index = entry.getIndex(); final Double result; try(final AutoCloseableLock writeLock = writeLock()) { result = assignment.evaluate(variables); updateLastAppliedTermIndex(entry.getTerm(), index); } final Expression r = Expression.Utils.double2Expression(result); final CompletableFuture<Message> f = CompletableFuture.completedFuture(Expression.Utils.toMessage(r)); final RaftPeerRole role = trx.getServerRole(); if (role == RaftPeerRole.LEADER) { LOG.info("{}:{}-{}: {} = {}", role, getId(), index, assignment, r); } else { LOG.debug("{}:{}-{}: {} = {}", role, getId(), index, assignment, r); } if (LOG.isTraceEnabled()) { LOG.trace("{}-{}: variables={}", getId(), index, variables); } return f; }
Example #7
Source File: ArithmeticStateMachine.java From incubator-ratis with Apache License 2.0 | 6 votes |
@Override public CompletableFuture<Message> applyTransaction(TransactionContext trx) { final LogEntryProto entry = trx.getLogEntry(); final AssignmentMessage assignment = new AssignmentMessage(entry.getStateMachineLogEntry().getLogData()); final long index = entry.getIndex(); final Double result; try(AutoCloseableLock writeLock = writeLock()) { result = assignment.evaluate(variables); updateLastAppliedTermIndex(entry.getTerm(), index); } final Expression r = Expression.Utils.double2Expression(result); final CompletableFuture<Message> f = CompletableFuture.completedFuture(Expression.Utils.toMessage(r)); final RaftPeerRole role = trx.getServerRole(); if (role == RaftPeerRole.LEADER) { LOG.info("{}:{}-{}: {} = {}", role, getId(), index, assignment, r); } else { LOG.debug("{}:{}-{}: {} = {}", role, getId(), index, assignment, r); } if (LOG.isTraceEnabled()) { LOG.trace("{}-{}: variables={}", getId(), index, variables); } return f; }
Example #8
Source File: LogStateMachine.java From ratis with Apache License 2.0 | 6 votes |
@Override public CompletableFuture<Message> applyTransaction(TransactionContext trx) { try { checkInitialization(); final LogEntryProto entry = trx.getLogEntry(); LogServiceRequestProto logServiceRequestProto = LogServiceRequestProto.parseFrom(entry.getStateMachineLogEntry().getLogData()); switch (logServiceRequestProto.getRequestCase()) { case CLOSELOG: return processCloseLog(logServiceRequestProto); case APPENDREQUEST: return processAppendRequest(trx, logServiceRequestProto); case SYNCREQUEST: return processSyncRequest(trx, logServiceRequestProto); default: //TODO return null; } } catch (IOException e) { // TODO exception handling throw new RuntimeException(e); } }
Example #9
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 #10
Source File: LogStateMachine.java From incubator-ratis with Apache License 2.0 | 5 votes |
private CompletableFuture<Message> processAppendRequest(TransactionContext trx, LogServiceRequestProto logProto) { final LogEntryProto entry = trx.getLogEntry(); AppendLogEntryRequestProto proto = logProto.getAppendRequest(); final long index = entry.getIndex(); long newSize = 0; Throwable t = verifyState(State.OPEN); final List<Long> ids = new ArrayList<Long>(); if (t == null) { try (AutoCloseableLock writeLock = writeLock()) { List<byte[]> entries = LogServiceProtoUtil.toListByteArray(proto.getDataList()); for (byte[] bb : entries) { ids.add(this.length); newSize += bb.length; this.length++; } this.dataRecordsSize += newSize; // TODO do we need this for other write request (close, sync) updateLastAppliedTermIndex(entry.getTerm(), index); } } final CompletableFuture<Message> f = CompletableFuture.completedFuture( Message.valueOf(LogServiceProtoUtil.toAppendLogReplyProto(ids, t).toByteString())); final RaftProtos.RaftPeerRole role = trx.getServerRole(); if (LOG.isTraceEnabled()) { LOG.trace("{}:{}-{}: {} new length {}", role, getId(), index, TextFormat.shortDebugString(proto), dataRecordsSize); } return f; }
Example #11
Source File: LogStateMachine.java From incubator-ratis with Apache License 2.0 | 5 votes |
/** * Process sync request * @param trx transaction * @param logMessage message * @return reply message */ private CompletableFuture<Message> processSyncRequest(TransactionContext trx, LogServiceRequestProto logMessage) { long index = trx.getLogEntry().getIndex(); // TODO: Do we really need this call? return CompletableFuture.completedFuture(Message .valueOf(LogServiceProtoUtil.toSyncLogReplyProto(index, null).toByteString())); }
Example #12
Source File: LogStateMachine.java From incubator-ratis with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<Message> applyTransaction(TransactionContext trx) { try { checkInitialization(); final LogEntryProto entry = trx.getLogEntry(); LogServiceRequestProto logServiceRequestProto = LogServiceRequestProto.parseFrom(entry.getStateMachineLogEntry().getLogData()); switch (logServiceRequestProto.getRequestCase()) { case CHANGESTATE: return recordTime(getCloseLogTimer, new Task(){ @Override public CompletableFuture<Message> run() { return processChangeState(logServiceRequestProto); }}); case APPENDREQUEST: return recordTime(appendRequestTimer, new Task(){ @Override public CompletableFuture<Message> run() { return processAppendRequest(trx, logServiceRequestProto); }}); case SYNCREQUEST: return recordTime(syncRequesTimer, new Task(){ @Override public CompletableFuture<Message> run() { return processSyncRequest(trx, logServiceRequestProto); }}); case ARCHIVELOG: return updateArchiveLogInfo(logServiceRequestProto); default: //TODO return null; } } catch (IOException e) { // TODO exception handling throw new RuntimeException(e); } }
Example #13
Source File: RaftStateMachineExceptionTests.java From ratis with Apache License 2.0 | 5 votes |
@Override public TransactionContext preAppendTransaction(TransactionContext trx) throws IOException { if (failPreAppend) { throw new IOException("Fake Exception in preAppend"); } else { return trx; } }
Example #14
Source File: LogStateMachine.java From incubator-ratis with Apache License 2.0 | 5 votes |
@Override public void notifyNotLeader(Collection<TransactionContext> pendingEntries) throws IOException { for(Future<Boolean> archiveFuture:archiveExportFutures.values()) { if (archiveFuture != null && !archiveFuture.isCancelled()) { archiveFuture.cancel(true); } } }
Example #15
Source File: OzoneManagerStateMachine.java From hadoop-ozone with Apache License 2.0 | 5 votes |
/** * Handle the RaftClientRequest and return TransactionContext object. * @param raftClientRequest * @param omRequest * @return TransactionContext */ private TransactionContext handleStartTransactionRequests( RaftClientRequest raftClientRequest, OMRequest omRequest) { return TransactionContext.newBuilder() .setClientRequest(raftClientRequest) .setStateMachine(this) .setServerRole(RaftProtos.RaftPeerRole.LEADER) .setLogData(raftClientRequest.getMessage().getContent()) .build(); }
Example #16
Source File: RaftServerImpl.java From ratis with Apache License 2.0 | 5 votes |
/** * Handle a normal update request from client. */ private CompletableFuture<RaftClientReply> appendTransaction( RaftClientRequest request, TransactionContext context, RetryCache.CacheEntry cacheEntry) throws IOException { assertLifeCycleState(RUNNING); CompletableFuture<RaftClientReply> reply; final PendingRequest pending; synchronized (this) { reply = checkLeaderState(request, cacheEntry); if (reply != null) { return reply; } // append the message to its local log final LeaderState leaderState = role.getLeaderStateNonNull(); try { state.appendLog(context); } catch (StateMachineException e) { // the StateMachineException is thrown by the SM in the preAppend stage. // Return the exception in a RaftClientReply. RaftClientReply exceptionReply = new RaftClientReply(request, e, getCommitInfos()); cacheEntry.failWithReply(exceptionReply); // leader will step down here if (isLeader() && leaderState != null) { leaderState.submitStepDownEvent(); } return CompletableFuture.completedFuture(exceptionReply); } // put the request into the pending queue pending = leaderState.addPendingRequest(request, context); leaderState.notifySenders(); } return pending.getFuture(); }
Example #17
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 #18
Source File: PendingRequests.java From incubator-ratis with Apache License 2.0 | 5 votes |
/** * The leader state is stopped. Send NotLeaderException to all the pending * requests since they have not got applied to the state machine yet. */ Collection<TransactionContext> sendNotLeaderResponses(NotLeaderException nle, Collection<CommitInfoProto> commitInfos) { LOG.info("{}: sendNotLeaderResponses", name); final Collection<TransactionContext> transactions = pendingRequests.setNotLeaderException(nle, commitInfos); if (pendingSetConf != null) { pendingSetConf.setNotLeaderException(nle, commitInfos); } return transactions; }
Example #19
Source File: FileStoreStateMachine.java From incubator-ratis with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<Message> applyTransaction(TransactionContext trx) { final LogEntryProto entry = trx.getLogEntry(); final long index = entry.getIndex(); updateLastAppliedTermIndex(entry.getTerm(), index); final StateMachineLogEntryProto smLog = entry.getStateMachineLogEntry(); final FileStoreRequestProto request; try { request = FileStoreRequestProto.parseFrom(smLog.getLogData()); } catch (InvalidProtocolBufferException e) { return FileStoreCommon.completeExceptionally(index, "Failed to parse logData in" + smLog, e); } switch(request.getRequestCase()) { case DELETE: return delete(index, request.getDelete()); case WRITEHEADER: return writeCommit(index, request.getWriteHeader(), smLog.getStateMachineEntry().getStateMachineData().size()); case WRITE: // WRITE should not happen here since // startTransaction converts WRITE requests to WRITEHEADER requests. default: LOG.error(getId() + ": Unexpected request case " + request.getRequestCase()); return FileStoreCommon.completeExceptionally(index, "Unexpected request case " + request.getRequestCase()); } }
Example #20
Source File: LeaderState.java From incubator-ratis with Apache License 2.0 | 5 votes |
PendingRequest addPendingRequest(PendingRequests.Permit permit, RaftClientRequest request, TransactionContext entry) { if (LOG.isDebugEnabled()) { LOG.debug("{}: addPendingRequest at {}, entry={}", this, request, ServerProtoUtils.toLogEntryString(entry.getLogEntry())); } return pendingRequests.add(permit, request, entry); }
Example #21
Source File: RaftServerImpl.java From ratis with Apache License 2.0 | 5 votes |
CompletableFuture<Message> applyLogToStateMachine(LogEntryProto next) { final StateMachine stateMachine = getStateMachine(); if (next.hasConfigurationEntry()) { // the reply should have already been set. only need to record // the new conf in the metadata file. state.writeRaftConfiguration(next); } else if (next.hasStateMachineLogEntry()) { // check whether there is a TransactionContext because we are the leader. TransactionContext trx = role.getLeaderState() .map(leader -> leader.getTransactionContext(next.getIndex())).orElseGet( () -> TransactionContext.newBuilder() .setServerRole(role.getCurrentRole()) .setStateMachine(stateMachine) .setLogEntry(next) .build()); // Let the StateMachine inject logic for committed transactions in sequential order. trx = stateMachine.applyTransactionSerial(trx); try { // TODO: This step can be parallelized CompletableFuture<Message> stateMachineFuture = stateMachine.applyTransaction(trx); return replyPendingRequest(next, stateMachineFuture); } catch (Throwable e) { LOG.error("{}: applyTransaction failed for index:{} proto:{}", getId(), next.getIndex(), ServerProtoUtils.toString(next), e.getMessage()); throw e; } } return null; }
Example #22
Source File: RaftServerImpl.java From incubator-ratis with Apache License 2.0 | 5 votes |
CompletableFuture<Message> applyLogToStateMachine(LogEntryProto next) { if (!next.hasStateMachineLogEntry()) { stateMachine.notifyIndexUpdate(next.getTerm(), next.getIndex()); } if (next.hasConfigurationEntry()) { // the reply should have already been set. only need to record // the new conf in the metadata file. state.writeRaftConfiguration(next); } else if (next.hasStateMachineLogEntry()) { // check whether there is a TransactionContext because we are the leader. TransactionContext trx = role.getLeaderState() .map(leader -> leader.getTransactionContext(next.getIndex())).orElseGet( () -> TransactionContext.newBuilder() .setServerRole(role.getCurrentRole()) .setStateMachine(stateMachine) .setLogEntry(next) .build()); // Let the StateMachine inject logic for committed transactions in sequential order. trx = stateMachine.applyTransactionSerial(trx); try { // TODO: This step can be parallelized CompletableFuture<Message> stateMachineFuture = stateMachine.applyTransaction(trx); return replyPendingRequest(next, stateMachineFuture); } catch (Throwable e) { LOG.error("{}: applyTransaction failed for index:{} proto:{}", getMemberId(), next.getIndex(), ServerProtoUtils.toString(next), e); throw e; } } return null; }
Example #23
Source File: RaftStateMachineExceptionTests.java From incubator-ratis with Apache License 2.0 | 5 votes |
@Override public TransactionContext preAppendTransaction(TransactionContext trx) throws IOException { if (failPreAppend) { throw new IOException("Fake Exception in preAppend"); } else { return trx; } }
Example #24
Source File: FileStoreStateMachine.java From ratis with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<Message> applyTransaction(TransactionContext trx) { final LogEntryProto entry = trx.getLogEntry(); final long index = entry.getIndex(); updateLastAppliedTermIndex(entry.getTerm(), index); final StateMachineLogEntryProto smLog = entry.getStateMachineLogEntry(); final FileStoreRequestProto request; try { request = FileStoreRequestProto.parseFrom(smLog.getLogData()); } catch (InvalidProtocolBufferException e) { return FileStoreCommon.completeExceptionally(index, "Failed to parse logData in" + smLog, e); } switch(request.getRequestCase()) { case DELETE: return delete(index, request.getDelete()); case WRITEHEADER: return writeCommit(index, request.getWriteHeader(), smLog.getStateMachineEntry().getStateMachineData().size()); case WRITE: // WRITE should not happen here since // startTransaction converts WRITE requests to WRITEHEADER requests. default: LOG.error(getId() + ": Unexpected request case " + request.getRequestCase()); return FileStoreCommon.completeExceptionally(index, "Unexpected request case " + request.getRequestCase()); } }
Example #25
Source File: LogStateMachine.java From ratis with Apache License 2.0 | 5 votes |
/** * Process sync request * @param trx transaction * @param logMessage message * @return reply message */ private CompletableFuture<Message> processSyncRequest(TransactionContext trx, LogServiceRequestProto logMessage) { long index = trx.getLogEntry().getIndex(); // TODO: Do we really need this call? return CompletableFuture.completedFuture(Message .valueOf(LogServiceProtoUtil.toSyncLogReplyProto(index, null).toByteString())); }
Example #26
Source File: StateMachineShutdownTests.java From ratis with Apache License 2.0 | 5 votes |
@Override public CompletableFuture<Message> applyTransaction(TransactionContext trx) { CompletableFuture<Message> future = new CompletableFuture<Message>(); if (blockOnApply) { synchronized (objectToWait) { try { objectToWait.wait(); } catch (InterruptedException e) { throw new RuntimeException(); } } } future.complete(new RaftTestUtil.SimpleMessage("done")); return future; }
Example #27
Source File: BaseStateMachine.java From ratis with Apache License 2.0 | 5 votes |
@Override public TransactionContext startTransaction(RaftClientRequest request) throws IOException { return TransactionContext.newBuilder() .setStateMachine(this) .setClientRequest(request) .build(); }
Example #28
Source File: PendingRequests.java From ratis with Apache License 2.0 | 5 votes |
Collection<TransactionContext> setNotLeaderException(NotLeaderException nle, Collection<CommitInfoProto> commitInfos) { LOG.debug("{}: PendingRequests.setNotLeaderException", name); try { return map.values().stream() .map(p -> p.setNotLeaderException(nle, commitInfos)) .collect(Collectors.toList()); } finally { map.clear(); } }
Example #29
Source File: PendingRequests.java From ratis with Apache License 2.0 | 5 votes |
PendingRequest add(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); pendingRequests.put(index, pending); return pending; }
Example #30
Source File: PendingRequests.java From ratis with Apache License 2.0 | 5 votes |
/** * The leader state is stopped. Send NotLeaderException to all the pending * requests since they have not got applied to the state machine yet. */ Collection<TransactionContext> sendNotLeaderResponses(NotLeaderException nle, Collection<CommitInfoProto> commitInfos) { LOG.info("{}: sendNotLeaderResponses", name); final Collection<TransactionContext> transactions = pendingRequests.setNotLeaderException(nle, commitInfos); if (pendingSetConf != null) { pendingSetConf.setNotLeaderException(nle, commitInfos); } return transactions; }