syhily commented on a change in pull request #17119:
URL: https://github.com/apache/flink/pull/17119#discussion_r701500537
##########
File path:
flink-connectors/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/PulsarSourceEnumerator.java
##########
@@ -148,8 +162,44 @@ public void close() {
* @return Set of subscribed {@link TopicPartition}s
*/
private Set<TopicPartition> getSubscribedTopicPartitions() {
- return subscriber.getSubscribedTopicPartitions(
- pulsarAdmin, rangeGenerator, context.currentParallelism());
+ int parallelism = context.currentParallelism();
+ Set<TopicPartition> partitions =
+ subscriber.getSubscribedTopicPartitions(pulsarAdmin,
rangeGenerator, parallelism);
+
+ // Seek start position for given partitions.
+ seekStartPosition(partitions);
+
+ return partitions;
+ }
+
+ private void seekStartPosition(Set<TopicPartition> partitions) {
+ ConsumerBuilder<byte[]> consumerBuilder =
+ createConsumerBuilder(pulsarClient, Schema.BYTES,
configuration);
+ Set<String> seekedPartitions = new HashSet<>();
+
+ for (TopicPartition partition : partitions) {
+ String topicName = partition.getFullTopicName();
Review comment:
If we use the Key_Shared subscription, the `topicName` could be
duplicated. We would create the same topic partition with a different
`TopicRange`.
##########
File path:
flink-connectors/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/PulsarSourceEnumerator.java
##########
@@ -148,8 +162,44 @@ public void close() {
* @return Set of subscribed {@link TopicPartition}s
*/
private Set<TopicPartition> getSubscribedTopicPartitions() {
- return subscriber.getSubscribedTopicPartitions(
- pulsarAdmin, rangeGenerator, context.currentParallelism());
+ int parallelism = context.currentParallelism();
+ Set<TopicPartition> partitions =
+ subscriber.getSubscribedTopicPartitions(pulsarAdmin,
rangeGenerator, parallelism);
+
+ // Seek start position for given partitions.
+ seekStartPosition(partitions);
+
+ return partitions;
+ }
+
+ private void seekStartPosition(Set<TopicPartition> partitions) {
+ ConsumerBuilder<byte[]> consumerBuilder =
+ createConsumerBuilder(pulsarClient, Schema.BYTES,
configuration);
+ Set<String> seekedPartitions = new HashSet<>();
+
+ for (TopicPartition partition : partitions) {
+ String topicName = partition.getFullTopicName();
+ if (!seekedPartitions.contains(topicName)) {
+ try (Consumer<byte[]> consumer =
+ sneakyClient(() ->
consumerBuilder.clone().topic(topicName).subscribe())) {
+ startCursor.seekPosition(
+ partition.getTopic(), partition.getPartitionId(),
consumer);
+ } catch (PulsarClientException e) {
+ if (sourceConfiguration.getVerifyInitialOffsets() ==
FAIL_ON_MISMATCH) {
+ throw new IllegalArgumentException(e);
+ } else {
+ // WARN_ON_MISMATCH would just print this warning
message.
+ // No need to print the stacktrace.
+ LOG.warn(
+ "Failed to set initial consuming position for
partition {}",
+ partition,
+ e);
Review comment:
The seek exception could be multiple. Such as the timeout when
connecting with the broker, bookie, etc. Print the exception would help us
debug.
##########
File path:
flink-connectors/flink-connector-pulsar/src/test/java/org/apache/flink/connector/pulsar/source/split/PulsarPartitionSplitTest.java
##########
@@ -1,54 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.flink.connector.pulsar.source.split;
-
-import
org.apache.flink.connector.pulsar.source.enumerator.cursor.CursorPosition;
-import org.apache.flink.connector.pulsar.source.enumerator.cursor.StartCursor;
-import
org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition;
-
-import org.apache.pulsar.client.impl.MessageIdImpl;
-import org.junit.jupiter.api.Test;
-
-import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
-import static
org.apache.flink.connector.pulsar.source.enumerator.cursor.StartCursor.defaultStartCursor;
-import static
org.apache.flink.connector.pulsar.source.enumerator.cursor.StopCursor.defaultStopCursor;
-import static
org.apache.flink.connector.pulsar.source.enumerator.topic.TopicRange.createFullRange;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-
-/** Unit tests for {@link PulsarPartitionSplit}. */
-class PulsarPartitionSplitTest {
-
- @Test
- void partitionSplitWouldHaveANewStartCursorWhenCheckpointing() {
Review comment:
This test was originally used to check if the StartCursor was updated
when the PulsarPartitionSplit was created, but now the StartCursor is no longer
used in the Split, so the corresponding test is no longer necessary.
##########
File path:
flink-connectors/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/reader/split/PulsarOrderedPartitionSplitReader.java
##########
@@ -75,7 +78,30 @@ protected void finishedPollMessage(Message<byte[]> message) {
@Override
protected void startConsumer(PulsarPartitionSplit split, Consumer<byte[]>
consumer) {
- initialStartPosition(split, consumer);
+ MessageId latestConsumedId = split.getLatestConsumedId();
+
+ // Reset the start position for ordered pulsar consumer.
+ if (latestConsumedId != null) {
+ StartCursor startCursor =
StartCursor.fromMessageId(latestConsumedId, false);
+ TopicPartition partition = split.getPartition();
+
+ try {
+ startCursor.seekPosition(
+ partition.getTopic(), partition.getPartitionId(),
consumer);
Review comment:
seek in enumerator is the initial seek when we create the topic
partition. This seek is used when we meet a restart or failure. We need to
consume from the last successful checkpoint position.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]