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

schemadiff: Online DDL support, declarative based #16462

Merged
merged 10 commits into from
Jul 30, 2024

Conversation

shlomi-noach
Copy link
Contributor

@shlomi-noach shlomi-noach commented Jul 23, 2024

Description

Addresses #16229 and more. Replaces #16256 just as a succinct single commit change.

In this PR schemadiff takes more ownership in the Online DDL process. Specifically, it takes away much of the logic from vttablet/onlineddl/vrepl/* and vttablet/onlineddl/vrepl.go, but also:

  • Since schemadiff is purely declarative and does not use database connections, query based logic is replaced with programmatic logic.
  • We get rid of old and redundant structures, which we inherited from gh-ost: Column, ColumnList, UniqueKey and more. We already have the formal AST types in go/vt/sqlparser, and there was a lot of duplication of logic.
  • Likewise, we get rid of foreign-key specific logic, of regexp based parsing, and more.

Much of the logic removed as mentioned above is reincarnated in go/vt/schemadiff/onlineddl.go, where we introduce helper functions specific to Vitess's Online DDL implementations. Intentionally set apart from the general purpose table.go or column.go, these functions deal with questions such as:

  • Given a table change, is any data type expanded? This is used for revertibility analysis
  • Given a table change, is a new unique constraint introduces? Likewise used for revertibility analysis
  • Same for foreign keys.
  • Is a given index valid for VReplication iteration?
  • Which are the best keys for VReplication iteration?
  • Do the before/after tables satisfy iteration key requirements?
  • Which columns are shared between before/after tables? Which are dropped? Which iteration keys should we use?

To help formalizing some of the above, we introduce a few data types in schemadiff that assist in iteration, filtering, and generally inter-operate with each other to enrich column information:

  • ColumnDefinitionEntity (introduced in a previous PR) and many helper functions (is this column nullable? Is it numeric? What's the column's charset? etc.)
  • ColumnDefinitionEntityList - a list of the above ColumnDefinitionEntity
  • IndexDefinitionEntity and many helper functions (is this index unique? Does it have nullable columns? etc.)
  • IndexDefinitionEntityList - a list of the above IndexDefinitionEntity

These inter-connect, as mentioned above. For example, a column i int looks to be nullable, but if we know it's part of a PRIMARY key, then we implicitly know it is in fact non nullable.

What this means

This lets schemadiff pre-evaluate the ability to run Online DDL on a table, what risks there might be, what the revertibility implications might be, what keys will be used, and so forth, without actually having to read/write anything from/to the the database.

Testing

Thanks to the declarative-only nature of schemadiff, it is easier to do more testing in unit tests. For example, we copy+pasted all the revertible tests from go/test/endtoend/onlineddl/revert/ and they are now tested in unit tests.
There's otherwise a bunch of tests moved over from go/vt/vttablet/onlineddl/vrepl, almost as-is. And otherwise just a bunch more tests. Finally, all of the existing endtoend tests, including vrepl suite, reverts, scheduler, and more -- all are unchanged and are expected to exhibit no change of behavior.

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

Deployment Notes

…ysis, remove redundant datatypes from onlineddl/vrepl

Signed-off-by: Shlomi Noach <[email protected]>
Signed-off-by: Shlomi Noach <[email protected]>
@shlomi-noach shlomi-noach added Type: Enhancement Logical improvement (somewhere between a bug and feature) Component: schema management schemadiff and schema changes labels Jul 23, 2024
Copy link
Contributor

vitess-bot bot commented Jul 23, 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 Jul 23, 2024
@shlomi-noach shlomi-noach changed the title schemadiff: Online DDL suppor schemadiff: Online DDL support, declarative based Jul 23, 2024
@shlomi-noach shlomi-noach requested review from dbussink and a team July 23, 2024 12:25
@github-actions github-actions bot added this to the v21.0.0 milestone Jul 23, 2024
@shlomi-noach shlomi-noach removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work 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 Jul 23, 2024
Copy link

codecov bot commented Jul 23, 2024

Codecov Report

Attention: Patch coverage is 78.07018% with 150 lines in your changes missing coverage. Please review.

Project coverage is 68.76%. Comparing base (67b5a6d) to head (dbd20de).

Files Patch % Lines
go/vt/vttablet/onlineddl/vrepl.go 27.63% 55 Missing ⚠️
go/vt/schemadiff/onlineddl.go 89.56% 31 Missing ⚠️
go/vt/vttablet/onlineddl/executor.go 0.00% 29 Missing ⚠️
go/vt/schemadiff/column.go 87.68% 17 Missing ⚠️
go/vt/schemadiff/key.go 83.07% 11 Missing ⚠️
go/vt/schemadiff/table.go 90.74% 5 Missing ⚠️
go/vt/schemadiff/view.go 75.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16462      +/-   ##
==========================================
+ Coverage   68.62%   68.76%   +0.13%     
==========================================
  Files        1552     1549       -3     
  Lines      199559   199624      +65     
==========================================
+ Hits       136957   137264     +307     
+ Misses      62602    62360     -242     

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

m := make(map[int]string, len(c.ColumnDefinition.Type.EnumValues))
for i, enumValue := range c.ColumnDefinition.Type.EnumValues {
// SET and ENUM values are 1 indexed.
m[i+1] = enumValue
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't seem symmetric with EnumValuesOrdinals?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch. Fixed EnumValuesOrdinals, which is as yet unused.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added tests

@@ -445,6 +445,18 @@ type CreateTableEntity struct {
Env *Environment
}

func NewCreateTableEntityFromSQL(env *Environment, sql string) (*CreateTableEntity, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@shlomi-noach Do we want to have a symmetric function for views 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.

Added + testing.

Signed-off-by: Shlomi Noach <[email protected]>
Signed-off-by: Shlomi Noach <[email protected]>
@shlomi-noach shlomi-noach requested a review from a team July 25, 2024 08:19

// ColumnNames returns the names of the columns in the index.
func (i *IndexDefinitionEntity) ColumnNames() []string {
var names []string
Copy link
Contributor

Choose a reason for hiding this comment

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

We could preallocate this slice to a size equal to the length of i.IndexDefinition.Columns.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed.

@@ -0,0 +1,960 @@
/*
Copyright 2022 The Vitess Authors.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should use 2024 here.

@@ -0,0 +1,590 @@
/*
Copyright 2022 The Vitess Authors.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should use 2024 here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed.

// assumption that the later `targetColumn := targetColumns.GetColumn(expectedTargetName)`
// check is sufficient. It is not. It is possible that a columns was explicitly dropped
// and added (`DROP COLUMN c, ADD COLUMN c INT`) in the same ALTER TABLE statement.
// Without checking the ALTER TABLE statement, we would be folled to believe that column
Copy link
Contributor

Choose a reason for hiding this comment

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

Assuming folled should be fooled

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed.

@@ -4,3 +4,224 @@
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we remove this copyright at this point?

Either way, we should also add the vitess copyright.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. For this file the copyright should be just vitess. There is not derivative work from freno 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.

fixed.

Signed-off-by: Shlomi Noach <[email protected]>
Signed-off-by: Shlomi Noach <[email protected]>
Signed-off-by: Shlomi Noach <[email protected]>
Signed-off-by: Shlomi Noach <[email protected]>
@shlomi-noach shlomi-noach merged commit ad27066 into vitessio:main Jul 30, 2024
130 checks passed
@shlomi-noach shlomi-noach deleted the schemadiff-onlineddl-analysis branch July 30, 2024 15:49
venkatraju pushed a commit to slackhq/vitess that referenced this pull request Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: schema management schemadiff and schema changes Type: Enhancement Logical improvement (somewhere between a bug and feature)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants