Java Code Examples for org.ehcache.config.builders.CacheManagerBuilder#build()

The following examples show how to use org.ehcache.config.builders.CacheManagerBuilder#build() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BasicClusteredCacheOpsReplicationMultiThreadedTest.java    From ehcache3 with Apache License 2.0 7 votes vote down vote up
@Before
public void startServers() throws Exception {
  CLUSTER.getClusterControl().startAllServers();
  final CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder
      = CacheManagerBuilder.newCacheManagerBuilder()
      .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.getConnectionURI().resolve("/crud-cm-replication"))
          .timeouts(TimeoutsBuilder.timeouts() // we need to give some time for the failover to occur
              .read(Duration.ofMinutes(1))
              .write(Duration.ofMinutes(1)))
          .autoCreate(server -> server.defaultServerResource("primary-server-resource")));
  cacheManager1 = clusteredCacheManagerBuilder.build(true);
  cacheManager2 = clusteredCacheManagerBuilder.build(true);
  CacheConfiguration<Long, BlobValue> config = CacheConfigurationBuilder
      .newCacheConfigurationBuilder(Long.class, BlobValue.class,
          ResourcePoolsBuilder.newResourcePoolsBuilder().heap(500, EntryUnit.ENTRIES)
              .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 4, MemoryUnit.MB)))
      .withService(ClusteredStoreConfigurationBuilder.withConsistency(cacheConsistency))
      .build();

  cache1 = cacheManager1.createCache(testName.getMethodName(), config);
  cache2 = cacheManager2.createCache(testName.getMethodName(), config);

  caches = Arrays.asList(cache1, cache2);
}
 
Example 2
Source File: GettingStarted.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void explicitConsistencyConfiguration() throws Exception {
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder
          = CacheManagerBuilder.newCacheManagerBuilder()
          .with(ClusteringServiceConfigurationBuilder.cluster(URI.create("terracotta://localhost/my-application"))
                  .autoCreateOnReconnect(server -> server.defaultServerResource("primary-server-resource")
                    .resourcePool("resource-pool-a", 8, MemoryUnit.MB)));
  PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  try {
    // tag::clusteredCacheConsistency[]
    CacheConfiguration<Long, String> config = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
            ResourcePoolsBuilder.newResourcePoolsBuilder()
                    .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 2, MemoryUnit.MB)))
        .withService(ClusteredStoreConfigurationBuilder.withConsistency(Consistency.STRONG)) // <1>
        .build();

    Cache<Long, String> cache = cacheManager.createCache("clustered-cache", config);
    cache.put(42L, "All you need to know!"); // <2>

    // end::clusteredCacheConsistency[]
  } finally {
    cacheManager.close();
  }
}
 
Example 3
Source File: UnSupportedCombinationsWithClusteredCacheTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testClusteredCacheWithSynchronousEventListeners() {
  CacheEventListenerConfigurationBuilder cacheEventListenerConfiguration = CacheEventListenerConfigurationBuilder
      .newEventListenerConfiguration(new TestEventListener(), EventType.CREATED, EventType.UPDATED)
      .unordered().synchronous();

  final CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder
      = CacheManagerBuilder.newCacheManagerBuilder()
      .with(ClusteringServiceConfigurationBuilder.cluster(URI.create("terracotta://localhost/my-application"))
          .autoCreate(c -> c));
  try (PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(true)) {
    CacheConfiguration<Long, String> config = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
        ResourcePoolsBuilder.newResourcePoolsBuilder()
            .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 8, MemoryUnit.MB)))
        .withService(cacheEventListenerConfiguration)
        .build();

    cacheManager.createCache("test", config);
    fail("IllegalStateException expected");
  } catch (IllegalStateException e){
    assertThat(e.getCause().getMessage(), is("Synchronous CacheEventListener is not supported with clustered tiers"));
  }
}
 
