zk中ZooKeeperServer的作用是什么
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,zk中ZooKeeperServer的作用是什么,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。内部类ChangeRecord 处理P
千家信息网最后更新 2026年11月03日zk中ZooKeeperServer的作用是什么
zk中ZooKeeperServer的作用是什么,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
内部类
ChangeRecord 处理PrepRP和FinalRP之间的信息
static class ChangeRecord { ChangeRecord(long zxid, String path, StatPersisted stat, int childCount, List acl) { this.zxid = zxid; this.path = path; this.stat = stat; this.childCount = childCount; this.acl = acl; } long zxid; String path; StatPersisted stat; /* Make sure to create a new object when changing */ int childCount; List acl; /* Make sure to create a new object when changing */ ChangeRecord duplicate(long zxid) { StatPersisted stat = new StatPersisted(); if (this.stat != null) { DataTree.copyStatPersisted(this.stat, stat); } return new ChangeRecord(zxid, path, stat, childCount, acl == null ? new ArrayList<>() : new ArrayList<>(acl)); }}protected enum State { INITIAL, RUNNING, SHUTDOWN, ERROR}初始化函数public ZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime, int minSessionTimeout, int maxSessionTimeout, int clientPortListenBacklog, ZKDatabase zkDb, String initialConfig) { serverStats = new ServerStats(this); this.txnLogFactory = txnLogFactory; this.txnLogFactory.setServerStats(this.serverStats); this.zkDb = zkDb; this.tickTime = tickTime; setMinSessionTimeout(minSessionTimeout); setMaxSessionTimeout(maxSessionTimeout); this.listenBacklog = clientPortListenBacklog; listener = new ZooKeeperServerListenerImpl(this); readResponseCache = new ResponseCache(); connThrottle = new BlueThrottle(); this.initialConfig = initialConfig; this.requestPathMetricsCollector = new RequestPathMetricsCollector(); this.initLargeRequestThrottlingSettings(); LOG.info("Created server with tickTime " + tickTime + " minSessionTimeout " + getMinSessionTimeout() + " maxSessionTimeout " + getMaxSessionTimeout() + " clientPortListenBacklog " + getClientPortListenBacklog() + " datadir " + txnLogFactory.getDataDir() + " snapdir " + txnLogFactory.getSnapDir());} 通过参数构造一个数据管理Log FileTxnSnapLog
public ZooKeeperServer(File snapDir, File logDir, int tickTime) throws IOException { this(new FileTxnSnapLog(snapDir, logDir), tickTime, "");}集群和单机中加载数据
集群调用顺序
Leader#lead
ZooKeeperServer#loadData
单机调用顺序
ServerCnxFactory#startUp
ZooKeeperServer#startdata
ZooKeeperServer#loadData
单机版的startData方法
public void startdata() throws IOException, InterruptedException { //check to see if zkDb is not null if (zkDb == null) { zkDb = new ZKDatabase(this.txnLogFactory);//实例化zkdatabase } if (!zkDb.isInitialized()) { loadData();//没有初始化就重新初始化 }}//加载数据public void loadData() throws IOException, InterruptedException { /* * When a new leader starts executing Leader#lead, it * invokes this method. The database, however, has been * initialized before running leader election so that * the server could pick its zxid for its initial vote. * It does it by invoking QuorumPeer#getLastLoggedZxid. * Consequently, we don't need to initialize it once more * and avoid the penalty of loading it a second time. Not * reloading it is particularly important for applications * that host a large database. * * The following if block checks whether the database has * been initialized or not. Note that this method is * invoked by at least one other method: * ZooKeeperServer#startdata */ //加载信息 if (zkDb.isInitialized()) { setZxid(zkDb.getDataTreeLastProcessedZxid()); } else { setZxid(zkDb.loadDataBase()); } // Clean up dead sessions //获取超时deadSessions List deadSessions = new ArrayList<>(); for (Long session : zkDb.getSessions()) { if (zkDb.getSessionWithTimeOuts().get(session) == null) { deadSessions.add(session); } } //杀掉session for (long session : deadSessions) { // TODO: Is lastProcessedZxid really the best thing to use? killSession(session, zkDb.getDataTreeLastProcessedZxid()); } // Make a clean snapshot 创建快照 takeSnapshot();}删除会话protected void killSession(long sessionId, long zxid) { zkDb.killSession(sessionId, zxid); if (LOG.isTraceEnabled()) { ZooTrace.logTraceMessage( LOG, ZooTrace.SESSION_TRACE_MASK, "ZooKeeperServer --- killSession: 0x" + Long.toHexString(sessionId)); } if (sessionTracker != null) { sessionTracker.removeSession(sessionId); }}public synchronized void startup() { if (sessionTracker == null) { createSessionTracker(); } //责任链处理 startSessionTracker(); //设置请求处理器 setupRequestProcessors(); startRequestThrottler(); //注册jmx registerJMX(); startJvmPauseMonitor(); registerMetrics(); //设置状态 setState(State.RUNNING); requestPathMetricsCollector.start(); localSessionEnabled = sessionTracker.isLocalSessionsEnabled(); notifyAll();} 关于zk中ZooKeeperServer的作用是什么问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。
单机
数据
问题
处理
作用
信息
方法
更多
集群
顺序
帮助
解答
易行
简单易行
之间
内容
函数
单机版
参数
处理器
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网高科技
深圳恒合互联网络科技有限公司
关于新基建和互联网科技
中资互联网科技
苏州互联网科技贷款公司
时间互联网络科技
顺玮阁互联网科技
深圳期待互联网科技
广西拼得多互联网科技有限公司
创业天下互联网科技有限公司
互联网企业和科技型企业
深圳市优智互联网科技有限公司
互联网科技创新的路径
为客找房互联网科技公司怎么样
互联网科技企业商标注意事项
互联网时代高科技布局
现代互联网科技公司
现代科技的标志之一的互联网
互联网科技创业有什么好项目
互联网科技快速发展优美句子
深圳市人人互联网科技有限公司
科技股互联网
工业互联网科技新基建
济南和联互联网科技有限公司
中国科技互联网公司收入
苏州铭阳互联网科技有限公司
天津腾达互联网科技有限公司
风投北京互联网科技
郑州欣桑互联网科技有限公司
中国互联网公司的科技投入