You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "poorbarcode (via GitHub)" <gi...@apache.org> on 2023/02/13 06:22:38 UTC

[GitHub] [pulsar] poorbarcode opened a new pull request, #18688: [fix][ml] There are two same-named managed ledgers in the one broker

poorbarcode opened a new pull request, #18688:
URL: https://github.com/apache/pulsar/pull/18688

   ### Motivation
   
   In PR #17526, we know that a `topic` can be closed multiple times and that it is possible to have two same-named objects of class `Persistenttopic` in the same `broker` instance.
   
   We know that closing the `topic` triggers the closure of the `ManagedLedger`. The `topic` object can be closed multiple times which means the `ManagedLedger` can be closed multiple times. This PR is used to prove: If a `ManagedLedger` is closed more than once, and switched `ledgerHandle` operation of `ManagedLedger` and method closed executed concurrently, there will be two of the same-named `Managedledger` in the same broker, possibly with different numbers of cursors.
   
   If both Managedledgers are available and there are different numbers of cursors, this can cause the operation `trimLedgers` to delete too many ledgers from the meta of `ManagedLedger`.
   
   Here is the process: 
   
   | `managedLedger_1.close`| `switch ledgerHandle(managedLedger_1)` | `create managedLedger_2` | `create managedLedger_3` |
   | --- | --- | --- | --- |
   | | close current `LedgerHandle` |
   | | async create new `LedgerHandle` |
   | do close | |
   | set the state to `Closed` | |
   | remove `managedLedger_1` from `ManagedFactory.ledgers` | |
   | | set current ledger to the new `LedgerHandle` |
   | | set the state to `LedgerOpened` |
   | do close the second time | |
   | | | create `managedLedger_2`|
   | | | put `managedLedger_2` to `ManagedFactory.ledgers` |
   | remove `managedLedger_2` from `ManagedFactory.ledgers` | |
   | | | create `cursor_1` into `managedLedger_2` |
   | | | | create `managedLedger_3`|
   | | | | put `managedLedger_3` to `ManagedFactory.ledgers` |
   | | | | recover `cursor_1` from meta |
   | | | | create `cursor_2` into `managedLedger_3` |
   | | | has one cursor `cursor_1` | has two cursor: `cursor_1`, `cursor_2` |
   ----
   
   ### Modifications
   
   - Use Method `remove(k,v)` instead of `remove(k)` when deleting `ManagedLedger` from `ManagedLedgerFactroy.ledgers`.
   - The issue that makes the ManagedLedger state incorrect will be resolved with another PR.
   
   
   ### Documentation
   
   <!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
   
   - [ ] `doc` <!-- Your PR contains doc changes. Please attach the local preview screenshots (run `sh start.sh` at `pulsar/site2/website`) to your PR description, or else your PR might not get merged. -->
   - [ ] `doc-required` <!-- Your PR changes impact docs and you will update later -->
   - [x] `doc-not-needed` <!-- Your PR changes do not impact docs -->
   - [ ] `doc-complete` <!-- Docs have been already added -->
   
   ### Matching PR in forked repository
   
   PR in forked repository: 
   - https://github.com/poorbarcode/pulsar/pull/46
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] poorbarcode commented on a diff in pull request #18688: [fix][ml] There are two same-named managed ledgers in the one broker

Posted by "poorbarcode (via GitHub)" <gi...@apache.org>.
poorbarcode commented on code in PR #18688:
URL: https://github.com/apache/pulsar/pull/18688#discussion_r1190241330


