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

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