LoginSignup
3
1

More than 1 year has passed since last update.

[TiDB] テーブルのリージョン情報の確認

Posted at

はじめに

SHOW TABLE REGIONS文を下記それぞれのタイミングで実行し、リージョン情報を確認しました。

  • データ追加前
  • データ追加後
  • TiKVの異常終了

動作確認環境

TiDB v7.0.0の下記環境にて動作確認を行いました。

[root@tisim ~]# tiup cluster display demo-cluster
tiup is checking updates for component cluster ...
Starting component `cluster`: /root/.tiup/components/cluster/v1.12.1/tiup-cluster display demo-cluster
Cluster type:       tidb
Cluster name:       demo-cluster
Cluster version:    v7.0.0
Deploy user:        tidb
SSH type:           builtin
Dashboard URL:      http://192.168.3.171:2379/dashboard
Grafana URL:        http://192.168.3.171:3000
ID                   Role        Host           Ports                            OS/Arch       Status   Data Dir                    Deploy Dir
--                   ----        ----           -----                            -------       ------   --------                    ----------
192.168.3.171:3000   grafana     192.168.3.171  3000                             linux/x86_64  Up       -                           /tidb-deploy/grafana-3000
192.168.3.171:2379   pd          192.168.3.171  2379/2380                        linux/x86_64  Up|L|UI  /tidb-data/pd-2379          /tidb-deploy/pd-2379
192.168.3.171:9090   prometheus  192.168.3.171  9090/12020                       linux/x86_64  Up       /tidb-data/prometheus-9090  /tidb-deploy/prometheus-9090
192.168.3.171:4000   tidb        192.168.3.171  4000/10080                       linux/x86_64  Up       -                           /tidb-deploy/tidb-4000
192.168.3.171:9000   tiflash     192.168.3.171  9000/8123/3930/20170/20292/8234  linux/x86_64  Up       /tidb-data/tiflash-9000     /tidb-deploy/tiflash-9000
192.168.3.171:20160  tikv        192.168.3.171  20160/20180                      linux/x86_64  Up       /tidb-data/tikv-20160       /tidb-deploy/tikv-20160
192.168.3.171:20161  tikv        192.168.3.171  20161/20181                      linux/x86_64  Up       /tidb-data/tikv-20161       /tidb-deploy/tikv-20161
192.168.3.171:20162  tikv        192.168.3.171  20162/20182                      linux/x86_64  Up       /tidb-data/tikv-20162       /tidb-deploy/tikv-20162
Total nodes: 8

リージョン情報の確認

SHOW TABLE REGIONS文を使用して、tripsテーブルのリージョン情報を確認します。

mysql> SHOW TABLE trips 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 |
+-----------+---------------+--------------------+-----------+-----------------+------------------+------------+---------------+------------+----------------------+------------------+------------------------+------------------+
|      3005 | t_94_         | t_94_r_182668      |      3007 |               1 | 3006, 3007, 3008 |          0 |            27 |          0 |                   39 |           177442 |                        |                  |
|      3009 | t_94_r_182668 | t_94_r_595336      |      3011 |               1 | 3010, 3011, 3012 |          0 |            39 |          0 |                   37 |           231736 |                        |                  |
|        10 | t_94_r_595336 | t_281474976710654_ |       156 |               1 | 11, 156, 204     |          0 |      35241357 |  149414338 |                   37 |           231736 |                        |                  |
+-----------+---------------+--------------------+-----------+-----------------+------------------+------------+---------------+------------+----------------------+------------------+------------------------+------------------+
3 rows in set (0.00 sec)

条件を指定して特定行を絞り込む事が可能です。region_idが3009の行を対象に実行します。

mysql> SHOW TABLE trips REGIONS WHERE region_id = 3009 \G
*************************** 1. row ***************************
             REGION_ID: 3009
             START_KEY: t_94_r_182668
               END_KEY: t_94_r_595336
             LEADER_ID: 3011
       LEADER_STORE_ID: 1
                 PEERS: 3010, 3011, 3012
            SCATTERING: 0
         WRITTEN_BYTES: 0
            READ_BYTES: 0
  APPROXIMATE_SIZE(MB): 60
      APPROXIMATE_KEYS: 388612
SCHEDULING_CONSTRAINTS:
      SCHEDULING_STATE:
1 row in set (0.00 sec)

上記region_idが3009のリージョン情報から下記の事がわかります。

  • start_keyがt_94_r_182668、かつend_keyがt_94_r_595336である事から、こちらのリージョンのキー値の範囲が、182668から595336である事がわかります。また、プレフィックスのt_94は、テーブルidが94であることを示しています。
  • leader_idが3011で、peersが3010, 3011, 3012である事から、3011のリージョンがリーダ(読み書き可能)で、3010と3012のリージョンが、リーダのレプリカである事を示しています。
  • leader_store_idが1である事から、3011のリージョンは、id1のTiKVに格納されている事がわかります。
  • approximate_size(MB)の60は、こちらのリージョンにおよそ60MBのデータが格納されている事を示しています。
  • approximate_keysの388612は、こちらのリージョンにおよそ388612個のキー値が格納されている事を示しています。
  • リージョンには上限サイズ(デフォルト96MiB)が設定されており、上限を超えると二つのリージョンに分割されます。
  • リージョンのデフォルトの冗長度は3です。

上記の状態からデータをINSERTしました。データ追加後のリージョン情報を確認します。

mysql> SHOW TABLE trips 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 |
+-----------+----------------+----------------+-----------+-----------------+------------------+------------+---------------+------------+----------------------+------------------+------------------------+------------------+
|      3005 | t_94_          | t_94_r_182668  |      3007 |               1 | 3006, 3007, 3008 |          0 |             0 |          0 |                   28 |           182223 |                        |                  |
|      3013 | t_94_r_182668  | t_94_r_842765  |      3015 |               1 | 3014, 3015, 3016 |          0 |            39 |          0 |                   41 |           243353 |                        |                  |
|      3017 | t_94_r_842765  | t_94_r_1245906 |      3019 |               1 | 3018, 3019, 3020 |          0 |             0 |          0 |                   21 |           123626 |                        |                  |
|      3009 | t_94_r_1245906 | 78000000       |      3011 |               1 | 3010, 3011, 3012 |          0 |     161824266 |          0 |                   21 |           123626 |                        |                  |
+-----------+----------------+----------------+-----------+-----------------+------------------+------------+---------------+------------+----------------------+------------------+------------------------+------------------+
4 rows in set (0.01 sec)

データ追加前は、3009のリージョンが、182,668から595,336までを管理していましたが、データ追加後は、3009のリージョンが分割された結果、新たに3013と3017のリージョンが、上記範囲のキーを管理するようになった事が確認できます。

TiKV異常終了時の動作確認

TiKVを1台スケールアウトし、計4つのTiKV構成の状態から、1つのTiKVを強制停止した際の動作を確認します。

TiKVスケールアウト

Scale a TiDB Cluster Using TiUPの手順を参考に実施しました。
まずは、TiKVの構成ファイルを用意します。

[root@tisim ~]# cat scale-out.yml
tikv_servers:
 - host: 192.168.3.171
   port: 20163
   status_port: 20183
   config:
     server.labels: { host: "logic-host-4" }

筆者環境のように1ノードでTiKVクラスタを構成している場合は、既存のTiKVポートと競合しないように注意します。

事前チェックします。

[root@tisim ~]# tiup cluster check demo-cluster scale-out.yml --cluster --user root -p
tiup is checking updates for component cluster ...
Starting component `cluster`: /root/.tiup/components/cluster/v1.12.1/tiup-cluster check demo-cluster scale-out.yml --cluster --user root -p
Input SSH password:

+ Detect CPU Arch Name
  - Detecting node 192.168.3.171 Arch info ... Done

+ Detect CPU OS Name
  - Detecting node 192.168.3.171 OS info ... Done
+ Download necessary tools
  - Downloading check tools for linux/amd64 ... Done
+ Collect basic system information
+ Collect basic system information
  - Getting system info of 192.168.3.171:22 ... Done
+ Check time zone
  - Checking node 192.168.3.171 ... Done

+ Check system requirements
+ Check system requirements
+ Check system requirements
  - Checking node 192.168.3.171 ... Done
  - Checking node 192.168.3.171 ... Done
+ Cleanup check files
  - Cleanup check files on 192.168.3.171:22 ... Done