##########
managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/DuplicateManagedLedgerTest.java:
##########
@@ -0,0 +1,187 @@
+/*
+ * 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.bookkeeper.mledger.impl;
+
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.spy;
+import io.netty.buffer.ByteBuf;
+import java.nio.charset.Charset;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.bookkeeper.client.LedgerHandle;
+import org.apache.bookkeeper.mledger.AsyncCallbacks;
+import org.apache.bookkeeper.mledger.ManagedLedger;
+import org.apache.bookkeeper.mledger.ManagedLedgerConfig;
+import org.apache.bookkeeper.mledger.ManagedLedgerException;
+import org.apache.bookkeeper.mledger.Position;
+import org.apache.bookkeeper.test.MockedBookKeeperTestCase;
+import org.awaitility.Awaitility;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+@Test(groups = "broker")
+@Slf4j
+public class DuplicateManagedLedgerTest extends MockedBookKeeperTestCase {
+
+    private void triggerLedgerRollover(ManagedLedger ledger, int maxEntriesPerLedger) {
+        new Thread(() -> {
+            int writeLedgerCount = 2;
+            for (int i = 0; i < writeLedgerCount; i++) {
+                for (int j = 0; j < maxEntriesPerLedger; j++) {
+                    byte[] data = String.format("%s_%s", i, j).getBytes(Charset.defaultCharset());
+                    Object ctx = "";
+                    ledger.asyncAddEntry(data, new AsyncCallbacks.AddEntryCallback() {
+                        @Override
+                        public void addComplete(Position position, ByteBuf entryData, Object ctx) {
+
+                        }
+
+                        @Override
+                        public void addFailed(ManagedLedgerException exception, Object ctx) {
+
+                        }
+                    }, ctx);
+                }
+            }
+        }).start();
+    }
+
+    @Test
+    public void testConcurrentCloseLedgerAndSwitchLedgerForReproduceIssue() throws Exception {
+        String managedLedgerName = "lg_" + UUID.randomUUID().toString().replaceAll("-", "_");
+        int maxEntriesPerLedger = 5;
+        ManagedLedgerConfig config = new ManagedLedgerConfig();
+        config.setThrottleMarkDelete(1);
+        config.setMaximumRolloverTime(Integer.MAX_VALUE, TimeUnit.SECONDS);
+        config.setMaxEntriesPerLedger(5);
+        final ProcessCoordinator processCoordinator = new ProcessCoordinator();
+
+        // call "switch ledger" and "managedLedger.close" concurrently.
+        final ManagedLedgerImpl managedLedger1 = (ManagedLedgerImpl) factory.open(managedLedgerName, config);
+        waitManagedLedgerStateEquals(managedLedger1, ManagedLedgerImpl.State.LedgerOpened);
+        processCoordinator.on();
+        final ManagedLedgerImpl sequentiallyManagedLedger1 =
+                makeManagedLedgerWorksWithStrictlySequentially(managedLedger1, processCoordinator);
+        triggerLedgerRollover(sequentiallyManagedLedger1, maxEntriesPerLedger);
+        sequentiallyManagedLedger1.close();
+        waitManagedLedgerStateNotEquals(managedLedger1, ManagedLedgerImpl.State.Closed);
+
+        // create managedLedger2.
+        final ManagedLedgerImpl managedLedger2 = (ManagedLedgerImpl) factory.open(managedLedgerName, config);
+        Assert.assertEquals(factory.ledgers.size(), 1);
+        Assert.assertNotEquals(managedLedger1, managedLedger2);
+        waitManagedLedgerInFactoryEquals(managedLedger2);
+        processCoordinator.off();
+        managedLedger1.close();
+        waitManagedLedgerStateEquals(managedLedger1, ManagedLedgerImpl.State.Closed);
+        Assert.assertFalse(factory.ledgers.isEmpty());
+        Assert.assertEquals(factory.ledgers.get(managedLedger2.getName()).join(), managedLedger2);
+
+        // cleanup.
+        managedLedger2.close();
+    }
+
+    private ManagedLedgerImpl makeManagedLedgerWorksWithStrictlySequentially(ManagedLedgerImpl originalManagedLedger,
+                                                                             ProcessCoordinator processCoordinator)
+            throws Exception {
+        ManagedLedgerImpl sequentiallyManagedLedger = spy(originalManagedLedger);
+        // step-1.
+        doAnswer(invocation -> {
+            synchronized (originalManagedLedger) {
+                // step-3.
+                // Wait for `managedLedger.close`, then do task: "asyncCreateLedger()".
+                // Because the thread selector in "managedLedger.executor" is random logic, so it is possible to fail.
+                // Adding 1000 tasks to stuck the executor gives a high chance of success.
+                for (int i = 0; i < 1000; i++) {
+                    originalManagedLedger.getExecutor().execute(() -> {
+                        processCoordinator.waitPreviousAndSetStep(3);
+                    });
+                }
+                LedgerHandle lh = (LedgerHandle) invocation.getArguments()[0];
+                processCoordinator.waitPreviousAndSetStep(1);
+                originalManagedLedger.ledgerClosed(lh);
+            }
+            return null;
+        }).when(sequentiallyManagedLedger).ledgerClosed(any(LedgerHandle.class));
+        // step-2.
+        doAnswer(invocation -> {
+            processCoordinator.waitPreviousAndSetStep(2);
+            originalManagedLedger.close();
+            return null;
+        }).when(sequentiallyManagedLedger).close();
+        return sequentiallyManagedLedger;
+    }

Review Comment:
   @lhotari 
   
   And I added other changes<sup>[1]</sup>: 
   - fix the wrong state of the closed managed ledger.
   - release the `ledgerHandle`, which is created after the ML is closed
   
   <strong>[1]<strong>: https://github.com/apache/pulsar/pull/18688/files#diff-f6a849bd8fdb782ef6c17a2e07a2c54c3dc7d1655c00ec3546d5f3b3fc61e970R1531



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] lhotari merged pull request #18688: [fix][ml] There are two same-named managed ledgers in the one broker

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari merged PR #18688:
URL: https://github.com/apache/pulsar/pull/18688


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] michaeljmarshall commented on pull request #18688: [fix][ml] There are two same-named managed ledgers in the one broker

Posted by "michaeljmarshall (via GitHub)" <gi...@apache.org>.
michaeljmarshall commented on PR #18688:
URL: https://github.com/apache/pulsar/pull/18688#issuecomment-1610271956

   As discussed on the mailing list https://lists.apache.org/thread/w4jzk27qhtosgsz7l9bmhf1t7o9mxjhp, there is no plan to release 2.9.6, so I am going to remove the release/2.9.6 label


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] poorbarcode commented on a diff in pull request #18688: [fix][ml] There are two same-named managed ledgers in the one broker

Posted by "poorbarcode (via GitHub)" <gi...@apache.org>.
poorbarcode commented on code in PR #18688:
URL: https://github.com/apache/pulsar/pull/18688#discussion_r1190224889


##########
managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/DuplicateManagedLedgerTest.java:
##########
@@ -0,0 +1,187 @@
+/*
+ * 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.bookkeeper.mledger.impl;
+
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.spy;
+import io.netty.buffer.ByteBuf;
+import java.nio.charset.Charset;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.bookkeeper.client.LedgerHandle;
+import org.apache.bookkeeper.mledger.AsyncCallbacks;
+import org.apache.bookkeeper.mledger.ManagedLedger;
+import org.apache.bookkeeper.mledger.ManagedLedgerConfig;
+import org.apache.bookkeeper.mledger.ManagedLedgerException;
+import org.apache.bookkeeper.mledger.Position;
+import org.apache.bookkeeper.test.MockedBookKeeperTestCase;
+import org.awaitility.Awaitility;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+@Test(groups = "broker")
+@Slf4j
+public class DuplicateManagedLedgerTest extends MockedBookKeeperTestCase {
+
+    private void triggerLedgerRollover(ManagedLedger ledger, int maxEntriesPerLedger) {
+        new Thread(() -> {
+            int writeLedgerCount = 2;
+            for (int i = 0; i < writeLedgerCount; i++) {
+                for (int j = 0; j < maxEntriesPerLedger; j++) {
+                    byte[] data = String.format("%s_%s", i, j).getBytes(Charset.defaultCharset());
+                    Object ctx = "";
+                    ledger.asyncAddEntry(data, new AsyncCallbacks.AddEntryCallback() {
+                        @Override
+                        public void addComplete(Position position, ByteBuf entryData, Object ctx) {
+
+                        }
+
+                        @Override
+                        public void addFailed(ManagedLedgerException exception, Object ctx) {
+
+                        }
+                    }, ctx);
+                }
+            }
+        }).start();
+    }
+
+    @Test
+    public void testConcurrentCloseLedgerAndSwitchLedgerForReproduceIssue() throws Exception {
+        String managedLedgerName = "lg_" + UUID.randomUUID().toString().replaceAll("-", "_");
+        int maxEntriesPerLedger = 5;
+        ManagedLedgerConfig config = new ManagedLedgerConfig();
+        config.setThrottleMarkDelete(1);
+        config.setMaximumRolloverTime(Integer.MAX_VALUE, TimeUnit.SECONDS);
+        config.setMaxEntriesPerLedger(5);
+        final ProcessCoordinator processCoordinator = new ProcessCoordinator();
+
+        // call "switch ledger" and "managedLedger.close" concurrently.
+        final ManagedLedgerImpl managedLedger1 = (ManagedLedgerImpl) factory.open(managedLedgerName, config);
+        waitManagedLedgerStateEquals(managedLedger1, ManagedLedgerImpl.State.LedgerOpened);
+        processCoordinator.on();
+        final ManagedLedgerImpl sequentiallyManagedLedger1 =
+                makeManagedLedgerWorksWithStrictlySequentially(managedLedger1, processCoordinator);
+        triggerLedgerRollover(sequentiallyManagedLedger1, maxEntriesPerLedger);
+        sequentiallyManagedLedger1.close();
+        waitManagedLedgerStateNotEquals(managedLedger1, ManagedLedgerImpl.State.Closed);
+
+        // create managedLedger2.
+        final ManagedLedgerImpl managedLedger2 = (ManagedLedgerImpl) factory.open(managedLedgerName, config);
+        Assert.assertEquals(factory.ledgers.size(), 1);
+        Assert.assertNotEquals(managedLedger1, managedLedger2);
+        waitManagedLedgerInFactoryEquals(managedLedger2);
+        processCoordinator.off();
+        managedLedger1.close();
+        waitManagedLedgerStateEquals(managedLedger1, ManagedLedgerImpl.State.Closed);
+        Assert.assertFalse(factory.ledgers.isEmpty());
+        Assert.assertEquals(factory.ledgers.get(managedLedger2.getName()).join(), managedLedger2);
+
+        // cleanup.
+        managedLedger2.close();
+    }
+
+    private ManagedLedgerImpl makeManagedLedgerWorksWithStrictlySequentially(ManagedLedgerImpl originalManagedLedger,
+                                                                             ProcessCoordinator processCoordinator)
+            throws Exception {
+        ManagedLedgerImpl sequentiallyManagedLedger = spy(originalManagedLedger);
+        // step-1.
+        doAnswer(invocation -> {
+            synchronized (originalManagedLedger) {
+                // step-3.
+                // Wait for `managedLedger.close`, then do task: "asyncCreateLedger()".
+                // Because the thread selector in "managedLedger.executor" is random logic, so it is possible to fail.
+                // Adding 1000 tasks to stuck the executor gives a high chance of success.
+                for (int i = 0; i < 1000; i++) {
+                    originalManagedLedger.getExecutor().execute(() -> {
+                        processCoordinator.waitPreviousAndSetStep(3);
+                    });
+                }
+                LedgerHandle lh = (LedgerHandle) invocation.getArguments()[0];
+                processCoordinator.waitPreviousAndSetStep(1);
+                originalManagedLedger.ledgerClosed(lh);
+            }
+            return null;
+        }).when(sequentiallyManagedLedger).ledgerClosed(any(LedgerHandle.class));
+        // step-2.
+        doAnswer(invocation -> {
+            processCoordinator.waitPreviousAndSetStep(2);
+            originalManagedLedger.close();
+            return null;
+        }).when(sequentiallyManagedLedger).close();
+        return sequentiallyManagedLedger;
+    }

Review Comment:
   @lhotari 
   
   Thanks for your suggestion. I have rewritten the test to make it simpler. Could you take a look again?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] github-actions[bot] commented on pull request #18688: [fix] [ml] There are two same-named managed ledgers in the one broker

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on PR #18688:
URL: https://github.com/apache/pulsar/pull/18688#issuecomment-1383023608

   The pr had no activity for 30 days, mark with Stale label.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] lhotari commented on a diff in pull request #18688: [fix][ml] There are two same-named managed ledgers in the one broker

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on code in PR #18688:
URL: https://github.com/apache/pulsar/pull/18688#discussion_r1189737621


##########
managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/DuplicateManagedLedgerTest.java:
##########
@@ -0,0 +1,187 @@
+/*
+ * 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.bookkeeper.mledger.impl;
+
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.spy;
+import io.netty.buffer.ByteBuf;
+import java.nio.charset.Charset;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.bookkeeper.client.LedgerHandle;
+import org.apache.bookkeeper.mledger.AsyncCallbacks;
+import org.apache.bookkeeper.mledger.ManagedLedger;
+import org.apache.bookkeeper.mledger.ManagedLedgerConfig;
+import org.apache.bookkeeper.mledger.ManagedLedgerException;
+import org.apache.bookkeeper.mledger.Position;
+import org.apache.bookkeeper.test.MockedBookKeeperTestCase;
+import org.awaitility.Awaitility;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+@Test(groups = "broker")
+@Slf4j
+public class DuplicateManagedLedgerTest extends MockedBookKeeperTestCase {
+
+    private void triggerLedgerRollover(ManagedLedger ledger, int maxEntriesPerLedger) {
+        new Thread(() -> {
+            int writeLedgerCount = 2;
+            for (int i = 0; i < writeLedgerCount; i++) {
+                for (int j = 0; j < maxEntriesPerLedger; j++) {
+                    byte[] data = String.format("%s_%s", i, j).getBytes(Charset.defaultCharset());
+                    Object ctx = "";
+                    ledger.asyncAddEntry(data, new AsyncCallbacks.AddEntryCallback() {
+                        @Override
+                        public void addComplete(Position position, ByteBuf entryData, Object ctx) {
+
+                        }
+
+                        @Override
+                        public void addFailed(ManagedLedgerException exception, Object ctx) {
+
+                        }
+                    }, ctx);
+                }
+            }
+        }).start();
+    }
+
+    @Test
+    public void testConcurrentCloseLedgerAndSwitchLedgerForReproduceIssue() throws Exception {
+        String managedLedgerName = "lg_" + UUID.randomUUID().toString().replaceAll("-", "_");
+        int maxEntriesPerLedger = 5;
+        ManagedLedgerConfig config = new ManagedLedgerConfig();
+        config.setThrottleMarkDelete(1);
+        config.setMaximumRolloverTime(Integer.MAX_VALUE, TimeUnit.SECONDS);
+        config.setMaxEntriesPerLedger(5);
+        final ProcessCoordinator processCoordinator = new ProcessCoordinator();
+
+        // call "switch ledger" and "managedLedger.close" concurrently.
+        final ManagedLedgerImpl managedLedger1 = (ManagedLedgerImpl) factory.open(managedLedgerName, config);
+        waitManagedLedgerStateEquals(managedLedger1, ManagedLedgerImpl.State.LedgerOpened);
+        processCoordinator.on();
+        final ManagedLedgerImpl sequentiallyManagedLedger1 =
+                makeManagedLedgerWorksWithStrictlySequentially(managedLedger1, processCoordinator);
+        triggerLedgerRollover(sequentiallyManagedLedger1, maxEntriesPerLedger);
+        sequentiallyManagedLedger1.close();
+        waitManagedLedgerStateNotEquals(managedLedger1, ManagedLedgerImpl.State.Closed);
+
+        // create managedLedger2.
+        final ManagedLedgerImpl managedLedger2 = (ManagedLedgerImpl) factory.open(managedLedgerName, config);
+        Assert.assertEquals(factory.ledgers.size(), 1);
+        Assert.assertNotEquals(managedLedger1, managedLedger2);
+        waitManagedLedgerInFactoryEquals(managedLedger2);
+        processCoordinator.off();
+        managedLedger1.close();
+        waitManagedLedgerStateEquals(managedLedger1, ManagedLedgerImpl.State.Closed);
+        Assert.assertFalse(factory.ledgers.isEmpty());
+        Assert.assertEquals(factory.ledgers.get(managedLedger2.getName()).join(), managedLedger2);
+
+        // cleanup.
+        managedLedger2.close();
+    }
+
+    private ManagedLedgerImpl makeManagedLedgerWorksWithStrictlySequentially(ManagedLedgerImpl originalManagedLedger,
+                                                                             ProcessCoordinator processCoordinator)
+            throws Exception {
+        ManagedLedgerImpl sequentiallyManagedLedger = spy(originalManagedLedger);
+        // step-1.
+        doAnswer(invocation -> {
+            synchronized (originalManagedLedger) {
+                // step-3.
+                // Wait for `managedLedger.close`, then do task: "asyncCreateLedger()".
+                // Because the thread selector in "managedLedger.executor" is random logic, so it is possible to fail.
+                // Adding 1000 tasks to stuck the executor gives a high chance of success.
+                for (int i = 0; i < 1000; i++) {
+                    originalManagedLedger.getExecutor().execute(() -> {
+                        processCoordinator.waitPreviousAndSetStep(3);
+                    });
+                }
+                LedgerHandle lh = (LedgerHandle) invocation.getArguments()[0];
+                processCoordinator.waitPreviousAndSetStep(1);
+                originalManagedLedger.ledgerClosed(lh);
+            }
+            return null;
+        }).when(sequentiallyManagedLedger).ledgerClosed(any(LedgerHandle.class));
+        // step-2.
+        doAnswer(invocation -> {
+            processCoordinator.waitPreviousAndSetStep(2);
+            originalManagedLedger.close();
+            return null;
+        }).when(sequentiallyManagedLedger).close();
+        return sequentiallyManagedLedger;
+    }

Review Comment:
   this looks like a hack, especially the loop of adding 1000 tasks to the executor. The "ProcessCoordinator" implementation looks like something that could be handled with [`java.util.concurrent.Phaser`](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Phaser.html). 
   It would be better to modify ManagedLedgerFactoryImpl so that it's possible to override a method that creates the ledger instance. That way it would be possible to have a way to override the method for tests and inject test logic without relying on Mockito, which isn't thread safe. That itself could cause issues.
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] poorbarcode commented on pull request #18688: [fix][ml] There are two same-named managed ledgers in the one broker

Posted by "poorbarcode (via GitHub)" <gi...@apache.org>.
poorbarcode commented on PR #18688:
URL: https://github.com/apache/pulsar/pull/18688#issuecomment-1541933006

   @lhotari 
   
   > I assume this PR is still relevant?
   
   Yes.
   
   > Please merge latest changes from master and check that tests still pass.
   
   Done.
   
   Could you please help review this PR?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] Technoboy- closed pull request #18688: [fix][ml] There are two same-named managed ledgers in the one broker

Posted by "Technoboy- (via GitHub)" <gi...@apache.org>.
Technoboy- closed pull request #18688: [fix][ml] There are two same-named managed ledgers in the one broker
URL: https://github.com/apache/pulsar/pull/18688


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] codecov-commenter commented on pull request #18688: [fix][ml] There are two same-named managed ledgers in the one broker

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #18688:
URL: https://github.com/apache/pulsar/pull/18688#issuecomment-1428497711

   # [Codecov](https://codecov.io/gh/apache/pulsar/pull/18688?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
   > Merging [#18688](https://codecov.io/gh/apache/pulsar/pull/18688?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (f64173d) into [master](https://codecov.io/gh/apache/pulsar/commit/5c8f92965637fbcd2b7e9e0e429884ce527e73ae?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (5c8f929) will **increase** coverage by `0.60%`.
   > The diff coverage is `53.05%`.
   
   [![Impacted file tree graph](https://codecov.io/gh/apache/pulsar/pull/18688/graphs/tree.svg?width=650&height=150&src=pr&token=acYqCpsK9J&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)](https://codecov.io/gh/apache/pulsar/pull/18688?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
   
   ```diff
   @@             Coverage Diff              @@
   ##             master   #18688      +/-   ##
   ============================================
   + Coverage     63.30%   63.90%   +0.60%     
   + Complexity    26123     3490   -22633     
   ============================================
     Files          1836     1843       +7     
     Lines        134416   135163     +747     
     Branches      14772    14859      +87     
   ============================================
   + Hits          85087    86371    +1284     
   + Misses        41649    40949     -700     
   - Partials       7680     7843     +163     
   ```
   
   | Flag | Coverage Δ | |
   |---|---|---|
   | inttests | `24.70% <5.95%> (-0.18%)` | :arrow_down: |
   | systests | `25.36% <0.49%> (-0.20%)` | :arrow_down: |
   | unittests | `61.21% <53.05%> (+0.58%)` | :arrow_up: |
   
   Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#carryforward-flags-in-the-pull-request-comment) to find out more.
   
   | [Impacted Files](https://codecov.io/gh/apache/pulsar/pull/18688?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
   |---|---|---|
   | [...g/apache/pulsar/client/cli/AbstractCmdConsume.java](https://codecov.io/gh/apache/pulsar/pull/18688?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWNsaWVudC10b29scy9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2NsaWVudC9jbGkvQWJzdHJhY3RDbWRDb25zdW1lLmphdmE=) | `20.45% <20.45%> (ø)` | |
   | [.../java/org/apache/pulsar/client/cli/CmdConsume.java](https://codecov.io/gh/apache/pulsar/pull/18688?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWNsaWVudC10b29scy9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2NsaWVudC9jbGkvQ21kQ29uc3VtZS5qYXZh) | `40.44% <25.00%> (+8.45%)` | :arrow_up: |
   | [...ain/java/org/apache/pulsar/client/cli/CmdRead.java](https://codecov.io/gh/apache/pulsar/pull/18688?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWNsaWVudC10b29scy9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2NsaWVudC9jbGkvQ21kUmVhZC5qYXZh) | `41.13% <41.13%> (ø)` | |
   | [...r/impl/SnapshotSegmentAbortedTxnProcessorImpl.java](https://codecov.io/gh/apache/pulsar/pull/18688?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci90cmFuc2FjdGlvbi9idWZmZXIvaW1wbC9TbmFwc2hvdFNlZ21lbnRBYm9ydGVkVHhuUHJvY2Vzc29ySW1wbC5qYXZh) | `62.79% <62.79%> (ø)` | |
   | [...kkeeper/mledger/impl/ManagedLedgerFactoryImpl.java](https://codecov.io/gh/apache/pulsar/pull/18688?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-bWFuYWdlZC1sZWRnZXIvc3JjL21haW4vamF2YS9vcmcvYXBhY2hlL2Jvb2trZWVwZXIvbWxlZGdlci9pbXBsL01hbmFnZWRMZWRnZXJGYWN0b3J5SW1wbC5qYXZh) | `78.86% <71.42%> (-0.57%)` | :arrow_down: |
   | [...ransaction/buffer/impl/TopicTransactionBuffer.java](https://codecov.io/gh/apache/pulsar/pull/18688?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci90cmFuc2FjdGlvbi9idWZmZXIvaW1wbC9Ub3BpY1RyYW5zYWN0aW9uQnVmZmVyLmphdmE=) | `74.27% <85.71%> (+11.77%)` | :arrow_up: |
   | [...r/service/SystemTopicTxnBufferSnapshotService.java](https://codecov.io/gh/apache/pulsar/pull/18688?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci9zZXJ2aWNlL1N5c3RlbVRvcGljVHhuQnVmZmVyU25hcHNob3RTZXJ2aWNlLmphdmE=) | `78.26% <100.00%> (ø)` | |
   | [...er/systopic/NamespaceEventsSystemTopicFactory.java](https://codecov.io/gh/apache/pulsar/pull/18688?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci9zeXN0b3BpYy9OYW1lc3BhY2VFdmVudHNTeXN0ZW1Ub3BpY0ZhY3RvcnkuamF2YQ==) | `85.71% <100.00%> (+12.38%)` | :arrow_up: |
   | [...ransactionBufferSnapshotBaseSystemTopicClient.java](https://codecov.io/gh/apache/pulsar/pull/18688?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci9zeXN0b3BpYy9UcmFuc2FjdGlvbkJ1ZmZlclNuYXBzaG90QmFzZVN5c3RlbVRvcGljQ2xpZW50LmphdmE=) | `72.36% <100.00%> (+4.36%)` | :arrow_up: |
   | [...er/impl/SingleSnapshotAbortedTxnProcessorImpl.java](https://codecov.io/gh/apache/pulsar/pull/18688?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci90cmFuc2FjdGlvbi9idWZmZXIvaW1wbC9TaW5nbGVTbmFwc2hvdEFib3J0ZWRUeG5Qcm9jZXNzb3JJbXBsLmphdmE=) | `77.65% <100.00%> (+7.00%)` | :arrow_up: |
   | ... and [217 more](https://codecov.io/gh/apache/pulsar/pull/18688?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | |
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] lhotari commented on pull request #18688: [fix][ml] There are two same-named managed ledgers in the one broker

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on PR #18688:
URL: https://github.com/apache/pulsar/pull/18688#issuecomment-1541947065

   > Could you please help review this PR?
   
   @poorbarcode The change to production code itself looks good, but I don't like the test. Addressing that would require more changes to ML factory to make it possible to sub class it for tests with test hooks instead of relying on Mockito.
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] lhotari commented on pull request #18688: [fix][ml] There are two same-named managed ledgers in the one broker

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on PR #18688:
URL: https://github.com/apache/pulsar/pull/18688#issuecomment-1541909042

   @poorbarcode I assume this PR is still relevant? Please merge latest changes from master and check that tests still pass.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] github-actions[bot] commented on pull request #18688: [fix][ml] There are two same-named managed ledgers in the one broker

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #18688:
URL: https://github.com/apache/pulsar/pull/18688#issuecomment-1471164497

   The pr had no activity for 30 days, mark with Stale label.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org