Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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 @@ -174,6 +174,10 @@ abstract class AbstractSubscriptionConsumer implements AutoCloseable {
private final Map<String, Set<SubscriptionCommitContext>> pendingRedirectAcksByTopicRegion =
new ConcurrentHashMap<>();

private final Map<String, Set<Integer>> terminatedDataNodeIdsByTopic = new ConcurrentHashMap<>();
private final Set<String> autoUnsubscribingTopics = ConcurrentHashMap.newKeySet();
private final Object topicTerminationLock = new Object();

@SuppressWarnings("java:S3077")
protected volatile Map<String, TopicConfig> subscribedTopics = new HashMap<>();

Expand Down Expand Up @@ -409,6 +413,8 @@ public void close() {
// in network I/O. prepareClose() bounds or interrupts those RPCs before providers are detached.
providers.prepareClose();
providers.closeProviders(!isFenced());
clearTerminatedProviderStates();
autoUnsubscribingTopics.clear();
}

boolean isClosed() {
Expand Down Expand Up @@ -456,6 +462,9 @@ private void subscribe(Set<String> topicNames, final boolean needParse)
.collect(Collectors.toSet());
}

topicNames.forEach(terminatedDataNodeIdsByTopic::remove);
topicNames.forEach(autoUnsubscribingTopics::remove);

providers.acquireReadLock();
try {
subscribeWithRedirection(topicNames);
Expand Down Expand Up @@ -492,6 +501,7 @@ private void unsubscribe(Set<String> topicNames, final boolean needParse)
try {
unsubscribeWithRedirection(topicNames);
topicNames.forEach(this::clearPendingRedirectAcks);
topicNames.forEach(terminatedDataNodeIdsByTopic::remove);
} finally {
providers.releaseReadLock();
}
Expand Down Expand Up @@ -773,13 +783,8 @@ private Path getFilePath(
TERMINATION,
(resp, timer) -> {
final SubscriptionCommitContext commitContext = resp.getCommitContext();
final String topicNameToUnsubscribe = commitContext.getTopicName();
LOGGER.info(
SubscriptionMessages
.LOG_TERMINATION_OCCURRED_SUBSCRIPTIONCONSUMER_ARG_POLLING_TOPICS_UNSUBSCRIBE_TOPIC_ARG_AUTOMATICALLY_E9B695FA,
coreReportMessage(),
topicNameToUnsubscribe);
unsubscribe(Collections.singleton(topicNameToUnsubscribe), false);
handleProviderTopicTermination(
commitContext.getDataNodeId(), commitContext.getTopicName());
return Optional.empty();
});
put(
Expand Down Expand Up @@ -811,6 +816,93 @@ protected Set<Integer> getAvailableDataNodeIds() {
}
}

protected Set<Integer> getExpectedDataNodeIds() {
providers.acquireReadLock();
try {
return providers.getExpectedDataNodeIds();
} finally {
providers.releaseReadLock();
}
}

void clearTerminatedProviderStates() {
synchronized (topicTerminationLock) {
terminatedDataNodeIdsByTopic.clear();
}
}

void invalidateProviderTopicCompletions(
final int dataNodeId, final Collection<String> completedTopics) {
synchronized (topicTerminationLock) {
terminatedDataNodeIdsByTopic
.entrySet()
.removeIf(
entry ->
!completedTopics.contains(entry.getKey())
&& entry.getValue().remove(dataNodeId)
&& entry.getValue().isEmpty());
}
}

void handleProviderTopicTermination(final int dataNodeId, final String topicName)
throws SubscriptionException {
final TopicConfig topicConfig = subscribedTopics.get(topicName);
if (Objects.nonNull(topicConfig)
&& topicConfig.isSnapshotMode()
&& topicConfig.isProgressRetainedAfterUnsubscribe()) {
final long topologyGeneration;
providers.acquireReadLock();
try {
final Set<Integer> expectedDataNodeIds = providers.getExpectedDataNodeIds();
topologyGeneration = providers.getTopologyGeneration();
synchronized (topicTerminationLock) {
if (!subscribedTopics.containsKey(topicName)) {
terminatedDataNodeIdsByTopic.remove(topicName);
return;
}
final Set<Integer> terminated =
terminatedDataNodeIdsByTopic.computeIfAbsent(
topicName, ignored -> ConcurrentHashMap.newKeySet());
if (dataNodeId >= 0) {
terminated.add(dataNodeId);
}
if (expectedDataNodeIds.isEmpty()
|| !terminated.containsAll(expectedDataNodeIds)
|| !autoUnsubscribingTopics.add(topicName)) {
return;
}
}
try {
if (providers.getTopologyGeneration() != topologyGeneration) {
return;
}
LOGGER.info(
SubscriptionMessages
.LOG_TERMINATION_OCCURRED_SUBSCRIPTIONCONSUMER_ARG_POLLING_TOPICS_UNSUBSCRIBE_TOPIC_ARG_AUTOMATICALLY_E9B695FA,
coreReportMessage(),
topicName);
unsubscribe(Collections.singleton(topicName), false);
} finally {
autoUnsubscribingTopics.remove(topicName);
}
} finally {
providers.releaseReadLock();
}
return;
}
if (terminatedDataNodeIdsByTopic.remove(topicName) != null
&& !subscribedTopics.containsKey(topicName)) {
return;
}

LOGGER.info(
SubscriptionMessages
.LOG_TERMINATION_OCCURRED_SUBSCRIPTIONCONSUMER_ARG_POLLING_TOPICS_UNSUBSCRIBE_TOPIC_ARG_AUTOMATICALLY_E9B695FA,
coreReportMessage(),
topicName);
unsubscribe(Collections.singleton(topicName), false);
}
Comment thread
jt2594838 marked this conversation as resolved.
Resolved

/**
* Returns the latest watermark timestamp received from the server. This tracks the maximum data
* timestamp observed across all polled regions. Returns {@code Long.MIN_VALUE} if no watermark
Expand Down Expand Up @@ -1507,6 +1599,17 @@ private static void mergeTimeSelectedByTable(
private List<SubscriptionPollResponse> pollInternal(
final Set<String> topicNames, final long timeoutMs) throws SubscriptionException {
checkIfFenced();
if (!terminatedDataNodeIdsByTopic.isEmpty()) {
for (final String topicName : new ArrayList<>(topicNames)) {
final Set<Integer> terminated = terminatedDataNodeIdsByTopic.get(topicName);
if (Objects.nonNull(terminated)) {
final Set<Integer> expectedDataNodeIds = getExpectedDataNodeIds();
if (!expectedDataNodeIds.isEmpty() && terminated.containsAll(expectedDataNodeIds)) {
handleProviderTopicTermination(-1, topicName);
}
}
}
}
boolean syncIfUnavailable = false;
providers.acquireReadLock();
try {
Expand All @@ -1517,10 +1620,30 @@ private List<SubscriptionPollResponse> pollInternal(
}
syncIfUnavailable = true;
} else {
final Set<String> effectiveTopicNames;
if (terminatedDataNodeIdsByTopic.isEmpty()) {
effectiveTopicNames = topicNames;
} else {
effectiveTopicNames = new HashSet<>(topicNames.size());
for (final String topicName : topicNames) {
final Set<Integer> terminated = terminatedDataNodeIdsByTopic.get(topicName);
if (Objects.nonNull(terminated)
&& (!subscribedTopics.containsKey(topicName)
|| terminated.contains(provider.getDataNodeId()))) {
continue;
}
effectiveTopicNames.add(topicName);
}
if (effectiveTopicNames.isEmpty()) {
return Collections.emptyList();
}
}
// ignore SubscriptionConnectionException to improve poll auto retry
try {
return provider.poll(topicNames, timeoutMs, buildCurrentProgressByTopic(topicNames));
return provider.poll(
effectiveTopicNames, timeoutMs, buildCurrentProgressByTopic(effectiveTopicNames));
} catch (final SubscriptionConnectionException ignored) {
clearTerminatedProviderStates();
syncIfUnavailable = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -500,18 +499,15 @@ PipeSubscribeHeartbeatResp heartbeat(
}

PipeSubscribeHeartbeatResp latestResp = null;
final Set<String> topicNamesToUnsubscribe = new LinkedHashSet<>();
rpcLock.lock();
try {
for (final List<SubscriptionCommitContext> batch : batches) {
latestResp = heartbeatOnce(batch);
topicNamesToUnsubscribe.addAll(latestResp.getTopicNamesToUnsubscribe());
}
} finally {
rpcLock.unlock();
}
latestResp.getTopicNamesToUnsubscribe().clear();
latestResp.getTopicNamesToUnsubscribe().addAll(topicNamesToUnsubscribe);
// Completion describes the current state. A later batch can revoke an earlier notice.
return latestResp;
}

Expand Down
Loading
Loading