// TODO: add a feature to mancenter to sync cache to db completely public void flush() { try { MapOperation mapFlushOperation = operationProvider.createMapFlushOperation(name); BinaryOperationFactory operationFactory = new BinaryOperationFactory(mapFlushOperation, getNodeEngine()); Map<Integer, Object> results = operationService.invokeOnAllPartitions(SERVICE_NAME, operationFactory); List<Future> futures = new ArrayList<Future>(); for (Entry<Integer, Object> entry : results.entrySet()) { Integer partitionId = entry.getKey(); Long count = ((Long) entry.getValue()); if (count != 0) { Operation operation = new AwaitMapFlushOperation(name, count); futures.add( operationService.invokeOnPartition(MapService.SERVICE_NAME, operation, partitionId)); } } for (Future future : futures) { future.get(); } } catch (Throwable t) { throw rethrow(t); } }
private Object invokeOperation(Data key, MapOperation operation) { int partitionId = getNodeEngine().getPartitionService().getPartitionId(key); operation.setThreadId(ThreadUtil.getThreadId()); try { Object result; if (statisticsEnabled) { long time = System.currentTimeMillis(); Future future = operationService .createInvocationBuilder(SERVICE_NAME, operation, partitionId) .setResultDeserialized(false) .invoke(); result = future.get(); mapServiceContext.incrementOperationStats(time, localMapStats, name, operation); } else { Future future = operationService .createInvocationBuilder(SERVICE_NAME, operation, partitionId) .setResultDeserialized(false) .invoke(); result = future.get(); } return result; } catch (Throwable t) { throw rethrow(t); } }
public int size() { checkTransactionState(); try { final OperationService operationService = getNodeEngine().getOperationService(); final Map<Integer, Object> results = operationService.invokeOnAllPartitions( MultiMapService.SERVICE_NAME, new MultiMapOperationFactory( name, MultiMapOperationFactory.OperationFactoryType.SIZE)); int size = 0; for (Object obj : results.values()) { if (obj == null) { continue; } Integer result = getNodeEngine().toObject(obj); size += result; } for (Data key : txMap.keySet()) { MultiMapTransactionLog log = (MultiMapTransactionLog) tx.getTransactionLog(getTxLogKey(key)); if (log != null) { size += log.size(); } } return size; } catch (Throwable t) { throw ExceptionUtil.rethrow(t); } }
@Override public void run() { final Address endpoint = getCallerAddress(); if (endpoint == null) { return; } final ClusterServiceImpl clusterService = getService(); final ILogger logger = getLogger(); final MemberImpl member = clusterService.getMember(endpoint); if (member == null) { logger.warning( "MasterConfirmation has been received from " + endpoint + ", but it is not a member of this cluster!"); OperationService operationService = getNodeEngine().getOperationService(); operationService.send(new MemberRemoveOperation(clusterService.getThisAddress()), endpoint); } else { if (clusterService.isMaster()) { clusterService.getClusterHeartbeatManager().acceptMasterConfirmation(member, timestamp); } else { logger.warning(endpoint + " has sent MasterConfirmation, but this node is not master!"); } } }
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(); } }
@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()]); }
public void clearInternal() { try { Operation clearOperation = operationProvider.createClearOperation(name); clearOperation.setServiceName(SERVICE_NAME); BinaryOperationFactory factory = new BinaryOperationFactory(clearOperation, getNodeEngine()); Map<Integer, Object> resultMap = operationService.invokeOnAllPartitions(SERVICE_NAME, factory); int numberOfAffectedEntries = 0; for (Object object : resultMap.values()) { numberOfAffectedEntries += (Integer) object; } MemberSelector selector = MemberSelectors.and(LITE_MEMBER_SELECTOR, NON_LOCAL_MEMBER_SELECTOR); for (Member member : getNodeEngine().getClusterService().getMembers(selector)) { operationService.invokeOnTarget( SERVICE_NAME, new ClearOperation(name), member.getAddress()); } if (numberOfAffectedEntries > 0) { publishMapEvent(numberOfAffectedEntries, EntryEventType.CLEAR_ALL); } } catch (Throwable t) { throw rethrow(t); } }
private <E> InternalCompletableFuture<E> asyncInvoke(Operation operation, NodeEngine nodeEngine) { try { OperationService operationService = nodeEngine.getOperationService(); return operationService.invokeOnPartition( AtomicReferenceService.SERVICE_NAME, operation, partitionId); } catch (Throwable throwable) { throw rethrow(throwable); } }
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()); } } }
@Override public Long call() throws Exception { try { OperationService operationService = HazelcastTestUtils.getOperationService(hz); return operationService.getExecutedOperationCount(); } catch (NoSuchMethodError e) { LOGGER.warning(e); return -1L; } }
@Override public Map<ClientType, Integer> getConnectedClientStats() { int numberOfCppClients = 0; int numberOfDotNetClients = 0; int numberOfJavaClients = 0; int numberOfOtherClients = 0; Operation clientInfoOperation = new GetConnectedClientsOperation(); OperationService operationService = node.nodeEngine.getOperationService(); Map<ClientType, Integer> resultMap = new HashMap<ClientType, Integer>(); Map<String, ClientType> clientsMap = new HashMap<String, ClientType>(); for (Member member : node.getClusterService().getMembers()) { Address target = member.getAddress(); Future<Map<String, ClientType>> future = operationService.invokeOnTarget(SERVICE_NAME, clientInfoOperation, target); try { Map<String, ClientType> endpoints = future.get(); if (endpoints == null) { continue; } // Merge connected clients according to their uuid. for (Map.Entry<String, ClientType> entry : endpoints.entrySet()) { clientsMap.put(entry.getKey(), entry.getValue()); } } catch (Exception e) { logger.warning("Cannot get client information from: " + target.toString(), e); } } // Now we are regrouping according to the client type for (ClientType clientType : clientsMap.values()) { switch (clientType) { case JAVA: numberOfJavaClients++; break; case CSHARP: numberOfDotNetClients++; break; case CPP: numberOfCppClients++; break; default: numberOfOtherClients++; } } resultMap.put(ClientType.CPP, numberOfCppClients); resultMap.put(ClientType.CSHARP, numberOfDotNetClients); resultMap.put(ClientType.JAVA, numberOfJavaClients); resultMap.put(ClientType.OTHER, numberOfOtherClients); return resultMap; }
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); }
protected void replicateClearOperation() { final OperationService operationService = getNodeEngine().getOperationService(); Collection<Address> members = getMemberAddresses(); for (Address address : members) { ClearOperation clearOperation = new ClearOperation(mapName, false); clearOperation.setPartitionId(getPartitionId()); clearOperation.setValidateTarget(false); operationService .createInvocationBuilder(getServiceName(), clearOperation, address) .setTryCount(INVOCATION_TRY_COUNT) .invoke(); } }
private long nextId(Data key) { final NodeEngine nodeEngine = getNodeEngine(); TxnGenerateRecordIdOperation operation = new TxnGenerateRecordIdOperation(name, key); try { int partitionId = nodeEngine.getPartitionService().getPartitionId(key); final OperationService operationService = nodeEngine.getOperationService(); Future<Long> f = operationService.invokeOnPartition(MultiMapService.SERVICE_NAME, operation, partitionId); return f.get(); } catch (Throwable t) { throw ExceptionUtil.rethrow(t); } }
private void callDisconnectionOperation(ClientEndpointImpl endpoint) { Collection<Member> memberList = nodeEngine.getClusterService().getMembers(); OperationService operationService = nodeEngine.getOperationService(); ClientDisconnectionOperation op = createClientDisconnectionOperation(endpoint.getUuid()); operationService.runOperationOnCallingThread(op); for (Member member : memberList) { if (!member.localMember()) { op = createClientDisconnectionOperation(endpoint.getUuid()); operationService.send(op, member.getAddress()); } } }
private MultiMapResponse lockAndGet(Data key, long timeout, long ttl) { final NodeEngine nodeEngine = getNodeEngine(); TxnLockAndGetOperation operation = new TxnLockAndGetOperation(name, key, timeout, ttl, getThreadId()); try { int partitionId = nodeEngine.getPartitionService().getPartitionId(key); final OperationService operationService = nodeEngine.getOperationService(); Future<MultiMapResponse> f = operationService.invokeOnPartition(MultiMapService.SERVICE_NAME, operation, partitionId); return f.get(); } catch (Throwable t) { throw ExceptionUtil.rethrow(t); } }
private List<Future> startTxBackup() { final OperationService operationService = nodeEngine.getOperationService(); List<Future> futures = new ArrayList<Future>(backupAddresses.length); for (Address backupAddress : backupAddresses) { if (nodeEngine.getClusterService().getMember(backupAddress) != null) { final Future f = operationService.invokeOnTarget( TransactionManagerServiceImpl.SERVICE_NAME, new BeginTxBackupOperation(txOwnerUuid, txnId, xid), backupAddress); futures.add(f); } } return futures; }
private void sendCleanupOperations(List<PartitionContainer> partitionContainers) { final int maxCleanupOperationCountInOneRound = getMaxCleanupOperationCountInOneRound(); final int start = 0; int end = maxCleanupOperationCountInOneRound; if (end > partitionContainers.size()) { end = partitionContainers.size(); } final List<PartitionContainer> partitionIds = partitionContainers.subList(start, end); for (PartitionContainer container : partitionIds) { // mark partition container as has on going expiration operation. container.setHasRunningCleanup(true); OperationService operationService = mapService.getNodeEngine().getOperationService(); operationService.executeOperation( createExpirationOperation(EXPIRATION_PERCENTAGE, container.getPartitionId())); } }
@Override public void run() { try { final OperationService operationService = nodeEngine.getOperationService(); final Map<Integer, Object> results = operationService.invokeOnAllPartitions(getServiceName(), operationFactory); validateResults(results); if (completionListener != null) { completionListener.onCompletion(); } } catch (Exception e) { if (completionListener != null) { completionListener.onException(e); } } }
@Override public Map<Integer, Object> invokeOnAllPartitions(Object request) throws Exception { checkInstanceOf(OperationFactory.class, request, "request"); OperationFactory factory = (OperationFactory) request; return operationService.invokeOnAllPartitions(MapService.SERVICE_NAME, factory); }
@Override public Future invokeOnPartitionOwner(Object operation, int partitionId) { checkNotNull(operation, "operation cannot be null"); checkNotNegative(partitionId, "partitionId"); Operation op = (Operation) operation; return operationService.invokeOnPartition(MapService.SERVICE_NAME, op, partitionId); }
private void purgeTxBackups() { if (durability > 0 && transactionType.equals(TransactionType.TWO_PHASE)) { final OperationService operationService = nodeEngine.getOperationService(); for (Address backupAddress : backupAddresses) { if (nodeEngine.getClusterService().getMember(backupAddress) != null) { try { operationService.invokeOnTarget( TransactionManagerServiceImpl.SERVICE_NAME, new PurgeTxBackupOperation(txnId), backupAddress); } catch (Throwable e) { nodeEngine.getLogger(getClass()).warning("Error during purging backups!", e); } } } } }
private Map<Integer, Object> retryPartitions( Collection<Integer> partitions, OperationFactory operationFactory) { try { return operationService.invokeOnPartitions(SERVICE_NAME, operationFactory, partitions); } catch (Throwable t) { throw rethrow(t); } }
protected void invokePutAllOperationFactory( Address address, final long size, int[] partitions, MapEntries[] entries) throws Exception { OperationFactory factory = operationProvider.createPutAllOperationFactory(name, partitions, entries); final long time = System.currentTimeMillis(); operationService.invokeOnPartitions(SERVICE_NAME, factory, partitions); localMapStats.incrementPuts(size, System.currentTimeMillis() - time); }
@Override public Future invokeOnTarget(Object operation, Address address) { checkNotNull(operation, "operation cannot be null"); checkNotNull(address, "address cannot be null"); Operation op = (Operation) operation; return operationService.invokeOnTarget(MapService.SERVICE_NAME, op, address); }
@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 ICompletableFuture executeOnKeyInternal( Data key, EntryProcessor entryProcessor, ExecutionCallback<Object> callback) { int partitionId = partitionService.getPartitionId(key); MapOperation operation = operationProvider.createEntryOperation(name, key, entryProcessor); operation.setThreadId(ThreadUtil.getThreadId()); try { if (callback == null) { return operationService.invokeOnPartition(SERVICE_NAME, operation, partitionId); } else { return operationService .createInvocationBuilder(SERVICE_NAME, operation, partitionId) .setExecutionCallback(new MapExecutionCallbackAdapter(callback)) .invoke(); } } catch (Throwable t) { throw rethrow(t); } }
public void addIndex(String attribute, boolean ordered) { validateIndexAttribute(attribute); try { AddIndexOperation addIndexOperation = new AddIndexOperation(name, attribute, ordered); operationService.invokeOnAllPartitions( SERVICE_NAME, new BinaryOperationFactory(addIndexOperation, getNodeEngine())); } catch (Throwable t) { throw rethrow(t); } }
private void replicateTxnLog() throws InterruptedException, ExecutionException, java.util.concurrent.TimeoutException { final List<Future> futures = new ArrayList<Future>(txLogs.size()); final OperationService operationService = nodeEngine.getOperationService(); for (Address backupAddress : backupAddresses) { if (nodeEngine.getClusterService().getMember(backupAddress) != null) { final Future f = operationService.invokeOnTarget( TransactionManagerServiceImpl.SERVICE_NAME, new ReplicateTxOperation(txLogs, txOwnerUuid, txnId, timeoutMillis, startTime), backupAddress); futures.add(f); } } for (Future future : futures) { future.get(timeoutMillis, TimeUnit.MILLISECONDS); } futures.clear(); }
protected int valueCountInternal(Data key) { throwExceptionIfNull(key); Collection<MultiMapRecord> coll = txMap.get(key); if (coll == null) { CountOperation operation = new CountOperation(name, key); operation.setThreadId(ThreadUtil.getThreadId()); try { int partitionId = getNodeEngine().getPartitionService().getPartitionId(key); final OperationService operationService = getNodeEngine().getOperationService(); Future<Integer> f = operationService.invokeOnPartition( MultiMapService.SERVICE_NAME, operation, partitionId); return f.get(); } catch (Throwable t) { throw ExceptionUtil.rethrow(t); } } return coll.size(); }