Search in sources :

Example 51 with CachingProvider

use of javax.cache.spi.CachingProvider in project hazelcast by hazelcast.

the class InvalidationMetadataDistortionTest method ensure_nearCachedClient_and_member_data_sync_eventually.

@Test
public void ensure_nearCachedClient_and_member_data_sync_eventually() throws Exception {
    final int cacheSize = 100000;
    final AtomicBoolean stopTest = new AtomicBoolean();
    final Config config = createConfig();
    final HazelcastInstance member = hazelcastFactory.newHazelcastInstance(config);
    CachingProvider provider = HazelcastServerCachingProvider.createCachingProvider(serverInstance);
    CacheManager serverCacheManager = provider.getCacheManager();
    // populated from member.
    final Cache<Integer, Integer> memberCache = serverCacheManager.createCache(DEFAULT_CACHE_NAME, createCacheConfig(BINARY));
    for (int i = 0; i < cacheSize; i++) {
        memberCache.put(i, i);
    }
    ClientConfig clientConfig = createClientConfig();
    clientConfig.addNearCacheConfig(createNearCacheConfig(BINARY));
    HazelcastClientProxy client = (HazelcastClientProxy) hazelcastFactory.newHazelcastClient(clientConfig);
    CachingProvider clientCachingProvider = HazelcastClientCachingProvider.createCachingProvider(client);
    final Cache<Integer, Integer> clientCache = clientCachingProvider.getCacheManager().createCache(DEFAULT_CACHE_NAME, createCacheConfig(BINARY));
    Thread populateNearCache = new Thread(new Runnable() {

        public void run() {
            while (!stopTest.get()) {
                for (int i = 0; i < cacheSize; i++) {
                    clientCache.get(i);
                }
            }
        }
    });
    Thread distortSequence = new Thread(new Runnable() {

        @Override
        public void run() {
            while (!stopTest.get()) {
                distortRandomPartitionSequence(DEFAULT_CACHE_NAME, member);
                sleepSeconds(1);
            }
        }
    });
    Thread distortUuid = new Thread(new Runnable() {

        @Override
        public void run() {
            while (!stopTest.get()) {
                distortRandomPartitionUuid(member);
                sleepSeconds(5);
            }
        }
    });
    Thread put = new Thread(new Runnable() {

        public void run() {
            // change some data
            while (!stopTest.get()) {
                int key = getInt(cacheSize);
                int value = getInt(Integer.MAX_VALUE);
                memberCache.put(key, value);
                sleepAtLeastMillis(100);
            }
        }
    });
    // start threads
    put.start();
    populateNearCache.start();
    distortSequence.start();
    distortUuid.start();
    sleepSeconds(60);
    // stop threads
    stopTest.set(true);
    distortUuid.join();
    distortSequence.join();
    populateNearCache.join();
    put.join();
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            for (int i = 0; i < cacheSize; i++) {
                Integer valueSeenFromMember = memberCache.get(i);
                Integer valueSeenFromClient = clientCache.get(i);
                assertEquals(valueSeenFromMember, valueSeenFromClient);
            }
        }
    });
}
Also used : ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) CacheConfig(com.hazelcast.config.CacheConfig) NearCacheConfig(com.hazelcast.config.NearCacheConfig) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HazelcastInstance(com.hazelcast.core.HazelcastInstance) CacheManager(javax.cache.CacheManager) AssertTask(com.hazelcast.test.AssertTask) ClientConfig(com.hazelcast.client.config.ClientConfig) HazelcastClientProxy(com.hazelcast.client.impl.HazelcastClientProxy) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) HazelcastClientCachingProvider(com.hazelcast.client.cache.impl.HazelcastClientCachingProvider) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 52 with CachingProvider

use of javax.cache.spi.CachingProvider in project hazelcast by hazelcast.

the class ClientCachePartitionLostListenerTest method test_cachePartitionLostListener_registered.

