Manage TiKV regions on TiDB

TiDB data is split into multiple nodes and they’re called the name as a region.

In this document, I just played around to confirm the places and behaviors for regions on TiDB.

Manage TiKV regions through PD metadata

To handle PD metadata from CLI, follow this document.

At first, a port-forward to access PD pods

kubectl port-forward -n tidb-cluster svc/basic-pd 2379:2379 &>/tmp/portforward-pd.log &

Then run following sub commands to check each. These commands are one of pd-ctl, listed in this document, and to execute them, it has to add a command like tiup ctl:v6.5.0 pd -u http://127.0.0.1:2379 store:

  • Show stores (TiKV nodes): store
  • Confirm regions: region
  • Confirm a specific region by its key: region key 7480000000000000FF5E00000000000000F8
  • Confirm the hot spots by hot (read|write)
  • Confirm an abnormal region: region check (miss-peer|extra-peer|down-peer|pending-peer)

Confirm regions on a table

To confirm regions via TiDB:

show table [table] regions;
show table [table] index [index] regions;

The examples to look around regions:

mysql> show table salaries regions;
+-----------+----------------------------------------------+----------------------------------------------+-----------+-----------------+------------------+------------+---------------+------------+----------------------+------------------+------------------------+------------------+
| REGION_ID | START_KEY                                    | END_KEY                                      | LEADER_ID | LEADER_STORE_ID | PEERS            | SCATTERING | WRITTEN_BYTES | READ_BYTES | APPROXIMATE_SIZE(MB) | APPROXIMATE_KEYS | SCHEDULING_CONSTRAINTS | SCHEDULING_STATE |
+-----------+----------------------------------------------+----------------------------------------------+-----------+-----------------+------------------+------------+---------------+------------+----------------------+------------------+------------------------+------------------+
|      5161 | t_113_                                       | t_113_r_03800000000003112104194fb40000000000 |      5164 |            5004 | 5162, 5163, 5164 |          0 |             0 |          0 |                   77 |           988924 |                        |                  |
|      5165 | t_113_r_03800000000003112104194fb40000000000 | t_113_r_0380000000000623c70419599e0000000000 |      5167 |            5003 | 5166, 5167, 5168 |          0 |            39 |          0 |                   72 |           942081 |                        |                  |
|      5149 | t_113_r_0380000000000623c70419599e0000000000 | t_115_                                       |      5151 |            5003 | 5150, 5151, 5152 |          0 |            42 |          0 |                   72 |           941966 |                        |                  |
+-----------+----------------------------------------------+----------------------------------------------+-----------+-----------------+------------------+------------+---------------+------------+----------------------+------------------+------------------------+------------------+
3 rows in set (0.04 sec)


