This is an automated email from the ASF dual-hosted git repository.
liuneng1994 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gluten.git
The following commit(s) were added to refs/heads/main by this push:
new ef82ee34a9 [GLUTEN-13811][BOLT] Close the exhausted shuffle stream
before fetching the next block (#13813)
ef82ee34a9 is described below
commit ef82ee34a90daafbdf4603ee19f749c4eadaec1d
Author: Zhang Xiaofeng <[email protected]>
AuthorDate: Mon Nov 30 21:38:49 2026 +0800
[GLUTEN-13811][BOLT] Close the exhausted shuffle stream before fetching the
next block (#13813)
What changes are proposed in this pull request?
Fixes #13811.
#12370 moved responsibility for closing shuffle streams from
ShuffleStreamReader.nextStream() to native consumers so that Velox GPU shuffle
readers could consume multiple streams concurrently. The Bolt reader introduced
in #12454 consumes streams sequentially, but it fetched the next block before
closing the exhausted stream.
Spark releases the fetcher's current block when a stream closes. Once the
fetcher has advanced to block B, closing the stream for block A can release B's
buffer. Reading B can then fail with IllegalReferenceCountException: refCnt: 0,
as observed in TPC-DS Q6 and Q10.
This patch resets the Bolt deserializer at end of stream, closing block A
while it is still the fetcher's current block.
How was this patch tested?
Added BoltShuffleReaderTest.closesCurrentStreamBeforeFetchingNext. It
writes two local shuffle files with BoltShuffleWriter, reads both through the
Bolt shuffle reader, and verifies the values. It also checks that the first
stream is closed before the second file is fetched and that the second stream
is closed before the fetcher reports end of input.
The regression test failed on unpatched main and passed 25 consecutive runs
with the fix. The full TPC-DS suite was not rerun.
Was this patch authored or co-authored using generative AI tooling?
Generated-by: OpenAI Codex (GPT-6)
---
cpp/bolt/shuffle/BoltShuffleReaderWrapper.h | 2 +
cpp/bolt/tests/BoltShuffleReaderTest.cc | 90 +++++++++++++++++++++++++++++
cpp/bolt/tests/CMakeLists.txt | 9 ++-
3 files changed, 99 insertions(+), 2 deletions(-)
diff --git a/cpp/bolt/shuffle/BoltShuffleReaderWrapper.h
b/cpp/bolt/shuffle/BoltShuffleReaderWrapper.h
index 0e81668c3f..81a2a2ec0b 100644
--- a/cpp/bolt/shuffle/BoltShuffleReaderWrapper.h
+++ b/cpp/bolt/shuffle/BoltShuffleReaderWrapper.h
@@ -51,6 +51,8 @@ class BoltShuffleReaderIteratorWrapper : public
ColumnarBatchIterator {
if (nextBatch) {
return std::make_shared<BoltColumnarBatch>(std::move(nextBatch));
} else {
+ // Closing the stream releases the Spark fetcher's current block.
+ deserializer_.reset();
deserializer_ = nextDeserializer_();
}
} while (deserializer_ != nullptr);
diff --git a/cpp/bolt/tests/BoltShuffleReaderTest.cc
b/cpp/bolt/tests/BoltShuffleReaderTest.cc
new file mode 100644
index 0000000000..159c1f9f3a
--- /dev/null
+++ b/cpp/bolt/tests/BoltShuffleReaderTest.cc
@@ -0,0 +1,90 @@
+/*
+ * 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.
+ */
+
+#include <arrow/io/file.h>
+#include <gtest/gtest.h>
+
+#include "bolt/exec/tests/utils/TempDirectoryPath.h"
+#include "bolt/shuffle/sparksql/BoltShuffleWriter.h"
+#include "bolt/vector/tests/utils/VectorMaker.h"
+#include "memory/BoltColumnarBatch.h"
+#include "shuffle/ShuffleReader.h"
+#include "shuffle/ShuffleReaderBase.h"
+
+#include "shuffle/BoltShuffleReaderWrapper.h"
+
+namespace gluten {
+namespace {
+
+using namespace bytedance::bolt::shuffle::sparksql;
+
+TEST(BoltShuffleReaderTest, closesCurrentStreamBeforeFetchingNext) {
+ bytedance::bolt::memory::MemoryManager::testingSetInstance({});
+ auto boltPool = bytedance::bolt::memory::memoryManager()->addLeafPool();
+ auto* pool = arrow::default_memory_pool();
+ ShuffleReaderOptions options;
+ options.compressionType = arrow::Compression::UNCOMPRESSED;
+ options.partitionShortName = "single";
+ options.numPartitions = 1;
+ options.forceShuffleWriterType = 0;
+ BoltShuffleReader reader(arrow::schema({arrow::field("id",
arrow::int64())}), options, pool, boltPool.get());
+
+ const auto tempDir =
bytedance::bolt::exec::test::TempDirectoryPath::create();
+ bytedance::bolt::test::VectorMaker vectors(boltPool.get());
+ ShuffleWriterOptions writerOptions;
+ writerOptions.partitioning = Partitioning::kSingle;
+ auto& partitionOptions = writerOptions.partitionWriterOptions;
+ partitionOptions.numPartitions = 1;
+ partitionOptions.compressionType = options.compressionType;
+ partitionOptions.configuredDirs = {tempDir->getPath()};
+
+ std::vector<std::string> blockFiles;
+ for (int64_t value : {1, 2}) {
+ partitionOptions.dataFile = tempDir->getPath() + "/block-" +
std::to_string(value);
+ auto writer = BoltShuffleWriter::createDefault(writerOptions,
boltPool.get(), pool);
+ auto input = vectors.rowVector({"id"},
{vectors.flatVector<int64_t>({value})});
+ auto status = writer->split(input, 64L << 20);
+ ASSERT_TRUE(status.ok()) << status.ToString();
+ status = writer->stop();
+ ASSERT_TRUE(status.ok()) << status.ToString();
+ blockFiles.push_back(partitionOptions.dataFile);
+ }
+
+ auto nextFile = blockFiles.begin();
+ std::shared_ptr<arrow::io::ReadableFile> currentStream;
+ BoltShuffleReaderIteratorWrapper batches([&]() ->
std::unique_ptr<BoltColumnarBatchDeserializer> {
+ if (currentStream) {
+ EXPECT_TRUE(currentStream->closed()) << "Fetcher advanced before the
current stream closed";
+ }
+ if (nextFile == blockFiles.end()) {
+ return nullptr;
+ }
+ currentStream = arrow::io::ReadableFile::Open(*nextFile++).ValueOrDie();
+ return reader.readStream(currentStream);
+ });
+
+ for (int64_t expected : {1, 2}) {
+ auto batch = std::static_pointer_cast<BoltColumnarBatch>(batches.next());
+ ASSERT_NE(batch, nullptr);
+ ASSERT_EQ(batch->numRows(), 1);
+
EXPECT_EQ(batch->getRowVector()->childAt(0)->asFlatVector<int64_t>()->valueAt(0),
expected);
+ }
+ EXPECT_EQ(batches.next(), nullptr);
+}
+
+} // namespace
+} // namespace gluten
diff --git a/cpp/bolt/tests/CMakeLists.txt b/cpp/bolt/tests/CMakeLists.txt
index 439f7d78e1..2ac4e6df44 100644
--- a/cpp/bolt/tests/CMakeLists.txt
+++ b/cpp/bolt/tests/CMakeLists.txt
@@ -55,8 +55,13 @@ set(BOLT_TEST_COMMON_SRCS JsonToProtoConverter.cc
FilePathGenerator.cc)
# TODO: ORC is not well supported. add_bolt_test(orc_test SOURCES OrcTest.cc)
add_bolt_test(
- bolt_operators_test SOURCES BoltColumnarToRowTest.cc BoltRowToColumnarTest.cc
- BoltColumnarBatchTest.cc BoltBatchResizerTest.cc)
+ bolt_operators_test
+ SOURCES
+ BoltColumnarToRowTest.cc
+ BoltRowToColumnarTest.cc
+ BoltColumnarBatchTest.cc
+ BoltBatchResizerTest.cc
+ BoltShuffleReaderTest.cc)
add_bolt_test(
bolt_plan_conversion_test
SOURCES
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]