Bug 2461 : Adding capture snapshot JMX operation.
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / SnapshotManagerTest.java
index d94eb6b041a52631c5ca02684e0c24bc32b170e6..df83f8f55843b4f82ad9f4f8336c8b6913482c5b 100644 (file)
@@ -1,3 +1,11 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
 package org.opendaylight.controller.cluster.raft;
 
 import static org.junit.Assert.assertArrayEquals;
@@ -67,6 +75,7 @@ public class SnapshotManagerTest extends AbstractActorTest {
         doReturn(new HashMap<>()).when(mockRaftActorContext).getPeerAddresses();
         doReturn(mockConfigParams).when(mockRaftActorContext).getConfigParams();
         doReturn(10L).when(mockConfigParams).getSnapshotBatchCount();
+        doReturn(70).when(mockConfigParams).getSnapshotDataThresholdPercentage();
         doReturn(mockReplicatedLog).when(mockRaftActorContext).getReplicatedLog();
         doReturn("123").when(mockRaftActorContext).getId();
         doReturn(mockDataPersistenceProvider).when(mockRaftActorContext).getPersistenceProvider();
@@ -151,6 +160,35 @@ public class SnapshotManagerTest extends AbstractActorTest {
 
     }
 
+    @Test
+    public void testCaptureWithNullLastLogEntry() throws Exception {
+        boolean capture = snapshotManager.capture(null, 1);
+
+        assertTrue(capture);
+
+        assertEquals(true, snapshotManager.isCapturing());
+
+        verify(mockProcedure).apply(null);
+
+        CaptureSnapshot captureSnapshot = snapshotManager.getCaptureSnapshot();
+
+        System.out.println(captureSnapshot);
+
+        // LastIndex and LastTerm are picked up from the lastLogEntry
+        assertEquals(-1L, captureSnapshot.getLastIndex());
+        assertEquals(-1L, captureSnapshot.getLastTerm());
+
+        // Since the actor does not have any followers (no peer addresses) lastApplied will be from lastLogEntry
+        assertEquals(-1L, captureSnapshot.getLastAppliedIndex());
+        assertEquals(-1L, captureSnapshot.getLastAppliedTerm());
+
+        //
+        assertEquals(-1L, captureSnapshot.getReplicatedToAllIndex());
+        assertEquals(-1L, captureSnapshot.getReplicatedToAllTerm());
+        actorRef.underlyingActor().clear();
+
+    }
+
     @Test
     public void testCaptureWithCreateProcedureError () throws Exception {
         doThrow(new Exception("mock")).when(mockProcedure).apply(null);
@@ -257,7 +295,6 @@ public class SnapshotManagerTest extends AbstractActorTest {
         verify(mockRaftActorBehavior).setReplicatedToAllIndex(9);
     }
 
-
     @Test
     public void testPersistWhenReplicatedLogDataSizeGreaterThanThreshold(){
         doReturn(Integer.MAX_VALUE).when(mockReplicatedLog).dataSize();
@@ -271,6 +308,34 @@ public class SnapshotManagerTest extends AbstractActorTest {
         verify(mockDataPersistenceProvider).saveSnapshot(any(Snapshot.class));
 
         verify(mockReplicatedLog).snapshotPreCommit(9L, 6L);
+
+        verify(mockRaftActorBehavior, never()).setReplicatedToAllIndex(anyLong());
+    }
+
+    @Test
+    public void testPersistWhenReplicatedLogSizeExceedsSnapshotBatchCount() {
+        doReturn(10L).when(mockReplicatedLog).size(); // matches snapshotBatchCount
+        doReturn(100).when(mockReplicatedLog).dataSize();
+
+        doReturn(5L).when(mockReplicatedLog).getSnapshotIndex();
+        doReturn(5L).when(mockReplicatedLog).getSnapshotTerm();
+
+        long replicatedToAllIndex = 1;
+        ReplicatedLogEntry replicatedLogEntry = mock(ReplicatedLogEntry.class);
+        doReturn(replicatedLogEntry).when(mockReplicatedLog).get(replicatedToAllIndex);
+        doReturn(6L).when(replicatedLogEntry).getTerm();
+        doReturn(replicatedToAllIndex).when(replicatedLogEntry).getIndex();
+
+        snapshotManager.capture(new MockRaftActorContext.MockReplicatedLogEntry(6, 9,
+                new MockRaftActorContext.MockPayload()), replicatedToAllIndex);
+
+        snapshotManager.persist(new byte[]{}, mockRaftActorBehavior, 2000000L);
+
+        verify(mockDataPersistenceProvider).saveSnapshot(any(Snapshot.class));
+
+        verify(mockReplicatedLog).snapshotPreCommit(9L, 6L);
+
+        verify(mockRaftActorBehavior).setReplicatedToAllIndex(replicatedToAllIndex);
     }
 
     @Test
@@ -287,6 +352,8 @@ public class SnapshotManagerTest extends AbstractActorTest {
 
         snapshotManager.persist(bytes, mockRaftActorBehavior, Runtime.getRuntime().totalMemory());
 
+        assertEquals(true, snapshotManager.isCapturing());
+
         verify(mockDataPersistenceProvider).saveSnapshot(any(Snapshot.class));
 
         verify(mockReplicatedLog).snapshotPreCommit(9L, 6L);
@@ -298,7 +365,7 @@ public class SnapshotManagerTest extends AbstractActorTest {
 
         SendInstallSnapshot sendInstallSnapshot = sendInstallSnapshotArgumentCaptor.getValue();
 
-        assertTrue(Arrays.equals(bytes, sendInstallSnapshot.getSnapshot().toByteArray()));
+        assertTrue(Arrays.equals(bytes, sendInstallSnapshot.getSnapshot().getState()));
     }
 
     @Test
@@ -340,8 +407,12 @@ public class SnapshotManagerTest extends AbstractActorTest {
 
         snapshotManager.persist(new byte[]{}, mockRaftActorBehavior, Runtime.getRuntime().totalMemory());
 
+        assertEquals(true, snapshotManager.isCapturing());
+
         snapshotManager.commit(100L, mockRaftActorBehavior);
 
+        assertEquals(false, snapshotManager.isCapturing());
+
         verify(mockReplicatedLog).snapshotCommit();
 
         verify(mockDataPersistenceProvider).deleteMessages(50L);
@@ -609,4 +680,4 @@ public class SnapshotManagerTest extends AbstractActorTest {
         assertEquals("getTerm", -1L, reader.getTerm());
         assertEquals("getIndex", -1L, reader.getIndex());
     }
-}
\ No newline at end of file
+}