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

Experimental: Multi-tenant import support in Vitess #15503

Merged

Conversation

rohit-nayak-ps
Copy link
Contributor

@rohit-nayak-ps rohit-nayak-ps commented Mar 17, 2024

Description

Please read the design RFC first at #15403.

This proposal outlines support for importing data from a multi-tenant database cluster into a single Vitess cluster

Motivation

The user has a multi-tenant database architecture. They have several separate MySQL databases, with common schemas, one per tenant. Each database will be imported using one MoveTables workflow, using an external keyspace pointing to the
source, with a common target keyspace. All tables, both in the source and target, will include a tenant_id column to uniquely identify the tenant.

We can run multiple such workflows in parallel, until all tenants are imported. Just like normal imports, we would also
like reverse workflows to be running: to allow for rollback.

Ideally, routing rules should be established. This ensures that queries targeting a specific tenant ID are routed correctly to the target if the tenant has already been imported. Otherwise, they should route to the source if the import workflow is in progress but not yet switched to the target.

Many setups with multi-tenancy will use a multi-schema approach, where each tenant has their own MySQL database on a
single server. In this case the queries will use a database qualifier, which matches their database name. So when they
are running the migrations into Vitess, they will continue to use the same database qualifier.

Assumptions

  • The target keyspace has its schema already initialized.
  • Each table in the target keyspace has a tenant id column with the same name and type on both source and target, and it
    should be part of the primary key of each table.
  • If the target keyspace is sharded, the vindex should be a multi-column vindex which includes the tenant id column.

Notes

  • The current implementation requires that all traffic, across all tablet types, be switched simultaneously for a tenant.
  • Although reverse replication will be operational, initiating ReverseTraffic is not feasible. This is because halting writes on the target is necessary for ReverseTraffic, which conflicts with the ongoing writes from previously migrated tenants and concurrent migrations for other tenants.
  • Only vtctldclient is supported

Related Issue(s)

#15403

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

Copy link
Contributor

vitess-bot bot commented Mar 17, 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 Mar 17, 2024
@github-actions github-actions bot added this to the v20.0.0 milestone Mar 17, 2024
@rohit-nayak-ps rohit-nayak-ps changed the title Multi-tenant import support in Vitess Experimental: Multi-tenant import support in Vitess Mar 17, 2024
@rohit-nayak-ps rohit-nayak-ps removed 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 Mar 17, 2024
Copy link

codecov bot commented Mar 17, 2024

Codecov Report

Attention: Patch coverage is 35.54084% with 292 lines in your changes are missing coverage. Please review.

Project coverage is 68.01%. Comparing base (e5eb981) to head (36027f3).
Report is 3 commits behind head on main.

Files Patch % Lines
go/vt/vtctl/workflow/server.go 43.26% 59 Missing ⚠️
go/vt/vtctl/workflow/traffic_switcher.go 10.76% 58 Missing ⚠️
go/vt/vtctl/workflow/utils.go 14.92% 57 Missing ⚠️
...cmd/vtctldclient/command/keyspace_routing_rules.go 12.90% 54 Missing ⚠️
go/vt/vtctl/grpcvtctldserver/server.go 0.00% 22 Missing ⚠️
go/vt/vtctl/workflow/materializer.go 59.37% 13 Missing ⚠️
go/vt/vtctl/grpcvtctldclient/client_gen.go 0.00% 8 Missing ⚠️
go/vt/topo/vschema.go 71.42% 6 Missing ⚠️
go/vt/vtctl/localvtctldclient/client_gen.go 0.00% 4 Missing ⚠️
go/vt/vtctl/workflow/switcher_dry_run.go 0.00% 4 Missing ⚠️
... and 4 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #15503      +/-   ##
==========================================
+ Coverage   65.78%   68.01%   +2.23%     
==========================================
  Files        1561     1562       +1     
  Lines      194838   195606     +768     
==========================================
+ Hits       128171   133051    +4880     
+ Misses      66667    62555    -4112     

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

@rohit-nayak-ps rohit-nayak-ps force-pushed the rohit/multi-tenant-migrations branch 2 times, most recently from 434d5c0 to 508273d Compare March 19, 2024 19:30
@rohit-nayak-ps rohit-nayak-ps removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says labels Mar 19, 2024
@rohit-nayak-ps rohit-nayak-ps marked this pull request as ready for review March 21, 2024 15:19
Copy link
Contributor

