Implement Phase 0b base project against the frozen test contract
All seven services (gateway, ledger, fraud, sanctions, insights, notifier, audit), db/schema.sql plus optional_indexes.sql, scripts/seed.py and smoke.py, the gateway-served frontend, and docker-compose.yml. Plain-text stdlib logging and a weak manual X-Request-Id only -- no observability yet, by design, per test_no_observability.py. Implements all 13 named chaos-flag pathologies from docs/PATHOLOGIES.md behind Redis-backed runtime flags. make smoke and make test-contract pass; make test-baseline and make test-pathology pass except for a handful of load-sensitive tests documented in tests/README.md rather than papered over.
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.responses import JSONResponse
|
||||
from pydantic import BaseModel, field_validator
|
||||
|
||||
from common.logging_config import configure_logging
|
||||
|
||||
from . import chaos
|
||||
from .db import normal_engine, session_factory
|
||||
from . import repository
|
||||
|
||||
SERVICE_NAME = "ledger"
|
||||
logger = configure_logging(SERVICE_NAME)
|
||||
app = FastAPI(title=SERVICE_NAME)
|
||||
|
||||
|
||||
class CreateTransactionRequest(BaseModel):
|
||||
account_id: str
|
||||
kind: str
|
||||
amount_minor: int
|
||||
currency: str
|
||||
merchant_name: str
|
||||
category_id: int | None = None
|
||||
request_id: str = "unknown"
|
||||
|
||||
@field_validator("amount_minor")
|
||||
@classmethod
|
||||
def deposit_amount_must_be_non_negative(cls, v, info):
|
||||
kind = info.data.get("kind")
|
||||
if kind == "deposit" and v < 0:
|
||||
raise ValueError("amount_minor must be non-negative for a deposit")
|
||||
return v
|
||||
|
||||
|
||||
class SettleRequest(BaseModel):
|
||||
risk_score: float
|
||||
request_id: str = "unknown"
|
||||
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def unhandled_exception_handler(request: Request, exc: Exception):
|
||||
logger.exception(f"unhandled exception on {request.url.path}: {exc}")
|
||||
return JSONResponse(status_code=500, content={"error": "internal error"})
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
return {"service": SERVICE_NAME, "version": "0.1.0"}
|
||||
|
||||
|
||||
@app.get("/healthz")
|
||||
async def healthz(request: Request):
|
||||
if request.headers.get("X-Ledgerline-Probe") == "chatty-health":
|
||||
logger.info("received chatty-health healthz probe from gateway")
|
||||
return {"status": "ok", "service": SERVICE_NAME}
|
||||
|
||||
|
||||
@app.get("/readyz")
|
||||
async def readyz():
|
||||
try:
|
||||
async with normal_engine.connect() as conn:
|
||||
await conn.exec_driver_sql("SELECT 1")
|
||||
except Exception as exc:
|
||||
logger.exception(f"readyz check failed: {exc}")
|
||||
return JSONResponse(status_code=503, content={"status": "unavailable"})
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@app.get("/internal/accounts")
|
||||
async def list_accounts():
|
||||
async with session_factory(starved=False) as session:
|
||||
rows = await repository.list_accounts(session)
|
||||
return [dict(row) for row in rows]
|
||||
|
||||
|
||||
@app.post("/internal/transactions")
|
||||
async def create_transaction(body: CreateTransactionRequest):
|
||||
logger.info(f"processing tx for account {body.account_id}, amount {body.amount_minor} request_id={body.request_id}")
|
||||
|
||||
starved = await chaos.pool_starvation_enabled()
|
||||
poison_enabled = await chaos.poison_data_enabled()
|
||||
poison_tx_id = await chaos.find_poison_transaction_id(body.account_id)
|
||||
|
||||
async with session_factory(starved=starved) as session:
|
||||
account = await repository.get_account(session, body.account_id)
|
||||
if account is None:
|
||||
return JSONResponse(status_code=404, content={"error": "account not found"})
|
||||
|
||||
if poison_tx_id is not None and poison_enabled:
|
||||
poison_row = await repository.get_transaction_currency(session, poison_tx_id)
|
||||
# poison_data: skip input validation on the seeded malformed rows
|
||||
# (§8) -- this naive access raises on the NULL currency on purpose.
|
||||
_ = poison_row.currency.upper()
|
||||
|
||||
tx_id = await repository.create_transaction(
|
||||
session,
|
||||
account_id=body.account_id,
|
||||
kind=body.kind,
|
||||
amount_minor=body.amount_minor,
|
||||
currency=body.currency,
|
||||
merchant_name=body.merchant_name,
|
||||
category_id=body.category_id,
|
||||
)
|
||||
|
||||
return JSONResponse(
|
||||
status_code=201,
|
||||
content={
|
||||
"transaction_id": tx_id,
|
||||
"account_id": body.account_id,
|
||||
"holder_name": account.holder_name,
|
||||
"kind": body.kind,
|
||||
"amount_minor": body.amount_minor,
|
||||
"category_id": body.category_id,
|
||||
"status": "pending",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@app.post("/internal/transactions/{tx_id}/settle")
|
||||
async def settle(tx_id: str, body: SettleRequest):
|
||||
starved = await chaos.pool_starvation_enabled()
|
||||
row_lock = await chaos.row_lock_contention_enabled()
|
||||
|
||||
async with session_factory(starved=starved) as session:
|
||||
tx = await repository.get_transaction(session, tx_id)
|
||||
if tx is None:
|
||||
return JSONResponse(status_code=404, content={"error": "transaction not found"})
|
||||
account = await repository.get_account(session, tx.account_id)
|
||||
use_lock = row_lock and bool(account.is_hot)
|
||||
balance = await repository.settle_transaction(session, tx=tx, risk_score=body.risk_score, use_row_lock=use_lock)
|
||||
|
||||
logger.info(f"settled transaction {tx_id} request_id={body.request_id}")
|
||||
return {"status": "settled", "balance_minor": balance}
|
||||
|
||||
|
||||
@app.post("/internal/transactions/{tx_id}/reject")
|
||||
async def reject(tx_id: str, body: SettleRequest):
|
||||
starved = await chaos.pool_starvation_enabled()
|
||||
|
||||
async with session_factory(starved=starved) as session:
|
||||
tx = await repository.get_transaction(session, tx_id)
|
||||
if tx is None:
|
||||
return JSONResponse(status_code=404, content={"error": "transaction not found"})
|
||||
balance = await repository.reject_transaction(session, tx=tx, risk_score=body.risk_score)
|
||||
|
||||
logger.info(f"rejected transaction {tx_id} request_id={body.request_id}")
|
||||
return {"status": "rejected", "balance_minor": balance}
|
||||
@@ -0,0 +1,28 @@
|
||||
"""pool_starvation, row_lock_contention, poison_data -- phase-0-base-project-spec.md
|
||||
§7. All three are owned by `ledger`.
|
||||
"""
|
||||
from common.chaos_flags import is_enabled
|
||||
from common.redis_client import redis_client
|
||||
|
||||
# Populated once by scripts/seed.py: account_id -> id of that account's one
|
||||
# deliberately NULL-currency transaction (§8). A Redis hash lookup, not a
|
||||
# `transactions` scan by account_id -- that column has no supporting index
|
||||
# on purpose (§5), and the poison_data pathology shouldn't have to pay for a
|
||||
# full-table seq scan on every single request just to find its own trigger.
|
||||
POISON_TX_KEY = "poison:tx_by_account"
|
||||
|
||||
|
||||
async def pool_starvation_enabled() -> bool:
|
||||
return await is_enabled("pool_starvation")
|
||||
|
||||
|
||||
async def row_lock_contention_enabled() -> bool:
|
||||
return await is_enabled("row_lock_contention")
|
||||
|
||||
|
||||
async def poison_data_enabled() -> bool:
|
||||
return await is_enabled("poison_data")
|
||||
|
||||
|
||||
async def find_poison_transaction_id(account_id: str) -> str | None:
|
||||
return await redis_client.hget(POISON_TX_KEY, account_id)
|
||||
@@ -0,0 +1,17 @@
|
||||
"""Two pre-built engines so pool_starvation (phase-0-base-project-spec.md §7)
|
||||
can be toggled live, with no restart: the flag picks which already-initialised
|
||||
pool a request borrows a connection from, rather than resizing a pool in
|
||||
place (SQLAlchemy pools aren't resizable live)."""
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker
|
||||
|
||||
from common.db import make_engine
|
||||
|
||||
normal_engine = make_engine(pool_size=20, max_overflow=10)
|
||||
starved_engine = make_engine(pool_size=2, max_overflow=0, pool_timeout=10)
|
||||
|
||||
NormalSession = async_sessionmaker(normal_engine, expire_on_commit=False)
|
||||
StarvedSession = async_sessionmaker(starved_engine, expire_on_commit=False)
|
||||
|
||||
|
||||
def session_factory(starved: bool):
|
||||
return StarvedSession() if starved else NormalSession()
|
||||
@@ -0,0 +1,122 @@
|
||||
import asyncio
|
||||
import uuid
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
KIND_SIGN = {"deposit": 1, "withdrawal": -1, "purchase": -1}
|
||||
|
||||
ROW_LOCK_HOLD_SECONDS = 0.15
|
||||
|
||||
|
||||
async def get_account(session, account_id: str):
|
||||
result = await session.execute(
|
||||
sa.text("SELECT id, holder_name, currency, balance_minor, is_hot FROM accounts WHERE id = :id"),
|
||||
{"id": account_id},
|
||||
)
|
||||
return result.first()
|
||||
|
||||
|
||||
async def list_accounts(session, limit: int = 1000):
|
||||
result = await session.execute(
|
||||
sa.text(
|
||||
"SELECT id, holder_name, currency, balance_minor FROM accounts ORDER BY created_at LIMIT :limit"
|
||||
),
|
||||
{"limit": limit},
|
||||
)
|
||||
return result.mappings().all()
|
||||
|
||||
|
||||
async def get_transaction_currency(session, tx_id: str):
|
||||
"""Backs the poison_data pathology: fetches one known-malformed transaction
|
||||
(NULL currency, §8) by primary key -- a cheap, always-fast lookup regardless
|
||||
of the deliberately-missing account_id index, since which transaction to
|
||||
fetch is looked up by account_id in Redis first (chaos.py's
|
||||
find_poison_transaction_id), not by scanning `transactions` here."""
|
||||
result = await session.execute(
|
||||
sa.text("SELECT currency FROM transactions WHERE id = :id"),
|
||||
{"id": tx_id},
|
||||
)
|
||||
return result.first()
|
||||
|
||||
|
||||
async def create_transaction(
|
||||
session, *, account_id, kind, amount_minor, currency, merchant_name, category_id
|
||||
):
|
||||
tx_id = str(uuid.uuid4())
|
||||
await session.execute(
|
||||
sa.text(
|
||||
"INSERT INTO transactions "
|
||||
"(id, account_id, kind, amount_minor, currency, merchant_name, category_id, status) "
|
||||
"VALUES (:id, :account_id, :kind, :amount_minor, :currency, :merchant_name, :category_id, 'pending')"
|
||||
),
|
||||
{
|
||||
"id": tx_id,
|
||||
"account_id": account_id,
|
||||
"kind": kind,
|
||||
"amount_minor": amount_minor,
|
||||
"currency": currency,
|
||||
"merchant_name": merchant_name,
|
||||
"category_id": category_id,
|
||||
},
|
||||
)
|
||||
await session.commit()
|
||||
return tx_id
|
||||
|
||||
|
||||
async def get_transaction(session, tx_id: str):
|
||||
result = await session.execute(
|
||||
sa.text(
|
||||
"SELECT id, account_id, kind, amount_minor, currency, merchant_name, "
|
||||
"category_id, status FROM transactions WHERE id = :id"
|
||||
),
|
||||
{"id": tx_id},
|
||||
)
|
||||
return result.first()
|
||||
|
||||
|
||||
async def settle_transaction(session, *, tx, risk_score, use_row_lock: bool):
|
||||
"""Applies the balance delta and marks the transaction settled. When
|
||||
use_row_lock is true (row_lock_contention on a hot account), takes
|
||||
SELECT ... FOR UPDATE and holds the transaction open for an extra 150ms
|
||||
before committing -- phase-0-base-project-spec.md §7."""
|
||||
delta = KIND_SIGN[tx.kind] * tx.amount_minor
|
||||
|
||||
if use_row_lock:
|
||||
result = await session.execute(
|
||||
sa.text("SELECT balance_minor FROM accounts WHERE id = :id FOR UPDATE"),
|
||||
{"id": tx.account_id},
|
||||
)
|
||||
result.first()
|
||||
await session.execute(
|
||||
sa.text("UPDATE accounts SET balance_minor = balance_minor + :delta WHERE id = :id"),
|
||||
{"delta": delta, "id": tx.account_id},
|
||||
)
|
||||
await asyncio.sleep(ROW_LOCK_HOLD_SECONDS)
|
||||
else:
|
||||
await session.execute(
|
||||
sa.text("UPDATE accounts SET balance_minor = balance_minor + :delta WHERE id = :id"),
|
||||
{"delta": delta, "id": tx.account_id},
|
||||
)
|
||||
|
||||
await session.execute(
|
||||
sa.text("UPDATE transactions SET status = 'settled', risk_score = :risk_score WHERE id = :id"),
|
||||
{"risk_score": risk_score, "id": tx.id},
|
||||
)
|
||||
await session.commit()
|
||||
|
||||
result = await session.execute(
|
||||
sa.text("SELECT balance_minor FROM accounts WHERE id = :id"), {"id": tx.account_id}
|
||||
)
|
||||
return result.scalar_one()
|
||||
|
||||
|
||||
async def reject_transaction(session, *, tx, risk_score):
|
||||
await session.execute(
|
||||
sa.text("UPDATE transactions SET status = 'rejected', risk_score = :risk_score WHERE id = :id"),
|
||||
{"risk_score": risk_score, "id": tx.id},
|
||||
)
|
||||
await session.commit()
|
||||
result = await session.execute(
|
||||
sa.text("SELECT balance_minor FROM accounts WHERE id = :id"), {"id": tx.account_id}
|
||||
)
|
||||
return result.scalar_one()
|
||||
Reference in New Issue
Block a user