Package org.jboss.netty.handler.traffic
Class GlobalChannelTrafficShapingHandler
- java.lang.Object
-
- org.jboss.netty.channel.SimpleChannelHandler
-
- org.jboss.netty.handler.traffic.AbstractTrafficShapingHandler
-
- org.jboss.netty.handler.traffic.GlobalChannelTrafficShapingHandler
-
- All Implemented Interfaces:
ChannelDownstreamHandler,ChannelHandler,ChannelUpstreamHandler,ExternalResourceReleasable
@Sharable public class GlobalChannelTrafficShapingHandler extends AbstractTrafficShapingHandler
This implementation of theAbstractTrafficShapingHandleris for global and per channel traffic shaping, that is to say a global limitation of the bandwidth, whatever the number of opened channels and a per channel limitation of the bandwidth.
This version shall not be in the same pipeline than other TrafficShapingHandler.
The general use should be as follow:
- Create your unique GlobalChannelTrafficShapingHandler like:
GlobalChannelTrafficShapingHandler myHandler = new GlobalChannelTrafficShapingHandler(executor);
The executor could be the underlying IO worker pool
pipeline.addLast(myHandler);
Note that this handler has a Pipeline Coverage of "all" which means only one such handler must be created and shared among all channels as the counter must be shared among all channels.
Other arguments can be passed like write or read limitation (in bytes/s where 0 means no limitation) or the check interval (in millisecond) that represents the delay between two computations of the bandwidth and so the call back of the doAccounting method (0 means no accounting at all).
Note that as this is a fusion of both Global and Channel Traffic Shaping, limits are in 2 sets, respectively Global and Channel.
A value of 0 means no accounting for checkInterval. If you need traffic shaping but no such accounting, it is recommended to set a positive value, even if it is high since the precision of the Traffic Shaping depends on the period where the traffic is computed. The highest the interval, the less precise the traffic shaping will be. It is suggested as higher value something close to 5 or 10 minutes.
maxTimeToWait, by default set to 15s, allows to specify an upper bound of time shaping.
- In your handler, you should consider to use the
channel.isWritable()andchannelWritabilityChanged(ctx)to handle writability, or throughfuture.addListener(new GenericFutureListener())on the future returned byctx.write(). - You shall also consider to have object size in read or write operations relatively adapted to
the bandwidth you required: for instance having 10 MB objects for 10KB/s will lead to burst effect,
while having 100 KB objects for 1 MB/s should be smoothly handle by this TrafficShaping handler.
- Some configuration methods will be taken as best effort, meaning
that all already scheduled traffics will not be
changed, but only applied to new traffics.
So the expected usage of those methods are to be used not too often, accordingly to the traffic shaping configuration.
Be sure to callrelease()once this handler is not needed anymore to release all internal resources. This will not shutdown theTimeras it may be shared, so you need to do this by your own.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classGlobalChannelTrafficShapingHandler.PerChannelprivate static classGlobalChannelTrafficShapingHandler.ToSend-
Nested classes/interfaces inherited from class org.jboss.netty.handler.traffic.AbstractTrafficShapingHandler
AbstractTrafficShapingHandler.ReadWriteStatus, AbstractTrafficShapingHandler.ReopenReadTimerTask, AbstractTrafficShapingHandler.SimpleObjectSizeEstimator
-
Nested classes/interfaces inherited from interface org.jboss.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private floataccelerationFactor(package private) java.util.concurrent.ConcurrentMap<java.lang.Integer,GlobalChannelTrafficShapingHandler.PerChannel>channelQueuesAll queues per channelprivate java.util.concurrent.atomic.AtomicLongcumulativeReadBytesMaximum cumulative read bytes for one channel among all (as long as channels stay the same)private java.util.concurrent.atomic.AtomicLongcumulativeWrittenBytesMaximum cumulative writing bytes for one channel among all (as long as channels stay the same)private static floatDEFAULT_ACCELERATIONprivate static floatDEFAULT_DEVIATIONprivate static floatDEFAULT_SLOWDOWNprivate static InternalLoggerloggerprivate static floatMAX_DEVIATIONprivate floatmaxDeviation(package private) longmaxGlobalWriteSizeMax size in the list before proposing to stop writing new objects from next handlers for all channel (global)private java.util.concurrent.atomic.AtomicLongqueuesSizeGlobal queues sizeprivate longreadChannelLimitLimit in B/s to apply to readprivate booleanreadDeviationActiveprivate floatslowDownFactorprivate longwriteChannelLimitLimit in B/s to apply to writeprivate booleanwriteDeviationActive-
Fields inherited from class org.jboss.netty.handler.traffic.AbstractTrafficShapingHandler
CHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX, checkInterval, DEFAULT_CHECK_INTERVAL, DEFAULT_MAX_SIZE, DEFAULT_MAX_TIME, GLOBAL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX, GLOBALCHANNEL_DEFAULT_USER_DEFINED_WRITABILITY_INDEX, index, maxTime, maxWriteDelay, maxWriteSize, MINIMAL_WAIT, release, timeout, timer, trafficCounter
-
-
Constructor Summary
Constructors Constructor Description GlobalChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer)GlobalChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long checkInterval)GlobalChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit, long writeChannelLimit, long readChannelLimit)GlobalChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit, long writeChannelLimit, long readChannelLimit, long checkInterval)GlobalChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit, long writeChannelLimit, long readChannelLimit, long checkInterval, long maxTime)GlobalChannelTrafficShapingHandler(Timer timer)Create a new instance.GlobalChannelTrafficShapingHandler(Timer timer, long checkInterval)Create a new instance.GlobalChannelTrafficShapingHandler(Timer timer, long writeGlobalLimit, long readGlobalLimit, long writeChannelLimit, long readChannelLimit)Create a new instance.GlobalChannelTrafficShapingHandler(Timer timer, long writeGlobalLimit, long readGlobalLimit, long writeChannelLimit, long readChannelLimit, long checkInterval)Create a new instance.GlobalChannelTrafficShapingHandler(Timer timer, long writeGlobalLimit, long readGlobalLimit, long writeChannelLimit, long readChannelLimit, long checkInterval, long maxTime)Create a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description floataccelerationFactor()voidchannelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)Invoked when aChannelwas closed and all its related resources were released.voidchannelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)Invoked when aChannelis open, bound to a local address, and connected to a remote address.java.util.Collection<TrafficCounter>channelTrafficCounters()To allow for instance doAccounting to use the TrafficCounter per channel.protected longcheckWaitReadTime(ChannelHandlerContext ctx, long wait, long now)Method overridden in GTSH to take into account specific timer for the channel.private longcomputeBalancedWait(float maxLocal, float maxGlobal, long wait)private voidcomputeDeviationCumulativeBytes()voidconfigureChannel(long newWriteLimit, long newReadLimit)(package private) voidcreateGlobalTrafficCounter(Timer timer)Create the global TrafficCounter.protected voiddoAccounting(TrafficCounter counter)Called each time the accounting is computed from the TrafficCounters.longgetMaxGlobalWriteSize()private GlobalChannelTrafficShapingHandler.PerChannelgetOrSetPerChannel(ChannelHandlerContext ctx)longgetReadChannelLimit()longgetWriteChannelLimit()protected voidinformReadOperation(ChannelHandlerContext ctx, long now)Method overridden in GTSH to take into account specific timer for the channel.floatmaxDeviation()protected longmaximumCumulativeReadBytes()protected longmaximumCumulativeWrittenBytes()voidmessageReceived(ChannelHandlerContext ctx, MessageEvent evt)Invoked when a message object (e.g:ChannelBuffer) was received from a remote peer.longqueuesSize()voidrelease()Release all internal resources of this instance.private voidsendAllValid(ChannelHandlerContext ctx, GlobalChannelTrafficShapingHandler.PerChannel perChannel, long now)voidsetMaxDeviation(float maxDeviation, float slowDownFactor, float accelerationFactor)voidsetMaxGlobalWriteSize(long maxGlobalWriteSize)Note the change will be taken as best effort, meaning that all already scheduled traffics will not be changed, but only applied to new traffics.
So the expected usage of this method is to be used not too often, accordingly to the traffic shaping configuration.voidsetReadChannelLimit(long readLimit)voidsetWriteChannelLimit(long writeLimit)floatslowDownFactor()protected voidsubmitWrite(ChannelHandlerContext ctx, MessageEvent evt, long size, long writedelay, long now)java.lang.StringtoString()(package private) intuserDefinedWritabilityIndex()voidwriteRequested(ChannelHandlerContext ctx, MessageEvent evt)Invoked whenChannel.write(Object)is called.-
Methods inherited from class org.jboss.netty.handler.traffic.AbstractTrafficShapingHandler
calculateSize, checkAttachment, checkWriteSuspend, configure, configure, configure, getCheckInterval, getMaxTimeWait, getMaxWriteDelay, getMaxWriteSize, getReadLimit, getTrafficCounter, getWriteLimit, internalSubmitWrite, releaseExternalResources, releaseReadSuspended, releaseWriteSuspended, setCheckInterval, setMaxTimeWait, setMaxWriteDelay, setMaxWriteSize, setReadLimit, setTrafficCounter, setWritable, setWriteLimit, submitWrite
-
Methods inherited from class org.jboss.netty.channel.SimpleChannelHandler
bindRequested, channelBound, channelDisconnected, channelInterestChanged, channelOpen, channelUnbound, childChannelClosed, childChannelOpen, closeRequested, connectRequested, disconnectRequested, exceptionCaught, handleDownstream, handleUpstream, setInterestOpsRequested, unbindRequested, writeComplete
-
-
-
-
Field Detail
-
logger
private static final InternalLogger logger
-
channelQueues
final java.util.concurrent.ConcurrentMap<java.lang.Integer,GlobalChannelTrafficShapingHandler.PerChannel> channelQueues
All queues per channel
-
queuesSize
private final java.util.concurrent.atomic.AtomicLong queuesSize
Global queues size
-
cumulativeWrittenBytes
private final java.util.concurrent.atomic.AtomicLong cumulativeWrittenBytes
Maximum cumulative writing bytes for one channel among all (as long as channels stay the same)
-
cumulativeReadBytes
private final java.util.concurrent.atomic.AtomicLong cumulativeReadBytes
Maximum cumulative read bytes for one channel among all (as long as channels stay the same)
-
maxGlobalWriteSize
long maxGlobalWriteSize
Max size in the list before proposing to stop writing new objects from next handlers for all channel (global)
-
writeChannelLimit
private volatile long writeChannelLimit
Limit in B/s to apply to write
-
readChannelLimit
private volatile long readChannelLimit
Limit in B/s to apply to read
-
DEFAULT_DEVIATION
private static final float DEFAULT_DEVIATION
- See Also:
- Constant Field Values
-
MAX_DEVIATION
private static final float MAX_DEVIATION
- See Also:
- Constant Field Values
-
DEFAULT_SLOWDOWN
private static final float DEFAULT_SLOWDOWN
- See Also:
- Constant Field Values
-
DEFAULT_ACCELERATION
private static final float DEFAULT_ACCELERATION
- See Also:
- Constant Field Values
-
maxDeviation
private volatile float maxDeviation
-
accelerationFactor
private volatile float accelerationFactor
-
slowDownFactor
private volatile float slowDownFactor
-
readDeviationActive
private volatile boolean readDeviationActive
-
writeDeviationActive
private volatile boolean writeDeviationActive
-
-
Constructor Detail
-
GlobalChannelTrafficShapingHandler
public GlobalChannelTrafficShapingHandler(Timer timer, long writeGlobalLimit, long readGlobalLimit, long writeChannelLimit, long readChannelLimit, long checkInterval, long maxTime)
Create a new instance.- Parameters:
timer- theTimerto use for theTrafficCounter.writeGlobalLimit- 0 or a limit in bytes/sreadGlobalLimit- 0 or a limit in bytes/swriteChannelLimit- 0 or a limit in bytes/sreadChannelLimit- 0 or a limit in bytes/scheckInterval- The delay between two computations of performances for channels or 0 if no stats are to be computed.maxTime- The maximum delay to wait in case of traffic excess.
-
GlobalChannelTrafficShapingHandler
public GlobalChannelTrafficShapingHandler(Timer timer, long writeGlobalLimit, long readGlobalLimit, long writeChannelLimit, long readChannelLimit, long checkInterval)
Create a new instance.- Parameters:
timer- theTimerto use for theTrafficCounter.writeGlobalLimit- 0 or a limit in bytes/sreadGlobalLimit- 0 or a limit in bytes/swriteChannelLimit- 0 or a limit in bytes/sreadChannelLimit- 0 or a limit in bytes/scheckInterval- The delay between two computations of performances for channels or 0 if no stats are to be computed.
-
GlobalChannelTrafficShapingHandler
public GlobalChannelTrafficShapingHandler(Timer timer, long writeGlobalLimit, long readGlobalLimit, long writeChannelLimit, long readChannelLimit)
Create a new instance.- Parameters:
timer- theTimerto use for theTrafficCounter.writeGlobalLimit- 0 or a limit in bytes/sreadGlobalLimit- 0 or a limit in bytes/swriteChannelLimit- 0 or a limit in bytes/sreadChannelLimit- 0 or a limit in bytes/s
-
GlobalChannelTrafficShapingHandler
public GlobalChannelTrafficShapingHandler(Timer timer, long checkInterval)
Create a new instance.- Parameters:
timer- theTimerto use for theTrafficCounter.checkInterval- The delay between two computations of performances for channels or 0 if no stats are to be computed.
-
GlobalChannelTrafficShapingHandler
public GlobalChannelTrafficShapingHandler(Timer timer)
Create a new instance.- Parameters:
timer- theTimerto use for theTrafficCounter.
-
GlobalChannelTrafficShapingHandler
public GlobalChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit, long writeChannelLimit, long readChannelLimit, long checkInterval, long maxTime)
- Parameters:
objectSizeEstimator- ObjectSizeEstimator to usetimer- theTimerto use for theTrafficCounter.writeLimit- write Global Limit 0 or a limit in bytes/sreadLimit- read Global Limit 0 or a limit in bytes/swriteChannelLimit- 0 or a limit in bytes/sreadChannelLimit- 0 or a limit in bytes/scheckInterval- The delay between two computations of performances for channels or 0 if no stats are to be computed.maxTime- The maximum delay to wait in case of traffic excess.
-
GlobalChannelTrafficShapingHandler
public GlobalChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit, long writeChannelLimit, long readChannelLimit, long checkInterval)
- Parameters:
objectSizeEstimator- ObjectSizeEstimator to usetimer- theTimerto use for theTrafficCounter.writeLimit- write Global Limit 0 or a limit in bytes/sreadLimit- read Global Limit 0 or a limit in bytes/swriteChannelLimit- 0 or a limit in bytes/sreadChannelLimit- 0 or a limit in bytes/scheckInterval- The delay between two computations of performances for channels or 0 if no stats are to be computed.
-
GlobalChannelTrafficShapingHandler
public GlobalChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long writeLimit, long readLimit, long writeChannelLimit, long readChannelLimit)
- Parameters:
objectSizeEstimator- ObjectSizeEstimator to usetimer- theTimerto use for theTrafficCounter.writeLimit- write Global Limit 0 or a limit in bytes/sreadLimit- read Global Limit 0 or a limit in bytes/swriteChannelLimit- 0 or a limit in bytes/sreadChannelLimit- 0 or a limit in bytes/s
-
GlobalChannelTrafficShapingHandler
public GlobalChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer, long checkInterval)
- Parameters:
objectSizeEstimator- ObjectSizeEstimator to usetimer- theTimerto use for theTrafficCounter.checkInterval- The delay between two computations of performances for channels or 0 if no stats are to be computed.
-
GlobalChannelTrafficShapingHandler
public GlobalChannelTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, Timer timer)
- Parameters:
objectSizeEstimator- ObjectSizeEstimator to usetimer- theTimerto use for theTrafficCounter.
-
-
Method Detail
-
createGlobalTrafficCounter
void createGlobalTrafficCounter(Timer timer)
Create the global TrafficCounter.
-
userDefinedWritabilityIndex
int userDefinedWritabilityIndex()
- Overrides:
userDefinedWritabilityIndexin classAbstractTrafficShapingHandler- Returns:
- the index to be used by the TrafficShapingHandler to manage the user defined writability. For Channel TSH it is defined as 1, for Global TSH it is defined as 2, for GlobalChannel TSH it is defined as 3.
-
maxDeviation
public float maxDeviation()
- Returns:
- the current max deviation.
-
accelerationFactor
public float accelerationFactor()
- Returns:
- the current acceleration factor.
-
slowDownFactor
public float slowDownFactor()
- Returns:
- the current slow down factor.
-
setMaxDeviation
public void setMaxDeviation(float maxDeviation, float slowDownFactor, float accelerationFactor)- Parameters:
maxDeviation- the maximum deviation to allow during computation of average, default deviation being 0.1, so +/-10% of the desired bandwidth. Maximum being 0.4.slowDownFactor- the factor set as +x% to the too fast client (minimal value being 0, meaning no slow down factor), default being 40% (0.4).accelerationFactor- the factor set as -x% to the too slow client (maximal value being 0, meaning no acceleration factor), default being -10% (-0.1).
-
computeDeviationCumulativeBytes
private void computeDeviationCumulativeBytes()
-
doAccounting
protected void doAccounting(TrafficCounter counter)
Description copied from class:AbstractTrafficShapingHandlerCalled each time the accounting is computed from the TrafficCounters. This method could be used for instance to implement almost real time accounting.- Overrides:
doAccountingin classAbstractTrafficShapingHandler- Parameters:
counter- the TrafficCounter that computes its performance
-
computeBalancedWait
private long computeBalancedWait(float maxLocal, float maxGlobal, long wait)
-
getMaxGlobalWriteSize
public long getMaxGlobalWriteSize()
- Returns:
- the maxGlobalWriteSize
-
setMaxGlobalWriteSize
public void setMaxGlobalWriteSize(long maxGlobalWriteSize)
Note the change will be taken as best effort, meaning that all already scheduled traffics will not be changed, but only applied to new traffics.
So the expected usage of this method is to be used not too often, accordingly to the traffic shaping configuration.- Parameters:
maxGlobalWriteSize- the maximum Global Write Size allowed in the buffer globally for all channels before write suspended is set.
-
queuesSize
public long queuesSize()
- Returns:
- the global size of the buffers for all queues.
-
configureChannel
public void configureChannel(long newWriteLimit, long newReadLimit)- Parameters:
newWriteLimit- Channel write limitnewReadLimit- Channel read limit
-
getWriteChannelLimit
public long getWriteChannelLimit()
- Returns:
- Channel write limit.
-
setWriteChannelLimit
public void setWriteChannelLimit(long writeLimit)
- Parameters:
writeLimit- Channel write limit
-
getReadChannelLimit
public long getReadChannelLimit()
- Returns:
- Channel read limit.
-
setReadChannelLimit
public void setReadChannelLimit(long readLimit)
- Parameters:
readLimit- Channel read limit
-
release
public final void release()
Release all internal resources of this instance.
-
getOrSetPerChannel
private GlobalChannelTrafficShapingHandler.PerChannel getOrSetPerChannel(ChannelHandlerContext ctx)
-
channelConnected
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelHandlerInvoked when aChannelis open, bound to a local address, and connected to a remote address.- Overrides:
channelConnectedin classAbstractTrafficShapingHandler- Throws:
java.lang.Exception
-
channelClosed
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelHandlerInvoked when aChannelwas closed and all its related resources were released.- Overrides:
channelClosedin classSimpleChannelHandler- Throws:
java.lang.Exception
-
messageReceived
public void messageReceived(ChannelHandlerContext ctx, MessageEvent evt) throws java.lang.Exception
Description copied from class:SimpleChannelHandlerInvoked when a message object (e.g:ChannelBuffer) was received from a remote peer.- Overrides:
messageReceivedin classAbstractTrafficShapingHandler- Throws:
java.lang.Exception
-
checkWaitReadTime
protected long checkWaitReadTime(ChannelHandlerContext ctx, long wait, long now)
Description copied from class:AbstractTrafficShapingHandlerMethod overridden in GTSH to take into account specific timer for the channel.- Overrides:
checkWaitReadTimein classAbstractTrafficShapingHandlerwait- the wait delay computed in msnow- the relative now time in ms- Returns:
- the wait to use according to the context.
-
informReadOperation
protected void informReadOperation(ChannelHandlerContext ctx, long now)
Description copied from class:AbstractTrafficShapingHandlerMethod overridden in GTSH to take into account specific timer for the channel.- Overrides:
informReadOperationin classAbstractTrafficShapingHandlernow- the relative now time in ms
-
maximumCumulativeWrittenBytes
protected long maximumCumulativeWrittenBytes()
-
maximumCumulativeReadBytes
protected long maximumCumulativeReadBytes()
-
channelTrafficCounters
public java.util.Collection<TrafficCounter> channelTrafficCounters()
To allow for instance doAccounting to use the TrafficCounter per channel.- Returns:
- the list of TrafficCounters that exists at the time of the call.
-
writeRequested
public void writeRequested(ChannelHandlerContext ctx, MessageEvent evt) throws java.lang.Exception
Description copied from class:SimpleChannelHandlerInvoked whenChannel.write(Object)is called.- Overrides:
writeRequestedin classAbstractTrafficShapingHandler- Throws:
java.lang.Exception
-
submitWrite
protected void submitWrite(ChannelHandlerContext ctx, MessageEvent evt, long size, long writedelay, long now) throws java.lang.Exception
- Specified by:
submitWritein classAbstractTrafficShapingHandler- Throws:
java.lang.Exception
-
sendAllValid
private void sendAllValid(ChannelHandlerContext ctx, GlobalChannelTrafficShapingHandler.PerChannel perChannel, long now) throws java.lang.Exception
- Throws:
java.lang.Exception
-
toString
public java.lang.String toString()
- Overrides:
toStringin classAbstractTrafficShapingHandler
-
-