Skip to content

Commit

Permalink
fix getPreviousPosition npe (#11621)
Browse files Browse the repository at this point in the history
Fixes #11619

### Motivation
There's a chance that headMap might be empty even if a empty check added. because remove would be executed in ledgers. after the empty check between line 3214 and line 3219 in following code snippet:

headMap is a view of ledgers

pulsar/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java

Lines 3206 to 3222 in 31231d6
```
 NavigableMap<Long, LedgerInfo> headMap = ledgers.headMap(position.getLedgerId(), false); 
  
 if (headMap.isEmpty()) { 
     // There is no previous ledger, return an invalid position in the current ledger 
     return PositionImpl.get(position.getLedgerId(), -1); 
 } 
  
 // We need to find the most recent non-empty ledger 
 for (long ledgerId : headMap.descendingKeySet()) { 
     LedgerInfo li = headMap.get(ledgerId); 
     if (li.getEntries() > 0) { 
         return PositionImpl.get(li.getLedgerId(), li.getEntries() - 1); 
     } 
 } 
  
 // in case there are only empty ledgers, we return a position in the first one 
 return PositionImpl.get(headMap.firstEntry().getKey(), -1); 
```

### Modifications
Add a null checker of headMap.firstEntry

Verifying this change
Make sure that the change passes the CI checks.
This change is a trivial rework / code cleanup without any test coverage.

Co-authored-by: gavingaozhangmin <gavingaozhangmin@didiglobal.com>
  • Loading branch information
gaozhangmin and gavingaozhangmin committed Aug 11, 2021
1 parent 33436f6 commit 7f7eb9d
Showing 1 changed file with 5 additions and 3 deletions.
Expand Up @@ -48,6 +48,7 @@
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
Expand Down Expand Up @@ -3205,21 +3206,22 @@ public PositionImpl getPreviousPosition(PositionImpl position) {
// The previous position will be the last position of an earlier ledgers
NavigableMap<Long, LedgerInfo> headMap = ledgers.headMap(position.getLedgerId(), false);

if (headMap.isEmpty()) {
final Map.Entry<Long, LedgerInfo> firstEntry = headMap.firstEntry();
if (firstEntry == null) {
// There is no previous ledger, return an invalid position in the current ledger
return PositionImpl.get(position.getLedgerId(), -1);
}

// We need to find the most recent non-empty ledger
for (long ledgerId : headMap.descendingKeySet()) {
LedgerInfo li = headMap.get(ledgerId);
if (li.getEntries() > 0) {
if (li != null && li.getEntries() > 0) {
return PositionImpl.get(li.getLedgerId(), li.getEntries() - 1);
}
}

// in case there are only empty ledgers, we return a position in the first one
return PositionImpl.get(headMap.firstEntry().getKey(), -1);
return PositionImpl.get(firstEntry.getKey(), -1);
}

/**
Expand Down

0 comments on commit 7f7eb9d

Please sign in to comment.