Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP FUSETOOLS2-1624 - Support automatic restart #93

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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