Esempio n. 1
0
  public Runnable prepareMergeRunnable() {
    Map<MapContainer, Collection<Record>> recordMap =
        new HashMap<MapContainer, Collection<Record>>(mapContainers.size());
    InternalPartitionService partitionService = nodeEngine.getPartitionService();
    int partitionCount = partitionService.getPartitionCount();
    Address thisAddress = nodeEngine.getClusterService().getThisAddress();

    for (MapContainer mapContainer : mapContainers.values()) {
      for (int i = 0; i < partitionCount; i++) {
        RecordStore recordStore = getPartitionContainer(i).getRecordStore(mapContainer.getName());
        // add your owned entries to the map so they will be merged
        if (thisAddress.equals(partitionService.getPartitionOwner(i))) {
          Collection<Record> records = recordMap.get(mapContainer);
          if (records == null) {
            records = new ArrayList<Record>();
            recordMap.put(mapContainer, records);
          }
          records.addAll(recordStore.getReadonlyRecordMap().values());
        }
        // clear all records either owned or backup
        recordStore.reset();
      }
    }
    return new Merger(recordMap);
  }
Esempio n. 2
0
  public void init(final NodeEngine nodeEngine, Properties properties) {
    int partitionCount = nodeEngine.getPartitionService().getPartitionCount();
    for (int i = 0; i < partitionCount; i++) {
      partitionContainers[i] = new PartitionContainer(this, i);
    }
    final LockService lockService = nodeEngine.getSharedService(LockService.SERVICE_NAME);
    if (lockService != null) {
      lockService.registerLockStoreConstructor(
          SERVICE_NAME,
          new ConstructorFunction<ObjectNamespace, LockStoreInfo>() {
            public LockStoreInfo createNew(final ObjectNamespace key) {
              final MapContainer mapContainer = getMapContainer(key.getObjectName());
              return new LockStoreInfo() {
                public int getBackupCount() {
                  return mapContainer.getBackupCount();
                }

                public int getAsyncBackupCount() {
                  return mapContainer.getAsyncBackupCount();
                }
              };
            }
          });
    }
    mapEvictionManager.init();
  }
Esempio n. 3
0
 @Override
 public void onReplicationEvent(WanReplicationEvent replicationEvent) {
   Object eventObject = replicationEvent.getEventObject();
   if (eventObject instanceof MapReplicationUpdate) {
     MapReplicationUpdate replicationUpdate = (MapReplicationUpdate) eventObject;
     EntryView entryView = replicationUpdate.getEntryView();
     MapMergePolicy mergePolicy = replicationUpdate.getMergePolicy();
     String mapName = replicationUpdate.getMapName();
     MapContainer mapContainer = getMapContainer(mapName);
     MergeOperation operation =
         new MergeOperation(
             mapName,
             toData(entryView.getKey(), mapContainer.getPartitioningStrategy()),
             entryView,
             mergePolicy);
     try {
       int partitionId = nodeEngine.getPartitionService().getPartitionId(entryView.getKey());
       Future f =
           nodeEngine
               .getOperationService()
               .invokeOnPartition(SERVICE_NAME, operation, partitionId);
       f.get();
     } catch (Throwable t) {
       throw ExceptionUtil.rethrow(t);
     }
   } else if (eventObject instanceof MapReplicationRemove) {
     MapReplicationRemove replicationRemove = (MapReplicationRemove) eventObject;
     WanOriginatedDeleteOperation operation =
         new WanOriginatedDeleteOperation(
             replicationRemove.getMapName(), replicationRemove.getKey());
     try {
       int partitionId =
           nodeEngine.getPartitionService().getPartitionId(replicationRemove.getKey());
       Future f =
           nodeEngine
               .getOperationService()
               .invokeOnPartition(SERVICE_NAME, operation, partitionId);
       f.get();
     } catch (Throwable t) {
       throw ExceptionUtil.rethrow(t);
     }
   }
 }
 private Object invokeData(CollectionOperation operation, Data dataKey) {
   final NodeEngine nodeEngine = getNodeEngine();
   try {
     int partitionId = nodeEngine.getPartitionService().getPartitionId(dataKey);
     Invocation inv =
         nodeEngine
             .getOperationService()
             .createInvocationBuilder(CollectionService.SERVICE_NAME, operation, partitionId)
             .build();
     Future f = inv.invoke();
     return nodeEngine.toObject(f.get());
   } catch (Throwable throwable) {
     throw ExceptionUtil.rethrow(throwable);
   }
 }
