-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): add detection and recovery for missing mutation events (#7576
) ### Description In very rare cases, the /listen endpoint may drop mutation events. This PR implements detection of "holes" in the received mutation events, and implements error recovery by restarting the connection. ### What to review Are the threshold values sensible? Error detection and recovery is now triggered by the following thresholds. - `DEFAULT_MAX_BUFFER_SIZE=20`: If we se more than 20 mutation events that can't be applied in order we treat it as an error - `DEFAULT_DEADLINE_MS=30_000`: If 30 seconds pass since we last received a message that can't be applied in order we treat it as an error ### Testing This PR includes unit tests and has also undergone extensive manual testing. ### Notes for release - Fixes an issue that could in rare cases lead to an outdated version of the document being displayed locally
- Loading branch information
Showing
9 changed files
with
628 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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,3 @@ | ||
import createDebug from 'debug' | ||
|
||
export const debug = createDebug('sanity:document-store') |
This file contains 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 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
44 changes: 44 additions & 0 deletions
44
packages/sanity/src/core/store/_legacy/document/utils/__test__/eventChainUtils.test.ts
This file contains 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,44 @@ | ||
import {describe, expect, it} from '@jest/globals' | ||
|
||
import {discardChainTo, toOrderedChains} from '../eventChainUtils' | ||
import {mutationEvent} from './test-utils' | ||
|
||
describe(toOrderedChains.name, () => { | ||
it('returns a list of chains', () => { | ||
const events = [ | ||
// missing | ||
mutationEvent({previousRev: 'a', resultRev: 'b', mutations: []}), | ||
mutationEvent({previousRev: 'b', resultRev: 'c', mutations: []}), | ||
mutationEvent({previousRev: 'c', resultRev: 'd', mutations: []}), | ||
mutationEvent({previousRev: 'd', resultRev: 'e', mutations: []}), | ||
mutationEvent({previousRev: 'e', resultRev: 'f', mutations: []}), | ||
|
||
// mutationEvent({previousRev: 'g', resultRev: 'h', mutations: []}), // missing | ||
mutationEvent({previousRev: 'h', resultRev: 'i', mutations: []}), | ||
mutationEvent({previousRev: 'i', resultRev: 'j', mutations: []}), | ||
mutationEvent({previousRev: 'j', resultRev: 'k', mutations: []}), | ||
mutationEvent({previousRev: 'k', resultRev: 'l', mutations: []}), | ||
mutationEvent({previousRev: 'l', resultRev: 'm', mutations: []}), | ||
] | ||
const [first, second] = toOrderedChains(events) | ||
|
||
expect(first.map((ev) => ev.resultRev)).toEqual(['b', 'c', 'd', 'e', 'f']) | ||
expect(second.map((ev) => ev.resultRev)).toEqual(['i', 'j', 'k', 'l', 'm']) | ||
}) | ||
}) | ||
|
||
describe(discardChainTo.name, () => { | ||
it('discards mutation events in the chain up to the provided revision', () => { | ||
const events = [ | ||
mutationEvent({previousRev: 'a', resultRev: 'b', mutations: []}), | ||
mutationEvent({previousRev: 'b', resultRev: 'c', mutations: []}), | ||
mutationEvent({previousRev: 'c', resultRev: 'd', mutations: []}), | ||
mutationEvent({previousRev: 'd', resultRev: 'e', mutations: []}), | ||
mutationEvent({previousRev: 'e', resultRev: 'f', mutations: []}), | ||
] | ||
const [discarded, applicable] = discardChainTo(events, 'd') | ||
// Note, it's still in the order received | ||
expect(discarded.map((ev) => ev.resultRev)).toEqual(['b', 'c', 'd']) | ||
expect(applicable.map((ev) => ev.resultRev)).toEqual(['e', 'f']) | ||
}) | ||
}) |
Oops, something went wrong.