美文网首页
十四 hugegraph Raft 高可用

十四 hugegraph Raft 高可用

作者: NazgulSun | 来源:发表于2021-03-17 18:58 被阅读0次

Raft 高可用

hugegraph 的开源版本是单机sever
使用内存缓存了schema 和节点相关的信息,
所以,在做 hugegraph server 的高可用性的时候存在问题。
比如,使用nigix 后面接多个 hugegraph server, 然后连接一个 cassandra 集群。
这种方式,如果只读的场景没有问题, 但是如果有线上写入就会有 数据一致性的问题。

关闭缓存功能,是一个临时的HA方案。

0.11版本的 Raft 协议

Raft 是一个一致性协议,核心原理,对于 hugegraph 任何的一个 写操作,
都交给 Raft, Raft 通过Leader,把数据写入到 Follower,并在协议上保证一致性。

Hugegraph 使用Raft来保证缓存一致性, 对于任何一个 写的操作被apply的时候,都会
调用 updateCache方法来更新节点的缓存。

    @Override
    public void onApply(Iterator iter) {
        LOG.debug("Node role: {}", this.node().selfIsLeader() ?
                                   "leader" : "follower");
        StoreClosure closure = null;
        try {
            while (iter.hasNext()) {
                closure = (StoreClosure) iter.done();
                if (closure != null) {
                    // Leader just take it out from the closure
                    StoreCommand command = closure.command();
                    BytesBuffer buffer = BytesBuffer.wrap(command.data());
                    // The first two bytes are StoreType and StoreAction
                    StoreType type = StoreType.valueOf(buffer.read());
                    StoreAction action = StoreAction.valueOf(buffer.read());
                    boolean forwarded = command.forwarded();
                    // Let the producer thread to handle it
                    closure.complete(Status.OK(), () -> {
                        this.applyCommand(type, action, buffer, forwarded);
                        return null;
                    });
                } else {

this.applyCommand()方法:

    private void applyCommand(StoreType type, StoreAction action,
                              BytesBuffer buffer, boolean forwarded) {
        BackendStore store = type != StoreType.ALL ? this.store(type) : null;
        switch (action) {
            case CLEAR:
                boolean clearSpace = buffer.read() > 0;
                store.clear(clearSpace);
                this.context.clearCache();
                break;
            case TRUNCATE:
                store.truncate();
                this.context.clearCache();
                break;
            case SNAPSHOT:
                assert store == null;
                this.node().snapshot();
                break;
            case BEGIN_TX:
                store.beginTx();
                break;
            case COMMIT_TX:
                List<BackendMutation> ms = StoreSerializer.readMutations(buffer);
                // RaftBackendStore doesn't write raft log for beginTx
                store.beginTx();
                for (BackendMutation mutation : ms) {
                    store.mutate(mutation);
                    this.updateCacheIfNeeded(mutation, forwarded);
                }
                store.commitTx();
                break;
            case ROLLBACK_TX:

在 COMMIT的时候,会调用
this.updateCacheIfNeeded(mutation, forwarded);负责每台机器上的更新


    private void updateCacheIfNeeded(BackendMutation mutation,
                                     boolean forwarded) {
        // Update cache only when graph run in general mode
        if (this.context.graphMode() != GraphMode.NONE) {
            return;
        }
        /*
         * 1. Follower need to update cache from store to tx
         * 2. If request come from leader, cache will be updated by upper layer
         * 3. If request is forwarded by follower, need to update cache
         */
        if (!forwarded && this.node().selfIsLeader()) {
            return;
        }
        for (HugeType type : mutation.types()) {
            if (!type.isGraph() && !type.isSchema()) {
                continue;
            }
            for (java.util.Iterator<BackendAction> it = mutation.mutation(type);
                 it.hasNext();) {
                BackendEntry entry = it.next().entry();
                this.context.notifyCache(ACTION_INVALID, type, entry.originId());
            }
        }
    }
RAFT 协议 + rocket存储

官方推荐使用 Rocketdb,这本身是一个单机数据库。
而nebula的存储同样也是raft+rocketdb, 应该性能是比较有保证的。
其实,我们同样可以使用cassanda,mysql 这样的存储,这不过他们本身就有集群功能,把他们当作单节点来用,显得不伦不类。

那么Raft 协议是怎么来管理集群和partition的呢。是通过 group 来管理的。原则上一个group 为一个partion+replics。

假设有 9台,机器,我们想做三个副本。
那么可以分为 3个group, 每个group 3个raft 节点。 将数据将会被均匀的分布在在3个 group中,就可以达到partition+replics 的效果。

相关文章

  • 十四 hugegraph Raft 高可用

    Raft 高可用 hugegraph 的开源版本是单机sever使用内存缓存了schema 和节点相关的信息,所以...

  • Raft协议

    Raft概述 Raft 是工程上使用较为广泛的 强一致性、去中心化、高可用 的分布式协议,用于管理副本复制(Log...

  • 在 Rust 中使用 Raft

    自从 Raft 一致性算法提出以来,越来越多的分布式应用开始基于 Raft 来构造自己的高可用服务,包括我们的分布...

  • 分布式数据库--ETCD(一)

    一、ETCD简介 ETCD 是一个高可用的分布式键值数据库,可用于服务发现。ETCD 采用 raft 一致性算法,...

  • Hyperledger Fabric(高可用之Raft部署)

    Raft共识在1.4.1版本时正式支持,本次基于1.4.4版本部署Raft版的Fabric网络。由于Raft共识集...

  • k8s学习笔记之etcd

    etcd简介 etcd是一个高可用的分布式键值(key-value)数据库。etcd内部采用raft协议作为一致性...

  • Raft 协议

    Raft 是工程上使用较为广泛的强一致性、去中心化、高可用的分布式协议,用于管理副本复制(Log Replicat...

  • ubuntu下etcd的安装部署

    一、简介 etcd是一个高可用的分布式键值(key-value)数据库。etcd内部采用raft协议作为一致性算法...

  • hashicorp raft 源码学习 - 1

    开始准备阅读 raft 源码, 目的 分布式看了那么多概念,究竟怎么实现,真的就非常高可用,绝对一致性吗,从编码的...

  • ​Etcd源码剖析(一)

    简介 etcd是一个高可用的分布式键值(key-value)数据库。etcd内部采用raft协议作为一致性算法,e...

网友评论

      本文标题:十四 hugegraph Raft 高可用

      本文链接:https://www.haomeiwen.com/subject/vciecltx.html