-
-
Notifications
You must be signed in to change notification settings - Fork 3k
fix: don't let a failed revision write leave a hole in pad history #8141
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
JohnMcLear
wants to merge
2
commits into
develop
Choose a base branch
from
fix/8134-append-revision-atomicity
base: develop
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.
+254
−1
Open
Changes from all commits
Commits
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
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
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,189 @@ | ||
| 'use strict'; | ||
|
|
||
| // Regression coverage for #8134. | ||
| // | ||
| // appendRevision() writes the revision record and the pad record (which | ||
| // carries `head`) as two independent writes. When the revision write failed | ||
| // and the pad record landed, the pad claimed a revision that was never | ||
| // stored -- and the next successful append wrote head+1 straight over it, | ||
| // making the gap permanent. Every later pad.check() then tripped on the | ||
| // missing revision, which blocks cleanup/compaction forever. | ||
| // | ||
| // The reporter on #8134 hit exactly this: revisions 599 and 601 present, | ||
| // 600 absent, cleanup refusing to run. | ||
|
|
||
| const assert = require('assert').strict; | ||
| const common = require('../common'); | ||
| const padManager = require('../../../node/db/PadManager'); | ||
| const db = require('../../../node/db/DB'); | ||
|
|
||
| describe(__filename, function () { | ||
| let padId: string; | ||
|
|
||
| before(async function () { await common.init(); }); | ||
|
|
||
| beforeEach(async function () { | ||
| padId = common.randomString(); | ||
| assert(!await padManager.doesPadExist(padId)); | ||
| }); | ||
|
|
||
| // Runs `fn` with the write to `failKey` rejecting. | ||
| const withFailingWrite = async (failKey: string, fn: () => Promise<any>) => { | ||
| const realSet = db.set; | ||
| db.set = async (key: string, value: unknown) => { | ||
| if (key === failKey) throw new Error('simulated backend write failure'); | ||
| return await realSet(key, value); | ||
| }; | ||
| try { | ||
| return await fn(); | ||
| } finally { | ||
| db.set = realSet; | ||
| } | ||
| }; | ||
|
|
||
| describe('when the revision write fails', function () { | ||
| it('rejects', async function () { | ||
| const pad = await padManager.getPad(padId); | ||
| await pad.appendText('one\n'); | ||
| const doomed = pad.getHeadRevisionNumber() + 1; | ||
| await withFailingWrite(`pad:${padId}:revs:${doomed}`, async () => { | ||
| await assert.rejects(pad.appendText('two\n'), | ||
| /simulated backend write failure/); | ||
| }); | ||
| }); | ||
|
|
||
| it('does not leave head pointing past the stored history', async function () { | ||
| const pad = await padManager.getPad(padId); | ||
| await pad.appendText('one\n'); | ||
| const goodHead = pad.getHeadRevisionNumber(); | ||
| const doomed = goodHead + 1; | ||
|
|
||
| await withFailingWrite(`pad:${padId}:revs:${doomed}`, async () => { | ||
| await assert.rejects(pad.appendText('two\n')); | ||
| }); | ||
|
|
||
| assert.equal(pad.getHeadRevisionNumber(), goodHead, | ||
| 'in-memory head should be rolled back'); | ||
|
|
||
| padManager.unloadPad(padId); | ||
| const padRecord = await db.get(`pad:${padId}`); | ||
| assert.equal(padRecord.head, goodHead, | ||
| 'persisted head should be rolled back'); | ||
| // `== null`, not `assert.equal(..., null)`: the dirty/rusty driver | ||
| // yields null for an absent key on Linux but undefined on Windows. | ||
| // Either way the record is not there. | ||
| assert.ok(await db.get(`pad:${padId}:revs:${doomed}`) == null, | ||
| 'the failed revision should not exist'); | ||
| }); | ||
|
|
||
| it('rolls the in-memory text back too', async function () { | ||
| const pad = await padManager.getPad(padId); | ||
| await pad.appendText('one\n'); | ||
| const textBefore = pad.atext.text; | ||
| const doomed = pad.getHeadRevisionNumber() + 1; | ||
|
|
||
| await withFailingWrite(`pad:${padId}:revs:${doomed}`, async () => { | ||
| await assert.rejects(pad.appendText('two\n')); | ||
| }); | ||
|
|
||
| assert.equal(pad.atext.text, textBefore, | ||
| 'atext must not keep changes that were never stored'); | ||
| assert.ok(!pad.atext.text.includes('two')); | ||
| }); | ||
|
|
||
| it('leaves the pad consistent for a later append', async function () { | ||
| const pad = await padManager.getPad(padId); | ||
| await pad.appendText('one\n'); | ||
| const goodHead = pad.getHeadRevisionNumber(); | ||
| const doomed = goodHead + 1; | ||
|
|
||
| await withFailingWrite(`pad:${padId}:revs:${doomed}`, async () => { | ||
| await assert.rejects(pad.appendText('two\n')); | ||
| }); | ||
|
|
||
| // The next append reuses the revision number rather than skipping it. | ||
| await pad.appendText('three\n'); | ||
| assert.equal(pad.getHeadRevisionNumber(), doomed); | ||
| assert.ok(await db.get(`pad:${padId}:revs:${doomed}`) != null); | ||
| }); | ||
|
|
||
| it('leaves the pad passing check()', async function () { | ||
| // The whole point: a failed write must not make the pad | ||
| // permanently uncleanable. | ||
| const pad = await padManager.getPad(padId); | ||
| for (let i = 0; i < 3; i++) await pad.appendText(`line ${i}\n`); | ||
| const doomed = pad.getHeadRevisionNumber() + 1; | ||
|
|
||
| await withFailingWrite(`pad:${padId}:revs:${doomed}`, async () => { | ||
| await assert.rejects(pad.appendText('doomed\n')); | ||
| }); | ||
| await pad.appendText('after\n'); | ||
|
|
||
| padManager.unloadPad(padId); | ||
| await (await padManager.getPad(padId)).check(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('when the pad record write fails', function () { | ||
| it('rejects and rolls back without orphaning head', async function () { | ||
| const pad = await padManager.getPad(padId); | ||
| await pad.appendText('one\n'); | ||
| const goodHead = pad.getHeadRevisionNumber(); | ||
|
|
||
| // The rollback re-saves the pad record, so let only the first | ||
| // `pad:<id>` write fail. | ||
| const realSet = db.set; | ||
| let failed = false; | ||
| db.set = async (key: string, value: unknown) => { | ||
| if (key === `pad:${padId}` && !failed) { | ||
| failed = true; | ||
| throw new Error('simulated pad record write failure'); | ||
| } | ||
| return await realSet(key, value); | ||
| }; | ||
| try { | ||
| await assert.rejects(pad.appendText('two\n')); | ||
| } finally { | ||
| db.set = realSet; | ||
| } | ||
|
|
||
| assert.equal(pad.getHeadRevisionNumber(), goodHead); | ||
| padManager.unloadPad(padId); | ||
| const padRecord = await db.get(`pad:${padId}`); | ||
| assert.equal(padRecord.head, goodHead); | ||
| // A stored-but-unreferenced revision record is harmless: check() | ||
| // only walks 0..head. | ||
| await (await padManager.getPad(padId)).check(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('side effects', function () { | ||
| it('a throwing padUpdate hook does not roll back a stored revision', | ||
| async function () { | ||
| // Hook failures are not storage failures. Rolling back here would | ||
| // discard a revision that was written successfully. | ||
| const hooks = require('../../../static/js/pluginfw/hooks'); | ||
| const pad = await padManager.getPad(padId); | ||
| await pad.appendText('one\n'); | ||
| const goodHead = pad.getHeadRevisionNumber(); | ||
|
|
||
| const realACallAll = hooks.aCallAll; | ||
| hooks.aCallAll = async (hookName: string, ...rest: any[]) => { | ||
| if (hookName === 'padUpdate') throw new Error('plugin blew up'); | ||
| return await realACallAll(hookName, ...rest); | ||
| }; | ||
| try { | ||
| await assert.rejects(pad.appendText('two\n'), /plugin blew up/); | ||
| } finally { | ||
| hooks.aCallAll = realACallAll; | ||
| } | ||
|
|
||
| assert.equal(pad.getHeadRevisionNumber(), goodHead + 1, | ||
| 'the revision was stored, so head must stand'); | ||
| assert.ok(await db.get(`pad:${padId}:revs:${goodHead + 1}`) != null); | ||
|
|
||
| padManager.unloadPad(padId); | ||
| await (await padManager.getPad(padId)).check(); | ||
| }); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Rollback can be overwritten
🐞 Bug≡ CorrectnessAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools