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

[JNI] Adds HostColumnVector.EventHandler for spillability checks #13898

Merged
43 changes: 43 additions & 0 deletions java/src/main/java/ai/rapids/cudf/HostColumnVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,31 @@
* and call incRefCount to increment the reference count.
*/
public final class HostColumnVector extends HostColumnVectorCore {
/**
* Interface to handle events for this HostColumnVector. Only invoked during
* close, hence `onClosed` is the only event.
*/
public interface EventHandler {
/**
* `onClosed` is invoked with the updated `refCount` during `close`.
* The last invocation of `onClosed` will be with `refCount=0`.
*
* @note the callback is invoked with this `HostColumnVector`'s lock held.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* @note the callback is invoked with this `HostColumnVector`'s lock held.
* @note The callback is invoked with this `HostColumnVector`'s lock held.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We tend to favor starting with lowercase and we also favor indenting the description so that it reads as one block, but I do think the hyphen is discouraged. How about a compromise? (a0944ee)

Copy link
Contributor

@ttnghia ttnghia Aug 17, 2023

Choose a reason for hiding this comment

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

It's not very important since we are not using the generated docds. But if we are the users who read the generated docs, we will see they are ugly with bad style: Sentences start without first letter being Cap.

Copy link
Contributor

Choose a reason for hiding this comment

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

and sentences starting without cap. letter is not good IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since our code styles on this are all over the place, I am going to suggest filing a follow on if you feel strongly about it. We should then in that issue fix all the comments and make sure that the pre commit hooks exercise them, so we have something consistent and easy to test.

*
* @param cv - a reference to the HostColumnVector we are closing
* @param refCount - the updated ref count for this HostColumnVector at
* the time of invocation
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* @param cv - a reference to the HostColumnVector we are closing
* @param refCount - the updated ref count for this HostColumnVector at
* the time of invocation
* @param cv Reference to the HostColumnVector we are closing
* @param refCount The updated ref count for this HostColumnVector at
* the time of invocation

*/
void onClosed(HostColumnVector cv, int refCount);
}

/**
* The size in bytes of an offset entry
*/
static final int OFFSET_SIZE = DType.INT32.getSizeInBytes();

private int refCount;
private EventHandler eventHandler;

/**
* Create a new column vector with data populated on the host.
Expand Down Expand Up @@ -93,6 +112,27 @@ public HostColumnVector(DType type, long rows, Optional<Long> nullCount,
incRefCountInternal(true);
}

/**
* Set an event handler for this host vector. This method can be invoked with
* null to unset the handler.
*
* @param newHandler - the EventHandler to use from this point forward
* @return the prior event handler, or null if not set.
*/
public synchronized EventHandler setEventHandler(EventHandler newHandler) {
EventHandler prev = this.eventHandler;
this.eventHandler = newHandler;
return prev;
}

/**
* Returns the current event handler for this HostColumnVector or null if no
* handler is associated.
*/
public synchronized EventHandler getEventHandler() {
return this.eventHandler;
}

/**
* This is a really ugly API, but it is possible that the lifecycle of a column of
* data may not have a clear lifecycle thanks to java and GC. This API informs the leak
Expand All @@ -110,6 +150,9 @@ public void noWarnLeakExpected() {
public synchronized void close() {
refCount--;
offHeap.delRef();
if (eventHandler != null) {
eventHandler.onClosed(this, refCount);
}
if (refCount == 0) {
offHeap.clean(false);
for( HostColumnVectorCore child : children) {
Expand Down
29 changes: 29 additions & 0 deletions java/src/test/java/ai/rapids/cudf/ColumnVectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6791,6 +6791,18 @@ public void testEventHandlerIsCalledForEachClose() {
assertEquals(1, onClosedWasCalled.get());
}

@Test
public void testHostEventHandlerIsCalledForEachClose() {
final AtomicInteger onClosedWasCalled = new AtomicInteger(0);
try (HostColumnVector cv = HostColumnVector.fromInts(1,2,3,4)) {
cv.setEventHandler((col, refCount) -> {
assertEquals(cv, col);
onClosedWasCalled.incrementAndGet();
});
}
assertEquals(1, onClosedWasCalled.get());
}

@Test
public void testEventHandlerIsNotCalledIfNotSet() {
final AtomicInteger onClosedWasCalled = new AtomicInteger(0);
Expand All @@ -6808,6 +6820,23 @@ public void testEventHandlerIsNotCalledIfNotSet() {
assertEquals(0, onClosedWasCalled.get());
}

@Test
public void testHostEventHandlerIsNotCalledIfNotSet() {
final AtomicInteger onClosedWasCalled = new AtomicInteger(0);
try (HostColumnVector cv = HostColumnVector.fromInts(1,2,3,4)) {
assertNull(cv.getEventHandler());
}
assertEquals(0, onClosedWasCalled.get());

try (HostColumnVector cv = HostColumnVector.fromInts(1,2,3,4)) {
cv.setEventHandler((col, refCount) -> {
onClosedWasCalled.incrementAndGet();
});
cv.setEventHandler(null);
}
assertEquals(0, onClosedWasCalled.get());
}

/**
* Test that the ColumnView with unknown null-counts still returns
* the correct null-count when queried.
Expand Down