sijie commented on a change in pull request #1218: Support LacPiggyback, 
LongPoll and ExplicitLac in db ledger storage
URL: https://github.com/apache/bookkeeper/pull/1218#discussion_r171256273
 
 

 ##########
 File path: 
bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/ldb/DbLedgerStorage.java
 ##########
 @@ -74,10 +83,121 @@
  */
 public class DbLedgerStorage implements CompactableLedgerStorage {
 
+    /**
+     * This class borrows the logic from FileInfo.
+     *
+     * <p>This class is used for holding all the transient states for a given 
ledger.
+     */
+    private static class TransientLedgerInfo
+            extends Watchable<LastAddConfirmedUpdateNotification>
+            implements AutoCloseable {
+
+        // lac
+        private Long lac = null;
+        // request from explicit lac requests
+        private ByteBuffer explicitLac = null;
+        // is the ledger info closed?
+        private boolean isClosed;
+
+        private final long ledgerId;
+        // reference to LedgerMetadataIndex
+        private final LedgerMetadataIndex ledgerIndex;
+
+        /**
+         * Construct an Watchable with zero watchers.
+         */
+        public TransientLedgerInfo(long ledgerId, LedgerMetadataIndex 
ledgerIndex) {
+            super(WATCHER_RECYCLER);
+            this.ledgerId = ledgerId;
+            this.ledgerIndex = ledgerIndex;
+        }
+
+        synchronized Long getLastAddConfirmed() {
+            return lac;
+        }
+
+        Long setLastAddConfirmed(long lac) {
+            long lacToReturn;
+            boolean changed = false;
+            synchronized (this) {
+                if (null == this.lac || this.lac < lac) {
+                    this.lac = lac;
+                    changed = true;
+                }
+                lacToReturn = this.lac;
+            }
+            if (changed) {
+                notifyWatchers(lacToReturn);
+            }
+            return lacToReturn;
+        }
+
+        synchronized boolean waitForLastAddConfirmedUpdate(long previousLAC,
+                                                           
Watcher<LastAddConfirmedUpdateNotification> watcher)
+                throws IOException {
+            if ((null != lac && lac > previousLAC)
+                    || isClosed || ledgerIndex.get(ledgerId).getFenced()) {
+                return false;
+            }
+
+            addWatcher(watcher);
+            return true;
+        }
+
+        public ByteBuf getExplicitLac() {
+            ByteBuf retLac = null;
+            synchronized (this) {
+                if (explicitLac != null) {
+                    retLac = Unpooled.buffer(explicitLac.capacity());
+                    explicitLac.rewind(); //copy from the beginning
+                    retLac.writeBytes(explicitLac);
+                    explicitLac.rewind();
+                    return retLac;
+                }
+            }
+            return retLac;
+        }
+
+        public void setExplicitLac(ByteBuf lac) {
+            long explicitLacValue;
+            synchronized (this) {
+                if (explicitLac == null) {
+                    explicitLac = ByteBuffer.allocate(lac.capacity());
+                }
+                lac.readBytes(explicitLac);
+                explicitLac.rewind();
+
+                // skip the ledger id
+                explicitLac.getLong();
+                explicitLacValue = explicitLac.getLong();
+                explicitLac.rewind();
+            }
+            setLastAddConfirmed(explicitLacValue);
+        }
+
+        void notifyWatchers(long lastAddConfirmed) {
+            notifyWatchers(LastAddConfirmedUpdateNotification.FUNC, 
lastAddConfirmed);
+        }
+
+        @Override
+        public void close() {
+           synchronized (this) {
+               if (isClosed) {
+                   return;
+               }
+               isClosed = true;
+           }
+           // notify watchers
+            notifyWatchers(Long.MAX_VALUE);
 
 Review comment:
   we don't use the value notified as the source-of-truth. the value is used 
for trigger the logic when the lac is changed. we re-read lac and put that 
value in the response. we did this because LAC can potentially be updated 
between the watcher received notification and sending the response.  re-read 
lac would give the most recent value, so the client can have as many entries to 
read before hitting lac again.

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