-- Check regions for a secondary index
mysql> create index salaries_to_date on salaries(to_date);
mysql> show table salaries index salaries_to_date regions;
+-----------+--------------------------------------------------------------------+--------------------------------------------------------------------+-----------+-----------------+------------------+------------+---------------+------------+----------------------+------------------+------------------------+------------------+
| REGION_ID | START_KEY                                                          | END_KEY                                                            | LEADER_ID | LEADER_STORE_ID | PEERS            | SCATTERING | WRITTEN_BYTES | READ_BYTES | APPROXIMATE_SIZE(MB) | APPROXIMATE_KEYS | SCHEDULING_CONSTRAINTS | SCHEDULING_STATE |
+-----------+--------------------------------------------------------------------+--------------------------------------------------------------------+-----------+-----------------+------------------+------------+---------------+------------+----------------------+------------------+------------------------+------------------+
|      6001 | t_113_                                                             | t_113_i_2_041934020000000000038000000000005185041933e20000000000   |      6004 |            5004 | 6002, 6003, 6004 |          0 |            39 |          0 |                   25 |           328620 |                        |                  |
|      6021 | t_113_i_2_041934020000000000038000000000005185041933e20000000000   | t_113_i_2_04195b0c0000000000038000000000037e2e041957cc0000000000   |      6024 |            5004 | 6022, 6023, 6024 |          0 |            39 |          0 |                   52 |                0 |                        |                  |
|      6009 | t_113_i_2_04195b0c0000000000038000000000037e2e041957cc0000000000   | t_113_i_2_04195b7400000000000380000000000652cb041958340000000000   |      6010 |               1 | 6010, 6011, 6012 |          0 |           427 |          0 |                    2 |            30720 |                        |                  |
|      6017 | t_113_i_2_04195b7400000000000380000000000652cb041958340000000000   | t_113_i_2_04196c5a000000000003800000000001677d0419691a0000000000   |      6020 |            5004 | 6018, 6019, 6020 |          0 |            39 |          0 |                   52 |                0 |                        |                  |
|      6013 | t_113_i_2_04196c5a000000000003800000000001677d0419691a0000000000   | t_113_i_2_047ef1020000000000038000000000007c3e04196bd80000000000   |      6015 |            5003 | 6014, 6015, 6016 |          0 |           427 |          0 |                    2 |            30720 |                        |                  |
|      6005 | t_113_i_2_047ef1020000000000038000000000007c3e04196bd80000000000   | t_113_i_2_047ef102000000000003800000000007a11f04196a3a000000000000 |      6008 |            5004 | 6006, 6007, 6008 |          0 |           613 |          0 |                   18 |           222607 |                        |                  |
|      5161 | t_113_i_2_047ef102000000000003800000000007a11f04196a3a000000000000 | t_113_r_03800000000003112104194fb40000000000                       |      5164 |            5004 | 5162, 5163, 5164 |          0 |           517 |          0 |                   76 |           985862 |                        |                  |
+-----------+--------------------------------------------------------------------+--------------------------------------------------------------------+-----------+-----------------+------------------+------------+---------------+------------+----------------------+------------------+------------------------+------------------+
7 rows in set (0.07 sec)

--- Check region peers and if it's leader
mysql> select * from information_schema.tikv_region_peers order by region_id;
+-----------+---------+----------+------------+-----------+--------+--------------+
| REGION_ID | PEER_ID | STORE_ID | IS_LEARNER | IS_LEADER | STATUS | DOWN_SECONDS |
+-----------+---------+----------+------------+-----------+--------+--------------+
|         2 |       3 |        1 |          0 |         0 | NORMAL |         NULL |
|         2 |    5048 |     5003 |          0 |         1 | NORMAL |         NULL |
|         2 |    5092 |     5004 |          0 |         0 | NORMAL |         NULL |
|        10 |    5088 |     5004 |          0 |         0 | NORMAL |         NULL |
|        10 |      11 |        1 |          0 |         1 | NORMAL |         NULL |
|        10 |    5044 |     5003 |          0 |         0 | NORMAL |         NULL |
|      5129 |    5132 |     5004 |          0 |         0 | NORMAL |         NULL |
|      5129 |    5130 |        1 |          0 |         0 | NORMAL |         NULL |
|      5129 |    5131 |     5003 |          0 |         1 | NORMAL |         NULL |
|      5141 |    5143 |     5003 |          0 |         1 | NORMAL |         NULL |
|      5141 |    5144 |     5004 |          0 |         0 | NORMAL |         NULL |
|      5141 |    5142 |        1 |          0 |         0 | NORMAL |         NULL |
...

Split regions

There is a query to split by SPLIT TABLE table_name [INDEX index_name] BETWEEN (lower_value) AND (upper_value) REGIONS region_num

Add new node and distribute regions

Following this document, just adding nodes dynamically. Then regions are automatically distributed by PD.

kubectl patch -n tidb-cluster tc basic --type merge --patch '{"spec":{"tikv":{"replicas":5}}}'
kubectl get pods -w

Then run tiup ctl:v6.5.0 pod -u http://127.0.0.1:2379 store a few times and see the number of region and leader counts would increase on the new node.

Confirm multiple regions

Follow

tiup ctl:v6.5.0 pd  -u http://127.0.0.1:2379 config set replication.max-replicas 5

On TiDB, update tidb_replica_read to follower to enable a follower read

