From dev-return-73665-archive-asf-public=cust-asf.ponee.io@zookeeper.apache.org Tue Sep 25 05:31:54 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 2AFDA180649 for ; Tue, 25 Sep 2018 05:31:54 +0200 (CEST) Received: (qmail 84262 invoked by uid 500); 25 Sep 2018 03:31:52 -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 84251 invoked by uid 99); 25 Sep 2018 03:31:51 -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:31:51 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id AA894DFF92; Tue, 25 Sep 2018 03:31:51 +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: <20180925033151.AA894DFF92@git1-us-west.apache.org> Date: Tue, 25 Sep 2018 03:31:51 +0000 (UTC) Github user lvfangmin commented on a diff in the pull request: https://github.com/apache/zookeeper/pull/590#discussion_r220050127 --- 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(); --- End diff -- Yes, it's used to improve the read throughput, creating new watcher bit and adding it to the BitHashSet has it's own lock to minimize the lock scope. I'll add some comments here. ---