RaftServerImpl getLeader(RaftGroupId groupId, Runnable handleNoLeaders, Consumer<List<RaftServerImpl>> handleMultipleLeaders) { return getLeader(getLeaders(groupId), handleNoLeaders, handleMultipleLeaders); }
public RaftServerImpl getLeader() { return getLeader((RaftGroupId)null); }
boolean isLeader(String leaderId) { final RaftServerImpl leader = getLeader(); return leader != null && leader.getId().toString().equals(leaderId); }
/** * Get leader for the single group case. * Do not use this method if this cluster has multiple groups. * * @return the unique leader with the highest term. Or, return null if there is no leader. * @throws IllegalStateException if there are multiple leaders with the same highest term. */ public RaftServerImpl getLeader() { return getLeader(getLeaders(null), null, leaders -> { throw newIllegalStateExceptionForMultipleLeaders(null, leaders); }); }
boolean isLeader(String leaderId) throws InterruptedException { final RaftServerImpl leader = getLeader(); return leader != null && leader.getId().toString().equals(leaderId); }
static void sendMessageInNewThread(MiniRaftCluster cluster, SimpleMessage... messages) { RaftPeerId leaderId = cluster.getLeader().getId(); new Thread(() -> { try (final RaftClient client = cluster.createClient(leaderId)) { for (SimpleMessage mssg: messages) { client.send(mssg); } } catch (Exception e) { e.printStackTrace(); } }).start(); } }
public RaftClient createClientWithLeader() { return createClient(getLeader().getId(), group); }
public RaftServerImpl getLeader(RaftGroupId groupId) { Stream<RaftServerImpl> stream = getServerAliveStream(); if (groupId != null) { stream = stream.filter(s -> groupId.equals(s.getGroupId())); } return getLeader(stream); }
public RaftClient createClientWithLeader() { return createClient(getLeader().getId(), group); }
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()); }
static RaftServerImpl waitForLeader( MiniRaftCluster cluster, final String leaderId) throws InterruptedException { LOG.info(cluster.printServers()); for(int i = 0; !cluster.tryEnforceLeader(leaderId) && i < 10; i++) { RaftServerImpl currLeader = cluster.getLeader(); LOG.info("try enforcing leader to " + leaderId + " but " + (currLeader == null ? "no leader for this round" : "new leader is " + currLeader.getId())); } LOG.info(cluster.printServers()); final RaftServerImpl leader = cluster.getLeader(); Assert.assertEquals(leaderId, leader.getId().toString()); 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; }
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; }
static List<File> getSnapshotFiles(MiniRaftCluster cluster, long startIndex, long endIndex) { final RaftServerImpl leader = cluster.getLeader(); final SimpleStateMachineStorage storage = SimpleStateMachine4Testing.get(leader).getStateMachineStorage(); final long term = leader.getState().getCurrentTerm(); return LongStream.range(startIndex, endIndex) .mapToObj(i -> storage.getSnapshotFile(term, i)) .collect(Collectors.toList()); }
static File getSnapshotFile(MiniRaftCluster cluster, int i) { final RaftServerImpl leader = cluster.getLeader(); final SimpleStateMachine4Testing sm = SimpleStateMachine4Testing.get(leader); return sm.getStateMachineStorage().getSnapshotFile( leader.getState().getCurrentTerm(), i); }
@Before public void setup() throws IOException { Assert.assertNull(getCluster().getLeader()); getCluster().start(); }
@Before public void setup() throws IOException { Assert.assertNull(getCluster().getLeader()); getCluster().start(); }
@Test public void testHandleStateMachineException() throws Exception { setAndStart(cluster); final RaftPeerId leaderId = cluster.getLeader().getId(); try(final RaftClient client = cluster.createClient(leaderId)) { client.send(new SimpleMessage("m")); fail("Exception expected"); } catch (StateMachineException e) { e.printStackTrace(); Assert.assertTrue(e.getCause().getMessage().contains("Fake Exception")); } }
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 runTestFlushMetric(MiniRaftCluster cluster) throws Exception { int numMsg = 2; final RaftTestUtil.SimpleMessage[] messages = RaftTestUtil.SimpleMessage.create(numMsg); try (final RaftClient client = cluster.createClient()) { for (RaftTestUtil.SimpleMessage message : messages) { client.send(message); } } // For leader, flush must happen before client can get replies. assertFlushCount(cluster.getLeader()); // For followers, flush can be lagged behind. Attempt multiple times. for(RaftServerImpl f : cluster.getFollowers()) { JavaUtils.attempt(() -> assertFlushCount(f), 10, 100, f.getId() + "-assertFlushCount", null); } }