Node           Check         Result  Message
----           -----         ------  -------
192.168.3.171  thp           Pass    THP is disabled
192.168.3.171  cpu-cores     Pass    number of CPU cores / threads: 4
192.168.3.171  memory        Pass    memory size is 0MB
192.168.3.171  network       Pass    network speed of enp0s3 is 1000MB
192.168.3.171  selinux       Pass    SELinux is disabled
192.168.3.171  command       Pass    numactl: policy: default
192.168.3.171  os-version    Pass    OS is CentOS Linux 7 (Core) 7.9.2009
192.168.3.171  cpu-governor  Warn    Unable to determine current CPU frequency governor policy
192.168.3.171  disk          Warn    mount point / does not have 'noatime' option set

一部、Warnがありますが、無視してスケールアウトを実行します。

[root@tisim ~]# tiup cluster scale-out demo-cluster scale-out.yml -p
tiup is checking updates for component cluster ...
Starting component `cluster`: /root/.tiup/components/cluster/v1.12.1/tiup-cluster scale-out demo-cluster scale-out.yml -p
Input SSH password:

+ Detect CPU Arch Name
  - Detecting node 192.168.3.171 Arch info ... Done

+ Detect CPU OS Name
  - Detecting node 192.168.3.171 OS info ... Done
Please confirm your topology:
Cluster type:    tidb
Cluster name:    demo-cluster
Cluster version: v7.0.0
Role  Host           Ports        OS/Arch       Directories
----  ----           -----        -------       -----------
tikv  192.168.3.171  20163/20183  linux/x86_64  /tidb-deploy/tikv-20163,/tidb-data/tikv-20163
Attention:
    1. If the topology is not what you expected, check your yaml file.
    2. Please confirm there is no port/directory conflicts in same host.
Do you want to continue? [y/N]: (default=N)

yを入力します。

Do you want to continue? [y/N]: (default=N) y
+ [ Serial ] - SSHKeySet: privateKey=/root/.tiup/storage/cluster/clusters/demo-cluster/ssh/id_rsa, publicKey=/root/.tiup/storage/cluster/clusters/demo-cluster/ssh/id_rsa.pub
+ [Parallel] - UserSSH: user=tidb, host=192.168.3.171
+ [Parallel] - UserSSH: user=tidb, host=192.168.3.171
+ [Parallel] - UserSSH: user=tidb, host=192.168.3.171
+ [Parallel] - UserSSH: user=tidb, host=192.168.3.171
+ [Parallel] - UserSSH: user=tidb, host=192.168.3.171
+ [Parallel] - UserSSH: user=tidb, host=192.168.3.171
+ [Parallel] - UserSSH: user=tidb, host=192.168.3.171
+ [Parallel] - UserSSH: user=tidb, host=192.168.3.171
+ Download TiDB components
  - Download tikv:v7.0.0 (linux/amd64) ... Done
+ Initialize target host environments
+ Deploy TiDB instance
  - Deploy instance tikv -> 192.168.3.171:20163 ... Done
+ Copy certificate to remote host
+ Generate scale-out config
  - Generate scale-out config tikv -> 192.168.3.171:20163 ... Done
+ Init monitor config
Enabling component tikv
        Enabling instance 192.168.3.171:20163
        Enable instance 192.168.3.171:20163 success
Enabling component node_exporter
        Enabling instance 192.168.3.171
        Enable 192.168.3.171 success
Enabling component blackbox_exporter
        Enabling instance 192.168.3.171
        Enable 192.168.3.171 success
+ [ Serial ] - Save meta
+ [ Serial ] - Start new instances
Starting component tikv
        Starting instance 192.168.3.171:20163
        Start instance 192.168.3.171:20163 success
Starting component node_exporter
        Starting instance 192.168.3.171
        Start 192.168.3.171 success
Starting component blackbox_exporter
        Starting instance 192.168.3.171
        Start 192.168.3.171 success
+ Refresh components conifgs
  - Generate config pd -> 192.168.3.171:2379 ... Done
  - Generate config tikv -> 192.168.3.171:20160 ... Done
  - Generate config tikv -> 192.168.3.171:20161 ... Done
  - Generate config tikv -> 192.168.3.171:20162 ... Done
  - Generate config tikv -> 192.168.3.171:20163 ... Done
  - Generate config tidb -> 192.168.3.171:4000 ... Done
  - Generate config tiflash -> 192.168.3.171:9000 ... Done
  - Generate config prometheus -> 192.168.3.171:9090 ... Done
  - Generate config grafana -> 192.168.3.171:3000 ... Done
