1、错误异常:document_missing_exception
org.springframework.data.elasticsearch.UncategorizedElasticsearchException: Elasticsearch exception [type=document_missing_exception, reason=[_doc][412457]: document missing];
服务环境:
- 三台服务器 各部署一个服务
- Mysql8.0 + Canal + Kafka2.6.0 + Elasticsearch7.6.2
原因:配置了文档不存在 更新时不创建文档
DocAsUpsert=false
,在文档新增前进行修改会报此错误。
解决办法:由于本项目环境在新增时会获取最新数据,所以即使报错,也可以保证文档的最终数据一致性,所以只是把异常捕获掉;
问题情况:
1、考虑消费顺序是否异常
2、根据实际情况可配置文档不存在即创建
//DocAsUpsert 不存在是否插入 默认false
//Document 更新数据
UpdateQuery updateQuery = builder.withDocAsUpsert(false).withDocument(document).build();
try {
UpdateResponse response = elasticsearchRestTemplate.update(updateQuery,IndexCoordinates.of("indexName"));
} catch (UncategorizedElasticsearchException e){
}
贴出完整错误:
org.springframework.data.elasticsearch.UncategorizedElasticsearchException: Elasticsearch exception [type=document_missing_exception, reason=[_doc][412457]: document missing]; nested exception is [索引名称/NSavibY2QLOJObm0xJgJ-g][[索引名称][2]] ElasticsearchStatusException[Elasticsearch exception [type=document_missing_exception, reason=[_doc][412457]: document missing]]
at org.springframework.data.elasticsearch.core.ElasticsearchExceptionTranslator.translateExceptionIfPossible(ElasticsearchExceptionTranslator.java:67)
at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.translateException(ElasticsearchRestTemplate.java:378)
at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.execute(ElasticsearchRestTemplate.java:361)
at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.update(ElasticsearchRestTemplate.java:234)
报错异常类:UncategorizedElasticsearchException
ES异常类:ElasticsearchException
ES错误枚举:ElasticsearchExceptionHandle
2、错误异常:Cannot index a document due to seq_no+primary_term conflict
org.springframework.dao.OptimisticLockingFailureException: Cannot index a document due to seq_no+primary_term conflict; nested exception is [case_info-prod/NSavibY2QLOJObm0xJgJ-g][[case_info-prod][0]] ElasticsearchStatusException[Elasticsearch exception [type=version_conflict_engine_exception, reason=[121857]: version conflict, required seqNo [1323993], primary term [1]. current document has seqNo [1747408] and primary term [1]]]
原因:Es更新操作
乐观锁
,主要是在集群环境的情况下同时操作导致,并发修改触发乐观锁
需求环境:本项目根据Canal监听binlog消息到kafka 消费增量同步Es;
问题出现情况:多台服务同时处理同一条消息到Es
解决办法:
1、多台服务器配置消费不同Partition
2、可通过RetryOnConflict设置重试次数
//DocAsUpsert 不存在是否插入 默认false
//RetryOnConflict 失败重试次数
//Document 更新数据
UpdateQuery updateQuery = builder.withDocAsUpsert(false).withRetryOnConflict(3).withDocument(document).build();
UpdateResponse response = elasticsearchRestTemplate.update(updateQuery,IndexCoordinates.of("indexName"));
贴出完整错误:
org.springframework.dao.OptimisticLockingFailureException: Cannot index a document due to seq_no+primary_term conflict; nested exception is [case_info-prod/NSavibY2QLOJObm0xJgJ-g][[case_info-prod][0]] ElasticsearchStatusException[Elasticsearch exception [type=version_conflict_engine_exception, reason=[121857]: version conflict, required seqNo [1323993], primary term [1]. current document has seqNo [1747408] and primary term [1]]]
at org.springframework.data.elasticsearch.core.ElasticsearchExceptionTranslator.translateExceptionIfPossible(ElasticsearchExceptionTranslator.java:55)
at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.translateException(ElasticsearchRestTemplate.java:378)
at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.execute(ElasticsearchRestTemplate.java:361)
at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.update(ElasticsearchRestTemplate.java:234)
嘀嘀嘀 彩蛋时间~
如何查看Es版本
访问ES地址 返回JSON数据
{
"name" : "node-1",
"cluster_name" : "es",
"cluster_uuid" : "Yz9huLzYRZ-K6fVYuE4zXA",
"version" : {
"number" : "7.6.2",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
"build_date" : "2020-03-26T06:34:37.794943Z",
"build_snapshot" : false,
"lucene_version" : "8.4.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
kibana可通过'GET /'命令查看
linux查看kafka版本
find /opt -name \*kafka_\* | head -1 | grep -o '\kafka[^\n]*'