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;
}
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);
}
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);
}
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;
}
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);
}
}
}
}
Aggregations