Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  *
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 
 package com.sun.enterprise.web.connector.grizzly;
 
This class is a placeholde for gathering statistic from a Pipeline

Author(s):
Jean-Francois Arcand
 
 public class PipelineStatistic {

    
The port of which we gather statistics
 
     private int port = -1;
    
    
    
Is this object started?
  
     private boolean started = false;
    
    
    
Maximum pending connection before refusing requests.
 
     private int maxQueueSizeInBytes = -1;
    
    
    
The total number of connections queued during the lifetime of the pipeline
 
     private int totalCount;
    
    
    
The largest number of connections that have been in the pipeline simultaneouly
 
     private int peakCount;
    
    
    
Total number of pipeline overflows
 
     private int overflowCount;
    
    
    
The Thread Pool used when gathering count statistic.
 
     private ScheduledThreadPoolExecutor countAverageExecutor;
    
    
    
Average number of connection queued in that last 1 minute
    private Statistic lastMinuteStat = new Statistic(1 * 60);
    
    
    
Average number of connection queued in that last 5 minute
    private Statistic lastFiveMinuteStat = new Statistic(5 * 60);
    
    
    
Average number of connection queued in that last 15 minute
    private Statistic lastFifteenMinuteStat = new Statistic(15 * 60);
    
    
    
Placeholder to gather statistics.
            new ConcurrentHashMap<Integer,Statistic>();

    
The pipelines whose stats are being collected
    private Pipeline processorPipeline;
 
    
    
Future instance in case we need to stop this object.
    private Future futures[] = new Future[3];


    
Total number of connections that have been accepted.
    private int totalAcceptCount;

    
Map of open pipeline connections
            new ConcurrentHashMap<SelectableChannel,Long>();
    // -------------------------------------------------------------------//
    
    
    
Constructor

Parameters:
port Port number for which pipeline (connection) stats will be gathered
    public PipelineStatistic(int port) {
        this. = port;
        
            new GrizzlyThreadFactory("GrizzlyPipelineStat",
                port,.));        
    }
    
    
    
Start gathering statistics.
    public void start(){    
        if (  ) return;
        
                .getSeconds(), .);
                .getSeconds(), .);
                .getSeconds(), .);    
        
        
         = true;
    }
    
    
    
Stop gathering statistics.
    public void stop(){
        if ( ! ) return;
        
        for (Future future){
            future.cancel(true);
        }
               
        .clear();
         = false;
    }    
    
    
    
Gather Pipeline statistic.
    public boolean gather(int queueLength){
        if ( queueLength == ){
            ++;
            return false;
        }
       
        if ( queueLength > 0 )
            ++;
        
        // Track peak of this Pipeline
        if (queueLength > ) {
             = queueLength;
        } 
        return true;
    }
 
    
    
Total number of pipeline overflow
    public int getCountOverflows(){
        return ;
    }
     
     
    
Gets the largest number of connections that were in the queue simultaneously.

Returns:
Largest number of connections that were in the queue simultaneously
    
    public int getPeakQueued(){
       return ;
    }    
    

    
Gets the maximum size of the connection queue

Returns:
Maximum size of the connection queue
    
    public int getMaxQueued() {
        return ;
    }


    
Gets the total number of connections that have been accepted.

Returns:
Total number of connections that have been accepted.
    public int getCountTotalConnections() {
        return ;
    }

    
    
Set the maximum pending connection this Pipeline can handle.
    public void setQueueSizeInBytes(int maxQueueSizeInBytesCount){
        this. = maxQueueSizeInBytesCount;
    }
    
    
    
Get the maximum pending connection this Pipeline can handle.
    public int getQueueSizeInBytes(){
        return ;
    }
        

    
Gets the total number of connections that have been queued. A given connection may be queued multiple times, so counttotalqueued may be greater than or equal to counttotalconnections.

Returns:
Total number of connections that have been queued
        
    public int getCountTotalQueued() {
        return ;
    }
    

    
Gets the number of connections currently in the queue

Returns:
Number of connections currently in the queue
    
    public int getCountQueued() {
        int size = 0;
        if ( != null) {
            size += .size();
        }
        return size;
    }

   
    
Gets the total number of ticks that connections have spent in the queue. A tick is a system-dependent unit of time.

Returns:
Total number of ticks that connections have spent in the queue
    public int getTicksTotalQueued() {
        return -1; // Not supported
    }

    
    
Gets the average number of connections queued in the last 1 minute

Returns:
Average number of connections queued in the last 1 minute
    
    public int getCountQueued1MinuteAverage() {
        return getCountAverage(1);
    }


    
Gets the average number of connections queued in the last 5 minutes

Returns:
Average number of connections queued in the last 5 minutes
    
    public int getCountQueued5MinuteAverage() {
        return getCountAverage(5);
    }


    
Gets the average number of connections queued in the last 15 minutes

Returns:
Average number of connections queued in the last 15 minutes
    
    public int getCountQueued15MinuteAverage() {
        return getCountAverage(15);
    }
    // -------------------------------------------------------------------//
    // Package protected methods
    public void incrementTotalAcceptCount() {
        ++;
    }
    void setProcessorPipeline(Pipeline processorPipeline) {
        this. = processorPipeline;
    }
    // -------------------------------------------------------------------//
    // Private methods

    
Gets the average number of connection queued in the last minutes minutes.

Parameters:
minutes The number of minutes for which the average number of connections queued is requested
Returns:
Average number of connections queued
    private int getCountAverage(int minutes){
        Statistic stat = .get((minutes * 60));
        return (stat == null ? 0 : stat.average());
    }

    
Increase the number of open connections, which are being handled by the Pipeline

Parameters:
channel just open channel
Returns:
true, if channel was added, false if channel was already counted in the statistics
    public boolean incrementOpenConnectionsCount(SelectableChannel channel) {
        Long prevValue = 
                .putIfAbsent(channel, System.currentTimeMillis());
        return prevValue == null;
    }

    
Decrease the number of open connections, which are being handled by the Pipeline

Parameters:
channel just closed channel
Returns:
true, if channel was removed from statics of open channels, false if channel is not in the statistics (probably removed from statics earlier)
    public boolean decrementOpenConnectionsCount(SelectableChannel channel) {
        Long prevValue = .remove(channel);
        return prevValue != null;
    }

    
Get the current number of open channels, which are being handled by the pipeline

Returns:
the current number of open channels, which are being handled by the pipeline
    public int getOpenConnectionsCount() {
        return .size();
    }

    
Utility class to track average count.
    class Statistic implements Runnable{
                
        int lastCount = 0;
        int average = 0;
        int seconds;
             
        public Statistic(int seconds){
            this. = seconds;
        }
                               
        public void run() {
             =  - ;
             = ;
        }  
        
        public int average(){
            return ;
        }   
        
        
        public int getSeconds(){
            return ;
        }
    }
    
    
New to GrepCode? Check out our FAQ X