Skip to content

Commit

Permalink
WIP FUSETOOLS2-1624 - Support automatic restart
Browse files Browse the repository at this point in the history
When Automatic restart of Camel route is configured, the JMX connection
vanishes for few seconds. Adding code to be able to reconnect. As a
first iteration ,allow 5 seconds for the connection to be up again.

- how to test it?
- How to check we have no potential zombies if reconnection last more
than 5 seconds or if it was an abrupt stop?
- if automatic reload is done in the one second time between two check
of suspended nodes, we won't detect the reload. For now, I do not see a
better way than to live with it as it shouldn't occur a lot, with a very
simple use case, it is taking always more than a second on my relatively
fast machine.

Current proposal is working only with Quarkus with a not robust
implementation and I cannot manage to write a test for this case.

i tis not working with Camel JBangand I do not see how to detect that
there was a reload. I'm wondering if the breakpoints are kept on route
reload, it would not be enough to work on most cases.

Maybe a better approach is to support the "restart" of the debug adapter
which is available in VS Code UI when the code has been detected as
modified.

Signed-off-by: Aurélien Pupier <[email protected]>
  • Loading branch information
apupier committed May 11, 2022
1 parent 3194a7b commit 233a05c
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -28,6 +29,7 @@

import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
Expand All @@ -43,6 +45,7 @@
import org.apache.camel.api.management.mbean.ManagedCamelContextMBean;
import org.eclipse.lsp4j.debug.OutputEventArguments;
import org.eclipse.lsp4j.debug.OutputEventArgumentsCategory;
import org.eclipse.lsp4j.debug.Source;
import org.eclipse.lsp4j.debug.StoppedEventArguments;
import org.eclipse.lsp4j.debug.StoppedEventArgumentsReason;
import org.eclipse.lsp4j.debug.ThreadEventArguments;
Expand All @@ -66,6 +69,7 @@ public class BacklogDebuggerConnectionManager {
private static final String OBJECTNAME_CAMELCONTEXT = "org.apache.camel:context=*,type=context,name=*";
public static final String DEFAULT_JMX_URI = "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi/camel";
private static final Logger LOGGER = LoggerFactory.getLogger(BacklogDebuggerConnectionManager.class);
private static final int AUTOMATIC_RESTART_TIMEOUT = 5000;

public static final String ATTACH_PARAM_PID = "attach_pid";
public static final String ATTACH_PARAM_JMX_URL = "attach_jmx_url";
Expand All @@ -81,6 +85,7 @@ public class BacklogDebuggerConnectionManager {
private Map<String, CamelBreakpoint> camelBreakpointsWithSources = new HashMap<>();

private boolean isStepping = false;
private String jmxAddress;

private String getLocalJMXUrl(String javaProcessPID) {
try {
Expand All @@ -104,7 +109,7 @@ private String getLocalJMXUrl(String javaProcessPID) {
public boolean attach(Map<String, Object> args, IDebugProtocolClient client) {
this.client = client;
try {
String jmxAddress = (String) args.getOrDefault(ATTACH_PARAM_JMX_URL, DEFAULT_JMX_URI);
jmxAddress = (String) args.getOrDefault(ATTACH_PARAM_JMX_URL, DEFAULT_JMX_URI);
Object pid = args.get(ATTACH_PARAM_PID);
if (pid != null) {
jmxAddress = getLocalJMXUrl((String) pid);
Expand Down Expand Up @@ -172,6 +177,7 @@ private JMXConnector connect(JMXServiceURL jmxUrl) throws IOException {
}

private void checkSuspendedBreakpoints() {
try {
while(backlogDebugger != null && backlogDebugger.isEnabled()) {
Set<String> suspendedBreakpointNodeIds = backlogDebugger.suspendedBreakpointNodeIds();
for (String nodeId : suspendedBreakpointNodeIds) {
Expand All @@ -187,6 +193,105 @@ private void checkSuspendedBreakpoints() {
return;
}
}
} catch (UndeclaredThrowableException ute) {
automaticReconnection();
}
}

private void automaticReconnection() {
if (backlogDebugger != null) {
// assume that it is an Automatic restart
resetDebuggerState();
int count = 0;
boolean isConnectionAvailableAgain = false;
while (backlogDebugger != null && count < AUTOMATIC_RESTART_TIMEOUT && !isConnectionAvailableAgain) {
System.out.println("Loop in automaticReconnection");
count +=100;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
isConnectionAvailableAgain = connect(jmxAddress);
}
if (isConnectionAvailableAgain) {
try {
routesDOMDocument = retrieveRoutesWithSourceLineNumber(jmxAddress);
addBackBreakpoints();
checkSuspendedBreakpoints();
} catch (Exception e) {
LOGGER.error("Cannot update routes dom document from "+ jmxAddress, e);
}
}
}
}

private void addBackBreakpoints() {
for (CamelBreakpoint camelBreakpoint : camelBreakpointsWithSources.values()) {
setBreakpoint(camelBreakpoint.getSource(), camelBreakpoint.getLine(), camelBreakpoint);
}
}

private void resetDebuggerState() {
isStepping = false;
for (CamelThread camelThread : camelThreads) {
sendThreadExitEvent(camelThread);
}
camelThreads.clear();
notifiedSuspendedBreakpointIds.clear();
}

private boolean connect(String jmxAddress) {
System.out.println("Try connect to "+ jmxAddress);
try {
JMXServiceURL jmxUrl = new JMXServiceURL(jmxAddress);
jmxConnector = connect(jmxUrl);
System.out.println("JMX connector retrieved");
mbeanConnection = jmxConnector.getMBeanServerConnection();
ObjectName objectName = new ObjectName(OBJECTNAME_BACKLOGDEBUGGER);
Set<ObjectName> names = mbeanConnection.queryNames(objectName, null);
if (names != null && !names.isEmpty()) {
ObjectName debuggerMBeanObjectName = names.iterator().next();
backlogDebugger = JMX.newMBeanProxy(mbeanConnection, debuggerMBeanObjectName, ManagedBacklogDebuggerMBean.class);
System.out.println("connection succesful");
return true;
}
} catch (MalformedObjectNameException | IOException | UndeclaredThrowableException e) {
// Silence while trying to reconnect. We know it will fail a lot because the connection might not be ready.
System.out.println(e);
} catch (Throwable t) {
System.out.println(t);
}
System.out.println("failed to connect");
return false;
}

public String setBreakpoint(Source source, int line, CamelBreakpoint breakpoint) {
String breakpointId = null;
if (routesDOMDocument != null) {
String path = "//*[@sourceLineNumber='" + line + "']";
//TODO: take care of sourceLocation and not only line number
// "//*[@sourceLocation='" + sourceLocation + "' and @sourceLineNumber='" + line + "']";

try {
XPath xPath = XPathFactory.newInstance().newXPath();
Node breakpointTagFromContext = (Node) xPath.evaluate(path, routesDOMDocument, XPathConstants.NODE);
if (breakpointTagFromContext != null) {
String nodeId = breakpointTagFromContext.getAttributes().getNamedItem("id").getTextContent();
breakpoint.setNodeId(nodeId);
updateBreakpointsWithSources(breakpoint);
breakpointId = nodeId;
getBacklogDebugger().addBreakpoint(nodeId);
breakpoint.setVerified(true);
}
} catch (Exception e) {
LOGGER.warn("Cannot find related id for "+ source.getPath() + "l." + line, e);
}
} else {
LOGGER.warn("No active routes find in Camel context. Consequently, cannot set breakpoint for {} l.{}", source.getPath(), line);
}
return breakpointId;
}

private void handleSuspendedBreakpoint(String nodeId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
import java.util.Set;
import java.util.concurrent.CompletableFuture;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.apache.camel.api.management.mbean.ManagedBacklogDebuggerMBean;
import org.eclipse.lsp4j.debug.Breakpoint;
import org.eclipse.lsp4j.debug.Capabilities;
Expand All @@ -43,6 +39,7 @@
import org.eclipse.lsp4j.debug.ScopesResponse;
import org.eclipse.lsp4j.debug.SetBreakpointsArguments;
import org.eclipse.lsp4j.debug.SetBreakpointsResponse;
import org.eclipse.lsp4j.debug.SetExceptionBreakpointsArguments;
import org.eclipse.lsp4j.debug.SetVariableArguments;
import org.eclipse.lsp4j.debug.SetVariableResponse;
import org.eclipse.lsp4j.debug.Source;
Expand All @@ -59,8 +56,6 @@
import org.eclipse.lsp4j.debug.services.IDebugProtocolServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import com.github.cameltooling.dap.internal.model.CamelBreakpoint;
import com.github.cameltooling.dap.internal.model.CamelScope;
Expand Down Expand Up @@ -115,28 +110,9 @@ public CompletableFuture<SetBreakpointsResponse> setBreakpoints(SetBreakpointsAr
breakpoint.setLine(line);
breakpoint.setMessage("the breakpoint "+ i);
breakpoints[i] = breakpoint;
Document routesDOMDocument = connectionManager.getRoutesDOMDocument();
if (routesDOMDocument != null) {
String path = "//*[@sourceLineNumber='" + line + "']";
//TODO: take care of sourceLocation and not only line number
// "//*[@sourceLocation='" + sourceLocation + "' and @sourceLineNumber='" + line + "']";

try {
XPath xPath = XPathFactory.newInstance().newXPath();
Node breakpointTagFromContext = (Node) xPath.evaluate(path, routesDOMDocument, XPathConstants.NODE);
if (breakpointTagFromContext != null) {
String nodeId = breakpointTagFromContext.getAttributes().getNamedItem("id").getTextContent();
breakpoint.setNodeId(nodeId);
connectionManager.updateBreakpointsWithSources(breakpoint);
breakpointIds.add(nodeId);
connectionManager.getBacklogDebugger().addBreakpoint(nodeId);
breakpoint.setVerified(true);
}
} catch (Exception e) {
LOGGER.warn("Cannot find related id for "+ source.getPath() + "l." + line, e);
}
} else {
LOGGER.warn("No active routes find in Camel context. Consequently, cannot set breakpoint for {} l.{}", source.getPath(), line);
String breakpointId = connectionManager.setBreakpoint(source, line, breakpoint);
if (breakpointId != null) {
breakpointIds.add(breakpointId);
}
}
removeOldBreakpoints(source, breakpointIds);
Expand Down Expand Up @@ -274,5 +250,14 @@ public CompletableFuture<SetVariableResponse> setVariable(SetVariableArguments a
public BacklogDebuggerConnectionManager getConnectionManager() {
return connectionManager;
}

@Override
public CompletableFuture<Void> setExceptionBreakpoints(SetExceptionBreakpointsArguments args) {
LOGGER.error("Setting Exception breakpoint is not supported. it should not be called");
LOGGER.error("Filters: " + String.join(",",args.getFilters()));
LOGGER.error("getExceptionOptions length: " + args.getExceptionOptions().length);
LOGGER.error("getFilterOptions length: " + args.getFilterOptions().length);
return IDebugProtocolServer.super.setExceptionBreakpoints(args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.cameltooling.dap.internal;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

import java.util.concurrent.CompletableFuture;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.engine.DefaultProducerTemplate;
import org.eclipse.lsp4j.debug.SetBreakpointsArguments;
import org.eclipse.lsp4j.debug.StoppedEventArguments;
import org.eclipse.lsp4j.debug.StoppedEventArgumentsReason;
import org.junit.jupiter.api.Test;

class AutomaticReloadTest extends BaseTest {

private void addRoute(CamelContext context, String startEndpointUri) throws Exception {
System.out.println("add route");
context.addRoutes(new RouteBuilder() {

@Override
public void configure() throws Exception {
from(startEndpointUri)
.log("Log from test 1"); // XXX-breakpoint-XXX
}
});
}

@Test
void testAutomaticReloadOfRoutes() throws Exception {
try (CamelContext context = new DefaultCamelContext()) {
String startEndpointUri = "direct:testAutoReload";

addRoute(context, startEndpointUri);

context.start();
initDebugger();
attach(server);
SetBreakpointsArguments setBreakpointsArguments = createSetBreakpointArgument("XXX-breakpoint-XXX");

server.setBreakpoints(setBreakpointsArguments).get();

DefaultProducerTemplate producerTemplate = DefaultProducerTemplate.newInstance(context, startEndpointUri);
producerTemplate.start();

CompletableFuture<Object> asyncSendBody1 = producerTemplate.asyncSendBody(startEndpointUri, "a body 1");
waitBreakpointNotification(1);
awaitAllVariablesFilled(0);
StoppedEventArguments stoppedEventArgument = clientProxy.getStoppedEventArguments().get(0);
assertThat(stoppedEventArgument.getReason()).isEqualTo(StoppedEventArgumentsReason.BREAKPOINT);

//TODO how to trigger/simulate the live reload? stop/remove/add is simulating what is happening with Camel JBang. But not with quarkus:dev which is restarting context.
String routeId = context.getRoutes().get(0).getId();
context.getRouteController().stopRoute(routeId);
assertThat(context.removeRoute(routeId)).isTrue();
Thread.sleep(1000); // we have a know hole, if the JMX service is down less than a second
addRoute(context, startEndpointUri);

Thread.sleep(1000); // find a conditional wait for breakpoint to be added back


producerTemplate.asyncSendBody(startEndpointUri, "a body 2");
waitBreakpointNotification(2);
awaitAllVariablesFilled(1);
StoppedEventArguments stoppedEventArgument2 = clientProxy.getStoppedEventArguments().get(1);
assertThat(stoppedEventArgument2.getReason()).isEqualTo(StoppedEventArgumentsReason.BREAKPOINT);
}
}

@Test
void testAutomaticReloadOfContext() throws Exception {
String startEndpointUri = "direct:testAutoReload";
try (CamelContext context = new DefaultCamelContext()) {

addRoute(context, startEndpointUri);

context.start();
initDebugger();
attach(server);
SetBreakpointsArguments setBreakpointsArguments = createSetBreakpointArgument("XXX-breakpoint-XXX");

server.setBreakpoints(setBreakpointsArguments).get();

DefaultProducerTemplate producerTemplate = DefaultProducerTemplate.newInstance(context, startEndpointUri);
producerTemplate.start();

CompletableFuture<Object> asyncSendBody1 = producerTemplate.asyncSendBody(startEndpointUri, "a body 1");
waitBreakpointNotification(1);
awaitAllVariablesFilled(0);
StoppedEventArguments stoppedEventArgument = clientProxy.getStoppedEventArguments().get(0);
assertThat(stoppedEventArgument.getReason()).isEqualTo(StoppedEventArgumentsReason.BREAKPOINT);

// TODO how to trigger/simulate the live reload? stop context/start context/ add route again is simulating
// what is happening with Camel Quarkus.
System.out.println("Will stop context");
context.stop();
System.out.println("context stopped");
Thread.sleep(1000); // we have a known hole, if the JMX service is down less than a second
}
try (CamelContext context = new DefaultCamelContext()) {
addRoute(context, startEndpointUri);

Thread.sleep(1000); // find a conditional wait for breakpoint to be added back

DefaultProducerTemplate producerTemplate = DefaultProducerTemplate.newInstance(context, startEndpointUri);
producerTemplate.start();
producerTemplate.asyncSendBody(startEndpointUri, "a body 2");
waitBreakpointNotification(2, 5);
awaitAllVariablesFilled(1);
StoppedEventArguments stoppedEventArgument2 = clientProxy.getStoppedEventArguments().get(1);
assertThat(stoppedEventArgument2.getReason()).isEqualTo(StoppedEventArgumentsReason.BREAKPOINT);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ void tearDown() {
}

protected void waitBreakpointNotification(int numberOfBreakpointNotifications) {
waitBreakpointNotification(numberOfBreakpointNotifications, 3);
}

protected void waitBreakpointNotification(int numberOfBreakpointNotifications, int waitTime) {
await("Wait that breakpoint hit is notified")
.atMost(Duration.ofSeconds(3))
.atMost(Duration.ofSeconds(waitTime))
.until(() -> {
return clientProxy.getStoppedEventArguments().size() == numberOfBreakpointNotifications;
});
Expand Down

0 comments on commit 233a05c

Please sign in to comment.