Search in sources :

Example 6 with HazelcastClientProxy

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

the class HazelcastClient method shutdownAll.

/**
 * Shuts down all the client HazelcastInstance created in this JVM.
 * <p>
 * To be more precise it shuts down the HazelcastInstances loaded using the same classloader this HazelcastClient has been
 * loaded with.
 * <p>
 * This method is mostly used for testing purposes.
 *
 * @see #getAllHazelcastClients()
 */
public static void shutdownAll() {
    for (InstanceFuture<HazelcastClientProxy> future : CLIENTS.values()) {
        try {
            HazelcastClientProxy proxy = future.get();
            HazelcastClientInstanceImpl client = proxy.client;
            if (client == null) {
                continue;
            }
            proxy.client = null;
            client.shutdown();
        } catch (Throwable ignored) {
            EmptyStatement.ignore(ignored);
        }
    }
    OutOfMemoryErrorDispatcher.clearClients();
    CLIENTS.clear();
}
Also used : HazelcastClientInstanceImpl(com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl) HazelcastClientProxy(com.hazelcast.client.impl.clientside.HazelcastClientProxy)

Example 7 with HazelcastClientProxy

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

the class HazelcastClient method shutdown.

/**
 * Shutdown the provided client and remove it from the managed list
 *
 * @param instanceName the hazelcast client instance name
 */
public static void shutdown(String instanceName) {
    InstanceFuture<HazelcastClientProxy> future = CLIENTS.remove(instanceName);
    if (future == null || !future.isSet()) {
        return;
    }
    HazelcastClientProxy proxy = future.get();
    HazelcastClientInstanceImpl client = proxy.client;
    if (client == null) {
        return;
    }
    proxy.client = null;
    try {
        client.shutdown();
    } catch (Throwable ignored) {
        EmptyStatement.ignore(ignored);
    } finally {
        OutOfMemoryErrorDispatcher.deregisterClient(client);
    }
}
Also used : HazelcastClientInstanceImpl(com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl) HazelcastClientProxy(com.hazelcast.client.impl.clientside.HazelcastClientProxy)

Example 8 with HazelcastClientProxy

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

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

the class ClientCacheNearCacheCacheOnUpdateTest method newNearCachedCache.

private ICache<Integer, Integer> newNearCachedCache(NearCacheConfig.LocalUpdatePolicy localUpdatePolicy) {
    NearCacheConfig nearCacheConfig = getNearCacheConfig(localUpdatePolicy);
    ClientConfig clientConfig = getClientConfig().addNearCacheConfig(nearCacheConfig);
    HazelcastClientProxy client = (HazelcastClientProxy) hazelcastFactory.newHazelcastClient(clientConfig);
    CachingProvider provider = new HazelcastClientCachingProvider(client);
    HazelcastClientCacheManager cacheManager = (HazelcastClientCacheManager) provider.getCacheManager();
    return cacheManager.createCache(DEFAULT_CACHE_NAME, newCacheConfig(InMemoryFormat.BINARY));
}
Also used : HazelcastClientCachingProvider(com.hazelcast.client.cache.impl.HazelcastClientCachingProvider) NearCacheConfig(com.hazelcast.config.NearCacheConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) HazelcastClientProxy(com.hazelcast.client.impl.clientside.HazelcastClientProxy) CachingProvider(javax.cache.spi.CachingProvider) HazelcastClientCachingProvider(com.hazelcast.client.cache.impl.HazelcastClientCachingProvider) HazelcastClientCacheManager(com.hazelcast.client.cache.impl.HazelcastClientCacheManager)

Example 10 with HazelcastClientProxy

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

the class ClientCacheNearCacheInvalidationTest method setup.

@Before
public void setup() {
    hazelcastFactory = new TestHazelcastFactory();
    HazelcastInstance[] allMembers = new HazelcastInstance[MEMBER_COUNT];
    for (int i = 0; i < MEMBER_COUNT; i++) {
        // every instance should have its own getConfig() call because an existing EE test relies on this
        Config config = getConfig().setProperty(CACHE_INVALIDATION_MESSAGE_BATCH_ENABLED.getName(), "false");
        allMembers[i] = hazelcastFactory.newHazelcastInstance(config);
    }
    waitAllForSafeState(allMembers);
    NearCacheConfig nearCacheConfig = getNearCacheConfig(inMemoryFormat);
    ClientConfig clientConfig = getClientConfig().addNearCacheConfig(nearCacheConfig);
    HazelcastClientProxy client = (HazelcastClientProxy) hazelcastFactory.newHazelcastClient(clientConfig);
    CachingProvider provider = createClientCachingProvider(client);
    HazelcastServerCachingProvider memberProvider = createServerCachingProvider(allMembers[0]);
    HazelcastClientCacheManager cacheManager = (HazelcastClientCacheManager) provider.getCacheManager();
    HazelcastServerCacheManager memberCacheManager = (HazelcastServerCacheManager) memberProvider.getCacheManager();
    CacheConfig<Integer, String> cacheConfig = getCacheConfig(inMemoryFormat);
    ICache<Integer, String> cache = cacheManager.createCache(DEFAULT_CACHE_NAME, cacheConfig);
    ICache<Integer, String> memberCache = memberCacheManager.getCache(getPrefixedCacheName(DEFAULT_CACHE_NAME, null, null));
    NearCacheManager nearCacheManager = ((ClientCacheProxy) cache).getContext().getNearCacheManager(cache.getServiceName());
    NearCache<Object, String> nearCache = nearCacheManager.getNearCache(cacheManager.getCacheNameWithPrefix(DEFAULT_CACHE_NAME));
    SerializationService serializationService = client.getSerializationService();
    invalidationListener = createInvalidationEventHandler(cache);
    NearCacheTestContextBuilder<Integer, String, Object, String> builder = new NearCacheTestContextBuilder<>(nearCacheConfig, serializationService);
    testContext = builder.setDataInstance(allMembers[0]).setNearCacheInstance(client).setDataAdapter(new ICacheDataStructureAdapter<>(memberCache)).setNearCacheAdapter(new ICacheDataStructureAdapter<>(cache)).setMemberCacheManager(memberCacheManager).setCacheManager(cacheManager).setNearCacheManager(nearCacheManager).setNearCache(nearCache).build();
}
Also used : NearCacheManager(com.hazelcast.internal.nearcache.NearCacheManager) CacheConfig(com.hazelcast.config.CacheConfig) NearCacheConfig(com.hazelcast.config.NearCacheConfig) NearCacheTestUtils.getBaseConfig(com.hazelcast.internal.nearcache.impl.NearCacheTestUtils.getBaseConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) NearCacheConfig(com.hazelcast.config.NearCacheConfig) HazelcastServerCacheManager(com.hazelcast.cache.impl.HazelcastServerCacheManager) SerializationService(com.hazelcast.internal.serialization.SerializationService) HazelcastInstance(com.hazelcast.core.HazelcastInstance) NearCacheTestContextBuilder(com.hazelcast.internal.nearcache.impl.NearCacheTestContextBuilder) ICacheDataStructureAdapter(com.hazelcast.internal.adapter.ICacheDataStructureAdapter) TestHazelcastFactory(com.hazelcast.client.test.TestHazelcastFactory) ClientConfig(com.hazelcast.client.config.ClientConfig) HazelcastClientProxy(com.hazelcast.client.impl.clientside.HazelcastClientProxy) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CacheTestSupport.createServerCachingProvider(com.hazelcast.cache.CacheTestSupport.createServerCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) CacheTestSupport.createClientCachingProvider(com.hazelcast.cache.CacheTestSupport.createClientCachingProvider) HazelcastClientCacheManager(com.hazelcast.client.cache.impl.HazelcastClientCacheManager) Before(org.junit.Before)

Aggregations

HazelcastClientProxy (com.hazelcast.client.impl.clientside.HazelcastClientProxy)42 ClientConfig (com.hazelcast.client.config.ClientConfig)27 Test (org.junit.Test)16 QuickTest (com.hazelcast.test.annotation.QuickTest)14 NearCacheManager (com.hazelcast.internal.nearcache.NearCacheManager)13 CachingProvider (javax.cache.spi.CachingProvider)13 CacheTestSupport.createClientCachingProvider (com.hazelcast.cache.CacheTestSupport.createClientCachingProvider)12 Data (com.hazelcast.internal.serialization.Data)12 CacheTestSupport.createServerCachingProvider (com.hazelcast.cache.CacheTestSupport.createServerCachingProvider)10 HazelcastInstance (com.hazelcast.core.HazelcastInstance)10 NearCacheTestContextBuilder (com.hazelcast.internal.nearcache.impl.NearCacheTestContextBuilder)9 HazelcastClientCacheManager (com.hazelcast.client.cache.impl.HazelcastClientCacheManager)7 HazelcastClientInstanceImpl (com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl)7 ClientMetricsConfig (com.hazelcast.client.config.ClientMetricsConfig)6 NearCacheConfig (com.hazelcast.config.NearCacheConfig)6 Config (com.hazelcast.config.Config)5 CacheManager (javax.cache.CacheManager)5 CacheConfig (com.hazelcast.config.CacheConfig)4 ICacheDataStructureAdapter (com.hazelcast.internal.adapter.ICacheDataStructureAdapter)4 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)4