Search in sources :

Example 46 with CachingProvider

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

the class CacheThroughHazelcastInstanceTest method whenThereIsCacheConfigAndCreatedByCacheManager_thenReturnsSameCache.

private void whenThereIsCacheConfigAndCreatedByCacheManager_thenReturnsSameCache(boolean getCache) {
    HazelcastInstance instance = createInstance();
    CachingProvider cachingProvider = createCachingProvider(instance);
    CacheManager cacheManager = cachingProvider.getCacheManager();
    Cache cache1 = cacheManager.createCache(CACHE_NAME, createCacheConfig(CACHE_NAME));
    assertNotNull(cache1);
    Cache cache2 = retrieveCache(instance, getCache);
    assertNotNull(cache2);
    // verify that they are same cache instance
    assertTrue(cache1 == cache2);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastInstanceCacheManager(com.hazelcast.instance.HazelcastInstanceCacheManager) ICacheManager(com.hazelcast.core.ICacheManager) HazelcastCacheManager(com.hazelcast.cache.HazelcastCacheManager) CacheManager(javax.cache.CacheManager) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) HazelcastCachingProvider(com.hazelcast.cache.HazelcastCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) Cache(javax.cache.Cache) ICache(com.hazelcast.cache.ICache)

Example 47 with CachingProvider

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

the class CacheStatsTest method doTestForOwnedEntryCount.

private void doTestForOwnedEntryCount(boolean useBackups, boolean triggerMigration) {
    final String cacheName = randomName();
    ICache<Integer, String> cache;
    CacheStatistics[] allStats;
    HazelcastInstance instance2 = null;
    if (useBackups) {
        // Create the second instance to store data as backup.
        instance2 = getHazelcastInstance();
        CachingProvider cp = getCachingProvider(instance2);
        CacheManager cm = cp.getCacheManager();
        cache = createCache(cacheName);
        ICache<Integer, String> c = cm.getCache(cacheName).unwrap(ICache.class);
        allStats = new CacheStatistics[] { cache.getLocalCacheStatistics(), c.getLocalCacheStatistics() };
    } else {
        cache = createCache(cacheName);
        allStats = new CacheStatistics[] { cache.getLocalCacheStatistics() };
    }
    final int ENTRY_COUNT = 100;
    for (int i = 0; i < ENTRY_COUNT; i++) {
        cache.put(i, "Value-" + i);
    }
    assertOwnedEntryCount(ENTRY_COUNT, allStats);
    if (triggerMigration && instance2 != null) {
        // Shutdown the second instance to trigger migration so first instance will be owner of all partitions.
        instance2.shutdown();
        allStats = new CacheStatistics[] { cache.getLocalCacheStatistics() };
        assertOwnedEntryCount(ENTRY_COUNT, allStats);
    }
    for (int i = 0; i < 10; i++) {
        cache.remove(i);
    }
    assertOwnedEntryCount(ENTRY_COUNT - 10, allStats);
    for (int i = 10; i < ENTRY_COUNT; i++) {
        cache.remove(i);
    }
    assertOwnedEntryCount(0, allStats);
    for (int i = 0; i < ENTRY_COUNT; i++) {
        cache.put(i, "Value-" + i);
    }
    assertOwnedEntryCount(ENTRY_COUNT, allStats);
    if (triggerMigration) {
        // Create the second instance to trigger migration
        // so the second instance will be owner of some partitions
        // and the first instance will lose ownership of some instances.
        instance2 = getHazelcastInstance();
        CachingProvider cp = getCachingProvider(instance2);
        CacheManager cm = cp.getCacheManager();
        ICache<Integer, String> c = cm.getCache(cacheName).unwrap(ICache.class);
        allStats = new CacheStatistics[] { cache.getLocalCacheStatistics(), c.getLocalCacheStatistics() };
        assertOwnedEntryCount(ENTRY_COUNT, allStats);
    }
    cache.clear();
    assertOwnedEntryCount(0, allStats);
    for (int i = 0; i < ENTRY_COUNT; i++) {
        cache.put(i, "Value-" + i);
    }
    assertOwnedEntryCount(ENTRY_COUNT, allStats);
    cache.destroy();
    assertOwnedEntryCount(0, allStats);
}
Also used : CacheStatistics(com.hazelcast.cache.CacheStatistics) HazelcastInstance(com.hazelcast.core.HazelcastInstance) CacheManager(javax.cache.CacheManager) CachingProvider(javax.cache.spi.CachingProvider)

Example 48 with CachingProvider

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

