vitess
vitess copied to clipboard
Multi-tenant migrations: add topo locking while updating keyspace routing rules
Description
While updating routing rules (table routing rules, shard routing rules or keyspace routing rules), in certain paths we initialize or update them without holding any specific locks. Only SwitchTraffic holds locks (at the keyspace level).
Usually this is fine, since vreplication workflows that impact routing rules (MoveTables/Reshard) are expected to be used mostly for imports or resharding operations, which are infrequent. The user is (implicitly) expected to serialize these operations.
However for multi-tenant migrations with thousands of tenants we could have several concurrent per-tenant migrations in flight. This PR adds locking, ensuring that operations that update the global keyspace routing rules are serialized.
Keyspace Routing Rules are updated in the following cases:
- On
Create
when initial routing rules are setup - On
SwitchTraffic
for replica/rdonly - On
SwitchTraffic
for primary - On
Complete
when routing rules are dropped - The
ApplyKeyspaceRoutingRules
command
Additionally we also drop keyspace routing rules on a Cancel
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
Deployment Notes
Review Checklist
Hello reviewers! :wave: 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 asrequired
. - [ ] 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 andawk
-able.
Codecov Report
Attention: Patch coverage is 59.88701%
with 71 lines
in your changes are missing coverage. Please review.
Project coverage is 68.43%. Comparing base (
f118ba2
) to head (c468c8d
). Report is 89 commits behind head on main.
Additional details and impacted files
@@ Coverage Diff @@
## main #15807 +/- ##
==========================================
+ Coverage 68.40% 68.43% +0.02%
==========================================
Files 1556 1562 +6
Lines 195121 197053 +1932
==========================================
+ Hits 133479 134854 +1375
- Misses 61642 62199 +557
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
I know this is still draft, but I'm wondering whether it affects Cancel
at all?
I know this is still draft, but I'm wondering whether it affects
Cancel
at all?
Yes, just updated description as well.
https://github.com/vitessio/vitess/pull/15807/files#diff-327b08a43697d540e06dfd4e4c4d0cb617dc66f9ad3d74c451b926405c9faae5R2701
After going through the topo locking code for etcd I understood why we needed to create the KeyspaceRoutingRules
key in the topo before we can lock. etcd itself allows a key to be locked without it existing. However Vitess has added a check and requires that the key being locked is a "directory", meaning it has child keys. Otherwise it results in a faliure.
Hence the need for createTopoDirIfNeeded()
(https://github.com/vitessio/vitess/pull/15807/files#diff-0c87b5f04f1774bf3c849178da4bc0eb66f3465e9edc3315961262a6713f7f19R37)
Also adding the output of watching the locking key for our reference. It is one way to confirm that we do hold the locks before performing crud on the keyspace routing rules file.
% etcdctl watch "/vitess/global/KeyspaceRoutingRules" --prefix=true
PUT
/vitess/global/KeyspaceRoutingRules/sentinel
force creation of the keyspace routing rules root key
PUT
/vitess/global/KeyspaceRoutingRules/locks/7668661805839059282
{
"Action": "lock for KeyspaceRoutingRules:: Create",
"HostName": "RSs-Laptop.local",
"UserName": "rohit",
"Time": "2024-05-05T12:06:16+02:00",
"Status": "Running"
}
PUT
/vitess/global/KeyspaceRoutingRules/KeyspaceRoutingRules
s1@rdonlys1
s1s1
s1@replicas1
DELETE
/vitess/global/KeyspaceRoutingRules/locks/7668661805839059282
PUT
/vitess/global/KeyspaceRoutingRules/locks/7668661805839059436
{
"Action": "lock for KeyspaceRoutingRules:: SwitchReads",
"HostName": "RSs-Laptop.local",
"UserName": "rohit",
"Time": "2024-05-05T12:06:17+02:00",
"Status": "Running"
}
PUT
/vitess/global/KeyspaceRoutingRules/KeyspaceRoutingRules
s1@rdonlymt
s1s1
s1@replicamt
DELETE
/vitess/global/KeyspaceRoutingRules/locks/7668661805839059436
PUT
/vitess/global/KeyspaceRoutingRules/locks/7668661805839059891
{
"Action": "lock for KeyspaceRoutingRules:: SwitchWrites",
"HostName": "RSs-Laptop.local",
"UserName": "rohit",
"Time": "2024-05-05T12:06:18+02:00",
"Status": "Running"
}
PUT
/vitess/global/KeyspaceRoutingRules/KeyspaceRoutingRules
s1@replicamt
s1@rdonlymt
s1mt
DELETE
/vitess/global/KeyspaceRoutingRules/locks/7668661805839059891
PUT
/vitess/global/KeyspaceRoutingRules/locks/7668661805839060050
{
"Action": "lock for KeyspaceRoutingRules:: Deleting s1",
"HostName": "RSs-Laptop.local",
"UserName": "rohit",
"Time": "2024-05-05T12:06:18+02:00",
"Status": "Running"
}
DELETE
/vitess/global/KeyspaceRoutingRules/KeyspaceRoutingRules
DELETE
/vitess/global/KeyspaceRoutingRules/locks/7668661805839060050
The watch output after changing the key paths to /vitess/global/routing_rules/keyspace/Rules
etcdctl watch "/vitess/global/routing_rules" --prefix=true
PUT
/vitess/global/routing_rules/sentinel
ensure creation of Routing Rules root key: routing_rules
PUT
/vitess/global/routing_rules/locks/7668661917856618578
{
"Action": "lock for RoutingRules::Create",
"HostName": "RSs-Laptop.local",
"UserName": "rohit",
"Time": "2024-05-10T13:39:04+02:00",
"Status": "Running"
}
PUT
/vitess/global/routing_rules/keyspace/Rules
s1s1
s1@replicas1
s1@rdonlys1
DELETE
/vitess/global/routing_rules/locks/7668661917856618578
PUT
/vitess/global/routing_rules/locks/7668661917856618743
{
"Action": "lock for RoutingRules::SwitchReads",
"HostName": "RSs-Laptop.local",
"UserName": "rohit",
"Time": "2024-05-10T13:39:04+02:00",
"Status": "Running"
}
PUT
/vitess/global/routing_rules/keyspace/Rules
s1s1
s1@replicamt
s1@rdonlymt
DELETE
/vitess/global/routing_rules/locks/7668661917856618743
PUT
/vitess/global/routing_rules/locks/7668661917856619181
{
"Action": "lock for RoutingRules::SwitchWrites",
"HostName": "RSs-Laptop.local",
"UserName": "rohit",
"Time": "2024-05-10T13:39:06+02:00",
"Status": "Running"
}
PUT
/vitess/global/routing_rules/keyspace/Rules
s1mt
s1@replicamt
s1@rdonlymt
DELETE
/vitess/global/routing_rules/locks/7668661917856619181
PUT
/vitess/global/routing_rules/locks/7668661917856619570
{
"Action": "lock for RoutingRules::Deleting s1",
"HostName": "RSs-Laptop.local",
"UserName": "rohit",
"Time": "2024-05-10T13:39:07+02:00",
"Status": "Running"
}
DELETE
/vitess/global/routing_rules/keyspace/Rules
DELETE
/vitess/global/routing_rules/locks/7668661917856619570
Is SwithTraffic calls not going to be serialized?
Is SwithTraffic calls not going to be serialized?
They should be, but we are trying to guard against the possibility that people might run them in parallel (perhaps by mistake), when migrating 1000's of tenants into Vitess.