X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FDatastoreSnapshotRestore.java;h=e736ddae3136ae43a8b7163e4a4941990e8184dd;hp=bed23895d3e5dbdd9681631c494855130f0d5a9a;hb=c3607fd866e5cfde0d4806820981fe95ad29392c;hpb=288a70d15252b3c5fafd202fe7935563f05da9c8 diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DatastoreSnapshotRestore.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DatastoreSnapshotRestore.java index bed23895d3..e736ddae31 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DatastoreSnapshotRestore.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DatastoreSnapshotRestore.java @@ -15,7 +15,6 @@ import java.io.InputStream; import java.io.ObjectInputStream; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshot; import org.opendaylight.controller.cluster.datastore.messages.DatastoreSnapshotList; @@ -35,41 +34,33 @@ public class DatastoreSnapshotRestore { private final String restoreDirectoryPath; private final Map datastoreSnapshots = new ConcurrentHashMap<>(); - private final AtomicBoolean initialized = new AtomicBoolean(); - public static void createInstance(String restoreDirectoryPath) { + public static DatastoreSnapshotRestore instance(String restoreDirectoryPath) { instance.compareAndSet(null, new DatastoreSnapshotRestore(restoreDirectoryPath)); - } - - public static void removeInstance() { - instance.set(null); - } - - public static DatastoreSnapshotRestore instance() { - DatastoreSnapshotRestore localInstance = instance.get(); - return Preconditions.checkNotNull(localInstance, "DatastoreSnapshotRestore instance was not created"); + return instance.get(); } private DatastoreSnapshotRestore(String restoreDirectoryPath) { this.restoreDirectoryPath = Preconditions.checkNotNull(restoreDirectoryPath); } - private void initialize() { - if(!initialized.compareAndSet(false, true)) { - return; - } + // synchronize this method so that, in case of concurrent access to getAndRemove(), + // no one ends up with partially initialized data + @SuppressWarnings("checkstyle:IllegalCatch") + private synchronized void initialize() { File restoreDirectoryFile = new File(restoreDirectoryPath); String[] files = restoreDirectoryFile.list(); - if(files == null || files.length == 0) { + if (files == null || files.length == 0) { LOG.debug("Restore directory {} does not exist or is empty", restoreDirectoryFile); return; } - if(files.length > 1) { - LOG.error("Found {} files in clustered datastore restore directory {} - expected 1. No restore will be attempted", - files.length, restoreDirectoryFile); + if (files.length > 1) { + LOG.error( + "Found {} files in clustered datastore restore directory {} - expected 1. No restore will be attempted", + files.length, restoreDirectoryFile); return; } @@ -77,24 +68,25 @@ public class DatastoreSnapshotRestore { LOG.info("Clustered datastore will be restored from file {}", restoreFile); - try(FileInputStream fis = new FileInputStream(restoreFile)) { + try (FileInputStream fis = new FileInputStream(restoreFile)) { DatastoreSnapshotList snapshots = deserialize(fis); LOG.debug("Deserialized {} snapshots", snapshots.size()); - for(DatastoreSnapshot snapshot: snapshots) { + for (DatastoreSnapshot snapshot: snapshots) { datastoreSnapshots.put(snapshot.getType(), snapshot); } - } catch (Exception e) { + } catch (ClassNotFoundException | IOException e) { LOG.error("Error reading clustered datastore restore file {}", restoreFile, e); } finally { - if(!restoreFile.delete()) { + if (!restoreFile.delete()) { LOG.error("Could not delete clustered datastore restore file {}", restoreFile); } } } - private DatastoreSnapshotList deserialize(InputStream inputStream) throws IOException, ClassNotFoundException { - try(ObjectInputStream ois = new ObjectInputStream(inputStream)) { + private static DatastoreSnapshotList deserialize(InputStream inputStream) + throws IOException, ClassNotFoundException { + try (ObjectInputStream ois = new ObjectInputStream(inputStream)) { return (DatastoreSnapshotList) ois.readObject(); } }