Search in sources :

Example 1 with ClusterState

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");
}
Also used : ClusterState(com.newcosoft.zookeeper.ClusterState) Shard(com.newcosoft.cache.Shard) Replica(com.newcosoft.cache.Replica)

Example 2 with ClusterState

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");
}
Also used : ClusterState(com.newcosoft.zookeeper.ClusterState) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Shard(com.newcosoft.cache.Shard) Replica(com.newcosoft.cache.Replica)

Example 3 with ClusterState

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");
}
Also used : ClusterState(com.newcosoft.zookeeper.ClusterState) Shard(com.newcosoft.cache.Shard) Replica(com.newcosoft.cache.Replica)

Example 4 with ClusterState

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");
}
Also used : ClusterState(com.newcosoft.zookeeper.ClusterState) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Shard(com.newcosoft.cache.Shard) Replica(com.newcosoft.cache.Replica)

Example 5 with ClusterState

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);
}
Also used : ClusterState(com.newcosoft.zookeeper.ClusterState) HashSet(java.util.HashSet) Set(java.util.Set) Watcher(org.apache.zookeeper.Watcher) WatchedEvent(org.apache.zookeeper.WatchedEvent) Stat(org.apache.zookeeper.data.Stat) List(java.util.List) KeeperException(org.apache.zookeeper.KeeperException) HashSet(java.util.HashSet)

Aggregations

ClusterState (com.newcosoft.zookeeper.ClusterState)5 Replica (com.newcosoft.cache.Replica)4 Shard (com.newcosoft.cache.Shard)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 KeeperException (org.apache.zookeeper.KeeperException)1 WatchedEvent (org.apache.zookeeper.WatchedEvent)1 Watcher (org.apache.zookeeper.Watcher)1 Stat (org.apache.zookeeper.data.Stat)1