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