@Test
public void test_cachePartitionLostListener_registered() {
    final String cacheName = randomName();
    HazelcastInstance instance = hazelcastFactory.newHazelcastInstance();
    final HazelcastInstance client = hazelcastFactory.newHazelcastClient();
    final CachingProvider cachingProvider = HazelcastClientCachingProvider.createCachingProvider(client);
    final CacheManager cacheManager = cachingProvider.getCacheManager();
    final CacheConfig<Integer, String> cacheConfig = new CacheConfig<Integer, String>();
    final Cache<Integer, String> cache = cacheManager.createCache(cacheName, cacheConfig);
    final ICache iCache = cache.unwrap(ICache.class);
    iCache.addPartitionLostListener(new CachePartitionLostListener() {

        @Override
        public void partitionLost(CachePartitionLostEvent event) {
        }
    });
    assertRegistrationsSizeEventually(instance, cacheName, 1);
}
Also used : CachePartitionLostListener(com.hazelcast.cache.impl.event.CachePartitionLostListener) HazelcastInstance(com.hazelcast.core.HazelcastInstance) CachePartitionLostEvent(com.hazelcast.cache.impl.event.CachePartitionLostEvent) ICache(com.hazelcast.cache.ICache) CacheManager(javax.cache.CacheManager) CacheConfig(com.hazelcast.config.CacheConfig) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) HazelcastServerCachingProvider.createCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider.createCachingProvider) HazelcastClientCachingProvider(com.hazelcast.client.cache.impl.HazelcastClientCachingProvider) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 53 with CachingProvider

use of javax.cache.spi.CachingProvider in project hazelcast by hazelcast.

the class ClientCachePartitionLostListenerTest method test_cachePartitionLostListener_invoked.

@Test
public void test_cachePartitionLostListener_invoked() {
    final String cacheName = randomName();
    HazelcastInstance instance = hazelcastFactory.newHazelcastInstance();
    final HazelcastInstance client = hazelcastFactory.newHazelcastClient();
    final HazelcastServerCachingProvider cachingProvider = createCachingProvider(instance);
    final CacheManager cacheManager = cachingProvider.getCacheManager();
    final CacheConfig<Integer, String> config = new CacheConfig<Integer, String>();
    config.setBackupCount(0);
    cacheManager.createCache(cacheName, config);
    final CachingProvider clientCachingProvider = HazelcastClientCachingProvider.createCachingProvider(client);
    final CacheManager clientCacheManager = clientCachingProvider.getCacheManager();
    final Cache<Integer, String> cache = clientCacheManager.getCache(cacheName);
    final ICache iCache = cache.unwrap(ICache.class);
    final EventCollectingCachePartitionLostListener listener = new EventCollectingCachePartitionLostListener();
    iCache.addPartitionLostListener(listener);
    final CacheService cacheService = getNode(instance).getNodeEngine().getService(CacheService.SERVICE_NAME);
    final int partitionId = 5;
    cacheService.onPartitionLost(new IPartitionLostEvent(partitionId, 0, null));
    assertCachePartitionLostEventEventually(listener, partitionId);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ICache(com.hazelcast.cache.ICache) CacheManager(javax.cache.CacheManager) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CacheConfig(com.hazelcast.config.CacheConfig) IPartitionLostEvent(com.hazelcast.spi.partition.IPartitionLostEvent) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) HazelcastServerCachingProvider.createCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider.createCachingProvider) HazelcastClientCachingProvider(com.hazelcast.client.cache.impl.HazelcastClientCachingProvider) CacheService(com.hazelcast.cache.impl.CacheService) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 54 with CachingProvider

use of javax.cache.spi.CachingProvider in project hazelcast by hazelcast.

the class CacheDestroyTest method test_cacheDestroyOperation.