set @@tidb_replica_read = 'follower';

TODO: But still all replicas show that role name is Voter, and no replica shows a Follower.

tiup ctl:v6.5.0 pd  -u http://127.0.0.1:2379 region | jq . | less

2024

Back to Top ↑

2023

Follow Kubernetes the Hard way

4 minute read

This article was written by just following Kelsey Hightower’s Kubernetes Hardway document to understand Kubernetes internal architecture.

Gcp Billing Analyze

less than 1 minute read

There are a few documents to manage billing data in BigQuery Attribution of committed use discount fees and credits How to export to BigQuery Structur...

Prometheus Metrics Overview on Grafana

1 minute read

In this post, some variables defined in Grafana are used for Prometheus metrics, including $__rate_interval: This article describes the benefit of this va...

Use Google Secret Manager in a GKE cluster

3 minute read

There are an awesome article about the options to use the Google Secret Manager and their pros and cons. In this article, use Secrets Store CSI Driver by fol...

Working around MySQL lock metadata

2 minute read

There are multiple documents about innodb locks on MySQL 5.7: InnoDB locking Locks Set by Different SQL Statements in InnoDB Using InnoDB Transaction ...

Upgrade Windows 10 to Windows 11

3 minute read

I used to use Windows 11, but for some reasons, the OS stopped working and I needed to clean-install it from Windows 10 from windows recovery environment.

Back to Top ↑

2022

MySQL backup and restore

1 minute read

In this article, explain how to backup MySQL database using Percona Xtrabackup. There are two binaries, innobackupex and xtrabackup. innobackupex is a wrappe...

tmux

1 minute read

Basic configuration

Back to Top ↑

2021

MySQL Replication

1 minute read

This configuration is for the version 5.7 and it’s minimum configuration in the official document.

jq cheetsheet

less than 1 minute read

jq is used to parse JSON result, format and output on the cli.

Compare static site generator

less than 1 minute read

There are many web sites to compare static site generator, but they miss some explanations that require to me. For some people, these features are important ...

Back to Top ↑

2020

Getting Started with Kubernetes Deployment

less than 1 minute read

The deployment is many use cases and in this page, they’re not described. For the details for those use cases or the concept of deployment, see official page.

Overview about MySQL Lock

2 minute read

This document is written for MySQL 5.7, so these contents may be not correct for other versions.

MySQL Performance

2 minute read

This document is written for MySQL 5.7, so these contents may be not correct for other versions. In this page, performance_schema is mainly discussed.

Git hooks

less than 1 minute read

Configurations

gitHub pages

3 minute read

Getting Started See Official tutorial for detail steps.

Gitconfig

1 minute read

Configuration The detail for gitconfig is written in official page.

git cli

less than 1 minute read

Written in March 2020.

MySQL Tuner

less than 1 minute read

MySQL Tuner tool This is a tool to review a configuration for MySQL server.

kubectl cheetsheet

less than 1 minute read

Collect recent error logs If the logs are outputted by zap, error messages are aggregated by checking level = error. This log does not work very well if the ...

Introduction to GCP Cloud endpoints

less than 1 minute read

The Cloud endpoint is actually the NGINX proxy which offers the following features on GCP. Authentication and validation Logging and monitoring in GCP

HTTP/2 for Go

1 minute read

http package in golang supports HTTP/2 protocols. It’s automatically configured.

Back to Top ↑

2019

Terraform overview

1 minute read

Basic concepts There are some basic components for terraform.

Protocol Buffers for Go with Gadgets

less than 1 minute read

gogo/protobuf is the library to store some extensions from golang/protobuf in this repository. There are some useful packages that golang/protobuf does not p...

Introduction to GCP Cloud CDN

less than 1 minute read

Target upstream services Cloud CDN can have only GCP load balancer as the upstream services. And GCP load balancer can configure one of followings for backen...

Getting Started with Google closure library

less than 1 minute read

Some JavaScript library depends on Google Closure. If you need to understand the behavior of such a library, you have to know closure. The official document ...

Back to Top ↑