2 * Copyright (c) 2017 Brocade Communications 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
8 package org.opendaylight.controller.cluster.persistence;
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;
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;
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;
47 * Unit tests for LocalSnapshotStore. These are in addition to LocalSnapshotStoreSpecTest to cover a few cases
48 * that SnapshotStoreSpec doesn't.
50 * @author Thomas Pantelis
52 public class LocalSnapshotStoreTest {
53 private static final String PERSISTENCE_ID = "member-1-shard-default-config";
55 private static ActorSystem system;
56 private static ActorRef snapshotStore;
59 public static void staticSetup() {
62 system = ActorSystem.create("test", ConfigFactory.load("LocalSnapshotStoreTest.conf"));
63 snapshotStore = system.registerExtension(Persistence.lookup()).snapshotStoreFor(null);
67 public static void staticCleanup() {
68 FileUtils.deleteQuietly(SNAPSHOT_DIR);
69 JavaTestKit.shutdownActorSystem(system);
74 MockitoAnnotations.initMocks(this);
79 public void cleanup() {
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);
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();
94 SnapshotMetadata metadata3 = new SnapshotMetadata(PERSISTENCE_ID, 1, 3000);
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();
102 assertEquals("SelectedSnapshot present", TRUE, possibleSnapshot.nonEmpty());
103 assertEquals("SelectedSnapshot metadata", metadata3, possibleSnapshot.get().metadata());
104 assertEquals("SelectedSnapshot snapshot", "three", possibleSnapshot.get().snapshot());
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();
115 assertEquals("SelectedSnapshot present", FALSE, possibleSnapshot.nonEmpty());
119 public void testDoLoadAsyncWithRetry() throws IOException {
120 createSnapshotFile(PERSISTENCE_ID, "one", 0, 1000);
121 createSnapshotFile(PERSISTENCE_ID, null, 1, 2000);
123 SnapshotMetadata metadata = new SnapshotMetadata(PERSISTENCE_ID, 0, 1000);
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();
131 assertEquals("SelectedSnapshot present", TRUE, possibleSnapshot.nonEmpty());
132 assertEquals("SelectedSnapshot metadata", metadata, possibleSnapshot.get().metadata());
133 assertEquals("SelectedSnapshot snapshot", "one", possibleSnapshot.get().snapshot());
136 @Test(expected = IOException.class)
137 public void testDoLoadAsyncWithFailure() throws IOException {
138 createSnapshotFile(PERSISTENCE_ID, null, 1, 2000);
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);
148 public void testDoLoadAsyncWithAkkaSerializedSnapshot() throws IOException {
149 SnapshotSerializer snapshotSerializer = new SnapshotSerializer((ExtendedActorSystem) system);
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")));
156 SnapshotMetadata metadata = new SnapshotMetadata(PERSISTENCE_ID, 1, 1000);
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();
164 assertEquals("SelectedSnapshot present", TRUE, possibleSnapshot.nonEmpty());
165 assertEquals("SelectedSnapshot metadata", metadata, possibleSnapshot.get().metadata());
166 assertEquals("SelectedSnapshot snapshot", "one", possibleSnapshot.get().snapshot());
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));
178 private static String toSnapshotName(String persistenceId, int seqNr, int timestamp) {
179 return "snapshot-" + persistenceId + "-" + seqNr + "-" + timestamp;