2 * Copyright (c) 2014, 2015 Cisco 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
9 package org.opendaylight.controller.cluster.raft.behaviors;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNull;
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;
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;
48 public abstract class AbstractRaftActorBehaviorTest<T extends RaftActorBehavior> extends AbstractActorTest {
50 protected final TestActorFactory actorFactory = new TestActorFactory(getSystem());
52 private final TestActorRef<MessageCollectorActor> behaviorActor = actorFactory.createTestActor(
53 Props.create(MessageCollectorActor.class), actorFactory.generateActorId("behavior"));
55 RaftActorBehavior behavior;
58 public void tearDown() throws Exception {
59 if (behavior != null) {
65 InMemoryJournal.clear();
66 InMemorySnapshotStore.clear();
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.
74 public void testHandleRaftRPCWithNewerTerm() throws Exception {
75 MockRaftActorContext actorContext = createActorContext();
77 assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(actorContext, behaviorActor,
78 createAppendEntriesWithNewerTerm());
80 assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(actorContext, behaviorActor,
81 createAppendEntriesReplyWithNewerTerm());
83 assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(actorContext, behaviorActor,
84 createRequestVoteWithNewerTerm());
86 assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(actorContext, behaviorActor,
87 createRequestVoteReplyWithNewerTerm());
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.
97 public void testHandleAppendEntriesSenderTermLessThanReceiverTerm() throws Exception {
98 MockRaftActorContext context = createActorContext();
99 short payloadVersion = 5;
100 context.setPayloadVersion(payloadVersion);
102 // First set the receivers term to a high number (1000)
103 context.getTermInformation().update(1000, "test");
105 AppendEntries appendEntries = new AppendEntries(100, "leader-1", 0, 0, Collections.emptyList(), 101, -1,
108 behavior = createBehavior(context);
110 RaftState expected = behavior.state();
112 RaftActorBehavior raftBehavior = behavior.handleMessage(behaviorActor, appendEntries);
114 assertEquals("Raft state", expected, raftBehavior.state());
116 // Also expect an AppendEntriesReply to be sent where success is false
118 AppendEntriesReply reply = MessageCollectorActor.expectFirstMatching(
119 behaviorActor, AppendEntriesReply.class);
121 assertEquals("isSuccess", false, reply.isSuccess());
122 assertEquals("getPayloadVersion", payloadVersion, reply.getPayloadVersion());
127 public void testHandleAppendEntriesAddSameEntryToLog() throws Exception {
128 MockRaftActorContext context = createActorContext();
130 context.getTermInformation().update(2, "test");
132 // Prepare the receivers log
133 MockRaftActorContext.MockPayload payload = new MockRaftActorContext.MockPayload("zero");
134 setLastLogEntry(context, 2, 0, payload);
136 List<ReplicatedLogEntry> entries = new ArrayList<>();
137 entries.add(new SimpleReplicatedLogEntry(0, 2, payload));
139 final AppendEntries appendEntries = new AppendEntries(2, "leader-1", -1, -1, entries, 2, -1, (short)0);
141 behavior = createBehavior(context);
143 assertFalse("This test should be overridden when testing Candidate", behavior instanceof Candidate);
145 RaftState expected = behavior.state();
147 // Check that the behavior does not handle unknwon message
148 assertNull(behavior.handleMessage(behaviorActor, "unknown"));
150 RaftActorBehavior raftBehavior = behavior.handleMessage(behaviorActor, appendEntries);
152 assertEquals("Raft state", expected, raftBehavior.state());
154 assertEquals("ReplicatedLog size", 1, context.getReplicatedLog().size());
156 handleAppendEntriesAddSameEntryToLogReply(behaviorActor);
159 protected void handleAppendEntriesAddSameEntryToLogReply(TestActorRef<MessageCollectorActor> replyActor)
161 AppendEntriesReply reply = MessageCollectorActor.getFirstMatching(replyActor, AppendEntriesReply.class);
162 Assert.assertNull("Expected no AppendEntriesReply", reply);
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.
171 public void testHandleRequestVoteWhenSenderLogMoreUpToDate() {
172 MockRaftActorContext context = createActorContext();
174 behavior = createBehavior(context);
176 context.getTermInformation().update(1, "test");
178 behavior.handleMessage(behaviorActor, new RequestVote(context.getTermInformation().getCurrentTerm(),
179 "test", 10000, 999));
181 RequestVoteReply reply = MessageCollectorActor.expectFirstMatching(behaviorActor,
182 RequestVoteReply.class);
183 assertEquals("isVoteGranted", true, reply.isVoteGranted());
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.
192 public void testHandleRequestVoteWhenSenderLogLessUptoDate() {
193 MockRaftActorContext context = createActorContext();
195 behavior = createBehavior(context);
197 context.getTermInformation().update(1, "test");
200 setLastLogEntry(context, context.getTermInformation().getCurrentTerm(), index,
201 new MockRaftActorContext.MockPayload(""));
203 behavior.handleMessage(behaviorActor, new RequestVote(
204 context.getTermInformation().getCurrentTerm(), "test",
205 index - 1, context.getTermInformation().getCurrentTerm()));
207 RequestVoteReply reply = MessageCollectorActor.expectFirstMatching(behaviorActor,
208 RequestVoteReply.class);
209 assertEquals("isVoteGranted", false, reply.isVoteGranted());
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.
220 public void testHandleRequestVoteWhenSenderTermLessThanCurrentTerm() {
221 MockRaftActorContext context = createActorContext();
223 context.getTermInformation().update(1000, null);
225 behavior = createBehavior(context);
227 behavior.handleMessage(behaviorActor, new RequestVote(999, "test", 10000, 999));
229 RequestVoteReply reply = MessageCollectorActor.expectFirstMatching(behaviorActor,
230 RequestVoteReply.class);
231 assertEquals("isVoteGranted", false, reply.isVoteGranted());
235 public void testPerformSnapshot() {
236 MockRaftActorContext context = new MockRaftActorContext("test", getSystem(), behaviorActor);
237 AbstractRaftActorBehavior abstractBehavior = (AbstractRaftActorBehavior) createBehavior(context);
238 if (abstractBehavior instanceof Candidate) {
242 context.getTermInformation().update(1, "test");
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());
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());
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());
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());
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());
282 protected void assertStateChangesToFollowerWhenRaftRPCHasNewerTerm(MockRaftActorContext actorContext,
283 ActorRef actorRef, RaftRPC rpc) throws Exception {
285 Payload payload = new MockRaftActorContext.MockPayload("");
286 setLastLogEntry(actorContext, 1, 0, payload);
287 actorContext.getTermInformation().update(1, "test");
289 RaftActorBehavior origBehavior = createBehavior(actorContext);
290 RaftActorBehavior raftBehavior = origBehavior.handleMessage(actorRef, rpc);
292 assertEquals("New raft state", RaftState.Follower, raftBehavior.state());
293 assertEquals("New election term", rpc.getTerm(), actorContext.getTermInformation().getCurrentTerm());
295 origBehavior.close();
296 raftBehavior.close();
299 protected MockRaftActorContext.SimpleReplicatedLog setLastLogEntry(
300 MockRaftActorContext actorContext, long term, long index, Payload data) {
301 return setLastLogEntry(actorContext, new SimpleReplicatedLogEntry(index, term, data));
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);
313 protected abstract T createBehavior(RaftActorContext actorContext);
315 protected final T createBehavior(MockRaftActorContext actorContext) {
316 T ret = createBehavior((RaftActorContext)actorContext);
317 actorContext.setCurrentBehavior(ret);
321 protected RaftActorBehavior createBehavior() {
322 return createBehavior(createActorContext());
325 protected MockRaftActorContext createActorContext() {
326 return new MockRaftActorContext();
329 protected MockRaftActorContext createActorContext(ActorRef actor) {
330 return new MockRaftActorContext("test", getSystem(), actor);
333 protected AppendEntries createAppendEntriesWithNewerTerm() {
334 return new AppendEntries(100, "leader-1", 0, 0, Collections.emptyList(), 1, -1, (short)0);
337 protected AppendEntriesReply createAppendEntriesReplyWithNewerTerm() {
338 return new AppendEntriesReply("follower-1", 100, false, 100, 100, (short)0);
341 protected RequestVote createRequestVoteWithNewerTerm() {
342 return new RequestVote(100, "candidate-1", 10, 100);
345 protected RequestVoteReply createRequestVoteReplyWithNewerTerm() {
346 return new RequestVoteReply(100, false);
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);
359 protected void logStart(String name) {
360 LoggerFactory.getLogger(getClass()).info("Starting " + name);
363 protected RaftPolicy createRaftPolicy(final boolean automaticElectionsEnabled,
364 final boolean applyModificationToStateBeforeConsensus) {
365 return new RaftPolicy() {
367 public boolean automaticElectionsEnabled() {
368 return automaticElectionsEnabled;
372 public boolean applyModificationToStateBeforeConsensus() {
373 return applyModificationToStateBeforeConsensus;