X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2FRaftActorRecoverySupportTest.java;h=71ca4cae5df490d6d6d5faa00954b167dc614fb9;hb=24a5bafd22b83c4d838b7c3fc5225934fe969561;hp=26bf7228c1adece502635ca71c1c586ada3615b1;hpb=7873bfcd1735ce86a58c015b2865c7f3627fb121;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorRecoverySupportTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorRecoverySupportTest.java index 26bf7228c1..71ca4cae5d 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorRecoverySupportTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorRecoverySupportTest.java @@ -8,9 +8,12 @@ package org.opendaylight.controller.cluster.raft; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyInt; import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; import akka.japi.Procedure; @@ -18,6 +21,7 @@ import akka.persistence.RecoveryCompleted; import akka.persistence.SnapshotMetadata; import akka.persistence.SnapshotOffer; import akka.persistence.SnapshotSelectionCriteria; +import com.google.common.collect.Sets; import java.util.Arrays; import java.util.Collections; import org.hamcrest.Description; @@ -31,11 +35,13 @@ import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.opendaylight.controller.cluster.DataPersistenceProvider; import org.opendaylight.controller.cluster.PersistentDataProvider; +import org.opendaylight.controller.cluster.raft.ServerConfigurationPayload.ServerInfo; import org.opendaylight.controller.cluster.raft.base.messages.ApplyJournalEntries; import org.opendaylight.controller.cluster.raft.base.messages.ApplyLogEntries; import org.opendaylight.controller.cluster.raft.base.messages.DeleteEntries; import org.opendaylight.controller.cluster.raft.base.messages.UpdateElectionTerm; import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior; +import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -64,12 +70,14 @@ public class RaftActorRecoverySupportTest { private RaftActorContext context; private final DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl(); + private final String localId = "leader"; + @Before public void setup() { MockitoAnnotations.initMocks(this); - context = new RaftActorContextImpl(null, null, "test", new ElectionTermImpl(mockPersistentProvider, "test", LOG), + context = new RaftActorContextImpl(null, null, localId, new ElectionTermImpl(mockPersistentProvider, "test", LOG), -1, -1, Collections.emptyMap(), configParams, mockPersistence, LOG); support = new RaftActorRecoverySupport(context, mockBehavior , mockCohort); @@ -207,6 +215,7 @@ public class RaftActorRecoverySupportTest { assertEquals("Snapshot index", lastAppliedDuringSnapshotCapture, context.getReplicatedLog().getSnapshotIndex()); assertEquals("Election term", electionTerm, context.getTermInformation().getCurrentTerm()); assertEquals("Election votedFor", electionVotedFor, context.getTermInformation().getVotedFor()); + assertFalse("Dynamic server configuration", context.isDynamicServerConfigurationInUse()); verify(mockCohort).applyRecoverySnapshot(snapshotBytes); } @@ -234,7 +243,7 @@ public class RaftActorRecoverySupportTest { } inOrder.verify(mockCohort).applyCurrentLogRecoveryBatch(); - + inOrder.verify(mockCohort).getRestoreFromSnapshot(); inOrder.verifyNoMoreInteractions(); } @@ -242,6 +251,7 @@ public class RaftActorRecoverySupportTest { public void testOnRecoveryCompletedWithNoRemainingBatch() { sendMessageToSupport(RecoveryCompleted.getInstance(), true); + verify(mockCohort).getRestoreFromSnapshot(); verifyNoMoreInteractions(mockCohort); } @@ -331,6 +341,7 @@ public class RaftActorRecoverySupportTest { sendMessageToSupport(RecoveryCompleted.getInstance(), true); + verify(mockCohort).getRestoreFromSnapshot(); verifyNoMoreInteractions(mockCohort); verify(mockPersistentProvider).deleteMessages(10L); @@ -364,6 +375,90 @@ public class RaftActorRecoverySupportTest { sendMessageToSupport(RecoveryCompleted.getInstance(), true); + verify(mockCohort).getRestoreFromSnapshot(); verifyNoMoreInteractions(mockCohort, mockPersistentProvider); } + + @Test + public void testServerConfigurationPayloadApplied() { + String follower1 = "follower1"; + String follower2 = "follower2"; + String follower3 = "follower3"; + + context.addToPeers(follower1, null, VotingState.VOTING); + context.addToPeers(follower2, null, VotingState.VOTING); + + //add new Server + ServerConfigurationPayload obj = new ServerConfigurationPayload(Arrays.asList( + new ServerInfo(localId, true), + new ServerInfo(follower1, true), + new ServerInfo(follower2, false), + new ServerInfo(follower3, true))); + + sendMessageToSupport(new MockRaftActorContext.MockReplicatedLogEntry(1, 0, obj)); + + //verify new peers + assertTrue("Dynamic server configuration", context.isDynamicServerConfigurationInUse()); + assertEquals("New peer Ids", Sets.newHashSet(follower1, follower2, follower3), + Sets.newHashSet(context.getPeerIds())); + assertEquals("follower1 isVoting", true, context.getPeerInfo(follower1).isVoting()); + assertEquals("follower2 isVoting", false, context.getPeerInfo(follower2).isVoting()); + assertEquals("follower3 isVoting", true, context.getPeerInfo(follower3).isVoting()); + + sendMessageToSupport(new ApplyJournalEntries(0)); + + verify(mockCohort, never()).startLogRecoveryBatch(anyInt()); + verify(mockCohort, never()).appendRecoveredLogEntry(any(Payload.class)); + + //remove existing follower1 + obj = new ServerConfigurationPayload(Arrays.asList( + new ServerInfo(localId, true), + new ServerInfo("follower2", true), + new ServerInfo("follower3", true))); + + sendMessageToSupport(new MockRaftActorContext.MockReplicatedLogEntry(1, 1, obj)); + + //verify new peers + assertTrue("Dynamic server configuration", context.isDynamicServerConfigurationInUse()); + assertEquals("New peer Ids", Sets.newHashSet(follower2, follower3), Sets.newHashSet(context.getPeerIds())); + } + + @Test + public void testServerConfigurationPayloadAppliedWithPersistenceDisabled() { + doReturn(false).when(mockPersistence).isRecoveryApplicable(); + + String follower = "follower"; + ServerConfigurationPayload obj = new ServerConfigurationPayload(Arrays.asList( + new ServerInfo(localId, true), new ServerInfo(follower, true))); + + sendMessageToSupport(new MockRaftActorContext.MockReplicatedLogEntry(1, 0, obj)); + + //verify new peers + assertEquals("New peer Ids", Sets.newHashSet(follower), Sets.newHashSet(context.getPeerIds())); + } + + @Test + public void testOnSnapshotOfferWithServerConfiguration() { + long electionTerm = 2; + String electionVotedFor = "member-2"; + ServerConfigurationPayload serverPayload = new ServerConfigurationPayload(Arrays.asList( + new ServerInfo(localId, true), + new ServerInfo("follower1", true), + new ServerInfo("follower2", true))); + + Snapshot snapshot = Snapshot.create(new byte[]{1}, Collections.emptyList(), + -1, -1, -1, -1, electionTerm, electionVotedFor, serverPayload); + + SnapshotMetadata metadata = new SnapshotMetadata("test", 6, 12345); + SnapshotOffer snapshotOffer = new SnapshotOffer(metadata , snapshot); + + sendMessageToSupport(snapshotOffer); + + assertEquals("Journal log size", 0, context.getReplicatedLog().size()); + assertEquals("Election term", electionTerm, context.getTermInformation().getCurrentTerm()); + assertEquals("Election votedFor", electionVotedFor, context.getTermInformation().getVotedFor()); + assertTrue("Dynamic server configuration", context.isDynamicServerConfigurationInUse()); + assertEquals("Peer List", Sets.newHashSet("follower1", "follower2"), + Sets.newHashSet(context.getPeerIds())); + } }