Search in sources :

Example 6 with LogSegmentNamesListener

use of org.apache.distributedlog.callback.LogSegmentNamesListener in project bookkeeper by apache.

the class ZKLogSegmentMetadataStore method getLogSegmentNames.

@Override
public CompletableFuture<Versioned<List<String>>> getLogSegmentNames(String logSegmentsPath, LogSegmentNamesListener listener) {
    Watcher zkWatcher;
    if (null == listener) {
        zkWatcher = null;
    } else {
        closeLock.readLock().lock();
        try {
            if (closed) {
                zkWatcher = null;
            } else {
                Map<LogSegmentNamesListener, VersionedLogSegmentNamesListener> listenerSet = listeners.get(logSegmentsPath);
                if (null == listenerSet) {
                    Map<LogSegmentNamesListener, VersionedLogSegmentNamesListener> newListenerSet = new HashMap<LogSegmentNamesListener, VersionedLogSegmentNamesListener>();
                    Map<LogSegmentNamesListener, VersionedLogSegmentNamesListener> oldListenerSet = listeners.putIfAbsent(logSegmentsPath, newListenerSet);
                    if (null != oldListenerSet) {
                        listenerSet = oldListenerSet;
                    } else {
                        listenerSet = newListenerSet;
                    }
                }
                synchronized (listenerSet) {
                    listenerSet.put(listener, new VersionedLogSegmentNamesListener(listener));
                    if (!listeners.containsKey(logSegmentsPath)) {
                        // listener set has been removed, add it back
                        if (null != listeners.putIfAbsent(logSegmentsPath, listenerSet)) {
                            logger.debug("Listener set is already found for log segments path {}", logSegmentsPath);
                        }
                    }
                }
                zkWatcher = ZKLogSegmentMetadataStore.this;
            }
        } finally {
            closeLock.readLock().unlock();
        }
    }
    CompletableFuture<Versioned<List<String>>> getLogSegmentNamesResult = zkGetLogSegmentNames(logSegmentsPath, zkWatcher);
    if (null != listener) {
        getLogSegmentNamesResult.whenComplete(new ReadLogSegmentsTask(logSegmentsPath, this));
    }
    return zkGetLogSegmentNames(logSegmentsPath, zkWatcher);
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Watcher(org.apache.zookeeper.Watcher) LogSegmentNamesListener(org.apache.distributedlog.callback.LogSegmentNamesListener)

Aggregations

LogSegmentNamesListener (org.apache.distributedlog.callback.LogSegmentNamesListener)6 List (java.util.List)5 Versioned (org.apache.bookkeeper.versioning.Versioned)5 LogSegmentMetadata (org.apache.distributedlog.LogSegmentMetadata)5 Test (org.junit.Test)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Watcher (org.apache.zookeeper.Watcher)1