Improve config system shutdown
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / BlankTransactionServiceTracker.java
index 2e2bf969a93203c3beb2f7a126e2f3f04b57bb17..fa46cae5167925c4bf7e459d21a7dc150a1ff92f 100644 (file)
@@ -8,6 +8,9 @@
 package org.opendaylight.controller.config.manager.impl.osgi;
 
 import com.google.common.annotations.VisibleForTesting;
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 import javax.management.ObjectName;
 import org.opendaylight.controller.config.api.ConflictingVersionException;
 import org.opendaylight.controller.config.api.ValidationException;
@@ -30,12 +33,14 @@ public class BlankTransactionServiceTracker implements ServiceTrackerCustomizer<
     public static final int DEFAULT_MAX_ATTEMPTS = 10;
 
     private final BlankTransaction blankTransaction;
-    private int maxAttempts;
+    private final ExecutorService txExecutor;
+    private final int maxAttempts;
 
     public BlankTransactionServiceTracker(final ConfigRegistryImpl configRegistry) {
         this(new BlankTransaction() {
             @Override
-            public CommitStatus hit() throws ValidationException, ConflictingVersionException {
+            public CommitStatus hit()
+                    throws ValidationException, ConflictingVersionException {
                 ObjectName tx = configRegistry.beginConfig(true);
                 return configRegistry.commitConfig(tx);
             }
@@ -43,22 +48,29 @@ public class BlankTransactionServiceTracker implements ServiceTrackerCustomizer<
     }
 
     public BlankTransactionServiceTracker(final BlankTransaction blankTransaction) {
-        this(blankTransaction, DEFAULT_MAX_ATTEMPTS);
+        this(blankTransaction, DEFAULT_MAX_ATTEMPTS, Executors.newSingleThreadExecutor(new ThreadFactoryBuilder()
+                .setNameFormat("config-blank-txn-%d").build()));
     }
 
     @VisibleForTesting
-    BlankTransactionServiceTracker(final BlankTransaction blankTx, final int maxAttempts) {
+    BlankTransactionServiceTracker(final BlankTransaction blankTx, final int maxAttempts,
+            final ExecutorService txExecutor) {
         this.blankTransaction = blankTx;
         this.maxAttempts = maxAttempts;
+        this.txExecutor = txExecutor;
     }
 
     @Override
     public Object addingService(ServiceReference<ModuleFactory> moduleFactoryServiceReference) {
-        blankTransaction();
+        blankTransactionAsync();
         return null;
     }
 
-    synchronized void blankTransaction() {
+    private void blankTransactionAsync() {
+        txExecutor.execute(() -> { blankTransactionSync(); });
+    }
+
+    void blankTransactionSync() {
         // race condition check: config-persister might push new configuration while server is starting up.
         ConflictingVersionException lastException = null;
         for (int i = 0; i < maxAttempts; i++) {
@@ -73,25 +85,26 @@ public class BlankTransactionServiceTracker implements ServiceTrackerCustomizer<
                     Thread.sleep(1000);
                 } catch (InterruptedException interruptedException) {
                     Thread.currentThread().interrupt();
-                    throw new IllegalStateException(interruptedException);
+                    LOG.debug("blankTransactionSync was interrupted");
+                    return;
                 }
             } catch (ValidationException e) {
                 LOG.error("Validation exception while running blank transaction indicates programming error", e);
-                throw new RuntimeException("Validation exception while running blank transaction indicates programming error", e);
             }
         }
-        throw new RuntimeException("Maximal number of attempts reached and still cannot get optimistic lock from " +
-                "config manager",lastException);
+
+        LOG.error("Maximal number of attempts reached and still cannot get optimistic lock from config manager",
+                lastException);
     }
 
     @Override
     public void modifiedService(ServiceReference <ModuleFactory> moduleFactoryServiceReference, Object o) {
-        blankTransaction();
+        blankTransactionAsync();
     }
 
     @Override
     public void removedService(ServiceReference<ModuleFactory> moduleFactoryServiceReference, Object o) {
-        blankTransaction();
+        blankTransactionAsync();
     }
 
     @VisibleForTesting