BUG-8858: remove sleeps from test driver
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / impl / FlappingSingletonService.java
index 2894ee35986e95ac102972efb926e341d0f8d90c..90ce618f3d14a403146e44a1c4ed07401eea2f17 100644 (file)
@@ -10,9 +10,8 @@ package org.opendaylight.controller.clustering.it.provider.impl;
 
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicLong;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
@@ -27,12 +26,10 @@ public class FlappingSingletonService implements ClusterSingletonService {
     private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER =
             ServiceGroupIdentifier.create("flapping-singleton-service");
 
-    private static final ScheduledExecutorService EXECUTOR = FinalizableScheduledExecutorService.newSingleThread();
-
     private final ClusterSingletonServiceProvider singletonServiceProvider;
     private final AtomicBoolean active = new AtomicBoolean(true);
 
-    private volatile long flapCount = 0;
+    private final AtomicLong flapCount = new AtomicLong();
     private volatile ClusterSingletonServiceRegistration registration;
 
     public FlappingSingletonService(final ClusterSingletonServiceProvider singletonServiceProvider) {
@@ -46,19 +43,16 @@ public class FlappingSingletonService implements ClusterSingletonService {
     @SuppressWarnings("checkstyle:IllegalCatch")
     public void instantiateServiceInstance() {
         LOG.debug("Instantiating flapping-singleton-service.");
-
-        // TODO direct registration/close seem to trigger a bug in singleton state transitions,
-        // remove the whole executor shenanigans after it's fixed.
-        EXECUTOR.submit(() -> {
-            try {
-                registration.close();
-                registration = null;
-            } catch (Exception e) {
-                LOG.warn("There was a problem closing flapping singleton service.", e);
-                setInactive();
-                flapCount = -flapCount;
-            }
-        });
+        try {
+            registration.close();
+            registration = null;
+        } catch (Exception e) {
+            LOG.warn("There was a problem closing flapping singleton service.", e);
+            setInactive();
+
+            final long count = flapCount.get();
+            flapCount.compareAndSet(count, -count);
+        }
     }
 
     @Override
@@ -66,22 +60,18 @@ public class FlappingSingletonService implements ClusterSingletonService {
     public ListenableFuture<Void> closeServiceInstance() {
         LOG.debug("Closing flapping-singleton-service, flapCount: {}", flapCount);
 
-        flapCount++;
+        flapCount.incrementAndGet();
         if (active.get()) {
-            // TODO direct registration/close seem to trigger a bug in singleton state transitions,
-            // remove  whole executor shenanigans after it's fixed.
-            // Needs to be delayed slightly otherwise it's triggered as well.
-            EXECUTOR.schedule(() -> {
-                LOG.debug("Running re-registration");
-                try {
-                    registration = singletonServiceProvider.registerClusterSingletonService(this);
-                } catch (RuntimeException e) {
-                    LOG.warn("There was a problem re-registering flapping singleton service.", e);
-                    setInactive();
-                    flapCount = -flapCount - 1;
-                }
-
-            }, 200, TimeUnit.MILLISECONDS);
+            LOG.debug("Running re-registration");
+            try {
+                registration = singletonServiceProvider.registerClusterSingletonService(this);
+            } catch (RuntimeException e) {
+                LOG.warn("There was a problem re-registering flapping singleton service.", e);
+                setInactive();
+
+                final long count = flapCount.get();
+                flapCount.compareAndSet(count, -count - 1);
+            }
         }
 
         return Futures.immediateFuture(null);
@@ -96,6 +86,6 @@ public class FlappingSingletonService implements ClusterSingletonService {
         LOG.debug("Setting flapping-singleton-service to inactive, flap-count: {}", flapCount);
 
         active.set(false);
-        return flapCount;
+        return flapCount.get();
     }
 }