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

moving BucketUtils.deleteOnExit -> IOUtils #5332

Merged
merged 4 commits into from Jan 16, 2019
Merged

Conversation

lbergelson
Copy link
Member

@droazen droazen self-requested a review October 19, 2018 18:35
@droazen droazen self-assigned this Oct 19, 2018
@codecov-io
Copy link

codecov-io commented Oct 19, 2018

Codecov Report

Merging #5332 into master will decrease coverage by 0.005%.
The diff coverage is 73.418%.

@@               Coverage Diff               @@
##              master     #5332       +/-   ##
===============================================
- Coverage     87.083%   87.078%   -0.005%     
- Complexity     31232     31480      +248     
===============================================
  Files           1915      1930       +15     
  Lines         144130    145044      +914     
  Branches       15901     16074      +173     
===============================================
+ Hits          125513    126302      +789     
- Misses         12834     12890       +56     
- Partials        5783      5852       +69
Impacted Files Coverage Δ Complexity Δ
...nstitute/hellbender/utils/gcs/BucketUtilsTest.java 59.406% <ø> (+3.103%) 12 <0> (-1) ⬇️
...ls/genomicsdb/GenomicsDBImportIntegrationTest.java 88.367% <0%> (ø) 80 <0> (ø) ⬇️
...oadinstitute/hellbender/utils/gcs/BucketUtils.java 78.231% <100%> (-1.647%) 39 <0> (-3)
...rg/broadinstitute/hellbender/utils/io/IOUtils.java 72.981% <100%> (+0.254%) 95 <4> (+2) ⬆️
...institute/hellbender/utils/io/IOUtilsUnitTest.java 79.205% <54.839%> (-2.526%) 46 <2> (+1)
...nder/utils/io/DeleteRecursivelyOnExitPathHook.java 75% <75%> (ø) 3 <3> (?)
...tute/hellbender/utils/codecs/table/TableCodec.java 77.778% <0%> (-4.365%) 14% <0%> (+3%)
...itute/hellbender/tools/walkers/mutect/Mutect2.java 78.846% <0%> (-3.376%) 25% <0%> (+1%)
...r/tools/walkers/mutect/Mutect2IntegrationTest.java 91.603% <0%> (-2.89%) 83% <0%> (+11%)
...ools/walkers/mutect/SubsettedLikelihoodMatrix.java 60.87% <0%> (-2.767%) 12% <0%> (+2%)
... and 53 more

