BUG 2187 - Removal of old snapshots in ShardManager
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / utils / InMemorySnapshotStore.java
index 130c707e3a1f06d6b2c51b112eb4ba316e69d0ae..81b2d7844fc6546cee60d38343fde1390f237187 100644 (file)
@@ -39,6 +39,7 @@ public class InMemorySnapshotStore extends SnapshotStore {
 
     private static Map<String, List<StoredSnapshot>> snapshots = new ConcurrentHashMap<>();
     private static final Map<String, CountDownLatch> snapshotSavedLatches = new ConcurrentHashMap<>();
+    private static final Map<String, CountDownLatch> snapshotDeletedLatches = new ConcurrentHashMap<>();
 
     public static void addSnapshot(String persistentId, Object snapshot) {
         List<StoredSnapshot> snapshotList = snapshots.get(persistentId);
@@ -82,6 +83,10 @@ public class InMemorySnapshotStore extends SnapshotStore {
         snapshotSavedLatches.put(persistenceId, new CountDownLatch(1));
     }
 
+    public static void addSnapshotDeletedLatch(String persistenceId) {
+        snapshotDeletedLatches.put(persistenceId, new CountDownLatch(1));
+    }
+
     public static <T> T waitForSavedSnapshot(String persistenceId, Class<T> type) {
         if(!Uninterruptibles.awaitUninterruptibly(snapshotSavedLatches.get(persistenceId), 5, TimeUnit.SECONDS)) {
             throw new AssertionError("Snapshot was not saved");
@@ -90,6 +95,12 @@ public class InMemorySnapshotStore extends SnapshotStore {
         return getSnapshots(persistenceId, type).get(0);
     }
 
+    public static void waitForDeletedSnapshot(String persistenceId) {
+        if(!Uninterruptibles.awaitUninterruptibly(snapshotDeletedLatches.get(persistenceId), 5, TimeUnit.SECONDS)) {
+            throw new AssertionError("Snapshot was not deleted");
+        }
+    }
+
     @Override
     public Future<Option<SelectedSnapshot>> doLoadAsync(String persistenceId,
             SnapshotSelectionCriteria snapshotSelectionCriteria) {
@@ -120,6 +131,9 @@ public class InMemorySnapshotStore extends SnapshotStore {
     public Future<Void> doSaveAsync(SnapshotMetadata snapshotMetadata, Object o) {
         List<StoredSnapshot> snapshotList = snapshots.get(snapshotMetadata.persistenceId());
 
+        LOG.trace("doSaveAsync: persistentId {}: sequenceNr: {}: timestamp {}: {}", snapshotMetadata.persistenceId(),
+                snapshotMetadata.sequenceNr(), snapshotMetadata.timestamp(), o);
+
         if(snapshotList == null){
             snapshotList = new ArrayList<>();
             snapshots.put(snapshotMetadata.persistenceId(), snapshotList);
@@ -166,22 +180,25 @@ public class InMemorySnapshotStore extends SnapshotStore {
                 snapshotSelectionCriteria.maxSequenceNr(), snapshotSelectionCriteria.maxTimestamp());
 
         List<StoredSnapshot> snapshotList = snapshots.get(persistentId);
-        if(snapshotList == null){
-            return;
-        }
-
-        synchronized (snapshotList) {
-            Iterator<StoredSnapshot> iter = snapshotList.iterator();
-            while(iter.hasNext()) {
-                StoredSnapshot s = iter.next();
-                if(matches(s, snapshotSelectionCriteria)) {
-                    LOG.trace("Deleting snapshot for sequenceNr: {}, timestamp: {}",
-                            s.metadata.sequenceNr(), s.metadata.timestamp());
-
-                    iter.remove();
+        if(snapshotList != null){
+            synchronized (snapshotList) {
+                Iterator<StoredSnapshot> iter = snapshotList.iterator();
+                while(iter.hasNext()) {
+                    StoredSnapshot s = iter.next();
+                    if(matches(s, snapshotSelectionCriteria)) {
+                        LOG.trace("Deleting snapshot for sequenceNr: {}, timestamp: {}: {}",
+                                s.metadata.sequenceNr(), s.metadata.timestamp(), s.data);
+
+                        iter.remove();
+                    }
                 }
             }
         }
+
+        CountDownLatch latch = snapshotDeletedLatches.get(persistentId);
+        if(latch != null) {
+            latch.countDown();
+        }
     }
 
     private static class StoredSnapshot {