Bug 7521: Add custom local snapshot store
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / persistence / LocalSnapshotStoreTest.java
1 /*
2  * Copyright (c) 2017 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.persistence;
9
10 import static java.lang.Boolean.FALSE;
11 import static java.lang.Boolean.TRUE;
12 import static org.junit.Assert.assertEquals;
13 import static org.opendaylight.controller.cluster.persistence.LocalSnapshotStoreSpecTest.SNAPSHOT_DIR;
14 import static org.opendaylight.controller.cluster.persistence.LocalSnapshotStoreSpecTest.cleanSnapshotDir;
15 import static org.opendaylight.controller.cluster.persistence.LocalSnapshotStoreSpecTest.createSnapshotDir;
16
17 import akka.actor.ActorRef;
18 import akka.actor.ActorSystem;
19 import akka.actor.ExtendedActorSystem;
20 import akka.persistence.Persistence;
21 import akka.persistence.SelectedSnapshot;
22 import akka.persistence.SnapshotMetadata;
23 import akka.persistence.SnapshotProtocol;
24 import akka.persistence.SnapshotProtocol.LoadSnapshot;
25 import akka.persistence.SnapshotProtocol.LoadSnapshotFailed;
26 import akka.persistence.SnapshotProtocol.LoadSnapshotResult;
27 import akka.persistence.SnapshotSelectionCriteria;
28 import akka.persistence.serialization.Snapshot;
29 import akka.persistence.serialization.SnapshotSerializer;
30 import akka.testkit.JavaTestKit;
31 import com.google.common.base.Throwables;
32 import com.typesafe.config.ConfigFactory;
33 import java.io.File;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
36 import org.apache.commons.io.FileUtils;
37 import org.apache.commons.lang.SerializationUtils;
38 import org.junit.After;
39 import org.junit.AfterClass;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import org.mockito.MockitoAnnotations;
44 import scala.Option;
45
46 /**
47  * Unit tests for LocalSnapshotStore. These are in addition to LocalSnapshotStoreSpecTest to cover a few cases
48  * that SnapshotStoreSpec doesn't.
49  *
50  * @author Thomas Pantelis
51  */
52 public class LocalSnapshotStoreTest {
53     private static final String PERSISTENCE_ID = "member-1-shard-default-config";
54
55     private static ActorSystem system;
56     private static ActorRef snapshotStore;
57
58     @BeforeClass
59     public static void staticSetup() {
60         createSnapshotDir();
61
62         system = ActorSystem.create("test", ConfigFactory.load("LocalSnapshotStoreTest.conf"));
63         snapshotStore = system.registerExtension(Persistence.lookup()).snapshotStoreFor(null);
64     }
65
66     @AfterClass
67     public static void staticCleanup() {
68         FileUtils.deleteQuietly(SNAPSHOT_DIR);
69         JavaTestKit.shutdownActorSystem(system);
70     }
71
72     @Before
73     public void setup() {
74         MockitoAnnotations.initMocks(this);
75         cleanSnapshotDir();
76     }
77
78     @After
79     public void cleanup() {
80         cleanSnapshotDir();
81     }
82
83     @Test
84     public void testDoLoadAsync() throws IOException {
85         createSnapshotFile(PERSISTENCE_ID, "one", 0, 1000);
86         createSnapshotFile(PERSISTENCE_ID, "two", 1, 2000);
87         createSnapshotFile(PERSISTENCE_ID, "three", 1, 3000);
88
89         createSnapshotFile("member-1-shard-default-oper", "foo", 0, 1000);
90         createSnapshotFile("member-1-shard-toaster-oper", "foo", 0, 1000);
91         new File(SNAPSHOT_DIR, "other").createNewFile();
92         new File(SNAPSHOT_DIR, "other-1485349217290").createNewFile();
93
94         SnapshotMetadata metadata3 = new SnapshotMetadata(PERSISTENCE_ID, 1, 3000);
95
96         JavaTestKit probe = new JavaTestKit(system);
97         snapshotStore.tell(new LoadSnapshot(PERSISTENCE_ID,
98                 SnapshotSelectionCriteria.latest(), Long.MAX_VALUE), probe.getRef());
99         LoadSnapshotResult result = probe.expectMsgClass(LoadSnapshotResult.class);
100         Option<SelectedSnapshot> possibleSnapshot = result.snapshot();
101
102         assertEquals("SelectedSnapshot present", TRUE, possibleSnapshot.nonEmpty());
103         assertEquals("SelectedSnapshot metadata", metadata3, possibleSnapshot.get().metadata());
104         assertEquals("SelectedSnapshot snapshot", "three", possibleSnapshot.get().snapshot());
105     }
106
107     @Test
108     public void testDoLoadAsyncWithNoSnapshots() throws IOException {
109         JavaTestKit probe = new JavaTestKit(system);
110         snapshotStore.tell(new LoadSnapshot(PERSISTENCE_ID,
111                 SnapshotSelectionCriteria.latest(), Long.MAX_VALUE), probe.getRef());
112         LoadSnapshotResult result = probe.expectMsgClass(LoadSnapshotResult.class);
113         Option<SelectedSnapshot> possibleSnapshot = result.snapshot();
114
115         assertEquals("SelectedSnapshot present", FALSE, possibleSnapshot.nonEmpty());
116     }
117
118     @Test
119     public void testDoLoadAsyncWithRetry() throws IOException  {
120         createSnapshotFile(PERSISTENCE_ID, "one", 0, 1000);
121         createSnapshotFile(PERSISTENCE_ID, null, 1, 2000);
122
123         SnapshotMetadata metadata = new SnapshotMetadata(PERSISTENCE_ID, 0, 1000);
124
125         JavaTestKit probe = new JavaTestKit(system);
126         snapshotStore.tell(new LoadSnapshot(PERSISTENCE_ID,
127                 SnapshotSelectionCriteria.latest(), Long.MAX_VALUE), probe.getRef());
128         LoadSnapshotResult result = probe.expectMsgClass(LoadSnapshotResult.class);
129         Option<SelectedSnapshot> possibleSnapshot = result.snapshot();
130
131         assertEquals("SelectedSnapshot present", TRUE, possibleSnapshot.nonEmpty());
132         assertEquals("SelectedSnapshot metadata", metadata, possibleSnapshot.get().metadata());
133         assertEquals("SelectedSnapshot snapshot", "one", possibleSnapshot.get().snapshot());
134     }
135
136     @Test(expected = IOException.class)
137     public void testDoLoadAsyncWithFailure() throws IOException {
138         createSnapshotFile(PERSISTENCE_ID, null, 1, 2000);
139
140         JavaTestKit probe = new JavaTestKit(system);
141         snapshotStore.tell(new SnapshotProtocol.LoadSnapshot(PERSISTENCE_ID,
142                 SnapshotSelectionCriteria.latest(), Long.MAX_VALUE), probe.getRef());
143         LoadSnapshotFailed failed = probe.expectMsgClass(LoadSnapshotFailed.class);
144         Throwables.propagateIfInstanceOf(failed.cause(), IOException.class);
145     }
146
147     @Test
148     public void testDoLoadAsyncWithAkkaSerializedSnapshot() throws IOException {
149         SnapshotSerializer snapshotSerializer = new SnapshotSerializer((ExtendedActorSystem) system);
150
151         String name = toSnapshotName(PERSISTENCE_ID, 1, 1000);
152         try (FileOutputStream fos = new FileOutputStream(new File(SNAPSHOT_DIR, name))) {
153             fos.write(snapshotSerializer.toBinary(new Snapshot("one")));
154         }
155
156         SnapshotMetadata metadata = new SnapshotMetadata(PERSISTENCE_ID, 1, 1000);
157
158         JavaTestKit probe = new JavaTestKit(system);
159         snapshotStore.tell(new LoadSnapshot(PERSISTENCE_ID,
160                 SnapshotSelectionCriteria.latest(), Long.MAX_VALUE), probe.getRef());
161         LoadSnapshotResult result = probe.expectMsgClass(LoadSnapshotResult.class);
162         Option<SelectedSnapshot> possibleSnapshot = result.snapshot();
163
164         assertEquals("SelectedSnapshot present", TRUE, possibleSnapshot.nonEmpty());
165         assertEquals("SelectedSnapshot metadata", metadata, possibleSnapshot.get().metadata());
166         assertEquals("SelectedSnapshot snapshot", "one", possibleSnapshot.get().snapshot());
167     }
168
169     private void createSnapshotFile(String persistenceId, String payload, int seqNr, int timestamp) throws IOException {
170         String name = toSnapshotName(persistenceId, seqNr, timestamp);
171         try (FileOutputStream fos = new FileOutputStream(new File(SNAPSHOT_DIR, name))) {
172             if (payload != null) {
173                 fos.write(SerializationUtils.serialize(payload));
174             }
175         }
176     }
177
178     private static String toSnapshotName(String persistenceId, int seqNr, int timestamp) {
179         return "snapshot-" + persistenceId + "-" + seqNr + "-" + timestamp;
180     }
181 }