TiDB在建表时如何放置全新的Region?

PD是如何确定将全新的Region放置在哪几个TiKV节点上的?

默认split-table是开着的,就相当于建表就分裂成一个新的region。
至于这个region怎么放,那是看tikv的调度了。
show table xxx regions;看这个表有哪几个region
然后去pd-ctl 看region的位置。

只有第一个 Region 是“创建”出来的,其余的 Region 是“分裂”出来的,你可以看下日志,或者追踪下系统视图观察下这个现象,分裂时原 Region 在哪几个节点上,新分裂出的 Region 就在哪几个节点上,然后 PD 再根据情况去调度。

每张表的第一个region都是“创建”出来的吧,那么在创建每张表的第一个region的时候,是怎么确定各个副本放在哪几个节点上的?是由PD决定的吗?是随机放置还是遵循各种调度策略进行放置的?刚开始学习TiDB,在源码中没有找到对应入口。

其实我主要是想知道每张表的第一个region的各个副本分别放置在哪几个tikv节点上是由谁确定的,是PD吗?是随机放置还是遵循各种调度策略进行放置的?刚开始接触TiDB,没有在源码中找到对应的处理逻辑。

不是创建出来的,是分裂出来的,你通过日志观察下

很多决定因素,主要是pd掌控的。tidb不关心region怎么放。
关于region调度可以看看:placement-rule

那新建一张表时,如何确定这张表的region从已有的哪个region分裂呢?

看你们在纠结创建这个事儿,我解释下吧:
tikv把所有的数据放在了一个 从 “” 到 “” 的范围内,理解成 [-∞,+∞) 这么一个区间。

然后随着数据的插入,可能 从 A 到 C 这个范围内的数据太多了,假设B正好是中间,那就分割下。分割成了 [-∞,B) [B,+∞)这两个region。

假设又创建了一个表,表根据编码规则,假设这个表的所有前缀是 db1_tb1_,那就再把 [B,+∞)分割下,分割成 [B, db1_tb1_), [db1_tb1_,+∞)

就基本上是这样的。所有的region首尾相连,始终能连城一个 [-∞,+∞) 的区间。

3 个赞

强!解答了我的疑惑。

按照您的描述,当一个表的前缀确定后,它在区间中的位置也就确定了,同时这张表的“原始region”所在的tikv节点也就确定了对吧?

另外,每张表的前缀是怎么确定的?以及您描述的这种机制可以从哪里学习到更详细的说明呢?

创建表会为表分配一个 table_id,table 的 key 的格式是一定的,所以就能确定范围


