com.hazelcast.spi.impl.operationexecutor
Class OperationRunner

java.lang.Object
  extended by com.hazelcast.spi.impl.operationexecutor.OperationRunner

public abstract class OperationRunner
extends Object

The OperationRunner is responsible for the actual running of operations.

So the OperationExecutor is responsible for 'executing' them (so finding a thread to run on), the actual work is done by the OperationRunner. This separation of concerns makes the code a lot simpler and makes it possible to swap parts of the system.

Since HZ 3.5 there are multiple OperationRunner instances; each partition will have its own OperationRunner, but also generic threads will have their own OperationRunners. Each OperationRunner exposes the Operation it is currently working on and this makes it possible to hook on all kinds of additional functionality like detecting slow operations, sampling which operations are executed most frequently, check if an operation is still running, etc etc.


Field Summary
protected  Object currentTask
           
protected  int partitionId
           
 
Constructor Summary
OperationRunner(int partitionId)
           
 
Method Summary
 Object currentTask()
          Returns the current task that is executing.
 Thread currentThread()
          Get the thread that is currently running this OperationRunner instance.
 int getPartitionId()
          Returns the partitionId this OperationRunner is responsible for.
abstract  void run(Operation task)
           
abstract  void run(Packet packet)
           
abstract  void run(Runnable task)
           
 void setCurrentThread(Thread currentThread)
          Sets the thread that is running this OperationRunner instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

partitionId

protected final int partitionId

currentTask

protected volatile Object currentTask
Constructor Detail

OperationRunner

public OperationRunner(int partitionId)
Method Detail

run

public abstract void run(Packet packet)
                  throws Exception
Throws:
Exception

run

public abstract void run(Runnable task)

run

public abstract void run(Operation task)

currentTask

public final Object currentTask()
Returns the current task that is executing. This value could be null if no operation is executing.

Value could be stale as soon as it is returned.

This method is thread-safe; so the thread that executes a task will set/unset the current task, any other thread in the system is allowed to read it.

Returns:
the current running task.

setCurrentThread

public final void setCurrentThread(Thread currentThread)
Sets the thread that is running this OperationRunner instance.

This method should only be called from the OperationExecutor since this component is responsible for executing operations on an OperationRunner.

Parameters:
currentThread - the current Thread. Can be called with 'null', clearing the currentThread field.

currentThread

public final Thread currentThread()
Get the thread that is currently running this OperationRunner instance.

This value only has meaning when an Operation is running. It depends on the implementation if the field is unset after an operation is executed or not. So it could be that a value is returned while no operation is running.

For example, the ClassicOperationExecutor will never unset this field since each OperationRunner is bound to a single OperationThread; so this field is initialized when the OperationRunner is created.

The returned value could be null. When it is null, currently no thread is running this OperationRunner.

Recommended idiom for slow operation detection: 1: First read the operation and store the reference. 2: Then read the current thread and store the reference 3: Later read the operation again. If the operation-instance is the same, it means that you have captured the right thread.

Then you use this Thread to create a stack trace. It can happen that the stracktrace doesn't reflect the call-state the thread had when the slow operation was detected. This could be solved by rechecking the currentTask after you have detected the slow operation. BUt don't create a stacktrace before you do the first recheck of the operation because otherwise it will cause a lot of overhead.

Returns:
the Thread that currently is running this OperationRunner instance.

getPartitionId

public final int getPartitionId()
Returns the partitionId this OperationRunner is responsible for. If the partition id is smaller than 0, it is either a generic or ad hoc OperationRunner.

The value will never change for this OperationRunner instance.

Returns:
the partition id.


Copyright © 2015 Hazelcast, Inc.. All Rights Reserved.