ZK

zk的watcher模型(下)

孤者浪人 提交于 2019-11-30 13:32:29
watcher在zk server中实现 WatcherManager存储管理 属性: private final Map<String, Set<Watcher>> watchTable = new HashMap<String, Set<Watcher>>(); 路径和watcher的对应关系 private final Map<Watcher, Set<String>> watch2Paths = new HashMap<Watcher, Set<String>>(); watcher和路径集合的对应关系 方法: 使用synchronized同步方法,不够灵活,线程不能中断,效率不够高 public synchronized int size() { int result = 0; for (Set<Watcher> watches : watchTable.values()) { result += watches.size(); } return result; } 添加watcher @Override public synchronized boolean addWatch(String path, Watcher watcher) { if (isDeadWatcher(watcher)) { LOG.debug("Ignoring addWatch with

zk的watch模型

牧云@^-^@ 提交于 2019-11-30 13:22:19
WatchManager中watch机制 watcher作用:通知状态与事件类型 接口定义了process方法,定义了Event接口,包含KeeperState(通知时zk状态),EventType说明event类型 void process(WatchedEvent event);回调函数实现该函数,表示该event执行行为相关逻辑 WatchedEvent 描述zk检测到的状态变化事件,回应变化,发生了什么,zk当前状态,发生事件znode的路径 /**将属性转换成网络传输的WatcherEvent * Convert WatchedEvent to type that can be sent over network */ public WatcherEvent getWrapper() { return new WatcherEvent(eventType.getIntValue(), keeperState.getIntValue(), path); } WatcherEvent 是一个接口,实现了Record,属性有type,state,path,是网络传输的封装 ClientWatchManager 接口 根据event得到需要通知的watcher集合,实现类为ZKWatcherManager 属性: private final Map<String, Set

zk的树形数据模型

落花浮王杯 提交于 2019-11-30 13:15:09
zookeeper的数据模型是树结构 在内存数据库中存储了整颗树内容,包含所有节点路径,节点数据,状态信息 会定时将数据刷到磁盘上 数据模型表示主要有DataTree DataNode ZKDatabase datatree是一颗树结构,不包含和客户端,网络,请求相关的业务逻辑 dataNode是数据存储的最小单元,保存了节点数据内容,ACL列表,状态,父节点引用和子节点列表 zkdatabase是内存数据库,管理zk所有会话,DataTree存储和事务日志,定时将日志刷写到磁盘 在zookeeper启动时,会通过磁盘的事务日志和快照文件恢复一个完整的内存数据库 来源: https://my.oschina.net/iioschina/blog/3111282

zk的CreateNode

流过昼夜 提交于 2019-11-30 12:25:12
节点类型 /** * The znode will not be automatically deleted upon client's disconnect. * 客户端关闭后znode才会删除 */ PERSISTENT(0, false, false, false, false), /** * znode的名字添加一个单调递增的数字 * The znode will not be automatically deleted upon client's disconnect, * and its name will be appended with a monotonically increasing number. */ PERSISTENT_SEQUENTIAL(2, false, true, false, false), /** * The znode will be deleted upon the client's disconnect. */ EPHEMERAL(1, true, false, false, false), /** * The znode will be deleted upon the client's disconnect, and its name * will be appended with a monotonically

zk中的数据模型DataNode

有些话、适合烂在心里 提交于 2019-11-30 12:21:44
dataNode是数据存储的最小单元,保存节点数据内容,ACL控制列表,节点状态,父节点引用和子节点列表,同时也提供了子节点列表更新的方法 DataNode 属性 //节点签名,通过路径,数据,状态计算的long型数据 // the digest value of this node, calculated from path, data and stat private volatile long digest; // indicate if the digest of this node is up to date or not, used to // optimize the performance. volatile boolean digestCached; //字节数组存储数据 /** the data for this datanode */ byte[] data; /**acl long标识 * the acl map long for this datanode. the datatree has the map */ Long acl; /**datanode持久化的状态表示 * the stat for this node that is persisted to disk. */ public StatPersisted stat; /**子节点列表 *

zk中的Helper Class FileTxnSnapLog

三世轮回 提交于 2019-11-30 12:21:01
这个类是FileTxnLog和FileSnap的包装类 属性 //the directory containing the //the transaction logs final File dataDir; //the directory containing the //the snapshot directory final File snapDir; TxnLog txnLog; SnapShot snapLog; private final boolean autoCreateDB; private final boolean trustEmptySnapshot; public static final int VERSION = 2; public static final String version = "version-"; private static final Logger LOG = LoggerFactory.getLogger(FileTxnSnapLog.class); public static final String ZOOKEEPER_DATADIR_AUTOCREATE = "zookeeper.datadir.autocreate"; public static final String ZOOKEEPER_DATADIR

zk中的文件快照

萝らか妹 提交于 2019-11-30 12:07:16
zk中的数据在内存中是DataTree为数据结构存储的 快照是间隔时间段将整个DataTree持久化 快照是备份版本,所以并不是最新数据 接口所在位置org.apache.zookeeper.server.persistence ** * snapshot interface for the persistence layer. * implement this interface for implementing * snapshots. */ public interface SnapShot { /** * 将数据反序列化 * deserialize a data tree from the last valid snapshot and * return the last zxid that was deserialized * @param dt the datatree to be deserialized into * @param sessions the sessions to be deserialized into * @return the last zxid that was deserialized from the snapshot * @throws IOException */ long deserialize(DataTree dt, Map

zk中的序列化

寵の児 提交于 2019-11-30 12:04:14
网络传输和数据存储中都离不开序列化 使用的是jute,性能更强的有 Apache Avro ,thrift,protobuf等序列化组件 序列化所在模块org.apache.jute compiler下面有 外层有接口 Index 是用来做什么的? InputArchive 实现类BinaryInputArchive OutputArchive 实现类 BrinaryOutputArchive ToStringOutputArchive Record 外层有接口 Index 是做什么用的? 来源: https://my.oschina.net/iioschina/blog/3110648

zk中的事务日志

僤鯓⒐⒋嵵緔 提交于 2019-11-30 11:58:11
zk事务日志文件用来标记事务操作,每一个事务操作如添加,删除节点等等 都会在事务日志中记录一条记录,用来在zookeeper异常情况下恢复数据 public interface TxnLog extends Closeable { /** * Setter for ServerStats to monitor fsync threshold exceed * 设置服务状态 * @param serverStats used to update fsyncThresholdExceedCount */ void setServerStats(ServerStats serverStats); /** * roll the current * 将前添加日志滚动 * log being appended to * @throws IOException */ void rollLog() throws IOException; /** * Append a request to the transaction log * 添加请求 * @param hdr the transaction header * @param r the transaction itself * @return true iff something appended, otw false * @throws

zk- org.apache.zookeeper.KeeperException$NoAuthException: KeeperErrorCode = NoAuth for

删除回忆录丶 提交于 2019-11-30 10:24:32
项目服务都是发布在dubbo+zookeeper上 昨天项目报这个错。 org.apache.zookeeper.KeeperException$NoAuthException: KeeperErrorCode = NoAuth for 查了一下,是说权限不对。但是项目里连接dubbo并没有密码。 启动 zkCli.sh 看了一下根目录, 发现看其他目录都好好的,可以查看 看 /dubbo 这个目录 就查看不了,权限有问题。 /dubbo 这个目录就是项目发布服务的目录。 Authentication is not valid 可能是因为之前有个同事把这台服务加入了大数据集群。 get rmr dele不了。最后是启用了超级账户进行删除的。 使用超级用户删除带权限的节点 1.使用org.apache.zookeeper.server.auth.DigestAuthenticationProvider生成superDigest: try { System.out.println(DigestAuthenticationProvider.generateDigest("super:superpw")); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(