forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflight.cc
More file actions
226 lines (197 loc) · 7.87 KB
/
Copy pathflight.cc
File metadata and controls
226 lines (197 loc) · 7.87 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
// 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/flight/internal.h"
#include "arrow/python/flight.h"
#include "arrow/util/logging.h"
namespace arrow {
namespace py {
namespace flight {
PyServerAuthHandler::PyServerAuthHandler(PyObject* handler,
PyServerAuthHandlerVtable vtable)
: vtable_(vtable) {
Py_INCREF(handler);
handler_.reset(handler);
}
Status PyServerAuthHandler::Authenticate(arrow::flight::ServerAuthSender* outgoing,
arrow::flight::ServerAuthReader* incoming) {
return SafeCallIntoPython([=] {
vtable_.authenticate(handler_.obj(), outgoing, incoming);
return CheckPyError();
});
}
Status PyServerAuthHandler::IsValid(const std::string& token,
std::string* peer_identity) {
return SafeCallIntoPython([=] {
vtable_.is_valid(handler_.obj(), token, peer_identity);
return CheckPyError();
});
}
PyClientAuthHandler::PyClientAuthHandler(PyObject* handler,
PyClientAuthHandlerVtable vtable)
: vtable_(vtable) {
Py_INCREF(handler);
handler_.reset(handler);
}
Status PyClientAuthHandler::Authenticate(arrow::flight::ClientAuthSender* outgoing,
arrow::flight::ClientAuthReader* incoming) {
return SafeCallIntoPython([=] {
vtable_.authenticate(handler_.obj(), outgoing, incoming);
return CheckPyError();
});
}
Status PyClientAuthHandler::GetToken(std::string* token) {
return SafeCallIntoPython([=] {
vtable_.get_token(handler_.obj(), token);
return CheckPyError();
});
}
PyFlightServer::PyFlightServer(PyObject* server, 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([&] {
vtable_.list_flights(server_.obj(), context, criteria, listings);
return CheckPyError();
});
}
Status PyFlightServer::GetFlightInfo(const arrow::flight::ServerCallContext& context,
const arrow::flight::FlightDescriptor& request,
std::unique_ptr<arrow::flight::FlightInfo>* info) {
return SafeCallIntoPython([&] {
vtable_.get_flight_info(server_.obj(), context, request, info);
return CheckPyError();
});
}
Status PyFlightServer::DoGet(const arrow::flight::ServerCallContext& context,
const arrow::flight::Ticket& request,
std::unique_ptr<arrow::flight::FlightDataStream>* stream) {
return SafeCallIntoPython([&] {
vtable_.do_get(server_.obj(), context, request, stream);
return CheckPyError();
});
}
Status PyFlightServer::DoPut(const arrow::flight::ServerCallContext& context,
std::unique_ptr<arrow::flight::FlightMessageReader> reader) {
return SafeCallIntoPython([&] {
vtable_.do_put(server_.obj(), context, std::move(reader));
return CheckPyError();
});
}
Status PyFlightServer::DoAction(const arrow::flight::ServerCallContext& context,
const arrow::flight::Action& action,
std::unique_ptr<arrow::flight::ResultStream>* result) {
return SafeCallIntoPython([&] {
vtable_.do_action(server_.obj(), context, action, result);
return CheckPyError();
});
}
Status PyFlightServer::ListActions(const arrow::flight::ServerCallContext& context,
std::vector<arrow::flight::ActionType>* actions) {
return SafeCallIntoPython([&] {
vtable_.list_actions(server_.obj(), context, actions);
return CheckPyError();
});
}
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}) {
struct sigaction handler;
int ret = sigaction(signum, nullptr, &handler);
if (ret != 0) {
return Status::IOError("sigaction call failed");
}
if (handler.sa_handler != SIG_DFL && handler.sa_handler != 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);
}
Status PyFlightResultStream::Next(std::unique_ptr<arrow::flight::Result>* result) {
return SafeCallIntoPython([=] {
callback_(generator_.obj(), result);
return CheckPyError();
});
}
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<arrow::Schema> PyFlightDataStream::schema() { return stream_->schema(); }
Status PyFlightDataStream::Next(arrow::flight::FlightPayload* payload) {
return stream_->Next(payload);
}
PyGeneratorFlightDataStream::PyGeneratorFlightDataStream(
PyObject* generator, std::shared_ptr<arrow::Schema> schema,
PyGeneratorFlightDataStreamCallback callback)
: schema_(schema), callback_(callback) {
Py_INCREF(generator);
generator_.reset(generator);
}
std::shared_ptr<arrow::Schema> PyGeneratorFlightDataStream::schema() { return schema_; }
Status PyGeneratorFlightDataStream::Next(arrow::flight::FlightPayload* payload) {
return SafeCallIntoPython([=] {
callback_(generator_.obj(), payload);
return CheckPyError();
});
}
Status CreateFlightInfo(const std::shared_ptr<arrow::Schema>& schema,
const arrow::flight::FlightDescriptor& descriptor,
const std::vector<arrow::flight::FlightEndpoint>& endpoints,
uint64_t total_records, uint64_t total_bytes,
std::unique_ptr<arrow::flight::FlightInfo>* out) {
arrow::flight::FlightInfo::Data flight_data;
RETURN_NOT_OK(arrow::flight::internal::SchemaToString(*schema, &flight_data.schema));
flight_data.descriptor = descriptor;
flight_data.endpoints = endpoints;
flight_data.total_records = total_records;
flight_data.total_bytes = total_bytes;
arrow::flight::FlightInfo value(flight_data);
*out = std::unique_ptr<arrow::flight::FlightInfo>(new arrow::flight::FlightInfo(value));
return Status::OK();
}
} // namespace flight
} // namespace py
} // namespace arrow