# [FAQ]如何分析 TiKV 的 Region 未根据 Split region 机制进行预期拆分

## Key Words

TiKV, Region, Split region, Lightning, Import mode

## Description

TiKV 中大 Region 不及时 Split 拆分，导致 Dumpling 备份请求该 Region 时触发超过 gRPC 最大的 4 GiB 限制的 Panic ，报错关键信息 `refresh_buffer must not be called on CodedOutputStream create from slice` (该问题已在 v5.2.2 以上版本修复，遇到类似问题会报错而不是 Panic，解决方法可以通过调小读写请求大小或者 Split region 避免该问题)。 实际 TiKV 运维中 Region 不能及时 Split 情况并不常见，通过 TiKV 日志可以看到 Region 是有 Split region 操作，同时 Region size 和 keys 都达到了 Split 触发阈值。

以 Region 9990317 为例, 在此次 split 之前曾在 10/19 split 过一次，其他时间没有 split check。

```text
[2022/10/18 16:37:14.193 +08:00] [INFO] [pd.rs:507] ["try to batch split region"] [task=batch_split] [region="id: 9885160 start_key: 7480000000000001FFE45F7280000002C6FF73A8900000000000FA end_key: 7480000000000001FFE45F7280000002C6FF7A6A2B0000000000FA region_epoch { conf_ver: 340 version: 6880 } peers { id: 9885161 store_id: 9 } peers { id: 9885162 store_id: 4 } peers { id: 9885163 store_id: 2 }"] [new_region_ids="[new_region_id: 9990317 new_peer_ids: 9990318 new_peer_ids: 9990319 new_peer_ids: 9990320]"] [region_id=9885160]
......
[2022/10/19 07:51:34.195 +08:00] [INFO] [size.rs:172] ["approximate size over threshold, need to do split check"] [threshold=150994944] [size=201159060] [region_id=9990317]
......
[2022/10/30 02:19:33.243 +08:00] [INFO] [size.rs:172] ["approximate size over threshold, need to do split check"] [threshold=150994944] [size=3959188265] [region_id=9990317]
```

从下面日志来看 `region-size` 和 `region-keys` 都是满足了 split 的要求的，同时可以通过历史监控的 `Split check CPU` 看到 TiKV 有过 Split check 记录。

```text
177_tikv-20161.log:/data2/tidb/deploy/tikv-20161/log/tikv.log.2022-10-30-02:29:13.617578058:[2022/10/30 02:19:33.243 +08:00] [INFO] [size.rs:172] ["approximate size over threshold, need to do split check"] [threshold=150994944] [size=3959188265] [region_id=9990317]
177_tikv-20161.log:/data2/tidb/deploy/tikv-20161/log/tikv.log.2022-10-30-02:29:13.617578058:[2022/10/30 02:19:33.245 +08:00] [INFO] [keys.rs:157] ["approximate keys over threshold, need to do split check"] [threshold=1440000] [keys=9301120] [region_id=9990317] 
```

## Content

Region 不分裂应该是由于之前 Lightning 导入数据的时候，流程如下：

Lightning 异常了 -> 导致部分 TiKV 节点没有正确 switch mode 切回到 normal 状态 -> 最终造成部分 Region 的 split-check 失败引起的。

目前，可以通过 TiKV 日志判断，关键词 `switch mode` 确认当前模式为 `Import` 还是 `Normal`，如果一直处于 `import mode` 状态，导致 `split-check-tick` 一直跳过，使得部分 Region 的 size 持续上涨。

### Workarounds

将 TiKV 恢复到 normal 模式

```shell
tidb-Lightning-ctl --switch-mode normal
```

## Affected Versions

N/A