Skip to content

Commit

Permalink
GH-3371: Fence child containers after ConcurrentContainer stops
Browse files Browse the repository at this point in the history
Fixes: #3371

Containers are not restricted from starting after ConcurrentContainer stopped or restarted. These changes would fix this issue.

* Enhancements to fence container after ConcurrentContainer stops

(cherry picked from commit 20696f2)

# Conflicts:
#	spring-kafka/src/main/java/org/springframework/kafka/listener/AbstractMessageListenerContainer.java
  • Loading branch information
LokeshAlamuri authored and artembilan committed Jul 31, 2024
1 parent c900940 commit 3fdf468
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public abstract class AbstractMessageListenerContainer<K, V>

private volatile boolean running = false;

private volatile boolean fenced = false;

private volatile boolean paused;

private volatile boolean stoppedNormally = true;
Expand Down Expand Up @@ -274,6 +276,10 @@ public boolean isRunning() {
return this.running;
}

protected void setFenced(boolean fenced) {
this.fenced = fenced;
}

protected boolean isPaused() {
return this.paused;
}
Expand Down Expand Up @@ -507,6 +513,7 @@ public final void start() {
if (!isRunning()) {
Assert.state(this.containerProperties.getMessageListener() instanceof GenericMessageListener,
() -> "A " + GenericMessageListener.class.getName() + " implementation must be provided");
Assert.state(!this.fenced, "Container Fenced. It is not allowed to start.");
doStart();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ protected void doStop(final Runnable callback, boolean normal) {
}
}
for (KafkaMessageListenerContainer<K, V> container : this.containers) {
container.setFenced(true);
if (container.isRunning()) {
if (normal) {
container.stop(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.kafka.listener;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
Expand Down Expand Up @@ -198,6 +199,7 @@ protected Consumer<Integer, String> createKafkaConsumer(String groupId, String c
assertThat(container.metrics()).isNotNull();
Set<KafkaMessageListenerContainer<Integer, String>> children = new HashSet<>(containers);
assertThat(container.isInExpectedState()).isTrue();
MessageListenerContainer childContainer = container.getContainers().get(0);
container.getContainers().get(0).stopAbnormally(() -> { });
assertThat(container.isInExpectedState()).isFalse();
container.getContainers().get(0).start();
Expand All @@ -220,6 +222,10 @@ protected Consumer<Integer, String> createKafkaConsumer(String groupId, String c
});
assertThat(overrides.get().getProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG)).isNull();
this.logger.info("Stop auto");
assertThat(childContainer.isRunning()).isFalse();
assertThat(container.isRunning()).isFalse();
// Fenced container. Throws exception
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> childContainer.start());
}

@Test
Expand Down

0 comments on commit 3fdf468

Please sign in to comment.