You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@gluten.apache.org by ma...@apache.org on 2025/06/17 12:28:46 UTC
(incubator-gluten) branch main updated: [GLUTEN-9942] Refactor the JNI for the native ShuffleWriter and PartitionWriter (#9944)
This is an automated email from the ASF dual-hosted git repository.
marong pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-gluten.git
The following commit(s) were added to refs/heads/main by this push:
new 2d44c9b28b [GLUTEN-9942] Refactor the JNI for the native ShuffleWriter and PartitionWriter (#9944)
2d44c9b28b is described below
commit 2d44c9b28b306f7bc9bd4c357f16d6d82a9f7d7f
Author: Rong Ma <ma...@apache.org>
AuthorDate: Tue Jun 17 13:28:42 2025 +0100
[GLUTEN-9942] Refactor the JNI for the native ShuffleWriter and PartitionWriter (#9944)
---
.../CelebornPartitionWriterJniWrapper.java | 47 +++++
.../VeloxCelebornColumnarShuffleWriter.scala | 67 ++++---
.../UnifflePartitionWriterJniWrapper.java | 47 +++++
.../writer/VeloxUniffleColumnarShuffleWriter.java | 83 +++++---
.../backendsapi/velox/VeloxSparkPlanExecApi.scala | 1 +
.../spark/shuffle/ColumnarShuffleWriter.scala | 104 +++++-----
cpp/core/compute/Runtime.h | 6 +-
cpp/core/jni/JniCommon.h | 12 +-
cpp/core/jni/JniWrapper.cc | 219 ++++++++++-----------
cpp/core/shuffle/LocalPartitionWriter.cc | 55 ++++--
cpp/core/shuffle/LocalPartitionWriter.h | 6 +-
cpp/core/shuffle/Options.h | 109 ++++++++--
cpp/core/shuffle/PartitionWriter.h | 13 +-
cpp/core/shuffle/ShuffleWriter.cc | 8 +-
cpp/core/shuffle/ShuffleWriter.h | 7 +-
cpp/core/shuffle/rss/RssPartitionWriter.cc | 6 +-
cpp/core/shuffle/rss/RssPartitionWriter.h | 10 +-
cpp/velox/benchmarks/GenericBenchmark.cc | 116 ++++++-----
cpp/velox/compute/VeloxRuntime.cc | 13 +-
cpp/velox/compute/VeloxRuntime.h | 4 +-
cpp/velox/jni/VeloxJniWrapper.cc | 89 ++++++++-
cpp/velox/shuffle/VeloxHashShuffleWriter.cc | 37 ++--
cpp/velox/shuffle/VeloxHashShuffleWriter.h | 15 +-
cpp/velox/shuffle/VeloxRssSortShuffleWriter.cc | 53 ++---
cpp/velox/shuffle/VeloxRssSortShuffleWriter.h | 24 ++-
cpp/velox/shuffle/VeloxShuffleWriter.cc | 13 +-
cpp/velox/shuffle/VeloxShuffleWriter.h | 18 +-
cpp/velox/shuffle/VeloxSortShuffleWriter.cc | 47 +++--
cpp/velox/shuffle/VeloxSortShuffleWriter.h | 12 +-
cpp/velox/tests/RuntimeTest.cc | 6 +-
cpp/velox/tests/VeloxShuffleWriterSpillTest.cc | 107 ++++++----
cpp/velox/tests/VeloxShuffleWriterTest.cc | 213 ++++++++++----------
cpp/velox/tests/VeloxShuffleWriterTestBase.h | 19 --
.../vectorized/LocalPartitionWriterJniWrapper.java | 51 +++++
.../gluten/vectorized/ShuffleWriterJniWrapper.java | 169 +++-------------
.../shuffle/CelebornColumnarShuffleWriter.scala | 3 -
36 files changed, 1043 insertions(+), 766 deletions(-)
diff --git a/backends-velox/src-celeborn/main/java/org/apache/gluten/vectorized/CelebornPartitionWriterJniWrapper.java b/backends-velox/src-celeborn/main/java/org/apache/gluten/vectorized/CelebornPartitionWriterJniWrapper.java
new file mode 100644
index 0000000000..7cbfe68955
--- /dev/null
+++ b/backends-velox/src-celeborn/main/java/org/apache/gluten/vectorized/CelebornPartitionWriterJniWrapper.java
@@ -0,0 +1,47 @@
+/*
+ * 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.gluten.vectorized;
+
+import org.apache.gluten.runtime.Runtime;
+import org.apache.gluten.runtime.RuntimeAware;
+
+public class CelebornPartitionWriterJniWrapper implements RuntimeAware {
+ private final Runtime runtime;
+
+ private CelebornPartitionWriterJniWrapper(org.apache.gluten.runtime.Runtime runtime) {
+ this.runtime = runtime;
+ }
+
+ public static CelebornPartitionWriterJniWrapper create(Runtime runtime) {
+ return new CelebornPartitionWriterJniWrapper(runtime);
+ }
+
+ @Override
+ public long rtHandle() {
+ return runtime.getHandle();
+ }
+
+ public native long createPartitionWriter(
+ int numPartitions,
+ String codec,
+ String codecBackend,
+ int compressionLevel,
+ int compressionBufferSize,
+ int pushBufferMaxSize,
+ long sortBufferMaxSize,
+ Object pusher);
+}
diff --git a/backends-velox/src-celeborn/main/scala/org/apache/spark/shuffle/VeloxCelebornColumnarShuffleWriter.scala b/backends-velox/src-celeborn/main/scala/org/apache/spark/shuffle/VeloxCelebornColumnarShuffleWriter.scala
index 547e2322fb..3dd9920bda 100644
--- a/backends-velox/src-celeborn/main/scala/org/apache/spark/shuffle/VeloxCelebornColumnarShuffleWriter.scala
+++ b/backends-velox/src-celeborn/main/scala/org/apache/spark/shuffle/VeloxCelebornColumnarShuffleWriter.scala
@@ -18,13 +18,12 @@ package org.apache.spark.shuffle
import org.apache.gluten.backendsapi.BackendsApiManager
import org.apache.gluten.columnarbatch.ColumnarBatches
-import org.apache.gluten.config.GlutenConfig
+import org.apache.gluten.config.{GlutenConfig, ReservedKeys}
import org.apache.gluten.memory.memtarget.{MemoryTarget, Spiller, Spillers}
import org.apache.gluten.runtime.Runtimes
import org.apache.gluten.vectorized._
import org.apache.spark._
-import org.apache.spark.internal.config.{SHUFFLE_DISK_WRITE_BUFFER_SIZE, SHUFFLE_SORT_INIT_BUFFER_SIZE, SHUFFLE_SORT_USE_RADIXSORT}
import org.apache.spark.memory.SparkMemoryUtil
import org.apache.spark.scheduler.MapStatus
import org.apache.spark.shuffle.celeborn.CelebornShuffleHandle
@@ -54,7 +53,8 @@ class VeloxCelebornColumnarShuffleWriter[K, V](
private val runtime =
Runtimes.contextInstance(BackendsApiManager.getBackendName, "CelebornShuffleWriter")
- private val jniWrapper = ShuffleWriterJniWrapper.create(runtime)
+ private val partitionWriterJniWrapper = CelebornPartitionWriterJniWrapper.create(runtime)
+ private val shuffleWriterJniWrapper = ShuffleWriterJniWrapper.create(runtime)
private var splitResult: GlutenSplitResult = _
@@ -82,9 +82,14 @@ class VeloxCelebornColumnarShuffleWriter[K, V](
logInfo(s"Skip ColumnarBatch of ${cb.numRows} rows, ${cb.numCols} cols")
} else {
initShuffleWriter(cb)
- val handle = ColumnarBatches.getNativeHandle(BackendsApiManager.getBackendName, cb)
+ val columnarBatchHandle =
+ ColumnarBatches.getNativeHandle(BackendsApiManager.getBackendName, cb)
val startTime = System.nanoTime()
- jniWrapper.write(nativeShuffleWriter, cb.numRows, handle, availableOffHeapPerTask())
+ shuffleWriterJniWrapper.write(
+ nativeShuffleWriter,
+ cb.numRows,
+ columnarBatchHandle,
+ availableOffHeapPerTask())
dep.metrics("shuffleWallTime").add(System.nanoTime() - startTime)
dep.metrics("numInputRows").add(cb.numRows)
dep.metrics("inputBatches").add(1)
@@ -100,7 +105,7 @@ class VeloxCelebornColumnarShuffleWriter[K, V](
}
val startTime = System.nanoTime()
- splitResult = jniWrapper.stop(nativeShuffleWriter)
+ splitResult = shuffleWriterJniWrapper.stop(nativeShuffleWriter)
dep.metrics("shuffleWallTime").add(System.nanoTime() - startTime)
nativeMetrics
@@ -119,28 +124,42 @@ class VeloxCelebornColumnarShuffleWriter[K, V](
}
override def createShuffleWriter(columnarBatch: ColumnarBatch): Unit = {
- nativeShuffleWriter = jniWrapper.makeForRSS(
- dep.nativePartitioning.getShortName,
- dep.nativePartitioning.getNumPartitions,
- nativeBufferSize,
+ val partitionWriterHandle = partitionWriterJniWrapper.createPartitionWriter(
+ numPartitions,
compressionCodec.orNull,
+ GlutenConfig.get.columnarShuffleCodecBackend.orNull,
compressionLevel,
compressionBufferSize,
- conf.get(SHUFFLE_DISK_WRITE_BUFFER_SIZE).toInt,
- bufferCompressThreshold,
- GlutenConfig.get.columnarShuffleCompressionMode,
- conf.get(SHUFFLE_SORT_INIT_BUFFER_SIZE).toInt,
- conf.get(SHUFFLE_SORT_USE_RADIXSORT),
clientPushBufferMaxSize,
clientPushSortMemoryThreshold,
- celebornPartitionPusher,
- ColumnarBatches.getNativeHandle(BackendsApiManager.getBackendName, columnarBatch),
- context.taskAttemptId(),
- GlutenShuffleUtils.getStartPartitionId(dep.nativePartitioning, context.partitionId),
- "celeborn",
- shuffleWriterType,
- GlutenConfig.get.columnarShuffleReallocThreshold
+ celebornPartitionPusher
)
+
+ nativeShuffleWriter = shuffleWriterType match {
+ case ReservedKeys.GLUTEN_HASH_SHUFFLE_WRITER =>
+ shuffleWriterJniWrapper.createHashShuffleWriter(
+ numPartitions,
+ dep.nativePartitioning.getShortName,
+ GlutenShuffleUtils.getStartPartitionId(dep.nativePartitioning, context.partitionId),
+ nativeBufferSize,
+ GlutenConfig.get.columnarShuffleReallocThreshold,
+ partitionWriterHandle
+ )
+ case ReservedKeys.GLUTEN_RSS_SORT_SHUFFLE_WRITER =>
+ shuffleWriterJniWrapper.createRssSortShuffleWriter(
+ numPartitions,
+ dep.nativePartitioning.getShortName,
+ GlutenShuffleUtils.getStartPartitionId(dep.nativePartitioning, context.partitionId),
+ nativeBufferSize,
+ clientPushSortMemoryThreshold,
+ compressionCodec.orNull,
+ partitionWriterHandle
+ )
+ case _ =>
+ throw new UnsupportedOperationException(
+ s"Unsupported celeborn shuffle writer type: $shuffleWriterType")
+ }
+
runtime
.memoryManager()
.addSpiller(new Spiller() {
@@ -150,7 +169,7 @@ class VeloxCelebornColumnarShuffleWriter[K, V](
}
logInfo(s"Gluten shuffle writer: Trying to push $size bytes of data")
// fixme pass true when being called by self
- val pushed = jniWrapper.nativeEvict(nativeShuffleWriter, size, false)
+ val pushed = shuffleWriterJniWrapper.reclaim(nativeShuffleWriter, size)
logInfo(s"Gluten shuffle writer: Pushed $pushed / $size bytes of data")
pushed
}
@@ -158,6 +177,6 @@ class VeloxCelebornColumnarShuffleWriter[K, V](
}
override def closeShuffleWriter(): Unit = {
- jniWrapper.close(nativeShuffleWriter)
+ shuffleWriterJniWrapper.close(nativeShuffleWriter)
}
}
diff --git a/backends-velox/src-uniffle/main/java/org/apache/gluten/vectorized/UnifflePartitionWriterJniWrapper.java b/backends-velox/src-uniffle/main/java/org/apache/gluten/vectorized/UnifflePartitionWriterJniWrapper.java
new file mode 100644
index 0000000000..cf3ee9c664
--- /dev/null
+++ b/backends-velox/src-uniffle/main/java/org/apache/gluten/vectorized/UnifflePartitionWriterJniWrapper.java
@@ -0,0 +1,47 @@
+/*
+ * 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.gluten.vectorized;
+
+import org.apache.gluten.runtime.Runtime;
+import org.apache.gluten.runtime.RuntimeAware;
+
+public class UnifflePartitionWriterJniWrapper implements RuntimeAware {
+ private final Runtime runtime;
+
+ private UnifflePartitionWriterJniWrapper(Runtime runtime) {
+ this.runtime = runtime;
+ }
+
+ public static UnifflePartitionWriterJniWrapper create(Runtime runtime) {
+ return new UnifflePartitionWriterJniWrapper(runtime);
+ }
+
+ @Override
+ public long rtHandle() {
+ return runtime.getHandle();
+ }
+
+ public native long createPartitionWriter(
+ int numPartitions,
+ String codec,
+ String codecBackend,
+ int compressionLevel,
+ int compressionBufferSize,
+ int pushBufferMaxSize,
+ long sortBufferMaxSize,
+ Object pusher);
+}
diff --git a/backends-velox/src-uniffle/main/java/org/apache/spark/shuffle/writer/VeloxUniffleColumnarShuffleWriter.java b/backends-velox/src-uniffle/main/java/org/apache/spark/shuffle/writer/VeloxUniffleColumnarShuffleWriter.java
index 383c30939f..96aaa5121a 100644
--- a/backends-velox/src-uniffle/main/java/org/apache/spark/shuffle/writer/VeloxUniffleColumnarShuffleWriter.java
+++ b/backends-velox/src-uniffle/main/java/org/apache/spark/shuffle/writer/VeloxUniffleColumnarShuffleWriter.java
@@ -19,7 +19,6 @@ package org.apache.spark.shuffle.writer;
import org.apache.gluten.backendsapi.BackendsApiManager;
import org.apache.gluten.columnarbatch.ColumnarBatches;
import org.apache.gluten.config.GlutenConfig;
-import org.apache.gluten.config.ReservedKeys;
import org.apache.gluten.memory.memtarget.MemoryTarget;
import org.apache.gluten.memory.memtarget.Spiller;
import org.apache.gluten.memory.memtarget.Spillers;
@@ -27,6 +26,7 @@ import org.apache.gluten.runtime.Runtime;
import org.apache.gluten.runtime.Runtimes;
import org.apache.gluten.vectorized.GlutenSplitResult;
import org.apache.gluten.vectorized.ShuffleWriterJniWrapper;
+import org.apache.gluten.vectorized.UnifflePartitionWriterJniWrapper;
import org.apache.spark.SparkConf;
import org.apache.spark.TaskContext;
@@ -64,20 +64,24 @@ public class VeloxUniffleColumnarShuffleWriter<K, V> extends RssShuffleWriter<K,
private long nativeShuffleWriter = -1L;
private boolean stopping = false;
- private final int compressThreshold = GlutenConfig.get().columnarShuffleCompressionThreshold();
private final double reallocThreshold = GlutenConfig.get().columnarShuffleReallocThreshold();
private String compressionCodec;
+ private String codecBackend;
private int compressionLevel;
private int compressionBufferSize;
- private int diskWriteBufferSize;
+ private final int diskWriteBufferSize;
private final int partitionId;
private final Runtime runtime =
Runtimes.contextInstance(BackendsApiManager.getBackendName(), "UniffleShuffleWriter");
- private final ShuffleWriterJniWrapper jniWrapper = ShuffleWriterJniWrapper.create(runtime);
+ private final UnifflePartitionWriterJniWrapper partitionWriterJniWrapper =
+ UnifflePartitionWriterJniWrapper.create(runtime);
+ private final ShuffleWriterJniWrapper shuffleWriterJniWrapper =
+ ShuffleWriterJniWrapper.create(runtime);
private final int nativeBufferSize = GlutenConfig.get().maxBatchSize();
private final int bufferSize;
private final Boolean isSort;
+ private final int numPartitions;
private final ColumnarShuffleDependency<K, V, V> columnarDep;
private final SparkConf sparkConf;
@@ -117,18 +121,23 @@ public class VeloxUniffleColumnarShuffleWriter<K, V> extends RssShuffleWriter<K,
this.partitionId = partitionId;
this.sparkConf = sparkConf;
this.isSort = isSort;
+ this.numPartitions = columnarDep.nativePartitioning().getNumPartitions();
bufferSize =
(int)
sparkConf.getSizeAsBytes(
RssSparkConfig.RSS_WRITER_BUFFER_SIZE.key(),
RssSparkConfig.RSS_WRITER_BUFFER_SIZE.defaultValue().get());
+ this.diskWriteBufferSize =
+ (int) (long) sparkConf.get(package$.MODULE$.SHUFFLE_DISK_WRITE_BUFFER_SIZE());
if ((boolean) sparkConf.get(package$.MODULE$.SHUFFLE_COMPRESS())) {
compressionCodec = GlutenShuffleUtils.getCompressionCodec(sparkConf);
compressionLevel = GlutenShuffleUtils.getCompressionLevel(sparkConf, compressionCodec);
compressionBufferSize =
GlutenShuffleUtils.getCompressionBufferSize(sparkConf, compressionCodec);
- diskWriteBufferSize =
- (int) (long) sparkConf.get(package$.MODULE$.SHUFFLE_DISK_WRITE_BUFFER_SIZE());
+ Option<String> codecBackend = GlutenConfig.get().columnarShuffleCodecBackend();
+ if (codecBackend.isDefined()) {
+ this.codecBackend = codecBackend.get();
+ }
}
}
@@ -145,34 +154,41 @@ public class VeloxUniffleColumnarShuffleWriter<K, V> extends RssShuffleWriter<K,
if (cb.numRows() == 0 || cb.numCols() == 0) {
LOG.info("Skip ColumnarBatch of 0 rows or 0 cols");
} else {
- long handle = ColumnarBatches.getNativeHandle(BackendsApiManager.getBackendName(), cb);
if (nativeShuffleWriter == -1) {
- nativeShuffleWriter =
- jniWrapper.makeForRSS(
- columnarDep.nativePartitioning().getShortName(),
- columnarDep.nativePartitioning().getNumPartitions(),
- nativeBufferSize,
- // use field do this
+ long partitionWriterHandle =
+ partitionWriterJniWrapper.createPartitionWriter(
+ numPartitions,
compressionCodec,
+ codecBackend,
compressionLevel,
compressionBufferSize,
- diskWriteBufferSize,
- compressThreshold,
- GlutenConfig.get().columnarShuffleCompressionMode(),
- (int) (long) sparkConf.get(package$.MODULE$.SHUFFLE_SORT_INIT_BUFFER_SIZE()),
- (boolean) sparkConf.get(package$.MODULE$.SHUFFLE_SORT_USE_RADIXSORT()),
bufferSize,
bufferSize,
- partitionPusher,
- handle,
- taskAttemptId,
- GlutenShuffleUtils.getStartPartitionId(
- columnarDep.nativePartitioning(), partitionId),
- "uniffle",
- isSort
- ? ReservedKeys.GLUTEN_SORT_SHUFFLE_WRITER()
- : ReservedKeys.GLUTEN_HASH_SHUFFLE_WRITER(),
- reallocThreshold);
+ partitionPusher);
+
+ if (isSort) {
+ nativeShuffleWriter =
+ shuffleWriterJniWrapper.createSortShuffleWriter(
+ numPartitions,
+ columnarDep.nativePartitioning().getShortName(),
+ GlutenShuffleUtils.getStartPartitionId(
+ columnarDep.nativePartitioning(), partitionId),
+ diskWriteBufferSize,
+ (int) (long) sparkConf.get(package$.MODULE$.SHUFFLE_SORT_INIT_BUFFER_SIZE()),
+ (boolean) sparkConf.get(package$.MODULE$.SHUFFLE_SORT_USE_RADIXSORT()),
+ partitionWriterHandle);
+ } else {
+ nativeShuffleWriter =
+ shuffleWriterJniWrapper.createHashShuffleWriter(
+ numPartitions,
+ columnarDep.nativePartitioning().getShortName(),
+ GlutenShuffleUtils.getStartPartitionId(
+ columnarDep.nativePartitioning(), partitionId),
+ nativeBufferSize,
+ reallocThreshold,
+ partitionWriterHandle);
+ }
+
runtime
.memoryManager()
.addSpiller(
@@ -183,15 +199,18 @@ public class VeloxUniffleColumnarShuffleWriter<K, V> extends RssShuffleWriter<K,
return 0L;
}
LOG.info("Gluten shuffle writer: Trying to push {} bytes of data", size);
- long pushed = jniWrapper.nativeEvict(nativeShuffleWriter, size, false);
+ long pushed = shuffleWriterJniWrapper.reclaim(nativeShuffleWriter, size);
LOG.info("Gluten shuffle writer: Pushed {} / {} bytes of data", pushed, size);
return pushed;
}
});
}
long startTime = System.nanoTime();
+ long columnarBatchHandle =
+ ColumnarBatches.getNativeHandle(BackendsApiManager.getBackendName(), cb);
long bytes =
- jniWrapper.write(nativeShuffleWriter, cb.numRows(), handle, availableOffHeapPerTask());
+ shuffleWriterJniWrapper.write(
+ nativeShuffleWriter, cb.numRows(), columnarBatchHandle, availableOffHeapPerTask());
LOG.debug("jniWrapper.write rows {}, split bytes {}", cb.numRows(), bytes);
columnarDep.metrics().get("dataSize").get().add(bytes);
// this metric replace part of uniffle shuffle write time
@@ -211,7 +230,7 @@ public class VeloxUniffleColumnarShuffleWriter<K, V> extends RssShuffleWriter<K,
long startTime = System.nanoTime();
GlutenSplitResult splitResult;
try {
- splitResult = jniWrapper.stop(nativeShuffleWriter);
+ splitResult = shuffleWriterJniWrapper.stop(nativeShuffleWriter);
} catch (IOException e) {
throw new RssException(e);
}
@@ -262,7 +281,7 @@ public class VeloxUniffleColumnarShuffleWriter<K, V> extends RssShuffleWriter<K,
private void closeShuffleWriter() {
if (nativeShuffleWriter != -1) {
- jniWrapper.close(nativeShuffleWriter);
+ shuffleWriterJniWrapper.close(nativeShuffleWriter);
nativeShuffleWriter = -1;
}
}
diff --git a/backends-velox/src/main/scala/org/apache/gluten/backendsapi/velox/VeloxSparkPlanExecApi.scala b/backends-velox/src/main/scala/org/apache/gluten/backendsapi/velox/VeloxSparkPlanExecApi.scala
index 82b9eb1956..72fb3e3b13 100644
--- a/backends-velox/src/main/scala/org/apache/gluten/backendsapi/velox/VeloxSparkPlanExecApi.scala
+++ b/backends-velox/src/main/scala/org/apache/gluten/backendsapi/velox/VeloxSparkPlanExecApi.scala
@@ -567,6 +567,7 @@ class VeloxSparkPlanExecApi extends SparkPlanExecApi {
parameters: GenShuffleWriterParameters[K, V]): GlutenShuffleWriterWrapper[K, V] = {
ShuffleUtil.genColumnarShuffleWriter(parameters)
}
+
override def createColumnarWriteFilesExec(
child: WriteFilesExecTransformer,
noop: SparkPlan,
diff --git a/backends-velox/src/main/scala/org/apache/spark/shuffle/ColumnarShuffleWriter.scala b/backends-velox/src/main/scala/org/apache/spark/shuffle/ColumnarShuffleWriter.scala
index 4478140780..1597f45b88 100644
--- a/backends-velox/src/main/scala/org/apache/spark/shuffle/ColumnarShuffleWriter.scala
+++ b/backends-velox/src/main/scala/org/apache/spark/shuffle/ColumnarShuffleWriter.scala
@@ -19,14 +19,13 @@ package org.apache.spark.shuffle
import org.apache.gluten.backendsapi.BackendsApiManager
import org.apache.gluten.columnarbatch.ColumnarBatches
import org.apache.gluten.config.GlutenConfig
-import org.apache.gluten.config.ReservedKeys
import org.apache.gluten.memory.memtarget.{MemoryTarget, Spiller, Spillers}
import org.apache.gluten.runtime.Runtimes
import org.apache.gluten.vectorized._
import org.apache.spark._
import org.apache.spark.internal.Logging
-import org.apache.spark.internal.config.{SHUFFLE_COMPRESS, SHUFFLE_DISK_WRITE_BUFFER_SIZE, SHUFFLE_SORT_INIT_BUFFER_SIZE, SHUFFLE_SORT_USE_RADIXSORT}
+import org.apache.spark.internal.config.{SHUFFLE_COMPRESS, SHUFFLE_DISK_WRITE_BUFFER_SIZE, SHUFFLE_FILE_BUFFER_SIZE, SHUFFLE_SORT_INIT_BUFFER_SIZE, SHUFFLE_SORT_USE_RADIXSORT}
import org.apache.spark.memory.SparkMemoryUtil
import org.apache.spark.scheduler.MapStatus
import org.apache.spark.sql.vectorized.ColumnarBatch
@@ -45,6 +44,8 @@ class ColumnarShuffleWriter[K, V](
private val dep = handle.dependency.asInstanceOf[ColumnarShuffleDependency[K, V, V]]
protected val isSort: Boolean = dep.isSort
+ private val numPartitions: Int = dep.partitioner.numPartitions
+
private val conf = SparkEnv.get.conf
private val blockManager = SparkEnv.get.blockManager
@@ -76,10 +77,6 @@ class ColumnarShuffleWriter[K, V](
}
}
- private val nativeMergeBufferSize = GlutenConfig.get.maxBatchSize
-
- private val nativeMergeThreshold = GlutenConfig.get.columnarShuffleMergeThreshold
-
private val compressionCodec: Option[String] =
if (conf.getBoolean(SHUFFLE_COMPRESS.key, SHUFFLE_COMPRESS.defaultValue.get)) {
Some(GlutenShuffleUtils.getCompressionCodec(conf))
@@ -87,9 +84,6 @@ class ColumnarShuffleWriter[K, V](
None
}
- private val compressionCodecBackend: Option[String] =
- GlutenConfig.get.columnarShuffleCodecBackend
-
private val compressionLevel = {
compressionCodec
.map(codec => GlutenShuffleUtils.getCompressionLevel(conf, codec))
@@ -102,14 +96,13 @@ class ColumnarShuffleWriter[K, V](
.getOrElse(0)
}
- private val bufferCompressThreshold =
- GlutenConfig.get.columnarShuffleCompressionThreshold
-
private val reallocThreshold = GlutenConfig.get.columnarShuffleReallocThreshold
private val runtime = Runtimes.contextInstance(BackendsApiManager.getBackendName, "ShuffleWriter")
- private val jniWrapper = ShuffleWriterJniWrapper.create(runtime)
+ private val partitionWriterJniWrapper = LocalPartitionWriterJniWrapper.create(runtime)
+
+ private val shuffleWriterJniWrapper = ShuffleWriterJniWrapper.create(runtime)
private var nativeShuffleWriter: Long = -1L
@@ -119,9 +112,6 @@ class ColumnarShuffleWriter[K, V](
private val taskContext: TaskContext = TaskContext.get()
- private val shuffleWriterType: String =
- if (isSort) ReservedKeys.GLUTEN_SORT_SHUFFLE_WRITER else ReservedKeys.GLUTEN_HASH_SHUFFLE_WRITER
-
private def availableOffHeapPerTask(): Long = {
val perTask =
SparkMemoryUtil.getCurrentAvailableOffHeapMemory / SparkResourceUtil.getTaskSlots(conf)
@@ -135,40 +125,54 @@ class ColumnarShuffleWriter[K, V](
return
}
- val dataTmp = Utils.tempFileWith(shuffleBlockResolver.getDataFile(dep.shuffleId, mapId))
+ val tempDataFile = Utils.tempFileWith(shuffleBlockResolver.getDataFile(dep.shuffleId, mapId))
while (records.hasNext) {
val cb = records.next()._2.asInstanceOf[ColumnarBatch]
if (cb.numRows == 0 || cb.numCols == 0) {
logInfo(s"Skip ColumnarBatch of ${cb.numRows} rows, ${cb.numCols} cols")
} else {
- val rows = cb.numRows()
- val handle = ColumnarBatches.getNativeHandle(BackendsApiManager.getBackendName, cb)
if (nativeShuffleWriter == -1L) {
- nativeShuffleWriter = jniWrapper.make(
- dep.nativePartitioning.getShortName,
- dep.nativePartitioning.getNumPartitions,
- nativeBufferSize,
- nativeMergeBufferSize,
- nativeMergeThreshold,
+ val partitionWriterHandle = partitionWriterJniWrapper.createPartitionWriter(
+ numPartitions,
compressionCodec.orNull,
- compressionCodecBackend.orNull,
+ GlutenConfig.get.columnarShuffleCodecBackend.orNull,
compressionLevel,
compressionBufferSize,
- conf.get(SHUFFLE_DISK_WRITE_BUFFER_SIZE).toInt,
- bufferCompressThreshold,
- GlutenConfig.get.columnarShuffleCompressionMode,
- conf.get(SHUFFLE_SORT_INIT_BUFFER_SIZE).toInt,
- conf.get(SHUFFLE_SORT_USE_RADIXSORT),
- dataTmp.getAbsolutePath,
+ GlutenConfig.get.columnarShuffleCompressionThreshold,
+ nativeBufferSize,
+ GlutenConfig.get.columnarShuffleMergeThreshold,
blockManager.subDirsPerLocalDir,
- localDirs,
- reallocThreshold,
- handle,
- taskContext.taskAttemptId(),
- GlutenShuffleUtils.getStartPartitionId(dep.nativePartitioning, taskContext.partitionId),
- shuffleWriterType
+ conf.get(SHUFFLE_FILE_BUFFER_SIZE).toInt,
+ tempDataFile.getAbsolutePath,
+ localDirs
)
+
+ nativeShuffleWriter = if (isSort) {
+ shuffleWriterJniWrapper.createSortShuffleWriter(
+ numPartitions,
+ dep.nativePartitioning.getShortName,
+ GlutenShuffleUtils.getStartPartitionId(
+ dep.nativePartitioning,
+ taskContext.partitionId),
+ conf.get(SHUFFLE_DISK_WRITE_BUFFER_SIZE).toInt,
+ conf.get(SHUFFLE_SORT_INIT_BUFFER_SIZE).toInt,
+ conf.get(SHUFFLE_SORT_USE_RADIXSORT),
+ partitionWriterHandle
+ )
+ } else {
+ shuffleWriterJniWrapper.createHashShuffleWriter(
+ numPartitions,
+ dep.nativePartitioning.getShortName,
+ GlutenShuffleUtils.getStartPartitionId(
+ dep.nativePartitioning,
+ taskContext.partitionId),
+ nativeBufferSize,
+ reallocThreshold,
+ partitionWriterHandle
+ )
+ }
+
runtime
.memoryManager()
.addSpiller(new Spiller() {
@@ -177,16 +181,22 @@ class ColumnarShuffleWriter[K, V](
return 0L
}
logInfo(s"Gluten shuffle writer: Trying to spill $size bytes of data")
- // fixme pass true when being called by self
- val spilled =
- jniWrapper.nativeEvict(nativeShuffleWriter, size, false)
+ val spilled = shuffleWriterJniWrapper.reclaim(nativeShuffleWriter, size)
logInfo(s"Gluten shuffle writer: Spilled $spilled / $size bytes of data")
spilled
}
})
}
+
+ val rows = cb.numRows()
+ val columnarBatchHandle =
+ ColumnarBatches.getNativeHandle(BackendsApiManager.getBackendName, cb)
val startTime = System.nanoTime()
- jniWrapper.write(nativeShuffleWriter, rows, handle, availableOffHeapPerTask())
+ shuffleWriterJniWrapper.write(
+ nativeShuffleWriter,
+ rows,
+ columnarBatchHandle,
+ availableOffHeapPerTask())
dep.metrics("shuffleWallTime").add(System.nanoTime() - startTime)
dep.metrics("numInputRows").add(rows)
dep.metrics("inputBatches").add(1)
@@ -203,7 +213,7 @@ class ColumnarShuffleWriter[K, V](
val startTime = System.nanoTime()
assert(nativeShuffleWriter != -1L)
- splitResult = jniWrapper.stop(nativeShuffleWriter)
+ splitResult = shuffleWriterJniWrapper.stop(nativeShuffleWriter)
closeShuffleWriter()
dep.metrics("shuffleWallTime").add(System.nanoTime() - startTime)
if (!isSort) {
@@ -234,10 +244,10 @@ class ColumnarShuffleWriter[K, V](
mapId,
partitionLengths,
Array[Long](),
- dataTmp)
+ tempDataFile)
} finally {
- if (dataTmp.exists() && !dataTmp.delete()) {
- logError(s"Error while deleting temp file ${dataTmp.getAbsolutePath}")
+ if (tempDataFile.exists() && !tempDataFile.delete()) {
+ logError(s"Error while deleting temp file ${tempDataFile.getAbsolutePath}")
}
}
@@ -268,7 +278,7 @@ class ColumnarShuffleWriter[K, V](
if (nativeShuffleWriter == -1L) {
return
}
- jniWrapper.close(nativeShuffleWriter)
+ shuffleWriterJniWrapper.close(nativeShuffleWriter)
nativeShuffleWriter = -1L
}
diff --git a/cpp/core/compute/Runtime.h b/cpp/core/compute/Runtime.h
index 119bcda57b..c9962642ea 100644
--- a/cpp/core/compute/Runtime.h
+++ b/cpp/core/compute/Runtime.h
@@ -129,9 +129,9 @@ class Runtime : public std::enable_shared_from_this<Runtime> {
}
virtual std::shared_ptr<ShuffleWriter> createShuffleWriter(
- int numPartitions,
- std::unique_ptr<PartitionWriter> partitionWriter,
- ShuffleWriterOptions options) {
+ int32_t numPartitions,
+ const std::shared_ptr<PartitionWriter>& partitionWriter,
+ const std::shared_ptr<ShuffleWriterOptions>& options) {
throw GlutenException("Not implemented");
}
diff --git a/cpp/core/jni/JniCommon.h b/cpp/core/jni/JniCommon.h
index 67b3d2a911..d777d39fa7 100644
--- a/cpp/core/jni/JniCommon.h
+++ b/cpp/core/jni/JniCommon.h
@@ -354,18 +354,18 @@ static inline arrow::Compression::type getCompressionType(JNIEnv* env, jstring c
return compressionType;
}
-static inline gluten::CodecBackend getCodecBackend(JNIEnv* env, jstring codecJstr) {
- if (codecJstr == nullptr) {
+static inline gluten::CodecBackend getCodecBackend(JNIEnv* env, jstring codecBackendJstr) {
+ if (codecBackendJstr == nullptr) {
return gluten::CodecBackend::NONE;
}
- auto codecBackend = jStringToCString(env, codecJstr);
+ auto codecBackend = jStringToCString(env, codecBackendJstr);
if (codecBackend == "qat") {
return gluten::CodecBackend::QAT;
- } else if (codecBackend == "iaa") {
+ }
+ if (codecBackend == "iaa") {
return gluten::CodecBackend::IAA;
- } else {
- throw std::invalid_argument("Not support this codec backend " + codecBackend);
}
+ throw std::invalid_argument("Not support this codec backend " + codecBackend);
}
static inline gluten::CompressionMode getCompressionMode(JNIEnv* env, jstring compressionModeJstr) {
diff --git a/cpp/core/jni/JniWrapper.cc b/cpp/core/jni/JniWrapper.cc
index 2fde8ca4cd..59c0fa5078 100644
--- a/cpp/core/jni/JniWrapper.cc
+++ b/cpp/core/jni/JniWrapper.cc
@@ -35,7 +35,6 @@
#include "shuffle/ShuffleReader.h"
#include "shuffle/ShuffleWriter.h"
#include "shuffle/Utils.h"
-#include "shuffle/rss/RssPartitionWriter.h"
#include "utils/ArrowStatus.h"
#include "utils/StringUtil.h"
@@ -780,141 +779,137 @@ JNIEXPORT jlong JNICALL Java_org_apache_gluten_columnarbatch_ColumnarBatchJniWra
JNI_METHOD_END(kInvalidObjectHandle)
}
-// Shuffle
-JNIEXPORT jlong JNICALL Java_org_apache_gluten_vectorized_ShuffleWriterJniWrapper_nativeMake( // NOLINT
+JNIEXPORT jlong JNICALL
+Java_org_apache_gluten_vectorized_LocalPartitionWriterJniWrapper_createPartitionWriter( // NOLINT
JNIEnv* env,
jobject wrapper,
- jstring partitioningNameJstr,
jint numPartitions,
- jint bufferSize,
- jint mergeBufferSize,
- jdouble mergeThreshold,
jstring codecJstr,
jstring codecBackendJstr,
jint compressionLevel,
jint compressionBufferSize,
- jint diskWriteBufferSize,
jint compressionThreshold,
- jstring compressionModeJstr,
- jint initialSortBufferSize,
- jboolean useRadixSort,
- jstring dataFileJstr,
+ jint mergeBufferSize,
+ jdouble mergeThreshold,
jint numSubDirs,
- jstring localDirsJstr,
- jdouble reallocThreshold,
- jlong firstBatchHandle,
- jlong taskAttemptId,
+ jint shuffleFileBufferSize,
+ jstring dataFileJstr,
+ jstring localDirsJstr) {
+ JNI_METHOD_START
+
+ const auto ctx = getRuntime(env, wrapper);
+
+ auto dataFile = jStringToCString(env, dataFileJstr);
+ auto localDirs = splitPaths(jStringToCString(env, localDirsJstr));
+
+ auto partitionWriterOptions = std::make_shared<LocalPartitionWriterOptions>(
+ shuffleFileBufferSize, compressionBufferSize, compressionThreshold, mergeBufferSize, mergeThreshold, numSubDirs);
+
+ auto partitionWriter = std::make_shared<LocalPartitionWriter>(
+ numPartitions,
+ createArrowIpcCodec(getCompressionType(env, codecJstr), getCodecBackend(env, codecBackendJstr), compressionLevel),
+ ctx->memoryManager(),
+ partitionWriterOptions,
+ dataFile,
+ std::move(localDirs));
+
+ return ctx->saveObject(partitionWriter);
+ JNI_METHOD_END(kInvalidObjectHandle)
+}
+
+JNIEXPORT jlong JNICALL Java_org_apache_gluten_vectorized_ShuffleWriterJniWrapper_createHashShuffleWriter(
+ JNIEnv* env,
+ jobject wrapper,
+ jint numPartitions,
+ jstring partitioningNameJstr,
jint startPartitionId,
- jint pushBufferMaxSize,
- jlong sortBufferMaxSize,
- jobject partitionPusher,
- jstring partitionWriterTypeJstr,
- jstring shuffleWriterTypeJstr) {
+ jint splitBufferSize,
+ jdouble splitBufferReallocThreshold,
+ jlong partitionWriterHandle) {
JNI_METHOD_START
- auto ctx = getRuntime(env, wrapper);
- if (partitioningNameJstr == nullptr) {
- throw GlutenException(std::string("Short partitioning name can't be null"));
- }
+ const auto ctx = getRuntime(env, wrapper);
- // Build ShuffleWriterOptions.
- auto shuffleWriterOptions = ShuffleWriterOptions{
- .bufferSize = bufferSize,
- .bufferReallocThreshold = reallocThreshold,
- .partitioning = toPartitioning(jStringToCString(env, partitioningNameJstr)),
- .taskAttemptId = static_cast<int64_t>(taskAttemptId),
- .startPartitionId = startPartitionId,
- .shuffleWriterType = ShuffleWriter::stringToType(jStringToCString(env, shuffleWriterTypeJstr)),
- .initialSortBufferSize = initialSortBufferSize,
- .diskWriteBufferSize = diskWriteBufferSize,
- .useRadixSort = static_cast<bool>(useRadixSort)};
-
- // Build PartitionWriterOptions.
- auto partitionWriterOptions = PartitionWriterOptions{
- .mergeBufferSize = mergeBufferSize,
- .mergeThreshold = mergeThreshold,
- .compressionBufferSize = compressionBufferSize,
- .compressionThreshold = compressionThreshold,
- .compressionType = getCompressionType(env, codecJstr),
- .compressionLevel = compressionLevel,
- .numSubDirs = numSubDirs,
- .pushBufferMaxSize = pushBufferMaxSize > 0 ? pushBufferMaxSize : kDefaultPushMemoryThreshold,
- .sortBufferMaxSize = sortBufferMaxSize > 0 ? sortBufferMaxSize : kDefaultSortBufferThreshold};
- if (codecJstr != NULL) {
- partitionWriterOptions.codecBackend = getCodecBackend(env, codecBackendJstr);
- partitionWriterOptions.compressionMode = getCompressionMode(env, compressionModeJstr);
+ auto partitionWriter = ObjectStore::retrieve<PartitionWriter>(partitionWriterHandle);
+ if (partitionWriter == nullptr) {
+ throw GlutenException("Partition writer handle is invalid: " + std::to_string(partitionWriterHandle));
}
- const auto& conf = ctx->getConfMap();
- {
- auto it = conf.find(kShuffleFileBufferSize);
- if (it != conf.end()) {
- partitionWriterOptions.shuffleFileBufferSize = static_cast<int64_t>(stoi(it->second));
- }
+ ObjectStore::release(partitionWriterHandle);
+
+ auto shuffleWriterOptions = std::make_shared<HashShuffleWriterOptions>(
+ toPartitioning(jStringToCString(env, partitioningNameJstr)),
+ startPartitionId,
+ splitBufferSize,
+ splitBufferReallocThreshold);
+
+ return ctx->saveObject(ctx->createShuffleWriter(numPartitions, partitionWriter, shuffleWriterOptions));
+ JNI_METHOD_END(kInvalidObjectHandle)
+}
+
+JNIEXPORT jlong JNICALL Java_org_apache_gluten_vectorized_ShuffleWriterJniWrapper_createSortShuffleWriter(
+ JNIEnv* env,
+ jobject wrapper,
+ jint numPartitions,
+ jstring partitioningNameJstr,
+ jint startPartitionId,
+ jint diskWriteBufferSize,
+ jint initialSortBufferSize,
+ jboolean useRadixSort,
+ jlong partitionWriterHandle) {
+ JNI_METHOD_START
+ const auto ctx = getRuntime(env, wrapper);
+
+ auto partitionWriter = ObjectStore::retrieve<PartitionWriter>(partitionWriterHandle);
+ if (partitionWriter == nullptr) {
+ throw GlutenException("Partition writer handle is invalid: " + std::to_string(partitionWriterHandle));
}
+ ObjectStore::release(partitionWriterHandle);
+
+ auto shuffleWriterOptions = std::make_shared<SortShuffleWriterOptions>(
+ toPartitioning(jStringToCString(env, partitioningNameJstr)),
+ startPartitionId,
+ initialSortBufferSize,
+ diskWriteBufferSize,
+ static_cast<bool>(useRadixSort));
- std::unique_ptr<PartitionWriter> partitionWriter;
+ return ctx->saveObject(ctx->createShuffleWriter(numPartitions, partitionWriter, shuffleWriterOptions));
+ JNI_METHOD_END(kInvalidObjectHandle)
+}
- auto partitionWriterTypeC = env->GetStringUTFChars(partitionWriterTypeJstr, JNI_FALSE);
- auto partitionWriterType = std::string(partitionWriterTypeC);
- env->ReleaseStringUTFChars(partitionWriterTypeJstr, partitionWriterTypeC);
+JNIEXPORT jlong JNICALL Java_org_apache_gluten_vectorized_ShuffleWriterJniWrapper_createRssSortShuffleWriter(
+ JNIEnv* env,
+ jobject wrapper,
+ jint numPartitions,
+ jstring partitioningNameJstr,
+ jint startPartitionId,
+ jint splitBufferSize,
+ jlong sortBufferMaxSize,
+ jstring codecJstr,
+ jlong partitionWriterHandle) {
+ JNI_METHOD_START
+ const auto ctx = getRuntime(env, wrapper);
- if (partitionWriterType == "local") {
- if (dataFileJstr == NULL) {
- throw GlutenException(std::string("Shuffle DataFile can't be null"));
- }
- if (localDirsJstr == NULL) {
- throw GlutenException(std::string("Shuffle DataFile can't be null"));
- }
- auto dataFileC = env->GetStringUTFChars(dataFileJstr, JNI_FALSE);
- auto dataFile = std::string(dataFileC);
- env->ReleaseStringUTFChars(dataFileJstr, dataFileC);
-
- auto localDirsC = env->GetStringUTFChars(localDirsJstr, JNI_FALSE);
- auto configuredDirs = splitPaths(std::string(localDirsC));
- env->ReleaseStringUTFChars(localDirsJstr, localDirsC);
-
- partitionWriter = std::make_unique<LocalPartitionWriter>(
- numPartitions, std::move(partitionWriterOptions), ctx->memoryManager(), dataFile, configuredDirs);
- } else if (partitionWriterType == "celeborn") {
- jclass celebornPartitionPusherClass =
- createGlobalClassReferenceOrError(env, "Lorg/apache/spark/shuffle/CelebornPartitionPusher;");
- jmethodID celebornPushPartitionDataMethod =
- getMethodIdOrError(env, celebornPartitionPusherClass, "pushPartitionData", "(I[BI)I");
- JavaVM* vm;
- if (env->GetJavaVM(&vm) != JNI_OK) {
- throw GlutenException("Unable to get JavaVM instance");
- }
- std::shared_ptr<JavaRssClient> celebornClient =
- std::make_shared<JavaRssClient>(vm, partitionPusher, celebornPushPartitionDataMethod);
- partitionWriter = std::make_unique<RssPartitionWriter>(
- numPartitions, std::move(partitionWriterOptions), ctx->memoryManager(), std::move(celebornClient));
- } else if (partitionWriterType == "uniffle") {
- jclass unifflePartitionPusherClass =
- createGlobalClassReferenceOrError(env, "Lorg/apache/spark/shuffle/writer/PartitionPusher;");
- jmethodID unifflePushPartitionDataMethod =
- getMethodIdOrError(env, unifflePartitionPusherClass, "pushPartitionData", "(I[BI)I");
- JavaVM* vm;
- if (env->GetJavaVM(&vm) != JNI_OK) {
- throw GlutenException("Unable to get JavaVM instance");
- }
- std::shared_ptr<JavaRssClient> uniffleClient =
- std::make_shared<JavaRssClient>(vm, partitionPusher, unifflePushPartitionDataMethod);
- partitionWriter = std::make_unique<RssPartitionWriter>(
- numPartitions, std::move(partitionWriterOptions), ctx->memoryManager(), std::move(uniffleClient));
- } else {
- throw GlutenException("Unrecognizable partition writer type: " + partitionWriterType);
+ auto partitionWriter = ObjectStore::retrieve<PartitionWriter>(partitionWriterHandle);
+ if (partitionWriter == nullptr) {
+ throw GlutenException("Partition writer handle is invalid: " + std::to_string(partitionWriterHandle));
}
+ ObjectStore::release(partitionWriterHandle);
+
+ auto shuffleWriterOptions = std::make_shared<RssSortShuffleWriterOptions>(
+ toPartitioning(jStringToCString(env, partitioningNameJstr)),
+ startPartitionId,
+ splitBufferSize,
+ sortBufferMaxSize,
+ getCompressionType(env, codecJstr));
- return ctx->saveObject(
- ctx->createShuffleWriter(numPartitions, std::move(partitionWriter), std::move(shuffleWriterOptions)));
+ return ctx->saveObject(ctx->createShuffleWriter(numPartitions, partitionWriter, shuffleWriterOptions));
JNI_METHOD_END(kInvalidObjectHandle)
}
-JNIEXPORT jlong JNICALL Java_org_apache_gluten_vectorized_ShuffleWriterJniWrapper_nativeEvict( // NOLINT
+JNIEXPORT jlong JNICALL Java_org_apache_gluten_vectorized_ShuffleWriterJniWrapper_reclaim( // NOLINT
JNIEnv* env,
jobject wrapper,
jlong shuffleWriterHandle,
- jlong size,
- jboolean callBySelf) {
+ jlong size) {
JNI_METHOD_START
auto shuffleWriter = ObjectStore::retrieve<ShuffleWriter>(shuffleWriterHandle);
if (!shuffleWriter) {
@@ -923,7 +918,7 @@ JNIEXPORT jlong JNICALL Java_org_apache_gluten_vectorized_ShuffleWriterJniWrappe
}
int64_t evictedSize;
arrowAssertOkOrThrow(shuffleWriter->reclaimFixedSize(size, &evictedSize), "(shuffle) nativeEvict: evict failed");
- return (jlong)evictedSize;
+ return evictedSize;
JNI_METHOD_END(kInvalidObjectHandle)
}
diff --git a/cpp/core/shuffle/LocalPartitionWriter.cc b/cpp/core/shuffle/LocalPartitionWriter.cc
index 0a87c79fe8..eb7668185e 100644
--- a/cpp/core/shuffle/LocalPartitionWriter.cc
+++ b/cpp/core/shuffle/LocalPartitionWriter.cc
@@ -176,12 +176,17 @@ class LocalPartitionWriter::LocalSpiller {
class LocalPartitionWriter::PayloadMerger {
public:
- PayloadMerger(const PartitionWriterOptions& options, arrow::MemoryPool* pool, arrow::util::Codec* codec)
+ PayloadMerger(
+ arrow::MemoryPool* pool,
+ arrow::util::Codec* codec,
+ int32_t compressionThreshold,
+ int32_t mergeBufferSize,
+ int32_t mergeBufferMinSize)
: pool_(pool),
codec_(codec),
- compressionThreshold_(options.compressionThreshold),
- mergeBufferSize_(options.mergeBufferSize),
- mergeBufferMinSize_(options.mergeBufferSize * options.mergeThreshold) {}
+ compressionThreshold_(compressionThreshold),
+ mergeBufferSize_(mergeBufferSize),
+ mergeBufferMinSize_(mergeBufferMinSize) {}
arrow::Result<std::vector<std::unique_ptr<InMemoryPayload>>>
merge(uint32_t partitionId, std::unique_ptr<InMemoryPayload> append, bool reuseBuffers) {
@@ -410,17 +415,21 @@ class LocalPartitionWriter::PayloadCache {
LocalPartitionWriter::LocalPartitionWriter(
uint32_t numPartitions,
- PartitionWriterOptions options,
+ std::unique_ptr<arrow::util::Codec> codec,
MemoryManager* memoryManager,
+ const std::shared_ptr<LocalPartitionWriterOptions>& options,
const std::string& dataFile,
- const std::vector<std::string>& localDirs)
- : PartitionWriter(numPartitions, std::move(options), memoryManager), dataFile_(dataFile), localDirs_(localDirs) {
+ std::vector<std::string> localDirs)
+ : PartitionWriter(numPartitions, std::move(codec), memoryManager),
+ options_(options),
+ dataFile_(dataFile),
+ localDirs_(std::move(localDirs)) {
init();
}
std::string LocalPartitionWriter::nextSpilledFileDir() {
auto spilledFileDir = getShuffleSpillDir(localDirs_[dirSelection_], subDirSelection_[dirSelection_]);
- subDirSelection_[dirSelection_] = (subDirSelection_[dirSelection_] + 1) % options_.numSubDirs;
+ subDirSelection_[dirSelection_] = (subDirSelection_[dirSelection_] + 1) % options_->numSubDirs;
dirSelection_ = (dirSelection_ + 1) % localDirs_.size();
return spilledFileDir;
}
@@ -454,6 +463,9 @@ arrow::Status LocalPartitionWriter::clearResource() {
}
void LocalPartitionWriter::init() {
+ GLUTEN_CHECK(!dataFile_.empty(), "Shuffle data file path is not configured.");
+ GLUTEN_CHECK(!localDirs_.empty(), "Shuffle local directories can not be empty.");
+
partitionLengths_.resize(numPartitions_, 0);
rawPartitionLengths_.resize(numPartitions_, 0);
@@ -472,7 +484,7 @@ arrow::Result<int64_t> LocalPartitionWriter::mergeSpills(uint32_t partitionId, a
for (const auto& spill : spills_) {
ARROW_ASSIGN_OR_RAISE(auto startPos, os->Tell());
- spill->openForRead(options_.shuffleFileBufferSize);
+ spill->openForRead(options_->shuffleFileBufferSize);
// Read if partition exists in the spilled file. Then write to the final data file.
while (auto payload = spill->nextPayload(partitionId)) {
@@ -528,7 +540,7 @@ arrow::Status LocalPartitionWriter::stop(ShuffleWriterMetrics* metrics) {
RETURN_NOT_OK(finishSpill());
RETURN_NOT_OK(finishMerger());
- ARROW_ASSIGN_OR_RAISE(dataFileOs_, openFile(dataFile_, options_.shuffleFileBufferSize));
+ ARROW_ASSIGN_OR_RAISE(dataFileOs_, openFile(dataFile_, options_->shuffleFileBufferSize));
int64_t endInFinalFile = 0;
DLOG(INFO) << "LocalPartitionWriter stopped. Total spills: " << spills_.size();
@@ -561,16 +573,16 @@ arrow::Status LocalPartitionWriter::requestSpill(bool isFinal) {
std::shared_ptr<arrow::io::OutputStream> os;
if (isFinal) {
// If `spill()` is requested after `stop()`, open the final data file for writing.
- ARROW_ASSIGN_OR_RAISE(dataFileOs_, openFile(dataFile_, options_.shuffleFileBufferSize));
+ ARROW_ASSIGN_OR_RAISE(dataFileOs_, openFile(dataFile_, options_->shuffleFileBufferSize));
spillFile = dataFile_;
os = dataFileOs_;
useSpillFileAsDataFile_ = true;
} else {
ARROW_ASSIGN_OR_RAISE(spillFile, createTempShuffleFile(nextSpilledFileDir()));
- ARROW_ASSIGN_OR_RAISE(os, openFile(spillFile, options_.shuffleFileBufferSize));
+ ARROW_ASSIGN_OR_RAISE(os, openFile(spillFile, options_->shuffleFileBufferSize));
}
spiller_ = std::make_unique<LocalSpiller>(
- isFinal, os, std::move(spillFile), options_.compressionBufferSize, payloadPool_.get(), codec_.get());
+ isFinal, os, std::move(spillFile), options_->compressionBufferSize, payloadPool_.get(), codec_.get());
}
return arrow::Status::OK();
}
@@ -591,7 +603,7 @@ arrow::Status LocalPartitionWriter::finishMerger() {
if (maybeMerged.has_value()) {
if (payloadCache_ == nullptr) {
payloadCache_ = std::make_shared<PayloadCache>(
- numPartitions_, codec_.get(), options_.compressionThreshold, payloadPool_.get());
+ numPartitions_, codec_.get(), options_->compressionThreshold, payloadPool_.get());
}
// Spill can be triggered by compressing or building dictionaries.
RETURN_NOT_OK(payloadCache_->cache(pid, std::move(maybeMerged.value())));
@@ -612,7 +624,7 @@ arrow::Status LocalPartitionWriter::hashEvict(
if (evictType == Evict::kSpill) {
RETURN_NOT_OK(requestSpill(false));
- auto shouldCompress = codec_ != nullptr && inMemoryPayload->numRows() >= options_.compressionThreshold;
+ auto shouldCompress = codec_ != nullptr && inMemoryPayload->numRows() >= options_->compressionThreshold;
ARROW_ASSIGN_OR_RAISE(
auto payload,
inMemoryPayload->toBlockPayload(
@@ -623,13 +635,18 @@ arrow::Status LocalPartitionWriter::hashEvict(
}
if (!merger_) {
- merger_ = std::make_shared<PayloadMerger>(options_, payloadPool_.get(), codec_.get());
+ merger_ = std::make_shared<PayloadMerger>(
+ payloadPool_.get(),
+ codec_.get(),
+ options_->compressionThreshold,
+ options_->mergeBufferSize,
+ options_->mergeBufferSize * options_->mergeThreshold);
}
ARROW_ASSIGN_OR_RAISE(auto merged, merger_->merge(partitionId, std::move(inMemoryPayload), reuseBuffers));
if (!merged.empty()) {
if (UNLIKELY(!payloadCache_)) {
payloadCache_ = std::make_shared<PayloadCache>(
- numPartitions_, codec_.get(), options_.compressionThreshold, payloadPool_.get());
+ numPartitions_, codec_.get(), options_->compressionThreshold, payloadPool_.get());
}
for (auto& payload : merged) {
RETURN_NOT_OK(payloadCache_->cache(partitionId, std::move(payload)));
@@ -683,7 +700,7 @@ arrow::Status LocalPartitionWriter::reclaimFixedSize(int64_t size, int64_t* actu
ARROW_ASSIGN_OR_RAISE(
spills_.back(),
payloadCache_->spill(
- spillFile, payloadPool_.get(), codec_.get(), options_.shuffleFileBufferSize, totalBytesToEvict_));
+ spillFile, payloadPool_.get(), codec_.get(), options_->shuffleFileBufferSize, totalBytesToEvict_));
reclaimed += beforeSpill - payloadPool_->bytes_allocated();
@@ -706,7 +723,7 @@ arrow::Status LocalPartitionWriter::reclaimFixedSize(int64_t size, int64_t* actu
RETURN_NOT_OK(requestSpill(false));
- bool shouldCompress = codec_ != nullptr && merged->numRows() >= options_.compressionThreshold;
+ bool shouldCompress = codec_ != nullptr && merged->numRows() >= options_->compressionThreshold;
ARROW_ASSIGN_OR_RAISE(
auto payload,
merged->toBlockPayload(
diff --git a/cpp/core/shuffle/LocalPartitionWriter.h b/cpp/core/shuffle/LocalPartitionWriter.h
index 8d7e4a2a0f..3709b03c28 100644
--- a/cpp/core/shuffle/LocalPartitionWriter.h
+++ b/cpp/core/shuffle/LocalPartitionWriter.h
@@ -29,10 +29,11 @@ class LocalPartitionWriter : public PartitionWriter {
public:
explicit LocalPartitionWriter(
uint32_t numPartitions,
- PartitionWriterOptions options,
+ std::unique_ptr<arrow::util::Codec> codec,
MemoryManager* memoryManager,
+ const std::shared_ptr<LocalPartitionWriterOptions>& options,
const std::string& dataFile,
- const std::vector<std::string>& localDirs);
+ std::vector<std::string> localDirs);
arrow::Status hashEvict(
uint32_t partitionId,
@@ -100,6 +101,7 @@ class LocalPartitionWriter : public PartitionWriter {
arrow::Status populateMetrics(ShuffleWriterMetrics* metrics);
+ std::shared_ptr<LocalPartitionWriterOptions> options_;
std::string dataFile_;
std::vector<std::string> localDirs_;
diff --git a/cpp/core/shuffle/Options.h b/cpp/core/shuffle/Options.h
index 898bb5b9b8..a5f051848c 100644
--- a/cpp/core/shuffle/Options.h
+++ b/cpp/core/shuffle/Options.h
@@ -34,7 +34,7 @@ static constexpr int32_t kDefaultNumSubDirs = 64;
static constexpr int32_t kDefaultCompressionThreshold = 100;
static constexpr int32_t kDefaultCompressionBufferSize = 32 * 1024;
static constexpr int32_t kDefaultDiskWriteBufferSize = 1024 * 1024;
-static constexpr double kDefaultBufferReallocThreshold = 0.25;
+static constexpr double kDefaultSplitBufferReallocThreshold = 0.25;
static constexpr double kDefaultMergeBufferThreshold = 0.25;
static constexpr bool kDefaultUseRadixSort = true;
static constexpr int32_t kDefaultSortBufferSize = 4096;
@@ -64,39 +64,112 @@ struct ShuffleReaderOptions {
};
struct ShuffleWriterOptions {
- int32_t bufferSize = kDefaultShuffleWriterBufferSize;
- double bufferReallocThreshold = kDefaultBufferReallocThreshold;
- int64_t pushMemoryThreshold = kDefaultPushMemoryThreshold;
+ ShuffleWriterType shuffleWriterType;
Partitioning partitioning = Partitioning::kRoundRobin;
- int64_t taskAttemptId = -1;
int32_t startPartitionId = 0;
- int64_t threadId = -1;
- ShuffleWriterType shuffleWriterType = ShuffleWriterType::kHashShuffle;
- // Sort shuffle writer.
+ ShuffleWriterOptions(ShuffleWriterType shuffleWriterType) : shuffleWriterType(shuffleWriterType) {}
+
+ ShuffleWriterOptions(ShuffleWriterType shuffleWriterType, Partitioning partitioning, int32_t startPartitionId)
+ : shuffleWriterType(shuffleWriterType), partitioning(partitioning), startPartitionId(startPartitionId) {}
+
+ virtual ~ShuffleWriterOptions() = default;
+};
+
+struct HashShuffleWriterOptions : ShuffleWriterOptions {
+ int32_t splitBufferSize = kDefaultShuffleWriterBufferSize;
+ double splitBufferReallocThreshold = kDefaultSplitBufferReallocThreshold;
+
+ HashShuffleWriterOptions() : ShuffleWriterOptions(ShuffleWriterType::kHashShuffle) {}
+
+ HashShuffleWriterOptions(
+ Partitioning partitioning,
+ int32_t startPartitionId,
+ int32_t partitionBufferSize,
+ double partitionBufferReallocThreshold)
+ : ShuffleWriterOptions(ShuffleWriterType::kHashShuffle, partitioning, startPartitionId),
+ splitBufferSize(partitionBufferSize),
+ splitBufferReallocThreshold(partitionBufferReallocThreshold) {}
+};
+
+struct SortShuffleWriterOptions : ShuffleWriterOptions {
int32_t initialSortBufferSize = kDefaultSortBufferSize; // spark.shuffle.sort.initialBufferSize
int32_t diskWriteBufferSize = kDefaultDiskWriteBufferSize; // spark.shuffle.spill.diskWriteBufferSize
bool useRadixSort = kDefaultUseRadixSort; // spark.shuffle.sort.useRadixSort
+
+ SortShuffleWriterOptions() : ShuffleWriterOptions(ShuffleWriterType::kSortShuffle) {}
+
+ SortShuffleWriterOptions(
+ Partitioning partitioning,
+ int32_t startPartitionId,
+ int32_t initialSortBufferSize,
+ int32_t diskWriteBufferSize,
+ bool useRadixSort)
+ : ShuffleWriterOptions(ShuffleWriterType::kSortShuffle, partitioning, startPartitionId),
+ initialSortBufferSize(initialSortBufferSize),
+ diskWriteBufferSize(diskWriteBufferSize),
+ useRadixSort(useRadixSort) {}
};
-struct PartitionWriterOptions {
- int32_t mergeBufferSize = kDefaultShuffleWriterBufferSize;
- double mergeThreshold = kDefaultMergeBufferThreshold;
+struct RssSortShuffleWriterOptions : ShuffleWriterOptions {
+ int32_t splitBufferSize = kDefaultShuffleWriterBufferSize;
+ int64_t sortBufferMaxSize = kDefaultSortBufferThreshold;
+ arrow::Compression::type compressionType = arrow::Compression::type::LZ4_FRAME;
+
+ RssSortShuffleWriterOptions() : ShuffleWriterOptions(ShuffleWriterType::kRssSortShuffle) {}
+
+ RssSortShuffleWriterOptions(
+ Partitioning partitioning,
+ int32_t startPartitionId,
+ int32_t splitBufferSize,
+ int64_t sortBufferMaxSize,
+ arrow::Compression::type compressionType)
+ : ShuffleWriterOptions(ShuffleWriterType::kRssSortShuffle, partitioning, startPartitionId),
+ splitBufferSize(splitBufferSize),
+ sortBufferMaxSize(sortBufferMaxSize),
+ compressionType(compressionType) {}
+};
+
+struct LocalPartitionWriterOptions {
+ int64_t shuffleFileBufferSize = kDefaultShuffleFileBufferSize; // spark.shuffle.file.buffer
int32_t compressionBufferSize =
kDefaultCompressionBufferSize; // spark.io.compression.lz4.blockSize,spark.io.compression.zstd.bufferSize
+
int32_t compressionThreshold = kDefaultCompressionThreshold;
- arrow::Compression::type compressionType = arrow::Compression::LZ4_FRAME;
- CodecBackend codecBackend = CodecBackend::NONE;
- int32_t compressionLevel = arrow::util::kUseDefaultCompressionLevel;
- CompressionMode compressionMode = CompressionMode::BUFFER;
+ int32_t mergeBufferSize = kDefaultShuffleWriterBufferSize;
+ double mergeThreshold = kDefaultMergeBufferThreshold;
- int32_t numSubDirs = kDefaultNumSubDirs;
+ int32_t numSubDirs = kDefaultNumSubDirs; // spark.diskStore.subDirectories
+
+ LocalPartitionWriterOptions() = default;
+
+ LocalPartitionWriterOptions(
+ int64_t shuffleFileBufferSize,
+ int32_t compressionBufferSize,
+ int64_t compressionThreshold,
+ int32_t mergeBufferSize,
+ double mergeThreshold,
+ int32_t numSubDirs)
+ : shuffleFileBufferSize(shuffleFileBufferSize),
+ compressionBufferSize(compressionBufferSize),
+ compressionThreshold(compressionThreshold),
+ mergeBufferSize(mergeBufferSize),
+ mergeThreshold(mergeThreshold),
+ numSubDirs(numSubDirs) {}
+};
+struct RssPartitionWriterOptions {
+ int32_t compressionBufferSize =
+ kDefaultCompressionBufferSize; // spark.io.compression.lz4.blockSize,spark.io.compression.zstd.bufferSize
int64_t pushBufferMaxSize = kDefaultPushMemoryThreshold;
-
int64_t sortBufferMaxSize = kDefaultSortBufferThreshold;
- int64_t shuffleFileBufferSize = kDefaultShuffleFileBufferSize;
+ RssPartitionWriterOptions() = default;
+
+ RssPartitionWriterOptions(int32_t compressionBufferSize, int64_t pushBufferMaxSize, int64_t sortBufferMaxSize)
+ : compressionBufferSize(compressionBufferSize),
+ pushBufferMaxSize(pushBufferMaxSize),
+ sortBufferMaxSize(sortBufferMaxSize) {}
};
struct ShuffleWriterMetrics {
diff --git a/cpp/core/shuffle/PartitionWriter.h b/cpp/core/shuffle/PartitionWriter.h
index 73e05ed287..7fbcd73f58 100644
--- a/cpp/core/shuffle/PartitionWriter.h
+++ b/cpp/core/shuffle/PartitionWriter.h
@@ -31,10 +31,9 @@ struct Evict {
class PartitionWriter : public Reclaimable {
public:
- PartitionWriter(uint32_t numPartitions, PartitionWriterOptions options, MemoryManager* memoryManager)
- : numPartitions_(numPartitions), options_(std::move(options)), memoryManager_(memoryManager) {
+ PartitionWriter(uint32_t numPartitions, std::unique_ptr<arrow::util::Codec> codec, MemoryManager* memoryManager)
+ : numPartitions_(numPartitions), codec_(std::move(codec)), memoryManager_(memoryManager) {
payloadPool_ = memoryManager->createArrowMemoryPool("PartitionWriter.cached_payload");
- codec_ = createArrowIpcCodec(options_.compressionType, options_.codecBackend, options_.compressionLevel);
}
static inline std::string typeToString(PartitionWriterType type) {
@@ -67,21 +66,15 @@ class PartitionWriter : public Reclaimable {
return payloadPool_->bytes_allocated();
}
- PartitionWriterOptions& options() {
- return options_;
- }
-
protected:
uint32_t numPartitions_;
- PartitionWriterOptions options_;
+ std::unique_ptr<arrow::util::Codec> codec_;
MemoryManager* memoryManager_;
// Memory Pool used to track memory allocation of partition payloads.
// The actual allocation is delegated to options_.memoryPool.
std::shared_ptr<arrow::MemoryPool> payloadPool_;
- std::unique_ptr<arrow::util::Codec> codec_;
-
int64_t compressTime_{0};
int64_t spillTime_{0};
int64_t writeTime_{0};
diff --git a/cpp/core/shuffle/ShuffleWriter.cc b/cpp/core/shuffle/ShuffleWriter.cc
index 192de798d8..16e8b32624 100644
--- a/cpp/core/shuffle/ShuffleWriter.cc
+++ b/cpp/core/shuffle/ShuffleWriter.cc
@@ -55,10 +55,6 @@ int32_t ShuffleWriter::numPartitions() const {
return numPartitions_;
}
-ShuffleWriterOptions& ShuffleWriter::options() {
- return options_;
-}
-
int64_t ShuffleWriter::totalBytesWritten() const {
return metrics_.totalBytesWritten;
}
@@ -99,6 +95,6 @@ const std::vector<int64_t>& ShuffleWriter::rawPartitionLengths() const {
return metrics_.rawPartitionLengths;
}
-ShuffleWriter::ShuffleWriter(int32_t numPartitions, ShuffleWriterOptions options)
- : numPartitions_(numPartitions), options_(std::move(options)) {}
+ShuffleWriter::ShuffleWriter(int32_t numPartitions, Partitioning partitioning)
+ : numPartitions_(numPartitions), partitioning_(partitioning) {}
} // namespace gluten
diff --git a/cpp/core/shuffle/ShuffleWriter.h b/cpp/core/shuffle/ShuffleWriter.h
index fea4708f97..224c2726e4 100644
--- a/cpp/core/shuffle/ShuffleWriter.h
+++ b/cpp/core/shuffle/ShuffleWriter.h
@@ -41,8 +41,6 @@ class ShuffleWriter : public Reclaimable {
int32_t numPartitions() const;
- ShuffleWriterOptions& options();
-
int64_t totalBytesWritten() const;
int64_t totalBytesEvicted() const;
@@ -66,13 +64,12 @@ class ShuffleWriter : public Reclaimable {
const std::vector<int64_t>& rawPartitionLengths() const;
protected:
- ShuffleWriter(int32_t numPartitions, ShuffleWriterOptions options);
+ ShuffleWriter(int32_t numPartitions, Partitioning partitioning);
~ShuffleWriter() override = default;
int32_t numPartitions_;
-
- ShuffleWriterOptions options_;
+ Partitioning partitioning_;
ShuffleWriterMetrics metrics_{};
};
diff --git a/cpp/core/shuffle/rss/RssPartitionWriter.cc b/cpp/core/shuffle/rss/RssPartitionWriter.cc
index 3e8880e3b8..83a31cac1e 100644
--- a/cpp/core/shuffle/rss/RssPartitionWriter.cc
+++ b/cpp/core/shuffle/rss/RssPartitionWriter.cc
@@ -82,13 +82,13 @@ RssPartitionWriter::sortEvict(uint32_t partitionId, std::unique_ptr<InMemoryPayl
}
rssOs_ =
- std::make_shared<RssPartitionWriterOutputStream>(partitionId, rssClient_.get(), options_.pushBufferMaxSize);
+ std::make_shared<RssPartitionWriterOutputStream>(partitionId, rssClient_.get(), options_->pushBufferMaxSize);
RETURN_NOT_OK(rssOs_->init());
if (codec_ != nullptr) {
ARROW_ASSIGN_OR_RAISE(
compressedOs_,
ShuffleCompressedOutputStream::Make(
- codec_.get(), options_.compressionBufferSize, rssOs_, arrow::default_memory_pool()));
+ codec_.get(), options_->compressionBufferSize, rssOs_, arrow::default_memory_pool()));
}
lastEvictedPartitionId_ = partitionId;
@@ -117,7 +117,7 @@ arrow::Status RssPartitionWriter::doEvict(uint32_t partitionId, std::unique_ptr<
ARROW_ASSIGN_OR_RAISE(
auto payload, inMemoryPayload->toBlockPayload(payloadType, payloadPool_.get(), codec_ ? codec_.get() : nullptr));
// Copy payload to arrow buffered os.
- ARROW_ASSIGN_OR_RAISE(auto rssBufferOs, arrow::io::BufferOutputStream::Create(options_.pushBufferMaxSize));
+ ARROW_ASSIGN_OR_RAISE(auto rssBufferOs, arrow::io::BufferOutputStream::Create(options_->pushBufferMaxSize));
RETURN_NOT_OK(payload->serialize(rssBufferOs.get()));
payload = nullptr; // Invalidate payload immediately.
diff --git a/cpp/core/shuffle/rss/RssPartitionWriter.h b/cpp/core/shuffle/rss/RssPartitionWriter.h
index 6ad896425d..3e3a078aa5 100644
--- a/cpp/core/shuffle/rss/RssPartitionWriter.h
+++ b/cpp/core/shuffle/rss/RssPartitionWriter.h
@@ -31,11 +31,12 @@ class RssPartitionWriterOutputStream;
class RssPartitionWriter final : public PartitionWriter {
public:
RssPartitionWriter(
- uint32_t numPartitions,
- PartitionWriterOptions options,
+ const uint32_t numPartitions,
+ std::unique_ptr<arrow::util::Codec> codec,
MemoryManager* memoryManager,
- std::shared_ptr<RssClient> rssClient)
- : PartitionWriter(numPartitions, std::move(options), memoryManager), rssClient_(rssClient) {
+ const std::shared_ptr<RssPartitionWriterOptions>& options,
+ const std::shared_ptr<RssClient>& rssClient)
+ : PartitionWriter(numPartitions, std::move(codec), memoryManager), options_(options), rssClient_(rssClient) {
init();
}
@@ -59,6 +60,7 @@ class RssPartitionWriter final : public PartitionWriter {
arrow::Status doEvict(uint32_t partitionId, std::unique_ptr<InMemoryPayload> inMemoryPayload);
+ std::shared_ptr<RssPartitionWriterOptions> options_;
std::shared_ptr<RssClient> rssClient_;
std::vector<int64_t> bytesEvicted_;
diff --git a/cpp/velox/benchmarks/GenericBenchmark.cc b/cpp/velox/benchmarks/GenericBenchmark.cc
index b0ec44802f..bc811c19cf 100644
--- a/cpp/velox/benchmarks/GenericBenchmark.cc
+++ b/cpp/velox/benchmarks/GenericBenchmark.cc
@@ -163,60 +163,91 @@ void cleanupLocalDirs(const std::vector<std::string>& localDirs) {
}
}
-PartitionWriterOptions createPartitionWriterOptions() {
- PartitionWriterOptions partitionWriterOptions{};
-
- // Configure compression.
+void setCompressionTypeFromFlag(arrow::Compression::type& compressionType, CodecBackend& codecBackend) {
+ codecBackend = CodecBackend::NONE;
if (FLAGS_compression == "none") {
- partitionWriterOptions.compressionType = arrow::Compression::UNCOMPRESSED;
+ compressionType = arrow::Compression::UNCOMPRESSED;
} else if (FLAGS_compression == "lz4") {
- partitionWriterOptions.compressionType = arrow::Compression::LZ4_FRAME;
+ compressionType = arrow::Compression::LZ4_FRAME;
} else if (FLAGS_compression == "zstd") {
- partitionWriterOptions.compressionType = arrow::Compression::ZSTD;
+ compressionType = arrow::Compression::ZSTD;
} else if (FLAGS_compression == "qat_gzip") {
- partitionWriterOptions.codecBackend = CodecBackend::QAT;
- partitionWriterOptions.compressionType = arrow::Compression::GZIP;
+ codecBackend = CodecBackend::QAT;
+ compressionType = arrow::Compression::GZIP;
} else if (FLAGS_compression == "qat_zstd") {
- partitionWriterOptions.codecBackend = CodecBackend::QAT;
- partitionWriterOptions.compressionType = arrow::Compression::ZSTD;
+ codecBackend = CodecBackend::QAT;
+ compressionType = arrow::Compression::ZSTD;
} else if (FLAGS_compression == "iaa_gzip") {
- partitionWriterOptions.codecBackend = CodecBackend::IAA;
- partitionWriterOptions.compressionType = arrow::Compression::GZIP;
+ codecBackend = CodecBackend::IAA;
+ compressionType = arrow::Compression::GZIP;
+ } else {
+ throw GlutenException("Unrecognized compression type: " + FLAGS_compression);
}
- return partitionWriterOptions;
}
-std::unique_ptr<PartitionWriter> createPartitionWriter(
- Runtime* runtime,
- PartitionWriterOptions options,
- const std::string& dataFile,
- const std::vector<std::string>& localDirs) {
+std::unique_ptr<arrow::util::Codec> createCodec() {
+ // Configure compression.
+ if (FLAGS_compression == "none") {
+ return nullptr;
+ }
+
+ arrow::Compression::type compressionType;
+ CodecBackend codecBackend;
+
+ setCompressionTypeFromFlag(compressionType, codecBackend);
+
+ return createArrowIpcCodec(compressionType, codecBackend);
+}
+
+std::shared_ptr<PartitionWriter>
+createPartitionWriter(Runtime* runtime, const std::string& dataFile, const std::vector<std::string>& localDirs) {
std::unique_ptr<PartitionWriter> partitionWriter;
if (FLAGS_rss) {
+ auto options = std::make_shared<RssPartitionWriterOptions>();
auto rssClient = std::make_unique<LocalRssClient>(dataFile);
- partitionWriter = std::make_unique<RssPartitionWriter>(
- FLAGS_shuffle_partitions, std::move(options), runtime->memoryManager(), std::move(rssClient));
- } else {
- partitionWriter = std::make_unique<LocalPartitionWriter>(
- FLAGS_shuffle_partitions, std::move(options), runtime->memoryManager(), dataFile, localDirs);
+ return std::make_shared<RssPartitionWriter>(
+ FLAGS_shuffle_partitions, createCodec(), runtime->memoryManager(), options, std::move(rssClient));
+ }
+ auto options = std::make_shared<LocalPartitionWriterOptions>();
+ return std::make_unique<LocalPartitionWriter>(
+ FLAGS_shuffle_partitions, createCodec(), runtime->memoryManager(), options, dataFile, localDirs);
+}
+
+std::shared_ptr<ShuffleWriterOptions> createShuffleWriterOptions() {
+ std::shared_ptr<ShuffleWriterOptions> options;
+
+ switch (ShuffleWriter::stringToType(FLAGS_shuffle_writer)) {
+ case ShuffleWriterType::kHashShuffle:
+ options = std::make_shared<HashShuffleWriterOptions>();
+ break;
+ case ShuffleWriterType::kSortShuffle:
+ options = std::make_shared<SortShuffleWriterOptions>();
+ break;
+ case ShuffleWriterType::kRssSortShuffle:
+ options = std::make_shared<RssSortShuffleWriterOptions>();
+ break;
+ default:
+ throw GlutenException("Unsupported shuffle writer type: " + FLAGS_shuffle_writer);
}
- return partitionWriter;
+
+ options->partitioning = toPartitioning(FLAGS_partitioning);
+ return options;
}
std::shared_ptr<VeloxShuffleWriter> createShuffleWriter(
Runtime* runtime,
- std::unique_ptr<PartitionWriter> partitionWriter) {
- auto options = ShuffleWriterOptions{};
- options.partitioning = gluten::toPartitioning(FLAGS_partitioning);
- if (FLAGS_rss || FLAGS_shuffle_writer == "rss_sort") {
- options.shuffleWriterType = gluten::ShuffleWriterType::kRssSortShuffle;
- } else if (FLAGS_shuffle_writer == "sort") {
- options.shuffleWriterType = gluten::ShuffleWriterType::kSortShuffle;
- }
+ std::shared_ptr<PartitionWriter> partitionWriter) {
+ auto shuffleWriterOptions = createShuffleWriterOptions();
auto shuffleWriter =
- runtime->createShuffleWriter(FLAGS_shuffle_partitions, std::move(partitionWriter), std::move(options));
+ runtime->createShuffleWriter(FLAGS_shuffle_partitions, std::move(partitionWriter), shuffleWriterOptions);
+ return std::dynamic_pointer_cast<VeloxShuffleWriter>(shuffleWriter);
+}
- return std::reinterpret_pointer_cast<VeloxShuffleWriter>(shuffleWriter);
+std::shared_ptr<ShuffleReader> createShuffleReader(Runtime* runtime, const std::shared_ptr<arrow::Schema>& schema) {
+ auto readerOptions = ShuffleReaderOptions{};
+ readerOptions.shuffleWriterType = ShuffleWriter::stringToType(FLAGS_shuffle_writer),
+ setCompressionTypeFromFlag(readerOptions.compressionType, readerOptions.codecBackend);
+ return runtime->createShuffleReader(schema, readerOptions);
}
void populateWriterMetrics(
@@ -257,8 +288,7 @@ void runShuffle(
const std::string& dataFileDir) {
GLUTEN_ASSIGN_OR_THROW(auto dataFile, gluten::createTempShuffleFile(dataFileDir));
- auto partitionWriterOptions = createPartitionWriterOptions();
- auto partitionWriter = createPartitionWriter(runtime, partitionWriterOptions, dataFile, localDirs);
+ auto partitionWriter = createPartitionWriter(runtime, dataFile, localDirs);
auto shuffleWriter = createShuffleWriter(runtime, std::move(partitionWriter));
listener->setShuffleWriter(shuffleWriter.get());
@@ -278,15 +308,9 @@ void runShuffle(
populateWriterMetrics(shuffleWriter, totalTime, writerMetrics);
- if (readAfterWrite && cSchema) {
- auto readerOptions = ShuffleReaderOptions{};
- readerOptions.shuffleWriterType = shuffleWriter->options().shuffleWriterType;
- readerOptions.compressionType = partitionWriterOptions.compressionType;
- readerOptions.codecBackend = partitionWriterOptions.codecBackend;
-
- std::shared_ptr<arrow::Schema> schema =
- gluten::arrowGetOrThrow(arrow::ImportSchema(reinterpret_cast<struct ArrowSchema*>(cSchema.get())));
- auto reader = runtime->createShuffleReader(schema, readerOptions);
+ if (readAfterWrite && cSchema != nullptr) {
+ const auto schema = arrowGetOrThrow(arrow::ImportSchema(cSchema.get()));
+ const auto reader = createShuffleReader(runtime, schema);
GLUTEN_ASSIGN_OR_THROW(auto in, arrow::io::ReadableFile::Open(dataFile));
// Read all partitions.
diff --git a/cpp/velox/compute/VeloxRuntime.cc b/cpp/velox/compute/VeloxRuntime.cc
index f9f3e127e5..1c32dcb923 100644
--- a/cpp/velox/compute/VeloxRuntime.cc
+++ b/cpp/velox/compute/VeloxRuntime.cc
@@ -214,17 +214,12 @@ std::shared_ptr<RowToColumnarConverter> VeloxRuntime::createRow2ColumnarConverte
}
std::shared_ptr<ShuffleWriter> VeloxRuntime::createShuffleWriter(
- int numPartitions,
- std::unique_ptr<PartitionWriter> partitionWriter,
- ShuffleWriterOptions options) {
+ int32_t numPartitions,
+ const std::shared_ptr<PartitionWriter>& partitionWriter,
+ const std::shared_ptr<ShuffleWriterOptions>& options) {
GLUTEN_ASSIGN_OR_THROW(
std::shared_ptr<ShuffleWriter> shuffleWriter,
- VeloxShuffleWriter::create(
- options.shuffleWriterType,
- numPartitions,
- std::move(partitionWriter),
- std::move(options),
- memoryManager()));
+ VeloxShuffleWriter::create(options->shuffleWriterType, numPartitions, partitionWriter, options, memoryManager()));
return shuffleWriter;
}
diff --git a/cpp/velox/compute/VeloxRuntime.h b/cpp/velox/compute/VeloxRuntime.h
index 0a46accb43..72475429ea 100644
--- a/cpp/velox/compute/VeloxRuntime.h
+++ b/cpp/velox/compute/VeloxRuntime.h
@@ -64,8 +64,8 @@ class VeloxRuntime final : public Runtime {
std::shared_ptr<ShuffleWriter> createShuffleWriter(
int numPartitions,
- std::unique_ptr<PartitionWriter> partitionWriter,
- ShuffleWriterOptions options) override;
+ const std::shared_ptr<PartitionWriter>& partitionWriter,
+ const std::shared_ptr<ShuffleWriterOptions>& options) override;
Metrics* getMetrics(ColumnarBatchIterator* rawIter, int64_t exportNanos) override {
auto iter = static_cast<WholeStageResultIterator*>(rawIter);
diff --git a/cpp/velox/jni/VeloxJniWrapper.cc b/cpp/velox/jni/VeloxJniWrapper.cc
index c60b36c423..ca1b4b0c1f 100644
--- a/cpp/velox/jni/VeloxJniWrapper.cc
+++ b/cpp/velox/jni/VeloxJniWrapper.cc
@@ -30,14 +30,13 @@
#include "jni/JniFileSystem.h"
#include "memory/VeloxColumnarBatch.h"
#include "memory/VeloxMemoryManager.h"
+#include "shuffle/rss/RssPartitionWriter.h"
#include "substrait/SubstraitToVeloxPlanValidator.h"
#include "utils/ObjectStore.h"
#include "utils/VeloxBatchResizer.h"
#include "velox/common/base/BloomFilter.h"
#include "velox/common/file/FileSystems.h"
-#include <iostream>
-
using namespace gluten;
using namespace facebook;
@@ -495,6 +494,92 @@ JNIEXPORT void JNICALL Java_org_apache_gluten_monitor_VeloxMemoryProfiler_stop(
JNI_METHOD_END()
}
+JNIEXPORT jlong JNICALL
+Java_org_apache_gluten_vectorized_CelebornPartitionWriterJniWrapper_createPartitionWriter( // NOLINT
+ JNIEnv* env,
+ jobject wrapper,
+ jint numPartitions,
+ jstring codecJstr,
+ jstring codecBackendJstr,
+ jint compressionLevel,
+ jint compressionBufferSize,
+ jint pushBufferMaxSize,
+ jlong sortBufferMaxSize,
+ jobject partitionPusher) {
+ JNI_METHOD_START
+ JavaVM* vm;
+ if (env->GetJavaVM(&vm) != JNI_OK) {
+ throw GlutenException("Unable to get JavaVM instance");
+ }
+
+ const auto ctx = getRuntime(env, wrapper);
+
+ jclass celebornPartitionPusherClass =
+ createGlobalClassReferenceOrError(env, "Lorg/apache/spark/shuffle/CelebornPartitionPusher;");
+ jmethodID celebornPushPartitionDataMethod =
+ getMethodIdOrError(env, celebornPartitionPusherClass, "pushPartitionData", "(I[BI)I");
+ std::shared_ptr<JavaRssClient> celebornClient =
+ std::make_shared<JavaRssClient>(vm, partitionPusher, celebornPushPartitionDataMethod);
+
+ auto partitionWriterOptions = std::make_shared<RssPartitionWriterOptions>(
+ compressionBufferSize,
+ pushBufferMaxSize > 0 ? pushBufferMaxSize : kDefaultPushMemoryThreshold,
+ sortBufferMaxSize > 0 ? sortBufferMaxSize : kDefaultSortBufferThreshold);
+
+ auto partitionWriter = std::make_shared<RssPartitionWriter>(
+ numPartitions,
+ createArrowIpcCodec(getCompressionType(env, codecJstr), getCodecBackend(env, codecBackendJstr), compressionLevel),
+ ctx->memoryManager(),
+ partitionWriterOptions,
+ celebornClient);
+
+ return ctx->saveObject(partitionWriter);
+ JNI_METHOD_END(kInvalidObjectHandle)
+}
+
+JNIEXPORT jlong JNICALL
+Java_org_apache_gluten_vectorized_UnifflePartitionWriterJniWrapper_createPartitionWriter( // NOLINT
+ JNIEnv* env,
+ jobject wrapper,
+ jint numPartitions,
+ jstring codecJstr,
+ jstring codecBackendJstr,
+ jint compressionLevel,
+ jint compressionBufferSize,
+ jint pushBufferMaxSize,
+ jlong sortBufferMaxSize,
+ jobject partitionPusher) {
+ JNI_METHOD_START
+ JavaVM* vm;
+ if (env->GetJavaVM(&vm) != JNI_OK) {
+ throw GlutenException("Unable to get JavaVM instance");
+ }
+
+ const auto ctx = getRuntime(env, wrapper);
+
+ jclass unifflePartitionPusherClass =
+ createGlobalClassReferenceOrError(env, "Lorg/apache/spark/shuffle/writer/PartitionPusher;");
+ jmethodID unifflePushPartitionDataMethod =
+ getMethodIdOrError(env, unifflePartitionPusherClass, "pushPartitionData", "(I[BI)I");
+ std::shared_ptr<JavaRssClient> uniffleClient =
+ std::make_shared<JavaRssClient>(vm, partitionPusher, unifflePushPartitionDataMethod);
+
+ auto partitionWriterOptions = std::make_shared<RssPartitionWriterOptions>(
+ compressionBufferSize,
+ pushBufferMaxSize > 0 ? pushBufferMaxSize : kDefaultPushMemoryThreshold,
+ sortBufferMaxSize > 0 ? sortBufferMaxSize : kDefaultSortBufferThreshold);
+
+ auto partitionWriter = std::make_shared<RssPartitionWriter>(
+ numPartitions,
+ createArrowIpcCodec(getCompressionType(env, codecJstr), getCodecBackend(env, codecBackendJstr), compressionLevel),
+ ctx->memoryManager(),
+ partitionWriterOptions,
+ uniffleClient);
+
+ return ctx->saveObject(partitionWriter);
+ JNI_METHOD_END(kInvalidObjectHandle)
+}
+
#ifdef __cplusplus
}
#endif
diff --git a/cpp/velox/shuffle/VeloxHashShuffleWriter.cc b/cpp/velox/shuffle/VeloxHashShuffleWriter.cc
index 2098332484..e75e710a77 100644
--- a/cpp/velox/shuffle/VeloxHashShuffleWriter.cc
+++ b/cpp/velox/shuffle/VeloxHashShuffleWriter.cc
@@ -155,19 +155,22 @@ arrow::Status collectFlatVectorBuffer<facebook::velox::TypeKind::VARBINARY>(
arrow::Result<std::shared_ptr<VeloxShuffleWriter>> VeloxHashShuffleWriter::create(
uint32_t numPartitions,
- std::unique_ptr<PartitionWriter> partitionWriter,
- ShuffleWriterOptions options,
+ const std::shared_ptr<PartitionWriter>& partitionWriter,
+ const std::shared_ptr<ShuffleWriterOptions>& options,
MemoryManager* memoryManager) {
- std::shared_ptr<VeloxHashShuffleWriter> res(
- new VeloxHashShuffleWriter(numPartitions, std::move(partitionWriter), std::move(options), memoryManager));
- RETURN_NOT_OK(res->init());
- return res;
+ if (auto hashOptions = std::dynamic_pointer_cast<HashShuffleWriterOptions>(options)) {
+ std::shared_ptr<VeloxHashShuffleWriter> res(
+ new VeloxHashShuffleWriter(numPartitions, partitionWriter, hashOptions, memoryManager));
+ RETURN_NOT_OK(res->init());
+ return res;
+ }
+ return arrow::Status::Invalid("Error casting ShuffleWriterOptions to HashShuffleWriterOptions. ");
} // namespace gluten
arrow::Status VeloxHashShuffleWriter::init() {
// pre-allocated buffer size for each partition, unit is row count
// when partitioner is SinglePart, partial variables don`t need init
- if (options_.partitioning != Partitioning::kSingle) {
+ if (partitioning_ != Partitioning::kSingle) {
partition2RowCount_.resize(numPartitions_);
partitionBufferSize_.resize(numPartitions_);
partition2RowOffsetBase_.resize(numPartitions_ + 1);
@@ -204,7 +207,7 @@ arrow::Status VeloxHashShuffleWriter::initPartitions() {
}
void VeloxHashShuffleWriter::setPartitionBufferSize(uint32_t newSize) {
- options_.bufferSize = newSize;
+ splitBufferSize_ = newSize;
}
arrow::Result<std::shared_ptr<arrow::Buffer>> VeloxHashShuffleWriter::generateComplexTypeBuffers(
@@ -231,7 +234,7 @@ arrow::Result<std::shared_ptr<arrow::Buffer>> VeloxHashShuffleWriter::generateCo
}
arrow::Status VeloxHashShuffleWriter::write(std::shared_ptr<ColumnarBatch> cb, int64_t memLimit) {
- if (options_.partitioning == Partitioning::kSingle) {
+ if (partitioning_ == Partitioning::kSingle) {
auto veloxColumnBatch = VeloxColumnarBatch::from(veloxPool_.get(), cb);
VELOX_CHECK_NOT_NULL(veloxColumnBatch);
auto& rv = *veloxColumnBatch->getFlattenedRowVector();
@@ -258,7 +261,7 @@ arrow::Status VeloxHashShuffleWriter::write(std::shared_ptr<ColumnarBatch> cb, i
ARROW_ASSIGN_OR_RAISE(buffers.back(), generateComplexTypeBuffers(rowVector));
}
RETURN_NOT_OK(evictBuffers(0, rv.size(), std::move(buffers), false));
- } else if (options_.partitioning == Partitioning::kRange) {
+ } else if (partitioning_ == Partitioning::kRange) {
auto veloxColumnBatch = VeloxColumnarBatch::from(veloxPool_.get(), cb);
VELOX_CHECK_NOT_NULL(veloxColumnBatch);
const int32_t numColumns = veloxColumnBatch->numColumns();
@@ -332,7 +335,7 @@ arrow::Status VeloxHashShuffleWriter::partitioningAndDoSplit(facebook::velox::Ro
arrow::Status VeloxHashShuffleWriter::stop() {
setSplitState(SplitState::kStopEvict);
- if (options_.partitioning != Partitioning::kSingle) {
+ if (partitioning_ != Partitioning::kSingle) {
for (auto pid = 0; pid < numPartitions_; ++pid) {
PartitionScopeGuard stopEvictGuard(partitionBufferInUse_, pid);
RETURN_NOT_OK(evictPartitionBuffers(pid, false));
@@ -810,8 +813,8 @@ arrow::Status VeloxHashShuffleWriter::initFromRowVector(const facebook::velox::R
inline bool VeloxHashShuffleWriter::beyondThreshold(uint32_t partitionId, uint32_t newSize) {
auto currentBufferSize = partitionBufferSize_[partitionId];
- return newSize > (1 + options_.bufferReallocThreshold) * currentBufferSize ||
- newSize < (1 - options_.bufferReallocThreshold) * currentBufferSize;
+ return newSize > (1 + splitBufferReallocThreshold_) * currentBufferSize ||
+ newSize < (1 - splitBufferReallocThreshold_) * currentBufferSize;
}
void VeloxHashShuffleWriter::calculateSimpleColumnBytes() {
@@ -868,8 +871,8 @@ uint32_t VeloxHashShuffleWriter::calculatePartitionBufferSize(const facebook::ve
}
uint64_t preAllocRowCnt =
- memLimit > 0 && bytesPerRow > 0 ? memLimit / bytesPerRow / numPartitions_ >> 2 : options_.bufferSize;
- preAllocRowCnt = std::min(preAllocRowCnt, (uint64_t)options_.bufferSize);
+ memLimit > 0 && bytesPerRow > 0 ? memLimit / bytesPerRow / numPartitions_ >> 2 : splitBufferSize_;
+ preAllocRowCnt = std::min(preAllocRowCnt, static_cast<uint64_t>(splitBufferSize_));
DLOG(INFO) << "Calculated partition buffer size - memLimit: " << memLimit << ", bytesPerRow: " << bytesPerRow
<< ", preAllocRowCnt: " << preAllocRowCnt << std::endl;
@@ -1393,14 +1396,14 @@ bool VeloxHashShuffleWriter::shrinkPartitionBuffersAfterSpill() const {
// PartitionBufferGuard to prevent the target partition from being shrunk.
// The reclaim order is spill->shrink, because the partition buffers can be reused.
// SinglePartitioning doesn't maintain partition buffers.
- return options_.partitioning != Partitioning::kSingle &&
+ return partitioning_ != Partitioning::kSingle &&
(splitState_ == SplitState::kSplit || splitState_ == SplitState::kInit || splitState_ == SplitState::kStopEvict);
}
bool VeloxHashShuffleWriter::evictPartitionBuffersAfterSpill() const {
// If OOM triggered by other operators, the splitState_ is SplitState::kInit.
// The last resort is to evict the partition buffers to reclaim more space.
- return options_.partitioning != Partitioning::kSingle && splitState_ == SplitState::kInit;
+ return partitioning_ != Partitioning::kSingle && splitState_ == SplitState::kInit;
}
arrow::Result<uint32_t> VeloxHashShuffleWriter::partitionBufferSizeAfterShrink(uint32_t partitionId) const {
diff --git a/cpp/velox/shuffle/VeloxHashShuffleWriter.h b/cpp/velox/shuffle/VeloxHashShuffleWriter.h
index e8f37d89f6..06e13db773 100644
--- a/cpp/velox/shuffle/VeloxHashShuffleWriter.h
+++ b/cpp/velox/shuffle/VeloxHashShuffleWriter.h
@@ -124,8 +124,8 @@ class VeloxHashShuffleWriter : public VeloxShuffleWriter {
static arrow::Result<std::shared_ptr<VeloxShuffleWriter>> create(
uint32_t numPartitions,
- std::unique_ptr<PartitionWriter> partitionWriter,
- ShuffleWriterOptions options,
+ const std::shared_ptr<PartitionWriter>& partitionWriter,
+ const std::shared_ptr<ShuffleWriterOptions>& options,
MemoryManager* memoryManager);
arrow::Status write(std::shared_ptr<ColumnarBatch> cb, int64_t memLimit) override;
@@ -191,10 +191,12 @@ class VeloxHashShuffleWriter : public VeloxShuffleWriter {
private:
VeloxHashShuffleWriter(
uint32_t numPartitions,
- std::unique_ptr<PartitionWriter> partitionWriter,
- ShuffleWriterOptions options,
+ const std::shared_ptr<PartitionWriter>& partitionWriter,
+ const std::shared_ptr<HashShuffleWriterOptions>& options,
MemoryManager* memoryManager)
- : VeloxShuffleWriter(numPartitions, std::move(partitionWriter), std::move(options), memoryManager) {}
+ : VeloxShuffleWriter(numPartitions, partitionWriter, options, memoryManager),
+ splitBufferSize_(options->splitBufferSize),
+ splitBufferReallocThreshold_(options->splitBufferReallocThreshold) {}
arrow::Status init();
@@ -301,6 +303,9 @@ class VeloxHashShuffleWriter : public VeloxShuffleWriter {
arrow::Status partitioningAndDoSplit(facebook::velox::RowVectorPtr rv, int64_t memLimit);
+ int32_t splitBufferSize_;
+ double splitBufferReallocThreshold_;
+
std::shared_ptr<arrow::Schema> schema_;
// Column index, partition id, buffers.
diff --git a/cpp/velox/shuffle/VeloxRssSortShuffleWriter.cc b/cpp/velox/shuffle/VeloxRssSortShuffleWriter.cc
index bb495ddf35..98b4b95ef4 100644
--- a/cpp/velox/shuffle/VeloxRssSortShuffleWriter.cc
+++ b/cpp/velox/shuffle/VeloxRssSortShuffleWriter.cc
@@ -31,14 +31,17 @@ namespace gluten {
arrow::Result<std::shared_ptr<VeloxShuffleWriter>> VeloxRssSortShuffleWriter::create(
uint32_t numPartitions,
- std::unique_ptr<PartitionWriter> partitionWriter,
- ShuffleWriterOptions options,
+ const std::shared_ptr<PartitionWriter>& partitionWriter,
+ const std::shared_ptr<ShuffleWriterOptions>& options,
MemoryManager* memoryManager) {
- std::shared_ptr<VeloxRssSortShuffleWriter> res(
- new VeloxRssSortShuffleWriter(numPartitions, std::move(partitionWriter), std::move(options), memoryManager));
- RETURN_NOT_OK(res->init());
- return res;
-} // namespace gluten
+ if (auto rssOptions = std::dynamic_pointer_cast<RssSortShuffleWriterOptions>(options)) {
+ std::shared_ptr<VeloxRssSortShuffleWriter> res(
+ new VeloxRssSortShuffleWriter(numPartitions, partitionWriter, rssOptions, memoryManager));
+ RETURN_NOT_OK(res->init());
+ return res;
+ }
+ return arrow::Status::Invalid("Error casting ShuffleWriterOptions to RssSortShuffleWriterOptions.");
+}
arrow::Status VeloxRssSortShuffleWriter::init() {
rowVectorIndexMap_.reserve(numPartitions_);
@@ -57,18 +60,18 @@ arrow::Status VeloxRssSortShuffleWriter::doSort(facebook::velox::RowVectorPtr rv
batches_.clear();
currentInputColumnBytes_ = 0;
}
- setSortState(SortState::kSortInit);
+ setSortState(RssSortState::kSortInit);
return arrow::Status::OK();
}
arrow::Status VeloxRssSortShuffleWriter::write(std::shared_ptr<ColumnarBatch> cb, int64_t /* memLimit */) {
- if (options_.partitioning == Partitioning::kSingle) {
+ if (partitioning_ == Partitioning::kSingle) {
auto veloxColumnBatch = VeloxColumnarBatch::from(veloxPool_.get(), cb);
VELOX_CHECK_NOT_NULL(veloxColumnBatch);
auto rv = veloxColumnBatch->getFlattenedRowVector();
RETURN_NOT_OK(initFromRowVector(*rv.get()));
- RETURN_NOT_OK(doSort(rv, partitionWriter_.get()->options().sortBufferMaxSize));
- } else if (options_.partitioning == Partitioning::kRange) {
+ RETURN_NOT_OK(doSort(rv, sortBufferMaxSize_));
+ } else if (partitioning_ == Partitioning::kRange) {
auto veloxColumnBatch = VeloxColumnarBatch::from(veloxPool_.get(), cb);
VELOX_CHECK_NOT_NULL(veloxColumnBatch);
const int32_t numColumns = veloxColumnBatch->numColumns();
@@ -76,7 +79,7 @@ arrow::Status VeloxRssSortShuffleWriter::write(std::shared_ptr<ColumnarBatch> cb
auto pidBatch = veloxColumnBatch->select(veloxPool_.get(), {0});
auto pidArr = getFirstColumn(*(pidBatch->getRowVector()));
START_TIMING(cpuWallTimingList_[CpuWallTimingCompute]);
- setSortState(SortState::kSort);
+ setSortState(RssSortState::kSort);
RETURN_NOT_OK(partitioner_->compute(pidArr, pidBatch->numRows(), batches_.size(), rowVectorIndexMap_));
END_TIMING();
std::vector<int32_t> range;
@@ -86,7 +89,7 @@ arrow::Status VeloxRssSortShuffleWriter::write(std::shared_ptr<ColumnarBatch> cb
auto rvBatch = veloxColumnBatch->select(veloxPool_.get(), range);
auto rv = rvBatch->getFlattenedRowVector();
RETURN_NOT_OK(initFromRowVector(*rv.get()));
- RETURN_NOT_OK(doSort(rv, partitionWriter_.get()->options().sortBufferMaxSize));
+ RETURN_NOT_OK(doSort(rv, sortBufferMaxSize_));
} else {
auto veloxColumnBatch = VeloxColumnarBatch::from(veloxPool_.get(), cb);
VELOX_CHECK_NOT_NULL(veloxColumnBatch);
@@ -97,19 +100,19 @@ arrow::Status VeloxRssSortShuffleWriter::write(std::shared_ptr<ColumnarBatch> cb
if (partitioner_->hasPid()) {
auto pidArr = getFirstColumn(*rv);
START_TIMING(cpuWallTimingList_[CpuWallTimingCompute]);
- setSortState(SortState::kSort);
+ setSortState(RssSortState::kSort);
RETURN_NOT_OK(partitioner_->compute(pidArr, rv->size(), batches_.size(), rowVectorIndexMap_));
END_TIMING();
auto strippedRv = getStrippedRowVector(*rv);
RETURN_NOT_OK(initFromRowVector(*strippedRv));
- RETURN_NOT_OK(doSort(strippedRv, partitionWriter_.get()->options().sortBufferMaxSize));
+ RETURN_NOT_OK(doSort(strippedRv, sortBufferMaxSize_));
} else {
RETURN_NOT_OK(initFromRowVector(*rv));
START_TIMING(cpuWallTimingList_[CpuWallTimingCompute]);
- setSortState(SortState::kSort);
+ setSortState(RssSortState::kSort);
RETURN_NOT_OK(partitioner_->compute(nullptr, rv->size(), batches_.size(), rowVectorIndexMap_));
END_TIMING();
- RETURN_NOT_OK(doSort(rv, partitionWriter_.get()->options().sortBufferMaxSize));
+ RETURN_NOT_OK(doSort(rv, sortBufferMaxSize_));
}
}
return arrow::Status::OK();
@@ -124,15 +127,15 @@ arrow::Status VeloxRssSortShuffleWriter::evictBatch(uint32_t partitionId) {
auto payload, BlockPayload::fromBuffers(Payload::kRaw, 0, {std::move(arrowBuffer)}, nullptr, nullptr, nullptr));
RETURN_NOT_OK(partitionWriter_->evict(partitionId, std::move(payload), stopped_));
batch_ = std::make_unique<facebook::velox::VectorStreamGroup>(veloxPool_.get(), serde_.get());
- batch_->createStreamTree(rowType_, options_.bufferSize, &serdeOptions_);
+ batch_->createStreamTree(rowType_, splitBufferSize_, &serdeOptions_);
return arrow::Status::OK();
}
arrow::Status VeloxRssSortShuffleWriter::evictRowVector(uint32_t partitionId) {
int32_t accumulatedRows = 0;
- const int32_t maxRowsPerBatch = options_.bufferSize;
+ const int32_t maxRowsPerBatch = splitBufferSize_;
- if (options_.partitioning != Partitioning::kSingle) {
+ if (partitioning_ != Partitioning::kSingle) {
if (auto it = rowVectorIndexMap_.find(partitionId); it != rowVectorIndexMap_.end()) {
const auto& rowIndices = it->second;
VELOX_DCHECK(!rowIndices.empty());
@@ -195,7 +198,7 @@ arrow::Status VeloxRssSortShuffleWriter::stop() {
currentInputColumnBytes_ = 0;
{
SCOPED_TIMER(cpuWallTimingList_[CpuWallTimingStop]);
- setSortState(SortState::kSortStop);
+ setSortState(RssSortState::kSortStop);
RETURN_NOT_OK(partitionWriter_->stop(&metrics_));
}
@@ -207,9 +210,9 @@ arrow::Status VeloxRssSortShuffleWriter::stop() {
arrow::Status VeloxRssSortShuffleWriter::initFromRowVector(const facebook::velox::RowVector& rv) {
if (!rowType_) {
rowType_ = facebook::velox::asRowType(rv.type());
- serdeOptions_ = {false, arrowCompressionTypeToVelox(partitionWriter_->options().compressionType)};
+ serdeOptions_ = {false, compressionKind_};
batch_ = std::make_unique<facebook::velox::VectorStreamGroup>(veloxPool_.get(), serde_.get());
- batch_->createStreamTree(rowType_, options_.bufferSize, &serdeOptions_);
+ batch_->createStreamTree(rowType_, splitBufferSize_, &serdeOptions_);
}
return arrow::Status::OK();
}
@@ -221,7 +224,7 @@ arrow::Status VeloxRssSortShuffleWriter::reclaimFixedSize(int64_t size, int64_t*
}
EvictGuard evictGuard{evictState_};
- if (sortState_ == SortState::kSortInit) {
+ if (sortState_ == RssSortState::kSortInit) {
for (auto pid = 0; pid < numPartitions(); ++pid) {
RETURN_NOT_OK(evictRowVector(pid));
}
@@ -248,7 +251,7 @@ void VeloxRssSortShuffleWriter::stat() const {
#endif
}
-void VeloxRssSortShuffleWriter::setSortState(SortState state) {
+void VeloxRssSortShuffleWriter::setSortState(RssSortState state) {
sortState_ = state;
}
diff --git a/cpp/velox/shuffle/VeloxRssSortShuffleWriter.h b/cpp/velox/shuffle/VeloxRssSortShuffleWriter.h
index a9b1ae1fd1..ec35609103 100644
--- a/cpp/velox/shuffle/VeloxRssSortShuffleWriter.h
+++ b/cpp/velox/shuffle/VeloxRssSortShuffleWriter.h
@@ -44,17 +44,18 @@
#include "shuffle/Utils.h"
#include "utils/Print.h"
+#include "utils/VeloxArrowUtils.h"
namespace gluten {
-enum SortState { kSortInit, kSort, kSortStop };
+enum RssSortState { kSortInit, kSort, kSortStop };
class VeloxRssSortShuffleWriter final : public VeloxShuffleWriter {
public:
static arrow::Result<std::shared_ptr<VeloxShuffleWriter>> create(
uint32_t numPartitions,
- std::unique_ptr<PartitionWriter> partitionWriter,
- ShuffleWriterOptions options,
+ const std::shared_ptr<PartitionWriter>& partitionWriter,
+ const std::shared_ptr<ShuffleWriterOptions>& options,
MemoryManager* memoryManager);
arrow::Status write(std::shared_ptr<ColumnarBatch> cb, int64_t memLimit) override;
@@ -68,16 +69,19 @@ class VeloxRssSortShuffleWriter final : public VeloxShuffleWriter {
private:
VeloxRssSortShuffleWriter(
uint32_t numPartitions,
- std::unique_ptr<PartitionWriter> partitionWriter,
- ShuffleWriterOptions options,
+ const std::shared_ptr<PartitionWriter>& partitionWriter,
+ const std::shared_ptr<RssSortShuffleWriterOptions>& options,
MemoryManager* memoryManager)
- : VeloxShuffleWriter(numPartitions, std::move(partitionWriter), std::move(options), memoryManager) {}
+ : VeloxShuffleWriter(numPartitions, partitionWriter, options, memoryManager),
+ splitBufferSize_(options->splitBufferSize),
+ sortBufferMaxSize_(options->sortBufferMaxSize),
+ compressionKind_(arrowCompressionTypeToVelox(options->compressionType)) {}
arrow::Status init();
arrow::Status initFromRowVector(const facebook::velox::RowVector& rv);
- void setSortState(SortState state);
+ void setSortState(RssSortState state);
arrow::Status doSort(facebook::velox::RowVectorPtr rv, int64_t /* memLimit */);
@@ -85,6 +89,10 @@ class VeloxRssSortShuffleWriter final : public VeloxShuffleWriter {
void stat() const;
+ int32_t splitBufferSize_;
+ int64_t sortBufferMaxSize_;
+ facebook::velox::common::CompressionKind compressionKind_;
+
facebook::velox::RowTypePtr rowType_;
std::unique_ptr<facebook::velox::VectorStreamGroup> batch_;
@@ -99,7 +107,7 @@ class VeloxRssSortShuffleWriter final : public VeloxShuffleWriter {
uint32_t currentInputColumnBytes_ = 0;
- SortState sortState_{kSortInit};
+ RssSortState sortState_{kSortInit};
bool stopped_{false};
}; // class VeloxSortBasedShuffleWriter
diff --git a/cpp/velox/shuffle/VeloxShuffleWriter.cc b/cpp/velox/shuffle/VeloxShuffleWriter.cc
index 66fa2be2db..c45d941972 100644
--- a/cpp/velox/shuffle/VeloxShuffleWriter.cc
+++ b/cpp/velox/shuffle/VeloxShuffleWriter.cc
@@ -25,20 +25,17 @@ namespace gluten {
arrow::Result<std::shared_ptr<VeloxShuffleWriter>> VeloxShuffleWriter::create(
ShuffleWriterType type,
uint32_t numPartitions,
- std::unique_ptr<PartitionWriter> partitionWriter,
- ShuffleWriterOptions options,
+ const std::shared_ptr<PartitionWriter>& partitionWriter,
+ const std::shared_ptr<ShuffleWriterOptions>& options,
MemoryManager* memoryManager) {
std::shared_ptr<VeloxShuffleWriter> shuffleWriter;
switch (type) {
case ShuffleWriterType::kHashShuffle:
- return VeloxHashShuffleWriter::create(
- numPartitions, std::move(partitionWriter), std::move(options), memoryManager);
+ return VeloxHashShuffleWriter::create(numPartitions, std::move(partitionWriter), options, memoryManager);
case ShuffleWriterType::kSortShuffle:
- return VeloxSortShuffleWriter::create(
- numPartitions, std::move(partitionWriter), std::move(options), memoryManager);
+ return VeloxSortShuffleWriter::create(numPartitions, std::move(partitionWriter), options, memoryManager);
case ShuffleWriterType::kRssSortShuffle:
- return VeloxRssSortShuffleWriter::create(
- numPartitions, std::move(partitionWriter), std::move(options), memoryManager);
+ return VeloxRssSortShuffleWriter::create(numPartitions, std::move(partitionWriter), options, memoryManager);
default:
return arrow::Status::Invalid("Unsupported shuffle writer type: ", typeToString(type));
}
diff --git a/cpp/velox/shuffle/VeloxShuffleWriter.h b/cpp/velox/shuffle/VeloxShuffleWriter.h
index db1ab54b91..8a7b0bf187 100644
--- a/cpp/velox/shuffle/VeloxShuffleWriter.h
+++ b/cpp/velox/shuffle/VeloxShuffleWriter.h
@@ -30,9 +30,7 @@
#include "velox/vector/VectorStream.h"
#include <arrow/array/util.h>
-#include <arrow/ipc/writer.h>
#include <arrow/memory_pool.h>
-#include <arrow/record_batch.h>
#include <arrow/result.h>
#include <arrow/type.h>
@@ -52,8 +50,8 @@ class VeloxShuffleWriter : public ShuffleWriter {
static arrow::Result<std::shared_ptr<VeloxShuffleWriter>> create(
ShuffleWriterType type,
uint32_t numPartitions,
- std::unique_ptr<PartitionWriter> partitionWriter,
- ShuffleWriterOptions options,
+ const std::shared_ptr<PartitionWriter>& partitionWriter,
+ const std::shared_ptr<ShuffleWriterOptions>& options,
MemoryManager* memoryManager);
facebook::velox::RowVectorPtr getStrippedRowVector(const facebook::velox::RowVector& rv) {
@@ -118,14 +116,14 @@ class VeloxShuffleWriter : public ShuffleWriter {
protected:
VeloxShuffleWriter(
uint32_t numPartitions,
- std::unique_ptr<PartitionWriter> partitionWriter,
- ShuffleWriterOptions options,
+ const std::shared_ptr<PartitionWriter>& partitionWriter,
+ const std::shared_ptr<ShuffleWriterOptions>& options,
MemoryManager* memoryManager)
- : ShuffleWriter(numPartitions, std::move(options)),
+ : ShuffleWriter(numPartitions, options->partitioning),
partitionBufferPool_(memoryManager->createArrowMemoryPool("VeloxShuffleWriter.partitionBufferPool")),
veloxPool_(dynamic_cast<VeloxMemoryManager*>(memoryManager)->getLeafMemoryPool()),
- partitionWriter_(std::move(partitionWriter)) {
- partitioner_ = Partitioner::make(options_.partitioning, numPartitions_, options_.startPartitionId);
+ partitionWriter_(partitionWriter) {
+ partitioner_ = Partitioner::make(options->partitioning, numPartitions_, options->startPartitionId);
arenas_.resize(numPartitions);
serdeOptions_.useLosslessTimestamp = true;
}
@@ -140,7 +138,7 @@ class VeloxShuffleWriter : public ShuffleWriter {
// PartitionWriter must destruct before partitionBufferPool_, as it may hold buffers allocated by
// partitionBufferPool_.
- std::unique_ptr<PartitionWriter> partitionWriter_;
+ std::shared_ptr<PartitionWriter> partitionWriter_;
std::shared_ptr<Partitioner> partitioner_;
diff --git a/cpp/velox/shuffle/VeloxSortShuffleWriter.cc b/cpp/velox/shuffle/VeloxSortShuffleWriter.cc
index 7936c9c4e3..3bcdacf3e9 100644
--- a/cpp/velox/shuffle/VeloxSortShuffleWriter.cc
+++ b/cpp/velox/shuffle/VeloxSortShuffleWriter.cc
@@ -49,21 +49,27 @@ std::pair<uint32_t, uint32_t> extractPageNumberAndOffset(uint64_t compactRowId)
arrow::Result<std::shared_ptr<VeloxShuffleWriter>> VeloxSortShuffleWriter::create(
uint32_t numPartitions,
- std::unique_ptr<PartitionWriter> partitionWriter,
- ShuffleWriterOptions options,
+ const std::shared_ptr<PartitionWriter>& partitionWriter,
+ const std::shared_ptr<ShuffleWriterOptions>& options,
MemoryManager* memoryManager) {
- std::shared_ptr<VeloxSortShuffleWriter> writer(
- new VeloxSortShuffleWriter(numPartitions, std::move(partitionWriter), std::move(options), memoryManager));
- RETURN_NOT_OK(writer->init());
- return writer;
+ if (auto sortOptions = std::dynamic_pointer_cast<SortShuffleWriterOptions>(options)) {
+ std::shared_ptr<VeloxSortShuffleWriter> writer(
+ new VeloxSortShuffleWriter(numPartitions, partitionWriter, sortOptions, memoryManager));
+ RETURN_NOT_OK(writer->init());
+ return writer;
+ }
+ return arrow::Status::Invalid("Error casting ShuffleWriterOptions to SortShuffleWriterOptions.");
}
VeloxSortShuffleWriter::VeloxSortShuffleWriter(
uint32_t numPartitions,
- std::unique_ptr<PartitionWriter> partitionWriter,
- ShuffleWriterOptions options,
+ const std::shared_ptr<PartitionWriter>& partitionWriter,
+ const std::shared_ptr<SortShuffleWriterOptions>& options,
MemoryManager* memoryManager)
- : VeloxShuffleWriter(numPartitions, std::move(partitionWriter), std::move(options), memoryManager) {}
+ : VeloxShuffleWriter(numPartitions, partitionWriter, options, memoryManager),
+ useRadixSort_(options->useRadixSort),
+ initialSortBufferSize_(options->initialSortBufferSize),
+ diskWriteBufferSize_(options->diskWriteBufferSize) {}
arrow::Status VeloxSortShuffleWriter::write(std::shared_ptr<ColumnarBatch> cb, int64_t memLimit) {
ARROW_ASSIGN_OR_RAISE(auto rv, getPeeledRowVector(cb));
@@ -100,13 +106,12 @@ arrow::Status VeloxSortShuffleWriter::reclaimFixedSize(int64_t size, int64_t* ac
arrow::Status VeloxSortShuffleWriter::init() {
ARROW_RETURN_IF(
- options_.partitioning == Partitioning::kSingle,
+ partitioning_ == Partitioning::kSingle,
arrow::Status::Invalid("VeloxSortShuffleWriter doesn't support single partition."));
allocateMinimalArray();
// In Spark, sortedBuffer_ memory and compressionBuffer_ memory are pre-allocated and counted into executor
// memory overhead. To align with Spark, we use arrow::default_memory_pool() to avoid counting these memory in Gluten.
- ARROW_ASSIGN_OR_RAISE(
- sortedBuffer_, arrow::AllocateBuffer(options_.diskWriteBufferSize, arrow::default_memory_pool()));
+ ARROW_ASSIGN_OR_RAISE(sortedBuffer_, arrow::AllocateBuffer(diskWriteBufferSize_, arrow::default_memory_pool()));
sortedBufferPtr_ = sortedBuffer_->mutable_data();
return arrow::Status::OK();
}
@@ -120,7 +125,7 @@ void VeloxSortShuffleWriter::initRowType(const facebook::velox::RowVectorPtr& rv
arrow::Result<facebook::velox::RowVectorPtr> VeloxSortShuffleWriter::getPeeledRowVector(
const std::shared_ptr<ColumnarBatch>& cb) {
- if (options_.partitioning == Partitioning::kRange) {
+ if (partitioning_ == Partitioning::kRange) {
auto veloxColumnBatch = VeloxColumnarBatch::from(veloxPool_.get(), cb);
VELOX_CHECK_NOT_NULL(veloxColumnBatch);
const int32_t numColumns = veloxColumnBatch->numColumns();
@@ -227,7 +232,7 @@ arrow::Status VeloxSortShuffleWriter::evictAllPartitions() {
int32_t begin = 0;
{
ScopedTimer timer(&sortTime_);
- if (options_.useRadixSort) {
+ if (useRadixSort_) {
begin = RadixSort::sort(arrayPtr_, arraySize_, numRecords, kPartitionIdStartByteIndex, kPartitionIdEndByteIndex);
} else {
std::sort(arrayPtr_, arrayPtr_ + numRecords);
@@ -284,20 +289,20 @@ arrow::Status VeloxSortShuffleWriter::evictPartition(uint32_t partitionId, size_
auto pageIndex = extractPageNumberAndOffset(arrayPtr_[index]);
addr = pageAddresses_[pageIndex.first] + pageIndex.second;
recordSize = *(reinterpret_cast<RowSizeType*>(addr)) + sizeof(RowSizeType);
- if (offset + recordSize > options_.diskWriteBufferSize && offset > 0) {
+ if (offset + recordSize > diskWriteBufferSize_ && offset > 0) {
sortTime.stop();
RETURN_NOT_OK(evictPartitionInternal(partitionId, sortedBufferPtr_, offset));
sortTime.start();
begin = index;
offset = 0;
}
- if (recordSize > static_cast<uint32_t>(options_.diskWriteBufferSize)) {
+ if (recordSize > static_cast<uint32_t>(diskWriteBufferSize_)) {
// Split large rows.
sortTime.stop();
RowSizeType bytes = 0;
auto* buffer = reinterpret_cast<uint8_t*>(addr);
while (bytes < recordSize) {
- auto rawLength = std::min<RowSizeType>((uint32_t)options_.diskWriteBufferSize, recordSize - bytes);
+ auto rawLength = std::min<RowSizeType>(static_cast<uint32_t>(diskWriteBufferSize_), recordSize - bytes);
// Use numRows = 0 to represent a part of row.
RETURN_NOT_OK(evictPartitionInternal(partitionId, buffer + bytes, rawLength));
bytes += rawLength;
@@ -400,10 +405,10 @@ void VeloxSortShuffleWriter::growArrayIfNecessary(uint32_t rows) {
uint32_t VeloxSortShuffleWriter::newArraySize(uint32_t rows) {
auto newSize = arraySize_;
- auto usableCapacity = options_.useRadixSort ? newSize / 2 : newSize;
+ auto usableCapacity = useRadixSort_ ? newSize / 2 : newSize;
while (offset_ + rows > usableCapacity) {
newSize <<= 1;
- usableCapacity = options_.useRadixSort ? newSize / 2 : newSize;
+ usableCapacity = useRadixSort_ ? newSize / 2 : newSize;
}
return newSize;
}
@@ -431,8 +436,8 @@ int64_t VeloxSortShuffleWriter::totalC2RTime() const {
}
void VeloxSortShuffleWriter::allocateMinimalArray() {
- auto array = facebook::velox::AlignedBuffer::allocate<char>(
- options_.initialSortBufferSize * sizeof(uint64_t), veloxPool_.get());
+ auto array =
+ facebook::velox::AlignedBuffer::allocate<char>(initialSortBufferSize_ * sizeof(uint64_t), veloxPool_.get());
setUpArray(std::move(array));
}
diff --git a/cpp/velox/shuffle/VeloxSortShuffleWriter.h b/cpp/velox/shuffle/VeloxSortShuffleWriter.h
index 0580d3d810..6fc08434bd 100644
--- a/cpp/velox/shuffle/VeloxSortShuffleWriter.h
+++ b/cpp/velox/shuffle/VeloxSortShuffleWriter.h
@@ -33,8 +33,8 @@ class VeloxSortShuffleWriter final : public VeloxShuffleWriter {
static arrow::Result<std::shared_ptr<VeloxShuffleWriter>> create(
uint32_t numPartitions,
- std::unique_ptr<PartitionWriter> partitionWriter,
- ShuffleWriterOptions options,
+ const std::shared_ptr<PartitionWriter>& partitionWriter,
+ const std::shared_ptr<ShuffleWriterOptions>& options,
MemoryManager* memoryManager);
arrow::Status write(std::shared_ptr<ColumnarBatch> cb, int64_t memLimit) override;
@@ -52,8 +52,8 @@ class VeloxSortShuffleWriter final : public VeloxShuffleWriter {
private:
VeloxSortShuffleWriter(
uint32_t numPartitions,
- std::unique_ptr<PartitionWriter> partitionWriter,
- ShuffleWriterOptions options,
+ const std::shared_ptr<PartitionWriter>& partitionWriter,
+ const std::shared_ptr<SortShuffleWriterOptions>& options,
MemoryManager* memoryManager);
arrow::Status init();
@@ -93,6 +93,10 @@ class VeloxSortShuffleWriter final : public VeloxShuffleWriter {
void updateSpillMetrics(const std::unique_ptr<InMemoryPayload>& payload);
+ bool useRadixSort_;
+ int32_t initialSortBufferSize_;
+ int32_t diskWriteBufferSize_;
+
// Stores compact row id -> row
facebook::velox::BufferPtr array_;
uint64_t* arrayPtr_;
diff --git a/cpp/velox/tests/RuntimeTest.cc b/cpp/velox/tests/RuntimeTest.cc
index f611a060f0..2086012e5c 100644
--- a/cpp/velox/tests/RuntimeTest.cc
+++ b/cpp/velox/tests/RuntimeTest.cc
@@ -79,9 +79,9 @@ class DummyRuntime final : public Runtime {
throw GlutenException("Not yet implemented");
}
std::shared_ptr<ShuffleWriter> createShuffleWriter(
- int numPartitions,
- std::unique_ptr<PartitionWriter> partitionWriter,
- ShuffleWriterOptions) override {
+ int32_t numPartitions,
+ const std::shared_ptr<PartitionWriter>& partitionWriter,
+ const std::shared_ptr<ShuffleWriterOptions>&) override {
throw GlutenException("Not yet implemented");
}
Metrics* getMetrics(ColumnarBatchIterator* rawIter, int64_t exportNanos) override {
diff --git a/cpp/velox/tests/VeloxShuffleWriterSpillTest.cc b/cpp/velox/tests/VeloxShuffleWriterSpillTest.cc
index d4f4704525..45d25eb260 100644
--- a/cpp/velox/tests/VeloxShuffleWriterSpillTest.cc
+++ b/cpp/velox/tests/VeloxShuffleWriterSpillTest.cc
@@ -15,6 +15,8 @@
* limitations under the License.
*/
+#include <duckdb/common/enums/compression_type.hpp>
+
#include "config/GlutenConfig.h"
#include "shuffle/VeloxHashShuffleWriter.h"
#include "tests/VeloxShuffleWriterTestBase.h"
@@ -49,14 +51,29 @@ class VeloxHashShuffleWriterSpillTest : public VeloxShuffleWriterTestBase, publi
setUpTestData();
}
- std::shared_ptr<VeloxShuffleWriter> createShuffleWriter(uint32_t numPartitions) override {
- auto partitionWriter = createPartitionWriter(
- PartitionWriterType::kLocal, numPartitions, dataFile_, localDirs_, partitionWriterOptions_);
+ std::shared_ptr<VeloxShuffleWriter> createHashShuffleWriter(
+ uint32_t numPartitions,
+ const std::shared_ptr<HashShuffleWriterOptions>& shuffleWriterOptions,
+ std::shared_ptr<LocalPartitionWriterOptions> partitionWriterOptions = nullptr,
+ arrow::Compression::type compressionType = arrow::Compression::type::LZ4_FRAME) {
+ if (partitionWriterOptions == nullptr) {
+ partitionWriterOptions = std::make_shared<LocalPartitionWriterOptions>();
+ }
+
+ std::unique_ptr<arrow::util::Codec> codec;
+ if (compressionType == arrow::Compression::type::UNCOMPRESSED) {
+ codec = nullptr;
+ } else {
+ GLUTEN_ASSIGN_OR_THROW(codec, arrow::util::Codec::Create(compressionType));
+ }
+
+ auto partitionWriter = std::make_shared<LocalPartitionWriter>(
+ numPartitions, std::move(codec), getDefaultMemoryManager(), partitionWriterOptions, dataFile_, localDirs_);
GLUTEN_ASSIGN_OR_THROW(
auto shuffleWriter,
VeloxHashShuffleWriter::create(
- numPartitions, std::move(partitionWriter), shuffleWriterOptions_, getDefaultMemoryManager()));
+ numPartitions, partitionWriter, shuffleWriterOptions, getDefaultMemoryManager()));
return shuffleWriter;
}
@@ -81,9 +98,9 @@ class VeloxHashShuffleWriterSpillTest : public VeloxShuffleWriterTestBase, publi
};
TEST_F(VeloxHashShuffleWriterSpillTest, memoryLeak) {
- shuffleWriterOptions_.bufferSize = 4;
-
- auto shuffleWriter = createShuffleWriter(2);
+ auto shuffleWriterOptions = std::make_shared<HashShuffleWriterOptions>();
+ shuffleWriterOptions->splitBufferSize = 4;
+ auto shuffleWriter = createHashShuffleWriter(2, shuffleWriterOptions);
ASSERT_NOT_OK(splitRowVector(*shuffleWriter, inputVector1_));
ASSERT_NOT_OK(splitRowVector(*shuffleWriter, inputVector2_));
@@ -100,9 +117,9 @@ TEST_F(VeloxHashShuffleWriterSpillTest, memoryLeak) {
}
TEST_F(VeloxHashShuffleWriterSpillTest, spillFailWithOutOfMemory) {
- shuffleWriterOptions_.bufferSize = 4;
-
- auto shuffleWriter = createShuffleWriter(2);
+ auto shuffleWriterOptions = std::make_shared<HashShuffleWriterOptions>();
+ shuffleWriterOptions->splitBufferSize = 4;
+ auto shuffleWriter = createHashShuffleWriter(2, shuffleWriterOptions);
listener_->updateLimit(0L);
listener_->setShuffleWriter(shuffleWriter.get());
@@ -116,10 +133,9 @@ TEST_F(VeloxHashShuffleWriterSpillTest, spillFailWithOutOfMemory) {
}
TEST_F(VeloxHashShuffleWriterSpillTest, kInit) {
- shuffleWriterOptions_.bufferSize = 4;
-
- const int32_t numPartitions = 2;
- auto shuffleWriter = createShuffleWriter(numPartitions);
+ auto shuffleWriterOptions = std::make_shared<HashShuffleWriterOptions>();
+ shuffleWriterOptions->splitBufferSize = 4;
+ auto shuffleWriter = createHashShuffleWriter(2, shuffleWriterOptions);
// Test spill all partition buffers.
{
@@ -163,7 +179,7 @@ TEST_F(VeloxHashShuffleWriterSpillTest, kInit) {
ASSERT_NOT_OK(splitRowVector(*shuffleWriter, inputVector1_));
// Clear buffers then the size after shrink will be 0.
- for (auto pid = 0; pid < numPartitions; ++pid) {
+ for (auto pid = 0; pid < shuffleWriter->numPartitions(); ++pid) {
ASSERT_NOT_OK(shuffleWriter->evictPartitionBuffers(pid, true));
}
@@ -187,10 +203,10 @@ TEST_F(VeloxHashShuffleWriterSpillTest, kInit) {
}
TEST_F(VeloxHashShuffleWriterSpillTest, kInitSingle) {
- shuffleWriterOptions_.partitioning = Partitioning::kSingle;
- shuffleWriterOptions_.bufferSize = 4;
-
- auto shuffleWriter = createShuffleWriter(2);
+ auto shuffleWriterOptions = std::make_shared<HashShuffleWriterOptions>();
+ shuffleWriterOptions->partitioning = Partitioning::kSingle;
+ shuffleWriterOptions->splitBufferSize = 4;
+ auto shuffleWriter = createHashShuffleWriter(1, shuffleWriterOptions);
{
ASSERT_NOT_OK(splitRowVector(*shuffleWriter, inputVector1_));
@@ -209,9 +225,9 @@ TEST_F(VeloxHashShuffleWriterSpillTest, kInitSingle) {
}
TEST_F(VeloxHashShuffleWriterSpillTest, kSplit) {
- shuffleWriterOptions_.bufferSize = 4;
-
- auto shuffleWriter = createShuffleWriter(2);
+ auto shuffleWriterOptions = std::make_shared<HashShuffleWriterOptions>();
+ shuffleWriterOptions->splitBufferSize = 4;
+ auto shuffleWriter = createHashShuffleWriter(2, shuffleWriterOptions);
listener_->setShuffleWriter(shuffleWriter.get());
@@ -234,9 +250,10 @@ TEST_F(VeloxHashShuffleWriterSpillTest, kSplit) {
}
TEST_F(VeloxHashShuffleWriterSpillTest, kSplitSingle) {
- shuffleWriterOptions_.partitioning = Partitioning::kSingle;
-
- auto shuffleWriter = createShuffleWriter(1);
+ auto shuffleWriterOptions = std::make_shared<HashShuffleWriterOptions>();
+ shuffleWriterOptions->partitioning = Partitioning::kSingle;
+ shuffleWriterOptions->splitBufferSize = 4;
+ auto shuffleWriter = createHashShuffleWriter(1, shuffleWriterOptions);
listener_->setShuffleWriter(shuffleWriter.get());
@@ -253,12 +270,13 @@ TEST_F(VeloxHashShuffleWriterSpillTest, kSplitSingle) {
}
TEST_F(VeloxHashShuffleWriterSpillTest, kStop) {
- shuffleWriterOptions_.bufferSize = 4096;
+ auto shuffleWriterOptions = std::make_shared<HashShuffleWriterOptions>();
// Force compression.
- partitionWriterOptions_.compressionThreshold = 0;
- partitionWriterOptions_.mergeThreshold = 0;
+ auto partitionWriterOptions = std::make_shared<LocalPartitionWriterOptions>();
+ partitionWriterOptions->compressionThreshold = 0;
+ partitionWriterOptions->mergeThreshold = 0;
- auto shuffleWriter = createShuffleWriter(2);
+ auto shuffleWriter = createHashShuffleWriter(2, shuffleWriterOptions, partitionWriterOptions);
for (int i = 0; i < 10; ++i) {
ASSERT_NOT_OK(splitRowVector(*shuffleWriter, inputVector1_));
@@ -278,13 +296,13 @@ TEST_F(VeloxHashShuffleWriterSpillTest, kStop) {
}
TEST_F(VeloxHashShuffleWriterSpillTest, kStopComplex) {
- shuffleWriterOptions_.bufferSize = 4096;
-
+ auto shuffleWriterOptions = std::make_shared<HashShuffleWriterOptions>();
// Force compression.
- partitionWriterOptions_.compressionThreshold = 0;
- partitionWriterOptions_.mergeThreshold = 0;
+ auto partitionWriterOptions = std::make_shared<LocalPartitionWriterOptions>();
+ partitionWriterOptions->compressionThreshold = 0;
+ partitionWriterOptions->mergeThreshold = 0;
- auto shuffleWriter = createShuffleWriter(2);
+ auto shuffleWriter = createHashShuffleWriter(2, shuffleWriterOptions, partitionWriterOptions);
for (int i = 0; i < 3; ++i) {
ASSERT_NOT_OK(splitRowVector(*shuffleWriter, inputVectorComplex_));
@@ -311,9 +329,9 @@ TEST_F(VeloxHashShuffleWriterSpillTest, kStopComplex) {
}
TEST_F(VeloxHashShuffleWriterSpillTest, evictPartitionBuffers) {
- shuffleWriterOptions_.bufferSize = 4;
-
- auto shuffleWriter = createShuffleWriter(2);
+ auto shuffleWriterOptions = std::make_shared<HashShuffleWriterOptions>();
+ shuffleWriterOptions->splitBufferSize = 4;
+ auto shuffleWriter = createHashShuffleWriter(2, shuffleWriterOptions);
ASSERT_NOT_OK(splitRowVector(*shuffleWriter, inputVector1_));
@@ -329,9 +347,9 @@ TEST_F(VeloxHashShuffleWriterSpillTest, evictPartitionBuffers) {
}
TEST_F(VeloxHashShuffleWriterSpillTest, kUnevictableSingle) {
- shuffleWriterOptions_.partitioning = Partitioning::kSingle;
-
- auto shuffleWriter = createShuffleWriter(1);
+ auto shuffleWriterOptions = std::make_shared<HashShuffleWriterOptions>();
+ shuffleWriterOptions->partitioning = Partitioning::kSingle;
+ auto shuffleWriter = createHashShuffleWriter(1, shuffleWriterOptions);
ASSERT_NOT_OK(splitRowVector(*shuffleWriter, inputVector1_));
@@ -348,10 +366,13 @@ TEST_F(VeloxHashShuffleWriterSpillTest, kUnevictableSingle) {
}
TEST_F(VeloxHashShuffleWriterSpillTest, resizeBinaryBufferTriggerSpill) {
- shuffleWriterOptions_.bufferReallocThreshold = 1;
- partitionWriterOptions_.compressionType = arrow::Compression::type::UNCOMPRESSED;
+ auto shuffleWriterOptions = std::make_shared<HashShuffleWriterOptions>();
+ shuffleWriterOptions->splitBufferReallocThreshold = 1;
+
+ auto partitionWriterOptions = std::make_shared<LocalPartitionWriterOptions>();
- auto shuffleWriter = createShuffleWriter(1);
+ auto shuffleWriter =
+ createHashShuffleWriter(1, shuffleWriterOptions, partitionWriterOptions, arrow::Compression::type::UNCOMPRESSED);
// Split first input vector. Large average string length.
ASSERT_NOT_OK(splitRowVector(*shuffleWriter, inputVectorLargeBinary1_));
diff --git a/cpp/velox/tests/VeloxShuffleWriterTest.cc b/cpp/velox/tests/VeloxShuffleWriterTest.cc
index 04178f5c43..cb6f1b0feb 100644
--- a/cpp/velox/tests/VeloxShuffleWriterTest.cc
+++ b/cpp/velox/tests/VeloxShuffleWriterTest.cc
@@ -143,6 +143,35 @@ std::vector<ShuffleTestParams> getTestParams() {
return params;
}
+
+std::shared_ptr<PartitionWriter> createPartitionWriter(
+ PartitionWriterType partitionWriterType,
+ uint32_t numPartitions,
+ const std::string& dataFile,
+ const std::vector<std::string>& localDirs,
+ arrow::Compression::type compressionType,
+ int32_t mergeBufferSize,
+ int32_t compressionThreshold) {
+ GLUTEN_ASSIGN_OR_THROW(auto codec, arrow::util::Codec::Create(compressionType));
+ switch (partitionWriterType) {
+ case PartitionWriterType::kLocal: {
+ auto options = std::make_shared<LocalPartitionWriterOptions>();
+ options->mergeBufferSize = mergeBufferSize;
+ options->compressionThreshold = compressionThreshold;
+ return std::make_shared<LocalPartitionWriter>(
+ numPartitions, std::move(codec), getDefaultMemoryManager(), options, dataFile, std::move(localDirs));
+ }
+ case PartitionWriterType::kRss: {
+ auto options = std::make_shared<RssPartitionWriterOptions>();
+ auto rssClient = std::make_unique<LocalRssClient>(dataFile);
+ return std::make_shared<RssPartitionWriter>(
+ numPartitions, std::move(codec), getDefaultMemoryManager(), options, std::move(rssClient));
+ }
+ default:
+ throw GlutenException("Unsupported partition writer type.");
+ }
+}
+
} // namespace
class VeloxShuffleWriterTestEnvironment : public ::testing::Environment {
@@ -157,21 +186,68 @@ class VeloxShuffleWriterTestEnvironment : public ::testing::Environment {
};
class VeloxShuffleWriterTest : public ::testing::TestWithParam<ShuffleTestParams>, public VeloxShuffleWriterTestBase {
- public:
- arrow::Status initShuffleWriterOptions() {
- ShuffleTestParams params = GetParam();
+ protected:
+ static std::shared_ptr<ShuffleWriterOptions> createShuffleWriterOptions(
+ Partitioning partitioning,
+ int32_t splitBufferSize) {
+ std::shared_ptr<ShuffleWriterOptions> options;
+ const auto& params = GetParam();
+ switch (params.shuffleWriterType) {
+ case ShuffleWriterType::kHashShuffle: {
+ auto hashOptions = std::make_shared<HashShuffleWriterOptions>();
+ hashOptions->splitBufferSize = splitBufferSize;
+ options = hashOptions;
+ } break;
+ case ShuffleWriterType::kSortShuffle: {
+ auto sortOptions = std::make_shared<SortShuffleWriterOptions>();
+ sortOptions->diskWriteBufferSize = params.diskWriteBufferSize;
+ sortOptions->useRadixSort = params.useRadixSort;
+ options = sortOptions;
+ } break;
+ case ShuffleWriterType::kRssSortShuffle: {
+ auto rssOptions = std::make_shared<RssSortShuffleWriterOptions>();
+ rssOptions->splitBufferSize = splitBufferSize;
+ rssOptions->compressionType = params.compressionType;
+ options = rssOptions;
+ } break;
+ default:
+ throw GlutenException("Unreachable");
+ }
+ options->partitioning = partitioning;
- shuffleWriterOptions_.useRadixSort = params.useRadixSort;
- shuffleWriterOptions_.diskWriteBufferSize = params.diskWriteBufferSize;
+ return options;
+ }
+
+ virtual std::shared_ptr<ShuffleWriterOptions> defaultShuffleWriterOptions() = 0;
+
+ std::shared_ptr<VeloxShuffleWriter> createShuffleWriter(
+ uint32_t numPartitions,
+ std::shared_ptr<ShuffleWriterOptions> shuffleWriterOptions = nullptr) {
+ if (shuffleWriterOptions == nullptr) {
+ shuffleWriterOptions = defaultShuffleWriterOptions();
+ }
- partitionWriterOptions_.compressionType = params.compressionType;
- partitionWriterOptions_.compressionThreshold = params.compressionThreshold;
- partitionWriterOptions_.mergeBufferSize = params.mergeBufferSize;
+ const auto partitionWriter = createPartitionWriter(
+ GetParam().partitionWriterType,
+ numPartitions,
+ dataFile_,
+ localDirs_,
+ GetParam().compressionType,
+ GetParam().mergeBufferSize,
+ GetParam().compressionThreshold);
+
+ GLUTEN_ASSIGN_OR_THROW(
+ auto shuffleWriter,
+ VeloxShuffleWriter::create(
+ GetParam().shuffleWriterType,
+ numPartitions,
+ partitionWriter,
+ shuffleWriterOptions,
+ getDefaultMemoryManager()));
- return arrow::Status::OK();
+ return shuffleWriter;
}
- protected:
void SetUp() override {
std::cout << "Running test with param: " << GetParam().toString() << std::endl;
VeloxShuffleWriterTestBase::setUpTestData();
@@ -233,7 +309,7 @@ class VeloxShuffleWriterTest : public ::testing::TestWithParam<ShuffleTestParams
void shuffleWriteReadMultiBlocks(
VeloxShuffleWriter& shuffleWriter,
int32_t expectPartitionLength,
- const std::vector<std::vector<facebook::velox::RowVectorPtr>>& expectedVectors) {
+ const std::vector<std::vector<RowVectorPtr>>& expectedVectors) {
ASSERT_NOT_OK(shuffleWriter.stop());
checkFileExists(dataFile_);
@@ -249,11 +325,10 @@ class VeloxShuffleWriterTest : public ::testing::TestWithParam<ShuffleTestParams
if (expectedVectors[i].size() == 0) {
ASSERT_EQ(lengths[i], 0);
} else {
- std::vector<facebook::velox::RowVectorPtr> deserializedVectors;
+ std::vector<RowVectorPtr> deserializedVectors;
GLUTEN_ASSIGN_OR_THROW(
auto in, arrow::io::RandomAccessFile::GetStream(file_, i == 0 ? 0 : lengths[i - 1], lengths[i]));
- getRowVectors(
- partitionWriterOptions_.compressionType, asRowType(expectedVectors[i][0]->type()), deserializedVectors, in);
+ getRowVectors(GetParam().compressionType, asRowType(expectedVectors[i][0]->type()), deserializedVectors, in);
const auto expectedVector = mergeRowVectors(expectedVectors[i]);
const auto deserializedVector = mergeRowVectors(deserializedVectors);
@@ -264,9 +339,9 @@ class VeloxShuffleWriterTest : public ::testing::TestWithParam<ShuffleTestParams
void testShuffleRoundTrip(
VeloxShuffleWriter& shuffleWriter,
- std::vector<std::shared_ptr<ColumnarBatch>> batches,
+ const std::vector<std::shared_ptr<ColumnarBatch>>& batches,
int32_t expectPartitionLength,
- std::vector<std::vector<facebook::velox::RowVectorPtr>> expectedVectors) {
+ const std::vector<std::vector<RowVectorPtr>>& expectedVectors) {
for (const auto& batch : batches) {
ASSERT_NOT_OK(shuffleWriter.write(batch, ShuffleWriter::kMinMemLimit));
}
@@ -292,23 +367,8 @@ class VeloxShuffleWriterTest : public ::testing::TestWithParam<ShuffleTestParams
class SinglePartitioningShuffleWriterTest : public VeloxShuffleWriterTest {
protected:
- std::shared_ptr<VeloxShuffleWriter> createShuffleWriter(uint32_t) override {
- shuffleWriterOptions_.partitioning = Partitioning::kSingle;
- shuffleWriterOptions_.bufferSize = 10;
-
- auto partitionWriter =
- createPartitionWriter(GetParam().partitionWriterType, 1, dataFile_, localDirs_, partitionWriterOptions_);
-
- GLUTEN_ASSIGN_OR_THROW(
- auto shuffleWriter,
- VeloxShuffleWriter::create(
- GetParam().shuffleWriterType,
- 1,
- std::move(partitionWriter),
- shuffleWriterOptions_,
- getDefaultMemoryManager()));
-
- return shuffleWriter;
+ std::shared_ptr<ShuffleWriterOptions> defaultShuffleWriterOptions() override {
+ return createShuffleWriterOptions(Partitioning::kSingle, 10);
}
};
@@ -323,29 +383,14 @@ class HashPartitioningShuffleWriterTest : public VeloxShuffleWriterTest {
hashInputVector2_ = makeRowVector(children2_);
}
- std::shared_ptr<VeloxShuffleWriter> createShuffleWriter(uint32_t numPartitions) override {
- shuffleWriterOptions_.partitioning = Partitioning::kHash;
- shuffleWriterOptions_.bufferSize = 4;
-
- auto partitionWriter = createPartitionWriter(
- GetParam().partitionWriterType, numPartitions, dataFile_, localDirs_, partitionWriterOptions_);
-
- GLUTEN_ASSIGN_OR_THROW(
- auto shuffleWriter,
- VeloxShuffleWriter::create(
- GetParam().shuffleWriterType,
- numPartitions,
- std::move(partitionWriter),
- shuffleWriterOptions_,
- getDefaultMemoryManager()));
-
- return shuffleWriter;
+ std::shared_ptr<ShuffleWriterOptions> defaultShuffleWriterOptions() override {
+ return createShuffleWriterOptions(Partitioning::kHash, 4);
}
std::vector<uint32_t> hashPartitionIds_{1, 2};
- facebook::velox::RowVectorPtr hashInputVector1_;
- facebook::velox::RowVectorPtr hashInputVector2_;
+ RowVectorPtr hashInputVector1_;
+ RowVectorPtr hashInputVector2_;
};
class RangePartitioningShuffleWriterTest : public VeloxShuffleWriterTest {
@@ -364,23 +409,8 @@ class RangePartitioningShuffleWriterTest : public VeloxShuffleWriterTest {
pool(), {std::make_shared<VeloxColumnarBatch>(pid2), std::make_shared<VeloxColumnarBatch>(rangeVector2)});
}
- std::shared_ptr<VeloxShuffleWriter> createShuffleWriter(uint32_t numPartitions) override {
- shuffleWriterOptions_.partitioning = Partitioning::kRange;
- shuffleWriterOptions_.bufferSize = 4;
-
- auto partitionWriter = createPartitionWriter(
- GetParam().partitionWriterType, numPartitions, dataFile_, localDirs_, partitionWriterOptions_);
-
- GLUTEN_ASSIGN_OR_THROW(
- auto shuffleWriter,
- VeloxShuffleWriter::create(
- GetParam().shuffleWriterType,
- numPartitions,
- std::move(partitionWriter),
- shuffleWriterOptions_,
- getDefaultMemoryManager()));
-
- return shuffleWriter;
+ std::shared_ptr<ShuffleWriterOptions> defaultShuffleWriterOptions() override {
+ return createShuffleWriterOptions(Partitioning::kRange, 4);
}
std::shared_ptr<ColumnarBatch> compositeBatch1_;
@@ -389,23 +419,8 @@ class RangePartitioningShuffleWriterTest : public VeloxShuffleWriterTest {
class RoundRobinPartitioningShuffleWriterTest : public VeloxShuffleWriterTest {
protected:
- std::shared_ptr<VeloxShuffleWriter> createShuffleWriter(uint32_t numPartitions) override {
- shuffleWriterOptions_.partitioning = Partitioning::kRoundRobin;
- shuffleWriterOptions_.bufferSize = 4096;
-
- auto partitionWriter = createPartitionWriter(
- GetParam().partitionWriterType, numPartitions, dataFile_, localDirs_, partitionWriterOptions_);
-
- GLUTEN_ASSIGN_OR_THROW(
- auto shuffleWriter,
- VeloxShuffleWriter::create(
- GetParam().shuffleWriterType,
- numPartitions,
- std::move(partitionWriter),
- shuffleWriterOptions_,
- getDefaultMemoryManager()));
-
- return shuffleWriter;
+ std::shared_ptr<ShuffleWriterOptions> defaultShuffleWriterOptions() override {
+ return createShuffleWriterOptions(Partitioning::kRoundRobin, 4096);
}
};
@@ -414,8 +429,6 @@ TEST_P(SinglePartitioningShuffleWriterTest, single) {
return;
}
- ASSERT_NOT_OK(initShuffleWriterOptions());
-
// Split 1 RowVector.
{
auto shuffleWriter = createShuffleWriter(1);
@@ -466,8 +479,6 @@ TEST_P(SinglePartitioningShuffleWriterTest, single) {
}
TEST_P(HashPartitioningShuffleWriterTest, hashPart1Vector) {
- ASSERT_NOT_OK(initShuffleWriterOptions());
-
// Fixed-length input.
{
auto shuffleWriter = createShuffleWriter(2);
@@ -522,7 +533,6 @@ TEST_P(HashPartitioningShuffleWriterTest, hashPart1Vector) {
}
TEST_P(HashPartitioningShuffleWriterTest, hashPart1VectorComplexType) {
- ASSERT_NOT_OK(initShuffleWriterOptions());
auto shuffleWriter = createShuffleWriter(2);
auto children = childrenComplex_;
children.insert((children.begin()), makeFlatVector<int32_t>({1, 2}));
@@ -534,7 +544,6 @@ TEST_P(HashPartitioningShuffleWriterTest, hashPart1VectorComplexType) {
}
TEST_P(HashPartitioningShuffleWriterTest, hashPart3Vectors) {
- ASSERT_NOT_OK(initShuffleWriterOptions());
auto shuffleWriter = createShuffleWriter(2);
auto blockPid2 = takeRows({inputVector1_, inputVector2_, inputVector1_}, {{1, 2, 3, 4, 8}, {0, 1}, {1, 2, 3, 4, 8}});
@@ -546,7 +555,6 @@ TEST_P(HashPartitioningShuffleWriterTest, hashPart3Vectors) {
TEST_P(HashPartitioningShuffleWriterTest, hashLargeVectors) {
const int32_t expectedMaxBatchSize = 8;
- ASSERT_NOT_OK(initShuffleWriterOptions());
auto shuffleWriter = createShuffleWriter(2);
// calculate maxBatchSize_
@@ -563,7 +571,6 @@ TEST_P(HashPartitioningShuffleWriterTest, hashLargeVectors) {
}
TEST_P(RangePartitioningShuffleWriterTest, range) {
- ASSERT_NOT_OK(initShuffleWriterOptions());
auto shuffleWriter = createShuffleWriter(2);
auto blockPid1 = takeRows({inputVector1_, inputVector2_, inputVector1_}, {{0, 2, 4, 6, 8}, {0}, {0, 2, 4, 6, 8}});
@@ -574,7 +581,6 @@ TEST_P(RangePartitioningShuffleWriterTest, range) {
}
TEST_P(RoundRobinPartitioningShuffleWriterTest, roundRobin) {
- ASSERT_NOT_OK(initShuffleWriterOptions());
auto shuffleWriter = createShuffleWriter(2);
auto blockPid1 = takeRows({inputVector1_, inputVector2_, inputVector1_}, {{0, 2, 4, 6, 8}, {0}, {0, 2, 4, 6, 8}});
@@ -587,9 +593,11 @@ TEST_P(RoundRobinPartitioningShuffleWriterTest, preAllocForceRealloc) {
if (GetParam().shuffleWriterType != ShuffleWriterType::kHashShuffle) {
return;
}
- ASSERT_NOT_OK(initShuffleWriterOptions());
- shuffleWriterOptions_.bufferReallocThreshold = 0; // Force re-alloc on buffer size changed.
- auto shuffleWriter = createShuffleWriter(2);
+
+ auto shuffleWriterOptions = std::make_shared<HashShuffleWriterOptions>();
+ shuffleWriterOptions->splitBufferSize = 4096;
+ shuffleWriterOptions->splitBufferReallocThreshold = 0; // Force re-alloc on buffer size changed.
+ auto shuffleWriter = createShuffleWriter(2, shuffleWriterOptions);
// First spilt no null.
auto inputNoNull = inputVectorNoNull_;
@@ -645,9 +653,10 @@ TEST_P(RoundRobinPartitioningShuffleWriterTest, preAllocForceReuse) {
if (GetParam().shuffleWriterType != ShuffleWriterType::kHashShuffle) {
return;
}
- ASSERT_NOT_OK(initShuffleWriterOptions());
- shuffleWriterOptions_.bufferReallocThreshold = 1; // Force re-alloc on buffer size changed.
- auto shuffleWriter = createShuffleWriter(2);
+ auto shuffleWriterOptions = std::make_shared<HashShuffleWriterOptions>();
+ shuffleWriterOptions->splitBufferSize = 4096;
+ shuffleWriterOptions->splitBufferReallocThreshold = 1; // Force reuse on buffer size changed.
+ auto shuffleWriter = createShuffleWriter(2, shuffleWriterOptions);
// First spilt no null.
auto inputNoNull = inputVectorNoNull_;
@@ -681,7 +690,6 @@ TEST_P(RoundRobinPartitioningShuffleWriterTest, spillVerifyResult) {
return;
}
- ASSERT_NOT_OK(initShuffleWriterOptions());
auto shuffleWriter = createShuffleWriter(2);
ASSERT_NOT_OK(splitRowVector(*shuffleWriter, inputVector1_));
@@ -720,7 +728,6 @@ TEST_P(RoundRobinPartitioningShuffleWriterTest, sortMaxRows) {
if (GetParam().shuffleWriterType != ShuffleWriterType::kSortShuffle) {
return;
}
- ASSERT_NOT_OK(initShuffleWriterOptions());
auto shuffleWriter = createShuffleWriter(2);
// Set memLimit to 0 to force allocate a new buffer for each row.
diff --git a/cpp/velox/tests/VeloxShuffleWriterTestBase.h b/cpp/velox/tests/VeloxShuffleWriterTestBase.h
index 278da23c27..3f1fca58b0 100644
--- a/cpp/velox/tests/VeloxShuffleWriterTestBase.h
+++ b/cpp/velox/tests/VeloxShuffleWriterTestBase.h
@@ -51,20 +51,6 @@ std::string makeString(uint32_t length) {
}
return res;
}
-
-std::unique_ptr<PartitionWriter> createPartitionWriter(
- PartitionWriterType partitionWriterType,
- uint32_t numPartitions,
- const std::string& dataFile,
- const std::vector<std::string>& localDirs,
- const PartitionWriterOptions& options) {
- if (partitionWriterType == PartitionWriterType::kRss) {
- auto rssClient = std::make_unique<LocalRssClient>(dataFile);
- return std::make_unique<RssPartitionWriter>(
- numPartitions, options, getDefaultMemoryManager(), std::move(rssClient));
- }
- return std::make_unique<LocalPartitionWriter>(numPartitions, options, getDefaultMemoryManager(), dataFile, localDirs);
-}
} // namespace
class VeloxShuffleWriterTestBase : public facebook::velox::test::VectorTestBase {
@@ -235,13 +221,8 @@ class VeloxShuffleWriterTestBase : public facebook::velox::test::VectorTestBase
return arrow::Status::OK();
}
- virtual std::shared_ptr<VeloxShuffleWriter> createShuffleWriter(uint32_t numPartitions) = 0;
-
inline static TestAllocationListener* listener_{nullptr};
- ShuffleWriterOptions shuffleWriterOptions_{};
- PartitionWriterOptions partitionWriterOptions_{};
-
std::vector<std::unique_ptr<arrow::internal::TemporaryDir>> tmpDirs_;
std::string dataFile_;
std::vector<std::string> localDirs_;
diff --git a/gluten-arrow/src/main/java/org/apache/gluten/vectorized/LocalPartitionWriterJniWrapper.java b/gluten-arrow/src/main/java/org/apache/gluten/vectorized/LocalPartitionWriterJniWrapper.java
new file mode 100644
index 0000000000..be74df1b8a
--- /dev/null
+++ b/gluten-arrow/src/main/java/org/apache/gluten/vectorized/LocalPartitionWriterJniWrapper.java
@@ -0,0 +1,51 @@
+/*
+ * 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.gluten.vectorized;
+
+import org.apache.gluten.runtime.Runtime;
+import org.apache.gluten.runtime.RuntimeAware;
+
+public class LocalPartitionWriterJniWrapper implements RuntimeAware {
+ private final Runtime runtime;
+
+ private LocalPartitionWriterJniWrapper(Runtime runtime) {
+ this.runtime = runtime;
+ }
+
+ public static LocalPartitionWriterJniWrapper create(Runtime runtime) {
+ return new LocalPartitionWriterJniWrapper(runtime);
+ }
+
+ @Override
+ public long rtHandle() {
+ return runtime.getHandle();
+ }
+
+ public native long createPartitionWriter(
+ int numPartitions,
+ String codec,
+ String codecBackend,
+ int compressionLevel,
+ int compressionBufferSize,
+ int compressionThreshold,
+ int mergeBufferSize,
+ double mergeThreshold,
+ int subDirsPerLocalDir,
+ int shuffleFileBufferSize,
+ String dataFile,
+ String localDirs);
+}
diff --git a/gluten-arrow/src/main/java/org/apache/gluten/vectorized/ShuffleWriterJniWrapper.java b/gluten-arrow/src/main/java/org/apache/gluten/vectorized/ShuffleWriterJniWrapper.java
index 90fea2c11e..8e8b44347f 100644
--- a/gluten-arrow/src/main/java/org/apache/gluten/vectorized/ShuffleWriterJniWrapper.java
+++ b/gluten-arrow/src/main/java/org/apache/gluten/vectorized/ShuffleWriterJniWrapper.java
@@ -37,167 +37,41 @@ public class ShuffleWriterJniWrapper implements RuntimeAware {
return runtime.getHandle();
}
- /**
- * Construct native shuffle writer for shuffled RecordBatch over
- *
- * @param bufferSize size of native buffers held by each partition writer
- * @param mergeBufferSize maximum size of the merged buffer
- * @param mergeThreshold threshold to control whether native partition buffer need to be merged
- * @param codec compression codec
- * @param codecBackend HW backend for offloading compression
- * @param dataFile acquired from spark IndexShuffleBlockResolver
- * @param subDirsPerLocalDir SparkConf spark.diskStore.subDirectories
- * @param localDirs configured local directories where Spark can write files
- * @return native shuffle writer instance handle if created successfully.
- */
- public long make(
- String shortName,
+ public native long createHashShuffleWriter(
int numPartitions,
- int bufferSize,
- int mergeBufferSize,
- double mergeThreshold,
- String codec,
- String codecBackend,
- int compressionLevel,
- int compressionBufferSize,
- int diskWriteBufferSize,
- int bufferCompressThreshold,
- String compressionMode,
- int sortBufferInitialSize,
- boolean useRadixSort,
- String dataFile,
- int subDirsPerLocalDir,
- String localDirs,
- double reallocThreshold,
- long handle,
- long taskAttemptId,
+ String partitioningName,
int startPartitionId,
- String shuffleWriterType) {
- return nativeMake(
- shortName,
- numPartitions,
- bufferSize,
- mergeBufferSize,
- mergeThreshold,
- codec,
- codecBackend,
- compressionLevel,
- compressionBufferSize,
- diskWriteBufferSize,
- bufferCompressThreshold,
- compressionMode,
- sortBufferInitialSize,
- useRadixSort,
- dataFile,
- subDirsPerLocalDir,
- localDirs,
- reallocThreshold,
- handle,
- taskAttemptId,
- startPartitionId,
- 0,
- 0,
- null,
- "local",
- shuffleWriterType);
- }
+ int splitBufferSize,
+ double splitBufferReallocThreshold,
+ long partitionWriterHandle);
- /**
- * Construct RSS native shuffle writer for shuffled RecordBatch over
- *
- * @param bufferSize size of native buffers hold by partition writer
- * @param codec compression codec
- * @return native shuffle writer instance handle if created successfully.
- */
- public long makeForRSS(
- String shortName,
+ public native long createSortShuffleWriter(
int numPartitions,
- int bufferSize,
- String codec,
- int compressionLevel,
- int compressionBufferSize,
+ String partitioningName,
+ int startPartitionId,
int diskWriteBufferSize,
- int bufferCompressThreshold,
- String compressionMode,
- int sortBufferInitialSize,
+ int initialSortBufferSize,
boolean useRadixSort,
- int pushBufferMaxSize,
- long sortBufferMaxSize,
- Object pusher,
- long handle,
- long taskAttemptId,
- int startPartitionId,
- String partitionWriterType,
- String shuffleWriterType,
- double reallocThreshold) {
- return nativeMake(
- shortName,
- numPartitions,
- bufferSize,
- 0,
- 0,
- codec,
- null,
- compressionLevel,
- compressionBufferSize,
- diskWriteBufferSize,
- bufferCompressThreshold,
- compressionMode,
- sortBufferInitialSize,
- useRadixSort,
- null,
- 0,
- null,
- reallocThreshold,
- handle,
- taskAttemptId,
- startPartitionId,
- pushBufferMaxSize,
- sortBufferMaxSize,
- pusher,
- partitionWriterType,
- shuffleWriterType);
- }
+ long partitionWriterHandle);
- public native long nativeMake(
- String shortName,
+ public native long createRssSortShuffleWriter(
int numPartitions,
- int bufferSize,
- int mergeBufferSize,
- double mergeThreshold,
- String codec,
- String codecBackend,
- int compressionLevel,
- int compressionBufferSize,
- int diskWriteBufferSize,
- int bufferCompressThreshold,
- String compressionMode,
- int sortBufferInitialSize,
- boolean useRadixSort,
- String dataFile,
- int subDirsPerLocalDir,
- String localDirs,
- double reallocThreshold,
- long handle,
- long taskAttemptId,
+ String partitioningName,
int startPartitionId,
- int pushBufferMaxSize,
+ int splitBufferSize,
long sortBufferMaxSize,
- Object pusher,
- String partitionWriterType,
- String shuffleWriterType);
+ String codec,
+ long partitionWriterHandle);
/**
- * Evict partition data.
+ * Reclaim memory from the shuffle writer instance. It will first try to shrink allocated memory,
+ * and may trigger a spill if needed.
*
* @param shuffleWriterHandle shuffle writer instance handle
- * @param size expected size to Evict (in bytes)
- * @param callBySelf whether the caller is the shuffle writer itself, true when running out of
- * off-heap memory due to allocations from the evaluator itself
+ * @param size expected size to reclaim (in bytes)
* @return actual spilled size
*/
- public native long nativeEvict(long shuffleWriterHandle, long size, boolean callBySelf)
- throws RuntimeException;
+ public native long reclaim(long shuffleWriterHandle, long size) throws RuntimeException;
/**
* Split one record batch represented by bufAddrs and bufSizes into several batches. The batch is
@@ -205,12 +79,13 @@ public class ShuffleWriterJniWrapper implements RuntimeAware {
*
* @param shuffleWriterHandle shuffle writer instance handle
* @param numRows Rows per batch
- * @param handler handler of Velox Vector
+ * @param columnarBatchHandle handle of Velox Vector
* @param memLimit memory usage limit for the split operation FIXME setting a cap to pool /
* allocator instead
* @return batch bytes.
*/
- public native long write(long shuffleWriterHandle, int numRows, long handler, long memLimit);
+ public native long write(
+ long shuffleWriterHandle, int numRows, long columnarBatchHandle, long memLimit);
/**
* Write the data remained in the buffers hold by native shuffle writer to each partition's
diff --git a/gluten-celeborn/src/main/scala/org/apache/spark/shuffle/CelebornColumnarShuffleWriter.scala b/gluten-celeborn/src/main/scala/org/apache/spark/shuffle/CelebornColumnarShuffleWriter.scala
index 737c42c4ac..31a3c772f8 100644
--- a/gluten-celeborn/src/main/scala/org/apache/spark/shuffle/CelebornColumnarShuffleWriter.scala
+++ b/gluten-celeborn/src/main/scala/org/apache/spark/shuffle/CelebornColumnarShuffleWriter.scala
@@ -106,9 +106,6 @@ abstract class CelebornColumnarShuffleWriter[K, V](
.getOrElse(0)
}
- protected val bufferCompressThreshold: Int =
- GlutenConfig.get.columnarShuffleCompressionThreshold
-
// Are we in the process of stopping? Because map tasks can call stop() with success = true
// and then call stop() with success = false if they get an exception, we want to make sure
// we don't try deleting files, etc twice.
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@gluten.apache.org
For additional commands, e-mail: commits-help@gluten.apache.org