Skip to content
Permalink
Browse files
Merge pull request #130 from apache/AddExceptions
Cleanup exceptions
  • Loading branch information
leerho committed Jun 9, 2021
2 parents 7a4cfc0 + 9689486 commit 58e1efe666e9fe422a42b695c3aea23fedada9e9
Show file tree
Hide file tree
Showing 29 changed files with 446 additions and 257 deletions.
@@ -51,7 +51,7 @@
* moveAndResize(mem, newMem);
*
* //You are done with the old memory, so request close.
* //Note that it is up to the owner of the WritableDirectHandle whether or not to
* //Note that it is up to the owner of the WritableHandle whether or not to
* // actually close the resource.
* memReqSvr.requestClose(mem, newMem);
*
@@ -19,14 +19,23 @@

package org.apache.datasketches.memory;


/**
* A Handle for a writable direct memory resource.
* Joins a WritableMemory with a writable, AutoCloseable AllocateDirect resource.
* Please read Javadocs for {@link Handle}.
*
* Specific RuntimeException for the AutoCloseable.close() method.
*
* @author Lee Rhodes
* @author Roman Leventov
*
*/
public interface WritableDirectHandle extends WritableHandle { }
public class MemoryCloseException extends MemoryException {
private static final long serialVersionUID = 2L;

public MemoryCloseException() {
super("The associated resource failed to close.");
}

public MemoryCloseException(final String resource) {
super("The associated resource, " + resource + ", failed to close");
}

}


@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.datasketches.memory;

/**
* Specific RuntimeExceptions for the datasketches-memory component.
*
* @author Lee Rhodes
*
*/
public class MemoryException extends RuntimeException {
private static final long serialVersionUID = 1L;

/**
* Constructs a new runtime exception with the specified detail message. The cause is not
* initialized, and may subsequently be initialized by a call to
* Throwable.initCause(java.lang.Throwable).
*
* @param message the detail message. The detail message is saved for later retrieval by the
* Throwable.getMessage() method.
*/
public MemoryException(final String message) {
super(message);
}
}


