Repository navigation
CAMEL-25823: camel-core - Start Split and Multicast output producers when the route starts #28147
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
Open
gnodet
wants to merge
6
commits into
apache:main
Choose a base branch
from
gnodet:camel-25823-multicast-split-deferred-start
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+230
−4
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5d8407a
CAMEL-25823: camel-core - Defer error handler start in MulticastProce…
gnodet 6dde78b
CAMEL-25823: address review: use isRunAllowed() gate, fix errorHandle…
gnodet 12ef72d
test: fix split delimiter in testSplitWithFlakyProducer (use comma, n…
gnodet 95d444a
Address review: fix cache-before-start race, line length, upgrade gui…
gnodet 5074998
fix: correct isRunAllowed() phase comment to include SUSPENDING
gnodet 895e821
fix: log cut-off WARN in SimpleTask.runNotAllowed when error handler …
gnodet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
...ava/org/apache/camel/impl/engine/DefaultSupervisingRouteControllerMulticastSplitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| /* | ||
| * 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 | ||
| * | ||
| * http://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 org.apache.camel.impl.engine; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.Map; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import org.apache.camel.CamelContext; | ||
| import org.apache.camel.Component; | ||
| import org.apache.camel.Consumer; | ||
| import org.apache.camel.ContextTestSupport; | ||
| import org.apache.camel.Endpoint; | ||
| import org.apache.camel.Exchange; | ||
| import org.apache.camel.Processor; | ||
| import org.apache.camel.Producer; | ||
| import org.apache.camel.ServiceStatus; | ||
| import org.apache.camel.builder.RouteBuilder; | ||
| import org.apache.camel.spi.SupervisingRouteController; | ||
| import org.apache.camel.support.DefaultComponent; | ||
| import org.apache.camel.support.DefaultConsumer; | ||
| import org.apache.camel.support.DefaultEndpoint; | ||
| import org.apache.camel.support.DefaultProducer; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.awaitility.Awaitility.await; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| /** | ||
| * Tests that Split and Multicast EIPs do not start child producers during doInit(), which would bypass the | ||
| * SupervisingRouteController retry logic and fail the entire context when a producer cannot connect yet. | ||
| * <p> | ||
| * CAMEL-25823: Split and Multicast start their child producers before CamelContext starts, so with the supervising | ||
| * route controller a producer that cannot start yet fails the whole context. | ||
| */ | ||
| class DefaultSupervisingRouteControllerMulticastSplitTest extends ContextTestSupport { | ||
|
|
||
| @Override | ||
| public boolean isUseRouteBuilder() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| protected CamelContext createCamelContext() throws Exception { | ||
| CamelContext context = super.createCamelContext(); | ||
| SupervisingRouteController src = context.getRouteController().supervising(); | ||
| src.setBackOffDelay(25); | ||
| src.setBackOffMaxAttempts(10); | ||
| src.setInitialDelay(10); | ||
| src.setThreadPoolSize(2); | ||
| return context; | ||
| } | ||
|
|
||
| @Test | ||
| void testSplitWithFlakyProducer() throws Exception { | ||
| FlakyComponent component = new FlakyComponent(2); | ||
| context.addComponent("flaky", component); | ||
| context.addRoutes(new RouteBuilder() { | ||
| @Override | ||
| public void configure() { | ||
| from("direct:split") | ||
| .routeId("split-route") | ||
| .split(body()) | ||
| .to("flaky:target"); | ||
| } | ||
| }); | ||
|
|
||
| // Must not throw - supervising route controller should retry | ||
| context.start(); | ||
|
|
||
| // Route should eventually reach Started state after retries | ||
| await().atMost(Duration.ofSeconds(5)) | ||
| .untilAsserted(() -> assertEquals(ServiceStatus.Started, | ||
| context.getRouteController().getRouteStatus("split-route"))); | ||
|
|
||
| // Verify the producer was retried (failCount=2 means 3 start attempts total) | ||
| FlakyEndpoint endpoint = (FlakyEndpoint) context.getEndpoint("flaky:target"); | ||
| assertEquals(3, endpoint.startAttempts.get(), "Expected 3 start attempts (2 failures + 1 success)"); | ||
|
|
||
| // Verify that a message can actually be processed once the route is started | ||
| template.sendBody("direct:split", "a,b,c"); | ||
| assertEquals(3, endpoint.processCount.get(), "Expected 3 process calls for 3 split tokens"); | ||
| } | ||
|
|
||
| @Test | ||
| void testMulticastWithFlakyProducer() throws Exception { | ||
| FlakyComponent component = new FlakyComponent(2); | ||
| context.addComponent("flaky", component); | ||
| context.addRoutes(new RouteBuilder() { | ||
| @Override | ||
| public void configure() { | ||
| from("direct:multicast") | ||
| .routeId("multicast-route") | ||
| .multicast() | ||
| .to("flaky:target"); | ||
| } | ||
| }); | ||
|
|
||
| // Must not throw - supervising route controller should retry | ||
| context.start(); | ||
|
|
||
| // Route should eventually reach Started state after retries | ||
| await().atMost(Duration.ofSeconds(5)) | ||
| .untilAsserted(() -> assertEquals(ServiceStatus.Started, | ||
| context.getRouteController().getRouteStatus("multicast-route"))); | ||
|
|
||
| // Verify the producer was retried (failCount=2 means 3 start attempts total) | ||
| FlakyEndpoint endpoint = (FlakyEndpoint) context.getEndpoint("flaky:target"); | ||
| assertEquals(3, endpoint.startAttempts.get(), "Expected 3 start attempts (2 failures + 1 success)"); | ||
|
|
||
| // Verify that a message can actually be processed once the route is started | ||
| template.sendBody("direct:multicast", "hello"); | ||
| assertEquals(1, endpoint.processCount.get(), "Expected 1 process call for multicast to single target"); | ||
| } | ||
|
|
||
| /** | ||
| * A component whose producer fails its first {@code failCount} doStart() calls with an exception, simulating a | ||
| * broker that is temporarily unavailable. The failure count is tracked on the endpoint (shared across producer | ||
| * instances) so retries eventually succeed even when a new producer is created on each restart attempt. | ||
| */ | ||
| static class FlakyComponent extends DefaultComponent { | ||
|
|
||
| private final int failCount; | ||
|
|
||
| FlakyComponent(int failCount) { | ||
| this.failCount = failCount; | ||
| } | ||
|
|
||
| @Override | ||
| protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) { | ||
| return new FlakyEndpoint(uri, this, failCount); | ||
| } | ||
| } | ||
|
|
||
| static class FlakyEndpoint extends DefaultEndpoint { | ||
|
|
||
| final AtomicInteger startAttempts = new AtomicInteger(); | ||
| final AtomicInteger processCount = new AtomicInteger(); | ||
| private final int failCount; | ||
|
|
||
| FlakyEndpoint(String uri, Component component, int failCount) { | ||
| super(uri, component); | ||
| this.failCount = failCount; | ||
| } | ||
|
|
||
| @Override | ||
| public Producer createProducer() { | ||
| return new FlakyProducer(this); | ||
| } | ||
|
|
||
| @Override | ||
| public Consumer createConsumer(Processor processor) { | ||
| return new DefaultConsumer(this, processor); | ||
| } | ||
|
|
||
| boolean shouldFail() { | ||
| return startAttempts.incrementAndGet() <= failCount; | ||
| } | ||
| } | ||
|
|
||
| static class FlakyProducer extends DefaultProducer { | ||
|
|
||
| FlakyProducer(FlakyEndpoint endpoint) { | ||
| super(endpoint); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doStart() throws Exception { | ||
| FlakyEndpoint ep = (FlakyEndpoint) getEndpoint(); | ||
| if (ep.shouldFail()) { | ||
| throw new IllegalStateException("Cannot connect"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void process(Exchange exchange) { | ||
| FlakyEndpoint ep = (FlakyEndpoint) getEndpoint(); | ||
| ep.processCount.incrementAndGet(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.