Split index API | Elasticsearch Guide [8.10] | Elastic
当您使用Elasticsearch集群出现索引分片设置不合理(例如索引主分片设置不合理、每个分片存在大量数据等)引发集群性能问题时,可通过_split API在线扩大主分片数,将现有索引拆分为具有更多主分片的索引。本文介绍如何通过_split API快速拆分主分片。
索引创建后,Elasticsearch不支持修改索引主分片的数量,如果需要修改,一般会使用reindex重建索引,耗时太久。而在6.x版本开始,Elasticsearch支持在线扩大主分片数的Split index API,支持将现有索引拆分为具有更多主分片的索引。
reindex与_split API的性能测试信息如下:
| 方式 | 耗时 | 资源占用 |
|---|---|---|
| reindex | 2.5小时 | 集群中有大量的写QPS,索引所占节点资源高。 |
_split API | 3分钟 | 集群数据节点CPU使用率为78%左右,load_1m为10左右。 |
说明
本文以阿里云Elasticsearch 7.10.0版本为例,其他版本操作可能略有差别,请以实际界面为准。
说明
使用时需要将dest1替换为您的业务索引名。
- PUT /dest1
- {
- "settings": {
- "index": {
- "number_of_routing_shards": 24,
- "number_of_shards":2
- }
- }
- }
| 参数 | 说明 |
|---|---|
| number_of_routing_shards | 路由分片数,定义索引可拆分的次数或原始分片可拆分的分片数。创建索引指定该参数,要求索引主分片数必须是路由分片数的一个因数。 说明
|
| number_of_shards | 索引的主分片数。 |
说明
以下数据仅供测试。
- POST /dest1/_doc/_bulk
- {"index":{}}
- {"productName":"产品A","annual_rate":"3.2200%","describe":"可以自助选择消息推送"}
- {"index":{}}
- {"productName":"产品B","annual_rate":"3.1100%","describe":"每天收益到账消息推送"}
- {"index":{}}
- {"productName":"产品C","annual_rate":"3.3500%","describe":"每天收益立即到账消息推送"}
- PUT /dest1/_settings
- {
- "settings": {
- "index.blocks.write": true
- }
- }
- POST dest1/_split/dest3
- {
- "settings": {
- "index.number_of_shards": 12,
- "index.blocks.write": null
- }
- }
以上示例使用_split API从原索引dest1拆分出新索引dest3,设置新索引的分片数为12,且取消新索引禁止写限制。 注意
_cat recovery API查看分片拆分进度,当无拆分分片相关的recovey,且集群状态健康,则分片拆分完成。
GET _cat/recovery?v&active_only 当返回结果的index列没有待拆分的索引时,说明无拆分分片相关的recovey。
GET _cluster/health 当返回结果中包含"status" : "green"时,说明集群状态健康。
Q:split完成后,为什么集群的CPU使用率、节点load_1m没有降下来?
A:split过程涉及到对文档进行重新路由,新索引会存在大量的docs.deleted文档。通过GET _nodes/hot_threads可看到索引在进行merge操作,所以计算资源负载会比较高,建议在业务低峰期操作。