3133c5bc45f96e71b1291d3bb5a6d496f5c8432d
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / SnapshotManagerTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.cluster.raft;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertTrue;
15 import static org.mockito.ArgumentMatchers.any;
16 import static org.mockito.ArgumentMatchers.anyLong;
17 import static org.mockito.Mockito.doNothing;
18 import static org.mockito.Mockito.doReturn;
19 import static org.mockito.Mockito.doThrow;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.never;
22 import static org.mockito.Mockito.reset;
23 import static org.mockito.Mockito.times;
24 import static org.mockito.Mockito.verify;
25
26 import akka.actor.ActorRef;
27 import akka.persistence.SnapshotSelectionCriteria;
28 import java.io.OutputStream;
29 import java.util.Arrays;
30 import java.util.Optional;
31 import java.util.function.Consumer;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.ArgumentCaptor;
37 import org.mockito.Mock;
38 import org.mockito.junit.MockitoJUnitRunner;
39 import org.opendaylight.controller.cluster.DataPersistenceProvider;
40 import org.opendaylight.controller.cluster.io.FileBackedOutputStreamFactory;
41 import org.opendaylight.controller.cluster.raft.SnapshotManager.LastAppliedTermInformationReader;
42 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshot;
43 import org.opendaylight.controller.cluster.raft.base.messages.SendInstallSnapshot;
44 import org.opendaylight.controller.cluster.raft.base.messages.SnapshotComplete;
45 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
46 import org.opendaylight.controller.cluster.raft.persisted.ByteState;
47 import org.opendaylight.controller.cluster.raft.persisted.SimpleReplicatedLogEntry;
48 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
49 import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
50 import org.slf4j.LoggerFactory;
51
52 @RunWith(MockitoJUnitRunner.StrictStubs.class)
53 public class SnapshotManagerTest extends AbstractActorTest {
54
55     @Mock
56     private RaftActorContext mockRaftActorContext;
57
58     @Mock
59     private ConfigParams mockConfigParams;
60
61     @Mock
62     private ReplicatedLog mockReplicatedLog;
63
64     @Mock
65     private DataPersistenceProvider mockDataPersistenceProvider;
66
67     @Mock
68     private RaftActorBehavior mockRaftActorBehavior;
69
70     @Mock
71     private Consumer<Optional<OutputStream>> mockProcedure;
72
73     @Mock
74     private ElectionTerm mockElectionTerm;
75
76     private SnapshotManager snapshotManager;
77
78     private TestActorFactory factory;
79
80     private ActorRef actorRef;
81
82     @Before
83     public void setUp() {
84         doReturn(false).when(mockRaftActorContext).hasFollowers();
85         doReturn(mockConfigParams).when(mockRaftActorContext).getConfigParams();
86         doReturn(10L).when(mockConfigParams).getSnapshotBatchCount();
87         doReturn(70).when(mockConfigParams).getSnapshotDataThresholdPercentage();
88         doReturn(mockReplicatedLog).when(mockRaftActorContext).getReplicatedLog();
89         doReturn("123").when(mockRaftActorContext).getId();
90         doReturn(mockDataPersistenceProvider).when(mockRaftActorContext).getPersistenceProvider();
91         doReturn(mockRaftActorBehavior).when(mockRaftActorContext).getCurrentBehavior();
92         doReturn("123").when(mockRaftActorBehavior).getLeaderId();
93
94         doReturn(mockElectionTerm).when(mockRaftActorContext).getTermInformation();
95         doReturn(5L).when(mockElectionTerm).getCurrentTerm();
96         doReturn("member5").when(mockElectionTerm).getVotedFor();
97
98         doReturn(new FileBackedOutputStreamFactory(10000000, "target"))
99                 .when(mockRaftActorContext).getFileBackedOutputStreamFactory();
100
101         snapshotManager = new SnapshotManager(mockRaftActorContext, LoggerFactory.getLogger(this.getClass()));
102         factory = new TestActorFactory(getSystem());
103
104         actorRef = factory.createActor(MessageCollectorActor.props(), factory.generateActorId("test-"));
105         doReturn(actorRef).when(mockRaftActorContext).getActor();
106
107         snapshotManager.setCreateSnapshotConsumer(mockProcedure);
108     }
109
110     @After
111     public void tearDown() {
112         factory.close();
113     }
114
115     @Test
116     public void testConstruction() {
117         assertEquals(false, snapshotManager.isCapturing());
118     }
119
120     @SuppressWarnings({ "unchecked", "rawtypes" })
121     @Test
122     public void testCaptureToInstall() {
123
124         // Force capturing toInstall = true
125         snapshotManager.captureToInstall(new SimpleReplicatedLogEntry(0, 1,
126                 new MockRaftActorContext.MockPayload()), 0, "follower-1");
127
128         assertEquals(true, snapshotManager.isCapturing());
129
130         ArgumentCaptor<Optional> outputStream = ArgumentCaptor.forClass(Optional.class);
131         verify(mockProcedure).accept(outputStream.capture());
132         assertEquals("isPresent", true, outputStream.getValue().isPresent());
133
134         CaptureSnapshot captureSnapshot = snapshotManager.getCaptureSnapshot();
135
136         // LastIndex and LastTerm are picked up from the lastLogEntry
137         assertEquals(0L, captureSnapshot.getLastIndex());
138         assertEquals(1L, captureSnapshot.getLastTerm());
139
140         // Since the actor does not have any followers (no peer addresses) lastApplied will be from lastLogEntry
141         assertEquals(0L, captureSnapshot.getLastAppliedIndex());
142         assertEquals(1L, captureSnapshot.getLastAppliedTerm());
143
144         //
145         assertEquals(-1L, captureSnapshot.getReplicatedToAllIndex());
146         assertEquals(-1L, captureSnapshot.getReplicatedToAllTerm());
147         MessageCollectorActor.clearMessages(actorRef);
148     }
149
150     @SuppressWarnings({ "rawtypes", "unchecked" })
151     @Test
152     public void testCapture() {
153         boolean capture = snapshotManager.capture(new SimpleReplicatedLogEntry(9, 1,
154                 new MockRaftActorContext.MockPayload()), 9);
155
156         assertTrue(capture);
157
158         assertEquals(true, snapshotManager.isCapturing());
159
160         ArgumentCaptor<Optional> outputStream = ArgumentCaptor.forClass(Optional.class);
161         verify(mockProcedure).accept(outputStream.capture());
162         assertEquals("isPresent", false, outputStream.getValue().isPresent());
163
164         CaptureSnapshot captureSnapshot = snapshotManager.getCaptureSnapshot();
165
166         // LastIndex and LastTerm are picked up from the lastLogEntry
167         assertEquals(9L, captureSnapshot.getLastIndex());
168         assertEquals(1L, captureSnapshot.getLastTerm());
169
170         // Since the actor does not have any followers (no peer addresses) lastApplied will be from lastLogEntry
171         assertEquals(9L, captureSnapshot.getLastAppliedIndex());
172         assertEquals(1L, captureSnapshot.getLastAppliedTerm());
173
174         //
175         assertEquals(-1L, captureSnapshot.getReplicatedToAllIndex());
176         assertEquals(-1L, captureSnapshot.getReplicatedToAllTerm());
177
178         MessageCollectorActor.clearMessages(actorRef);
179     }
180
181     @SuppressWarnings({ "unchecked", "rawtypes" })
182     @Test
183     public void testCaptureWithNullLastLogEntry() {
184         boolean capture = snapshotManager.capture(null, 1);
185
186         assertTrue(capture);
187
188         assertEquals(true, snapshotManager.isCapturing());
189
190         ArgumentCaptor<Optional> outputStream = ArgumentCaptor.forClass(Optional.class);
191         verify(mockProcedure).accept(outputStream.capture());
192         assertEquals("isPresent", false, outputStream.getValue().isPresent());
193
194         CaptureSnapshot captureSnapshot = snapshotManager.getCaptureSnapshot();
195
196         // LastIndex and LastTerm are picked up from the lastLogEntry
197         assertEquals(0, captureSnapshot.getLastIndex());
198         assertEquals(0, captureSnapshot.getLastTerm());
199
200         // Since the actor does not have any followers (no peer addresses) lastApplied will be from lastLogEntry
201         assertEquals(0, captureSnapshot.getLastAppliedIndex());
202         assertEquals(0, captureSnapshot.getLastAppliedTerm());
203
204         //
205         assertEquals(-1L, captureSnapshot.getReplicatedToAllIndex());
206         assertEquals(-1L, captureSnapshot.getReplicatedToAllTerm());
207         MessageCollectorActor.clearMessages(actorRef);
208     }
209
210     @Test
211     public void testCaptureWithCreateProcedureError() {
212         doThrow(new RuntimeException("mock")).when(mockProcedure).accept(any());
213
214         boolean capture = snapshotManager.capture(new SimpleReplicatedLogEntry(9, 1,
215                 new MockRaftActorContext.MockPayload()), 9);
216
217         assertFalse(capture);
218
219         assertEquals(false, snapshotManager.isCapturing());
220
221         verify(mockProcedure).accept(any());
222     }
223
224     @SuppressWarnings("unchecked")
225     @Test
226     public void testIllegalCapture() {
227         boolean capture = snapshotManager.capture(new SimpleReplicatedLogEntry(9, 1,
228                 new MockRaftActorContext.MockPayload()), 9);
229
230         assertTrue(capture);
231
232         verify(mockProcedure).accept(any());
233
234         reset(mockProcedure);
235
236         // This will not cause snapshot capture to start again
237         capture = snapshotManager.capture(new SimpleReplicatedLogEntry(9, 1,
238                 new MockRaftActorContext.MockPayload()), 9);
239
240         assertFalse(capture);
241
242         verify(mockProcedure, never()).accept(any());
243     }
244
245     @Test
246     public void testPersistWhenReplicatedToAllIndexMinusOne() {
247         doReturn(7L).when(mockReplicatedLog).getSnapshotIndex();
248         doReturn(1L).when(mockReplicatedLog).getSnapshotTerm();
249
250         doReturn(true).when(mockRaftActorContext).hasFollowers();
251
252         doReturn(8L).when(mockRaftActorContext).getLastApplied();
253
254         ReplicatedLogEntry lastLogEntry = new SimpleReplicatedLogEntry(9L, 3L, new MockRaftActorContext.MockPayload());
255
256         ReplicatedLogEntry lastAppliedEntry = new SimpleReplicatedLogEntry(
257                 8L, 2L, new MockRaftActorContext.MockPayload());
258
259         doReturn(lastAppliedEntry).when(mockReplicatedLog).get(8L);
260         doReturn(Arrays.asList(lastLogEntry)).when(mockReplicatedLog).getFrom(9L);
261
262         // when replicatedToAllIndex = -1
263         snapshotManager.capture(lastLogEntry, -1);
264
265         ByteState snapshotState = ByteState.of(new byte[] {1,2,3,4,5,6,7,8,9,10});
266         snapshotManager.persist(snapshotState, Optional.empty(), Runtime.getRuntime().totalMemory());
267
268         ArgumentCaptor<Snapshot> snapshotArgumentCaptor = ArgumentCaptor.forClass(Snapshot.class);
269         verify(mockDataPersistenceProvider).saveSnapshot(snapshotArgumentCaptor.capture());
270
271         Snapshot snapshot = snapshotArgumentCaptor.getValue();
272
273         assertEquals("getLastTerm", 3L, snapshot.getLastTerm());
274         assertEquals("getLastIndex", 9L, snapshot.getLastIndex());
275         assertEquals("getLastAppliedTerm", 2L, snapshot.getLastAppliedTerm());
276         assertEquals("getLastAppliedIndex", 8L, snapshot.getLastAppliedIndex());
277         assertEquals("getState", snapshotState, snapshot.getState());
278         assertEquals("getUnAppliedEntries", Arrays.asList(lastLogEntry), snapshot.getUnAppliedEntries());
279         assertEquals("electionTerm", mockElectionTerm.getCurrentTerm(), snapshot.getElectionTerm());
280         assertEquals("electionVotedFor", mockElectionTerm.getVotedFor(), snapshot.getElectionVotedFor());
281
282         verify(mockReplicatedLog).snapshotPreCommit(7L, 1L);
283     }
284
285     @Test
286     public void testPersistWhenReplicatedToAllIndexNotMinus() {
287         doReturn(45L).when(mockReplicatedLog).getSnapshotIndex();
288         doReturn(6L).when(mockReplicatedLog).getSnapshotTerm();
289         ReplicatedLogEntry replicatedLogEntry = mock(ReplicatedLogEntry.class);
290         doReturn(null).when(mockReplicatedLog).get(0);
291         doReturn(replicatedLogEntry).when(mockReplicatedLog).get(9);
292         doReturn(6L).when(replicatedLogEntry).getTerm();
293         doReturn(9L).when(replicatedLogEntry).getIndex();
294
295         // when replicatedToAllIndex != -1
296         snapshotManager.capture(new SimpleReplicatedLogEntry(9, 6, new MockRaftActorContext.MockPayload()), 9);
297
298         ByteState snapshotState = ByteState.of(new byte[] {1,2,3,4,5,6,7,8,9,10});
299         snapshotManager.persist(snapshotState, Optional.empty(), Runtime.getRuntime().totalMemory());
300
301         ArgumentCaptor<Snapshot> snapshotArgumentCaptor = ArgumentCaptor.forClass(Snapshot.class);
302         verify(mockDataPersistenceProvider).saveSnapshot(snapshotArgumentCaptor.capture());
303
304         Snapshot snapshot = snapshotArgumentCaptor.getValue();
305
306         assertEquals("getLastTerm", 6L, snapshot.getLastTerm());
307         assertEquals("getLastIndex", 9L, snapshot.getLastIndex());
308         assertEquals("getLastAppliedTerm", 6L, snapshot.getLastAppliedTerm());
309         assertEquals("getLastAppliedIndex", 9L, snapshot.getLastAppliedIndex());
310         assertEquals("getState", snapshotState, snapshot.getState());
311         assertEquals("getUnAppliedEntries size", 0, snapshot.getUnAppliedEntries().size());
312
313         verify(mockReplicatedLog).snapshotPreCommit(9L, 6L);
314
315         verify(mockRaftActorBehavior).setReplicatedToAllIndex(9);
316     }
317
318     @Test
319     public void testPersistWhenReplicatedLogDataSizeGreaterThanThreshold() {
320         doReturn(Integer.MAX_VALUE).when(mockReplicatedLog).dataSize();
321
322         // when replicatedToAllIndex = -1
323         snapshotManager.capture(new SimpleReplicatedLogEntry(9, 6, new MockRaftActorContext.MockPayload()), -1);
324
325         snapshotManager.persist(ByteState.empty(), Optional.empty(), Runtime.getRuntime().totalMemory());
326
327         verify(mockDataPersistenceProvider).saveSnapshot(any(Snapshot.class));
328
329         verify(mockReplicatedLog).snapshotPreCommit(9L, 6L);
330
331         verify(mockRaftActorBehavior, never()).setReplicatedToAllIndex(anyLong());
332     }
333
334     @Test
335     public void testPersistWhenReplicatedLogSizeExceedsSnapshotBatchCount() {
336         doReturn(10L).when(mockReplicatedLog).size(); // matches snapshotBatchCount
337         doReturn(100).when(mockReplicatedLog).dataSize();
338
339         doReturn(5L).when(mockReplicatedLog).getSnapshotIndex();
340         doReturn(5L).when(mockReplicatedLog).getSnapshotTerm();
341
342         long replicatedToAllIndex = 1;
343         ReplicatedLogEntry replicatedLogEntry = mock(ReplicatedLogEntry.class);
344         doReturn(null).when(mockReplicatedLog).get(0);
345         doReturn(replicatedLogEntry).when(mockReplicatedLog).get(replicatedToAllIndex);
346         doReturn(6L).when(replicatedLogEntry).getTerm();
347         doReturn(replicatedToAllIndex).when(replicatedLogEntry).getIndex();
348
349         snapshotManager.capture(new SimpleReplicatedLogEntry(9, 6,
350                 new MockRaftActorContext.MockPayload()), replicatedToAllIndex);
351
352         snapshotManager.persist(ByteState.empty(), Optional.empty(), 2000000L);
353
354         verify(mockDataPersistenceProvider).saveSnapshot(any(Snapshot.class));
355
356         verify(mockReplicatedLog).snapshotPreCommit(9L, 6L);
357
358         verify(mockRaftActorBehavior).setReplicatedToAllIndex(replicatedToAllIndex);
359     }
360
361     @SuppressWarnings({ "rawtypes", "unchecked" })
362     @Test
363     public void testPersistSendInstallSnapshot() throws Exception {
364         doReturn(Integer.MAX_VALUE).when(mockReplicatedLog).dataSize();
365         doNothing().when(mockProcedure).accept(any());
366
367         // when replicatedToAllIndex = -1
368         boolean capture = snapshotManager.captureToInstall(new SimpleReplicatedLogEntry(9, 6,
369                 new MockRaftActorContext.MockPayload()), -1, "follower-1");
370
371         assertTrue(capture);
372
373         ByteState snapshotState = ByteState.of(new byte[] {1,2,3,4,5,6,7,8,9,10});
374
375         ArgumentCaptor<Optional> installSnapshotStreamCapture = ArgumentCaptor.forClass(Optional.class);
376         verify(mockProcedure).accept(installSnapshotStreamCapture.capture());
377
378         Optional<OutputStream> installSnapshotStream = installSnapshotStreamCapture.getValue();
379         assertEquals("isPresent", true, installSnapshotStream.isPresent());
380
381         installSnapshotStream.get().write(snapshotState.getBytes());
382
383         snapshotManager.persist(snapshotState, installSnapshotStream, Runtime.getRuntime().totalMemory());
384
385         assertEquals(true, snapshotManager.isCapturing());
386
387         verify(mockDataPersistenceProvider).saveSnapshot(any(Snapshot.class));
388
389         verify(mockReplicatedLog).snapshotPreCommit(9L, 6L);
390
391         ArgumentCaptor<SendInstallSnapshot> sendInstallSnapshotArgumentCaptor
392                 = ArgumentCaptor.forClass(SendInstallSnapshot.class);
393
394         verify(mockRaftActorBehavior).handleMessage(any(ActorRef.class), sendInstallSnapshotArgumentCaptor.capture());
395
396         SendInstallSnapshot sendInstallSnapshot = sendInstallSnapshotArgumentCaptor.getValue();
397
398         assertEquals("state", snapshotState, sendInstallSnapshot.getSnapshot().getState());
399         assertArrayEquals("state", snapshotState.getBytes(), sendInstallSnapshot.getSnapshotBytes().read());
400     }
401
402     @Test
403     public void testCallingPersistWithoutCaptureWillDoNothing() {
404         snapshotManager.persist(ByteState.empty(), Optional.empty(), Runtime.getRuntime().totalMemory());
405
406         verify(mockDataPersistenceProvider, never()).saveSnapshot(any(Snapshot.class));
407
408         verify(mockReplicatedLog, never()).snapshotPreCommit(9L, 6L);
409
410         verify(mockRaftActorBehavior, never()).handleMessage(any(ActorRef.class), any(SendInstallSnapshot.class));
411     }
412
413     @Test
414     public void testCallingPersistTwiceWillDoNoHarm() {
415         doReturn(Integer.MAX_VALUE).when(mockReplicatedLog).dataSize();
416
417         // when replicatedToAllIndex = -1
418         snapshotManager.capture(new SimpleReplicatedLogEntry(9, 6, new MockRaftActorContext.MockPayload()), -1);
419
420         snapshotManager.persist(ByteState.empty(), Optional.empty(), Runtime.getRuntime().totalMemory());
421
422         snapshotManager.persist(ByteState.empty(), Optional.empty(), Runtime.getRuntime().totalMemory());
423
424         verify(mockDataPersistenceProvider).saveSnapshot(any(Snapshot.class));
425
426         verify(mockReplicatedLog).snapshotPreCommit(9L, 6L);
427     }
428
429     @Test
430     public void testCommit() {
431         doReturn(50L).when(mockDataPersistenceProvider).getLastSequenceNumber();
432
433         // when replicatedToAllIndex = -1
434         snapshotManager.capture(new SimpleReplicatedLogEntry(9, 6, new MockRaftActorContext.MockPayload()), -1);
435
436         snapshotManager.persist(ByteState.empty(), Optional.empty(), Runtime.getRuntime().totalMemory());
437
438         assertEquals(true, snapshotManager.isCapturing());
439
440         snapshotManager.commit(100L, 1234L);
441
442         assertEquals(false, snapshotManager.isCapturing());
443
444         verify(mockReplicatedLog).snapshotCommit();
445
446         verify(mockDataPersistenceProvider).deleteMessages(50L);
447
448         ArgumentCaptor<SnapshotSelectionCriteria> criteriaCaptor =
449                 ArgumentCaptor.forClass(SnapshotSelectionCriteria.class);
450
451         verify(mockDataPersistenceProvider).deleteSnapshots(criteriaCaptor.capture());
452
453         assertEquals(Long.MAX_VALUE, criteriaCaptor.getValue().maxSequenceNr());
454         assertEquals(1233L, criteriaCaptor.getValue().maxTimestamp());
455
456         MessageCollectorActor.expectFirstMatching(actorRef, SnapshotComplete.class);
457     }
458
459     @Test
460     public void testCommitBeforePersist() {
461         // when replicatedToAllIndex = -1
462         snapshotManager.capture(new SimpleReplicatedLogEntry(9, 6, new MockRaftActorContext.MockPayload()), -1);
463
464         snapshotManager.commit(100L, 0);
465
466         verify(mockReplicatedLog, never()).snapshotCommit();
467
468         verify(mockDataPersistenceProvider, never()).deleteMessages(100L);
469
470         verify(mockDataPersistenceProvider, never()).deleteSnapshots(any(SnapshotSelectionCriteria.class));
471
472     }
473
474     @Test
475     public void testCommitBeforeCapture() {
476         snapshotManager.commit(100L, 0);
477
478         verify(mockReplicatedLog, never()).snapshotCommit();
479
480         verify(mockDataPersistenceProvider, never()).deleteMessages(anyLong());
481
482         verify(mockDataPersistenceProvider, never()).deleteSnapshots(any(SnapshotSelectionCriteria.class));
483
484     }
485
486     @Test
487     public void testCallingCommitMultipleTimesCausesNoHarm() {
488         doReturn(50L).when(mockDataPersistenceProvider).getLastSequenceNumber();
489
490         // when replicatedToAllIndex = -1
491         snapshotManager.capture(new SimpleReplicatedLogEntry(9, 6, new MockRaftActorContext.MockPayload()), -1);
492
493         snapshotManager.persist(ByteState.empty(), Optional.empty(), Runtime.getRuntime().totalMemory());
494
495         snapshotManager.commit(100L, 0);
496
497         snapshotManager.commit(100L, 0);
498
499         verify(mockReplicatedLog, times(1)).snapshotCommit();
500
501         verify(mockDataPersistenceProvider, times(1)).deleteMessages(50L);
502
503         verify(mockDataPersistenceProvider, times(1)).deleteSnapshots(any(SnapshotSelectionCriteria.class));
504     }
505
506     @Test
507     public void testRollback() {
508         // when replicatedToAllIndex = -1
509         snapshotManager.capture(new SimpleReplicatedLogEntry(9, 6, new MockRaftActorContext.MockPayload()), -1);
510
511         snapshotManager.persist(ByteState.empty(), Optional.empty(), Runtime.getRuntime().totalMemory());
512
513         snapshotManager.rollback();
514
515         verify(mockReplicatedLog).snapshotRollback();
516
517         MessageCollectorActor.expectFirstMatching(actorRef, SnapshotComplete.class);
518     }
519
520
521     @Test
522     public void testRollbackBeforePersist() {
523         // when replicatedToAllIndex = -1
524         snapshotManager.capture(new SimpleReplicatedLogEntry(9, 6, new MockRaftActorContext.MockPayload()), -1);
525
526         snapshotManager.rollback();
527
528         verify(mockReplicatedLog, never()).snapshotRollback();
529     }
530
531     @Test
532     public void testRollbackBeforeCapture() {
533         snapshotManager.rollback();
534
535         verify(mockReplicatedLog, never()).snapshotRollback();
536     }
537
538     @Test
539     public void testCallingRollbackMultipleTimesCausesNoHarm() {
540         // when replicatedToAllIndex = -1
541         snapshotManager.capture(new SimpleReplicatedLogEntry(9, 6, new MockRaftActorContext.MockPayload()), -1);
542
543         snapshotManager.persist(ByteState.empty(), Optional.empty(), Runtime.getRuntime().totalMemory());
544
545         snapshotManager.rollback();
546
547         snapshotManager.rollback();
548
549         verify(mockReplicatedLog, times(1)).snapshotRollback();
550     }
551
552     @Test
553     public void testTrimLogWhenTrimIndexLessThanLastApplied() {
554         doReturn(20L).when(mockRaftActorContext).getLastApplied();
555
556         ReplicatedLogEntry replicatedLogEntry = mock(ReplicatedLogEntry.class);
557         doReturn(true).when(mockReplicatedLog).isPresent(10);
558         doReturn(replicatedLogEntry).when(mockReplicatedLog).get(10);
559         doReturn(5L).when(replicatedLogEntry).getTerm();
560
561         long retIndex = snapshotManager.trimLog(10);
562         assertEquals("return index", 10L, retIndex);
563
564         verify(mockReplicatedLog).snapshotPreCommit(10, 5);
565         verify(mockReplicatedLog).snapshotCommit(false);
566
567         verify(mockRaftActorBehavior, never()).setReplicatedToAllIndex(anyLong());
568     }
569
570     @Test
571     public void testTrimLogWhenLastAppliedNotSet() {
572         doReturn(-1L).when(mockRaftActorContext).getLastApplied();
573
574         long retIndex = snapshotManager.trimLog(10);
575         assertEquals("return index", -1L, retIndex);
576
577         verify(mockReplicatedLog, never()).snapshotPreCommit(anyLong(), anyLong());
578         verify(mockReplicatedLog, never()).snapshotCommit(false);
579
580         verify(mockRaftActorBehavior, never()).setReplicatedToAllIndex(anyLong());
581     }
582
583     @Test
584     public void testTrimLogWhenLastAppliedZero() {
585         doReturn(0L).when(mockRaftActorContext).getLastApplied();
586
587         long retIndex = snapshotManager.trimLog(10);
588         assertEquals("return index", -1L, retIndex);
589
590         verify(mockReplicatedLog, never()).snapshotPreCommit(anyLong(), anyLong());
591         verify(mockReplicatedLog, never()).snapshotCommit(false);
592
593         verify(mockRaftActorBehavior, never()).setReplicatedToAllIndex(anyLong());
594     }
595
596     @Test
597     public void testTrimLogWhenTrimIndexNotPresent() {
598         doReturn(20L).when(mockRaftActorContext).getLastApplied();
599
600         doReturn(false).when(mockReplicatedLog).isPresent(10);
601
602         long retIndex = snapshotManager.trimLog(10);
603         assertEquals("return index", -1L, retIndex);
604
605         verify(mockReplicatedLog, never()).snapshotPreCommit(anyLong(), anyLong());
606         verify(mockReplicatedLog, never()).snapshotCommit(false);
607
608         // Trim index is greater than replicatedToAllIndex so should update it.
609         verify(mockRaftActorBehavior).setReplicatedToAllIndex(10L);
610     }
611
612     @Test
613     public void testTrimLogAfterCapture() {
614         boolean capture = snapshotManager.capture(new SimpleReplicatedLogEntry(9, 1,
615                 new MockRaftActorContext.MockPayload()), 9);
616
617         assertTrue(capture);
618
619         assertEquals(true, snapshotManager.isCapturing());
620
621         snapshotManager.trimLog(10);
622
623         verify(mockReplicatedLog, never()).snapshotPreCommit(anyLong(), anyLong());
624         verify(mockReplicatedLog, never()).snapshotCommit(false);
625     }
626
627     @Test
628     public void testTrimLogAfterCaptureToInstall() {
629         boolean capture = snapshotManager.capture(new SimpleReplicatedLogEntry(9, 1,
630                 new MockRaftActorContext.MockPayload()), 9);
631
632         assertTrue(capture);
633
634         assertEquals(true, snapshotManager.isCapturing());
635
636         snapshotManager.trimLog(10);
637
638         verify(mockReplicatedLog, never()).snapshotPreCommit(10, 5);
639         verify(mockReplicatedLog, never()).snapshotCommit();
640     }
641
642     @Test
643     public void testLastAppliedTermInformationReader() {
644
645         LastAppliedTermInformationReader reader = new LastAppliedTermInformationReader();
646
647         doReturn(4L).when(mockReplicatedLog).getSnapshotTerm();
648         doReturn(7L).when(mockReplicatedLog).getSnapshotIndex();
649
650         ReplicatedLogEntry lastLogEntry = new SimpleReplicatedLogEntry(9L, 6L,
651                 new MockRaftActorContext.MockPayload());
652
653         // No followers and valid lastLogEntry
654         reader.init(mockReplicatedLog, 1L, lastLogEntry, false);
655
656         assertEquals("getTerm", 6L, reader.getTerm());
657         assertEquals("getIndex", 9L, reader.getIndex());
658
659         // No followers and null lastLogEntry
660         reader.init(mockReplicatedLog, 1L, null, false);
661
662         assertEquals("getTerm", -1L, reader.getTerm());
663         assertEquals("getIndex", -1L, reader.getIndex());
664
665         // Followers and valid originalIndex entry
666         doReturn(new SimpleReplicatedLogEntry(8L, 5L,
667                 new MockRaftActorContext.MockPayload())).when(mockReplicatedLog).get(8L);
668         reader.init(mockReplicatedLog, 8L, lastLogEntry, true);
669
670         assertEquals("getTerm", 5L, reader.getTerm());
671         assertEquals("getIndex", 8L, reader.getIndex());
672
673         // Followers and null originalIndex entry and valid snapshot index
674         reader.init(mockReplicatedLog, 7L, lastLogEntry, true);
675
676         assertEquals("getTerm", 4L, reader.getTerm());
677         assertEquals("getIndex", 7L, reader.getIndex());
678
679         // Followers and null originalIndex entry and invalid snapshot index
680         doReturn(-1L).when(mockReplicatedLog).getSnapshotIndex();
681         reader.init(mockReplicatedLog, 7L, lastLogEntry, true);
682
683         assertEquals("getTerm", -1L, reader.getTerm());
684         assertEquals("getIndex", -1L, reader.getIndex());
685     }
686 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.