@Test
  public void testEnableStatistics() {
    Properties p = createProperties();
    p.setProperty("hibernate.cache.infinispan.statistics", "true");
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.expiration.lifespan", "60000");
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.expiration.max_idle", "30000");
    p.setProperty("hibernate.cache.infinispan.entity.cfg", "myentity-cache");
    p.setProperty("hibernate.cache.infinispan.entity.eviction.strategy", "FIFO");
    p.setProperty("hibernate.cache.infinispan.entity.eviction.wake_up_interval", "3000");
    p.setProperty("hibernate.cache.infinispan.entity.eviction.max_entries", "10000");
    InfinispanRegionFactory factory = createRegionFactory(p);
    try {
      EmbeddedCacheManager manager = factory.getCacheManager();
      assertTrue(manager.getCacheManagerConfiguration().globalJmxStatistics().enabled());
      EntityRegionImpl region =
          (EntityRegionImpl)
              factory.buildEntityRegion("com.acme.Address", p, MUTABLE_NON_VERSIONED);
      AdvancedCache cache = region.getCache();
      assertTrue(factory.getTypeOverrides().get("entity").isExposeStatistics());
      assertTrue(cache.getCacheConfiguration().jmxStatistics().enabled());

      region =
          (EntityRegionImpl) factory.buildEntityRegion("com.acme.Person", p, MUTABLE_NON_VERSIONED);
      cache = region.getCache();
      assertTrue(factory.getTypeOverrides().get("com.acme.Person").isExposeStatistics());
      assertTrue(cache.getCacheConfiguration().jmxStatistics().enabled());

      final String query = "org.hibernate.cache.internal.StandardQueryCache";
      QueryResultsRegionImpl queryRegion =
          (QueryResultsRegionImpl) factory.buildQueryResultsRegion(query, p);
      cache = queryRegion.getCache();
      assertTrue(factory.getTypeOverrides().get("query").isExposeStatistics());
      assertTrue(cache.getCacheConfiguration().jmxStatistics().enabled());

      final String timestamps = "org.hibernate.cache.spi.UpdateTimestampsCache";
      ConfigurationBuilder builder = new ConfigurationBuilder();
      builder.clustering().stateTransfer().fetchInMemoryState(true);
      manager.defineConfiguration("timestamps", builder.build());
      TimestampsRegionImpl timestampsRegion =
          (TimestampsRegionImpl) factory.buildTimestampsRegion(timestamps, p);
      cache = timestampsRegion.getCache();
      assertTrue(factory.getTypeOverrides().get("timestamps").isExposeStatistics());
      assertTrue(cache.getCacheConfiguration().jmxStatistics().enabled());

      CollectionRegionImpl collectionRegion =
          (CollectionRegionImpl)
              factory.buildCollectionRegion("com.acme.Person.addresses", p, MUTABLE_NON_VERSIONED);
      cache = collectionRegion.getCache();
      assertTrue(factory.getTypeOverrides().get("collection").isExposeStatistics());
      assertTrue(cache.getCacheConfiguration().jmxStatistics().enabled());
    } finally {
      factory.stop();
    }
  }
 /** {@inheritDoc} */
 public CollectionRegion buildCollectionRegion(
     String regionName, Properties properties, CacheDataDescription metadata)
     throws CacheException {
   if (log.isDebugEnabled()) log.debug("Building collection cache region [" + regionName + "]");
   Cache cache = getCache(regionName, COLLECTION_KEY, properties);
   CacheAdapter cacheAdapter = CacheAdapterImpl.newInstance(cache);
   CollectionRegionImpl region =
       new CollectionRegionImpl(cacheAdapter, regionName, metadata, transactionManager, this);
   region.start();
   return region;
 }
  private void evictOrRemoveAllTest(boolean evict) {

    final String KEY = KEY_BASE + testCount++;

    assertEquals(0, getValidKeyCount(localCollectionRegion.getCacheAdapter().keySet()));

    assertEquals(0, getValidKeyCount(remoteCollectionRegion.getCacheAdapter().keySet()));

    assertNull("local is clean", localAccessStrategy.get(KEY, System.currentTimeMillis()));
    assertNull("remote is clean", remoteAccessStrategy.get(KEY, System.currentTimeMillis()));

    localAccessStrategy.putFromLoad(KEY, VALUE1, System.currentTimeMillis(), new Integer(1));
    assertEquals(VALUE1, localAccessStrategy.get(KEY, System.currentTimeMillis()));
    remoteAccessStrategy.putFromLoad(KEY, VALUE1, System.currentTimeMillis(), new Integer(1));
    assertEquals(VALUE1, remoteAccessStrategy.get(KEY, System.currentTimeMillis()));

    // Wait for async propagation
    sleep(250);

    if (evict) {
      localAccessStrategy.evictAll();
    } else {
      localAccessStrategy.removeAll();
    }

    // This should re-establish the region root node
    assertNull(localAccessStrategy.get(KEY, System.currentTimeMillis()));

    assertEquals(0, getValidKeyCount(localCollectionRegion.getCacheAdapter().keySet()));

    // Re-establishing the region root on the local node doesn't
    // propagate it to other nodes. Do a get on the remote node to re-establish
    assertEquals(null, remoteAccessStrategy.get(KEY, System.currentTimeMillis()));

    assertEquals(0, getValidKeyCount(remoteCollectionRegion.getCacheAdapter().keySet()));

    // Test whether the get above messes up the optimistic version
    remoteAccessStrategy.putFromLoad(KEY, VALUE1, System.currentTimeMillis(), new Integer(1));
    assertEquals(VALUE1, remoteAccessStrategy.get(KEY, System.currentTimeMillis()));

    assertEquals(1, getValidKeyCount(remoteCollectionRegion.getCacheAdapter().keySet()));

    // Wait for async propagation of the putFromLoad
    sleep(250);

    assertEquals(
        "local is correct",
        (isUsingInvalidation() ? null : VALUE1),
        localAccessStrategy.get(KEY, System.currentTimeMillis()));
    assertEquals(
        "remote is correct", VALUE1, remoteAccessStrategy.get(KEY, System.currentTimeMillis()));
  }
  @Test
  public void testPutFromLoadRemoveDoesNotProduceStaleData() throws Exception {
    final CountDownLatch pferLatch = new CountDownLatch(1);
    final CountDownLatch removeLatch = new CountDownLatch(1);
    TransactionManager tm = DualNodeJtaTransactionManagerImpl.getInstance("test1234");
    PutFromLoadValidator validator =
        new PutFromLoadValidator(tm) {
          @Override
          public boolean acquirePutFromLoadLock(Object key) {
            boolean acquired = super.acquirePutFromLoadLock(key);
            try {
              removeLatch.countDown();
              pferLatch.await(2, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
              log.debug("Interrupted");
              Thread.currentThread().interrupt();
            } catch (Exception e) {
              log.error("Error", e);
              throw new RuntimeException("Error", e);
            }
            return acquired;
          }
        };
    final TransactionalAccessDelegate delegate =
        new TransactionalAccessDelegate((CollectionRegionImpl) localCollectionRegion, validator);

    Callable<Void> pferCallable =
        new Callable<Void>() {
          public Void call() throws Exception {
            delegate.putFromLoad("k1", "v1", 0, null);
            return null;
          }
        };

    Callable<Void> removeCallable =
        new Callable<Void>() {
          public Void call() throws Exception {
            removeLatch.await();
            delegate.remove("k1");
            pferLatch.countDown();
            return null;
          }
        };

    ExecutorService executorService = Executors.newCachedThreadPool();
    Future<Void> pferFuture = executorService.submit(pferCallable);
    Future<Void> removeFuture = executorService.submit(removeCallable);

    pferFuture.get();
    removeFuture.get();

    assertFalse(localCollectionRegion.getCacheAdapter().containsKey("k1"));
  }
Beispiel #5
0
 public void release() throws Exception {
   if (entityRegionMap != null) {
     for (final EntityRegionImpl region : entityRegionMap.values()) {
       Caches.withinTx(
           region.getTransactionManager(),
           new Callable<Void>() {
             @Override
             public Void call() throws Exception {
               region.getCache().withFlags(Flag.CACHE_MODE_LOCAL).clear();
               return null;
             }
           });
       region.getCache().stop();
     }
     entityRegionMap.clear();
   }
   if (collectionRegionMap != null) {
     for (final CollectionRegionImpl collectionRegion : collectionRegionMap.values()) {
       Caches.withinTx(
           collectionRegion.getTransactionManager(),
           new Callable<Void>() {
             @Override
             public Void call() throws Exception {
               collectionRegion.getCache().withFlags(Flag.CACHE_MODE_LOCAL).clear();
               return null;
             }
           });
       collectionRegion.getCache().stop();
     }
     collectionRegionMap.clear();
   }
   if (regionFactory != null) {
     // Currently the RegionFactory is shutdown by its registration with the CacheTestSetup from
     // CacheTestUtil when built
     regionFactory.stop();
   }
   if (serviceRegistry != null) {
     serviceRegistry.destroy();
   }
 }
  @Test
  public void testBuildEntityCollectionRegionOverridesOnly() {
    AdvancedCache cache;
    Properties p = createProperties();
    p.setProperty("hibernate.cache.infinispan.entity.eviction.strategy", "LIRS");
    p.setProperty("hibernate.cache.infinispan.entity.eviction.wake_up_interval", "3000");
    p.setProperty("hibernate.cache.infinispan.entity.eviction.max_entries", "30000");
    p.setProperty("hibernate.cache.infinispan.collection.eviction.strategy", "LRU");
    p.setProperty("hibernate.cache.infinispan.collection.eviction.wake_up_interval", "3500");
    p.setProperty("hibernate.cache.infinispan.collection.eviction.max_entries", "35000");
    InfinispanRegionFactory factory = createRegionFactory(p);
    try {
      factory.getCacheManager();
      EntityRegionImpl region =
          (EntityRegionImpl)
              factory.buildEntityRegion("com.acme.Address", p, MUTABLE_NON_VERSIONED);
      assertNull(factory.getTypeOverrides().get("com.acme.Address"));
      cache = region.getCache();
      Configuration cacheCfg = cache.getCacheConfiguration();
      assertEquals(EvictionStrategy.LIRS, cacheCfg.eviction().strategy());
      assertEquals(3000, cacheCfg.expiration().wakeUpInterval());
      assertEquals(30000, cacheCfg.eviction().maxEntries());
      // Max idle value comes from base XML configuration
      assertEquals(100000, cacheCfg.expiration().maxIdle());

      CollectionRegionImpl collectionRegion =
          (CollectionRegionImpl)
              factory.buildCollectionRegion("com.acme.Person.addresses", p, MUTABLE_NON_VERSIONED);
      assertNull(factory.getTypeOverrides().get("com.acme.Person.addresses"));
      cache = collectionRegion.getCache();
      cacheCfg = cache.getCacheConfiguration();
      assertEquals(EvictionStrategy.LRU, cacheCfg.eviction().strategy());
      assertEquals(3500, cacheCfg.expiration().wakeUpInterval());
      assertEquals(35000, cacheCfg.eviction().maxEntries());
      assertEquals(100000, cacheCfg.expiration().maxIdle());
    } finally {
      factory.stop();
    }
  }
  @Before
  public void prepareResources() throws Exception {
    // to mimic exactly the old code results, both environments here are exactly the same...
    Configuration cfg = createConfiguration(getConfigurationName());
    localEnvironment = new NodeEnvironment(cfg);
    localEnvironment.prepare();

    localCollectionRegion =
        localEnvironment.getCollectionRegion(REGION_NAME, getCacheDataDescription());
    localAccessStrategy = localCollectionRegion.buildAccessStrategy(getAccessType());

    invalidation = localCollectionRegion.getCacheAdapter().isClusteredInvalidation();
    synchronous = localCollectionRegion.getCacheAdapter().isSynchronous();

    // Sleep a bit to avoid concurrent FLUSH problem
    avoidConcurrentFlush();

    remoteEnvironment = new NodeEnvironment(cfg);
    remoteEnvironment.prepare();

    remoteCollectionRegion =
        remoteEnvironment.getCollectionRegion(REGION_NAME, getCacheDataDescription());
    remoteAccessStrategy = remoteCollectionRegion.buildAccessStrategy(getAccessType());
  }
  @Test
  public void testBuildEntityCollectionRegionsPersonPlusEntityCollectionOverrides() {
    final String person = "com.acme.Person";
    final String address = "com.acme.Address";
    final String car = "com.acme.Car";
    final String addresses = "com.acme.Person.addresses";
    final String parts = "com.acme.Car.parts";
    Properties p = createProperties();
    // First option, cache defined for entity and overrides for generic entity data type and entity
    // itself.
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.cfg", "person-cache");
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.eviction.strategy", "LRU");
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.eviction.wake_up_interval", "2000");
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.eviction.max_entries", "5000");
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.expiration.lifespan", "60000");
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.expiration.max_idle", "30000");
    p.setProperty("hibernate.cache.infinispan.entity.cfg", "myentity-cache");
    p.setProperty("hibernate.cache.infinispan.entity.eviction.strategy", "LIRS");
    p.setProperty("hibernate.cache.infinispan.entity.eviction.wake_up_interval", "3000");
    p.setProperty("hibernate.cache.infinispan.entity.eviction.max_entries", "20000");
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.addresses.cfg", "addresses-cache");
    p.setProperty("hibernate.cache.infinispan.com.acme.Person.addresses.eviction.strategy", "LIRS");
    p.setProperty(
        "hibernate.cache.infinispan.com.acme.Person.addresses.eviction.wake_up_interval", "2500");
    p.setProperty(
        "hibernate.cache.infinispan.com.acme.Person.addresses.eviction.max_entries", "5500");
    p.setProperty(
        "hibernate.cache.infinispan.com.acme.Person.addresses.expiration.lifespan", "65000");
    p.setProperty(
        "hibernate.cache.infinispan.com.acme.Person.addresses.expiration.max_idle", "35000");
    p.setProperty("hibernate.cache.infinispan.collection.cfg", "mycollection-cache");
    p.setProperty("hibernate.cache.infinispan.collection.eviction.strategy", "LRU");
    p.setProperty("hibernate.cache.infinispan.collection.eviction.wake_up_interval", "3500");
    p.setProperty("hibernate.cache.infinispan.collection.eviction.max_entries", "25000");
    InfinispanRegionFactory factory = createRegionFactory(p);
    try {
      EmbeddedCacheManager manager = factory.getCacheManager();
      assertFalse(manager.getCacheManagerConfiguration().globalJmxStatistics().enabled());
      assertNotNull(factory.getTypeOverrides().get(person));
      assertFalse(factory.getDefinedConfigurations().contains(person));
      assertNotNull(factory.getTypeOverrides().get(addresses));
      assertFalse(factory.getDefinedConfigurations().contains(addresses));
      AdvancedCache cache;

      EntityRegionImpl region =
          (EntityRegionImpl) factory.buildEntityRegion(person, p, MUTABLE_NON_VERSIONED);
      assertNotNull(factory.getTypeOverrides().get(person));
      assertTrue(factory.getDefinedConfigurations().contains(person));
      assertNull(factory.getTypeOverrides().get(address));
      cache = region.getCache();
      Configuration cacheCfg = cache.getCacheConfiguration();
      assertEquals(EvictionStrategy.LRU, cacheCfg.eviction().strategy());
      assertEquals(2000, cacheCfg.expiration().wakeUpInterval());
      assertEquals(5000, cacheCfg.eviction().maxEntries());
      assertEquals(60000, cacheCfg.expiration().lifespan());
      assertEquals(30000, cacheCfg.expiration().maxIdle());
      assertFalse(cacheCfg.jmxStatistics().enabled());

      region = (EntityRegionImpl) factory.buildEntityRegion(address, p, MUTABLE_NON_VERSIONED);
      assertNotNull(factory.getTypeOverrides().get(person));
      assertTrue(factory.getDefinedConfigurations().contains(person));
      assertNull(factory.getTypeOverrides().get(address));
      cache = region.getCache();
      cacheCfg = cache.getCacheConfiguration();
      assertEquals(EvictionStrategy.LIRS, cacheCfg.eviction().strategy());
      assertEquals(3000, cacheCfg.expiration().wakeUpInterval());
      assertEquals(20000, cacheCfg.eviction().maxEntries());
      assertFalse(cacheCfg.jmxStatistics().enabled());

      region = (EntityRegionImpl) factory.buildEntityRegion(car, p, MUTABLE_NON_VERSIONED);
      assertNotNull(factory.getTypeOverrides().get(person));
      assertTrue(factory.getDefinedConfigurations().contains(person));
      assertNull(factory.getTypeOverrides().get(address));
      cache = region.getCache();
      cacheCfg = cache.getCacheConfiguration();
      assertEquals(EvictionStrategy.LIRS, cacheCfg.eviction().strategy());
      assertEquals(3000, cacheCfg.expiration().wakeUpInterval());
      assertEquals(20000, cacheCfg.eviction().maxEntries());
      assertFalse(cacheCfg.jmxStatistics().enabled());

      CollectionRegionImpl collectionRegion =
          (CollectionRegionImpl) factory.buildCollectionRegion(addresses, p, MUTABLE_NON_VERSIONED);
      assertNotNull(factory.getTypeOverrides().get(addresses));
      assertTrue(factory.getDefinedConfigurations().contains(person));
      assertNull(factory.getTypeOverrides().get(parts));
      cache = collectionRegion.getCache();
      cacheCfg = cache.getCacheConfiguration();
      assertEquals(EvictionStrategy.LIRS, cacheCfg.eviction().strategy());
      assertEquals(2500, cacheCfg.expiration().wakeUpInterval());
      assertEquals(5500, cacheCfg.eviction().maxEntries());
      assertEquals(65000, cacheCfg.expiration().lifespan());
      assertEquals(35000, cacheCfg.expiration().maxIdle());
      assertFalse(cacheCfg.jmxStatistics().enabled());

      collectionRegion =
          (CollectionRegionImpl) factory.buildCollectionRegion(parts, p, MUTABLE_NON_VERSIONED);
      assertNotNull(factory.getTypeOverrides().get(addresses));
      assertTrue(factory.getDefinedConfigurations().contains(addresses));
      assertNull(factory.getTypeOverrides().get(parts));
      cache = collectionRegion.getCache();
      cacheCfg = cache.getCacheConfiguration();
      assertEquals(EvictionStrategy.LRU, cacheCfg.eviction().strategy());
      assertEquals(3500, cacheCfg.expiration().wakeUpInterval());
      assertEquals(25000, cacheCfg.eviction().maxEntries());
      assertFalse(cacheCfg.jmxStatistics().enabled());

      collectionRegion =
          (CollectionRegionImpl) factory.buildCollectionRegion(parts, p, MUTABLE_NON_VERSIONED);
      assertNotNull(factory.getTypeOverrides().get(addresses));
      assertTrue(factory.getDefinedConfigurations().contains(addresses));
      assertNull(factory.getTypeOverrides().get(parts));
      cache = collectionRegion.getCache();
      cacheCfg = cache.getCacheConfiguration();
      assertEquals(EvictionStrategy.LRU, cacheCfg.eviction().strategy());
      assertEquals(3500, cacheCfg.expiration().wakeUpInterval());
      assertEquals(25000, cacheCfg.eviction().maxEntries());
      assertFalse(cacheCfg.jmxStatistics().enabled());
    } finally {
      factory.stop();
    }
  }