Skip to content

Commit

Permalink
Heap memory leak problem when ledger replication failed (#2794)
Browse files Browse the repository at this point in the history
### Motivation

production environment, memory leak always happened, and there were ledger cannot be replicated successfully.



This cause by when `openLedgerNoRecovery` with `BKNotEnoughBookiesException`,  the LedgerHandler won't  closed properly, caused memory leak

https://github.com/apache/bookkeeper/blob/c7236adc3cb659e65ae5ce53b7156569d7f50ebd/bookkeeper-server/src/main/java/org/apache/bookkeeper/replication/ReplicationWorker.java#L364-L424

### Changes

close LedgerHandler when openComplete with exception
  • Loading branch information
gaozhangmin authored Oct 15, 2021
1 parent a522fa3 commit 4dc4260
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ public void initiateWithoutRecovery() {
initiate();
}

private void closeLedgerHandle() {
try {
if (lh != null) {
lh.close();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOG.info("InterruptedException while closing ledger {}", ledgerId, e);
} catch (BKException e) {
LOG.warn("BKException while closing ledger {} ", ledgerId, e);
}
}

private void openWithMetadata(Versioned<LedgerMetadata> versionedMetadata) {
LedgerMetadata metadata = versionedMetadata.getValue();

Expand Down Expand Up @@ -196,8 +209,10 @@ public void safeOperationComplete(int rc, Void result) {
if (rc == BKException.Code.OK) {
openComplete(BKException.Code.OK, lh);
} else if (rc == BKException.Code.UnauthorizedAccessException) {
closeLedgerHandle();
openComplete(BKException.Code.UnauthorizedAccessException, null);
} else {
closeLedgerHandle();
openComplete(bk.getReturnRc(BKException.Code.LedgerRecoveryException), null);
}
}
Expand All @@ -212,6 +227,7 @@ public String toString() {
public void readLastConfirmedComplete(int rc,
long lastConfirmed, Object ctx) {
if (rc != BKException.Code.OK) {
closeLedgerHandle();
openComplete(bk.getReturnRc(BKException.Code.ReadException), null);
} else {
lh.lastAddConfirmed = lh.lastAddPushed = lastConfirmed;
Expand Down

0 comments on commit 4dc4260

Please sign in to comment.