Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,17 @@ public class ConsumerConfig extends AbstractConfig {
"be excluded from the subscription. It is always possible to explicitly subscribe to an internal topic.";
public static final boolean DEFAULT_EXCLUDE_INTERNAL_TOPICS = true;

/**
* <code>internal.leave.group.on.close</code>
* Whether or not the consumer should leave the group on close. If set to <code>false</code> then a rebalance
* won't occur until <code>session.timeout.ms</code> expires.
*
* <p>
* Note: this is an internal configuration and could be changed in the future in a backward incompatible way
*
*/
static final String LEAVE_GROUP_ON_CLOSE_CONFIG = "internal.leave.group.on.close";

/** <code>isolation.level</code> */
public static final String ISOLATION_LEVEL_CONFIG = "isolation.level";
public static final String ISOLATION_LEVEL_DOC = "<p>Controls how to read messages written transactionally. If set to <code>read_committed</code>, consumer.poll() will only return" +
Expand Down Expand Up @@ -476,6 +487,10 @@ public class ConsumerConfig extends AbstractConfig {
DEFAULT_EXCLUDE_INTERNAL_TOPICS,
Importance.MEDIUM,
EXCLUDE_INTERNAL_TOPICS_DOC)
.defineInternal(LEAVE_GROUP_ON_CLOSE_CONFIG,
Type.BOOLEAN,
true,
Importance.LOW)
.define(ISOLATION_LEVEL_CONFIG,
Type.STRING,
DEFAULT_ISOLATION_LEVEL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,8 @@ else if (enableAutoCommit)
retryBackoffMs,
enableAutoCommit,
config.getInt(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG),
this.interceptors);
this.interceptors,
config.getBoolean(ConsumerConfig.LEAVE_GROUP_ON_CLOSE_CONFIG));
this.fetcher = new Fetcher<>(
logContext,
this.client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ private enum MemberState {
private Generation generation = Generation.NO_GENERATION;

private RequestFuture<Void> findCoordinatorFuture = null;
private final boolean leaveGroupOnClose;

/**
* Initialize the coordination manager.
Expand All @@ -147,7 +148,8 @@ public AbstractCoordinator(LogContext logContext,
Metrics metrics,
String metricGrpPrefix,
Time time,
long retryBackoffMs) {
long retryBackoffMs,
boolean leaveGroupOnClose) {
this.log = logContext.logger(AbstractCoordinator.class);
this.client = client;
this.time = time;
Expand All @@ -159,6 +161,7 @@ public AbstractCoordinator(LogContext logContext,
this.heartbeat = heartbeat;
this.sensors = new GroupCoordinatorMetrics(metrics, metricGrpPrefix);
this.retryBackoffMs = retryBackoffMs;
this.leaveGroupOnClose = leaveGroupOnClose;
}

public AbstractCoordinator(LogContext logContext,
Expand All @@ -171,10 +174,11 @@ public AbstractCoordinator(LogContext logContext,
Metrics metrics,
String metricGrpPrefix,
Time time,
long retryBackoffMs) {
long retryBackoffMs,
boolean leaveGroupOnClose) {
this(logContext, client, groupId, groupInstanceId, rebalanceTimeoutMs, sessionTimeoutMs,
new Heartbeat(time, sessionTimeoutMs, heartbeatIntervalMs, rebalanceTimeoutMs, retryBackoffMs),
metrics, metricGrpPrefix, time, retryBackoffMs);
metrics, metricGrpPrefix, time, retryBackoffMs, leaveGroupOnClose);
}

/**
Expand Down Expand Up @@ -845,7 +849,9 @@ protected void close(Timer timer) {
// Synchronize after closing the heartbeat thread since heartbeat thread
// needs this lock to complete and terminate after close flag is set.
synchronized (this) {
maybeLeaveGroup();
if (leaveGroupOnClose) {
maybeLeaveGroup();
}

// At this point, there may be pending commits (async commits or sync commits that were
// interrupted using wakeup) and the leave group request which have been queued, but not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ public ConsumerCoordinator(LogContext logContext,
long retryBackoffMs,
boolean autoCommitEnabled,
int autoCommitIntervalMs,
ConsumerInterceptors<?, ?> interceptors) {
ConsumerInterceptors<?, ?> interceptors,
boolean leaveGroupOnClose) {
super(logContext,
client,
groupId,
Expand All @@ -148,7 +149,8 @@ public ConsumerCoordinator(LogContext logContext,
metrics,
metricGrpPrefix,
time,
retryBackoffMs);
retryBackoffMs,
leaveGroupOnClose);
this.log = logContext.logger(ConsumerCoordinator.class);
this.metadata = metadata;
this.metadataSnapshot = new MetadataSnapshot(subscriptions, metadata.fetch(), metadata.updateVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1902,7 +1902,8 @@ private KafkaConsumer<String, String> newConsumer(Time time,
retryBackoffMs,
autoCommitEnabled,
autoCommitIntervalMs,
interceptors);
interceptors,
true);

Fetcher<String, String> fetcher = new Fetcher<>(
loggerFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ public DummyCoordinator(ConsumerNetworkClient client,
int retryBackoffMs,
Optional<String> groupInstanceId) {
super(new LogContext(), client, GROUP_ID, groupInstanceId, rebalanceTimeoutMs,
SESSION_TIMEOUT_MS, HEARTBEAT_INTERVAL_MS, metrics, METRIC_GROUP_PREFIX, time, retryBackoffMs);
SESSION_TIMEOUT_MS, HEARTBEAT_INTERVAL_MS, metrics, METRIC_GROUP_PREFIX, time, retryBackoffMs, !groupInstanceId.isPresent());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2219,7 +2219,8 @@ private ConsumerCoordinator buildCoordinator(final Metrics metrics,
retryBackoffMs,
autoCommitEnabled,
autoCommitIntervalMs,
null
null,
!groupInstanceId.isPresent()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ public WorkerCoordinator(LogContext logContext,
metrics,
metricGrpPrefix,
time,
retryBackoffMs);
retryBackoffMs,
true);
this.log = logContext.logger(WorkerCoordinator.class);
this.restUrl = restUrl;
this.configStorage = configStorage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,7 @@ public class StreamsConfig extends AbstractConfig {
tempConsumerDefaultOverrides.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, "1000");
tempConsumerDefaultOverrides.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
tempConsumerDefaultOverrides.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false");
tempConsumerDefaultOverrides.put("internal.leave.group.on.close", false);
CONSUMER_DEFAULT_OVERRIDES = Collections.unmodifiableMap(tempConsumerDefaultOverrides);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.kafka.streams.processor.FailOnInvalidTimestamp;
import org.apache.kafka.streams.processor.TimestampExtractor;
import org.apache.kafka.streams.processor.internals.StreamsPartitionAssignor;
import org.hamcrest.CoreMatchers;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -424,6 +425,13 @@ public void testGetGlobalConsumerConfigsWithGlobalConsumerOverridenPrefix() {
assertEquals("50", returnedProps.get(ConsumerConfig.MAX_POLL_RECORDS_CONFIG));
}

@Test
public void shouldSetInternalLeaveGroupOnCloseConfigToFalseInConsumer() {
final StreamsConfig streamsConfig = new StreamsConfig(props);
final Map<String, Object> consumerConfigs = streamsConfig.getMainConsumerConfigs(groupId, clientId, threadIdx);
assertThat(consumerConfigs.get("internal.leave.group.on.close"), CoreMatchers.equalTo(false));
}

@Test
public void shouldAcceptAtLeastOnce() {
// don't use `StreamsConfig.AT_LEAST_ONCE` to actually do a useful test
Expand Down