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;
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;
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;
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.<String,String>emptyMap(), configParams, mockPersistence, LOG);
support = new RaftActorRecoverySupport(context, mockBehavior , mockCohort);
ReplicatedLogEntry unAppliedEntry2 = new MockRaftActorContext.MockReplicatedLogEntry(1,
5, new MockRaftActorContext.MockPayload("5", 5));
- int lastAppliedDuringSnapshotCapture = 3;
- int lastIndexDuringSnapshotCapture = 5;
+ long lastAppliedDuringSnapshotCapture = 3;
+ long lastIndexDuringSnapshotCapture = 5;
+ long electionTerm = 2;
+ String electionVotedFor = "member-2";
Snapshot snapshot = Snapshot.create(snapshotBytes, Arrays.asList(unAppliedEntry1, unAppliedEntry2),
- lastIndexDuringSnapshotCapture, 1, lastAppliedDuringSnapshotCapture, 1);
+ lastIndexDuringSnapshotCapture, 1, lastAppliedDuringSnapshotCapture, 1, electionTerm, electionVotedFor);
SnapshotMetadata metadata = new SnapshotMetadata("test", 6, 12345);
SnapshotOffer snapshotOffer = new SnapshotOffer(metadata , snapshot);
assertEquals("Commit index", lastAppliedDuringSnapshotCapture, context.getCommitIndex());
assertEquals("Snapshot term", 1, context.getReplicatedLog().getSnapshotTerm());
assertEquals("Snapshot index", lastAppliedDuringSnapshotCapture, context.getReplicatedLog().getSnapshotIndex());
+ assertEquals("Election term", electionTerm, context.getTermInformation().getCurrentTerm());
+ assertEquals("Election votedFor", electionVotedFor, context.getTermInformation().getVotedFor());
verify(mockCohort).applyRecoverySnapshot(snapshotBytes);
}
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
+ 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
+ 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()));
+ }
}