de-hasta 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- de_hasta-0.4.0/.gitignore +12 -0
- de_hasta-0.4.0/LICENSE +201 -0
- de_hasta-0.4.0/PKG-INFO +72 -0
- de_hasta-0.4.0/README.md +52 -0
- de_hasta-0.4.0/pyproject.toml +32 -0
- de_hasta-0.4.0/src/hasta/__init__.py +39 -0
- de_hasta-0.4.0/src/hasta/cli.py +193 -0
- de_hasta-0.4.0/src/hasta/loader.py +42 -0
- de_hasta-0.4.0/src/hasta/model.py +192 -0
- de_hasta-0.4.0/src/hasta/py.typed +1 -0
- de_hasta-0.4.0/src/hasta/redact.py +41 -0
- de_hasta-0.4.0/src/hasta/runner.py +670 -0
- de_hasta-0.4.0/src/hasta/sink.py +83 -0
- de_hasta-0.4.0/src/hasta/sources/__init__.py +6 -0
- de_hasta-0.4.0/src/hasta/sources/base.py +13 -0
- de_hasta-0.4.0/src/hasta/sources/outbox.py +193 -0
- de_hasta-0.4.0/src/hasta/sources/rest_poll.py +106 -0
- de_hasta-0.4.0/src/hasta/spec.py +193 -0
- de_hasta-0.4.0/src/hasta/status.py +109 -0
- de_hasta-0.4.0/tests/test_cdc.py +306 -0
- de_hasta-0.4.0/tests/test_operations.py +339 -0
- de_hasta-0.4.0/tests/test_redact.py +68 -0
- de_hasta-0.4.0/tests/test_resume_100k.py +189 -0
- de_hasta-0.4.0/tests/test_stream.py +329 -0
de_hasta-0.4.0/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 KooEric
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
de_hasta-0.4.0/PKG-INFO
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: de-hasta
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: Hasta — leased REST and Postgres outbox microbatch streaming
|
|
5
|
+
License-Expression: Apache-2.0
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Python: >=3.11
|
|
8
|
+
Requires-Dist: arsenal-core<0.5,>=0.4
|
|
9
|
+
Requires-Dist: de-scorpio<0.5,>=0.4
|
|
10
|
+
Requires-Dist: de-scutum<0.5,>=0.4
|
|
11
|
+
Requires-Dist: httpx>=0.27
|
|
12
|
+
Requires-Dist: pugio<0.5,>=0.4
|
|
13
|
+
Requires-Dist: pyarrow>=17
|
|
14
|
+
Requires-Dist: pydantic>=2.7
|
|
15
|
+
Requires-Dist: pyyaml>=6
|
|
16
|
+
Requires-Dist: typer>=0.12
|
|
17
|
+
Provides-Extra: postgres
|
|
18
|
+
Requires-Dist: psycopg[binary]>=3.1; extra == 'postgres'
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# Hasta (`de-hasta`)
|
|
22
|
+
|
|
23
|
+
Leased REST and Postgres outbox microbatch ingestion with durable checkpoints
|
|
24
|
+
and deterministic batch identities. CDC v1 consumes immutable `I`/`U`/`D`
|
|
25
|
+
outbox envelopes ordered by `sequence`, then applies them transactionally to
|
|
26
|
+
DuckDB or Postgres with an event-id ledger for replay safety.
|
|
27
|
+
|
|
28
|
+
`hasta run stream.yaml --once` processes at most one available batch. A table
|
|
29
|
+
cursor declaration is validated conservatively but is not treated as CDC: it
|
|
30
|
+
cannot observe hard deletes, so use an outbox when deletes matter. Kafka,
|
|
31
|
+
binlog/WAL or other log-based CDC, raw SQL, and long-running daemon supervision
|
|
32
|
+
are not part of v1.
|
|
33
|
+
|
|
34
|
+
## Quickstart
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
python examples/hasta/fake_orders_api.py # terminal 1: a local cursor API
|
|
38
|
+
hasta run examples/hasta/orders-stream.yaml # terminal 2: stream until stopped
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
`Ctrl-C` (or `SIGTERM` from a supervisor) finishes the batch in flight, commits
|
|
42
|
+
it, releases the lease and prints the run report with `"stopped": true`. A second
|
|
43
|
+
signal aborts immediately; the next run resumes from the last committed batch.
|
|
44
|
+
|
|
45
|
+
## Operating a stream
|
|
46
|
+
|
|
47
|
+
| Command | What it does |
|
|
48
|
+
|---|---|
|
|
49
|
+
| `hasta run spec.yaml --once` / `--max-batches N` | Process one or N batches, then exit |
|
|
50
|
+
| `hasta checkpoint show spec.yaml` | JSON: checkpoint (generation, position, watermark), lease holder and expiry, ledger totals by status, rows/bytes/late rows, sink target. `hasta status` is an alias |
|
|
51
|
+
| `hasta checkpoint reset spec.yaml` | Dry-run: prints the checkpoint before/after and what will be re-written |
|
|
52
|
+
| `hasta checkpoint reset spec.yaml --confirm` | Starts a new generation from the source's beginning. Refused while a live runner holds the lease |
|
|
53
|
+
|
|
54
|
+
A reset keeps the batch ledger for audit and gives every replayed batch a new
|
|
55
|
+
identity, so the replay is written again. A Parquet sink appends files: clear
|
|
56
|
+
its output first if you do not want both copies. DuckDB/Postgres sinks upsert
|
|
57
|
+
by `merge_key`.
|
|
58
|
+
|
|
59
|
+
### Guarantees
|
|
60
|
+
|
|
61
|
+
Each batch is written before its checkpoint advances, and the checkpoint commit
|
|
62
|
+
is fenced by the lease. A crash at any point replays at most the uncommitted
|
|
63
|
+
batch under the same identity, which the sink writes idempotently: 100,000
|
|
64
|
+
events through 12 crashing runners arrive exactly once
|
|
65
|
+
(`tests/test_resume_100k.py`).
|
|
66
|
+
|
|
67
|
+
### Limits (v1)
|
|
68
|
+
|
|
69
|
+
- A cursor API that returns a null cursor ends the stream permanently
|
|
70
|
+
(`_hasta_exhausted`). For a never-ending stream, the API must keep returning
|
|
71
|
+
the last cursor on empty pages, as `fake_orders_api.py` does.
|
|
72
|
+
- One runner per stream (single-host SQLite lease); no daemon supervision.
|
de_hasta-0.4.0/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# Hasta (`de-hasta`)
|
|
2
|
+
|
|
3
|
+
Leased REST and Postgres outbox microbatch ingestion with durable checkpoints
|
|
4
|
+
and deterministic batch identities. CDC v1 consumes immutable `I`/`U`/`D`
|
|
5
|
+
outbox envelopes ordered by `sequence`, then applies them transactionally to
|
|
6
|
+
DuckDB or Postgres with an event-id ledger for replay safety.
|
|
7
|
+
|
|
8
|
+
`hasta run stream.yaml --once` processes at most one available batch. A table
|
|
9
|
+
cursor declaration is validated conservatively but is not treated as CDC: it
|
|
10
|
+
cannot observe hard deletes, so use an outbox when deletes matter. Kafka,
|
|
11
|
+
binlog/WAL or other log-based CDC, raw SQL, and long-running daemon supervision
|
|
12
|
+
are not part of v1.
|
|
13
|
+
|
|
14
|
+
## Quickstart
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
python examples/hasta/fake_orders_api.py # terminal 1: a local cursor API
|
|
18
|
+
hasta run examples/hasta/orders-stream.yaml # terminal 2: stream until stopped
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`Ctrl-C` (or `SIGTERM` from a supervisor) finishes the batch in flight, commits
|
|
22
|
+
it, releases the lease and prints the run report with `"stopped": true`. A second
|
|
23
|
+
signal aborts immediately; the next run resumes from the last committed batch.
|
|
24
|
+
|
|
25
|
+
## Operating a stream
|
|
26
|
+
|
|
27
|
+
| Command | What it does |
|
|
28
|
+
|---|---|
|
|
29
|
+
| `hasta run spec.yaml --once` / `--max-batches N` | Process one or N batches, then exit |
|
|
30
|
+
| `hasta checkpoint show spec.yaml` | JSON: checkpoint (generation, position, watermark), lease holder and expiry, ledger totals by status, rows/bytes/late rows, sink target. `hasta status` is an alias |
|
|
31
|
+
| `hasta checkpoint reset spec.yaml` | Dry-run: prints the checkpoint before/after and what will be re-written |
|
|
32
|
+
| `hasta checkpoint reset spec.yaml --confirm` | Starts a new generation from the source's beginning. Refused while a live runner holds the lease |
|
|
33
|
+
|
|
34
|
+
A reset keeps the batch ledger for audit and gives every replayed batch a new
|
|
35
|
+
identity, so the replay is written again. A Parquet sink appends files: clear
|
|
36
|
+
its output first if you do not want both copies. DuckDB/Postgres sinks upsert
|
|
37
|
+
by `merge_key`.
|
|
38
|
+
|
|
39
|
+
### Guarantees
|
|
40
|
+
|
|
41
|
+
Each batch is written before its checkpoint advances, and the checkpoint commit
|
|
42
|
+
is fenced by the lease. A crash at any point replays at most the uncommitted
|
|
43
|
+
batch under the same identity, which the sink writes idempotently: 100,000
|
|
44
|
+
events through 12 crashing runners arrive exactly once
|
|
45
|
+
(`tests/test_resume_100k.py`).
|
|
46
|
+
|
|
47
|
+
### Limits (v1)
|
|
48
|
+
|
|
49
|
+
- A cursor API that returns a null cursor ends the stream permanently
|
|
50
|
+
(`_hasta_exhausted`). For a never-ending stream, the API must keep returning
|
|
51
|
+
the last cursor on empty pages, as `fake_orders_api.py` does.
|
|
52
|
+
- One runner per stream (single-host SQLite lease); no daemon supervision.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "de-hasta"
|
|
3
|
+
version = "0.4.0"
|
|
4
|
+
description = "Hasta — leased REST and Postgres outbox microbatch streaming"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.11"
|
|
7
|
+
license = "Apache-2.0"
|
|
8
|
+
license-files = ["LICENSE"]
|
|
9
|
+
dependencies = [
|
|
10
|
+
"arsenal-core>=0.4,<0.5",
|
|
11
|
+
"pugio>=0.4,<0.5",
|
|
12
|
+
"de-scutum>=0.4,<0.5",
|
|
13
|
+
"de-scorpio>=0.4,<0.5",
|
|
14
|
+
"httpx>=0.27",
|
|
15
|
+
"pyarrow>=17",
|
|
16
|
+
"pydantic>=2.7",
|
|
17
|
+
"pyyaml>=6",
|
|
18
|
+
"typer>=0.12",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[project.optional-dependencies]
|
|
22
|
+
postgres = ["psycopg[binary]>=3.1"]
|
|
23
|
+
|
|
24
|
+
[project.scripts]
|
|
25
|
+
hasta = "hasta.cli:app"
|
|
26
|
+
|
|
27
|
+
[build-system]
|
|
28
|
+
requires = ["hatchling"]
|
|
29
|
+
build-backend = "hatchling.build"
|
|
30
|
+
|
|
31
|
+
[tool.hatch.build.targets.wheel]
|
|
32
|
+
packages = ["src/hasta"]
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""Hasta — leased REST microbatch streaming with durable checkpoints."""
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
4
|
+
|
|
5
|
+
from hasta.model import (
|
|
6
|
+
BatchLimits,
|
|
7
|
+
CDCEvent,
|
|
8
|
+
CdcEvent,
|
|
9
|
+
ChangeEvent,
|
|
10
|
+
CommittedBatch,
|
|
11
|
+
MicroBatch,
|
|
12
|
+
StreamPosition,
|
|
13
|
+
batch_identity,
|
|
14
|
+
cdc_event_hash,
|
|
15
|
+
)
|
|
16
|
+
from hasta.runner import CdcRunner, StreamRunner
|
|
17
|
+
from hasta.spec import DatabaseOutboxSourceSpec, StreamSpec, TableCursorSourceSpec
|
|
18
|
+
|
|
19
|
+
try:
|
|
20
|
+
__version__ = version("de-hasta")
|
|
21
|
+
except PackageNotFoundError:
|
|
22
|
+
__version__ = "0.4.0"
|
|
23
|
+
|
|
24
|
+
__all__ = [
|
|
25
|
+
"BatchLimits",
|
|
26
|
+
"CDCEvent",
|
|
27
|
+
"ChangeEvent",
|
|
28
|
+
"CdcEvent",
|
|
29
|
+
"CdcRunner",
|
|
30
|
+
"CommittedBatch",
|
|
31
|
+
"MicroBatch",
|
|
32
|
+
"StreamPosition",
|
|
33
|
+
"StreamRunner",
|
|
34
|
+
"StreamSpec",
|
|
35
|
+
"DatabaseOutboxSourceSpec",
|
|
36
|
+
"TableCursorSourceSpec",
|
|
37
|
+
"batch_identity",
|
|
38
|
+
"cdc_event_hash",
|
|
39
|
+
]
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"""Hasta foreground stream CLI."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import signal
|
|
7
|
+
import threading
|
|
8
|
+
import uuid
|
|
9
|
+
from collections.abc import Generator
|
|
10
|
+
from contextlib import contextmanager
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from types import FrameType
|
|
13
|
+
from typing import Any
|
|
14
|
+
|
|
15
|
+
import typer
|
|
16
|
+
|
|
17
|
+
from arsenal_core.errors import ArsenalError
|
|
18
|
+
from arsenal_core.state import StateStore
|
|
19
|
+
from hasta.loader import load_stream
|
|
20
|
+
from hasta.runner import StreamRunner
|
|
21
|
+
from hasta.status import state_path, stream_resource, stream_status
|
|
22
|
+
|
|
23
|
+
app = typer.Typer(help="Hasta — leased REST microbatch streaming.")
|
|
24
|
+
checkpoint_app = typer.Typer(help="Inspect or reset a stream's durable checkpoint.")
|
|
25
|
+
app.add_typer(checkpoint_app, name="checkpoint")
|
|
26
|
+
|
|
27
|
+
_STOP_SIGNALS = (signal.SIGINT, signal.SIGTERM)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@contextmanager
|
|
31
|
+
def graceful_stop(stop: threading.Event) -> Generator[None, None, None]:
|
|
32
|
+
"""First SIGINT/SIGTERM finishes the in-flight batch; a second one aborts."""
|
|
33
|
+
|
|
34
|
+
previous: dict[int, Any] = {}
|
|
35
|
+
|
|
36
|
+
def handle(signum: int, frame: FrameType | None) -> None:
|
|
37
|
+
del frame
|
|
38
|
+
if stop.is_set():
|
|
39
|
+
raise KeyboardInterrupt
|
|
40
|
+
typer.echo(
|
|
41
|
+
f"received {signal.Signals(signum).name}: finishing the current batch, "
|
|
42
|
+
"then releasing the lease (send again to abort)",
|
|
43
|
+
err=True,
|
|
44
|
+
)
|
|
45
|
+
stop.set()
|
|
46
|
+
|
|
47
|
+
if threading.current_thread() is not threading.main_thread():
|
|
48
|
+
yield # signal handlers can only be installed from the main thread
|
|
49
|
+
return
|
|
50
|
+
for signum in _STOP_SIGNALS:
|
|
51
|
+
previous[signum] = signal.signal(signum, handle)
|
|
52
|
+
try:
|
|
53
|
+
yield
|
|
54
|
+
finally:
|
|
55
|
+
for signum, handler in previous.items():
|
|
56
|
+
signal.signal(signum, handler)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@app.command(name="run")
|
|
60
|
+
def run_cmd(
|
|
61
|
+
spec_path: Path,
|
|
62
|
+
once: bool = typer.Option(False, "--once", help="Process at most one available microbatch."),
|
|
63
|
+
max_batches: int | None = typer.Option(None, "--max-batches", min=1),
|
|
64
|
+
) -> None:
|
|
65
|
+
"""Run a stream in the foreground, or one deterministic batch with --once."""
|
|
66
|
+
|
|
67
|
+
if once and max_batches is not None:
|
|
68
|
+
typer.echo("error: --once and --max-batches cannot be used together", err=True)
|
|
69
|
+
raise typer.Exit(2)
|
|
70
|
+
runner: StreamRunner | None = None
|
|
71
|
+
stop = threading.Event()
|
|
72
|
+
try:
|
|
73
|
+
runner = StreamRunner(load_stream(spec_path), stop=stop)
|
|
74
|
+
with graceful_stop(stop):
|
|
75
|
+
report = (
|
|
76
|
+
runner.run_once()
|
|
77
|
+
if once
|
|
78
|
+
else runner.run(max_batches=max_batches, wait_between_polls=True)
|
|
79
|
+
)
|
|
80
|
+
except (ArsenalError, RuntimeError, ValueError, NotImplementedError) as error:
|
|
81
|
+
typer.echo(f"error: {error}", err=True)
|
|
82
|
+
raise typer.Exit(1) from error
|
|
83
|
+
finally:
|
|
84
|
+
if runner is not None:
|
|
85
|
+
runner.close()
|
|
86
|
+
typer.echo(json.dumps(report.__dict__, sort_keys=True))
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _show(spec_path: Path, max_lag_seconds: float | None) -> None:
|
|
90
|
+
try:
|
|
91
|
+
spec = load_stream(spec_path)
|
|
92
|
+
store = StateStore(state_path(spec))
|
|
93
|
+
try:
|
|
94
|
+
payload = stream_status(spec, store, max_lag_seconds=max_lag_seconds)
|
|
95
|
+
finally:
|
|
96
|
+
store.close()
|
|
97
|
+
except ArsenalError as error:
|
|
98
|
+
typer.echo(f"error: {error}", err=True)
|
|
99
|
+
raise typer.Exit(1) from error
|
|
100
|
+
typer.echo(json.dumps(payload, sort_keys=True))
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
_MAX_LAG = typer.Option(
|
|
104
|
+
None,
|
|
105
|
+
"--max-lag-seconds",
|
|
106
|
+
min=1,
|
|
107
|
+
help="Alert when the last commit is older than this (default: 5 polls, min 60 s).",
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
@app.command(name="status")
|
|
112
|
+
def status_cmd(spec_path: Path, max_lag_seconds: float | None = _MAX_LAG) -> None:
|
|
113
|
+
"""Show health, checkpoint, lease and ledger without polling (= checkpoint show)."""
|
|
114
|
+
|
|
115
|
+
_show(spec_path, max_lag_seconds)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@checkpoint_app.command(name="show")
|
|
119
|
+
def checkpoint_show(spec_path: Path, max_lag_seconds: float | None = _MAX_LAG) -> None:
|
|
120
|
+
"""Show health, checkpoint, lease holder/expiry and batch ledger totals as JSON."""
|
|
121
|
+
|
|
122
|
+
_show(spec_path, max_lag_seconds)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
@checkpoint_app.command(name="reset")
|
|
126
|
+
def checkpoint_reset(
|
|
127
|
+
spec_path: Path,
|
|
128
|
+
confirm: bool = typer.Option(
|
|
129
|
+
False, "--confirm", help="Apply the reset. Without it, only print the plan."
|
|
130
|
+
),
|
|
131
|
+
) -> None:
|
|
132
|
+
"""Replay the stream from the source's beginning under a new generation.
|
|
133
|
+
|
|
134
|
+
Dry-run by default. Refuses while a live runner holds the lease. Batches
|
|
135
|
+
replayed after a reset get new identities and are written again: a sink
|
|
136
|
+
that appends files (Parquet) will hold both copies unless its output is
|
|
137
|
+
cleared first.
|
|
138
|
+
"""
|
|
139
|
+
|
|
140
|
+
try:
|
|
141
|
+
spec = load_stream(spec_path)
|
|
142
|
+
store = StateStore(state_path(spec))
|
|
143
|
+
try:
|
|
144
|
+
before = stream_status(spec, store)
|
|
145
|
+
current = before["checkpoint"]
|
|
146
|
+
next_generation = (current["generation"] if current else 0) + 1
|
|
147
|
+
after: dict[str, Any] = {
|
|
148
|
+
"generation": next_generation,
|
|
149
|
+
"position": {},
|
|
150
|
+
"watermark": None,
|
|
151
|
+
"last_batch_id": None,
|
|
152
|
+
}
|
|
153
|
+
plan: dict[str, Any] = {
|
|
154
|
+
"action": "reset" if confirm else "dry-run",
|
|
155
|
+
"before": current,
|
|
156
|
+
"after": after,
|
|
157
|
+
"warning": (
|
|
158
|
+
f"the next run re-reads the source from the beginning and writes every "
|
|
159
|
+
f"batch again to {before['sink']['type']} '{before['sink']['target']}'; "
|
|
160
|
+
"clear that output first if the sink does not replace existing data"
|
|
161
|
+
),
|
|
162
|
+
}
|
|
163
|
+
lease_info = before["lease"]
|
|
164
|
+
if lease_info is not None and not lease_info["expired"]:
|
|
165
|
+
typer.echo(json.dumps({**plan, "refused": "stream lease is held"}, sort_keys=True))
|
|
166
|
+
typer.echo(
|
|
167
|
+
f"error: stream is running (lease held by {lease_info['owner_id']}); "
|
|
168
|
+
"stop it first",
|
|
169
|
+
err=True,
|
|
170
|
+
)
|
|
171
|
+
raise typer.Exit(1)
|
|
172
|
+
if not confirm:
|
|
173
|
+
typer.echo(json.dumps(plan, sort_keys=True))
|
|
174
|
+
typer.echo("dry-run: re-run with --confirm to apply", err=True)
|
|
175
|
+
return
|
|
176
|
+
token = store.acquire_lease(stream_resource(spec), f"hasta-reset-{uuid.uuid4()}", 30)
|
|
177
|
+
if token is None:
|
|
178
|
+
typer.echo("error: stream lease was taken while resetting", err=True)
|
|
179
|
+
raise typer.Exit(1)
|
|
180
|
+
try:
|
|
181
|
+
reset = store.reset_checkpoint(spec.name, spec.stream.name, lease=token)
|
|
182
|
+
finally:
|
|
183
|
+
store.release_lease(token)
|
|
184
|
+
finally:
|
|
185
|
+
store.close()
|
|
186
|
+
except ArsenalError as error:
|
|
187
|
+
typer.echo(f"error: {error}", err=True)
|
|
188
|
+
raise typer.Exit(1) from error
|
|
189
|
+
typer.echo(json.dumps({**plan, "after": {**after, "generation": reset.generation}}))
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
if __name__ == "__main__": # pragma: no cover
|
|
193
|
+
app()
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""YAML loader for the dedicated Hasta stream specification."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import re
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import yaml
|
|
10
|
+
from pydantic import ValidationError
|
|
11
|
+
|
|
12
|
+
from arsenal_core.errors import FatalError
|
|
13
|
+
from hasta.spec import StreamSpec
|
|
14
|
+
|
|
15
|
+
_ENV_PATTERN = re.compile(r"\$\{(\w+)\}")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _substitute_env(text: str) -> str:
|
|
19
|
+
def replace(match: re.Match[str]) -> str:
|
|
20
|
+
value = os.environ.get(match.group(1))
|
|
21
|
+
if value is None:
|
|
22
|
+
raise FatalError(f"environment variable not set: {match.group(1)}")
|
|
23
|
+
return value
|
|
24
|
+
|
|
25
|
+
return _ENV_PATTERN.sub(replace, text)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def load_stream(path: Path) -> StreamSpec:
|
|
29
|
+
try:
|
|
30
|
+
raw = yaml.safe_load(_substitute_env(path.read_text(encoding="utf-8")))
|
|
31
|
+
except FileNotFoundError as error:
|
|
32
|
+
raise FatalError(f"stream spec file not found: {path}") from error
|
|
33
|
+
except OSError as error:
|
|
34
|
+
raise FatalError(f"cannot read stream spec {path}: {error}") from error
|
|
35
|
+
try:
|
|
36
|
+
return StreamSpec.model_validate(raw)
|
|
37
|
+
except ValidationError as error:
|
|
38
|
+
lines = [
|
|
39
|
+
f"{'.'.join(str(part) for part in item['loc'])}: {item['msg']}"
|
|
40
|
+
for item in error.errors()
|
|
41
|
+
]
|
|
42
|
+
raise FatalError(f"invalid stream spec {path}:\n " + "\n ".join(lines)) from error
|