Example 4
Source File: TerminatedServerTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testTerminationBeforeCacheManagerRetrieve() throws Exception {
  // Close all servers
  CLUSTER.get().getClusterControl().terminateAllServers();

  // Try to retrieve an entity (that doesn't exist but I don't care... the server is not running anyway
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
      CacheManagerBuilder.newCacheManagerBuilder()
          .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.get().getConnectionURI().resolve("/").resolve(testName.getMethodName()))
                  .timeouts(TimeoutsBuilder.timeouts().connection(Duration.ofSeconds(1))) // Need a connection timeout shorter than the TimeLimitedTask timeout
                  .expecting(server -> server.defaultServerResource("primary-server-resource")));
  PersistentCacheManager cacheManagerExisting = clusteredCacheManagerBuilder.build(false);

  // Base test time limit on observed TRANSPORT_HANDSHAKE_SYNACK_TIMEOUT; might not have been set in time to be effective
  long synackTimeout = TimeUnit.MILLISECONDS.toSeconds(ClientMessageTransport.TRANSPORT_HANDSHAKE_SYNACK_TIMEOUT);

  assertExceptionOccurred(StateTransitionException.class,
    new TimeLimitedTask<Void>(ofSeconds(3 + synackTimeout)) {
      @Override
      Void runTask() {
        cacheManagerExisting.init();
        return null;
      }
    })
    .withRootCauseInstanceOf(TimeoutException.class);
}
 
Example 5
Source File: BasicClusteredCacheOpsReplicationWithServersApiTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  CLUSTER.getClusterControl().startAllServers();

  final CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder
    = CacheManagerBuilder.newCacheManagerBuilder()
    .with(getConfigBuilder()
      .timeouts(TimeoutsBuilder.timeouts() // we need to give some time for the failover to occur
        .read(Duration.ofMinutes(1))
        .write(Duration.ofMinutes(1)))
      .autoCreate(server -> server.defaultServerResource("primary-server-resource")));
  CACHE_MANAGER = clusteredCacheManagerBuilder.build(true);
  CacheConfiguration<Long, String> config = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
    ResourcePoolsBuilder.newResourcePoolsBuilder().heap(100, EntryUnit.ENTRIES)
      .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 4, MemoryUnit.MB)))
    .build();

  CACHE1 = CACHE_MANAGER.createCache("clustered-cache", config);
  CACHE2 = CACHE_MANAGER.createCache("another-cache", config);
}
 
Example 6
Source File: BasicClusteredCacheTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testTieredClusteredCache() throws Exception {
  final CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
      newCacheManagerBuilder()
          .with(cluster(CLUSTER_URI).autoCreate(c -> c))
          .withCache("clustered-cache", newCacheConfigurationBuilder(Long.class, String.class,
                  heap(2)
                  .with(clusteredDedicated("primary-server-resource", 2, MemoryUnit.MB))));
  try (PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(true)) {

    final Cache<Long, String> cache = cacheManager.getCache("clustered-cache", Long.class, String.class);

    cache.put(1L, "value");
    assertThat(cache.get(1L), is("value"));
  }
}
 
Example 7
Source File: ClusteredCacheExpirationTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetNoExpirationPropagatedToHigherTiers() throws CachePersistenceException {
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder = cacheManagerBuilder(ExpiryPolicyBuilder.noExpiration());

  try(PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(true)) {

    Map<String, TierStatistics> tierStatistics = statisticsService.getCacheStatistics(CLUSTERED_CACHE).getTierStatistics();
    TierStatistics onheap = tierStatistics.get("OnHeap");
    TierStatistics offheap = tierStatistics.get("OffHeap");

    Cache<Long, String> cache = cacheManager.getCache(CLUSTERED_CACHE, Long.class, String.class);
    for (long i = 0; i < 30; i++) {
      cache.put(i, "value"); // store on the cluster
      cache.get(i); // push it up on heap and offheap tier
    }

    assertThat(onheap.getMappings()).isEqualTo(10);
    assertThat(offheap.getMappings()).isEqualTo(20);
  }
}
 
