1cee5e0d88347cf629f2412c534f90e26b852b7d
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / behaviors / AbstractRaftActorBehaviorTest.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco 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
9 package org.opendaylight.controller.cluster.raft.behaviors;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNull;
14 import akka.actor.ActorRef;
15 import akka.actor.Props;
16 import akka.testkit.TestActorRef;
17 import com.google.protobuf.ByteString;
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.io.ObjectOutputStream;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Map;
24 import org.junit.After;
25 import org.junit.Assert;
26 import org.junit.Test;
27 import org.opendaylight.controller.cluster.raft.AbstractActorTest;
28 import org.opendaylight.controller.cluster.raft.MockRaftActorContext;
29 import org.opendaylight.controller.cluster.raft.RaftActorContext;
30 import org.opendaylight.controller.cluster.raft.RaftState;
31 import org.opendaylight.controller.cluster.raft.ReplicatedLogEntry;
32 import org.opendaylight.controller.cluster.raft.TestActorFactory;
33 import org.opendaylight.controller.cluster.raft.messages.AppendEntries;
34 import org.opendaylight.controller.cluster.raft.messages.AppendEntriesReply;
35 import org.opendaylight.controller.cluster.raft.messages.RaftRPC;
36 import org.opendaylight.controller.cluster.raft.messages.RequestVote;
37 import org.opendaylight.controller.cluster.raft.messages.RequestVoteReply;
38 import org.opendaylight.controller.cluster.raft.policy.RaftPolicy;
39 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
40 import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
41 import org.slf4j.LoggerFactory;
42
43 public abstract class AbstractRaftActorBehaviorTest<T extends RaftActorBehavior> extends AbstractActorTest {
44
45     protected final TestActorFactory actorFactory = new TestActorFactory(getSystem());
46
47     private final TestActorRef<MessageCollectorActor> behaviorActor = actorFactory.createTestActor(
48             Props.create(MessageCollectorActor.class), actorFactory.generateActorId("behavior"));
49
50     RaftActorBehavior behavior;
51
52     @After
53     public void tearDown() throws Exception {
54         if(behavior != null) {
55             behavior.close();
56         }
57
58         actorFactory.close();
59     }
60
61     /**
62      * This test checks that when a new Raft RPC message is received with a newer
63      * term the RaftActor gets into the Follower state.
64      *
65      * @throws Exception
66      */
67     @Test
68     public void testHandleRaftRPCWithNewerTerm() throws Exception {
69         MockRaftActorContext actorContext = createActorContext();
70
71         assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(actorContext, behaviorActor,
72                 createAppendEntriesWithNewerTerm());
73
74         assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(actorContext, behaviorActor,
75                 createAppendEntriesReplyWithNewerTerm());
76
77         assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(actorContext, behaviorActor,
78                 createRequestVoteWithNewerTerm());
79
80         assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(actorContext, behaviorActor,
81                 createRequestVoteReplyWithNewerTerm());
82     }
83
84
85     /**
86      * This test verifies that when an AppendEntries is received with a term that
87      * is less that the currentTerm of the RaftActor then the RaftActor does not
88      * change it's state and it responds back with a failure
89      *
90      * @throws Exception
91      */
92     @Test
93     public void testHandleAppendEntriesSenderTermLessThanReceiverTerm() throws Exception {
94         MockRaftActorContext context = createActorContext();
95         short payloadVersion = 5;
96         context.setPayloadVersion(payloadVersion);
97
98         // First set the receivers term to a high number (1000)
99         context.getTermInformation().update(1000, "test");
100
101         AppendEntries appendEntries = new AppendEntries(100, "leader-1", 0, 0, null, 101, -1, (short)4);
102
103         behavior = createBehavior(context);
104
105         RaftState expected = behavior.state();
106
107         RaftActorBehavior raftBehavior = behavior.handleMessage(behaviorActor, appendEntries);
108
109         assertEquals("Raft state", expected, raftBehavior.state());
110
111         // Also expect an AppendEntriesReply to be sent where success is false
112
113         AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(
114                 behaviorActor, AppendEntriesReply.class);
115
116         assertEquals("isSuccess", false, reply.isSuccess());
117         assertEquals("getPayloadVersion", payloadVersion, reply.getPayloadVersion());
118     }
119
120
121     @Test
122     public void testHandleAppendEntriesAddSameEntryToLog() throws Exception {
123         MockRaftActorContext context = createActorContext();
124
125         context.getTermInformation().update(2, "test");
126
127         // Prepare the receivers log
128         MockRaftActorContext.MockPayload payload = new MockRaftActorContext.MockPayload("zero");
129         setLastLogEntry(context, 2, 0, payload);
130
131         List<ReplicatedLogEntry> entries = new ArrayList<>();
132         entries.add(new MockRaftActorContext.MockReplicatedLogEntry(2, 0, payload));
133
134         AppendEntries appendEntries = new AppendEntries(2, "leader-1", -1, -1, entries, 2, -1, (short)0);
135
136         behavior = createBehavior(context);
137
138         assertFalse("This test should be overridden when testing Candidate", behavior instanceof Candidate);
139
140         RaftState expected = behavior.state();
141
142         // Check that the behavior does not handle unknwon message
143         assertNull(behavior.handleMessage(behaviorActor, "unknown"));
144
145         RaftActorBehavior raftBehavior = behavior.handleMessage(behaviorActor, appendEntries);
146
147         assertEquals("Raft state", expected, raftBehavior.state());
148
149         assertEquals("ReplicatedLog size", 1, context.getReplicatedLog().size());
150
151         handleAppendEntriesAddSameEntryToLogReply(behaviorActor);
152     }
153
154     protected void handleAppendEntriesAddSameEntryToLogReply(TestActorRef<MessageCollectorActor> replyActor)
155             throws Exception {
156         AppendEntriesReply reply = MessageCollectorActor.getFirstMatching(replyActor, AppendEntriesReply.class);
157         Assert.assertNull("Expected no AppendEntriesReply", reply);
158     }
159
160     /**
161      * This test verifies that when a RequestVote is received by the RaftActor
162      * with the senders' log is more up to date than the receiver that the receiver grants
163      * the vote to the sender.
164      */
165     @Test
166     public void testHandleRequestVoteWhenSenderLogMoreUpToDate() {
167         MockRaftActorContext context = createActorContext();
168
169         behavior = createBehavior(context);
170
171         context.getTermInformation().update(1, "test");
172
173         behavior.handleMessage(behaviorActor, new RequestVote(context.getTermInformation().getCurrentTerm(),
174                 "test", 10000, 999));
175
176         RequestVoteReply reply = MessageCollectorActor.expectFirstMatching(behaviorActor,
177                 RequestVoteReply.class);
178         assertEquals("isVoteGranted", true, reply.isVoteGranted());
179     }
180
181     /**
182      * This test verifies that when a RaftActor receives a RequestVote message
183      * with a term that is greater than it's currentTerm but a less up-to-date
184      * log then the receiving RaftActor will not grant the vote to the sender
185      */
186     @Test
187     public void testHandleRequestVoteWhenSenderLogLessUptoDate() {
188         MockRaftActorContext context = createActorContext();
189
190         behavior = createBehavior(context);
191
192         context.getTermInformation().update(1, "test");
193
194         int index = 2000;
195         setLastLogEntry(context, context.getTermInformation().getCurrentTerm(), index,
196                 new MockRaftActorContext.MockPayload(""));
197
198         behavior.handleMessage(behaviorActor, new RequestVote(
199                 context.getTermInformation().getCurrentTerm(), "test",
200                 index - 1, context.getTermInformation().getCurrentTerm()));
201
202         RequestVoteReply reply = MessageCollectorActor.expectFirstMatching(behaviorActor,
203                 RequestVoteReply.class);
204         assertEquals("isVoteGranted", false, reply.isVoteGranted());
205     }
206
207
208
209     /**
210      * This test verifies that the receiving RaftActor will not grant a vote
211      * to a sender if the sender's term is lesser than the currentTerm of the
212      * recipient RaftActor
213      */
214     @Test
215     public void testHandleRequestVoteWhenSenderTermLessThanCurrentTerm() {
216         MockRaftActorContext context = createActorContext();
217
218         context.getTermInformation().update(1000, null);
219
220         behavior = createBehavior(context);
221
222         behavior.handleMessage(behaviorActor, new RequestVote(999, "test", 10000, 999));
223
224         RequestVoteReply reply = MessageCollectorActor.expectFirstMatching(behaviorActor,
225                 RequestVoteReply.class);
226         assertEquals("isVoteGranted", false, reply.isVoteGranted());
227     }
228
229     @Test
230     public void testPerformSnapshot() {
231         MockRaftActorContext context = new MockRaftActorContext("test", getSystem(), behaviorActor);
232         AbstractRaftActorBehavior abstractBehavior =  (AbstractRaftActorBehavior) createBehavior(context);
233         if (abstractBehavior instanceof Candidate) {
234             return;
235         }
236
237         context.getTermInformation().update(1, "test");
238
239         //log has 1 entry with replicatedToAllIndex = 0, does not do anything, returns the
240         context.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(0, 1, 1).build());
241         context.setLastApplied(0);
242         abstractBehavior.performSnapshotWithoutCapture(0);
243         assertEquals(-1, abstractBehavior.getReplicatedToAllIndex());
244         assertEquals(1, context.getReplicatedLog().size());
245
246         //2 entries, lastApplied still 0, no purging.
247         context.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(0, 2, 1).build());
248         context.setLastApplied(0);
249         abstractBehavior.performSnapshotWithoutCapture(0);
250         assertEquals(-1, abstractBehavior.getReplicatedToAllIndex());
251         assertEquals(2, context.getReplicatedLog().size());
252
253         //2 entries, lastApplied still 0, no purging.
254         context.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(0, 2, 1).build());
255         context.setLastApplied(1);
256         abstractBehavior.performSnapshotWithoutCapture(0);
257         assertEquals(0, abstractBehavior.getReplicatedToAllIndex());
258         assertEquals(1, context.getReplicatedLog().size());
259
260         //5 entries, lastApplied =2 and replicatedIndex = 3, but since we want to keep the lastapplied, indices 0 and 1 will only get purged
261         context.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(0, 5, 1).build());
262         context.setLastApplied(2);
263         abstractBehavior.performSnapshotWithoutCapture(3);
264         assertEquals(1, abstractBehavior.getReplicatedToAllIndex());
265         assertEquals(3, context.getReplicatedLog().size());
266
267         // scenario where Last applied > Replicated to all index (becoz of a slow follower)
268         context.setReplicatedLog(new MockRaftActorContext.MockReplicatedLogBuilder().createEntries(0, 3, 1).build());
269         context.setLastApplied(2);
270         abstractBehavior.performSnapshotWithoutCapture(1);
271         assertEquals(1, abstractBehavior.getReplicatedToAllIndex());
272         assertEquals(1, context.getReplicatedLog().size());
273     }
274
275
276     protected void assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(MockRaftActorContext actorContext,
277             ActorRef actorRef, RaftRPC rpc) throws Exception {
278
279         Payload p = new MockRaftActorContext.MockPayload("");
280         setLastLogEntry(actorContext, 1, 0, p);
281         actorContext.getTermInformation().update(1, "test");
282
283         RaftActorBehavior origBehavior = createBehavior(actorContext);
284         RaftActorBehavior raftBehavior = origBehavior.handleMessage(actorRef, rpc);
285
286         assertEquals("New raft state", RaftState.Follower, raftBehavior.state());
287         assertEquals("New election term", rpc.getTerm(), actorContext.getTermInformation().getCurrentTerm());
288
289         origBehavior.close();
290         raftBehavior.close();
291     }
292
293     protected MockRaftActorContext.SimpleReplicatedLog setLastLogEntry(
294         MockRaftActorContext actorContext, long term, long index, Payload data) {
295         return setLastLogEntry(actorContext,
296             new MockRaftActorContext.MockReplicatedLogEntry(term, index, data));
297     }
298
299     protected MockRaftActorContext.SimpleReplicatedLog setLastLogEntry(MockRaftActorContext actorContext,
300             ReplicatedLogEntry logEntry) {
301         MockRaftActorContext.SimpleReplicatedLog log = new MockRaftActorContext.SimpleReplicatedLog();
302         log.append(logEntry);
303         actorContext.setReplicatedLog(log);
304
305         return log;
306     }
307
308     protected abstract T createBehavior(RaftActorContext actorContext);
309
310     protected final T createBehavior(MockRaftActorContext actorContext) {
311         T ret = createBehavior((RaftActorContext)actorContext);
312         actorContext.setCurrentBehavior(ret);
313         return ret;
314     }
315
316     protected RaftActorBehavior createBehavior() {
317         return createBehavior(createActorContext());
318     }
319
320     protected MockRaftActorContext createActorContext() {
321         return new MockRaftActorContext();
322     }
323
324     protected MockRaftActorContext createActorContext(ActorRef actor) {
325         return new MockRaftActorContext("test", getSystem(), actor);
326     }
327
328     protected AppendEntries createAppendEntriesWithNewerTerm() {
329         return new AppendEntries(100, "leader-1", 0, 0, null, 1, -1, (short)0);
330     }
331
332     protected AppendEntriesReply createAppendEntriesReplyWithNewerTerm() {
333         return new AppendEntriesReply("follower-1", 100, false, 100, 100, (short)0);
334     }
335
336     protected RequestVote createRequestVoteWithNewerTerm() {
337         return new RequestVote(100, "candidate-1", 10, 100);
338     }
339
340     protected RequestVoteReply createRequestVoteReplyWithNewerTerm() {
341         return new RequestVoteReply(100, false);
342     }
343
344     protected ByteString toByteString(Map<String, String> state) {
345         ByteArrayOutputStream bos = new ByteArrayOutputStream();
346         try(ObjectOutputStream oos = new ObjectOutputStream(bos)) {
347             oos.writeObject(state);
348             return ByteString.copyFrom(bos.toByteArray());
349         } catch (IOException e) {
350             throw new AssertionError("IOException occurred converting Map to Bytestring", e);
351         }
352     }
353
354     protected void logStart(String name) {
355         LoggerFactory.getLogger(LeaderTest.class).info("Starting " + name);
356     }
357
358     protected RaftPolicy createRaftPolicy(final boolean automaticElectionsEnabled,
359                                           final boolean applyModificationToStateBeforeConsensus){
360         return new RaftPolicy() {
361             @Override
362             public boolean automaticElectionsEnabled() {
363                 return automaticElectionsEnabled;
364             }
365
366             @Override
367             public boolean applyModificationToStateBeforeConsensus() {
368                 return applyModificationToStateBeforeConsensus;
369             }
370         };
371     }
372 }

©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.