*
* @param fileToDelete file or directory to be deleted at JVM shutdown.
*/
public static void deleteOnExit(final Path fileToDelete){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added time ago a method in htsjdk which does a similar think (IOUtils.deleteOnExit), which has a common shutdown hook to delete all at the same time (DeleteOnExitPathHook) mimicking the behavior of the java.io.File#deleteOnExit.

I would suggest to add a common hook to delete recursively, and change the name of this method. Like that, it would not create too many threads to delete on exit.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@magicDGS Yeah, yours is better than this. I guess I should mimic what you did.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've copied your class to do this now so it shouldn't create many threads.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also rename this method to deleteRecursivelyOnExit to be explicit - otherwise, the difference with the htsjdk version is not clear. Also, it would be nice to backport this to htsjdk at some point, and thus it will be easier to migrate afterwards.

* replacing existing deleteRecursivelyOnExit method with deleteOnExit
* closes #5319
Copy link
Collaborator

@droazen droazen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Back to @lbergelson with my comments

IOUtils.deleteOnExit(IOUtils.getPath(path + TabixUtils.STANDARD_INDEX_EXTENSION));
IOUtils.deleteOnExit(IOUtils.getPath(path + ".bai"));
IOUtils.deleteOnExit(IOUtils.getPath(path + ".md5"));
IOUtils.deleteOnExit(IOUtils.getPath(path.replaceAll(extension + "$", ".bai"))); //if path ends with extension, replace it with .bai
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add the fasta indices here too, now that we're in the business of writing fastas?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we change this to just create each file in it's own directory instead of trying to delete every possible index?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to punt on this one and do it in the follow up PR because it touches code in a few different places.

* @param rootPath is the file/directory to be deleted
*/
public static void deleteRecursively(final Path rootPath) throws IOException {
final List<Path> pathsToDelete = Files.walk(rootPath)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is Files.walk() guaranteed to succeed on a non-directory? I feel like when @nalinigans implemented the original recursive deletion method, she might have had a legitimate reason for avoiding doing recursive deletion on non-directories.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the doc the result of calling it on a single file is the file itself.

The {@code Stream} returned is guaranteed to have at least one
* element, the starting file itself.

/**
* Class to hold a set of {@link Path} to be delete on the JVM exit through a shutdown hook.
*
* <p>This class is a modification of {@link htsjdk.samtools.util.nio}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Complete this @link

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woops, done

*
* <p>This class is a modification of {@link htsjdk.samtools.util.nio}
*/
class DeleteRecursivelyOnExitPathHook {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document the reason why this class is package-protected.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

/**
* Adds a {@link Path} for deletion on JVM exit.
*
* @param path path to be deleted.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a note here that path can be a (non-empty) directory.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call, done

@@ -695,15 +694,15 @@ public static Path createTempPath(String name, String extension) {
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method seems redundant with BucketUtils.getTempFilePath(), which you've preserved.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also should probably be unified with IOUtils.createTempFileInDirectory()/IOUtils.createTempFile() above, since those are the methods called by the widely-used BaseTest.createTempFile()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored a ton of stuff in the next PR. Let's revisit this there instead. I may have done that already.

@@ -987,4 +969,27 @@ public static String getGenomicsDBPath(final String path) {
}
return genomicsdbPath;
}

/**
* Schedule a file to be deleted on JVM shutdown.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file -> file or directory

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deleted -> deleted recursively

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And add explicit note that this calls IOUtils.deleteRecursively() on fileToDelete at shutdown

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

File tempFile = createTempFile(String.format("testWriteThenReadFileIntoByteArray_%d_%d", fileSize, readBufferSize), "tmp");

byte[] dataWritten = getDeterministicRandomData(fileSize);
IOUtils.writeByteArrayToFile(dataWritten, tempFile);
byte[] dataRead = IOUtils.readFileIntoByteArray(tempFile, readBufferSize);

Assert.assertEquals(dataRead.length, dataWritten.length);
Assert.assertTrue(Arrays.equals(dataRead, dataWritten));
Assert.assertEquals(dataWritten, dataRead);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Order of args seems wrong here -- dataRead is the "actual" value here, isn't it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, good catch

@@ -329,7 +330,7 @@ public void testWriteThenReadStreamIntoByteArray ( int fileSize, int readBufferS
byte[] dataRead = IOUtils.readStreamIntoByteArray(new FileInputStream(tempFile), readBufferSize);

Assert.assertEquals(dataRead.length, dataWritten.length);
Assert.assertTrue(Arrays.equals(dataRead, dataWritten));
Assert.assertEquals(dataWritten, dataRead);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, actual and expected values might be swapped here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


final File dir = new File(GATKBaseTest.publicTestDir + "I_SHOULD_HAVE_BEEN_DELETED");
IOUtils.deleteRecursivelyOnExit(dir);
final File dir = new File( "I_SHOULD_HAVE_BEEN_DELETED");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really want to create this file in the working directory?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's more visible that way.... but I can move it back to where it was...

@droazen droazen assigned lbergelson and unassigned droazen Jan 3, 2019
@lbergelson
Copy link
Member Author

@droazen Could you take a second pass on this when you get a chance?

Copy link
Collaborator

@droazen droazen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Latest version looks good @lbergelson. Still a bunch of refactoring to be done in this part of the codebase, but it can wait until the next PR. Merging!

@droazen droazen merged commit 83c0b8c into master Jan 16, 2019
@droazen droazen deleted the lb_merge_delete_on_exits branch January 16, 2019 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unify BucketUtils.deleteRecursively()/deleteOnExit() with IOUtils.deleteRecursivelyOnExit()
4 participants