Search in sources :

Example 1 with OperationRunner

use of com.hazelcast.spi.impl.operationexecutor.OperationRunner in project hazelcast by hazelcast.

the class OperationExecutorImpl method isInvocationAllowed.

@Override
public boolean isInvocationAllowed(Operation op, boolean isAsync) {
    checkNotNull(op, "op can't be null");
    Thread currentThread = Thread.currentThread();
    // IO threads are not allowed to run any operation
    if (currentThread instanceof OperationHostileThread) {
        return false;
    }
    // if it is async we don't need to check if it is PartitionOperationThread or not
    if (isAsync) {
        return true;
    }
    // allowed to invoke non partition specific task
    if (op.getPartitionId() < 0) {
        return true;
    }
    // allowed to invoke from non PartitionOperationThreads (including GenericOperationThread)
    if (currentThread.getClass() != PartitionOperationThread.class) {
        return true;
    }
    PartitionOperationThread partitionThread = (PartitionOperationThread) currentThread;
    OperationRunner runner = partitionThread.currentRunner;
    if (runner != null) {
        // in this case partitionId of both inner and outer operations have to match
        return runner.getPartitionId() == op.getPartitionId();
    }
    return toPartitionThreadIndex(op.getPartitionId()) == partitionThread.threadId;
}
Also used : OperationHostileThread(com.hazelcast.spi.impl.operationexecutor.OperationHostileThread) OperationRunner(com.hazelcast.spi.impl.operationexecutor.OperationRunner) OperationHostileThread(com.hazelcast.spi.impl.operationexecutor.OperationHostileThread)

Example 2 with OperationRunner

use of com.hazelcast.spi.impl.operationexecutor.OperationRunner in project hazelcast by hazelcast.

the class OperationExecutorImpl method initPartitionThreads.

private PartitionOperationThread[] initPartitionThreads(HazelcastProperties properties, HazelcastThreadGroup threadGroup, NodeExtension nodeExtension) {
    int threadCount = properties.getInteger(PARTITION_OPERATION_THREAD_COUNT);
    if (threadCount <= 0) {
        // default partition operation thread count
        int coreSize = Runtime.getRuntime().availableProcessors();
        threadCount = Math.max(2, coreSize);
    }
    PartitionOperationThread[] threads = new PartitionOperationThread[threadCount];
    for (int threadId = 0; threadId < threads.length; threadId++) {
        String threadName = threadGroup.getThreadPoolNamePrefix("partition-operation") + threadId;
        // the normalQueue will be a blocking queue. We don't want to idle, because there are many operation threads.
        MPSCQueue<Object> normalQueue = new MPSCQueue<Object>(null);
        OperationQueue operationQueue = new DefaultOperationQueue(normalQueue, new ConcurrentLinkedQueue<Object>());
        PartitionOperationThread partitionThread = new PartitionOperationThread(threadName, threadId, operationQueue, logger, threadGroup, nodeExtension, partitionOperationRunners);
        threads[threadId] = partitionThread;
        normalQueue.setConsumerThread(partitionThread);
    }
    // we need to assign the PartitionOperationThreads to all OperationRunners they own
    for (int partitionId = 0; partitionId < partitionOperationRunners.length; partitionId++) {
        int threadId = getPartitionThreadId(partitionId, threadCount);
        Thread thread = threads[threadId];
        OperationRunner runner = partitionOperationRunners[partitionId];
        runner.setCurrentThread(thread);
    }
    return threads;
}
Also used : MPSCQueue(com.hazelcast.internal.util.concurrent.MPSCQueue) OperationRunner(com.hazelcast.spi.impl.operationexecutor.OperationRunner) OperationHostileThread(com.hazelcast.spi.impl.operationexecutor.OperationHostileThread)

Example 3 with OperationRunner

use of com.hazelcast.spi.impl.operationexecutor.OperationRunner in project hazelcast by hazelcast.

the class OperationExecutorImpl method run.

@Override
public void run(Operation operation) {
    checkNotNull(operation, "operation can't be null");
    if (!isRunAllowed(operation)) {
        throw new IllegalThreadStateException("Operation '" + operation + "' cannot be run in current thread: " + Thread.currentThread());
    }
    OperationRunner operationRunner = getOperationRunner(operation);
    operationRunner.run(operation);
}
Also used : OperationRunner(com.hazelcast.spi.impl.operationexecutor.OperationRunner)

Example 4 with OperationRunner

use of com.hazelcast.spi.impl.operationexecutor.OperationRunner in project hazelcast by hazelcast.

the class InterceptorRegistryTest method getPartitionOperationThread.

private PartitionOperationThread getPartitionOperationThread(OperationQueue queue) {
    HazelcastThreadGroup threadGroup = new HazelcastThreadGroup("instanceName", LOGGER, getClass().getClassLoader());
    NodeExtension nodeExtension = mock(NodeExtension.class);
    OperationRunner operationRunner = mock(OperationRunner.class);
    OperationRunner[] operationRunners = new OperationRunner[] { operationRunner };
    return new PartitionOperationThread("threadName", 0, queue, LOGGER, threadGroup, nodeExtension, operationRunners);
}
Also used : PartitionOperationThread(com.hazelcast.spi.impl.operationexecutor.impl.PartitionOperationThread) OperationRunner(com.hazelcast.spi.impl.operationexecutor.OperationRunner) HazelcastThreadGroup(com.hazelcast.instance.HazelcastThreadGroup) NodeExtension(com.hazelcast.instance.NodeExtension)

Example 5 with OperationRunner

use of com.hazelcast.spi.impl.operationexecutor.OperationRunner in project hazelcast by hazelcast.

the class OperationExecutorImpl_GetOperationRunnerTest method test_whenCallerIsNormalThread_andGenericOperation_thenReturnAdHocRunner.

@Test
public void test_whenCallerIsNormalThread_andGenericOperation_thenReturnAdHocRunner() {
    initExecutor();
    Operation op = new DummyOperation(-1);
    OperationRunner operationRunner = executor.getOperationRunner(op);
    DummyOperationRunnerFactory f = (DummyOperationRunnerFactory) handlerFactory;
    assertSame(f.adhocHandler, operationRunner);
}
Also used : OperationRunner(com.hazelcast.spi.impl.operationexecutor.OperationRunner) Operation(com.hazelcast.spi.Operation) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

OperationRunner (com.hazelcast.spi.impl.operationexecutor.OperationRunner)13 Operation (com.hazelcast.spi.Operation)5 QuickTest (com.hazelcast.test.annotation.QuickTest)5 Test (org.junit.Test)5 AssertTask (com.hazelcast.test.AssertTask)4 Packet (com.hazelcast.nio.Packet)3 OperationHostileThread (com.hazelcast.spi.impl.operationexecutor.OperationHostileThread)2 OperationRunnerFactory (com.hazelcast.spi.impl.operationexecutor.OperationRunnerFactory)2 HazelcastThreadGroup (com.hazelcast.instance.HazelcastThreadGroup)1 NodeExtension (com.hazelcast.instance.NodeExtension)1 MPSCQueue (com.hazelcast.internal.util.concurrent.MPSCQueue)1 ILogger (com.hazelcast.logging.ILogger)1 UrgentSystemOperation (com.hazelcast.spi.UrgentSystemOperation)1 PartitionSpecificRunnable (com.hazelcast.spi.impl.PartitionSpecificRunnable)1 PartitionOperationThread (com.hazelcast.spi.impl.operationexecutor.impl.PartitionOperationThread)1 Backup (com.hazelcast.spi.impl.operationservice.impl.operations.Backup)1 ParallelTest (com.hazelcast.test.annotation.ParallelTest)1