Updated Branches:
  refs/heads/master 59f03554e -> d04a09fc4

CAMEL-6384: Added BacklogDebugger MBean for live debugging of Camel routes. 
Work in progress.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/d04a09fc
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d04a09fc
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d04a09fc

Branch: refs/heads/master
Commit: d04a09fc425b1e148d75778482210ce34b3e5a5e
Parents: 59f0355
Author: Claus Ibsen <[email protected]>
Authored: Thu May 23 10:14:05 2013 +0200
Committer: Claus Ibsen <[email protected]>
Committed: Thu May 23 10:14:20 2013 +0200

----------------------------------------------------------------------
 .../org/apache/camel/impl/DefaultDebugger.java     |    2 +-
 .../processor/interceptor/BacklogDebugger.java     |   40 ++++++----
 .../camel/management/BacklogDebuggerTest.java      |   60 ++++++++++++++-
 3 files changed, 83 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/d04a09fc/camel-core/src/main/java/org/apache/camel/impl/DefaultDebugger.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/impl/DefaultDebugger.java 
b/camel-core/src/main/java/org/apache/camel/impl/DefaultDebugger.java
index 59f1b26..f487fb5 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/DefaultDebugger.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultDebugger.java
@@ -122,7 +122,7 @@ public class DefaultDebugger implements Debugger, 
CamelContextAware {
     }
 
     public void addSingleStepBreakpoint(final Breakpoint breakpoint) {
-        breakpoints.add(new BreakpointConditions(breakpoint));
+        addSingleStepBreakpoint(breakpoint, null);
     }
 
     public void addSingleStepBreakpoint(final Breakpoint breakpoint, 
Condition... conditions) {

http://git-wip-us.apache.org/repos/asf/camel/blob/d04a09fc/camel-core/src/main/java/org/apache/camel/processor/interceptor/BacklogDebugger.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/processor/interceptor/BacklogDebugger.java
 
b/camel-core/src/main/java/org/apache/camel/processor/interceptor/BacklogDebugger.java
index a54a4ed..56337af 100644
--- 
a/camel-core/src/main/java/org/apache/camel/processor/interceptor/BacklogDebugger.java
+++ 
b/camel-core/src/main/java/org/apache/camel/processor/interceptor/BacklogDebugger.java
@@ -23,6 +23,8 @@ import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicLong;
@@ -63,9 +65,9 @@ public class BacklogDebugger extends ServiceSupport 
implements InterceptStrategy
     private final AtomicBoolean enabled = new AtomicBoolean();
     private final AtomicLong debugCounter = new AtomicLong(0);
     private final Debugger debugger;
-    private final Map<String, NodeBreakpoint> breakpoints = new 
HashMap<String, NodeBreakpoint>();
-    private final Map<String, CountDownLatch> suspendedBreakpoints = new 
HashMap<String, CountDownLatch>();
-    private final Map<String, BacklogTracerEventMessage> 
suspendedBreakpointMessages = new HashMap<String, BacklogTracerEventMessage>();
+    private final ConcurrentMap<String, NodeBreakpoint> breakpoints = new 
ConcurrentHashMap<String, NodeBreakpoint>();
+    private final ConcurrentMap<String, CountDownLatch> suspendedBreakpoints = 
new ConcurrentHashMap<String, CountDownLatch>();
+    private final ConcurrentMap<String, BacklogTracerEventMessage> 
suspendedBreakpointMessages = new ConcurrentHashMap<String, 
BacklogTracerEventMessage>();
     private volatile String singleStepExchangeId;
 
     public BacklogDebugger(CamelContext camelContext) {
@@ -155,7 +157,7 @@ public class BacklogDebugger extends ServiceSupport 
implements InterceptStrategy
             logger.log("Adding breakpoint " + nodeId);
             breakpoint = new NodeBreakpoint(nodeId, null);
             breakpoints.put(nodeId, breakpoint);
-            debugger.addBreakpoint(breakpoint);
+            debugger.addBreakpoint(breakpoint, breakpoint);
         } else {
             breakpoint.setCondition(null);
         }
@@ -304,7 +306,8 @@ public class BacklogDebugger extends ServiceSupport 
implements InterceptStrategy
     }
 
     public boolean afterProcess(Exchange exchange, Processor processor, 
ProcessorDefinition<?> definition, long timeTaken) {
-        return debugger.afterProcess(exchange, processor, definition, 
timeTaken);
+        // noop
+        return false;
     }
 
     protected void doStart() throws Exception {
@@ -344,7 +347,7 @@ public class BacklogDebugger extends ServiceSupport 
implements InterceptStrategy
         public void beforeProcess(Exchange exchange, Processor processor, 
ProcessorDefinition<?> definition) {
             // store a copy of the message so we can see that from the debugger
             Date timestamp = new Date();
-            String toNode = nodeId;
+            String toNode = definition.getId();
             String routeId = ProcessorDefinitionHelper.getRouteId(definition);
             String exchangeId = exchange.getExchangeId();
             String messageAsXml = MessageHelper.dumpAsXml(exchange.getIn(), 
true, 2, false, false, 1000);
@@ -354,28 +357,34 @@ public class BacklogDebugger extends ServiceSupport 
implements InterceptStrategy
             suspendedBreakpointMessages.put(nodeId, msg);
 
             // mark as suspend
-            final CountDownLatch latch = new CountDownLatch(1);
-            suspendedBreakpoints.put(nodeId, latch);
+            final CountDownLatch latch = suspendedBreakpoints.get(nodeId);
 
             // now wait until we should continue
-            logger.log("NodeBreakpoint at node " + nodeId + " is waiting to 
continue for exchangeId: " + exchange.getExchangeId());
+            logger.log("NodeBreakpoint at node " + toNode + " is waiting to 
continue for exchangeId: " + exchange.getExchangeId());
             try {
                 // TODO: have a fallback timeout so we wont wait forever
                 latch.await();
-                logger.log("NodeBreakpoint at node " + nodeId + " is continued 
exchangeId: " + exchange.getExchangeId());
+                logger.log("NodeBreakpoint at node " + toNode + " is continued 
exchangeId: " + exchange.getExchangeId());
             } catch (InterruptedException e) {
                 // ignore
             }
-            super.beforeProcess(exchange, processor, definition);
         }
 
         @Override
         public boolean matchProcess(Exchange exchange, Processor processor, 
ProcessorDefinition<?> definition) {
-            boolean match = nodeId.equals(definition.getId());
-            if (match && condition != null) {
-                return condition.matches(exchange);
+            // must match node
+            if (!nodeId.equals(definition.getId())) {
+                return false;
             }
-            return match;
+
+            // if condition then must match
+            if (condition != null && !condition.matches(exchange)) {
+                return false;
+            }
+
+            // we only want to break one exchange at a time, so if there is 
already a suspended breakpoint then do not match
+            boolean existing = suspendedBreakpoints.putIfAbsent(nodeId, new 
CountDownLatch(1)) != null;
+            return !existing;
         }
 
         @Override
@@ -415,7 +424,6 @@ public class BacklogDebugger extends ServiceSupport 
implements InterceptStrategy
             } catch (InterruptedException e) {
                 // ignore
             }
-            super.beforeProcess(exchange, processor, definition);
         }
 
         @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/d04a09fc/camel-core/src/test/java/org/apache/camel/management/BacklogDebuggerTest.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/test/java/org/apache/camel/management/BacklogDebuggerTest.java 
b/camel-core/src/test/java/org/apache/camel/management/BacklogDebuggerTest.java
index c2302cd..23b9aee 100644
--- 
a/camel-core/src/test/java/org/apache/camel/management/BacklogDebuggerTest.java
+++ 
b/camel-core/src/test/java/org/apache/camel/management/BacklogDebuggerTest.java
@@ -81,6 +81,62 @@ public class BacklogDebuggerTest extends 
ManagementTestSupport {
     }
 
     @SuppressWarnings("unchecked")
+    public void testBacklogDebuggerSuspendOnlyOneAtBreakpoint() throws 
Exception {
+        MBeanServer mbeanServer = getMBeanServer();
+        ObjectName on = new 
ObjectName("org.apache.camel:context=localhost/camel-1,type=tracer,name=BacklogDebugger");
+        assertNotNull(on);
+        mbeanServer.isRegistered(on);
+
+        Boolean enabled = (Boolean) mbeanServer.getAttribute(on, "Enabled");
+        assertEquals("Should not be enabled", Boolean.FALSE, enabled);
+
+        // enable debugger
+        mbeanServer.invoke(on, "enableDebugger", null, null);
+
+        enabled = (Boolean) mbeanServer.getAttribute(on, "Enabled");
+        assertEquals("Should be enabled", Boolean.TRUE, enabled);
+
+        // add breakpoint at bar
+        mbeanServer.invoke(on, "addBreakpoint", new Object[]{"bar"}, new 
String[]{"java.lang.String"});
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(2);
+
+        // only one of them is suspended
+        template.sendBody("seda:start", "Hello World");
+        template.sendBody("seda:start", "Hello Camel");
+        template.sendBody("seda:start", "Hello Earth");
+
+        assertMockEndpointsSatisfied();
+
+        // add breakpoint at bar
+        Set<String> nodes = (Set<String>) mbeanServer.invoke(on, 
"getSuspendedBreakpointNodeIds", null, null);
+        assertNotNull(nodes);
+        assertEquals(1, nodes.size());
+        assertEquals("bar", nodes.iterator().next());
+
+        // the message should be ours
+        String xml = (String) mbeanServer.invoke(on, 
"dumpTracedMessagesAsXml", new Object[]{"bar"}, new 
String[]{"java.lang.String"});
+        assertNotNull(xml);
+        log.info(xml);
+
+        assertTrue("Should contain bar node", 
xml.contains("<toNode>bar</toNode>"));
+
+        resetMocks();
+        mock.expectedMessageCount(1);
+
+        // resume breakpoint
+        mbeanServer.invoke(on, "resumeBreakpoint", new Object[]{"bar"}, new 
String[]{"java.lang.String"});
+
+        assertMockEndpointsSatisfied();
+
+        // and no suspended anymore
+        nodes = (Set<String>) mbeanServer.invoke(on, 
"getSuspendedBreakpointNodeIds", null, null);
+        assertNotNull(nodes);
+        assertEquals(0, nodes.size());
+    }
+
+    @SuppressWarnings("unchecked")
     public void testBacklogDebuggerConditional() throws Exception {
         MBeanServer mbeanServer = getMBeanServer();
         ObjectName on = new 
ObjectName("org.apache.camel:context=localhost/camel-1,type=tracer,name=BacklogDebugger");
@@ -250,11 +306,11 @@ public class BacklogDebuggerTest extends 
ManagementTestSupport {
             public void configure() throws Exception {
                 context.setUseBreadcrumb(false);
 
-                from("seda:start")
+                from("seda:start?concurrentConsumers=2")
                         .to("log:foo").id("foo")
                         .to("log:bar").id("bar")
                         .transform().constant("Bye World").id("transform")
-                        .to("log:cheese").id("cheese")
+                        .to("log:cheese?showExchangeId=true").id("cheese")
                         .to("mock:result").id("result");
             }
         };

Reply via email to