Make private methods static
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / RaftActorServerConfigurationSupportTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.raft;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor.clearMessages;
12 import static org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor.expectFirstMatching;
13 import akka.actor.ActorRef;
14 import akka.actor.Props;
15 import akka.actor.UntypedActor;
16 import akka.dispatch.Dispatchers;
17 import akka.testkit.JavaTestKit;
18 import akka.testkit.TestActorRef;
19 import com.google.common.base.Optional;
20 import com.google.common.collect.ImmutableMap;
21 import com.google.common.collect.Maps;
22 import com.google.common.collect.Sets;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.Map;
26 //import java.util.List;
27 import java.util.concurrent.TimeUnit;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.opendaylight.controller.cluster.DataPersistenceProvider;
32 import org.opendaylight.controller.cluster.NonPersistentDataProvider;
33 import org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot;
34 import org.opendaylight.controller.cluster.raft.base.messages.ApplyState;
35 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
36 import org.opendaylight.controller.cluster.raft.behaviors.AbstractLeader;
37 import org.opendaylight.controller.cluster.raft.behaviors.Follower;
38 import org.opendaylight.controller.cluster.raft.behaviors.Leader;
39 import org.opendaylight.controller.cluster.raft.messages.AddServer;
40 import org.opendaylight.controller.cluster.raft.messages.AddServerReply;
41 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
42 import org.opendaylight.controller.cluster.raft.messages.InstallSnapshot;
43 import org.opendaylight.controller.cluster.raft.messages.ServerChangeStatus;
44 import org.opendaylight.controller.cluster.raft.policy.DisableElectionsRaftPolicy;
45 import org.opendaylight.controller.cluster.raft.utils.ForwardMessageToBehaviorActor;
46 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
47 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
48 import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import scala.concurrent.duration.FiniteDuration;
52
53 /**
54  * Unit tests for RaftActorServerConfigurationSupport.
55  *
56  * @author Thomas Pantelis
57  */
58 public class RaftActorServerConfigurationSupportTest extends AbstractActorTest {
59     static final String LEADER_ID = "leader";
60     static final String FOLLOWER_ID = "follower";
61     static final String NEW_SERVER_ID = "new-server";
62     static final String NEW_SERVER_ID2 = "new-server2";
63     private static final Logger LOG = LoggerFactory.getLogger(RaftActorServerConfigurationSupportTest.class);
64     private static final DataPersistenceProvider NO_PERSISTENCE = new NonPersistentDataProvider();
65
66     private final TestActorFactory actorFactory = new TestActorFactory(getSystem());
67
68     private final TestActorRef<ForwardMessageToBehaviorActor> followerActor = actorFactory.createTestActor(
69             Props.create(ForwardMessageToBehaviorActor.class).withDispatcher(Dispatchers.DefaultDispatcherId()),
70             actorFactory.generateActorId(FOLLOWER_ID));
71
72     private TestActorRef<MockNewFollowerRaftActor> newFollowerRaftActor;
73     private TestActorRef<MessageCollectorActor> newFollowerCollectorActor;
74     private RaftActorContext newFollowerActorContext;
75
76     private final JavaTestKit testKit = new JavaTestKit(getSystem());
77
78     @Before
79     public void setup() {
80         InMemoryJournal.clear();
81         InMemorySnapshotStore.clear();
82
83         DefaultConfigParamsImpl configParams = newFollowerConfigParams();
84
85         newFollowerCollectorActor = actorFactory.createTestActor(
86                 MessageCollectorActor.props().withDispatcher(Dispatchers.DefaultDispatcherId()),
87                 actorFactory.generateActorId(NEW_SERVER_ID + "Collector"));
88         newFollowerRaftActor = actorFactory.createTestActor(MockNewFollowerRaftActor.props(
89                 configParams, newFollowerCollectorActor).withDispatcher(Dispatchers.DefaultDispatcherId()),
90                 actorFactory.generateActorId(NEW_SERVER_ID));
91
92         try {
93             newFollowerActorContext = newFollowerRaftActor.underlyingActor().getRaftActorContext();
94         } catch (Exception e) {
95             newFollowerActorContext = newFollowerRaftActor.underlyingActor().getRaftActorContext();
96         }
97     }
98
99     private static DefaultConfigParamsImpl newFollowerConfigParams() {
100         DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
101         configParams.setHeartBeatInterval(new FiniteDuration(100, TimeUnit.MILLISECONDS));
102         configParams.setElectionTimeoutFactor(100000);
103         configParams.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName());
104         return configParams;
105     }
106
107     @After
108     public void tearDown() throws Exception {
109         actorFactory.close();
110     }
111
112     @Test
113     public void testAddServerWithExistingFollower() throws Exception {
114         RaftActorContext followerActorContext = newFollowerContext(FOLLOWER_ID, followerActor);
115         followerActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(
116                 0, 3, 1).build());
117         followerActorContext.setCommitIndex(2);
118         followerActorContext.setLastApplied(2);
119
120         Follower follower = new Follower(followerActorContext);
121         followerActor.underlyingActor().setBehavior(follower);
122
123         TestActorRef<MockLeaderRaftActor> leaderActor = actorFactory.createTestActor(
124                 MockLeaderRaftActor.props(ImmutableMap.of(FOLLOWER_ID, followerActor.path().toString()),
125                         followerActorContext).withDispatcher(Dispatchers.DefaultDispatcherId()),
126                 actorFactory.generateActorId(LEADER_ID));
127
128         // Expect initial heartbeat from the leader.
129         expectFirstMatching(followerActor, AppendEntries.class);
130         clearMessages(followerActor);
131
132         MockLeaderRaftActor leaderRaftActor = leaderActor.underlyingActor();
133
134         leaderActor.tell(new AddServer(NEW_SERVER_ID, newFollowerRaftActor.path().toString(), true), testKit.getRef());
135
136         // Leader should install snapshot - capture and verify ApplySnapshot contents
137
138         ApplySnapshot applySnapshot = expectFirstMatching(newFollowerCollectorActor, ApplySnapshot.class);
139         List<Object> snapshotState = (List<Object>) MockRaftActor.toObject(applySnapshot.getSnapshot().getState());
140         assertEquals("Snapshot state", snapshotState, leaderRaftActor.getState());
141
142         AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class);
143         assertEquals("getStatus", ServerChangeStatus.OK, addServerReply.getStatus());
144         assertEquals("getLeaderHint", LEADER_ID, addServerReply.getLeaderHint());
145
146         // Verify ServerConfigurationPayload entry in leader's log
147
148         RaftActorContext leaderActorContext = leaderRaftActor.getRaftActorContext();
149         assertEquals("Leader journal last index", 3, leaderActorContext.getReplicatedLog().lastIndex());
150         assertEquals("Leader commit index", 3, leaderActorContext.getCommitIndex());
151         assertEquals("Leader last applied index", 3, leaderActorContext.getLastApplied());
152         verifyServerConfigurationPayloadEntry(leaderActorContext.getReplicatedLog(), LEADER_ID, FOLLOWER_ID, NEW_SERVER_ID);
153
154         // Verify ServerConfigurationPayload entry in both followers
155
156         assertEquals("Follower journal last index", 3, followerActorContext.getReplicatedLog().lastIndex());
157         verifyServerConfigurationPayloadEntry(followerActorContext.getReplicatedLog(), LEADER_ID, FOLLOWER_ID, NEW_SERVER_ID);
158
159         assertEquals("New follower journal last index", 3, newFollowerActorContext.getReplicatedLog().lastIndex());
160         verifyServerConfigurationPayloadEntry(newFollowerActorContext.getReplicatedLog(), LEADER_ID, FOLLOWER_ID, NEW_SERVER_ID);
161
162         // Verify new server config was applied in both followers
163
164         assertEquals("Follower peers", Sets.newHashSet(LEADER_ID, NEW_SERVER_ID), followerActorContext.getPeerIds());
165
166         assertEquals("New follower peers", Sets.newHashSet(LEADER_ID, FOLLOWER_ID), newFollowerActorContext.getPeerIds());
167
168         expectFirstMatching(newFollowerCollectorActor, ApplyState.class);
169         expectFirstMatching(followerActor, ApplyState.class);
170
171         assertEquals("Follower commit index", 3, followerActorContext.getCommitIndex());
172         assertEquals("Follower last applied index", 3, followerActorContext.getLastApplied());
173         assertEquals("New follower commit index", 3, newFollowerActorContext.getCommitIndex());
174         assertEquals("New follower last applied index", 3, newFollowerActorContext.getLastApplied());
175     }
176
177     @Test
178     public void testAddServerWithNoExistingFollower() throws Exception {
179         RaftActorContext initialActorContext = new MockRaftActorContext();
180         initialActorContext.setCommitIndex(1);
181         initialActorContext.setLastApplied(1);
182         initialActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(
183                 0, 2, 1).build());
184
185         TestActorRef<MockLeaderRaftActor> leaderActor = actorFactory.createTestActor(
186                 MockLeaderRaftActor.props(ImmutableMap.<String, String>of(),
187                         initialActorContext).withDispatcher(Dispatchers.DefaultDispatcherId()),
188                 actorFactory.generateActorId(LEADER_ID));
189
190         MockLeaderRaftActor leaderRaftActor = leaderActor.underlyingActor();
191         RaftActorContext leaderActorContext = leaderRaftActor.getRaftActorContext();
192
193         leaderActor.tell(new AddServer(NEW_SERVER_ID, newFollowerRaftActor.path().toString(), true), testKit.getRef());
194
195         // Leader should install snapshot - capture and verify ApplySnapshot contents
196
197         ApplySnapshot applySnapshot = expectFirstMatching(newFollowerCollectorActor, ApplySnapshot.class);
198         List<Object> snapshotState = (List<Object>) MockRaftActor.toObject(applySnapshot.getSnapshot().getState());
199         assertEquals("Snapshot state", snapshotState, leaderRaftActor.getState());
200
201         AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class);
202         assertEquals("getStatus", ServerChangeStatus.OK, addServerReply.getStatus());
203         assertEquals("getLeaderHint", LEADER_ID, addServerReply.getLeaderHint());
204
205         // Verify ServerConfigurationPayload entry in leader's log
206
207         assertEquals("Leader journal last index", 2, leaderActorContext.getReplicatedLog().lastIndex());
208         assertEquals("Leader commit index", 2, leaderActorContext.getCommitIndex());
209         assertEquals("Leader last applied index", 2, leaderActorContext.getLastApplied());
210         verifyServerConfigurationPayloadEntry(leaderActorContext.getReplicatedLog(), LEADER_ID, NEW_SERVER_ID);
211
212         // Verify ServerConfigurationPayload entry in the new follower
213
214         expectFirstMatching(newFollowerCollectorActor, ApplyState.class);
215         assertEquals("New follower journal last index", 2, newFollowerActorContext.getReplicatedLog().lastIndex());
216         verifyServerConfigurationPayloadEntry(newFollowerActorContext.getReplicatedLog(), LEADER_ID, NEW_SERVER_ID);
217
218         // Verify new server config was applied in the new follower
219
220         assertEquals("New follower peers", Sets.newHashSet(LEADER_ID), newFollowerActorContext.getPeerIds());
221     }
222
223     @Test
224     public void testAddServersAsNonVoting() throws Exception {
225         RaftActorContext initialActorContext = new MockRaftActorContext();
226         initialActorContext.setCommitIndex(-1);
227         initialActorContext.setLastApplied(-1);
228         initialActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().build());
229
230         TestActorRef<MockLeaderRaftActor> leaderActor = actorFactory.createTestActor(
231                 MockLeaderRaftActor.props(ImmutableMap.<String, String>of(),
232                         initialActorContext).withDispatcher(Dispatchers.DefaultDispatcherId()),
233                 actorFactory.generateActorId(LEADER_ID));
234
235         MockLeaderRaftActor leaderRaftActor = leaderActor.underlyingActor();
236         RaftActorContext leaderActorContext = leaderRaftActor.getRaftActorContext();
237
238         leaderActor.tell(new AddServer(NEW_SERVER_ID, newFollowerRaftActor.path().toString(), false), testKit.getRef());
239
240         AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class);
241         assertEquals("getStatus", ServerChangeStatus.OK, addServerReply.getStatus());
242         assertEquals("getLeaderHint", LEADER_ID, addServerReply.getLeaderHint());
243
244         // Verify ServerConfigurationPayload entry in leader's log
245
246         assertEquals("Leader journal last index", 0, leaderActorContext.getReplicatedLog().lastIndex());
247         assertEquals("Leader commit index", 0, leaderActorContext.getCommitIndex());
248         assertEquals("Leader last applied index", 0, leaderActorContext.getLastApplied());
249         verifyServerConfigurationPayloadEntry(leaderActorContext.getReplicatedLog(), LEADER_ID, NEW_SERVER_ID);
250
251         // Verify ServerConfigurationPayload entry in the new follower
252
253         expectFirstMatching(newFollowerCollectorActor, ApplyState.class);
254         assertEquals("New follower journal last index", 0, newFollowerActorContext.getReplicatedLog().lastIndex());
255         verifyServerConfigurationPayloadEntry(newFollowerActorContext.getReplicatedLog(), LEADER_ID, NEW_SERVER_ID);
256
257         // Verify new server config was applied in the new follower
258
259         assertEquals("New follower peers", Sets.newHashSet(LEADER_ID), newFollowerActorContext.getPeerIds());
260
261         MessageCollectorActor.assertNoneMatching(newFollowerCollectorActor, InstallSnapshot.class, 500);
262
263         // Add another non-voting server.
264
265         RaftActorContext follower2ActorContext = newFollowerContext(NEW_SERVER_ID2, followerActor);
266         Follower newFollower2 = new Follower(follower2ActorContext);
267         followerActor.underlyingActor().setBehavior(newFollower2);
268
269         leaderActor.tell(new AddServer(NEW_SERVER_ID2, followerActor.path().toString(), false), testKit.getRef());
270
271         addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class);
272         assertEquals("getStatus", ServerChangeStatus.OK, addServerReply.getStatus());
273         assertEquals("getLeaderHint", LEADER_ID, addServerReply.getLeaderHint());
274
275         assertEquals("Leader journal last index", 1, leaderActorContext.getReplicatedLog().lastIndex());
276         assertEquals("Leader commit index", 1, leaderActorContext.getCommitIndex());
277         assertEquals("Leader last applied index", 1, leaderActorContext.getLastApplied());
278         verifyServerConfigurationPayloadEntry(leaderActorContext.getReplicatedLog(),
279                 LEADER_ID, NEW_SERVER_ID, NEW_SERVER_ID2);
280     }
281
282     @Test
283     public void testAddServerWithOperationInProgress() throws Exception {
284         RaftActorContext initialActorContext = new MockRaftActorContext();
285         initialActorContext.setCommitIndex(-1);
286         initialActorContext.setLastApplied(-1);
287         initialActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().build());
288
289         TestActorRef<MockLeaderRaftActor> leaderActor = actorFactory.createTestActor(
290                 MockLeaderRaftActor.props(ImmutableMap.<String, String>of(),
291                         initialActorContext).withDispatcher(Dispatchers.DefaultDispatcherId()),
292                 actorFactory.generateActorId(LEADER_ID));
293
294         MockLeaderRaftActor leaderRaftActor = leaderActor.underlyingActor();
295         RaftActorContext leaderActorContext = leaderRaftActor.getRaftActorContext();
296
297         RaftActorContext follower2ActorContext = newFollowerContext(NEW_SERVER_ID2, followerActor);
298         Follower newFollower2 = new Follower(follower2ActorContext);
299         followerActor.underlyingActor().setBehavior(newFollower2);
300
301         MockNewFollowerRaftActor newFollowerRaftActorInstance = newFollowerRaftActor.underlyingActor();
302         newFollowerRaftActorInstance.setDropMessageOfType(InstallSnapshot.SERIALIZABLE_CLASS);
303
304         leaderActor.tell(new AddServer(NEW_SERVER_ID, newFollowerRaftActor.path().toString(), true), testKit.getRef());
305
306         // Wait for leader's install snapshot and capture it
307
308         Object installSnapshot = expectFirstMatching(newFollowerCollectorActor, InstallSnapshot.class);
309
310         JavaTestKit testKit2 = new JavaTestKit(getSystem());
311         leaderActor.tell(new AddServer(NEW_SERVER_ID2, followerActor.path().toString(), false), testKit2.getRef());
312
313         newFollowerRaftActorInstance.setDropMessageOfType(null);
314         newFollowerRaftActor.tell(installSnapshot, leaderActor);
315
316         AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class);
317         assertEquals("getStatus", ServerChangeStatus.OK, addServerReply.getStatus());
318
319         addServerReply = testKit2.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class);
320         assertEquals("getStatus", ServerChangeStatus.OK, addServerReply.getStatus());
321
322         // Verify ServerConfigurationPayload entries in leader's log
323
324         assertEquals("Leader journal last index", 1, leaderActorContext.getReplicatedLog().lastIndex());
325         assertEquals("Leader commit index", 1, leaderActorContext.getCommitIndex());
326         assertEquals("Leader last applied index", 1, leaderActorContext.getLastApplied());
327         verifyServerConfigurationPayloadEntry(leaderActorContext.getReplicatedLog(),
328                 LEADER_ID, NEW_SERVER_ID, NEW_SERVER_ID2);
329
330         // Verify ServerConfigurationPayload entry in the new follower
331
332         MessageCollectorActor.expectMatching(newFollowerCollectorActor, ApplyState.class, 2);
333
334         assertEquals("New follower peers", Sets.newHashSet(LEADER_ID, NEW_SERVER_ID2),
335                newFollowerActorContext.getPeerIds());
336     }
337
338     @Test
339     public void testAddServerWithInstallSnapshotTimeout() throws Exception {
340         newFollowerRaftActor.underlyingActor().setDropMessageOfType(InstallSnapshot.SERIALIZABLE_CLASS);
341
342         RaftActorContext initialActorContext = new MockRaftActorContext();
343         initialActorContext.setCommitIndex(-1);
344         initialActorContext.setLastApplied(-1);
345         initialActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().build());
346
347         TestActorRef<MockLeaderRaftActor> leaderActor = actorFactory.createTestActor(
348                 MockLeaderRaftActor.props(ImmutableMap.<String, String>of(),
349                         initialActorContext).withDispatcher(Dispatchers.DefaultDispatcherId()),
350                 actorFactory.generateActorId(LEADER_ID));
351
352         MockLeaderRaftActor leaderRaftActor = leaderActor.underlyingActor();
353         RaftActorContext leaderActorContext = leaderRaftActor.getRaftActorContext();
354         ((DefaultConfigParamsImpl)leaderActorContext.getConfigParams()).setElectionTimeoutFactor(1);
355
356         leaderActor.tell(new AddServer(NEW_SERVER_ID, newFollowerRaftActor.path().toString(), true), testKit.getRef());
357
358         AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class);
359         assertEquals("getStatus", ServerChangeStatus.TIMEOUT, addServerReply.getStatus());
360
361         assertEquals("Leader peers size", 0, leaderActorContext.getPeerIds().size());
362         assertEquals("Leader followers size", 0,
363                 ((AbstractLeader)leaderRaftActor.getCurrentBehavior()).getFollowerIds().size());
364     }
365
366     @Test
367     public void testAddServerWithNoLeader() {
368         DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
369         configParams.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
370
371         TestActorRef<MockRaftActor> noLeaderActor = actorFactory.createTestActor(
372                 MockRaftActor.props(LEADER_ID, ImmutableMap.<String,String>of(FOLLOWER_ID, followerActor.path().toString()),
373                         Optional.<ConfigParams>of(configParams), NO_PERSISTENCE).withDispatcher(Dispatchers.DefaultDispatcherId()),
374                 actorFactory.generateActorId(LEADER_ID));
375         noLeaderActor.underlyingActor().waitForInitializeBehaviorComplete();
376
377         noLeaderActor.tell(new AddServer(NEW_SERVER_ID, newFollowerRaftActor.path().toString(), true), testKit.getRef());
378         AddServerReply addServerReply = testKit.expectMsgClass(JavaTestKit.duration("5 seconds"), AddServerReply.class);
379         assertEquals("getStatus", ServerChangeStatus.NO_LEADER, addServerReply.getStatus());
380     }
381
382     @Test
383     public void testAddServerForwardedToLeader() {
384         DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
385         configParams.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS));
386
387         TestActorRef<MessageCollectorActor> leaderActor = actorFactory.createTestActor(
388                 MessageCollectorActor.props().withDispatcher(Dispatchers.DefaultDispatcherId()),
389                 actorFactory.generateActorId(LEADER_ID));
390
391         TestActorRef<MockRaftActor> followerRaftActor = actorFactory.createTestActor(
392                 MockRaftActor.props(FOLLOWER_ID, ImmutableMap.<String,String>of(LEADER_ID, leaderActor.path().toString()),
393                         Optional.<ConfigParams>of(configParams), NO_PERSISTENCE).withDispatcher(Dispatchers.DefaultDispatcherId()),
394                 actorFactory.generateActorId(FOLLOWER_ID));
395         followerRaftActor.underlyingActor().waitForInitializeBehaviorComplete();
396
397         followerRaftActor.tell(new AppendEntries(1, LEADER_ID, 0, 1, Collections.<ReplicatedLogEntry>emptyList(),
398                 -1, -1, (short)0), leaderActor);
399
400         followerRaftActor.tell(new AddServer(NEW_SERVER_ID, newFollowerRaftActor.path().toString(), true), testKit.getRef());
401         expectFirstMatching(leaderActor, AddServer.class);
402     }
403
404     private static void verifyServerConfigurationPayloadEntry(ReplicatedLog log, String... cNew) {
405         ReplicatedLogEntry logEntry = log.get(log.lastIndex());
406         assertEquals("Last log entry payload class", ServerConfigurationPayload.class, logEntry.getData().getClass());
407         ServerConfigurationPayload payload = (ServerConfigurationPayload)logEntry.getData();
408         assertEquals("getNewServerConfig", Sets.newHashSet(cNew), Sets.newHashSet(payload.getNewServerConfig()));
409     }
410
411     private static RaftActorContext newFollowerContext(String id, TestActorRef<? extends UntypedActor> actor) {
412         DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
413         configParams.setHeartBeatInterval(new FiniteDuration(100, TimeUnit.MILLISECONDS));
414         configParams.setElectionTimeoutFactor(100000);
415         ElectionTermImpl termInfo = new ElectionTermImpl(NO_PERSISTENCE, id, LOG);
416         termInfo.update(1, LEADER_ID);
417         RaftActorContext followerActorContext = new RaftActorContextImpl(actor, actor.underlyingActor().getContext(),
418                 id, termInfo, -1, -1,
419                 ImmutableMap.of(LEADER_ID, ""), configParams, NO_PERSISTENCE, LOG);
420         followerActorContext.setCommitIndex(-1);
421         followerActorContext.setLastApplied(-1);
422         followerActorContext.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().build());
423
424         return followerActorContext;
425     }
426
427     public static class MockLeaderRaftActor extends MockRaftActor {
428         public MockLeaderRaftActor(Map<String, String> peerAddresses, ConfigParams config,
429                 RaftActorContext fromContext) {
430             super(LEADER_ID, peerAddresses, Optional.of(config), NO_PERSISTENCE);
431
432             RaftActorContext context = getRaftActorContext();
433             for(int i = 0; i < fromContext.getReplicatedLog().size(); i++) {
434                 ReplicatedLogEntry entry = fromContext.getReplicatedLog().get(i);
435                 getState().add(entry.getData());
436                 context.getReplicatedLog().append(entry);
437             }
438
439             context.setCommitIndex(fromContext.getCommitIndex());
440             context.setLastApplied(fromContext.getLastApplied());
441             context.getTermInformation().update(fromContext.getTermInformation().getCurrentTerm(),
442                     fromContext.getTermInformation().getVotedFor());
443         }
444
445         @Override
446         protected void initializeBehavior() {
447             changeCurrentBehavior(new Leader(getRaftActorContext()));
448             initializeBehaviorComplete.countDown();
449         }
450
451         @Override
452         public void createSnapshot(ActorRef actorRef) {
453             try {
454                 actorRef.tell(new CaptureSnapshotReply(RaftActorTest.fromObject(getState()).toByteArray()), actorRef);
455             } catch (Exception e) {
456                 LOG.error("createSnapshot failed", e);
457             }
458         }
459
460         static Props props(Map<String, String> peerAddresses, RaftActorContext fromContext) {
461             DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
462             configParams.setHeartBeatInterval(new FiniteDuration(100, TimeUnit.MILLISECONDS));
463             configParams.setElectionTimeoutFactor(10);
464             return Props.create(MockLeaderRaftActor.class, peerAddresses, configParams, fromContext);
465         }
466     }
467
468     public static class MockNewFollowerRaftActor extends MockRaftActor {
469         private final TestActorRef<MessageCollectorActor> collectorActor;
470         private volatile Class<?> dropMessageOfType;
471
472         public MockNewFollowerRaftActor(ConfigParams config, TestActorRef<MessageCollectorActor> collectorActor) {
473             super(NEW_SERVER_ID, Maps.<String, String>newHashMap(), Optional.of(config), null);
474             this.collectorActor = collectorActor;
475         }
476
477         void setDropMessageOfType(Class<?> dropMessageOfType) {
478             this.dropMessageOfType = dropMessageOfType;
479         }
480
481         @Override
482         public void handleCommand(Object message) {
483             if(dropMessageOfType == null || !dropMessageOfType.equals(message.getClass())) {
484                 super.handleCommand(message);
485             }
486
487             collectorActor.tell(message, getSender());
488         }
489
490         static Props props(ConfigParams config, TestActorRef<MessageCollectorActor> collectorActor) {
491             return Props.create(MockNewFollowerRaftActor.class, config, collectorActor);
492         }
493     }
494 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.