forked from restatedev/sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvm.py
More file actions
496 lines (405 loc) · 14.7 KB
/
vm.py
File metadata and controls
496 lines (405 loc) · 14.7 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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#
# Copyright (c) 2023-2024 - Restate Software, Inc., Restate GmbH
#
# This file is part of the Restate SDK for Python,
# which is released under the MIT license.
#
# You can find a copy of the license in file LICENSE in the root
# directory of this repository or package, or at
# https://github.com/restatedev/sdk-typescript/blob/main/LICENSE
#
"""
wrap the restate._internal.PyVM class
"""
# pylint: disable=E1101,R0917
# pylint: disable=too-many-arguments
# pylint: disable=too-few-public-methods
from typing import Optional
from datetime import timedelta
from dataclasses import dataclass
import typing
from restate._internal import (
PyVM,
PyHeader,
PyFailure,
VMException,
PySuspended,
PyVoid,
PyStateKeys,
PyExponentialRetryConfig,
PyDoProgressAnyCompleted,
PyDoProgressReadFromInput,
PyDoProgressExecuteRun,
PyDoWaitForPendingRun,
PyDoProgressCancelSignalReceived,
CANCEL_NOTIFICATION_HANDLE,
) # pylint: disable=import-error,no-name-in-module,line-too-long
@dataclass
class Invocation:
"""
Invocation dataclass
"""
invocation_id: str
random_seed: int
headers: typing.List[typing.Tuple[str, str]]
input_buffer: bytes
key: str
@dataclass
class RunRetryConfig:
"""
Exponential Retry Configuration
All duration/interval values are in milliseconds.
"""
initial_interval: typing.Optional[int] = None
max_attempts: typing.Optional[int] = None
max_duration: typing.Optional[int] = None
max_interval: typing.Optional[int] = None
interval_factor: typing.Optional[float] = None
@dataclass
class Failure:
"""
Failure
"""
code: int
message: str
stacktrace: typing.Optional[str] = None
@dataclass
class NotReady:
"""
NotReady
"""
NOT_READY = NotReady()
CANCEL_HANDLE = CANCEL_NOTIFICATION_HANDLE
NotificationType = typing.Optional[typing.Union[bytes, Failure, NotReady, list[str], str]]
class Suspended:
"""
Represents a suspended error
"""
SUSPENDED = Suspended()
class DoProgressAnyCompleted:
"""
Represents a notification that any of the handles has completed.
"""
class DoProgressReadFromInput:
"""
Represents a notification that the input needs to be read.
"""
class DoProgressExecuteRun:
"""
Represents a notification that a run needs to be executed.
"""
handle: int
def __init__(self, handle):
self.handle = handle
class DoProgressCancelSignalReceived:
"""
Represents a notification that a cancel signal has been received
"""
class DoWaitPendingRun:
"""
Represents a notification that a run is pending
"""
DO_PROGRESS_ANY_COMPLETED = DoProgressAnyCompleted()
DO_PROGRESS_READ_FROM_INPUT = DoProgressReadFromInput()
DO_PROGRESS_CANCEL_SIGNAL_RECEIVED = DoProgressCancelSignalReceived()
DO_WAIT_PENDING_RUN = DoWaitPendingRun()
DoProgressResult = typing.Union[
DoProgressAnyCompleted,
DoProgressReadFromInput,
DoProgressExecuteRun,
DoProgressCancelSignalReceived,
DoWaitPendingRun,
]
# pylint: disable=too-many-public-methods
class VMWrapper:
"""
A wrapper class for the restate_sdk._internal.PyVM class.
It provides a type-friendly interface to our shared vm.
"""
def __init__(self, headers: typing.List[typing.Tuple[str, str]]):
self.vm = PyVM(headers)
def get_response_head(self) -> typing.Tuple[int, typing.Iterable[typing.Tuple[str, str]]]:
"""
Retrieves the response head from the virtual machine.
Returns:
A tuple containing the status code and a list of header tuples.
"""
result = self.vm.get_response_head()
return (result.status_code, result.headers)
def notify_input(self, input_buf: bytes):
"""Send input to the virtual machine."""
self.vm.notify_input(input_buf)
def notify_input_closed(self):
"""Notify the virtual machine that the input has been closed."""
self.vm.notify_input_closed()
def notify_error(self, error: str, stacktrace: str, delay_override: Optional[timedelta] = None):
"""Notify the virtual machine of an error."""
self.vm.notify_error(
error, stacktrace, int(delay_override.total_seconds() * 1000) if delay_override is not None else None
)
def take_output(self) -> typing.Optional[bytes]:
"""Take the output from the virtual machine."""
return self.vm.take_output()
def is_ready_to_execute(self) -> bool:
"""Returns true when the VM is ready to operate."""
return self.vm.is_ready_to_execute()
def is_completed(self, handle: int) -> bool:
"""Returns true when the notification handle is completed and hasn't been taken yet."""
return self.vm.is_completed(handle)
# pylint: disable=R0911
def do_progress(self, handles: list[int]) -> typing.Union[DoProgressResult, Exception, Suspended]:
"""Do progress with notifications."""
try:
result = self.vm.do_progress(handles)
except VMException as e:
return e
if isinstance(result, PySuspended):
return SUSPENDED
if isinstance(result, PyDoProgressAnyCompleted):
return DO_PROGRESS_ANY_COMPLETED
if isinstance(result, PyDoProgressReadFromInput):
return DO_PROGRESS_READ_FROM_INPUT
if isinstance(result, PyDoProgressExecuteRun):
return DoProgressExecuteRun(result.handle)
if isinstance(result, PyDoProgressCancelSignalReceived):
return DO_PROGRESS_CANCEL_SIGNAL_RECEIVED
if isinstance(result, PyDoWaitForPendingRun):
return DO_WAIT_PENDING_RUN
return ValueError(f"Unknown progress type: {result}")
def take_notification(self, handle: int) -> typing.Union[NotificationType, Exception, Suspended]:
"""Take the result of an asynchronous operation."""
try:
result = self.vm.take_notification(handle)
except VMException as e:
return e
if isinstance(result, PySuspended):
return SUSPENDED
if result is None:
return NOT_READY
if isinstance(result, PyVoid):
# success with an empty value
return None
if isinstance(result, bytes):
# success with a non-empty value
return result
if isinstance(result, PyStateKeys):
# success with state keys
return result.keys
if isinstance(result, str):
# success with invocation id
return result
if isinstance(result, PyFailure):
# a terminal failure
code = result.code
message = result.message
return Failure(code, message)
return ValueError(f"Unknown result type: {result}")
def sys_input(self) -> Invocation:
"""
Retrieves the system input from the virtual machine.
Returns:
An instance of the Invocation class containing the system input.
"""
inp = self.vm.sys_input()
invocation_id: str = inp.invocation_id
random_seed: int = inp.random_seed
headers: typing.List[typing.Tuple[str, str]] = [(h.key, h.value) for h in inp.headers]
input_buffer: bytes = bytes(inp.input)
key: str = inp.key
return Invocation(
invocation_id=invocation_id, random_seed=random_seed, headers=headers, input_buffer=input_buffer, key=key
)
def sys_write_output_success(self, output: bytes):
"""
Writes the output to the system.
Args:
output: The output to be written. It can be either a bytes or a Failure object.
Returns:
None
"""
self.vm.sys_write_output_success(output)
def sys_write_output_failure(self, output: Failure):
"""
Writes the output to the system.
Args:
output: The output to be written. It can be either a bytes or a Failure object.
Returns:
None
"""
res = PyFailure(output.code, output.message)
self.vm.sys_write_output_failure(res)
def sys_get_state(self, name) -> int:
"""
Retrieves a key-value binding.
Args:
name: The name of the value to be retrieved.
Returns:
The value associated with the given name.
"""
return self.vm.sys_get_state(name)
def sys_get_state_keys(self) -> int:
"""
Retrieves all keys.
Returns:
The state keys
"""
return self.vm.sys_get_state_keys()
def sys_set_state(self, name: str, value: bytes):
"""
Sets a key-value binding.
Args:
name: The name of the value to be set.
value: The value to be set.
Returns:
None
"""
self.vm.sys_set_state(name, value)
def sys_clear_state(self, name: str):
"""Clear the state associated with the given name."""
self.vm.sys_clear_state(name)
def sys_clear_all_state(self):
"""Clear the state associated with the given name."""
self.vm.sys_clear_all_state()
def sys_sleep(self, millis: int, name: typing.Optional[str] = None):
"""Ask to sleep for a given duration"""
return self.vm.sys_sleep(millis, name)
def sys_call(
self,
service: str,
handler: str,
parameter: bytes,
key: typing.Optional[str] = None,
idempotency_key: typing.Optional[str] = None,
headers: typing.Optional[typing.List[typing.Tuple[str, str]]] = None,
):
"""Call a service"""
if headers:
headers = [PyHeader(key=h[0], value=h[1]) for h in headers]
return self.vm.sys_call(service, handler, parameter, key, idempotency_key, headers)
# pylint: disable=too-many-arguments
def sys_send(
self,
service: str,
handler: str,
parameter: bytes,
key: typing.Optional[str] = None,
delay: typing.Optional[int] = None,
idempotency_key: typing.Optional[str] = None,
headers: typing.Optional[typing.List[typing.Tuple[str, str]]] = None,
) -> int:
"""
send an invocation to a service, and return the handle
to the promise that will resolve with the invocation id
"""
if headers:
headers = [PyHeader(key=h[0], value=h[1]) for h in headers]
return self.vm.sys_send(service, handler, parameter, key, delay, idempotency_key, headers)
def sys_run(self, name: str) -> int:
"""
Register a run
"""
return self.vm.sys_run(name)
def sys_awakeable(self) -> typing.Tuple[str, int]:
"""
Return a fresh awaitable
"""
return self.vm.sys_awakeable()
def sys_resolve_awakeable(self, name: str, value: bytes):
"""
Resolve
"""
self.vm.sys_complete_awakeable_success(name, value)
def sys_reject_awakeable(self, name: str, failure: Failure):
"""
Reject
"""
py_failure = PyFailure(failure.code, failure.message)
self.vm.sys_complete_awakeable_failure(name, py_failure)
def propose_run_completion_success(self, handle: int, output: bytes) -> int:
"""
Exit a side effect
Args:
output: The output of the side effect.
Returns:
handle
"""
return self.vm.propose_run_completion_success(handle, output)
def sys_get_promise(self, name: str) -> int:
"""Returns the promise handle"""
return self.vm.sys_get_promise(name)
def sys_peek_promise(self, name: str) -> int:
"""Peek into the workflow promise"""
return self.vm.sys_peek_promise(name)
def sys_complete_promise_success(self, name: str, value: bytes) -> int:
"""Complete the promise"""
return self.vm.sys_complete_promise_success(name, value)
def sys_complete_promise_failure(self, name: str, failure: Failure) -> int:
"""reject the promise on failure"""
res = PyFailure(failure.code, failure.message)
return self.vm.sys_complete_promise_failure(name, res)
def propose_run_completion_failure(self, handle: int, output: Failure) -> int:
"""
Exit a side effect
Args:
name: The name of the side effect.
output: The output of the side effect.
"""
res = PyFailure(output.code, output.message)
return self.vm.propose_run_completion_failure(handle, res)
# pylint: disable=line-too-long
def propose_run_completion_transient(
self, handle: int, failure: Failure, attempt_duration_ms: int, config: RunRetryConfig
):
"""
Exit a side effect with a transient Error.
This requires a retry policy to be provided.
"""
py_failure = PyFailure(failure.code, failure.message, failure.stacktrace)
py_config = PyExponentialRetryConfig(
config.initial_interval,
config.max_attempts,
config.max_duration,
config.max_interval,
config.interval_factor,
)
self.vm.propose_run_completion_failure_transient(handle, py_failure, attempt_duration_ms, py_config)
def propose_run_completion_transient_with_delay_override(
self,
handle: int,
failure: Failure,
attempt_duration_ms: int,
delay_override: timedelta | None,
max_retry_attempts_override: int | None,
max_retry_duration_override: timedelta | None,
):
"""
Exit a side effect with a transient Error and override the retry policy with explicit parameters.
"""
py_failure = PyFailure(failure.code, failure.message, failure.stacktrace)
self.vm.propose_run_completion_failure_transient_with_delay_override(
handle,
py_failure,
attempt_duration_ms,
int(delay_override.total_seconds() * 1000) if delay_override else None,
max_retry_attempts_override,
int(max_retry_duration_override.total_seconds() * 1000) if max_retry_duration_override else None,
)
def sys_end(self):
"""
This method is responsible for ending the system.
It calls the `sys_end` method of the `vm` object.
"""
self.vm.sys_end()
def sys_cancel(self, invocation_id: str):
"""
Cancel a running invocation
"""
self.vm.sys_cancel(invocation_id)
def attach_invocation(self, invocation_id: str) -> int:
"""
Attach to an invocation
"""
return self.vm.attach_invocation(invocation_id)
def is_replaying(self) -> bool:
"""Returns true if the state machine is replaying."""
return self.vm.is_replaying()