《阿里云产品四月刊》—得物 ZooKeeper SLA 也可以 99.99%丨最佳实践(6)https://developer.aliyun.com/article/1554135
WatchManagerOptimized 使用 BitMap 来存储所有的 Watcher。这样即便是存在1W 的 Watcher 。位图的内存消耗也只有 8Byte*1W/64/1024=1.2KB 。如果换成HashSet ,则至少需要 32Byte*10000/1024=305KB,存储效率相差近 300 倍。
WatchManager.java:private final Map<String, Set<Watcher>> watchTable = new HashMap<>();private final Map<Watcher, Set<String>> watch2Paths = new HashMap<>(); WatchManagerOptimized.java:private final ConcurrentHashMap<String, BitHashSet> pathWatches = new ConcurrentHashMap<String, BitHashSet>();private final BitMap<Watcher> watcherBitIdMap = new BitMap<Watcher>();
ZNode 到 Watcher 的映射存储,由 Map 换成了 ConcurrentHashMapBitHashSet>。也就是说不再存储 Set,而是用位图来存储位图索引值。
用 1W 的 ZNode,1W 的 Watcher,极端点走全订阅(所有的 Watcher 订阅所有的 ZNode),做存储效率 PK:
可以看到 11.7MB PK 5.9GB,内存的存储效率相差:516 倍。
《阿里云产品四月刊》—得物 ZooKeeper SLA 也可以 99.99%丨最佳实践(8)https://developer.aliyun.com/article/1554133