-
Notifications
You must be signed in to change notification settings - Fork 44
Add ResourceHandler, FileUtils.map() returning ResourceHandler #51
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
Changes from all commits
752d741
f6b2d4a
f1315de
9f18dac
c9b2461
c5c30ed
3e06ed5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Copyright 2016 Metamarkets Group Inc. | ||
| * | ||
| * Licensed 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 com.metamx.common; | ||
|
|
||
| import java.io.File; | ||
| import java.nio.MappedByteBuffer; | ||
|
|
||
| /** | ||
| * Facilitates using try-with-resources with {@link MappedByteBuffer}s which don't implement {@link AutoCloseable}. | ||
| * | ||
| * <p>This interface is a specialization of {@code io.druid.collections.ResourceHandler}. | ||
| * @see FileUtils#map(File) | ||
| */ | ||
| public final class MappedByteBufferHandler implements AutoCloseable | ||
| { | ||
| private final MappedByteBuffer mappedByteBuffer; | ||
|
|
||
| MappedByteBufferHandler(MappedByteBuffer mappedByteBuffer) | ||
| { | ||
| this.mappedByteBuffer = mappedByteBuffer; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the wrapped buffer. | ||
| */ | ||
| public MappedByteBuffer get() | ||
| { | ||
| return mappedByteBuffer; | ||
| } | ||
|
|
||
| /** | ||
| * Unmaps the wrapped buffer. | ||
| */ | ||
| @Override | ||
| public void close() | ||
| { | ||
| ByteBufferUtils.unmap(mappedByteBuffer); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| package com.metamx.common.io.smoosh; | ||
|
|
||
| import com.google.common.base.Charsets; | ||
| import com.google.common.base.Throwables; | ||
| import com.google.common.collect.Lists; | ||
| import com.google.common.collect.Maps; | ||
| import com.google.common.io.Closeables; | ||
|
|
@@ -131,10 +132,20 @@ public ByteBuffer mapFile(String name) throws IOException | |
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException | ||
| public void close() | ||
| { | ||
| Throwable thrown = null; | ||
| for (MappedByteBuffer mappedByteBuffer : buffersList) { | ||
| ByteBufferUtils.unmap(mappedByteBuffer); | ||
| try { | ||
| ByteBufferUtils.unmap(mappedByteBuffer); | ||
| } catch (Throwable t) { | ||
| if (thrown == null) { | ||
| thrown = t; | ||
| } else { | ||
| thrown.addSuppressed(t); | ||
| } | ||
| } | ||
| } | ||
| Throwables.propagateIfPossible(thrown); | ||
|
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. This changes behavior as it will now completely swallow Is there a reason the exception should be swallowed now?
Contributor
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.
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. Gotcha |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Copyright 2016 Metamarkets Group Inc. | ||
| * | ||
| * Licensed 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 com.metamx.common; | ||
|
|
||
| import java.lang.management.BufferPoolMXBean; | ||
| import java.lang.management.ManagementFactory; | ||
| import java.util.List; | ||
|
|
||
| public final class BufferUtils | ||
| { | ||
|
|
||
| public static long totalMemoryUsedByDirectAndMappedBuffers() | ||
| { | ||
| long totalMemoryUsed = 0L; | ||
| List<BufferPoolMXBean> pools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class); | ||
| for (BufferPoolMXBean pool : pools) { | ||
| totalMemoryUsed += pool.getMemoryUsed(); | ||
| } | ||
| return totalMemoryUsed; | ||
| } | ||
|
|
||
| private BufferUtils() {} | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * Copyright 2016 Metamarkets Group Inc. | ||
| * | ||
| * Licensed 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 com.metamx.common; | ||
|
|
||
| import org.junit.Assert; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.TemporaryFolder; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.RandomAccessFile; | ||
|
|
||
| public class FileUtilsTest | ||
| { | ||
| @Rule | ||
| public TemporaryFolder folder = new TemporaryFolder(); | ||
|
|
||
| @Test | ||
| public void testMap() throws IOException | ||
| { | ||
| File dataFile = folder.newFile("data"); | ||
| long buffersMemoryBefore = BufferUtils.totalMemoryUsedByDirectAndMappedBuffers(); | ||
| try (RandomAccessFile raf = new RandomAccessFile(dataFile, "rw")) { | ||
| raf.write(42); | ||
| raf.setLength(1 << 20); // 1 MB | ||
| } | ||
| try (MappedByteBufferHandler mappedByteBufferHandler = FileUtils.map(dataFile)) { | ||
| Assert.assertEquals(42, mappedByteBufferHandler.get().get(0)); | ||
| } | ||
| long buffersMemoryAfter = BufferUtils.totalMemoryUsedByDirectAndMappedBuffers(); | ||
| Assert.assertEquals(buffersMemoryBefore, buffersMemoryAfter); | ||
| } | ||
| } |
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.
FYI, there are a few
Throwables that, in general, should probably not allow continuation in the running process.http://www.scala-lang.org/api/2.10.6/index.html#scala.util.control.NonFatal$ has some good examples of things which should probably not continue.
Digging through the call stack for
unmapthough, it doesn't look like it throws anything that should be caught.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.
In this particular case I think the behavior is OK due to the very limited nature of what is accomplished, or intended, by the unmap.