@Test
public void test_cacheDestroyOperation() throws ExecutionException, InterruptedException {
    final String CACHE_NAME = "MyCache";
    final String FULL_CACHE_NAME = HazelcastCacheManager.CACHE_MANAGER_PREFIX + CACHE_NAME;
    CachingProvider cachingProvider = HazelcastServerCachingProvider.createCachingProvider(getHazelcastInstance());
    CacheManager cacheManager = cachingProvider.getCacheManager();
    cacheManager.createCache(CACHE_NAME, new CacheConfig());
    NodeEngineImpl nodeEngine1 = getNode(getHazelcastInstance()).getNodeEngine();
    final ICacheService cacheService1 = nodeEngine1.getService(ICacheService.SERVICE_NAME);
    InternalOperationService operationService1 = nodeEngine1.getOperationService();
    NodeEngineImpl nodeEngine2 = getNode(hazelcastInstances[1]).getNodeEngine();
    final ICacheService cacheService2 = nodeEngine2.getService(ICacheService.SERVICE_NAME);
    assertNotNull(cacheService1.getCacheConfig(FULL_CACHE_NAME));
    assertNotNull(cacheService2.getCacheConfig(FULL_CACHE_NAME));
    // Invoke on single node and the operation is also forward to others nodes by the operation itself
    operationService1.invokeOnTarget(ICacheService.SERVICE_NAME, new CacheDestroyOperation(FULL_CACHE_NAME), nodeEngine1.getThisAddress());
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertNull(cacheService1.getCacheConfig(FULL_CACHE_NAME));
            assertNull(cacheService2.getCacheConfig(FULL_CACHE_NAME));
        }
    });
}
Also used : NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) CacheDestroyOperation(com.hazelcast.cache.impl.operation.CacheDestroyOperation) ICacheService(com.hazelcast.cache.impl.ICacheService) CacheManager(javax.cache.CacheManager) AssertTask(com.hazelcast.test.AssertTask) InternalOperationService(com.hazelcast.spi.impl.operationservice.InternalOperationService) CacheConfig(com.hazelcast.config.CacheConfig) ExecutionException(java.util.concurrent.ExecutionException) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 55 with CachingProvider

use of javax.cache.spi.CachingProvider in project hazelcast by hazelcast.

the class CacheListenerTest method cacheEntryListenerCountIncreasedAndDecreasedCorrectly.

@Test
public void cacheEntryListenerCountIncreasedAndDecreasedCorrectly() {
    final CachingProvider provider = getCachingProvider();
    CacheManager cacheManager = provider.getCacheManager();
    CompleteConfiguration<String, String> config = new MutableConfiguration<String, String>().addCacheEntryListenerConfiguration(new MutableCacheEntryListenerConfiguration<String, String>(FactoryBuilder.factoryOf(new TestListener(new AtomicInteger())), null, true, true));
    final Cache<String, String> cache = cacheManager.createCache("MyCache", config);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheManager(javax.cache.CacheManager) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

CachingProvider (javax.cache.spi.CachingProvider)66 CacheManager (javax.cache.CacheManager)48 HazelcastServerCachingProvider (com.hazelcast.cache.impl.HazelcastServerCachingProvider)42 Test (org.junit.Test)33 QuickTest (com.hazelcast.test.annotation.QuickTest)28 HazelcastInstance (com.hazelcast.core.HazelcastInstance)24 ParallelTest (com.hazelcast.test.annotation.ParallelTest)24 CacheConfig (com.hazelcast.config.CacheConfig)21 HazelcastClientCachingProvider (com.hazelcast.client.cache.impl.HazelcastClientCachingProvider)18 ClientConfig (com.hazelcast.client.config.ClientConfig)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 MutableConfiguration (javax.cache.configuration.MutableConfiguration)12 ICache (com.hazelcast.cache.ICache)10 HazelcastClientProxy (com.hazelcast.client.impl.HazelcastClientProxy)10 Cache (javax.cache.Cache)10 HazelcastCachingProvider (com.hazelcast.cache.HazelcastCachingProvider)9 Config (com.hazelcast.config.Config)8 Data (com.hazelcast.nio.serialization.Data)8 Before (org.junit.Before)8 HazelcastCacheManager (com.hazelcast.cache.HazelcastCacheManager)7