File size: 13,995 Bytes
a1e6eab |
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 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
// 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 <signal.h>
#include <utility>
#include "arrow/python/flight.h"
#include "arrow/util/io_util.h"
#include "arrow/util/logging.h"
using arrow::flight::FlightPayload;
namespace arrow {
namespace py {
namespace flight {
const char* kPyServerMiddlewareName = "arrow.py_server_middleware";
PyServerAuthHandler::PyServerAuthHandler(PyObject* handler,
const PyServerAuthHandlerVtable& vtable)
: vtable_(vtable) {
Py_INCREF(handler);
handler_.reset(handler);
}
Status PyServerAuthHandler::Authenticate(arrow::flight::ServerAuthSender* outgoing,
arrow::flight::ServerAuthReader* incoming) {
return SafeCallIntoPython([=] {
const Status status = vtable_.authenticate(handler_.obj(), outgoing, incoming);
RETURN_NOT_OK(CheckPyError());
return status;
});
}
Status PyServerAuthHandler::IsValid(const std::string& token,
std::string* peer_identity) {
return SafeCallIntoPython([=] {
const Status status = vtable_.is_valid(handler_.obj(), token, peer_identity);
RETURN_NOT_OK(CheckPyError());
return status;
});
}
PyClientAuthHandler::PyClientAuthHandler(PyObject* handler,
const PyClientAuthHandlerVtable& vtable)
: vtable_(vtable) {
Py_INCREF(handler);
handler_.reset(handler);
}
Status PyClientAuthHandler::Authenticate(arrow::flight::ClientAuthSender* outgoing,
arrow::flight::ClientAuthReader* incoming) {
return SafeCallIntoPython([=] {
const Status status = vtable_.authenticate(handler_.obj(), outgoing, incoming);
RETURN_NOT_OK(CheckPyError());
return status;
});
}
Status PyClientAuthHandler::GetToken(std::string* token) {
return SafeCallIntoPython([=] {
const Status status = vtable_.get_token(handler_.obj(), token);
RETURN_NOT_OK(CheckPyError());
return status;
});
}
PyFlightServer::PyFlightServer(PyObject* server, const PyFlightServerVtable& vtable)
: vtable_(vtable) {
Py_INCREF(server);
server_.reset(server);
}
Status PyFlightServer::ListFlights(
const arrow::flight::ServerCallContext& context,
const arrow::flight::Criteria* criteria,
std::unique_ptr<arrow::flight::FlightListing>* listings) {
return SafeCallIntoPython([&] {
const Status status =
vtable_.list_flights(server_.obj(), context, criteria, listings);
RETURN_NOT_OK(CheckPyError());
return status;
});
}
Status PyFlightServer::GetFlightInfo(const arrow::flight::ServerCallContext& context,
const arrow::flight::FlightDescriptor& request,
std::unique_ptr<arrow::flight::FlightInfo>* info) {
return SafeCallIntoPython([&] {
const Status status = vtable_.get_flight_info(server_.obj(), context, request, info);
RETURN_NOT_OK(CheckPyError());
return status;
});
}
Status PyFlightServer::GetSchema(const arrow::flight::ServerCallContext& context,
const arrow::flight::FlightDescriptor& request,
std::unique_ptr<arrow::flight::SchemaResult>* result) {
return SafeCallIntoPython([&] {
const Status status = vtable_.get_schema(server_.obj(), context, request, result);
RETURN_NOT_OK(CheckPyError());
return status;
});
}
Status PyFlightServer::DoGet(const arrow::flight::ServerCallContext& context,
const arrow::flight::Ticket& request,
std::unique_ptr<arrow::flight::FlightDataStream>* stream) {
return SafeCallIntoPython([&] {
const Status status = vtable_.do_get(server_.obj(), context, request, stream);
RETURN_NOT_OK(CheckPyError());
return status;
});
}
Status PyFlightServer::DoPut(
const arrow::flight::ServerCallContext& context,
std::unique_ptr<arrow::flight::FlightMessageReader> reader,
std::unique_ptr<arrow::flight::FlightMetadataWriter> writer) {
return SafeCallIntoPython([&] {
const Status status =
vtable_.do_put(server_.obj(), context, std::move(reader), std::move(writer));
RETURN_NOT_OK(CheckPyError());
return status;
});
}
Status PyFlightServer::DoExchange(
const arrow::flight::ServerCallContext& context,
std::unique_ptr<arrow::flight::FlightMessageReader> reader,
std::unique_ptr<arrow::flight::FlightMessageWriter> writer) {
return SafeCallIntoPython([&] {
const Status status =
vtable_.do_exchange(server_.obj(), context, std::move(reader), std::move(writer));
RETURN_NOT_OK(CheckPyError());
return status;
});
}
Status PyFlightServer::DoAction(const arrow::flight::ServerCallContext& context,
const arrow::flight::Action& action,
std::unique_ptr<arrow::flight::ResultStream>* result) {
return SafeCallIntoPython([&] {
const Status status = vtable_.do_action(server_.obj(), context, action, result);
RETURN_NOT_OK(CheckPyError());
return status;
});
}
Status PyFlightServer::ListActions(const arrow::flight::ServerCallContext& context,
std::vector<arrow::flight::ActionType>* actions) {
return SafeCallIntoPython([&] {
const Status status = vtable_.list_actions(server_.obj(), context, actions);
RETURN_NOT_OK(CheckPyError());
return status;
});
}
Status PyFlightServer::ServeWithSignals() {
// Respect the current Python settings, i.e. only interrupt the server if there is
// an active signal handler for SIGINT and SIGTERM.
std::vector<int> signals;
for (const int signum : {SIGINT, SIGTERM}) {
ARROW_ASSIGN_OR_RAISE(auto handler, ::arrow::internal::GetSignalHandler(signum));
auto cb = handler.callback();
if (cb != SIG_DFL && cb != SIG_IGN) {
signals.push_back(signum);
}
}
RETURN_NOT_OK(SetShutdownOnSignals(signals));
// Serve until we got told to shutdown or a signal interrupted us
RETURN_NOT_OK(Serve());
int signum = GotSignal();
if (signum != 0) {
// Issue the signal again with Python's signal handlers restored
PyAcquireGIL lock;
raise(signum);
// XXX Ideally we would loop and serve again if no exception was raised.
// Unfortunately, gRPC will return immediately if Serve() is called again.
ARROW_UNUSED(PyErr_CheckSignals());
}
return Status::OK();
}
PyFlightResultStream::PyFlightResultStream(PyObject* generator,
PyFlightResultStreamCallback callback)
: callback_(callback) {
Py_INCREF(generator);
generator_.reset(generator);
}
arrow::Result<std::unique_ptr<arrow::flight::Result>> PyFlightResultStream::Next() {
return SafeCallIntoPython(
[=]() -> arrow::Result<std::unique_ptr<arrow::flight::Result>> {
std::unique_ptr<arrow::flight::Result> result;
const Status status = callback_(generator_.obj(), &result);
RETURN_NOT_OK(CheckPyError());
RETURN_NOT_OK(status);
return result;
});
}
PyFlightDataStream::PyFlightDataStream(
PyObject* data_source, std::unique_ptr<arrow::flight::FlightDataStream> stream)
: stream_(std::move(stream)) {
Py_INCREF(data_source);
data_source_.reset(data_source);
}
std::shared_ptr<Schema> PyFlightDataStream::schema() { return stream_->schema(); }
arrow::Result<FlightPayload> PyFlightDataStream::GetSchemaPayload() {
return stream_->GetSchemaPayload();
}
arrow::Result<FlightPayload> PyFlightDataStream::Next() { return stream_->Next(); }
PyGeneratorFlightDataStream::PyGeneratorFlightDataStream(
PyObject* generator, std::shared_ptr<arrow::Schema> schema,
PyGeneratorFlightDataStreamCallback callback, const ipc::IpcWriteOptions& options)
: schema_(schema), mapper_(*schema_), options_(options), callback_(callback) {
Py_INCREF(generator);
generator_.reset(generator);
}
std::shared_ptr<Schema> PyGeneratorFlightDataStream::schema() { return schema_; }
arrow::Result<FlightPayload> PyGeneratorFlightDataStream::GetSchemaPayload() {
FlightPayload payload;
RETURN_NOT_OK(ipc::GetSchemaPayload(*schema_, options_, mapper_, &payload.ipc_message));
return payload;
}
arrow::Result<FlightPayload> PyGeneratorFlightDataStream::Next() {
return SafeCallIntoPython([=]() -> arrow::Result<FlightPayload> {
FlightPayload payload;
const Status status = callback_(generator_.obj(), &payload);
RETURN_NOT_OK(CheckPyError());
RETURN_NOT_OK(status);
return payload;
});
}
// Flight Server Middleware
PyServerMiddlewareFactory::PyServerMiddlewareFactory(PyObject* factory,
StartCallCallback start_call)
: start_call_(start_call) {
Py_INCREF(factory);
factory_.reset(factory);
}
Status PyServerMiddlewareFactory::StartCall(
const arrow::flight::CallInfo& info,
const arrow::flight::CallHeaders& incoming_headers,
std::shared_ptr<arrow::flight::ServerMiddleware>* middleware) {
return SafeCallIntoPython([&] {
const Status status = start_call_(factory_.obj(), info, incoming_headers, middleware);
RETURN_NOT_OK(CheckPyError());
return status;
});
}
PyServerMiddleware::PyServerMiddleware(PyObject* middleware, Vtable vtable)
: vtable_(vtable) {
Py_INCREF(middleware);
middleware_.reset(middleware);
}
void PyServerMiddleware::SendingHeaders(arrow::flight::AddCallHeaders* outgoing_headers) {
const Status& status = SafeCallIntoPython([&] {
const Status status = vtable_.sending_headers(middleware_.obj(), outgoing_headers);
RETURN_NOT_OK(CheckPyError());
return status;
});
ARROW_WARN_NOT_OK(status, "Python server middleware failed in SendingHeaders");
}
void PyServerMiddleware::CallCompleted(const Status& call_status) {
const Status& status = SafeCallIntoPython([&] {
const Status status = vtable_.call_completed(middleware_.obj(), call_status);
RETURN_NOT_OK(CheckPyError());
return status;
});
ARROW_WARN_NOT_OK(status, "Python server middleware failed in CallCompleted");
}
std::string PyServerMiddleware::name() const { return kPyServerMiddlewareName; }
PyObject* PyServerMiddleware::py_object() const { return middleware_.obj(); }
// Flight Client Middleware
PyClientMiddlewareFactory::PyClientMiddlewareFactory(PyObject* factory,
StartCallCallback start_call)
: start_call_(start_call) {
Py_INCREF(factory);
factory_.reset(factory);
}
void PyClientMiddlewareFactory::StartCall(
const arrow::flight::CallInfo& info,
std::unique_ptr<arrow::flight::ClientMiddleware>* middleware) {
const Status& status = SafeCallIntoPython([&] {
const Status status = start_call_(factory_.obj(), info, middleware);
RETURN_NOT_OK(CheckPyError());
return status;
});
ARROW_WARN_NOT_OK(status, "Python client middleware failed in StartCall");
}
PyClientMiddleware::PyClientMiddleware(PyObject* middleware, Vtable vtable)
: vtable_(vtable) {
Py_INCREF(middleware);
middleware_.reset(middleware);
}
void PyClientMiddleware::SendingHeaders(arrow::flight::AddCallHeaders* outgoing_headers) {
const Status& status = SafeCallIntoPython([&] {
const Status status = vtable_.sending_headers(middleware_.obj(), outgoing_headers);
RETURN_NOT_OK(CheckPyError());
return status;
});
ARROW_WARN_NOT_OK(status, "Python client middleware failed in StartCall");
}
void PyClientMiddleware::ReceivedHeaders(
const arrow::flight::CallHeaders& incoming_headers) {
const Status& status = SafeCallIntoPython([&] {
const Status status = vtable_.received_headers(middleware_.obj(), incoming_headers);
RETURN_NOT_OK(CheckPyError());
return status;
});
ARROW_WARN_NOT_OK(status, "Python client middleware failed in StartCall");
}
void PyClientMiddleware::CallCompleted(const Status& call_status) {
const Status& status = SafeCallIntoPython([&] {
const Status status = vtable_.call_completed(middleware_.obj(), call_status);
RETURN_NOT_OK(CheckPyError());
return status;
});
ARROW_WARN_NOT_OK(status, "Python client middleware failed in StartCall");
}
Status CreateFlightInfo(const std::shared_ptr<arrow::Schema>& schema,
const arrow::flight::FlightDescriptor& descriptor,
const std::vector<arrow::flight::FlightEndpoint>& endpoints,
int64_t total_records, int64_t total_bytes,
std::unique_ptr<arrow::flight::FlightInfo>* out) {
ARROW_ASSIGN_OR_RAISE(auto result,
arrow::flight::FlightInfo::Make(*schema, descriptor, endpoints,
total_records, total_bytes));
*out = std::unique_ptr<arrow::flight::FlightInfo>(
new arrow::flight::FlightInfo(std::move(result)));
return Status::OK();
}
Status CreateSchemaResult(const std::shared_ptr<arrow::Schema>& schema,
std::unique_ptr<arrow::flight::SchemaResult>* out) {
return arrow::flight::SchemaResult::Make(*schema).Value(out);
}
} // namespace flight
} // namespace py
} // namespace arrow
|