Example 8
Source File: ResourcePoolAllocationFailureTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testTooLowResourceException() throws InterruptedException {

  DedicatedClusteredResourcePool resourcePool = ClusteredResourcePoolBuilder.clusteredDedicated(10, MemoryUnit.KB);
  CacheManagerBuilder<PersistentCacheManager> cacheManagerBuilder = getPersistentCacheManagerCacheManagerBuilder(resourcePool);

  try {
    cacheManagerBuilder.build(true);
    fail("InvalidServerStoreConfigurationException expected");
  } catch (Exception e) {
    Throwable cause = getCause(e, PerpetualCachePersistenceException.class);
    assertThat(cause, notNullValue());
    assertThat(cause.getMessage(), startsWith("Unable to create"));
  }
  resourcePool = ClusteredResourcePoolBuilder.clusteredDedicated(100, MemoryUnit.KB);
  cacheManagerBuilder = getPersistentCacheManagerCacheManagerBuilder(resourcePool);
  PersistentCacheManager persistentCacheManager = cacheManagerBuilder.build(true);

  assertThat(persistentCacheManager, notNullValue());
  persistentCacheManager.close();

}
 
Example 9
Source File: BasicClusteredCacheTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testLargeValues() throws Exception {
  DefaultStatisticsService statisticsService = new DefaultStatisticsService();
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
          newCacheManagerBuilder()
                  .using(statisticsService)
                  .with(cluster(CLUSTER_URI).autoCreate(c -> c))
                  .withCache("small-cache", newCacheConfigurationBuilder(Long.class, BigInteger.class,
                          ResourcePoolsBuilder.newResourcePoolsBuilder()
                                  .with(clusteredDedicated("secondary-server-resource", 4, MemoryUnit.MB))));

  // The idea here is to add big things in the cache, and cause eviction of them to see if something crashes

  try(PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(true)) {

    Cache<Long, BigInteger> cache = cacheManager.getCache("small-cache", Long.class, BigInteger.class);

    Random random = new Random();
    for (long i = 0; i < 100; i++) {
      BigInteger value = new BigInteger(30 * 1024 * 128 * (1 + random.nextInt(10)), random);
      cache.put(i, value);
    }
  }
}
 
Example 10
Source File: CacheManagerDestroyTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testCloseCacheManagerSingleClient() {
  CacheManagerBuilder<PersistentCacheManager> cacheManagerBuilder = clusteredCacheManagerBuilder
      .withCache("test", newCacheConfigurationBuilder(Long.class, String.class,
          ResourcePoolsBuilder.newResourcePoolsBuilder()
              .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 2, MemoryUnit.MB))));

  PersistentCacheManager persistentCacheManager1 = cacheManagerBuilder.build(true);

  persistentCacheManager1.close();

  persistentCacheManager1.init();

  Cache<Long, String> cache = persistentCacheManager1.getCache("test", Long.class, String.class);
  cache.put(1L, "One");

  assertThat(cache.get(1L), is("One"));

  persistentCacheManager1.close();
}
 
Example 11
Source File: TerminatedServerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testTerminationThenContainsKey() throws Exception {
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
      CacheManagerBuilder.newCacheManagerBuilder()
          .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.get().getConnectionURI().resolve("/").resolve(testName.getMethodName()))
              .timeouts(TimeoutsBuilder.timeouts().read(Duration.of(1, ChronoUnit.SECONDS)).build())
              .autoCreate(server -> server.defaultServerResource("primary-server-resource")))
      .withCache("simple-cache",
          CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
              ResourcePoolsBuilder.newResourcePoolsBuilder()
                  .with(ClusteredResourcePoolBuilder.clusteredDedicated(4, MemoryUnit.MB))));
  PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
  cacheManager.init();

  Cache<Long, String> cache = cacheManager.getCache("simple-cache", Long.class, String.class);
  cache.put(1L, "un");
  cache.put(2L, "deux");
  cache.put(3L, "trois");

  assertThat(cache.containsKey(2L)).isTrue();

  CLUSTER.get().getClusterControl().terminateAllServers();

  boolean value = new TimeLimitedTask<Boolean>(ofSeconds(5)) {
    @Override
    Boolean runTask() throws Exception {
      return cache.containsKey(2L);
    }
  }.run();

  assertThat(value).isFalse();
}
 
