GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [fix][ml] Fix active cursor position tracking after untracking (#26629) · ascentstream/pulsar@467159e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • managed-ledger/src
    • main/java/org/apache/bookkeeper/mledger/impl
      • ActiveManagedCursorContainerImpl.java
    • test/java/org/apache/bookkeeper/mledger/impl
      • ActiveManagedCursorContainerUntrackingTest.java
      • ManagedLedgerBkTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ActiveManagedCursorContainerImpl.java‎

Copy file name to clipboardExpand all lines: managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ActiveManagedCursorContainerImpl.java
+11-3Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,10 @@ public void updateCursor(ManagedCursor cursor, Position newPosition) {
387387
return;
388388
}
389389
if (newPosition == null) {
390-
pendingRemovedCursors.put(cursor.getName(), node);
391-
node.pendingRemove = true;
390+
if (node.position != null) {
391+
pendingRemovedCursors.put(cursor.getName(), node);
392+
node.pendingRemove = true;
393+
}
392394
node.pendingPosition = null;
393395
} else {
394396
if (node.pendingRemove) {
@@ -431,6 +433,7 @@ private void performIncrementalUpdate() {
431433
if (!pendingRemovedCursors.isEmpty()) {
432434
for (Node node : pendingRemovedCursors.values()) {
433435
removeNodeFromList(node);
436+
node.position = null;
434437
node.pendingRemove = false;
435438
}
436439
pendingRemovedCursors.clear();
@@ -481,7 +484,12 @@ private void rebuildEntireList() {
481484
node.position = node.pendingPosition;
482485
node.pendingPosition = null;
483486
}
484-
if (node.position != null && !node.pendingRemove) {
487+
if (node.pendingRemove) {
488+
// Untracked cursors stay in the map and may later be inserted incrementally.
489+
node.position = null;
490+
node.prev = null;
491+
node.next = null;
492+
} else if (node.position != null) {
485493
activeNodes.add(node);
486494
trackedNodeCount++;
487495
}
Collapse file

‎managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ActiveManagedCursorContainerUntrackingTest.java‎

Copy file name to clipboard
+163Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.bookkeeper.mledger.impl;
20+
21+
import static org.apache.bookkeeper.mledger.impl.MockManagedCursor.createCursor;
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
import org.apache.bookkeeper.mledger.ManagedCursor;
26+
import org.apache.bookkeeper.mledger.Position;
27+
import org.apache.bookkeeper.mledger.PositionFactory;
28+
import org.testng.annotations.BeforeMethod;
29+
import org.testng.annotations.DataProvider;
30+
import org.testng.annotations.Test;
31+
32+
public class ActiveManagedCursorContainerUntrackingTest {
33+
private ActiveManagedCursorContainerImpl container;
34+
private final List<ManagedCursor> cursors = new ArrayList<>();
35+
private final List<Position> positions = new ArrayList<>();
36+
37+
@BeforeMethod(alwaysRun = true)
38+
public void createContainer() {
39+
container = new ActiveManagedCursorContainerImpl();
40+
positions.clear();
41+
cursors.clear();
42+
for (int i = 0; i < 7; i++) {
43+
positions.add(PositionFactory.create(1, i));
44+
}
45+
for (int i = 0; i < 5; i++) {
46+
Position position = positions.get(i + 1);
47+
ManagedCursor cursor = createCursor(container, "cursor" + i, position);
48+
container.add(cursor, position);
49+
cursors.add(cursor);
50+
}
51+
assertThat(container.size()).isEqualTo(5);
52+
assertThat(container.getSlowestCursorPosition()).isEqualTo(positions.get(1));
53+
}
54+
55+
@DataProvider
56+
public Object[][] untrackingPaths() {
57+
return new Object[][] {{false, false}, {false, true}, {true, false}, {true, true}};
58+
}
59+
60+
@Test(dataProvider = "untrackingPaths")
61+
public void testFlushedUntrackingRemainsUntracked(boolean rebuild, boolean useAdd) {
62+
ManagedCursor cursor = cursors.get(2);
63+
untrack(cursor, useAdd);
64+
flushUntracking(rebuild);
65+
66+
assertUntracked(cursor);
67+
// Updating two of the four remaining nodes forces a rebuild. It must not resurrect the cursor.
68+
queueRebuild();
69+
assertUntracked(cursor);
70+
assertThat(container.getNumberOfCursorsAtSamePositionOrBefore(cursors.get(4))).isEqualTo(4);
71+
container.checkOrderingAndNumberOfCursorsState();
72+
}
73+
74+
@DataProvider
75+
public Object[][] reactivationPaths() {
76+
return new Object[][] {
77+
{false, false, false}, {false, true, false}, {true, false, false}, {true, true, false},
78+
{false, false, true}, {false, true, true}, {true, false, true}, {true, true, true}
79+
};
80+
}
81+
82+
@Test(dataProvider = "reactivationPaths", timeOut = 30000)
83+
public void testRetrackAfterFlush(boolean rebuild, boolean moveForward, boolean useAdd) {
84+
ManagedCursor cursor = cursors.get(2);
85+
untrack(cursor, false);
86+
flushUntracking(rebuild);
87+
Position restored = positions.get(moveForward ? 6 : 0);
88+
if (useAdd) {
89+
container.add(cursor, restored);
90+
} else {
91+
container.updateCursor(cursor, restored);
92+
}
93+
// One update among four tracked nodes forces incremental insertion after either removal path.
94+
assertThat(container.getNumberOfCursorsAtSamePositionOrBefore(cursor)).isEqualTo(moveForward ? 5 : 1);
95+
assertThat(container.getSlowestCursorPosition()).isEqualTo(moveForward ? positions.get(1) : restored);
96+
for (int i = 0; i < cursors.size(); i++) {
97+
if (i != 2) {
98+
int rankWithoutRestored = i < 2 ? i + 1 : i;
99+
assertThat(container.getNumberOfCursorsAtSamePositionOrBefore(cursors.get(i)))
100+
.as("rank of cursor%s", i).isEqualTo(rankWithoutRestored + (moveForward ? 0 : 1));
101+
}
102+
}
103+
container.checkOrderingAndNumberOfCursorsState();
104+
}
105+
106+
@Test
107+
public void testRepeatedUntrackingAfterFlush() {
108+
ManagedCursor cursor = cursors.get(2);
109+
container.updateCursor(cursor, null);
110+
container.getSlowestCursorPosition();
111+
container.updateCursor(cursor, null);
112+
113+
assertThat(container.getSlowestCursorPosition()).isEqualTo(positions.get(1));
114+
assertUntracked(cursor);
115+
container.checkOrderingAndNumberOfCursorsState();
116+
}
117+
118+
@Test
119+
public void testUntrackingBeforeFirstPositionFlush() {
120+
// The last cursor never enters this container's ordered list.
121+
ActiveManagedCursorContainerImpl other = new ActiveManagedCursorContainerImpl();
122+
for (int i = 0; i < 4; i++) {
123+
other.add(cursors.get(i), positions.get(i + 1));
124+
}
125+
other.getSlowestCursorPosition();
126+
ManagedCursor cursor = cursors.get(4);
127+
other.add(cursor, null);
128+
other.updateCursor(cursor, null);
129+
130+
assertThat(other.getSlowestCursorPosition()).isEqualTo(positions.get(1));
131+
assertThat(other.getNumberOfCursorsAtSamePositionOrBefore(cursor)).isZero();
132+
assertThat(other.getNumberOfCursorsAtSamePositionOrBefore(cursors.get(3))).isEqualTo(4);
133+
other.checkOrderingAndNumberOfCursorsState();
134+
}
135+
136+
private void untrack(ManagedCursor cursor, boolean useAdd) {
137+
// Normal broker deactivation removes the cursor. Null untracking is a separate container API operation.
138+
if (useAdd) {
139+
container.add(cursor, null);
140+
} else {
141+
container.updateCursor(cursor, null);
142+
}
143+
}
144+
145+
private void flushUntracking(boolean rebuild) {
146+
if (rebuild) {
147+
queueRebuild();
148+
}
149+
// No position updates selects incremental removal; two updates among five nodes select a rebuild.
150+
assertThat(container.getSlowestCursorPosition()).isEqualTo(positions.get(1));
151+
}
152+
153+
private void queueRebuild() {
154+
container.updateCursor(cursors.get(0), positions.get(1));
155+
container.updateCursor(cursors.get(4), positions.get(5));
156+
}
157+
158+
private void assertUntracked(ManagedCursor cursor) {
159+
assertThat(container.getNumberOfCursorsAtSamePositionOrBefore(cursor)).isZero();
160+
assertThat(container.size()).isEqualTo(5);
161+
assertThat(container.get(cursor.getName())).isSameAs(cursor);
162+
}
163+
}
Collapse file

‎managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerBkTest.java‎

Copy file name to clipboardExpand all lines: managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerBkTest.java
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.bookkeeper.mledger.impl;
2020

2121
import static org.apache.bookkeeper.mledger.util.ManagedLedgerTestUtil.rawEntryConfig;
22+
import static org.assertj.core.api.Assertions.assertThat;
2223
import static org.testng.Assert.assertEquals;
2324
import static org.testng.Assert.assertFalse;
2425
import static org.testng.Assert.assertNotEquals;
@@ -66,6 +67,7 @@
6667
import org.apache.bookkeeper.mledger.proto.PositionInfo;
6768
import org.apache.bookkeeper.mledger.util.ThrowableToStringUtil;
6869
import org.apache.bookkeeper.test.BookKeeperClusterTestCase;
70+
import org.apache.pulsar.common.api.proto.CommandSubscribe.InitialPosition;
6971
import org.apache.pulsar.common.policies.data.PersistentOfflineTopicStats;
7072
import org.apache.pulsar.common.util.FutureUtil;
7173
import org.awaitility.Awaitility;
@@ -129,6 +131,47 @@ public void testSimpleRead() throws Exception {
129131
entries.forEach(Entry::release);
130132
}
131133

134+
@Test
135+
public void testSeekRetracksCursorAfterUntracking() throws Exception {
136+
@Cleanup("shutdown")
137+
ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(metadataStore, bkc);
138+
ManagedLedgerConfig config = rawEntryConfig()
139+
.setEnsembleSize(1).setWriteQuorumSize(1).setAckQuorumSize(1);
140+
config.setCacheEvictionByExpectedReadCount(true);
141+
config.setCacheEvictionByMarkDeletedPosition(false);
142+
ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open("cursor-untracking-" + testName, config);
143+
List<Position> positions = new ArrayList<>();
144+
for (int i = 0; i < 7; i++) {
145+
positions.add(ledger.addEntry(("entry-" + i).getBytes(StandardCharsets.UTF_8)));
146+
}
147+
List<ManagedCursor> cursors = new ArrayList<>();
148+
for (int i = 0; i < 5; i++) {
149+
ManagedCursor cursor = ledger.openCursor("cursor" + i, InitialPosition.Earliest);
150+
cursor.seek(positions.get(i + 1));
151+
cursor.setActive();
152+
cursors.add(cursor);
153+
}
154+
ActiveManagedCursorContainer container = ledger.getActiveCursors();
155+
assertThat(container.getSlowestCursorPosition()).isEqualTo(positions.get(1));
156+
ManagedCursor cursor = cursors.get(2);
157+
// Null untracking is a container API operation; normal broker deactivation removes the cursor.
158+
container.updateCursor(cursor, null);
159+
assertThat(container.getSlowestCursorPosition()).isEqualTo(positions.get(1));
160+
161+
// Exercise ManagedCursorImpl -> ManagedLedgerImpl -> the active cursor container.
162+
// One update among four tracked nodes forces incremental reinsertion.
163+
cursor.seek(positions.get(6));
164+
assertThat(ledger.getNumberOfCursorsAtSamePositionOrBefore(cursor)).isEqualTo(5);
165+
assertThat(container.getSlowestCursorPosition()).isEqualTo(positions.get(1));
166+
assertThat(container.size()).isEqualTo(5);
167+
for (int i = 0; i < cursors.size(); i++) {
168+
if (i != 2) {
169+
assertThat(ledger.getNumberOfCursorsAtSamePositionOrBefore(cursors.get(i)))
170+
.as("rank of cursor%s", i).isEqualTo(i < 2 ? i + 1 : i);
171+
}
172+
}
173+
}
174+
132175
@Test
133176
public void testBookieFailure() throws Exception {
134177
ManagedLedgerFactory factory = new ManagedLedgerFactoryImpl(metadataStore, bkc);

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.