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); } }
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 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(); }
public void init(final NodeEngine nodeEngine, Properties properties) { int partitionCount = nodeEngine.getPartitionService().getPartitionCount(); for (int partition = 0; partition < partitionCount; partition++) { partitionContainers[partition] = new MultiMapPartitionContainer(this, partition); } 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) { String name = key.getObjectName(); final MultiMapConfig multiMapConfig = nodeEngine.getConfig().findMultiMapConfig(name); return new LockStoreInfo() { public int getBackupCount() { return multiMapConfig.getSyncBackupCount(); } public int getAsyncBackupCount() { return multiMapConfig.getAsyncBackupCount(); } }; } }); } }
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()); } } } } }
@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()); } } }
QueueProxySupport(final String name, final QueueService queueService, NodeEngine nodeEngine) { super(nodeEngine, queueService); this.name = name; this.partitionId = nodeEngine.getPartitionService().getPartitionId(getNameAsPartitionAwareData()); this.config = nodeEngine.getConfig().getQueueConfig(name); }
Collection<EventRegistration> getRegistrations(String mapName) { final MapServiceContext mapServiceContext = this.mapServiceContext; final NodeEngine nodeEngine = mapServiceContext.getNodeEngine(); final EventService eventService = nodeEngine.getEventService(); return eventService.getRegistrations(SERVICE_NAME, mapName); }
CacheEventHandler(NodeEngine nodeEngine) { this.nodeEngine = nodeEngine; GroupProperties groupProperties = nodeEngine.getGroupProperties(); invalidationMessageBatchEnabled = groupProperties.getBoolean(CACHE_INVALIDATION_MESSAGE_BATCH_ENABLED); if (invalidationMessageBatchEnabled) { invalidationMessageBatchSize = groupProperties.getInteger(CACHE_INVALIDATION_MESSAGE_BATCH_SIZE); int invalidationMessageBatchFreq = groupProperties.getInteger(CACHE_INVALIDATION_MESSAGE_BATCH_FREQUENCY_SECONDS); ExecutionService executionService = nodeEngine.getExecutionService(); CacheBatchInvalidationMessageSender batchInvalidationMessageSender = new CacheBatchInvalidationMessageSender(); cacheBatchInvalidationMessageSenderScheduler = executionService.scheduleWithRepetition( ICacheService.SERVICE_NAME + ":cacheBatchInvalidationMessageSender", batchInvalidationMessageSender, invalidationMessageBatchFreq, invalidationMessageBatchFreq, TimeUnit.SECONDS); } LifecycleService lifecycleService = nodeEngine.getHazelcastInstance().getLifecycleService(); lifecycleService.addLifecycleListener( new LifecycleListener() { @Override public void stateChanged(LifecycleEvent event) { if (event.getState() == LifecycleEvent.LifecycleState.SHUTTING_DOWN) { invalidateAllCaches(); } } }); }
protected MapProxySupport( String name, MapService service, NodeEngine nodeEngine, MapConfig mapConfig) { super(nodeEngine, service); this.name = name; HazelcastProperties properties = nodeEngine.getProperties(); this.mapServiceContext = service.getMapServiceContext(); this.mapConfig = mapConfig; this.partitionStrategy = PartitioningStrategyFactory.getPartitioningStrategy( nodeEngine, mapConfig.getName(), mapConfig.getPartitioningStrategyConfig()); this.localMapStats = mapServiceContext.getLocalMapStatsProvider().getLocalMapStatsImpl(name); this.partitionService = getNodeEngine().getPartitionService(); this.lockSupport = new LockProxySupport( new DefaultObjectNamespace(SERVICE_NAME, name), LockServiceImpl.getMaxLeaseTimeInMillis(properties)); this.operationProvider = mapServiceContext.getMapOperationProvider(name); this.operationService = nodeEngine.getOperationService(); this.serializationService = nodeEngine.getSerializationService(); this.thisAddress = nodeEngine.getClusterService().getThisAddress(); this.statisticsEnabled = mapConfig.isStatisticsEnabled(); this.putAllBatchSize = properties.getInteger(MAP_PUT_ALL_BATCH_SIZE); this.putAllInitialSizeFactor = properties.getFloat(MAP_PUT_ALL_INITIAL_SIZE_FACTOR); }
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); }
/** * - Sends eviction event. - Invalidates near cache. * * @param key the key to be processed. * @param value the value to be processed. */ @Override public void doPostEvictionOperations(Data key, Object value, boolean isExpired) { MapEventPublisher mapEventPublisher = mapServiceContext.getMapEventPublisher(); NodeEngine nodeEngine = mapServiceContext.getNodeEngine(); EventService eventService = nodeEngine.getEventService(); if (!eventService.hasEventRegistration(SERVICE_NAME, name)) { return; } Address thisAddress = nodeEngine.getThisAddress(); // Fire EVICTED event also in case of expiration because historically eviction-listener // listens all kind of eviction and expiration events and by firing EVICTED event we are // preserving // this behavior. mapEventPublisher.publishEvent(thisAddress, name, EVICTED, key, value, null); if (isExpired) { // We will be in this if in two cases: // 1. In case of TTL or max-idle-seconds expiration. // 2. When evicting due to the size-based eviction, we are also firing an EXPIRED event // because there is a possibility that evicted entry may be also an expired one. Trying to // catch // as much as possible expired entries. mapEventPublisher.publishEvent(thisAddress, name, EXPIRED, key, value, null); } }
@Override public InternalCompletableFuture<Boolean> asyncCompareAndSet(E expect, E update) { NodeEngine nodeEngine = getNodeEngine(); Operation operation = new CompareAndSetOperation(name, nodeEngine.toData(expect), nodeEngine.toData(update)); return asyncInvoke(operation, nodeEngine); }
@Override public Xid[] recover(int flag) throws XAException { NodeEngine nodeEngine = getNodeEngine(); XAService xaService = getService(); OperationService operationService = nodeEngine.getOperationService(); ClusterService clusterService = nodeEngine.getClusterService(); Collection<Member> memberList = clusterService.getMembers(); List<InternalCompletableFuture<SerializableList>> futureList = new ArrayList<InternalCompletableFuture<SerializableList>>(); for (Member member : memberList) { if (member.localMember()) { continue; } CollectRemoteTransactionsOperation op = new CollectRemoteTransactionsOperation(); Address address = member.getAddress(); InternalCompletableFuture<SerializableList> future = operationService.invokeOnTarget(SERVICE_NAME, op, address); futureList.add(future); } HashSet<SerializableXID> xids = new HashSet<SerializableXID>(); xids.addAll(xaService.getPreparedXids()); for (InternalCompletableFuture<SerializableList> future : futureList) { SerializableList xidSet = future.getSafely(); for (Data xidData : xidSet) { SerializableXID xid = nodeEngine.toObject(xidData); xids.add(xid); } } return xids.toArray(new SerializableXID[xids.size()]); }
private Result processQueryEventFilter( EventFilter filter, EntryEventType eventType, final Data dataKey, Data dataOldValue, Data dataValue) { final NodeEngine nodeEngine = mapServiceContext.getNodeEngine(); final SerializationService serializationService = nodeEngine.getSerializationService(); Object testValue; if (eventType == EntryEventType.REMOVED || eventType == EntryEventType.EVICTED) { testValue = serializationService.toObject(dataOldValue); } else { testValue = serializationService.toObject(dataValue); } Object key = serializationService.toObject(dataKey); QueryEventFilter queryEventFilter = (QueryEventFilter) filter; QueryEntry entry = new QueryEntry(serializationService, dataKey, key, testValue); if (queryEventFilter.eval(entry)) { if (queryEventFilter.isIncludeValue()) { return Result.VALUE_INCLUDED; } else { return Result.NO_VALUE_INCLUDED; } } return Result.NONE; }
private void publishWithValue( Set<EventRegistration> registrationsWithValue, EntryEventData event, int orderKey) { final NodeEngine nodeEngine = mapServiceContext.getNodeEngine(); nodeEngine .getEventService() .publishEvent(mapServiceContext.serviceName(), registrationsWithValue, event, orderKey); }
@Override public void run() throws Exception { if (!valid) { onExecutionFailure( new IllegalStateException("Wrong target! " + toString() + " cannot be processed!")); return; } NodeEngine nodeEngine = getNodeEngine(); if (backupOp == null && backupOpData != null) { backupOp = nodeEngine.getSerializationService().toObject(backupOpData); } if (backupOp != null) { ensureBackupOperationInitialized(); backupOp.beforeRun(); backupOp.run(); backupOp.afterRun(); } InternalPartitionService partitionService = nodeEngine.getPartitionService(); partitionService.updatePartitionReplicaVersions( getPartitionId(), replicaVersions, getReplicaIndex()); }
protected AbstractEvictableRecordStore(MapContainer mapContainer, int partitionId) { super(mapContainer, partitionId); NodeEngine nodeEngine = mapServiceContext.getNodeEngine(); GroupProperties groupProperties = nodeEngine.getGroupProperties(); expiryDelayMillis = groupProperties.getMillis(GroupProperty.MAP_EXPIRY_DELAY_SECONDS); evictor = mapContainer.getEvictor(); }
private void rollbackTxBackup() { final OperationService operationService = nodeEngine.getOperationService(); final List<Future> futures = new ArrayList<Future>(txLogs.size()); // rollback tx backup if (durability > 0 && transactionType.equals(TransactionType.TWO_PHASE)) { for (Address backupAddress : backupAddresses) { if (nodeEngine.getClusterService().getMember(backupAddress) != null) { final Future f = operationService.invokeOnTarget( TransactionManagerServiceImpl.SERVICE_NAME, new RollbackTxBackupOperation(txnId), backupAddress); futures.add(f); } } for (Future future : futures) { try { future.get(timeoutMillis, TimeUnit.MILLISECONDS); } catch (Throwable e) { nodeEngine.getLogger(getClass()).warning("Error during tx rollback backup!", e); } } futures.clear(); } }
public MultiMapService(NodeEngine nodeEngine) { this.nodeEngine = nodeEngine; int partitionCount = nodeEngine.getPartitionService().getPartitionCount(); partitionContainers = new MultiMapPartitionContainer[partitionCount]; this.logger = nodeEngine.getLogger(MultiMapService.class); dispatcher = new MultiMapEventsDispatcher(this, nodeEngine.getClusterService()); publisher = new MultiMapEventsPublisher(nodeEngine); }
public Future<V> getAsync(final K k) { if (k == null) { throw new NullPointerException(NULL_KEY_IS_NOT_ALLOWED); } Data key = getService().toData(k, partitionStrategy); NodeEngine nodeEngine = getNodeEngine(); return new DelegatingFuture<V>(getAsyncInternal(key), nodeEngine.getSerializationService()); }
void publishEventInternal( Collection<EventRegistration> registrations, Object eventData, int orderKey) { final MapServiceContext mapServiceContext = this.mapServiceContext; final NodeEngine nodeEngine = mapServiceContext.getNodeEngine(); final EventService eventService = nodeEngine.getEventService(); eventService.publishEvent(SERVICE_NAME, registrations, eventData, orderKey); }
public LocalMapStatsProvider(MapServiceContext mapServiceContext) { this.mapServiceContext = mapServiceContext; NodeEngine nodeEngine = mapServiceContext.getNodeEngine(); this.logger = nodeEngine.getLogger(getClass()); this.nearCacheProvider = mapServiceContext.getNearCacheProvider(); this.clusterService = nodeEngine.getClusterService(); this.partitionService = nodeEngine.getPartitionService(); }
@Override public InternalCompletableFuture<E> asyncGetAndAlter(IFunction<E, E> function) { isNotNull(function, "function"); NodeEngine nodeEngine = getNodeEngine(); Operation operation = new GetAndAlterOperation(name, nodeEngine.toData(function)); return asyncInvoke(operation, nodeEngine); }
@Override public <R> InternalCompletableFuture<R> asyncApply(IFunction<E, R> function) { isNotNull(function, "function"); NodeEngine nodeEngine = getNodeEngine(); Operation operation = new ApplyOperation(name, nodeEngine.toData(function)); return asyncInvoke(operation, nodeEngine); }
@Test public void testPostregisteredExecutionCallbackCompletableFuture() throws Exception { HazelcastInstanceProxy proxy = (HazelcastInstanceProxy) createHazelcastInstance(); Field originalField = HazelcastInstanceProxy.class.getDeclaredField("original"); originalField.setAccessible(true); HazelcastInstanceImpl hz = (HazelcastInstanceImpl) originalField.get(proxy); NodeEngine nodeEngine = hz.node.nodeEngine; ExecutionService es = nodeEngine.getExecutionService(); final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); final ExecutorService executorService = Executors.newSingleThreadExecutor(); try { Future future = executorService.submit( new Callable<String>() { @Override public String call() { try { return "success"; } finally { latch1.countDown(); } } }); final ICompletableFuture completableFuture = es.asCompletableFuture(future); latch1.await(30, TimeUnit.SECONDS); final AtomicReference reference = new AtomicReference(); completableFuture.andThen( new ExecutionCallback() { @Override public void onResponse(Object response) { reference.set(response); latch2.countDown(); } @Override public void onFailure(Throwable t) { reference.set(t); latch2.countDown(); } }); latch2.await(30, TimeUnit.SECONDS); if (reference.get() instanceof Throwable) { ((Throwable) reference.get()).printStackTrace(); } assertEquals("success", reference.get()); } finally { executorService.shutdown(); } }
public LocalMapStats createStats(String name) { LocalMultiMapStatsImpl stats = getLocalMultiMapStatsImpl(name); long ownedEntryCount = 0; long backupEntryCount = 0; long hits = 0; long lockedEntryCount = 0; ClusterServiceImpl clusterService = (ClusterServiceImpl) nodeEngine.getClusterService(); Address thisAddress = clusterService.getThisAddress(); for (int i = 0; i < nodeEngine.getPartitionService().getPartitionCount(); i++) { InternalPartition partition = nodeEngine.getPartitionService().getPartition(i); MultiMapPartitionContainer partitionContainer = getPartitionContainer(i); MultiMapContainer multiMapContainer = partitionContainer.getCollectionContainer(name); if (multiMapContainer == null) { continue; } Address owner = partition.getOwnerOrNull(); if (owner != null) { if (owner.equals(thisAddress)) { lockedEntryCount += multiMapContainer.getLockedCount(); for (MultiMapWrapper wrapper : multiMapContainer.multiMapWrappers.values()) { hits += wrapper.getHits(); ownedEntryCount += wrapper.getCollection(false).size(); } } else { int backupCount = multiMapContainer.config.getTotalBackupCount(); for (int j = 1; j <= backupCount; j++) { Address replicaAddress = partition.getReplicaAddress(j); int memberSize = nodeEngine.getClusterService().getMembers().size(); int tryCount = REPLICA_ADDRESS_TRY_COUNT; // wait if the partition table is not updated yet while (memberSize > backupCount && replicaAddress == null && tryCount-- > 0) { try { Thread.sleep(REPLICA_ADDRESS_SLEEP_WAIT_MILLIS); } catch (InterruptedException e) { throw ExceptionUtil.rethrow(e); } replicaAddress = partition.getReplicaAddress(j); } if (replicaAddress != null && replicaAddress.equals(thisAddress)) { for (MultiMapWrapper wrapper : multiMapContainer.multiMapWrappers.values()) { backupEntryCount += wrapper.getCollection(false).size(); } } } } } } stats.setOwnedEntryCount(ownedEntryCount); stats.setBackupEntryCount(backupEntryCount); stats.setHits(hits); stats.setLockedEntryCount(lockedEntryCount); return stats; }
@Before public void setUp() throws Exception { NodeEngine nodeEngine = getNode(createHazelcastInstance()).getNodeEngine(); executionService = nodeEngine.getExecutionService(); startLogicLatch = new CountDownLatch(1); executedLogic = new CountDownLatch(1); inExecutionLatch = new CountDownLatch(1); reference1 = new AtomicReference<Object>(); reference2 = new AtomicReference<Object>(); }
protected void destroyCacheOnAllMembers(String name, String callerUuid) { final OperationService operationService = nodeEngine.getOperationService(); final Collection<Member> members = nodeEngine.getClusterService().getMembers(); for (Member member : members) { if (!member.localMember() && !member.getUuid().equals(callerUuid)) { final CacheDestroyOperation op = new CacheDestroyOperation(name, true); operationService.invokeOnTarget(AbstractCacheService.SERVICE_NAME, op, member.getAddress()); } } }
public static void fireEvent(Data key, Object value, String mapName, MapService mapService) { final NodeEngine nodeEngine = mapService.getNodeEngine(); mapService.publishEvent( nodeEngine.getThisAddress(), mapName, EntryEventType.EVICTED, key, mapService.toData(value), null); }