Search in sources :

Example 6 with ClientPartitionService

use of com.hazelcast.client.impl.spi.ClientPartitionService in project hazelcast by hazelcast.

the class ClientMultiMapProxy method putAllInternal.

@SuppressWarnings({ "checkstyle:cyclomaticcomplexity", "checkstyle:npathcomplexity", "checkstyle:methodlength" })
private void putAllInternal(@Nonnull Map<Data, Collection<Data>> map, @Nonnull InternalCompletableFuture<Void> future) {
    if (map.isEmpty()) {
        future.complete(null);
        return;
    }
    ClientPartitionService partitionService = getContext().getPartitionService();
    int partitionCount = partitionService.getPartitionCount();
    Map<Integer, Collection<Map.Entry<Data, Collection<Data>>>> entryMap = new HashMap<>(partitionCount);
    for (Map.Entry<Data, Collection<Data>> entry : map.entrySet()) {
        checkNotNull(entry.getKey(), NULL_KEY_IS_NOT_ALLOWED);
        checkNotNull(entry.getValue(), NULL_VALUE_IS_NOT_ALLOWED);
        Data keyData = entry.getKey();
        int partitionId = partitionService.getPartitionId(keyData);
        Collection<Map.Entry<Data, Collection<Data>>> partition = entryMap.get(partitionId);
        if (partition == null) {
            partition = new ArrayList<>();
            entryMap.put(partitionId, partition);
        }
        partition.add(new AbstractMap.SimpleEntry<>(keyData, entry.getValue()));
    }
    assert entryMap.size() > 0;
    AtomicInteger counter = new AtomicInteger(entryMap.size());
    InternalCompletableFuture<Void> resultFuture = future;
    BiConsumer<ClientMessage, Throwable> callback = (response, t) -> {
        if (t != null) {
            resultFuture.completeExceptionally(t);
        }
        if (counter.decrementAndGet() == 0) {
            if (!resultFuture.isDone()) {
                resultFuture.complete(null);
            }
        }
    };
    for (Map.Entry<Integer, Collection<Map.Entry<Data, Collection<Data>>>> entry : entryMap.entrySet()) {
        Integer partitionId = entry.getKey();
        // if there is only one entry, consider how we can use MapPutRequest
        // without having to get back the return value
        ClientMessage request = MultiMapPutAllCodec.encodeRequest(name, entry.getValue());
        new ClientInvocation(getClient(), request, getName(), partitionId).invoke().whenCompleteAsync(callback);
    }
}
Also used : MultiMapPutCodec(com.hazelcast.client.impl.protocol.codec.MultiMapPutCodec) MultiMapContainsValueCodec(com.hazelcast.client.impl.protocol.codec.MultiMapContainsValueCodec) MultiMapAddEntryListenerCodec(com.hazelcast.client.impl.protocol.codec.MultiMapAddEntryListenerCodec) MultiMapForceUnlockCodec(com.hazelcast.client.impl.protocol.codec.MultiMapForceUnlockCodec) Member(com.hazelcast.cluster.Member) ListenerAdapters.createListenerAdapter(com.hazelcast.map.impl.ListenerAdapters.createListenerAdapter) MultiMapSizeCodec(com.hazelcast.client.impl.protocol.codec.MultiMapSizeCodec) Thread.currentThread(java.lang.Thread.currentThread) CollectionUtil(com.hazelcast.internal.util.CollectionUtil) EventHandler(com.hazelcast.client.impl.spi.EventHandler) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) MultiMapClearCodec(com.hazelcast.client.impl.protocol.codec.MultiMapClearCodec) ListenerMessageCodec(com.hazelcast.client.impl.spi.impl.ListenerMessageCodec) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MultiMapContainsEntryCodec(com.hazelcast.client.impl.protocol.codec.MultiMapContainsEntryCodec) Map(java.util.Map) ClientInvocation(com.hazelcast.client.impl.spi.impl.ClientInvocation) ClientLockReferenceIdGenerator(com.hazelcast.client.impl.clientside.ClientLockReferenceIdGenerator) MultiMapIsLockedCodec(com.hazelcast.client.impl.protocol.codec.MultiMapIsLockedCodec) MultiMapPutAllCodec(com.hazelcast.client.impl.protocol.codec.MultiMapPutAllCodec) EntryEvent(com.hazelcast.core.EntryEvent) MultiMapEntrySetCodec(com.hazelcast.client.impl.protocol.codec.MultiMapEntrySetCodec) LocalMultiMapStats(com.hazelcast.multimap.LocalMultiMapStats) ClientPartitionService(com.hazelcast.client.impl.spi.ClientPartitionService) MultiMapLockCodec(com.hazelcast.client.impl.protocol.codec.MultiMapLockCodec) Collection(java.util.Collection) Set(java.util.Set) EntryListener(com.hazelcast.core.EntryListener) UUID(java.util.UUID) MultiMapRemoveEntryCodec(com.hazelcast.client.impl.protocol.codec.MultiMapRemoveEntryCodec) MultiMapDeleteCodec(com.hazelcast.client.impl.protocol.codec.MultiMapDeleteCodec) UnmodifiableLazyList(com.hazelcast.spi.impl.UnmodifiableLazyList) CompletionStage(java.util.concurrent.CompletionStage) Preconditions.checkPositive(com.hazelcast.internal.util.Preconditions.checkPositive) MultiMapAddEntryListenerToKeyCodec(com.hazelcast.client.impl.protocol.codec.MultiMapAddEntryListenerToKeyCodec) MultiMapGetCodec(com.hazelcast.client.impl.protocol.codec.MultiMapGetCodec) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) MultiMapRemoveEntryListenerCodec(com.hazelcast.client.impl.protocol.codec.MultiMapRemoveEntryListenerCodec) MultiMapTryLockCodec(com.hazelcast.client.impl.protocol.codec.MultiMapTryLockCodec) DataAwareEntryEvent(com.hazelcast.map.impl.DataAwareEntryEvent) HashMap(java.util.HashMap) IMapEvent(com.hazelcast.map.IMapEvent) MultiMapUnlockCodec(com.hazelcast.client.impl.protocol.codec.MultiMapUnlockCodec) ArrayList(java.util.ArrayList) MultiMapValuesCodec(com.hazelcast.client.impl.protocol.codec.MultiMapValuesCodec) MultiMap(com.hazelcast.multimap.MultiMap) ClientContext(com.hazelcast.client.impl.spi.ClientContext) BiConsumer(java.util.function.BiConsumer) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) EntryEventType(com.hazelcast.core.EntryEventType) UnmodifiableLazySet(com.hazelcast.spi.impl.UnmodifiableLazySet) MultiMapRemoveCodec(com.hazelcast.client.impl.protocol.codec.MultiMapRemoveCodec) Data(com.hazelcast.internal.serialization.Data) MultiMapContainsKeyCodec(com.hazelcast.client.impl.protocol.codec.MultiMapContainsKeyCodec) MultiMapKeySetCodec(com.hazelcast.client.impl.protocol.codec.MultiMapKeySetCodec) Preconditions.checkNotNull(com.hazelcast.internal.util.Preconditions.checkNotNull) ClientProxy(com.hazelcast.client.impl.spi.ClientProxy) TimeUnit(java.util.concurrent.TimeUnit) AbstractMap(java.util.AbstractMap) MultiMapValueCountCodec(com.hazelcast.client.impl.protocol.codec.MultiMapValueCountCodec) MapEvent(com.hazelcast.map.MapEvent) ListenerAdapter(com.hazelcast.map.impl.ListenerAdapter) ThreadUtil(com.hazelcast.internal.util.ThreadUtil) HashMap(java.util.HashMap) Data(com.hazelcast.internal.serialization.Data) ClientInvocation(com.hazelcast.client.impl.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractMap(java.util.AbstractMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collection(java.util.Collection) ClientPartitionService(com.hazelcast.client.impl.spi.ClientPartitionService) Map(java.util.Map) HashMap(java.util.HashMap) MultiMap(com.hazelcast.multimap.MultiMap) AbstractMap(java.util.AbstractMap)

Example 7 with ClientPartitionService

use of com.hazelcast.client.impl.spi.ClientPartitionService in project hazelcast by hazelcast.

the class ClientPartitionServiceLiteMemberTest method testWithLiteMemberAndDataMember.

@Test
public void testWithLiteMemberAndDataMember() {
    HazelcastInstance hazelcastInstance = factory.newHazelcastInstance();
    factory.newHazelcastInstance(new Config().setLiteMember(true));
    TestUtil.warmUpPartitions(hazelcastInstance);
    HazelcastInstance client = factory.newHazelcastClient();
    ClientPartitionService clientPartitionService = getClientPartitionService(client);
    assertTrueEventually(() -> {
        assertNotEquals(0, clientPartitionService.getPartitionCount());
        assertNotNull(clientPartitionService.getPartitionOwner(0));
    });
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) ClientPartitionService(com.hazelcast.client.impl.spi.ClientPartitionService) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 8 with ClientPartitionService

use of com.hazelcast.client.impl.spi.ClientPartitionService in project hazelcast by hazelcast.

the class ClientCacheProxySupport method groupDataToPartitions.

private void groupDataToPartitions(Map<? extends K, ? extends V> userInputMap, List<Map.Entry<Data, Data>>[] entriesPerPartition, NearCachingHook nearCachingHook) {
    ClientPartitionService partitionService = getContext().getPartitionService();
    for (Map.Entry<? extends K, ? extends V> entry : userInputMap.entrySet()) {
        K key = entry.getKey();
        V value = entry.getValue();
        validateNotNull(key, value);
        Data keyData = toData(key);
        Data valueData = toData(value);
        nearCachingHook.beforeRemoteCall(key, keyData, value, valueData);
        int partitionId = partitionService.getPartitionId(keyData);
        List<Map.Entry<Data, Data>> entries = entriesPerPartition[partitionId];
        if (entries == null) {
            entries = new ArrayList<>();
            entriesPerPartition[partitionId] = entries;
        }
        // TODO Can we do this without creating SimpleImmutableEntry?
        entries.add(new AbstractMap.SimpleImmutableEntry<>(keyData, valueData));
    }
}
Also used : AbstractMap(java.util.AbstractMap) Data(com.hazelcast.internal.serialization.Data) ClientPartitionService(com.hazelcast.client.impl.spi.ClientPartitionService) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) AbstractMap(java.util.AbstractMap)

Example 9 with ClientPartitionService

use of com.hazelcast.client.impl.spi.ClientPartitionService in project hazelcast by hazelcast.

the class ClientPartitionServiceLiteMemberTest method testWithOnlyLiteMember.

@Test
public void testWithOnlyLiteMember() {
    factory.newHazelcastInstance(new Config().setLiteMember(true));
    HazelcastInstance client = factory.newHazelcastClient();
    ClientPartitionService clientPartitionService = getClientPartitionService(client);
    assertTrueEventually(() -> assertEquals(271, clientPartitionService.getPartitionCount()));
    assertNull(clientPartitionService.getPartitionOwner(0));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) ClientPartitionService(com.hazelcast.client.impl.spi.ClientPartitionService) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 10 with ClientPartitionService

use of com.hazelcast.client.impl.spi.ClientPartitionService in project hazelcast by hazelcast.

the class ClientMapProxy method iterator.

@Override
@Nonnull
public Iterator<Entry<K, V>> iterator() {
    ClientPartitionService partitionService = getContext().getPartitionService();
    int partitionCount = partitionService.getPartitionCount();
    return new ClientMapIterator<>(this, partitionCount, false);
}
Also used : ClientMapIterator(com.hazelcast.client.map.impl.iterator.ClientMapIterator) ClientPartitionService(com.hazelcast.client.impl.spi.ClientPartitionService) Nonnull(javax.annotation.Nonnull)

Aggregations

ClientPartitionService (com.hazelcast.client.impl.spi.ClientPartitionService)11 Data (com.hazelcast.internal.serialization.Data)5 Nonnull (javax.annotation.Nonnull)4 ClientMapIterator (com.hazelcast.client.map.impl.iterator.ClientMapIterator)3 AbstractMap (java.util.AbstractMap)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 ClientLockReferenceIdGenerator (com.hazelcast.client.impl.clientside.ClientLockReferenceIdGenerator)2 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)2 ClientContext (com.hazelcast.client.impl.spi.ClientContext)2 ClientProxy (com.hazelcast.client.impl.spi.ClientProxy)2 EventHandler (com.hazelcast.client.impl.spi.EventHandler)2 ClientInvocation (com.hazelcast.client.impl.spi.impl.ClientInvocation)2 ListenerMessageCodec (com.hazelcast.client.impl.spi.impl.ListenerMessageCodec)2 Member (com.hazelcast.cluster.Member)2 EntryEvent (com.hazelcast.core.EntryEvent)2 EntryEventType (com.hazelcast.core.EntryEventType)2 CollectionUtil (com.hazelcast.internal.util.CollectionUtil)2 Preconditions.checkNotNull (com.hazelcast.internal.util.Preconditions.checkNotNull)2 Preconditions.checkPositive (com.hazelcast.internal.util.Preconditions.checkPositive)2