Dashboard > GridGain User Guide > Table Of Contents > Developers Guide > Collision SPI > GridPriorityQueueCollisionSpi
GridPriorityQueueCollisionSpi
Added by user-1-ds, last edited by morpheus on Dec 15, 2009  (view change)
Labels: 
(None)


Click on Javadoc link to open Javadoc documentation.

Package

org.gridgain.grid.spi.collision.priorityqueue Javadoc

Description

GridPriorityQueueCollisionSpi Javadoc allows a certain number of jobs with highest priority to proceed without interruptions. All other jobs will be put on waiting list until their turn. Job priority is retrieved from job priority attribute. If no priority has been assigned to a job (job priority attribute was not found), then default priority of 0 is used.

Note that if parallelJobsNumber configuration parameter is not set, then this SPI will allow all concurrent jobs to proceed without interruptions. Make sure to set parallelJobNumber Javadoc parameter to enforce an upper limit for a maximum number of concurrent jobs that can proceed without interruptions. For example, to have only one job with highest priority execute at a time, you should set parallelJobsNumber parameter to 1.

GridTask Code Example

Here is an example of a grid tasks that uses priority collision SPI is configured. Note that priority collision resolution is absolutely transparent to the user and is simply a matter of proper grid configuration. Also, priority may be defined only for task (it can be defined within the task, not at a job level). All split jobs will be started with priority declared in their owner task.

This example demonstrates how urgent task may be declared with a higher priority value. Priority SPI guarantees (see its configuration in example below, where number of parallel jobs is set to 1) that all jobs from MyGridUrgentTask will most likely be activated first (one by one) and jobs from MyGridUsualTask with lowest priority will wait. Once higher priority jobs complete, lower priority jobs will be scheduled.

public class MyGridUrgentTask extends GridTaskSplitAdapter<Object, Object> {                              
    public static final int SPLIT_COUNT = 5;                                                            
                                                                                                        
    @GridTaskSessionResource                                                                            
    private GridTaskSession taskSes = null;                                                             
                                                                                                        
    @Override                                                                                           
    protected Collection<? extends GridJob> split(int gridSize, Object arg) throws GridException {
        ...
        // Set high task priority (note that attribute name is used by the SPI 
        // and should not be changed).                                                                              
        taskSes.setAttribute("grid.task.priority", 10);                                             
                                                                                                        
        Collection<GridJob> jobs = new ArrayList<GridJob>(SPLIT_COUNT);                                 
                                                                                                        
        for (int i = 1; i <= SPLIT_COUNT; i++) {                                                        
            jobs.add(new GridJobAdapter<Integer>(i) {                                                   
                ...                                                                                     
            });                                                                                         
        }                                                                                               
        ...                                                                                             
    }                                                                                                   
}

and

public class MyGridUsualTask extends GridTaskSplitAdapter<Object, Object> {
   public static final int SPLIT_COUNT = 20;

   @GridTaskSessionResource
   private GridTaskSession taskSes = null;

   @Override
   protected Collection<? extends GridJob> split(int gridSize, Object arg) throws GridException {
       ...
       // Set low task priority (note that attribute name is used by the SPI
       // and should not be changed).
       taskSes.setAttribute("grid.task.priority", 5);

       Collection<GridJob> jobs = new ArrayList<GridJob>(SPLIT_COUNT);

       for (int i = 1; i <= SPLIT_COUNT; i++) {
           jobs.add(new GridJobAdapter<Integer>(i) {
               ...
           });
       }
       ...
}

Configuration

The following configuration parameters can be used to configure GridPriorityQueueCollisionSpi

Setter Method Description Optional Default
setDefaultPriority(int) Javadoc Sets default priority used if job does not have job priority attribute set in the context. Yes 0 specified in GridPriorityQueueCollisionSpi.DFLT_PRIORITY Javadoc
setParallelJobsNumber(int) Javadoc Sets upper liimit for a number of jobs that will proceed without interruptions. Yes 95, specified in GridPriorityQueueCollisionSpi.DFLT_PARALLEL_JOBS_NUM Javadoc .
setPriorityAttributeKey(String) Javadoc This key will be used to look up job priorities from job context (GridJobContext.getAttribute(String) Javadoc method). Yes Value "grid.job.priority", specified in GridPriorityQueueCollisionSpi.DFLT_PRIORITY_ATTRIBUTE_KEY Javadoc

Examples

As any GridGain SPI, GridPriorityQueueCollisionSpi Javadoc can be configured either directly from code or from Spring configuration file. Here is an example of GridPriorityQueueCollisionSpi Javadoc configuration from code:

GridPriorityQueueCollisionSpi colSpi = new GridPriorityQueueCollisionSpi();

// Execute all jobs sequentially by setting parallel job number to 1.
colSpi.setParallelJobsNumber(5);

GridConfigurationAdapter cfg = new GridConfigurationAdapter();

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

// Start grid.
GridFactory.start(cfg);

or from Spring configuration file

<bean id="grid.custom.cfg" class="org.gridgain.grid.GridConfigurationAdapter" singleton="true">
        ...
        <property name="collisionSpi">
            <bean class="org.gridgain.grid.spi.collision.priorityqueue.GridPriorityQueueCollisionSpi">
                <property name="parallelJobsNumber" value="5"/>
            </bean>
        </property>
        ...
</bean>


For more information about using Spring framework for configuration click here.

Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.10 Build:#528 Nov 29, 2006) - Bug/feature request - Contact Administrators