-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Fix races in LookupSnapshotTaker, CoordinatorPollingBasicAuthenticatorCacheManager #5344
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
Merged
Merged
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
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 |
|---|---|---|
|
|
@@ -27,11 +27,16 @@ | |
|
|
||
| import java.io.File; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.FileOutputStream; | ||
| import java.io.FilterOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.nio.MappedByteBuffer; | ||
| import java.nio.channels.FileChannel; | ||
| import java.nio.file.StandardCopyOption; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.UUID; | ||
|
|
||
| public class FileUtils | ||
| { | ||
|
|
@@ -46,6 +51,7 @@ public boolean apply(Throwable input) | |
| return input instanceof Exception; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Copy input byte source to outFile. If outFile exists, it is attempted to be deleted. | ||
| * | ||
|
|
@@ -150,15 +156,76 @@ public void addFile(File file) | |
| * }}</pre> | ||
| * | ||
| * @param file the file to map | ||
| * | ||
| * @return a {@link MappedByteBufferHandler}, wrapping a read-only buffer reflecting {@code file} | ||
| * @throws FileNotFoundException if the {@code file} does not exist | ||
| * @throws IOException if an I/O error occurs | ||
| * | ||
| * @throws FileNotFoundException if the {@code file} does not exist | ||
| * @throws IOException if an I/O error occurs | ||
| * @see FileChannel#map(FileChannel.MapMode, long, long) | ||
| */ | ||
| public static MappedByteBufferHandler map(File file) throws IOException | ||
| { | ||
| MappedByteBuffer mappedByteBuffer = Files.map(file); | ||
| return new MappedByteBufferHandler(mappedByteBuffer); | ||
| } | ||
|
|
||
| /** | ||
| * Write to a file atomically, by first writing to a temporary file in the same directory and then moving it to | ||
| * the target location. This function attempts to clean up its temporary files when possible, but they may stick | ||
| * around (for example, if the JVM crashes partway through executing the function). In any case, the target file | ||
| * should be unharmed. | ||
| * | ||
| * The OutputStream passed to the consumer is uncloseable; calling close on it will do nothing. This is to ensure | ||
| * that the stream stays open so we can fsync it here before closing. Hopefully, this doesn't cause any problems | ||
| * for callers. | ||
| * | ||
| * This method is not just thread-safe, but is also safe to use from multiple processes on the same machine. | ||
| */ | ||
| public static void writeAtomically(final File file, OutputStreamConsumer f) throws IOException | ||
| { | ||
| writeAtomically(file, file.getParentFile(), f); | ||
| } | ||
|
|
||
| private static void writeAtomically(final File file, final File tmpDir, OutputStreamConsumer f) throws IOException | ||
| { | ||
| final File tmpFile = new File(tmpDir, StringUtils.format(".%s.%s", file.getName(), UUID.randomUUID())); | ||
|
|
||
| try { | ||
| try (final FileOutputStream out = new FileOutputStream(tmpFile)) { | ||
| // Pass f an uncloseable stream so we can fsync before closing. | ||
| f.accept(uncloseable(out)); | ||
|
|
||
| // fsync to avoid write-then-rename-then-crash causing empty files on some filesystems. | ||
| out.getChannel().force(true); | ||
| } | ||
|
|
||
| // No exception thrown; do the move. | ||
| java.nio.file.Files.move( | ||
| tmpFile.toPath(), | ||
| file.toPath(), | ||
| StandardCopyOption.ATOMIC_MOVE, | ||
| StandardCopyOption.REPLACE_EXISTING | ||
| ); | ||
| } | ||
| finally { | ||
| tmpFile.delete(); | ||
| } | ||
| } | ||
|
|
||
| private static OutputStream uncloseable(final OutputStream out) throws IOException | ||
| { | ||
| return new FilterOutputStream(out) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In unit tests locally with 15mb files, using a BufferedOutputStream here, takes 1 second vs. ~16 seconds. FilterOutputStream isn't buffered, is too slow for practical use? |
||
| { | ||
| @Override | ||
| public void close() throws IOException | ||
| { | ||
| // Do nothing. | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| public interface OutputStreamConsumer | ||
| { | ||
| void accept(OutputStream outputStream) throws IOException; | ||
| } | ||
| } | ||
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
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
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
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.
I wonder if there is no such utility already in Apache Commons IO, Guava, or JDK itself?
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.
I looked (briefly) but did not find one.