use of com.newcosoft.zookeeper.ClusterState in project disgear by yangbutao.
the class TestClient method getWriteUrl.
public static String getWriteUrl(String col, String key) {
ClusterState clusterState = ClusterStateCacheManager.INSTANCE.getClusterState();
Set<String> liveNodes = clusterState.getLiveNodes();
Shard shard = Router.DEFAULT.getTargetShard(key, clusterState.getCollection("col1"));
Replica leader = shard.getLeader();
while (!liveNodes.contains(leader.getName())) {
try {
Thread.currentThread().sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
leader = shard.getLeader();
}
return leader.getStr("base_url");
}
use of com.newcosoft.zookeeper.ClusterState in project disgear by yangbutao.
the class TestClient method getReaderUrl.
public static String getReaderUrl(String col, String key) {
// String key = "000000000000000001";
ClusterState clusterState = ClusterStateCacheManager.INSTANCE.getClusterState();
Set<String> liveNodes = clusterState.getLiveNodes();
Shard shard = Router.DEFAULT.getTargetShard(key, clusterState.getCollection("col1"));
// for read
// roundrobin
Replica[] replicas = shard.getReplicas().toArray(new Replica[shard.getReplicas().size()]);
int totals = ClusterStateCacheManager.INSTANCE.getTotals().getAndDecrement();
if (totals <= 0) {
ClusterStateCacheManager.INSTANCE.setTotals(new AtomicInteger(10000));
}
int index = totals % (replicas.length);
Replica reader = replicas[index];
while (!liveNodes.contains(reader.getName())) {
totals = ClusterStateCacheManager.INSTANCE.getTotals().getAndDecrement();
if (totals <= 0) {
ClusterStateCacheManager.INSTANCE.setTotals(new AtomicInteger(10000));
}
index = totals % (replicas.length);
reader = replicas[index];
}
return reader.getStr("base_url");
}
use of com.newcosoft.zookeeper.ClusterState in project disgear by yangbutao.
the class CacheClient method getWriteUrl.
public static String getWriteUrl(String key, String collection) {
ClusterState clusterState = ClusterStateCacheManager.INSTANCE.getClusterState();
Set<String> liveNodes = clusterState.getLiveNodes();
Shard shard = Router.DEFAULT.getTargetShard(key, clusterState.getCollection(collection));
Replica leader = shard.getLeader();
while (!liveNodes.contains(leader.getName())) {
try {
Thread.currentThread().sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
leader = shard.getLeader();
}
return leader.getStr("base_url");
}
use of com.newcosoft.zookeeper.ClusterState in project disgear by yangbutao.
the class CacheClient method getReaderUrl.
public static String getReaderUrl(String key) {
// String key = "000000000000000001";
ClusterState clusterState = ClusterStateCacheManager.INSTANCE.getClusterState();
Set<String> liveNodes = clusterState.getLiveNodes();
Shard shard = Router.DEFAULT.getTargetShard(key, clusterState.getCollection("DEFAULT_COL"));
// for read
// roundrobin
Replica[] replicas = shard.getReplicas().toArray(new Replica[shard.getReplicas().size()]);
int totals = ClusterStateCacheManager.INSTANCE.getTotals().getAndDecrement();
if (totals <= 0) {
ClusterStateCacheManager.INSTANCE.setTotals(new AtomicInteger(10000));
}
int index = totals % (replicas.length);
// System.out.println("***********index size="+index+"************replicas="+replicas.length);
Replica reader = replicas[index < 0 ? (index + 1) : index];
while (!liveNodes.contains(reader.getName())) {
totals = ClusterStateCacheManager.INSTANCE.getTotals().getAndDecrement();
if (totals <= 0) {
ClusterStateCacheManager.INSTANCE.setTotals(new AtomicInteger(10000));
}
index = totals % (replicas.length);
reader = replicas[index < 0 ? (index + 1) : index];
}
return reader.getStr("base_url");
}
use of com.newcosoft.zookeeper.ClusterState in project disgear by yangbutao.
the class ClusterStateCacheManager method createClusterStateWatcher.
/**
* monitor to clusterstate.json
*/
public void createClusterStateWatcher() throws Exception {
byte[] datas = zkClient.getData(CLUSTER_STATE, new Watcher() {
public void process(WatchedEvent event) {
if (EventType.None.equals(event.getType())) {
return;
}
try {
synchronized (this) {
final Watcher thisWatch = this;
Stat stat = new Stat();
byte[] data = zkClient.getData(CLUSTER_STATE, thisWatch, stat);
List<String> liveNodes = zkClient.getChildren(LIVE_NODES_ZKNODE, this);
Set<String> liveNodesSet = new HashSet<String>();
liveNodesSet.addAll(liveNodes);
Set<String> ln = clusterState.getLiveNodes();
ClusterState newClusterState = ClusterState.load(stat.getVersion(), data, liveNodesSet);
clusterState = newClusterState;
CacheOperation.INSTANCE.clearJRedisPool();
System.out.println("CLUSTER_STATE changed");
System.out.println(clusterState.toString());
}
} catch (KeeperException e) {
if (e.code() == KeeperException.Code.SESSIONEXPIRED || e.code() == KeeperException.Code.CONNECTIONLOSS) {
return;
}
// log.error("", e);
throw new RuntimeException(e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
}, null);
Stat stat = new Stat();
List<String> liveNodes = zkClient.getChildren(LIVE_NODES_ZKNODE, null);
Set<String> liveNodesSet = new HashSet<String>();
liveNodesSet.addAll(liveNodes);
clusterState = ClusterState.load(stat.getVersion(), datas, liveNodesSet);
}
Aggregations