text stringlengths 0 1.47k |
|---|
try: |
return await _retryable(lambda: cont.read_item(f"idem::{org}::{sha}", partition_key=_pk(org))) |
except cex.CosmosResourceNotFoundError: |
return None |
async def idem_release(org: str, sha: str, resp_id: str) -> None: |
"""Best-effort release of a reservation whose request failed BEFORE it |
persisted any response record — otherwise a retry would replay a resp_id that |
never produced a result (a 'poisoned' key). Only deletes the item if it still |
points at OUR resp_id (never clobber a reservation that a retry already reused).""" |
cex = _exceptions() |
try: |
cont = await _container() |
cur = await cont.read_item(f"idem::{org}::{sha}", partition_key=_pk(org)) |
if str(cur.get("resp_id") or "") == resp_id: |
await cont.delete_item(f"idem::{org}::{sha}", partition_key=_pk(org)) |
except cex.CosmosResourceNotFoundError: |
return |
except Exception: # noqa: BLE001 |
return |
# ── per-session execution lease (fencing) ────────────────────────────────────── |
async def lease_acquire(org: str, sid: str, owner: str, ttl_s: int) -> int: |
"""Bump and take the session lease; returns THIS turn's fence token (monotonic |
int). A newer turn will get a higher fence, so a stale turn detects it lost the |
workspace at checkpoint time. Best-effort: returns 0 on contention exhaustion OR |
any store error (the caller then behaves as today — no fencing, but the turn |
still runs). NOTE: fencing is not yet wired into the turn path (deferred).""" |
cex = _exceptions() |
try: |
cont = await _container() |
doc_id = f"lease::{org}::{sid}" |
for _ in range(5): # bounded CAS retry |
try: |
cur = await cont.read_item(doc_id, partition_key=_pk(org)) |
fence = int(cur.get("fence", 0)) + 1 |
cur.update(fence=fence, owner=owner, expires=time.time() + ttl_s, |
ts=time.time(), ttl=max(int(ttl_s) * 4, 3600)) |
await cont.replace_item(doc_id, cur, |
etag=cur["_etag"], match_condition=_IF_MATCH) |
return fence |
except cex.CosmosResourceNotFoundError: |
try: |
await cont.create_item({"id": doc_id, "pk": _pk(org), "kind": "lease", |
"fence": 1, "owner": owner, |
"expires": time.time() + ttl_s, |
"ts": time.time(), "ttl": max(int(ttl_s) * 4, 3600)}) |
return 1 |
except cex.CosmosResourceExistsError: |
continue # someone created it first — retry the read/bump |
except cex.CosmosAccessConditionFailedError: |
continue # ETag moved under us — retry |
return 0 # gave up (contention) → unfenced fallback |
except Exception: # noqa: BLE001 — store down: fail open per docstring |
return 0 |
async def lease_admit(org: str, sid: str, owner: str, ttl_s: int, *, |
reject_on_conflict: bool = False, fresh_s: float = 75.0) -> dict: |
"""Admission gate at turn start. Reads the session lease and classifies the incumbent: |
a DIFFERENT owner counts as an ACTIVE conflict only if its lease is BOTH live (expires in |
the future) AND fresh (heartbeated within `fresh_s`) — so a crashed/completed-but-unreleased |
lease (which stops renewing) does NOT falsely reject a legitimate follow-up. |
Returns {fence, conflict, rejected, holder}. |
- reject_on_conflict=False (observe): ALWAYS take ownership (bump fence) and just report the |
conflict — the turn proceeds; fence identifies it for checkpoint/heartbeat. |
- reject_on_conflict=True (enforce): on an ACTIVE conflict, DO NOT touch the lease (never steal |
the running turn's fence — that would make ITS checkpoint stale) and return rejected=True so |
the caller refuses the duplicate. Otherwise take ownership. |
Best-effort: on any store error returns {fence:0, conflict:False, rejected:False} → fail OPEN.""" |
cex = _exceptions() |
doc_id = f"lease::{org}::{sid}" |
now = time.time() |
try: |
cont = await _container() |
for _ in range(5): |
try: |
cur = await cont.read_item(doc_id, partition_key=_pk(org)) |
held_by = str(cur.get("owner") or "") |
live = float(cur.get("expires") or 0) > now |
fresh = (now - float(cur.get("ts") or 0)) < fresh_s |
other = bool(held_by and held_by != owner) |
conflict = bool(live and other) |
active_conflict = bool(conflict and fresh) |
if reject_on_conflict and active_conflict: |
# Refuse WITHOUT modifying the lease — the incumbent keeps its fence. |
return {"fence": 0, "conflict": True, "rejected": True, "holder": held_by} |
fence = int(cur.get("fence", 0)) + 1 |
cur.update(fence=fence, owner=owner, expires=now + ttl_s, ts=now, |
ttl=max(int(ttl_s) * 4, 3600)) |
try: |
await cont.replace_item(doc_id, cur, etag=cur["_etag"], match_condition=_IF_MATCH) |
return {"fence": fence, "conflict": conflict, "rejected": False, "holder": held_by} |
except cex.CosmosAccessConditionFailedError: |
continue # another writer moved it — re-read |
except cex.CosmosResourceNotFoundError: |
try: |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.