-
Notifications
You must be signed in to change notification settings - Fork 5.1k
CAMEL-23352: Add syncOptimisticRetry option to Aggregate EIP #22769
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -90,6 +90,9 @@ public class AggregateDefinition extends OutputDefinition<AggregateDefinition> | |||||||||||||||||||||
| @Metadata(javaType = "java.lang.Boolean") | ||||||||||||||||||||||
| private String optimisticLocking; | ||||||||||||||||||||||
| @XmlAttribute | ||||||||||||||||||||||
| @Metadata(label = "advanced", javaType = "java.lang.Boolean", defaultValue = "false") | ||||||||||||||||||||||
| private String syncOptimisticRetry; | ||||||||||||||||||||||
| @XmlAttribute | ||||||||||||||||||||||
| @Metadata(label = "advanced", javaType = "java.util.concurrent.ExecutorService") | ||||||||||||||||||||||
| private String executorService; | ||||||||||||||||||||||
| @XmlAttribute | ||||||||||||||||||||||
|
|
@@ -173,6 +176,7 @@ protected AggregateDefinition(AggregateDefinition source) { | |||||||||||||||||||||
| ? source.optimisticLockRetryPolicyDefinition.copyDefinition() : null; | ||||||||||||||||||||||
| this.parallelProcessing = source.parallelProcessing; | ||||||||||||||||||||||
| this.optimisticLocking = source.optimisticLocking; | ||||||||||||||||||||||
| this.syncOptimisticRetry = source.syncOptimisticRetry; | ||||||||||||||||||||||
| this.executorService = source.executorService; | ||||||||||||||||||||||
| this.timeoutCheckerExecutorService = source.timeoutCheckerExecutorService; | ||||||||||||||||||||||
| this.aggregateController = source.aggregateController; | ||||||||||||||||||||||
|
|
@@ -501,6 +505,14 @@ public void setOptimisticLocking(String optimisticLocking) { | |||||||||||||||||||||
| this.optimisticLocking = optimisticLocking; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public String getSyncOptimisticRetry() { | ||||||||||||||||||||||
| return syncOptimisticRetry; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public void setSyncOptimisticRetry(String syncOptimisticRetry) { | ||||||||||||||||||||||
| this.syncOptimisticRetry = syncOptimisticRetry; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| public String getParallelProcessing() { | ||||||||||||||||||||||
| return parallelProcessing; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
@@ -1004,6 +1016,16 @@ public AggregateDefinition optimisticLocking() { | |||||||||||||||||||||
| return this; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * When optimistic locking is enabled, retries happen synchronously in the same thread instead of being scheduled on | ||||||||||||||||||||||
| * a background thread. This preserves transaction context for repositories that require single-thread transactional | ||||||||||||||||||||||
| * guarantees. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
|
Comment on lines
+1019
to
+1023
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice Javadoc. Consider adding a note that this option only takes effect when
Suggested change
|
||||||||||||||||||||||
| public AggregateDefinition syncOptimisticRetry() { | ||||||||||||||||||||||
| setSyncOptimisticRetry(Boolean.toString(true)); | ||||||||||||||||||||||
| return this; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Allows to configure retry settings when using optimistic locking. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /* | ||
| * 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.processor.aggregator; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import org.apache.camel.CamelContext; | ||
| import org.apache.camel.ContextTestSupport; | ||
| import org.apache.camel.Exchange; | ||
| import org.apache.camel.builder.RouteBuilder; | ||
| import org.apache.camel.component.mock.MockEndpoint; | ||
| import org.apache.camel.processor.BodyInAggregatingStrategy; | ||
| import org.apache.camel.processor.aggregate.MemoryAggregationRepository; | ||
| import org.apache.camel.processor.aggregate.OptimisticLockRetryPolicy; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** | ||
| * Tests that optimistic locking retries happen synchronously in the same thread when syncOptimisticRetry is enabled. | ||
| */ | ||
| public class AggregateOptimisticLockSyncRetryTest extends ContextTestSupport { | ||
|
|
||
| private static final int FAIL_FIRST_N_ATTEMPTS = 3; | ||
|
|
||
| private final AtomicInteger addCounter = new AtomicInteger(); | ||
| private volatile String aggregateThreadName; | ||
|
|
||
| /** | ||
| * Repository that throws OptimisticLockingException for the first N attempts, then succeeds. | ||
| */ | ||
| private final MemoryAggregationRepository repository = new MemoryAggregationRepository(true) { | ||
| @Override | ||
| public Exchange add(CamelContext camelContext, String key, Exchange oldExchange, Exchange newExchange) { | ||
| int count = addCounter.incrementAndGet(); | ||
| // Record the thread name on every attempt | ||
| aggregateThreadName = Thread.currentThread().getName(); | ||
| if (count <= FAIL_FIRST_N_ATTEMPTS) { | ||
| throw new OptimisticLockingException(); | ||
| } | ||
| return super.add(camelContext, key, oldExchange, newExchange); | ||
| } | ||
| }; | ||
|
|
||
| @Test | ||
| public void testSyncRetryHappensInSameThread() throws Exception { | ||
| MockEndpoint mock = getMockEndpoint("mock:result"); | ||
| mock.expectedMessageCount(1); | ||
|
|
||
| String callerThread = Thread.currentThread().getName(); | ||
|
|
||
| template.sendBodyAndHeader("direct:start", "A", "id", 1); | ||
| template.sendBodyAndHeader("direct:start", "B", "id", 1); | ||
|
|
||
| mock.assertIsSatisfied(); | ||
|
|
||
| // The repository should have been called more than FAIL_FIRST_N_ATTEMPTS times | ||
| // (the first N fail, then succeed) | ||
| assertTrue(addCounter.get() > FAIL_FIRST_N_ATTEMPTS, | ||
| "Expected more than " + FAIL_FIRST_N_ATTEMPTS + " attempts, got " + addCounter.get()); | ||
|
|
||
| // Since syncOptimisticRetry is enabled, the retry should happen in a Camel thread | ||
| // (the route's thread), NOT in the AggregateOptimisticLockingExecutor thread pool. | ||
| // The key assertion is that the thread name does NOT contain the async executor name. | ||
| if (aggregateThreadName != null) { | ||
| assertFalse(aggregateThreadName.contains("AggregateOptimisticLockingExecutor"), | ||
| "Expected synchronous retry but found async executor thread: " + aggregateThreadName); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testSyncRetryCompletes() throws Exception { | ||
| MockEndpoint mock = getMockEndpoint("mock:result"); | ||
| mock.expectedBodiesReceived("A+B"); | ||
|
|
||
| template.sendBodyAndHeader("direct:start", "A", "id", 1); | ||
|
|
||
| // Reset counter so next message triggers failures and retries | ||
| addCounter.set(0); | ||
| template.sendBodyAndHeader("direct:start", "B", "id", 1); | ||
|
|
||
| mock.assertIsSatisfied(); | ||
| } | ||
|
|
||
| @Override | ||
| protected RouteBuilder createRouteBuilder() { | ||
| return new RouteBuilder() { | ||
| @Override | ||
| public void configure() { | ||
| from("direct:start") | ||
| .aggregate(header("id"), new BodyInAggregatingStrategy()) | ||
| .aggregationRepository(repository) | ||
| .optimisticLocking() | ||
| .syncOptimisticRetry() | ||
| .optimisticLockRetryPolicy( | ||
| new OptimisticLockRetryPolicy().maximumRetries(10).retryDelay(0)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good test coverage overall. One observation: Consider adding a small test with a nonzero delay (e.g., |
||
| .completionSize(2) | ||
| .to("mock:result"); | ||
| } | ||
| }; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit / naming: consider
optimisticLockingSyncRetry(oroptimisticLockRetrySync) to group it visually with the existingoptimisticLockingfield in docs and configuration.syncOptimisticRetryreads a bit ambiguously ("synchronize" vs "synchronous"). Not a blocker — just a readability suggestion.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the suggested rename of the option