the class ClientCacheMetaDataFetcherTest method getRepairingTask.

private RepairingTask getRepairingTask(String cacheName, int partition, long givenSequence, UUID givenUuid) {
    HazelcastInstance member = factory.newHazelcastInstance();
    distortRandomPartitionSequence(getPrefixedName(cacheName), partition, givenSequence, member);
    distortRandomPartitionUuid(partition, givenUuid, member);
    ClientConfig clientConfig = new ClientConfig().addNearCacheConfig(new NearCacheConfig(cacheName));
    HazelcastClientProxy client = (HazelcastClientProxy) factory.newHazelcastClient(clientConfig);
    CachingProvider clientCachingProvider = HazelcastClientCachingProvider.createCachingProvider(client);
    Cache<Integer, Integer> clientCache = clientCachingProvider.getCacheManager().createCache(cacheName, newCacheConfig());
    ClientContext clientContext = ((ClientCacheProxy) clientCache).getClientContext();
    return clientContext.getRepairingTask(SERVICE_NAME);
}
Also used : ClientCacheProxy(com.hazelcast.client.cache.impl.ClientCacheProxy) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ClientContext(com.hazelcast.client.spi.ClientContext) NearCacheConfig(com.hazelcast.config.NearCacheConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) HazelcastClientProxy(com.hazelcast.client.impl.HazelcastClientProxy) CachingProvider(javax.cache.spi.CachingProvider) HazelcastClientCachingProvider(com.hazelcast.client.cache.impl.HazelcastClientCachingProvider)

Example 49 with CachingProvider

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

the class ClientCacheNearCacheConfigTest method native_backed_nearCache_throws_illegalArgumentException_whenNoNativeConfigAvailable.

@Test(expected = IllegalArgumentException.class)
public void native_backed_nearCache_throws_illegalArgumentException_whenNoNativeConfigAvailable() throws Exception {
    String xml = "<hazelcast-client xmlns=\"http://www.hazelcast.com/schema/client-config\">" + "<near-cache name=\"test\">" + "<in-memory-format>NATIVE</in-memory-format>" + "</near-cache></hazelcast-client>";
    ClientConfig clientConfig = buildConfig(xml);
    HazelcastClientProxy client = (HazelcastClientProxy) factory.newHazelcastClient(clientConfig);
    CachingProvider clientCachingProvider = HazelcastClientCachingProvider.createCachingProvider(client);
    CacheManager cacheManager = clientCachingProvider.getCacheManager();
    cacheManager.createCache("test", newCacheConfig());
}
Also used : CacheManager(javax.cache.CacheManager) ClientConfig(com.hazelcast.client.config.ClientConfig) HazelcastClientProxy(com.hazelcast.client.impl.HazelcastClientProxy) CachingProvider(javax.cache.spi.CachingProvider) HazelcastClientCachingProvider(com.hazelcast.client.cache.impl.HazelcastClientCachingProvider) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 50 with CachingProvider

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

the class InvalidationMemberAddRemoveTest method ensure_nearCachedClient_and_member_data_sync_eventually.

