private void checkInitialization() throws IOException { if (this.log == null) { ServerState state = proxy.getImpl(groupId).getState(); this.log = state.getLog(); } }
boolean isConfCommitted() { return getLog().getLastCommittedIndex() >= getRaftConf().getLogEntryIndex(); }
boolean isConfCommitted() { return getLog().getLastCommittedIndex() >= getRaftConf().getLogEntryIndex(); }
private boolean containPrevious(TermIndex previous) { if (LOG.isTraceEnabled()) { LOG.trace("{}: prev:{}, latestSnapshot:{}, latestInstalledSnapshot:{}", getId(), previous, state.getLatestSnapshot(), state.getLatestInstalledSnapshot()); } return state.getLog().contains(previous) || (state.getLatestSnapshot() != null && state.getLatestSnapshot().getTermIndex().equals(previous)) || (state.getLatestInstalledSnapshot() != null) && state.getLatestInstalledSnapshot().equals(previous); }
private boolean containPrevious(TermIndex previous) { if (LOG.isTraceEnabled()) { LOG.trace("{}: prev:{}, latestSnapshot:{}, latestInstalledSnapshot:{}", getId(), previous, state.getLatestSnapshot(), state.getLatestInstalledSnapshot()); } return state.getLog().contains(previous) || (state.getLatestSnapshot() != null && state.getLatestSnapshot().getTermIndex().equals(previous)) || (state.getLatestInstalledSnapshot() != null) && state.getLatestInstalledSnapshot().equals(previous); }
private void applyOldNewConf() { final ServerState state = server.getState(); final RaftConfiguration current = server.getRaftConf(); final RaftConfiguration oldNewConf= stagingState.generateOldNewConf(current, state.getLog().getNextIndex()); // apply the (old, new) configuration to log, and use it as the current conf long index = state.getLog().append(state.getCurrentTerm(), oldNewConf); updateConfiguration(index, oldNewConf); this.stagingState = null; notifySenders(); }
public String printAllLogs() { StringBuilder b = new StringBuilder("\n#servers = " + servers.size() + "\n"); for (RaftServerImpl s : iterateServerImpls()) { b.append(" "); b.append(s).append("\n"); final RaftLog log = s.getState().getLog(); if (log instanceof MemoryRaftLog) { b.append(" "); b.append(((MemoryRaftLog) log).getEntryString()); } } return b.toString(); }
private void applyOldNewConf() { final ServerState state = server.getState(); final RaftConfiguration current = server.getRaftConf(); final RaftConfiguration oldNewConf= stagingState.generateOldNewConf(current, state.getLog().getNextIndex()); // apply the (old, new) configuration to log, and use it as the current conf long index = state.getLog().append(state.getCurrentTerm(), oldNewConf); updateConfiguration(index, oldNewConf); this.stagingState = null; notifySenders(); }
public String printAllLogs() { StringBuilder b = new StringBuilder("\n#servers = " + servers.size() + "\n"); for (RaftServerImpl s : iterateServerImpls()) { b.append(" "); b.append(s).append("\n"); final RaftLog log = s.getState().getLog(); if (log instanceof MemoryRaftLog) { b.append(" "); b.append(((MemoryRaftLog) log).getEntryString()); } } return b.toString(); }
static void assertLogEntries(Collection<RaftServerProxy> servers, SimpleMessage... expectedMessages) { final int size = servers.size(); final long count = MiniRaftCluster.getServerStream(servers) .filter(RaftServerImpl::isAlive) .map(s -> s.getState().getLog()) .filter(log -> logEntriesContains(log, expectedMessages)) .count(); if (2*count <= size) { throw new AssertionError("Not in majority: size=" + size + " but count=" + count); } }
static void assertLogEntries(MiniRaftCluster cluster, SimpleMessage expectedMessage) { final int size = cluster.getServers().size(); final long count = cluster.getServerAliveStream() .map(s -> s.getState().getLog()) .filter(log -> logEntriesContains(log, expectedMessage)) .count(); if (2*count <= size) { throw new AssertionError("Not in majority: size=" + size + " but count=" + count); } }
static void assertLogEntries(RaftServerImpl server, long expectedTerm, SimpleMessage... expectedMessages) { LOG.info("checking raft log for " + server.getId()); final RaftLog log = server.getState().getLog(); try { RaftTestUtil.assertLogEntries(log, expectedTerm, expectedMessages); } catch (AssertionError e) { LOG.error(server.getId() + ": Unexpected raft log", e); throw e; } }
static void assertLogEntries(RaftServerImpl server, long expectedTerm, SimpleMessage... expectedMessages) { LOG.info("checking raft log for " + server.getId()); final RaftLog log = server.getState().getLog(); try { RaftTestUtil.assertLogEntries(log, expectedTerm, expectedMessages); } catch (AssertionError e) { LOG.error(server.getId() + ": Unexpected raft log", e); throw e; } }
private CompletableFuture<RaftClientReply> staleReadAsync(RaftClientRequest request) { final long minIndex = request.getType().getStaleRead().getMinIndex(); final long commitIndex = state.getLog().getLastCommittedIndex(); LOG.debug("{}: minIndex={}, commitIndex={}", getId(), minIndex, commitIndex); if (commitIndex < minIndex) { final StaleReadException e = new StaleReadException( "Unable to serve stale-read due to server commit index = " + commitIndex + " < min = " + minIndex); return CompletableFuture.completedFuture( new RaftClientReply(request, new StateMachineException(getId(), e), getCommitInfos())); } return processQueryFuture(getStateMachine().queryStale(request.getMessage(), minIndex), request); }
private CompletableFuture<RaftClientReply> staleReadAsync(RaftClientRequest request) { final long minIndex = request.getType().getStaleRead().getMinIndex(); final long commitIndex = state.getLog().getLastCommittedIndex(); LOG.debug("{}: minIndex={}, commitIndex={}", getId(), minIndex, commitIndex); if (commitIndex < minIndex) { final StaleReadException e = new StaleReadException( "Unable to serve stale-read due to server commit index = " + commitIndex + " < min = " + minIndex); return CompletableFuture.completedFuture( new RaftClientReply(request, new StateMachineException(getId(), e), getCommitInfos())); } return processQueryFuture(getStateMachine().queryStale(request.getMessage(), minIndex), request); }
public LogAppender(RaftServerImpl server, LeaderState leaderState, FollowerInfo f) { this.follower = f; this.server = server; this.leaderState = leaderState; this.raftLog = server.getState().getLog(); final RaftProperties properties = server.getProxy().getProperties(); this.snapshotChunkMaxSize = RaftServerConfigKeys.Log.Appender.snapshotChunkSizeMax(properties).getSizeInt(); this.halfMinTimeoutMs = server.getMinTimeoutMs() / 2; final SizeInBytes bufferByteLimit = RaftServerConfigKeys.Log.Appender.bufferByteLimit(properties); final int bufferElementLimit = RaftServerConfigKeys.Log.Appender.bufferElementLimit(properties); this.buffer = new DataQueue<>(this, bufferByteLimit, bufferElementLimit, EntryWithData::getSerializedSize); this.lifeCycle = new LifeCycle(this); }
public LogAppender(RaftServerImpl server, LeaderState leaderState, FollowerInfo f) { this.follower = f; this.server = server; this.leaderState = leaderState; this.raftLog = server.getState().getLog(); final RaftProperties properties = server.getProxy().getProperties(); this.maxBufferSize = RaftServerConfigKeys.Log.Appender.bufferCapacity(properties).getSizeInt(); this.batchSending = RaftServerConfigKeys.Log.Appender.batchEnabled(properties); this.snapshotChunkMaxSize = RaftServerConfigKeys.Log.Appender.snapshotChunkSizeMax(properties).getSizeInt(); this.halfMinTimeoutMs = server.getMinTimeoutMs() / 2; this.buffer = new LogEntryBuffer(); this.lifeCycle = new LifeCycle(this); }
static void assertTruncatedLog(RaftPeerId id, File openLogFile, long lastIndex, MiniRaftCluster cluster) throws Exception { // truncate log FileUtils.truncateFile(openLogFile, openLogFile.length() - 1); final RaftServerImpl server = cluster.restartServer(id, false); // the last index should be one less than before Assert.assertEquals(lastIndex - 1, server.getState().getLog().getLastEntryTermIndex().getIndex()); server.getProxy().close(); }
public void assertServer(MiniRaftCluster cluster, ClientId clientId, long callId, long oldLastApplied) throws Exception { long leaderApplied = cluster.getLeader().getState().getLastAppliedIndex(); // make sure retry cache has the entry for (RaftServerImpl server : cluster.iterateServerImpls()) { LOG.info("check server " + server.getId()); if (server.getState().getLastAppliedIndex() < leaderApplied) { Thread.sleep(1000); } Assert.assertEquals(2, RaftServerTestUtil.getRetryCacheSize(server)); Assert.assertNotNull(RaftServerTestUtil.getRetryEntry(server, clientId, callId)); // make sure there is only one log entry committed Assert.assertEquals(1, count(server.getState().getLog(), oldLastApplied + 1)); } }
static void assertLeaderContent(MiniRaftCluster cluster) throws InterruptedException { final RaftServerImpl leader = RaftTestUtil.waitForLeader(cluster); Assert.assertEquals(SNAPSHOT_TRIGGER_THRESHOLD * 2, leader.getState().getLog().getLastCommittedIndex()); final LogEntryProto[] entries = SimpleStateMachine4Testing.get(leader).getContent(); for (int i = 1; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) { Assert.assertEquals(i+1, entries[i].getIndex()); Assert.assertArrayEquals( new SimpleMessage("m" + i).getContent().toByteArray(), entries[i].getSmLogEntry().getData().toByteArray()); } }