-
Notifications
You must be signed in to change notification settings - Fork 143
TrackingFileManager changes #1692
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 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fa570cc
TrackingFileManager changes
cstamas bed34fd
Merge remote-tracking branch 'upstream/master' into tracking-file-man…
cstamas e6fbd3c
Extend tracking file mgr
cstamas 945f52d
Return to ol' school
cstamas c32602d
Return this check
cstamas b6167a2
Simplify
cstamas 469d884
Add UT
cstamas 0ae44d7
Simplify locking, extend UT for JIMFS
cstamas 537b8a4
Use proper method
cstamas a41669d
PR comment
cstamas 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 |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| import java.nio.channels.FileLock; | ||
| import java.nio.channels.OverlappingFileLockException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.NoSuchFileException; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.StandardOpenOption; | ||
| import java.util.Map; | ||
|
|
@@ -61,15 +62,14 @@ public Properties read(File file) { | |
| @Override | ||
| public Properties read(Path path) { | ||
| if (Files.isReadable(path)) { | ||
| synchronized (getMutex(path)) { | ||
| try { | ||
| long fileSize = Files.size(path); | ||
| try (FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.READ); | ||
| FileLock unused = fileLock(fileChannel, Math.max(1, fileSize), true)) { | ||
| Properties props = new Properties(); | ||
| props.load(Channels.newInputStream(fileChannel)); | ||
| return props; | ||
| } | ||
| synchronized (mutex(path)) { | ||
| try (FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.READ); | ||
| FileLock unused = fileLock(fileChannel, true)) { | ||
| Properties props = new Properties(); | ||
| props.load(Channels.newInputStream(fileChannel)); | ||
| return props; | ||
| } catch (NoSuchFileException e) { | ||
| LOGGER.debug("No such file to read {}: {}", path, e.getMessage()); | ||
| } catch (IOException e) { | ||
| LOGGER.warn("Failed to read tracking file '{}'", path, e); | ||
| throw new UncheckedIOException(e); | ||
|
|
@@ -87,72 +87,79 @@ public Properties update(File file, Map<String, String> updates) { | |
|
|
||
| @Override | ||
| public Properties update(Path path, Map<String, String> updates) { | ||
| Properties props = new Properties(); | ||
| try { | ||
| Files.createDirectories(path.getParent()); | ||
| } catch (IOException e) { | ||
| LOGGER.warn("Failed to create tracking file parent '{}'", path, e); | ||
| throw new UncheckedIOException(e); | ||
| } | ||
|
|
||
| synchronized (getMutex(path)) { | ||
| try { | ||
| long fileSize; | ||
| try { | ||
| fileSize = Files.size(path); | ||
| } catch (IOException e) { | ||
| fileSize = 0L; | ||
| Properties props = new Properties(); | ||
| synchronized (mutex(path)) { | ||
| try (FileChannel fileChannel = FileChannel.open( | ||
| path, StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE); | ||
| FileLock unused = fileLock(fileChannel, false)) { | ||
| if (fileChannel.size() > 0) { | ||
|
Contributor
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. why is there a check here and not while reading ?
Member
Author
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. Reading will be unable to open absent file, here we may create it (and will be 0 byte) |
||
| props.load(Channels.newInputStream(fileChannel)); | ||
| } | ||
| try (FileChannel fileChannel = FileChannel.open( | ||
| path, StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE); | ||
| FileLock unused = fileLock(fileChannel, Math.max(1, fileSize), false)) { | ||
| if (fileSize > 0) { | ||
| props.load(Channels.newInputStream(fileChannel)); | ||
| } | ||
|
|
||
| for (Map.Entry<String, String> update : updates.entrySet()) { | ||
| if (update.getValue() == null) { | ||
| props.remove(update.getKey()); | ||
| } else { | ||
| props.setProperty(update.getKey(), update.getValue()); | ||
| } | ||
| for (Map.Entry<String, String> update : updates.entrySet()) { | ||
| if (update.getValue() == null) { | ||
| props.remove(update.getKey()); | ||
| } else { | ||
| props.setProperty(update.getKey(), update.getValue()); | ||
| } | ||
|
|
||
| LOGGER.debug("Writing tracking file '{}'", path); | ||
| ByteArrayOutputStream stream = new ByteArrayOutputStream(1024 * 2); | ||
| props.store( | ||
| stream, | ||
| "NOTE: This is a Maven Resolver internal implementation file" | ||
| + ", its format can be changed without prior notice."); | ||
| fileChannel.position(0); | ||
| int written = fileChannel.write(ByteBuffer.wrap(stream.toByteArray())); | ||
| fileChannel.truncate(written); | ||
| } | ||
|
|
||
| LOGGER.debug("Writing tracking file '{}'", path); | ||
| ByteArrayOutputStream stream = new ByteArrayOutputStream(1024 * 2); | ||
| props.store( | ||
| stream, | ||
| "NOTE: This is a Maven Resolver internal implementation file" | ||
| + ", its format can be changed without prior notice."); | ||
| fileChannel.position(0); | ||
| int written = fileChannel.write(ByteBuffer.wrap(stream.toByteArray())); | ||
| fileChannel.truncate(written); | ||
| } catch (IOException e) { | ||
| LOGGER.warn("Failed to write tracking file '{}'", path, e); | ||
| throw new UncheckedIOException(e); | ||
| } | ||
| } | ||
|
|
||
| return props; | ||
| } | ||
|
|
||
| private Object getMutex(Path path) { | ||
| // The interned string of path is (mis)used as mutex, to exclude different threads going for same file, | ||
| // as JVM file locking happens on JVM not on Thread level. This is how original code did it ¯\_(ツ)_/¯ | ||
| /* | ||
| * NOTE: Locks held by one JVM must not overlap and using the canonical path is our best bet, still another | ||
| * piece of code might have locked the same file (unlikely though) or the canonical path fails to capture file | ||
| * identity sufficiently as is the case with Java 1.6 and symlinks on Windows. | ||
| */ | ||
| @Override | ||
| public boolean delete(File file) { | ||
| return delete(file.toPath()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean delete(Path path) { | ||
| if (Files.isReadable(path)) { | ||
| synchronized (mutex(path)) { | ||
| try (FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.WRITE); | ||
| FileLock unused = fileLock(fileChannel, false)) { | ||
| Files.delete(path); | ||
| return true; | ||
| } catch (NoSuchFileException e) { | ||
| LOGGER.debug("No such file to delete {}: {}", path, e.getMessage()); | ||
| } catch (IOException e) { | ||
| LOGGER.warn("Failed to delete tracking file '{}'", path, e); | ||
| throw new UncheckedIOException(e); | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private Object mutex(Path path) { | ||
| return path.toAbsolutePath().normalize().toString().intern(); | ||
| } | ||
|
|
||
| private FileLock fileLock(FileChannel channel, long size, boolean shared) throws IOException { | ||
| private FileLock fileLock(FileChannel channel, boolean shared) throws IOException { | ||
| FileLock lock = null; | ||
| for (int attempts = 8; attempts >= 0; attempts--) { | ||
| try { | ||
| lock = channel.lock(0, size, shared); | ||
| lock = channel.lock(0, Long.MAX_VALUE, shared); | ||
| break; | ||
| } catch (OverlappingFileLockException e) { | ||
| if (attempts <= 0) { | ||
|
|
||
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
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.
The
propsvariable should be defined inside thetryblock, with thereturnstatement moved at the end of the same block.