2 * Copyright (c) 2015 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
9 package org.opendaylight.controller.cluster.raft.utils;
11 import akka.dispatch.Futures;
12 import akka.persistence.SelectedSnapshot;
13 import akka.persistence.SnapshotMetadata;
14 import akka.persistence.SnapshotSelectionCriteria;
15 import akka.persistence.snapshot.japi.SnapshotStore;
16 import com.google.common.collect.Lists;
17 import com.google.common.util.concurrent.Uninterruptibles;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.Iterator;
21 import java.util.List;
23 import java.util.Optional;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.concurrent.CountDownLatch;
26 import java.util.concurrent.TimeUnit;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import scala.concurrent.Future;
32 * An akka SnapshotStore implementation that stores data in memory. This is intended for testing.
34 * @author Thomas Pantelis
36 public class InMemorySnapshotStore extends SnapshotStore {
38 static final Logger LOG = LoggerFactory.getLogger(InMemorySnapshotStore.class);
40 private static final Map<String, CountDownLatch> SNAPSHOT_SAVED_LATCHES = new ConcurrentHashMap<>();
41 private static final Map<String, CountDownLatch> SNAPSHOT_DELETED_LATCHES = new ConcurrentHashMap<>();
42 private static Map<String, List<StoredSnapshot>> snapshots = new ConcurrentHashMap<>();
44 public static void addSnapshot(String persistentId, Object snapshot) {
45 List<StoredSnapshot> snapshotList = snapshots.get(persistentId);
47 if (snapshotList == null) {
48 snapshotList = new ArrayList<>();
49 snapshots.put(persistentId, snapshotList);
52 synchronized (snapshotList) {
53 snapshotList.add(new StoredSnapshot(new SnapshotMetadata(persistentId, snapshotList.size(),
54 System.currentTimeMillis()), snapshot));
58 @SuppressWarnings("unchecked")
59 public static <T> List<T> getSnapshots(String persistentId, Class<T> type) {
60 List<StoredSnapshot> stored = snapshots.get(persistentId);
62 return Collections.emptyList();
66 synchronized (stored) {
67 retList = Lists.newArrayListWithCapacity(stored.size());
68 for (StoredSnapshot s: stored) {
69 if (type.isInstance(s.data)) {
70 retList.add((T) s.data);
78 public static void clear() {
82 public static void addSnapshotSavedLatch(String persistenceId) {
83 SNAPSHOT_SAVED_LATCHES.put(persistenceId, new CountDownLatch(1));
86 public static void addSnapshotDeletedLatch(String persistenceId) {
87 SNAPSHOT_DELETED_LATCHES.put(persistenceId, new CountDownLatch(1));
90 public static <T> T waitForSavedSnapshot(String persistenceId, Class<T> type) {
91 if (!Uninterruptibles.awaitUninterruptibly(SNAPSHOT_SAVED_LATCHES.get(persistenceId), 5, TimeUnit.SECONDS)) {
92 throw new AssertionError("Snapshot was not saved");
95 return getSnapshots(persistenceId, type).get(0);
98 public static void waitForDeletedSnapshot(String persistenceId) {
99 if (!Uninterruptibles.awaitUninterruptibly(SNAPSHOT_DELETED_LATCHES.get(persistenceId), 5, TimeUnit.SECONDS)) {
100 throw new AssertionError("Snapshot was not deleted");
105 public Future<Optional<SelectedSnapshot>> doLoadAsync(String persistenceId,
106 SnapshotSelectionCriteria snapshotSelectionCriteria) {
107 List<StoredSnapshot> snapshotList = snapshots.get(persistenceId);
108 if (snapshotList == null) {
109 return Futures.successful(Optional.<SelectedSnapshot>empty());
112 synchronized (snapshotList) {
113 for (int i = snapshotList.size() - 1; i >= 0; i--) {
114 StoredSnapshot snapshot = snapshotList.get(i);
115 if (matches(snapshot, snapshotSelectionCriteria)) {
116 return Futures.successful(Optional.of(new SelectedSnapshot(snapshot.metadata,
122 return Futures.successful(Optional.<SelectedSnapshot>empty());
125 private static boolean matches(StoredSnapshot snapshot, SnapshotSelectionCriteria criteria) {
126 return snapshot.metadata.sequenceNr() <= criteria.maxSequenceNr()
127 && snapshot.metadata.timestamp() <= criteria.maxTimestamp();
131 public Future<Void> doSaveAsync(SnapshotMetadata snapshotMetadata, Object obj) {
132 List<StoredSnapshot> snapshotList = snapshots.get(snapshotMetadata.persistenceId());
134 LOG.trace("doSaveAsync: persistentId {}: sequenceNr: {}: timestamp {}: {}", snapshotMetadata.persistenceId(),
135 snapshotMetadata.sequenceNr(), snapshotMetadata.timestamp(), obj);
137 if (snapshotList == null) {
138 snapshotList = new ArrayList<>();
139 snapshots.put(snapshotMetadata.persistenceId(), snapshotList);
141 synchronized (snapshotList) {
142 snapshotList.add(new StoredSnapshot(snapshotMetadata, obj));
145 CountDownLatch latch = SNAPSHOT_SAVED_LATCHES.get(snapshotMetadata.persistenceId());
150 return Futures.successful(null);
154 public Future<Void> doDeleteAsync(SnapshotMetadata metadata) {
155 List<StoredSnapshot> snapshotList = snapshots.get(metadata.persistenceId());
157 if (snapshotList != null) {
158 synchronized (snapshotList) {
159 for (int i = 0; i < snapshotList.size(); i++) {
160 StoredSnapshot snapshot = snapshotList.get(i);
161 if (metadata.equals(snapshot.metadata)) {
162 snapshotList.remove(i);
169 return Futures.successful(null);
173 public Future<Void> doDeleteAsync(String persistenceId, SnapshotSelectionCriteria criteria) {
174 LOG.trace("doDelete: persistentId {}: maxSequenceNr: {}: maxTimestamp {}", persistenceId,
175 criteria.maxSequenceNr(), criteria.maxTimestamp());
177 List<StoredSnapshot> snapshotList = snapshots.get(persistenceId);
178 if (snapshotList != null) {
179 synchronized (snapshotList) {
180 Iterator<StoredSnapshot> iter = snapshotList.iterator();
181 while (iter.hasNext()) {
182 StoredSnapshot stored = iter.next();
183 if (matches(stored, criteria)) {
184 LOG.trace("Deleting snapshot for sequenceNr: {}, timestamp: {}: {}",
185 stored.metadata.sequenceNr(), stored.metadata.timestamp(), stored.data);
193 CountDownLatch latch = SNAPSHOT_DELETED_LATCHES.get(persistenceId);
198 return Futures.successful(null);
201 private static class StoredSnapshot {
202 private final SnapshotMetadata metadata;
203 private final Object data;
205 private StoredSnapshot(SnapshotMetadata metadata, Object data) {
206 this.metadata = metadata;