Java Code Examples for org.apache.ratis.conf.RaftProperties#setClass()
The following examples show how to use
org.apache.ratis.conf.RaftProperties#setClass() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RaftStateMachineExceptionTests.java From ratis with Apache License 2.0 | 6 votes |
@Test public void testHandleStateMachineException() throws Exception { final RaftProperties prop = getProperties(); prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, StateMachineWithException.class, StateMachine.class); final MiniRaftCluster cluster = newCluster(3); cluster.start(); RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId(); try(final RaftClient client = cluster.createClient(leaderId)) { client.send(new RaftTestUtil.SimpleMessage("m")); fail("Exception expected"); } catch (StateMachineException e) { e.printStackTrace(); Assert.assertTrue(e.getCause().getMessage().contains("Fake Exception")); } cluster.shutdown(); }
Example 2
Source File: TestExceptionDependentRetry.java From incubator-ratis with Apache License 2.0 | 5 votes |
@Test public void testExceptionRetryAttempts() throws InterruptedException, IOException { RaftProperties prop = new RaftProperties(); RaftClientConfigKeys.Rpc.setRequestTimeout(prop, TimeDuration.valueOf(100, TimeUnit.MILLISECONDS)); prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SimpleStateMachine4Testing.class, StateMachine.class); RaftServerConfigKeys.Write.setElementLimit(prop, 1); MiniRaftClusterWithGrpc cluster = getFactory().newCluster(1, prop); RaftServerImpl leader = null; try { cluster.start(); ExceptionDependentRetry.Builder builder = ExceptionDependentRetry.newBuilder(); builder.setExceptionToPolicy(TimeoutIOException.class, MultipleLinearRandomRetry.parseCommaSeparated("1ms, 5")); builder.setDefaultPolicy(RetryPolicies.retryForeverNoSleep()); // create a client with the exception dependent policy try (final RaftClient client = cluster.createClient(builder.build())) { client.sendAsync(new RaftTestUtil.SimpleMessage("1")).get(); leader = cluster.getLeader(); ((SimpleStateMachine4Testing) leader.getStateMachine()).blockWriteStateMachineData(); client.sendAsync(new RaftTestUtil.SimpleMessage("2")).get(); } Assert.fail("Test should have failed."); } catch (ExecutionException e) { RaftRetryFailureException rrfe = (RaftRetryFailureException) e.getCause(); Assert.assertEquals(6, rrfe.getAttemptCount()); } finally { ((SimpleStateMachine4Testing)leader.getStateMachine()).unblockWriteStateMachineData(); cluster.shutdown(); } }
Example 3
Source File: ParameterizedBaseTest.java From incubator-ratis with Apache License 2.0 | 5 votes |
public static <S extends StateMachine> Collection<Object[]> getMiniRaftClusters( Class<S> stateMachineClass, int clusterSize, Class<?>... clusterClasses) throws IOException { final RaftProperties prop = new RaftProperties(); prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, stateMachineClass, StateMachine.class); return getMiniRaftClusters(prop, clusterSize, clusterClasses); }
Example 4
Source File: StreamApiTests.java From incubator-ratis with Apache License 2.0 | 5 votes |
@Test public void testStream() throws Exception { final RaftProperties p = getProperties(); p.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SimpleStateMachine4Testing.class, StateMachine.class); runWithNewCluster(NUM_SERVERS, this::runTestStream); }
Example 5
Source File: StreamApiTests.java From incubator-ratis with Apache License 2.0 | 5 votes |
@Test public void testStreamAsync() throws Exception { final RaftProperties p = getProperties(); RaftClientConfigKeys.Stream.setSubmessageSize(p, SUBMESSAGE_SIZE); p.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SimpleStateMachine4Testing.class, StateMachine.class); runWithNewCluster(NUM_SERVERS, this::runTestStreamAsync); RaftClientConfigKeys.Stream.setSubmessageSize(p); }
Example 6
Source File: RaftSnapshotBaseTest.java From incubator-ratis with Apache License 2.0 | 5 votes |
@Before public void setup() throws IOException { final RaftProperties prop = new RaftProperties(); prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SimpleStateMachine4Testing.class, StateMachine.class); RaftServerConfigKeys.Snapshot.setAutoTriggerThreshold( prop, SNAPSHOT_TRIGGER_THRESHOLD); RaftServerConfigKeys.Snapshot.setAutoTriggerEnabled(prop, true); this.cluster = getFactory().newCluster(1, prop); cluster.start(); }
Example 7
Source File: TestStateMachine.java From incubator-ratis with Apache License 2.0 | 5 votes |
void runTestTransactionContextIsPassedBack(boolean useMemory) throws Throwable { final RaftProperties properties = new RaftProperties(); properties.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SMTransactionContext.class, StateMachine.class); RaftServerConfigKeys.Log.setUseMemory(properties, useMemory); try(MiniRaftClusterWithSimulatedRpc cluster = getFactory().newCluster(NUM_SERVERS, properties)) { cluster.start(); runTestTransactionContextIsPassedBack(cluster); } }
Example 8
Source File: TestStateMachine.java From ratis with Apache License 2.0 | 5 votes |
void runTestTransactionContextIsPassedBack(boolean useMemory) throws Throwable { final RaftProperties properties = new RaftProperties(); properties.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SMTransactionContext.class, StateMachine.class); RaftServerConfigKeys.Log.setUseMemory(properties, useMemory); try(MiniRaftClusterWithSimulatedRpc cluster = getFactory().newCluster(NUM_SERVERS, properties)) { cluster.start(); runTestTransactionContextIsPassedBack(cluster); } }
Example 9
Source File: ParameterizedBaseTest.java From ratis with Apache License 2.0 | 5 votes |
public static <S extends StateMachine> Collection<Object[]> getMiniRaftClusters( Class<S> stateMachineClass, int clusterSize, Class<?>... clusterClasses) throws IOException { final RaftProperties prop = new RaftProperties(); prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, stateMachineClass, StateMachine.class); return getMiniRaftClusters(prop, clusterSize, clusterClasses); }
Example 10
Source File: RaftSnapshotBaseTest.java From ratis with Apache License 2.0 | 5 votes |
@Before public void setup() throws IOException { final RaftProperties prop = new RaftProperties(); prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SimpleStateMachine4Testing.class, StateMachine.class); RaftServerConfigKeys.Snapshot.setAutoTriggerThreshold( prop, SNAPSHOT_TRIGGER_THRESHOLD); RaftServerConfigKeys.Snapshot.setAutoTriggerEnabled(prop, true); this.cluster = getFactory().newCluster(1, prop); cluster.start(); }
Example 11
Source File: WatchRequestTests.java From incubator-ratis with Apache License 2.0 | 4 votes |
@Before public void setup() { final RaftProperties p = getProperties(); p.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SimpleStateMachine4Testing.class, StateMachine.class); }
Example 12
Source File: WatchRequestTests.java From ratis with Apache License 2.0 | 4 votes |
@Before public void setup() { final RaftProperties p = getProperties(); p.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SimpleStateMachine4Testing.class, StateMachine.class); }
Example 13
Source File: StateMachineShutdownTests.java From ratis with Apache License 2.0 | 4 votes |
@Test public void testStateMachineShutdownWaitsForApplyTxn() throws Exception { final RaftProperties prop = getProperties(); prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, StateMachineWithConditionalWait.class, StateMachine.class); final MiniRaftCluster cluster = newCluster(3); cluster.start(); RaftTestUtil.waitForLeader(cluster); RaftServerImpl leader = cluster.getLeader(); RaftPeerId leaderId = leader.getId(); //Unblock leader and one follower ((StateMachineWithConditionalWait)leader.getStateMachine()) .unBlockApplyTxn(); ((StateMachineWithConditionalWait)cluster. getFollowers().get(0).getStateMachine()).unBlockApplyTxn(); cluster.getLeaderAndSendFirstMessage(true); final RaftClient client = cluster.createClient(leaderId); client.send(new RaftTestUtil.SimpleMessage("message")); RaftClientReply reply = client.send( new RaftTestUtil.SimpleMessage("message2")); long logIndex = reply.getLogIndex(); //Confirm that followers have committed RaftClientReply watchReply = client.sendWatch( logIndex, RaftProtos.ReplicationLevel.ALL_COMMITTED); watchReply.getCommitInfos().forEach( val -> Assert.assertTrue(val.getCommitIndex() >= logIndex)); RaftServerImpl secondFollower = cluster.getFollowers().get(1); // Second follower is blocked in apply transaction Assert.assertTrue( secondFollower.getState().getLastAppliedIndex() < logIndex); // Now shutdown the follower in a separate thread Thread t = new Thread(() -> secondFollower.shutdown(true)); t.start(); // The second follower should still be blocked in apply transaction Assert.assertTrue( secondFollower.getState().getLastAppliedIndex() < logIndex); // Now unblock the second follower ((StateMachineWithConditionalWait)secondFollower.getStateMachine()) .unBlockApplyTxn(); // Now wait for the thread t.join(5000); Assert.assertEquals( secondFollower.getState().getLastAppliedIndex(), logIndex); client.close(); cluster.shutdown(); }
Example 14
Source File: RaftStateMachineExceptionTests.java From ratis with Apache License 2.0 | 4 votes |
@Test public void testRetryOnExceptionDuringReplication() throws Exception { final RaftProperties prop = getProperties(); prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, StateMachineWithException.class, StateMachine.class); final MiniRaftCluster cluster = newCluster(3); cluster.start(); RaftTestUtil.waitForLeader(cluster); RaftServerImpl leader = cluster.getLeader(); RaftPeerId leaderId = leader.getId(); cluster.getLeaderAndSendFirstMessage(true); // turn on the preAppend failure switch failPreAppend = true; final RaftClient client = cluster.createClient(leaderId); final RaftClientRpc rpc = client.getClientRpc(); final long callId = 999; final long seqNum = 111; RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), leaderId, callId, seqNum, new RaftTestUtil.SimpleMessage("message")); RaftClientReply reply = rpc.sendRequest(r); Objects.requireNonNull(reply.getStateMachineException()); RetryCache.CacheEntry oldEntry = RaftServerTestUtil.getRetryEntry( leader, client.getId(), callId); Assert.assertNotNull(oldEntry); Assert.assertTrue(RaftServerTestUtil.isRetryCacheEntryFailed(oldEntry)); // At this point of time the old leader would have stepped down. wait for // leader election to complete RaftTestUtil.waitForLeader(cluster); leader = cluster.getLeader(); leaderId = leader.getId(); // retry r = cluster.newRaftClientRequest(client.getId(), leaderId, callId, seqNum, new RaftTestUtil.SimpleMessage("message")); reply = rpc.sendRequest(r); Objects.requireNonNull(reply.getStateMachineException()); RetryCache.CacheEntry currentEntry = RaftServerTestUtil.getRetryEntry( leader, client.getId(), callId); Assert.assertNotNull(currentEntry); Assert.assertTrue(RaftServerTestUtil.isRetryCacheEntryFailed(currentEntry)); Assert.assertNotEquals(oldEntry, currentEntry); failPreAppend = false; client.close(); }
Example 15
Source File: RaftStateMachineExceptionTests.java From ratis with Apache License 2.0 | 4 votes |
@Test public void testRetryOnStateMachineException() throws Exception { final RaftProperties prop = getProperties(); prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, StateMachineWithException.class, StateMachine.class); final MiniRaftCluster cluster = newCluster(3); cluster.start(); RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId(); cluster.getLeaderAndSendFirstMessage(true); long oldLastApplied = cluster.getLeader().getState().getLastAppliedIndex(); final RaftClient client = cluster.createClient(leaderId); final RaftClientRpc rpc = client.getClientRpc(); final long callId = 999; final long seqNum = 111; final SimpleMessage message = new SimpleMessage("message"); final RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), leaderId, callId, seqNum, message); RaftClientReply reply = rpc.sendRequest(r); Assert.assertFalse(reply.isSuccess()); Assert.assertNotNull(reply.getStateMachineException()); // retry with the same callId for (int i = 0; i < 5; i++) { reply = rpc.sendRequest(r); Assert.assertEquals(client.getId(), reply.getClientId()); Assert.assertEquals(callId, reply.getCallId()); Assert.assertFalse(reply.isSuccess()); Assert.assertNotNull(reply.getStateMachineException()); } 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.assertNotNull( RaftServerTestUtil.getRetryEntry(server, client.getId(), callId)); final RaftLog log = server.getState().getLog(); RaftTestUtil.logEntriesContains(log, oldLastApplied + 1, log.getNextIndex(), message); } client.close(); cluster.shutdown(); }
Example 16
Source File: TestCacheEviction.java From ratis with Apache License 2.0 | 4 votes |
@Test public void testEvictionInSegmentedLog() throws Exception { final RaftProperties prop = new RaftProperties(); prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SimpleStateMachine4Testing.class, StateMachine.class); RaftServerConfigKeys.Log.setSegmentSizeMax(prop, SizeInBytes.valueOf("8KB")); RaftServerConfigKeys.Log.setPreallocatedSize(prop, SizeInBytes.valueOf("8KB")); final RaftPeerId peerId = RaftPeerId.valueOf("s0"); final int maxCachedNum = RaftServerConfigKeys.Log.maxCachedSegmentNum(prop); File storageDir = getTestDir(); RaftServerConfigKeys.setStorageDirs(prop, Collections.singletonList(storageDir)); RaftStorage storage = new RaftStorage(storageDir, RaftServerConstants.StartupOption.REGULAR); RaftServerImpl server = Mockito.mock(RaftServerImpl.class); ServerState state = Mockito.mock(ServerState.class); Mockito.when(server.getState()).thenReturn(state); Mockito.when(server.getFollowerNextIndices()).thenReturn(new long[]{}); Mockito.when(state.getLastAppliedIndex()).thenReturn(0L); SegmentedRaftLog raftLog = new SegmentedRaftLog(peerId, server, storage, -1, prop); raftLog.open(RaftServerConstants.INVALID_LOG_INDEX, null); List<SegmentRange> slist = TestSegmentedRaftLog.prepareRanges(0, maxCachedNum, 7, 0); LogEntryProto[] entries = generateEntries(slist); raftLog.append(entries).forEach(CompletableFuture::join); // check the current cached segment number: the last segment is still open Assert.assertEquals(maxCachedNum - 1, raftLog.getRaftLogCache().getCachedSegmentNum()); Mockito.when(server.getFollowerNextIndices()).thenReturn(new long[]{21, 40, 40}); Mockito.when(state.getLastAppliedIndex()).thenReturn(35L); slist = TestSegmentedRaftLog.prepareRanges(maxCachedNum, maxCachedNum + 2, 7, 7 * maxCachedNum); entries = generateEntries(slist); raftLog.append(entries).forEach(CompletableFuture::join); // check the cached segment number again. since the slowest follower is on // index 21, the eviction should happen and evict 3 segments Assert.assertEquals(maxCachedNum + 1 - 3, raftLog.getRaftLogCache().getCachedSegmentNum()); }
Example 17
Source File: StateMachineShutdownTests.java From incubator-ratis with Apache License 2.0 | 4 votes |
@Test public void testStateMachineShutdownWaitsForApplyTxn() throws Exception { final RaftProperties prop = getProperties(); prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, StateMachineWithConditionalWait.class, StateMachine.class); final MiniRaftCluster cluster = newCluster(3); cluster.start(); RaftTestUtil.waitForLeader(cluster); RaftServerImpl leader = cluster.getLeader(); RaftPeerId leaderId = leader.getId(); //Unblock leader and one follower ((StateMachineWithConditionalWait)leader.getStateMachine()) .unBlockApplyTxn(); ((StateMachineWithConditionalWait)cluster. getFollowers().get(0).getStateMachine()).unBlockApplyTxn(); cluster.getLeaderAndSendFirstMessage(true); try (final RaftClient client = cluster.createClient(leaderId)) { client.send(new RaftTestUtil.SimpleMessage("message")); RaftClientReply reply = client.send( new RaftTestUtil.SimpleMessage("message2")); long logIndex = reply.getLogIndex(); //Confirm that followers have committed RaftClientReply watchReply = client.sendWatch( logIndex, RaftProtos.ReplicationLevel.ALL_COMMITTED); watchReply.getCommitInfos().forEach( val -> Assert.assertTrue(val.getCommitIndex() >= logIndex)); RaftServerImpl secondFollower = cluster.getFollowers().get(1); // Second follower is blocked in apply transaction Assert.assertTrue( secondFollower.getState().getLastAppliedIndex() < logIndex); // Now shutdown the follower in a separate thread Thread t = new Thread(() -> secondFollower.shutdown(true)); t.start(); // The second follower should still be blocked in apply transaction Assert.assertTrue( secondFollower.getState().getLastAppliedIndex() < logIndex); // Now unblock the second follower ((StateMachineWithConditionalWait) secondFollower.getStateMachine()) .unBlockApplyTxn(); // Now wait for the thread t.join(5000); Assert.assertEquals( secondFollower.getState().getLastAppliedIndex(), logIndex); cluster.shutdown(); } }
Example 18
Source File: MiniRaftCluster.java From incubator-ratis with Apache License 2.0 | 4 votes |
default RaftProperties setStateMachine(Class<? extends StateMachine> stateMachineClass) { final RaftProperties p = getProperties(); p.setClass(STATEMACHINE_CLASS_KEY, stateMachineClass, StateMachine.class); return p; }
Example 19
Source File: TestRaftServerWithGrpc.java From incubator-ratis with Apache License 2.0 | 4 votes |
@Before public void setup() { final RaftProperties p = getProperties(); p.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SimpleStateMachine4Testing.class, StateMachine.class); RaftClientConfigKeys.Rpc.setRequestTimeout(p, TimeDuration.valueOf(1, TimeUnit.SECONDS)); }
Example 20
Source File: TestCacheEviction.java From incubator-ratis with Apache License 2.0 | 4 votes |
@Test public void testEvictionInSegmentedLog() throws Exception { final RaftProperties prop = new RaftProperties(); prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SimpleStateMachine4Testing.class, StateMachine.class); RaftServerConfigKeys.Log.setSegmentSizeMax(prop, SizeInBytes.valueOf("8KB")); RaftServerConfigKeys.Log.setPreallocatedSize(prop, SizeInBytes.valueOf("8KB")); final RaftPeerId peerId = RaftPeerId.valueOf("s0"); final RaftGroupId groupId = RaftGroupId.randomId(); final RaftGroupMemberId memberId = RaftGroupMemberId.valueOf(peerId, groupId); final int maxCachedNum = RaftServerConfigKeys.Log.segmentCacheNumMax(prop); File storageDir = getTestDir(); RaftServerConfigKeys.setStorageDir(prop, Collections.singletonList(storageDir)); RaftStorage storage = new RaftStorage(storageDir, RaftServerConstants.StartupOption.REGULAR); RaftServerImpl server = Mockito.mock(RaftServerImpl.class); ServerState state = Mockito.mock(ServerState.class); Mockito.when(server.getState()).thenReturn(state); Mockito.when(server.getFollowerNextIndices()).thenReturn(new long[]{}); Mockito.when(state.getLastAppliedIndex()).thenReturn(0L); SegmentedRaftLog raftLog = new SegmentedRaftLog(memberId, server, storage, -1, prop); raftLog.open(RaftLog.INVALID_LOG_INDEX, null); List<SegmentRange> slist = TestSegmentedRaftLog.prepareRanges(0, maxCachedNum, 7, 0); LogEntryProto[] entries = generateEntries(slist); raftLog.append(entries).forEach(CompletableFuture::join); // check the current cached segment number: the last segment is still open Assert.assertEquals(maxCachedNum - 1, raftLog.getRaftLogCache().getCachedSegmentNum()); Mockito.when(server.getFollowerNextIndices()).thenReturn(new long[]{21, 40, 40}); Mockito.when(state.getLastAppliedIndex()).thenReturn(35L); slist = TestSegmentedRaftLog.prepareRanges(maxCachedNum, maxCachedNum + 2, 7, 7 * maxCachedNum); entries = generateEntries(slist); raftLog.append(entries).forEach(CompletableFuture::join); // check the cached segment number again. since the slowest follower is on // index 21, the eviction should happen and evict 3 segments Assert.assertEquals(maxCachedNum + 1 - 3, raftLog.getRaftLogCache().getCachedSegmentNum()); }