## モジュール 3: DC1 の全ノードを停止してプライマリ DC の障害をシミュレートする

この演習の目的は、プライマリデータセンターの完全な利用不可をシミュレートし、その可用性をどのように回復するかを確認することです。

DC1 が完全に障害を起こした時、DC2 はフォロワーの TiKV インスタンスに最新のデータを保持しています。バックアップ用のラーナー TiKV インスタンスと共に、2つのレプリカクラスタがすぐに復元され、サービスを再開することができます。

  ![60%](https://lab-static.pingcap.com/quick-demo/demo-303-7-en.png)

災害復旧データセンターの Placement Driver を通じて新しい TiDB クラスタを開始する必要があります（データは保持されるが、新しい クラスタID を持ちます）。さらに、PD ノードをスケールアウトし（事前に予約されたスタンバイの PD ノード）、災害復旧データセンターのラーナー TiKV ノードを投票権を持つ TiKV に変換し、他の TiKV ノードと共に機能的な Raft グループを形成する必要があります。

### 手順 1. DC1 のモニターサーバーに接続します

+ [ターミナル1](ssh://az1monitor1-ssh#1) を開き、データセンター1のmonitor1に接続します。

### 手順 2. ワークロードを開始します

+ DC1 および DC2 の両方で `sysbench` を起動し、アプリケーションの実行として機能させます。このタスクは 60 分間動作します。

+ DC1 で `sysbench` ワークロードを開始します。

  ```bash
  sysbench --config-file=sysbench_config oltp_read_write --tables=6 --mysql-ignore-errors=all run
  ```

  ```output
  sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)

  Running the test with following options:
  Number of threads: 1
  Report intermediate results every 1 second(s)
  Initializing random number generator from current time

  Initializing worker threads...

  Threads started!

  [ 1s ] thds: 1 tps: 61.90 qps: 1240.90 (r/w/o: 868.53/247.58/124.79) lat (ms,95%): 18.28 err/s: 0.00 reconn/s: 0.00
  [ 2s ] thds: 1 tps: 58.02 qps: 1176.46 (r/w/o: 824.32/236.09/116.04) lat (ms,95%): 18.28 err/s: 0.00 reconn/s: 0.00
  ...
  ```

+ [ターミナル2](ssh://az2monitor1-ssh#2) を開き、データセンター2のmonitor1に接続します。DC2で `sysbench` ワークロードを開始します。

+ DC2 の `sysbench` 設定をセットアップします。

  ```bash
  cat << EOF > sysbench_config
  mysql-host=${HOST_AZ2DB1_PRIVATE_IP}
  mysql-port=4000
  mysql-user=root
  mysql-password=
  mysql-db=sbtest
  time=3600
  threads=6
  report-interval=1
  EOF
  cat sysbench_config
  ```

  ```output
  mysql-host=10.90.2.125
  mysql-port=4000
  mysql-user=root
  mysql-password=
  mysql-db=sbtest
  time=3600
  threads=16
  report-interval=1
  ```

+ DC2 で `sysbench` ワークロードを開始します。

  ```bash
  sysbench --config-file=sysbench_config oltp_read_write --tables=6 --mysql-ignore-errors=all run
  ```

  ```output
  Threads started!

  [ 1s ] thds: 6 tps: 316.47 qps: 6401.24 (r/w/o: 4486.46/1158.05/756.73) lat (ms,95%): 23.10 err/s: 0.00 reconn/s: 0.00
  [ 2s ] thds: 6 tps: 330.09 qps: 6611.89 (r/w/o: 4631.33/1171.34/809.23) lat (ms,95%): 22.69 err/s: 0.00 reconn/s: 0.00
  ...
  ```

### 手順 3. レプリケーション状態が同期していることを確認します

+ [ターミナル3](ssh://az2monitor1-ssh#3) を開き、データセンター2のmonitor1に接続します。データのレプリケーション状態が `sync` モードであることを確認します。

+ データのレプリケーション状態を確認します。

  ```bash
  curl ${HOST_AZ1PD1_PRIVATE_IP}:2379/pd/api/v1/replication_mode/status
  ```

  ```output
  {
   "mode": "dr-auto-sync",
   "dr-auto-sync": {
     "label_key": "dc",
     "state": "sync",
     "state_id": 1006,
     "acid_consistent": true
   }
  }
  ```

### 手順 4. プライマリデータセンターの完全な障害をシミュレーションします

+ TiDB クラスタのノード上のすべての TiDB プロセスを停止することで、プライマリデータセンターの完全な障害をシミュレーションします。注意: アプリケーションは生きたままにして、TiDB クラスタのアクセス可能性を観察します。

+ DC1 にある各 TiDB、TiKV、および PD ノードのプロセスを終了し、データディレクトリを削除します。プロンプトが表示された場合は `yes` で応答します。

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_AZ1DB1_PRIVATE_IP} "sudo systemctl stop tidb-4000.service; sudo systemctl stop node_exporter-9100.service; sudo systemctl stop blackbox_exporter-9115.service; sudo rm -rf /tidb-deploy/; sudo rm -rf /tidb-data;"
  ```

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_AZ1KV1_PRIVATE_IP} "sudo systemctl stop tikv-20160.service; sudo systemctl stop node_exporter-9100.service; sudo systemctl stop blackbox_exporter-9115.service; sudo rm -rf /tidb-deploy/; sudo rm -rf /tidb-data;"
  ```

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_AZ1KV2_PRIVATE_IP} "sudo systemctl stop tikv-20160.service; sudo systemctl stop node_exporter-9100.service; sudo systemctl stop blackbox_exporter-9115.service; sudo rm -rf /tidb-deploy/; sudo rm -rf /tidb-data;"
  ```

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_AZ1PD1_PRIVATE_IP} "sudo systemctl stop pd-2379.service; sudo systemctl stop node_exporter-9100.service; sudo systemctl stop blackbox_exporter-9115.service; sudo rm -rf /tidb-deploy/; sudo rm -rf /tidb-data;"
  ```

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_AZ1PD2_PRIVATE_IP} "sudo systemctl stop pd-2379.service; sudo systemctl stop node_exporter-9100.service; sudo systemctl stop blackbox_exporter-9115.service; sudo rm -rf /tidb-deploy/; sudo rm -rf /tidb-data;"
  ```

+ 監視のプロセスを終了し、データディレクトリを削除します。

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_AZ1MONITOR1_PRIVATE_IP} "sudo systemctl stop prometheus-10090.service; sudo systemctl stop grafana-3000.service; sudo systemctl stop node_exporter-9100.service; sudo systemctl stop blackbox_exporter-9115.service; sudo rm -rf /tidb-deploy/; sudo rm -rf /tidb-data;"
  ```

### 手順 5. アプリケーションと TiDB のステータスを観察します

+ TiDB クラスタ内のノードのステータスを観察します。すべての PD サーバーのステータスは `Down`、すべての TiKV サーバーは `N/A`、そして TiDB サーバーの1つは `Down` です。DC2 に配置されている TiDB サーバーのみが稼働しています。

  ```bash
  tiup cluster display tidb-demo
  ```

  ```output
  A new version of cluster is available: v1.15.1 -> v1.16.1

  To update this component:   tiup update cluster
  To update all components:   tiup update --all

  Cluster type:       tidb
  Cluster name:       tidb-demo
  Cluster version:    v${LAB:TIDB_VERSION}
  Deploy user:        ec2-user
  SSH type:           builtin
  Grafana URL:        http://10.90.1.182:3000,http://10.90.2.252:3000
  ID                 Role        Host         Ports        OS/Arch       Status  Data Dir                    Deploy Dir
  --                 ----        ----         -----        -------       ------  --------                    ----------
  10.90.1.182:3000   grafana     10.90.1.182  3000         linux/x86_64  Down    -                           /tidb-deploy/grafana-3000
  10.90.2.252:3000   grafana     10.90.2.252  3000         linux/x86_64  Up      -                           /tidb-deploy/grafana-3000
  10.90.1.213:2379   pd          10.90.1.213  2379/2380    linux/x86_64  Down    /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.1.90:2379    pd          10.90.1.90   2379/2380    linux/x86_64  Down    /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.2.65:2379    pd          10.90.2.65   2379/2380    linux/x86_64  Down    /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.1.182:10090  prometheus  10.90.1.182  10090/12020  linux/x86_64  Down    /tidb-data/prometheus_data  /tidb-deploy/prometheus-10090
  10.90.2.252:10090  prometheus  10.90.2.252  10090/12020  linux/x86_64  Up      /tidb-data/prometheus_data  /tidb-deploy/prometheus-10090
  10.90.1.196:4000   tidb        10.90.1.196  4000/10080   linux/x86_64  Down    -                           /tidb-deploy/tidb-4000
  10.90.2.226:4000   tidb        10.90.2.226  4000/10080   linux/x86_64  Up      -                           /tidb-deploy/tidb-4000
  10.90.1.228:20160  tikv        10.90.1.228  20160/20180  linux/x86_64  N/A     /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  10.90.1.91:20160   tikv        10.90.1.91   20160/20180  linux/x86_64  N/A     /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  10.90.2.202:20160  tikv        10.90.2.202  20160/20180  linux/x86_64  N/A     /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  10.90.2.206:20160  tikv        10.90.2.206  20160/20180  linux/x86_64  N/A     /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  Total nodes: 13
  ```

+ TiDB の同期モードを観察します。DC1 にある PD リーダーにアクセスできないため、DC2 にあるフォロアー PD にアクセスする必要があります。しかし、リーダーへのリダイレクトができないため、操作はタイムアウトします。

  ```bash
  curl ${HOST_AZ2PD1_PRIVATE_IP}:2379/pd/api/v1/replication_mode/status
  ```

  ```output
  [PD:apiutil:ErrRedirectNoLeader]redirect finds no leader
  ```

+ [ターミナル1](ssh://az1monitor1-ssh#1)に切り替えます。TiDB クラスタが利用不可になったため、QPS が 0 になったことが確認できます。

  ```output
  ...
  [ 57s ] thds: 6 tps: 193.99 qps: 3834.86 (r/w/o: 2679.90/665.98/488.98) lat (ms,95%): 28.67 err/s: 0.00 reconn/s: 0.00
  [ 58s ] thds: 6 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,95%): 0.00 err/s: 0.00 reconn/s: 0.00
  [ 59s ] thds: 6 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,95%): 0.00 err/s: 0.00 reconn/s: 0.00
  ...
  ```

+ [ターミナル2](ssh://az2monitor1-ssh#2)に切り替えます。TiDB クラスタが利用不可になったため、QPS が 0 になったことが確認できます。

  ```output
  ...
  [ 34s ] thds: 6 tps: 116.00 qps: 2313.99 (r/w/o: 1624.99/385.00/304.00) lat (ms,95%): 28.67 err/s: 0.00 reconn/s: 0.00
  [ 35s ] thds: 6 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,95%): 0.00 err/s: 0.00 reconn/s: 0.00
  [ 36s ] thds: 6 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,95%): 0.00 err/s: 0.00 reconn/s: 0.00
  ...
  ```

### 手順 6. 生存しているノードのみを使用して TiDB クラスタを再形成します

+ [ターミナル3](ssh://az2monitor1-ssh#3)に切り替えて、用意されたメタファイルを使用して `tiup` のメタファイルを上書きします。このメタファイルにはデータセンター2（AZ2）のノードのみが含まれています。

+ 上書きする前にメタファイルをバックアップします。

  ```bash
  cp /home/ec2-user/.tiup/storage/cluster/clusters/tidb-demo/meta.yaml /home/ec2-user/.tiup/storage/cluster/clusters/tidb-demo/meta.yaml.backup
  ```

+ yamlファイル `majority-DC2.yaml` をチェックします。

  ```bash
  cat majority-DC2.yaml
  ```

  ```output
  user: ec2-user
  tidb_version: v${LAB:TIDB_VERSION}
  topology:
   global:
     user: "ec2-user"
     ssh_port: 22
     deploy_dir: "/tidb-deploy"
     data_dir: "/tidb-data"
     arch: "amd64"
   monitored:
     node_exporter_port: 9100
     blackbox_exporter_port: 9115
     deploy_dir: "/tidb-deploy/monitored-9100"
     data_dir: "/tidb-data/monitor_9115"
     log_dir: "/tidb-deploy/monitor-9100/log"
   server_configs:
     pd:
       replication.location-labels: ["dc","logic","rack","host"]
   tidb_servers:
     - host: 10.90.2.210
       port: 4000
       status_port: 10080
       deploy_dir: "/tidb-deploy/tidb-4000"
       log_dir: "/tidb-deploy/tidb-4000/log"
   tikv_servers:
     - host: 10.90.2.111
       port: 20160
       status_port: 20180
       deploy_dir: "/tidb-deploy/tikv-20160"
       data_dir: "/tidb-data/tikv_data"
       config:
         server.labels: { dc: "dc2",logic: "logic3",rack: "r1",host: "10.90.2.111" }
     - host: 10.90.2.91
       port: 20160
       status_port: 20180
       deploy_dir: "/tidb-deploy/tikv-20160"
       data_dir: "/tidb-data/tikv_data"
       config:
         server.labels: { dc: "dc2",logic: "logic4",rack: "r1",host: "10.90.2.91" }
   pd_servers:
     - host: 10.90.2.83
       client_port: 2379
       peer_port: 2380
       deploy_dir: "/tidb-deploy/pd-2379"
       data_dir: "/tidb-data/pd_data"
       advertise_client_addr: "http://54.212.25.205:2379"
   monitoring_servers:
     - host: 10.90.2.107
       port: 10090
       deploy_dir: "/tidb-deploy/prometheus-10090"
       data_dir: "/tidb-data/prometheus_data"
   grafana_servers:
     - host: 10.90.2.107
       port: 3000
       deploy_dir: "/tidb-deploy/grafana-3000"
  ```

+ プロビジョンファイルを使用してメタファイルを置き換えます。

  ```bash
  cp -rf majority-DC2.yaml /home/ec2-user/.tiup/storage/cluster/clusters/tidb-demo/meta.yaml
  ```

### 手順 7. TiDB クラスタを停止する

+ TiDBクラスタのステータスを確認します。

  ```bash
  tiup cluster display tidb-demo
  ```

  ```output
  ...
  ID                 Role        Host         Ports        OS/Arch  Status  Data Dir                    Deploy Dir
  --                 ----        ----         -----        -------  ------  --------                    ----------
  10.90.2.14:3000    grafana     10.90.2.14   3000         /        Up      -                           /tidb-deploy/grafana-3000
  10.90.2.53:2379    pd          10.90.2.53   2379/2380    /        Down    /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.2.14:10090   prometheus  10.90.2.14   10090        /        Up      /tidb-data/prometheus_data  /tidb-deploy/prometheus-10090
  10.90.2.97:4000    tidb        10.90.2.97   4000/10080   /        Up      -                           /tidb-deploy/tidb-4000
  10.90.2.170:20160  tikv        10.90.2.170  20160/20180  /        N/A     /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  10.90.2.78:20160   tikv        10.90.2.78   20160/20180  /        N/A     /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  Total nodes: 6
  ```

+ TiDBクラスタのノードを停止します。お待ちください。この操作はハンズオン設定では数分かかります（本番環境用ではありません）。

  ```bash
  tiup cluster stop tidb-demo -y
  ```

  ```output
  ...
  Stopping component grafana
   Stopping instance 10.90.2.14
   Stop grafana 10.90.2.14:3000 success
  Stopping component prometheus
   Stopping instance 10.90.2.14
   Stop prometheus 10.90.2.14:10090 success
  Stopping component tidb
   Stopping instance 10.90.2.97
   Stop tidb 10.90.2.97:4000 success
  Stopping component tikv
   Stopping instance 10.90.2.170
   Stopping instance 10.90.2.78
   Stop tikv 10.90.2.170:20160 success
   Stop tikv 10.90.2.78:20160 success
  Stopping component pd
   Stopping instance 10.90.2.53
   Stop pd 10.90.2.53:2379 success
  Stopping component node_exporter
   Stopping instance 10.90.2.170
   Stopping instance 10.90.2.14
   Stopping instance 10.90.2.53
   Stopping instance 10.90.2.97
   Stopping instance 10.90.2.78
   Stop 10.90.2.170 success
   Stop 10.90.2.78 success
   Stop 10.90.2.53 success
   Stop 10.90.2.97 success
   Stop 10.90.2.14 success
  Stopping component blackbox_exporter
   Stopping instance 10.90.2.170
   Stopping instance 10.90.2.14
   Stopping instance 10.90.2.97
   Stopping instance 10.90.2.78
   Stopping instance 10.90.2.53
   Stop 10.90.2.78 success
   Stop 10.90.2.170 success
   Stop 10.90.2.97 success
   Stop 10.90.2.53 success
   Stop 10.90.2.14 success
  Stopped cluster `tidb-demo` successfully
  ```

+ TiDBノードのステータスを確認し、すべてのpd, tidb, tikv, およびprometheusがダウンしていることを確認します。

  ```bash
  tiup cluster display tidb-demo
  ```

  ```output
  ...
  ID                 Role        Host         Ports        OS/Arch       Status  Data Dir                    Deploy Dir
  --                 ----        ----         -----        -------       ------  --------                    ----------
  10.90.2.82:3000    grafana     10.90.2.82   3000         linux/x86_64  Down    -                           /tidb-deploy/grafana-3000
  10.90.2.125:2379   pd          10.90.2.125  2379/2380    linux/x86_64  Down    /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.2.82:10090   prometheus  10.90.2.82   10090/12020  linux/x86_64  Down    /tidb-data/prometheus_data  /tidb-deploy/prometheus-10090
  10.90.2.173:4000   tidb        10.90.2.173  4000/10080   linux/x86_64  Down    -                           /tidb-deploy/tidb-4000
  10.90.2.129:20160  tikv        10.90.2.129  20160/20180  linux/x86_64  N/A     /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  10.90.2.216:20160  tikv        10.90.2.216  20160/20180  linux/x86_64  N/A     /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  Total nodes: 6
  ```

### 手順 8. Placement Driver を復元する

+ pd-recover ツールをインストールします。

  ```bash
  tiup install pd-recover:v${LAB:TIDB_VERSION}
  ```

  ```output
  download https://tiup-mirrors.pingcap.com/pd-recover-v${LAB:TIDB_VERSION}-darwin-arm64.tar.gz 13.88 MiB / 13.88 MiB 100.00% 6.99 MiB/s
  ```

+ 配置ドライバーサーバーにSSHでアクセスし、DC2に配置されている新しい配置ドライバークラスタを強制的に開始します。

+ PD にログインします。プロンプトが表示されたら、`yes`と入力します。

  ```bash
  ssh -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem ec2-user@${HOST_AZ2PD1_PRIVATE_IP}
  ```

+ DC1が失われる前のTiDBクラスタの同期モードであるpdのステータスを確認し、現在のTiDBのデータ一貫性を示します。

  ```bash
  cat /tidb-data/pd_data/DR_STATE
  ```

  ```output
  {"state":"sync","state_id":1947}
  ```

+ pdの初期スクリプトを修正し、`--force-new-cluster`パラメータを追加します。このパラメータはpdの新しいetcdクラスタを開始するために使用されます。

  ```bash
  sed -i '/pd-server/ s#pd-server#pd-server --force-new-cluster#' /tidb-deploy/pd-2379/scripts/run_pd.sh
  ```

+ モニターOSからログアウトします。

  ```bash
  exit
  ```

  ```output
  logout
  Connection to 10.90.2.26 closed.
  ```

+ PD ノードを開始してクラスタを始動します。

  ```bash
  tiup cluster start tidb-demo -N ${HOST_AZ2PD1_PRIVATE_IP}:2379 -y
  ```

  ```output
  ...
  Starting cluster tidb-demo...
  + [ Serial ] - SSHKeySet: privateKey=/home/ec2-user/.tiup/storage/cluster/clusters/tidb-demo/ssh/id_rsa, publicKey=/home/ec2-user/.tiup/storage/cluster/clusters/tidb-demo/ssh/id_rsa.pub
  + [Parallel] - UserSSH: user=ec2-user, host=10.90.2.38
  + [Parallel] - UserSSH: user=ec2-user, host=10.90.2.26
  + [Parallel] - UserSSH: user=ec2-user, host=10.90.2.38
  + [Parallel] - UserSSH: user=ec2-user, host=10.90.2.254
  + [Parallel] - UserSSH: user=ec2-user, host=10.90.2.182
  + [Parallel] - UserSSH: user=ec2-user, host=10.90.2.49
  + [ Serial ] - StartCluster
  Starting component pd
   Starting instance 10.90.2.26:2379
   Start instance 10.90.2.26:2379 success
  Starting component node_exporter
   Starting instance 10.90.2.26
   Start 10.90.2.26 success
  Starting component blackbox_exporter
   Starting instance 10.90.2.26
   Start 10.90.2.26 success
  + [ Serial ] - UpdateTopology: cluster=tidb-demo
  Started cluster `tidb-demo` successfully
  ```

+ PD が起動していることを確認します。

  ```bash
  tiup cluster display tidb-demo
  ```

  ```output
  A new version of cluster is available: v1.15.1 -> v1.16.1

     To update this component:   tiup update cluster
     To update all components:   tiup update --all

  Cluster type:       tidb
  Cluster name:       tidb-demo
  Cluster version:    v${LAB:TIDB_VERSION}
  Deploy user:        ec2-user
  SSH type:           builtin
  Dashboard URL:      http://35.94.179.113:2379/dashboard
  Grafana URL:        http://10.90.2.82:3000
  ID                 Role        Host         Ports        OS/Arch  Status        Data Dir                    Deploy Dir
  --                 ----        ----         -----        -------  ------        --------                    ----------
  10.90.2.107:3000   grafana     10.90.2.107  3000         /        Down          -                           /tidb-deploy/grafana-3000
  10.90.2.83:2379    pd          10.90.2.83   2379/2380    /        Up|L          /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.2.107:10090  prometheus  10.90.2.107  10090        /        Down          /tidb-data/prometheus_data  /tidb-deploy/prometheus-10090
  10.90.2.210:4000   tidb        10.90.2.210  4000/10080   /        Down          -                           /tidb-deploy/tidb-4000
  10.90.2.111:20160  tikv        10.90.2.111  20160/20180  /        Disconnected  /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  10.90.2.91:20160   tikv        10.90.2.91   20160/20180  /        Disconnected  /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  Total nodes: 6
  ```

+ PD のメタデータを修正します。このステップの後、ストレージの`alloc_id`は自動的に`100000000`の安全な値まで増加します。

  ```bash
  tiup pd-recover:v${LAB:TIDB_VERSION} --from-old-member --endpoints=http://${HOST_AZ2PD1_PRIVATE_IP}:2379
  ```

  ```output
  download https://tiup-mirrors.pingcap.com/pd-recover-v${LAB:TIDB_VERSION}-linux-amd64.tar.gz 15.87 MiB / 15.87 MiB 100.00% 3.78 MiB/s
  Checking updates for component pd-recover... Starting component pd-recover: /home/ec2-user/.tiup/components/pd-recover/v{LAB:TIDB_VERSION}/pd-recover --from-old-member --endpoints=http://10.90.2.83:2379
  recover success! please restart the PD cluster
  ```

+ PD インスタンスを再起動します。

  ```bash
  tiup cluster restart tidb-demo -N ${HOST_AZ2PD1_PRIVATE_IP}:2379 -y
  ```

  ```output
  ...
  + [ Serial ] - RestartCluster
  Stopping component pd
    Stopping instance 10.90.2.83
    Stop pd 10.90.2.83:2379 success
  Stopping component node_exporter
    Stopping instance 10.90.2.83
    Stop 10.90.2.83 success
  Stopping component blackbox_exporter
    Stopping instance 10.90.2.83
    Stop 10.90.2.83 success
  Starting component pd
    Starting instance 10.90.2.83:2379
    Start instance 10.90.2.83:2379 success
  Starting component node_exporter
    Starting instance 10.90.2.83
    Start 10.90.2.83 success
  Starting component blackbox_exporter
    Starting instance 10.90.2.83
    Start 10.90.2.83 success
  Restarted cluster `tidb-demo` successfully
  ```

### 手順 9. 新しい Placement Driver サーバーを1つスケールアウトする

+ yamlファイルを確認します。

  ```bash
  cat scale-out-pd.yaml
  ```

  ```output
  pd_servers:
   - host: 10.90.2.102
     client_port: 2379
     peer_port: 2380
     deploy_dir: "/tidb-deploy/pd-2379"
     data_dir: "/tidb-data/pd_data"
     advertise_client_addr: "http://35.161.129.28:2379"
  ```

+ PD ノードをスケールアウトします。

  ```bash
  tiup cluster scale-out tidb-demo scale-out-pd.yaml --user ec2-user -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem -y
  ```

  ```output
  ...
  Starting component pd
   Starting instance 10.90.2.102:2379
   Start instance 10.90.2.102:2379 success
  Starting component node_exporter
   Starting instance 10.90.2.102
   Start 10.90.2.102 success
  Starting component blackbox_exporter
   Starting instance 10.90.2.102
   Start 10.90.2.102 success
  + Refresh components conifgs
   - Generate config pd -> 10.90.2.26:2379 ... Done
   - Generate config pd -> 10.90.2.102:2379 ... Done
   - Generate config tikv -> 10.90.2.254:20160 ... Done
   - Generate config tikv -> 10.90.2.182:20160 ... Done
   - Generate config tidb -> 10.90.2.49:4000 ... Done
   - Generate config prometheus -> 10.90.2.38:10090 ... Done
   - Generate config grafana -> 10.90.2.38:3000 ... Done
  + Reload prometheus and grafana
   - Reload prometheus -> 10.90.2.38:10090 ... Done
   - Reload grafana -> 10.90.2.38:3000 ... Done
  + [ Serial ] - UpdateTopology: cluster=tidb-demo
  Scaled cluster `tidb-demo` out successfully
  ```

+ DC2の2つのpdノードがすべて起動していることを確認します。

  ```bash
  tiup cluster display tidb-demo
  ```

  ```output
  A new version of cluster is available: v1.15.1 -> v1.16.1

     To update this component:   tiup update cluster
     To update all components:   tiup update --all

  Cluster type:       tidb
  Cluster name:       tidb-demo
  Cluster version:    v${LAB:TIDB_VERSION}
  Deploy user:        ec2-user
  SSH type:           builtin
  Dashboard URL:      http://35.94.179.113:2379/dashboard
  Grafana URL:        http://10.90.2.82:3000
  ID                 Role        Host         Ports        OS/Arch       Status        Data Dir                    Deploy Dir
  --                 ----        ----         -----        -------       ------        --------                    ----------
  10.90.2.107:3000   grafana     10.90.2.107  3000         /             Up            -                           /tidb-deploy/grafana-3000
  10.90.2.83:2379    pd          10.90.2.83   2379/2380    /             Up|L          /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.2.96:2379    pd          10.90.2.96   2379/2380    linux/x86_64  Up            /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.2.107:10090  prometheus  10.90.2.107  10090        /             Down          /tidb-data/prometheus_data  /tidb-deploy/prometheus-10090
  10.90.2.210:4000   tidb        10.90.2.210  4000/10080   /             Down          -                           /tidb-deploy/tidb-4000
  10.90.2.111:20160  tikv        10.90.2.111  20160/20180  /             Disconnected  /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  10.90.2.91:20160   tikv        10.90.2.91   20160/20180  /             Disconnected  /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  Total nodes: 7
  ```

### 手順 10. クラスタを `majority` モードに設定する

+ クラスタが依然として`dr-auto-sync`レプリケーションモードにあることを確認します。

  ```bash
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ2PD1_PRIVATE_IP}:2379 config show replication-mode
  ```

  ```output
  The component `ctl` version v${LAB:TIDB_VERSION} is not installed; downloading from repository.
  download https://tiup-mirrors.pingcap.com/ctl-v${LAB:TIDB_VERSION}-linux-amd64.tar.gz 408.65 MiB / 408.65 MiB 100.00% 97.75 MiB/s
  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u 10.90.2.83:2379 config show replication-mode
  {
   "replication-mode": "dr-auto-sync",
   "dr-auto-sync": {
     "label-key": "dc",
     "primary": "dc1",
     "dr": "dc2",
     "primary-replicas": 2,
     "dr-replicas": 1,
     "wait-store-timeout": "1m0s",
     "wait-recover-timeout": "0s",
     "pause-region-split": "false"
   }
  }
  ```

+ `majority`ルールファイルを確認します。

  ```bash
  cat majority-rules.json
  ```

  ```output
  [
   {
     "group_id": "pd",
     "group_index": 0,
     "group_override": false,
     "rules": [
       {
         "group_id": "pd",
         "id": "voters",
         "start_key": "",
         "end_key": "",
         "role": "voter",
         "count": 3,
         "location_labels": ["dc","logic","rack", "host"]
       }
     ]
   }
  ]
  ```

+ `majority`配置ルール構成をロードします。

  ```bash
  tiup ctl:v${LAB:TIDB_VERSION} pd config placement-rules rule-bundle -u ${HOST_AZ2PD1_PRIVATE_IP}:2379 save --in="/home/ec2-user/majority-rules.json"
  ```

  ```output
  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd config placement-rules rule-bundle -u http://10.90.2.162:2379 save --in=/home/ec2-user/majority-rules.json
  "Update rules and groups successfully."
  ```

+ 配置ルールがロードされていることを確認します。

  ```bash
  tiup ctl:v${LAB:TIDB_VERSION} pd config placement-rules show -u ${HOST_AZ2PD1_PRIVATE_IP}:2379
  ```

  ```output
  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd config placement-rules show -u 10.90.2.125:2379
  [
   {
     "group_id": "pd",
     "id": "voters",
     "start_key": "",
     "end_key": "",
     "role": "voter",
     "is_witness": false,
     "count": 3,
     "location_labels": [
       "dc",
       "logic",
       "rack",
       "host"
     ],
     "create_timestamp": 1734071450
   }
  ]
  ```

+ `majority`設定を有効にします。

  ```bash
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ2PD1_PRIVATE_IP}:2379 config set replication-mode majority
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ2PD1_PRIVATE_IP}:2379 config set max-replicas 3
  ```

  ```output
  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u 10.90.2.125:2379 config set replication-mode majority
  Success! The replication mode config is updated."

  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u 10.90.2.125:2379 config set max-replicas 3
  Success!
  ```

+ クラスタの`majority`レプリケーションモードが有効であることを確認します。

  ```bash
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ2PD1_PRIVATE_IP}:2379 config show replication-mode
  ```

  ```output
  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u 10.90.2.125:2379 config show replication-mode
  {
   "replication-mode": "majority",
   "dr-auto-sync": {
     "label-key": "dc",
     "primary": "dc1",
     "dr": "dc2",
     "primary-replicas": 2,
     "dr-replicas": 1,
     "wait-store-timeout": "1m0s",
     "wait-recover-timeout": "0s",
     "pause-region-split": "false"
   }
  }
  ```

### 手順 11. 利用不可の TiKV ノードを削除します

+ TiKVノードを開始します。

  ```bash
  tiup cluster start tidb-demo -R tikv -y
  ```

  ```output
  ...
  Starting component tikv
   Starting instance 10.90.2.129:20160
   Starting instance 10.90.2.216:20160
   Start instance 10.90.2.216:20160 success
   Start instance 10.90.2.129:20160 success
  Starting component node_exporter
   Starting instance 10.90.2.129
   Starting instance 10.90.2.216
   Start 10.90.2.216 success
   Start 10.90.2.129 success
  Starting component blackbox_exporter
   Starting instance 10.90.2.129
   Starting instance 10.90.2.216
   Start 10.90.2.216 success
   Start 10.90.2.129 success
  + [ Serial ] - UpdateTopology: cluster=tidb-demo
  Started cluster `tidb-demo` successfully
  ...
  ```

+ 2つのTiKVノードが起動するのを待ちます。

  ```bash
  tiup cluster display tidb-demo
  ```

  ```output
  ...
  ID                 Role        Host         Ports        OS/Arch       Status  Data Dir                    Deploy Dir
  --                 ----        ----         -----        -------       ------  --------                    ----------
  10.90.2.82:3000    grafana     10.90.2.82   3000         linux/x86_64  Up      -                           /tidb-deploy/grafana-3000
  10.90.2.125:2379   pd          10.90.2.125  2379/2380    linux/x86_64  Up|L    /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.2.135:2379   pd          10.90.2.135  2379/2380    linux/x86_64  Up      /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.2.82:10090   prometheus  10.90.2.82   10090/12020  linux/x86_64  Down    /tidb-data/prometheus_data  /tidb-deploy/prometheus-10090
  10.90.2.173:4000   tidb        10.90.2.173  4000/10080   linux/x86_64  Down    -                           /tidb-deploy/tidb-4000
  10.90.2.129:20160  tikv        10.90.2.129  20160/20180  linux/x86_64  Up      /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  10.90.2.216:20160  tikv        10.90.2.216  20160/20180  linux/x86_64  Up      /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  Total nodes: 7
  ```

+ 利用不可の TiKV ノードを削除します。

  ```bash
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ2PD1_PRIVATE_IP}:2379 unsafe remove-failed-stores --auto-detect
  ```

  ```output
  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u 10.90.2.110:2379 unsafe remove-failed-stores --auto-detect
  Success! Request has been accepted.
  ```

+ 削除操作の状態を表示します。しばらく後、次の説明を確認することでプロセスが完了したことを確認できます: "info": "Unsafe recovery Finished" コマンド結果の最後に表示されます。

  ```bash
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ2PD1_PRIVATE_IP}:2379 unsafe remove-failed-stores show
  ```

  ```output
  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u 10.90.2.125:2379 unsafe remove-failed-stores show
  [
   {
     "info": "Unsafe recovery enters collect report stage",
     "time": "2024-12-13 07:01:51.714",
     "details": [
       "auto detect mode with no specified Failed stores"
     ]
   },
   {
     "info": "Unsafe recovery enters force leader stage",
     "time": "2024-12-13 07:01:54.000",
     "actions": {
       "store 1": [
         "force leader on regions: 7, 9, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 101, 104, 107, 110, 113, 116, 119, 122, 125, 128, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 209, 213, 217, 221, 225, 229, 233, 237, 241, 245, 249, 253, 1978, 2013, 2018, 1993, 2043, 2048, 1983, 2023, 2028, 1988, 2033, 2038, 1998, 2063, 2068, 2008, 2053, 2058, 35, 31, 27, 23, 19, 17, 11, 13, 3"
       ],
       "store 16": [
         "force leader on regions: "
       ]
     }
   },
   {
     "info": "Unsafe recovery enters demote Failed voter stage",
     "time": "2024-12-13 07:02:04.586",
     "actions": {
       "store 1": [
         "region 7 demotes peers { id:75 store_id:2 }, { id:170 store_id:15 }",
         ...
         "region 3 demotes peers { id:77 store_id:2 }, { id:208 store_id:15 }"
       ]
     }
   },
   {
     "info": "Unsafe recovery Finished",
     "time": "2024-12-13 07:02:14.002",
     "details": [
       "affected table ids: 74, 94, 110, 24, 58, 78, 92, 107, 281474976710649, 56, 82, 84, 106, 108, 109, 281474976710652, 281474976710654, 52, 111, 50, 66, 281474976710653, 48, 98, 54, 38, 42, 46, 62, 72, 76, 6, 281474976710651, 86, 28, 30, 40, 68, 96, 20, 60, 4, 18, 34, 80, 12, 10, 32, 70, 8, 64, 22, 36, 88, 90, 102, 14, 26, 44, 281474976710650, 16",
       "no newly created empty regions"
     ]
   }
  ]
  ```

### 手順 12. 回復した TiDB クラスタを起動します

+ TiDBとPrometheusインスタンスを開始します。

  ```bash
  tiup cluster start tidb-demo -R tidb,prometheus -y
  ```

  ```output
  Starting component tidb
   Starting instance 10.90.2.49:4000
   Start instance 10.90.2.49:4000 success
  Starting component prometheus
   Starting instance 10.90.2.38:10090
   Start instance 10.90.2.38:10090 success
  Starting component node_exporter
   Starting instance 10.90.2.38
   Starting instance 10.90.2.49
   Start 10.90.2.49 success
   Start 10.90.2.38 success
  Starting component blackbox_exporter
   Starting instance 10.90.2.38
   Starting instance 10.90.2.49
   Start 10.90.2.49 success
   Start 10.90.2.38 success
  + [ Serial ] - UpdateTopology: cluster=tidb-demo
  Started cluster `tidb-demo` successfully
  ```

+ TiDBクラスタの状態を確認します。DC2内のすべてのノードの状態が「Up」であることを確認します。

  ```bash
  tiup cluster display tidb-demo
  ```

  ```output
  A new version of cluster is available: v1.15.1 -> v1.16.1

     To update this component:   tiup update cluster
     To update all components:   tiup update --all

  Cluster type:       tidb
  Cluster name:       tidb-demo
  Cluster version:    v${LAB:TIDB_VERSION}
  Deploy user:        ec2-user
  SSH type:           builtin
  Dashboard URL:      http://35.94.179.113:2379/dashboard
  Grafana URL:        http://10.90.2.82:3000
  ID                 Role        Host         Ports        OS/Arch       Status  Data Dir                    Deploy Dir
  --                 ----        ----         -----        -------       ------  --------                    ----------
  10.90.2.82:3000    grafana     10.90.2.82   3000         linux/x86_64  Up      -                           /tidb-deploy/grafana-3000
  10.90.2.125:2379   pd          10.90.2.125  2379/2380    linux/x86_64  Up|L    /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.2.135:2379   pd          10.90.2.135  2379/2380    linux/x86_64  Up      /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.2.82:10090   prometheus  10.90.2.82   10090/12020  linux/x86_64  Up      /tidb-data/prometheus_data  /tidb-deploy/prometheus-10090
  10.90.2.173:4000   tidb        10.90.2.173  4000/10080   linux/x86_64  Up      -                           /tidb-deploy/tidb-4000
  10.90.2.129:20160  tikv        10.90.2.129  20160/20180  linux/x86_64  Up      /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  10.90.2.216:20160  tikv        10.90.2.216  20160/20180  linux/x86_64  Up      /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  Total nodes: 7
  ```

+ TiDBクラスタのレプリケーションモードを確認します。

  ```bash
  curl ${HOST_AZ2PD1_PRIVATE_IP}:2379/pd/api/v1/replication_mode/status
  ```

  ```output
  {
   "mode": "majority",
   "dr-auto-sync": {
     "label_key": "",
     "state": "",
     "acid_consistent": false
   }
  }
  ```

### 手順 13. `sysbench` ワークロードが回復していることを確認します

+ [ターミナル1](ssh://az1monitor1-ssh#1)に切り替え、sysbenchワークロードのQPSが依然として0であることを確認します。これは、DC1にあるTiDBサーバーが利用不可であるためです。

  ```output
  ...
  [ 1697s ] thds: 6 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,95%): 0.00 err/s: 0.00 reconn/s: 0.00
  [ 1698s ] thds: 6 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,95%): 0.00 err/s: 0.00 reconn/s: 0.00
  [ 1699s ] thds: 6 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,95%): 0.00 err/s: 0.00 reconn/s: 0.00
  ```

+ [ターミナル2](ssh://az2monitor1-ssh#2)に切り替え、sysbenchワークロードのQPSが正常に戻ったことを確認します。

  ```output
  ...
  [ 1618s ] thds: 6 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,95%): 0.00 err/s: 0.00 reconn/s: 0.00
  [ 1619s ] thds: 6 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,95%): 0.00 err/s: 0.00 reconn/s: 6.00
  [ 1620s ] thds: 6 tps: 321.06 qps: 6505.17 (r/w/o: 4564.82/1103.20/837.15) lat (ms,95%): 23.10 err/s: 0.00 reconn/s: 0.00
  [ 1621s ] thds: 6 tps: 313.95 qps: 6261.93 (r/w/o: 4378.25/1098.81/784.87) lat (ms,95%): 26.68 err/s: 0.00 reconn/s: 0.00
  ```

### 手順 14. 再度 DR Auto-Sync を有効にします

+ DC1が回復した後、TiDBクラスタをDR Auto-syncデプロイメントに戻す必要があります。

+ [ターミナル3](ssh://az2monitor1-ssh#3)に切り替え、失われたノードを再びTiDBクラスタに参加させます。yamlファイルを確認してください。

  ```bash
  cat scale-out-DC1.yaml
  ```

  ```output
  tidb_servers:
   - host: 10.90.1.58
     port: 4000
     status_port: 10080
     deploy_dir: "/tidb-deploy/tidb-4000"
     log_dir: "/tidb-deploy/tidb-4000/log"
  tikv_servers:
   - host: 10.90.1.219
     port: 20160
     status_port: 20180
     deploy_dir: "/tidb-deploy/tikv-20160"
     data_dir: "/tidb-data/tikv_data"
     config:
       server.labels: { dc: "dc1",logic: "logic1",rack: "r1",host: "10.90.1.219" }
   - host: 10.90.1.71
     port: 20160
     status_port: 20180
     deploy_dir: "/tidb-deploy/tikv-20160"
     data_dir: "/tidb-data/tikv_data"
     config:
       server.labels: { dc: "dc1",logic: "logic2",rack: "r1",host: "10.90.1.71" }
  pd_servers:
   - host: 10.90.1.95
     client_port: 2379
     peer_port: 2380
     deploy_dir: "/tidb-deploy/pd-2379"
     data_dir: "/tidb-data/pd_data"
     advertise_client_addr: "http://34.214.79.226:2379"
   - host: 10.90.1.55
     client_port: 2379
     peer_port: 2380
     deploy_dir: "/tidb-deploy/pd-2379"
     data_dir: "/tidb-data/pd_data"
     advertise_client_addr: "http://54.202.186.157:2379"
  monitoring_servers:
   - host: 10.90.1.221
     port: 10090
     deploy_dir: "/tidb-deploy/prometheus-10090"
     data_dir: "/tidb-data/prometheus_data"
  grafana_servers:
   - host: 10.90.1.221
     port: 3000
     deploy_dir: "/tidb-deploy/grafana-3000"
  ```

+ DC1のノードをスケールアウトします。

  ```bash
  tiup cluster scale-out tidb-demo scale-out-DC1.yaml --user ec2-user -i ~/.ssh/pe-class-key-${LAB:REGION_NAME}.pem -y
  ```

  ```output
  ...
  Starting component pd
   Starting instance 10.90.1.95:2379
   Start instance 10.90.1.95:2379 success
   Starting instance 10.90.1.55:2379
   Start instance 10.90.1.55:2379 success
  Starting component tikv
   Starting instance 10.90.1.71:20160
   Starting instance 10.90.1.219:20160
   Start instance 10.90.1.71:20160 success
   Start instance 10.90.1.219:20160 success
  Starting component tidb
   Starting instance 10.90.1.58:4000
   Start instance 10.90.1.58:4000 success
  Starting component prometheus
   Starting instance 10.90.1.221:10090
   Start instance 10.90.1.221:10090 success
  Starting component grafana
   Starting instance 10.90.1.221:3000
   Start instance 10.90.1.221:3000 success
  Starting component node_exporter
   Starting instance 10.90.1.55
   Starting instance 10.90.1.58
   Starting instance 10.90.1.221
   Starting instance 10.90.1.95
   Starting instance 10.90.1.219
   Starting instance 10.90.1.71
   Start 10.90.1.71 success
   Start 10.90.1.95 success
   Start 10.90.1.219 success
   Start 10.90.1.55 success
   Start 10.90.1.221 success
   Start 10.90.1.58 success
  Starting component blackbox_exporter
   Starting instance 10.90.1.55
   Starting instance 10.90.1.58
   Starting instance 10.90.1.221
   Starting instance 10.90.1.95
   Starting instance 10.90.1.219
   Starting instance 10.90.1.71
   Start 10.90.1.219 success
   Start 10.90.1.95 success
   Start 10.90.1.71 success
   Start 10.90.1.55 success
   Start 10.90.1.221 success
   Start 10.90.1.58 success
  + Refresh components conifgs
   - Generate config pd -> 10.90.2.83:2379 ... Done
   - Generate config pd -> 10.90.2.96:2379 ... Done
   - Generate config pd -> 10.90.1.95:2379 ... Done
   - Generate config pd -> 10.90.1.55:2379 ... Done
   - Generate config tikv -> 10.90.2.111:20160 ... Done
   - Generate config tikv -> 10.90.2.91:20160 ... Done
   - Generate config tikv -> 10.90.1.219:20160 ... Done
   - Generate config tikv -> 10.90.1.71:20160 ... Done
   - Generate config tidb -> 10.90.2.210:4000 ... Done
   - Generate config tidb -> 10.90.1.58:4000 ... Done
   - Generate config prometheus -> 10.90.2.107:10090 ... Done
   - Generate config prometheus -> 10.90.1.221:10090 ... Done
   - Generate config grafana -> 10.90.2.107:3000 ... Done
   - Generate config grafana -> 10.90.1.221:3000 ... Done
  + Reload prometheus and grafana
   - Reload prometheus -> 10.90.2.107:10090 ... Done
   - Reload grafana -> 10.90.2.107:3000 ... Done
  + [ Serial ] - UpdateTopology: cluster=tidb-demo
  Scaled cluster `tidb-demo` out successfully
  ```

+ TiDBクラスタの状態を確認し、すべてのノードが回復していることを確認します。

  ```bash
  tiup cluster display tidb-demo
  ```

  ```output
  ID                 Role        Host         Ports        OS/Arch       Status  Data Dir                    Deploy Dir
  --                 ----        ----         -----        -------       ------  --------                    ----------
  10.90.1.221:3000   grafana     10.90.1.221  3000         linux/x86_64  Up      -                           /tidb-deploy/grafana-3000
  10.90.2.107:3000   grafana     10.90.2.107  3000         /             Up      -                           /tidb-deploy/grafana-3000
  10.90.1.55:2379    pd          10.90.1.55   2379/2380    linux/x86_64  Up      /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.1.95:2379    pd          10.90.1.95   2379/2380    linux/x86_64  Up      /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.2.83:2379    pd          10.90.2.83   2379/2380    /             Up|L    /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.2.96:2379    pd          10.90.2.96   2379/2380    linux/x86_64  Up      /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.1.221:10090  prometheus  10.90.1.221  10090/12020  linux/x86_64  Up      /tidb-data/prometheus_data  /tidb-deploy/prometheus-10090
  10.90.2.107:10090  prometheus  10.90.2.107  10090        /             Up      /tidb-data/prometheus_data  /tidb-deploy/prometheus-10090
  10.90.1.58:4000    tidb        10.90.1.58   4000/10080   linux/x86_64  Up      -                           /tidb-deploy/tidb-4000
  10.90.2.210:4000   tidb        10.90.2.210  4000/10080   /             Up      -                           /tidb-deploy/tidb-4000
  10.90.1.219:20160  tikv        10.90.1.219  20160/20180  linux/x86_64  Up      /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  10.90.1.71:20160   tikv        10.90.1.71   20160/20180  linux/x86_64  Up      /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  10.90.2.111:20160  tikv        10.90.2.111  20160/20180  /             Up      /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  10.90.2.91:20160   tikv        10.90.2.91   20160/20180  /             Up      /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  Total nodes: 14
  ```

+ DR Auto-Sync配置ルール設定をロードします。

  ```bash
  tiup ctl:v${LAB:TIDB_VERSION} pd config placement-rules rule-bundle -u ${HOST_AZ2PD1_PRIVATE_IP}:2379 save --in="/home/ec2-user/dr-anto-sync-rules.json"
  ```

  ```output
  The component `ctl` version v${LAB:TIDB_VERSION} is not installed; downloading from repository.
  download https://tiup-mirrors.pingcap.com/ctl-v${LAB:TIDB_VERSION}-linux-amd64.tar.gz 408.65 MiB / 408.65 MiB 100.00% 103.24 MiB/s
  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd config placement-rules rule-bundle -u http://10.90.2.162:2379 save --in=/home/ec2-user/dr-anto-sync-rules.json
  "Update rules and groups successfully."
  ```

+ `jq`ツールをインストールします。

  ```bash
  sudo yum install jq -y
  ```

  ```output
  ...
  Installed:
   jq.x86_64 0:1.5-1.amzn2.0. 2

  Dependency Installed:
   oniguruma.x86_64 0:5.9.6-1.amzn2.0. 7

  Complete!
  ```

+ Learner PeersのないTiKVリージョンを確認します。リージョンにLearner Peersがいないことを確認することで、各役割間のデータ同期が確保され、データが一貫してクラスター内で複製されていることを確認します。これは高可用性とフェイルオーバー準備を維持するために重要です。

  ```bash
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ2PD1_PRIVATE_IP}:2379 region --jq='.regions[] | select(.peers | any(.role_name=="Learner") | not) |  {id: .id, peers: [.peers]}'
  ```

  ```output
  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://34.215.73.186:2379 region --jq=.regions[] | select(.peers | any(.role_name=="Learner") | not) |  {id: .id, peers: [.peers]}
  ```

+ DR Auto-Syncの設定を有効にします。

  ```bash
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ2PD1_PRIVATE_IP}:2379 config set replication-mode dr-auto-sync
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ2PD1_PRIVATE_IP}:2379 config set replication-mode dr-auto-sync label-key dc
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ2PD1_PRIVATE_IP}:2379 config set replication-mode dr-auto-sync primary dc1
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ2PD1_PRIVATE_IP}:2379 config set replication-mode dr-auto-sync dr dc2
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ2PD1_PRIVATE_IP}:2379 config set replication-mode dr-auto-sync primary-replicas 2
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ2PD1_PRIVATE_IP}:2379 config set replication-mode dr-auto-sync dr-replicas 1
  ```

  ```output
  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.162:2379 config set replication-mode dr-auto-sync
  Success! The replication mode config is updated."

  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.162:2379 config set replication-mode dr-auto-sync label-key dc
  Success! The replication mode config is updated."

  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.162:2379 config set replication-mode dr-auto-sync primary dc1
  Success! The replication mode config is updated."

  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.162:2379 config set replication-mode dr-auto-sync dr dc2
  Success! The replication mode config is updated."

  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.162:2379 config set replication-mode dr-auto-sync primary-replicas 2
  Success! The replication mode config is updated."

  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.162:2379 config set replication-mode dr-auto-sync dr-replicas 1
  Success! The replication mode config is updated."
  ```

+ 設定が有効であることを確認します。

  ```bash
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ2PD1_PRIVATE_IP}:2379 config show replication-mode
  ```

  ```output
  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u http://10.90.2.162:2379:2379 config show replication-mode
  {
   "replication-mode": "dr-auto-sync",
   "dr-auto-sync": {
     "label-key": "dc",
     "primary": "dc1",
     "dr": "dc2",
     "primary-replicas": 2,
     "dr-replicas": 1,
     "wait-store-timeout": "1m0s",
     "wait-recover-timeout": "0s",
     "pause-region-split": "false"
   }
  }
  ```

### 手順 15. レプリケーション状態が `sync` に戻っていることを確認します

+ クラスタの現在のレプリケーション状態を確認します。状態が`sync`モードになるまで、一時的に`sync_recover`モードまたは`recover_progress`の状態が表示されることがあります。

  ```bash
  curl ${HOST_AZ2PD1_PRIVATE_IP}:2379/pd/api/v1/replication_mode/status
  ```

  ```output
  {
   "mode": "dr-auto-sync",
   "dr-auto-sync": {
     "label_key": "dc",
     "state": "sync_recover",
     "state_id": 2003,
     "acid_consistent": false,
     "total_regions": 60,
     "synced_regions": 37,
     "recover_progress": 0.98333335
   }
  }
  ```

+ 最後に`sync`状態を取得します。

  ```output
  {
   "mode": "dr-auto-sync",
   "dr-auto-sync": {
     "label_key": "dc",
     "state": "sync",
     "state_id": 378,
     "acid_consistent": true
   }
  }
  ```

+ [ターミナル1](ssh://az1monitor1-ssh#1)に切り替え、sysbenchワークロードのQPSが回復したことを確認します。

  ```output
  ...
  [ 2309s ] thds: 6 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,95%): 0.00 err/s: 0.00 reconn/s: 0.00
  [ 2310s ] thds: 6 tps: 0.00 qps: 0.00 (r/w/o: 0.00/0.00/0.00) lat (ms,95%): 0.00 err/s: 0.00 reconn/s: 0.00
  [ 2311s ] thds: 6 tps: 105.01 qps: 2170.15 (r/w/o: 1530.11/416.03/224.02) lat (ms,95%): 100000.00 err/s: 0.00 reconn/s: 6.00
  [ 2312s ] thds: 6 tps: 193.97 qps: 3869.44 (r/w/o: 2705.61/763.89/399.94) lat (ms,95%): 34.33 err/s: 0.00 reconn/s: 0.00
  ```

### 手順 16. Placement Driver と TiKV のリーダーを DC1 に転送します

+ [ターミナル3](ssh://az2monitor1-ssh#3)に切り替え、DC2の2番目のPlacement Driverノードをスケールインします。

  ```bash
  tiup cluster scale-in tidb-demo -N "${HOST_AZ2PD2_PRIVATE_IP}:2379" -y
  ```

  ```output
  ...
  Scaled cluster `tidb-demo` in successfully
  ```

+ 前の手順の結果に関係なく、このコマンドを実行してPDリーダーをDC1に転送しようとします。

  ```bash
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ1PD1_PRIVATE_IP}:2379 member leader transfer "pd-${HOST_AZ1PD1_PRIVATE_IP}-2379"
  ```

  ```output
  The component `ctl` version v${LAB:TIDB_VERSION} is not installed; downloading from repository.
  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u 10.90.1.19:2379 member leader transfer pd-10.90.1.106-2379
  Success!
  ```

+ TiDBクラスタの状態を確認します。PDのリーダーがDC1に配置され、PDノードの数が3であることを確認します。

  ```bash
  tiup cluster display tidb-demo
  ```

  ```output
  ID                 Role        Host         Ports        OS/Arch       Status  Data Dir                    Deploy Dir
  --                 ----        ----         -----        -------       ------  --------                    ----------
  10.90.1.221:3000   grafana     10.90.1.221  3000         linux/x86_64  Up      -                           /tidb-deploy/grafana-3000
  10.90.2.107:3000   grafana     10.90.2.107  3000         /             Up      -                           /tidb-deploy/grafana-3000
  10.90.1.55:2379    pd          10.90.1.55   2379/2380    linux/x86_64  Up      /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.1.95:2379    pd          10.90.1.95   2379/2380    linux/x86_64  Up|L    /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.2.83:2379    pd          10.90.2.83   2379/2380    /             Up      /tidb-data/pd_data          /tidb-deploy/pd-2379
  10.90.1.221:10090  prometheus  10.90.1.221  10090/12020  linux/x86_64  Up      /tidb-data/prometheus_data  /tidb-deploy/prometheus-10090
  10.90.2.107:10090  prometheus  10.90.2.107  10090        /             Up      /tidb-data/prometheus_data  /tidb-deploy/prometheus-10090
  10.90.1.58:4000    tidb        10.90.1.58   4000/10080   linux/x86_64  Up      -                           /tidb-deploy/tidb-4000
  10.90.2.210:4000   tidb        10.90.2.210  4000/10080   /             Up      -                           /tidb-deploy/tidb-4000
  10.90.1.219:20160  tikv        10.90.1.219  20160/20180  linux/x86_64  Up      /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  10.90.1.71:20160   tikv        10.90.1.71   20160/20180  linux/x86_64  Up      /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  10.90.2.111:20160  tikv        10.90.2.111  20160/20180  /             Up      /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  10.90.2.91:20160   tikv        10.90.2.91   20160/20180  /             Up      /tidb-data/tikv_data        /tidb-deploy/tikv-20160
  Total nodes: 13
  ```

+ 各役割のリージョン配分を調べることで、DR Auto-sync設定がTiKVノードにうまく適用されていることを確認できます。

  ```bash
  tiup ctl:v${LAB:TIDB_VERSION} pd -u ${HOST_AZ1PD1_PRIVATE_IP}:2379 store
  ```

  ```output
  Starting component ctl: /home/ec2-user/.tiup/components/ctl/v${LAB:TIDB_VERSION}/ctl pd -u 10.90.2.83:2379 store
  {
   "count": 4,
   "stores": [
     {
       "store": {
         "id": 1,
         "address": "10.90.2.111:20160",
         ...
       },
       "status": {
         "leader_count": 0,
         "region_count": 79,
         ...
       }
     },
     {
       "store": {
         "id": 14,
         "address": "10.90.2.91:20160",
         ...
       },
       "status": {
         "leader_count": 0,
         "region_count": 79,
         "learner_count": 79,
         ...
       }
     },
     {
       "store": {
         "id": 2001,
         "address": "10.90.1.219:20160",
         ...
       },
       "status": {
         "leader_count": 42,
         "region_count": 79,
         ...
       }
     },
     {
       "store": {
         "id": 2002,
         "address": "10.90.1.71:20160",
         ...
       },
       "status": {
         "leader_count": 37,
         "region_count": 79,
         ...
       }
     }
   ]
  }
  ```

+ 上記の情報から、各TiKVノードの役割をリージョン分布から特定できます（ストアIDは例とは異なるかもしれません）。
  + ストア1は「フォロワー」: "leader_count": 0, "region_count": 79
  + ストア14は「ラーナー」: "leader_count": 0, "region_count": 79, "learner_count": 79
  + ストア2001は「投票者」: "leader_count": 42, "region_count": 79
  + ストア2002は「投票者」: "leader_count": 37, "region_count": 79

+ [ターミナル1](ssh://az1monitor1-ssh#1)に移動し、sysbenchスレッドを終了してターミナル1を閉じます。

+ [ターミナル2](ssh://az2monitor1-ssh#2)に移動し、sysbenchスレッドを終了してターミナル2を閉じます。

+ ターミナル3を閉じます。

### モジュール 3のまとめ

+ このモジュールを通じて、プライマリデータセンターが完全に故障した場合でも、TiDBクラスタを正常に復元し、RPO=0を保証できることを観察しました。おおまかに言うと、復旧操作には以下のステップが含まれます:
  + DC2で唯一残っているPDを回復させ、新しいクラスタを作成します。
  + DC2に新しいPDをスケールアウトし、新しいRaftグループを形成します。
  + クラスタモードを過半数モードに変更します。
  + DC1にある利用不可のTiKVノードを削除します。
  + すべてのクラスタノードを再起動し、2つの生き残ったTiKVノードで運用します。アプリケーションの読み書き操作の回復を観察します。
  + 故障を修正します。
  + DC1内のノードを初期化し、TiDBクラスタに再統合します。
  + クラスタ設定を変更してDR Auto-syncモードに戻します。
