写入优化
https://cloud.tencent.com/developer/article/1511890
索引优化
乐观锁版本控制
通过版本实现乐观锁,防止数据被低版本的覆盖掉。
org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: version type [EXTERNAL_GTE] is not supported by the update API;
String json = ObjectMapperUtils.toJSON(item);
// 目前是双集群,获取当前要写的集群
String index = esIndex.get().shardIndexName(item.pickShardFactor());
String type = esIndex.type();
IndexRequest indexRequest = new IndexRequest(index, type, item.getId());
indexRequest.version(162218455442L);
indexRequest.versionType(VersionType.EXTERNAL_GTE);
indexRequest.source(json, XContentType.JSON);
UpdateRequest updateRequest = new UpdateRequest(index, type, item.getId());
System.out.println(item.getUpdateTime());
updateRequest.version(162218455442L);
updateRequest.versionType(VersionType.EXTERNAL_GTE);
updateRequest.doc(json, XContentType.JSON);
updateRequest.docAsUpsert(true);
bulkProcessor.add(updateRequest);
遇到的问题
indexRequest是覆盖操作
首先要明确概念IndexRequest是覆盖操作,不存在创建,存在直接覆盖(ES内部原理删除再创建)。
调试记录
读表:40个线程,20个processor, 3w一批次
1000条一批次查询,0.6s
粗略估计
实际数据
平均qps: 6.6w
读表:100个线程,20个processor, 3w一批次
1000条一批次查询,2s
粗略估计
实际数据
平均qps: 6.6w
40个processor,gc耗时过长,
G1NewSizePercent 从30%调整到50%, 机器内存调整到64g
db5. es5
2021-11-23 14:51:12.048 INFO order-write-es-task-2 - MerchantFinancialCenterOrderWriteEsTask shard-14, total_cost:2 min, progress:548000/0, total_avg:4089/s, cost:212 ms
db5. es20(10)
2021-11-23 15:21:32.598 INFO order-write-es-task-2 - MerchantFinancialCenterOrderWriteEsTask shard-14, total_cost:6 min, progress:2497500/0, total_avg:6642/s, cost:55 ms
db10. es20(10)
2021-11-23 15:52:49.231 INFO order-write-es-task-3 - MerchantFinancialCenterOrderWriteEsTask shard-15, total_cost:5 min, progress:1781500/0, total_avg:5286/s, read_db_cost:25, process_cost:207 ms
经过查询才知道 删除文档并没有真正删除,仅作了删除标记,从而不能再被搜索到。要想释放磁盘的空间,要使用forcemerge命令合并段减少分片中段数量、删除冗余数据。
1、优化所有索引:
POST http://localhost:9200/_forcemerge?only_expunge_deletes=true
2、优化单个索引:
POST http://localhost:9200/索引名/_forcemerge?only_expunge_deletes=true