From dev-return-73664-archive-asf-public=cust-asf.ponee.io@zookeeper.apache.org Tue Sep 25 05:25:30 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id C80C6180649 for ; Tue, 25 Sep 2018 05:25:29 +0200 (CEST) Received: (qmail 80544 invoked by uid 500); 25 Sep 2018 03:25:28 -0000 Mailing-List: contact dev-help@zookeeper.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@zookeeper.apache.org Delivered-To: mailing list dev@zookeeper.apache.org Received: (qmail 80524 invoked by uid 99); 25 Sep 2018 03:25:27 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 25 Sep 2018 03:25:27 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 6193AE0016; Tue, 25 Sep 2018 03:25:27 +0000 (UTC) From: lvfangmin To: dev@zookeeper.apache.org Reply-To: dev@zookeeper.apache.org References: In-Reply-To: Subject: [GitHub] zookeeper pull request #590: [ZOOKEEPER-1177] Add the memory optimized watch... Content-Type: text/plain Message-Id: <20180925032527.6193AE0016@git1-us-west.apache.org> Date: Tue, 25 Sep 2018 03:25:27 +0000 (UTC) Github user lvfangmin commented on a diff in the pull request: https://github.com/apache/zookeeper/pull/590#discussion_r220049442 --- Diff: src/java/main/org/apache/zookeeper/server/watch/WatchManagerOptimized.java --- @@ -0,0 +1,355 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.zookeeper.server.watch; + +import java.io.PrintWriter; +import java.util.HashMap; +import java.util.BitSet; +import java.util.HashSet; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Random; +import java.util.Set; +import java.util.Iterator; +import java.lang.Iterable; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.util.BitMap; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Optimized in memory and time complexity, compared to WatchManager, both the + * memory consumption and time complexity improved a lot, but it cannot + * efficiently remove the watcher when the session or socket is closed, for + * majority usecase this is not a problem. + * + * Changed made compared to WatchManager: + * + * - Use HashSet and BitSet to store the watchers to find a balance between + * memory usage and time complexity + * - Use ReadWriteLock instead of synchronized to reduce lock retention + * - Lazily clean up the closed watchers + */ +public class WatchManagerOptimized + implements IWatchManager, DeadWatcherListener { + + private static final Logger LOG = + LoggerFactory.getLogger(WatchManagerOptimized.class); + + private final ConcurrentHashMap pathWatches = + new ConcurrentHashMap(); + + // watcher to bit id mapping + private final BitMap watcherBitIdMap = new BitMap(); + + // used to lazily remove the dead watchers + private final WatcherCleaner watcherCleaner; + + private final ReentrantReadWriteLock addRemovePathRWLock = new ReentrantReadWriteLock(); + + public WatchManagerOptimized() { + watcherCleaner = new WatcherCleaner(this); + watcherCleaner.start(); + } + + @Override + public boolean addWatch(String path, Watcher watcher) { + boolean result = false; + addRemovePathRWLock.readLock().lock(); + try { + // avoid race condition of adding a on flying dead watcher + if (isDeadWatcher(watcher)) { + LOG.debug("Ignoring addWatch with closed cnxn"); + } else { + Integer bit = watcherBitIdMap.add(watcher); + BitHashSet watchers = pathWatches.get(path); + if (watchers == null) { + watchers = new BitHashSet(); + BitHashSet existingWatchers = pathWatches.putIfAbsent(path, watchers); + if (existingWatchers != null) { + watchers = existingWatchers; + } + } + result = watchers.add(bit); + } + } finally { + addRemovePathRWLock.readLock().unlock(); + } + return result; + } + + @Override + public boolean containsWatcher(String path, Watcher watcher) { + BitHashSet watchers = pathWatches.get(path); + if (watchers == null || !watchers.contains(watcherBitIdMap.getBit(watcher))) { + return false; + } + return true; + } + + @Override + public boolean removeWatcher(String path, Watcher watcher) { + addRemovePathRWLock.writeLock().lock(); + try { + BitHashSet list = pathWatches.get(path); + if (list == null || !list.remove(watcherBitIdMap.getBit(watcher))) { + return false; + } + if (list.isEmpty()) { + pathWatches.remove(path); + } + return true; + } finally { + addRemovePathRWLock.writeLock().unlock(); + } + } + + @Override + public void removeWatcher(Watcher watcher) { + Integer watcherBit; + addRemovePathRWLock.writeLock().lock(); + try { + // do nothing if the watcher is not tracked + watcherBit = watcherBitIdMap.getBit(watcher); + if (watcherBit == null) { + return; + } + } finally { + addRemovePathRWLock.writeLock().unlock(); + } + watcherCleaner.addDeadWatcher(watcherBit); + } + + /** + * Entry for WatcherCleaner to remove dead watchers + * + * @param deadWatchers the watchers need to be removed + */ + @Override + public void processDeadWatchers(Set deadWatchers) { + BitSet bits = new BitSet(); + for (int dw: deadWatchers) { + bits.set(dw); + } + // The value iterator will reflect the state when it was + // created, don't need to synchronize. + for (BitHashSet watchers: pathWatches.values()) { + watchers.remove(deadWatchers, bits); + } + // Better to remove the empty path from pathWatches, but it will add + // lot of lock contention and affect the throughput of addWatch, + // let's rely on the triggerWatch to delete it. + for (Integer wbit: deadWatchers) { + watcherBitIdMap.remove(wbit); + } + } + + @Override + public WatcherOrBitSet triggerWatch(String path, EventType type) { + return triggerWatch(path, type, null); + } + + @Override + public WatcherOrBitSet triggerWatch( + String path, EventType type, WatcherOrBitSet supress) { + WatchedEvent e = new WatchedEvent(type, KeeperState.SyncConnected, path); + + BitHashSet watchers = remove(path); + if (watchers == null) { + return null; + } + + int triggeredWatches = 0; --- End diff -- We had metrics for this, I removed those metrics because #580 was still in review, I'll add those metrics back since that patch has been merged. ---