void submit(StateUpdateEvent event) { try { queue.put(event); } catch (InterruptedException e) { LOG.info("{}: Interrupted when submitting {} ", server.getId(), event); } }
private void setRole(Role newRole, String op) { LOG.info("{} changes role from {} to {} at term {} for {}", getId(), this.role, newRole, state.getCurrentTerm(), op); this.role = newRole; }
PendingRequest addPendingRequest(long index, RaftClientRequest request, TransactionContext entry) { LOG.debug("{}: addPendingRequest at index={}, request={}", server.getId(), index, request); return pendingRequests.addPendingRequest(index, request, entry); }
synchronized RequestVoteRequestProto createRequestVoteRequest( RaftPeerId targetId, long term, TermIndex lastEntry) { return ServerProtoUtils.toRequestVoteRequestProto(getId(), targetId, groupId, term, lastEntry); }
private void stop() { state = State.STOP; try { stateMachine.close(); } catch (IOException ignored) { LOG.warn(server.getId() + ": Failed to close " + stateMachine.getClass().getSimpleName() + " " + stateMachine, ignored); } }
@Override public String toString() { return getClass().getSimpleName() + "(" + server.getId() + " -> " + follower.getPeer().getId() + ")"; }
void stop() { this.running = false; // do not interrupt event processor since it may be in the middle of logSync senders.forEach(LogAppender::stopAppender); try { pendingRequests.sendNotLeaderResponses(); } catch (IOException e) { LOG.warn(server.getId() + ": Caught exception in sendNotLeaderResponses", e); } }
PendingRequest addPendingRequest(RaftClientRequest request, TransactionContext entry) { if (LOG.isDebugEnabled()) { LOG.debug("{}: addPendingRequest at {}, entry=", server.getId(), request, ServerProtoUtils.toLogEntryString(entry.getLogEntry())); } return pendingRequests.add(request, entry); }
@Override public RaftClientReply setConfiguration(SetConfigurationRequest request) throws IOException { return waitForReply(getId(), request, setConfigurationAsync(request)); }
LeaderElection(RaftServerImpl server) { this.server = server; conf = server.getRaftConf(); others = conf.getOtherPeers(server.getId()); this.running = true; }
@Override public RaftClientReply setConfiguration(SetConfigurationRequest request) throws IOException { return waitForReply(getId(), request, setConfigurationAsync(request)); }
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 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 sendRequest(AppendEntriesRequestProto request, StreamObserver<AppendEntriesRequestProto> s) { CodeInjectionForTesting.execute(GrpcService.GRPC_SEND_SERVER_REQUEST, server.getId(), null, request); s.onNext(request); scheduler.onTimeout(requestTimeoutDuration, () -> timeoutAppendRequest(request), LOG, () -> "Timeout check failed for append entry request: " + request); follower.updateLastRpcSendTime(); }
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); }
static String waitAndKillLeader(MiniRaftCluster cluster, boolean expectLeader) throws InterruptedException { final RaftServerImpl leader = waitForLeader(cluster); if (!expectLeader) { Assert.assertNull(leader); } else { Assert.assertNotNull(leader); LOG.info("killing leader = " + leader); cluster.killServer(leader.getId()); } return leader != null ? leader.getId().toString() : null; }
static void enforceLeader(MiniRaftCluster cluster, final String newLeader, Logger LOG) throws InterruptedException { LOG.info(cluster.printServers()); for(int i = 0; !cluster.tryEnforceLeader(newLeader) && i < 10; i++) { RaftServerImpl currLeader = cluster.getLeader(); LOG.info("try enforcing leader to " + newLeader + " but " + (currLeader == null ? "no leader for round " + i : "new leader is " + currLeader.getId())); } LOG.info(cluster.printServers()); final RaftServerImpl leader = cluster.getLeader(); Assert.assertEquals(newLeader, leader.getId().toString()); }
public RaftServerImpl getLeaderAndSendFirstMessage(boolean ignoreException) throws IOException { final RaftServerImpl leader = getLeader(); try(RaftClient client = createClient(leader.getId())) { client.send(new RaftTestUtil.SimpleMessage("first msg to make leader ready")); } catch (IOException e) { if (!ignoreException) { throw e; } } return leader; }
public RaftServerImpl getLeaderAndSendFirstMessage(boolean ignoreException) throws IOException { final RaftServerImpl leader = getLeader(); try(RaftClient client = createClient(leader.getId())) { client.send(new RaftTestUtil.SimpleMessage("first msg to make leader ready")); } catch (IOException e) { if (!ignoreException) { throw e; } } return leader; }