allthingssecurity commented on code in PR #30912:
URL: https://github.com/apache/camel/pull/30912#discussion_r4717086784
##########
core/camel-support/src/main/java/org/apache/camel/support/cluster/RebalancingCamelClusterService.java:
##########
@@ -47,11 +48,15 @@ public class RebalancingCamelClusterService extends
ServiceSupport implements Ca
private static final Logger LOG =
LoggerFactory.getLogger(RebalancingCamelClusterService.class);
- protected ScheduledExecutorService serializedExecutor;
+ // set to null by doStop: a pass that runs on an executor that is no
longer this one does not schedule the next pass
+ protected volatile ScheduledExecutorService serializedExecutor;
protected CamelPreemptiveClusterService delegate;
protected CamelContext camelContext;
protected final long periodMillis;
+ // whether the previous pass failed (only used by the passes, one at a
time)
+ private boolean previousPassFailed;
Review Comment:
Fixed in 778242c5 by confining the state rather than making it volatile. The
doStart write was already ordered by executor.execute; the real race was a pass
from before a stop that was still running after doStop's bounded wait. Each
start now gets its own PassState, passed along its chain of passes, so nothing
is shared between starts.
##########
core/camel-support/src/main/java/org/apache/camel/support/cluster/RebalancingCamelClusterService.java:
##########
@@ -104,13 +114,40 @@ public void setDelegate(CamelPreemptiveClusterService
delegate) {
this.delegate = delegate;
}
+ /**
+ * Runs a rebalancing pass, and schedules the next one on the same
executor while it is the executor of this
+ * service, also when the pass fails.
+ */
+ private void reconcileAndReschedule(ScheduledExecutorService executor) {
+ try {
+ reconcile();
+ previousPassFailed = false;
+ } catch (Throwable e) {
Review Comment:
Kept on purpose: rethrowing from a scheduled task only ends up in its
Future, so nothing would log it and rebalancing would stop for good. Since
e3be62b8 a VirtualMachineError is logged once at ERROR with its stack trace and
the loop goes on.
##########
core/camel-support/src/main/java/org/apache/camel/support/cluster/RebalancingCamelClusterService.java:
##########
@@ -104,13 +114,40 @@ public void setDelegate(CamelPreemptiveClusterService
delegate) {
this.delegate = delegate;
}
+ /**
+ * Runs a rebalancing pass, and schedules the next one on the same
executor while it is the executor of this
+ * service, also when the pass fails.
+ */
+ private void reconcileAndReschedule(ScheduledExecutorService executor) {
+ try {
+ reconcile();
+ previousPassFailed = false;
+ } catch (Throwable e) {
+ if (previousPassFailed) {
+ LOG.debug("Error while rebalancing the partitions (will try
again in {} ms)", periodMillis, e);
+ } else {
+ LOG.warn("Error while rebalancing the partitions (will try
again in {} ms): {}", periodMillis,
+ e.toString());
Review Comment:
Fixed in e3be62b8: the exception is the last argument now, so WARN and ERROR
carry the stack trace.
##########
core/camel-support/src/main/java/org/apache/camel/support/cluster/RebalancingCamelClusterService.java:
##########
@@ -104,13 +125,45 @@ public void setDelegate(CamelPreemptiveClusterService
delegate) {
this.delegate = delegate;
}
+ /**
+ * Runs a rebalancing pass, and schedules the next one on the same
executor while it is the executor of this
Review Comment:
On e3be62b8 the first failure in a row is logged at WARN (or ERROR) with the
full stack trace; only repeats in a row go to DEBUG.
##########
core/camel-core/src/test/java/org/apache/camel/cluster/RebalancingClusterServiceLoopTest.java:
##########
@@ -0,0 +1,227 @@
+/*
+ * 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.cluster;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Optional;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.support.cluster.AbstractCamelClusterService;
+import org.apache.camel.support.cluster.AbstractCamelClusterView;
+import org.apache.camel.support.cluster.RebalancingCamelClusterService;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The passes of the rebalancing cluster service form one chain: a failing
pass does not end it, and a stop and start of
+ * the service does not leave the chain of the previous start running next to
the new one.
+ */
+class RebalancingClusterServiceLoopTest {
+
+ private static final long PERIOD_MILLIS = 200;
+
+ private CamelContext context;
+ private TestClusterService delegate;
+ private CountingRebalancingClusterService service;
+
+ @BeforeEach
+ void setUp() throws Exception {
+ context = new DefaultCamelContext();
+ delegate = new TestClusterService("node-1");
+ service = new CountingRebalancingClusterService(delegate);
+ context.addService(service);
+ }
+
+ @AfterEach
+ void tearDown() {
+ context.stop();
+ }
+
+ @Test
+ void failingPassDoesNotEndRebalancing() throws Exception {
+ context.start();
+ delegate.getView("my-ns");
+ await().atMost(10, TimeUnit.SECONDS).until(() -> service.passes.get()
>= 2);
+
+ delegate.failures.set(1);
+ await().atMost(10, TimeUnit.SECONDS).until(() ->
delegate.failures.get() == 0);
+ int passes = service.passes.get();
+
+ // the next passes run, and they still rebalance: a disabled partition
of the only member is opened
+ TestClusterView view = delegate.view("my-ns");
+ view.setDisabled(true);
+ await().atMost(10, TimeUnit.SECONDS).until(() -> service.passes.get()
>= passes + 3 && !view.isDisabled());
+ }
+
+ @Test
+ void restartDoesNotAddChainOfPasses() throws Exception {
+ context.start();
+ delegate.getView("my-ns");
+ await().atMost(10, TimeUnit.SECONDS).until(() -> service.passes.get()
>= 2);
+
+ for (int i = 0; i < 3; i++) {
+ service.stop();
+ Thread stopped = service.lastThread;
+ service.threads.clear();
+ service.start();
+ await().atMost(10, TimeUnit.SECONDS).until(() ->
service.threads.size() >= 3);
+ // no pass starts on the executor of the previous start once the
service is stopped
+ assertTrue(service.threads.stream().noneMatch(t -> t == stopped),
+ "A pass ran on the executor of the previous start after
the stop");
+ }
+
+ // one chain: at most one pass per period (fewer on a busy machine),
and a few more for the window edges
+ int before = service.passes.get();
+ long start = System.nanoTime();
+ await().during(2, TimeUnit.SECONDS).atMost(10,
TimeUnit.SECONDS).until(() -> service.passes.get() - before
+
<= TimeUnit.NANOSECONDS.toMillis(
+
System.nanoTime() - start)
+
/ PERIOD_MILLIS + 3);
+ assertEquals(1, service.maxConcurrent.get(), "Passes ran at the same
time");
Review Comment:
Fixed in e3be62b8: doStop now waits up to periodMillis for the running pass
after shutdownNow, and stopWaitsForTheRunningPass covers it.
##########
core/camel-core/src/test/java/org/apache/camel/cluster/RebalancingClusterServiceLoopTest.java:
##########
@@ -0,0 +1,331 @@
+/*
+ * 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.cluster;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Optional;
+import java.util.Queue;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.support.cluster.AbstractCamelClusterService;
+import org.apache.camel.support.cluster.AbstractCamelClusterView;
+import org.apache.camel.support.cluster.RebalancingCamelClusterService;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.config.Property;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The passes of the rebalancing cluster service form one chain: a failing
pass does not end it, and a stop and start of
+ * the service does not leave the chain of the previous start running next to
the new one.
+ */
+class RebalancingClusterServiceLoopTest {
+
+ private static final long PERIOD_MILLIS = 200;
+
+ private CamelContext context;
+ private TestClusterService delegate;
+ private CountingRebalancingClusterService service;
+
+ @BeforeEach
+ void setUp() throws Exception {
+ context = new DefaultCamelContext();
+ delegate = new TestClusterService("node-1");
+ service = new CountingRebalancingClusterService(delegate);
+ context.addService(service);
+ }
+
+ @AfterEach
+ void tearDown() {
+ context.stop();
+ }
+
+ @Test
+ void failingPassDoesNotEndRebalancing() throws Exception {
+ context.start();
+ delegate.getView("my-ns");
+ await().atMost(10, TimeUnit.SECONDS).until(() -> service.passes.get()
>= 2);
+
+ delegate.failures.set(1);
+ await().atMost(10, TimeUnit.SECONDS).until(() ->
delegate.failures.get() == 0);
+ int passes = service.passes.get();
+
+ // the next passes run, and they still rebalance: a disabled partition
of the only member is opened
+ TestClusterView view = delegate.view("my-ns");
+ view.setDisabled(true);
+ await().atMost(10, TimeUnit.SECONDS).until(() -> service.passes.get()
>= passes + 3 && !view.isDisabled());
+ }
+
+ @Test
+ void restartDoesNotAddChainOfPasses() throws Exception {
+ context.start();
+ delegate.getView("my-ns");
+ await().atMost(10, TimeUnit.SECONDS).until(() -> service.passes.get()
>= 2);
+
+ for (int i = 0; i < 3; i++) {
+ service.stop();
+ Thread stopped = service.lastThread;
+ service.threads.clear();
+ service.start();
Review Comment:
Added a comment in 778242c5: one chain starts at most elapsed/period + 1
passes, the rest is slack for reading the counter and the clock apart, and a
second chain would add about 10 passes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]