-
Notifications
You must be signed in to change notification settings - Fork 12
CS-11125: per-realm advisory lock on data-plane write paths #4840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lukemelia
wants to merge
3
commits into
withwritelock-refactor
Choose a base branch
from
cs-11125-per-realm-advisory-lock-for-data-plane-write-paths
base: withwritelock-refactor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+632
−354
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
214 changes: 214 additions & 0 deletions
214
packages/realm-server/tests/data-plane-write-lock-test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| import { module, test } from 'qunit'; | ||
| import type { Test, SuperTest } from 'supertest'; | ||
| import { basename } from 'path'; | ||
| import type { Server } from 'http'; | ||
| import type { DirResult } from 'tmp'; | ||
| import type { Realm } from '@cardstack/runtime-common'; | ||
| import { | ||
| SupportedMimeType, | ||
| type LooseSingleCardDocument, | ||
| } from '@cardstack/runtime-common'; | ||
| import { | ||
| setupPermissionedRealmCached, | ||
| createJWT, | ||
| type RealmRequest, | ||
| withRealmPath, | ||
| } from './helpers'; | ||
| import '@cardstack/runtime-common/helpers/code-equality-assertion'; | ||
|
|
||
| // CS-11125 regression coverage: per-realm advisory lock on the data-plane | ||
| // write paths. These tests reach the realm-server HTTP API the same way a | ||
| // real client would, and rely on Node's event-loop interleaving + the | ||
| // Postgres advisory lock to serialize critical sections that pre-CS-11125 | ||
| // raced. Without the lock applied to PATCH and /_atomic, the assertions | ||
| // below detect the lost update / TOCTOU directly. | ||
| module(basename(__filename), function () { | ||
| module('CS-11125: data-plane write serialization', function () { | ||
| let realmURL = new URL('http://127.0.0.1:4444/test/'); | ||
| let testRealm: Realm; | ||
| let request: RealmRequest; | ||
|
|
||
| function onRealmSetup(args: { | ||
| testRealm: Realm; | ||
| testRealmHttpServer: Server; | ||
| request: SuperTest<Test>; | ||
| dir: DirResult; | ||
| }) { | ||
| testRealm = args.testRealm; | ||
| request = withRealmPath(args.request, realmURL); | ||
| } | ||
|
|
||
| module('concurrent PATCH on same card', function (hooks) { | ||
| setupPermissionedRealmCached(hooks, { | ||
| fixture: 'simple', | ||
| realmURL, | ||
| permissions: { | ||
| '*': ['read', 'write'], | ||
| '@node-test_realm:localhost': ['read', 'realm-owner'], | ||
| }, | ||
| onRealmSetup, | ||
| }); | ||
|
|
||
| test('two concurrent PATCHes preserve both non-overlapping field changes', async function (assert) { | ||
| // Replica A patches `firstName`; Replica B patches a different | ||
| // attribute path (`cardInfo.summary`). Pre-CS-11125, both | ||
| // handlers would read `indexEntry.instance` from the same | ||
| // pre-state, compute independent merges, and the second writer | ||
| // would clobber the first's field — last-writer-wins on disk | ||
| // with the loser's change silently dropped. With the per-realm | ||
| // advisory lock around the indexEntry read + writeMany, the | ||
| // second PATCH waits until the first commits, then sees the | ||
| // updated `original` and merges on top of it. | ||
| let auth = `Bearer ${createJWT(testRealm, 'user', ['read', 'write'])}`; | ||
| let firstNamePatch = request | ||
| .patch('/person-1') | ||
| .set('Accept', SupportedMimeType.CardJson) | ||
| .set('Authorization', auth) | ||
| .send({ | ||
| data: { | ||
| type: 'card', | ||
| attributes: { | ||
| firstName: 'ConcurrentA', | ||
| }, | ||
| meta: { | ||
| adoptsFrom: { module: './person.gts', name: 'Person' }, | ||
| }, | ||
| }, | ||
| }); | ||
| let summaryPatch = request | ||
| .patch('/person-1') | ||
| .set('Accept', SupportedMimeType.CardJson) | ||
| .set('Authorization', auth) | ||
| .send({ | ||
| data: { | ||
| type: 'card', | ||
| attributes: { | ||
| cardInfo: { | ||
| summary: 'ConcurrentB summary', | ||
| }, | ||
| }, | ||
| meta: { | ||
| adoptsFrom: { module: './person.gts', name: 'Person' }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| let [firstNameResponse, summaryResponse] = await Promise.all([ | ||
| firstNamePatch, | ||
| summaryPatch, | ||
| ]); | ||
|
|
||
| assert.strictEqual( | ||
| firstNameResponse.status, | ||
| 200, | ||
| 'firstName PATCH succeeds', | ||
| ); | ||
| assert.strictEqual( | ||
| summaryResponse.status, | ||
| 200, | ||
| 'summary PATCH succeeds', | ||
| ); | ||
|
|
||
| // Final state on disk must reflect BOTH patches. Without the | ||
| // lock the second writer would have written a merge based on | ||
| // the pre-first-writer state and dropped the other field. | ||
| let finalResponse = await request | ||
| .get('/person-1') | ||
| .set('Accept', SupportedMimeType.CardJson) | ||
| .set('Authorization', auth); | ||
| assert.strictEqual(finalResponse.status, 200, 'final GET succeeds'); | ||
| let finalDoc = finalResponse.body as LooseSingleCardDocument; | ||
| assert.strictEqual( | ||
| finalDoc.data.attributes?.firstName, | ||
| 'ConcurrentA', | ||
| 'firstName from PATCH A persisted', | ||
| ); | ||
| let cardInfo = finalDoc.data.attributes?.cardInfo as | ||
| | { summary?: string } | ||
| | undefined; | ||
| assert.strictEqual( | ||
| cardInfo?.summary, | ||
| 'ConcurrentB summary', | ||
| 'cardInfo.summary from PATCH B persisted', | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| module('concurrent /_atomic add on same href', function (hooks) { | ||
| setupPermissionedRealmCached(hooks, { | ||
| fixture: 'simple', | ||
| realmURL, | ||
| permissions: { | ||
| '*': ['read', 'write'], | ||
| '@node-test_realm:localhost': ['read', 'realm-owner'], | ||
| }, | ||
| onRealmSetup, | ||
| }); | ||
|
|
||
| test('exactly one of two concurrent `add` ops succeeds; the other gets 409', async function (assert) { | ||
| // Pre-CS-11125, two replicas calling POST /_atomic with op=add | ||
| // for the same href could both pass `checkBeforeAtomicWrite` | ||
| // (file doesn't exist yet on either) and proceed to write — | ||
| // last writer silently wins on disk and `boxel_index` ends up | ||
| // interleaved. With the per-realm advisory lock taken at the | ||
| // handler entry, the second request blocks until the first | ||
| // commits, then re-runs the exists check inside the lock and | ||
| // correctly returns 409. | ||
| let auth = `Bearer ${createJWT(testRealm, 'user', ['read', 'write'])}`; | ||
| let makeAddBody = (firstName: string) => ({ | ||
| 'atomic:operations': [ | ||
| { | ||
| op: 'add', | ||
| href: 'concurrent-add.json', | ||
| data: { | ||
| type: 'card', | ||
| attributes: { firstName }, | ||
| meta: { | ||
| adoptsFrom: { module: './person.gts', name: 'Person' }, | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }); | ||
| // `?waitForIndex=true` makes the POST return only once indexing | ||
| // has caught up to the write. Without it, `/_atomic` resolves on | ||
| // durability and the GET below races the indexer (404 or stale). | ||
| let postA = request | ||
| .post('/_atomic?waitForIndex=true') | ||
| .set('Accept', SupportedMimeType.JSONAPI) | ||
| .set('Authorization', auth) | ||
| .send(JSON.stringify(makeAddBody('AddA'))); | ||
| let postB = request | ||
| .post('/_atomic?waitForIndex=true') | ||
| .set('Accept', SupportedMimeType.JSONAPI) | ||
| .set('Authorization', auth) | ||
| .send(JSON.stringify(makeAddBody('AddB'))); | ||
|
|
||
| let [respA, respB] = await Promise.all([postA, postB]); | ||
|
|
||
| let statuses = [respA.status, respB.status].sort(); | ||
| assert.deepEqual( | ||
| statuses, | ||
| [201, 409], | ||
| 'exactly one add succeeds (201), the other is rejected (409); without the lock both would 201 and silently last-writer-win', | ||
| ); | ||
|
|
||
| // The card that did land must come from the winning request, | ||
| // not a torn write. Both POSTs above used waitForIndex=true so | ||
| // the read here is deterministic. | ||
| let winning = respA.status === 201 ? 'AddA' : 'AddB'; | ||
| let finalResponse = await request | ||
| .get('/concurrent-add') | ||
| .set('Accept', SupportedMimeType.CardJson) | ||
| .set('Authorization', auth); | ||
| assert.strictEqual(finalResponse.status, 200, 'card readable'); | ||
| let finalDoc = finalResponse.body as LooseSingleCardDocument; | ||
| assert.strictEqual( | ||
| finalDoc.data.attributes?.firstName, | ||
| winning, | ||
| 'final card matches the winning concurrent add', | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.