private static int getRunningOperationCount(OperationRunner[] runners) { int result = 0; for (OperationRunner runner : runners) { if (runner.currentTask() != null) { result++; } } return result; }
private String getStackTraceOrNull(OperationRunner operationRunner, Object operation) { String prefix = ""; for (StackTraceElement stackTraceElement : operationRunner.currentThread().getStackTrace()) { stackTraceStringBuilder.append(prefix).append(stackTraceElement.toString()); prefix = "\n\t"; } // check if the operation is still the same if (operationRunner.currentTask() != operation) { stackTraceStringBuilder.setLength(0); return null; } String stackTrace = stackTraceStringBuilder.toString(); stackTraceStringBuilder.setLength(0); return stackTrace; }
@Probe(name = "completedCount", level = MANDATORY) public long getExecutedOperationCount() { long result = adHocOperationRunner.executedOperationsCount(); for (OperationRunner runner : genericOperationRunners) { result += runner.executedOperationsCount(); } for (OperationRunner runner : partitionOperationRunners) { result += runner.executedOperationsCount(); } return result; }
private void process(Packet packet) throws Exception { currentRunner = operationRunner(packet.getPartitionId()); currentRunner.run(packet); completedPacketCount.inc(); }
private GenericOperationThread[] initGenericThreads(String hzName, NodeExtension nodeExtension, ClassLoader configClassLoader) { // we created as many generic operation handlers, as there are generic threads int threadCount = genericOperationRunners.length; GenericOperationThread[] threads = new GenericOperationThread[threadCount]; int threadId = 0; for (int threadIndex = 0; threadIndex < threads.length; threadIndex++) { boolean priority = threadIndex < priorityThreadCount; String baseName = priority ? "priority-generic-operation" : "generic-operation"; String threadName = createThreadPoolName(hzName, baseName) + threadId; OperationRunner operationRunner = genericOperationRunners[threadIndex]; GenericOperationThread operationThread = new GenericOperationThread( threadName, threadIndex, genericQueue, logger, nodeExtension, operationRunner, priority, configClassLoader); threads[threadIndex] = operationThread; operationRunner.setCurrentThread(operationThread); if (threadIndex == priorityThreadCount - 1) { threadId = 0; } else { threadId++; } } return threads; }
@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) { // non null runner means it's a nested call // in this case partitionId of both inner and outer operations have to match return runner.getPartitionId() == op.getPartitionId(); } return toPartitionThreadIndex(op.getPartitionId()) == partitionThread.threadId; }
@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); }
private GenericOperationThread[] initGenericThreads(String hzName, NodeExtension nodeExtension, ClassLoader configClassLoader) { // we created as many generic operation handlers, as there are generic threads int threadCount = genericOperationRunners.length; GenericOperationThread[] threads = new GenericOperationThread[threadCount]; int threadId = 0; for (int threadIndex = 0; threadIndex < threads.length; threadIndex++) { boolean priority = threadIndex < priorityThreadCount; String baseName = priority ? "priority-generic-operation" : "generic-operation"; String threadName = createThreadPoolName(hzName, baseName) + threadId; OperationRunner operationRunner = genericOperationRunners[threadIndex]; GenericOperationThread operationThread = new GenericOperationThread( threadName, threadIndex, genericQueue, logger, nodeExtension, operationRunner, priority, configClassLoader); threads[threadIndex] = operationThread; operationRunner.setCurrentThread(operationThread); if (threadIndex == priorityThreadCount - 1) { threadId = 0; } else { threadId++; } } return threads; }
@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) { // non null runner means it's a nested call // in this case partitionId of both inner and outer operations have to match return runner.getPartitionId() == op.getPartitionId(); } return toPartitionThreadIndex(op.getPartitionId()) == partitionThread.threadId; }
private void process(Operation operation) { currentRunner = operationRunner(operation.getPartitionId()); currentRunner.run(operation); completedOperationCount.inc(); }
private static int getRunningOperationCount(OperationRunner[] runners) { int result = 0; for (OperationRunner runner : runners) { if (runner.currentTask() != null) { result++; } } return result; }
private String getStackTraceOrNull(OperationRunner operationRunner, Object operation) { String prefix = ""; for (StackTraceElement stackTraceElement : operationRunner.currentThread().getStackTrace()) { stackTraceStringBuilder.append(prefix).append(stackTraceElement.toString()); prefix = "\n\t"; } // check if the operation is still the same if (operationRunner.currentTask() != operation) { stackTraceStringBuilder.setLength(0); return null; } String stackTrace = stackTraceStringBuilder.toString(); stackTraceStringBuilder.setLength(0); return stackTrace; }
private PartitionOperationThread[] initPartitionThreads(HazelcastProperties properties, String hzName, NodeExtension nodeExtension, ClassLoader configClassLoader) { int threadCount = properties.getInteger(PARTITION_OPERATION_THREAD_COUNT); if (threadCount <= 0) { // default partition operation thread count int coreSize = RuntimeAvailableProcessors.get(); threadCount = Math.max(2, coreSize); } IdleStrategy idleStrategy = getIdleStrategy(properties, IDLE_STRATEGY); PartitionOperationThread[] threads = new PartitionOperationThread[threadCount]; for (int threadId = 0; threadId < threads.length; threadId++) { String threadName = createThreadPoolName(hzName, "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>(idleStrategy); OperationQueue operationQueue = new OperationQueueImpl(normalQueue, new ConcurrentLinkedQueue<Object>()); PartitionOperationThread partitionThread = new PartitionOperationThread(threadName, threadId, operationQueue, logger, nodeExtension, partitionOperationRunners, configClassLoader); 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; }
@Probe(name = "completedCount", level = MANDATORY) public long getExecutedOperationCount() { long result = adHocOperationRunner.executedOperationsCount(); for (OperationRunner runner : genericOperationRunners) { result += runner.executedOperationsCount(); } for (OperationRunner runner : partitionOperationRunners) { result += runner.executedOperationsCount(); } return result; }
private void process(Packet packet) throws Exception { currentRunner = operationRunner(packet.getPartitionId()); currentRunner.run(packet); completedPacketCount.inc(); }
private void sample(OperationRunner[] runners, ItemCounter<String> samples) { for (OperationRunner runner : runners) { Object task = runner.currentTask(); if (task != null) { samples.inc(toKey(task)); } } }
private PartitionOperationThread[] initPartitionThreads(HazelcastProperties properties, String hzName, NodeExtension nodeExtension, ClassLoader configClassLoader) { int threadCount = properties.getInteger(PARTITION_OPERATION_THREAD_COUNT); if (threadCount <= 0) { // default partition operation thread count int coreSize = RuntimeAvailableProcessors.get(); threadCount = Math.max(2, coreSize); } IdleStrategy idleStrategy = getIdleStrategy(properties, IDLE_STRATEGY); PartitionOperationThread[] threads = new PartitionOperationThread[threadCount]; for (int threadId = 0; threadId < threads.length; threadId++) { String threadName = createThreadPoolName(hzName, "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>(idleStrategy); OperationQueue operationQueue = new OperationQueueImpl(normalQueue, new ConcurrentLinkedQueue<Object>()); PartitionOperationThread partitionThread = new PartitionOperationThread(threadName, threadId, operationQueue, logger, nodeExtension, partitionOperationRunners, configClassLoader); 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; }
@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); }
private void sample(OperationRunner[] runners, ItemCounter<String> samples) { for (OperationRunner runner : runners) { Object task = runner.currentTask(); if (task != null) { samples.inc(toKey(task)); } } }
private void process(PartitionSpecificRunnable runnable) { currentRunner = operationRunner(runnable.getPartitionId()); currentRunner.run(runnable); completedPartitionSpecificRunnableCount.inc(); }