cad98b30f4edd9a3f2689fd43bfa0a6127316103
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / DeadlockMonitor.java
1 /*
2  * Copyright (c) 2014, 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.config.manager.impl;
10
11 import java.util.Deque;
12 import java.util.LinkedList;
13 import java.util.concurrent.TimeUnit;
14 import javax.annotation.Nullable;
15 import javax.annotation.concurrent.GuardedBy;
16 import org.opendaylight.controller.config.api.ModuleIdentifier;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public class DeadlockMonitor implements AutoCloseable {
21     private static final Logger LOG = LoggerFactory.getLogger(DeadlockMonitor.class);
22
23     private static final long WARN_AFTER_MILLIS = 5000;
24
25     private final TransactionIdentifier transactionIdentifier;
26     private final DeadlockMonitorRunnable thread;
27     @GuardedBy("this")
28     private final Deque<ModuleIdentifierWithNanos> moduleIdentifierWithNanosStack = new LinkedList<>();
29     @GuardedBy("this")
30     private ModuleIdentifierWithNanos top = ModuleIdentifierWithNanos.empty;
31
32     public DeadlockMonitor(final TransactionIdentifier transactionIdentifier) {
33         this.transactionIdentifier = transactionIdentifier;
34         thread = new DeadlockMonitorRunnable();
35         thread.start();
36     }
37
38     public synchronized void setCurrentlyInstantiatedModule(final ModuleIdentifier currentlyInstantiatedModule) {
39
40         boolean popping = currentlyInstantiatedModule == null;
41         if (popping) {
42             moduleIdentifierWithNanosStack.pop();
43             if (moduleIdentifierWithNanosStack.isEmpty()) {
44                 top = ModuleIdentifierWithNanos.empty;
45             } else {
46                 top = moduleIdentifierWithNanosStack.peekLast();
47             }
48         } else {
49             ModuleIdentifierWithNanos current = new ModuleIdentifierWithNanos(currentlyInstantiatedModule);
50             moduleIdentifierWithNanosStack.push(current);
51             top = current;
52         }
53         LOG.trace("setCurrentlyInstantiatedModule {}, top {}", currentlyInstantiatedModule, top);
54     }
55
56     public boolean isAlive() {
57         return thread.isAlive();
58     }
59
60     @Override
61     public void close() {
62         thread.interrupt();
63     }
64
65     @Override
66     public String toString() {
67         return "DeadlockMonitor{" + transactionIdentifier + '}';
68     }
69
70     private class DeadlockMonitorRunnable extends Thread {
71
72         private DeadlockMonitorRunnable() {
73             super(DeadlockMonitor.this.toString());
74         }
75
76         @Override
77         public void run() {
78             // null moduleId
79             ModuleIdentifierWithNanos old = new ModuleIdentifierWithNanos();
80             while (!this.isInterrupted()) {
81                 ModuleIdentifierWithNanos copy;
82                 synchronized (this) {
83                     copy = new ModuleIdentifierWithNanos(DeadlockMonitor.this.top);
84                 }
85
86                 if (old.moduleIdentifier == null || !old.equals(copy)) {
87                     // started
88                     old = copy;
89                 } else {
90                     // is the getInstance() running longer than WARN_AFTER_MILLIS ?
91                     long runningTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - copy.nanoTime);
92                     if (runningTime > WARN_AFTER_MILLIS) {
93                         LOG.warn("{} did not finish after {} ms", copy.moduleIdentifier, runningTime);
94                     }
95                 }
96                 try {
97                     sleep(WARN_AFTER_MILLIS);
98                 } catch (final InterruptedException e) {
99                     interrupt();
100                 }
101             }
102             LOG.trace("Exiting {}", this);
103         }
104
105         @Override
106         public String toString() {
107             return "DeadLockMonitorRunnable{" + transactionIdentifier + "}";
108         }
109     }
110
111     private static class ModuleIdentifierWithNanos {
112         private static ModuleIdentifierWithNanos empty = new ModuleIdentifierWithNanos();
113         @Nullable
114         private final ModuleIdentifier moduleIdentifier;
115
116         private final long nanoTime;
117
118         private ModuleIdentifierWithNanos() {
119             this((ModuleIdentifier) null);
120         }
121
122         private ModuleIdentifierWithNanos(final ModuleIdentifier moduleIdentifier) {
123             this.moduleIdentifier = moduleIdentifier;
124             nanoTime = System.nanoTime();
125         }
126
127         private ModuleIdentifierWithNanos(final ModuleIdentifierWithNanos copy) {
128             moduleIdentifier = copy.moduleIdentifier;
129             nanoTime = copy.nanoTime;
130         }
131
132         @Override
133         public boolean equals(final Object object) {
134             if (this == object) {
135                 return true;
136             }
137             if (object == null || getClass() != object.getClass()) {
138                 return false;
139             }
140
141             ModuleIdentifierWithNanos that = (ModuleIdentifierWithNanos) object;
142
143             if (nanoTime != that.nanoTime) {
144                 return false;
145             }
146             if (moduleIdentifier != null ? !moduleIdentifier.equals(that.moduleIdentifier)
147                     : that.moduleIdentifier != null) {
148                 return false;
149             }
150
151             return true;
152         }
153
154         @Override
155         public int hashCode() {
156             int result = moduleIdentifier != null ? moduleIdentifier.hashCode() : 0;
157             result = 31 * result + (int) (nanoTime ^ nanoTime >>> 32);
158             return result;
159         }
160
161         @Override
162         public String toString() {
163             return "ModuleIdentifierWithNanos{" + moduleIdentifier + '}';
164         }
165     }
166 }