Coverage report

  %line %branch
org.apache.jcs.auxiliary.disk.jdbc.ShrinkerThread
88% 
98% 

 1  
 package org.apache.jcs.auxiliary.disk.jdbc;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.util.Collections;
 23  
 import java.util.HashSet;
 24  
 import java.util.Set;
 25  
 
 26  
 import org.apache.commons.logging.Log;
 27  
 import org.apache.commons.logging.LogFactory;
 28  
 
 29  
 /**
 30  
  * Calls delete expired on the disk caches. The shrinker is run by a clock daemon. The shrinker
 31  
  * calls delete on each region. It pauses between calls.
 32  
  * <p>
 33  
  * @author Aaron Smuts
 34  
  */
 35  
 public class ShrinkerThread
 36  
     implements Runnable
 37  
 {
 38  
     /** The logger. */
 39  56
     private final static Log log = LogFactory.getLog( ShrinkerThread.class );
 40  
 
 41  
     /** A set of JDBCDiskCache objects to call deleteExpired on. */
 42  28
     private Set shrinkSet = Collections.synchronizedSet( new HashSet() );
 43  
 
 44  
     /**
 45  
      * Default time period to use.
 46  
      */
 47  
     private static final long DEFAULT_PAUSE_BETWEEN_REGION_CALLS_MILLIS = 5000;
 48  
 
 49  
     /**
 50  
      * How long should we wait between calls to deleteExpired when we are iterating through the list
 51  
      * of regions. Delete can lock the table. We want to give clients a chance to get some work
 52  
      * done.
 53  
      */
 54  28
     private long pauseBetweenRegionCallsMillis = DEFAULT_PAUSE_BETWEEN_REGION_CALLS_MILLIS;
 55  
 
 56  
     /**
 57  
      * Does nothing special.
 58  
      * <p>
 59  
      * @param diskCache
 60  
      */
 61  
     protected ShrinkerThread()
 62  
     {
 63  28
         super();
 64  28
     }
 65  
 
 66  
     /**
 67  
      * Adds a JDBC disk cache to the set of disk cache to shrink.
 68  
      * <p>
 69  
      * @param diskCache
 70  
      */
 71  
     public void addDiskCacheToShrinkList( JDBCDiskCache diskCache )
 72  
     {
 73  
         // the set will prevent dupes.
 74  
         // we could also just add these to a hasmap by region name
 75  
         // but that might cause a problem if you wanted to use two different
 76  
         // jbdc disk caches for the same region.
 77  42
         shrinkSet.add( diskCache );
 78  42
     }
 79  
 
 80  
     /**
 81  
      * Calls deleteExpired on each item in the set. It pauses between each call.
 82  
      */
 83  
     public void run()
 84  
     {
 85  13
         if ( log.isInfoEnabled() )
 86  
         {
 87  13
             log.info( "Running JDBC disk cache shrinker.  Number of regions [" + shrinkSet.size() + "]" );
 88  
         }
 89  
 
 90  13
         Object[] caches = null;
 91  
 
 92  13
         synchronized ( shrinkSet )
 93  
         {
 94  13
             caches = this.shrinkSet.toArray();
 95  13
         }
 96  
 
 97  13
         if ( caches != null )
 98  
         {
 99  32
             for ( int i = 0; i < caches.length; i++ )
 100  
             {
 101  26
                 JDBCDiskCache cache = (JDBCDiskCache) caches[i];
 102  
 
 103  26
                 long start = System.currentTimeMillis();
 104  26
                 int deleted = cache.deleteExpired();
 105  26
                 long end = System.currentTimeMillis();
 106  
 
 107  26
                 if ( log.isInfoEnabled() )
 108  
                 {
 109  26
                     log.info( "Deleted [" + deleted + "] expired for region [" + cache.getCacheName() + "] for table ["
 110  
                         + cache.getTableName() + "] in " + ( end - start ) + " ms." );
 111  
                 }
 112  
 
 113  
                 // don't pause after the last call to delete expired.
 114  26
                 if ( i < caches.length - 1 )
 115  
                 {
 116  20
                     if ( log.isInfoEnabled() )
 117  
                     {
 118  20
                         log.info( "Pausing for [" + this.getPauseBetweenRegionCallsMillis()
 119  
                             + "] ms. before shinker the next region." );
 120  
                     }
 121  
 
 122  
                     try
 123  
                     {
 124  20
                         Thread.sleep( this.getPauseBetweenRegionCallsMillis() );
 125  
                     }
 126  0
                     catch ( InterruptedException e )
 127  
                     {
 128  0
                         log.warn( "Interrupted while waiting to delete expired for the enxt region." );
 129  13
                     }
 130  
                 }
 131  
             }
 132  
         }
 133  6
     }
 134  
 
 135  
     /**
 136  
      * How long should we wait between calls to deleteExpired when we are iterating through the list
 137  
      * of regions.
 138  
      * <p>
 139  
      * @param pauseBetweenRegionCallsMillis The pauseBetweenRegionCallsMillis to set.
 140  
      */
 141  
     public void setPauseBetweenRegionCallsMillis( long pauseBetweenRegionCallsMillis )
 142  
     {
 143  0
         this.pauseBetweenRegionCallsMillis = pauseBetweenRegionCallsMillis;
 144  0
     }
 145  
 
 146  
     /**
 147  
      * How long should we wait between calls to deleteExpired when we are iterating through the list
 148  
      * of regions.
 149  
      * <p>
 150  
      * @return Returns the pauseBetweenRegionCallsMillis.
 151  
      */
 152  
     public long getPauseBetweenRegionCallsMillis()
 153  
     {
 154  40
         return pauseBetweenRegionCallsMillis;
 155  
     }
 156  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.