Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql: implement locality-aware DistSQL planning in multi-tenant setup
This commit implements the region-aware DistSQL physical planning in the multi-tenant setup. Previously, we would use a round-robin strategy of assigning single-range spans to all available SQL instances and now we'll make a much better decision. The algorithm is as follows: 1. find all available SQL pods and determine their locality (via the `sqlinstance.Provider`) 2. iterate over all spans and break them down into single-range spans using the range cache that the gateway SQL instance maintains 3. for each single-range span, find the KV leaseholder and determine its locality (via the `kvtenantccl.Connector`), then pick a random SQL instance in the same locality 4. if there is no SQL instance in that locality, then assign the single-range span to the gateway SQL instance. At the moment, the only locality "tier" that we look at is the "region", but in the future we can easily extend it (say, to also include the "availability zone"). The choice of randomly picking the SQL instance in the same locality in the step 3 is done so that we distribute the load evenly among multiple pods. The step 2 was already being performed for the system tenant, so this commit extracts out some helpers to reuse the code as much as possible. Additionally, it fixes an omission in the physical planning in multi-tenancy when the query has a LIMIT (`getInstanceIDForScan`). If in step 1 we couldn't determine the region of any available SQL pod, then we fallback to the old naive locality-ignorant strategy of assigning pods in round-robin fashion. Release note: None (I don't think that any of these changes are user-visible since the serverless doesn't yet support the multi-region clusters.)
- Loading branch information