要不要也来分享分享一下啊

es底层lucene保证indexwrite和translog的数据一致性,为什么还要采用2PC机制?

Elasticsearch | 作者 jianjianhe | 发布于2018年06月29日 | 阅读数:3269

通过es的lucene源码中看到有一个TwoPhaseCommit接口,接口的实现类有两个:IndexWrite和Translog,
public interface TwoPhaseCommit {

/**
* The first stage of a 2-phase commit. Implementations should do as much work
* as possible in this method, but avoid actual committing changes. If the
* 2-phase commit fails, {@link #rollback()} is called to discard all changes
* since last successful commit.
*/
public long prepareCommit() throws IOException;

/**
* The second phase of a 2-phase commit. Implementations should ideally do
* very little work in this method (following {@link #prepareCommit()}, and
* after it returns, the caller can assume that the changes were successfully
* committed to the underlying storage.
*/
public long commit() throws IOException;

/**
* Discards any changes that have occurred since the last commit. In a 2-phase
* commit algorithm, where one of the objects failed to {@link #commit()} or
* {@link #prepareCommit()}, this method is used to roll all other objects
* back to their previous state.
*/
public void rollback() throws IOException;
}
我记得es的一致性性是采用类似raft协议的选举算法实现的,为什么这里还要用的两段式提交,2PC效率低,因为是同步阻塞,必须全部成功,
已邀请:

ScriptShi

赞同来自:

es的一致性不是raft的算法。比较类似,但是还有些区别。
 
 
 


与使用raft相比

raft算法是近几年很火的一个分布式一致性算法,其实现相比paxos简单,在各种分布式系统中也得到了应用。这里不再描述其算法的细节,我们单从master选举算法角度,比较一下raft与ES目前选举算法的异同点:

相同点

多数派原则:必须得到超过半数的选票才能成为master。
选出的leader一定拥有最新已提交数据:在raft中,数据更新的节点不会给数据旧的节点投选票,而当选需要多数派的选票,则当选人一定有最新已提交数据。在es中,version大的节点排序优先级高,同样用于保证这一点。

不同点

正确性论证:raft是一个被论证过正确性的算法,而ES的算法是一个没有经过论证的算法,只能在实践中发现问题,做bug fix,这是我认为最大的不同。
是否有选举周期term:raft引入了选举周期的概念,每轮选举term加1,保证了在同一个term下每个参与人只能投1票。ES在选举时没有term的概念,不能保证每轮每个节点只投一票。
选举的倾向性:raft中只要一个节点拥有最新的已提交的数据,则有机会选举成为master。在ES中,version相同时会按照NodeId排序,总是NodeId小的人优先级高。

看法

raft从正确性上看肯定是更好的选择,而ES的选举算法经过几次bug fix也越来越像raft。当然,在ES最早开发时还没有raft,而未来ES如果继续沿着这个方向走很可能最终就变成一个raft实现。

raft不仅仅是选举,下一篇介绍meta数据一致性时也会继续比较ES目前的实现与raft的异同。


参考来源:https://zhuanlan.zhihu.com/p/34830403

ScriptShi

赞同来自:

Es是分布式的协议,Lucene是单机的。2pc的目的和lucene不一样吧。 lucene能保证我底层的数据一致性,但是es的2pc能保证多台机器之间的数据同步的一致性

code4j - coder github: https://github.com/rpgmakervx

赞同来自:

elasticsearch不是用的bully选举算法么,好像是nodeId大的获得master角色。并不是raft吧(对raft并不是很了解)

要回复问题请先登录注册