简述
Elasticsearch是一个分布式的免费开源搜索和分析引擎,能够实现近实时的数据搜索。在使用的过程中,由于各种原因可能导致集群写入或者查询缓慢,本文主要讲述集中常见的原因和解决方法。
写入拒绝或者慢
现象
当像索引(存储和使文档可被搜索)或者搜索数据的时候会出现类似如下429状态码的报错:
"status": 429, "error": {"type": "es_rejected_execution_exception", "reason": "rejected execution of org.elasticsearch.transport.TransportService$7@77c11b3c on EsThreadPoolExecutor[name = VM-1-1-1-1/write, queue capacity = 800, org.elasticsearch.common.util.concurrent.EsThreadPoolExecutor@4349a9ab[Running, pool size = 32, active threads = 32, queued tasks = 800, completed tasks = 13026004]]"}}
排查
查看Indexing Rate监控指标确认是不是写入速率过高
1)通过kibana或者其它monitor监控查看
2)通过API自己取目前的值,然后存储自己计算:
http://192.168.1.12:9200/_stats
查看集群目前的ThreadpoolWriteQueue和ThreadpoolWriteRejected,确认是不是集群写入太慢或者并发写入过多,导致单node write队列满了。7.x版本每个node的ThreadpoolWriteQueue默认为10000,线程池大小默认为此node的cpu核心数。
1)通过kibana或者其它monitor监控查看
2)通过API自己取目前的值,然后存储自己计算:
http://192.168.1.12:9200/_cat/thread_pool/write?v&h=id,name,active,queue,rejected,completed
根据实际情况,打开慢写入日志,查看哪些写入比较消耗时间,从而进行优化
curl -X PUT "192.168.1.12:9200/_settings?pretty" -H 'Content-Type: application/json' -d'
{
"index.indexing.slowlog.threshold.index.warn": "10s",
"index.indexing.slowlog.threshold.index.info": "5s",
"index.indexing.slowlog.threshold.index.debug": "2s",
"index.indexing.slowlog.threshold.index.trace": "500ms",
"index.indexing.slowlog.level": "info",
"index.indexing.slowlog.source": "1000"
}
'
解决
client写入端增加重试,重试时间设置为随机生成的时间
使用bulk批量写入
增大索引的refresh时间,降低开销
降低shard的replica数量
关闭swap