Skip to content

Commit 2e0bb21

Browse files
committed
[refactor](arrow-flight) Keep cancellation checks in result reader
1 parent 1824417 commit 2e0bb21

4 files changed

Lines changed: 80 additions & 20 deletions

File tree

‎be/src/exec/sink/writer/varrow_flight_result_writer.cpp‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,20 @@ void ArrowFlightResultBlockBuffer::cancel_query(const Status& reason) {
106106
}
107107
}
108108

109-
Status ArrowFlightResultBlockBuffer::get_arrow_batch(std::shared_ptr<Block>* result,
110-
const std::function<bool()>& is_cancelled) {
109+
Status ArrowFlightResultBlockBuffer::get_arrow_batch(std::shared_ptr<Block>* result, bool* eos) {
110+
*result = nullptr;
111+
*eos = false;
111112
std::unique_lock<std::mutex> l(_lock);
112113
Defer defer {[&]() { _update_dependency(); }};
113114
if (!_status.ok()) {
114115
return _status;
115116
}
116117

117-
while (_result_batch_queue.empty() && _status.ok() && !_is_close) {
118-
if (is_cancelled && is_cancelled()) {
119-
return Status::Cancelled("Arrow Flight fetch cancelled");
120-
}
121-
_arrow_data_arrival.wait_for(l, std::chrono::milliseconds(20));
118+
if (!_arrow_data_arrival.wait_for(l, std::chrono::milliseconds(20), [&] {
119+
return !_result_batch_queue.empty() || !_status.ok() || _is_close;
120+
})) {
121+
// Let the reader check RPC cancellation without treating an empty wait as EOF.
122+
return Status::OK();
122123
}
123124

124125
if (!_status.ok()) {
@@ -139,6 +140,7 @@ Status ArrowFlightResultBlockBuffer::get_arrow_batch(std::shared_ptr<Block>* res
139140

140141
// normal path end
141142
if (_is_close) {
143+
*eos = true;
142144
if (!_status.ok()) {
143145
return _status;
144146
}

‎be/src/exec/sink/writer/varrow_flight_result_writer.h‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
#pragma once
1919

20-
#include <functional>
21-
2220
#include "common/status.h"
2321
#include "exec/sink/writer/result_writer.h"
2422
#include "exprs/vexpr_fwd.h"
@@ -75,8 +73,8 @@ class ArrowFlightResultBlockBuffer final : public ResultBlockBuffer<GetArrowResu
7573
_compressed_bytes_counter = ADD_COUNTER(&_profile, "CompressedBytes", TUnit::BYTES);
7674
}
7775
~ArrowFlightResultBlockBuffer() override = default;
78-
Status get_arrow_batch(std::shared_ptr<Block>* result,
79-
const std::function<bool()>& is_cancelled = {});
76+
// A timed wait may return OK with no block and eos=false; the caller must retry.
77+
Status get_arrow_batch(std::shared_ptr<Block>* result, bool* eos);
8078
void cancel_query(const Status& reason);
8179
void get_timezone(cctz::time_zone& timezone_obj) { timezone_obj = _timezone_obj; }
8280
Status get_schema(std::shared_ptr<arrow::Schema>* arrow_schema);

‎be/src/service/arrow_flight/arrow_flight_batch_reader.cpp‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,19 +172,25 @@ arrow::Status ArrowFlightBatchLocalReader::ReadNextImpl(std::shared_ptr<arrow::R
172172
RETURN_ARROW_STATUS_IF_ERROR(
173173
ExecEnv::GetInstance()->result_mgr()->find_buffer(tid, arrow_buffer));
174174
std::shared_ptr<Block> result;
175-
auto st = arrow_buffer->get_arrow_batch(&result, [this] { return is_cancelled(); });
176-
st.prepend("ArrowFlightBatchLocalReader fetch arrow data failed");
177-
ARROW_RETURN_NOT_OK(to_arrow_status(st));
178-
if (result == nullptr) {
175+
bool eos = false;
176+
while (!result && !eos) {
177+
if (is_cancelled()) {
178+
return arrow::Status::Cancelled("Arrow Flight fetch cancelled");
179+
}
180+
auto st = arrow_buffer->get_arrow_batch(&result, &eos);
181+
st.prepend("ArrowFlightBatchLocalReader fetch arrow data failed");
182+
ARROW_RETURN_NOT_OK(to_arrow_status(st));
183+
}
184+
if (eos) {
179185
// eof, normal path end
180186
return arrow::Status::OK();
181187
}
182188

183189
{
184190
// convert one batch
185191
SCOPED_ATOMIC_TIMER(&_convert_arrow_batch_timer);
186-
st = ArrowFlightArrowBlockConvertor(_schema, _timezone_obj)
187-
.convert_to_arrow(*result, arrow::default_memory_pool(), out);
192+
auto st = ArrowFlightArrowBlockConvertor(_schema, _timezone_obj)
193+
.convert_to_arrow(*result, arrow::default_memory_pool(), out);
188194
st.prepend("ArrowFlightBatchLocalReader convert block to arrow batch failed");
189195
ARROW_RETURN_NOT_OK(to_arrow_status(st));
190196
}

‎be/test/service/arrow_flight/arrow_flight_cancel_test.cpp‎

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ TEST_F(ArrowFlightCancelTest, EarlyCloseReleasesBackpressuredBuffer) {
8282
EXPECT_TRUE(_dep->ready());
8383
EXPECT_TRUE(_state.get_query_ctx()->is_cancelled());
8484
std::shared_ptr<Block> block;
85-
EXPECT_FALSE(_buffer->get_arrow_batch(&block).ok());
85+
bool eos = false;
86+
EXPECT_FALSE(_buffer->get_arrow_batch(&block, &eos).ok());
8687
EXPECT_TRUE(reader->Close().ok());
8788
}
8889

@@ -108,7 +109,9 @@ TEST_F(ArrowFlightCancelTest, NormalEofIsNotCancellation) {
108109
EXPECT_EQ(batch, nullptr);
109110
EXPECT_TRUE(reader->Close().ok());
110111
std::shared_ptr<Block> block;
111-
EXPECT_TRUE(_buffer->get_arrow_batch(&block).ok());
112+
bool eos = false;
113+
EXPECT_TRUE(_buffer->get_arrow_batch(&block, &eos).ok());
114+
EXPECT_TRUE(eos);
112115
EXPECT_FALSE(_state.get_query_ctx()->is_cancelled());
113116
}
114117

@@ -117,7 +120,7 @@ TEST_F(ArrowFlightCancelTest, CancellationInterruptsEmptyBufferWait) {
117120
std::promise<void> checked;
118121
std::atomic<int> checks = 0;
119122
auto result = ArrowFlightBatchLocalReader::Create(_statement, [&] {
120-
if (checks.fetch_add(1) == 1) {
123+
if (checks.fetch_add(1) == 3) {
121124
checked.set_value();
122125
}
123126
return cancelled.load();
@@ -141,6 +144,57 @@ TEST_F(ArrowFlightCancelTest, CancellationInterruptsEmptyBufferWait) {
141144
EXPECT_TRUE(_state.get_query_ctx()->is_cancelled());
142145
}
143146

147+
TEST_F(ArrowFlightCancelTest, EmptyBufferReadReturnsForRetry) {
148+
auto fetch = std::async(std::launch::async, [&] {
149+
auto block = std::make_shared<Block>();
150+
bool eos = true;
151+
auto status = _buffer->get_arrow_batch(&block, &eos);
152+
EXPECT_EQ(block, nullptr);
153+
EXPECT_FALSE(eos);
154+
return status;
155+
});
156+
const auto ready = fetch.wait_for(std::chrono::seconds(2));
157+
// Bound the test even if an empty buffer incorrectly waits until query completion.
158+
if (ready != std::future_status::ready) {
159+
_buffer->cancel(Status::Cancelled("test cleanup"));
160+
}
161+
EXPECT_EQ(ready, std::future_status::ready);
162+
EXPECT_TRUE(fetch.get().ok());
163+
}
164+
165+
TEST_F(ArrowFlightCancelTest, EmptyWaitsDoNotFinishReader) {
166+
std::promise<void> retried;
167+
std::atomic<int> checks = 0;
168+
auto result = ArrowFlightBatchLocalReader::Create(_statement, [&] {
169+
if (checks.fetch_add(1) == 3) {
170+
retried.set_value();
171+
}
172+
return false;
173+
});
174+
ASSERT_TRUE(result.ok()) << result.status();
175+
auto reader = *result;
176+
std::shared_ptr<arrow::RecordBatch> batch;
177+
auto fetch = std::async(std::launch::async, [&] { return reader->ReadNext(&batch); });
178+
EXPECT_EQ(retried.get_future().wait_for(std::chrono::seconds(2)), std::future_status::ready);
179+
EXPECT_EQ(fetch.wait_for(std::chrono::seconds(0)), std::future_status::timeout);
180+
auto block = std::make_shared<Block>(ColumnHelper::create_block<DataTypeInt64>({1, 2}));
181+
EXPECT_TRUE(_buffer->add_batch(&_state, block).ok());
182+
bool fully_closed = false;
183+
EXPECT_TRUE(_buffer->close(_state.fragment_instance_id(), Status::OK(), 2, fully_closed).ok());
184+
const auto ready = fetch.wait_for(std::chrono::seconds(2));
185+
if (ready != std::future_status::ready) {
186+
_buffer->cancel(Status::Cancelled("test cleanup"));
187+
}
188+
EXPECT_EQ(ready, std::future_status::ready);
189+
ASSERT_TRUE(fetch.get().ok());
190+
ASSERT_NE(batch, nullptr);
191+
EXPECT_EQ(batch->num_rows(), 2);
192+
ASSERT_TRUE(reader->ReadNext(&batch).ok());
193+
EXPECT_EQ(batch, nullptr);
194+
EXPECT_TRUE(reader->Close().ok());
195+
EXPECT_FALSE(_state.get_query_ctx()->is_cancelled());
196+
}
197+
144198
TEST_F(ArrowFlightCancelTest, ConversionFailureCancelsQuery) {
145199
auto block = std::make_shared<Block>(ColumnHelper::create_block<DataTypeString>({"bad"}));
146200
ASSERT_TRUE(_buffer->add_batch(&_state, block).ok());

0 commit comments

Comments
 (0)