es查询主分片副本分片造成es每次查询结果不一致问题

方案一
对于7.0之前的版本可以指定只查询主分片,7.0之后去掉了这个参数
elasticsearch可以使用preference参数来指定分片查询的优先级
java的调用接口:client.prepareSearch(“index”).setPreference(“_primary”)。
指定_primary 或者_primary_first(线上优先选择这个) 防止每次在主分片和从分片轮训结果不一致

`_primary`:发送到集群的相关操作请求只会在主分片上执行。
`_primary_first`:指查询会先在主分片中查询,如果主分片找不到(挂了),就会在副本中查询。 
`_replica`:发送到集群的相关操作请求只会在副本上执行。
`_replica_first`:指查询会先在副本中查询,如果副本找不到(挂了),就会在主分片中查询。
`_local`: 指查询操作会优先在本地节点有的分片中查询,没有的话再在其它节点查询。
`_prefer_nodes:abc,xyz`:在提供的节点上优先执行(在这种情况下为'abc''xyz'`_shards:2,3`:限制操作到指定的分片。 (`2`和“3”)。这个偏好可以与其他偏好组合,但必须首先出现:`_shards:2,3 | _primary`
`_only_nodes:node1,node2`:指在指定id的节点里面进行查询,如果该节点只有要查询索引的部分分片,就只在这部分分片中查找,不同节点之间用“,”分隔。

https://www.elastic.co/guide/en/elasticsearch/guide/2.x/_search_options.html#_preference

https://www.elastic.co/guide/cn/elasticsearch/guide/current/relevance-is-broken.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/consistent-scoring.html
https://elasticsearch.cn/question/2275

方案二、
取消副本, 风险有数据丢失风险

方案三、
_only_nodes 手动去指定一下当前索引的主shard所在节点(没验证)

方案四:
减少refresh时间, 我们这个业务刚开始refresh 是20s 最后调整到5s,减少了每次查询结果不一致的问题发生概率。

但是出现了插入数据报错

[ElasticsearchException[Elasticsearch exception [type=es_rejected_execution_exception, reason=rejected execution of processing of [39496755349][indices:data/write/bulk[s][p]]: request: BulkShardRequest [[xxxx][8]] containing [7] requests, target allocation id: xxxx, primary term: 6 on EsThreadPoolExecutor[name = es-cn-xxxxx/write, queue capacity = 200, org.elasticsearch.common.util.concurrent.EsThreadPoolExecutor@f1fddde[Running, pool size = 16, active threads = 16, queued tasks = 200, completed tasks = 16113082956]]]  

默认thread_pool.write.queue_size 是200
调整到500 程序报错消失

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值