Search in sources :

Example 11 with HazelcastClientProxy

use of com.hazelcast.client.impl.HazelcastClientProxy in project hazelcast by hazelcast.

the class HazelcastClientManager method shutdownAll.

public static void shutdownAll() {
    for (HazelcastClientProxy proxy : INSTANCE.clients.values()) {
        HazelcastClientInstanceImpl client = proxy.client;
        if (client == null) {
            continue;
        }
        proxy.client = null;
        try {
            client.shutdown();
        } catch (Throwable ignored) {
            EmptyStatement.ignore(ignored);
        }
    }
    OutOfMemoryErrorDispatcher.clearClients();
    INSTANCE.clients.clear();
}
Also used : HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) HazelcastClientProxy(com.hazelcast.client.impl.HazelcastClientProxy)

Example 12 with HazelcastClientProxy

use of com.hazelcast.client.impl.HazelcastClientProxy in project hazelcast by hazelcast.

the class HazelcastClientManager method shutdown.

public static void shutdown(HazelcastInstance instance) {
    if (instance instanceof HazelcastClientProxy) {
        final HazelcastClientProxy proxy = (HazelcastClientProxy) instance;
        HazelcastClientInstanceImpl client = proxy.client;
        if (client == null) {
            return;
        }
        proxy.client = null;
        INSTANCE.clients.remove(client.getName());
        try {
            client.shutdown();
        } catch (Throwable ignored) {
            EmptyStatement.ignore(ignored);
        } finally {
            OutOfMemoryErrorDispatcher.deregisterClient(client);
        }
    }
}
Also used : HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) HazelcastClientProxy(com.hazelcast.client.impl.HazelcastClientProxy)

Example 13 with HazelcastClientProxy

use of com.hazelcast.client.impl.HazelcastClientProxy 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 14 with HazelcastClientProxy

use of com.hazelcast.client.impl.HazelcastClientProxy 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 15 with HazelcastClientProxy

use of com.hazelcast.client.impl.HazelcastClientProxy 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)

Aggregations

HazelcastClientProxy (com.hazelcast.client.impl.HazelcastClientProxy)18 ClientConfig (com.hazelcast.client.config.ClientConfig)13 HazelcastClientCachingProvider (com.hazelcast.client.cache.impl.HazelcastClientCachingProvider)9 HazelcastInstance (com.hazelcast.core.HazelcastInstance)9 Data (com.hazelcast.nio.serialization.Data)9 CachingProvider (javax.cache.spi.CachingProvider)9 NearCacheManager (com.hazelcast.internal.nearcache.NearCacheManager)8 HazelcastServerCachingProvider (com.hazelcast.cache.impl.HazelcastServerCachingProvider)7 NearCacheTestContext (com.hazelcast.internal.nearcache.NearCacheTestContext)6 HazelcastClientCacheManager (com.hazelcast.client.cache.impl.HazelcastClientCacheManager)5 HazelcastClientInstanceImpl (com.hazelcast.client.impl.HazelcastClientInstanceImpl)5 CacheConfig (com.hazelcast.config.CacheConfig)5 NearCacheConfig (com.hazelcast.config.NearCacheConfig)5 Config (com.hazelcast.config.Config)4 Test (org.junit.Test)4 HazelcastServerCacheManager (com.hazelcast.cache.impl.HazelcastServerCacheManager)3 NightlyTest (com.hazelcast.test.annotation.NightlyTest)3 CacheManager (javax.cache.CacheManager)3 ClientCacheProxy (com.hazelcast.client.cache.impl.ClientCacheProxy)2 XmlClientConfigBuilder (com.hazelcast.client.config.XmlClientConfigBuilder)2