All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gridgain.grid.spi.collision.fifoqueue.GridFifoQueueCollisionSpi Maven / Gradle / Ivy

Go to download

Java-based middleware for in-memory processing of big data in a distributed environment.

There is a newer version: 6.2.1-p1
Show newest version
/* 
 Copyright (C) GridGain Systems. All Rights Reserved.
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

/*  _________        _____ __________________        _____
 *  __  ____/___________(_)______  /__  ____/______ ____(_)_______
 *  _  / __  __  ___/__  / _  __  / _  / __  _  __ `/__  / __  __ \
 *  / /_/ /  _  /    _  /  / /_/ /  / /_/ /  / /_/ / _  /  _  / / /
 *  \____/   /_/     /_/   \_,__/   \____/   \__,_/  /_/   /_/ /_/
 */

package org.gridgain.grid.spi.collision.fifoqueue;

import org.gridgain.grid.logger.*;
import org.gridgain.grid.resources.*;
import org.gridgain.grid.spi.*;
import org.gridgain.grid.spi.collision.*;
import org.gridgain.grid.util.typedef.internal.*;

import java.util.*;

/**
 * This class provides implementation for Collision SPI based on FIFO queue. Jobs are ordered
 * as they arrived and only {@link #getParallelJobsNumber()} number of jobs is allowed to
 * execute in parallel. Other jobs will be buffered in the passive queue.
 * 

Configuration

*

Mandatory

* This SPI has no mandatory configuration parameters. *

Optional

* This SPI has following optional configuration parameters: *
    *
  • * Number of jobs that can execute in parallel (see {@link #setParallelJobsNumber(int)}). * This number should usually be set to the number of threads in the execution thread pool. *
  • *
*

Java Example

* {@code GridFifoQueueCollisionSpi} can be configured as follows: *
 * GridFifoQueueCollisionSpi colSpi = new GridFifoQueueCollisionSpi();
 *
 * // Execute all jobs sequentially by setting parallel job number to 1.
 * colSpi.setParallelJobsNumber(1);
 *
 * GridConfiguration cfg = new GridConfiguration();
 *
 * // Override default collision SPI.
 * cfg.setCollisionSpi(colSpi);
 *
 * // Starts grid.
 * G.start(cfg);
 * 
*

Spring Example

* {@code GridFifoQueueCollisionSpi} can be configured from Spring XML configuration file: *
 * <bean id="grid.custom.cfg" class="org.gridgain.grid.GridConfiguration" singleton="true">
 *       ...
 *       <property name="collisionSpi">
 *           <bean class="org.gridgain.grid.spi.collision.fifoqueue.GridFifoQueueCollisionSpi">
 *               <property name="parallelJobsNumber" value="1"/>
 *           </bean>
 *       </property>
 *       ...
 * </bean>
 * 
*/ @GridSpiInfo( author = /*@java.spi.author*/"GridGain Systems", url = /*@java.spi.url*/"www.gridgain.com", email = /*@java.spi.email*/"[email protected]", version = /*@java.spi.version*/"x.x") @GridSpiMultipleInstancesSupport(true) public class GridFifoQueueCollisionSpi extends GridSpiAdapter implements GridCollisionSpi, GridFifoQueueCollisionSpiMBean { /** * Default number of parallel jobs allowed (value is {@code 95} which is * slightly less same as default value of threads in the execution thread pool * to allow some extra threads for system processing). */ public static final int DFLT_PARALLEL_JOBS_NUM = 95; /** * Default waiting jobs number. If number of waiting jobs exceeds this number, * jobs will be rejected. Default value is {@link Integer#MAX_VALUE}. */ public static final int DFLT_WAIT_JOBS_NUM = Integer.MAX_VALUE; /** Number of jobs that can be executed in parallel. */ private volatile int parallelJobsNum = DFLT_PARALLEL_JOBS_NUM; /** Wait jobs number. */ private volatile int waitJobsNum = DFLT_WAIT_JOBS_NUM; /** Grid logger. */ @GridLoggerResource private GridLogger log; /** Number of jobs that were active last time. */ private volatile int runningCnt; /** Number of jobs that were waiting for execution last time. */ private volatile int waitingCnt; /** Number of jobs that are held. */ private volatile int heldCnt; /** {@inheritDoc} */ @Override public int getParallelJobsNumber() { return parallelJobsNum; } /** {@inheritDoc} */ @GridSpiConfiguration(optional = true) @Override public void setParallelJobsNumber(int parallelJobsNum) { A.ensure(parallelJobsNum > 0, "parallelJobsNum > 0"); this.parallelJobsNum = parallelJobsNum; } /** {@inheritDoc} */ @Override public int getWaitingJobsNumber() { return waitJobsNum; } /** {@inheritDoc} */ @GridSpiConfiguration(optional = true) @Override public void setWaitingJobsNumber(int waitJobsNum) { A.ensure(waitJobsNum >= 0, "waitingJobsNum >= 0"); this.waitJobsNum = waitJobsNum; } /** {@inheritDoc} */ @Override public int getCurrentWaitJobsNumber() { return waitingCnt; } /** {@inheritDoc} */ @Override public int getCurrentActiveJobsNumber() { return runningCnt + heldCnt; } /** {@inheritDoc} */ @Override public int getCurrentRunningJobsNumber() { return runningCnt; } /** {@inheritDoc} */ @Override public int getCurrentHeldJobsNumber() { return heldCnt; } /** {@inheritDoc} */ @Override public void spiStart(String gridName) throws GridSpiException { assertParameter(parallelJobsNum > 0, "parallelJobsNum > 0"); assertParameter(waitJobsNum >= 0, "waitingJobsNum >= 0"); // Start SPI start stopwatch. startStopwatch(); // Ack parameters. if (log.isDebugEnabled()) log.debug(configInfo("parallelJobsNum", parallelJobsNum)); registerMBean(gridName, this, GridFifoQueueCollisionSpiMBean.class); // Ack start. if (log.isDebugEnabled()) log.debug(startInfo()); } /** {@inheritDoc} */ @Override public void spiStop() throws GridSpiException { unregisterMBean(); // Ack ok stop. if (log.isDebugEnabled()) log.debug(stopInfo()); } /** {@inheritDoc} */ @Override public void setExternalCollisionListener(GridCollisionExternalListener lsnr) { // No-op. } /** {@inheritDoc} */ @Override public void onCollision(GridCollisionContext ctx) { assert ctx != null; Collection activeJobs = ctx.activeJobs(); Collection waitJobs = ctx.waitingJobs(); // Save initial sizes to limit iteration. int activeSize = activeJobs.size(); int waitSize = waitJobs.size(); waitingCnt = waitSize; runningCnt = activeSize; heldCnt = ctx.heldJobs().size(); int parallelJobsNum0 = parallelJobsNum; Iterator it = null; if (activeSize < parallelJobsNum0) { it = waitJobs.iterator(); while (it.hasNext()) { GridCollisionJobContext waitCtx = it.next(); waitCtx.activate(); if (--waitSize == 0) // No more waiting jobs to process (to limit iterations). return; // Take actual size, since it might have been changed. if (activeJobs.size() >= parallelJobsNum0) // Max active jobs threshold reached. break; } } int waitJobsNum0 = waitJobsNum; // Take actual size, since it might have been changed. if (waitJobs.size() > waitJobsNum0) { if (it == null) it = waitJobs.iterator(); while (it.hasNext()) { GridCollisionJobContext waitCtx = it.next(); waitCtx.cancel(); if (--waitSize == 0) // No more waiting jobs to process (to limit iterations). return; // Take actual size, since it might have been changed. if (waitJobs.size() <= waitJobsNum0) // No need to reject more jobs. return; } } } /** {@inheritDoc} */ @Override public String toString() { return S.toString(GridFifoQueueCollisionSpi.class, this); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy