-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[common] Release the BTree index file on every path that abandons the writer #9474
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
Open
PDGGK
wants to merge
2
commits into
apache:master
Choose a base branch
from
PDGGK:fix-btree-writer-leak
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+190
−5
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
...n-common/src/test/java/org/apache/paimon/globalindex/btree/BTreeIndexWriterCloseTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.paimon.globalindex.btree; | ||
|
|
||
| import org.apache.paimon.compression.BlockCompressionFactory; | ||
| import org.apache.paimon.fs.PositionOutputStream; | ||
| import org.apache.paimon.globalindex.KeySerializer; | ||
| import org.apache.paimon.globalindex.io.GlobalIndexFileWriter; | ||
| import org.apache.paimon.types.IntType; | ||
| import org.apache.paimon.utils.IOUtils; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| /** | ||
| * Tests that {@link BTreeIndexWriter} releases the file it opened, on every path that abandons it. | ||
| */ | ||
| public class BTreeIndexWriterCloseTest { | ||
|
|
||
| /** | ||
| * finish() writes the null bitmap, the bloom filter, the index block and the footer before | ||
| * closing. A write that fails part way through - a full disk is the ordinary cause - must not | ||
| * leave the file open. | ||
| */ | ||
| @Test | ||
| public void testFinishReleasesTheFileWhenWritingFails() throws IOException { | ||
| AtomicInteger closed = new AtomicInteger(); | ||
| BTreeIndexWriter writer = | ||
| new BTreeIndexWriter( | ||
| failingWriter(closed), | ||
| KeySerializer.create(new IntType()), | ||
| 1024, | ||
| (BlockCompressionFactory) null); | ||
|
|
||
| for (int i = 0; i < 200; i++) { | ||
| writer.write(i, (long) i); | ||
| } | ||
|
|
||
| assertThatThrownBy(writer::finish) | ||
| .isInstanceOf(RuntimeException.class) | ||
| .hasMessageContaining("Error in closing BTree index writer"); | ||
|
|
||
| assertThat(closed).hasValue(1); | ||
| } | ||
|
|
||
| /** | ||
| * A build can be abandoned before finish() ever runs - the row source fails, or a Flink or | ||
| * Spark index build task is cancelled. The owner cleanup paths, PkSortedIndexFile#build and | ||
| * SortedSingleColumnIndexWriter#close, release the writer only if they can see it as an | ||
| * AutoCloseable, and skip it silently otherwise. This is that exact idiom. | ||
| */ | ||
| @Test | ||
| public void testTheOwnerCleanupPathReleasesAnAbandonedWriter() throws IOException { | ||
| AtomicInteger closed = new AtomicInteger(); | ||
| BTreeIndexWriter writer = | ||
| new BTreeIndexWriter( | ||
| failingWriter(closed), | ||
| KeySerializer.create(new IntType()), | ||
| 1024, | ||
| (BlockCompressionFactory) null); | ||
| writer.write(1, 1L); | ||
|
|
||
| if (writer instanceof AutoCloseable) { | ||
| IOUtils.closeQuietly((AutoCloseable) writer); | ||
| } | ||
|
|
||
| assertThat(closed).hasValue(1); | ||
| } | ||
|
|
||
| /** | ||
| * SortedGlobalIndexWriter holds the task writer in a try-with-resources, so close() can follow | ||
| * a successful finish(). It must not close the stream a second time. | ||
| */ | ||
| @Test | ||
| public void testCloseIsIdempotent() throws IOException { | ||
| AtomicInteger closed = new AtomicInteger(); | ||
| BTreeIndexWriter writer = | ||
| new BTreeIndexWriter( | ||
| failingWriter(closed), | ||
| KeySerializer.create(new IntType()), | ||
| 1024, | ||
| (BlockCompressionFactory) null); | ||
| writer.write(1, 1L); | ||
|
|
||
| writer.close(); | ||
| writer.close(); | ||
|
|
||
| assertThat(closed).hasValue(1); | ||
| } | ||
|
|
||
| private static GlobalIndexFileWriter failingWriter(AtomicInteger closed) { | ||
| return new GlobalIndexFileWriter() { | ||
|
|
||
| @Override | ||
| public String newFileName(String prefix) { | ||
| return "test-btree" + prefix; | ||
| } | ||
|
|
||
| @Override | ||
| public PositionOutputStream newOutputStream(String fileName) { | ||
| return new PositionOutputStream() { | ||
|
|
||
| private long pos; | ||
| private final long capacity = 1500L; | ||
|
|
||
| @Override | ||
| public long getPos() { | ||
| return pos; | ||
| } | ||
|
|
||
| @Override | ||
| public void write(int b) throws IOException { | ||
| write(new byte[] {(byte) b}, 0, 1); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(byte[] b) throws IOException { | ||
| write(b, 0, b.length); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(byte[] b, int off, int len) throws IOException { | ||
| if (pos + len > capacity) { | ||
| throw new IOException("no space left on device"); | ||
| } | ||
| pos += len; | ||
| } | ||
|
|
||
| @Override | ||
| public void flush() {} | ||
|
|
||
| @Override | ||
| public void close() { | ||
| closed.incrementAndGet(); | ||
| } | ||
| }; | ||
| } | ||
| }; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2] Close the stream when constructor setup throws an Error
This constructor-cleanup guard still misses
Error. In particular,new SstFileWriter(...)allocates its data and index block buffers afterouthas been opened, so anOutOfMemoryErrorleaves the constructor without returning anAutoCloseableobject and strands the output stream. A task/runtime may catch that failure while keeping the JVM alive, just like the abandoned-build paths this PR addresses. Please catch and rethrowRuntimeException | Error(or use the usualcatch (Throwable)constructor-cleanup pattern) so every post-open setup failure closesout.