Search in sources :

Example 1 with Replica

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

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

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

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

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

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