+ Reload prometheus and grafana
  - Reload prometheus -> 192.168.3.171:9090 ... Done
  - Reload grafana -> 192.168.3.171:3000 ... Done
+ [ Serial ] - UpdateTopology: cluster=demo-cluster
Scaled cluster `demo-cluster` out successfully

スケールアウト後のトポロジを確認します。想定通りtikv-20163が追加されている事が確認できました。

[root@tisim ~]# tiup cluster display demo-cluster
tiup is checking updates for component cluster ...
Starting component `cluster`: /root/.tiup/components/cluster/v1.12.1/tiup-cluster display demo-cluster
Cluster type:       tidb
Cluster name:       demo-cluster
Cluster version:    v7.0.0
Deploy user:        tidb
SSH type:           builtin
Dashboard URL:      http://192.168.3.171:2379/dashboard
Grafana URL:        http://192.168.3.171:3000
ID                   Role        Host           Ports                            OS/Arch       Status   Data Dir                    Deploy Dir
--                   ----        ----           -----                            -------       ------   --------                    ----------
192.168.3.171:3000   grafana     192.168.3.171  3000                             linux/x86_64  Up       -                           /tidb-deploy/grafana-3000
192.168.3.171:2379   pd          192.168.3.171  2379/2380                        linux/x86_64  Up|L|UI  /tidb-data/pd-2379          /tidb-deploy/pd-2379
192.168.3.171:9090   prometheus  192.168.3.171  9090/12020                       linux/x86_64  Up       /tidb-data/prometheus-9090  /tidb-deploy/prometheus-9090
192.168.3.171:4000   tidb        192.168.3.171  4000/10080                       linux/x86_64  Up       -                           /tidb-deploy/tidb-4000
192.168.3.171:9000   tiflash     192.168.3.171  9000/8123/3930/20170/20292/8234  linux/x86_64  Up       /tidb-data/tiflash-9000     /tidb-deploy/tiflash-9000
192.168.3.171:20160  tikv        192.168.3.171  20160/20180                      linux/x86_64  Up       /tidb-data/tikv-20160       /tidb-deploy/tikv-20160
192.168.3.171:20161  tikv        192.168.3.171  20161/20181                      linux/x86_64  Up       /tidb-data/tikv-20161       /tidb-deploy/tikv-20161
192.168.3.171:20162  tikv        192.168.3.171  20162/20182                      linux/x86_64  Up       /tidb-data/tikv-20162       /tidb-deploy/tikv-20162
192.168.3.171:20163  tikv        192.168.3.171  20163/20183                      linux/x86_64  Up       /tidb-data/tikv-20163       /tidb-deploy/tikv-20163
Total nodes: 9

TiKV強制停止

tripsテーブルのリージョン情報を確認します。

mysql> SHOW TABLE trips 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 |
+-----------+----------------+----------------+-----------+-----------------+------------------+------------+---------------+------------+----------------------+------------------+------------------------+------------------+
|      3005 | 72000001       | t_94_r_182668  |      3008 |               2 | 3006, 3008, 3029 |          0 |           502 |     108038 |                   32 |           252517 |                        |                  |
|      3013 | t_94_r_182668  | t_94_r_842765  |      3015 |               1 | 3014, 3015, 3016 |          0 |             0 |          0 |                   99 |           641578 |                        |                  |
|      3017 | t_94_r_842765  | t_94_r_1245906 |      3019 |               1 | 3018, 3019, 3023 |          0 |           155 |          0 |                   65 |           423135 |                        |                  |
|      3009 | t_94_r_1245906 | 78000000       |      3011 |               1 | 3011, 3012, 3025 |          0 |           140 |          0 |                  108 |           530975 |                        |                  |
+-----------+----------------+----------------+-----------+-----------------+------------------+------------+---------------+------------+----------------------+------------------+------------------------+------------------+
4 rows in set (0.01 sec)

leader_store_idが1のTikVを強制停止の対象にしたいと思います。leader_store_idが1のTiKVを特定するために、information_schema.tikv_store_statusを確認します。

