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.japi.Option;
13 import akka.persistence.SelectedSnapshot;
14 import akka.persistence.SnapshotMetadata;
15 import akka.persistence.SnapshotSelectionCriteria;
16 import akka.persistence.snapshot.japi.SnapshotStore;
17 import com.google.common.collect.Lists;
18 import com.google.common.util.concurrent.Uninterruptibles;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Iterator;
22 import java.util.List;
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 Map<String, List<StoredSnapshot>> snapshots = new ConcurrentHashMap<>();
41 private static final Map<String, CountDownLatch> snapshotSavedLatches = new ConcurrentHashMap<>();
42 private static final Map<String, CountDownLatch> snapshotDeletedLatches = 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 snapshotSavedLatches.put(persistenceId, new CountDownLatch(1));
86 public static void addSnapshotDeletedLatch(String persistenceId) {
87 snapshotDeletedLatches.put(persistenceId, new CountDownLatch(1));
90 public static <T> T waitForSavedSnapshot(String persistenceId, Class<T> type) {
91 if(!Uninterruptibles.awaitUninterruptibly(snapshotSavedLatches.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(snapshotDeletedLatches.get(persistenceId), 5, TimeUnit.SECONDS)) {
100 throw new AssertionError("Snapshot was not deleted");
105 public Future<Option<SelectedSnapshot>> doLoadAsync(String persistenceId,
106 SnapshotSelectionCriteria snapshotSelectionCriteria) {
107 List<StoredSnapshot> snapshotList = snapshots.get(persistenceId);
108 if(snapshotList == null){
109 return Futures.successful(Option.<SelectedSnapshot>none());
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(Option.some(new SelectedSnapshot(snapshot.metadata,
122 return Futures.successful(Option.<SelectedSnapshot>none());
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 o) {
132 List<StoredSnapshot> snapshotList = snapshots.get(snapshotMetadata.persistenceId());
134 LOG.trace("doSaveAsync: persistentId {}: sequenceNr: {}: timestamp {}: {}", snapshotMetadata.persistenceId(),
135 snapshotMetadata.sequenceNr(), snapshotMetadata.timestamp(), o);
137 if(snapshotList == null){
138 snapshotList = new ArrayList<>();
139 snapshots.put(snapshotMetadata.persistenceId(), snapshotList);
141 synchronized (snapshotList) {
142 snapshotList.add(new StoredSnapshot(snapshotMetadata, o));
145 CountDownLatch latch = snapshotSavedLatches.get(snapshotMetadata.persistenceId());
150 return Futures.successful(null);
154 public void onSaved(SnapshotMetadata snapshotMetadata) throws Exception {
158 public void doDelete(SnapshotMetadata snapshotMetadata) throws Exception {
159 List<StoredSnapshot> snapshotList = snapshots.get(snapshotMetadata.persistenceId());
161 if(snapshotList == null){
165 synchronized (snapshotList) {
166 for(int i=0;i<snapshotList.size(); i++){
167 StoredSnapshot snapshot = snapshotList.get(i);
168 if(snapshotMetadata.equals(snapshot.metadata)){
169 snapshotList.remove(i);
177 public void doDelete(String persistentId, SnapshotSelectionCriteria snapshotSelectionCriteria)
179 LOG.trace("doDelete: persistentId {}: maxSequenceNr: {}: maxTimestamp {}", persistentId,
180 snapshotSelectionCriteria.maxSequenceNr(), snapshotSelectionCriteria.maxTimestamp());
182 List<StoredSnapshot> snapshotList = snapshots.get(persistentId);
183 if(snapshotList != null){
184 synchronized (snapshotList) {
185 Iterator<StoredSnapshot> iter = snapshotList.iterator();
186 while(iter.hasNext()) {
187 StoredSnapshot s = iter.next();
188 if(matches(s, snapshotSelectionCriteria)) {
189 LOG.trace("Deleting snapshot for sequenceNr: {}, timestamp: {}: {}",
190 s.metadata.sequenceNr(), s.metadata.timestamp(), s.data);
198 CountDownLatch latch = snapshotDeletedLatches.get(persistentId);
204 private static class StoredSnapshot {
205 private final SnapshotMetadata metadata;
206 private final Object data;
208 private StoredSnapshot(SnapshotMetadata metadata, Object data) {
209 this.metadata = metadata;