Search in sources :

Example 11 with Replica

use of com.newcosoft.cache.Replica in project disgear by yangbutao.

the class OverseerCollectionProcessor method waitForCoreNodeName.

private String waitForCoreNodeName(CacheCollection collection, String msgBaseUrl, String msgCore) {
    int retryCount = 320;
    while (retryCount-- > 0) {
        Map<String, Shard> slicesMap = zkStateReader.getClusterState().getSlicesMap(collection.getName());
        if (slicesMap != null) {
            for (Shard slice : slicesMap.values()) {
                for (Replica replica : slice.getReplicas()) {
                    String baseUrl = replica.getStr(ZkStateReader.BASE_URL_PROP);
                    String core = replica.getStr(ZkStateReader.CORE_NAME_PROP);
                    if (baseUrl.equals(msgBaseUrl) && core.equals(msgCore)) {
                        return replica.getName();
                    }
                }
            }
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
    throw new RuntimeException("Could not find coreNodeName");
}
Also used : Shard(com.newcosoft.cache.Shard) Replica(com.newcosoft.cache.Replica)

Example 12 with Replica

use of com.newcosoft.cache.Replica in project disgear by yangbutao.

the class ZkStateReader method createClusterStateWatchersAndUpdate.

public synchronized void createClusterStateWatchersAndUpdate() throws KeeperException, InterruptedException {
    // We need to fetch the current cluster state and the set of live nodes
    synchronized (getUpdateLock()) {
        new ZookeeperClient(zkClient).ensureExists(CLUSTER_STATE, null, CreateMode.PERSISTENT);
        new ZookeeperClient(zkClient).ensureExists(ALIASES, null, CreateMode.PERSISTENT);
        log.info("Updating cluster state from ZooKeeper... ");
        zkClient.exists(CLUSTER_STATE, new Watcher() {

            public void process(WatchedEvent event) {
                // and do not remove the watcher
                if (EventType.None.equals(event.getType())) {
                    return;
                }
                try {
                    // ZkStateReader.this.updateClusterState(false, false);
                    synchronized (ZkStateReader.this.getUpdateLock()) {
                        // remake watch
                        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 = ZkStateReader.this.clusterState.getLiveNodes();
                        ClusterState clusterState = ClusterState.load(stat.getVersion(), data, liveNodesSet);
                        // if leader is changed��then change current node slave status to sync from new leader
                        String currNodeName = ZkStateReader.this.zkController.getNodeName();
                        Replica leader = clusterState.getLeader(ZkStateReader.this.zkController.getCollectionName(), ZkStateReader.this.zkController.getShardName());
                        String leaderNodeName = leader == null ? null : leader.getNodeName();
                        Replica oldLeader = ZkStateReader.this.clusterState.getLeader(ZkStateReader.this.zkController.getCollectionName(), ZkStateReader.this.zkController.getShardName());
                        String oldLeaderNodeName = oldLeader == null ? null : oldLeader.getNodeName();
                        BaseElectionContext electContext = ZkStateReader.this.zkController.getElectionContexts().get(currNodeName);
                        if (electContext != null && electContext.leaderPath != null) {
                            if (zkClient.exists(electContext.leaderPath, null) != null) {
                                byte[] data2 = zkClient.getData(electContext.leaderPath, null, stat);
                                Map<String, Object> stateMap2 = (Map<String, Object>) ZkStateReader.fromJSON(data2);
                                if (!currNodeName.equals(leaderNodeName) && (leaderNodeName != null & !leaderNodeName.equals(oldLeaderNodeName)) && liveNodes.contains(leaderNodeName)) {
                                    // current node is slave
                                    String base_url = (String) stateMap2.get("base_url");
                                    if (base_url != null) {
                                        String[] args = base_url.split(":");
                                        ShellExec.runExec(zkController.getScriptPath() + File.separator + "redis_slaves.sh " + args[0] + " " + args[1]);
                                    }
                                }
                            }
                        }
                        // update volatile
                        ZkStateReader.this.clusterState = clusterState;
                        System.out.println(clusterState.toString());
                    }
                } catch (KeeperException e) {
                    if (e.code() == KeeperException.Code.SESSIONEXPIRED || e.code() == KeeperException.Code.CONNECTIONLOSS) {
                        log.warn("ZooKeeper watch triggered, but Solr cannot talk to ZK");
                        return;
                    }
                    log.error("", e);
                    throw new RuntimeException(e);
                } catch (InterruptedException e) {
                    // Restore the interrupted status
                    Thread.currentThread().interrupt();
                    log.warn("", e);
                    return;
                }
            }
        });
    }
    synchronized (ZkStateReader.this.getUpdateLock()) {
        List<String> liveNodes = zkClient.getChildren(LIVE_NODES_ZKNODE, new Watcher() {

            public void process(WatchedEvent event) {
                // and do not remove the watcher
                if (EventType.None.equals(event.getType())) {
                    return;
                }
                try {
                    // true);
                    synchronized (ZkStateReader.this.getUpdateLock()) {
                        List<String> liveNodes = zkClient.getChildren(LIVE_NODES_ZKNODE, this);
                        log.info("Updating live nodes... ({})", liveNodes.size());
                        Set<String> liveNodesSet = new HashSet<String>();
                        liveNodesSet.addAll(liveNodes);
                        ClusterState clusterState = new ClusterState(ZkStateReader.this.clusterState.getZkClusterStateVersion(), liveNodesSet, ZkStateReader.this.clusterState.getCollectionStates());
                        ZkStateReader.this.clusterState = clusterState;
                        System.out.println(clusterState.toString());
                    }
                } catch (KeeperException e) {
                    if (e.code() == KeeperException.Code.SESSIONEXPIRED || e.code() == KeeperException.Code.CONNECTIONLOSS) {
                        log.warn("ZooKeeper watch triggered, but Solr cannot talk to ZK");
                        return;
                    }
                    log.error("", e);
                    throw new RuntimeException(e);
                } catch (InterruptedException e) {
                    // Restore the interrupted status
                    Thread.currentThread().interrupt();
                    log.warn("", e);
                    return;
                }
            }
        });
        Set<String> liveNodeSet = new HashSet<String>();
        liveNodeSet.addAll(liveNodes);
        ClusterState clusterState = ClusterState.load(zkClient, liveNodeSet);
        this.clusterState = clusterState;
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Watcher(org.apache.zookeeper.Watcher) Replica(com.newcosoft.cache.Replica) WatchedEvent(org.apache.zookeeper.WatchedEvent) Stat(org.apache.zookeeper.data.Stat) List(java.util.List) Map(java.util.Map) KeeperException(org.apache.zookeeper.KeeperException) HashSet(java.util.HashSet)

Example 13 with Replica

use of com.newcosoft.cache.Replica in project disgear by yangbutao.

the class ZookeeperController method waitForCoreNodeName.

private void waitForCoreNodeName(CollectionDesc descriptor) {
    int retryCount = 320;
    log.info("look for our core node name");
    while (retryCount-- > 0) {
        Map<String, Shard> slicesMap = zkStateReader.getClusterState().getSlicesMap(descriptor.getCollectionName());
        if (slicesMap != null) {
            for (Shard slice : slicesMap.values()) {
                for (Replica replica : slice.getReplicas()) {
                    String baseUrl = replica.getStr(ZkStateReader.BASE_URL_PROP);
                    String core = replica.getStr(ZkStateReader.CORE_NAME_PROP);
                    String msgBaseUrl = getBaseUrl();
                    String msgCore = descriptor.getCollectionName();
                    if (baseUrl.equals(msgBaseUrl) && core.equals(msgCore)) {
                        descriptor.setCoreNodeName(replica.getName());
                        return;
                    }
                }
            }
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
Also used : Shard(com.newcosoft.cache.Shard) Replica(com.newcosoft.cache.Replica)

Aggregations

Replica (com.newcosoft.cache.Replica)13 Shard (com.newcosoft.cache.Shard)12 CacheCollection (com.newcosoft.cache.CacheCollection)6 Range (com.newcosoft.cache.Range)4 ClusterState (com.newcosoft.zookeeper.ClusterState)4 HashMap (java.util.HashMap)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)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