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

VDiff: Support diffing tables without a defined Primary Key #14794

Merged
merged 6 commits into from
Dec 20, 2023

Conversation

mattlord
Copy link
Contributor

@mattlord mattlord commented Dec 15, 2023

Description

VReplication supports tables without defined Primary Keys, but VDiff did not. If you executed a VDiff on a workflow that included a table without one, it would fail like this (see issue):

VDiff Summary for customer.commerce2customer (4ecb6a28-9263-404e-a4e9-d737ecbca5e5)
State:        error
              Error: (shard 0) buildPlan: error getting PK column collations for table tnopk: empty list supplied for vars2
RowsCompared: 0
HasMismatch:  false
StartedAt:    2023-12-15 21:29:47

Use "--format=json" for more detailed output

This PR adds support for diffing tables that have no defined Primary Key. It uses a Primary Key equivalent (unique index on non-null column(s)) if one exists, and if not then all of the columns in the table as a substitute — just as does VReplication, and as does MySQL row-based replication (although there it will use any index if there is one first).

Basic manual test:

./101_initial_cluster.sh

mysql < ../common/insert_commerce_data.sql

./201_customer_tablets.sh

mysql commerce -e "create table nopk (name varchar(100), age bigint unsigned)"

for _ in {1..15}; do
  mysql commerce -e "insert into nopk values ('${RANDOM}_person_${RANDOM}', ${RANDOM}); insert into nopk select * from nopk;"
done

vtctldclient MoveTables --workflow commerce2customer --target-keyspace customer create --source-keyspace commerce --all-tables

vtctldclient vdiff --workflow=commerce2customer --target-keyspace=customer create

sleep 10

vtctldclient vdiff --workflow=commerce2customer --target-keyspace=customer show last --verbose

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: Add warning now that we support tables with PKs website#1651

Copy link
Contributor

vitess-bot bot commented Dec 15, 2023

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 15, 2023
@github-actions github-actions bot added this to the v19.0.0 milestone Dec 15, 2023
This also more generally adds support for diffing tables
without PK columns.

