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

penghui pushed a commit to branch branch-2.10
in repository https://gitbox.apache.org/repos/asf/pulsar.git

commit 964f0ec7c65a57f99e0b4a1c3d16c2d2a8fc4f42
Author: houxiaoyu <[email protected]>
AuthorDate: Fri Nov 18 10:13:15 2022 +0800

    [fix][broker] Fix open cursor with null-initialPosition result with 
earliest position (#18416)
    
    (cherry picked from commit e0606b44c86aded4d46ddf4b2845967a3f9dc4ed)
---
 .../java/org/apache/bookkeeper/mledger/ManagedLedger.java | 12 ++++--------
 .../apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java |  5 +++--
 .../apache/bookkeeper/mledger/impl/ManagedCursorTest.java | 15 +++++++++++++++
 3 files changed, 22 insertions(+), 10 deletions(-)

diff --git 
a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/ManagedLedger.java 
b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/ManagedLedger.java
index ba33b5e43e8..497cbeb171c 100644
--- 
a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/ManagedLedger.java
+++ 
b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/ManagedLedger.java
@@ -220,8 +220,7 @@ public interface ManagedLedger {
      * @param name
      *            the name associated with the ManagedCursor
      * @param initialPosition
-     *            the cursor will be set at latest position or not when first 
created
-     *            default is <b>true</b>
+     *            if null, the cursor will be set at latest position when 
first created
      * @return the ManagedCursor
      * @throws ManagedLedgerException
      */
@@ -236,8 +235,7 @@ public interface ManagedLedger {
      * @param name
      *            the name associated with the ManagedCursor
      * @param initialPosition
-     *            the cursor will be set at latest position or not when first 
created
-     *            default is <b>true</b>
+     *            if null, the cursor will be set at latest position when 
first created
      * @param properties
      *             user defined properties that will be attached to the first 
position of the cursor, if the open
      *             operation will trigger the creation of the cursor.
@@ -323,8 +321,7 @@ public interface ManagedLedger {
      * @param name
      *            the name associated with the ManagedCursor
      * @param initialPosition
-     *            the cursor will be set at lastest position or not when first 
created
-     *            default is <b>true</b>
+     *            if null, the cursor will be set at latest position when 
first created
      * @param callback
      *            callback object
      * @param ctx
@@ -339,8 +336,7 @@ public interface ManagedLedger {
      * @param name
      *            the name associated with the ManagedCursor
      * @param initialPosition
-     *            the cursor will be set at lastest position or not when first 
created
-     *            default is <b>true</b>
+     *            if null, the cursor will be set at latest position when 
first created
      * @param cursorProperties
      *            the properties for the Cursor
      * @param callback
diff --git 
a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java
 
b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java
index bdec6522f3c..a8736ea666c 100644
--- 
a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java
+++ 
b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java
@@ -964,8 +964,9 @@ public class ManagedLedgerImpl implements ManagedLedger, 
CreateCallback {
                 log.info("[{}] Opened new cursor: {}", name, cursor);
                 cursor.setActive();
                 // Update the ack position (ignoring entries that were written 
while the cursor was being created)
-                cursor.initializeCursorPosition(initialPosition == 
InitialPosition.Latest ? getLastPositionAndCounter()
-                        : getFirstPositionAndCounter());
+                cursor.initializeCursorPosition(InitialPosition.Earliest == 
initialPosition
+                        ? getFirstPositionAndCounter()
+                        : getLastPositionAndCounter());
 
                 synchronized (ManagedLedgerImpl.this) {
                     addCursor(cursor);
diff --git 
a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java
 
b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java
index 8b4dfeac1da..e42f46a364b 100644
--- 
a/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java
+++ 
b/managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedCursorTest.java
@@ -118,6 +118,21 @@ public class ManagedCursorTest extends 
MockedBookKeeperTestCase {
     }
 
 
+    @Test
+    public void testOpenCursorWithNullInitialPosition() throws Exception {
+        ManagedLedgerConfig config = new ManagedLedgerConfig();
+        ManagedLedger ledger = 
factory.open("testOpenCursorWithNullInitialPosition", config);
+        // Write some data.
+        ledger.addEntry(new byte[]{1});
+        ledger.addEntry(new byte[]{2});
+        ledger.addEntry(new byte[]{3});
+        ledger.addEntry(new byte[]{4});
+        ledger.addEntry(new byte[]{5});
+
+        ManagedCursorImpl cursor = (ManagedCursorImpl) 
ledger.openCursor("c_testOpenCursorWithNullInitialPosition", null);
+        assertEquals(cursor.getMarkDeletedPosition(), 
ledger.getLastConfirmedEntry());
+    }
+
     @Test(timeOut = 20000)
     void readFromEmptyLedger() throws Exception {
         ManagedLedger ledger = factory.open("my_test_ledger");

Reply via email to