Search in sources :

Example 6 with CacheCollection

use of com.newcosoft.cache.CacheCollection 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)

Example 7 with CacheCollection

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

the class AgentMain method doIfRedisRolechanged.

public void doIfRedisRolechanged(ClusterState oldClusterState, ClusterState newClusterState) {
    for (CacheCollection coll : oldClusterState.getCollectionStates().values()) {
        String currCol = coll.getName();
        for (Shard slice : coll.getShards()) {
            if (slice.getName().equals(AgentMain.INSTANCE.getShardName())) {
                String oldLeaderName = (String) slice.getLeader().get("node_name");
                Shard shard = newClusterState.getCollection(currCol).getShard(AgentMain.INSTANCE.getShardName());
                String newLeaderName = shard.getLeader().getStr("node_name");
                if ((leaderName == null && newLeaderName != null) || !leaderName.equals(newLeaderName)) {
                    leaderName = newLeaderName;
                // ShellExec.runExec("/opt/lsmp/redis_slave.sh");
                }
            }
        }
    }
}
Also used : Shard(com.newcosoft.cache.Shard) CacheCollection(com.newcosoft.cache.CacheCollection)

Example 8 with CacheCollection

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

the class TestShard method main1.

public static void main1(String[] args) {
    /*List<Range> ranges1 = Router.DEFAULT.partitionRange(2);
		for (Range range : ranges1) {
			System.out.println(range.toString());
		}*/
    List<Range> ranges = Router.DEFAULT.partitionRange(3);
    Replica replica1 = new Replica("node1", new HashMap());
    Replica replica11 = new Replica("node11", new HashMap());
    Map<String, Replica> replicas_1 = new HashMap<String, Replica>();
    replicas_1.put("replica1", replica1);
    replicas_1.put("replica11", replica11);
    Map<String, Object> propMap1 = new HashMap<String, Object>();
    propMap1.put("range", ranges.get(0));
    Shard shard1 = new Shard("shard1", replicas_1, propMap1);
    Replica replica2 = new Replica("node2", new HashMap());
    Replica replica21 = new Replica("node21", new HashMap());
    Map<String, Replica> replicas_2 = new HashMap<String, Replica>();
    replicas_2.put("replica2", replica2);
    replicas_2.put("replica21", replica21);
    Map<String, Object> propMap2 = new HashMap<String, Object>();
    propMap2.put("range", ranges.get(1));
    Shard shard2 = new Shard("shard2", replicas_2, propMap2);
    Replica replica3 = new Replica("node3", new HashMap());
    Replica replica31 = new Replica("node31", new HashMap());
    Map<String, Replica> replicas_3 = new HashMap<String, Replica>();
    replicas_3.put("replica3", replica3);
    replicas_3.put("replica31", replica31);
    Map<String, Object> propMap3 = new HashMap<String, Object>();
    propMap3.put("range", ranges.get(2));
    Shard shard3 = new Shard("shard3", replicas_3, propMap3);
    Map<String, Shard> shards = new HashMap<String, Shard>();
    shards.put("shard1", shard1);
    shards.put("shard2", shard2);
    shards.put("shard3", shard3);
    CacheCollection col = new CacheCollection("testCol", shards, new HashMap(), Router.DEFAULT);
    System.out.println("*****************uuuuudoo1**********" + Router.DEFAULT.getTargetShard("uuuuudoo5", col).getName());
    System.out.println("*****************uuuuudoo2**********" + Router.DEFAULT.getTargetShard("423423468913", col).getName());
    System.out.println("*****************uuuuudoo3**********" + Router.DEFAULT.getTargetShard("423423468925", col).getName());
}
Also used : HashMap(java.util.HashMap) Range(com.newcosoft.cache.Range) Shard(com.newcosoft.cache.Shard) Replica(com.newcosoft.cache.Replica) CacheCollection(com.newcosoft.cache.CacheCollection)

Example 9 with CacheCollection

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

the class TestShard method main.

