
com.twitter.distributedlog.subscription.ZKSubscriptionsStore Maven / Gradle / Ivy
The newest version!
/**
* 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 com.twitter.distributedlog.subscription;
import com.twitter.distributedlog.DLSN;
import com.twitter.distributedlog.ZooKeeperClient;
import com.twitter.distributedlog.exceptions.DLInterruptedException;
import com.twitter.util.Function;
import com.twitter.util.Future;
import com.twitter.util.Promise;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scala.runtime.AbstractFunction1;
import scala.runtime.BoxedUnit;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* ZooKeeper Based Subscriptions Store.
*/
public class ZKSubscriptionsStore implements SubscriptionsStore {
private final ZooKeeperClient zkc;
private final String zkPath;
private final ConcurrentMap subscribers =
new ConcurrentHashMap();
public ZKSubscriptionsStore(ZooKeeperClient zkc, String zkPath) {
this.zkc = zkc;
this.zkPath = zkPath;
}
private ZKSubscriptionStateStore getSubscriber(String subscriberId) {
ZKSubscriptionStateStore ss = subscribers.get(subscriberId);
if (ss == null) {
ZKSubscriptionStateStore newSS = new ZKSubscriptionStateStore(zkc,
String.format("%s/%s", zkPath, subscriberId));
ZKSubscriptionStateStore oldSS = subscribers.putIfAbsent(subscriberId, newSS);
if (oldSS == null) {
ss = newSS;
} else {
try {
newSS.close();
} catch (IOException e) {
// ignore the exception
}
ss = oldSS;
}
}
return ss;
}
@Override
public Future getLastCommitPosition(String subscriberId) {
return getSubscriber(subscriberId).getLastCommitPosition();
}
@Override
public Future
© 2015 - 2025 Weber Informatics LLC | Privacy Policy