博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
QuorumPeer
阅读量:5021 次
发布时间:2019-06-12

本文共 5608 字,大约阅读时间需要 18 分钟。

QuorumPeer是zookeeper执行同步,选主过程的线程

主要逻辑:

  1.当自己处于 LOOKING状态:  执行lookForLeader()

  2.当自己处于OBSERVING状态: 建立一个Observer服务和leader进行同步

  3.当自己处于LEADING时: 建立一个Leader服务,处理peer的请求. 

  4.当自己处于FOLLOWING时:建立一个Follwer服务和leader执行同步

 

@Override    public void run() {        setName("QuorumPeer" + "[myid=" + getId() + "]" +                cnxnFactory.getLocalAddress());       ......try {            /*             * Main loop             */            while (running) {                switch (getPeerState()) {                case LOOKING:                    LOG.info("LOOKING");                    if (Boolean.getBoolean("readonlymode.enabled")) {                        LOG.info("Attempting to start ReadOnlyZooKeeperServer");                        // Create read-only server but don't start it immediately                        final ReadOnlyZooKeeperServer roZk = new ReadOnlyZooKeeperServer(                                logFactory, this,                                new ZooKeeperServer.BasicDataTreeBuilder(),                                this.zkDb);                            // Instead of starting roZk immediately, wait some grace                        // period before we decide we're partitioned.                        //                        // Thread is used here because otherwise it would require                        // changes in each of election strategy classes which is                        // unnecessary code coupling.                        Thread roZkMgr = new Thread() {                            public void run() {                                try {                                    // lower-bound grace period to 2 secs                                    sleep(Math.max(2000, tickTime));                                    if (ServerState.LOOKING.equals(getPeerState())) {                                        roZk.startup();                                    }                                } catch (InterruptedException e) {                                    LOG.info("Interrupted while attempting to start ReadOnlyZooKeeperServer, not started");                                } catch (Exception e) {                                    LOG.error("FAILED to start ReadOnlyZooKeeperServer", e);                                }                            }                        };                        try {                            roZkMgr.start();                            setBCVote(null);                            setCurrentVote(makeLEStrategy().lookForLeader());                        } catch (Exception e) {                            LOG.warn("Unexpected exception",e);                            setPeerState(ServerState.LOOKING);                        } finally {                            // If the thread is in the the grace period, interrupt                            // to come out of waiting.                            roZkMgr.interrupt();                            roZk.shutdown();                        }                    } else {                        try {                            setBCVote(null);                            setCurrentVote(makeLEStrategy().lookForLeader());                        } catch (Exception e) {                            LOG.warn("Unexpected exception", e);                            setPeerState(ServerState.LOOKING);                        }                    }                    break;                case OBSERVING: //建立obser服务                    try {                        LOG.info("OBSERVING");                        setObserver(makeObserver(logFactory));                        observer.observeLeader();                    } catch (Exception e) {                        LOG.warn("Unexpected exception",e );                                            } finally {                        observer.shutdown();                        setObserver(null);                        setPeerState(ServerState.LOOKING);                    }                    break;                case FOLLOWING: //建立 Follower服务                    try {                        LOG.info("FOLLOWING");                        setFollower(makeFollower(logFactory));                        follower.followLeader();                    } catch (Exception e) {                        LOG.warn("Unexpected exception",e);                    } finally {                        follower.shutdown();                        setFollower(null);                        setPeerState(ServerState.LOOKING);                    }                    break;                case LEADING:  //建立 Leader服务                    LOG.info("LEADING");                    try {                        setLeader(makeLeader(logFactory));                        leader.lead();                        setLeader(null);                    } catch (Exception e) {                        LOG.warn("Unexpected exception",e);                    } finally {                        if (leader != null) {                            leader.shutdown("Forcing shutdown");                            setLeader(null);                        }                        setPeerState(ServerState.LOOKING);                    }                    break;                }            }        } finally {            LOG.warn("QuorumPeer main thread exited");            try {                MBeanRegistry.getInstance().unregisterAll();            } catch (Exception e) {                LOG.warn("Failed to unregister with JMX", e);            }            jmxQuorumBean = null;            jmxLocalPeerBean = null;        }    }

 

转载于:https://www.cnblogs.com/ironroot/p/7403897.html

你可能感兴趣的文章
android 插件形式运行未安装apk
查看>>
ios开发之 manage the concurrency with NSOperation
查看>>
Android权限 uses-permission
查看>>
NSEnumerator用法小结
查看>>
vim如何配置go语言环境
查看>>
机器学习好网站
查看>>
python 中的 sys , os 模块用法总结
查看>>
解题:国家集训队 Middle
查看>>
响应者链
查看>>
指针从函数内部带回返回值
查看>>
在使用webView播放flash或视频文件时无法关闭声音的问题
查看>>
redhat 7 源码安装 mysql5.5.49
查看>>
CCP浅谈
查看>>
NAT虚拟网络配置
查看>>
c#部分---需要实例化的内容;
查看>>
销售类
查看>>
技术项目,问题
查看>>
线程池总结
查看>>
Learning to rank (software, datasets)
查看>>
git常见问题
查看>>