+/*
+ * 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;
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();
verify(mockRaftActorBehavior).setReplicatedToAllIndex(9);
}
-
@Test
public void testPersistWhenReplicatedLogDataSizeGreaterThanThreshold(){
doReturn(Integer.MAX_VALUE).when(mockReplicatedLog).dataSize();
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
snapshotManager.persist(bytes, mockRaftActorBehavior, Runtime.getRuntime().totalMemory());
+ assertEquals(true, snapshotManager.isCapturing());
+
verify(mockDataPersistenceProvider).saveSnapshot(any(Snapshot.class));
verify(mockReplicatedLog).snapshotPreCommit(9L, 6L);
SendInstallSnapshot sendInstallSnapshot = sendInstallSnapshotArgumentCaptor.getValue();
- assertTrue(Arrays.equals(bytes, sendInstallSnapshot.getSnapshot().toByteArray()));
+ assertTrue(Arrays.equals(bytes, sendInstallSnapshot.getSnapshot().getState()));
}
@Test
snapshotManager.persist(new byte[]{}, mockRaftActorBehavior, Runtime.getRuntime().totalMemory());
- snapshotManager.commit(100L);
+ assertEquals(true, snapshotManager.isCapturing());
+
+ snapshotManager.commit(100L, mockRaftActorBehavior);
+
+ assertEquals(false, snapshotManager.isCapturing());
verify(mockReplicatedLog).snapshotCommit();
snapshotManager.captureToInstall(new MockRaftActorContext.MockReplicatedLogEntry(6, 9,
new MockRaftActorContext.MockPayload()), -1, "follower-1");
- snapshotManager.commit(100L);
+ snapshotManager.commit(100L, mockRaftActorBehavior);
verify(mockReplicatedLog, never()).snapshotCommit();
@Test
public void testCommitBeforeCapture(){
- snapshotManager.commit(100L);
+ snapshotManager.commit(100L, mockRaftActorBehavior);
verify(mockReplicatedLog, never()).snapshotCommit();
snapshotManager.persist(new byte[]{}, mockRaftActorBehavior, Runtime.getRuntime().totalMemory());
- snapshotManager.commit(100L);
+ snapshotManager.commit(100L, mockRaftActorBehavior);
- snapshotManager.commit(100L);
+ snapshotManager.commit(100L, mockRaftActorBehavior);
verify(mockReplicatedLog, times(1)).snapshotCommit();
assertEquals("getTerm", -1L, reader.getTerm());
assertEquals("getIndex", -1L, reader.getIndex());
}
-}
\ No newline at end of file
+}