Skip to content
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

VReplication: Support binlog_row_value_options=PARTIAL_JSON #17345

Open
wants to merge 25 commits into
base: main
Choose a base branch
from

Conversation

mattlord
Copy link
Contributor

@mattlord mattlord commented Dec 9, 2024

Description

This PR adds experimental support for --binlog-row-value-options=PARTIAL_JSON. You can read more about this here.

This can offer big wins in specific cases. For example...

  1. Let's say that you have a table with 1 JSON column, and the JSON document is often large.
  2. You update just a single row, to update $.price from 99.99 to 102.99 (inflation) and the original JSON document is 5MiB.
  3. Without binlog_row_value_options=PARTIAL_JSON the binlog event will have the full 5MiB document in the BEFORE (original) and AFTER (updated) images. So 10MiB now.
  4. With the option enabled, the BEFORE image still has the 5MiB document but the AFTER image only has a handful of bytes to represent the diff operation, JSON_REPLACE in this case, along with the path of $.price and the value of 102.99. So we've cut the bytes stored on disk, sent over the wire, and processed in memory by ~ 1/2.

Now if you imagine that instead of updating a single row, you updated 10,000 rows... let's say to remove the $.on_sale flag on the documents that had it set (Black Friday is over), you can see the big savings that comes with it.

Manual test:

make build

cd examples/local

./101_initial_cluster.sh; mysql < ../common/insert_commerce_data.sql; ./201_customer_tablets.sh
  
alias vtctldclient='command vtctldclient --server=localhost:15999'
  
mysql commerce -e "alter table customer add jd json; update customer set jd = '{\"salary\":100}'; update customer set jd = '{\"salary\":99}' where customer_id in (2,3,4)"
  
for uid in $(vtctldclient GetTablets | awk '{print $1}' | cut -d- -f2 | bc); do
  command mysql -u root --socket "${VTDATAROOT}/vt_0000000${uid}/mysql.sock" -e "set @@global.binlog_row_value_options=PARTIAL_JSON; set @@global.general_log=ON"
done
  
./202_move_tables.sh
  
commerce_primary_uid=$(vtctldclient GetTablets --keyspace commerce --tablet-type primary --shard "0" | awk '{print $1}' | cut -d- -f2 | bc)
customer_primary_uid=$(vtctldclient GetTablets --keyspace customer --tablet-type primary --shard "0" | awk '{print $1}' | cut -d- -f2 | bc)
  
command mysql -u root --socket "${VTDATAROOT}/vt_0000000${commerce_primary_uid}/mysql.sock" vt_commerce -e "update customer set jd = JSON_SET(jd, '$.role', 'manager')"
command mysql -u root --socket "${VTDATAROOT}/vt_0000000${commerce_primary_uid}/mysql.sock" vt_commerce -e "update customer set jd = JSON_SET(jd, '$.color', 'red')"
command mysql -u root --socket "${VTDATAROOT}/vt_0000000${commerce_primary_uid}/mysql.sock" vt_commerce -e "update customer set email = concat('new', email)"
  
sleep 2
command mysql -u root --socket "${VTDATAROOT}/vt_0000000${customer_primary_uid}/mysql.sock" vt_customer -e "select * from customer"
  
command mysql -u root --socket "${VTDATAROOT}/vt_0000000${commerce_primary_uid}/mysql.sock" vt_commerce -e "update customer set jd = JSON_SET(jd, '$.day', 'wednesday')"
command mysql -u root --socket "${VTDATAROOT}/vt_0000000${commerce_primary_uid}/mysql.sock" vt_commerce -e "update customer set jd = JSON_INSERT(JSON_REPLACE(jd, '$.day', 'friday'), '$.favorite_color', 'black')"
command mysql -u root --socket "${VTDATAROOT}/vt_0000000${commerce_primary_uid}/mysql.sock" vt_commerce -e "update customer set jd = JSON_SET(JSON_REMOVE(JSON_REPLACE(jd, '$.day', 'monday'), '$.favorite_color'), '$.hobby', 'skiing') where customer_id = 3"
  
sleep 2
command mysql -u root --socket "${VTDATAROOT}/vt_0000000${customer_primary_uid}/mysql.sock" vt_customer -e "select * from customer"
  
command mysql -u root --socket "${VTDATAROOT}/vt_0000000${commerce_primary_uid}/mysql.sock" vt_commerce -e "update customer set jd = JSON_SET(JSON_REMOVE(JSON_REPLACE(jd, '$.day', 'tuesday'), '$.favorite_color'), '$.hobby', 'skiing') where customer_id = 4"
command mysql -u root --socket "${VTDATAROOT}/vt_0000000${commerce_primary_uid}/mysql.sock" vt_commerce -e "update customer set jd = JSON_SET(JSON_SET(jd, '$.salary', 110), '$.role', 'IC') where customer_id = 4"
command mysql -u root --socket "${VTDATAROOT}/vt_0000000${commerce_primary_uid}/mysql.sock" vt_commerce -e "update customer set jd = JSON_SET(jd, '$.misc', '{\"address\":\"1012 S Park\", \"town\":\"Hastings\", \"state\":\"MI\"}') where customer_id = 1"
command mysql -u root --socket "${VTDATAROOT}/vt_0000000${commerce_primary_uid}/mysql.sock" vt_commerce -e "insert into customer (customer_id, email) values (10, '[email protected]'), (11, '[email protected]')"
command mysql -u root --socket "${VTDATAROOT}/vt_0000000${commerce_primary_uid}/mysql.sock" vt_commerce -e "update customer set jd = '\"null\"' where customer_id = 10"
command mysql -u root --socket "${VTDATAROOT}/vt_0000000${commerce_primary_uid}/mysql.sock" vt_commerce -e "update customer set jd = 'null' where customer_id = 11"
  
