BewareMyPower commented on issue #12616:
URL: https://github.com/apache/pulsar/issues/12616#issuecomment-966144710


   @Jason918
   
   `internalTrimLedgers` should not update the `lastConfirmedEntry`, which 
represents an entry that has been written to BK successfully. After a rollover, 
the `lastConfirmedEntry` could be invalid if the ledger was removed. However, 
there is no proper new value for it. So we should just keep it unchanged.
   
   See `ManagedLedgerImpl#getFirstPosition`, which handles the corner case.
   
   ```java
       public PositionImpl getFirstPosition() {
           Long ledgerId = ledgers.firstKey();
           if (ledgerId == null) {
               return null;
           }
           
           if (ledgerId > lastConfirmedEntry.getLedgerId()) { // 1. It means 
the `lastConfirmedEntry` is invalid
               // 2. In this case, the first ledger must be empty because once 
a new entry was added, the `lastConfirmedEntry` should be updated.
               checkState(ledgers.get(ledgerId).getEntries() == 0);
               ledgerId = lastConfirmedEntry.getLedgerId();
           }
           return new PositionImpl(ledgerId, -1);
       }
   ```
   
   However, the `isValidPosition` check doesn't consider the case when 
`lastConfirmedEntry` is invalid.
   
   ```java
       public boolean isValidPosition(PositionImpl position) {
           PositionImpl last = lastConfirmedEntry;
           if (position.getEntryId() < 0) {
               return false;
           } else if (position.getLedgerId() > last.getLedgerId()) {
               return false;
           } else if (position.getLedgerId() == last.getLedgerId()) {
               // `last` could be an invalid position, so even position's 
ledger id is the same, it could still be invalid
               return position.getEntryId() <= (last.getEntryId() + 1);
           } else {
   ```
   
   Then it affects the `getNextValidPosition` method that is called in KoP to 
get the first valid position. You can see 
https://github.com/streamnative/kop/pull/894 for more details about how this 
corner case is handled in KoP.
   
   But the current `isValidPosition` semantics also affects other methods like 
`asyncOffloadPrefix` and `asyncResetCursor`. So we must take it more carefully 
to determine whether the semantics should be changed.


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to