Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 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 @@ -49,8 +49,17 @@ public IcebergFileIO(FileIO delegate) {


@Override
public RapidsInputFile newInputFile(String path) throws IOException {
InputFile inputFile = delegate.newInputFile(path);
public IcebergInputFile newInputFile(String path) throws IOException {
return newInputFile(delegate.newInputFile(path));
}

/**
* Wraps an existing Iceberg input file, preserving any decryption performed by Iceberg.
*
* @param inputFile the Iceberg input file to wrap
*/
public IcebergInputFile newInputFile(InputFile inputFile) {
Objects.requireNonNull(inputFile, "inputFile can't be null");
return IcebergS3InputFile.maybeCreate(inputFile, delegate);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.nvidia.spark.rapids.fileio.iceberg.IcebergInputFile;
import com.nvidia.spark.rapids.iceberg.ShimUtils;
import com.nvidia.spark.rapids.jni.fileio.RapidsInputFile;
import com.nvidia.spark.rapids.jni.fileio.SeekableInputStream;
import org.apache.iceberg.io.FileIO;
import org.apache.iceberg.io.InputFile;
import org.apache.spark.TaskContext;
Expand All @@ -33,33 +32,31 @@

import java.io.IOException;
import java.util.List;
import java.util.OptionalLong;

/**
* S3-backed {@link RapidsInputFile} that delegates byte-range reads to
* {@link IcebergS3RangeCopier}. The supplied {@link FileIO} is only used for
* its property map and any per-prefix storage-credential overlays.
*/
public final class IcebergS3InputFile implements RapidsInputFile {
public final class IcebergS3InputFile extends IcebergInputFile {
Comment thread
liurenjie1024 marked this conversation as resolved.
Resolved
private static final Logger LOG = LoggerFactory.getLogger(IcebergS3InputFile.class);

private final IcebergInputFile delegate;
private final String s3Bucket;
private final String s3Key;
private final IcebergS3Client icebergS3Client;

private IcebergS3InputFile(
IcebergInputFile delegate,
InputFile delegate,
String s3Bucket,
String s3Key,
IcebergS3Client icebergS3Client) {
this.delegate = delegate;
super(delegate);
this.s3Bucket = s3Bucket;
this.s3Key = s3Key;
this.icebergS3Client = icebergS3Client;
}

public static RapidsInputFile maybeCreate(InputFile inputFile, FileIO fileIO) {
public static IcebergInputFile maybeCreate(InputFile inputFile, FileIO fileIO) {
// When the gating conf is off (or the file is not an S3 file), return the
// default IcebergInputFile so the standard Iceberg SeekableInputStream path is used.
IcebergInputFile delegate = new IcebergInputFile(inputFile);
Expand All @@ -86,36 +83,7 @@ public static RapidsInputFile maybeCreate(InputFile inputFile, FileIO fileIO) {
return delegate;
}
LOG.debug("IcebergS3RangeCopier path active for {}", inputFile.location());
return new IcebergS3InputFile(delegate, s3Bucket, s3Key, icebergS3Client);
}

@Override
public String path() {
return delegate.path();
}

@Override
public long getLength() throws IOException {
return delegate.getLength();
}

@Override
public OptionalLong getLastModificationTime() throws IOException {
return delegate.getLastModificationTime();
}

@Override
public SeekableInputStream open() throws IOException {
return delegate.open();
}

/**
* Returns the underlying Iceberg {@link InputFile}, matching
* {@link IcebergInputFile#getDelegate()} for use by iceberg-internal
* code paths that need direct access to the iceberg API.
*/
public InputFile getDelegate() {
return delegate.getDelegate();
return new IcebergS3InputFile(inputFile, s3Bucket, s3Key, icebergS3Client);
}

@Override
Expand All @@ -140,5 +108,11 @@ public void readTail(long length, HostMemoryBuffer output) throws IOException {
}
IcebergS3RangeCopier.copyTailToHMB(
icebergS3Client, output, s3Bucket, s3Key, length, /*dstOffset*/ 0L);
LOG.debug(
"PerfIO S3 Iceberg readTail suffix-range GET completed: uri=s3://{}/{}, "
+ "range=bytes=-{}",
s3Bucket,
s3Key,
length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import scala.collection.JavaConverters._

import com.nvidia.spark.rapids.GpuMetric
import com.nvidia.spark.rapids.MapUtil.toMapStrict
import com.nvidia.spark.rapids.fileio.iceberg.{IcebergFileIO, IcebergInputFile}
import com.nvidia.spark.rapids.fileio.iceberg.IcebergFileIO
import com.nvidia.spark.rapids.iceberg.ShimUtils
import com.nvidia.spark.rapids.iceberg.ShimUtils.locationOf
import com.nvidia.spark.rapids.iceberg.data.GpuDeleteFilter
Expand Down Expand Up @@ -110,7 +110,7 @@ class GpuIcebergPartitionReader(private val task: GpuSparkInputPartition,
val inputFiles = table.encryption()
.decrypt(encryptedFiles.asJava)
.asScala
.map(f => f.location() -> new IcebergInputFile(f))
.map(f => f.location() -> rapidsFileIO.newInputFile(f))
.toMap

val taskMap = toMapStrict(tasks.map(t => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@

package com.nvidia.spark.rapids.parquet

import java.nio.ByteOrder
import java.nio.charset.StandardCharsets
import java.util.Arrays
import java.util.{ArrayList, Arrays}

import ai.rapids.cudf.HostMemoryBuffer
import com.nvidia.spark.rapids.{GpuMetric, NoopMetric, NvtxRegistry, RapidsConf}
import com.nvidia.spark.rapids.Arm.{closeOnExcept, withResource}
import com.nvidia.spark.rapids.filecache.FileCache
import com.nvidia.spark.rapids.jni.fileio.RapidsInputFile
import com.nvidia.spark.rapids.jni.fileio.RapidsInputFile.CopyRange
import org.apache.hadoop.fs.Path
import org.apache.hadoop.io.IOUtils
import org.apache.parquet.bytes.BytesUtils.readIntLittleEndian
import org.apache.parquet.hadoop.ParquetFileWriter.MAGIC

/**
Expand Down Expand Up @@ -84,31 +84,26 @@ object ParquetFooterUtils {
if (fileLen < MAGIC.length + FooterLengthSize + MAGIC.length) {
throw new RuntimeException(s"$filePath is not a Parquet file (too small length: $fileLen )")
}
val footerLengthIndex = fileLen - FooterLengthSize - MAGIC.length
withResource(inputFile.open()) { inputStream =>
NvtxRegistry.PARQUET_READ_FOOTER_BYTES {
inputStream.seek(footerLengthIndex)
val footerLength = readIntLittleEndian(inputStream)
val magic = new Array[Byte](MAGIC.length)
IOUtils.readFully(inputStream, magic, 0, magic.length)
verifyParquetMagic(filePath, magic)
val fIdx = footerIndex(filePath, fileLen, footerLength, FooterLengthSize)
val tailBytes = Math.toIntExact(fileLen - fIdx)
closeOnExcept(HostMemoryBuffer.allocate(tailBytes + MAGIC.length, false)) { outBuffer =>
outBuffer.setBytes(0, MAGIC, 0, MAGIC.length)
inputStream.seek(fIdx)
val tmp = new Array[Byte](4096)
var written = MAGIC.length.toLong
var bytesLeft = tailBytes
while (bytesLeft > 0) {
val toRead = math.min(bytesLeft, tmp.length)
IOUtils.readFully(inputStream, tmp, 0, toRead)
outBuffer.setBytes(written, tmp, 0, toRead)
written += toRead
bytesLeft -= toRead
}
outBuffer
}
NvtxRegistry.PARQUET_READ_FOOTER_BYTES {
val trailerLength = FooterLengthSize + MAGIC.length
val (footerLength, magic) = withResource(
HostMemoryBuffer.allocate(trailerLength, false)) { trailerBuffer =>
inputFile.readTail(trailerLength, trailerBuffer)
val view = trailerBuffer.asByteBuffer(0, trailerLength).order(ByteOrder.LITTLE_ENDIAN)
val length = view.getInt()
val trailerMagic = new Array[Byte](MAGIC.length)
view.get(trailerMagic)
(length, trailerMagic)
}
verifyParquetMagic(filePath, magic)
val fIdx = footerIndex(filePath, fileLen, footerLength, FooterLengthSize)
val tailBytes = Math.toIntExact(fileLen - fIdx)
closeOnExcept(HostMemoryBuffer.allocate(tailBytes + MAGIC.length, false)) { outBuffer =>
outBuffer.setBytes(0, MAGIC, 0, MAGIC.length)
val ranges = new ArrayList[CopyRange](1)
ranges.add(new CopyRange(fIdx, tailBytes, MAGIC.length))
inputFile.readVectored(outBuffer, ranges)
outBuffer
}
}
}
Expand Down
Loading