pingcap / tidb

TiDB is an open-source, cloud-native, distributed, MySQL-Compatible database for elastic scale and real-time analytics. Try AI-powered Chat2Query free at : https://www.pingcap.com/tidb-serverless/
https://pingcap.com
Apache License 2.0
37.03k stars 5.82k forks source link

Increase flexibility of stale read placement labels #28470

Open kolbe opened 3 years ago

kolbe commented 3 years ago

Feature Request

Is your feature request related to a problem? Please describe:

It seems that DCLabelKey is hardcoded (https://github.com/pingcap/tidb/blob/7755d25aba5120dafde98fff11ab3b98ca4d192f/ddl/placement/common.go#L49) as "zone", which means that tidb-server will prefer to read from a replica with the same "zone" label if one is available.

This is not a very flexible approach, and it doesn't allow for the kind of tiered system of labels that's used for placement rules.

Describe the feature you'd like:

When using placement rules, it's possible to use [zone,rack,host] to define a three-layer topology. It should also be possible for tidb-server to understand a topology of that sort to first choose a replica on the same host, then a replica in the same rack, and then a replica in the same zone.

Describe alternatives you've considered:

Teachability, Documentation, Adoption, Migration Strategy:

hihihuhu commented 3 years ago

hi! i am wondering if i could take this. we are using aws and if the traffic is within same az as much as possible in a multi zone(region) setup, there could be benefits on both latency and cost.

the fix i am thinking is to add a new config in tidb like location-labels in pd config with default value zone, this config is ordered label list for replica selection

then pass the labels in order in places like https://github.com/pingcap/tidb/blob/87648de76e128e4a7575435443f0ee4c208aca09/distsql/request_builder.go#L54,

and then in place like https://github.com/tikv/client-go/blob/f65172694bc4364b8bb37581d0556dd1a85fc58a/internal/locate/region_cache.go#L195 to first try to match all labels, if none, then remove the last label and repeat, if it is still no match until there is no label, returns leader instead

meantime, also change https://github.com/pingcap/tidb/blob/c01f1a3c5fb783969b288636535a0e54fbbf702d/config/config.go#L212 to be the value of the first label

i am very new to tidb, please let me know if this is feasible, thanks!

kolbe commented 3 years ago

@hihihuhu sure, I think it would be great for you to try to put together a patch and submit it as a PR!