private void clearRemoteTransactions(Xid xid) {
   NodeEngine nodeEngine = getNodeEngine();
   InternalPartitionService partitionService = nodeEngine.getPartitionService();
   OperationService operationService = nodeEngine.getOperationService();
   SerializableXID serializableXID =
       new SerializableXID(
           xid.getFormatId(), xid.getGlobalTransactionId(), xid.getBranchQualifier());
   Data xidData = nodeEngine.toData(serializableXID);
   int partitionId = partitionService.getPartitionId(xidData);
   ClearRemoteTransactionOperation operation = new ClearRemoteTransactionOperation(xidData);
   operationService.invokeOnPartition(SERVICE_NAME, operation, partitionId);
 }
 private List<Object>[] buildMapping(InternalPartitionService partitionService) {
   List<Object>[] mapping = new List[partitionService.getPartitionCount()];
   for (Object key : keys) {
     int pid = partitionService.getPartitionId(key);
     List<Object> list = mapping[pid];
     if (list == null) {
       list = new ArrayList<Object>();
       mapping[pid] = list;
     }
     list.add(key);
   }
   return mapping;
 }
  @Override
  public Collection<Integer> getPartitions() {
    InternalPartitionService partitionService = getClientEngine().getPartitionService();
    int partitions = partitionService.getPartitionCount();
    int capacity = Math.min(partitions, keys.size()); // todo: is there better way to estimate size?
    Set<Integer> partitionIds = new HashSet<Integer>(capacity);

    Iterator<Data> iterator = keys.iterator();
    while (iterator.hasNext() && partitionIds.size() < partitions) {
      Data key = iterator.next();
      partitionIds.add(partitionService.getPartitionId(key));
    }
    return partitionIds;
  }
  @Override
  public void process(
      EntryTaskScheduler<String, Void> scheduler,
      Collection<ScheduledEntry<String, Void>> entries) {
    if (entries.isEmpty()) {
      return;
    }

    InternalPartitionService partitionService = nodeEngine.getPartitionService();
    OperationService operationService = nodeEngine.getOperationService();

    for (ScheduledEntry<String, Void> entry : entries) {
      String name = entry.getKey();
      int partitionId = partitionService.getPartitionId(nodeEngine.toData(name));
      CheckAndEvictOperation op = new CheckAndEvictOperation(entry.getKey());
      operationService.invokeOnPartition(QueueService.SERVICE_NAME, op, partitionId).getSafely();
    }
  }
 public void run() {
   backupRecordInfos = new ArrayList<RecordInfo>();
   backupEntrySet = new ArrayList<Map.Entry<Data, Data>>();
   int partitionId = getPartitionId();
   final MapServiceContext mapServiceContext = mapService.getMapServiceContext();
   RecordStore recordStore = mapServiceContext.getRecordStore(partitionId, name);
   Set<Map.Entry<Data, Data>> entries = entrySet.getEntrySet();
   InternalPartitionService partitionService = getNodeEngine().getPartitionService();
   Set<Data> keysToInvalidate = new HashSet<Data>();
   for (Map.Entry<Data, Data> entry : entries) {
     Data dataKey = entry.getKey();
     Data dataValue = entry.getValue();
     if (partitionId == partitionService.getPartitionId(dataKey)) {
       Data dataOldValue = null;
       if (initialLoad) {
         recordStore.putFromLoad(dataKey, dataValue, -1);
       } else {
         dataOldValue = mapServiceContext.toData(recordStore.put(dataKey, dataValue, -1));
       }
       mapServiceContext.interceptAfterPut(name, dataValue);
       EntryEventType eventType =
           dataOldValue == null ? EntryEventType.ADDED : EntryEventType.UPDATED;
       final MapEventPublisher mapEventPublisher = mapServiceContext.getMapEventPublisher();
       mapEventPublisher.publishEvent(
           getCallerAddress(), name, eventType, dataKey, dataOldValue, dataValue);
       keysToInvalidate.add(dataKey);
       if (mapContainer.getWanReplicationPublisher() != null
           && mapContainer.getWanMergePolicy() != null) {
         Record record = recordStore.getRecord(dataKey);
         final Data dataValueAsData = mapServiceContext.toData(dataValue);
         final EntryView entryView =
             EntryViews.createSimpleEntryView(dataKey, dataValueAsData, record);
         mapEventPublisher.publishWanReplicationUpdate(name, entryView);
       }
       backupEntrySet.add(entry);
       RecordInfo replicationInfo = Records.buildRecordInfo(recordStore.getRecord(dataKey));
       backupRecordInfos.add(replicationInfo);
     }
   }
   invalidateNearCaches(keysToInvalidate);
 }
  @Override
  public void run() throws Exception {
    final int partitionId = getPartitionId();
    final InternalPartitionService partitionService = getNodeEngine().getPartitionService();

    Set<Data> filteredKeys = new HashSet<Data>();
    if (keys != null) {
      for (Data k : keys) {
        if (partitionService.getPartitionId(k) == partitionId) {
          filteredKeys.add(k);
        }
      }
    }

    if (filteredKeys.isEmpty()) {
      return;
    }

    try {
      final ICacheService service = getService();
      cache = service.getOrCreateRecordStore(name, partitionId);
      final Set<Data> keysLoaded = cache.loadAll(filteredKeys, replaceExistingValues);
      shouldBackup = !keysLoaded.isEmpty();
      if (shouldBackup) {
        backupRecords = new HashMap<Data, CacheRecord>(keysLoaded.size());
        for (Data key : keysLoaded) {
          CacheRecord record = cache.getRecord(key);
          // Loaded keys may have been evicted, then record will be null.
          // So if the loaded key is evicted, don't send it to backup.
          if (record != null) {
            backupRecords.put(key, record);
          }
        }
      }
    } catch (CacheException e) {
      response = new CacheClearResponse(e);
    }
  }
 private void finalizeTransactionRemotely(Xid xid, boolean isCommit) throws XAException {
   NodeEngine nodeEngine = getNodeEngine();
   InternalPartitionService partitionService = nodeEngine.getPartitionService();
   OperationService operationService = nodeEngine.getOperationService();
   SerializableXID serializableXID =
       new SerializableXID(
           xid.getFormatId(), xid.getGlobalTransactionId(), xid.getBranchQualifier());
   Data xidData = nodeEngine.toData(serializableXID);
   int partitionId = partitionService.getPartitionId(xidData);
   FinalizeRemoteTransactionOperation operation =
       new FinalizeRemoteTransactionOperation(xidData, isCommit);
   InternalCompletableFuture<Integer> future =
       operationService.invokeOnPartition(SERVICE_NAME, operation, partitionId);
   Integer errorCode;
   try {
     errorCode = future.get();
   } catch (Exception e) {
     throw ExceptionUtil.rethrow(e);
   }
   if (errorCode != null) {
     throw new XAException(errorCode);
   }
 }
 protected boolean keyNotOwnedByThisPartition(Data key) {
   final InternalPartitionService partitionService = getNodeEngine().getPartitionService();
   return partitionService.getPartitionId(key) != getPartitionId();
 }
 @Override
 protected int getPartition() {
   InternalPartitionService partitionService = clientEngine.getPartitionService();
   return partitionService.getPartitionId(key);
 }