FileDocCategorySizeDatePackage
ThreadRatioRule.javaAPI DocGlassfish v2 API11874Fri May 04 22:37:02 BST 2007com.sun.enterprise.web.ara.rules

ThreadRatioRule

public class ThreadRatioRule extends Object implements com.sun.enterprise.web.connector.grizzly.Rule
Based on the application context-root, configure the ReadTask Pipeline. Based on the thread-ratio defined in domain.xml, an application can have privileged Pipeline, configured to use specific percentage of the maximum number of threads. This Rule instanciate two types of Pipeline privilegedPipeline is will be used to execute privileged applications. victimsPipeline is will be used to execute others application that aren't included within the privileged tokens. An application is marked privileged if the set of Rule applied to the application requests is matched.
author
Jeanfrancois Arcand

Fields Summary
protected static final String
RESERVE
protected static final String
CEILING
protected static final String
ALLOCATION_MODE
protected static final String
RULE_TOKENS
protected static final String
QUERY_STRING
protected static final String
PATH_STRING
protected com.sun.enterprise.web.connector.grizzly.ReadTask
readTask
The ReadTask attached to this Rule
protected static final ConcurrentHashMap
pipelines
The Pipeline configured based on the threadRatio. This Pipeline is only used by privileged application.
protected static final ConcurrentHashMap
privilegedTokens
The list of privileged token used to decide if a request can be serviced by the privileged Pipeline.
protected static double
leftRatio
The thread ratio used when an application isn't listed as a privileged application.
protected static String
allocationPolicy
The allocation mode used: celling or Reserve. With Ceiling policy, the strategy is to wait until all apps queus are showing some slack. With Reserve policiy, if 100% reservation is made by other apps, cancel the request processing.
Constructors Summary
public ThreadRatioRule()
Creates a new ThreadRationRule.

        
   
     
        try{
            if ( System.getProperty(RULE_TOKENS) != null){
                StringTokenizer privList = 
                        new StringTokenizer(System.getProperty(RULE_TOKENS),",");
                StringTokenizer privElement;
                String tokens;
                double countRatio = 0;
                double tokenValue;
                while (privList.hasMoreElements()){
                    privElement = new StringTokenizer(privList.nextToken()); 
                    
                    while (privElement.hasMoreElements()){
                        tokens = privElement.nextToken();
                        int index = tokens.indexOf("|");
                        tokenValue = Double.valueOf(tokens.substring(index+1));
                        privilegedTokens.put
                                (tokens.substring(0, index),tokenValue);
                        countRatio += tokenValue;
                    }
                }
                if ( countRatio > 1 ) {
                    SelectorThread.logger().log(Level.WARNING,
                     "Thread ratio too high. The total must be lower or equal to 1");
                }  else {
                    leftRatio = 1 - countRatio;
                }     
            }
        } catch (Exception ex){
             SelectorThread.logger()
                .log(Level.WARNING,"Unable to parse thread ratio", ex);
        } 
        
        if ( System.getProperty(ALLOCATION_MODE) != null){
            allocationPolicy = System.getProperty(ALLOCATION_MODE);   
            if ( !allocationPolicy.equals(RESERVE) && 
                    !allocationPolicy.equals(CEILING) ){
                SelectorThread.logger()
                    .log(Level.WARNING,"Invalid allocation policy");
                allocationPolicy = RESERVE;
            }
        }      
    
    
Methods Summary
protected com.sun.enterprise.web.connector.grizzly.PipelineapplyRule(java.lang.String token)
Apply the thread ratio.

  
        Pipeline p = readTask.getPipeline();
        int maxThreads = p.getMaxThreads();
        
        Double threadRatio = privilegedTokens.get(token);
        if (threadRatio == null) {
            threadRatio = (leftRatio == 0? 0.5:leftRatio);
        }
        
        int privilegedCount  = (threadRatio==1 ? maxThreads : 
            (int) (maxThreads * threadRatio) + 1);
               
        return newPipeline(privilegedCount,p);
    
public voidattach(com.sun.enterprise.web.connector.grizzly.ReadTask o)
Attach a ReadTask to this rule.

        this.readTask = o;
    
public com.sun.enterprise.web.connector.grizzly.ReadTaskattachement()
Return the current attachement.

        return readTask;
    
public java.lang.Integercall()
Invoke the rule. Based on the result of the ContextRootAlgorithm, configure the ReadTask Pipeline.

        boolean noCache = false;
        if ( leftRatio == 0 ) {
            if ( allocationPolicy.equals(RESERVE) )
                return IsolationRulesExecutor.RULE_BLOCKED;
            else if ( allocationPolicy.equals(CEILING) ) {
                
                // If true, then we need to wait for free space. If false, then
                // we can go ahead and let the task execute with its default
                // pipeline
                if ( isPipelineInUse() )
                    return IsolationRulesExecutor.RULE_DELAY;   
                else
                    noCache = true;
            }
        } 
             
        String token = getContextRoot();
        
        // Lazy instanciation
        Pipeline pipeline = pipelines.get(token);
        if ( pipeline == null ){
            pipeline = applyRule(token);
            pipelines.put(token,pipeline);
        }
                
        readTask.setPipeline(pipeline);
        if (!noCache)
            return IsolationRulesExecutor.RULE_OK;
        else
            return IsolationRulesExecutor.RULE_OK_NOCACHE;
    
public voidcancel()
Cancel execution of this rule.

        readTask = null;
    
protected java.lang.StringgetContextRoot()
Get the context-root from the ByteBuffer

        // (1) Get the token the Algorithm has processed for us.
        ByteBuffer byteBuffer = readTask.getByteBuffer();
        byte[] chars = new byte[byteBuffer.limit() - byteBuffer.position()];
               
        byteBuffer.get(chars);
        
        String token = new String(chars);
  
        int index = token.indexOf(PATH_STRING);
        if ( index != -1){
            token = token.substring(0,index);
        }
               
        // Remove query string.
        index = token.indexOf(QUERY_STRING);
        if ( index != -1){
            token = token.substring(0,index);
        }
        
        boolean slash = token.endsWith(PATH_STRING);
        if ( slash ){
            token = token.substring(0,token.length() -1);
        }  
        return token;
    
public intgetExecutionTime()
Return the time in second before this rule will be executed.

        return -1; // now
    
protected booleanisPipelineInUse()
Check to see if the privileged pipeline are in-use right now.

        Collection<Pipeline> collection = pipelines.values();
        for (Pipeline pipeline: collection){
            if (pipeline.size() > 0) {
                return true;
            }
        }
        return false;
    
protected com.sun.enterprise.web.connector.grizzly.PipelinenewPipeline(int threadCount, com.sun.enterprise.web.connector.grizzly.Pipeline p)
Creates a new Pipeline

        // Run the Task on the SelectorThread
        if ( threadCount == 0){
            return null;
        }
        Pipeline pipeline = new LinkedListPipeline();
        pipeline.setMinThreads(1);
        pipeline.setMaxThreads(threadCount);
        pipeline.setName(p.getName());
        pipeline.setQueueSizeInBytes(
                readTask.getSelectorThread().getQueueSizeInBytes());
        pipeline.initPipeline();   
        pipeline.startPipeline();
        return pipeline;
    
public voidsetExecutionTime(int time)
Set the interval in seconds to wait before executing this rule.

        ; 
    
public voidsetFuture(java.util.concurrent.Future future)
Set the Future associated with this execution of this rule.

        ;