一、定位GC问题的关键步骤
- 检查JVM配置
bash
# 查看Elasticsearch节点JVM配置
GET /_nodes/jvm
重点关注heap_max_in_bytes
和垃圾回收器类型(G1/CMS)
- 分析GC日志
yaml:config/jvm.options
# 启用GC日志(需重启集群)
-Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m
- 监控内存压力
bash
# 实时监控内存使用
GET /_nodes/stats/jvm?filter_path=**.heap_used_percent
二、常见优化方案
- JVM参数优化
yaml:config/jvm.options
# 调整G1GC参数(适用于JDK11+)
-XX:+UseG1GC
-XX:G1ReservePercent=25
-XX:InitiatingHeapOccupancyPercent=30
-XX:MaxGCPauseMillis=200
- 查询优化建议
json
// 避免深度分页
GET /index/_search
{
"query": {...},
"size": 100,
"sort": "_doc", // 无评分排序
"track_total_hits": false
}
- 索引结构调整
bash
# 设置doc_values优化字段存储
PUT /index/_mapping
{
"properties": {
"large_field": {
"type": "keyword",
"doc_values": true
}
}
}
三、高级调优措施
- 缓存策略调整
yaml:config/elasticsearch.yml
indices.requests.cache.size: 5%
indices.queries.cache.size: 5%
indices.fielddata.cache.size: 30%
- 线程池优化
yaml:config/elasticsearch.yml
thread_pool.search.size: 4 # 建议等于CPU核心数
thread_pool.search.queue_size: 1000
- GC监控命令示例
bash
# 实时监控GC状态(需节点安装JDK)
jstat -gcutil <pid> 1000 10
四、配套优化建议
- 使用Search Profiler分析查询瓶颈
json
GET /index/_profile
{
"query": {...}
}
- 对于超大聚合查询,建议:
- 启用
execution_hint: map
- 设置合理
size
参数 - 使用composite aggregation代替terms aggregation
- 集群层面优化:
bash
PUT /_cluster/settings
{
"transient": {
"search.default_pre_filter_shard_size": 128
}
}
建议优先从查询优化和JVM参数调整入手,同时结合GC日志分析具体GC类型(Young GC/Full GC)和停顿时间。若频繁出现Full GC,需重点检查内存泄漏或大对象分配问题。