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 TestActorRef<MessageCollectorActor> leaderNotifierActor;
44 private TestActorRef<MessageCollectorActor> follower1NotifierActor;
45 private TestActorRef<MessageCollectorActor> follower2NotifierActor;
48 public void testLeaderTransferOnShutDown() throws Throwable {
49 testLog.info("testLeaderTransferOnShutDown starting");
53 sendPayloadWithFollower2Lagging();
55 sendShutDownToLeaderAndVerifyLeadershipTransferToFollower1();
57 sendShutDown(follower2Actor);
59 testLog.info("testLeaderTransferOnShutDown ending");
62 private void sendShutDown(ActorRef actor) throws Exception {
63 testLog.info("sendShutDown for {} starting", actor.path());
65 FiniteDuration duration = FiniteDuration.create(5, TimeUnit.SECONDS);
66 Future<Boolean> stopFuture = Patterns.gracefulStop(actor, duration, new Shutdown());
68 Boolean stopped = Await.result(stopFuture, duration);
69 assertEquals("Stopped", Boolean.TRUE, stopped);
71 testLog.info("sendShutDown for {} ending", actor.path());
74 private void sendShutDownToLeaderAndVerifyLeadershipTransferToFollower1() throws Throwable {
75 testLog.info("sendShutDownToLeaderAndVerifyLeadershipTransferToFollower1 starting");
77 clearMessages(leaderNotifierActor);
78 clearMessages(follower1NotifierActor);
79 clearMessages(follower2NotifierActor);
81 FiniteDuration duration = FiniteDuration.create(5, TimeUnit.SECONDS);
82 Future<Boolean> stopFuture = Patterns.gracefulStop(leaderActor, duration, new Shutdown());
84 assertNullLeaderIdChange(leaderNotifierActor);
85 assertNullLeaderIdChange(follower1NotifierActor);
86 assertNullLeaderIdChange(follower2NotifierActor);
88 verifyRaftState(follower1Actor, RaftState.Leader);
90 Boolean stopped = Await.result(stopFuture, duration);
91 assertEquals("Stopped", Boolean.TRUE, stopped);
93 follower2Actor.underlyingActor().stopDropMessages(AppendEntries.class);
94 ApplyState applyState = expectFirstMatching(follower2CollectorActor, ApplyState.class);
95 assertEquals("Apply sate index", 0, applyState.getReplicatedLogEntry().getIndex());
97 testLog.info("sendShutDownToLeaderAndVerifyLeadershipTransferToFollower1 ending");
100 private void sendPayloadWithFollower2Lagging() {
101 testLog.info("sendPayloadWithFollower2Lagging starting");
103 follower2Actor.underlyingActor().startDropMessages(AppendEntries.class);
105 sendPayloadData(leaderActor, "zero");
107 expectFirstMatching(leaderCollectorActor, ApplyState.class);
108 expectFirstMatching(follower1CollectorActor, ApplyState.class);
110 testLog.info("sendPayloadWithFollower2Lagging ending");
113 private void createRaftActors() {
114 testLog.info("createRaftActors starting");
116 follower1NotifierActor = factory.createTestActor(Props.create(MessageCollectorActor.class),
117 factory.generateActorId(follower1Id + "-notifier"));
118 follower1Actor = newTestRaftActor(follower1Id, TestRaftActor.newBuilder().peerAddresses(
119 ImmutableMap.of(leaderId, testActorPath(leaderId), follower2Id, testActorPath(follower2Id))).
120 config(newFollowerConfigParams()).roleChangeNotifier(follower1NotifierActor));
122 follower2NotifierActor = factory.createTestActor(Props.create(MessageCollectorActor.class),
123 factory.generateActorId(follower2Id + "-notifier"));
124 follower2Actor = newTestRaftActor(follower2Id,TestRaftActor.newBuilder().peerAddresses(
125 ImmutableMap.of(leaderId, testActorPath(leaderId), follower1Id, follower1Actor.path().toString())).
126 config(newFollowerConfigParams()).roleChangeNotifier(follower2NotifierActor));
128 peerAddresses = ImmutableMap.<String, String>builder().
129 put(follower1Id, follower1Actor.path().toString()).
130 put(follower2Id, follower2Actor.path().toString()).build();
132 leaderConfigParams = newLeaderConfigParams();
133 leaderConfigParams.setElectionTimeoutFactor(3);
134 leaderNotifierActor = factory.createTestActor(Props.create(MessageCollectorActor.class),
135 factory.generateActorId(leaderId + "-notifier"));
136 leaderActor = newTestRaftActor(leaderId, TestRaftActor.newBuilder().peerAddresses(peerAddresses).
137 config(leaderConfigParams).roleChangeNotifier(leaderNotifierActor));
139 follower1CollectorActor = follower1Actor.underlyingActor().collectorActor();
140 follower2CollectorActor = follower2Actor.underlyingActor().collectorActor();
141 leaderCollectorActor = leaderActor.underlyingActor().collectorActor();
143 leaderContext = leaderActor.underlyingActor().getRaftActorContext();
145 waitUntilLeader(leaderActor);
147 testLog.info("createRaftActors starting");
150 private void verifyRaftState(ActorRef raftActor, final RaftState expState) throws Throwable {
151 Timeout timeout = new Timeout(500, TimeUnit.MILLISECONDS);
152 Throwable lastError = null;
153 Stopwatch sw = Stopwatch.createStarted();
154 while(sw.elapsed(TimeUnit.SECONDS) <= 5) {
156 OnDemandRaftState raftState = (OnDemandRaftState)Await.result(ask(raftActor,
157 GetOnDemandRaftState.INSTANCE, timeout), timeout.duration());
158 assertEquals("getRaftState", expState.toString(), raftState.getRaftState());
160 } catch (Exception | AssertionError e) {
162 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
169 private void assertNullLeaderIdChange(TestActorRef<MessageCollectorActor> notifierActor) {
170 LeaderStateChanged change = expectFirstMatching(notifierActor, LeaderStateChanged.class);
171 assertNull("Expected null leader Id", change.getLeaderId());
175 public void testLeaderTransferAborted() throws Throwable {
176 testLog.info("testLeaderTransferAborted starting");
180 follower1Actor.underlyingActor().startDropMessages(AppendEntries.class);
181 follower2Actor.underlyingActor().startDropMessages(AppendEntries.class);
183 sendShutDown(leaderActor);
185 testLog.info("testLeaderTransferOnShutDown ending");
189 public void testLeaderTransferSkippedOnShutdownWithNoFollowers() throws Throwable {
190 testLog.info("testLeaderTransferSkippedOnShutdownWithNoFollowers starting");
192 leaderActor = newTestRaftActor(leaderId, TestRaftActor.newBuilder().config(newLeaderConfigParams()));
193 waitUntilLeader(leaderActor);
195 sendShutDown(leaderActor);
197 testLog.info("testLeaderTransferSkippedOnShutdownWithNoFollowers ending");