Search in sources :

Example 51 with HazelcastClientInstanceImpl

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

the class ProxyFactoryTest method testProxyCreation.

private void testProxyCreation(String serviceName, ClientConfig clientConfig) {
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
    HazelcastClientInstanceImpl clientInstanceImpl = getHazelcastClientInstanceImpl(client);
    context = clientInstanceImpl.getProxyManager().getContext();
    ClientProxy proxy = client.getDistributedObject(serviceName, "CustomClientProxy");
    assertEquals(serviceName, proxy.getServiceName());
    assertEquals("CustomClientProxy", proxy.getName());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastClientInstanceImpl(com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl) ClientTestUtil.getHazelcastClientInstanceImpl(com.hazelcast.client.impl.clientside.ClientTestUtil.getHazelcastClientInstanceImpl)

Example 52 with HazelcastClientInstanceImpl

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

the class ClientQueryCacheDestroyResourcesTest method destroy_deregisters_listeners.

@Test
public void destroy_deregisters_listeners() {
    QueryCache<String, String> queryCache1 = map1.getQueryCache(QUERY_CACHE_NAME_1);
    QueryCache<String, String> queryCache2 = map2.getQueryCache(QUERY_CACHE_NAME_2);
    QueryCache<String, String> queryCache3 = map3.getQueryCache(QUERY_CACHE_NAME_3);
    HazelcastClientInstanceImpl client = getHazelcastClientInstanceImpl(clientInstance);
    ClientListenerServiceImpl listenerService = (ClientListenerServiceImpl) client.getListenerService();
    int numberOfListenersBeforeDestroy = listenerService.getRegistrations().size();
    queryCache1.destroy();
    queryCache2.destroy();
    queryCache3.destroy();
    final Map<UUID, ClientListenerRegistration> registrations = listenerService.getRegistrations();
    // we expect at least 1 for backup listener and 1 listener for ProxyManager and
    // we expect 3 listeners to be deleted one for each queryCache.
    assertEquals(registrations.toString(), 3, numberOfListenersBeforeDestroy - registrations.size());
}
Also used : ClientListenerServiceImpl(com.hazelcast.client.impl.spi.impl.listener.ClientListenerServiceImpl) HazelcastClientInstanceImpl(com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl) UUID(java.util.UUID) ClientListenerRegistration(com.hazelcast.client.impl.spi.impl.listener.ClientListenerRegistration) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 53 with HazelcastClientInstanceImpl

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

the class AsyncMapImpl method putAllAsync.

@SuppressWarnings("unchecked")
public CompletionStage<Void> putAllAsync(Map<? extends K, ? extends V> items) {
    ClientMapProxy<K, V> targetMap = (ClientMapProxy<K, V>) map;
    if (items.isEmpty()) {
        return completedFuture(null);
    }
    checkNotNull(targetMap, "Null argument map is not allowed");
    ClientPartitionService partitionService = targetMap.getContext().getPartitionService();
    int partitionCount = partitionService.getPartitionCount();
    Map<Integer, List<Entry<Data, Data>>> entryMap = new HashMap<>(partitionCount);
    InternalSerializationService serializationService = targetMap.getContext().getSerializationService();
    for (Entry<? extends K, ? extends V> entry : items.entrySet()) {
        checkNotNull(entry.getKey(), "Null key is not allowed");
        checkNotNull(entry.getValue(), "Null value is not allowed");
        Data keyData = serializationService.toData(entry.getKey());
        int partitionId = partitionService.getPartitionId(keyData);
        entryMap.computeIfAbsent(partitionId, k -> new ArrayList<>()).add(new AbstractMap.SimpleEntry<>(keyData, serializationService.toData(entry.getValue())));
    }
    HazelcastClientInstanceImpl client = (HazelcastClientInstanceImpl) targetMap.getContext().getHazelcastInstance();
    CompletableFuture<Void> resultFuture = new CompletableFuture<>();
    ExecutionCallback callback = createPutAllCallback(entryMap.size(), targetMap instanceof NearCachedClientMapProxy ? ((NearCachedClientMapProxy) targetMap).getNearCache() : null, items.keySet(), entryMap.values().stream().flatMap(List::stream).map(Entry::getKey), resultFuture);
    for (Entry<Integer, List<Entry<Data, Data>>> partitionEntries : entryMap.entrySet()) {
        Integer partitionId = partitionEntries.getKey();
        // use setAsync if there's only one entry
        if (partitionEntries.getValue().size() == 1) {
            Entry<Data, Data> onlyEntry = partitionEntries.getValue().get(0);
            // cast to raw so that we can pass serialized key and value
            ((IMap) targetMap).setAsync(onlyEntry.getKey(), onlyEntry.getValue()).andThen(callback);
        } else {
            ClientMessage request = MapPutAllCodec.encodeRequest(targetMap.getName(), partitionEntries.getValue());
            new ClientInvocation(client, request, targetMap.getName(), partitionId).invoke().andThen(callback);
        }
    }
    return resultFuture;
}
Also used : HazelcastClientInstanceImpl(com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) NearCachedClientMapProxy(com.hazelcast.client.proxy.NearCachedClientMapProxy) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) Preconditions.checkNotNull(com.hazelcast.util.Preconditions.checkNotNull) Map(java.util.Map) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ClientMapProxy(com.hazelcast.client.proxy.ClientMapProxy) AsyncMap(com.hazelcast.connector.map.AsyncMap) Data(com.hazelcast.nio.serialization.Data) Collection(java.util.Collection) Set(java.util.Set) MapPutAllCodec(com.hazelcast.client.impl.protocol.codec.MapPutAllCodec) NearCache(com.hazelcast.internal.nearcache.NearCache) AbstractMap(java.util.AbstractMap) List(java.util.List) IMap(com.hazelcast.core.IMap) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) ClientInvocation(com.hazelcast.client.spi.impl.ClientInvocation) ClientPartitionService(com.hazelcast.client.spi.ClientPartitionService) ExecutionCallback(com.hazelcast.core.ExecutionCallback) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ClientInvocation(com.hazelcast.client.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) NearCachedClientMapProxy(com.hazelcast.client.proxy.NearCachedClientMapProxy) ClientMapProxy(com.hazelcast.client.proxy.ClientMapProxy) AbstractMap(java.util.AbstractMap) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) List(java.util.List) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ClientPartitionService(com.hazelcast.client.spi.ClientPartitionService) NearCachedClientMapProxy(com.hazelcast.client.proxy.NearCachedClientMapProxy) ExecutionCallback(com.hazelcast.core.ExecutionCallback) Data(com.hazelcast.nio.serialization.Data) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HazelcastClientInstanceImpl(com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl)