Example 12
Source File: GettingStarted.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void clusteredCacheManagerExample() throws Exception {
  // tag::clusteredCacheManagerExample[]
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
      CacheManagerBuilder.newCacheManagerBuilder() // <1>
          .with(ClusteringServiceConfigurationBuilder.cluster(URI.create("terracotta://localhost/my-application")) // <2>
              .autoCreateOnReconnect(c -> c)); // <3>
  PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(true); // <4>

  cacheManager.close(); // <5>
  // end::clusteredCacheManagerExample[]
}
 
Example 13
Source File: SerializersTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
private void testSerializerWithByRefHeapCache(Serializer<Long> serializer) throws Exception {
  CacheManagerBuilder<CacheManager> cmBuilder =
    newCacheManagerBuilder()
      .withCache("heapByRefCache",
        newCacheConfigurationBuilder(Long.class, Person.class, newResourcePoolsBuilder().heap(10, EntryUnit.ENTRIES))
          .withKeySerializer(serializer)
      );
  cmBuilder.build(true);
}
 
Example 14
Source File: BasicClusteredCacheOpsTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void basicCacheCAS() throws Exception {
  final CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
      newCacheManagerBuilder()
          .with(cluster(CLUSTER.getConnectionURI().resolve("/cas-cm")).autoCreate(c -> c))
          .withCache("clustered-cache", newCacheConfigurationBuilder(Long.class, String.class,
              ResourcePoolsBuilder.newResourcePoolsBuilder().heap(100, EntryUnit.ENTRIES)
                  .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 2, MemoryUnit.MB)))
              .withService(new ClusteredStoreConfiguration(Consistency.STRONG)));

  try (PersistentCacheManager cacheManager1 = clusteredCacheManagerBuilder.build(true)) {

    try (PersistentCacheManager cacheManager2 = clusteredCacheManagerBuilder.build(true)) {
      final Cache<Long, String> cache1 = cacheManager1.getCache("clustered-cache", Long.class, String.class);
      final Cache<Long, String> cache2 = cacheManager2.getCache("clustered-cache", Long.class, String.class);

      assertThat(cache1.putIfAbsent(1L, "one"), nullValue());
      assertThat(cache2.putIfAbsent(1L, "another one"), is("one"));
      assertThat(cache2.remove(1L, "another one"), is(false));
      assertThat(cache1.replace(1L, "another one"), is("one"));
      assertThat(cache2.replace(1L, "another one", "yet another one"), is(true));
      assertThat(cache1.remove(1L, "yet another one"), is(true));
      assertThat(cache2.replace(1L, "one"), nullValue());
      assertThat(cache1.replace(1L, "another one", "yet another one"), is(false));
    }
  }
}
 
Example 15
Source File: ClusteredCacheExpirationTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetExpirationPropagatedToHigherTiers() throws CachePersistenceException {
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder = cacheManagerBuilder(oneSecondExpiration());
  try(PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(true)) {

    Map<String, TierStatistics> tierStatistics = statisticsService.getCacheStatistics(CLUSTERED_CACHE).getTierStatistics();
    TierStatistics onheap = tierStatistics.get("OnHeap");
    TierStatistics offheap = tierStatistics.get("OffHeap");

    Cache<Long, String> cache = cacheManager.getCache(CLUSTERED_CACHE, Long.class, String.class);
    for (long i = 0; i < 30; i++) {
      cache.put(i, "value"); // store on the cluster
      cache.get(i); // push it up on heap and offheap tier
    }

    assertThat(onheap.getMappings()).isEqualTo(10);
    assertThat(offheap.getMappings()).isEqualTo(20);

    timeSource.advanceTime(1500); // go after expiration

    for (long i = 0; i < 30; i++) {
      assertThat(cache.get(i)).isEqualTo(null); // the value should have expired
    }

    assertThat(onheap.getMappings()).isEqualTo(0);
    assertThat(offheap.getMappings()).isEqualTo(0);
  }
}
 
