Runs API reads Dagster again when its SQLite run storage is locked; still locked is a 503 with Retry-After
The Postgres TEA gate got a 502 polling an ingest: logsForRun failed with 'database is locked' while the run wrote its events.
This commit is contained in:
+34
-10
@@ -1,6 +1,7 @@
|
||||
"""Runs live in Dagster: the API starts them and reads their state, never computes."""
|
||||
|
||||
import os
|
||||
import time
|
||||
from typing import Literal
|
||||
|
||||
import requests
|
||||
@@ -100,19 +101,42 @@ def dagster() -> DagsterGraphQLClient:
|
||||
return DagsterGraphQLClient(host, port_number=port)
|
||||
|
||||
|
||||
LOCKED = "database is locked" # Dagster's SQLite run storage, busy while a run writes its events
|
||||
LOCK_RETRIES = 5
|
||||
|
||||
|
||||
def _locked(payload: dict) -> bool:
|
||||
results = (payload.get("data") or {}).values()
|
||||
return LOCKED in str(payload.get("errors", "")) or any(
|
||||
isinstance(r, dict)
|
||||
and r.get("__typename") == "PythonError"
|
||||
and LOCKED in r.get("message", "")
|
||||
for r in results
|
||||
)
|
||||
|
||||
|
||||
def graphql(query: str, variables: dict) -> dict:
|
||||
"""Dagster's GraphQL answer; a read that found its run storage locked is tried again."""
|
||||
host, port = _dagster_address()
|
||||
try:
|
||||
response = requests.post(
|
||||
f"http://{host}:{port}/graphql",
|
||||
json={"query": query, "variables": variables},
|
||||
timeout=15,
|
||||
for attempt in range(LOCK_RETRIES + 1):
|
||||
try:
|
||||
response = requests.post(
|
||||
f"http://{host}:{port}/graphql",
|
||||
json={"query": query, "variables": variables},
|
||||
timeout=15,
|
||||
)
|
||||
if response.status_code != 200:
|
||||
raise HTTPException(502, detail=response.text)
|
||||
payload = response.json()
|
||||
except requests.RequestException as e:
|
||||
raise HTTPException(502, detail=str(e)) from e
|
||||
if not _locked(payload) or attempt == LOCK_RETRIES:
|
||||
break
|
||||
time.sleep(0.2 * 2**attempt)
|
||||
if _locked(payload):
|
||||
raise HTTPException(
|
||||
503, detail="Dagster's run storage is busy; try again", headers={"Retry-After": "5"}
|
||||
)
|
||||
if response.status_code != 200:
|
||||
raise HTTPException(502, detail=response.text)
|
||||
payload = response.json()
|
||||
except requests.RequestException as e:
|
||||
raise HTTPException(502, detail=str(e)) from e
|
||||
if "errors" in payload:
|
||||
raise HTTPException(502, detail=str(payload["errors"]))
|
||||
return payload["data"]
|
||||
|
||||
Reference in New Issue
Block a user