Signed-off-by: Matt Lord <[email protected]>
@mattlord mattlord changed the title VDiff: Don't try to get PK col info from I_S when there are no PK cols VDiff: Support diffing tables without a defined Primary Key Dec 16, 2023
@mattlord mattlord added Type: Enhancement Logical improvement (somewhere between a bug and feature) and removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work 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 16, 2023
Signed-off-by: Matt Lord <[email protected]>
@mattlord mattlord force-pushed the vdiff_no_pk branch 2 times, most recently from 3926e94 to afaa767 Compare December 16, 2023 19:36
@mattlord mattlord marked this pull request as ready for review December 16, 2023 19:52
@mattlord mattlord removed the request for review from deepthi December 16, 2023 19:53
@mattlord mattlord requested review from deepthi and removed request for systay, GuptaManan100 and harshit-gangal December 16, 2023 19:53
@@ -579,13 +579,7 @@ func (mysqld *Mysqld) ApplySchemaChange(ctx context.Context, dbName string, chan
// defined PRIMARY KEY then it may return the columns for
// that index if it is likely the most efficient one amongst
// the available PKE indexes on the table.
func (mysqld *Mysqld) GetPrimaryKeyEquivalentColumns(ctx context.Context, dbName, table string) ([]string, string, error) {
conn, err := getPoolReconnect(ctx, mysqld.dbaPool)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did the functionality change for this PR require a change in how we acquire a connection here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I first created this I had a mysqlctl.Mysqld instance readily available in the callsite. In working on this, I realized that I should have changed it when using it in vstreamer as I created a new instance there, which is heavy as it creates conn pools etc (and I wasn't properly calling close in a defer 🤦‍♂️), so I changed it here to use a callback to talk to the DB instead. It's much lighter and is far better (and is an already established pattern in the mysqlctl package).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we adopt this pattern, then we will find ourselves passing exec functions in a large amount of functions - not saying it's wrong - just thinking of the code/signature impact.

Copy link
Contributor Author

@mattlord mattlord Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already a pattern in that file/package and elsewhere. I agree that it's not one which should be used w/o good reason.

Copy link
Contributor

@shlomi-noach shlomi-noach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good with one question about allowing full table scans.

Also, it looks like we're implementing similar behaviors in different approaches; the vstreamer/vplayer approach uses different code paths, right?

tp.table.PrimaryKeyColumns = append(tp.table.PrimaryKeyColumns, pkeCols...)
} else {
// We use every column together as a substitute PK.
tp.table.PrimaryKeyColumns = append(tp.table.PrimaryKeyColumns, tp.table.Columns...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to allow full scan in VDiff? Should we instead say this table isn't supported? I'm imagining a VDiff running over weeks - as a user of VDiff I'd not want it to run so long.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, yes. We allow it in VReplication as well. This is up to the user -- it's not uncommon to have small tables w/o a primary key or PKE. In some cases all queries against the table will be a full scan. It's not our call to say that you shouldn't be able to move those tables and diff them after doing so.

@mattlord
Copy link
Contributor Author

Also, it looks like we're implementing similar behaviors in different approaches; the vstreamer/vplayer approach uses different code paths, right?

I'm not sure what you're referring to here. The vreplicator, vstreamer, and now vdiff are all using the same function and code path for managing PKEs. Maybe I'm misunderstanding?

@shlomi-noach
Copy link
Contributor

shlomi-noach commented Dec 19, 2023

I'm not sure what you're referring to here. The vreplicator, vstreamer, and now vdiff are all using the same function and code path for managing PKEs. Maybe I'm misunderstanding?

No, you were good. I wasn't clear, even to myself. OK, the example I was thinking of was actually Online DDL, where we can use ay non-NULLable unique key for the migration. Context: #8364.

In Online DDL, we get the shared unique key of two tables:

https://github.com/vitessio/vitess/pull/8364/files#diff-552cf284bd2c1a9e4ae1c9f9f1c350c7dcc04177ed3a663b0670d084518e7fe1R107

Having read a table's unique keys:

https://github.com/vitessio/vitess/pull/8364/files#diff-552cf284bd2c1a9e4ae1c9f9f1c350c7dcc04177ed3a663b0670d084518e7fe1R205

We populate the Rule with unique key info:

https://github.com/vitessio/vitess/pull/8364/files#diff-5e69c397e15dcfda72bb6fe8dba93b2d11a8d2d7b594ebfbbdcfdc07bfc3791dR165

We then read that as needed to override PK columns:

https://github.com/vitessio/vitess/pull/8364/files#diff-944642971422bb00ed68466f784f8b586f5e800f57e0c46002d6194c517cb1a0R526-R533

etc.

This is what I was thinking of as different approaches to analyzing/using an alternate unique key.

@mattlord
Copy link
Contributor Author

I'm not sure what you're referring to here. The vreplicator, vstreamer, and now vdiff are all using the same function and code path for managing PKEs. Maybe I'm misunderstanding?

No, you were good. I wasn't clear, even to myself. OK, the example I was thinking of was actually Online DDL, where we can use ay non-NULLable unique key for the migration. Context: #8364.

In Online DDL, we get the shared unique key of two tables:

https://github.com/vitessio/vitess/pull/8364/files#diff-552cf284bd2c1a9e4ae1c9f9f1c350c7dcc04177ed3a663b0670d084518e7fe1R107

Having read a table's unique keys:

https://github.com/vitessio/vitess/pull/8364/files#diff-552cf284bd2c1a9e4ae1c9f9f1c350c7dcc04177ed3a663b0670d084518e7fe1R205

We populate the Rule with unique key info:

https://github.com/vitessio/vitess/pull/8364/files#diff-5e69c397e15dcfda72bb6fe8dba93b2d11a8d2d7b594ebfbbdcfdc07bfc3791dR165

We then read that as needed to override PK columns:

https://github.com/vitessio/vitess/pull/8364/files#diff-944642971422bb00ed68466f784f8b586f5e800f57e0c46002d6194c517cb1a0R526-R533

etc.

This is what I was thinking of as different approaches to analyzing/using an alternate unique key.

Ah, yes. I have a standing TODO to unify these. My thinking is that we'll probably eventually drop non-native OnlineDDL support -- meaning GH-OST and PTOSC -- and then we can remove OnlineDDL's handling and rely on VReplication. That make sense?

@shlomi-noach
Copy link
Contributor

shlomi-noach commented Dec 19, 2023

we'll probably eventually drop non-native OnlineDDL support -- meaning GH-OST and PTOSC -- and then we can remove OnlineDDL's handling and rely on VReplication. That make sense?

The two are unrelated. Meaning, the logic used today for analyzing shared unique key -- is done only for the vreplication migrations.

At any case, this cleanup is for the future - no need to do anything in this PR, I just saw fit to mention this duality.

mattlord added a commit to planetscale/vitess-website that referenced this pull request Dec 19, 2023
Support for which was added in:
vitessio/vitess#14794

Signed-off-by: Matt Lord <[email protected]>
@mattlord mattlord removed the NeedsWebsiteDocsUpdate What it says label Dec 19, 2023
mattlord added a commit to planetscale/vitess-website that referenced this pull request Dec 19, 2023
Support for which was added in:
vitessio/vitess#14794

Signed-off-by: Matt Lord <[email protected]>
mattlord added a commit to planetscale/vitess-website that referenced this pull request Dec 20, 2023
Support for which was added in:
vitessio/vitess#14794

Signed-off-by: Matt Lord <[email protected]>
@mattlord mattlord merged commit be7b670 into vitessio:main Dec 20, 2023
116 of 117 checks passed
@mattlord mattlord deleted the vdiff_no_pk branch December 20, 2023 02:20
rohit-nayak-ps pushed a commit to vitessio/website that referenced this pull request Dec 20, 2023
ejortegau referenced this pull request in slackhq/vitess Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: VReplication Type: Bug Type: Enhancement Logical improvement (somewhere between a bug and feature)
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Bug Report: VDiff fails on workflow that includes a table without a Primary Key
3 participants