You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by ti...@apache.org on 2023/01/11 07:57:19 UTC

[flink-connector-pulsar] branch v3.0 updated: [FLINK-30552][Connector/Pulsar] Drop next message id calculation, use resetCursor api to exclude the given message. (#16)

This is an automated email from the ASF dual-hosted git repository.

tison pushed a commit to branch v3.0
in repository https://gitbox.apache.org/repos/asf/flink-connector-pulsar.git


The following commit(s) were added to refs/heads/v3.0 by this push:
     new 9ab0a44  [FLINK-30552][Connector/Pulsar] Drop next message id calculation, use resetCursor api to exclude the given message. (#16)
9ab0a44 is described below

commit 9ab0a44c9936555afa13076e1ebbae34a5e20f63
Author: Yufan Sheng <yu...@streamnative.io>
AuthorDate: Wed Jan 11 15:57:15 2023 +0800

    [FLINK-30552][Connector/Pulsar] Drop next message id calculation, use resetCursor api to exclude the given message. (#16)
---
 .../source/enumerator/PulsarSourceEnumerator.java  | 35 ++---------
 .../source/enumerator/cursor/CursorPosition.java   | 73 +++++++++++++++++++---
 .../source/enumerator/cursor/MessageIdUtils.java   | 73 ----------------------
 .../cursor/start/MessageIdStartCursor.java         | 27 +++-----
 .../cursor/stop/MessageIdStopCursor.java           |  4 +-
 .../split/PulsarOrderedPartitionSplitReader.java   | 28 +++------
 6 files changed, 87 insertions(+), 153 deletions(-)

diff --git a/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/PulsarSourceEnumerator.java b/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/PulsarSourceEnumerator.java
index 931dc6a..ea61247 100644
--- a/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/PulsarSourceEnumerator.java
+++ b/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/PulsarSourceEnumerator.java
@@ -33,7 +33,6 @@ import org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit;
 import org.apache.flink.util.FlinkRuntimeException;
 
 import org.apache.pulsar.client.admin.PulsarAdmin;
-import org.apache.pulsar.client.api.MessageId;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -210,37 +209,11 @@ public class PulsarSourceEnumerator
         for (TopicPartition partition : newPartitions) {
             String topicName = partition.getFullTopicName();
             String subscriptionName = sourceConfiguration.getSubscriptionName();
+            CursorPosition position =
+                    startCursor.position(partition.getTopic(), partition.getPartitionId());
 
-            List<String> subscriptions =
-                    sneakyAdmin(() -> pulsarAdmin.topics().getSubscriptions(topicName));
-            if (!subscriptions.contains(subscriptionName)) {
-                CursorPosition position =
-                        startCursor.position(partition.getTopic(), partition.getPartitionId());
-                MessageId initialPosition = queryInitialPosition(topicName, position);
-
-                sneakyAdmin(
-                        () ->
-                                pulsarAdmin
-                                        .topics()
-                                        .createSubscription(
-                                                topicName, subscriptionName, initialPosition));
-            }
-        }
-    }
-
-    /** Query the available message id from Pulsar. */
-    private MessageId queryInitialPosition(String topicName, CursorPosition position) {
-        CursorPosition.Type type = position.getType();
-        if (type == CursorPosition.Type.TIMESTAMP) {
-            return sneakyAdmin(
-                    () ->
-                            pulsarAdmin
-                                    .topics()
-                                    .getMessageIdByTimestamp(topicName, position.getTimestamp()));
-        } else if (type == CursorPosition.Type.MESSAGE_ID) {
-            return position.getMessageId();
-        } else {
-            throw new UnsupportedOperationException("We don't support this seek type " + type);
+            sneakyAdmin(
+                    () -> position.createInitialPosition(pulsarAdmin, topicName, subscriptionName));
         }
     }
 
diff --git a/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/cursor/CursorPosition.java b/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/cursor/CursorPosition.java
index 55b5513..3a984fb 100644
--- a/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/cursor/CursorPosition.java
+++ b/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/cursor/CursorPosition.java
@@ -20,10 +20,15 @@ package org.apache.flink.connector.pulsar.source.enumerator.cursor;
 
 import org.apache.flink.annotation.Internal;
 import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.connector.pulsar.source.enumerator.PulsarSourceEnumerator;
 
+import org.apache.pulsar.client.admin.PulsarAdmin;
+import org.apache.pulsar.client.admin.PulsarAdminException;
 import org.apache.pulsar.client.api.MessageId;
+import org.apache.pulsar.client.impl.ChunkMessageIdImpl;
 
 import java.io.Serializable;
+import java.util.List;
 
 import static org.apache.flink.util.Preconditions.checkNotNull;
 
@@ -37,38 +42,88 @@ public final class CursorPosition implements Serializable {
     private final Type type;
 
     private final MessageId messageId;
+    private final boolean include;
 
     private final Long timestamp;
 
-    public CursorPosition(MessageId messageId) {
+    /**
+     * Start consuming from the given message id. The message id couldn't be the {@code
+     * MultipleMessageIdImpl}.
+     *
+     * @param include Whether the cosponsored position will be (in/ex)cluded in the consuming
+     *     result.
+     */
+    public CursorPosition(MessageId messageId, boolean include) {
         checkNotNull(messageId, "Message id couldn't be null.");
 
         this.type = Type.MESSAGE_ID;
         this.messageId = messageId;
+        this.include = include;
         this.timestamp = null;
     }
 
+    /**
+     * Start consuming from the given timestamp position. The cosponsored position will be included
+     * in the consuming result.
+     */
     public CursorPosition(Long timestamp) {
         checkNotNull(timestamp, "Timestamp couldn't be null.");
 
         this.type = Type.TIMESTAMP;
         this.messageId = null;
+        this.include = true;
         this.timestamp = timestamp;
     }
 
+    /** This method is used to create the initial position in {@link PulsarSourceEnumerator}. */
     @Internal
-    public Type getType() {
-        return type;
+    public boolean createInitialPosition(
+            PulsarAdmin pulsarAdmin, String topicName, String subscriptionName)
+            throws PulsarAdminException {
+        List<String> subscriptions = pulsarAdmin.topics().getSubscriptions(topicName);
+
+        if (!subscriptions.contains(subscriptionName)) {
+            pulsarAdmin
+                    .topics()
+                    .createSubscription(topicName, subscriptionName, MessageId.earliest);
+
+            // Reset cursor to desired position.
+            MessageId initialPosition = getMessageId(pulsarAdmin, topicName);
+            pulsarAdmin
+                    .topics()
+                    .resetCursor(topicName, subscriptionName, initialPosition, !include);
+
+            return true;
+        }
+
+        return false;
     }
 
+    /**
+     * This method is used to reset the consuming position in {@code
+     * PulsarPartitionSplitReaderBase}.
+     */
     @Internal
-    public MessageId getMessageId() {
-        return messageId;
+    public void seekPosition(PulsarAdmin pulsarAdmin, String topicName, String subscriptionName)
+            throws PulsarAdminException {
+        if (!createInitialPosition(pulsarAdmin, topicName, subscriptionName)) {
+            // Reset cursor to desired position.
+            MessageId initialPosition = getMessageId(pulsarAdmin, topicName);
+            pulsarAdmin
+                    .topics()
+                    .resetCursor(topicName, subscriptionName, initialPosition, !include);
+        }
     }
 
-    @Internal
-    public Long getTimestamp() {
-        return timestamp;
+    private MessageId getMessageId(PulsarAdmin pulsarAdmin, String topicName)
+            throws PulsarAdminException {
+        if (type == Type.TIMESTAMP) {
+            return pulsarAdmin.topics().getMessageIdByTimestamp(topicName, timestamp);
+        } else if (messageId instanceof ChunkMessageIdImpl) {
+            return ((ChunkMessageIdImpl) messageId).getFirstChunkMessageId();
+        } else {
+            return messageId;
+        }
     }
 
     @Override
@@ -76,7 +131,7 @@ public final class CursorPosition implements Serializable {
         if (type == Type.TIMESTAMP) {
             return "timestamp: " + timestamp;
         } else {
-            return "message id: " + messageId;
+            return "message id: " + messageId + " include: " + include;
         }
     }
 
diff --git a/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/cursor/MessageIdUtils.java b/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/cursor/MessageIdUtils.java
deleted file mode 100644
index 0ae1a1f..0000000
--- a/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/cursor/MessageIdUtils.java
+++ /dev/null
@@ -1,73 +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.enumerator.cursor;
-
-import org.apache.flink.annotation.Internal;
-
-import org.apache.pulsar.client.api.MessageId;
-import org.apache.pulsar.client.impl.BatchMessageIdImpl;
-import org.apache.pulsar.client.impl.MessageIdImpl;
-
-import static org.apache.flink.util.Preconditions.checkArgument;
-import static org.apache.pulsar.client.impl.MessageIdImpl.convertToMessageIdImpl;
-
-/** The helper class for Pulsar's message id. */
-@Internal
-public final class MessageIdUtils {
-
-    private MessageIdUtils() {
-        // No public constructor.
-    }
-
-    /**
-     * The implementation from <a
-     * href="https://github.com/apache/pulsar/blob/7c8dc3201baad7d02d886dbc26db5c03abce77d6/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/PositionImpl.java#L85">this
-     * code snippet</a> to get next message id.
-     */
-    public static MessageId nextMessageId(MessageId messageId) {
-        MessageIdImpl idImpl = unwrapMessageId(messageId);
-
-        long ledgerId = idImpl.getLedgerId();
-        long entryId = idImpl.getEntryId();
-        int partitionIndex = idImpl.getPartitionIndex();
-
-        if (entryId < 0) {
-            return new MessageIdImpl(ledgerId, 0, partitionIndex);
-        } else {
-            return new MessageIdImpl(ledgerId, entryId + 1, partitionIndex);
-        }
-    }
-
-    /**
-     * Convert the message id interface to its backend implementation. And check if it's a batch
-     * message id. We don't support the batch message for its low performance now.
-     */
-    public static MessageIdImpl unwrapMessageId(MessageId messageId) {
-        MessageIdImpl idImpl = convertToMessageIdImpl(messageId);
-        if (idImpl instanceof BatchMessageIdImpl) {
-            int batchSize = ((BatchMessageIdImpl) idImpl).getBatchSize();
-            checkArgument(
-                    batchSize <= 1,
-                    "We only support normal message id currently. This batch size is %d",
-                    batchSize);
-        }
-
-        return idImpl;
-    }
-}
diff --git a/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/cursor/start/MessageIdStartCursor.java b/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/cursor/start/MessageIdStartCursor.java
index bf63675..23dd98e 100644
--- a/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/cursor/start/MessageIdStartCursor.java
+++ b/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/cursor/start/MessageIdStartCursor.java
@@ -24,41 +24,30 @@ import org.apache.flink.connector.pulsar.source.enumerator.cursor.StartCursor;
 import org.apache.pulsar.client.api.ConsumerBuilder;
 import org.apache.pulsar.client.api.MessageId;
 
-import static org.apache.flink.connector.pulsar.source.enumerator.cursor.MessageIdUtils.nextMessageId;
-import static org.apache.flink.connector.pulsar.source.enumerator.cursor.MessageIdUtils.unwrapMessageId;
-
 /** This cursor would leave pulsar start consuming from a specific message id. */
 public class MessageIdStartCursor implements StartCursor {
     private static final long serialVersionUID = -8057345435887170111L;
 
     private final MessageId messageId;
+    private final boolean inclusive;
 
     /**
      * The default {@code inclusive} behavior should be controlled in {@link
-     * ConsumerBuilder#startMessageIdInclusive}. But pulsar has a bug and don't support this
-     * currently. We have to use {@code entry + 1} policy for consuming the next available message.
-     * If the message id entry is not valid. Pulsar would automatically find next valid message id.
-     * Please referer <a
-     * href="https://github.com/apache/pulsar/blob/36d5738412bb1ed9018178007bf63d9202b675db/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java#L1151">this
-     * code</a> for understanding pulsar internal logic.
+     * ConsumerBuilder#startMessageIdInclusive}. But pulsar has a bug and doesn't support this
+     * feature currently. We have to use admin API to reset the cursor instead.
      *
      * @param messageId The message id for start position.
-     * @param inclusive Whether we include the start message id in consuming result. This works only
-     *     if we provide a specified message id instead of {@link MessageId#earliest} or {@link
+     * @param inclusive Whether we include the start message id in the consuming result. This works
+     *     only if we provide a specified message id instead of {@link MessageId#earliest} or {@link
      *     MessageId#latest}.
      */
     public MessageIdStartCursor(MessageId messageId, boolean inclusive) {
-        if (MessageId.earliest.equals(messageId)
-                || MessageId.latest.equals(messageId)
-                || inclusive) {
-            this.messageId = unwrapMessageId(messageId);
-        } else {
-            this.messageId = nextMessageId(messageId);
-        }
+        this.messageId = messageId;
+        this.inclusive = inclusive;
     }
 
     @Override
     public CursorPosition position(String topic, int partitionId) {
-        return new CursorPosition(messageId);
+        return new CursorPosition(messageId, inclusive);
     }
 }
diff --git a/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/cursor/stop/MessageIdStopCursor.java b/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/cursor/stop/MessageIdStopCursor.java
index 3e0a86e..ada2fd3 100644
--- a/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/cursor/stop/MessageIdStopCursor.java
+++ b/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/enumerator/cursor/stop/MessageIdStopCursor.java
@@ -23,10 +23,10 @@ import org.apache.flink.connector.pulsar.source.enumerator.cursor.StopCursor;
 import org.apache.pulsar.client.api.Message;
 import org.apache.pulsar.client.api.MessageId;
 
-import static org.apache.flink.connector.pulsar.source.enumerator.cursor.MessageIdUtils.unwrapMessageId;
 import static org.apache.flink.util.Preconditions.checkArgument;
 import static org.apache.pulsar.client.api.MessageId.earliest;
 import static org.apache.pulsar.client.api.MessageId.latest;
+import static org.apache.pulsar.client.impl.MessageIdImpl.convertToMessageIdImpl;
 
 /**
  * Stop consuming message at a given message id. We use the {@link MessageId#compareTo(Object)} for
@@ -43,7 +43,7 @@ public class MessageIdStopCursor implements StopCursor {
         checkArgument(!earliest.equals(messageId), "MessageId.earliest is not supported.");
         checkArgument(!latest.equals(messageId), "Use LatestMessageStopCursor instead.");
 
-        this.messageId = unwrapMessageId(messageId);
+        this.messageId = convertToMessageIdImpl(messageId);
         this.inclusive = inclusive;
     }
 
diff --git a/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/reader/split/PulsarOrderedPartitionSplitReader.java b/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/reader/split/PulsarOrderedPartitionSplitReader.java
index 316431a..507abe2 100644
--- a/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/reader/split/PulsarOrderedPartitionSplitReader.java
+++ b/flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/source/reader/split/PulsarOrderedPartitionSplitReader.java
@@ -20,6 +20,7 @@ package org.apache.flink.connector.pulsar.source.reader.split;
 
 import org.apache.flink.annotation.Internal;
 import org.apache.flink.connector.pulsar.source.config.SourceConfiguration;
+import org.apache.flink.connector.pulsar.source.enumerator.cursor.CursorPosition;
 import org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition;
 import org.apache.flink.connector.pulsar.source.reader.deserializer.PulsarDeserializationSchema;
 import org.apache.flink.connector.pulsar.source.reader.source.PulsarOrderedSourceReader;
@@ -35,12 +36,10 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.time.Duration;
-import java.util.List;
 import java.util.concurrent.TimeUnit;
 
 import static org.apache.flink.connector.pulsar.common.utils.PulsarExceptionUtils.sneakyClient;
 import static org.apache.flink.connector.pulsar.source.config.CursorVerification.FAIL_ON_MISMATCH;
-import static org.apache.flink.connector.pulsar.source.enumerator.cursor.MessageIdUtils.nextMessageId;
 
 /**
  * The split reader a given {@link PulsarPartitionSplit}, it would be closed once the {@link
@@ -83,31 +82,22 @@ public class PulsarOrderedPartitionSplitReader<OUT> extends PulsarPartitionSplit
         if (latestConsumedId != null) {
             LOG.info("Reset subscription position by the checkpoint {}", latestConsumedId);
             try {
-                MessageId initialPosition;
+                CursorPosition cursorPosition;
                 if (latestConsumedId == MessageId.latest
                         || latestConsumedId == MessageId.earliest) {
                     // for compatibility
-                    initialPosition = latestConsumedId;
+                    cursorPosition = new CursorPosition(latestConsumedId, true);
                 } else {
-                    initialPosition = nextMessageId(latestConsumedId);
+                    cursorPosition = new CursorPosition(latestConsumedId, false);
                 }
 
+                String topicName = registeredSplit.getPartition().getFullTopicName();
+                String subscriptionName = sourceConfiguration.getSubscriptionName();
+
                 // Remove Consumer.seek() here for waiting for pulsar-client-all 2.12.0
                 // See https://github.com/apache/pulsar/issues/16757 for more details.
 
-                String topicName = split.getPartition().getFullTopicName();
-                List<String> subscriptions = pulsarAdmin.topics().getSubscriptions(topicName);
-                String subscriptionName = sourceConfiguration.getSubscriptionName();
-
-                if (!subscriptions.contains(subscriptionName)) {
-                    // If this subscription is not available. Just create it.
-                    pulsarAdmin
-                            .topics()
-                            .createSubscription(topicName, subscriptionName, initialPosition);
-                } else {
-                    // Reset the subscription if this is existed.
-                    pulsarAdmin.topics().resetCursor(topicName, subscriptionName, initialPosition);
-                }
+                cursorPosition.seekPosition(pulsarAdmin, topicName, subscriptionName);
             } catch (PulsarAdminException e) {
                 if (sourceConfiguration.getVerifyInitialOffsets() == FAIL_ON_MISMATCH) {
                     throw new IllegalArgumentException(e);
@@ -117,7 +107,7 @@ public class PulsarOrderedPartitionSplitReader<OUT> extends PulsarPartitionSplit
                     LOG.warn(
                             "Failed to reset cursor to {} on partition {}",
                             latestConsumedId,
-                            split.getPartition(),
+                            registeredSplit.getPartition(),
                             e);
                 }
             }