# Restoring a Cluster After an Unsafe Recovery

>💡 The audience for this article is front-line DBAs, covering operations when there is a cluster majority copy loss and non-secure recovery

## test environment

For our test, there were three copies of each region, distributed over 5 TiKV instances. The host IPs were setup as follows:

<table><thead><tr><th><span><span><span><br></span></span></span></th><th><span><span><span><br></span></span></span></th></tr></thead><tbody><tr><td><p><span><span><span><strong>Component</strong></span></span></span></p></td><td><p><span><span><span ><strong>IP</strong></span></span></span></p></td></tr><tr><td><p><span><span><span >TiDB</span></span></span></p></td><td><p><span><span><span >172.16.7.200</span></span></span></p></td></tr><tr><td><p><span><span><span >PD</span></span></span></p></td><td><p><span><span><span >172.16.7.200</span></span></span></p></td></tr><tr><td><p><span><span><span >TiKV</span></span></span></p></td><td><p><span><span><span >172.16.7.176</span></span></span></p></td></tr><tr><td><span><span><span><br></span></span></span></td><td><p><span><span><span >172.16.7.182</span></span></span></p></td></tr><tr><td><span><span><span><br></span></span></span></td><td><p><span><span><span >172.16.7.200</span></span></span></p></td></tr><tr><td><span><span><span><br></span></span></span></td><td><p><span><span><span >172.16.7.222</span></span></span></p></td></tr><tr><td><span><span><span><br></span></span></span></td><td><p><span><span><span >172.16.7.229</span></span></span></p></td></tr></tbody></table>

## 1\. Background

------------------------

TiDB defaults to 3 replicas, and between the replicas, the leader is elected through raft and the majority is to ensure strong consistency and high availability. If the majority is lost, the region may be unavailable, or even permanent data loss may occur. If similar unrecoverable failures occur , The business needs to be restored urgently. At this time, the TiDB cluster needs to be restored quickly and the data is restored to the greatest extent. The specific situation is divided into the following categories:

* Only one copy is lost, no need to deal with;

* If the majority copy is lost, data needs to be restored based on the remaining copy. In extreme cases, the latest transaction-level data will be lost. At this time, the business side needs to redo the latest data at the time of the disaster;

* All copies are lost. At this time, the data loss of the region cannot be recovered. Business basis is required: redo data related to all lost tables in the copy.

## 2\. Simulate the occurrence of a disaster, that is, the loss of the majority

------------------------

 1\. Get all store ids, find the store id corresponding to a certain region of a table

Execute the command to get the mapping relationship between store id and ip

```bash
tiup ctl:v5.2.1 pd -u 172.16.7.200:9233 store
```