Example 16
Source File: CacheManagerProvider.java    From TweetwallFX with MIT License 5 votes vote down vote up
private static org.ehcache.CacheManager createCacheManager() {
    final CacheSettings cacheSettings = Configuration.getInstance().getConfigTyped(CacheSettings.CONFIG_KEY, CacheSettings.class);
    CacheManagerBuilder<? extends org.ehcache.CacheManager> cacheManagerBuilder = CacheManagerBuilder
            .newCacheManagerBuilder()
            .with(new CacheManagerPersistenceConfiguration(new File(
                    System.getProperty("user.home"),
                    cacheSettings.getPersistenceDirectoryName())));

    for (final Map.Entry<String, CacheSettings.CacheSetting> entry : cacheSettings.getCaches().entrySet()) {
        final String alias = entry.getKey();
        final CacheSettings.CacheSetting cacheSetting = entry.getValue();

        CacheConfigurationBuilder<?, ?> builder = CacheConfigurationBuilder
                .newCacheConfigurationBuilder(
                        loadClass(cacheSetting.getKeyType()),
                        loadClass(cacheSetting.getValueType()),
                        createResourcePoolsBuilder(cacheSetting.getCacheResources()));

        if (null != cacheSetting.getExpiry()) {
            builder = builder.withExpiry(createExpiryPolicy(cacheSetting.getExpiry()));
        }

        cacheManagerBuilder = cacheManagerBuilder.withCache(alias, builder);
    }

    final org.ehcache.CacheManager cacheManager = cacheManagerBuilder.build(true);

    LOG.info("EHCaches: " + cacheManager.getRuntimeConfiguration().getCacheConfigurations().keySet());
    cacheManager.getRuntimeConfiguration().getCacheConfigurations().keySet().forEach(s -> LOG.info("EHCache: " + s));
    Runtime.getRuntime().addShutdownHook(new Thread(cacheManager::close, "cache-shutdown"));

    return cacheManager;
}
 
Example 17
Source File: ClusteredEventsTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Test
public void testExpiringEventSequence() throws TimeoutException {
  TestTimeSource timeSource = new TestTimeSource();

  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
    newCacheManagerBuilder()
      .using(new TimeSourceConfiguration(timeSource))
      .with(cluster(CLUSTER_URI).autoCreate(s -> s.defaultServerResource("primary-server-resource")))
      .withCache(runningTest.getMethodName(), newCacheConfigurationBuilder(Long.class, String.class,
        newResourcePoolsBuilder().with(clusteredDedicated(16, MemoryUnit.MB)))
        .withExpiry(timeToLiveExpiration(Duration.ofMillis(1000))));

  try (PersistentCacheManager driver = clusteredCacheManagerBuilder.build(true)) {
    Cache<Long, String> driverCache = driver.getCache(runningTest.getMethodName(), Long.class, String.class);
    try (PersistentCacheManager observer = clusteredCacheManagerBuilder.build(true)) {
      Cache<Long, String> observerCache = observer.getCache(runningTest.getMethodName(), Long.class, String.class);

      List<CacheEvent<? extends Long, ? extends String>> driverEvents = new ArrayList<>();
      driverCache.getRuntimeConfiguration().registerCacheEventListener(driverEvents::add, EventOrdering.ORDERED, EventFiring.ASYNCHRONOUS, allOf(EventType.class));

      List<CacheEvent<? extends Long, ? extends String>> observerEvents = new ArrayList<>();
      observerCache.getRuntimeConfiguration().registerCacheEventListener(observerEvents::add, EventOrdering.ORDERED, EventFiring.ASYNCHRONOUS, allOf(EventType.class));


      driverCache.put(1L, "foo");
      timeSource.advanceTime(1100);
      driverCache.putIfAbsent(1L, "bar");
      timeSource.advanceTime(1100);
      driverCache.remove(1L);
      driverCache.put(1L, "baz");
      timeSource.advanceTime(1100);
      assertThat(driverCache.get(1L), nullValue());

      @SuppressWarnings("unchecked")
      Matcher<Iterable<? extends CacheEvent<? extends Long, ? extends String>>> expectedSequence = contains(
        created(1L, "foo"),
        expired(1L, "foo"),
        created(1L, "bar"),
        expired(1L, "bar"),
        created(1L, "baz"),
        expired(1L, "baz"));

      within(Duration.ofSeconds(10)).runsCleanly(() -> {
        assertThat(driverEvents, expectedSequence);
        assertThat(observerEvents, expectedSequence);
      });
    }
  }
}
 
