@Test
 public void testAdd() {
   String node1 = "node-A";
   nodes.add(node1);
   int totalNodes = nodes.getTotalNodes();
   assertEquals(2, totalNodes);
 }
 /**
  * Returns a new consistent hash of the same type with the given address removed.
  *
  * @param ch consistent hash to start with
  * @param toRemove address to remove
  * @param c configuration
  * @return a new consistent hash instance of the same type
  */
 public static ConsistentHash removeAddress(ConsistentHash ch, Address toRemove, Configuration c) {
   if (ch instanceof UnionConsistentHash)
     return removeAddressFromUnionConsistentHash((UnionConsistentHash) ch, toRemove, c);
   else {
     ConsistentHash newCH = constructConsistentHashInstance(c);
     Set<Address> caches = new HashSet<Address>(ch.getCaches());
     caches.remove(toRemove);
     newCH.setCaches(caches);
     return newCH;
   }
 }
 @Test
 public void testRemove() {
   String node1 = "node-A";
   nodes.add(node1);
   String node2 = "node-B";
   nodes.add(node2);
   int totalNodes = nodes.getTotalNodes();
   assertEquals(4, totalNodes);
   nodes.remove(node1);
   totalNodes = nodes.getTotalNodes();
   assertEquals(2, totalNodes);
 }
 @Test
 public void testGet() {
   String node1 = "node-C";
   nodes.add(node1);
   String node2 = "node-D";
   nodes.add(node2);
   int totalNodes = nodes.getTotalNodes();
   assertEquals(4, totalNodes);
   String node = nodes.get("abcd");
   assertEquals(node1, node);
   node = nodes.get(node2);
   assertEquals(node2, node);
 }
Beispiel #5
0
  public static void main(String[] args) {
    Map<String, AtomicInteger> distribution = Maps.newHashMap();

    System.out.println("======: ConsistentHash :========");
    ConsistentHash<String, String> c =
        new ConsistentHash(hfunc, strFunnel, strFunnel, getNodes(distribution));
    for (int i = 0; i < 10000; i++) {
      distribution.get(c.get("" + i)).incrementAndGet();
    }
    for (Entry<String, AtomicInteger> e : distribution.entrySet()) {
      System.out.println(e.getKey() + "," + e.getValue().get());
      e.getValue().set(0);
    }
    System.out.println("====== remove 2 ========");
    for (int i = 0; i < 2; i++) {
      c.remove("Node" + i);
      distribution.remove("Node" + i);
    }
    for (int i = 0; i < 10000; i++) {
      distribution.get(c.get("" + i)).incrementAndGet();
    }
    for (Entry<String, AtomicInteger> e : distribution.entrySet()) {
      System.out.println(e.getKey() + "," + e.getValue().get());
    }

    System.out.println("======: RendezvousHash :========");
    distribution = Maps.newHashMap();
    RendezvousHash<String, String> r =
        new RendezvousHash(hfunc, strFunnel, strFunnel, getNodes(distribution));

    for (int i = 0; i < 10000; i++) {
      distribution.get(r.get("" + i)).incrementAndGet();
    }
    for (Entry<String, AtomicInteger> e : distribution.entrySet()) {
      System.out.println(e.getKey() + "," + e.getValue().get());
      e.getValue().set(0);
    }
    System.out.println("====== remove 2 ========");
    for (int i = 0; i < 2; i++) {
      r.remove("Node" + i);
      distribution.remove("Node" + i);
    }
    for (int i = 0; i < 10000; i++) {
      distribution.get(r.get("" + i)).incrementAndGet();
    }
    for (Entry<String, AtomicInteger> e : distribution.entrySet()) {
      System.out.println(e.getKey() + "," + e.getValue().get());
    }
  }
  @Test
  public void AddedRouteWithSpecificIndex_AddedRouteBetweenTwoOthers_DidNotChangedRouteOfItem() {
    // arrange
    Route route1 = new Route("", 1123);
    Route route2 = new Route("", 2234);
    Route route3 = new Route("", 3333);
    _routes.add(route1);
    _routes.add(route2);
    int index1 = _routes.getIndex(route1);
    int index2 = _routes.getIndex(route2);
    Object item = new Object();
    Route routeOfItem = _routes.get(item);
    int specificIndex;
    if (!routeOfItem.equals(route2)) {
      specificIndex = index2 + 1;
    } else {
      specificIndex = index1 + 1;
    }

    // act

    _routes.add(route3, specificIndex);

    // assert
    Route result = _routes.get(item);
    assertEquals(routeOfItem, result);
    assertEquals(index1, _routes.getIndex(route1));
    assertEquals(index2, _routes.getIndex(route2));
    assertEquals(specificIndex, _routes.getIndex(route3));
  }
  @Test
  public void Add_Two_RoundBinding() {
    // arrange
    Route route1 = new Route("", 1);
    Route route2 = new Route("", 2);
    Route result = null;

    // act
    _routes.add(route1);
    _routes.add(route2);

    // assert
    result = _routes.get(route1);
    assertEquals(route1, result);
    result = _routes.get(route2);
    assertEquals(route2, result);
    result = _routes.getNext(route1);
    assertEquals(route2, result);
    result = _routes.getNext(result);
    assertEquals(route1, result);
    result = _routes.getPrevious(route2);
    assertEquals(route1, result);
    result = _routes.getPrevious(result);
    assertEquals(route2, result);
  }
 /**
  * Creates a new consistent hash instance based on the type specified, and populates the
  * consistent hash with the collection of addresses passed in.
  *
  * @param template An older consistent hash instance to clone
  * @param addresses with which to populate the consistent hash
  * @return a new consistent hash instance
  */
 public static ConsistentHash createConsistentHash(
     ConsistentHash template, Collection<Address> addresses) {
   Hash hf = null;
   HashSeed hs = null;
   int numVirtualNodes = 1;
   GroupManager groupManager = null;
   if (template instanceof AbstractWheelConsistentHash) {
     AbstractWheelConsistentHash wTemplate = (AbstractWheelConsistentHash) template;
     hf = wTemplate.hashFunction;
     hs = wTemplate.hashSeed;
     numVirtualNodes = wTemplate.numVirtualNodes;
     groupManager = wTemplate.groupManager;
   }
   ConsistentHash ch =
       constructConsistentHashInstance(template.getClass(), hf, hs, numVirtualNodes, groupManager);
   if (addresses != null && !addresses.isEmpty()) ch.setCaches(toSet(addresses));
   return ch;
 }
