merlimat commented on a change in pull request #2041: Added ReadOnlyCursor to 
ManagedLedger
URL: https://github.com/apache/incubator-pulsar/pull/2041#discussion_r200460406
 
 

 ##########
 File path: 
managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ReadOnlyManagedLedgerImpl.java
 ##########
 @@ -0,0 +1,130 @@
+/**
+ * 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 java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionException;
+
+import org.apache.bookkeeper.client.BookKeeper;
+import org.apache.bookkeeper.common.util.OrderedExecutor;
+import org.apache.bookkeeper.common.util.OrderedScheduler;
+import org.apache.bookkeeper.mledger.ManagedLedgerConfig;
+import org.apache.bookkeeper.mledger.ManagedLedgerException;
+import 
org.apache.bookkeeper.mledger.ManagedLedgerException.ManagedLedgerNotFoundException;
+import org.apache.bookkeeper.mledger.ManagedLedgerException.MetaStoreException;
+import 
org.apache.bookkeeper.mledger.ManagedLedgerException.MetadataNotFoundException;
+import org.apache.bookkeeper.mledger.ReadOnlyCursor;
+import org.apache.bookkeeper.mledger.impl.MetaStore.MetaStoreCallback;
+import org.apache.bookkeeper.mledger.impl.MetaStore.Stat;
+import org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo;
+import 
org.apache.bookkeeper.mledger.proto.MLDataFormats.ManagedLedgerInfo.LedgerInfo;
+
+public class ReadOnlyManagedLedgerImpl extends ManagedLedgerImpl {
+
+    public ReadOnlyManagedLedgerImpl(ManagedLedgerFactoryImpl factory, 
BookKeeper bookKeeper, MetaStore store,
+            ManagedLedgerConfig config, OrderedScheduler scheduledExecutor, 
OrderedExecutor orderedExecutor,
+            String name) {
+        super(factory, bookKeeper, store, config, scheduledExecutor, 
orderedExecutor, name);
+    }
+
+    CompletableFuture<ReadOnlyCursor> initializeAndCreateCursor(PositionImpl 
startPosition) {
+        CompletableFuture<ReadOnlyCursor> future = new CompletableFuture<>();
+
+        // Fetch the list of existing ledgers in the managed ledger
+        store.getManagedLedgerInfo(name, false, new 
MetaStoreCallback<ManagedLedgerInfo>() {
+            @Override
+            public void operationComplete(ManagedLedgerInfo mlInfo, Stat stat) 
{
+                state = State.LedgerOpened;
+
+                for (LedgerInfo ls : mlInfo.getLedgerInfoList()) {
+                    ledgers.put(ls.getLedgerId(), ls);
+                }
+
+                // Last ledger stat may be zeroed, we must update it
+                if (ledgers.size() > 0 && 
ledgers.lastEntry().getValue().getEntries() == 0) {
 
 Review comment:
   When a ledger is added to the list of ManagedLedger, we add it with 0 
entries, and then this entry count is only updated after the ledger is closed. 
If the broker crashes, the metadata is left at 0 and the we'll get "last entry" 
after running the ledger recovery. 
   
   In this case, we don't run the ledger recovery, but we just have to find out 
the last add confirmed in the ledger. 
   
   If the entry count was > 0, it means the managed ledger was gracefully 
closed and the number of entries in last ledger was updated in the metadata, so 
there's no need to discover the last add confirmed.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to