View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.traffic;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.ChannelHandler.Sharable;
20  import io.netty.channel.Channel;
21  import io.netty.channel.ChannelHandlerContext;
22  import io.netty.channel.ChannelPromise;
23  import io.netty.util.concurrent.EventExecutor;
24  import io.netty.util.internal.PlatformDependent;
25  
26  import java.util.ArrayDeque;
27  import java.util.concurrent.ConcurrentMap;
28  import java.util.concurrent.ScheduledExecutorService;
29  import java.util.concurrent.TimeUnit;
30  import java.util.concurrent.atomic.AtomicLong;
31  
32  /**
33   * <p>This implementation of the {@link AbstractTrafficShapingHandler} is for global
34   * traffic shaping, that is to say a global limitation of the bandwidth, whatever
35   * the number of opened channels.</p>
36   * <p>Note the index used in {@code OutboundBuffer.setUserDefinedWritability(index, boolean)} is <b>2</b>.</p>
37   *
38   * <p>The general use should be as follow:</p>
39   * <ul>
40   * <li><p>Create your unique GlobalTrafficShapingHandler like:</p>
41   * <p><tt>GlobalTrafficShapingHandler myHandler = new GlobalTrafficShapingHandler(executor);</tt></p>
42   * <p>The executor could be the underlying IO worker pool</p>
43   * <p><tt>pipeline.addLast(myHandler);</tt></p>
44   *
45   * <p><b>Note that this handler has a Pipeline Coverage of "all" which means only one such handler must be created
46   * and shared among all channels as the counter must be shared among all channels.</b></p>
47   *
48   * <p>Other arguments can be passed like write or read limitation (in bytes/s where 0 means no limitation)
49   * or the check interval (in millisecond) that represents the delay between two computations of the
50   * bandwidth and so the call back of the doAccounting method (0 means no accounting at all).</p>
51   *
52   * <p>A value of 0 means no accounting for checkInterval. If you need traffic shaping but no such accounting,
53   * it is recommended to set a positive value, even if it is high since the precision of the
54   * Traffic Shaping depends on the period where the traffic is computed. The highest the interval,
55   * the less precise the traffic shaping will be. It is suggested as higher value something close
56   * to 5 or 10 minutes.</p>
57   *
58   * <p>maxTimeToWait, by default set to 15s, allows to specify an upper bound of time shaping.</p>
59   * </li>
60   * <li>In your handler, you should consider to use the {@code channel.isWritable()} and
61   * {@code channelWritabilityChanged(ctx)} to handle writability, or through
62   * {@code future.addListener(new GenericFutureListener())} on the future returned by
63   * {@code ctx.write()}.</li>
64   * <li><p>You shall also consider to have object size in read or write operations relatively adapted to
65   * the bandwidth you required: for instance having 10 MB objects for 10KB/s will lead to burst effect,
66   * while having 100 KB objects for 1 MB/s should be smoothly handle by this TrafficShaping handler.</p></li>
67   * <li><p>Some configuration methods will be taken as best effort, meaning
68   * that all already scheduled traffics will not be
69   * changed, but only applied to new traffics.</p>
70   * So the expected usage of those methods are to be used not too often,
71   * accordingly to the traffic shaping configuration.</li>
72   * </ul>
73   *
74   * Be sure to call {@link #release()} once this handler is not needed anymore to release all internal resources.
75   * This will not shutdown the {@link EventExecutor} as it may be shared, so you need to do this by your own.
76   */
77  @Sharable
78  public class GlobalTrafficShapingHandler extends AbstractTrafficShapingHandler {
79      /**
80       * All queues per channel
81       */
82      private final ConcurrentMap<Integer, PerChannel> channelQueues = PlatformDependent.newConcurrentHashMap();
83  
84      /**
85       * Global queues size
86       */
87      private final AtomicLong queuesSize = new AtomicLong();
88  
89      /**
90       * Max size in the list before proposing to stop writing new objects from next handlers
91       * for all channel (global)
92       */
93      long maxGlobalWriteSize = DEFAULT_MAX_SIZE * 100; // default 400MB
94  
95      private static final class PerChannel {
96          ArrayDeque<ToSend> messagesQueue;
97          long queueSize;
98          long lastWriteTimestamp;
99          long lastReadTimestamp;
100     }
101 
102     /**
103      * Create the global TrafficCounter.
104      */
105     void createGlobalTrafficCounter(ScheduledExecutorService executor) {
106         if (executor == null) {
107             throw new NullPointerException("executor");
108         }
109         TrafficCounter tc = new TrafficCounter(this, executor, "GlobalTC", checkInterval);
110         setTrafficCounter(tc);
111         tc.start();
112     }
113 
114     /**
115      * Create a new instance.
116      *
117      * @param executor
118      *            the {@link ScheduledExecutorService} to use for the {@link TrafficCounter}.
119      * @param writeLimit
120      *            0 or a limit in bytes/s
121      * @param readLimit
122      *            0 or a limit in bytes/s
123      * @param checkInterval
124      *            The delay between two computations of performances for
125      *            channels or 0 if no stats are to be computed.
126      * @param maxTime
127      *            The maximum delay to wait in case of traffic excess.
128      */
129     public GlobalTrafficShapingHandler(ScheduledExecutorService executor, long writeLimit, long readLimit,
130             long checkInterval, long maxTime) {
131         super(writeLimit, readLimit, checkInterval, maxTime);
132         createGlobalTrafficCounter(executor);
133     }
134 
135     /**
136      * Create a new instance using
137      * default max time as delay allowed value of 15000 ms.
138      *
139      * @param executor
140      *          the {@link ScheduledExecutorService} to use for the {@link TrafficCounter}.
141      * @param writeLimit
142      *          0 or a limit in bytes/s
143      * @param readLimit
144      *          0 or a limit in bytes/s
145      * @param checkInterval
146      *          The delay between two computations of performances for
147      *            channels or 0 if no stats are to be computed.
148      */
149     public GlobalTrafficShapingHandler(ScheduledExecutorService executor, long writeLimit,
150             long readLimit, long checkInterval) {
151         super(writeLimit, readLimit, checkInterval);
152         createGlobalTrafficCounter(executor);
153     }
154 
155     /**
156      * Create a new instance using default Check Interval value of 1000 ms and
157      * default max time as delay allowed value of 15000 ms.
158      *
159      * @param executor
160      *          the {@link ScheduledExecutorService} to use for the {@link TrafficCounter}.
161      * @param writeLimit
162      *          0 or a limit in bytes/s
163      * @param readLimit
164      *          0 or a limit in bytes/s
165      */
166     public GlobalTrafficShapingHandler(ScheduledExecutorService executor, long writeLimit,
167             long readLimit) {
168         super(writeLimit, readLimit);
169         createGlobalTrafficCounter(executor);
170     }
171 
172     /**
173      * Create a new instance using
174      * default max time as delay allowed value of 15000 ms and no limit.
175      *
176      * @param executor
177      *          the {@link ScheduledExecutorService} to use for the {@link TrafficCounter}.
178      * @param checkInterval
179      *          The delay between two computations of performances for
180      *            channels or 0 if no stats are to be computed.
181      */
182     public GlobalTrafficShapingHandler(ScheduledExecutorService executor, long checkInterval) {
183         super(checkInterval);
184         createGlobalTrafficCounter(executor);
185     }
186 
187     /**
188      * Create a new instance using default Check Interval value of 1000 ms and
189      * default max time as delay allowed value of 15000 ms and no limit.
190      *
191      * @param executor
192      *          the {@link ScheduledExecutorService} to use for the {@link TrafficCounter}.
193      */
194     public GlobalTrafficShapingHandler(EventExecutor executor) {
195         createGlobalTrafficCounter(executor);
196     }
197 
198     /**
199      * @return the maxGlobalWriteSize default value being 400 MB.
200      */
201     public long getMaxGlobalWriteSize() {
202         return maxGlobalWriteSize;
203     }
204 
205     /**
206      * Note the change will be taken as best effort, meaning
207      * that all already scheduled traffics will not be
208      * changed, but only applied to new traffics.<br>
209      * So the expected usage of this method is to be used not too often,
210      * accordingly to the traffic shaping configuration.
211      *
212      * @param maxGlobalWriteSize the maximum Global Write Size allowed in the buffer
213      *            globally for all channels before write suspended is set,
214      *            default value being 400 MB.
215      */
216     public void setMaxGlobalWriteSize(long maxGlobalWriteSize) {
217         this.maxGlobalWriteSize = maxGlobalWriteSize;
218     }
219 
220     /**
221      * @return the global size of the buffers for all queues.
222      */
223     public long queuesSize() {
224         return queuesSize.get();
225     }
226 
227     /**
228      * Release all internal resources of this instance.
229      */
230     public final void release() {
231         trafficCounter.stop();
232     }
233 
234     private PerChannel getOrSetPerChannel(ChannelHandlerContext ctx) {
235         // ensure creation is limited to one thread per channel
236         Channel channel = ctx.channel();
237         Integer key = channel.hashCode();
238         PerChannel perChannel = channelQueues.get(key);
239         if (perChannel == null) {
240             perChannel = new PerChannel();
241             perChannel.messagesQueue = new ArrayDeque<ToSend>();
242             perChannel.queueSize = 0L;
243             perChannel.lastReadTimestamp = TrafficCounter.milliSecondFromNano();
244             perChannel.lastWriteTimestamp = perChannel.lastReadTimestamp;
245             channelQueues.put(key, perChannel);
246         }
247         return perChannel;
248     }
249 
250     @Override
251     public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
252         getOrSetPerChannel(ctx);
253         super.handlerAdded(ctx);
254     }
255 
256     @Override
257     public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
258         Channel channel = ctx.channel();
259         Integer key = channel.hashCode();
260         PerChannel perChannel = channelQueues.remove(key);
261         if (perChannel != null) {
262             // write operations need synchronization
263             synchronized (perChannel) {
264                 if (channel.isActive()) {
265                     for (ToSend toSend : perChannel.messagesQueue) {
266                         long size = calculateSize(toSend.toSend);
267                         trafficCounter.bytesRealWriteFlowControl(size);
268                         perChannel.queueSize -= size;
269                         queuesSize.addAndGet(-size);
270                         ctx.write(toSend.toSend, toSend.promise);
271                     }
272                 } else {
273                     queuesSize.addAndGet(-perChannel.queueSize);
274                     for (ToSend toSend : perChannel.messagesQueue) {
275                         if (toSend.toSend instanceof ByteBuf) {
276                             ((ByteBuf) toSend.toSend).release();
277                         }
278                     }
279                 }
280                 perChannel.messagesQueue.clear();
281             }
282         }
283         releaseWriteSuspended(ctx);
284         releaseReadSuspended(ctx);
285         super.handlerRemoved(ctx);
286     }
287 
288     @Override
289     long checkWaitReadTime(final ChannelHandlerContext ctx, long wait, final long now) {
290         Integer key = ctx.channel().hashCode();
291         PerChannel perChannel = channelQueues.get(key);
292         if (perChannel != null) {
293             if (wait > maxTime && now + wait - perChannel.lastReadTimestamp > maxTime) {
294                 wait = maxTime;
295             }
296         }
297         return wait;
298     }
299 
300     @Override
301     void informReadOperation(final ChannelHandlerContext ctx, final long now) {
302         Integer key = ctx.channel().hashCode();
303         PerChannel perChannel = channelQueues.get(key);
304         if (perChannel != null) {
305             perChannel.lastReadTimestamp = now;
306         }
307     }
308 
309     private static final class ToSend {
310         final long relativeTimeAction;
311         final Object toSend;
312         final long size;
313         final ChannelPromise promise;
314 
315         private ToSend(final long delay, final Object toSend, final long size, final ChannelPromise promise) {
316             relativeTimeAction = delay;
317             this.toSend = toSend;
318             this.size = size;
319             this.promise = promise;
320         }
321     }
322 
323     @Override
324     void submitWrite(final ChannelHandlerContext ctx, final Object msg,
325             final long size, final long writedelay, final long now,
326             final ChannelPromise promise) {
327         Channel channel = ctx.channel();
328         Integer key = channel.hashCode();
329         PerChannel perChannel = channelQueues.get(key);
330         if (perChannel == null) {
331             // in case write occurs before handlerAdded is raized for this handler
332             // imply a synchronized only if needed
333             perChannel = getOrSetPerChannel(ctx);
334         }
335         final ToSend newToSend;
336         long delay = writedelay;
337         boolean globalSizeExceeded = false;
338         // write operations need synchronization
339         synchronized (perChannel) {
340             if (writedelay == 0 && perChannel.messagesQueue.isEmpty()) {
341                 trafficCounter.bytesRealWriteFlowControl(size);
342                 ctx.write(msg, promise);
343                 perChannel.lastWriteTimestamp = now;
344                 return;
345             }
346             if (delay > maxTime && now + delay - perChannel.lastWriteTimestamp > maxTime) {
347                 delay = maxTime;
348             }
349             newToSend = new ToSend(delay + now, msg, size, promise);
350             perChannel.messagesQueue.addLast(newToSend);
351             perChannel.queueSize += size;
352             queuesSize.addAndGet(size);
353             checkWriteSuspend(ctx, delay, perChannel.queueSize);
354             if (queuesSize.get() > maxGlobalWriteSize) {
355                 globalSizeExceeded = true;
356             }
357         }
358         if (globalSizeExceeded) {
359             setUserDefinedWritability(ctx, false);
360         }
361         final long futureNow = newToSend.relativeTimeAction;
362         final PerChannel forSchedule = perChannel;
363         ctx.executor().schedule(new Runnable() {
364             @Override
365             public void run() {
366                 sendAllValid(ctx, forSchedule, futureNow);
367             }
368         }, delay, TimeUnit.MILLISECONDS);
369     }
370 
371     private void sendAllValid(final ChannelHandlerContext ctx, final PerChannel perChannel, final long now) {
372         // write operations need synchronization
373         synchronized (perChannel) {
374             ToSend newToSend = perChannel.messagesQueue.pollFirst();
375             for (; newToSend != null; newToSend = perChannel.messagesQueue.pollFirst()) {
376                 if (newToSend.relativeTimeAction <= now) {
377                     long size = newToSend.size;
378                     trafficCounter.bytesRealWriteFlowControl(size);
379                     perChannel.queueSize -= size;
380                     queuesSize.addAndGet(-size);
381                     ctx.write(newToSend.toSend, newToSend.promise);
382                     perChannel.lastWriteTimestamp = now;
383                 } else {
384                     perChannel.messagesQueue.addFirst(newToSend);
385                     break;
386                 }
387             }
388             if (perChannel.messagesQueue.isEmpty()) {
389                 releaseWriteSuspended(ctx);
390             }
391         }
392         ctx.flush();
393     }
394 }