Esempio n. 5
0
 @Override
 public void beforeRun() throws Exception {
   final NodeEngine nodeEngine = getNodeEngine();
   final int partitionId = getPartitionId();
   final InternalPartition partition = nodeEngine.getPartitionService().getPartition(partitionId);
   final Address owner = partition.getReplicaAddress(getReplicaIndex());
   if (!nodeEngine.getThisAddress().equals(owner)) {
     valid = false;
     final ILogger logger = getLogger();
     if (logger.isFinestEnabled()) {
       logger.finest(
           "Wrong target! " + toString() + " cannot be processed! Target should be: " + owner);
     }
   }
 }
Esempio n. 6
0
 public MapService(NodeEngine nodeEngine) {
   this.nodeEngine = nodeEngine;
   this.mapEvictionManager = new MapEvictionManager(this);
   this.replicaWaitSecondsForScheduledTasks =
       getNodeEngine()
               .getGroupProperties()
               .MAP_REPLICA_WAIT_SECONDS_FOR_SCHEDULED_TASKS
               .getInteger()
           * 1000;
   logger = nodeEngine.getLogger(MapService.class.getName());
   partitionContainers =
       new PartitionContainer[nodeEngine.getPartitionService().getPartitionCount()];
   ownedPartitions = new AtomicReference<List<Integer>>();
   mergePolicyMap = new ConcurrentHashMap<String, MapMergePolicy>();
   mergePolicyMap.put(PutIfAbsentMapMergePolicy.class.getName(), new PutIfAbsentMapMergePolicy());
   mergePolicyMap.put(HigherHitsMapMergePolicy.class.getName(), new HigherHitsMapMergePolicy());
   mergePolicyMap.put(PassThroughMergePolicy.class.getName(), new PassThroughMergePolicy());
   mergePolicyMap.put(
       LatestUpdateMapMergePolicy.class.getName(), new LatestUpdateMapMergePolicy());
 }
Esempio n. 7
0
  @Override
  public void run() throws Exception {
    if (!valid) {
      return;
    }
    final NodeEngine nodeEngine = getNodeEngine();
    final PartitionServiceImpl partitionService =
        (PartitionServiceImpl) nodeEngine.getPartitionService();
    partitionService.updatePartitionReplicaVersions(
        getPartitionId(), replicaVersions, getReplicaIndex());

    if (backupOp != null) {
      backupOp.setNodeEngine(nodeEngine);
      backupOp.setResponseHandler(ResponseHandlerFactory.createEmptyResponseHandler());
      backupOp.setCallerUuid(getCallerUuid());
      OperationAccessor.setCallerAddress(backupOp, getCallerAddress());
      OperationAccessor.setInvocationTime(backupOp, Clock.currentTimeMillis());

      final OperationService operationService = nodeEngine.getOperationService();
      operationService.runOperation(backupOp);
    }
  }
 private <T> T invoke(Operation operation, Data dataKey) {
   final NodeEngine nodeEngine = getNodeEngine();
   try {
     int partitionId = nodeEngine.getPartitionService().getPartitionId(dataKey);
     Invocation invocation =
         nodeEngine
             .getOperationService()
             .createInvocationBuilder(CollectionService.SERVICE_NAME, operation, partitionId)
             .build();
     Future f;
     Object o;
     if (config.isStatisticsEnabled()) {
       long time = System.currentTimeMillis();
       f = invocation.invoke();
       o = f.get();
       if (operation instanceof PutOperation) {
         getService()
             .getLocalMultiMapStatsImpl(proxyId)
             .incrementPuts(System.currentTimeMillis() - time);
       } else if (operation instanceof RemoveOperation
           || operation instanceof RemoveAllOperation) {
         getService()
             .getLocalMultiMapStatsImpl(proxyId)
             .incrementRemoves(System.currentTimeMillis() - time);
       } else if (operation instanceof GetAllOperation) {
         getService()
             .getLocalMultiMapStatsImpl(proxyId)
             .incrementGets(System.currentTimeMillis() - time);
       }
     } else {
       f = invocation.invoke();
       o = f.get();
     }
     return (T) nodeEngine.toObject(o);
   } catch (Throwable throwable) {
     throw ExceptionUtil.rethrow(throwable);
   }
 }
 protected AbstractCollectionProxyImpl(String name, NodeEngine nodeEngine, S service) {
   super(nodeEngine, service);
   this.name = name;
   this.partitionId =
       nodeEngine.getPartitionService().getPartitionId(getNameAsPartitionAwareData());
 }
Esempio n. 10
0
 private List<Integer> getMemberPartitions() {
   InternalPartitionService partitionService = nodeEngine.getPartitionService();
   List<Integer> partitions = partitionService.getMemberPartitions(nodeEngine.getThisAddress());
   return Collections.unmodifiableList(partitions);
 }
