@AfterTest(alwaysRun = true)
 public void tearDown() {
   hotRodServer1.stop();
   hotRodServer2.stop();
   hotRodServer3.stop();
   hotRodServer4.stop();
 }
  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 static String getServersString(HotRodServer... servers) {
   StringBuilder builder = new StringBuilder();
   for (HotRodServer server : servers) {
     builder.append("localhost").append(':').append(server.getPort()).append(";");
   }
   return builder.toString();
 }
Exemplo n.º 4
0
  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;
  }
 private HotRodServer addHotRodServer() {
   EmbeddedCacheManager cm = addClusterEnabledCacheManager(defaultBuilder);
   cm.defineConfiguration(DIST_ONE_CACHE_NAME, distOneBuilder.build());
   cm.defineConfiguration(DIST_TWO_CACHE_NAME, distTwoBuilder.build());
   HotRodServer server = HotRodClientTestingUtil.startHotRodServer(cm);
   addr2hrServer.put(new InetSocketAddress(server.getHost(), server.getPort()), server);
   return server;
 }
 private void assertRequestRouting(byte[] key, String cacheName, HotRodServer server) {
   RemoteCache<Object, Object> rcOne = rcm.getCache(cacheName);
   InetSocketAddress serverAddress = new InetSocketAddress(server.getHost(), server.getPort());
   for (int i = 0; i < 2; i++) {
     log.infof("Routing put test for key %s", Util.printArray(key, false));
     rcOne.put(key, "value");
     assertServerHit(serverAddress, cacheName, i + 1);
   }
 }
 /**
  * Kills a group of Hot Rod servers.
  *
  * @param servers the group of Hot Rod servers to kill
  */
 public static void killServers(HotRodServer... servers) {
   if (servers != null) {
     for (HotRodServer server : servers) {
       try {
         if (server != null) server.stop();
       } catch (Throwable t) {
         log.warn("Error stopping Hot Rod server", t);
       }
     }
   }
 }
 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);
 }
Exemplo n.º 9
0
 @Override
 protected void setup() throws Exception {
   super.setup();
   hotrodServer = HotRodClientTestingUtil.startHotRodServer(cacheManager);
   port = hotrodServer.getPort();
   remoteCacheManager =
       new RemoteCacheManager(
           new org.infinispan.client.hotrod.configuration.ConfigurationBuilder()
               .addServers("localhost:" + hotrodServer.getPort())
               .build());
   remoteCacheManager.start();
   GlobalComponentRegistry gcr = TestingUtil.extractGlobalComponentRegistry(cacheManager);
   interpreter = gcr.getComponent(Interpreter.class);
 }
 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);
     }
   }
 }
  @Test(dependsOnMethods = "testAddNewHotrodServer")
  public void testStopServer() {
    remoteCache.put("k1", "v1");
    remoteCache.put("k2", "v2");
    remoteCache.put("k3", "v3");
    remoteCache.put("k4", "v4");

    assertEquals(1, c1.size());
    assertEquals(1, c2.size());
    assertEquals(1, c3.size());
    assertEquals(1, c4.size());

    assertEquals("v1", remoteCache.get("k1"));
    assertEquals("v2", remoteCache.get("k2"));
    assertEquals("v3", remoteCache.get("k3"));
    assertEquals("v4", remoteCache.get("k4"));

    hotRodServer4.stop();

    try {
      remoteCache.put("k5", "v1");
      remoteCache.put("k6", "v2");
      remoteCache.put("k7", "v3");
      remoteCache.put("k8", "v4");
    } catch (Exception e) {
      assert false
          : "exception should not happen even if the balancer redirects to failed node at the beggining";
    }
  }
 public void testTouchServer() {
   RemoteCacheManager remoteCacheManager =
       new RemoteCacheManager("localhost", hotRodServer1.getPort(), true);
   RemoteCache<Object, Object> remoteCache = remoteCacheManager.getCache();
   remoteCache.put("k", "v");
   assertEquals("v", remoteCache.get("k"));
   killRemoteCacheManager(remoteCacheManager);
 }
