-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtask.py
More file actions
221 lines (184 loc) · 7.65 KB
/
task.py
File metadata and controls
221 lines (184 loc) · 7.65 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
from dataclasses import dataclass
import decimal
from estuary_cdk.capture.connector_status import ConnectorStatus
from pydantic import Field
from typing import Generic, Awaitable, Any, BinaryIO, Callable
import orjson
import base64
from logging import Logger
import asyncio
import tempfile
import traceback
import xxhash
from . import request, response
from ._emit import emit_from_buffer
from ..flow import (
ConnectorSpec,
ConnectorState,
ConnectorStateUpdate,
EndpointConfig,
ResourceConfig,
)
from ..pydantic_polyfill import GenericModel
class Request(GenericModel, Generic[EndpointConfig, ResourceConfig, ConnectorState]):
spec: request.Spec | None = None
discover: request.Discover[EndpointConfig] | None = None
validate_: request.Validate[EndpointConfig, ResourceConfig] | None = Field(
default=None, alias="validate"
)
apply: request.Apply[EndpointConfig, ResourceConfig] | None = None
open: request.Open[EndpointConfig, ResourceConfig, ConnectorState] | None = None
acknowledge: request.Acknowledge | None = None
class Response(GenericModel, Generic[EndpointConfig, ResourceConfig, ConnectorState]):
spec: ConnectorSpec | None = None
discovered: response.Discovered[ResourceConfig] | None = None
validated: response.Validated | None = None
applied: response.Applied | None = None
opened: response.Opened | None = None
captured: response.Captured | None = None
sourcedSchema: response.SourcedSchema | None = None
checkpoint: response.Checkpoint[ConnectorState] | None = None
def orjson_default(obj):
# Pydantic automatically serializes Decimals as strings, but orjson doesn't
# know about that. In order to handle this, we must provide this as
# the default= kwarg to orjson.dumps
if isinstance(obj, decimal.Decimal):
return str(obj)
if isinstance(obj, (bytes, bytearray)):
return base64.b64encode(obj).decode('utf-8')
raise TypeError
@dataclass
class Task:
"""
Task bundles a capture coroutine and its associated context.
It facilitates the task in queuing any number of captured documents and
emitting them upon a checkpoint. Each instance of Task manages an independent
internal buffer (backed by memory or disk) which is only written to the
connector's output upon a call to checkpoint(). This allows concurrent
Tasks to capture consistent checkpoints without trampling one another.
Task also facilitates logging and graceful stop of a capture coroutine.
"""
log: Logger
"""Attached Logger of this Task instance, to use for scoped logging."""
connector_status: ConnectorStatus
"""Shared ConnectorStatus instance of the entire capture."""
@dataclass
class Stopping:
"""
Stopping coordinates the graceful exit of capture Tasks.
Its `event` is set when Tasks should gracefully stop.
The Task's coroutine should monitor this event and exit when it's set AND
it has no more immediate work to do (for example, no further documents are
currently ready to be captured).
"""
event: asyncio.Event
first_error: Exception | None = None
first_error_task: str | None = None
stopping: Stopping
_buffer: tempfile.SpooledTemporaryFile
_hasher: xxhash.xxh3_128
_name: str
_output: BinaryIO
_tg: asyncio.TaskGroup
MAX_BUFFER_MEM: int = 1_000_000
"""Maximum amount of memory to use for captured documents between checkpoints,
before spilling to disk."""
def __init__(
self,
log: Logger,
connector_status: ConnectorStatus,
name: str,
output: BinaryIO,
stopping: Stopping,
tg: asyncio.TaskGroup,
):
self._buffer = tempfile.SpooledTemporaryFile(max_size=self.MAX_BUFFER_MEM)
self._hasher = xxhash.xxh3_128()
self._name = name
self._output = output
self._tg = tg
self.log = log
self.connector_status = connector_status
self.stopping = stopping
def captured(self, binding: int, document: Any):
"""Enqueue the document to be captured under the given binding.
Documents are not actually captured until checkpoint() is called.
Or, reset() will discard any queued documents."""
if isinstance(document, dict):
b = orjson.dumps({
"captured": {
"binding": binding,
"doc": document,
}
}, default=orjson_default)
else:
b = Response(
captured=response.Captured(binding=binding, doc=document)
).model_dump_json(by_alias=True, exclude_unset=True).encode()
self._buffer.write(b)
self._buffer.write(b"\n")
self._hasher.update(b)
def pending_digest(self) -> str:
"""pending_digest returns the digest of captured() documents
since the last checkpoint() or reset()"""
return self._hasher.digest().hex()
def sourced_schema(self, binding_index: int, schema: dict[str, Any]):
"""Write a SourcedSchema message for the given binding to the buffer.
SourcedSchema messages won't be emitted until checkpoint() is called."""
b = Response(
sourcedSchema=response.SourcedSchema(binding=binding_index, schema_json=schema)
).model_dump_json(by_alias=True, exclude_unset=True).encode()
self._buffer.write(b)
self._buffer.write(b"\n")
async def checkpoint(self, state: ConnectorState, merge_patch: bool = True):
"""Emit previously-queued, captured documents followed by a checkpoint."""
await self._emit(
Response[Any, Any, ConnectorState](
checkpoint=response.Checkpoint(
state=ConnectorStateUpdate(updated=state, mergePatch=merge_patch)
)
)
)
def reset(self):
"""Discard any captured documents, resetting to an empty state."""
self._buffer.truncate(0)
self._buffer.seek(0)
self._hasher.reset()
def spawn_child(
self, name_suffix: str, child: Callable[["Task"], Awaitable[None]]
) -> asyncio.Task:
"""
Spawn a child Task of this Task, using the given name suffix and coroutine.
The child coroutine will be invoked with a child Task and be polled concurrently.
"""
child_name = f"{self._name}.{name_suffix}"
child_log = self.log.getChild(name_suffix)
async def run_task(parent: Task):
async with asyncio.TaskGroup() as child_tg:
try:
t = Task(
child_log,
parent.connector_status,
child_name,
parent._output,
parent.stopping,
child_tg,
)
await child(t)
except Exception as exc:
child_log.error("".join(traceback.format_exception(exc)))
if parent.stopping.first_error is None:
parent.stopping.first_error = exc
parent.stopping.first_error_task = child_name
parent.stopping.event.set()
task = self._tg.create_task(run_task(self))
task.set_name(child_name)
return task
async def _emit(self, response: Response[EndpointConfig, ResourceConfig, ConnectorState]):
self._buffer.write(
response.model_dump_json(by_alias=True, exclude_unset=True).encode()
)
self._buffer.write(b"\n")
self._buffer.seek(0)
await emit_from_buffer(self._buffer, self._output)
self.reset()