davsclaus commented on code in PR #30818:
URL: https://github.com/apache/camel/pull/30818#discussion_r4706676317


##########
components/camel-infinispan/camel-infinispan/src/main/java/org/apache/camel/component/infinispan/remote/cluster/InfinispanRemoteClusterView.java:
##########
@@ -270,7 +297,7 @@ private void refreshLeadership() {
                     } else if (Objects.equals(getClusterService().getId(), 
result) && !isLeader()) {
                         // Hey, I may have recovered from failure (or reboot 
was really
                         // fast) and my key was still there so yeah, I'm the 
leader again!
-                        setLeader(true);
+                        takeLeadership();
 
                         // Get the version
                         version = 
cache.getWithMetadata(leaderKey).getVersion();

Review Comment:
   Done in 5c98d411db86: both places that read the version after 
`takeLeadership()` now say why it is read also when stopping (so that `doStop` 
releases a key the run acquired meanwhile).
   
   _Claude Code on behalf of davsclaus_



##########
components/camel-infinispan/camel-infinispan/src/test/java/org/apache/camel/component/infinispan/remote/cluster/InfinispanRemoteClusterViewStopTest.java:
##########
@@ -0,0 +1,384 @@
+/*
+ * 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.component.infinispan.remote.cluster;
+
+import java.lang.reflect.Proxy;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.apache.camel.cluster.CamelClusterEventListener;
+import org.apache.camel.cluster.CamelClusterView;
+import org.apache.camel.component.infinispan.cluster.InfinispanClusterService;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.spi.ThreadPoolProfile;
+import org.apache.camel.support.DefaultThreadPoolFactory;
+import org.infinispan.client.hotrod.MetadataValue;
+import org.infinispan.client.hotrod.RemoteCache;
+import org.infinispan.client.hotrod.RemoteCacheManager;
+import org.infinispan.client.hotrod.exceptions.HotRodClientException;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.awaitility.Awaitility.await;
+
+/**
+ * Stopping the view while the leadership thread is busy: the local member 
gives up the leadership at once, and the stop
+ * does not wait on the lock of the leadership service.
+ */
+class InfinispanRemoteClusterViewStopTest {
+    private static final String VIEW_NAME = "myView";
+    private static final String NODE_ID = "node";
+    private static final String LEADER_KEY = 
InfinispanClusterService.LEADER_KEY;
+    private static final long SHUTDOWN_AWAIT_TERMINATION = 1000;
+
+    private final FakeRemoteCache server = new FakeRemoteCache();
+    private final LeadershipThreadPoolFactory pools = new 
LeadershipThreadPoolFactory();
+    private final ExecutorService stopper = 
Executors.newSingleThreadExecutor();
+    private final List<Boolean> events = new CopyOnWriteArrayList<>();
+    private DefaultCamelContext context;
+    private CamelClusterView view;
+
+    @AfterEach
+    void tearDown() throws Exception {
+        server.releaseAll();
+        stopper.shutdownNow();
+        stopper.awaitTermination(10, TimeUnit.SECONDS);
+        if (context != null) {
+            context.close();
+        }
+    }
+
+    @Test
+    void runStartedWhileStoppingDoesNotDelayTheStop() throws Exception {
+        startView();
+        await().atMost(10, TimeUnit.SECONDS).until(() -> 
view.getLocalMember().isLeader());
+        int received = events.size();
+
+        // the stop is held in the removal of the client listener, while a 
leadership run starts (as the periodic run
+        // or a run queued by an event would)
+        server.blockRemoveClientListener = true;
+        CompletableFuture<Long> stop = stopAsync();
+        assertThat(server.removeClientListenerCalled.await(10, 
TimeUnit.SECONDS)).isTrue();
+        CountDownLatch runStarted = new CountDownLatch(1);
+        pools.runAgain(runStarted::countDown);
+        assertThat(runStarted.await(10, TimeUnit.SECONDS)).isTrue();
+        server.removeClientListenerRelease.countDown();
+
+        // the stop does not wait for that run until the shutdown await 
termination runs out twice, as the run does not
+        // wait for the lock of the leadership service held by the stop
+        assertThat(stop.get(10, 
TimeUnit.SECONDS)).isLessThan(SHUTDOWN_AWAIT_TERMINATION);
+
+        // and the leadership was given up before the stop waited for anything 
else
+        assertThat(server.leaderWhenRemovingClientListener).isFalse();
+        assertThat(server.eventsWhenRemovingClientListener).isEqualTo(events);
+        assertThat(events.subList(received, 
events.size())).containsExactly(false);
+        assertThat(server.value(LEADER_KEY)).isNull();
+    }
+
+    @Test
+    void stopGivesUpTheLeadershipWhileARunWaitsForTheServer() throws Exception 
{
+        startView();
+        await().atMost(10, TimeUnit.SECONDS).until(() -> 
view.getLocalMember().isLeader());
+        int received = events.size();
+
+        // the refresh of the leader key waits for the server
+        server.blockReplaceWithVersion = true;
+        pools.runAgain(() -> {
+        });
+        assertThat(server.replaceWithVersionCalled.await(10, 
TimeUnit.SECONDS)).isTrue();
+
+        CompletableFuture<Long> stop = stopAsync();
+
+        // the listeners are told that the local member is no longer the 
leader while that refresh still waits
+        await().atMost(10, TimeUnit.SECONDS).until(() -> 
!view.getLocalMember().isLeader());
+        assertThat(server.replaceWithVersionReturned).isFalse();
+        assertThat(events.subList(received, 
events.size())).containsExactly(false);
+
+        // the stop waits for the run once, then interrupts the call that 
still waits for the server, which ends the run
+        assertThat(stop.get(10, 
TimeUnit.SECONDS)).isBetween(SHUTDOWN_AWAIT_TERMINATION, 2 * 
SHUTDOWN_AWAIT_TERMINATION);

Review Comment:
   Addressed in 5c98d411db86, but not with `3 * SHUTDOWN_AWAIT_TERMINATION`: 
the upper bound of `2 *` is what tells a stop that waits for the leadership 
thread once from one that waits twice (the bug this PR fixes), and with `3 *` a 
double wait would pass. Instead `SHUTDOWN_AWAIT_TERMINATION` is now 2000 ms, 
which keeps that check and gives the stop 2 s instead of 1 s of room for a slow 
machine (also for the `isLessThan` of the other test). The test class takes 
about 1 s longer.
   
   _Claude Code on behalf of davsclaus_



-- 
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]

Reply via email to