Take snapshot after recovery on migrated messages
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / RecoveryIntegrationSingleNodeTest.java
1 /*
2  * Copyright (c) 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 package org.opendaylight.controller.cluster.raft;
9
10 import static org.junit.Assert.assertEquals;
11 import akka.actor.ActorRef;
12 import akka.persistence.SaveSnapshotSuccess;
13 import akka.testkit.TestActorRef;
14 import com.google.common.collect.ImmutableMap;
15 import com.google.common.collect.Lists;
16 import java.util.List;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.controller.cluster.raft.persisted.ApplyJournalEntries;
20 import org.opendaylight.controller.cluster.raft.utils.InMemoryJournal;
21 import org.opendaylight.controller.cluster.raft.utils.InMemorySnapshotStore;
22 import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Recovery Integration Test for single node
28  */
29 public class RecoveryIntegrationSingleNodeTest extends AbstractRaftActorIntegrationTest {
30
31     static final Logger LOG = LoggerFactory.getLogger(RecoveryIntegrationSingleNodeTest.class);
32
33     @Before
34     public void setup() {
35         leaderConfigParams = newLeaderConfigParams();
36     }
37
38
39     @Test
40     public void testJournalReplayAfterSnapshotWithSingleNode() throws Exception {
41
42         String persistenceId = factory.generateActorId("singleNode");
43         TestActorRef<AbstractRaftActorIntegrationTest.TestRaftActor> singleNodeActorRef = newTestRaftActor(persistenceId,
44                 ImmutableMap.<String, String>builder().build(), leaderConfigParams);
45
46         waitUntilLeader(singleNodeActorRef);
47
48         ActorRef singleNodeCollectorActor = singleNodeActorRef.underlyingActor().collectorActor();
49         RaftActorContext singleNodeContext = singleNodeActorRef.underlyingActor().getRaftActorContext();
50
51
52         MockRaftActorContext.MockPayload payload0 = sendPayloadData(singleNodeActorRef, "zero");
53         MockRaftActorContext.MockPayload payload1 = sendPayloadData(singleNodeActorRef, "one");
54         MockRaftActorContext.MockPayload payload2 = sendPayloadData(singleNodeActorRef, "two");
55
56         MessageCollectorActor.expectMatching(singleNodeCollectorActor, ApplyJournalEntries.class, 3);
57
58         // this should trigger a snapshot
59         MockRaftActorContext.MockPayload payload3 = sendPayloadData(singleNodeActorRef, "three");
60
61         MessageCollectorActor.expectMatching(singleNodeCollectorActor, ApplyJournalEntries.class, 4);
62
63         //add 2 more
64         MockRaftActorContext.MockPayload payload4 = sendPayloadData(singleNodeActorRef, "four");
65         MockRaftActorContext.MockPayload payload5 = sendPayloadData(singleNodeActorRef, "five");
66
67
68         // Wait for snapshot complete.
69         MessageCollectorActor.expectFirstMatching(singleNodeCollectorActor, SaveSnapshotSuccess.class);
70
71         MessageCollectorActor.expectMatching(singleNodeCollectorActor, ApplyJournalEntries.class, 6);
72
73         assertEquals("Last applied", 5, singleNodeContext.getLastApplied());
74
75         assertEquals("Incorrect State after snapshot success is received ",
76                 Lists.newArrayList(payload0, payload1, payload2, payload3, payload4, payload5), singleNodeActorRef.underlyingActor().getState());
77
78         // we get 2 log entries (4 and 5 indexes) and 3 ApplyJournalEntries (for 3, 4, and 5 indexes)
79         assertEquals(5, InMemoryJournal.get(persistenceId).size());
80
81         List<Snapshot> persistedSnapshots = InMemorySnapshotStore.getSnapshots(persistenceId, Snapshot.class);
82         assertEquals(1, persistedSnapshots.size());
83
84         List<Object> snapshottedState = (List<Object>)MockRaftActor.toObject(persistedSnapshots.get(0).getState());
85         assertEquals("Incorrect Snapshot", Lists.newArrayList(payload0, payload1, payload2, payload3), snapshottedState);
86
87         //recovery logic starts
88         killActor(singleNodeActorRef);
89
90         singleNodeActorRef = newTestRaftActor(persistenceId,
91                 ImmutableMap.<String, String>builder().build(), leaderConfigParams);
92
93         singleNodeActorRef.underlyingActor().waitForRecoveryComplete();
94
95         assertEquals("Incorrect State after Recovery ",
96                 Lists.newArrayList(payload0, payload1, payload2, payload3, payload4, payload5), singleNodeActorRef.underlyingActor().getState());
97
98     }
99 }

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