4949import org .apache .ratis .util .JavaUtils ;
5050import org .apache .ratis .util .LifeCycle ;
5151import org .apache .ratis .util .Slf4jUtils ;
52- import org .junit .jupiter .api .AfterEach ;
5352import org .junit .jupiter .api .Assertions ;
54- import org .junit .jupiter .api .BeforeEach ;
5553import org .junit .jupiter .api .Test ;
5654import org .slf4j .Logger ;
5755import org .slf4j .LoggerFactory ;
5856
5957import java .io .File ;
60- import java .io .IOException ;
6158import java .util .Arrays ;
6259import java .util .List ;
6360import java .util .Optional ;
6764import org .apache .ratis .thirdparty .com .codahale .metrics .Timer ;
6865import org .slf4j .event .Level ;
6966
70- public abstract class RaftSnapshotBaseTest extends BaseTest {
67+ public abstract class RaftSnapshotBaseTest <CLUSTER extends MiniRaftCluster >
68+ extends BaseTest
69+ implements MiniRaftCluster .Factory .Get <CLUSTER > {
7170 {
7271 Slf4jUtils .setLogLevel (RaftServer .Division .LOG , Level .DEBUG );
7372 Slf4jUtils .setLogLevel (RaftLog .LOG , Level .DEBUG );
74- Slf4jUtils .setLogLevel (RaftClient .LOG , Level .DEBUG );
73+
74+ final RaftProperties p = getProperties ();
75+ p .setClass (MiniRaftCluster .STATEMACHINE_CLASS_KEY , SimpleStateMachine4Testing .class , StateMachine .class );
76+ RaftServerConfigKeys .Snapshot .setAutoTriggerThreshold (p , SNAPSHOT_TRIGGER_THRESHOLD );
77+ RaftServerConfigKeys .Snapshot .setAutoTriggerEnabled (p , true );
78+ RaftServerConfigKeys .LeaderElection .setMemberMajorityAdd (p , true );
7579 }
7680
7781 static final Logger LOG = LoggerFactory .getLogger (RaftSnapshotBaseTest .class );
@@ -119,38 +123,20 @@ public static void assertLogContent(RaftServer.Division server, boolean isLeader
119123 }
120124 }
121125
122- private MiniRaftCluster cluster ;
123-
124- public abstract MiniRaftCluster .Factory <?> getFactory ();
125-
126- @ BeforeEach
127- public void setup () throws IOException {
128- final RaftProperties prop = new RaftProperties ();
129- prop .setClass (MiniRaftCluster .STATEMACHINE_CLASS_KEY ,
130- SimpleStateMachine4Testing .class , StateMachine .class );
131- RaftServerConfigKeys .Snapshot .setAutoTriggerThreshold (
132- prop , SNAPSHOT_TRIGGER_THRESHOLD );
133- RaftServerConfigKeys .Snapshot .setAutoTriggerEnabled (prop , true );
134- this .cluster = getFactory ().newCluster (1 , prop );
135- cluster .start ();
136- }
137-
138- @ AfterEach
139- public void tearDown () {
140- if (cluster != null ) {
141- cluster .shutdown ();
142- }
143- }
144-
145126 /**
146127 * Keep generating writing traffic and make sure snapshots are taken.
147128 * We then restart the whole raft peer and check if it can correctly load
148129 * snapshots + raft log.
149130 */
150131 @ Test
151132 public void testRestartPeer () throws Exception {
152- RaftTestUtil .waitForLeader (cluster );
153- final RaftPeerId leaderId = cluster .getLeader ().getId ();
133+ runWithNewCluster (1 , this ::runTestRestartPeer );
134+
135+ }
136+
137+ void runTestRestartPeer (CLUSTER cluster ) throws Exception {
138+ LOG .info ("runTestRestartPeer" );
139+ final RaftPeerId leaderId = RaftTestUtil .waitForLeader (cluster ).getId ();
154140 int i = 0 ;
155141 try (final RaftClient client = cluster .createClient (leaderId )) {
156142 for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1 ; i ++) {
@@ -180,7 +166,7 @@ public void testRestartPeer() throws Exception {
180166
181167 public static boolean exists (File f ) {
182168 if (f .exists ()) {
183- LOG .info ("File exists: " + f );
169+ LOG .info ("File exists: {}" , f );
184170 return true ;
185171 }
186172 return false ;
@@ -193,11 +179,15 @@ public static boolean exists(File f) {
193179 */
194180 @ Test
195181 public void testBasicInstallSnapshot () throws Exception {
182+ runWithNewCluster (1 , this ::runTestBasicInstallSnapshot );
183+ }
184+
185+ void runTestBasicInstallSnapshot (CLUSTER cluster ) throws Exception {
186+ LOG .info ("runTestBasicInstallSnapshot" );
196187 final List <LogSegmentPath > logs ;
197188 int i = 0 ;
198189 try {
199- RaftTestUtil .waitForLeader (cluster );
200- final RaftPeerId leaderId = cluster .getLeader ().getId ();
190+ final RaftPeerId leaderId = RaftTestUtil .waitForLeader (cluster ).getId ();
201191
202192 try (final RaftClient client = cluster .createClient (leaderId )) {
203193 for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1 ; i ++) {
@@ -236,16 +226,14 @@ public void testBasicInstallSnapshot() throws Exception {
236226 Assertions .assertTrue (client .io ().send (new SimpleMessage ("m" + i )).isSuccess ());
237227 }
238228
239- // add two more peers
240- String [] newPeers = new String []{"s3" , "s4" };
241- MiniRaftCluster .PeerChanges change = cluster .addNewPeers (
242- newPeers , true , false );
229+ // add a new peer
230+ final MiniRaftCluster .PeerChanges change = cluster .addNewPeers (1 , true , true );
243231 // trigger setConfiguration
244232 RaftServerTestUtil .runWithMinorityPeers (cluster , Arrays .asList (change .allPeersInNewConf ),
245233 peers -> cluster .setConfiguration (peers .toArray (RaftPeer .emptyArray ())));
246234
247- for (String newPeer : newPeers ) {
248- final RaftServer .Division s = cluster .getDivision (RaftPeerId . valueOf ( newPeer ));
235+ for (RaftPeer newPeer : change . newPeers ) {
236+ final RaftServer .Division s = cluster .getDivision (newPeer . getId ( ));
249237 SimpleStateMachine4Testing simpleStateMachine = SimpleStateMachine4Testing .get (s );
250238 Assertions .assertSame (LifeCycle .State .RUNNING , simpleStateMachine .getLifeCycleState ());
251239 }
@@ -275,6 +263,11 @@ public void testBasicInstallSnapshot() throws Exception {
275263 */
276264 @ Test
277265 public void testInstallSnapshotDuringBootstrap () throws Exception {
266+ runWithNewCluster (1 , this ::runTestInstallSnapshotDuringBootstrap );
267+ }
268+
269+ void runTestInstallSnapshotDuringBootstrap (CLUSTER cluster ) throws Exception {
270+ LOG .info ("runTestInstallSnapshotDuringBootstrap" );
278271 int i = 0 ;
279272 try {
280273 RaftTestUtil .waitForLeader (cluster );
@@ -299,16 +292,14 @@ public void testInstallSnapshotDuringBootstrap() throws Exception {
299292
300293 assertLeaderContent (cluster );
301294
302- // add two more peers
303- String [] newPeers = new String []{"s3" , "s4" };
304- MiniRaftCluster .PeerChanges change = cluster .addNewPeers (
305- newPeers , true , false );
295+ // add a new peer
296+ final MiniRaftCluster .PeerChanges change = cluster .addNewPeers (1 , true , true );
306297 // trigger setConfiguration
307298 RaftServerTestUtil .runWithMinorityPeers (cluster , Arrays .asList (change .allPeersInNewConf ),
308299 peers -> cluster .setConfiguration (peers .toArray (RaftPeer .emptyArray ())));
309300
310- for (String newPeer : newPeers ) {
311- final RaftServer .Division s = cluster .getDivision (RaftPeerId . valueOf ( newPeer ));
301+ for (RaftPeer newPeer : change . newPeers ) {
302+ final RaftServer .Division s = cluster .getDivision (newPeer . getId ( ));
312303 SimpleStateMachine4Testing simpleStateMachine = SimpleStateMachine4Testing .get (s );
313304 Assertions .assertSame (LifeCycle .State .RUNNING , simpleStateMachine .getLifeCycleState ());
314305 }
0 commit comments