# [KB]-TiFlash-query-error-region-xx-is-unavailable

## Key Words

TiFlash, region unavailable, learner read

## Description

1. 执行 TiFlash 查询报错：1105 - other error for mpp stream: DB::TiFlashException: Region 21679 is unavailable: (while doing learner read for table,table_id: 1056)，TiFlash 包含如下日志信息：

![know-issue-tiflash-unavailable1.png](/images/TiFlash/know-issue-tiflash-unavailable1.png)

从日志截图看， TiFlash 确实一直没有追上同步信息，导致报错。最终同步信息追上后，可以成功查询。

```golang
2022/10/12 18:55:38141 +08:00] [INFO][apply.rs:1272] ["persist apply state"[state="applied index:2910 commit2910 commit term: 8 truncated state{ index: 2907 term:8}"] [peer id=40029][region id=21679]
2022/10/12 18:55:44.145+08:00][INF0][apply.rs:1272] ["persist apply state"] [state="applied_index: 3078 commit_index:3078 commit term: 8 truncated state{ index: 3076 term: 8 }"] [peer id=40029] [region id=21679]
2022/10/12 18:55:50.157 +08:001 INF01[applv.rs:12721 ["persist applv state"l Istate="apblied index: 3240 commit indexa3240 commit term: 8 truncated state { index: 3238 term: 8 }"] [peer id=40029] [region id=21679]
[2022/10/12 18:55:56.181 +08:00] [INFO] [apply.rs:1272] ["persist apply state"][state="applied index: 3416 commit index:3416 commit term: 8 truncated state {index: 3413 term:8}"] [peerid=40029] [region id=21679]
2022/10/12 18:56:02.185 +08:00] [INFO] [apply.rs:1272] ["persist apply state"][state="applied index:3584 commit index:3584 commit term: 8 truncated state {index: 3582 term:8}"] [peerid=40029][region id=21679]
2022/10/12 18:56:08.343 +08:00] [INFO] [apply.rs:12721 ["persist apply state"][state="applied index: 3746 commit index:3746 commit term: 8 truncated state { index: 3743 term:8}"] [peer id=40029] [region id=21679
2022/10/12 18:56:17.240 +08:0][INF0] [apply.rs:1272] ["persist apply state"] [state="applied index: 3983 commit index:3983 commit term: 8 truncated state { index:3980 term: 8 }"] [peer id=40029] [region id=21679]
2022/10/12 19:14:32.617+08:00][INF0][apply.rs:1272] ["persist apply state"] [state="applied index: 3991 commit index:3991 commit term: 8 truncated state { index: 3989 term:8}"] [peer id=40029][region id=21679]
2022/10/12 22:06:34.942 +08:00][INF0][apply.rs:1272] ["persist apply state"] [state="applied index: 4595 commit index:5259 commit term: 8 truncated state { index: 4007 term: 8 "1 [peer id=400291 [reaion id=21679]
2022/10/12 22:06:35.284 +08:001 INF1 [applv.rs:12721 ["persist applv state"1 Istate="applied index: 5199 commit indexs5259 commit term: 8 truncated state{ index: 4024 term: 8 }"] [peer id=40029] [region id=216791]
2022/10/13 18:45:36.423 +08:00][INF0][apply.rs:1272] ["persist apply state"] [state="applied_index: 6325 commit_index:6326 commit term:8 truncated state{ index:5613 term: 8}"] [peer id=40029] [region id=21679]
2022/10/13 18:45:45.668 +08:00][INF0] [apply.rs:1272] ["persist apply state"] [state="applied_index: 6561 commit_index:6561 commit term: 8 truncated state { index: 6557 term: 8 }"] [peer id=40029] [region id=21679]
```

## Root Cause

TiDB 向 TiFlash 发起查询，需要 Learner Read 来确保外部一致性，所有的查询都需要由上层拆分为一个或多个 Region 的读请求，需要包含 Region 的两种版本号（可从 PD 或 TiKV 获取），Timestamp（用于 Snapshot Read 的时间戳，从 PD 获取），以及 Table 相关信息（Schema Version 从 TiDB 获取）。单次读请求可大致分为以下步骤：

1. 校验并更新本节点的 Schema
2. 向 Region 的 Leader Peer 获取最新的 Index，等待当前节点中 Learner 的 Applied Index 追上
3. 校对 Version 和 Conf Version，检查 Lock CF 中的锁信息
4. 读取内存中的半结构化数据和存储引擎中 Region 范围对应的结构化数据
5. 按照 Timestamp 进行 Snapshot Read 和多路合并
6. Coprocessor 计算（主要借助 ClickHouse 的向量化计算引擎）

该报错为：第二步当前节点中 Learner 同步 Leader Applied Index  等待时间过长。

## Workaround

1.建议可以调大参数，加快同步信息。 `raftstore.snap-handle-pool-size: 4` ## 默认 2，控制处理 snapshot 的线程数，默认为 2。等同步完成继续查询即可。

2.升到 v6.2.0 及以上版本，这个 [fix](https://github.com/pingcap/tiflash/pull/5498) 后应该会对该问题有所缓解。
