Elasticsearch无规律查询延迟问题排查求助
我们此次提交技术支持请求,是因为部分Elasticsearch查询出现极高延迟,目前已无更多排查思路定位问题根源。
我们知晓本次提问内容较长,但已自行完成多轮排查,提供的信息越充分,越便于大家给出精准的建议与帮助。我们不奢求直接获得解决方案,仅希望得到可探索的排查方向,以便定位并解决问题。
问题描述
大多数情况下我们的搜索查询响应时间不足1秒,但部分查询无明显诱因出现超过5秒、10秒甚至20秒的延迟。查询结构完全一致,仅搜索关键词不同,且未识别出会触发高延迟的特定关键词。
目前最阻碍排查的问题是该故障难以复现,我们无法定位触发延迟的具体场景。慢查询示例
搜索场景说明
我们基于Spring Boot应用通过Elasticsearch Java Client执行搜索查询,仅使用文末提供的同一查询语句,当前每小时仅触发少量查询,可完全排除负载过高导致的问题。我们通过Kibana手动测试也复现了相同的现象。
索引场景说明
我们每日执行一次覆盖约5%文档的局部索引,每周执行一次全量索引,操作以文档更新为主,存在少量新增/删除操作。所有索引任务均在非业务时段执行,可确保索引请求不会影响搜索请求性能。
压测场景说明
我们使用Locust进行压测,基于生产环境用户实际执行的查询构建了约2000条查询的测试词库。最反常的现象是:生产环境或手动测试低频率发起查询时,偶尔会出现1~20秒的延迟;但使用压测工具将QPS提升至50时,Elasticsearch响应时间反而低于300ms。
已尝试的无效优化方案
- 执行force_merge索引合并,排除分片碎片化问题
- 改为Elasticsearch自动生成文档ID(原索引阶段自定义文档ID),排除“advance”阶段延迟问题
- 配置
index.store.preload: ["nvd", "dvd"]预加载数据,排除冷启动问题 - 将
index.search.idle.after参数调大至1小时 - 简化托管架构,不使用Docker、Openstack卷,直接在Openstack虚拟机本地磁盘运行
已尝试的有效优化方案
我们运行了每分钟发起一次随机搜索查询的保活脚本(使用压测数据集),维持Elasticsearch“热”状态,该方案可将最高延迟降至2~5秒区间。
环境规格
版本
Elasticsearch 7.6.2
托管环境
OVH Public Cloud(Openstack),目标部署架构为Docker Swarm,无Docker环境下也观测到相同问题。
- 操作系统:Debian 9.13
- JDK版本:openjdk 16
- 内核版本:Linux kernel 4.9.0
资源配置
简化托管架构下使用3台OVH b2-30节点,节点专用于Elasticsearch:
- 每台8v核
- 30GB内存
- SSD存储
- 内网带宽保证500Mbps,最高可达2Gbps
索引信息
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size green open our-index-v1.1 6yUxerMtTViMlAtOO498Jg 3 1 12830216 2976961 31.7gb 16gb
索引配置
{ "our-index-v1.1" : { "settings" : { "index" : { "refresh_interval" : "1s", "number_of_shards" : "3", "number_of_replicas" : "1", "store" : { "preload" : [ "nvd", "dvd" ] }, "uuid" : "6yUxerMtTViMlAtOO498Jg" } } } }
索引映射
{ "our-index-v1.1" : { "mappings" : { "properties" : { "matchingGroups" : { "properties" : { "address" : { "type" : "text" }, "naming" : { "type" : "text" }, "onlineData" : { "type" : "text", "analyzer" : "french" }, "openData" : { "type" : "text", "analyzer" : "french" }, "internalData" : { "type" : "text", "analyzer" : "french" } } }, /* * 以下字段通过copy_to配置同步到上方的matchingGroups对应字段 */ "example1": { "type" : "text", "index" : false, "copy_to" : [ "matchingGroups.naming" ] }, "example2": { "type" : "keyword", "index" : false, "copy_to" : [ "matchingGroups.naming" ] } /* * 其余字段省略 */ } } } }
搜索查询语句
GET /our-index-v1.1/_search/template { "params": { "value_to_search": "spécialiste en poterie" }, "source": { "from": 0, "size": 100, "query": { "bool": { "must": [ { "bool": { "should": [ { "match_phrase": { "matchingGroups.naming": { "query": "{{value_to_search}}", "slop": 0, "boost": 20 } } }, { "match": { "matchingGroups.naming": { "query": "{{value_to_search}}", "boost": 6 } } }, { "match_phrase": { "matchingGroups.openData": { "query": "{{value_to_search}}", "slop": 0, "boost": 20 } } }, { "match": { "matchingGroups.openData": { "query": "{{value_to_search}}", "boost": 6 } } }, { "match_phrase": { "matchingGroups.onlineData": { "query": "{{value_to_search}}", "slop": 0, "boost": 20 } } }, { "match": { "matchingGroups.onlineData": { "query": "{{value_to_search}}", "boost": 6 } } }, { "match_phrase": { "matchingGroups.internalData": { "query": "{{value_to_search}}", "slop": 0, "boost": 20 } } }, { "match": { "matchingGroups.internalData": { "query": "{{value_to_search}}", "boost": 6 } } } ] } } ], "must_not": [ { "term": { "customFilter.code": { "value": "1000" } } }, { "term": { "customFilter.code": { "value": "5485" } } }, { "term": { "customFilter.code": { "value": "6540" } } }, { "term": { "customFilter.code": { "value": "6589" } } }, { "term": { "customFilter.code": { "value": "6599" } } }, { "term": { "customFilter.code": { "value": "9110" } } } ] } } } }
内容的提问来源于stack exchange,提问作者silex-kbui

