You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by gu...@apache.org on 2023/02/16 09:33:41 UTC
[flink-connector-pulsar] 04/05: [FLINK-28959][Stream] Fix the generation of TopicPartition.
This is an automated email from the ASF dual-hosted git repository.
guoweijie pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/flink-connector-pulsar.git
commit 59d13632ca2743ffb742777b2fd9fd4d2a8a47f4
Author: Yufan Sheng <yu...@streamnative.io>
AuthorDate: Thu Feb 16 14:10:30 2023 +0800
[FLINK-28959][Stream] Fix the generation of TopicPartition.
---
.../source/enumerator/topic/TopicNameUtils.java | 11 +++++------
.../source/enumerator/topic/TopicPartition.java | 22 ++++++++++++++++++----
2 files changed, 23 insertions(+), 10 deletions(-)
diff --git a/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/topic/TopicNameUtils.java b/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/topic/TopicNameUtils.java
index d417dc9..b85d063 100644
--- a/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/topic/TopicNameUtils.java
+++ b/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/topic/TopicNameUtils.java
@@ -24,7 +24,6 @@ import org.apache.flink.shaded.guava30.com.google.common.collect.ImmutableList;
import org.apache.pulsar.common.naming.TopicName;
-import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -66,10 +65,10 @@ public final class TopicNameUtils {
return TopicName.get(topic).isPartitioned();
}
- /** Merge the same topics into one topic. */
+ /** Merge the same topics or partitions into one topic. */
public static List<String> distinctTopics(List<String> topics) {
Set<String> fullTopics = new HashSet<>();
- Map<String, List<Integer>> partitionedTopics = new HashMap<>();
+ Map<String, Set<Integer>> partitionedTopics = new HashMap<>();
for (String topic : topics) {
TopicName topicName = TopicName.get(topic);
@@ -79,16 +78,16 @@ public final class TopicNameUtils {
fullTopics.add(partitionedTopicName);
partitionedTopics.remove(partitionedTopicName);
} else if (!fullTopics.contains(partitionedTopicName)) {
- List<Integer> partitionIds =
+ Set<Integer> partitionIds =
partitionedTopics.computeIfAbsent(
- partitionedTopicName, k -> new ArrayList<>());
+ partitionedTopicName, k -> new HashSet<>());
partitionIds.add(topicName.getPartitionIndex());
}
}
ImmutableList.Builder<String> builder = ImmutableList.<String>builder().addAll(fullTopics);
- for (Map.Entry<String, List<Integer>> topicSet : partitionedTopics.entrySet()) {
+ for (Map.Entry<String, Set<Integer>> topicSet : partitionedTopics.entrySet()) {
String topicName = topicSet.getKey();
for (Integer partitionId : topicSet.getValue()) {
builder.add(topicNameWithPartition(topicName, partitionId));
diff --git a/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/topic/TopicPartition.java b/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/topic/TopicPartition.java
index d6b98bd..569f41f 100644
--- a/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/topic/TopicPartition.java
+++ b/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/topic/TopicPartition.java
@@ -27,13 +27,15 @@ import org.apache.flink.shaded.guava30.com.google.common.collect.ImmutableList;
import org.apache.pulsar.client.api.Range;
import org.apache.pulsar.client.api.SubscriptionType;
+import org.apache.pulsar.common.naming.TopicName;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import static java.util.stream.Collectors.toList;
-import static org.apache.flink.connector.pulsar.source.enumerator.topic.TopicNameUtils.topicName;
import static org.apache.flink.connector.pulsar.source.enumerator.topic.TopicNameUtils.topicNameWithPartition;
import static org.apache.flink.connector.pulsar.source.enumerator.topic.TopicRange.createFullRange;
import static org.apache.flink.util.Preconditions.checkArgument;
@@ -47,6 +49,8 @@ import static org.apache.flink.util.Preconditions.checkNotNull;
public class TopicPartition implements Serializable {
private static final long serialVersionUID = -1474354741550810953L;
+ private static final Logger LOG = LoggerFactory.getLogger(TopicPartition.class);
+
/**
* If {@link TopicPartition#getPartitionId()} is equal to this. This topic partition wouldn't be
* a partition instance. It would be a non-partitioned topic name.
@@ -77,7 +81,11 @@ public class TopicPartition implements Serializable {
/** Create a non-partition topic without partition information. */
@PublicEvolving
public TopicPartition(String topic) {
- this(topic, NON_PARTITION_ID, FULL_RANGES);
+ TopicName topicName = TopicName.get(topic);
+ this.topic = topicName.getPartitionedTopicName();
+ this.partitionId =
+ topicName.isPartitioned() ? topicName.getPartitionIndex() : NON_PARTITION_ID;
+ this.ranges = FULL_RANGES;
}
/** Create a topic partition without key hash range. */
@@ -96,8 +104,14 @@ public class TopicPartition implements Serializable {
public TopicPartition(String topic, int partitionId, List<TopicRange> ranges) {
checkArgument(partitionId >= NON_PARTITION_ID, "Invalid partition id.");
- this.topic = topicName(checkNotNull(topic));
- this.partitionId = partitionId;
+ TopicName topicName = TopicName.get(topic);
+ this.topic = topicName.getPartitionedTopicName();
+ if (partitionId == NON_PARTITION_ID && topicName.isPartitioned()) {
+ LOG.warn("Invalid non partition id, this topic {} is a partitioned topic", topic);
+ this.partitionId = topicName.getPartitionIndex();
+ } else {
+ this.partitionId = partitionId;
+ }
this.ranges = checkNotNull(ranges);
}