Skip to content

Commit fa7e7a3

Browse files
authored
ARROW-17242: [C++][FlightRPC] Propagate RecordBatchReader::Close errors through Flight (#13738)
Authored-by: David Li <[email protected]> Signed-off-by: Yibo Cai <[email protected]>
1 parent 3e87c66 commit fa7e7a3

6 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎cpp/src/arrow/flight/server.cc‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ class RecordBatchStream::RecordBatchStreamImpl {
323323
}
324324
}
325325

326+
Status Close() { return reader_->Close(); }
327+
326328
private:
327329
Status GetNextDictionary(FlightPayload* payload) {
328330
const auto& it = dictionaries_[dictionary_index_++];
@@ -344,6 +346,7 @@ class RecordBatchStream::RecordBatchStreamImpl {
344346
FlightMetadataWriter::~FlightMetadataWriter() = default;
345347

346348
FlightDataStream::~FlightDataStream() {}
349+
Status FlightDataStream::Close() { return Status::OK(); }
347350

348351
RecordBatchStream::RecordBatchStream(const std::shared_ptr<RecordBatchReader>& reader,
349352
const ipc::IpcWriteOptions& options) {
@@ -352,6 +355,8 @@ RecordBatchStream::RecordBatchStream(const std::shared_ptr<RecordBatchReader>& r
352355

353356
RecordBatchStream::~RecordBatchStream() {}
354357

358+
Status RecordBatchStream::Close() { return impl_->Close(); }
359+
355360
std::shared_ptr<Schema> RecordBatchStream::schema() { return impl_->schema(); }
356361

357362
arrow::Result<FlightPayload> RecordBatchStream::GetSchemaPayload() {

‎cpp/src/arrow/flight/server.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class ARROW_FLIGHT_EXPORT FlightDataStream {
6464

6565
ARROW_DEPRECATED("Deprecated in 8.0.0. Use Result-returning overload instead.")
6666
Status Next(FlightPayload* payload) { return Next().Value(payload); }
67+
68+
virtual Status Close();
6769
};
6870

6971
/// \brief A basic implementation of FlightDataStream that will provide
@@ -85,6 +87,7 @@ class ARROW_FLIGHT_EXPORT RecordBatchStream : public FlightDataStream {
8587
arrow::Result<FlightPayload> GetSchemaPayload() override;
8688

8789
arrow::Result<FlightPayload> Next() override;
90+
Status Close() override;
8891

8992
private:
9093
class RecordBatchStreamImpl;

‎cpp/src/arrow/flight/test_definitions.cc‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,21 @@ void DataTest::TestDoGetLargeBatch() {
237237
Ticket ticket{"ticket-large-batch-1"};
238238
CheckDoGet(ticket, expected_batches);
239239
}
240+
// Ensure FlightDataStream/RecordBatchStream::Close errors are propagated
241+
void DataTest::TestFlightDataStreamError() {
242+
Ticket ticket{"ticket-stream-error"};
243+
244+
ASSERT_OK_AND_ASSIGN(auto stream, client_->DoGet(ticket));
245+
Status status;
246+
while (true) {
247+
FlightStreamChunk chunk;
248+
status = stream->Next().Value(&chunk);
249+
if (!chunk.data) break;
250+
if (!status.ok()) break;
251+
}
252+
EXPECT_RAISES_WITH_MESSAGE_THAT(IOError, ::testing::HasSubstr("Expected error"),
253+
status);
254+
}
240255
void DataTest::TestOverflowServerBatch() {
241256
// Regression test for ARROW-13253
242257
// N.B. this is rather a slow and memory-hungry test

‎cpp/src/arrow/flight/test_definitions.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class ARROW_FLIGHT_EXPORT DataTest : public FlightTest {
7676
void TestDoGetFloats();
7777
void TestDoGetDicts();
7878
void TestDoGetLargeBatch();
79+
void TestFlightDataStreamError();
7980
void TestOverflowServerBatch();
8081
void TestOverflowClientBatch();
8182
void TestDoExchange();
@@ -107,6 +108,7 @@ class ARROW_FLIGHT_EXPORT DataTest : public FlightTest {
107108
TEST_F(FIXTURE, TestDoGetFloats) { TestDoGetFloats(); } \
108109
TEST_F(FIXTURE, TestDoGetDicts) { TestDoGetDicts(); } \
109110
TEST_F(FIXTURE, TestDoGetLargeBatch) { TestDoGetLargeBatch(); } \
111+
TEST_F(FIXTURE, TestFlightDataStreamError) { TestFlightDataStreamError(); } \
110112
TEST_F(FIXTURE, TestOverflowServerBatch) { TestOverflowServerBatch(); } \
111113
TEST_F(FIXTURE, TestOverflowClientBatch) { TestOverflowClientBatch(); } \
112114
TEST_F(FIXTURE, TestDoExchange) { TestDoExchange(); } \

‎cpp/src/arrow/flight/test_util.cc‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,25 @@ Status ResolveCurrentExecutable(fs::path* out) {
8989
}
9090
}
9191

92+
class ErrorRecordBatchReader : public RecordBatchReader {
93+
public:
94+
ErrorRecordBatchReader() : schema_(arrow::schema({})) {}
95+
96+
std::shared_ptr<Schema> schema() const override { return schema_; }
97+
98+
Status ReadNext(std::shared_ptr<RecordBatch>* out) override {
99+
*out = nullptr;
100+
return Status::OK();
101+
}
102+
103+
Status Close() override {
104+
// This should be propagated over DoGet to the client
105+
return Status::IOError("Expected error");
106+
}
107+
108+
private:
109+
std::shared_ptr<Schema> schema_;
110+
};
92111
} // namespace
93112

94113
void TestServer::Start(const std::vector<std::string>& extra_args) {
@@ -225,6 +244,12 @@ class FlightTestServer : public FlightServerBase {
225244
std::unique_ptr<FlightDataStream>(new RecordBatchStream(std::move(reader)));
226245
return Status::OK();
227246
}
247+
if (request.ticket == "ticket-stream-error") {
248+
auto reader = std::make_shared<ErrorRecordBatchReader>();
249+
*data_stream =
250+
std::unique_ptr<FlightDataStream>(new RecordBatchStream(std::move(reader)));
251+
return Status::OK();
252+
}
228253

229254
std::shared_ptr<RecordBatchReader> batch_reader;
230255
RETURN_NOT_OK(GetBatchForFlight(request, &batch_reader));

‎cpp/src/arrow/flight/transport_server.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ Status ServerTransport::DoGet(const ServerCallContext& context, const Ticket& ti
293293
if (!success) return Status::OK();
294294
}
295295
RETURN_NOT_OK(stream->WritesDone());
296-
return Status::OK();
296+
return data_stream->Close();
297297
}
298298

299299
Status ServerTransport::DoPut(const ServerCallContext& context,

0 commit comments

Comments
 (0)