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.mdsal.trace.impl;
10 import static java.util.Objects.requireNonNull;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.io.PrintStream;
14 import java.util.ArrayList;
15 import java.util.List;
17 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener;
20 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
24 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
25 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
26 import org.opendaylight.mdsal.trace.api.TracingDOMDataBroker;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsaltrace.rev160908.Config;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 @SuppressWarnings("checkstyle:JavadocStyle")
35 //...because otherwise it whines about the elements in the @code block even though it's completely valid Javadoc
37 * TracingBroker logs "write" operations and listener registrations to the md-sal. It logs the instance identifier path,
38 * the objects themselves, as well as the stack trace of the call invoking the registration or write operation.
39 * It works by operating as a "bump on the stack" between the application and actual DataBroker, intercepting write
40 * and registration calls and writing to the log.
42 * <p>In addition, it (optionally) can also keep track of the stack trace of all new transaction allocations
43 * (including TransactionChains, and transactions created in turn from them), in order to detect and report leaks
44 * from transactions which were not closed.
47 * TracingBroker is designed to be easy to use. In fact, for bundles using Blueprint to inject their DataBroker
48 * TracingBroker can be used without modifying your code at all in two simple steps:
51 * Simply add the dependency "mdsaltrace-features" to
56 * <groupId>org.opendaylight.controller</groupId>
57 * <artifactId>features-mdsal-trace</artifactId>
58 * <version>1.7.0-SNAPSHOT</version>
59 * <classifier>features</classifier>
61 * <scope>runtime</scope>
67 * Then just "feature:install odl-mdsal-trace" before you install your "real" feature(s) and you're done.
68 * Beware that with Karaf 4 due to <a href="https://bugs.opendaylight.org/show_bug.cgi?id=9068">Bug 9068</a>
69 * you'll probably have to use feature:install's --no-auto-refresh flag when installing your "real" feature.
72 * This works because the mdsaltrace-impl bundle registers its service implementing DOMDataBroker with a higher
73 * rank than sal-binding-broker. As such, any OSGi service lookup for DataBroker will receive the TracingBroker.
75 * <h1>Avoiding log bloat:</h1>
76 * TracingBroker can be configured to only print registrations or write ops pertaining to certain subtrees of the
77 * md-sal. This can be done in the code via the methods of this class or via a config file. TracingBroker uses a more
78 * convenient but non-standard representation of the instance identifiers. Each instance identifier segment's
79 * class.getSimpleName() is used separated by a '/'.
81 * <h1>Known issues</h1>
84 * Filtering by paths. For some registrations the codec that converts back from the DOM to binding paths is
85 * busted. As such, an aproximated path is used in the output. For now it is recommended not to use
86 * watchRegistrations and allow all registrations to be logged.
90 public class TracingBroker implements TracingDOMDataBroker {
91 private static final Logger LOG = LoggerFactory.getLogger(TracingBroker.class);
93 private static final int STACK_TRACE_FIRST_RELEVANT_FRAME = 2;
95 private final BindingCodecTree codec;
96 private final DOMDataBroker delegate;
97 private final List<Watch> registrationWatches = new ArrayList<>();
98 private final List<Watch> writeWatches = new ArrayList<>();
100 private final boolean isDebugging;
101 private final CloseTrackedRegistry<TracingTransactionChain> transactionChainsRegistry;
102 private final CloseTrackedRegistry<TracingReadOnlyTransaction> readOnlyTransactionsRegistry;
103 private final CloseTrackedRegistry<TracingWriteTransaction> writeTransactionsRegistry;
104 private final CloseTrackedRegistry<TracingReadWriteTransaction> readWriteTransactionsRegistry;
106 private class Watch {
107 final String iidString;
108 final LogicalDatastoreType store;
110 Watch(final String iidString, final LogicalDatastoreType storeOrNull) {
112 this.iidString = iidString;
115 private String toIidCompString(final YangInstanceIdentifier iid) {
116 StringBuilder builder = new StringBuilder();
117 toPathString(iid, builder);
118 return builder.append('/').toString();
121 private boolean isParent(final String parent, final String child) {
122 int parentOffset = 0;
123 if (parent.length() > 0 && parent.charAt(0) == '<') {
124 parentOffset = parent.indexOf('>') + 1;
128 if (child.length() > 0 && child.charAt(0) == '<') {
129 childOffset = child.indexOf('>') + 1;
132 return child.startsWith(parent.substring(parentOffset), childOffset);
135 @SuppressWarnings({ "checkstyle:hiddenField", "hiding" })
136 public boolean subtreesOverlap(final YangInstanceIdentifier iid, final LogicalDatastoreType store) {
137 if (this.store != null && !this.store.equals(store)) {
141 String otherIidString = toIidCompString(iid);
142 return isParent(iidString, otherIidString) || isParent(otherIidString, iidString);
145 @SuppressWarnings({ "checkstyle:hiddenField", "hiding" })
146 public boolean eventIsOfInterest(final YangInstanceIdentifier iid, final LogicalDatastoreType store) {
147 if (this.store != null && !this.store.equals(store)) {
151 return isParent(iidString, toPathString(iid));
155 public TracingBroker(final DOMDataBroker delegate, final Config config, final BindingCodecTree codec) {
156 this.delegate = requireNonNull(delegate, "delegate");
157 this.codec = requireNonNull(codec, "codec");
160 isDebugging = Boolean.TRUE.equals(config.getTransactionDebugContextEnabled());
161 final String db = "DataBroker";
162 transactionChainsRegistry = new CloseTrackedRegistry<>(db, "createTransactionChain()", isDebugging);
163 readOnlyTransactionsRegistry = new CloseTrackedRegistry<>(db, "newReadOnlyTransaction()", isDebugging);
164 writeTransactionsRegistry = new CloseTrackedRegistry<>(db, "newWriteOnlyTransaction()", isDebugging);
165 readWriteTransactionsRegistry = new CloseTrackedRegistry<>(db, "newReadWriteTransaction()", isDebugging);
168 private void configure(final Config config) {
169 registrationWatches.clear();
170 Set<String> paths = config.getRegistrationWatches();
172 for (String path : paths) {
173 watchRegistrations(path, null);
177 writeWatches.clear();
178 paths = config.getWriteWatches();
180 for (String path : paths) {
181 watchWrites(path, null);
187 * Log registrations to this subtree of the md-sal.
188 * @param iidString the iid path of the root of the subtree
189 * @param store Which LogicalDataStore? or null for both
191 public void watchRegistrations(final String iidString, final LogicalDatastoreType store) {
192 LOG.info("Watching registrations to {} in {}", iidString, store);
193 registrationWatches.add(new Watch(iidString, store));
197 * Log writes to this subtree of the md-sal.
198 * @param iidString the iid path of the root of the subtree
199 * @param store Which LogicalDataStore? or null for both
201 public void watchWrites(final String iidString, final LogicalDatastoreType store) {
202 LOG.info("Watching writes to {} in {}", iidString, store);
203 Watch watch = new Watch(iidString, store);
204 writeWatches.add(watch);
207 private boolean isRegistrationWatched(final YangInstanceIdentifier iid, final LogicalDatastoreType store) {
208 if (registrationWatches.isEmpty()) {
212 for (Watch regInterest : registrationWatches) {
213 if (regInterest.subtreesOverlap(iid, store)) {
221 boolean isWriteWatched(final YangInstanceIdentifier iid, final LogicalDatastoreType store) {
222 if (writeWatches.isEmpty()) {
226 for (Watch watch : writeWatches) {
227 if (watch.eventIsOfInterest(iid, store)) {
235 static void toPathString(final InstanceIdentifier<? extends DataObject> iid, final StringBuilder builder) {
236 for (InstanceIdentifier.PathArgument pathArg : iid.getPathArguments()) {
237 builder.append('/').append(pathArg.getType().getSimpleName());
241 String toPathString(final YangInstanceIdentifier yiid) {
242 StringBuilder sb = new StringBuilder();
243 toPathString(yiid, sb);
244 return sb.toString();
248 private void toPathString(final YangInstanceIdentifier yiid, final StringBuilder sb) {
249 InstanceIdentifier<?> iid = codec.getInstanceIdentifierCodec().toBinding(yiid);
251 reconstructIidPathString(yiid, sb);
253 toPathString(iid, sb);
257 private static void reconstructIidPathString(final YangInstanceIdentifier yiid, final StringBuilder sb) {
258 sb.append("<RECONSTRUCTED FROM: \"").append(yiid.toString()).append("\">");
259 for (YangInstanceIdentifier.PathArgument pathArg : yiid.getPathArguments()) {
260 sb.append('/').append(pathArg.getNodeType().getLocalName());
264 String getStackSummary() {
265 StackTraceElement[] stack = Thread.currentThread().getStackTrace();
267 StringBuilder sb = new StringBuilder();
268 for (int i = STACK_TRACE_FIRST_RELEVANT_FRAME; i < stack.length; i++) {
269 StackTraceElement frame = stack[i];
270 sb.append("\n\t(TracingBroker)\t").append(frame.getClassName()).append('.').append(frame.getMethodName());
273 return sb.toString();
277 public DOMDataTreeReadWriteTransaction newReadWriteTransaction() {
278 return new TracingReadWriteTransaction(delegate.newReadWriteTransaction(), this, readWriteTransactionsRegistry);
282 public DOMDataTreeWriteTransaction newWriteOnlyTransaction() {
283 return new TracingWriteTransaction(delegate.newWriteOnlyTransaction(), this, writeTransactionsRegistry);
287 public DOMTransactionChain createTransactionChain() {
288 return new TracingTransactionChain(delegate.createTransactionChain(), this, transactionChainsRegistry);
292 public DOMTransactionChain createMergingTransactionChain() {
293 return new TracingTransactionChain(delegate.createMergingTransactionChain(), this, transactionChainsRegistry);
297 public DOMDataTreeReadTransaction newReadOnlyTransaction() {
298 return new TracingReadOnlyTransaction(delegate.newReadOnlyTransaction(), readOnlyTransactionsRegistry);
302 public <T extends Extension> T extension(final Class<T> type) {
303 final var ext = delegate.extension(type);
304 if (DOMDataTreeChangeService.class.equals(type) && ext instanceof DOMDataTreeChangeService treeChange) {
305 return type.cast((DOMDataTreeChangeService) (domDataTreeIdentifier, listener) -> {
306 final var rootId = domDataTreeIdentifier.path();
307 if (isRegistrationWatched(rootId, domDataTreeIdentifier.datastore())) {
308 LOG.warn("{} registration (registerDataTreeChangeListener) for {} from {}.",
309 listener instanceof ClusteredDOMDataTreeChangeListener ? "Clustered" : "Non-clustered",
310 toPathString(rootId), getStackSummary());
312 return treeChange.registerDataTreeChangeListener(domDataTreeIdentifier, listener);
319 public boolean printOpenTransactions(final PrintStream ps, final int minOpenTXs) {
320 if (transactionChainsRegistry.getAllUnique().isEmpty()
321 && readOnlyTransactionsRegistry.getAllUnique().isEmpty()
322 && writeTransactionsRegistry.getAllUnique().isEmpty()
323 && readWriteTransactionsRegistry.getAllUnique().isEmpty()) {
325 ps.println("No open transactions, great!");
329 ps.println(getClass().getSimpleName() + " found some not yet (or never..) closed transaction[chain]s!");
330 ps.println("[NB: If no stack traces are shown below, then "
331 + "enable transaction-debug-context-enabled in mdsaltrace_config.xml]");
333 // Flag to track if we really found any real leaks with more (or equal) to minOpenTXs
334 boolean hasFound = print(readOnlyTransactionsRegistry, ps, " ", minOpenTXs);
335 hasFound |= print(writeTransactionsRegistry, ps, " ", minOpenTXs);
336 hasFound |= print(readWriteTransactionsRegistry, ps, " ", minOpenTXs);
338 // Now print details for each non-closed TransactionChain
339 // incl. in turn each ones own read/Write[Only]TransactionsRegistry
340 Set<CloseTrackedRegistryReportEntry<TracingTransactionChain>>
341 entries = transactionChainsRegistry.getAllUnique();
342 if (!entries.isEmpty()) {
343 ps.println(" " + transactionChainsRegistry.getAnchor() + " : "
344 + transactionChainsRegistry.getCreateDescription());
346 for (CloseTrackedRegistryReportEntry<TracingTransactionChain> entry : entries) {
347 ps.println(" " + entry.getNumberAddedNotRemoved() + "x TransactionChains opened but not closed here:");
348 printStackTraceElements(ps, " ", entry.getStackTraceElements());
349 @SuppressWarnings("resource")
350 TracingTransactionChain txChain = (TracingTransactionChain) entry
351 .getExampleCloseTracked().getRealCloseTracked();
352 hasFound |= print(txChain.getReadOnlyTransactionsRegistry(), ps, " ", minOpenTXs);
353 hasFound |= print(txChain.getWriteTransactionsRegistry(), ps, " ", minOpenTXs);
354 hasFound |= print(txChain.getReadWriteTransactionsRegistry(), ps, " ", minOpenTXs);
361 final void logEmptySet(final YangInstanceIdentifier yiid) {
362 if (LOG.isDebugEnabled()) {
363 LOG.debug("Empty data set write to {}", toPathString(yiid));
367 @SuppressFBWarnings(value = "SLF4J_SIGN_ONLY_FORMAT", justification = "pre-formatted logs")
368 static final void logOperations(final Object identifier, final List<?> operations) {
369 if (LOG.isWarnEnabled()) {
370 LOG.warn("Transaction {} contains the following operations:", identifier);
371 for (var operation : operations) {
372 LOG.warn("{}", operation);
377 private <T extends CloseTracked<T>> boolean print(final CloseTrackedRegistry<T> registry, final PrintStream ps,
378 final String indent, final int minOpenTransactions) {
379 Set<CloseTrackedRegistryReportEntry<T>> unsorted = registry.getAllUnique();
380 if (unsorted.size() < minOpenTransactions) {
384 List<CloseTrackedRegistryReportEntry<T>> entries = new ArrayList<>(unsorted);
385 entries.sort((o1, o2) -> Long.compare(o2.getNumberAddedNotRemoved(), o1.getNumberAddedNotRemoved()));
387 if (!entries.isEmpty()) {
388 ps.println(indent + registry.getAnchor() + " : " + registry.getCreateDescription());
390 entries.forEach(entry -> {
391 ps.println(indent + " " + entry.getNumberAddedNotRemoved()
392 + "x transactions opened here, which are not closed:");
393 printStackTraceElements(ps, indent + " ", entry.getStackTraceElements());
395 if (!entries.isEmpty()) {
401 private void printStackTraceElements(final PrintStream ps, final String indent,
402 final List<StackTraceElement> stackTraceElements) {
403 boolean ellipsis = false;
404 for (final StackTraceElement stackTraceElement : stackTraceElements) {
405 if (isStackTraceElementInteresting(stackTraceElement)) {
406 ps.println(indent + stackTraceElement);
408 } else if (!ellipsis) {
409 ps.println(indent + "(...)");
415 private boolean isStackTraceElementInteresting(final StackTraceElement element) {
416 final String className = element.getClassName();
417 return !className.startsWith(getClass().getPackage().getName())
418 && !className.startsWith(CloseTracked.class.getPackage().getName())
419 && !className.startsWith("Proxy")
420 && !className.startsWith("akka")
421 && !className.startsWith("scala")
422 && !className.startsWith("sun.reflect")
423 && !className.startsWith("java.lang.reflect")
424 && !className.startsWith("org.apache.aries.blueprint")
425 && !className.startsWith("org.osgi.util.tracker");