Prevent partial init in DatastoreSnapshotRestore 81/29881/1
authorGary Wu <gary.wu1@huawei.com>
Wed, 18 Nov 2015 19:23:06 +0000 (11:23 -0800)
committerGary Wu <gary.wu1@huawei.com>
Wed, 18 Nov 2015 19:23:06 +0000 (11:23 -0800)
The config subsystem should only push one config
at a time, but in case it doesn't, synchronize
DatastoreSnapshotRestore.initialize() to prevent
partial initialization in the event of concurrent
calls to getAndRemove().

Change-Id: Ie614e8b2045d86ea46b55609bf5cde9e6597b086
Signed-off-by: Gary Wu <gary.wu1@huawei.com>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DatastoreSnapshotRestore.java

index bed2389..7ef05e2 100644 (file)
@@ -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,7 +34,6 @@ public class DatastoreSnapshotRestore {
 
     private final String restoreDirectoryPath;
     private final Map<String, DatastoreSnapshot> datastoreSnapshots = new ConcurrentHashMap<>();
-    private final AtomicBoolean initialized = new AtomicBoolean();
 
     public static void createInstance(String restoreDirectoryPath) {
         instance.compareAndSet(null, new DatastoreSnapshotRestore(restoreDirectoryPath));
@@ -54,10 +52,9 @@ public class DatastoreSnapshotRestore {
         this.restoreDirectoryPath = Preconditions.checkNotNull(restoreDirectoryPath);
     }
 
-    private void initialize() {
-        if(!initialized.compareAndSet(false, true)) {
-            return;
-        }
+    // sychronize this method so that, in case of concurrent access to getAndRemove(),
+    // no one ends up with partially initialized data
+    private synchronized void initialize() {
 
         File restoreDirectoryFile = new File(restoreDirectoryPath);
 

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.