forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransport.cc
More file actions
357 lines (336 loc) · 15 KB
/
Copy pathtransport.cc
File metadata and controls
357 lines (336 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// 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/flight/transport.h"
#include <memory>
#include <sstream>
#include <unordered_map>
#include "arrow/flight/client_auth.h"
#include "arrow/flight/transport_server.h"
#include "arrow/flight/types.h"
#include "arrow/ipc/message.h"
#include "arrow/result.h"
#include "arrow/status.h"
namespace arrow {
namespace flight {
namespace internal {
::arrow::Result<std::unique_ptr<ipc::Message>> FlightData::OpenMessage() {
return ipc::Message::Open(metadata, body);
}
bool TransportDataStream::ReadData(internal::FlightData*) { return false; }
arrow::Result<bool> TransportDataStream::WriteData(const FlightPayload&) {
return Status::NotImplemented("Writing data for this stream");
}
Status TransportDataStream::WritesDone() { return Status::OK(); }
bool ClientDataStream::ReadPutMetadata(std::shared_ptr<Buffer>*) { return false; }
Status ClientDataStream::Finish(Status st) {
auto server_status = DoFinish();
if (server_status.ok()) return st;
return Status::FromDetailAndArgs(server_status.code(), server_status.detail(),
server_status.message(),
". Client context: ", st.ToString());
}
Status ClientTransport::Authenticate(const FlightCallOptions& options,
std::unique_ptr<ClientAuthHandler> auth_handler) {
return Status::NotImplemented("Authenticate for this transport");
}
arrow::Result<std::pair<std::string, std::string>>
ClientTransport::AuthenticateBasicToken(const FlightCallOptions& options,
const std::string& username,
const std::string& password) {
return Status::NotImplemented("AuthenticateBasicToken for this transport");
}
Status ClientTransport::DoAction(const FlightCallOptions& options, const Action& action,
std::unique_ptr<ResultStream>* results) {
return Status::NotImplemented("DoAction for this transport");
}
Status ClientTransport::ListActions(const FlightCallOptions& options,
std::vector<ActionType>* actions) {
return Status::NotImplemented("ListActions for this transport");
}
Status ClientTransport::GetFlightInfo(const FlightCallOptions& options,
const FlightDescriptor& descriptor,
std::unique_ptr<FlightInfo>* info) {
return Status::NotImplemented("GetFlightInfo for this transport");
}
arrow::Result<std::unique_ptr<SchemaResult>> ClientTransport::GetSchema(
const FlightCallOptions& options, const FlightDescriptor& descriptor) {
return Status::NotImplemented("GetSchema for this transport");
}
Status ClientTransport::ListFlights(const FlightCallOptions& options,
const Criteria& criteria,
std::unique_ptr<FlightListing>* listing) {
return Status::NotImplemented("ListFlights for this transport");
}
Status ClientTransport::DoGet(const FlightCallOptions& options, const Ticket& ticket,
std::unique_ptr<ClientDataStream>* stream) {
return Status::NotImplemented("DoGet for this transport");
}
Status ClientTransport::DoPut(const FlightCallOptions& options,
std::unique_ptr<ClientDataStream>* stream) {
return Status::NotImplemented("DoPut for this transport");
}
Status ClientTransport::DoExchange(const FlightCallOptions& options,
std::unique_ptr<ClientDataStream>* stream) {
return Status::NotImplemented("DoExchange for this transport");
}
class TransportRegistry::Impl final {
public:
arrow::Result<std::unique_ptr<ClientTransport>> MakeClient(
const std::string& scheme) const {
auto it = client_factories_.find(scheme);
if (it == client_factories_.end()) {
return Status::KeyError("No client transport implementation for ", scheme);
}
return it->second();
}
arrow::Result<std::unique_ptr<ServerTransport>> MakeServer(
const std::string& scheme, FlightServerBase* base,
std::shared_ptr<MemoryManager> memory_manager) const {
auto it = server_factories_.find(scheme);
if (it == server_factories_.end()) {
return Status::KeyError("No server transport implementation for ", scheme);
}
return it->second(base, std::move(memory_manager));
}
Status RegisterClient(const std::string& scheme, ClientFactory factory) {
auto it = client_factories_.insert({scheme, std::move(factory)});
if (!it.second) {
return Status::Invalid("Client transport already registered for ", scheme);
}
return Status::OK();
}
Status RegisterServer(const std::string& scheme, ServerFactory factory) {
auto it = server_factories_.insert({scheme, std::move(factory)});
if (!it.second) {
return Status::Invalid("Server transport already registered for ", scheme);
}
return Status::OK();
}
private:
std::unordered_map<std::string, TransportRegistry::ClientFactory> client_factories_;
std::unordered_map<std::string, TransportRegistry::ServerFactory> server_factories_;
};
TransportRegistry::TransportRegistry() { impl_ = std::make_unique<Impl>(); }
TransportRegistry::~TransportRegistry() = default;
arrow::Result<std::unique_ptr<ClientTransport>> TransportRegistry::MakeClient(
const std::string& scheme) const {
return impl_->MakeClient(scheme);
}
arrow::Result<std::unique_ptr<ServerTransport>> TransportRegistry::MakeServer(
const std::string& scheme, FlightServerBase* base,
std::shared_ptr<MemoryManager> memory_manager) const {
return impl_->MakeServer(scheme, base, std::move(memory_manager));
}
Status TransportRegistry::RegisterClient(const std::string& scheme,
ClientFactory factory) {
return impl_->RegisterClient(scheme, std::move(factory));
}
Status TransportRegistry::RegisterServer(const std::string& scheme,
ServerFactory factory) {
return impl_->RegisterServer(scheme, std::move(factory));
}
TransportRegistry* GetDefaultTransportRegistry() {
static TransportRegistry kRegistry;
return &kRegistry;
}
//------------------------------------------------------------
// Error propagation helpers
TransportStatus TransportStatus::FromStatus(const Status& arrow_status) {
if (arrow_status.ok()) {
return TransportStatus{TransportStatusCode::kOk, ""};
}
TransportStatusCode code = TransportStatusCode::kUnknown;
std::string message = arrow_status.message();
if (arrow_status.detail()) {
message += ". Detail: ";
message += arrow_status.detail()->ToString();
}
std::shared_ptr<FlightStatusDetail> flight_status =
FlightStatusDetail::UnwrapStatus(arrow_status);
if (flight_status) {
switch (flight_status->code()) {
case FlightStatusCode::Internal:
code = TransportStatusCode::kInternal;
break;
case FlightStatusCode::TimedOut:
code = TransportStatusCode::kTimedOut;
break;
case FlightStatusCode::Cancelled:
code = TransportStatusCode::kCancelled;
break;
case FlightStatusCode::Unauthenticated:
code = TransportStatusCode::kUnauthenticated;
break;
case FlightStatusCode::Unauthorized:
code = TransportStatusCode::kUnauthorized;
break;
case FlightStatusCode::Unavailable:
code = TransportStatusCode::kUnavailable;
break;
default:
break;
}
} else if (arrow_status.IsKeyError()) {
code = TransportStatusCode::kNotFound;
} else if (arrow_status.IsInvalid()) {
code = TransportStatusCode::kInvalidArgument;
} else if (arrow_status.IsCancelled()) {
code = TransportStatusCode::kCancelled;
} else if (arrow_status.IsNotImplemented()) {
code = TransportStatusCode::kUnimplemented;
} else if (arrow_status.IsAlreadyExists()) {
code = TransportStatusCode::kAlreadyExists;
}
return TransportStatus{code, std::move(message)};
}
TransportStatus TransportStatus::FromCodeStringAndMessage(const std::string& code_str,
std::string message) {
int code_int = 0;
try {
code_int = std::stoi(code_str);
} catch (...) {
return TransportStatus{
TransportStatusCode::kUnknown,
message + ". Also, server sent unknown or invalid Arrow status code " + code_str};
}
switch (code_int) {
case static_cast<int>(TransportStatusCode::kOk):
case static_cast<int>(TransportStatusCode::kUnknown):
case static_cast<int>(TransportStatusCode::kInternal):
case static_cast<int>(TransportStatusCode::kInvalidArgument):
case static_cast<int>(TransportStatusCode::kTimedOut):
case static_cast<int>(TransportStatusCode::kNotFound):
case static_cast<int>(TransportStatusCode::kAlreadyExists):
case static_cast<int>(TransportStatusCode::kCancelled):
case static_cast<int>(TransportStatusCode::kUnauthenticated):
case static_cast<int>(TransportStatusCode::kUnauthorized):
case static_cast<int>(TransportStatusCode::kUnimplemented):
case static_cast<int>(TransportStatusCode::kUnavailable):
return TransportStatus{static_cast<TransportStatusCode>(code_int),
std::move(message)};
default: {
return TransportStatus{
TransportStatusCode::kUnknown,
message + ". Also, server sent unknown or invalid Arrow status code " +
code_str};
}
}
}
Status TransportStatus::ToStatus() const {
switch (code) {
case TransportStatusCode::kOk:
return Status::OK();
case TransportStatusCode::kUnknown: {
std::stringstream ss;
ss << "Flight RPC failed with message: " << message;
return Status::UnknownError(ss.str()).WithDetail(
std::make_shared<FlightStatusDetail>(FlightStatusCode::Failed));
}
case TransportStatusCode::kInternal:
return Status::IOError("Flight returned internal error, with message: ", message)
.WithDetail(std::make_shared<FlightStatusDetail>(FlightStatusCode::Internal));
case TransportStatusCode::kInvalidArgument:
return Status::Invalid("Flight returned invalid argument error, with message: ",
message);
case TransportStatusCode::kTimedOut:
return Status::IOError("Flight returned timeout error, with message: ", message)
.WithDetail(std::make_shared<FlightStatusDetail>(FlightStatusCode::TimedOut));
case TransportStatusCode::kNotFound:
return Status::KeyError("Flight returned not found error, with message: ", message);
case TransportStatusCode::kAlreadyExists:
return Status::AlreadyExists("Flight returned already exists error, with message: ",
message);
case TransportStatusCode::kCancelled:
return Status::Cancelled("Flight cancelled call, with message: ", message)
.WithDetail(std::make_shared<FlightStatusDetail>(FlightStatusCode::Cancelled));
case TransportStatusCode::kUnauthenticated:
return Status::IOError("Flight returned unauthenticated error, with message: ",
message)
.WithDetail(
std::make_shared<FlightStatusDetail>(FlightStatusCode::Unauthenticated));
case TransportStatusCode::kUnauthorized:
return Status::IOError("Flight returned unauthorized error, with message: ",
message)
.WithDetail(
std::make_shared<FlightStatusDetail>(FlightStatusCode::Unauthorized));
case TransportStatusCode::kUnimplemented:
return Status::NotImplemented("Flight returned unimplemented error, with message: ",
message);
case TransportStatusCode::kUnavailable:
return Status::IOError("Flight returned unavailable error, with message: ", message)
.WithDetail(
std::make_shared<FlightStatusDetail>(FlightStatusCode::Unavailable));
default:
return Status::UnknownError("Flight failed with error code ",
static_cast<int>(code), " and message: ", message);
}
}
Status ReconstructStatus(const std::string& code_str, const Status& current_status,
std::optional<std::string> message,
std::optional<std::string> detail_message,
std::optional<std::string> detail_bin,
std::shared_ptr<FlightStatusDetail> detail) {
// Bounce through std::string to get a proper null-terminated C string
StatusCode status_code = current_status.code();
std::stringstream status_message;
try {
const auto code_int = std::stoi(code_str);
switch (code_int) {
case static_cast<int>(StatusCode::OutOfMemory):
case static_cast<int>(StatusCode::KeyError):
case static_cast<int>(StatusCode::TypeError):
case static_cast<int>(StatusCode::Invalid):
case static_cast<int>(StatusCode::IOError):
case static_cast<int>(StatusCode::CapacityError):
case static_cast<int>(StatusCode::IndexError):
case static_cast<int>(StatusCode::Cancelled):
case static_cast<int>(StatusCode::UnknownError):
case static_cast<int>(StatusCode::NotImplemented):
case static_cast<int>(StatusCode::SerializationError):
case static_cast<int>(StatusCode::RError):
case static_cast<int>(StatusCode::CodeGenError):
case static_cast<int>(StatusCode::ExpressionValidationError):
case static_cast<int>(StatusCode::ExecutionError):
case static_cast<int>(StatusCode::AlreadyExists): {
status_code = static_cast<StatusCode>(code_int);
break;
}
default: {
status_message << ". Also, server sent unknown or invalid Arrow status code "
<< code_str;
break;
}
}
} catch (...) {
status_message << ". Also, server sent unknown or invalid Arrow status code "
<< code_str;
}
status_message << (message.has_value() ? *message : current_status.message());
if (detail_message.has_value()) {
status_message << ". Detail: " << *detail_message;
}
if (detail_bin.has_value()) {
if (!detail) {
detail = std::make_shared<FlightStatusDetail>(FlightStatusCode::Internal);
}
detail->set_extra_info(std::move(*detail_bin));
}
return Status(status_code, status_message.str(), std::move(detail));
}
} // namespace internal
} // namespace flight
} // namespace arrow