1 package org.opendaylight.controller.config.manager.impl;
3 import org.opendaylight.controller.config.api.ModuleIdentifier;
4 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory;
7 import javax.annotation.Nullable;
8 import javax.annotation.concurrent.GuardedBy;
9 import java.util.concurrent.TimeUnit;
11 public class DeadlockMonitor implements AutoCloseable {
12 private static final Logger logger = LoggerFactory.getLogger(DeadlockMonitorRunnable.class);
14 private static final long WARN_AFTER_MILLIS = 5000;
16 private final TransactionIdentifier transactionIdentifier;
17 private final DeadlockMonitorRunnable thread;
19 private ModuleIdentifierWithNanos moduleIdentifierWithNanos = new ModuleIdentifierWithNanos();
21 public DeadlockMonitor(TransactionIdentifier transactionIdentifier) {
22 this.transactionIdentifier = transactionIdentifier;
23 thread = new DeadlockMonitorRunnable();
27 public synchronized void setCurrentlyInstantiatedModule(ModuleIdentifier currentlyInstantiatedModule) {
28 this.moduleIdentifierWithNanos = new ModuleIdentifierWithNanos(currentlyInstantiatedModule);
31 public boolean isAlive() {
32 return thread.isAlive();
41 public String toString() {
42 return "DeadlockMonitor{" + transactionIdentifier + '}';
45 private class DeadlockMonitorRunnable extends Thread {
47 private DeadlockMonitorRunnable() {
48 super(DeadlockMonitor.this.toString());
53 ModuleIdentifierWithNanos old = new ModuleIdentifierWithNanos(); // null moduleId
54 while (this.isInterrupted() == false) {
55 ModuleIdentifierWithNanos copy = new ModuleIdentifierWithNanos(DeadlockMonitor.this.moduleIdentifierWithNanos);
56 if (old.moduleIdentifier == null) {
59 } else if (old.moduleIdentifier != null && old.equals(copy)) {
60 // is the getInstance() running longer than WARN_AFTER_MILLIS ?
61 long runningTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - copy.nanoTime);
62 if (runningTime > WARN_AFTER_MILLIS) {
63 logger.warn("{} did not finish after {} ms", copy.moduleIdentifier, runningTime);
68 } catch (InterruptedException e) {
72 logger.trace("Exiting {}", this);
76 public String toString() {
77 return "DeadLockMonitorRunnable{" + transactionIdentifier + "}";
81 private class ModuleIdentifierWithNanos {
83 private final ModuleIdentifier moduleIdentifier;
84 private final long nanoTime;
86 private ModuleIdentifierWithNanos() {
87 moduleIdentifier = null;
88 nanoTime = System.nanoTime();
91 private ModuleIdentifierWithNanos(ModuleIdentifier moduleIdentifier) {
92 this.moduleIdentifier = moduleIdentifier;
93 nanoTime = System.nanoTime();
96 private ModuleIdentifierWithNanos(ModuleIdentifierWithNanos copy) {
97 moduleIdentifier = copy.moduleIdentifier;
98 nanoTime = copy.nanoTime;
102 public boolean equals(Object o) {
103 if (this == o) return true;
104 if (o == null || getClass() != o.getClass()) return false;
106 ModuleIdentifierWithNanos that = (ModuleIdentifierWithNanos) o;
108 if (nanoTime != that.nanoTime) return false;
109 if (moduleIdentifier != null ? !moduleIdentifier.equals(that.moduleIdentifier) : that.moduleIdentifier != null)
116 public int hashCode() {
117 int result = moduleIdentifier != null ? moduleIdentifier.hashCode() : 0;
118 result = 31 * result + (int) (nanoTime ^ (nanoTime >>> 32));