@mattlord mattlord 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! Nice work on this. I see that there's a few FIXMEs left so I'll come back to this and review the new code. At this point I only had minor comments and notes.

go/cmd/vtctldclient/command/keyspace_routing_rules.go Outdated Show resolved Hide resolved
go/cmd/vtctldclient/command/keyspace_routing_rules.go Outdated Show resolved Hide resolved
test/config.json Outdated Show resolved Hide resolved
go/vt/vtctl/grpcvtctldserver/server.go Outdated Show resolved Hide resolved
go/vt/topotools/routing_rules.go Outdated Show resolved Hide resolved
go/test/endtoend/vreplication/multi_tenant_test.go Outdated Show resolved Hide resolved
go/test/endtoend/vreplication/multi_tenant_test.go Outdated Show resolved Hide resolved
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
…outing rules on complete

Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
…which is not reproduced locally

Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
…g e2e tests

Signed-off-by: Rohit Nayak <rohit@planetscale.com>
@rohit-nayak-ps
Copy link
Contributor Author

Looks good! Nice work on this. I see that there's a few FIXMEs left so I'll come back to this and review the new code. At this point I only had minor comments and notes.

No more FIXMEs remaining. @mattlord please review when you get a chance...

… type for tenant id column

Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Copy link
Contributor

@mattlord mattlord left a comment

Choose a reason for hiding this comment

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

Nice work on this, @rohit-nayak-ps ! Very clean. I only had a few very minor comments that I'll leave to your discretion. Thanks!

test/config.json Outdated Show resolved Hide resolved
}

func init() {
ApplyKeyspaceRoutingRules.Flags().StringVarP(&applyKeyspaceRoutingRulesOptions.Rules, "rules", "r", "", "Keyspace routing rules, specified as a string")
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we mean a valid JSON document as a string or something else? This is where examples in the command definition can be helpful for the user. For example, see:

Example: `vtctldclient --server localhost:15999 materialize --workflow product_sales --target-keyspace commerce create --source-keyspace commerce --table-settings '[{"target_table": "sales_by_sku", "create_ddl": "create table sales_by_sku (sku varbinary(128) not null primary key, orders bigint, revenue bigint)", "source_expression": "select sku, count(*) as orders, sum(price) as revenue from corder group by sku"}]' --cells zone1 --cells zone2 --tablet-types replica`,
Long: `Materialize is a lower level VReplication command that allows for generalized materialization
of tables. The target tables can be copies, aggregations, or views. The target tables are kept
in sync in near-realtime. The primary flag used to define the materializations (you can have
multiple per workflow) is table-settings which is a JSON array where each value must contain
two key/value pairs. The first required key is 'target_table' and it is the name of the table
in the target-keyspace to store the results in. The second required key is 'source_expression'
and its value is the select query to run against the source table. An optional key/value pair
can also be specified for 'create_ddl' which provides the DDL to create the target table if it
does not exist -- you can alternatively specify a value of 'copy' if the target table schema
should be copied as-is from the source keyspace. Here's an example value for table-settings:
[
{
"target_table": "customer_one_email",
"source_expression": "select email from customer where customer_id = 1"
},
{
"target_table": "states",
"source_expression": "select * from states",
"create_ddl": "copy"
},
{
"target_table": "sales_by_sku",
"source_expression": "select sku, count(*) as orders, sum(price) as revenue from corder group by sku",
"create_ddl": "create table sales_by_sku (sku varbinary(128) not null primary key, orders bigint, revenue bigint)"
}
]
`,

go/vt/topotools/routing_rules.go Outdated Show resolved Hide resolved
go/vt/vtctl/workflow/server.go Show resolved Hide resolved
go/vt/vtctl/workflow/server.go Outdated Show resolved Hide resolved
go/vt/vtctl/workflow/server.go Outdated Show resolved Hide resolved
go/vt/vtctl/workflow/server.go Outdated Show resolved Hide resolved
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
@rohit-nayak-ps rohit-nayak-ps merged commit 25cc5e1 into vitessio:main Apr 1, 2024
106 checks passed
@rohit-nayak-ps rohit-nayak-ps deleted the rohit/multi-tenant-migrations branch April 1, 2024 22:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants