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

Add retry support to RowToColumnarIterator #9066

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.function.Function;

/**
* A GPU accelerated version of the Spark ColumnVector.
Expand Down Expand Up @@ -134,16 +135,18 @@ public static abstract class GpuColumnarBatchBuilderBase implements AutoCloseabl

public abstract void copyColumnar(ColumnVector cv, int colNum, int rows);

protected abstract ColumnVector buildAndPutOnDevice(int builderIndex);
protected abstract int buildersLength();
protected abstract ai.rapids.cudf.ColumnVector buildAndPutOnDevice(int builderIndex);

public ColumnarBatch build(int rows) {
int buildersLen = buildersLength();
ColumnVector[] vectors = new ColumnVector[buildersLen];
return build(rows, this::buildAndPutOnDevice);
}

protected ColumnarBatch build(int rows, Function<Integer, ai.rapids.cudf.ColumnVector> col) {
ColumnVector[] vectors = new ColumnVector[fields.length];
boolean success = false;
try {
for (int i = 0; i < buildersLen; i++) {
vectors[i] = buildAndPutOnDevice(i);
for (int i = 0; i < fields.length; i++) {
vectors[i] = new GpuColumnVector(fields[i].dataType(), col.apply(i));
}
ColumnarBatch ret = new ColumnarBatch(vectors, rows);
success = true;
Expand Down Expand Up @@ -191,17 +194,11 @@ public GpuArrowColumnarBatchBuilder(StructType schema) {
}

@Override
protected int buildersLength() {
return builders.length;
}

@Override
protected ColumnVector buildAndPutOnDevice(int builderIndex) {
protected ai.rapids.cudf.ColumnVector buildAndPutOnDevice(int builderIndex) {
ai.rapids.cudf.ColumnVector cv = builders[builderIndex].buildAndPutOnDevice();
GpuColumnVector gcv = new GpuColumnVector(fields[builderIndex].dataType(), cv);
referenceHolders[builderIndex].releaseReferences();
builders[builderIndex] = null;
return gcv;
return cv;
}

@Override
Expand Down Expand Up @@ -230,6 +227,7 @@ public void close() {

public static final class GpuColumnarBatchBuilder extends GpuColumnarBatchBuilderBase {
private final ai.rapids.cudf.HostColumnVector.ColumnBuilder[] builders;
private ai.rapids.cudf.HostColumnVector[] hostColumns;

/**
* A collection of builders for building up columnar data.
Expand Down Expand Up @@ -270,16 +268,10 @@ public ai.rapids.cudf.HostColumnVector.ColumnBuilder builder(int i) {
}

@Override
protected int buildersLength() {
return builders.length;
}

@Override
protected ColumnVector buildAndPutOnDevice(int builderIndex) {
protected ai.rapids.cudf.ColumnVector buildAndPutOnDevice(int builderIndex) {
ai.rapids.cudf.ColumnVector cv = builders[builderIndex].buildAndPutOnDevice();
GpuColumnVector gcv = new GpuColumnVector(fields[builderIndex].dataType(), cv);
builders[builderIndex] = null;
return gcv;
return cv;
}

public HostColumnVector[] buildHostColumns() {
Expand All @@ -303,12 +295,40 @@ public HostColumnVector[] buildHostColumns() {
}
}

/**
* Build a columnar batch without releasing the holding data on host.
* It is safe to call this multiple times, and data will be released
* after a call to `close`.
*/
public ColumnarBatch tryBuild(int rows) {
if (hostColumns == null) {
hostColumns = buildHostColumns();
}
return build(rows, i -> hostColumns[i].copyToDevice());
}

