-
Notifications
You must be signed in to change notification settings - Fork 3.5k
RTC: Ensure a single canonical update log for collaborative edits. #11660
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
49f278e
9c9c36d
37e8a55
a3354ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -256,14 +256,132 @@ private function get_storage_post_id( string $room ): ?int { | |
| ) | ||
| ); | ||
|
|
||
| if ( is_int( $post_id ) ) { | ||
| self::$storage_post_ids[ $room_hash ] = $post_id; | ||
| return $post_id; | ||
| if ( is_int( $post_id ) && $post_id > 0 ) { | ||
| $canonical_post_id = $this->resolve_canonical_storage_post_id_after_insert( $room_hash, $post_id ); | ||
| if ( null === $canonical_post_id ) { | ||
| return null; | ||
| } | ||
|
|
||
| self::$storage_post_ids[ $room_hash ] = $canonical_post_id; | ||
| return $canonical_post_id; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the canonical room storage post after inserting a new post. | ||
| * | ||
| * Two concurrent first writers can both miss the lookup above and create | ||
| * storage posts for the same room hash. Depending on the exact interleaving, | ||
| * WordPress may create either a duplicate exact slug or a suffixed slug. | ||
| * When this request receives a non-canonical post, redirect it to the | ||
| * canonical storage before any sync or awareness data is written. | ||
| * | ||
| * @since 7.0.0 | ||
| * | ||
| * @param string $room_hash MD5 hash of the room identifier. | ||
| * @param int $inserted_post_id Post ID returned by wp_insert_post(). | ||
| * @return int|null Canonical storage post ID. | ||
| */ | ||
| private function resolve_canonical_storage_post_id_after_insert( string $room_hash, int $inserted_post_id ): ?int { | ||
| $canonical_post_id = $this->find_canonical_storage_post_id( $room_hash ); | ||
| if ( null === $canonical_post_id ) { | ||
| $canonical_post_id = $this->promote_storage_post_to_canonical_slug( $room_hash, $inserted_post_id ); | ||
| } | ||
|
|
||
| if ( null === $canonical_post_id ) { | ||
| wp_delete_post( $inserted_post_id, true ); | ||
| return null; | ||
| } | ||
|
|
||
| if ( $inserted_post_id !== $canonical_post_id ) { | ||
| /* | ||
| * This request just created a duplicate empty storage post because | ||
| * another first writer won the exact-slug race. Delete only that | ||
| * just-created empty post and write this request's data to canonical | ||
| * storage. | ||
| * | ||
| * Do not merge or delete older duplicate storage posts here. A stale | ||
| * request may already hold a duplicate post ID, and MySQL advisory | ||
| * locks/raw transactions are not a reliable cross-server fence under | ||
| * HyperDB or database proxies. Future historical repair should be | ||
| * bounded and idempotent, or run out of band with primary-pinned | ||
| * verification and a grace period before deleting duplicates. | ||
| */ | ||
| wp_delete_post( $inserted_post_id, true ); | ||
| } | ||
|
|
||
| return $canonical_post_id; | ||
| } | ||
|
|
||
| /** | ||
| * Finds the canonical storage post for a room hash. | ||
| * | ||
| * The canonical post is the oldest published storage post with the exact | ||
| * room hash slug. Suffixed slugs are repair candidates, not canonical. | ||
| * | ||
| * @since 7.0.0 | ||
| * | ||
| * @param string $room_hash MD5 hash of the room identifier. | ||
| * @return int|null Canonical storage post ID. | ||
| */ | ||
| private function find_canonical_storage_post_id( string $room_hash ): ?int { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function appears to always return null. I have submitted additional fixes in Gutenberg.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks @t-hamano — this slipped in though my partial application of a review comment. I apologize for missing that. |
||
| $posts = get_posts( | ||
| array( | ||
| 'post_type' => self::POST_TYPE, | ||
| 'posts_per_page' => 1, | ||
| 'post_status' => 'publish', | ||
| 'name' => $room_hash, | ||
| 'fields' => 'ids', | ||
| 'orderby' => 'ID', | ||
| 'order' => 'ASC', | ||
| ) | ||
| ); | ||
|
|
||
| if ( empty( $posts ) ) { | ||
| return null; | ||
| } | ||
|
|
||
| return $posts[0]; | ||
| } | ||
|
|
||
| /** | ||
| * Promotes a storage post to the canonical room slug. | ||
| * | ||
| * @since 7.0.0 | ||
| * | ||
| * @param string $room_hash MD5 hash of the room identifier. | ||
| * @param int $post_id Post ID to promote. | ||
| * @return int|null Promoted post ID on success. | ||
| */ | ||
| private function promote_storage_post_to_canonical_slug( string $room_hash, int $post_id ): ?int { | ||
| global $wpdb; | ||
|
|
||
| /* | ||
| * @todo Could this be replaced by {@see wp_update_post()}? Could we experience | ||
| * a race with other posts having a different post type or post status? | ||
| */ | ||
| $result = $wpdb->update( | ||
| $wpdb->posts, | ||
| array( 'post_name' => $room_hash ), | ||
| array( | ||
| 'ID' => $post_id, | ||
| 'post_type' => self::POST_TYPE, | ||
| 'post_status' => 'publish', | ||
| ), | ||
| array( '%s' ), | ||
| array( '%d', '%s', '%s' ) | ||
| ); | ||
|
|
||
| if ( false === $result ) { | ||
| return null; | ||
| } | ||
|
|
||
| clean_post_cache( $post_id ); | ||
| return $post_id; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the number of updates stored for a given room. | ||
| * | ||
|
|
||
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.
I would still consider checking for
intval()instead fornull, but in these cases the case is not as strong as above.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.
yeah. hm. the functions returning the
$canonical_post_idare both typed to returnint|null(unlike$wpdb->insert()etc…)there should be no reasonable way to get to this point if it’s neither
nullnorint(strict-mode aside), so I think in this case it’s more or less equivalent, no?the
intval()emphasizes what it expects and the null-check emphasizes the fact that a post id might not exist.