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(); }
protected Set<Data> keySetInternal() { final NodeEngine nodeEngine = getNodeEngine(); try { Map<Integer, Object> results = nodeEngine .getOperationService() .invokeOnAllPartitions( CollectionService.SERVICE_NAME, new MultiMapOperationFactory(proxyId, OperationFactoryType.KEY_SET)); Set<Data> keySet = new HashSet<Data>(); for (Object result : results.values()) { if (result == null) { continue; } CollectionResponse response = nodeEngine.toObject(result); if (response.getCollection() != null) { keySet.addAll(response.getCollection()); } } return keySet; } catch (Throwable throwable) { throw ExceptionUtil.rethrow(throwable); } }
public void connectionRemoved(Connection connection) { if (connection.isClient() && connection instanceof TcpIpConnection && nodeEngine.isActive()) { final ClientEndpoint endpoint = endpoints.get(connection); if (endpoint != null && node.getLocalMember().getUuid().equals(endpoint.getPrincipal().getOwnerUuid())) { removeEndpoint(connection, true); if (!endpoint.isFirstConnection()) { return; } NodeEngine nodeEngine = node.nodeEngine; final Collection<MemberImpl> memberList = nodeEngine.getClusterService().getMemberList(); for (MemberImpl member : memberList) { final ClientDisconnectionOperation op = new ClientDisconnectionOperation(endpoint.getUuid()); op.setNodeEngine(nodeEngine) .setServiceName(SERVICE_NAME) .setService(this) .setResponseHandler(ResponseHandlerFactory.createEmptyResponseHandler()); if (member.localMember()) { nodeEngine.getOperationService().runOperation(op); } else { nodeEngine.getOperationService().send(op, member.getAddress()); } } } } }
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); }
public void invalidateAllNearCaches(String mapName, Set<Data> keys) { if (!isNearCacheEnabled(mapName)) { return; } if (keys == null || keys.isEmpty()) { return; } // send operation. Operation operation = new NearCacheKeySetInvalidationOperation(mapName, keys).setServiceName(SERVICE_NAME); Collection<MemberImpl> members = nodeEngine.getClusterService().getMemberList(); for (MemberImpl member : members) { try { if (member.localMember()) continue; nodeEngine.getOperationService().send(operation, member.getAddress()); } catch (Throwable throwable) { logger.warning(throwable); } } // below local invalidation is for the case the data is cached before partition is // owned/migrated for (final Data key : keys) { invalidateNearCache(mapName, key); } }
@Override public void initialize() { final NodeEngine nodeEngine = getNodeEngine(); CollectionConfig config = getConfig(nodeEngine); final List<ItemListenerConfig> itemListenerConfigs = config.getItemListenerConfigs(); for (ItemListenerConfig itemListenerConfig : itemListenerConfigs) { ItemListener listener = itemListenerConfig.getImplementation(); if (listener == null && itemListenerConfig.getClassName() != null) { try { listener = ClassLoaderUtil.newInstance( nodeEngine.getConfigClassLoader(), itemListenerConfig.getClassName()); } catch (Exception e) { throw ExceptionUtil.rethrow(e); } } if (listener != null) { if (listener instanceof HazelcastInstanceAware) { ((HazelcastInstanceAware) listener) .setHazelcastInstance(nodeEngine.getHazelcastInstance()); } addItemListener(listener, itemListenerConfig.isIncludeValue()); } } }
private Collection<E> getAll() { final CollectionGetAllOperation operation = new CollectionGetAllOperation(name); final SerializableCollection result = invoke(operation); final Collection<Data> collection = result.getCollection(); final List<E> list = new ArrayList<E>(collection.size()); final NodeEngine nodeEngine = getNodeEngine(); for (Data data : collection) { list.add(nodeEngine.<E>toObject(data)); } return list; }
public boolean containsAll(Collection<?> c) { throwExceptionIfNull(c); Set<Data> valueSet = new HashSet<Data>(c.size()); final NodeEngine nodeEngine = getNodeEngine(); for (Object o : c) { throwExceptionIfNull(o); valueSet.add(nodeEngine.toData(o)); } final CollectionContainsOperation operation = new CollectionContainsOperation(name, valueSet); final Boolean result = invoke(operation); return result; }
public boolean addAll(Collection<? extends E> c) { throwExceptionIfNull(c); List<Data> valueList = new ArrayList<Data>(c.size()); final NodeEngine nodeEngine = getNodeEngine(); for (E e : c) { throwExceptionIfNull(e); valueList.add(nodeEngine.toData(e)); } final CollectionAddAllOperation operation = new CollectionAddAllOperation(name, valueList); final Boolean result = invoke(operation); return result; }
public void clear() { final NodeEngine nodeEngine = getNodeEngine(); try { nodeEngine .getOperationService() .invokeOnAllPartitions( CollectionService.SERVICE_NAME, new MultiMapOperationFactory(proxyId, OperationFactoryType.CLEAR)); } catch (Throwable throwable) { throw ExceptionUtil.rethrow(throwable); } }
private boolean compareAndRemove(boolean retain, Collection<?> c) { throwExceptionIfNull(c); Set<Data> valueSet = new HashSet<Data>(c.size()); final NodeEngine nodeEngine = getNodeEngine(); for (Object o : c) { throwExceptionIfNull(o); valueSet.add(nodeEngine.toData(o)); } final CollectionCompareAndRemoveOperation operation = new CollectionCompareAndRemoveOperation(name, retain, valueSet); final Boolean result = invoke(operation); return result; }
protected Map entrySetInternal() { final NodeEngine nodeEngine = getNodeEngine(); try { Map<Integer, Object> results = nodeEngine .getOperationService() .invokeOnAllPartitions( CollectionService.SERVICE_NAME, new MultiMapOperationFactory(proxyId, OperationFactoryType.ENTRY_SET)); return results; } catch (Throwable throwable) { throw ExceptionUtil.rethrow(throwable); } }
protected <T> T invoke(CollectionOperation operation) { final NodeEngine nodeEngine = getNodeEngine(); try { Invocation inv = nodeEngine .getOperationService() .createInvocationBuilder(getServiceName(), operation, partitionId) .build(); Future f = inv.invoke(); return nodeEngine.toObject(f.get()); } catch (Throwable throwable) { throw ExceptionUtil.rethrow(throwable); } }
@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); } } }
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); } }
@SuppressWarnings("unchecked") public void dispatchEvent(EventData eventData, EntryListener listener) { Member member = nodeEngine.getClusterService().getMember(eventData.getCaller()); EntryEvent event = new DataAwareEntryEvent( member, eventData.getEventType(), eventData.getMapName(), eventData.getDataKey(), eventData.getDataNewValue(), eventData.getDataOldValue(), getSerializationService()); switch (event.getEventType()) { case ADDED: listener.entryAdded(event); break; case EVICTED: listener.entryEvicted(event); break; case UPDATED: listener.entryUpdated(event); break; case REMOVED: listener.entryRemoved(event); break; default: throw new IllegalArgumentException("Invalid event type: " + event.getEventType()); } MapContainer mapContainer = getMapContainer(eventData.getMapName()); if (mapContainer.getMapConfig().isStatisticsEnabled()) { getLocalMapStatsImpl(eventData.getMapName()).incrementReceivedEvents(); } }
public Data toData(Object object) { if (object == null) return null; if (object instanceof Data) { return (Data) object; } else { return nodeEngine.getSerializationService().toData(object); } }
public String addEventListener( EntryListener entryListener, EventFilter eventFilter, String mapName) { EventRegistration registration = nodeEngine .getEventService() .registerListener(SERVICE_NAME, mapName, eventFilter, entryListener); return registration.getId(); }
public Object toObject(Object data) { if (data == null) return null; if (data instanceof Data) { return nodeEngine.toObject(data); } else { return data; } }
public Data toData(Object object, PartitioningStrategy partitionStrategy) { if (object == null) return null; if (object instanceof Data) { return (Data) object; } else { return nodeEngine.getSerializationService().toData(object, partitionStrategy); } }
@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); } } }
public void invalidateAllNearCaches(String mapName, Data key) { if (!isNearCacheEnabled(mapName)) { return; } Collection<MemberImpl> members = nodeEngine.getClusterService().getMemberList(); for (MemberImpl member : members) { try { if (member.localMember()) continue; Operation operation = new InvalidateNearCacheOperation(mapName, key).setServiceName(SERVICE_NAME); nodeEngine.getOperationService().send(operation, member.getAddress()); } catch (Throwable throwable) { throw new HazelcastException(throwable); } } // below local invalidation is for the case the data is cached before partition is // owned/migrated invalidateNearCache(mapName, key); }
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()); }
@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); } }
public int size() { final NodeEngine nodeEngine = getNodeEngine(); try { Map<Integer, Object> results = nodeEngine .getOperationService() .invokeOnAllPartitions( CollectionService.SERVICE_NAME, new MultiMapOperationFactory(proxyId, OperationFactoryType.SIZE)); int size = 0; for (Object obj : results.values()) { if (obj == null) { continue; } Integer result = nodeEngine.toObject(obj); size += result; } return size; } catch (Throwable throwable) { throw ExceptionUtil.rethrow(throwable); } }
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 boolean containsInternal(Data key, Data value) { final NodeEngine nodeEngine = getNodeEngine(); try { Map<Integer, Object> results = nodeEngine .getOperationService() .invokeOnAllPartitions( CollectionService.SERVICE_NAME, new MultiMapOperationFactory(proxyId, OperationFactoryType.CONTAINS, key, value)); for (Object obj : results.values()) { if (obj == null) { continue; } Boolean result = nodeEngine.toObject(obj); if (result) { return true; } } return false; } catch (Throwable throwable) { throw ExceptionUtil.rethrow(throwable); } }
public QueryResult queryOnPartition(String mapName, Predicate predicate, int partitionId) { final QueryResult result = new QueryResult(); List<QueryEntry> list = new LinkedList<QueryEntry>(); PartitionContainer container = getPartitionContainer(partitionId); RecordStore recordStore = container.getRecordStore(mapName); Map<Data, Record> records = recordStore.getReadonlyRecordMap(); SerializationService serializationService = nodeEngine.getSerializationService(); final PagingPredicate pagingPredicate = predicate instanceof PagingPredicate ? (PagingPredicate) predicate : null; Comparator<Map.Entry> wrapperComparator = SortingUtil.newComparator(pagingPredicate); for (Record record : records.values()) { Data key = record.getKey(); Object value = record.getValue(); if (value == null) { continue; } QueryEntry queryEntry = new QueryEntry(serializationService, key, key, value); if (predicate.apply(queryEntry)) { if (pagingPredicate != null) { Map.Entry anchor = pagingPredicate.getAnchor(); if (anchor != null && SortingUtil.compare( pagingPredicate.getComparator(), pagingPredicate.getIterationType(), anchor, queryEntry) >= 0) { continue; } } list.add(queryEntry); } } if (pagingPredicate != null) { Collections.sort(list, wrapperComparator); if (list.size() > pagingPredicate.getPageSize()) { list = list.subList(0, pagingPredicate.getPageSize()); } } for (QueryEntry entry : list) { result.add( new QueryResultEntryImpl(entry.getKeyData(), entry.getKeyData(), entry.getValueData())); } return result; }
public MapMergePolicy getMergePolicy(String mergePolicyName) { MapMergePolicy mergePolicy = mergePolicyMap.get(mergePolicyName); if (mergePolicy == null && mergePolicyName != null) { try { // check if user has entered custom class name instead of policy name mergePolicy = ClassLoaderUtil.newInstance(nodeEngine.getConfigClassLoader(), mergePolicyName); mergePolicyMap.put(mergePolicyName, mergePolicy); } catch (Exception e) { logger.severe(e); throw ExceptionUtil.rethrow(e); } } if (mergePolicy == null) { return mergePolicyMap.get(MapConfig.DEFAULT_MAP_MERGE_POLICY); } return mergePolicy; }
public void destroyDistributedObject(String name) { MapContainer mapContainer = mapContainers.remove(name); if (mapContainer != null) { if (mapContainer.isNearCacheEnabled()) { NearCache nearCache = nearCacheMap.remove(name); if (nearCache != null) { nearCache.clear(); } } mapContainer.shutDownMapStoreScheduledExecutor(); } final PartitionContainer[] containers = partitionContainers; for (PartitionContainer container : containers) { if (container != null) { container.destroyMap(name); } } nodeEngine.getEventService().deregisterAllListeners(SERVICE_NAME, name); }