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 @@ -175,6 +175,10 @@
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 @@ -410,6 +414,8 @@
// 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 @@ -457,6 +463,9 @@
.collect(Collectors.toSet());
}

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

providers.acquireReadLock();
try {
subscribeWithRedirection(topicNames);
Expand Down Expand Up @@ -493,6 +502,7 @@
try {
unsubscribeWithRedirection(topicNames);
topicNames.forEach(this::clearPendingRedirectAcks);
topicNames.forEach(terminatedDataNodeIdsByTopic::remove);
} finally {
providers.releaseReadLock();
}
Expand Down Expand Up @@ -774,13 +784,8 @@
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 @@ -812,6 +817,93 @@
}
}

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,

Check warning on line 901 in iotdb-client/subscription/src/main/java/org/apache/iotdb/session/subscription/consumer/base/AbstractSubscriptionConsumer.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Line is longer than 100 characters (found 123).

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AaISholT0x2neM30YBaJ&open=AaISholT0x2neM30YBaJ&pullRequest=19364
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 @@ -1519,9 +1611,20 @@
});
}

private List<SubscriptionPollResponse> pollInternal(

Check failure on line 1614 in iotdb-client/subscription/src/main/java/org/apache/iotdb/session/subscription/consumer/base/AbstractSubscriptionConsumer.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 34 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AaISholT0x2neM30YBaI&open=AaISholT0x2neM30YBaI&pullRequest=19364
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 @@ -1532,10 +1635,30 @@
}
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