Search in sources :

Example 36 with OperationService

use of com.hazelcast.spi.OperationService in project hazelcast by hazelcast.

the class MapReduceUtil method executeOperation.

public static <V> List<V> executeOperation(Collection<Member> members, OperationFactory operationFactory, MapReduceService mapReduceService, NodeEngine nodeEngine) {
    final OperationService operationService = nodeEngine.getOperationService();
    final List<InternalCompletableFuture<V>> futures = new ArrayList<InternalCompletableFuture<V>>();
    final List<V> results = new ArrayList<V>();
    final List<Exception> exceptions = new ArrayList<Exception>(members.size());
    for (Member member : members) {
        try {
            Operation operation = operationFactory.createOperation();
            if (nodeEngine.getThisAddress().equals(member.getAddress())) {
                // Locally we can call the operation directly
                operation.setNodeEngine(nodeEngine);
                operation.setCallerUuid(nodeEngine.getLocalMember().getUuid());
                operation.setService(mapReduceService);
                operation.run();
                V response = (V) operation.getResponse();
                if (response != null) {
                    results.add(response);
                }
            } else {
                InvocationBuilder ib = operationService.createInvocationBuilder(SERVICE_NAME, operation, member.getAddress());
                final InternalCompletableFuture<V> future = ib.invoke();
                futures.add(future);
            }
        } catch (Exception e) {
            exceptions.add(e);
        }
    }
    for (InternalCompletableFuture<V> future : futures) {
        try {
            V response = future.join();
            if (response != null) {
                results.add(response);
            }
        } catch (Exception e) {
            exceptions.add(e);
        }
    }
    if (exceptions.size() > 0) {
        throw new RemoteMapReduceException("Exception on mapreduce operation", exceptions);
    }
    return results;
}
Also used : InternalCompletableFuture(com.hazelcast.spi.InternalCompletableFuture) ArrayList(java.util.ArrayList) Operation(com.hazelcast.spi.Operation) NotifyRemoteExceptionOperation(com.hazelcast.mapreduce.impl.operation.NotifyRemoteExceptionOperation) RemoteMapReduceException(com.hazelcast.mapreduce.RemoteMapReduceException) TimeoutException(java.util.concurrent.TimeoutException) RemoteMapReduceException(com.hazelcast.mapreduce.RemoteMapReduceException) OperationService(com.hazelcast.spi.OperationService) InvocationBuilder(com.hazelcast.spi.InvocationBuilder) Member(com.hazelcast.core.Member)

Example 37 with OperationService

use of com.hazelcast.spi.OperationService in project hazelcast by hazelcast.

the class TransactionalMultiMapProxySupport method nextId.

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);
    }
}
Also used : NodeEngine(com.hazelcast.spi.NodeEngine) OperationService(com.hazelcast.spi.OperationService)

Example 38 with OperationService

use of com.hazelcast.spi.OperationService in project hazelcast by hazelcast.

the class TransactionalMultiMapProxySupport method size.

public int size() {
    checkTransactionActive();
    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()) {
            MultiMapTransactionLogRecord log = (MultiMapTransactionLogRecord) tx.get(getRecordLogKey(key));
            if (log != null) {
                size += log.size();
            }
        }
        return size;
    } catch (Throwable t) {
        throw ExceptionUtil.rethrow(t);
    }
}
Also used : MultiMapOperationFactory(com.hazelcast.multimap.impl.operations.MultiMapOperationFactory) TransactionalDistributedObject(com.hazelcast.spi.TransactionalDistributedObject) TransactionalObject(com.hazelcast.transaction.TransactionalObject) Data(com.hazelcast.nio.serialization.Data) OperationService(com.hazelcast.spi.OperationService)

Example 39 with OperationService

use of com.hazelcast.spi.OperationService in project hazelcast by hazelcast.

the class TransactionalMultiMapProxySupport method lockAndGet.

private MultiMapResponse lockAndGet(Data key, long timeout, long ttl) {
    final NodeEngine nodeEngine = getNodeEngine();
    boolean blockReads = tx.getTransactionType() == TransactionType.ONE_PHASE;
    TxnLockAndGetOperation operation = new TxnLockAndGetOperation(name, key, timeout, ttl, getThreadId(), blockReads);
    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);
    }
}
Also used : NodeEngine(com.hazelcast.spi.NodeEngine) MultiMapResponse(com.hazelcast.multimap.impl.operations.MultiMapResponse) OperationService(com.hazelcast.spi.OperationService)

Example 40 with OperationService

use of com.hazelcast.spi.OperationService in project hazelcast by hazelcast.

the class RingbufferProxy method readManyAsync.

@Override
public ICompletableFuture<ReadResultSet<E>> readManyAsync(long startSequence, int minCount, int maxCount, IFunction<E, Boolean> filter) {
    checkSequence(startSequence);
    checkNotNegative(minCount, "minCount can't be smaller than 0");
    checkTrue(maxCount >= minCount, "maxCount should be equal or larger than minCount");
    checkTrue(minCount <= config.getCapacity(), "the minCount should be smaller than or equal to the capacity");
    checkTrue(maxCount <= MAX_BATCH_SIZE, "maxCount can't be larger than " + MAX_BATCH_SIZE);
    Operation op = new ReadManyOperation(name, startSequence, minCount, maxCount, filter).setPartitionId(partitionId);
    OperationService operationService = getOperationService();
    return operationService.createInvocationBuilder(null, op, partitionId).setCallTimeout(Long.MAX_VALUE).invoke();
}
Also used : AddAllOperation(com.hazelcast.ringbuffer.impl.operations.AddAllOperation) GenericOperation(com.hazelcast.ringbuffer.impl.operations.GenericOperation) ReadOneOperation(com.hazelcast.ringbuffer.impl.operations.ReadOneOperation) ReadManyOperation(com.hazelcast.ringbuffer.impl.operations.ReadManyOperation) AddOperation(com.hazelcast.ringbuffer.impl.operations.AddOperation) Operation(com.hazelcast.spi.Operation) OperationService(com.hazelcast.spi.OperationService) ReadManyOperation(com.hazelcast.ringbuffer.impl.operations.ReadManyOperation)

Aggregations

OperationService (com.hazelcast.spi.OperationService)135 Test (org.junit.Test)49 QuickTest (com.hazelcast.test.annotation.QuickTest)48 ParallelTest (com.hazelcast.test.annotation.ParallelTest)46 HazelcastInstance (com.hazelcast.core.HazelcastInstance)45 Operation (com.hazelcast.spi.Operation)39 NodeEngine (com.hazelcast.spi.NodeEngine)30 Address (com.hazelcast.nio.Address)26 Future (java.util.concurrent.Future)26 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)24 Config (com.hazelcast.config.Config)21 InternalCompletableFuture (com.hazelcast.spi.InternalCompletableFuture)21 Member (com.hazelcast.core.Member)19 Data (com.hazelcast.nio.serialization.Data)14 ArrayList (java.util.ArrayList)11 Node (com.hazelcast.instance.Node)7 InternalOperationService (com.hazelcast.spi.impl.operationservice.InternalOperationService)7 ExecutionException (java.util.concurrent.ExecutionException)7 TimeoutException (java.util.concurrent.TimeoutException)7 OperationTimeoutException (com.hazelcast.core.OperationTimeoutException)6