/** * Return a new log entry based on the input log entry with stateMachineData added. * @param stateMachineData - state machine data to be added * @param entry - log entry to which stateMachineData needs to be added * @return LogEntryProto with stateMachineData added */ static LogEntryProto addStateMachineData(ByteString stateMachineData, LogEntryProto entry) { Preconditions.assertTrue(shouldReadStateMachineData(entry), () -> "Failed to addStateMachineData to " + entry + " since shouldReadStateMachineData is false."); return rebuildLogEntryProto(entry, toStateMachineEntryProtoBuilder(stateMachineData)); }
@Override public EntryWithData getEntryWithData(long index) throws RaftLogIOException { final LogEntryProto entry = get(index); if (!ServerProtoUtils.shouldReadStateMachineData(entry)) { return new EntryWithData(entry, null); } try { return new EntryWithData(entry, server.map(s -> s.getStateMachine().readStateMachineData(entry)).orElse(null)); } catch (Throwable e) { final String err = getSelfId() + ": Failed readStateMachineData for " + ServerProtoUtils.toLogEntryString(entry); LOG.error(err, e); throw new RaftLogIOException(err, JavaUtils.unwrapCompletionException(e)); } }
public LogEntryProto getEntry(TimeDuration timeout) throws RaftLogIOException, TimeoutException { LogEntryProto entryProto; if (future == null) { return logEntry; } try { entryProto = future.thenApply(data -> ServerProtoUtils.addStateMachineData(data, logEntry)) .get(timeout.getDuration(), timeout.getUnit()); } catch (TimeoutException t) { throw t; } catch (Throwable t) { final String err = selfId + ": Failed readStateMachineData for " + ServerProtoUtils.toLogEntryString(logEntry); LogAppender.LOG.error(err, t); throw new RaftLogIOException(err, JavaUtils.unwrapCompletionException(t)); } // by this time we have already read the state machine data, // so the log entry data should be set now if (ServerProtoUtils.shouldReadStateMachineData(entryProto)) { final String err = selfId + ": State machine data not set for " + ServerProtoUtils.toLogEntryString(logEntry); LogAppender.LOG.error(err); throw new RaftLogIOException(err); } return entryProto; }