@Test
public void ensure_nearCachedClient_and_member_data_sync_eventually() throws Exception {
    final AtomicBoolean stopTest = new AtomicBoolean();
    final Config config = createConfig();
    hazelcastFactory.newHazelcastInstance(config);
    CachingProvider provider = HazelcastServerCachingProvider.createCachingProvider(serverInstance);
    final CacheManager serverCacheManager = provider.getCacheManager();
    // populated from member.
    final Cache<Integer, Integer> memberCache = serverCacheManager.createCache(DEFAULT_CACHE_NAME, createCacheConfig(BINARY));
    for (int i = 0; i < KEY_COUNT; 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));
    ArrayList<Thread> threads = new ArrayList<Thread>();
    // continuously adds and removes member
    Thread shadowMember = new Thread(new Runnable() {

        @Override
        public void run() {
            while (!stopTest.get()) {
                HazelcastInstance member = hazelcastFactory.newHazelcastInstance(config);
                sleepSeconds(5);
                member.getLifecycleService().terminate();
            }
        }
    });
    threads.add(shadowMember);
    for (int i = 0; i < NEAR_CACHE_POPULATOR_THREAD_COUNT; i++) {
        // populates client near-cache
        Thread populateClientNearCache = new Thread(new Runnable() {

            public void run() {
                while (!stopTest.get()) {
                    for (int i = 0; i < KEY_COUNT; i++) {
                        clientCache.get(i);
                    }
                }
            }
        });
        threads.add(populateClientNearCache);
    }
    // updates data from member.
    Thread putFromMember = new Thread(new Runnable() {

        public void run() {
            while (!stopTest.get()) {
                int key = getInt(KEY_COUNT);
                int value = getInt(Integer.MAX_VALUE);
                memberCache.put(key, value);
                sleepAtLeastMillis(2);
            }
        }
    });
    threads.add(putFromMember);
    Thread clearFromMember = new Thread(new Runnable() {

        public void run() {
            while (!stopTest.get()) {
                memberCache.clear();
                sleepSeconds(3);
            }
        }
    });
    threads.add(clearFromMember);
    // start threads
    for (Thread thread : threads) {
        thread.start();
    }
    // stress system some seconds
    sleepSeconds(TEST_RUN_SECONDS);
    //stop threads
    stopTest.set(true);
    for (Thread thread : threads) {
        thread.join();
    }
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            for (int i = 0; i < KEY_COUNT; i++) {
                Integer valueSeenFromMember = memberCache.get(i);
                Integer valueSeenFromClient = clientCache.get(i);
                String msg = createFailureMessage(i);
                assertEquals(msg, valueSeenFromMember, valueSeenFromClient);
            }
        }

        private String createFailureMessage(int i) {
            InternalPartitionService partitionService = getPartitionService(serverInstance);
            Data keyData = getSerializationService(serverInstance).toData(i);
            int partitionId = partitionService.getPartitionId(keyData);
            AbstractNearCacheRecordStore nearCacheRecordStore = getAbstractNearCacheRecordStore();
            NearCacheRecord record = nearCacheRecordStore.getRecord(keyData);
            long recordSequence = record == null ? NO_SEQUENCE : record.getInvalidationSequence();
            MetaDataGenerator metaDataGenerator = getMetaDataGenerator();
            long memberSequence = metaDataGenerator.currentSequence("/hz/" + DEFAULT_CACHE_NAME, partitionId);
            MetaDataContainer metaDataContainer = nearCacheRecordStore.getStaleReadDetector().getMetaDataContainer(keyData);
            return String.format("partition=%d, onRecordSequence=%d, latestSequence=%d, staleSequence=%d, memberSequence=%d", partitionService.getPartitionId(keyData), recordSequence, metaDataContainer.getSequence(), metaDataContainer.getStaleSequence(), memberSequence);
        }

        private MetaDataGenerator getMetaDataGenerator() {
            CacheEventHandler cacheEventHandler = ((CacheService) ((CacheProxy) memberCache).getService()).getCacheEventHandler();
            return cacheEventHandler.getMetaDataGenerator();
        }

        private AbstractNearCacheRecordStore getAbstractNearCacheRecordStore() {
            DefaultNearCache defaultNearCache = (DefaultNearCache) ((ClientCacheProxy) clientCache).getNearCache().unwrap(DefaultNearCache.class);
            return (AbstractNearCacheRecordStore) defaultNearCache.getNearCacheRecordStore();
        }
    });
}
Also used : DefaultNearCache(com.hazelcast.internal.nearcache.impl.DefaultNearCache) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) CacheConfig(com.hazelcast.config.CacheConfig) NearCacheConfig(com.hazelcast.config.NearCacheConfig) ArrayList(java.util.ArrayList) CacheEventHandler(com.hazelcast.cache.impl.CacheEventHandler) MetaDataContainer(com.hazelcast.internal.nearcache.impl.invalidation.MetaDataContainer) ClientCacheProxy(com.hazelcast.client.cache.impl.ClientCacheProxy) CacheProxy(com.hazelcast.cache.impl.CacheProxy) CacheManager(javax.cache.CacheManager) AbstractNearCacheRecordStore(com.hazelcast.internal.nearcache.impl.store.AbstractNearCacheRecordStore) ClientConfig(com.hazelcast.client.config.ClientConfig) HazelcastClientProxy(com.hazelcast.client.impl.HazelcastClientProxy) MetaDataGenerator(com.hazelcast.internal.nearcache.impl.invalidation.MetaDataGenerator) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) HazelcastClientCachingProvider(com.hazelcast.client.cache.impl.HazelcastClientCachingProvider) InternalPartitionService(com.hazelcast.internal.partition.InternalPartitionService) Data(com.hazelcast.nio.serialization.Data) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HazelcastInstance(com.hazelcast.core.HazelcastInstance) NearCacheRecord(com.hazelcast.internal.nearcache.NearCacheRecord) AssertTask(com.hazelcast.test.AssertTask) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

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