Example 18
Source File: DuplicateTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Test
public void duplicateAfterFailoverAreReturningTheCorrectResponse() throws Exception {
  CacheManagerBuilder<PersistentCacheManager> builder = CacheManagerBuilder.newCacheManagerBuilder()
    .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.getConnectionURI())
      .timeouts(TimeoutsBuilder.timeouts().write(Duration.ofSeconds(30)))
      .autoCreate(server -> server.defaultServerResource("primary-server-resource")))
    .withCache("cache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, String.class,
      ResourcePoolsBuilder.newResourcePoolsBuilder()
        .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 10, MemoryUnit.MB)))
      .withResilienceStrategy(failingResilienceStrategy())
      .withService(ClusteredStoreConfigurationBuilder.withConsistency(Consistency.STRONG)));

  cacheManager =  builder.build(true);

  Cache<Integer, String> cache = cacheManager.getCache("cache", Integer.class, String.class);

  int numEntries = 3000;
  AtomicInteger currentEntry = new AtomicInteger();

  //Perform put operations in another thread
  ExecutorService executorService = Executors.newSingleThreadExecutor();
  try {
    Future<?> puts = executorService.submit(() -> {
      while (true) {
        int i = currentEntry.getAndIncrement();
        if (i >= numEntries) {
          break;
        }
        cache.put(i, "value:" + i);
      }
    });

    while (currentEntry.get() < 100); // wait to make sure some entries are added before shutdown

    // Failover to mirror when put & replication are in progress
    CLUSTER.getClusterControl().waitForRunningPassivesInStandby();
    CLUSTER.getClusterControl().terminateActive();

    puts.get(30, TimeUnit.SECONDS);

    //Verify cache entries on mirror
    for (int i = 0; i < numEntries; i++) {
      assertThat(cache.get(i)).isEqualTo("value:" + i);
    }
  } finally {
    executorService.shutdownNow();
  }

}
 
Example 19
Source File: IterationFailureBehaviorTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Test
public void testIteratorFailover() throws Exception {
  final CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder
    = CacheManagerBuilder.newCacheManagerBuilder()
    .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.get().getConnectionURI().resolve("/iterator-cm"))
      .autoCreate(server -> server.defaultServerResource("primary-server-resource"))
      .timeouts(timeouts().read(ofSeconds(10))));
  try (PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(true)) {
    CacheConfiguration<Long, String> smallConfig = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
      ResourcePoolsBuilder.newResourcePoolsBuilder()
        .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 1, MemoryUnit.MB))).build();

    Cache<Long, String> smallCache = cacheManager.createCache("small-cache", smallConfig);
    range(0, KEYS).forEach(k -> smallCache.put(k, Long.toString(k)));

    CacheConfiguration<Long, byte[]> largeConfig = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, byte[].class,
      ResourcePoolsBuilder.newResourcePoolsBuilder()
        .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 4, MemoryUnit.MB))).build();

    Cache<Long, byte[]> largeCache = cacheManager.createCache("large-cache", largeConfig);
    byte[] value = new byte[10 * 1024];
    range(0, KEYS).forEach(k -> {
      largeCache.put(k, value);
    });

    Map<Long, String> smallMap = new HashMap<>();

    Iterator<Cache.Entry<Long, String>> smallIterator = smallCache.iterator();
    Cache.Entry<Long, String> smallNext = smallIterator.next();
    smallMap.put(smallNext.getKey(), smallNext.getValue());

    Iterator<Cache.Entry<Long, byte[]>> largeIterator = largeCache.iterator();
    Cache.Entry<Long, byte[]> largeNext = largeIterator.next();
    assertThat(largeCache.get(largeNext.getKey()), notNullValue());

    CLUSTER.get().getClusterControl().waitForRunningPassivesInStandby();
    CLUSTER.get().getClusterControl().terminateActive();

    //large iterator fails
    try {
      largeIterator.forEachRemaining(k -> {});
      fail("Expected CacheIterationException");
    } catch (CacheIterationException e) {
      assertThat(e.getCause(), instanceOf(StoreAccessException.class));
      assertThat(e.getCause().getCause(), instanceOf(ServerStoreProxyException.class));
      assertThat(e.getCause().getCause().getCause(),
        either(instanceOf(ConnectionClosedException.class)) //lost in the space between active and passive
          .or(instanceOf(InvalidOperationException.class))); //picked up by the passive - it doesn't have our iterator
    }

    //small iterator completes... it fetched the entire batch in one shot
    smallIterator.forEachRemaining(k -> smallMap.put(k.getKey(), k.getValue()));

    assertThat(smallMap, is(range(0, KEYS).boxed().collect(toMap(identity(), k -> Long.toString(k)))));
  }
}
 