MySQL [information_schema]> select region_id,tidb_decode_key(START_KEY),tidb_decode_key(END_KEY),min(table_id) from TIKV_REGION_STATUS group by region_id,START_KEY,END_KEY order by min(table_id);+-----------+---------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------+---------------------+
| region_id | tidb_decode_key(START_KEY)                                                                              | tidb_decode_key(END_KEY)                                                                                | min(table_id)       |
+-----------+---------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------+---------------------+
|         2 | 7800000100000000FB                                                                                      |                                                                                                         |                NULL |
|        14 | 7800000000000000FB                                                                                      | 7800000100000000FB                                                                                      |                NULL |
|       262 | 6D00000000000000F8                                                                                      | 6E00000000000000F8                                                                                      |                NULL |
|       258 |                                                                                                         | 6D00000000000000F8                                                                                      |                NULL |
|        10 | 7200000000000000FB                                                                                      | 7200000100000000FB                                                                                      |                NULL |
|         8 | 6E00000000000000F8                                                                                      | 7200000000000000FB                                                                                      |                NULL |
|      1101 | 7200000100000000FB                                                                                      | {"handle":{"c_d_id":"7","c_id":"1066","c_w_id":"1"},"table_id":110}                                     |                   4 |
|      1013 | {"handle":{"c_d_id":"1","c_id":"1479","c_w_id":"18"},"table_id":110}                                    | {"table_id":112}                                                                                        |                 110 |
|      1077 | {"handle":{"c_d_id":"10","c_id":"2710","c_w_id":"3"},"table_id":110}                                    | {"handle":{"c_d_id":"7","c_id":"2326","c_w_id":"8"},"table_id":110}                                     |                 110 |
|      1109 | {"handle":{"c_d_id":"4","c_id":"1863","c_w_id":"13"},"table_id":110}                                    | {"handle":{"c_d_id":"1","c_id":"1479","c_w_id":"18"},"table_id":110}                                    |                 110 |
|      1089 | {"handle":{"c_d_id":"7","c_id":"2326","c_w_id":"8"},"table_id":110}                                     | {"handle":{"c_d_id":"4","c_id":"1863","c_w_id":"13"},"table_id":110}                                    |                 110 |
|      1069 | {"handle":{"c_d_id":"7","c_id":"1066","c_w_id":"1"},"table_id":110}                                     | {"handle":{"c_d_id":"10","c_id":"2710","c_w_id":"3"},"table_id":110}                                    |                 110 |
|      1097 | {"table_id":112}                                                                                        | {"index_id":2,"index_vals":{"h_c_w_id":"16"},"table_id":112}                                            |                 112 |
|      1017 | {"index_id":2,"index_vals":{"h_c_w_id":"16"},"table_id":112}                                            | {"table_id":114}                                                                                        |                 112 |
|      1025 | {"table_id":114}                                                                                        | {"table_id":118}                                                                                        |                 114 |
|      1029 | {"handle":{"ol_d_id":"1","ol_number":"7","ol_o_id":"404","ol_w_id":"18"},"table_id":118}                | {"table_id":120}                                                                                        |                 118 |
|      1073 | {"handle":{"ol_d_id":"9","ol_number":"4","ol_o_id":"1820","ol_w_id":"5"},"table_id":118}                | {"handle":{"ol_d_id":"3","ol_number":"9","ol_o_id":"2730","ol_w_id":"8"},"table_id":118}                |                 118 |
|      1061 | {"table_id":118}                                                                                        | {"handle":{"ol_d_id":"5","ol_number":"3","ol_o_id":"967","ol_w_id":"3"},"table_id":118}                 |                 118 |
|      1065 | {"handle":{"ol_d_id":"5","ol_number":"3","ol_o_id":"967","ol_w_id":"3"},"table_id":118}                 | {"handle":{"ol_d_id":"9","ol_number":"4","ol_o_id":"1820","ol_w_id":"5"},"table_id":118}                |                 118 |
|      1093 | {"handle":{"ol_d_id":"2","ol_number":"10","ol_o_id":"1341","ol_w_id":"13"},"table_id":118}              | {"handle":{"ol_d_id":"6","ol_number":"7","ol_o_id":"2240","ol_w_id":"15"},"table_id":118}               |                 118 |
|      1081 | {"handle":{"ol_d_id":"3","ol_number":"9","ol_o_id":"2730","ol_w_id":"8"},"table_id":118}                | {"handle":{"ol_d_id":"8","ol_number":"9","ol_o_id":"624","ol_w_id":"10"},"table_id":118}                |                 118 |
|      1085 | {"handle":{"ol_d_id":"8","ol_number":"9","ol_o_id":"624","ol_w_id":"10"},"table_id":118}                | {"handle":{"ol_d_id":"2","ol_number":"10","ol_o_id":"1341","ol_w_id":"13"},"table_id":118}              |                 118 |
|      1105 | {"handle":{"ol_d_id":"6","ol_number":"7","ol_o_id":"2240","ol_w_id":"15"},"table_id":118}               | {"handle":{"ol_d_id":"1","ol_number":"7","ol_o_id":"404","ol_w_id":"18"},"table_id":118}                |                 118 |
|      1053 | {"handle":{"s_i_id":"49025","s_w_id":"12"},"table_id":120}                                              | {"handle":{"s_i_id":"23033","s_w_id":"15"},"table_id":120}                                              |                 120 |
|      1033 | {"handle":{"s_i_id":"42196","s_w_id":"18"},"table_id":120}                                              | {"index_id":2,"index_vals":"20, 10, 3000, 1859, 20, 10, 1859, ","table_id":124}                         |                 120 |
|      1049 | {"handle":{"s_i_id":"16535","s_w_id":"10"},"table_id":120}                                              | {"handle":{"s_i_id":"49025","s_w_id":"12"},"table_id":120}                                              |                 120 |
|      1041 | {"handle":{"s_i_id":"67971","s_w_id":"3"},"table_id":120}                                               | {"handle":{"s_i_id":"84050","s_w_id":"6"},"table_id":120}                                               |                 120 |
|      1037 | {"table_id":120}                                                                                        | {"handle":{"s_i_id":"67971","s_w_id":"3"},"table_id":120}                                               |                 120 |
|      1057 | {"handle":{"s_i_id":"23033","s_w_id":"15"},"table_id":120}                                              | {"handle":{"s_i_id":"42196","s_w_id":"18"},"table_id":120}                                              |                 120 |
|      1045 | {"handle":{"s_i_id":"84050","s_w_id":"6"},"table_id":120}                                               | {"handle":{"s_i_id":"16535","s_w_id":"10"},"table_id":120}                                              |                 120 |
|      2121 | {"index_id":2,"index_vals":"20, 10, 3000, 1859, 20, 10, 1859, ","table_id":124}                         | {"index_id":2,"index_vals":{"o_c_id":"1","o_d_id":"1","o_id":"2941","o_w_id":"1"},"table_id":1292}      |                 308 |
|      2141 | {"index_id":2,"index_vals":{"o_c_id":"1","o_d_id":"1","o_id":"2941","o_w_id":"1"},"table_id":1292}      | {"index_id":2,"index_vals":{"o_c_id":"3000","o_d_id":"10","o_id":"1859","o_w_id":"20"},"table_id":1292} |                1292 |
|      2133 | {"index_id":2,"index_vals":{"o_c_id":"3000","o_d_id":"10","o_id":"1859","o_w_id":"20"},"table_id":1292} | {"table_id":1298}                                                                                       |                1292 |
|      2169 | {"table_id":1298}                                                                                       | {"handle":{"o_d_id":"1","o_id":"1989","o_w_id":"10"},"table_id":1298}                                   |                1298 |
|      2165 | {"handle":{"o_d_id":"1","o_id":"1989","o_w_id":"10"},"table_id":1298}                                   | {"table_id":281474976710654}                                                                            |                1298 |
|       266 | {"table_id":281474976710654}                                                                            | {"table_id":281474976710655}                                                                            |     281474976710654 |
|        12 | {"table_id":281474976710655}                                                                            | 7800000000000000FB                                                                                      | 4611686018427387906 |
+-----------+---------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------+---------------------+
37 rows in set, 14 warnings (0.01 sec)

1 个赞

学习 PCTA/PCTP https://learn.pingcap.cn/learner/certification-center

1 个赞

对,region原始位置其实就是它分裂前的region所在的位置,分裂以后这个region可能会被调pd迁移走。如果你初始化集群时把所有的scheduler都关掉,即使整个集群有很多tikv,有很多region,那么这些region都是集中在最一开始的那个tikv上,因为都是分裂出来的,没有scheduler就不会安排迁移,就出不去。

至于学习这些,虾总发的就挺好,还有博客,也可以看看。

1 个赞

谢谢虾总!

:call_me_hand:解释的很到位,每个region都是左闭右开区间。

建表的时候,默认配置就是全新的region

学习了

1 个赞

必须严谨 :crazy_face:

专业👍🏻,学习了

专业👍🏻,学习了