Search in sources :

Example 6 with Shard

use of com.newcosoft.cache.Shard 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 7 with Shard

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

the class ClusterState method getLeader.

public Replica getLeader(String collection, String sliceName) {
    CacheCollection coll = collectionStates.get(collection);
    if (coll == null)
        return null;
    Shard slice = coll.getShard(sliceName);
    if (slice == null)
        return null;
    return slice.getLeader();
}
Also used : Shard(com.newcosoft.cache.Shard) CacheCollection(com.newcosoft.cache.CacheCollection)

Example 8 with Shard

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

the class ClusterState method getShardId.

public String getShardId(String baseUrl, String coreName) {
    // ") in " + collectionStates);
    for (CacheCollection coll : collectionStates.values()) {
        for (Shard slice : coll.getShards()) {
            for (Replica replica : slice.getReplicas()) {
                String rbaseUrl = replica.getStr(ZkStateReader.BASE_URL_PROP);
                String rcore = replica.getStr(ZkStateReader.CORE_NAME_PROP);
                if (baseUrl.equals(rbaseUrl) && coreName.equals(rcore)) {
                    return slice.getName();
                }
            }
        }
    }
    return null;
}
Also used : Shard(com.newcosoft.cache.Shard) Replica(com.newcosoft.cache.Replica) CacheCollection(com.newcosoft.cache.CacheCollection)

Example 9 with Shard

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

the class OverseerCollectionProcessor method deleteShard.

private void deleteShard(ClusterState clusterState, ZkNodeProps message) {
    log.info("Delete shard invoked");
    String collection = message.getStr(ZkStateReader.COLLECTION_PROP);
    String sliceId = message.getStr(ZkStateReader.SHARD_ID_PROP);
    Shard slice = clusterState.getSlice(collection, sliceId);
    if (slice == null) {
        if (clusterState.getCollections().contains(collection)) {
            throw new RuntimeException("No shard with the specified name exists: " + slice);
        } else {
            throw new RuntimeException("No collection with the specified name exists: " + collection);
        }
    }
    if (!(slice.getRange() == null || slice.getState().equals(Shard.INACTIVE))) {
        throw new RuntimeException("The slice: " + slice.getName() + " is currently " + slice.getState() + ". Only INACTIVE (or custom-hashed) slices can be deleted.");
    }
}
Also used : Shard(com.newcosoft.cache.Shard)

Example 10 with Shard

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

the class ZookeeperController method publishAndWaitForDownStates.

public void publishAndWaitForDownStates() throws KeeperException, InterruptedException {
    ClusterState clusterState = zkStateReader.getClusterState();
    Set<String> collections = clusterState.getCollections();
    List<String> updatedNodes = new ArrayList<String>();
    for (String collectionName : collections) {
        CacheCollection collection = clusterState.getCollection(collectionName);
        Collection<Shard> slices = collection.getShards();
        for (Shard slice : slices) {
            Collection<Replica> replicas = slice.getReplicas();
            for (Replica replica : replicas) {
                if (replica.getNodeName().equals(getNodeName()) && !(replica.getStr(ZkStateReader.STATE_PROP).equals(ZkStateReader.DOWN))) {
                    ZkNodeProps m = new ZkNodeProps(Overseer.QUEUE_OPERATION, "state", ZkStateReader.STATE_PROP, ZkStateReader.DOWN, ZkStateReader.BASE_URL_PROP, getBaseUrl(), ZkStateReader.CORE_NAME_PROP, replica.getStr(ZkStateReader.CORE_NAME_PROP), ZkStateReader.ROLES_PROP, replica.getStr(ZkStateReader.ROLES_PROP), ZkStateReader.NODE_NAME_PROP, getNodeName(), ZkStateReader.SHARD_ID_PROP, replica.getStr(ZkStateReader.SHARD_ID_PROP), ZkStateReader.COLLECTION_PROP, collectionName, ZkStateReader.CORE_NODE_NAME_PROP, replica.getName());
                    updatedNodes.add(replica.getStr(ZkStateReader.CORE_NAME_PROP));
                    // put into overseer/queue��dealing by  Overseer
                    overseerJobQueue.offer(ZkStateReader.toJSON(m));
                }
            }
        }
    }
    // now wait till the updates are in our state
    long now = System.currentTimeMillis();
    long timeout = now + 1000 * 30;
    boolean foundStates = false;
    while (System.currentTimeMillis() < timeout) {
        clusterState = zkStateReader.getClusterState();
        collections = clusterState.getCollections();
        for (String collectionName : collections) {
            CacheCollection collection = clusterState.getCollection(collectionName);
            Collection<Shard> slices = collection.getShards();
            for (Shard slice : slices) {
                Collection<Replica> replicas = slice.getReplicas();
                for (Replica replica : replicas) {
                    if (replica.getStr(ZkStateReader.STATE_PROP).equals(ZkStateReader.DOWN)) {
                        updatedNodes.remove(replica.getStr(ZkStateReader.CORE_NAME_PROP));
                    }
                }
            }
        }
        if (updatedNodes.size() == 0) {
            foundStates = true;
            Thread.sleep(1000);
            break;
        }
        Thread.sleep(1000);
    }
    if (!foundStates) {
        log.warn("Timed out waiting to see all nodes published as DOWN in our cluster state.");
    }
}
Also used : ArrayList(java.util.ArrayList) Replica(com.newcosoft.cache.Replica) CacheCollection(com.newcosoft.cache.CacheCollection) Shard(com.newcosoft.cache.Shard)

Aggregations

Shard (com.newcosoft.cache.Shard)17 Replica (com.newcosoft.cache.Replica)12 CacheCollection (com.newcosoft.cache.CacheCollection)9 HashMap (java.util.HashMap)5 Range (com.newcosoft.cache.Range)4 ClusterState (com.newcosoft.zookeeper.ClusterState)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Router (com.newcosoft.cache.Router)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 KeeperException (org.apache.zookeeper.KeeperException)1