Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(issues1302): block bytebuf leak #1303

Merged
merged 2 commits into from
May 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ CompletableFuture<ReadDataBlock> read(long startOffset, long endOffset, int maxB
Throwable cause = FutureUtil.cause(ex);
if (cause != null) {
readContext.records.forEach(StreamRecordBatch::release);
if (leftRetries > 0 && (cause instanceof ObjectNotExistException || cause instanceof NoSuchKeyException || cause instanceof BlockNotContinuousException)) {
for (Block block : readContext.blocks) {
block.release();
}
if (leftRetries > 0 && isRecoverable(cause)) {
// The cached blocks maybe invalid after object compaction, so we need to reset the blocks and retry read
resetBlocks();
// use async to prevent recursive call cause stack overflow
eventLoop.execute(() -> FutureUtil.propagate(read(startOffset, endOffset, maxBytes, leftRetries - 1), retCf));
} else {
for (Block block : readContext.blocks) {
block.release();
}
retCf.completeExceptionally(cause);
}
} else {
Expand Down Expand Up @@ -163,6 +163,7 @@ void read0(ReadContext ctx, final long startOffset, final long endOffset, final

// 3. extract records from blocks
loadBlocksCf.thenAccept(nil -> {
ctx.blocks.addAll(blocks);
Optional<Block> failedBlock = blocks.stream().filter(block -> block.exception != null).findAny();
if (failedBlock.isPresent()) {
ctx.cf.completeExceptionally(failedBlock.get().exception);
Expand All @@ -173,7 +174,6 @@ void read0(ReadContext ctx, final long startOffset, final long endOffset, final
streamId, startOffset, endOffset, maxBytes)));
return;
}
ctx.blocks.addAll(blocks);
int remainingSize = maxBytes;
long nextStartOffset = startOffset;
long nextEndOffset;
Expand Down Expand Up @@ -440,6 +440,10 @@ private boolean putBlock(Block block) {
return true;
}

private static boolean isRecoverable(Throwable cause) {
return cause instanceof ObjectNotExistException || cause instanceof NoSuchKeyException || cause instanceof BlockNotContinuousException;
}

static class GetBlocksContext {
final List<Block> blocks = new ArrayList<>();
final CompletableFuture<List<Block>> cf = new CompletableFuture<>();
Expand Down Expand Up @@ -496,7 +500,6 @@ public Block newBlockWithData(boolean readahead) {

public void release() {
if (released) {
LOGGER.error("[BUG] duplicated release", new IllegalStateException());
return;
}
released = true;
Expand Down Expand Up @@ -557,7 +560,8 @@ public void tryReadahead(boolean cacheMiss) {
// For get block indexes and load data block are sync success,
// the whenComplete will invoke first before assign CompletableFuture to inflightReadaheadCf
inflightReadaheadCf.whenComplete((nil, ex) -> {
if (ex != null) {
Throwable cause = FutureUtil.cause(ex);
if (!isRecoverable(cause)) {
LOGGER.error("Readahead failed", ex);
}
inflightReadaheadCf = null;
Expand Down
Loading