Reduce output from DeadlockMonitor
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / DeadlockMonitor.java
index ba7ab7fcba924743c4ea579797f1dffbd117c7cb..9882b4662cce2b75519b509135fd17c961e12e9d 100644 (file)
@@ -1,22 +1,33 @@
+/*
+ * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.config.manager.impl;
 
+import java.util.Deque;
+import java.util.LinkedList;
+import java.util.concurrent.TimeUnit;
+import javax.annotation.Nullable;
+import javax.annotation.concurrent.GuardedBy;
 import org.opendaylight.controller.config.api.ModuleIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import javax.annotation.Nullable;
-import javax.annotation.concurrent.GuardedBy;
-import java.util.concurrent.TimeUnit;
-
 public class DeadlockMonitor implements AutoCloseable {
-    private static final Logger logger = LoggerFactory.getLogger(DeadlockMonitorRunnable.class);
+    private static final Logger LOG = LoggerFactory.getLogger(DeadlockMonitor.class);
 
     private static final long WARN_AFTER_MILLIS = 5000;
 
     private final TransactionIdentifier transactionIdentifier;
     private final DeadlockMonitorRunnable thread;
     @GuardedBy("this")
-    private ModuleIdentifierWithNanos moduleIdentifierWithNanos = new ModuleIdentifierWithNanos();
+    private final Deque<ModuleIdentifierWithNanos> moduleIdentifierWithNanosStack = new LinkedList<>();
+    @GuardedBy("this")
+    private ModuleIdentifierWithNanos top = ModuleIdentifierWithNanos.EMPTY;
 
     public DeadlockMonitor(TransactionIdentifier transactionIdentifier) {
         this.transactionIdentifier = transactionIdentifier;
@@ -25,7 +36,21 @@ public class DeadlockMonitor implements AutoCloseable {
     }
 
     public synchronized void setCurrentlyInstantiatedModule(ModuleIdentifier currentlyInstantiatedModule) {
-        this.moduleIdentifierWithNanos = new ModuleIdentifierWithNanos(currentlyInstantiatedModule);
+
+        boolean popping = currentlyInstantiatedModule == null;
+        if (popping) {
+            moduleIdentifierWithNanosStack.pop();
+            if (moduleIdentifierWithNanosStack.isEmpty()) {
+                top = ModuleIdentifierWithNanos.EMPTY;
+            } else {
+                top = moduleIdentifierWithNanosStack.peekLast();
+            }
+        } else {
+            ModuleIdentifierWithNanos current = new ModuleIdentifierWithNanos(currentlyInstantiatedModule);
+            moduleIdentifierWithNanosStack.push(current);
+            top = current;
+        }
+        LOG.trace("setCurrentlyInstantiatedModule {}, top {}", currentlyInstantiatedModule, top);
     }
 
     public boolean isAlive() {
@@ -52,24 +77,28 @@ public class DeadlockMonitor implements AutoCloseable {
         public void run() {
             ModuleIdentifierWithNanos old = new ModuleIdentifierWithNanos(); // null moduleId
             while (this.isInterrupted() == false) {
-                ModuleIdentifierWithNanos copy = new ModuleIdentifierWithNanos(DeadlockMonitor.this.moduleIdentifierWithNanos);
-                if (old.moduleIdentifier == null) {
+                ModuleIdentifierWithNanos copy;
+                synchronized(this) {
+                    copy = new ModuleIdentifierWithNanos(DeadlockMonitor.this.top);
+                }
+
+                if (old.moduleIdentifier == null || old.equals(copy) == false) {
                     // started
                     old = copy;
-                } else if (old.moduleIdentifier != null && old.equals(copy)) {
+                } else {
                     // is the getInstance() running longer than WARN_AFTER_MILLIS ?
                     long runningTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - copy.nanoTime);
                     if (runningTime > WARN_AFTER_MILLIS) {
-                        logger.warn("{} did not finish after {} ms", copy.moduleIdentifier, runningTime);
+                        LOG.warn("{} did not finish after {} ms", copy.moduleIdentifier, runningTime);
                     }
                 }
                 try {
-                    sleep(1000);
+                    sleep(WARN_AFTER_MILLIS);
                 } catch (InterruptedException e) {
                     interrupt();
                 }
             }
-            logger.trace("Exiting {}", this);
+            LOG.trace("Exiting {}", this);
         }
 
         @Override
@@ -78,14 +107,18 @@ public class DeadlockMonitor implements AutoCloseable {
         }
     }
 
-    private class ModuleIdentifierWithNanos {
+
+
+
+    private static class ModuleIdentifierWithNanos {
+        private static ModuleIdentifierWithNanos EMPTY = new ModuleIdentifierWithNanos();
         @Nullable
         private final ModuleIdentifier moduleIdentifier;
+
         private final long nanoTime;
 
         private ModuleIdentifierWithNanos() {
-            moduleIdentifier = null;
-            nanoTime = System.nanoTime();
+            this((ModuleIdentifier)null);
         }
 
         private ModuleIdentifierWithNanos(ModuleIdentifier moduleIdentifier) {
@@ -100,14 +133,21 @@ public class DeadlockMonitor implements AutoCloseable {
 
         @Override
         public boolean equals(Object o) {
-            if (this == o) return true;
-            if (o == null || getClass() != o.getClass()) return false;
+            if (this == o) {
+                return true;
+            }
+            if (o == null || getClass() != o.getClass()) {
+                return false;
+            }
 
             ModuleIdentifierWithNanos that = (ModuleIdentifierWithNanos) o;
 
-            if (nanoTime != that.nanoTime) return false;
-            if (moduleIdentifier != null ? !moduleIdentifier.equals(that.moduleIdentifier) : that.moduleIdentifier != null)
+            if (nanoTime != that.nanoTime) {
                 return false;
+            }
+            if (moduleIdentifier != null ? !moduleIdentifier.equals(that.moduleIdentifier) : that.moduleIdentifier != null) {
+                return false;
+            }
 
             return true;
         }
@@ -118,5 +158,12 @@ public class DeadlockMonitor implements AutoCloseable {
             result = 31 * result + (int) (nanoTime ^ (nanoTime >>> 32));
             return result;
         }
+
+        @Override
+        public String toString() {
+            return "ModuleIdentifierWithNanos{" +
+                    moduleIdentifier +
+                    '}';
+        }
     }
 }