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

Optimize EndpointHelper.matchEndpoint #11723

Merged
merged 3 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,20 @@ public Endpoint addEndpoint(String uri, Endpoint endpoint) throws Exception {

@Override
public void removeEndpoint(Endpoint endpoint) throws Exception {
removeEndpoints(endpoint.getEndpointUri());
// optimize as uri on endpoint is already normalized
String uri = endpoint.getEndpointUri();
NormalizedUri key = NormalizedUri.newNormalizedUri(uri, true);
Endpoint oldEndpoint = endpoints.remove(key);
if (oldEndpoint != null) {
try {
stopServices(oldEndpoint);
} catch (Exception e) {
LOG.warn("Error stopping endpoint {}. This exception will be ignored.", oldEndpoint, e);
}
for (LifecycleStrategy strategy : lifecycleStrategies) {
strategy.onEndpointRemove(oldEndpoint);
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,76 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.camel.Endpoint;
import org.apache.camel.FluentProducerTemplate;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.ServiceStatus;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.engine.DefaultEndpointRegistry;
import org.apache.camel.impl.engine.SimpleCamelContext;
import org.apache.camel.spi.EndpointRegistry;
import org.apache.camel.support.NormalizedUri;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class DefaultEndpointRegistryTest {

@Test
public void testRemoveEndpoint() throws Exception {
DefaultCamelContext ctx = new DefaultCamelContext();
ctx.start();

ctx.getEndpoint("direct:one");
Endpoint e = ctx.getEndpoint("direct:two");
ctx.getEndpoint("direct:three");

Assertions.assertEquals(3, ctx.getEndpoints().size());
ctx.removeEndpoint(e);
Assertions.assertEquals(2, ctx.getEndpoints().size());
}

@Test
public void testRemoveEndpointToD() throws Exception {
DefaultCamelContext ctx = new DefaultCamelContext();
ctx.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start")
.toD().cacheSize(10).uri("mock:${header.foo}");
}
});
final AtomicInteger cnt = new AtomicInteger();
ctx.addLifecycleStrategy(new DummyLifecycleStrategy() {
@Override
public void onEndpointRemove(Endpoint endpoint) {
cnt.incrementAndGet();
}
});
ctx.start();

Assertions.assertEquals(0, cnt.get());
Assertions.assertEquals(1, ctx.getEndpoints().size());

FluentProducerTemplate template = ctx.createFluentProducerTemplate();
for (int i = 0; i < 100; i++) {
template.withBody("Hello").withHeader("foo", "" + i).to("direct:start").send();
}

Awaitility.await().untilAsserted(() -> {
Assertions.assertEquals(11, ctx.getEndpoints().size());
});

Assertions.assertEquals(90, cnt.get());

ctx.stop();
}

@Test
public void testMigration() throws Exception {
DefaultCamelContext ctx = new DefaultCamelContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public static void pollEndpoint(Endpoint endpoint, Processor processor) throws E
* @param context the Camel context, if <tt>null</tt> then property placeholder resolution is skipped.
* @param uri the endpoint uri
* @param pattern a pattern to match
* @return <tt>true</tt> if match, <tt>false</tt> otherwise.
* @return <tt>true</tt> if matched, <tt>false</tt> otherwise.
*/
public static boolean matchEndpoint(CamelContext context, String uri, String pattern) {
if (context != null) {
Expand All @@ -235,18 +235,31 @@ public static boolean matchEndpoint(CamelContext context, String uri, String pat
// normalize uri so we can do endpoint hits with minor mistakes and parameters is not in the same order
uri = normalizeEndpointUri(uri);

// we need to test with and without scheme separators (//)
boolean match = PatternHelper.matchPattern(toggleUriSchemeSeparators(uri), pattern);
match |= PatternHelper.matchPattern(uri, pattern);
if (!match && pattern != null && pattern.contains("?")) {
// try normalizing the pattern as a uri for exact matching, so parameters are ordered the same as in the endpoint uri
// do fast matching without regexp first
boolean match = doMatchEndpoint(uri, pattern, false);
if (!match) {
// this is slower as pattern is compiled as regexp
match = doMatchEndpoint(uri, pattern, true);
}
return match;
}

private static boolean doMatchEndpoint(String uri, String pattern, boolean regexp) {
String toggleUri = null;
boolean match = regexp ? PatternHelper.matchRegex(uri, pattern) : PatternHelper.matchPattern(uri, pattern);
if (!match) {
toggleUri = toggleUriSchemeSeparators(uri);
match = regexp ? PatternHelper.matchRegex(toggleUri, pattern) : PatternHelper.matchPattern(toggleUri, pattern);
}
if (!match && !regexp && pattern != null && pattern.contains("?")) {
// this is only need to be done once (in fast mode when regexp=false)
// try normalizing the pattern as an uri for exact matching, so parameters are ordered the same as in the endpoint uri
try {
pattern = URISupport.normalizeUri(pattern);
// try both with and without scheme separators (//)
match = toggleUriSchemeSeparators(uri).equalsIgnoreCase(pattern);
return match || uri.equalsIgnoreCase(pattern);
return uri.equalsIgnoreCase(pattern) || toggleUri.equalsIgnoreCase(pattern);
} catch (URISyntaxException e) {
//Can't normalize and original match failed
// cannot normalize and original match failed
return false;
} catch (Exception e) {
throw new ResolveEndpointFailedException(uri, e);
Expand Down Expand Up @@ -278,7 +291,7 @@ private static String toggleUriSchemeSeparators(String normalizedUri) {
* Is the given parameter a reference parameter (starting with a # char)
*
* @param parameter the parameter
* @return <tt>true</tt> if its a reference parameter
* @return <tt>true</tt> if it's a reference parameter
*/
public static boolean isReferenceParameter(String parameter) {
return parameter != null && parameter.trim().startsWith("#") && parameter.trim().length() > 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private static boolean matchWildcard(String name, String pattern) {
* @param pattern a pattern to match
* @return <tt>true</tt> if match, <tt>false</tt> otherwise.
*/
private static boolean matchRegex(String name, String pattern) {
public static boolean matchRegex(String name, String pattern) {
// match by regular expression
try {
Pattern compiled = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
Expand Down