forked from gooddata/gooddata-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflight_methods.py
More file actions
356 lines (300 loc) · 14.2 KB
/
flight_methods.py
File metadata and controls
356 lines (300 loc) · 14.2 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
# (C) 2024 GoodData Corporation
import time
from collections.abc import Generator
from typing import Optional
import orjson
import pyarrow.flight
import structlog
from gooddata_flight_server import (
ErrorCode,
ErrorInfo,
FlightDataTaskResult,
FlightServerMethods,
ServerContext,
TaskExecutionResult,
TaskWaitTimeoutError,
flight_server_methods,
)
from gooddata_flexconnect.function.function import FlexConnectFunction
from gooddata_flexconnect.function.function_invocation import (
CancelInvocation,
RetryInvocation,
SubmitInvocation,
extract_pollable_invocation_from_descriptor,
extract_submit_invocation_from_descriptor,
)
from gooddata_flexconnect.function.function_registry import FlexConnectFunctionRegistry
from gooddata_flexconnect.function.function_task import FlexConnectFunctionTask
_LOGGER = structlog.get_logger("gooddata_flexconnect.rpc")
POLLING_HEADER_NAME = "x-quiver-pollable"
"""
If this header is present on the get flight info call, the polling extension will be used.
Otherwise the basic do get will be used.
"""
def _prepare_poll_error(task_id: str) -> pyarrow.flight.FlightError:
return ErrorInfo.poll(
flight_info=None,
cancel_descriptor=pyarrow.flight.FlightDescriptor.for_command(f"c:{task_id}".encode()),
retry_descriptor=pyarrow.flight.FlightDescriptor.for_command(f"r:{task_id}".encode()),
)
class _FlexConnectServerMethods(FlightServerMethods):
def __init__(
self,
ctx: ServerContext,
registry: FlexConnectFunctionRegistry,
call_deadline_ms: float,
poll_interval_ms: float,
) -> None:
self._ctx = ctx
self._registry = registry
self._call_deadline = call_deadline_ms / 1000
self._poll_interval = poll_interval_ms / 1000
@staticmethod
def _create_descriptor(fun_name: str, metadata: Optional[dict]) -> pyarrow.flight.FlightDescriptor:
cmd = {
"functionName": fun_name,
"metadata": metadata,
}
return pyarrow.flight.FlightDescriptor.for_command(orjson.dumps(cmd))
def _create_fun_info(self, fun: type[FlexConnectFunction]) -> pyarrow.flight.FlightInfo:
# these are for type checker; the registry will only register functions
# that have proper metadata on them
assert fun.Name is not None
assert fun.Schema is not None
return pyarrow.flight.FlightInfo(
schema=fun.Schema,
descriptor=self._create_descriptor(fun.Name, fun.Metadata),
endpoints=[],
total_bytes=-1,
total_records=-1,
)
def _prepare_task(
self,
context: pyarrow.flight.ServerCallContext,
submit_invocation: SubmitInvocation,
) -> FlexConnectFunctionTask:
headers = self.call_info_middleware(context).headers
fun = self._registry.create_function(submit_invocation.function_name)
return FlexConnectFunctionTask(
fun=fun,
parameters=submit_invocation.parameters,
columns=submit_invocation.columns,
headers=headers,
cmd=submit_invocation.command,
)
def _prepare_flight_info(
self, task_id: str, task_result: Optional[TaskExecutionResult]
) -> pyarrow.flight.FlightInfo:
if task_result is None:
raise ErrorInfo.for_reason(
ErrorCode.BAD_ARGUMENT, f"Task with id '{task_id}' does not exist."
).to_user_error()
if task_result.error is not None:
raise task_result.error.as_flight_error()
if task_result.cancelled:
raise ErrorInfo.for_reason(
ErrorCode.COMMAND_CANCELLED,
f"FlexConnect function invocation was cancelled. Invocation task was: '{task_id}'.",
).to_server_error()
result = task_result.result
assert isinstance(result, FlightDataTaskResult)
return pyarrow.flight.FlightInfo(
schema=result.get_schema(),
descriptor=pyarrow.flight.FlightDescriptor.for_command(task_result.cmd),
endpoints=[
pyarrow.flight.FlightEndpoint(
ticket=pyarrow.flight.Ticket(ticket=orjson.dumps({"task_id": task_id})),
locations=[self._ctx.location],
)
],
total_records=-1,
total_bytes=-1,
)
def _get_flight_info_no_polling(
self,
context: pyarrow.flight.ServerCallContext,
descriptor: pyarrow.flight.FlightDescriptor,
) -> pyarrow.flight.FlightInfo:
"""
Basic DoGetInfo flow with no polling extension.
This conforms to the mainline Arrow Flight RPC specification.
"""
structlog.contextvars.bind_contextvars(peer=context.peer())
invocation = extract_submit_invocation_from_descriptor(descriptor)
task: Optional[FlexConnectFunctionTask] = None
try:
task = self._prepare_task(context, invocation)
self._ctx.task_executor.submit(task)
try:
task_result = self._ctx.task_executor.wait_for_result(task.task_id, self._call_deadline)
except TaskWaitTimeoutError:
cancelled = self._ctx.task_executor.cancel(task.task_id)
_LOGGER.warning(
"flexconnect_fun_call_timeout", task_id=task.task_id, fun=task.fun_name, cancelled=cancelled
)
raise ErrorInfo.for_reason(
ErrorCode.TIMEOUT, f"GetFlightInfo timed out while waiting for task {task.task_id}."
).to_timeout_error()
# if this bombs then there must be something really wrong because the task
# was clearly submitted and code was waiting for its completion. this invariant
# should not happen in this particular code path. The None return value may
# be applicable one day when polling is in use and a request comes to check whether
# particular task id finished
assert task_result is not None
return self._prepare_flight_info(task_id=task.task_id, task_result=task_result)
except Exception:
if task is not None:
_LOGGER.error(
"get_flight_info_failed", task_id=task.task_id, fun=task.fun_name, exc_info=True, polling=False
)
else:
_LOGGER.error("flexconnect_fun_submit_failed", exc_info=True, polling=False)
raise
def _get_flight_info_polling(
self,
context: pyarrow.flight.ServerCallContext,
descriptor: pyarrow.flight.FlightDescriptor,
) -> pyarrow.flight.FlightInfo:
"""
DoGetInfo flow with polling extension.
This extends the mainline Arrow Flight RPC specification with polling capabilities using the RetryInfo
encoded into the FlightTimedOutError.extra_info.
Ideally, we would use the mainline PollFlightInfo, but that has yet to be implemented in the PyArrow library.
"""
structlog.contextvars.bind_contextvars(peer=context.peer())
invocation = extract_pollable_invocation_from_descriptor(descriptor)
task_id: str
fun_name: Optional[str] = None
if isinstance(invocation, CancelInvocation):
# cancel the given task and raise cancellation exception
if self._ctx.task_executor.cancel(invocation.task_id):
raise ErrorInfo.for_reason(
ErrorCode.COMMAND_CANCELLED, "FlexConnect function invocation was cancelled."
).to_cancelled_error()
raise ErrorInfo.for_reason(
ErrorCode.COMMAND_CANCEL_NOT_POSSIBLE, "FlexConnect function invocation could not be cancelled."
).to_cancelled_error()
elif isinstance(invocation, RetryInvocation):
# retry descriptor: extract the task_id, do not submit it again and do one polling iteration
task_id = invocation.task_id
elif isinstance(invocation, SubmitInvocation):
# basic first-time submit: submit the task and do one polling iteration.
# do not check call deadline to give it a chance to wait for the result at least once
try:
task = self._prepare_task(context, invocation)
self._ctx.task_executor.submit(task)
task_id = task.task_id
fun_name = task.fun_name
except Exception:
_LOGGER.error("flexconnect_fun_submit_failed", exc_info=True, polling=True)
raise
else:
# can be replaced by assert_never when we are on 3.11
raise AssertionError
try:
task_result = self._ctx.task_executor.wait_for_result(task_id, timeout=self._poll_interval)
return self._prepare_flight_info(task_id, task_result)
except TimeoutError:
# first, check the call deadline for the whole call duration
task_timestamp = self._ctx.task_executor.get_task_submitted_timestamp(task_id)
if task_timestamp is not None and time.perf_counter() - task_timestamp > self._call_deadline:
self._ctx.task_executor.cancel(task_id)
raise ErrorInfo.for_reason(
ErrorCode.TIMEOUT, f"GetFlightInfo timed out while waiting for task {task_id}."
).to_timeout_error()
# if the result is not ready, and we still have time, indicate to the client
# how to poll for the results
raise _prepare_poll_error(task_id)
except Exception:
_LOGGER.error("get_flight_info_failed", task_id=task_id, fun=fun_name, exc_info=True, polling=True)
raise
###################################################################
# Implementation of Flight RPC methods
###################################################################
def list_flights(
self, context: pyarrow.flight.ServerCallContext, criteria: bytes
) -> Generator[pyarrow.flight.FlightInfo, None, None]:
structlog.contextvars.bind_contextvars(peer=context.peer())
_LOGGER.info("list_flights", available_funs=self._registry.function_names)
return (self._create_fun_info(fun) for fun in self._registry.functions.values())
def get_flight_info(
self,
context: pyarrow.flight.ServerCallContext,
descriptor: pyarrow.flight.FlightDescriptor,
) -> pyarrow.flight.FlightInfo:
structlog.contextvars.bind_contextvars(peer=context.peer())
headers = self.call_info_middleware(context).headers
allow_polling = headers.get(POLLING_HEADER_NAME) is not None
if allow_polling:
return self._get_flight_info_polling(context, descriptor)
else:
return self._get_flight_info_no_polling(context, descriptor)
def do_get(
self,
context: pyarrow.flight.ServerCallContext,
ticket: pyarrow.flight.Ticket,
) -> pyarrow.flight.FlightDataStream:
structlog.contextvars.bind_contextvars(peer=context.peer())
try:
try:
ticket_payload = orjson.loads(ticket.ticket)
except Exception:
raise ErrorInfo.bad_argument("Incorrect ticket payload. The ticket payload is not a valid JSON.")
task_id = ticket_payload.get("task_id")
if task_id is None or not len(task_id):
raise ErrorInfo.bad_argument("Incorrect ticket payload. The ticket payload does not specify 'task_id'.")
return self.do_get_task_result(context, self._ctx.task_executor, task_id)
except Exception:
_LOGGER.error("do_get_failed", exc_info=True)
raise
_FLEX_CONNECT_CONFIG_SECTION = "flexconnect"
_FLEX_CONNECT_FUNCTION_LIST = "functions"
_FLEX_CONNECT_CALL_DEADLINE_MS = "call_deadline_ms"
_FLEX_CONNECT_POLLING_INTERVAL_MS = "polling_interval_ms"
_DEFAULT_FLEX_CONNECT_CALL_DEADLINE_MS = 180_000
_DEFAULT_FLEX_CONNECT_POLLING_INTERVAL_MS = 2000
def _read_call_deadline_ms(ctx: ServerContext) -> int:
call_deadline = ctx.settings.get(f"{_FLEX_CONNECT_CONFIG_SECTION}.{_FLEX_CONNECT_CALL_DEADLINE_MS}")
if call_deadline is None:
return _DEFAULT_FLEX_CONNECT_CALL_DEADLINE_MS
try:
call_deadline_ms = int(call_deadline)
if call_deadline_ms <= 0:
raise ValueError()
return call_deadline_ms
except ValueError:
raise ValueError(
f"Value of {_FLEX_CONNECT_CONFIG_SECTION}.{_FLEX_CONNECT_CALL_DEADLINE_MS} must "
f"be a positive number - duration, in milliseconds, that FlexConnect function "
f"calls are expected to run."
)
def _read_polling_interval_ms(ctx: ServerContext) -> int:
polling_interval = ctx.settings.get(f"{_FLEX_CONNECT_CONFIG_SECTION}.{_FLEX_CONNECT_POLLING_INTERVAL_MS}")
if polling_interval is None:
return _DEFAULT_FLEX_CONNECT_POLLING_INTERVAL_MS
try:
polling_interval = int(polling_interval)
if polling_interval <= 0:
raise ValueError()
return polling_interval
except ValueError:
raise ValueError(
f"Value of {_FLEX_CONNECT_CONFIG_SECTION}.{_FLEX_CONNECT_POLLING_INTERVAL_MS} must "
f"be a positive number - duration, in milliseconds, that FlexConnect function "
f"waits for the result during one polling iteration."
)
@flight_server_methods
def create_flexconnect_flight_methods(ctx: ServerContext) -> FlightServerMethods:
"""
This factory creates implementation of Flight RPC methods that realize the FlexConnect server.
FlexConnect Server hosts one or more functions developed externally, and linked to the server
at runtime - during startup.
:param ctx: server's context
:return: new instance of Flight RPC server methods to integrate into the server
"""
modules = list(ctx.settings.get(f"{_FLEX_CONNECT_CONFIG_SECTION}.{_FLEX_CONNECT_FUNCTION_LIST}") or [])
call_deadline_ms = _read_call_deadline_ms(ctx)
polling_interval_ms = _read_polling_interval_ms(ctx)
_LOGGER.info("flexconnect_init", modules=modules)
registry = FlexConnectFunctionRegistry().load(ctx, modules)
return _FlexConnectServerMethods(ctx, registry, call_deadline_ms, polling_interval_ms)