Skip to content

feat(route): optimistic concurrency control for route updates (#549) - #570

Open
Simultech369 wants to merge 1 commit into
StableRoute-Org:mainfrom
Simultech369:feat/549-occ-route-updates
Open

Simultech369 wants to merge 1 commit into
StableRoute-Org:mainfrom
Simultech369:feat/549-occ-route-updates

Conversation

@Simultech369

Copy link
Copy Markdown

Summary

Closes #549

Implements Optimistic Concurrency Control (OCC) for all route updates across the StableRoute backend to eliminate race conditions and prevent lost updates under concurrency.

Key Changes

  1. Store-Layer Atomic Compare-and-Set:

    • Added monotonic version: number to PairMeta model (defaults to 1).
    • Implemented updatePairMetaCas(key, expectedVersion, updater) in src/stores.ts ensuring atomic verification, mutation, and version increment at the store layer.
    • Added metaCas(key, expectedVersion, update) to the StorageAdapter interface, implemented for both InMemoryAdapter and JsonFileAdapter.
    • Updated persistence migration (migrateV0ToV1) and snapshot hydration to backfill version = 1 for legacy records.
  2. HTTP Route Updates & Reads:

    • Enforced required expected version on all route updates (PATCH /fee_bps, /min, /max, /liquidity, /rate, /enabled, and POST /reset).
    • Supports version ingestion via request body (version, expected_version) and HTTP If-Match headers ("1", W/"1").
    • Rejects missing, blank, null, negative, or non-integer versions with 400 invalid_request.
    • Rejects version mismatches with 409 version_conflict, returning fresh currentVersion and expectedVersion so callers can safely re-fetch and retry.
    • Read routes (GET /api/v1/pairs/:source/:destination/info and GET /api/v1/pairs/:source/:destination) return version and emit matching ETag: "<version>" headers.
  3. Documentation:

    • Updated src/openapi.ts with required version schemas and 409 responses.
    • Added docs/optimistic-concurrency-control.md detailing the OCC model, error taxonomy, and client retry patterns.
    • Updated docs/stores.md with PairMeta.version.
  4. Testing & Edge Cases Covered:

    • Added dedicated test suite src/__tests__/occRouteUpdates.test.ts (28 tests):
      1. Two updates from same base version -> second gets 409 version_conflict with fresh currentVersion.
      2. Sequential updates each bumping version -> all succeed (v1 -> v2 -> v3 -> v4).
      3. Update with missing / blank / null / negative / float / string version -> 400 invalid_request.
      4. Update with future version -> 409 version_conflict with fresh currentVersion.
      5. Read always exposes current version (via GET /info and GET /:source/:destination).
      6. Concurrent races: Promise.all competing updates -> exactly 1 succeeds, others receive 409.
      7. Conditional If-Match header handling.
      8. Store-layer updatePairMetaCas and StorageAdapter.metaCas unit tests.
    • All 36 test suites pass (1,013 tests total, 0 regressions).
    • npm run lint passes with 0 errors.
    • npm run build compiles cleanly with 0 errors.

…bleRoute-Org#549)

- Add monotonic version counter to PairMeta model (defaults to 1)
- Implement atomic store-layer compare-and-set (updatePairMetaCas and StorageAdapter.metaCas)
- Exforce required expected version on all route updates (liquidity, max, min, fee_bps, rate, enabled, reset)
- Support version via body (version, expected_version) and If-Match header
- Expose version in read routes (GET /info and GET /:source/:destination) and emit ETag headers
- Return 409 version_conflict with fresh currentVersion on version mismatch
- Return 400 invalid_request on missing, blank, or invalid version
- Backfill version in persistence schema migrations (v0 -> v1) and store snapshot hydration
- Update OpenAPI schema documentation for all route mutation endpoints
- Add comprehensive test suite covering all 5 edge cases, concurrency races, headers, and store adapters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

optimistic concurrency control for route updates

1 participant