mysql> SELECT store_id, address FROM information_schema.tikv_store_status;
+----------+---------------------+
| store_id | address             |
+----------+---------------------+
|        1 | 192.168.3.171:20160 |
|        2 | 192.168.3.171:20162 |
|        3 | 192.168.3.171:20161 |
|      114 | 192.168.3.171:3930  |
|     3021 | 192.168.3.171:20163 |
+----------+---------------------+
5 rows in set (0.00 sec)

上記より、store_idが1のTiKVが192.168.3.171:20160である事がわかりましたので、プロセスをkillします。

[root@tisim ~]# ps -ef |grep tikv-20160|grep -v grep
tidb      1121     1  2 14:44 ?        00:04:06 bin/tikv-server --addr 0.0.0.0:20160 --advertise-addr 192.168.3.171:20160 --status-addr 0.0.0.0:20180 --advertise-status-addr 192.168.3.171:20180 --pd 192.168.3.171:2379 --data-dir /tidb-data/tikv-20160 --config conf/tikv.toml --log-file /tidb-deploy/tikv-20160/log/tikv.log
[root@tisim ~]# kill -9 1121

ステータスを確認します。tikv-20160のステータスがDiscconectedになりました。

[root@tisim ~]# tiup cluster display demo-cluster
(省略)
ID                   Role        Host           Ports                            OS/Arch       Status        Data Dir                    Deploy Dir
--                   ----        ----           -----                            -------       ------        --------                    ----------
192.168.3.171:20160  tikv        192.168.3.171  20160/20180                      linux/x86_64  Disconnected  /tidb-data/tikv-20160       /tidb-deploy/tikv-20160
192.168.3.171:20161  tikv        192.168.3.171  20161/20181                      linux/x86_64  Up            /tidb-data/tikv-20161       /tidb-deploy/tikv-20161
192.168.3.171:20162  tikv        192.168.3.171  20162/20182                      linux/x86_64  Up            /tidb-data/tikv-20162       /tidb-deploy/tikv-20162
192.168.3.171:20163  tikv        192.168.3.171  20163/20183                      linux/x86_64  Up            /tidb-data/tikv-20163       /tidb-deploy/tikv-20163

リージョン情報を再度確認すると、leader_store_idが、1以外に変更されている事と、レプリカだったリージョンがリーダに昇格している事が、leader_idの値から確認できます。

mysql> SHOW TABLE trips 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 |
+-----------+----------------+----------------+-----------+-----------------+------------------+------------+---------------+------------+----------------------+------------------+------------------------+------------------+
|      3005 | 72000001       | t_94_r_182668  |      3008 |               2 | 3006, 3008, 3029 |          0 |           635 |     109725 |                   32 |           252517 |                        |                  |
|      3013 | t_94_r_182668  | t_94_r_842765  |      3014 |               3 | 3014, 3015, 3016 |          0 |            39 |          0 |                  102 |           665224 |                        |                  |
|      3017 | t_94_r_842765  | t_94_r_1245906 |      3023 |            3021 | 3018, 3019, 3023 |          0 |            39 |          0 |                   62 |           403141 |                        |                  |
|      3009 | t_94_r_1245906 | 78000000       |      3012 |               2 | 3011, 3012, 3025 |          0 |             0 |     558135 |                   79 |           505124 |                        |                  |
+-----------+----------------+----------------+-----------+-----------------+------------------+------------+---------------+------------+----------------------+------------------+------------------------+------------------+
4 rows in set (0.01 sec)

ちなみに、先ほどkillしたtikv-20160は、自動復旧していました。/var/log/messagesの抜粋です。

May 13 17:58:41 tisim systemd: tikv-20160.service: main process exited, code=killed, status=9/KILL
May 13 17:58:41 tisim systemd: Unit tikv-20160.service entered failed state.
May 13 17:58:41 tisim systemd: tikv-20160.service failed.
May 13 17:58:56 tisim systemd: tikv-20160.service holdoff time over, scheduling restart.
May 13 17:58:56 tisim systemd: Stopped tikv service.
May 13 17:58:56 tisim systemd: Started tikv service.

おわりに

本記事では下記を確認しました。ご参考になれば幸いです。

  • SHOW TABLE REGIONS文を使用したリージョン情報
  • tiupを使用したTiKVのスケールアウト
  • TiKV異常終了時のリージョン再配置
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1