You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by ho...@apache.org on 2022/12/10 13:29:42 UTC

[pulsar] branch master updated: [improve][broker] Improve ManagedCursorContainer if there is only one cursor in ledger (#18737)

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

houxiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new d63ff90966a [improve][broker] Improve ManagedCursorContainer if there is only one cursor in ledger (#18737)
d63ff90966a is described below

commit d63ff90966a5e3a866402fb715357b75f192cc1d
Author: houxiaoyu <ho...@apache.org>
AuthorDate: Sat Dec 10 21:29:35 2022 +0800

    [improve][broker] Improve ManagedCursorContainer if there is only one cursor in ledger (#18737)
---
 .../mledger/impl/ManagedCursorContainer.java       | 40 +++++++++++++---------
 1 file changed, 23 insertions(+), 17 deletions(-)

diff --git a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorContainer.java b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorContainer.java
index f9c7a6d2efe..58c83961d61 100644
--- a/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorContainer.java
+++ b/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorContainer.java
@@ -61,7 +61,7 @@ public class ManagedCursorContainer implements Iterable<ManagedCursor> {
     }
 
     // Used to keep track of slowest cursor.
-    private final ArrayList<Item> heap = new ArrayList();
+    private final ArrayList<Item> heap = new ArrayList<>();
 
     // Maps a cursor to its position in the heap
     private final ConcurrentMap<String, Item> cursors = new ConcurrentSkipListMap<>();
@@ -87,7 +87,9 @@ public class ManagedCursorContainer implements Iterable<ManagedCursor> {
             cursors.put(cursor.getName(), item);
             if (position != null) {
                 heap.add(item);
-                siftUp(item);
+                if (heap.size() > 1) {
+                    siftUp(item);
+                }
             }
             if (cursor.isDurable()) {
                 durableCursorCount++;
@@ -113,12 +115,16 @@ public class ManagedCursorContainer implements Iterable<ManagedCursor> {
             Item item = cursors.remove(name);
             if (item != null) {
                 if (item.idx >= 0) {
-                    // Move the item to the right end of the heap to be removed
-                    Item lastItem = heap.get(heap.size() - 1);
-                    swap(item, lastItem);
-                    heap.remove(item.idx);
-                    // Update the heap
-                    siftDown(lastItem);
+                    if (heap.size() == 1) {
+                        heap.clear();
+                    } else {
+                        // Move the item to the right end of the heap to be removed
+                        Item lastItem = heap.get(heap.size() - 1);
+                        swap(item, lastItem);
+                        heap.remove(item.idx);
+                        // Update the heap
+                        siftDown(lastItem);
+                    }
                 }
                 if (item.cursor.isDurable()) {
                     durableCursorCount--;
@@ -155,19 +161,19 @@ public class ManagedCursorContainer implements Iterable<ManagedCursor> {
             }
 
             PositionImpl previousSlowestConsumer = heap.get(0).position;
-
             item.position = (PositionImpl) newPosition;
-            if (heap.size() > 1) {
-                // When the cursor moves forward, we need to push it toward the
-                // bottom of the tree and push it up if a reset was done
 
-                if (item.idx == 0 || getParent(item).position.compareTo(item.position) <= 0) {
-                    siftDown(item);
-                } else {
-                    siftUp(item);
-                }
+            if (heap.size() == 1) {
+                return Pair.of(previousSlowestConsumer, item.position);
             }
 
+            // When the cursor moves forward, we need to push it toward the
+            // bottom of the tree and push it up if a reset was done
+            if (item.idx == 0 || getParent(item).position.compareTo(item.position) <= 0) {
+                siftDown(item);
+            } else {
+                siftUp(item);
+            }
             PositionImpl newSlowestConsumer = heap.get(0).position;
             return Pair.of(previousSlowestConsumer, newSlowestConsumer);
         } finally {