public static void main(String[] args) {
    List<Range> ranges = Router.DEFAULT.partitionRange(3);
    Replica replica1 = new Replica("node1", new HashMap());
    Replica replica11 = new Replica("node11", new HashMap());
    Map<String, Replica> replicas_1 = new HashMap<String, Replica>();
    replicas_1.put("replica1", replica1);
    replicas_1.put("replica11", replica11);
    Map<String, Object> propMap1 = new HashMap<String, Object>();
    propMap1.put("range", ranges.get(0));
    Shard shard1 = new Shard("shard1", replicas_1, propMap1);
    Replica replica2 = new Replica("node2", new HashMap());
    Replica replica21 = new Replica("node21", new HashMap());
    Map<String, Replica> replicas_2 = new HashMap<String, Replica>();
    replicas_2.put("replica2", replica2);
    replicas_2.put("replica21", replica21);
    Map<String, Object> propMap2 = new HashMap<String, Object>();
    propMap2.put("range", ranges.get(1));
    Shard shard2 = new Shard("shard2", replicas_2, propMap2);
    Replica replica3 = new Replica("node3", new HashMap());
    Replica replica31 = new Replica("node31", new HashMap());
    Map<String, Replica> replicas_3 = new HashMap<String, Replica>();
    replicas_3.put("replica3", replica3);
    replicas_3.put("replica31", replica31);
    Map<String, Object> propMap3 = new HashMap<String, Object>();
    propMap3.put("range", ranges.get(2));
    Shard shard3 = new Shard("shard3", replicas_3, propMap3);
    Map<String, Shard> shards = new HashMap<String, Shard>();
    shards.put("shard1", shard1);
    shards.put("shard2", shard2);
    shards.put("shard3", shard3);
    CacheCollection col = new CacheCollection("testCol", shards, new HashMap(), Router.DEFAULT);
    int shard1Count = 0;
    int shard2Count = 0;
    int shard3Count = 0;
    for (int i = 1; i < 9999999; i++) {
        String key = "0000000000" + i;
        String shardName = Router.DEFAULT.getTargetShard(key, col).getName();
        if (shardName.equals("shard1")) {
            shard1Count++;
        } else if (shardName.equals("shard2")) {
            shard2Count++;
        } else if (shardName.equals("shard3")) {
            shard3Count++;
        }
    }
    System.out.println("shard1=" + shard1Count);
    System.out.println("shard2=" + shard2Count);
    System.out.println("shard3=" + shard3Count);
/*
		System.out.println("*****************uuuuudoo1**********"+Router.DEFAULT.getTargetShard("uuuuudoo5", col).getName());
		System.out.println("*****************uuuuudoo2**********"+Router.DEFAULT.getTargetShard("423423468913", col).getName());
		System.out.println("*****************uuuuudoo3**********"+Router.DEFAULT.getTargetShard("423423468925", col).getName());
*/
}
Also used : HashMap(java.util.HashMap) Range(com.newcosoft.cache.Range) Shard(com.newcosoft.cache.Shard) Replica(com.newcosoft.cache.Replica) CacheCollection(com.newcosoft.cache.CacheCollection)

Example 10 with CacheCollection

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

the class ClusterState method collectionFromObjects.

private static CacheCollection collectionFromObjects(String name, Map<String, Object> objs) {
    Map<String, Object> props;
    Map<String, Shard> slices;
    Map<String, Object> sliceObjs = (Map<String, Object>) objs.get(CacheCollection.SHARDS);
    if (sliceObjs == null) {
        slices = makeShards(objs);
        props = Collections.emptyMap();
    } else {
        slices = makeShards(sliceObjs);
        props = new HashMap<String, Object>(objs);
        objs.remove(CacheCollection.SHARDS);
    }
    Router router = Router.DEFAULT;
    return new CacheCollection(name, slices, props, router);
}
Also used : Router(com.newcosoft.cache.Router) Shard(com.newcosoft.cache.Shard) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) CacheCollection(com.newcosoft.cache.CacheCollection)

Aggregations

CacheCollection (com.newcosoft.cache.CacheCollection)10 Shard (com.newcosoft.cache.Shard)9 Replica (com.newcosoft.cache.Replica)6 HashMap (java.util.HashMap)6 Range (com.newcosoft.cache.Range)4 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Router (com.newcosoft.cache.Router)1 ArrayList (java.util.ArrayList)1