2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.raft;
10 import static akka.pattern.Patterns.ask;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13 import static org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor.clearMessages;
14 import static org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor.expectFirstMatching;
15 import akka.actor.ActorRef;
16 import akka.actor.Props;
17 import akka.pattern.Patterns;
18 import akka.testkit.TestActorRef;
19 import akka.util.Timeout;
20 import com.google.common.base.Stopwatch;
21 import com.google.common.collect.ImmutableMap;
22 import com.google.common.util.concurrent.Uninterruptibles;
23 import java.util.concurrent.TimeUnit;
24 import org.junit.Test;
25 import org.opendaylight.controller.cluster.notifications.LeaderStateChanged;
26 import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
27 import org.opendaylight.controller.cluster.raft.client.messages.GetOnDemandRaftState;
28 import org.opendaylight.controller.cluster.raft.client.messages.OnDemandRaftState;
29 import org.opendaylight.controller.cluster.raft.client.messages.Shutdown;
30 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
31 import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
32 import scala.concurrent.Await;
33 import scala.concurrent.Future;
34 import scala.concurrent.duration.FiniteDuration;
37 * Tests leadership transfer end-to-end.
39 * @author Thomas Pantelis
41 public class LeadershipTransferIntegrationTest extends AbstractRaftActorIntegrationTest {
43 private final String follower3Id = factory.generateActorId("follower");
44 private TestActorRef<MessageCollectorActor> leaderNotifierActor;
45 private TestActorRef<MessageCollectorActor> follower1NotifierActor;
46 private TestActorRef<MessageCollectorActor> follower2NotifierActor;
47 private TestActorRef<MessageCollectorActor> follower3NotifierActor;
48 private TestActorRef<TestRaftActor> follower3Actor;
49 private ActorRef follower3CollectorActor;
52 public void testLeaderTransferOnShutDown() throws Throwable {
53 testLog.info("testLeaderTransferOnShutDown starting");
57 sendPayloadWithFollower2Lagging();
59 sendShutDownToLeaderAndVerifyLeadershipTransferToFollower1();
61 sendShutDown(follower2Actor);
63 testLog.info("testLeaderTransferOnShutDown ending");
66 private void sendShutDown(ActorRef actor) throws Exception {
67 testLog.info("sendShutDown for {} starting", actor.path());
69 FiniteDuration duration = FiniteDuration.create(5, TimeUnit.SECONDS);
70 Future<Boolean> stopFuture = Patterns.gracefulStop(actor, duration, Shutdown.INSTANCE);
72 Boolean stopped = Await.result(stopFuture, duration);
73 assertEquals("Stopped", Boolean.TRUE, stopped);
75 testLog.info("sendShutDown for {} ending", actor.path());
78 private void sendShutDownToLeaderAndVerifyLeadershipTransferToFollower1() throws Throwable {
79 testLog.info("sendShutDownToLeaderAndVerifyLeadershipTransferToFollower1 starting");
81 clearMessages(leaderNotifierActor);
82 clearMessages(follower1NotifierActor);
83 clearMessages(follower2NotifierActor);
84 clearMessages(follower3NotifierActor);
86 FiniteDuration duration = FiniteDuration.create(5, TimeUnit.SECONDS);
87 Future<Boolean> stopFuture = Patterns.gracefulStop(leaderActor, duration, Shutdown.INSTANCE);
89 assertNullLeaderIdChange(leaderNotifierActor);
90 assertNullLeaderIdChange(follower1NotifierActor);
91 assertNullLeaderIdChange(follower2NotifierActor);
92 assertNullLeaderIdChange(follower3NotifierActor);
94 verifyRaftState(follower1Actor, RaftState.Leader);
96 Boolean stopped = Await.result(stopFuture, duration);
97 assertEquals("Stopped", Boolean.TRUE, stopped);
99 follower2Actor.underlyingActor().stopDropMessages(AppendEntries.class);
100 ApplyState applyState = expectFirstMatching(follower2CollectorActor, ApplyState.class);
101 assertEquals("Apply sate index", 0, applyState.getReplicatedLogEntry().getIndex());
103 testLog.info("sendShutDownToLeaderAndVerifyLeadershipTransferToFollower1 ending");
106 private void sendPayloadWithFollower2Lagging() {
107 testLog.info("sendPayloadWithFollower2Lagging starting");
109 follower2Actor.underlyingActor().startDropMessages(AppendEntries.class);
111 sendPayloadData(leaderActor, "zero");
113 expectFirstMatching(leaderCollectorActor, ApplyState.class);
114 expectFirstMatching(follower1CollectorActor, ApplyState.class);
115 expectFirstMatching(follower3CollectorActor, ApplyState.class);
117 testLog.info("sendPayloadWithFollower2Lagging ending");
120 private void createRaftActors() {
121 testLog.info("createRaftActors starting");
123 follower1NotifierActor = factory.createTestActor(Props.create(MessageCollectorActor.class),
124 factory.generateActorId(follower1Id + "-notifier"));
125 follower1Actor = newTestRaftActor(follower1Id, TestRaftActor.newBuilder().peerAddresses(
126 ImmutableMap.of(leaderId, testActorPath(leaderId), follower2Id, testActorPath(follower2Id),
127 follower3Id, testActorPath(follower3Id))).
128 config(newFollowerConfigParams()).roleChangeNotifier(follower1NotifierActor));
130 follower2NotifierActor = factory.createTestActor(Props.create(MessageCollectorActor.class),
131 factory.generateActorId(follower2Id + "-notifier"));
132 follower2Actor = newTestRaftActor(follower2Id,TestRaftActor.newBuilder().peerAddresses(
133 ImmutableMap.of(leaderId, testActorPath(leaderId), follower1Id, follower1Actor.path().toString(),
134 follower3Id, testActorPath(follower3Id))).
135 config(newFollowerConfigParams()).roleChangeNotifier(follower2NotifierActor));
137 follower3NotifierActor = factory.createTestActor(Props.create(MessageCollectorActor.class),
138 factory.generateActorId(follower3Id + "-notifier"));
139 follower3Actor = newTestRaftActor(follower3Id,TestRaftActor.newBuilder().peerAddresses(
140 ImmutableMap.of(leaderId, testActorPath(leaderId), follower1Id, follower1Actor.path().toString(),
141 follower2Id, follower2Actor.path().toString())).
142 config(newFollowerConfigParams()).roleChangeNotifier(follower3NotifierActor));
144 peerAddresses = ImmutableMap.<String, String>builder().
145 put(follower1Id, follower1Actor.path().toString()).
146 put(follower2Id, follower2Actor.path().toString()).
147 put(follower3Id, follower3Actor.path().toString()).build();
149 leaderConfigParams = newLeaderConfigParams();
150 leaderConfigParams.setElectionTimeoutFactor(3);
151 leaderNotifierActor = factory.createTestActor(Props.create(MessageCollectorActor.class),
152 factory.generateActorId(leaderId + "-notifier"));
153 leaderActor = newTestRaftActor(leaderId, TestRaftActor.newBuilder().peerAddresses(peerAddresses).
154 config(leaderConfigParams).roleChangeNotifier(leaderNotifierActor));
156 follower1CollectorActor = follower1Actor.underlyingActor().collectorActor();
157 follower2CollectorActor = follower2Actor.underlyingActor().collectorActor();
158 follower3CollectorActor = follower3Actor.underlyingActor().collectorActor();
159 leaderCollectorActor = leaderActor.underlyingActor().collectorActor();
161 leaderContext = leaderActor.underlyingActor().getRaftActorContext();
162 leaderContext.getPeerInfo(follower3Id).setVotingState(VotingState.NON_VOTING);
164 waitUntilLeader(leaderActor);
166 testLog.info("createRaftActors starting");
169 private static void verifyRaftState(ActorRef raftActor, final RaftState expState) throws Throwable {
170 Timeout timeout = new Timeout(500, TimeUnit.MILLISECONDS);
171 Throwable lastError = null;
172 Stopwatch sw = Stopwatch.createStarted();
173 while(sw.elapsed(TimeUnit.SECONDS) <= 5) {
175 OnDemandRaftState raftState = (OnDemandRaftState)Await.result(ask(raftActor,
176 GetOnDemandRaftState.INSTANCE, timeout), timeout.duration());
177 assertEquals("getRaftState", expState.toString(), raftState.getRaftState());
179 } catch (Exception | AssertionError e) {
181 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
188 private static void assertNullLeaderIdChange(TestActorRef<MessageCollectorActor> notifierActor) {
189 LeaderStateChanged change = expectFirstMatching(notifierActor, LeaderStateChanged.class);
190 assertNull("Expected null leader Id", change.getLeaderId());
194 public void testLeaderTransferAborted() throws Throwable {
195 testLog.info("testLeaderTransferAborted starting");
199 follower1Actor.underlyingActor().startDropMessages(AppendEntries.class);
200 follower2Actor.underlyingActor().startDropMessages(AppendEntries.class);
202 sendShutDown(leaderActor);
204 verifyRaftState(follower1Actor, RaftState.Follower);
205 verifyRaftState(follower2Actor, RaftState.Follower);
206 verifyRaftState(follower3Actor, RaftState.Follower);
208 testLog.info("testLeaderTransferOnShutDown ending");
212 public void testLeaderTransferSkippedOnShutdownWithNoFollowers() throws Throwable {
213 testLog.info("testLeaderTransferSkippedOnShutdownWithNoFollowers starting");
215 leaderActor = newTestRaftActor(leaderId, TestRaftActor.newBuilder().config(newLeaderConfigParams()));
216 waitUntilLeader(leaderActor);
218 sendShutDown(leaderActor);
220 testLog.info("testLeaderTransferSkippedOnShutdownWithNoFollowers ending");