@Override
public void close() {
for (ai.rapids.cudf.HostColumnVector.ColumnBuilder b: builders) {
if (b != null) {
b.close();
try {
b.close();
} catch (Throwable e) {
/* ignore the exception */
Copy link
Collaborator

@abellina abellina Aug 17, 2023

Choose a reason for hiding this comment

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

this, and the next catch seem a bit odd. What exception were you seeing?

Should this instead use something like safeClose() (we only have that in scala right now) and produce a single exception after trying to close all, and returning a single exception object that has the individual exceptions supressed?

Copy link
Collaborator Author

@firestarman firestarman Aug 18, 2023

Choose a reason for hiding this comment

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

I just want to make sure it will always try to close the HostColumns no matter if any exception happens when closing the builders. Something likes

      try {
        for (ai.rapids.cudf.HostColumnVector.ColumnBuilder b: builders) {
          if (b != null) {
            b.close();
          }
        }
      } finally {
        if (hostColumns != null) {
          for (ai.rapids.cudf.HostColumnVector hcv: hostColumns) {
            if (hcv != null) {
              hcv.close();
            }
          }
          hostColumns = null;
        }
      }

Is this OK ? @abellina @revans2, or do you want me to do as the following. Java requires explicit declaration for non runtime exceptions, so we only need to catch the runtime exceptions here.

      RuntimeException toThrow = null;
      for (ai.rapids.cudf.HostColumnVector.ColumnBuilder b: builders) {
        if (b != null) {
          try {
            b.close();
          } catch (RuntimeException e) {
            if (toThrow == null) {
              toThrow = e;
            } else {
              toThrow.addSuppressed(e);
            }
          }
        }
      }
      if (hostColumns != null) {
        for (ai.rapids.cudf.HostColumnVector hcv: hostColumns) {
          if (hcv != null) {
            try {
              hcv.close();
            } catch (RuntimeException e) {
              if (toThrow == null) {
                toThrow = e;
              } else {
                toThrow.addSuppressed(e);
              }
            }
          }
        }
        hostColumns = null;
      }
      if (toThrow != null) {
        throw toThrow;
      }
    }

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Picked the first one. If you prefer to the second option, I will update it accordingly.

}
}
}
if (hostColumns != null) {
for (ai.rapids.cudf.HostColumnVector hcv: hostColumns) {
if (hcv != null) {
try {
hcv.close();
} catch (Throwable e) {
/* ignore the exception */
}
}
}
hostColumns = null;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,10 @@ class RowToColumnarIterator(
.foreach(ctx => GpuSemaphore.acquireIfNecessary(ctx))

val ret = withResource(new NvtxWithMetrics("RowToColumnar", NvtxColor.GREEN,
opTime)) { _ =>
builders.build(rowCount)
opTime)) { _ =>
RmmRapidsRetryIterator.withRetryNoSplit[ColumnarBatch] {
builders.tryBuild(rowCount)
}
}
numInputRows += rowCount
numOutputRows += rowCount
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2023, NVIDIA CORPORATION.
*
* 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.nvidia.spark.rapids

import com.nvidia.spark.rapids.jni.{RmmSpark, SplitAndRetryOOM}

import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.types._

class RowToColumnarIteratorRetrySuite extends RmmSparkRetrySuiteBase {
private val schema = StructType(Seq(StructField("a", IntegerType)))

test("test simple OOM retry") {
val rowIter: Iterator[InternalRow] = (1 to 10).map(InternalRow(_)).toIterator
val row2ColIter = new RowToColumnarIterator(
rowIter, schema, RequireSingleBatch, new GpuRowToColumnConverter(schema))
RmmSpark.forceRetryOOM(RmmSpark.getCurrentThreadId)
Arm.withResource(row2ColIter.next()) { batch =>
assertResult(10)(batch.numRows())
}
}

test("test simple OOM split and retry") {
val rowIter: Iterator[InternalRow] = (1 to 10).map(InternalRow(_)).toIterator
val row2ColIter = new RowToColumnarIterator(
rowIter, schema, RequireSingleBatch, new GpuRowToColumnConverter(schema))
RmmSpark.forceSplitAndRetryOOM(RmmSpark.getCurrentThreadId)
assertThrows[SplitAndRetryOOM] {
row2ColIter.next()
}
}
}
Loading