netherframe.backends.postgresql 0.2.1__py3-none-any.whl
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.
- netherframe/backends/postgresql/__init__.py +59 -0
- netherframe/backends/postgresql/connection_settings.py +40 -0
- netherframe/backends/postgresql/graph.py +2511 -0
- netherframe/backends/postgresql/graph_authorization.py +102 -0
- netherframe/backends/postgresql/graph_checkpoints.py +239 -0
- netherframe/backends/postgresql/graph_completion.py +66 -0
- netherframe/backends/postgresql/graph_convergence.py +103 -0
- netherframe/backends/postgresql/graph_invalidation.py +115 -0
- netherframe/backends/postgresql/graph_lease_queue.py +236 -0
- netherframe/backends/postgresql/graph_planning_reads.py +97 -0
- netherframe/backends/postgresql/graph_publication.py +373 -0
- netherframe/backends/postgresql/graph_retention.py +141 -0
- netherframe/backends/postgresql/graph_reverse_edges.py +143 -0
- netherframe/backends/postgresql/graph_schema.py +2038 -0
- netherframe/backends/postgresql/graph_transfers.py +128 -0
- netherframe/backends/postgresql/postgresql.py +1207 -0
- netherframe/backends/postgresql/postgresql_backend_bootstrap.py +139 -0
- netherframe/backends/postgresql/py.typed +0 -0
- netherframe_backends_postgresql-0.2.1.dist-info/METADATA +138 -0
- netherframe_backends_postgresql-0.2.1.dist-info/RECORD +40 -0
- netherframe_backends_postgresql-0.2.1.dist-info/WHEEL +4 -0
- netherframe_backends_postgresql-0.2.1.dist-info/licenses/LICENSE +21 -0
- nf/backends/postgresql/__init__.py +4 -0
- nf/backends/postgresql/connection_settings.py +4 -0
- nf/backends/postgresql/graph.py +4 -0
- nf/backends/postgresql/graph_authorization.py +4 -0
- nf/backends/postgresql/graph_checkpoints.py +4 -0
- nf/backends/postgresql/graph_completion.py +4 -0
- nf/backends/postgresql/graph_convergence.py +4 -0
- nf/backends/postgresql/graph_invalidation.py +4 -0
- nf/backends/postgresql/graph_lease_queue.py +4 -0
- nf/backends/postgresql/graph_planning_reads.py +4 -0
- nf/backends/postgresql/graph_publication.py +4 -0
- nf/backends/postgresql/graph_retention.py +4 -0
- nf/backends/postgresql/graph_reverse_edges.py +4 -0
- nf/backends/postgresql/graph_schema.py +4 -0
- nf/backends/postgresql/graph_transfers.py +4 -0
- nf/backends/postgresql/postgresql.py +4 -0
- nf/backends/postgresql/postgresql_backend_bootstrap.py +4 -0
- nf/backends/postgresql/py.typed +0 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"""Atomic migration staging, source fencing and destination publication."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING, Any
|
|
6
|
+
from dataclasses import replace
|
|
7
|
+
|
|
8
|
+
from netherframe.core.backends.graph_migration import StateTransfer, TransferRecord, storage_identity, transfer_writes
|
|
9
|
+
from netherframe.core.backends.state_models import State, StateVersion
|
|
10
|
+
from netherframe.core.backends.graph_state import Ownership, PreCallValidationError
|
|
11
|
+
from netherframe.core.exceptions import StateError
|
|
12
|
+
from netherframe.core.serialization import orjson_compat as json
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
import asyncpg
|
|
16
|
+
from .graph import PostgreSQLGraphBackend, _OwnedTransaction
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _records(raw: Any) -> dict[str, Any]:
|
|
20
|
+
return raw if isinstance(raw, dict) else json.loads(raw or "{}")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class PostgreSQLStateTransfers:
|
|
24
|
+
def __init__(self, backend: PostgreSQLGraphBackend) -> None:
|
|
25
|
+
self.backend = backend
|
|
26
|
+
|
|
27
|
+
async def record(self, transfer_id: str) -> TransferRecord | None:
|
|
28
|
+
backend = self.backend
|
|
29
|
+
async with backend._tx(readonly=True) as conn:
|
|
30
|
+
raw = await conn.fetchval(f"SELECT transfers -> $2 FROM {backend._t('apps')} WHERE state_id = $1", backend.state_id, transfer_id)
|
|
31
|
+
return TransferRecord.from_dict(_records(raw)) if raw is not None else None
|
|
32
|
+
|
|
33
|
+
async def _app(self, transaction: _OwnedTransaction, transfer: StateTransfer, side: str) -> Any:
|
|
34
|
+
backend = self.backend
|
|
35
|
+
conn, run = transaction.conn, transaction.run
|
|
36
|
+
if not run["holds"]:
|
|
37
|
+
raise PreCallValidationError("Migration requires the whole-app X lease")
|
|
38
|
+
if storage_identity(backend) != (transfer.source if side == "source" else transfer.target):
|
|
39
|
+
raise StateError("Migration receipt belongs to another graph state")
|
|
40
|
+
return await conn.fetchrow(f"SELECT migrated_to, transfer_id, transfers FROM {backend._t('apps')} WHERE state_id = $1 FOR UPDATE", backend.state_id)
|
|
41
|
+
|
|
42
|
+
async def _store(self, conn: asyncpg.Connection, ownership: Ownership, record: TransferRecord, *, migrated_to: str | None, transfer_id: str | None, aborted: TransferRecord | None = None) -> None:
|
|
43
|
+
backend = self.backend
|
|
44
|
+
previous = "jsonb_set(transfers, ARRAY[$6::text], $7::jsonb)" if aborted is not None else "transfers"
|
|
45
|
+
await conn.execute(
|
|
46
|
+
f"UPDATE {backend._t('apps')} SET transfers = jsonb_set({previous}, ARRAY[$2::text], $3::jsonb), migrated_to = $4, transfer_id = $5 WHERE state_id = $1",
|
|
47
|
+
backend.state_id,
|
|
48
|
+
record.transfer.transfer_id,
|
|
49
|
+
json.dumps(record.to_dict()),
|
|
50
|
+
migrated_to,
|
|
51
|
+
transfer_id,
|
|
52
|
+
*([aborted.transfer.transfer_id, json.dumps(aborted.to_dict())] if aborted is not None else []),
|
|
53
|
+
)
|
|
54
|
+
await conn.execute(
|
|
55
|
+
f"INSERT INTO {backend._t('run_mutations')} (run_id, state_id, started_at, cause) VALUES ($1, $2, clock_timestamp(), $3) ON CONFLICT (run_id) DO NOTHING",
|
|
56
|
+
ownership.run_id,
|
|
57
|
+
backend.state_id,
|
|
58
|
+
f"migration_{record.phase}",
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
async def stage(self, ownership: Ownership, transfer: StateTransfer, replace_staged: bool) -> None:
|
|
62
|
+
backend = self.backend
|
|
63
|
+
async with backend._owned_tx(ownership, "migration", migrating=True) as transaction:
|
|
64
|
+
conn = transaction.conn
|
|
65
|
+
app = await self._app(transaction, transfer, "target")
|
|
66
|
+
record = TransferRecord(transfer, "target", "staged")
|
|
67
|
+
if raw := _records(app["transfers"]).get(transfer.transfer_id):
|
|
68
|
+
if TransferRecord.from_dict(raw) not in (record, record.advance()):
|
|
69
|
+
raise StateError("Migration receipt changed")
|
|
70
|
+
return
|
|
71
|
+
snapshot = await backend._snapshot_on(conn)
|
|
72
|
+
old = _records(app["transfers"]).get(app["transfer_id"]) if app["transfer_id"] is not None else None
|
|
73
|
+
previous = TransferRecord.from_dict(old) if old is not None else None
|
|
74
|
+
replacing = (
|
|
75
|
+
replace_staged
|
|
76
|
+
and previous is not None
|
|
77
|
+
and previous.side == "target"
|
|
78
|
+
and previous.phase == "staged"
|
|
79
|
+
and (previous.transfer.source, previous.transfer.target, previous.transfer.direction) == (transfer.source, transfer.target, transfer.direction)
|
|
80
|
+
)
|
|
81
|
+
if (app["migrated_to"] is not None and not replacing) or snapshot.state.resources or snapshot.state.outputs or snapshot.unresolved or StateVersion.of(snapshot.state) != transfer.target_version:
|
|
82
|
+
raise StateError("The graph destination already holds state or changed before staging")
|
|
83
|
+
transfer.check_target(snapshot.state)
|
|
84
|
+
aborted = replace(previous, phase="aborted") if replacing and previous is not None else None
|
|
85
|
+
await self._store(conn, ownership, record, migrated_to="pending migration", transfer_id=transfer.transfer_id, aborted=aborted)
|
|
86
|
+
|
|
87
|
+
async def fence(self, ownership: Ownership, transfer: StateTransfer) -> None:
|
|
88
|
+
backend = self.backend
|
|
89
|
+
async with backend._owned_tx(ownership, "migration", migrating=True) as transaction:
|
|
90
|
+
conn = transaction.conn
|
|
91
|
+
app = await self._app(transaction, transfer, "source")
|
|
92
|
+
record = TransferRecord(transfer, "source", "fenced")
|
|
93
|
+
if raw := _records(app["transfers"]).get(transfer.transfer_id):
|
|
94
|
+
if TransferRecord.from_dict(raw) not in (record, record.advance()) or app["transfer_id"] != transfer.transfer_id:
|
|
95
|
+
raise StateError("Migration receipt changed")
|
|
96
|
+
return
|
|
97
|
+
snapshot = await backend._snapshot_on(conn)
|
|
98
|
+
if app["migrated_to"] is not None or snapshot.unresolved or StateVersion.of(snapshot.state) != transfer.source_version:
|
|
99
|
+
raise StateError("The graph source changed before migration fencing")
|
|
100
|
+
transfer.check_source(snapshot.state)
|
|
101
|
+
await self._store(conn, ownership, record, migrated_to="document", transfer_id=transfer.transfer_id)
|
|
102
|
+
|
|
103
|
+
async def publish(self, ownership: Ownership, transfer: StateTransfer, state: State) -> None:
|
|
104
|
+
backend = self.backend
|
|
105
|
+
async with backend._owned_tx(ownership, "migration", migrating=True) as transaction:
|
|
106
|
+
conn = transaction.conn
|
|
107
|
+
app = await self._app(transaction, transfer, "target")
|
|
108
|
+
record = TransferRecord(transfer, "target", "staged")
|
|
109
|
+
raw = _records(app["transfers"]).get(transfer.transfer_id)
|
|
110
|
+
previous = TransferRecord.from_dict(raw) if raw is not None else None
|
|
111
|
+
if previous == record.advance():
|
|
112
|
+
return
|
|
113
|
+
if previous != record or app["transfer_id"] != transfer.transfer_id:
|
|
114
|
+
raise StateError("The migration destination is not staged for this transfer")
|
|
115
|
+
writes = transfer_writes(await backend._snapshot_on(conn), transfer, state)
|
|
116
|
+
await backend._commit_on(transaction, writes, {}, None)
|
|
117
|
+
await self._store(conn, ownership, record.advance(), migrated_to=None, transfer_id=None)
|
|
118
|
+
|
|
119
|
+
async def finish(self, ownership: Ownership, transfer: StateTransfer) -> None:
|
|
120
|
+
backend = self.backend
|
|
121
|
+
async with backend._owned_tx(ownership, "migration", migrating=True) as transaction:
|
|
122
|
+
conn = transaction.conn
|
|
123
|
+
app = await self._app(transaction, transfer, "source")
|
|
124
|
+
record = TransferRecord(transfer, "source", "fenced")
|
|
125
|
+
raw = _records(app["transfers"]).get(transfer.transfer_id)
|
|
126
|
+
if raw is None or TransferRecord.from_dict(raw) not in (record, record.advance()) or app["transfer_id"] != transfer.transfer_id:
|
|
127
|
+
raise StateError("The migration source is not fenced for this transfer")
|
|
128
|
+
await self._store(conn, ownership, record.advance(), migrated_to="document", transfer_id=transfer.transfer_id)
|