Search in sources :

Example 1 with DefaultNearCache

use of com.hazelcast.internal.nearcache.impl.DefaultNearCache in project hazelcast by hazelcast.

the class ClientCacheRecordStateStressTest method assertFinalRecordStateIsReadPermitted.

private void assertFinalRecordStateIsReadPermitted(Cache clientCache, HazelcastInstance member) {
    ClientCacheProxy proxy = ((ClientCacheProxy) clientCache);
    NearCache nearCache = proxy.getNearCache();
    DefaultNearCache unwrap = (DefaultNearCache) nearCache.unwrap(DefaultNearCache.class);
    InternalSerializationService ss = getSerializationService(member);
    for (int i = 0; i < KEY_SPACE; i++) {
        Data key = ss.toData(i);
        AbstractNearCacheRecordStore nearCacheRecordStore = (AbstractNearCacheRecordStore) unwrap.getNearCacheRecordStore();
        NearCacheRecord record = nearCacheRecordStore.getRecord(key);
        if (record != null) {
            assertEquals(record.toString(), READ_PERMITTED, record.getRecordState());
        }
    }
}
Also used : ClientCacheProxy(com.hazelcast.client.cache.impl.ClientCacheProxy) DefaultNearCache(com.hazelcast.internal.nearcache.impl.DefaultNearCache) NearCacheRecord(com.hazelcast.internal.nearcache.NearCacheRecord) DefaultNearCache(com.hazelcast.internal.nearcache.impl.DefaultNearCache) NearCache(com.hazelcast.internal.nearcache.NearCache) Data(com.hazelcast.nio.serialization.Data) AbstractNearCacheRecordStore(com.hazelcast.internal.nearcache.impl.store.AbstractNearCacheRecordStore) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService)

Example 2 with DefaultNearCache

use of com.hazelcast.internal.nearcache.impl.DefaultNearCache in project hazelcast by hazelcast.

the class ClientCacheInvalidationMemberAddRemoveTest method ensure_nearCachedClient_and_member_data_sync_eventually.

@Test
public void ensure_nearCachedClient_and_member_data_sync_eventually() {
    final AtomicBoolean stopTest = new AtomicBoolean();
    final Config config = createConfig();
    secondNode = hazelcastFactory.newHazelcastInstance(config);
    CachingProvider provider = createServerCachingProvider(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().addNearCacheConfig(createNearCacheConfig(BINARY));
    HazelcastClientProxy client = (HazelcastClientProxy) hazelcastFactory.newHazelcastClient(clientConfig);
    CachingProvider clientCachingProvider = createClientCachingProvider(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_POPULATE_THREAD_COUNT; i++) {
        // populates client Near Cache
        Thread populateClientNearCache = new Thread(new Runnable() {

            public void run() {
                int i = 0;
                while (!stopTest.get()) {
                    clientCache.get(i++);
                    if (i == KEY_COUNT) {
                        i = 0;
                    }
                }
            }
        });
        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) {
        assertJoinable(thread);
    }
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() {
            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);
            }
        }

        @SuppressWarnings("unchecked")
        private String createFailureMessage(int i) {
            int partitionId = getPartitionService(serverInstance).getPartitionId(i);
            NearCacheRecordStore nearCacheRecordStore = getNearCacheRecordStore();
            NearCacheRecord record = nearCacheRecordStore.getRecord(i);
            long recordSequence = record == null ? NO_SEQUENCE : record.getInvalidationSequence();
            // member-1
            MetaDataGenerator metaDataGenerator1 = getMetaDataGenerator(serverInstance);
            long memberSequence1 = metaDataGenerator1.currentSequence("/hz/" + DEFAULT_CACHE_NAME, partitionId);
            UUID memberUuid1 = metaDataGenerator1.getUuidOrNull(partitionId);
            // member-2
            MetaDataGenerator metaDataGenerator2 = getMetaDataGenerator(secondNode);
            long memberSequence2 = metaDataGenerator2.currentSequence("/hz/" + DEFAULT_CACHE_NAME, partitionId);
            UUID memberUuid2 = metaDataGenerator2.getUuidOrNull(partitionId);
            StaleReadDetector staleReadDetector = getStaleReadDetector(nearCacheRecordStore);
            MetaDataContainer metaDataContainer = staleReadDetector.getMetaDataContainer(partitionId);
            return format("On client: [uuid=%s, partition=%d, onRecordSequence=%d, latestSequence=%d, staleSequence=%d]," + "%nOn members: [memberUuid1=%s, memberSequence1=%d, memberUuid2=%s, memberSequence2=%d]", metaDataContainer.getUuid(), partitionId, recordSequence, metaDataContainer.getSequence(), metaDataContainer.getStaleSequence(), memberUuid1, memberSequence1, memberUuid2, memberSequence2);
        }

        private MetaDataGenerator getMetaDataGenerator(HazelcastInstance node) {
            CacheService service = getNodeEngineImpl(node).getService(CacheService.SERVICE_NAME);
            CacheEventHandler cacheEventHandler = service.getCacheEventHandler();
            return cacheEventHandler.getMetaDataGenerator();
        }

        private NearCacheRecordStore getNearCacheRecordStore() {
            NearCache nearCache = ((NearCachedClientCacheProxy) clientCache).getNearCache();
            DefaultNearCache defaultNearCache = (DefaultNearCache) nearCache.unwrap(DefaultNearCache.class);
            return defaultNearCache.getNearCacheRecordStore();
        }
    });
}
Also used : StaleReadDetector(com.hazelcast.internal.nearcache.impl.invalidation.StaleReadDetector) DefaultNearCache(com.hazelcast.internal.nearcache.impl.DefaultNearCache) CacheConfig(com.hazelcast.config.CacheConfig) NearCacheConfig(com.hazelcast.config.NearCacheConfig) EvictionConfig(com.hazelcast.config.EvictionConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) ArrayList(java.util.ArrayList) DefaultNearCache(com.hazelcast.internal.nearcache.impl.DefaultNearCache) NearCache(com.hazelcast.internal.nearcache.NearCache) CacheEventHandler(com.hazelcast.cache.impl.CacheEventHandler) MetaDataContainer(com.hazelcast.internal.nearcache.impl.invalidation.MetaDataContainer) CacheManager(javax.cache.CacheManager) ClientConfig(com.hazelcast.client.config.ClientConfig) HazelcastClientProxy(com.hazelcast.client.impl.clientside.HazelcastClientProxy) MetaDataGenerator(com.hazelcast.internal.nearcache.impl.invalidation.MetaDataGenerator) UUID(java.util.UUID) CacheTestSupport.createServerCachingProvider(com.hazelcast.cache.CacheTestSupport.createServerCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) CacheTestSupport.createClientCachingProvider(com.hazelcast.cache.CacheTestSupport.createClientCachingProvider) CacheService(com.hazelcast.cache.impl.CacheService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AbstractNearCacheRecordStore(com.hazelcast.internal.nearcache.impl.store.AbstractNearCacheRecordStore) NearCacheRecordStore(com.hazelcast.internal.nearcache.NearCacheRecordStore) NearCacheRecord(com.hazelcast.internal.nearcache.NearCacheRecord) AssertTask(com.hazelcast.test.AssertTask) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 3 with DefaultNearCache

use of com.hazelcast.internal.nearcache.impl.DefaultNearCache in project hazelcast by hazelcast.

the class ClientMapNearCacheRecordStateStressTest method assertFinalRecordStateIsReadPermitted.

@SuppressWarnings("unchecked")
private static void assertFinalRecordStateIsReadPermitted(IMap clientMap) {
    NearCachedClientMapProxy proxy = (NearCachedClientMapProxy) clientMap;
    DefaultNearCache nearCache = (DefaultNearCache) proxy.getNearCache().unwrap(DefaultNearCache.class);
    assertNearCacheRecordStates(nearCache, KEY_SPACE);
}
Also used : DefaultNearCache(com.hazelcast.internal.nearcache.impl.DefaultNearCache)

Example 4 with DefaultNearCache

use of com.hazelcast.internal.nearcache.impl.DefaultNearCache 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)

