[
https://issues.apache.org/jira/browse/CAMEL-12075?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16288931#comment-16288931
]
ASF GitHub Bot commented on CAMEL-12075:
----------------------------------------
davsclaus closed pull request #2142: CAMEL-12075: aggregator thread does not
finish in iterating splitter
URL: https://github.com/apache/camel/pull/2142
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/camel-core/src/main/java/org/apache/camel/processor/MulticastProcessor.java
b/camel-core/src/main/java/org/apache/camel/processor/MulticastProcessor.java
index f924561a588..90a6f7b9013 100644
---
a/camel-core/src/main/java/org/apache/camel/processor/MulticastProcessor.java
+++
b/camel-core/src/main/java/org/apache/camel/processor/MulticastProcessor.java
@@ -301,56 +301,62 @@ protected void doProcessParallel(final Exchange original,
final AtomicExchange r
final AtomicBoolean aggregationTaskSubmitted = new AtomicBoolean();
LOG.trace("Starting to submit parallel tasks");
-
- while (it.hasNext()) {
- final ProcessorExchangePair pair = it.next();
- // in case the iterator returns null then continue to next
- if (pair == null) {
- continue;
- }
-
- final Exchange subExchange = pair.getExchange();
- updateNewExchange(subExchange, total.intValue(), pairs, it);
-
- completion.submit(new Callable<Exchange>() {
- public Exchange call() throws Exception {
- // start the aggregation task at this stage only in
order not to pile up too many threads
- if (aggregationTaskSubmitted.compareAndSet(false,
true)) {
- // but only submit the aggregation task once
-
aggregateExecutorService.submit(aggregateOnTheFlyTask);
- }
-
- if (!running.get()) {
- // do not start processing the task if we are not
running
- return subExchange;
- }
-
- try {
- doProcessParallel(pair);
- } catch (Throwable e) {
- subExchange.setException(e);
- }
-
- // Decide whether to continue with the multicast or
not; similar logic to the Pipeline
- Integer number = getExchangeIndex(subExchange);
- boolean continueProcessing =
PipelineHelper.continueProcessing(subExchange, "Parallel processing failed for
number " + number, LOG);
- if (stopOnException && !continueProcessing) {
- // signal to stop running
- running.set(false);
- // throw caused exception
- if (subExchange.getException() != null) {
- // wrap in exception to explain where it failed
- CamelExchangeException cause = new
CamelExchangeException("Parallel processing failed for number " + number,
subExchange, subExchange.getException());
- subExchange.setException(cause);
+
+ try {
+ while (it.hasNext()) {
+ final ProcessorExchangePair pair = it.next();
+ // in case the iterator returns null then continue to next
+ if (pair == null) {
+ continue;
+ }
+
+ final Exchange subExchange = pair.getExchange();
+ updateNewExchange(subExchange, total.intValue(), pairs,
it);
+
+ completion.submit(new Callable<Exchange>() {
+ public Exchange call() throws Exception {
+ // start the aggregation task at this stage only
in order not to pile up too many threads
+ if (aggregationTaskSubmitted.compareAndSet(false,
true)) {
+ // but only submit the aggregation task once
+
aggregateExecutorService.submit(aggregateOnTheFlyTask);
+ }
+
+ if (!running.get()) {
+ // do not start processing the task if we are
not running
+ return subExchange;
+ }
+
+ try {
+ doProcessParallel(pair);
+ } catch (Throwable e) {
+ subExchange.setException(e);
+ }
+
+ // Decide whether to continue with the multicast
or not; similar logic to the Pipeline
+ Integer number = getExchangeIndex(subExchange);
+ boolean continueProcessing =
PipelineHelper.continueProcessing(subExchange, "Parallel processing failed for
number " + number, LOG);
+ if (stopOnException && !continueProcessing) {
+ // signal to stop running
+ running.set(false);
+ // throw caused exception
+ if (subExchange.getException() != null) {
+ // wrap in exception to explain where it
failed
+ CamelExchangeException cause = new
CamelExchangeException("Parallel processing failed for number " + number,
subExchange, subExchange.getException());
+ subExchange.setException(cause);
+ }
}
+
+ LOG.trace("Parallel processing complete for
exchange: {}", subExchange);
+ return subExchange;
}
-
- LOG.trace("Parallel processing complete for exchange:
{}", subExchange);
- return subExchange;
- }
- });
-
- total.incrementAndGet();
+ });
+
+ total.incrementAndGet();
+ }
+ } catch (RuntimeException e) {
+ // The methods it.hasNext and it.next can throw
RuntimeExceptions when custom iterators are implemented.
+ // We have to catch the exception here otherwise the
aggregator threads would pile up.
+ executionException.set(e);
}
// signal all tasks has been submitted
diff --git
a/camel-core/src/test/java/org/apache/camel/issues/SplitterParallelRuntimeExceptionInHasNextOrNext.java
b/camel-core/src/test/java/org/apache/camel/issues/SplitterParallelRuntimeExceptionInHasNextOrNext.java
new file mode 100644
index 00000000000..948aada23e2
--- /dev/null
+++
b/camel-core/src/test/java/org/apache/camel/issues/SplitterParallelRuntimeExceptionInHasNextOrNext.java
@@ -0,0 +1,155 @@
+/**
+ * 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.issues;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+public class SplitterParallelRuntimeExceptionInHasNextOrNext extends
ContextTestSupport {
+
+ /**
+ * Tests that only one aggregator thread is created if a RuntimeException
in
+ * the hasNext method of a custom iterator occurs.
+ */
+ @Test
+ public void testSplitErrorInHasNext() throws Exception {
+
+ execute("direct:errorInHasNext");
+ }
+
+ /**
+ * Tests that only one aggregator thread is created if a RuntimeException
in
+ * the next method of a custom iterator occurs.
+ */
+ @Test
+ public void testSplitErrorInNext() throws Exception {
+
+ execute("direct:errorInNext");
+ }
+
+ private void execute(String from) throws InterruptedException {
+ for (int i = 0; i < 10; i++) {
+ try {
+ template.sendBody(from, "some content");
+ } catch (Exception e) {
+ // expected due to runtime exception in hasNext method
+ assertTrue(e.getMessage().startsWith("Exception occurred"));
+ }
+ assertMockEndpointsSatisfied();
+ }
+ List<Thread> aggregatorThreads = getAggregatorThreads();
+ assertEquals(1, aggregatorThreads.size());
+ }
+
+ private List<Thread> getAggregatorThreads() {
+ List<Thread> result = new ArrayList<>();
+ for (Thread t : Thread.getAllStackTraces().keySet()) {
+ if (t.getName().endsWith("Splitter-AggregateTask")) {
+ result.add(t);
+ }
+ }
+ return result;
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+
+
from("direct:errorInHasNext").split().method(SplitterImpl.class,
"errorInHasNext").streaming().parallelProcessing(true).to("mock:split1");
+
+ from("direct:errorInNext").split().method(SplitterImpl.class,
"errorInNext").streaming().parallelProcessing(true).to("mock:split2");
+
+ }
+ };
+ }
+
+ public static class SplitterImpl {
+
+ public Iterator<String> errorInHasNext(InputStream request, Exchange
exchange) {
+
+ return new CustomIterator(exchange, request, true);
+ }
+
+ public Iterator<String> errorInNext(InputStream request, Exchange
exchange) {
+
+ return new CustomIterator(exchange, request, false);
+ }
+
+ }
+
+ static class CustomIterator implements Iterator<String>, Closeable {
+
+ private int index = 0;
+ private InputStream request;
+ private boolean errorInHasNext;
+
+ CustomIterator(Exchange exchange, InputStream request, boolean
errorInHasNext) {
+
+ this.request = request;
+ this.errorInHasNext = errorInHasNext;
+
+ }
+
+ @Override
+ public boolean hasNext() {
+ if (index < 7) {
+ return true;
+ }
+ if (errorInHasNext) {
+ throw new RuntimeException("Exception thrown");
+ } else {
+ return false;
+ }
+ }
+
+ @Override
+ public String next() {
+ index++;
+
+ if (index < 7) {
+ return "<a>" + index + "</a>";
+ }
+ if (!errorInHasNext) {
+ throw new RuntimeException("Exception thrown");
+ } else {
+ return "<a>" + index + "</a>";
+ }
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void close() throws IOException {
+ request.close();
+ }
+ }
+
+}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Piling up of threads in iterating splitter in pararllel processing
> ------------------------------------------------------------------
>
> Key: CAMEL-12075
> URL: https://issues.apache.org/jira/browse/CAMEL-12075
> Project: Camel
> Issue Type: Bug
> Components: camel-core
> Affects Versions: 2.18.5, 2.19.4, 2.20.1
> Reporter: Franz Forsthofer
> Fix For: 2.19.5, 2.20.2, 2.21.0
>
>
> If you have a custom interating splitter which runs in parallel processing
> and which throws a RuntimeException either in the "hasNext" or "next" method
> after an aggregation thread was created then the aggregation thread will
> never finish.
> Therefore if the error occurs mulitple times the aggregatgor threads pile up.
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)