Exemplo n.º 13
0
 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());
 }
  @Test(dependsOnMethods = "testRoundRobinLoadBalancing")
  public void testAddNewHotrodServer() {
    c4 = TestCacheManagerFactory.createLocalCacheManager(false).getCache();
    hotRodServer4 = TestHelper.startHotRodServer((EmbeddedCacheManager) c4.getCacheManager());
    registerCacheManager(c4.getCacheManager());

    List<SocketAddress> serverAddresses = new ArrayList<SocketAddress>();
    serverAddresses.add(new InetSocketAddress("localhost", hotRodServer1.getPort()));
    serverAddresses.add(new InetSocketAddress("localhost", hotRodServer2.getPort()));
    serverAddresses.add(new InetSocketAddress("localhost", hotRodServer3.getPort()));
    serverAddresses.add(new InetSocketAddress("localhost", hotRodServer4.getPort()));

    RoundRobinBalancingStrategy balancer = getBalancer();
    balancer.setServers(serverAddresses);

    remoteCache.put("k1", "v1");
    remoteCache.put("k2", "v2");
    remoteCache.put("k3", "v3");
    remoteCache.put("k4", "v4");

    assertEquals(1, c1.size());
    assertEquals(1, c2.size());
    assertEquals(1, c3.size());
    assertEquals(1, c4.size());

    assertEquals("v1", remoteCache.get("k1"));
    assertEquals("v2", remoteCache.get("k2"));
    assertEquals("v3", remoteCache.get("k3"));
    assertEquals("v4", remoteCache.get("k4"));

    remoteCache.put("k5", "v2");
    remoteCache.put("k6", "v3");
    remoteCache.put("k7", "v1");
    remoteCache.put("k8", "v2");
    remoteCache.put("k9", "v3");
    remoteCache.put("k10", "v3");
    remoteCache.put("k11", "v3");
    remoteCache.put("k12", "v3");

    assertEquals(3, c1.size());
    assertEquals(3, c2.size());
    assertEquals(3, c3.size());
    assertEquals(3, c4.size());
  }
  @Override
  protected void createCacheManagers() throws Throwable {
    c1 = TestCacheManagerFactory.createLocalCacheManager(false).getCache();
    c2 = TestCacheManagerFactory.createLocalCacheManager(false).getCache();
    c3 = TestCacheManagerFactory.createLocalCacheManager(false).getCache();
    registerCacheManager(c1.getCacheManager(), c2.getCacheManager(), c3.getCacheManager());

    hotRodServer1 = TestHelper.startHotRodServer(c1.getCacheManager());
    hotRodServer2 = TestHelper.startHotRodServer(c2.getCacheManager());
    hotRodServer3 = TestHelper.startHotRodServer(c3.getCacheManager());

    log.trace("Server 1 port: " + hotRodServer1.getPort());
    log.trace("Server 2 port: " + hotRodServer2.getPort());
    log.trace("Server 3 port: " + hotRodServer3.getPort());
    String servers = TestHelper.getServersString(hotRodServer1, hotRodServer2, hotRodServer3);
    log.trace("Server list is: " + servers);
    RemoteCacheManager remoteCacheManager = new RemoteCacheManager(servers);
    remoteCache = remoteCacheManager.getCache();
  }
