Top Description Inners Fields Constructors Methods
org.apache.catalina.util

public Class TimeBucketCounter

extends Object
Class Inheritance
Imports
java.util.concurrent.ConcurrentHashMap, .ExecutionException, .ScheduledExecutorService, .ScheduledFuture, .TimeUnit, java.util.concurrent.atomic.AtomicInteger, org.apache.juli.logging.Log, .LogFactory, org.apache.tomcat.util.res.StringManager

This class maintains a thread safe hash map that has timestamp-based buckets followed by a string for a key, and a counter for a value. each time the increment() method is called it adds the key if it does not exist, increments its value and returns it. a maintenance thread cleans up keys that are prefixed by previous timestamp buckets.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
private class

Field Summary

Modifier and TypeField and Description
private final ScheduledExecutorService
private static final Log
private ScheduledFuture<?>
maintenanceFuture

The future allowing control of the background processor.

private final ConcurrentHashMap<String, AtomicInteger>
map

Map to hold the buckets

private ScheduledFuture<?>
private final int
numBits

Milliseconds bucket size as a Power of 2 for bit shift math, e.g. 16 for 65_536ms which is about 1:05 minute

private final double
ratio

Ratio of actual duration to config duration

private final long
private static final StringManager

Constructor Summary

AccessConstructor and Description
public
TimeBucketCounter(int
duration in seconds, e.g. for 1 minute pass 60
bucketDuration
,
ScheduledExecutorService
the executor service which will be used to run the maintenance
executorService
)

Creates a new TimeBucketCounter with the specified lifetime.

Method Summary

Modifier and TypeMethod and Description
public void
destroy()

Sets isRunning to false to terminate the maintenance thread.

public int

Returns:

the actual bucket duration in milliseconds
getActualDuration
()

The actual duration may differ from the configured duration because it is set to the next power of 2 value in order to perform very fast bit shift arithmetic.

public final int

Returns:

The current bucket prefix.
getCurrentBucketPrefix
()

Calculates the current time bucket prefix by shifting bits for fast division, e.g. shift 16 bits is the same as dividing by 65,536 which is about 1:05m.

public long

Returns:

the number of milliseconds until the next bucket
getMillisUntilNextBucket
()

When we want to test a full bucket duration we need to sleep until the next bucket starts.

public int
public double

Returns:

the ratio, e.g. 1.092 if the actual duration is 65_536 for the configured duration of 60_000
getRatio
()

Returns the ratio between the configured duration param and the actual duration which will be set to the next power of 2.

public final int

Returns:

the count within the current time bucket
increment
(String
an identifier for which we want to maintain count, e.g. IP Address
identifier
)

Increments the counter for the passed identifier in the current time bucket and returns the new value.

pack-priv static int
nextPowerOf2(int value)

Returns the next power of 2 given a value, e.g. 256 for 250, or 1024, for 1000.

pack-priv static double
ratioToPowerOf2(int value)

Returns the ratio to the next power of 2 so that we can adjust the value.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

executorServiceback to summary
private final ScheduledExecutorService executorService
logback to summary
private static final Log log
maintenanceFutureback to summary
private ScheduledFuture<?> maintenanceFuture

The future allowing control of the background processor.

mapback to summary
private final ConcurrentHashMap<String, AtomicInteger> map

Map to hold the buckets

monitorFutureback to summary
private ScheduledFuture<?> monitorFuture
numBitsback to summary
private final int numBits

Milliseconds bucket size as a Power of 2 for bit shift math, e.g. 16 for 65_536ms which is about 1:05 minute

ratioback to summary
private final double ratio

Ratio of actual duration to config duration

sleeptimeback to summary
private final long sleeptime
smback to summary
private static final StringManager sm

Constructor Detail

TimeBucketCounterback to summary
public TimeBucketCounter(int bucketDuration, ScheduledExecutorService executorService)

Creates a new TimeBucketCounter with the specified lifetime.

Parameters
bucketDuration:int

duration in seconds, e.g. for 1 minute pass 60

executorService:ScheduledExecutorService

the executor service which will be used to run the maintenance

Method Detail

destroyback to summary
public void destroy()

Sets isRunning to false to terminate the maintenance thread.

getActualDurationback to summary
public int getActualDuration()

The actual duration may differ from the configured duration because it is set to the next power of 2 value in order to perform very fast bit shift arithmetic.

Returns:int

the actual bucket duration in milliseconds

getCurrentBucketPrefixback to summary
public final int getCurrentBucketPrefix()

Calculates the current time bucket prefix by shifting bits for fast division, e.g. shift 16 bits is the same as dividing by 65,536 which is about 1:05m.

Returns:int

The current bucket prefix.

getMillisUntilNextBucketback to summary
public long getMillisUntilNextBucket()

When we want to test a full bucket duration we need to sleep until the next bucket starts.

Returns:long

the number of milliseconds until the next bucket

getNumBitsback to summary
public int getNumBits()
getRatioback to summary
public double getRatio()

Returns the ratio between the configured duration param and the actual duration which will be set to the next power of 2. We then multiply the configured requests param by the same ratio in order to compensate for the added time, if any.

Returns:double

the ratio, e.g. 1.092 if the actual duration is 65_536 for the configured duration of 60_000

incrementback to summary
public final int increment(String identifier)

Increments the counter for the passed identifier in the current time bucket and returns the new value.

Parameters
identifier:String

an identifier for which we want to maintain count, e.g. IP Address

Returns:int

the count within the current time bucket

nextPowerOf2back to summary
pack-priv static int nextPowerOf2(int value)

Returns the next power of 2 given a value, e.g. 256 for 250, or 1024, for 1000.

ratioToPowerOf2back to summary
pack-priv static double ratioToPowerOf2(int value)

Returns the ratio to the next power of 2 so that we can adjust the value.

org.apache.catalina.util back to summary

private Class TimeBucketCounter.Maintenance

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Maintenanceback to summary
private Maintenance()

Method Detail

runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override
org.apache.catalina.util back to summary

private Class TimeBucketCounter.MaintenanceMonitor

extends Object
implements Runnable
Class Inheritance
  • java.lang.Object
  • org.apache.catalina.util.TimeBucketCounter.MaintenanceMonitor
All Implemented Interfaces
java.lang.Runnable

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

MaintenanceMonitorback to summary
private MaintenanceMonitor()

Method Detail

runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override