2 * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.trace.dom.impl;
10 import java.io.PrintStream;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
15 import java.util.Objects;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
20 import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataTreeChangeListener;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataBrokerExtension;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
25 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
29 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
30 import org.opendaylight.controller.md.sal.trace.api.TracingDOMDataBroker;
31 import org.opendaylight.controller.md.sal.trace.closetracker.impl.CloseTracked;
32 import org.opendaylight.controller.md.sal.trace.closetracker.impl.CloseTrackedRegistry;
33 import org.opendaylight.controller.md.sal.trace.closetracker.impl.CloseTrackedRegistryReportEntry;
34 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsaltrace.rev160908.Config;
36 import org.opendaylight.yangtools.concepts.ListenerRegistration;
37 import org.opendaylight.yangtools.yang.binding.DataObject;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 @SuppressWarnings("checkstyle:JavadocStyle")
44 //...because otherwise it whines about the elements in the @code block even though it's completely valid Javadoc
47 * TracingBroker logs "write" operations and listener registrations to the md-sal. It logs the instance identifier path,
48 * the objects themselves, as well as the stack trace of the call invoking the registration or write operation.
49 * It works by operating as a "bump on the stack" between the application and actual DataBroker, intercepting write
50 * and registration calls and writing to the log.
52 * <p>In addition, it (optionally) can also keep track of the stack trace of all new transaction allocations
53 * (including TransactionChains, and transactions created in turn from them), in order to detect and report leaks
54 * from transactions which were not closed.
57 * TracingBroker is designed to be easy to use. In fact, for bundles using Blueprint to inject their DataBroker
58 * TracingBroker can be used without modifying your code at all in two simple steps:
61 * Simply add the dependency "mdsaltrace-features" to
66 * <groupId>org.opendaylight.controller</groupId>
67 * <artifactId>features-mdsal-trace</artifactId>
68 * <version>1.7.0-SNAPSHOT</version>
69 * <classifier>features</classifier>
71 * <scope>runtime</scope>
77 * Then just "feature:install odl-mdsal-trace" before you install your "real" feature(s) and you're done.
78 * Beware that with Karaf 4 due to <a href="https://bugs.opendaylight.org/show_bug.cgi?id=9068">Bug 9068</a>
79 * you'll probably have to use feature:install's --no-auto-refresh flag when installing your "real" feature.
82 * This works because the mdsaltrace-impl bundle registers its service implementing DOMDataBroker with a higher
83 * rank than sal-binding-broker. As such, any OSGi service lookup for DataBroker will receive the TracingBroker.
85 * <h1>Avoiding log bloat:</h1>
86 * TracingBroker can be configured to only print registrations or write ops pertaining to certain subtrees of the
87 * md-sal. This can be done in the code via the methods of this class or via a config file. TracingBroker uses a more
88 * convenient but non-standard representation of the instance identifiers. Each instance identifier segment's
89 * class.getSimpleName() is used separated by a '/'.
91 * <h1>Known issues</h1>
94 * Filtering by paths. For some registrations the codec that converts back from the DOM to binding paths is
95 * busted. As such, an aproximated path is used in the output. For now it is recommended not to use
96 * watchRegistrations and allow all registrations to be logged.
101 public class TracingBroker implements TracingDOMDataBroker {
103 static final Logger LOG = LoggerFactory.getLogger(TracingBroker.class);
105 private static final int STACK_TRACE_FIRST_RELEVANT_FRAME = 2;
107 private final BindingNormalizedNodeSerializer codec;
108 private final DOMDataBroker delegate;
109 private final List<Watch> registrationWatches = new ArrayList<>();
110 private final List<Watch> writeWatches = new ArrayList<>();
112 private final boolean isDebugging;
113 private final CloseTrackedRegistry<TracingTransactionChain> transactionChainsRegistry;
114 private final CloseTrackedRegistry<TracingReadOnlyTransaction> readOnlyTransactionsRegistry;
115 private final CloseTrackedRegistry<TracingWriteTransaction> writeTransactionsRegistry;
116 private final CloseTrackedRegistry<TracingReadWriteTransaction> readWriteTransactionsRegistry;
118 private class Watch {
119 final String iidString;
120 final LogicalDatastoreType store;
122 Watch(String iidString, LogicalDatastoreType storeOrNull) {
123 this.store = storeOrNull;
124 this.iidString = iidString;
127 private String toIidCompString(YangInstanceIdentifier iid) {
128 StringBuilder builder = new StringBuilder();
129 toPathString(iid, builder);
131 return builder.toString();
134 private boolean isParent(String parent, String child) {
135 int parentOffset = 0;
136 if (parent.length() > 0 && parent.charAt(0) == '<') {
137 parentOffset = parent.indexOf('>') + 1;
141 if (child.length() > 0 && child.charAt(0) == '<') {
142 childOffset = child.indexOf('>') + 1;
145 return child.startsWith(parent.substring(parentOffset), childOffset);
148 @SuppressWarnings("checkstyle:hiddenField")
149 public boolean subtreesOverlap(YangInstanceIdentifier iid, LogicalDatastoreType store) {
150 if (this.store != null && !this.store.equals(store)) {
154 String otherIidString = toIidCompString(iid);
155 return isParent(iidString, otherIidString) || isParent(otherIidString, iidString);
158 @SuppressWarnings("checkstyle:hiddenField")
159 public boolean eventIsOfInterest(YangInstanceIdentifier iid, LogicalDatastoreType store) {
160 if (this.store != null && !this.store.equals(store)) {
164 return isParent(iidString, toPathString(iid));
168 public TracingBroker(DOMDataBroker delegate, Config config, BindingNormalizedNodeSerializer codec) {
169 this.delegate = Objects.requireNonNull(delegate);
170 this.codec = Objects.requireNonNull(codec);
173 if (config.isTransactionDebugContextEnabled() != null) {
174 this.isDebugging = config.isTransactionDebugContextEnabled();
176 this.isDebugging = false;
178 final String db = "DataBroker";
179 this.transactionChainsRegistry = new CloseTrackedRegistry<>(db, "createTransactionChain()", isDebugging);
180 this.readOnlyTransactionsRegistry = new CloseTrackedRegistry<>(db, "newReadOnlyTransaction()", isDebugging);
181 this.writeTransactionsRegistry = new CloseTrackedRegistry<>(db, "newWriteOnlyTransaction()", isDebugging);
182 this.readWriteTransactionsRegistry = new CloseTrackedRegistry<>(db, "newReadWriteTransaction()", isDebugging);
185 private void configure(Config config) {
186 registrationWatches.clear();
187 List<String> paths = config.getRegistrationWatches();
189 for (String path : paths) {
190 watchRegistrations(path, null);
194 writeWatches.clear();
195 paths = config.getWriteWatches();
197 for (String path : paths) {
198 watchWrites(path, null);
204 * Log registrations to this subtree of the md-sal.
205 * @param iidString the iid path of the root of the subtree
206 * @param store Which LogicalDataStore? or null for both
208 public void watchRegistrations(String iidString, LogicalDatastoreType store) {
209 LOG.info("Watching registrations to {} in {}", iidString, store);
210 registrationWatches.add(new Watch(iidString, store));
214 * Log writes to this subtree of the md-sal.
215 * @param iidString the iid path of the root of the subtree
216 * @param store Which LogicalDataStore? or null for both
218 public void watchWrites(String iidString, LogicalDatastoreType store) {
219 LOG.info("Watching writes to {} in {}", iidString, store);
220 Watch watch = new Watch(iidString, store);
221 writeWatches.add(watch);
224 private boolean isRegistrationWatched(YangInstanceIdentifier iid, LogicalDatastoreType store) {
225 if (registrationWatches.isEmpty()) {
229 for (Watch regInterest : registrationWatches) {
230 if (regInterest.subtreesOverlap(iid, store)) {
238 boolean isWriteWatched(YangInstanceIdentifier iid, LogicalDatastoreType store) {
239 if (writeWatches.isEmpty()) {
243 for (Watch watch : writeWatches) {
244 if (watch.eventIsOfInterest(iid, store)) {
252 static void toPathString(InstanceIdentifier<? extends DataObject> iid, StringBuilder builder) {
253 for (InstanceIdentifier.PathArgument pathArg : iid.getPathArguments()) {
254 builder.append('/').append(pathArg.getType().getSimpleName());
258 String toPathString(YangInstanceIdentifier yiid) {
259 StringBuilder sb = new StringBuilder();
260 toPathString(yiid, sb);
261 return sb.toString();
265 private void toPathString(YangInstanceIdentifier yiid, StringBuilder sb) {
266 InstanceIdentifier<?> iid = codec.fromYangInstanceIdentifier(yiid);
268 reconstructIidPathString(yiid, sb);
270 toPathString(iid, sb);
274 private void reconstructIidPathString(YangInstanceIdentifier yiid, StringBuilder sb) {
275 sb.append("<RECONSTRUCTED FROM: \"").append(yiid.toString()).append("\">");
276 for (YangInstanceIdentifier.PathArgument pathArg : yiid.getPathArguments()) {
277 if (pathArg instanceof YangInstanceIdentifier.AugmentationIdentifier) {
278 sb.append('/').append("AUGMENTATION");
281 sb.append('/').append(pathArg.getNodeType().getLocalName());
285 String getStackSummary() {
286 StackTraceElement[] stack = Thread.currentThread().getStackTrace();
288 StringBuilder sb = new StringBuilder();
289 for (int i = STACK_TRACE_FIRST_RELEVANT_FRAME; i < stack.length; i++) {
290 StackTraceElement frame = stack[i];
291 sb.append("\n\t(TracingBroker)\t").append(frame.getClassName()).append('.').append(frame.getMethodName());
294 return sb.toString();
298 public DOMDataReadWriteTransaction newReadWriteTransaction() {
299 return new TracingReadWriteTransaction(delegate.newReadWriteTransaction(), this, readWriteTransactionsRegistry);
303 public DOMDataWriteTransaction newWriteOnlyTransaction() {
304 return new TracingWriteTransaction(delegate.newWriteOnlyTransaction(), this, writeTransactionsRegistry);
308 public DOMTransactionChain createTransactionChain(TransactionChainListener transactionChainListener) {
309 return new TracingTransactionChain(
310 delegate.createTransactionChain(transactionChainListener), this, transactionChainsRegistry);
314 public DOMDataReadOnlyTransaction newReadOnlyTransaction() {
315 return new TracingReadOnlyTransaction(delegate.newReadOnlyTransaction(), readOnlyTransactionsRegistry);
320 public Map<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> getSupportedExtensions() {
321 Map<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> res = delegate.getSupportedExtensions();
322 DOMDataTreeChangeService treeChangeSvc = (DOMDataTreeChangeService) res.get(DOMDataTreeChangeService.class);
323 if (treeChangeSvc == null) {
327 res = new HashMap<>(res);
329 res.put(DOMDataTreeChangeService.class, new DOMDataTreeChangeService() {
332 public <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerDataTreeChangeListener(
333 @Nonnull DOMDataTreeIdentifier domDataTreeIdentifier, @Nonnull L listener) {
334 if (isRegistrationWatched(domDataTreeIdentifier.getRootIdentifier(),
335 domDataTreeIdentifier.getDatastoreType())) {
336 LOG.warn("{} registration (registerDataTreeChangeListener) for {} from {}.",
337 listener instanceof ClusteredDOMDataTreeChangeListener ? "Clustered" : "Non-clustered",
338 toPathString(domDataTreeIdentifier.getRootIdentifier()), getStackSummary());
340 return treeChangeSvc.registerDataTreeChangeListener(domDataTreeIdentifier, listener);
348 public boolean printOpenTransactions(PrintStream ps) {
349 if (transactionChainsRegistry.getAllUnique().isEmpty()
350 && readOnlyTransactionsRegistry.getAllUnique().isEmpty()
351 && writeTransactionsRegistry.getAllUnique().isEmpty()
352 && readWriteTransactionsRegistry.getAllUnique().isEmpty()) {
357 ps.println(getClass().getSimpleName() + " found some not yet (or never..) closed transaction[chain]s!");
358 ps.println("[NB: If no stack traces are shown below, then "
359 + "enable transaction-debug-context-enabled in mdsaltrace_config.xml]");
361 printRegistryOpenTransactions(readOnlyTransactionsRegistry, ps, " ");
362 printRegistryOpenTransactions(writeTransactionsRegistry, ps, " ");
363 printRegistryOpenTransactions(readWriteTransactionsRegistry, ps, " ");
365 // Now print details for each non-closed TransactionChain
366 // incl. in turn each ones own read/Write[Only]TransactionsRegistry
367 Set<CloseTrackedRegistryReportEntry<TracingTransactionChain>>
368 entries = transactionChainsRegistry.getAllUnique();
369 if (!entries.isEmpty()) {
370 ps.println(" " + transactionChainsRegistry.getAnchor() + " : "
371 + transactionChainsRegistry.getCreateDescription());
373 entries.forEach(entry -> {
374 ps.println(" " + entry.getNumberAddedNotRemoved() + "x TransactionChains opened but not closed here:");
375 printStackTraceElements(ps, " ", entry.getStackTraceElements());
376 @SuppressWarnings("resource")
377 TracingTransactionChain txChain = (TracingTransactionChain) entry
378 .getExampleCloseTracked().getRealCloseTracked();
379 printRegistryOpenTransactions(txChain.getReadOnlyTransactionsRegistry(), ps, " ");
380 printRegistryOpenTransactions(txChain.getWriteTransactionsRegistry(), ps, " ");
381 printRegistryOpenTransactions(txChain.getReadWriteTransactionsRegistry(), ps, " ");
388 private <T extends CloseTracked<T>> void printRegistryOpenTransactions(
389 CloseTrackedRegistry<T> registry, PrintStream ps, String indent) {
390 Set<CloseTrackedRegistryReportEntry<T>> unsorted = registry.getAllUnique();
392 List<CloseTrackedRegistryReportEntry<T>> entries = new ArrayList<>(unsorted);
393 entries.sort((o1, o2) -> Long.compare(o2.getNumberAddedNotRemoved(), o1.getNumberAddedNotRemoved()));
395 if (!entries.isEmpty()) {
396 ps.println(indent + registry.getAnchor() + " : " + registry.getCreateDescription());
398 entries.forEach(entry -> {
399 ps.println(indent + " " + entry.getNumberAddedNotRemoved()
400 + "x transactions opened here, which are not closed:");
401 printStackTraceElements(ps, indent + " ", entry.getStackTraceElements());
403 if (!entries.isEmpty()) {
408 private void printStackTraceElements(PrintStream ps, String indent, List<StackTraceElement> stackTraceElements) {
409 boolean ellipsis = false;
410 for (final StackTraceElement stackTraceElement : stackTraceElements) {
411 if (isStackTraceElementInteresting(stackTraceElement)) {
412 ps.println(indent + stackTraceElement);
414 } else if (!ellipsis) {
415 ps.println(indent + "(...)");
421 private boolean isStackTraceElementInteresting(StackTraceElement element) {
422 final String className = element.getClassName();
423 return !className.startsWith(getClass().getPackage().getName())
424 && !className.startsWith(CloseTracked.class.getPackage().getName())
425 && !className.startsWith("Proxy")
426 && !className.startsWith("akka")
427 && !className.startsWith("scala")
428 && !className.startsWith("sun.reflect")
429 && !className.startsWith("java.lang.reflect")
430 && !className.startsWith("org.apache.aries.blueprint")
431 && !className.startsWith("org.osgi.util.tracker");