Exemplo n.º 16
0
  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;
  }
 protected RemoteCacheManager getRemoteCacheManager() {
   Properties config = new Properties();
   config.put("infinispan.client.hotrod.server_list", "127.0.0.1:" + hotrodServer.getPort());
   config.put("infinispan.client.hotrod.socket_timeout", "5000");
   config.put("infinispan.client.hotrod.connect_timeout", "5000");
   config.put("maxActive", 2);
   // Set ping on startup false, so that the hang can happen
   // when the put comes, and not when the remote cache manager is built.
   config.put("infinispan.client.hotrod.ping_on_startup", "false");
   return new RemoteCacheManager(config);
 }
  @Override
  protected CacheStoreConfig createCacheStoreConfig() throws Exception {
    RemoteCacheStoreConfig remoteCacheStoreConfig = new RemoteCacheStoreConfig();
    localCacheManager = TestCacheManagerFactory.createLocalCacheManager(false);
    hrServer = TestHelper.startHotRodServer(localCacheManager);

    remoteCacheStoreConfig.setRemoteCacheName(CacheContainer.DEFAULT_CACHE_NAME);
    Properties properties = new Properties();
    properties.put("infinispan.client.hotrod.server_list", "localhost:" + hrServer.getPort());
    remoteCacheStoreConfig.setHotRodClientProperties(properties);

    return remoteCacheStoreConfig;
  }
  @Test(dependsOnMethods = "testStopServer")
  public void testRemoveServers() {
    List<SocketAddress> serverAddresses = new ArrayList<SocketAddress>();
    serverAddresses.add(new InetSocketAddress("localhost", hotRodServer1.getPort()));
    serverAddresses.add(new InetSocketAddress("localhost", hotRodServer2.getPort()));

    RoundRobinBalancingStrategy balancer = getBalancer();
    balancer.setServers(serverAddresses);

    remoteCache.put("k1", "v1");
    remoteCache.put("k2", "v2");
    remoteCache.put("k3", "v3");
    remoteCache.put("k4", "v4");

    assertEquals(2, c1.size());
    assertEquals(2, c2.size());
    assertEquals(0, c3.size());
    assertEquals(0, c4.size());

    assertEquals("v1", remoteCache.get("k1"));
    assertEquals("v2", remoteCache.get("k2"));
    assertEquals("v3", remoteCache.get("k3"));
    assertEquals("v4", remoteCache.get("k4"));

    remoteCache.put("k5", "v2");
    remoteCache.put("k6", "v3");
    remoteCache.put("k7", "v1");
    remoteCache.put("k8", "v2");
    remoteCache.put("k9", "v3");
    remoteCache.put("k10", "v3");
    remoteCache.put("k11", "v3");
    remoteCache.put("k12", "v3");

    assertEquals(6, c1.size());
    assertEquals(6, c2.size());
    assertEquals(0, c3.size());
    assertEquals(0, c4.size());
  }
Exemplo n.º 20
0
  @Override
  protected void createCacheManagers() throws Throwable {
    defaultBuilder = defaultCacheConfigurationBuilder();
    distOneBuilder = hotRodCacheConfiguration(getDefaultClusteredCacheConfig(CacheMode.DIST_SYNC));
    distOneBuilder
        .clustering()
        .hash()
        .numOwners(1)
        .numSegments(1)
        .consistentHashFactory(new ControlledConsistentHashFactory(0));
    distTwoBuilder = hotRodCacheConfiguration(getDefaultClusteredCacheConfig(CacheMode.DIST_SYNC));
    distTwoBuilder
        .clustering()
        .hash()
        .numOwners(1)
        .numSegments(1)
        .consistentHashFactory(new ControlledConsistentHashFactory(1));

    server1 = addHotRodServer();
    server2 = addHotRodServer();

    blockUntilViewReceived(manager(0).getCache(), 2);
    blockUntilCacheStatusAchieved(manager(0).getCache(), ComponentStatus.RUNNING, 10000);
    blockUntilCacheStatusAchieved(manager(1).getCache(), ComponentStatus.RUNNING, 10000);

    org.infinispan.client.hotrod.configuration.ConfigurationBuilder clientBuilder =
        new org.infinispan.client.hotrod.configuration.ConfigurationBuilder();
    clientBuilder
        .addServer()
        .host(server1.getHost())
        .port(server1.getPort())
        .addServer()
        .host(server2.getHost())
        .port(server2.getPort());
    rcm = new RemoteCacheManager(clientBuilder.build());
  }
  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;
  }
  @BeforeClass
  public void setup() throws Exception {
    ConfigurationBuilder serverBuilder = TestCacheManagerFactory.getDefaultCacheConfiguration(true);
    serverBuilder
        .eviction()
        .maxEntries(100)
        .strategy(EvictionStrategy.UNORDERED)
        .expiration()
        .wakeUpInterval(10L);
    serverCacheManager = TestCacheManagerFactory.createCacheManager(serverBuilder);
    serverCache = serverCacheManager.getCache();
    sourceServer = TestHelper.startHotRodServer(serverCacheManager);

    remoteSourceCacheManager = new RemoteCacheManager("localhost", sourceServer.getPort());
    remoteSourceCacheManager.start();
    remoteSourceCache = remoteSourceCacheManager.getCache();

    ConfigurationBuilder clientBuilder =
        TestCacheManagerFactory.getDefaultCacheConfiguration(false);
    clientBuilder
        .loaders()
        .addStore(RemoteCacheStoreConfigurationBuilder.class)
        .hotRodWrapping(true)
        .addServer()
        .host("localhost")
        .port(sourceServer.getPort());
    targetCacheManager = TestCacheManagerFactory.createCacheManager(clientBuilder);
    targetCache = targetCacheManager.getCache();
    targetServer = TestHelper.startHotRodServer(targetCacheManager);

    remoteTargetCacheManager = new RemoteCacheManager("localhost", targetServer.getPort());
    remoteTargetCacheManager.start();
    remoteTargetCache = remoteTargetCacheManager.getCache();

    marshaller = remoteTargetCacheManager.getMarshaller();
  }
  @Override
  protected EmbeddedCacheManager createCacheManager() throws Exception {
    latch = new CountDownLatch(1);
    cacheManager =
        new HangingCacheManager(
            TestCacheManagerFactory.createCacheManager(hotRodCacheConfiguration()), latch);
    // cacheManager = TestCacheManagerFactory.createLocalCacheManager();
    // pass the config file to the cache
    hotrodServer = TestHelper.startHotRodServer(cacheManager);
    log.info("Started server on port: " + hotrodServer.getPort());

    remoteCacheManager = getRemoteCacheManager();
    defaultRemote = remoteCacheManager.getCache();

    return cacheManager;
  }
