vitess icon indicating copy to clipboard operation
vitess copied to clipboard

Multi-tenant migrations: add topo locking while updating keyspace routing rules

Open rohit-nayak-ps opened this issue 10 months ago • 5 comments

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

rohit-nayak-ps avatar Apr 28 '24 12:04 rohit-nayak-ps

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 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[bot] avatar Apr 28 '24 12:04 vitess-bot[bot]

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.

Files Patch % Lines
go/vt/vtctl/grpcvtctldserver/server.go 0.00% 18 Missing :warning:
go/vt/topo/topo_lock.go 79.72% 15 Missing :warning:
go/vt/topotools/routing_rules.go 59.37% 13 Missing :warning:
...cmd/vtctldclient/command/keyspace_routing_rules.go 0.00% 8 Missing :warning:
go/vt/vtctl/workflow/traffic_switcher.go 0.00% 8 Missing :warning:
go/vt/vtctl/workflow/utils.go 71.42% 4 Missing :warning:
go/vt/vtctl/workflow/server.go 0.00% 3 Missing :warning:
go/vt/topo/vschema.go 88.88% 1 Missing :warning:
go/vt/vtctl/workflow/switcher.go 0.00% 1 Missing :warning:
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.

codecov[bot] avatar Apr 28 '24 12:04 codecov[bot]

I know this is still draft, but I'm wondering whether it affects Cancel at all?

deepthi avatar May 01 '24 16:05 deepthi

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

rohit-nayak-ps avatar May 01 '24 17:05 rohit-nayak-ps

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

rohit-nayak-ps avatar May 05 '24 16:05 rohit-nayak-ps

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

rohit-nayak-ps avatar May 10 '24 11:05 rohit-nayak-ps

Is SwithTraffic calls not going to be serialized?

harshit-gangal avatar May 13 '24 15:05 harshit-gangal

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.

deepthi avatar May 14 '24 16:05 deepthi