feat(route): optimistic concurrency control for route updates (#549) - #570
Open
Simultech369 wants to merge 1 commit into
Open
Simultech369 wants to merge 1 commit into
Simultech369 wants to merge 1 commit into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Store-Layer Atomic Compare-and-Set:
version: numbertoPairMetamodel (defaults to 1).updatePairMetaCas(key, expectedVersion, updater)insrc/stores.tsensuring atomic verification, mutation, and version increment at the store layer.metaCas(key, expectedVersion, update)to theStorageAdapterinterface, implemented for bothInMemoryAdapterandJsonFileAdapter.migrateV0ToV1) and snapshot hydration to backfillversion = 1for legacy records.HTTP Route Updates & Reads:
PATCH /fee_bps,/min,/max,/liquidity,/rate,/enabled, andPOST /reset).version,expected_version) and HTTPIf-Matchheaders ("1",W/"1").400 invalid_request.409 version_conflict, returning freshcurrentVersionandexpectedVersionso callers can safely re-fetch and retry.GET /api/v1/pairs/:source/:destination/infoandGET /api/v1/pairs/:source/:destination) returnversionand emit matchingETag: "<version>"headers.Documentation:
src/openapi.tswith requiredversionschemas and409responses.docs/optimistic-concurrency-control.mddetailing the OCC model, error taxonomy, and client retry patterns.docs/stores.mdwithPairMeta.version.Testing & Edge Cases Covered:
src/__tests__/occRouteUpdates.test.ts(28 tests):version_conflictwith freshcurrentVersion.invalid_request.version_conflictwith freshcurrentVersion.GET /infoandGET /:source/:destination).Promise.allcompeting updates -> exactly 1 succeeds, others receive 409.If-Matchheader handling.updatePairMetaCasandStorageAdapter.metaCasunit tests.npm run lintpasses with 0 errors.npm run buildcompiles cleanly with 0 errors.