GridGain Developers Hub

Job Scheduling

When jobs arrive at the destination node, they are submitted to a thread pool and scheduled for execution in random order. However, you can change job ordering by configuring CollisionSpi. The CollisionSpi interface provides a way to control how jobs are scheduled for processing on each node.

GridGain provides several implementations of the CollisionSpi interface:

  • FifoQueueCollisionSpi — simple FIFO ordering in multiple threads. This implementation is used by default;

  • PriorityQueueCollisionSpi — priority ordering;

  • JobStealingFailoverSpi — use this implementation to enable job stealing.

To enable a specific collision spi, change the IgniteConfiguration.collisionSpi property.

FIFO Ordering

FifoQueueCollisionSpi provides FIFO ordering of jobs as they arrive. The jobs are executed in multiple threads. The number of threads is controlled by the parallelJobsNumber parameter. The default value equals 2 times the number of processor cores.

<bean class="org.apache.ignite.configuration.IgniteConfiguration">

    <property name="collisionSpi">
        <bean class="org.apache.ignite.spi.collision.fifoqueue.FifoQueueCollisionSpi">
            <!-- Execute one job at a time. -->
            <property name="parallelJobsNumber" value="1"/>
        </bean>
    </property>

</bean>
FifoQueueCollisionSpi colSpi = new FifoQueueCollisionSpi();

// Execute jobs sequentially, one at a time,
// by setting parallel job number to 1.
colSpi.setParallelJobsNumber(1);

IgniteConfiguration cfg = new IgniteConfiguration();

// Override default collision SPI.
cfg.setCollisionSpi(colSpi);

// Start a node.
Ignite ignite = Ignition.start(cfg);
This API is not presently available for C#/.NET. You can use XML configuration.

Priority Ordering

Use PriorityQueueCollisionSpi to assign priorities to individual jobs, so that jobs with higher priority are executed ahead of lower priority jobs. You can also specify the number of threads to process jobs.

<bean class="org.apache.ignite.configuration.IgniteConfiguration">

    <property name="collisionSpi">
        <bean class="org.apache.ignite.spi.collision.priorityqueue.PriorityQueueCollisionSpi">
            <!-- Change the parallel job number if needed. 
                 Default is number of cores times 2. -->
            <property name="parallelJobsNumber" value="5"/>

            <!-- Change the attribute key for tasks.
                 This key is used to look up task priorities from task context.
                 By default, "grid.task.priority" is used. -->
            <property name="priorityAttributeKey" value="grid.task.priority"/>

            <!-- Change the attribute key for jobs.
                 This key is used to look up job priorities from job context.
                 By default, "grid.job.priority" is used. -->
            <property name="jobPriorityAttributeKey" value="grid.job.priority"/>

            <!-- Set the default job priority.
                 By default is set to 0. -->
            <property name="defaultPriority" value="10"/>
        </bean>
    </property>

</bean>
PriorityQueueCollisionSpi colSpi = new PriorityQueueCollisionSpi();

// Change the parallel job number if needed.
// Default is number of cores times 2.
colSpi.setParallelJobsNumber(5);

// Change the attribute key for tasks.
// This key is used to look up task priorities from task context.
// By default, "grid.task.priority" is used.
colSpi.setPriorityAttributeKey("grid.task.priority");

// Change the attribute key for jobs.
// This key is used to look up job priorities from job context.
// By default, "grid.job.priority" is used.
colSpi.setJobPriorityAttributeKey("grid.job.priority");

// Set the default job priority.
// By default is set to 0.
colSpi.setDefaultPriority(10);

IgniteConfiguration cfg = new IgniteConfiguration();

// Override default collision SPI.
cfg.setCollisionSpi(colSpi);

// Start a node.
Ignite ignite = Ignition.start(cfg);
This API is not presently available for C#/.NET. You can use XML configuration.
This API is not presently available for C++. You can use XML configuration.

Changing specific task priority

Task priorities are set in the task session via the grid.task.priority attribute. To change it, the task`s class must have the @ComputeTaskSessionFullSupport annotation. If no priority is assigned to a task, the default priority of 0 is used.

@ComputeTaskSessionFullSupport
public class MyUrgentTask extends ComputeTaskSplitAdapter<Object, Object> {
    // Auto-injected task session.
    @TaskSessionResource
    private ComputeTaskSession taskSes = null;

    @Override
    protected Collection<ComputeJob> split(int gridSize, Object arg) {
        // Set high task priority.
        taskSes.setAttribute("grid.task.priority", 10);

        List<ComputeJob> jobs = new ArrayList<>(gridSize);

        for (int i = 1; i <= gridSize; i++) {
            jobs.add(new ComputeJobAdapter() {

                @Override
                public Object execute() throws IgniteException {

                    //your implementation goes here

                    return null;
                }
            });
        }

        // These jobs will be executed with higher priority.
        return jobs;
    }

    @Override
    public Object reduce(List<ComputeJobResult> results) throws IgniteException {
        return null;
    }
}