Esempio n. 11
0
  public LocalMapStatsImpl createLocalMapStats(String mapName) {
    MapContainer mapContainer = getMapContainer(mapName);
    LocalMapStatsImpl localMapStats = getLocalMapStatsImpl(mapName);
    if (!mapContainer.getMapConfig().isStatisticsEnabled()) {
      return localMapStats;
    }

    long ownedEntryCount = 0;
    long backupEntryCount = 0;
    long dirtyCount = 0;
    long ownedEntryMemoryCost = 0;
    long backupEntryMemoryCost = 0;
    long hits = 0;
    long lockedEntryCount = 0;
    long heapCost = 0;

    int backupCount = mapContainer.getTotalBackupCount();
    ClusterService clusterService = nodeEngine.getClusterService();
    final InternalPartitionService partitionService = nodeEngine.getPartitionService();

    Address thisAddress = clusterService.getThisAddress();
    for (int partitionId = 0; partitionId < partitionService.getPartitionCount(); partitionId++) {
      InternalPartition partition = partitionService.getPartition(partitionId);
      Address owner = partition.getOwner();
      if (owner == null) {
        // no-op because no owner is set yet. Therefor we don't know anything about the map
        continue;
      }
      if (owner.equals(thisAddress)) {
        PartitionContainer partitionContainer = getPartitionContainer(partitionId);
        RecordStore recordStore = partitionContainer.getExistingRecordStore(mapName);

        // we don't want to force loading the record store because we are loading statistics. So
        // that is why
        // we ask for 'getExistingRecordStore' instead of 'getRecordStore' which does the load.
        if (recordStore != null) {
          heapCost += recordStore.getHeapCost();
          Map<Data, Record> records = recordStore.getReadonlyRecordMap();
          for (Record record : records.values()) {
            RecordStatistics stats = record.getStatistics();
            // there is map store and the record is dirty (waits to be stored)
            ownedEntryCount++;
            ownedEntryMemoryCost += record.getCost();
            localMapStats.setLastAccessTime(stats.getLastAccessTime());
            localMapStats.setLastUpdateTime(stats.getLastUpdateTime());
            hits += stats.getHits();
            if (recordStore.isLocked(record.getKey())) {
              lockedEntryCount++;
            }
          }
        }
      } else {
        for (int replica = 1; replica <= backupCount; replica++) {
          Address replicaAddress = partition.getReplicaAddress(replica);
          int tryCount = 30;
          // wait if the partition table is not updated yet
          while (replicaAddress == null
              && clusterService.getSize() > backupCount
              && tryCount-- > 0) {
            try {
              Thread.sleep(100);
            } catch (InterruptedException e) {
              throw ExceptionUtil.rethrow(e);
            }
            replicaAddress = partition.getReplicaAddress(replica);
          }

          if (replicaAddress != null && replicaAddress.equals(thisAddress)) {
            PartitionContainer partitionContainer = getPartitionContainer(partitionId);
            RecordStore recordStore = partitionContainer.getRecordStore(mapName);
            heapCost += recordStore.getHeapCost();

            Map<Data, Record> records = recordStore.getReadonlyRecordMap();
            for (Record record : records.values()) {
              backupEntryCount++;
              backupEntryMemoryCost += record.getCost();
            }
          } else if (replicaAddress == null && clusterService.getSize() > backupCount) {
            logger.warning("Partition: " + partition + ", replica: " + replica + " has no owner!");
          }
        }
      }
    }

    if (mapContainer.getMapStoreScheduler() != null) {
      dirtyCount = mapContainer.getMapStoreScheduler().size();
    }
    localMapStats.setBackupCount(backupCount);
    localMapStats.setDirtyEntryCount(zeroOrPositive(dirtyCount));
    localMapStats.setLockedEntryCount(zeroOrPositive(lockedEntryCount));
    localMapStats.setHits(zeroOrPositive(hits));
    localMapStats.setOwnedEntryCount(zeroOrPositive(ownedEntryCount));
    localMapStats.setBackupEntryCount(zeroOrPositive(backupEntryCount));
    localMapStats.setOwnedEntryMemoryCost(zeroOrPositive(ownedEntryMemoryCost));
    localMapStats.setBackupEntryMemoryCost(zeroOrPositive(backupEntryMemoryCost));
    // add near cache heap cost.
    heapCost += mapContainer.getNearCacheSizeEstimator().getSize();
    localMapStats.setHeapCost(heapCost);
    if (mapContainer.getMapConfig().isNearCacheEnabled()) {
      NearCacheStatsImpl nearCacheStats = getNearCache(mapName).getNearCacheStats();
      localMapStats.setNearCacheStats(nearCacheStats);
    }

    return localMapStats;
  }