X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fraft%2Futils%2FInMemorySnapshotStore.java;h=662a063788fd522412d778eff71000bfcdaa9507;hb=refs%2Fchanges%2F32%2F83832%2F8;hp=bf130897354d12b9a89d75730f7353ba9135526c;hpb=922e20f44a002cf6eec1790f47e1bb68096ef8d9;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/utils/InMemorySnapshotStore.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/utils/InMemorySnapshotStore.java index bf13089735..662a063788 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/utils/InMemorySnapshotStore.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/utils/InMemorySnapshotStore.java @@ -5,22 +5,20 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.controller.cluster.raft.utils; import akka.dispatch.Futures; -import akka.japi.Option; import akka.persistence.SelectedSnapshot; import akka.persistence.SnapshotMetadata; import akka.persistence.SnapshotSelectionCriteria; import akka.persistence.snapshot.japi.SnapshotStore; -import com.google.common.collect.Lists; import com.google.common.util.concurrent.Uninterruptibles; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -37,17 +35,12 @@ public class InMemorySnapshotStore extends SnapshotStore { static final Logger LOG = LoggerFactory.getLogger(InMemorySnapshotStore.class); + private static final Map SNAPSHOT_SAVED_LATCHES = new ConcurrentHashMap<>(); + private static final Map SNAPSHOT_DELETED_LATCHES = new ConcurrentHashMap<>(); private static Map> snapshots = new ConcurrentHashMap<>(); - private static final Map snapshotSavedLatches = new ConcurrentHashMap<>(); - private static final Map snapshotDeletedLatches = new ConcurrentHashMap<>(); - - public static void addSnapshot(String persistentId, Object snapshot) { - List snapshotList = snapshots.get(persistentId); - if(snapshotList == null) { - snapshotList = new ArrayList<>(); - snapshots.put(persistentId, snapshotList); - } + public static void addSnapshot(final String persistentId, final Object snapshot) { + List snapshotList = snapshots.computeIfAbsent(persistentId, k -> new ArrayList<>()); synchronized (snapshotList) { snapshotList.add(new StoredSnapshot(new SnapshotMetadata(persistentId, snapshotList.size(), @@ -56,17 +49,17 @@ public class InMemorySnapshotStore extends SnapshotStore { } @SuppressWarnings("unchecked") - public static List getSnapshots(String persistentId, Class type) { + public static List getSnapshots(final String persistentId, final Class type) { List stored = snapshots.get(persistentId); - if(stored == null) { + if (stored == null) { return Collections.emptyList(); } List retList; synchronized (stored) { - retList = Lists.newArrayListWithCapacity(stored.size()); - for(StoredSnapshot s: stored) { - if(type.isInstance(s.data)) { + retList = new ArrayList<>(stored.size()); + for (StoredSnapshot s: stored) { + if (type.isInstance(s.data)) { retList.add((T) s.data); } } @@ -75,75 +68,79 @@ public class InMemorySnapshotStore extends SnapshotStore { return retList; } + public static void clearSnapshotsFor(final String persistenceId) { + snapshots.remove(persistenceId); + } + public static void clear() { snapshots.clear(); } - public static void addSnapshotSavedLatch(String persistenceId) { - snapshotSavedLatches.put(persistenceId, new CountDownLatch(1)); + public static void addSnapshotSavedLatch(final String persistenceId) { + SNAPSHOT_SAVED_LATCHES.put(persistenceId, new CountDownLatch(1)); } - public static void addSnapshotDeletedLatch(String persistenceId) { - snapshotDeletedLatches.put(persistenceId, new CountDownLatch(1)); + public static void addSnapshotDeletedLatch(final String persistenceId) { + SNAPSHOT_DELETED_LATCHES.put(persistenceId, new CountDownLatch(1)); } - public static T waitForSavedSnapshot(String persistenceId, Class type) { - if(!Uninterruptibles.awaitUninterruptibly(snapshotSavedLatches.get(persistenceId), 5, TimeUnit.SECONDS)) { + public static T waitForSavedSnapshot(final String persistenceId, final Class type) { + if (!Uninterruptibles.awaitUninterruptibly(SNAPSHOT_SAVED_LATCHES.get(persistenceId), 5, TimeUnit.SECONDS)) { throw new AssertionError("Snapshot was not saved"); } return getSnapshots(persistenceId, type).get(0); } - public static void waitForDeletedSnapshot(String persistenceId) { - if(!Uninterruptibles.awaitUninterruptibly(snapshotDeletedLatches.get(persistenceId), 5, TimeUnit.SECONDS)) { + public static void waitForDeletedSnapshot(final String persistenceId) { + if (!Uninterruptibles.awaitUninterruptibly(SNAPSHOT_DELETED_LATCHES.get(persistenceId), 5, TimeUnit.SECONDS)) { throw new AssertionError("Snapshot was not deleted"); } } @Override - public Future> doLoadAsync(String persistenceId, - SnapshotSelectionCriteria snapshotSelectionCriteria) { + public Future> doLoadAsync(final String persistenceId, + final SnapshotSelectionCriteria snapshotSelectionCriteria) { List snapshotList = snapshots.get(persistenceId); - if(snapshotList == null){ - return Futures.successful(Option.none()); + if (snapshotList == null) { + return Futures.successful(Optional.empty()); } - synchronized(snapshotList) { - for(int i = snapshotList.size() - 1; i >= 0; i--) { + synchronized (snapshotList) { + for (int i = snapshotList.size() - 1; i >= 0; i--) { StoredSnapshot snapshot = snapshotList.get(i); - if(matches(snapshot, snapshotSelectionCriteria)) { - return Futures.successful(Option.some(new SelectedSnapshot(snapshot.metadata, + if (matches(snapshot, snapshotSelectionCriteria)) { + return Futures.successful(Optional.of(new SelectedSnapshot(snapshot.metadata, snapshot.data))); } } } - return Futures.successful(Option.none()); + return Futures.successful(Optional.empty()); } - private static boolean matches(StoredSnapshot snapshot, SnapshotSelectionCriteria criteria) { - return snapshot.metadata.sequenceNr() <= criteria.maxSequenceNr() && - snapshot.metadata.timestamp() <= criteria.maxTimestamp(); + private static boolean matches(final StoredSnapshot snapshot, final SnapshotSelectionCriteria criteria) { + return snapshot.metadata.sequenceNr() <= criteria.maxSequenceNr() + && snapshot.metadata.timestamp() <= criteria.maxTimestamp(); } @Override - public Future doSaveAsync(SnapshotMetadata snapshotMetadata, Object o) { + public Future doSaveAsync(final SnapshotMetadata snapshotMetadata, final Object obj) { List snapshotList = snapshots.get(snapshotMetadata.persistenceId()); LOG.trace("doSaveAsync: persistentId {}: sequenceNr: {}: timestamp {}: {}", snapshotMetadata.persistenceId(), - snapshotMetadata.sequenceNr(), snapshotMetadata.timestamp(), o); + snapshotMetadata.sequenceNr(), snapshotMetadata.timestamp(), obj); - if(snapshotList == null){ + if (snapshotList == null) { snapshotList = new ArrayList<>(); snapshots.put(snapshotMetadata.persistenceId(), snapshotList); } synchronized (snapshotList) { - snapshotList.add(new StoredSnapshot(snapshotMetadata, o)); + snapshotList.add(new StoredSnapshot(snapshotMetadata, obj)); } - CountDownLatch latch = snapshotSavedLatches.get(snapshotMetadata.persistenceId()); - if(latch != null) { + CountDownLatch latch = SNAPSHOT_SAVED_LATCHES.get(snapshotMetadata.persistenceId()); + if (latch != null) { latch.countDown(); } @@ -151,43 +148,38 @@ public class InMemorySnapshotStore extends SnapshotStore { } @Override - public void onSaved(SnapshotMetadata snapshotMetadata) throws Exception { - } - - @Override - public void doDelete(SnapshotMetadata snapshotMetadata) throws Exception { - List snapshotList = snapshots.get(snapshotMetadata.persistenceId()); + public Future doDeleteAsync(final SnapshotMetadata metadata) { + List snapshotList = snapshots.get(metadata.persistenceId()); - if(snapshotList == null){ - return; - } - - synchronized (snapshotList) { - for(int i=0;i doDeleteAsync(final String persistenceId, final SnapshotSelectionCriteria criteria) { + LOG.trace("doDelete: persistentId {}: maxSequenceNr: {}: maxTimestamp {}", persistenceId, + criteria.maxSequenceNr(), criteria.maxTimestamp()); - List snapshotList = snapshots.get(persistentId); - if(snapshotList != null){ + List snapshotList = snapshots.get(persistenceId); + if (snapshotList != null) { synchronized (snapshotList) { Iterator iter = snapshotList.iterator(); - while(iter.hasNext()) { - StoredSnapshot s = iter.next(); - if(matches(s, snapshotSelectionCriteria)) { + while (iter.hasNext()) { + StoredSnapshot stored = iter.next(); + if (matches(stored, criteria)) { LOG.trace("Deleting snapshot for sequenceNr: {}, timestamp: {}: {}", - s.metadata.sequenceNr(), s.metadata.timestamp(), s.data); + stored.metadata.sequenceNr(), stored.metadata.timestamp(), stored.data); iter.remove(); } @@ -195,17 +187,19 @@ public class InMemorySnapshotStore extends SnapshotStore { } } - CountDownLatch latch = snapshotDeletedLatches.get(persistentId); - if(latch != null) { + CountDownLatch latch = SNAPSHOT_DELETED_LATCHES.get(persistenceId); + if (latch != null) { latch.countDown(); } + + return Futures.successful(null); } - private static class StoredSnapshot { + private static final class StoredSnapshot { private final SnapshotMetadata metadata; private final Object data; - private StoredSnapshot(SnapshotMetadata metadata, Object data) { + StoredSnapshot(final SnapshotMetadata metadata, final Object data) { this.metadata = metadata; this.data = data; }