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_r171343446
##########
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:
> But a watcher could receive a positive long from the Watchable, and
conceivably consider it valid.
yes the only case for Long.MAX_VALUE is from watchers. Then the meaning
given for Long.MAX_VALUE is the watcher logic. At this case, Long.MAX_VALUE
would then be effectively same as using some negative values.
----------------------------------------------------------------
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