Repository navigation
RTC: Add CRDT migrations, migrate table cell data in existing docs - #77075
alecgeatches wants to merge 10 commits into
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Size Change: +616 B (+0.01%) Total Size: 7.74 MB 📦 View Changed
ℹ️ View Unchanged
|
|
Flaky tests detected in 6245f61. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/24050241421
|
|
@youknowriad If you have time, I'd love your insight on this PR. We've seen some CRDT corruptions from earlier bugs that are still being encountered in places like https://make.wordpress.org/ and this is a strategy to fix/migrate CRDT documents in-place when the editor boots up. We could alternatively detect errors and just drop the whole document and start from scratch, but this particular problem is easier to fix. Do you have any overall advice on data migrations in GB? |
| ( stateMap.get( CRDT_STATE_MAP_VERSION_KEY ) as number ) ?? 0; | ||
|
|
||
| if ( docVersion >= CRDT_DOC_VERSION ) { | ||
| return 'clean'; |
There was a problem hiding this comment.
Would want to type these as enums so that can be used here instead
| * from the database record. | ||
| */ | ||
| const migrations: Array< { version: number; migrate: MigrationFn } > = [ | ||
| { version: 2, migrate: v2MigrateTableCellContent }, |
There was a problem hiding this comment.
Right now, there's no restriction on the version appearing just once.
There's also a complication that could happen if there's multiple migrations needed for a version down the line. Would it be better to have all those migrations in one single migrate function or have them be broken up into individual migration functions?
We are also relying on the defensive sort to make sure the migration is done in the right order.
Bearing all this in mind, I'd suggest two tweaks:
- Enforce that multiple versions are not allowed. There can only be one. This'll protect the defensive sort as well.
- Allow for multiple migrate functions for one version. We can skip this, and just make it clear that run everything in the migrate function.
There was a problem hiding this comment.
There's a part of me that also wants to have a starting and end version. I don't have a good use case for it just yet in the context of Yjs, but I have seen this being useful in the past for conversion systems. I do think it adds a lot more complications here like, what happens if a version in the middle is skipped? We need defensive checks for that. That'll be annoying.
| attrName | ||
| ); | ||
|
|
||
| if ( attrSchema ) { |
There was a problem hiding this comment.
Should there be a type check here?
| const schema: Record< string, BlockAttributeSchema > = {}; | ||
|
|
||
| for ( const attrName of attributes.keys() ) { | ||
| const attrSchema = getBlockAttributeSchema( |
There was a problem hiding this comment.
What would happen if by chance the underlying blockTypes its expecting are empty? Should we return incompatible in that case as well for defensive purposes?
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { Y } from '@wordpress/sync'; |
There was a problem hiding this comment.
There's a bunch of test paths missing that target the error pathways. Those are the biggest source of failure for a conversion system, especially when partial failures or random failures happen.
The ones I think should be covered:
- Incompatible migration which leads to the doc being dropped
- Defensive checks for types being triggered
- Partial corruption in blocks
- Reviewing if there are any other areas where a mix of clean/failures is useful so it's not just everything fails or everything is clean.
Whether this is an e2e test or in this suite, it's fine but just needs to be covered imo.
| * | ||
| * @param ydoc The CRDT document to migrate. | ||
| */ | ||
| export function v2MigrateTableCellContent( ydoc: CRDTDoc ): MigrationResult { |
There was a problem hiding this comment.
Nothing tells us that the ydoc here is a post type that supports blocks. Maybe this is another kind of blocks in another entity.
I wonder if the migrations should be part of the sync config in entities.ts
| * allows implementors to invalidate persisted CRDT docs. | ||
| */ | ||
| export const CRDT_DOC_VERSION = 1; | ||
| export const CRDT_DOC_VERSION = 2; |
There was a problem hiding this comment.
It's a bit weird that the version is in "sync" and the migration in "core-data". I also wonder if we actually need a version per entity, instead of a global version like that. So I'm wondering if this version should move to the entities.
Maybe we need to things, a global version which is more related to the YJS format/version and an entity version which is more related to how each entity is persisted.
|
Howdy @dmonad! We'd also like to run the idea of a live CRDT migration by you. Here, we're addressing a previous implementation bug that can corrupt an active CRDT document and blank out table cells. The solution here is to use the document version to set up a CRDT migration to fix broken content, with a pattern we can reuse in the future. Do you have any experience with a system like that? Any footguns we should be carefully consider here? |
|
This is a long topic, and I appreciate that you're thinking about this problem early. Usually this is something I discuss in lengthy meetings with my sponsors, but WordPress hasn't set up something like this yet.
|
|
Closing this PR given the advice above to avoid client-side migration. |
What?
This adds a migration system for persisted CRDT documents. This PR also includes a first migration, which fixes existing corrupted CRDTs from #76597 retroactively.
This PR gives future breaking changes or bug fixes the ability to migrate the CRDT on load. This improves upon a previous CRDT versioning attempt in #74561 by giving us a pathway to both migrations that are safe and those that require a new document.
Why?
PR #76597 fixed a bug where table cell content was serialized as
{}(empty objects) instead ofY.Textwith HTML strings. However, users who already had a persisted CRDT document from before the fix still see blank table cells when they open the editor, because the corrupted CRDT data still exists until a post is edited and resaved.We also anticipate needing to fix other CRDT data issues in the future, or make breaking changes to the protocol or structure of a document, so this PR introduces a general migration framework rather than a one-off patch.
How?
The CRDT state map already has a
versionfield, previously unused. This PR bumpsCRDT_DOC_VERSIONfrom 1 to 2 and introduces a migration pipeline that runs when a persisted doc's version is behind the current version. The version check is a simple int comparison, so there's very little performance cost when no migration is needed.Each migration returns one of three results:
'clean': No changes needed.'migrated': The document was patched in place.'incompatible': The document cannot be migrated and should be discarded entirely, rebuilding from the database record.The migration runs on the deserialized
tempDocbefore it's applied to the active Y.Doc. A try/catch wraps the call so that if a migration throws, it falls back to'incompatible'(discard and rebuild). This means a buggy migration won't corrupt a document beyond "discard and rebuild from the database."The first migration (
v2-table-cell-content) walks all blocks, uses the block attribute schema to find rich-text values that aren't stored asY.Text, and replaces them withY.Text(''). The actual cell content is then restored by the existing invalidation logic, which detects the empty cells vs. database HTML mismatch and fills in the correct values.New migrations can be added by creating a new file in
crdt-migrations/v<N>-<description>.ts, incrementingCRDT_DOC_VERSION, and appending to the registry array inindex.ts.Testing Instructions
First, use a fresh
trunkbuild and replicate the issue with:Ensure RTC is enabled via Settings -> Writing -> "Enable real-time collaboration" checkbox.
Download test-create-broken-table.sh locally to the root of your repo. This script will use
wp-envto create a post with the bug fixed in RTC: Fix CRDT serialization of nested RichText attributes #76597, so ensurewp-envis running.Run the script:
Open the post in the editor. See that table cells are broken:
Next, test the migration. Switch to this PR's branch and build:
git checkout add/rtc-crdt-migration && npm run buildRefresh the page. Cells should become populated:
table-bug-fixed.mov
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Used for: Designing and implementing the migration system