我有点怀疑你在刷屏

聊聊ELASTICSEARCH的集群状态的管理和维护

Elasticsearchvearne 发表了文章 • 2 个评论 • 6822 次浏览 • 2018-07-12 22:49 • 来自相关话题

注意 本文参考的ES 5.0.1的源码

1. 查看集群状态

可以通过API查看集群状态
<br /> GET /_cluster/state<br />
大致可以得到如下内容
![cluster_state](https://ut-bucket01.sh1a.qings ... 7.jpeg)
version 集群状态数字版本号
每次更新version + 1
集群重启version不会置0(只会单调增)

state_uuid 是集群状态字符串版本号(UUID)

master_node master节点

nodes 该版本中的所有节点信息

routing_tablerouting_nodes 都是描述不同index上的shard在node上的分布关系


2. 集群状态的维护

可以阅读
.//core/src/main/java/org/elasticsearch/cluster/ClusterState.java

  • 2.1 ClusterState是不可变对象,每次状态变更都会产生新的ClusterState,它们拥有不同的版本号
  • 2.2 在ES中, 集群状态由Master维护,并且只能由master节点更新集群状态
  • 2.3 更新完成后,Master会把新版本的集群状态推送给集群的其它所有节点
    对于publish,Master节点会根据它已知其它节点所拥有的集群状态版本,决定是执行
    sendFullClusterState()还是sendClusterStateDiff(),  前者是全量推送,后者增量推送。

  • 2.4 对于使用Zen Discovery的情况,只要有minimumMasterNodes响应了Master节点的publish消息, 那么这次的commit就算成功


      The cluster state can be updated only on the master node. All updates are performed by on a single thread and controlled by the {@link ClusterService}. After every update the
    {@link Discovery#publish} method publishes new version of the cluster state to all other nodes in the cluster.  
    In the Zen Discovery it is handled in the {@link PublishClusterStateAction#publish} method

  • 2.5 另外需要补充的是Elasticsearch使用Gossip + Bully算法进行选主。Bully算法在具体实现中,不是简单选取节点ID小的节点, 首先要先比较ClusterState的版本。版本高的优先当选。(各个版本实现有不同,但都需要考虑候选节点的集群状态版本)
    <br />     /**<br />      * Elects a new master out of the possible nodes, returning it. Returns <tt>null</tt><br />      * if no master has been elected.<br />      */<br />     public MasterCandidate electMaster(Collection<MasterCandidate> candidates) {<br />         assert hasEnoughCandidates(candidates);<br />         List<MasterCandidate> sortedCandidates = new ArrayList<>(candidates);<br />         sortedCandidates.sort(MasterCandidate::compare);<br />         return sortedCandidates.get(0);<br />     }<br />
    <br />         /**<br />          * compares two candidates to indicate which the a better master.<br />          * A higher cluster state version is better<br />          *<br />          * @return -1 if c1 is a batter candidate, 1 if c2.<br />          */<br />         public static int compare(MasterCandidate c1, MasterCandidate c2) {<br />             // we explicitly swap c1 and c2 here. the code expects "better" is lower in a sorted<br />             // list, so if c2 has a higher cluster state version, it needs to come first.<br />             int ret = Long.compare(c2.clusterStateVersion, c1.clusterStateVersion);<br />             if (ret == 0) {<br />                 ret = compareNodes(c1.getNode(), c2.getNode());<br />             }<br />             return ret;<br />         }<br />     }<br />

    参考资料

    1. [cluster-state](https://www.elastic.co/guide/e ... e.html)



      欢迎光临我的个人博客 萌叔 | http://vearne.cc

多个 logstash 并发数据给 ES集群,如何实现数据的负载均衡?

Logstashrochy 回复了问题 • 4 人关注 • 2 个回复 • 9797 次浏览 • 2018-07-13 10:49 • 来自相关话题

elasticsearch可以匹配一个字段的全部内容,不分词么?由于params json有很多json格式,导致logstash进入es报下面错

Elasticsearchxinfanwang 回复了问题 • 5 人关注 • 7 个回复 • 5590 次浏览 • 2018-07-30 10:00 • 来自相关话题

logstash 并联可以实现 负载均衡,发送数据到ES集群吗?

Logstashrochy 回复了问题 • 3 人关注 • 1 个回复 • 5903 次浏览 • 2018-07-13 10:58 • 来自相关话题

prefix query 特殊字符问题

ElasticsearchelasticStack 回复了问题 • 3 人关注 • 1 个回复 • 6153 次浏览 • 2018-07-25 18:32 • 来自相关话题

logstash如何判断数据类型

Logstashlbx6z 回复了问题 • 4 人关注 • 3 个回复 • 6413 次浏览 • 2020-03-09 11:22 • 来自相关话题

es 查询:_search 可以看到的数据 通过 index/type/id 却查不到

Elasticsearchyayg2008 回复了问题 • 5 人关注 • 3 个回复 • 24605 次浏览 • 2018-07-13 11:55 • 来自相关话题

es如何定位shard在那个节点?

Elasticsearchvearne 回复了问题 • 6 人关注 • 4 个回复 • 3112 次浏览 • 2018-07-13 14:18 • 来自相关话题

googleplay分成google play,什么分词器支持呢?

Elasticsearchrochy 回复了问题 • 2 人关注 • 1 个回复 • 2854 次浏览 • 2018-07-13 11:02 • 来自相关话题

社区日报 第330期 (2018-07-12)

社区日报白衬衣 发表了文章 • 0 个评论 • 1666 次浏览 • 2018-07-12 15:23 • 来自相关话题

1.携程酒店订单Elastic Search实战
http://t.cn/Rdumu4V
2.如何使用Kafka Streams构建广告消耗预测系统
http://t.cn/RlDKqWa
3.请自查!这些优秀日志实践准则,你做到了几点?
http://t.cn/Rldk5Ku

活动预告
1. 7月21日上海meetup倒计时
https://elasticsearch.cn/m/article/655 
2. Elastic 中国开发者大会 2018 ,开始接受演讲申请和赞助合作
https://conf.elasticsearch.cn/2018/shenzhen.html 
 
编辑:金桥
归档:https://elasticsearch.cn/article/710
订阅:https://tinyletter.com/elastic-daily

Transport Client问题

回复

Elasticsearchredhat 发起了问题 • 1 人关注 • 0 个回复 • 1570 次浏览 • 2018-07-12 14:39 • 来自相关话题

elasticsearch集群日志存储问题

Elasticsearchcode4j 回复了问题 • 3 人关注 • 1 个回复 • 4753 次浏览 • 2018-07-13 11:51 • 来自相关话题

请教个关于es里面如何是如何对数字、字母进行查询的,分词是如何分的?

ElasticsearchelasticStack 回复了问题 • 2 人关注 • 1 个回复 • 8364 次浏览 • 2018-07-25 18:34 • 来自相关话题

搜索报错:Failed to execute phase [merge], [reduce] ; shardFailures

Elasticsearchmdyzsz91 回复了问题 • 2 人关注 • 2 个回复 • 3224 次浏览 • 2018-07-24 22:33 • 来自相关话题

为什么Elastic.co 上的实例在不停的迁移

Elasticsearchmedcl 回复了问题 • 2 人关注 • 1 个回复 • 1779 次浏览 • 2018-07-12 16:41 • 来自相关话题