@@ -22,7 +22,7 @@
/**
* The MemoryRequestServer is a callback interface to provide a means for a direct (off-heap),
* dynamic WritableMemory object to request more memory from the owner of the
* {@link WritableDirectHandle}. Refer to {@link DefaultMemoryRequestServer} for how this can be
* {@link WritableHandle}. Refer to {@link DefaultMemoryRequestServer} for how this can be
* used.
*
* @author Lee Rhodes
@@ -100,7 +100,7 @@ static WritableMapHandle writableMap(File file, long fileOffsetBytes, long capac
* leveraging the WritableMemory API. Native byte order is assumed.
* The allocated memory will be 8-byte aligned, but may not be page aligned.
* If capacityBytes is zero, byte order, backing storage and read-only status
* of the WritableMemory object, returned from {@link WritableHandle#get()} are unspecified.
* of the WritableMemory object, returned from {@link WritableHandle#getWritable()} are unspecified.
*
* <p>The default MemoryRequestServer, which allocates any request for memory onto the heap,
* will be used.</p>
@@ -110,10 +110,10 @@ static WritableMapHandle writableMap(File file, long fileOffsetBytes, long capac
* and to call <i>close()</i> when done.</p>
*
* @param capacityBytes the size of the desired memory in bytes.
* @return WritableDirectHandle for this off-heap resource.
* @return WritableHandle for this off-heap resource.
* Please read Javadocs for {@link Handle}.
*/
static WritableDirectHandle allocateDirect(long capacityBytes) {
static WritableHandle allocateDirect(long capacityBytes) {
return WritableMemoryImpl.allocateDirect(capacityBytes);
}

@@ -133,7 +133,7 @@ static WritableDirectHandle allocateDirect(long capacityBytes) {
* @return WritableHandle for this off-heap resource.
* Please read Javadocs for {@link Handle}.
*/
static WritableDirectHandle allocateDirect(long capacityBytes, MemoryRequestServer memReqSvr) {
static WritableHandle allocateDirect(long capacityBytes, MemoryRequestServer memReqSvr) {
return WritableMemoryImpl.allocateDirect(capacityBytes, memReqSvr);
}

@@ -33,7 +33,7 @@
* @author Lee Rhodes
*/
@SuppressWarnings({"restriction","synthetic-access"})
final class AllocateDirect implements AutoCloseable {
final class AllocateDirect {
private static final Logger LOG = LoggerFactory.getLogger(AllocateDirect.class);

private final Deallocator deallocator;
@@ -71,11 +71,6 @@ final class AllocateDirect implements AutoCloseable {
cleaner = Cleaner.create(this, deallocator);
}

@Override
public void close() {
doClose();
}

boolean doClose() {
try {
if (deallocator.deallocate(false)) {
@@ -86,7 +81,6 @@ boolean doClose() {
return true;
}
return false;

} finally {
BaseStateImpl.reachabilityFence(this);
}
@@ -129,7 +123,7 @@ boolean deallocate(final boolean calledFromCleaner) {
if (valid.change()) {
if (calledFromCleaner) {
// Warn about non-deterministic resource cleanup.
LOG.warn("A WritableDirectHandleImpl was not closed manually");
LOG.warn("A WritableHandle was not closed manually");
}
unsafe.freeMemory(nativeAddress);
NioBits.unreserveMemory(allocationSize, capacity);
@@ -31,6 +31,7 @@
import java.nio.channels.FileChannel;

import org.apache.datasketches.memory.Map;
import org.apache.datasketches.memory.MemoryCloseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@@ -90,7 +91,7 @@ class AllocateDirectMap implements Map {
MAPPED_BYTE_BUFFER_FORCE0_METHOD = MappedByteBuffer.class
.getDeclaredMethod("force0", FileDescriptor.class, long.class, long.class); //JDK15 removed
MAPPED_BYTE_BUFFER_FORCE0_METHOD.setAccessible(true);
} catch (final Exception e) {
} catch (final SecurityException | NoSuchMethodException e) {
throw new RuntimeException("Could not reflect static methods: " + e);
}
}
@@ -147,7 +148,7 @@ public boolean isLoaded() {
nativeBaseOffset,
capacityBytes,
pageCount);
} catch (final Exception e) {
} catch (final IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException(
String.format("Encountered %s exception while loading", e.getClass()));
}
@@ -156,10 +157,10 @@ public boolean isLoaded() {

@Override
public void close() {
doClose();
doClose("AllocateDirectMap");
}

boolean doClose() {
boolean doClose(final String resource) {
try {
if (deallocator.deallocate(false)) {
// This Cleaner.clean() call effectively just removes the Cleaner from the internal linked
@@ -169,6 +170,8 @@ boolean doClose() {
return true;
}
return false;
} catch (final Exception e) {
throw new MemoryCloseException(resource);
} finally {
BaseStateImpl.reachabilityFence(this);
}
@@ -191,7 +194,7 @@ private void madvise() {
.invoke(AccessByteBuffer.ZERO_READ_ONLY_DIRECT_BYTE_BUFFER,
nativeBaseOffset,
capacityBytes);
} catch (final Exception e) {
} catch (final IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException(
String.format("Encountered %s exception while loading", e.getClass()));
}
@@ -306,7 +309,7 @@ private void unmap() throws RuntimeException {
try {
FILE_CHANNEL_IMPL_UNMAP0_METHOD.invoke(myFc, actualNativeBaseOffset, myCapacity);
myRaf.close();
} catch (final Exception e) {
} catch (final IllegalAccessException | IllegalArgumentException | InvocationTargetException | IOException e) {
throw new RuntimeException(
String.format("Encountered %s exception while freeing memory", e.getClass()));
}
@@ -20,6 +20,8 @@
package org.apache.datasketches.memory.internal;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import org.apache.datasketches.memory.WritableMap;

@@ -53,9 +55,8 @@ public void force() {
raf.getFD(),
nativeBaseOffset,
capacityBytes);
} catch (final Exception e) {
throw new RuntimeException(String.format("Encountered %s exception in force. "
+ e.getClass()));
} catch (final IOException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException(String.format("Encountered %s exception in force. " + e.getClass()));
}
}
}
@@ -28,9 +28,9 @@
import java.nio.ByteOrder;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.datasketches.memory.BaseState;
import org.apache.datasketches.memory.DefaultMemoryRequestServer;
import org.apache.datasketches.memory.MemoryRequestServer;
import org.apache.datasketches.memory.BaseState;

/**
* Keeps key configuration state for MemoryImpl and BufferImpl plus some common static variables
@@ -30,10 +30,10 @@
import static org.apache.datasketches.memory.internal.UnsafeUtil.checkBounds;
import static org.apache.datasketches.memory.internal.UnsafeUtil.unsafe;

import org.apache.datasketches.memory.Buffer;

import java.nio.ByteOrder;

import org.apache.datasketches.memory.Buffer;

/*
* Developer notes: The heavier methods, such as put/get arrays, duplicate, region, clear, fill,
* compareTo, etc., use hard checks (check*() and incrementAndCheck*() methods), which execute at
@@ -39,9 +39,9 @@
import org.apache.datasketches.memory.Memory;
import org.apache.datasketches.memory.MemoryRequestServer;
import org.apache.datasketches.memory.Utf8CodingException;
import org.apache.datasketches.memory.WritableHandle;
import org.apache.datasketches.memory.WritableMapHandle;
import org.apache.datasketches.memory.WritableMemory;
import org.apache.datasketches.memory.WritableDirectHandle;

/*
* Developer notes: The heavier methods, such as put/get arrays, duplicate, region, clear, fill,
@@ -122,7 +122,7 @@ static WritableMapHandle wrapMap(final File file, final long fileOffsetBytes,
}

@SuppressWarnings("resource")
static WritableDirectHandle wrapDirect(final long capacityBytes,
static WritableHandle wrapDirect(final long capacityBytes,
final ByteOrder byteOrder, final MemoryRequestServer memReqSvr) {
if (capacityBytes <= 0) {
throw new IllegalArgumentException(
@@ -136,7 +136,7 @@ static WritableDirectHandle wrapDirect(final long capacityBytes,
: new DirectNonNativeWritableMemoryImpl(direct.getNativeBaseOffset(), 0L, capacityBytes,
typeId, direct.getValid(), memReqSvr);

final WritableDirectHandle handle = new WritableDirectHandleImpl(direct, wmem);
final WritableHandle handle = new WritableDirectHandleImpl(direct, wmem);
return handle;
}

@@ -21,6 +21,7 @@

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import org.apache.datasketches.memory.Buffer;

/**
@@ -39,7 +39,7 @@ public Memory get() {

@Override
public void close() {
if (dirMap.doClose()) {
if (dirMap.doClose("MapHandle")) {
wMem = null;
}
}

0 comments on commit 58e1efe

Please sign in to comment.