Exemplo n.º 24
0
  @BeforeClass
  public static void beforeclass() {
    GlobalConfigurationBuilder globalConfigurationBuilder =
        new GlobalConfigurationBuilder().nonClusteredDefault();
    globalConfigurationBuilder.globalJmxStatistics().jmxDomain("infinispan-" + UUID.randomUUID());

    ConfigurationBuilder cacheConfiguration = new ConfigurationBuilder();
    cacheConfiguration.dataContainer().keyEquivalence(new AnyServerEquivalence());

    serverCache = new DefaultCacheManager(globalConfigurationBuilder.build());
    serverCache.defineConfiguration("sessions", cacheConfiguration.build());

    HotRodServerConfigurationBuilder hotRodServerConfigurationBuilder =
        new HotRodServerConfigurationBuilder();
    hotRodServerConfigurationBuilder.port(RemoteConfiguration.SERVER_PORT);

    server = new HotRodServer();
    server.start(hotRodServerConfigurationBuilder.build(), serverCache);
  }
  @Override
  protected EmbeddedCacheManager createCacheManager() throws Exception {
    cacheManager = TestCacheManagerFactory.createCacheManager(hotRodCacheConfiguration());
    cache = cacheManager.getCache();

    hotRodServer = HotRodClientTestingUtil.startHotRodServer(cacheManager);

    ConfigurationBuilder clientBuilder = new ConfigurationBuilder();
    clientBuilder.addServer().host("127.0.0.1").port(hotRodServer.getPort());
    clientBuilder.marshaller(new ProtoStreamMarshaller());
    remoteCacheManager = new RemoteCacheManager(clientBuilder.build());

    remoteCache = remoteCacheManager.getCache();

    // initialize client-side serialization context
    MarshallerRegistration.registerMarshallers(
        ProtoStreamMarshaller.getSerializationContext(remoteCacheManager));

    return cacheManager;
  }
Exemplo n.º 26
0
  public static synchronized void releaseServer() {
    --count;
    if (count <= 0) {

      try {
        if (CACHEMANAGER != null) {
          CACHEMANAGER.stop();
        }
      } finally {
        CACHEMANAGER = null;
      }

      try {
        // System.out.println("Stopping HotRot Server at " + hostAddress() + ":" + hostPort());
        if (server != null) {
          server.stop();
        }
      } finally {
        server = null;
      }
    }
  }
  /**
   * 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 RemoteCacheManager getRemoteCacheManager(HotRodServer server) {
   ConfigurationBuilder builder = new ConfigurationBuilder();
   builder.addServer().host(server.getHost()).port(server.getPort());
   return new InternalRemoteCacheManager(builder.build());
 }
Exemplo n.º 29
0
 @AfterClass
 public static void afterClass() {
   server.stop();
   serverCache.stop();
 }
 protected RemoteCacheManager getRemoteCacheManager() {
   ConfigurationBuilder builder = new ConfigurationBuilder();
   builder.addServer().host("127.0.0.1").port(hotrodServer.getPort());
   return new RemoteCacheManager(builder.build());
 }