Example 5 with DefaultNearCache

use of com.hazelcast.internal.nearcache.impl.DefaultNearCache in project hazelcast by hazelcast.

the class ClientMapRecordStateStressTest method assertFinalRecordStateIsReadPermitted.

private void assertFinalRecordStateIsReadPermitted(IMap clientMap, InternalSerializationService ss) {
    NearCachedClientMapProxy proxy = (NearCachedClientMapProxy) clientMap;
    NearCache nearCache = proxy.getNearCache();
    DefaultNearCache unwrap = (DefaultNearCache) nearCache.unwrap(DefaultNearCache.class);
    for (int i = 0; i < KEY_SPACE; i++) {
        Data key = ss.toData(i);
        AbstractNearCacheRecordStore nearCacheRecordStore = (AbstractNearCacheRecordStore) unwrap.getNearCacheRecordStore();
        NearCacheRecord record = nearCacheRecordStore.getRecord(key);
        if (record != null) {
            assertEquals(record.toString(), READ_PERMITTED, record.getRecordState());
        }
    }
}
Also used : DefaultNearCache(com.hazelcast.internal.nearcache.impl.DefaultNearCache) NearCacheRecord(com.hazelcast.internal.nearcache.NearCacheRecord) DefaultNearCache(com.hazelcast.internal.nearcache.impl.DefaultNearCache) NearCache(com.hazelcast.internal.nearcache.NearCache) Data(com.hazelcast.nio.serialization.Data) AbstractNearCacheRecordStore(com.hazelcast.internal.nearcache.impl.store.AbstractNearCacheRecordStore) NearCachedClientMapProxy(com.hazelcast.client.proxy.NearCachedClientMapProxy)

Aggregations

DefaultNearCache (com.hazelcast.internal.nearcache.impl.DefaultNearCache)8 NearCacheRecord (com.hazelcast.internal.nearcache.NearCacheRecord)5 AbstractNearCacheRecordStore (com.hazelcast.internal.nearcache.impl.store.AbstractNearCacheRecordStore)5 Data (com.hazelcast.nio.serialization.Data)4 NearCache (com.hazelcast.internal.nearcache.NearCache)3 CacheEventHandler (com.hazelcast.cache.impl.CacheEventHandler)2 ClientCacheProxy (com.hazelcast.client.cache.impl.ClientCacheProxy)2 ClientConfig (com.hazelcast.client.config.ClientConfig)2 CacheConfig (com.hazelcast.config.CacheConfig)2 Config (com.hazelcast.config.Config)2 NearCacheConfig (com.hazelcast.config.NearCacheConfig)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)2 MetaDataContainer (com.hazelcast.internal.nearcache.impl.invalidation.MetaDataContainer)2 MetaDataGenerator (com.hazelcast.internal.nearcache.impl.invalidation.MetaDataGenerator)2 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)2 AssertTask (com.hazelcast.test.AssertTask)2 NightlyTest (com.hazelcast.test.annotation.NightlyTest)2 ArrayList (java.util.ArrayList)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 CacheManager (javax.cache.CacheManager)2