Search in sources :

Example 1 with BitHashSet

use of org.apache.zookeeper.server.util.BitHashSet in project zookeeper by apache.

the class WatchManagerOptimized method getWatcher2PathesMap.

/**
 * May cause OOM if there are lots of watches, might better to forbid
 * it in this class.
 */
public Map<Watcher, Set<String>> getWatcher2PathesMap() {
    Map<Watcher, Set<String>> watcher2paths = new HashMap<Watcher, Set<String>>();
    for (Entry<String, BitHashSet> e : pathWatches.entrySet()) {
        String path = e.getKey();
        BitHashSet watchers = e.getValue();
        // avoid race condition with add/remove
        synchronized (watchers) {
            for (Integer wbit : watchers) {
                Watcher w = watcherBitIdMap.get(wbit);
                if (w == null) {
                    continue;
                }
                if (!watcher2paths.containsKey(w)) {
                    watcher2paths.put(w, new HashSet<String>());
                }
                watcher2paths.get(w).add(path);
            }
        }
    }
    return watcher2paths;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) BitSet(java.util.BitSet) BitHashSet(org.apache.zookeeper.server.util.BitHashSet) BitHashSet(org.apache.zookeeper.server.util.BitHashSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Watcher(org.apache.zookeeper.Watcher)

Example 2 with BitHashSet

use of org.apache.zookeeper.server.util.BitHashSet in project zookeeper by apache.

the class WatchManagerOptimized method triggerWatch.

@Override
public WatcherOrBitSet triggerWatch(String path, EventType type, WatcherOrBitSet suppress) {
    WatchedEvent e = new WatchedEvent(type, KeeperState.SyncConnected, path);
    BitHashSet watchers = remove(path);
    if (watchers == null) {
        return null;
    }
    int triggeredWatches = 0;
    // WatcherCleaner and iterating here
    synchronized (watchers) {
        for (Integer wBit : watchers) {
            if (suppress != null && suppress.contains(wBit)) {
                continue;
            }
            Watcher w = watcherBitIdMap.get(wBit);
            // skip dead watcher
            if (w == null || isDeadWatcher(w)) {
                continue;
            }
            w.process(e);
            triggeredWatches++;
        }
    }
    updateMetrics(type, triggeredWatches);
    return new WatcherOrBitSet(watchers);
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) BitHashSet(org.apache.zookeeper.server.util.BitHashSet) Watcher(org.apache.zookeeper.Watcher)

Example 3 with BitHashSet

use of org.apache.zookeeper.server.util.BitHashSet in project zookeeper by apache.

the class WatchManagerOptimized method getWatchesByPath.

/**
 * Iterate through ConcurrentHashMap is 'safe', it will reflect the state
 * of the map at the time iteration began, may miss update while iterating,
 * given this is used in the commands to get a general idea of the watches
 * state, we don't care about missing some update.
 */
@Override
public WatchesPathReport getWatchesByPath() {
    Map<String, Set<Long>> path2ids = new HashMap<String, Set<Long>>();
    for (Entry<String, BitHashSet> e : pathWatches.entrySet()) {
        BitHashSet watchers = e.getValue();
        synchronized (watchers) {
            Set<Long> ids = new HashSet<Long>(watchers.size());
            path2ids.put(e.getKey(), ids);
            for (Integer wbit : watchers) {
                Watcher watcher = watcherBitIdMap.get(wbit);
                if (watcher instanceof ServerCnxn) {
                    ids.add(((ServerCnxn) watcher).getSessionId());
                }
            }
        }
    }
    return new WatchesPathReport(path2ids);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) BitSet(java.util.BitSet) BitHashSet(org.apache.zookeeper.server.util.BitHashSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Watcher(org.apache.zookeeper.Watcher) ServerCnxn(org.apache.zookeeper.server.ServerCnxn) BitHashSet(org.apache.zookeeper.server.util.BitHashSet) HashSet(java.util.HashSet) BitHashSet(org.apache.zookeeper.server.util.BitHashSet)

Example 4 with BitHashSet

use of org.apache.zookeeper.server.util.BitHashSet in project zookeeper by apache.

the class WatchManagerOptimized method addWatch.

@Override
public boolean addWatch(String path, Watcher watcher) {
    boolean result = false;
    // Need readLock to exclusively lock with removeWatcher, otherwise we
    // may add a dead watch whose connection was just closed.
    // 
    // Creating new watcher bit and adding it to the BitHashSet has it's
    // own lock to minimize the write lock scope
    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);
                // here
                if (existingWatchers != null) {
                    watchers = existingWatchers;
                }
            }
            result = watchers.add(bit);
        }
    } finally {
        addRemovePathRWLock.readLock().unlock();
    }
    return result;
}
Also used : BitHashSet(org.apache.zookeeper.server.util.BitHashSet)

Example 5 with BitHashSet

use of org.apache.zookeeper.server.util.BitHashSet in project zookeeper by apache.

the class WatchManagerOptimized method dumpWatches.

@Override
public void dumpWatches(PrintWriter pwriter, boolean byPath) {
    if (byPath) {
        for (Entry<String, BitHashSet> e : pathWatches.entrySet()) {
            pwriter.println(e.getKey());
            BitHashSet watchers = e.getValue();
            synchronized (watchers) {
                for (Integer wbit : watchers) {
                    Watcher w = watcherBitIdMap.get(wbit);
                    if (!(w instanceof ServerCnxn)) {
                        continue;
                    }
                    pwriter.print("\t0x");
                    pwriter.print(Long.toHexString(((ServerCnxn) w).getSessionId()));
                    pwriter.print("\n");
                }
            }
        }
    } else {
        for (Entry<Watcher, Set<String>> e : getWatcher2PathesMap().entrySet()) {
            pwriter.print("0x");
            pwriter.println(Long.toHexString(((ServerCnxn) e.getKey()).getSessionId()));
            for (String path : e.getValue()) {
                pwriter.print("\t");
                pwriter.println(path);
            }
        }
    }
}
Also used : ServerCnxn(org.apache.zookeeper.server.ServerCnxn) BitHashSet(org.apache.zookeeper.server.util.BitHashSet) Set(java.util.Set) HashSet(java.util.HashSet) BitSet(java.util.BitSet) BitHashSet(org.apache.zookeeper.server.util.BitHashSet) Watcher(org.apache.zookeeper.Watcher)

Aggregations

BitHashSet (org.apache.zookeeper.server.util.BitHashSet)6 Watcher (org.apache.zookeeper.Watcher)4 BitSet (java.util.BitSet)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ServerCnxn (org.apache.zookeeper.server.ServerCnxn)2 WatchedEvent (org.apache.zookeeper.WatchedEvent)1 Test (org.junit.jupiter.api.Test)1