Skip to content

Add ResourceHandler, FileUtils.map() returning ResourceHandler#51

Merged
drcrallen merged 7 commits intometamx:masterfrom
leventov:master
Sep 21, 2016
Merged

Add ResourceHandler, FileUtils.map() returning ResourceHandler#51
drcrallen merged 7 commits intometamx:masterfrom
leventov:master

Conversation

@leventov
Copy link
Copy Markdown
Contributor

And improve resource closing in FileSmoosher and SmooshedFileMapper

Better version of #50

…ByteBuffer> and improve resource closing in FileSmoosher and SmooshedFileMapper
* Closes the wrapped resource.
*/
@Override
void close();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AutoCloseable.close() allows Exception, is there a need to erase that signature here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This interface is copied from Druid: io.druid.collections.ResourceHandler. It erases exception, apparently because no present application requires throwing exception, but having it in signature it more noisy on usage side

@drcrallen
Copy link
Copy Markdown
Contributor

This might collide a bit with io.druid.collections.ResourceHolder, if you have any thoughts on how to keep too many "resource holding interfaces" from polluting the namespace please let them be known.

@drcrallen
Copy link
Copy Markdown
Contributor

@leventov please add tests to show the functionality added here is accomplishing what it is intended to accomplish.

@drcrallen
Copy link
Copy Markdown
Contributor

Overall this is a very reasonable, appropriate, and well executed patch. I would like to see some way to limit the quantity of resource handling interfaces.

One concern is that io.druid.collections.ResourceHolder extends Closeable whereas the proposed addition here extends the more restrictive Autocloseable, which means if io.druid.collections.ResourceHolder were to extend the proposed interface, it would also need to extend Closeable if we were to ensure zero interface changes. We can always change the interface though, so that is by no means a strict requirement.

@leventov
Copy link
Copy Markdown
Contributor Author

It intentionally shadows Druid's ResourceHandler, because 1) ResourceHandler finds it's usage in java-utils source itself 2) Best place for map() is FileUtils, there is no similar class or even package in Druid codebase.

Druid's ResourceHandler is used only internally, so I don't see a big problem if they co-exist with java-utils, or Druid's could be deprecated, or removed.

Another way is adding io.druid.collections.ResourceHandler to java-util directly but it would be strange and module technologies like OSGi or Java 9 modules are not sympathetic to things like this (when multiple modules/project share packages).

@leventov
Copy link
Copy Markdown
Contributor Author

leventov commented Sep 12, 2016

@leventov please add tests to show the functionality added here is accomplishing what it is intended to accomplish.

The direct reason for this is still ability to run some druid's benchmarks in windows. So it's impossible to write a sensible test for this to run in Linux.

Also some Druid's Files.map()-using code could be simplified, apache/druid#3422 (comment)

@leventov
Copy link
Copy Markdown
Contributor Author

leventov commented Sep 12, 2016

One concern is that io.druid.collections.ResourceHolder extends Closeable whereas the proposed addition here extends the more restrictive Autocloseable, which means if io.druid.collections.ResourceHolder were to extend the proposed interface, it would also need to extend Closeable if we were to ensure zero interface changes. We can always change the interface though, so that is by no means a strict requirement.

I didn't mean io.druid.collections.ResourceHolder to extend the added ResourceHandler, I see no point but saving literally a couple of lines of code. io.druid.collections.ResourceHolder could be removed if used only internally, or if there is a suspection that some extensions use this interface, we can left it in the codebase until the next major Druid release, as is.

@leventov
Copy link
Copy Markdown
Contributor Author

Or, the added ResourceHolder could also be made to extend Closeable, it's ok.

@drcrallen
Copy link
Copy Markdown
Contributor

@leventov it's not about saving code, its about saving developer sanity. If someone sees there are two interfaces in place that do essentially the same thing, and have no logical reason they should be different, then that muddles the waters more than it should.

I propose having ResourceHandler from java-util be the interface used in druid instead of ResourceHolder.

@leventov
Copy link
Copy Markdown
Contributor Author

leventov commented Sep 13, 2016

@gianm could you please review and/or express your opinion as Druid commiter on replacing io.druid.collections.ResourceHandler with com.metamx.common.ResourceHandler?

@drcrallen
Copy link
Copy Markdown
Contributor

As per https://groups.google.com/d/msg/druid-development/SQK4FYxzeXw/i2jx_atBAAAJ looks like the interface is staying here

@leventov
Copy link
Copy Markdown
Contributor Author

@drcrallen as pulling generic ResourceHandler into java-util aimed to replace Druid's ResourceHandler, what is not going to happen, I can specialize it, i. e. have just MappedByteBufferHandler in java-util. Makes sense?

}
}
}
Throwables.propagateIfPossible(thrown);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This changes behavior as it will now completely swallow IOException instead of propagating it, or even logging it.

Is there a reason the exception should be swallowed now?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

IOException was not possible here, so I just removed unneeded throws IOException. Only RuntimeException or Error are possible. So Throwables.propagateIfPossible(thrown); is just a way to rethrow, without javac complaining.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Gotcha

* Facilitates using try-with-resources with resources that don't implement {@link AutoCloseable}, e. g.
* {@link java.nio.ByteBuffer}s.
*
* <p>This interface replaces {@code io.druid.collections.ResourceHandler}.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

don't forget to remove, you can say its modeled after, but not replaces as per discussion.

@leventov
Copy link
Copy Markdown
Contributor Author

@cheddar @drcrallen could you please express your opinion on the current state of the PR using the new Github's review system?

Copy link
Copy Markdown
Contributor

@drcrallen drcrallen left a comment

Choose a reason for hiding this comment

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

Code looks good. needs unit tests for new methods and for showing improved behavior in existing methods

@leventov leventov changed the title Add ResourceHandler, FileUtils.map() returning ResourceHandler Add ResourceHandler, FileUtils.map() returning ResourceHandler [WIP] Sep 20, 2016
public void testSanity() throws Exception
{
File baseDir = Files.createTempDir();
baseDir.deleteOnExit();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you use the TemporaryFolder @Rule for junit for this?

@leventov leventov changed the title Add ResourceHandler, FileUtils.map() returning ResourceHandler [WIP] Add ResourceHandler, FileUtils.map() returning ResourceHandler Sep 20, 2016
@leventov
Copy link
Copy Markdown
Contributor Author

@drcrallen could you please review latest changes?

File[] files = baseDir.listFiles();
Assert.assertEquals(1, files.length);
Assert.assertEquals(0, files[0].length());
FileSmoosher smoosher = new FileSmoosher(baseDir, 21);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this one be handled in a try-with-resources?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed

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.

2 participants