public static synchronized HotRodServer createServer() throws IOException {
    count++;
    if (server == null) {
      CACHEMANAGER = new DefaultCacheManager(CONFIG_FILE);

      CACHEMANAGER.start();
      // This doesn't work on IPv6, because the util assumes "127.0.0.1" ...
      // server = HotRodTestingUtil.startHotRodServer(cacheManager, HOST, PORT);

      server = new HotRodServer();
      String hostAddress = hostAddress();
      String hostPort = Integer.toString(hostPort());
      String timeoutStr = Integer.toString(TIMEOUT);
      Properties props = new Properties();
      props.setProperty(Main.PROP_KEY_HOST(), hostAddress);
      props.setProperty(Main.PROP_KEY_PORT(), hostPort);
      props.setProperty(Main.PROP_KEY_IDLE_TIMEOUT(), timeoutStr);
      props.setProperty(Main.PROP_KEY_PROXY_HOST(), hostAddress);
      props.setProperty(Main.PROP_KEY_PROXY_PORT(), hostPort);
      // System.out.println("Starting HotRot Server at " + hostAddress + ":" + hostPort);
      server.start(props, CACHEMANAGER);

      server.cacheManager().startCaches(CACHE_NAME);

      server
          .getCacheManager()
          .getCache(CACHE_NAME)
          .put("1", new String("value1")); // $NON-NLS-1$  //$NON-NLS-2$
      server
          .getCacheManager()
          .getCache(CACHE_NAME)
          .put("2", new String("value2")); // $NON-NLS-1$  //$NON-NLS-2$
    }
    return server;
  }
  public static byte[] getKeyForServer(HotRodServer primaryOwner, String cacheName) {
    GenericJBossMarshaller marshaller = new GenericJBossMarshaller();
    Cache<?, ?> cache =
        cacheName != null
            ? primaryOwner.getCacheManager().getCache(cacheName)
            : primaryOwner.getCacheManager().getCache();
    Random r = new Random();
    byte[] dummy = new byte[8];
    int attemptsLeft = 1000;
    try {
      do {
        r.nextBytes(dummy);
        attemptsLeft--;
      } while (!isFirstOwner(cache, marshaller.objectToByteBuffer(dummy)) && attemptsLeft >= 0);
    } catch (IOException e) {
      throw new AssertionError(e);
    } catch (InterruptedException e) {
      throw new AssertionError(e);
    }

    if (attemptsLeft < 0)
      throw new IllegalStateException("Could not find any key owned by " + primaryOwner);

    log.infof(
        "Binary key %s hashes to [cluster=%s,hotrod=%s]",
        Util.printArray(dummy, false),
        primaryOwner.getCacheManager().getAddress(),
        primaryOwner.getAddress());

    return dummy;
  }
 public void testRequestRouting() {
   addInterceptors(DIST_ONE_CACHE_NAME);
   addInterceptors(DIST_TWO_CACHE_NAME);
   byte[] keyDistOne = getKeyForServer(server1, DIST_ONE_CACHE_NAME);
   byte[] keyDistTwo = getKeyForServer(server2, DIST_TWO_CACHE_NAME);
   assertSegments(DIST_ONE_CACHE_NAME, server1, server1.getCacheManager().getAddress());
   assertSegments(DIST_ONE_CACHE_NAME, server2, server1.getCacheManager().getAddress());
   assertSegments(DIST_TWO_CACHE_NAME, server1, server2.getCacheManager().getAddress());
   assertSegments(DIST_TWO_CACHE_NAME, server2, server2.getCacheManager().getAddress());
   assertRequestRouting(keyDistOne, DIST_ONE_CACHE_NAME, server1);
   assertRequestRouting(keyDistTwo, DIST_TWO_CACHE_NAME, server2);
 }
 public static void findServerAndKill(
     RemoteCacheManager client,
     Collection<HotRodServer> servers,
     Collection<EmbeddedCacheManager> cacheManagers) {
   InetSocketAddress addr = (InetSocketAddress) getLoadBalancer(client).nextServer(null);
   for (HotRodServer server : servers) {
     if (server.getPort() == addr.getPort()) {
       HotRodClientTestingUtil.killServers(server);
       TestingUtil.killCacheManagers(server.getCacheManager());
       cacheManagers.remove(server.getCacheManager());
       TestingUtil.blockUntilViewsReceived(50000, false, cacheManagers);
     }
   }
 }
 private void assertSegments(String cacheName, HotRodServer server, Address owner) {
   AdvancedCache<Object, Object> cache =
       server.getCacheManager().getCache(cacheName).getAdvancedCache();
   ConsistentHash ch = cache.getDistributionManager().getReadConsistentHash();
   assertEquals(1, ch.getNumSegments());
   Set<Integer> segments = ch.getSegmentsForOwner(owner);
   assertEquals(1, segments.size());
   assertEquals(0, segments.iterator().next().intValue());
 }
  byte[] getKeyForServer(HotRodServer primaryOwner, String cacheName) {
    Cache<?, ?> cache = primaryOwner.getCacheManager().getCache(cacheName);
    Random r = new Random();
    byte[] dummy = new byte[8];
    int attemptsLeft = 1000;
    do {
      r.nextBytes(dummy);
    } while (!isFirstOwner(cache, dummy) && attemptsLeft >= 0);

    if (attemptsLeft < 0)
      throw new IllegalStateException("Could not find any key owned by " + primaryOwner);

    log.infof(
        "Binary key %s hashes to [cluster=%s,hotrod=%s]",
        Util.printArray(dummy, false),
        primaryOwner.getCacheManager().getAddress(),
        primaryOwner.getAddress());

    return dummy;
  }
  /**
   * Get a split-personality key, whose POJO version hashes to the primary owner passed in, but it's
   * binary version does not.
   */
  public static Integer getSplitIntKeyForServer(
      HotRodServer primaryOwner, HotRodServer binaryOwner, String cacheName) {
    Cache<?, ?> cache =
        cacheName != null
            ? primaryOwner.getCacheManager().getCache(cacheName)
            : primaryOwner.getCacheManager().getCache();

    Cache<?, ?> binaryOwnerCache =
        cacheName != null
            ? binaryOwner.getCacheManager().getCache(cacheName)
            : binaryOwner.getCacheManager().getCache();

    Random r = new Random();
    byte[] dummy;
    Integer dummyInt;
    int attemptsLeft = 1000;
    boolean primaryOwnerFound = false;
    boolean binaryOwnerFound = false;
    do {
      dummyInt = r.nextInt();
      dummy = toBytes(dummyInt);
      attemptsLeft--;
      primaryOwnerFound = isFirstOwner(cache, dummyInt);
      binaryOwnerFound = isFirstOwner(binaryOwnerCache, dummy);
    } while (!(primaryOwnerFound && binaryOwnerFound) && attemptsLeft >= 0);

    if (attemptsLeft < 0)
      throw new IllegalStateException("Could not find any key owned by " + primaryOwner);

    log.infof(
        "Integer key [pojo=%s,bytes=%s] hashes to [cluster=%s,hotrod=%s], but the binary version's owner is [cluster=%s,hotrod=%s]",
        Util.toHexString(dummy),
        dummyInt,
        primaryOwner.getCacheManager().getAddress(),
        primaryOwner.getAddress(),
        binaryOwner.getCacheManager().getAddress(),
        binaryOwner.getAddress());

    return dummyInt;
  }
  public static Integer getIntKeyForServer(HotRodServer primaryOwner, String cacheName) {
    Cache<?, ?> cache =
        cacheName != null
            ? primaryOwner.getCacheManager().getCache(cacheName)
            : primaryOwner.getCacheManager().getCache();
    Random r = new Random();
    byte[] dummy;
    Integer dummyInt;
    int attemptsLeft = 1000;
    do {
      dummyInt = r.nextInt();
      dummy = toBytes(dummyInt);
      attemptsLeft--;
    } while (!isFirstOwner(cache, dummy) && attemptsLeft >= 0);

    if (attemptsLeft < 0)
      throw new IllegalStateException("Could not find any key owned by " + primaryOwner);

    log.infof(
        "Integer key %s hashes to [cluster=%s,hotrod=%s]",
        dummyInt, primaryOwner.getCacheManager().getAddress(), primaryOwner.getAddress());

    return dummyInt;
  }