Beispiel #9
0
  public static void main(String[] args) throws Exception {
    System.out.println("Starting Cache Client...");
    ArrayList<CacheServiceInterface> servers = new ArrayList<>();
    servers.add(new DistributedCacheService("http://localhost:3000"));
    servers.add(new DistributedCacheService("http://localhost:3001"));
    servers.add(new DistributedCacheService("http://localhost:3002"));

    ConsistentHash hash = new ConsistentHash(Hashing.md5(), 3, servers);
    for (long i = 1; i <= 10; i++) {
      String aux = Character.toString((char) ('a' + i - 1));
      hash.get(i).put(i, aux);
      int bucket = servers.indexOf(hash.get(i));
      System.out.println(
          i + " routed to: " + ((DistributedCacheService) servers.get(bucket)).getUrl());
    }
    for (long i = 1; i <= 10; i++) {
      System.out.println("GET(" + i + ") ==> " + hash.get(i).get(i));
    }

    System.out.println("Existing Cache Client...");
  }
  @Test
  public void AddWithIndex_One_GotItInAnyCase() {
    // arrange
    Route route = new Route("", 1);
    Route result = null;

    // act
    _routes.add(route, 1);

    // assert
    result = _routes.get(route);
    assertEquals(route, result);
    result = _routes.get(1);
    assertEquals(route, result);
    result = _routes.get(2);
    assertEquals(route, result);
    result = _routes.get(3);
    assertEquals(route, result);
    result = _routes.getNext(route);
    assertEquals(route, result);
    result = _routes.getPrevious(route);
    assertEquals(route, result);
  }
 /**
  * Creates a new consistent hash instance based on the user's configuration, and populates the
  * consistent hash with the collection of addresses passed in.
  *
  * @param c configuration
  * @param addresses with which to populate the consistent hash
  * @return a new consistent hash instance
  */
 public static ConsistentHash createConsistentHash(
     Configuration c, Collection<Address> addresses) {
   ConsistentHash ch = constructConsistentHashInstance(c);
   ch.setCaches(toSet(addresses));
   return ch;
 }