![to get the mapping relationship between store id and ip](https://lh5.googleusercontent.com/TkhrS_04toBIwFxeuGZ66JJThHPC26o6yLVcoat5WlxiYq-yuH2bzrj2UVycAeXFIzGrGXpOdPND4lE-OvrQqQA5o0Xk4OAbwM6IdHV6zucc2Qd9E4bpFVZQGJRGGECWnWO8aFFm)
<br>
<table><thead><tr><th><p>store id</span></span></span></p></th><th><p>ip</span></span></span></p></th></tr></thead><tbody><tr><td><p>1</span></span></span></p></td><td><p>172.16.7.182</span></span></span></p></td></tr><tr><td><p>4</span></span></span></p></td><td><p>172.16.7.222</span></span></span></p></td></tr><tr><td><p>5</span></span></span></p></td><td><p>172.16.7.176</span></span></span></p></td></tr><tr><td><p>104</span></span></span></p></td><td><p>172.16.7.200</span></span></span></p></td></tr><tr><td><p>105</span></span></span></p></td><td><p>172.16.7.229</span></span></span></p></td></tr></tbody></table>

## Viewing sbtest.sbtest1 table region distribution

```sql
show table sbtest.sbtest1 regions;
```

![show table sbtest.sbtest1 regions](https://lh3.googleusercontent.com/mlVRDvmel-w44Lxk_rLdEinO6pNVOUafT-nz_TgR-frpOoK-O6WSJU9rn6HiS5zvHsshWmUe2QlW8CBtxHmWPbUk9UF2SQ_Qi2qlNw5ODfWvOsNuF_cr7YK8ISt1l804R9vo3yZk)

Here we select the two copies 151 and 403 of region id 149 of the sbtest.sbtest1 table as the object of the fault simulation, and find the addresses corresponding to these two copies

```bash
tiup ctl:v5.2.1 pd -u 172.16.7.200:9233 region 149
```

![Here we select the two copies 151 and 403 of region id 149 of the sbtest](https://lh4.googleusercontent.com/MsGIojMMuhRyL5OcuekOlkpLgnjygivgAN1ICFTX8Te5S-g5nY_vFF6HLmneM0AbkurqOD9EfFvOs3LPzxweuEpqoqShGWLZSo91bX-FQpinVr4yu4VE2rR_DEG4DYLUxjDjwt_5)

From the above figure, we can see that the 151 copy corresponds to store\_id 104 and the 403 copy corresponds to store\_id 105, which correspond to the 172.16.7.200 and 172.16.7.229 machines respectively.

## 2\. Manually simulate a downtime tikv node

------------------------

### Turn off automatic restart

```bash
vim /etc/systemd/system/tikv-30161.service
```

![Turn off automatic restart](https://lh6.googleusercontent.com/JBkcE7mxHySwwMTYIAjX06zGkMM5lYpmjtGDeTpHVQwJJGTlY_GaA7FYLoARA8Vm8jOLjby2cT86Omw7wv6FmsJAH926hqP0l2aoYmVmQC6OSknztzkPJS5_3osfENh5HWqiWRdx)

Make the configuration effective

```bash
systemctl daemon-reload
```

Kill the TiKV process of the 172.16.7.200 and 172.16.7.229 machines, and confirm that the TiKV node is down

```bash
kill -9 \[tikv server pid\]
```

![kill tikv server](https://lh3.googleusercontent.com/y8imQ2RWkJOAuS55ZHj3CntX-87MGNXUgkjQzBtgle6jqfh3ilxQLicFfF17Qkb_nssXlFVsxh_OzRXH2xwl8_QAt_7GvQZuS1B7H-NjPd-OqEW7X3anHdpQa9-YKu8-hKnrblKO)

![And display cluster](https://lh6.googleusercontent.com/k3HBo7kJ3LBur9WRBgQ5vKJ7x6Da3bDDeXf5lP9V3iJ3eYSHjd9j_oKD_O9fdU03MIJ1aXsLNuk0DELYwwmewdH5FaejdeDu4PuqhlluU3DTRgCsJVpOjo0EuV_akSpI27UkEVoe)

Delete the tikv deployment directory and data of the failed node

```bash
rm -rf /home/tidb-deploy\_xq/tikv-30161

rm -rf /home/tidb-data\_xq/tikv-30161
```

Table loses majority copy verification

![Error screen](https://lh4.googleusercontent.com/Pt7RXP46uIcGDgPVKb8UuI46TGfMLmaxtgbJs7sGVrxwJ84iunl4PAzxii7O-SwbgdssEfijBDnv2Tbp3nvfbrfJpwxfRKXMbYXh-rw8XNPaB8tMscsYmc1SDh574b8YpBrS6uTM)

## 3\. Unsafe recover steps and instructions

------------------------

### 1\. Query the downtime TiKV store id, here are 104 and 105

```bash
tiup ctl:v5.2.1 pd -u 172.16.7.200:9233 store | grep -C 1 -E "172.16.7.200|172.16.7.229"
```

![Query the down TiKV store id](https://lh4.googleusercontent.com/ClHhjslFfkUuZelmqvvQgzWnIZKfchQMCbhnshvxFrWx5gbiM8t_aKyCXuu_XFsDPghOYiWgYSb2zPA7xqWFumfHQJlwgNXNL5PCM15wzTFZjm7UTYpuvcINxuDYvxnjd2VfHSuZ)

### 2\. Obtain the default parameter values of region-schedule-limit, replica-schedule-limit, leader-schedule-limit, merge-schedule-limit, record them and set these four parameters to 0

View parameter values:

```bash
tiup ctl:v5.2.1 pd -u 172.16.7.200:9233 config show | grep -E'"region-schedule-limit"|replica-schedule-limit|leader-schedule-limit|merge-schedule-limit'
```

![View parameter values](https://lh6.googleusercontent.com/C3O3kgC_numYPXZpq5XaYuojRB6hLzZp1DhyI5RPClcBOLPIOYZlERDFa5mSThllPSWV_R8ixKD51kbgx88PsCfaZQFKXTHMlKs6HM8GkKeqpWTKA_g3ko-Zq_ul1RzToCMxYTGj)

Modify parameter value

```bash
tiup ctl:v5.2.1 pd -u 172.16.7.200:9233 config set region-schedule-limit 0

tiup ctl:v5.2.1 pd -u 172.16.7.200:9233 config set replica-schedule-limit 0

tiup ctl:v5.2.1 pd -u 172.16.7.200:9233 config set leader-schedule-limit 0

tiup ctl:v5.2.1 pd -u 172.16.7.200:9233 config set merge-schedule-limit 0
```

double check

![double check](https://lh6.googleusercontent.com/wk2Yy0V1WL7I5vmm8tyzH1drwmsFayoTNspwycDq8cQvLLHrWRt_VX7arJNggEKCe9B4msX_PaLZGzW3_sgPktXbpLtAFVpdJUnrVn3grLbfTT_umIZriDAVAz2hOVCCIFK6uwt6)

### 3\. Check the region where "more than half of the replicas are on the failed node" and record the region id

Failure node store id 104 and 105

Enter pd interactive command terminal

```bash
tiup ctl:v5.2.1 pd -u 172.16.7.200:9233 -i
```

View the region id of the lost majority copy

```bash
region --jq=".regions\[\] | {id: .id, peer\_stores: \[.peers\[\].store\_id\] | select(length as $total | map(if .==(104,105) then. else empty end) | length>=$total-length) }"
```

<br/>
![View the region id](https://lh3.googleusercontent.com/Lm3CgsZvdJTzfm7zbcbw1UXkvqeihWNVlOLlK8zYoS3dzvNaSml6is3kohOD2CG5A_9AO1KFUN1W3vR6Q0YE4eZ97gPp-NCCo4WQTqDlA63qRE64zZ71Cah5YhWPMUtS3UfKLOze)
<br/>

### 4\. Stop all TiKV nodes

```bash
tiup cluster stop tidb-xq -R=tikv
```

![Stop all TiKV nodes](https://lh4.googleusercontent.com/4uNmzEBb1rX4QZKAU-bVbo3VrfggQlrMsnidtQgvKXnVzX_0vTWfFE3fEnBF9CQSKV8wC1jLeiMDq0iwgs9oP5gY0qwuni-DQt6KeCcAoFk9Qbu_Jyt0CHCFlLRCJTNvQal8aDGO)

### 5\. Remove the store id 104 and 105 information of the failed TiKV node on all normal TiKV nodes

Send the ./.tiup/components/ctl/v5.2.1/tikv-ctl command under the tiup machine to the healthy tikv node

```bash
scp ./.tiup/components/ctl/v5.2.1/tikv-ctl 172.16.7.176:/root/
```

Remove fault store id information

```bash
./tikv-ctl --data-dir /home/tidb-data\_xq/tikv-21160 unsafe-recover remove-fail-stores -s 104,105 --all-regions
```

![Remove fault store id information](https://lh3.googleusercontent.com/U6JeA3ER9JR4SlTDjh9ectgOFCBhbiiM9w5_jrQUFnFsAAJvUqWR8xKS-j6mySWWEbWC1pOmjyfBOVH-sLyB9b9UAe_Vcc4tbB8I7DkqW3Uq5J6bJCLnTy_SEDu5JhNg3XPyeg86)

### 6\. Stop the PD node

```bash
tiup cluster stop tidb-xq -R=pd
```

### 7\. Start PD, TiKV node

```bash
tiup cluster start tidb-xq -R=pd,tikv
```

### 8\. View the region without a leader

Enter pd interactive command terminal

```bash
tiup ctl:v5.2.1 pd -u 172.16.7.200:9233 -i
```

View the region without a leader

```bash
region --jq'.regions\[\]|select(has("leader")|not)|{id: .id,peer\_stores: \[.peers\[\].store\_id\]}'
```

![View the region without a leader](https://lh3.googleusercontent.com/kZaMIo3cS0VJFWFHNn_3sjW_9CnoYPXTk7vEDf4KWhu13GHH6ipR7g5RnzCrCD40fMngkZtctO1mbehze2N7QaHRcEDYkVN2IIE6Xn_6pY2ZOTAMBLrx0vv5VknQXIJZeS2syTyO)

### 9\. Check the table data at this time, and the region distribution

![Check the table data](https://lh6.googleusercontent.com/jgdLw-Z34_6WvtCAtwBsElp1bNqWBCCzDiZEFOYOz_nSRG2SinERcbT9DDs3aPoVlCm4We-5fGQg2a0qab2RkZVH8RJoyDTGrrciM7dITSHBpBqFwOzN9-gpOzaxWRr_qxdH1399)

![Check the region distribution](https://lh6.googleusercontent.com/dGizc-cqj2dG-brPi3HGSDRQPyy3fQV0SYuXhsdiQ6aDPNCqO-bFlSOb4g7Pmg_9Gk0Bo5POM1ur3SWDnD-Pwv-EZHIHiGbEMt3Au083eyisr1om2hPZB-VlN5KyezlSifjsjlmT)

### 10\. Restore the scheduling parameters, continue to check the table region again, so far, the restoration is over

```bash
tiup ctl:v5.2.1 pd -u 172.16.7.200:9233 config set region-schedule-limit 2048

tiup ctl:v5.2.1 pd -u 172.16.7.200:9233 config set replica-schedule-limit 64

tiup ctl:v5.2.1 pd -u 172.16.7.200:9233 config set leader-schedule-limit 4

tiup ctl:v5.2.1 pd -u 172.16.7.200:9233 config set merge-schedule-limit 8
```

![check the table region again](https://lh6.googleusercontent.com/8d1bwONIgNetQxUoaknNeybfnXOCOTcQQJJBjPj4He1RCFO6zBivWPhUdlw6ALMcXqEGSBUubq14y2-XQHnGtzTDgOHJyZBo1TLume69U7SnbGDqCVbZqFyl-jGdEAVjQDjc6qDC)
<br/>
<br/>
<br/>
<br/>
<br/>

------------------------

> **Product**: TiDB <br/>
> **Component**: TiKV <br/>
> **Affected Versions**: 5.2.1 <br/>
> **Tags**: kb TiDB Supportability Troubleshoot
