Bug 9060: TracingBroker with transaction-debug-context-enabled
[controller.git] / opendaylight / md-sal / mdsal-trace / dom-impl / src / main / java / org / opendaylight / controller / md / sal / trace / closetracker / impl / CloseTrackedRegistry.java
1 /*
2  * Copyright (c) 2017 Red Hat, 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 package org.opendaylight.controller.md.sal.trace.closetracker.impl;
9
10 import java.util.Set;
11 import java.util.concurrent.ConcurrentSkipListSet;
12 import javax.annotation.concurrent.ThreadSafe;
13
14 /**
15  * Registry of {@link CloseTracked} instances.
16  *
17  * @author Michael Vorburger.ch
18  */
19 @ThreadSafe
20 public class CloseTrackedRegistry<T extends CloseTracked<T>> {
21
22     // unused OK for now, at least we'll be able to see this in HPROF heap dumps and know what is which
23     private final @SuppressWarnings("unused") Object anchor;
24     private final @SuppressWarnings("unused") String createDescription;
25
26     private final Set<CloseTracked<T>> tracked = new ConcurrentSkipListSet<>(
27         (o1, o2) -> o1.getObjectCreated().compareTo(o2.getObjectCreated()));
28
29     private final boolean isDebugContextEnabled;
30
31     /**
32      * Constructor.
33      *
34      * @param anchor
35      *            object where this registry is stored in, used for human output in
36      *            logging and other output
37      * @param createDescription
38      *            description of creator of instances of this registry, typically
39      *            e.g. name of method in the anchor class
40      * @param isDebugContextEnabled
41      *            whether or not the call stack should be preserved; this is (of
42      *            course) an expensive operation, and should only be used during
43      *            troubleshooting
44      */
45     public CloseTrackedRegistry(Object anchor, String createDescription, boolean isDebugContextEnabled) {
46         this.anchor = anchor;
47         this.createDescription = createDescription;
48         this.isDebugContextEnabled = isDebugContextEnabled;
49     }
50
51     public boolean isDebugContextEnabled() {
52         return isDebugContextEnabled;
53     }
54
55     // package protected, not public; only CloseTrackedTrait invokes this
56     void add(CloseTracked<T> closeTracked) {
57         tracked.add(closeTracked);
58     }
59
60     // package protected, not public; only CloseTrackedTrait invokes this
61     void remove(CloseTracked<T> closeTracked) {
62         tracked.remove(closeTracked);
63     }
64
65     // TODO Later add methods to dump & query what's not closed, by creation time, incl. creation stack trace
66
67     // TODO we could even support add/close of Object instead of CloseTracked, by creating a wrapper?
68
69 }