Example 54 with HazelcastClientInstanceImpl

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

the class TestHazelcastFactory method newHazelcastClient.

public HazelcastInstance newHazelcastClient(ClientConfig config, String sourceIp) {
    if (!mockNetwork) {
        HazelcastInstance client = HazelcastClient.newHazelcastClient(config);
        registerJvmNameAndPidMetric(((HazelcastClientProxy) client).client);
        return client;
    }
    if (config == null) {
        config = new XmlClientConfigBuilder().build();
    }
    Thread currentThread = Thread.currentThread();
    ClassLoader tccl = currentThread.getContextClassLoader();
    try {
        if (tccl == ClassLoader.getSystemClassLoader()) {
            currentThread.setContextClassLoader(HazelcastClient.class.getClassLoader());
        }
        HazelcastClientInstanceImpl client = new HazelcastClientInstanceImpl(getInstanceName(config), config, null, clientRegistry.createClientServiceFactory(sourceIp), createAddressProvider(config));
        registerJvmNameAndPidMetric(client);
        client.start();
        if (clients.putIfAbsent(client.getName(), client) != null) {
            throw new InvalidConfigurationException("HazelcastClientInstance with name '" + client.getName() + "' already exists!");
        }
        OutOfMemoryErrorDispatcher.registerClient(client);
        return new HazelcastClientProxy(client);
    } finally {
        currentThread.setContextClassLoader(tccl);
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) XmlClientConfigBuilder(com.hazelcast.client.config.XmlClientConfigBuilder) HazelcastClientInstanceImpl(com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl) HazelcastClient(com.hazelcast.client.HazelcastClient) HazelcastClientProxy(com.hazelcast.client.impl.clientside.HazelcastClientProxy) InvalidConfigurationException(com.hazelcast.config.InvalidConfigurationException)

Example 55 with HazelcastClientInstanceImpl

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

the class ClientStatisticsTest method testStatisticsCollectionNonDefaultPeriod.

@Test
public void testStatisticsCollectionNonDefaultPeriod() {
    HazelcastInstance hazelcastInstance = hazelcastFactory.newHazelcastInstance();
    final HazelcastClientInstanceImpl client = createHazelcastClient();
    final ClientEngineImpl clientEngine = getClientEngineImpl(hazelcastInstance);
    long clientConnectionTime = System.currentTimeMillis();
    // wait enough time for statistics collection
    waitForFirstStatisticsCollection(client, clientEngine);
    Map<String, String> stats = getStats(client, clientEngine);
    String connStat = stats.get("clusterConnectionTimestamp");
    assertNotNull(format("clusterConnectionTimestamp should not be null (%s)", stats), connStat);
    Long connectionTimeStat = Long.valueOf(connStat);
    assertNotNull(format("connectionTimeStat should not be null (%s)", stats), connStat);
    TcpClientConnection aConnection = (TcpClientConnection) client.getConnectionManager().getActiveConnections().iterator().next();
    String expectedClientAddress = aConnection.getLocalSocketAddress().getAddress().getHostAddress();
    assertEquals(expectedClientAddress, stats.get("clientAddress"));
    assertEquals(BuildInfoProvider.getBuildInfo().getVersion(), stats.get("clientVersion"));
    assertEquals(client.getName(), stats.get("clientName"));
    // time measured by us after client connection should be greater than the connection time reported by the statistics
    assertTrue(format("connectionTimeStat was %d, clientConnectionTime was %d (%s)", connectionTimeStat, clientConnectionTime, stats), clientConnectionTime >= connectionTimeStat);
    String mapHits = stats.get(MAP_HITS_KEY);
    assertNull(format("%s should be null (%s)", MAP_HITS_KEY, stats), mapHits);
    String cacheHits = stats.get(CACHE_HITS_KEY);
    assertNull(format("%s should be null (%s)", CACHE_HITS_KEY, stats), cacheHits);
    String lastStatisticsCollectionTimeString = stats.get("lastStatisticsCollectionTime");
    final long lastCollectionTime = Long.parseLong(lastStatisticsCollectionTimeString);
    // this creates empty map statistics
    client.getMap(MAP_NAME);
    // wait enough time for statistics collection
    waitForNextStatsCollection(client, clientEngine, lastStatisticsCollectionTimeString);
    assertTrueEventually(() -> {
        Map<String, String> stats12 = getStats(client, clientEngine);
        String mapHits12 = stats12.get(MAP_HITS_KEY);
        assertNotNull(format("%s should not be null (%s)", MAP_HITS_KEY, stats12), mapHits12);
        assertEquals(format("Expected 0 map hits (%s)", stats12), "0", mapHits12);
        String cacheHits12 = stats12.get(CACHE_HITS_KEY);
        assertNull(format("%s should be null (%s)", CACHE_HITS_KEY, stats12), cacheHits12);
        // verify that collection is periodic
        verifyThatCollectionIsPeriodic(stats12, lastCollectionTime);
    });
    // produce map and cache stat
    produceSomeStats(hazelcastInstance, client);
    assertTrueEventually(() -> {
        Map<String, String> stats1 = getStats(client, clientEngine);
        String mapHits1 = stats1.get(MAP_HITS_KEY);
        assertNotNull(format("%s should not be null (%s)", MAP_HITS_KEY, stats1), mapHits1);
        assertEquals(format("Expected 1 map hits (%s)", stats1), "1", mapHits1);
        String cacheHits1 = stats1.get(CACHE_HITS_KEY);
        assertNotNull(format("%s should not be null (%s)", CACHE_HITS_KEY, stats1), cacheHits1);
        assertEquals(format("Expected 1 cache hits (%s)", stats1), "1", cacheHits1);
    });
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ClientEngineImpl(com.hazelcast.client.impl.ClientEngineImpl) Accessors.getClientEngineImpl(com.hazelcast.test.Accessors.getClientEngineImpl) HazelcastClientInstanceImpl(com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl) TcpClientConnection(com.hazelcast.client.impl.connection.tcp.TcpClientConnection) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

HazelcastClientInstanceImpl (com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl)59 Test (org.junit.Test)24 HazelcastInstance (com.hazelcast.core.HazelcastInstance)23 QuickTest (com.hazelcast.test.annotation.QuickTest)23 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)21 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)19 ClientInvocation (com.hazelcast.client.impl.spi.impl.ClientInvocation)15 UUID (java.util.UUID)14 ClientConfig (com.hazelcast.client.config.ClientConfig)11 ClientConnectionManager (com.hazelcast.client.impl.connection.ClientConnectionManager)9 HazelcastClientProxy (com.hazelcast.client.impl.clientside.HazelcastClientProxy)7 ClientEngineImpl (com.hazelcast.client.impl.ClientEngineImpl)6 UuidUtil.newUnsecureUUID (com.hazelcast.internal.util.UuidUtil.newUnsecureUUID)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 TcpClientConnectionManager (com.hazelcast.client.impl.connection.tcp.TcpClientConnectionManager)4 Address (com.hazelcast.cluster.Address)4 SessionExpiredException (com.hazelcast.cp.internal.session.SessionExpiredException)4 Accessors.getClientEngineImpl (com.hazelcast.test.Accessors.getClientEngineImpl)4 LoadBalancer (com.hazelcast.client.LoadBalancer)3 MCClusterMetadata (com.hazelcast.client.impl.management.MCClusterMetadata)3