Example 20
Source File: IterationFailureBehaviorTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Test
public void testIteratorReconnect() throws Exception {
  final CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder
    = CacheManagerBuilder.newCacheManagerBuilder()
    .with(ClusteringServiceConfigurationBuilder.cluster(CLUSTER.get().getConnectionURI().resolve("/iterator-cm"))
      .autoCreate(server -> server.defaultServerResource("primary-server-resource")));
  try (PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(true)) {
    CacheConfiguration<Long, String> smallConfig = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
      ResourcePoolsBuilder.newResourcePoolsBuilder()
        .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 1, MemoryUnit.MB))).build();

    Cache<Long, String> smallCache = cacheManager.createCache("small-cache", smallConfig);
    range(0, KEYS).forEach(k -> smallCache.put(k, Long.toString(k)));

    CacheConfiguration<Long, byte[]> largeConfig = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, byte[].class,
      ResourcePoolsBuilder.newResourcePoolsBuilder()
        .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 4, MemoryUnit.MB))).build();

    Cache<Long, byte[]> largeCache = cacheManager.createCache("large-cache", largeConfig);
    byte[] value = new byte[10 * 1024];
    range(0, KEYS).forEach(k -> {
      largeCache.put(k, value);
    });

    Map<Long, String> smallMap = new HashMap<>();

    Iterator<Cache.Entry<Long, String>> smallIterator = smallCache.iterator();
    Cache.Entry<Long, String> smallNext = smallIterator.next();
    smallMap.put(smallNext.getKey(), smallNext.getValue());

    Iterator<Cache.Entry<Long, byte[]>> largeIterator = largeCache.iterator();
    Cache.Entry<Long, byte[]> largeNext = largeIterator.next();
    assertThat(largeCache.get(largeNext.getKey()), notNullValue());

    CLUSTER.get().getClusterControl().waitForRunningPassivesInStandby();
    CLUSTER.get().getClusterControl().terminateAllServers();
    Thread.sleep(CLUSTER.input().multipliedBy(2L).toMillis());
    CLUSTER.get().getClusterControl().startAllServers();

    //large iterator fails
    try {
      largeIterator.forEachRemaining(k -> {});
      fail("Expected CacheIterationException");
    } catch (CacheIterationException e) {
      assertThat(e.getCause(), instanceOf(StoreAccessException.class));
      assertThat(e.getCause().getCause(), instanceOf(ServerStoreProxyException.class));
      assertThat(e.getCause().getCause().getCause(),
        either(instanceOf(ConnectionClosedException.class)) //lost in the space between the two cluster executions
          .or(instanceOf(InvalidOperationException.class))); //picked up by the new cluster - it doesn't have our iterator
    }

    //small iterator completes... it fetched the entire batch in one shot
    smallIterator.forEachRemaining(k -> smallMap.put(k.getKey(), k.getValue()));

    assertThat(smallMap, is(range(0, KEYS).boxed().collect(toMap(identity(), k -> Long.toString(k)))));
  }
}