sleep 2
command mysql -u root --socket "${VTDATAROOT}/vt_0000000${customer_primary_uid}/mysql.sock" vt_customer -e "select * from customer"
  
command mysql -u root --socket "${VTDATAROOT}/vt_0000000${commerce_primary_uid}/mysql.sock" vt_commerce -e "update customer set customer_id=customer_id+100, jd=JSON_SET(jd, '$.day', 'friday')"
  
sleep 2
command mysql -u root --socket "${VTDATAROOT}/vt_0000000${customer_primary_uid}/mysql.sock" vt_customer -e "select * from customer"

With the final result:

+-------------+------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| customer_id | email                  | jd                                                                                                                                                                               |
+-------------+------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|         101 | [email protected]    | {"day": "friday", "misc": "{\"address\":\"1012 S Park\", \"town\":\"Hastings\", \"state\":\"MI\"}", "role": "manager", "color": "red", "salary": 100, "favorite_color": "black"} |
|         102 | [email protected]      | {"day": "friday", "role": "manager", "color": "red", "salary": 99, "favorite_color": "black"}                                                                                    |
|         103 | [email protected]  | {"day": "friday", "role": "manager", "color": "red", "hobby": "skiing", "salary": 99}                                                                                            |
|         104 | [email protected]      | {"day": "friday", "role": "IC", "color": "red", "hobby": "skiing", "salary": 110}                                                                                                |
|         105 | [email protected]      | {"day": "friday", "role": "manager", "color": "red", "salary": 100, "favorite_color": "black"}                                                                                   |
|         110 | [email protected]  | "null"                                                                                                                                                                           |
|         111 | [email protected] | null                                                                                                                                                                             |
+-------------+------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

Related Issue(s)

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

@mattlord mattlord added Type: Enhancement Logical improvement (somewhere between a bug and feature) Component: VReplication labels Dec 9, 2024
Copy link
Contributor

vitess-bot bot commented Dec 9, 2024

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Dec 9, 2024
@mattlord mattlord removed NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Dec 9, 2024
@github-actions github-actions bot added this to the v22.0.0 milestone Dec 9, 2024
@mattlord mattlord force-pushed the vrepl_partial_json branch 3 times, most recently from 1859876 to a44e7e5 Compare December 9, 2024 00:23
Copy link

codecov bot commented Dec 9, 2024

Codecov Report

Attention: Patch coverage is 53.21429% with 131 lines in your changes missing coverage. Please review.

Project coverage is 67.50%. Comparing base (0d84a49) to head (e68c035).
Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
...blet/tabletmanager/vreplication/replicator_plan.go 25.71% 78 Missing ⚠️
go/test/utils/binlog.go 48.00% 13 Missing ⚠️
go/mysql/binlog/binlog_json.go 84.28% 11 Missing ⚠️
go/mysql/binlog/rbr.go 38.46% 8 Missing ⚠️
...t/tabletmanager/vreplication/table_plan_partial.go 0.00% 8 Missing ⚠️
go/vt/vttablet/tabletserver/vstreamer/vstreamer.go 65.21% 8 Missing ⚠️
go/mysql/binlog_event_rbr.go 90.00% 3 Missing ⚠️
go/mysql/binlog_event_filepos.go 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #17345      +/-   ##
==========================================
+ Coverage   67.46%   67.50%   +0.04%     
==========================================
  Files        1581     1581              
  Lines      253934   254191     +257     
==========================================
+ Hits       171305   171595     +290     
+ Misses      82629    82596      -33     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: Matt Lord <[email protected]>
Signed-off-by: Matt Lord <[email protected]>
Signed-off-by: Matt Lord <[email protected]>
Signed-off-by: Matt Lord <[email protected]>
Signed-off-by: Matt Lord <[email protected]>
Signed-off-by: Matt Lord <[email protected]>
We still needed to increment the json col index for those.

Signed-off-by: Matt Lord <[email protected]>
@mattlord mattlord marked this pull request as draft December 12, 2024 20:06
@mattlord mattlord force-pushed the vrepl_partial_json branch 4 times, most recently from 3ad84b6 to fbd5648 Compare December 13, 2024 22:24
Signed-off-by: Matt Lord <[email protected]>
@mattlord mattlord changed the title VReplication: Support binlog-row-value-options=PARTIAL_JSON VReplication: Support binlog_row_value_options=PARTIAL_JSON Dec 13, 2024
@mattlord mattlord marked this pull request as ready for review December 14, 2024 00:48
@mattlord mattlord force-pushed the vrepl_partial_json branch 2 times, most recently from bb2c2e2 to 7fe38ad Compare December 14, 2024 03:16
@mattlord mattlord added release notes (needs details) This PR needs to be listed in the release notes in a dedicated section (deprecation notice, etc...) NeedsWebsiteDocsUpdate What it says labels Dec 14, 2024
@mattlord
Copy link
Contributor Author

mattlord commented Dec 14, 2024

I'm thinking that this is worth a note in the 22.0 summary. I'm also thinking that it's worth adding a blurb somewhere on the website about our experimental support for more efficient replication:

  • binlog_row_image=NOBLOB
  • binlog_row_value_options=PARTIAL_JSON

I'd like to know what reviewers think. If others disagree, then they or I can remove the labels for docs and release notes (I just added them now). Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: VReplication NeedsWebsiteDocsUpdate What it says release notes (needs details) This PR needs to be listed in the release notes in a dedicated section (deprecation notice, etc...) Type: Enhancement Logical improvement (somewhere between a bug and feature)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Support more efficient JSON replication
1 participant