/** * 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; }
private SimpleOperation(ClientId clientId, long callId, String op, boolean hasStateMachineData) { this.op = Objects.requireNonNull(op); final ByteString bytes = ProtoUtils.toByteString(op); this.smLogEntryProto = ServerProtoUtils.toStateMachineLogEntryProto( clientId, callId, bytes, hasStateMachineData? bytes: null); }
public TransactionContext build() { Objects.requireNonNull(serverRole, "serverRole == null"); Objects.requireNonNull(stateMachine, "stateMachine == null"); if (clientRequest != null) { Preconditions.assertTrue(serverRole == RaftPeerRole.LEADER, () -> "serverRole MUST be LEADER since clientRequest != null, serverRole is " + serverRole); Preconditions.assertNull(logEntry, () -> "logEntry MUST be null since clientRequest != null"); if (stateMachineLogEntry == null) { stateMachineLogEntry = ServerProtoUtils.toStateMachineLogEntryProto(clientRequest, logData, stateMachineData); } return new TransactionContextImpl(stateMachine, clientRequest, stateMachineLogEntry, stateMachineContext); } else { Objects.requireNonNull(logEntry, "logEntry MUST NOT be null since clientRequest == null"); Preconditions.assertTrue(logEntry.hasStateMachineLogEntry(), () -> "Unexpected logEntry: stateMachineLogEntry not found, logEntry=" + logEntry); return new TransactionContextImpl(serverRole, stateMachine, logEntry); } } }
static StateMachineLogEntryProto toStateMachineLogEntryProto( RaftClientRequest request, ByteString logData, ByteString stateMachineData) { if (logData == null) { logData = request.getMessage().getContent(); } return toStateMachineLogEntryProto(request.getClientId(), request.getCallId(), logData, stateMachineData); }