|
| 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 | +} |