use of com.hazelcast.cluster.Address in project hazelcast by hazelcast.
the class MapProxySupport method putAllInternal.
/**
* This method will group all entries per partition and send one operation
* per member. If there are e.g. five keys for a single member, even if
* they are from different partitions, there will only be a single remote
* invocation instead of five.
* <p>
* There is also an optional support for batching to send smaller packages.
* Takes care about {@code null} checks for keys and values.
*
* @param future iff not-null, execute asynchronously by completing this future.
* Batching is not supported in async mode
*/
@SuppressWarnings({ "checkstyle:MethodLength", "checkstyle:CyclomaticComplexity", "checkstyle:NPathComplexity" })
protected void putAllInternal(Map<? extends K, ? extends V> map, @Nullable InternalCompletableFuture<Void> future, boolean triggerMapLoader) {
try {
int mapSize = map.size();
if (mapSize == 0) {
if (future != null) {
future.complete(null);
}
return;
}
boolean useBatching = future == null && isPutAllUseBatching(mapSize);
int partitionCount = partitionService.getPartitionCount();
int initialSize = getPutAllInitialSize(useBatching, mapSize, partitionCount);
Map<Address, List<Integer>> memberPartitionsMap = partitionService.getMemberPartitionsMap();
// init counters for batching
MutableLong[] counterPerMember = null;
Address[] addresses = null;
if (useBatching) {
counterPerMember = new MutableLong[partitionCount];
addresses = new Address[partitionCount];
for (Entry<Address, List<Integer>> addressListEntry : memberPartitionsMap.entrySet()) {
MutableLong counter = new MutableLong();
Address address = addressListEntry.getKey();
for (int partitionId : addressListEntry.getValue()) {
counterPerMember[partitionId] = counter;
addresses[partitionId] = address;
}
}
}
// fill entriesPerPartition
MapEntries[] entriesPerPartition = new MapEntries[partitionCount];
for (Entry entry : map.entrySet()) {
checkNotNull(entry.getKey(), NULL_KEY_IS_NOT_ALLOWED);
checkNotNull(entry.getValue(), NULL_VALUE_IS_NOT_ALLOWED);
Data keyData = toDataWithStrategy(entry.getKey());
int partitionId = partitionService.getPartitionId(keyData);
MapEntries entries = entriesPerPartition[partitionId];
if (entries == null) {
entries = new MapEntries(initialSize);
entriesPerPartition[partitionId] = entries;
}
entries.add(keyData, toData(entry.getValue()));
if (useBatching) {
long currentSize = ++counterPerMember[partitionId].value;
if (currentSize % putAllBatchSize == 0) {
List<Integer> partitions = memberPartitionsMap.get(addresses[partitionId]);
invokePutAllOperation(addresses[partitionId], partitions, entriesPerPartition, triggerMapLoader).get();
}
}
}
// invoke operations for entriesPerPartition
AtomicInteger counter = new AtomicInteger(memberPartitionsMap.size());
InternalCompletableFuture<Void> resultFuture = future != null ? future : new InternalCompletableFuture<>();
BiConsumer<Void, Throwable> callback = (response, t) -> {
if (t != null) {
resultFuture.completeExceptionally(t);
}
if (counter.decrementAndGet() == 0) {
try {
// don't ignore errors here, see https://github.com/hazelcast/hazelcast-jet/issues/3046
finalizePutAll(map);
} catch (Throwable e) {
resultFuture.completeExceptionally(e);
return;
}
if (!resultFuture.isDone()) {
resultFuture.complete(null);
}
}
};
for (Entry<Address, List<Integer>> entry : memberPartitionsMap.entrySet()) {
invokePutAllOperation(entry.getKey(), entry.getValue(), entriesPerPartition, triggerMapLoader).whenCompleteAsync(callback);
}
// if executing in sync mode, block for the responses
if (future == null) {
resultFuture.get();
}
} catch (Throwable e) {
throw rethrow(e);
}
}
use of com.hazelcast.cluster.Address in project hazelcast by hazelcast.
the class QueryEngineImpl method dispatchFullQueryOnAllMembersOnQueryThread.
private List<Future<Result>> dispatchFullQueryOnAllMembersOnQueryThread(Query query) {
Collection<Address> members;
if (query.getPartitionIdSet().size() == partitionService.getPartitionCount()) {
members = clusterService.getMembers(DATA_MEMBER_SELECTOR).stream().map(m -> m.getAddress()).collect(Collectors.toList());
} else {
members = new HashSet<>();
for (PrimitiveIterator.OfInt iterator = query.getPartitionIdSet().intIterator(); iterator.hasNext(); ) {
members.add(partitionService.getPartitionOwnerOrWait(iterator.next()));
}
}
List<Future<Result>> futures = new ArrayList<>(members.size());
for (Address address : members) {
Operation operation = createQueryOperation(query);
Future<Result> future = operationService.invokeOnTarget(MapService.SERVICE_NAME, operation, address);
futures.add(future);
}
return futures;
}
use of com.hazelcast.cluster.Address in project hazelcast by hazelcast.
the class MultiMapProxySupport method putAllInternal.
// NB: this method is generally copied from MapProxySupport#putAllInternal
@SuppressWarnings({ "checkstyle:npathcomplexity", "checkstyle:methodlength" })
protected void putAllInternal(Map<Data, Data> map, @Nullable InternalCompletableFuture<Void> future) {
// get partition to entries mapping
try {
int mapSize = map.size();
if (mapSize == 0) {
if (future != null) {
future.complete(null);
}
return;
}
int partitionCount = partitionService.getPartitionCount();
int initialSize = getPutAllInitialSize(mapSize, partitionCount);
// get node to partition mapping
Map<Address, List<Integer>> memberPartitionsMap = partitionService.getMemberPartitionsMap();
// fill entriesPerPartition
MapEntries[] entriesPerPartition = new MapEntries[partitionCount];
for (Map.Entry<Data, 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);
MapEntries entries = entriesPerPartition[partitionId];
if (entries == null) {
entries = new MapEntries(initialSize);
entriesPerPartition[partitionId] = entries;
}
entries.add(keyData, entry.getValue());
}
// invoke operations for entriesPerPartition
AtomicInteger counter = new AtomicInteger(memberPartitionsMap.size());
InternalCompletableFuture<Void> resultFuture = future != null ? future : new InternalCompletableFuture<>();
BiConsumer<Void, Throwable> callback = (response, t) -> {
if (t != null) {
resultFuture.completeExceptionally(t);
}
if (counter.decrementAndGet() == 0) {
if (!resultFuture.isDone()) {
resultFuture.complete(null);
}
}
};
for (Map.Entry<Address, List<Integer>> entry : memberPartitionsMap.entrySet()) {
invokePutAllOperation(entry.getKey(), entry.getValue(), entriesPerPartition).whenCompleteAsync(callback);
}
// if executing in sync mode, block for the responses
if (future == null) {
resultFuture.get();
}
} catch (Throwable e) {
throw rethrow(e);
}
}
use of com.hazelcast.cluster.Address in project hazelcast by hazelcast.
the class MultiMapService method createStats.
LocalMultiMapStats createStats(String name) {
LocalMultiMapStatsImpl stats = getLocalMultiMapStatsImpl(name);
long ownedEntryCount = 0;
long backupEntryCount = 0;
long hits = 0;
long lockedEntryCount = 0;
long lastAccessTime = 0;
long lastUpdateTime = 0;
ClusterService clusterService = nodeEngine.getClusterService();
MultiMapConfig config = nodeEngine.getConfig().findMultiMapConfig(name);
int backupCount = config.getTotalBackupCount();
Address thisAddress = clusterService.getThisAddress();
for (int partitionId = 0; partitionId < nodeEngine.getPartitionService().getPartitionCount(); partitionId++) {
IPartition partition = nodeEngine.getPartitionService().getPartition(partitionId, false);
MultiMapPartitionContainer partitionContainer = getPartitionContainer(partitionId);
MultiMapContainer multiMapContainer = partitionContainer.getMultiMapContainer(name, false);
if (multiMapContainer == null) {
continue;
}
Address owner = partition.getOwnerOrNull();
if (owner != null) {
if (owner.equals(thisAddress)) {
lockedEntryCount += multiMapContainer.getLockedCount();
lastAccessTime = max(lastAccessTime, multiMapContainer.getLastAccessTime());
lastUpdateTime = max(lastUpdateTime, multiMapContainer.getLastUpdateTime());
for (MultiMapValue multiMapValue : multiMapContainer.getMultiMapValues().values()) {
hits += multiMapValue.getHits();
ownedEntryCount += multiMapValue.getCollection(false).size();
}
} else {
for (int j = 1; j <= backupCount; j++) {
// wait if the partition table is not updated yet
Address replicaAddress = getReplicaAddress(partition, backupCount, j);
if (replicaAddress != null && replicaAddress.equals(thisAddress)) {
for (MultiMapValue multiMapValue : multiMapContainer.getMultiMapValues().values()) {
backupEntryCount += multiMapValue.getCollection(false).size();
}
}
}
}
}
}
stats.setOwnedEntryCount(ownedEntryCount);
stats.setBackupEntryCount(backupEntryCount);
stats.setHits(hits);
stats.setLockedEntryCount(lockedEntryCount);
stats.setBackupCount(backupCount);
stats.setLastAccessTime(lastAccessTime);
stats.setLastUpdateTime(lastUpdateTime);
return stats;
}
use of com.hazelcast.cluster.Address in project hazelcast by hazelcast.
the class PutOperation method run.
@Override
public void run() throws Exception {
service = getService();
ReplicatedRecordStore store = service.getReplicatedRecordStore(name, true, getPartitionId());
Address thisAddress = getNodeEngine().getThisAddress();
boolean isLocal = getCallerAddress().equals(thisAddress);
Object putResult = store.put(key, value, ttl, TimeUnit.MILLISECONDS, isLocal);
oldValue = getNodeEngine().toData(putResult);
response = new VersionResponsePair(putResult, store.getVersion());
if (!isLocal) {
sendUpdateCallerOperation(false);
}
}
Aggregations