Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/nio/SelectorManager.java @ 928:23a57aad34c0
remove isAsync()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 09 Oct 2016 18:54:32 -0600 |
parents | 54308d65265a |
children | b77d631b9e28 |
rev | line source |
---|---|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 // ------------------------------------------------------------------------ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 // All rights reserved. This program and the accompanying materials |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 // are made available under the terms of the Eclipse Public License v1.0 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 // and Apache License v2.0 which accompanies this distribution. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 // The Eclipse Public License is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 // http://www.eclipse.org/legal/epl-v10.html |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 // The Apache License v2.0 is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 // http://www.opensource.org/licenses/apache2.0.php |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 // You may elect to redistribute this code under either of these licenses. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 package org.eclipse.jetty.io.nio; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 import java.io.IOException; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 import java.nio.channels.CancelledKeyException; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 import java.nio.channels.Channel; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 import java.nio.channels.ClosedSelectorException; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 import java.nio.channels.SelectableChannel; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 import java.nio.channels.SelectionKey; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 import java.nio.channels.Selector; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 import java.nio.channels.ServerSocketChannel; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 import java.nio.channels.SocketChannel; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 import java.util.ArrayList; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 import java.util.List; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 import java.util.Set; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 import java.util.concurrent.ConcurrentHashMap; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 import java.util.concurrent.ConcurrentLinkedQueue; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 import java.util.concurrent.ConcurrentMap; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
36 import java.util.concurrent.CountDownLatch; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
37 import java.util.concurrent.TimeUnit; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
38 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
39 import org.eclipse.jetty.io.AsyncEndPoint; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
40 import org.eclipse.jetty.io.ConnectedEndPoint; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 import org.eclipse.jetty.io.Connection; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
42 import org.eclipse.jetty.io.EndPoint; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
43 import org.eclipse.jetty.util.TypeUtil; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
44 import org.eclipse.jetty.util.component.AbstractLifeCycle; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
45 import org.eclipse.jetty.util.component.AggregateLifeCycle; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
46 import org.eclipse.jetty.util.component.Dumpable; |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
47 import org.slf4j.Logger; |
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
48 import org.slf4j.LoggerFactory; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
49 import org.eclipse.jetty.util.thread.Timeout; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
50 import org.eclipse.jetty.util.thread.Timeout.Task; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
51 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
52 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
53 /* ------------------------------------------------------------ */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
54 /** |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
55 * The Selector Manager manages and number of SelectSets to allow |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
56 * NIO scheduling to scale to large numbers of connections. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
57 * <p> |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
58 */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
59 public abstract class SelectorManager extends AbstractLifeCycle implements Dumpable |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
60 { |
865 | 61 public static final Logger LOG=LoggerFactory.getLogger("org.eclipse.jetty.io.nio"); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
62 |
865 | 63 private static final int __MONITOR_PERIOD=Integer.getInteger("org.eclipse.jetty.io.nio.MONITOR_PERIOD",1000).intValue(); |
64 private static final int __MAX_SELECTS=Integer.getInteger("org.eclipse.jetty.io.nio.MAX_SELECTS",100000).intValue(); | |
65 private static final int __BUSY_PAUSE=Integer.getInteger("org.eclipse.jetty.io.nio.BUSY_PAUSE",50).intValue(); | |
66 private static final int __IDLE_TICK=Integer.getInteger("org.eclipse.jetty.io.nio.IDLE_TICK",400).intValue(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
67 |
865 | 68 private int _maxIdleTime; |
69 private long _lowResourcesConnections; | |
70 private SelectSet[] _selectSet; | |
71 private int _selectSets=1; | |
72 private volatile int _set=0; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
73 |
865 | 74 /* ------------------------------------------------------------ */ |
75 /** | |
76 * @param maxIdleTime The maximum period in milli seconds that a connection may be idle before it is closed. | |
77 * @see #setLowResourcesMaxIdleTime(long) | |
78 */ | |
79 public void setMaxIdleTime(long maxIdleTime) | |
80 { | |
81 _maxIdleTime=(int)maxIdleTime; | |
82 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
83 |
865 | 84 /* ------------------------------------------------------------ */ |
85 /** | |
86 * @param selectSets number of select sets to create | |
87 */ | |
88 public void setSelectSets(int selectSets) | |
89 { | |
90 long lrc = _lowResourcesConnections * _selectSets; | |
91 _selectSets=selectSets; | |
92 _lowResourcesConnections=lrc/_selectSets; | |
93 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
94 |
865 | 95 /* ------------------------------------------------------------ */ |
96 /** | |
97 * @return the max idle time | |
98 */ | |
99 public long getMaxIdleTime() | |
100 { | |
101 return _maxIdleTime; | |
102 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
103 |
865 | 104 /* ------------------------------------------------------------ */ |
105 /** | |
106 * @return the number of select sets in use | |
107 */ | |
108 public int getSelectSets() | |
109 { | |
110 return _selectSets; | |
111 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
112 |
865 | 113 /* ------------------------------------------------------------ */ |
114 /** | |
115 * @param i | |
116 * @return The select set | |
117 */ | |
118 public SelectSet getSelectSet(int i) | |
119 { | |
120 return _selectSet[i]; | |
121 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
122 |
865 | 123 /* ------------------------------------------------------------ */ |
124 /** Register a channel | |
125 * @param channel | |
126 * @param att Attached Object | |
127 */ | |
128 public void register(SocketChannel channel, Object att) | |
129 { | |
130 // The ++ increment here is not atomic, but it does not matter. | |
131 // so long as the value changes sometimes, then connections will | |
132 // be distributed over the available sets. | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
133 |
865 | 134 int s=_set++; |
135 if (s<0) | |
136 s=-s; | |
137 s=s%_selectSets; | |
138 SelectSet[] sets=_selectSet; | |
139 if (sets!=null) | |
140 { | |
141 SelectSet set=sets[s]; | |
142 set.addChange(channel,att); | |
143 set.wakeup(); | |
144 } | |
145 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
146 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
147 |
865 | 148 /* ------------------------------------------------------------ */ |
149 /** Register a channel | |
150 * @param channel | |
151 */ | |
152 public void register(SocketChannel channel) | |
153 { | |
154 // The ++ increment here is not atomic, but it does not matter. | |
155 // so long as the value changes sometimes, then connections will | |
156 // be distributed over the available sets. | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
157 |
865 | 158 int s=_set++; |
159 if (s<0) | |
160 s=-s; | |
161 s=s%_selectSets; | |
162 SelectSet[] sets=_selectSet; | |
163 if (sets!=null) | |
164 { | |
165 SelectSet set=sets[s]; | |
166 set.addChange(channel); | |
167 set.wakeup(); | |
168 } | |
169 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
170 |
865 | 171 /* ------------------------------------------------------------ */ |
172 /** Register a {@link ServerSocketChannel} | |
173 * @param acceptChannel | |
174 */ | |
175 public void register(ServerSocketChannel acceptChannel) | |
176 { | |
177 int s=_set++; | |
178 if (s<0) | |
179 s=-s; | |
180 s=s%_selectSets; | |
181 SelectSet set=_selectSet[s]; | |
182 set.addChange(acceptChannel); | |
183 set.wakeup(); | |
184 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
185 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
186 |
865 | 187 /* ------------------------------------------------------------ */ |
188 /** | |
189 * @return the lowResourcesConnections | |
190 */ | |
191 public long getLowResourcesConnections() | |
192 { | |
193 return _lowResourcesConnections*_selectSets; | |
194 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
195 |
865 | 196 /* ------------------------------------------------------------ */ |
197 /** | |
198 * Set the number of connections, which if exceeded places this manager in low resources state. | |
199 * This is not an exact measure as the connection count is averaged over the select sets. | |
200 * @param lowResourcesConnections the number of connections | |
201 * @see #setLowResourcesMaxIdleTime(long) | |
202 */ | |
203 public void setLowResourcesConnections(long lowResourcesConnections) | |
204 { | |
205 _lowResourcesConnections=(lowResourcesConnections+_selectSets-1)/_selectSets; | |
206 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
207 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
208 |
865 | 209 public abstract void execute(Runnable task); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
210 |
865 | 211 /* ------------------------------------------------------------ */ |
212 /* (non-Javadoc) | |
213 * @see org.eclipse.component.AbstractLifeCycle#doStart() | |
214 */ | |
215 @Override | |
216 protected void doStart() throws Exception | |
217 { | |
218 _selectSet = new SelectSet[_selectSets]; | |
219 for (int i=0;i<_selectSet.length;i++) | |
220 _selectSet[i]= new SelectSet(i); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
221 |
865 | 222 super.doStart(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
223 |
865 | 224 // start a thread to Select |
225 for (int i=0;i<getSelectSets();i++) | |
226 { | |
227 final int id=i; | |
228 execute(new Runnable() | |
229 { | |
230 public void run() | |
231 { | |
232 String name=Thread.currentThread().getName(); | |
233 try | |
234 { | |
235 SelectSet[] sets=_selectSet; | |
236 if (sets==null) | |
237 return; | |
238 SelectSet set=sets[id]; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
239 |
865 | 240 Thread.currentThread().setName(name+" Selector"+id); |
241 LOG.debug("Starting {} on {}",Thread.currentThread(),this); | |
242 while (isRunning()) | |
243 { | |
244 try | |
245 { | |
246 set.doSelect(); | |
247 } | |
248 catch(IOException e) | |
249 { | |
250 LOG.trace("",e); | |
251 } | |
252 catch(Exception e) | |
253 { | |
254 LOG.warn("",e); | |
255 } | |
256 } | |
257 } | |
258 finally | |
259 { | |
260 LOG.debug("Stopped {} on {}",Thread.currentThread(),this); | |
261 Thread.currentThread().setName(name); | |
262 } | |
263 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
264 |
865 | 265 }); |
266 } | |
267 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
268 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
269 |
865 | 270 /* ------------------------------------------------------------------------------- */ |
271 @Override | |
272 protected void doStop() throws Exception | |
273 { | |
274 SelectSet[] sets= _selectSet; | |
275 _selectSet=null; | |
276 if (sets!=null) | |
277 { | |
278 for (SelectSet set : sets) | |
279 { | |
280 if (set!=null) | |
281 set.stop(); | |
282 } | |
283 } | |
284 super.doStop(); | |
285 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
286 |
865 | 287 /* ------------------------------------------------------------ */ |
288 /** | |
289 * @param endpoint | |
290 */ | |
291 protected abstract void endPointClosed(SelectChannelEndPoint endpoint); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
292 |
865 | 293 /* ------------------------------------------------------------------------------- */ |
294 public abstract AsyncConnection newConnection(SocketChannel channel, AsyncEndPoint endpoint, Object attachment); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
295 |
865 | 296 /* ------------------------------------------------------------ */ |
297 /** | |
298 * Create a new end point | |
299 * @param channel | |
300 * @param selectSet | |
301 * @param sKey the selection key | |
302 * @return the new endpoint {@link SelectChannelEndPoint} | |
303 * @throws IOException | |
304 */ | |
305 protected abstract SelectChannelEndPoint newEndPoint(SocketChannel channel, SelectorManager.SelectSet selectSet, SelectionKey sKey) throws IOException; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
306 |
865 | 307 /* ------------------------------------------------------------------------------- */ |
308 protected void connectionFailed(SocketChannel channel,Throwable ex,Object attachment) | |
309 { | |
310 LOG.warn(ex+","+channel+","+attachment); | |
311 LOG.debug("",ex); | |
312 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
313 |
865 | 314 /* ------------------------------------------------------------ */ |
315 public String dump() | |
316 { | |
317 return AggregateLifeCycle.dump(this); | |
318 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
319 |
865 | 320 /* ------------------------------------------------------------ */ |
321 public void dump(Appendable out, String indent) throws IOException | |
322 { | |
323 AggregateLifeCycle.dumpObject(out,this); | |
324 AggregateLifeCycle.dump(out,indent,TypeUtil.asList(_selectSet)); | |
325 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
326 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
327 |
865 | 328 /* ------------------------------------------------------------------------------- */ |
329 /* ------------------------------------------------------------------------------- */ | |
330 /* ------------------------------------------------------------------------------- */ | |
331 public class SelectSet implements Dumpable | |
332 { | |
333 private final int _setID; | |
334 private final Timeout _timeout; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
335 |
865 | 336 private final ConcurrentLinkedQueue<Object> _changes = new ConcurrentLinkedQueue<Object>(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
337 |
865 | 338 private volatile Selector _selector; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
339 |
865 | 340 private volatile Thread _selecting; |
341 private int _busySelects; | |
342 private long _monitorNext; | |
343 private boolean _pausing; | |
344 private boolean _paused; | |
345 private volatile long _idleTick; | |
346 private ConcurrentMap<SelectChannelEndPoint,Object> _endPoints = new ConcurrentHashMap<SelectChannelEndPoint, Object>(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
347 |
865 | 348 /* ------------------------------------------------------------ */ |
349 SelectSet(int acceptorID) throws Exception | |
350 { | |
351 _setID=acceptorID; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
352 |
865 | 353 _idleTick = System.currentTimeMillis(); |
354 _timeout = new Timeout(this); | |
355 _timeout.setDuration(0L); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
356 |
865 | 357 // create a selector; |
358 _selector = Selector.open(); | |
359 _monitorNext=System.currentTimeMillis()+__MONITOR_PERIOD; | |
360 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
361 |
865 | 362 /* ------------------------------------------------------------ */ |
363 public void addChange(Object change) | |
364 { | |
365 _changes.add(change); | |
366 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
367 |
865 | 368 /* ------------------------------------------------------------ */ |
369 public void addChange(SelectableChannel channel, Object att) | |
370 { | |
371 if (att==null) | |
372 addChange(channel); | |
373 else if (att instanceof EndPoint) | |
374 addChange(att); | |
375 else | |
376 addChange(new ChannelAndAttachment(channel,att)); | |
377 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
378 |
865 | 379 /* ------------------------------------------------------------ */ |
380 /** | |
381 * Select and dispatch tasks found from changes and the selector. | |
382 * | |
383 * @throws IOException | |
384 */ | |
385 public void doSelect() throws IOException | |
386 { | |
387 try | |
388 { | |
389 _selecting=Thread.currentThread(); | |
390 final Selector selector=_selector; | |
391 // Stopped concurrently ? | |
392 if (selector == null) | |
393 return; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
394 |
865 | 395 // Make any key changes required |
396 Object change; | |
397 int changes=_changes.size(); | |
398 while (changes-->0 && (change=_changes.poll())!=null) | |
399 { | |
400 Channel ch=null; | |
401 SelectionKey key=null; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
402 |
865 | 403 try |
404 { | |
405 if (change instanceof EndPoint) | |
406 { | |
407 // Update the operations for a key. | |
408 SelectChannelEndPoint endpoint = (SelectChannelEndPoint)change; | |
409 ch=endpoint.getChannel(); | |
410 endpoint.doUpdateKey(); | |
411 } | |
412 else if (change instanceof ChannelAndAttachment) | |
413 { | |
414 // finish accepting/connecting this connection | |
415 final ChannelAndAttachment asc = (ChannelAndAttachment)change; | |
416 final SelectableChannel channel=asc._channel; | |
417 ch=channel; | |
418 final Object att = asc._attachment; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
419 |
865 | 420 if ((channel instanceof SocketChannel) && ((SocketChannel)channel).isConnected()) |
421 { | |
422 key = channel.register(selector,SelectionKey.OP_READ,att); | |
423 SelectChannelEndPoint endpoint = createEndPoint((SocketChannel)channel,key); | |
424 key.attach(endpoint); | |
425 endpoint.schedule(); | |
426 } | |
427 else if (channel.isOpen()) | |
428 { | |
429 key = channel.register(selector,SelectionKey.OP_CONNECT,att); | |
430 } | |
431 } | |
432 else if (change instanceof SocketChannel) | |
433 { | |
434 // Newly registered channel | |
435 final SocketChannel channel=(SocketChannel)change; | |
436 ch=channel; | |
437 key = channel.register(selector,SelectionKey.OP_READ,null); | |
438 SelectChannelEndPoint endpoint = createEndPoint(channel,key); | |
439 key.attach(endpoint); | |
440 endpoint.schedule(); | |
441 } | |
442 else if (change instanceof ChangeTask) | |
443 { | |
444 ((Runnable)change).run(); | |
445 } | |
446 else if (change instanceof Runnable) | |
447 { | |
448 execute((Runnable)change); | |
449 } | |
450 else | |
451 throw new IllegalArgumentException(change.toString()); | |
452 } | |
453 catch (CancelledKeyException e) | |
454 { | |
455 LOG.trace("",e); | |
456 } | |
457 catch (Throwable e) | |
458 { | |
459 if (isRunning()) | |
460 LOG.warn("",e); | |
461 else | |
462 LOG.debug("",e); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
463 |
865 | 464 try |
465 { | |
466 if (ch!=null) | |
467 ch.close(); | |
468 } | |
469 catch(IOException e2) | |
470 { | |
471 LOG.debug("",e2); | |
472 } | |
473 } | |
474 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
475 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
476 |
865 | 477 // Do and instant select to see if any connections can be handled. |
478 int selected=selector.selectNow(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
479 |
865 | 480 long now=System.currentTimeMillis(); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
481 |
865 | 482 // if no immediate things to do |
483 if (selected==0 && selector.selectedKeys().isEmpty()) | |
484 { | |
485 // If we are in pausing mode | |
486 if (_pausing) | |
487 { | |
488 try | |
489 { | |
490 Thread.sleep(__BUSY_PAUSE); // pause to reduce impact of busy loop | |
491 } | |
492 catch(InterruptedException e) | |
493 { | |
494 LOG.trace("",e); | |
495 } | |
496 now=System.currentTimeMillis(); | |
497 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
498 |
865 | 499 // workout how long to wait in select |
500 _timeout.setNow(now); | |
501 long to_next_timeout=_timeout.getTimeToNext(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
502 |
865 | 503 long wait = _changes.size()==0?__IDLE_TICK:0L; |
504 if (wait > 0 && to_next_timeout >= 0 && wait > to_next_timeout) | |
505 wait = to_next_timeout; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
506 |
865 | 507 // If we should wait with a select |
508 if (wait>0) | |
509 { | |
510 long before=now; | |
511 selector.select(wait); | |
512 now = System.currentTimeMillis(); | |
513 _timeout.setNow(now); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
514 |
865 | 515 // If we are monitoring for busy selector |
516 // and this select did not wait more than 1ms | |
517 if (__MONITOR_PERIOD>0 && now-before <=1) | |
518 { | |
519 // count this as a busy select and if there have been too many this monitor cycle | |
520 if (++_busySelects>__MAX_SELECTS) | |
521 { | |
522 // Start injecting pauses | |
523 _pausing=true; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
524 |
865 | 525 // if this is the first pause |
526 if (!_paused) | |
527 { | |
528 // Log and dump some status | |
529 _paused=true; | |
530 LOG.warn("Selector {} is too busy, pausing!",this); | |
531 } | |
532 } | |
533 } | |
534 } | |
535 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
536 |
865 | 537 // have we been destroyed while sleeping |
538 if (_selector==null || !selector.isOpen()) | |
539 return; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
540 |
865 | 541 // Look for things to do |
542 for (SelectionKey key: selector.selectedKeys()) | |
543 { | |
544 SocketChannel channel=null; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
545 |
865 | 546 try |
547 { | |
548 if (!key.isValid()) | |
549 { | |
550 key.cancel(); | |
551 SelectChannelEndPoint endpoint = (SelectChannelEndPoint)key.attachment(); | |
552 if (endpoint != null) | |
553 endpoint.doUpdateKey(); | |
554 continue; | |
555 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
556 |
865 | 557 Object att = key.attachment(); |
558 if (att instanceof SelectChannelEndPoint) | |
559 { | |
560 if (key.isReadable()||key.isWritable()) | |
561 ((SelectChannelEndPoint)att).schedule(); | |
562 } | |
563 else if (key.isConnectable()) | |
564 { | |
565 // Complete a connection of a registered channel | |
566 channel = (SocketChannel)key.channel(); | |
567 boolean connected=false; | |
568 try | |
569 { | |
570 connected=channel.finishConnect(); | |
571 } | |
572 catch(Exception e) | |
573 { | |
574 connectionFailed(channel,e,att); | |
575 } | |
576 finally | |
577 { | |
578 if (connected) | |
579 { | |
580 key.interestOps(SelectionKey.OP_READ); | |
581 SelectChannelEndPoint endpoint = createEndPoint(channel,key); | |
582 key.attach(endpoint); | |
583 endpoint.schedule(); | |
584 } | |
585 else | |
586 { | |
587 key.cancel(); | |
588 channel.close(); | |
589 } | |
590 } | |
591 } | |
592 else | |
593 { | |
594 // Wrap readable registered channel in an endpoint | |
595 channel = (SocketChannel)key.channel(); | |
596 SelectChannelEndPoint endpoint = createEndPoint(channel,key); | |
597 key.attach(endpoint); | |
598 if (key.isReadable()) | |
599 endpoint.schedule(); | |
600 } | |
601 key = null; | |
602 } | |
603 catch (CancelledKeyException e) | |
604 { | |
605 LOG.trace("",e); | |
606 } | |
607 catch (Exception e) | |
608 { | |
609 if (isRunning()) | |
610 LOG.warn("",e); | |
611 else | |
612 LOG.trace("",e); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
613 |
865 | 614 try |
615 { | |
616 if (channel!=null) | |
617 channel.close(); | |
618 } | |
619 catch(IOException e2) | |
620 { | |
621 LOG.debug("",e2); | |
622 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
623 |
865 | 624 if (key != null && !(key.channel() instanceof ServerSocketChannel) && key.isValid()) |
625 key.cancel(); | |
626 } | |
627 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
628 |
865 | 629 // Everything always handled |
630 selector.selectedKeys().clear(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
631 |
865 | 632 now=System.currentTimeMillis(); |
633 _timeout.setNow(now); | |
634 Task task = _timeout.expired(); | |
635 while (task!=null) | |
636 { | |
637 if (task instanceof Runnable) | |
638 execute((Runnable)task); | |
639 task = _timeout.expired(); | |
640 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
641 |
865 | 642 // Idle tick |
643 if (now-_idleTick>__IDLE_TICK) | |
644 { | |
645 _idleTick=now; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
646 |
865 | 647 final long idle_now=((_lowResourcesConnections>0 && selector.keys().size()>_lowResourcesConnections)) |
914
54308d65265a
simplify SelectorManager
Franklin Schmidt <fschmidt@gmail.com>
parents:
865
diff
changeset
|
648 ?(now+_maxIdleTime) |
865 | 649 :now; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
650 |
865 | 651 execute(new Runnable() |
652 { | |
653 public void run() | |
654 { | |
655 for (SelectChannelEndPoint endp:_endPoints.keySet()) | |
656 { | |
657 endp.checkIdleTimestamp(idle_now); | |
658 } | |
659 } | |
660 public String toString() {return "Idle-"+super.toString();} | |
661 }); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
662 |
865 | 663 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
664 |
865 | 665 // Reset busy select monitor counts |
666 if (__MONITOR_PERIOD>0 && now>_monitorNext) | |
667 { | |
668 _busySelects=0; | |
669 _pausing=false; | |
670 _monitorNext=now+__MONITOR_PERIOD; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
671 |
865 | 672 } |
673 } | |
674 catch (ClosedSelectorException e) | |
675 { | |
676 if (isRunning()) | |
677 LOG.warn("",e); | |
678 else | |
679 LOG.trace("",e); | |
680 } | |
681 catch (CancelledKeyException e) | |
682 { | |
683 LOG.trace("",e); | |
684 } | |
685 finally | |
686 { | |
687 _selecting=null; | |
688 } | |
689 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
690 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
691 |
865 | 692 private void renewSelector() |
693 { | |
694 try | |
695 { | |
696 synchronized (this) | |
697 { | |
698 Selector selector=_selector; | |
699 if (selector==null) | |
700 return; | |
701 final Selector new_selector = Selector.open(); | |
702 for (SelectionKey k: selector.keys()) | |
703 { | |
704 if (!k.isValid() || k.interestOps()==0) | |
705 continue; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
706 |
865 | 707 final SelectableChannel channel = k.channel(); |
708 final Object attachment = k.attachment(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
709 |
865 | 710 if (attachment==null) |
711 addChange(channel); | |
712 else | |
713 addChange(channel,attachment); | |
714 } | |
715 _selector.close(); | |
716 _selector=new_selector; | |
717 } | |
718 } | |
719 catch(IOException e) | |
720 { | |
721 throw new RuntimeException("recreating selector",e); | |
722 } | |
723 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
724 |
865 | 725 public SelectorManager getManager() |
726 { | |
727 return SelectorManager.this; | |
728 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
729 |
865 | 730 public long getNow() |
731 { | |
732 return _timeout.getNow(); | |
733 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
734 |
865 | 735 /* ------------------------------------------------------------ */ |
736 /** | |
737 * @param task The task to timeout. If it implements Runnable, then | |
738 * expired will be called from a dispatched thread. | |
739 * | |
740 * @param timeoutMs | |
741 */ | |
742 public void scheduleTimeout(Timeout.Task task, long timeoutMs) | |
743 { | |
744 if (!(task instanceof Runnable)) | |
745 throw new IllegalArgumentException("!Runnable"); | |
746 _timeout.schedule(task, timeoutMs); | |
747 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
748 |
865 | 749 public void cancelTimeout(Timeout.Task task) |
750 { | |
751 task.cancel(); | |
752 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
753 |
865 | 754 public void wakeup() |
755 { | |
756 try | |
757 { | |
758 Selector selector = _selector; | |
759 if (selector!=null) | |
760 selector.wakeup(); | |
761 } | |
762 catch(Exception e) | |
763 { | |
764 addChange(new ChangeTask() | |
765 { | |
766 public void run() | |
767 { | |
768 renewSelector(); | |
769 } | |
770 }); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
771 |
865 | 772 renewSelector(); |
773 } | |
774 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
775 |
865 | 776 private SelectChannelEndPoint createEndPoint(SocketChannel channel, SelectionKey sKey) throws IOException |
777 { | |
778 SelectChannelEndPoint endp = newEndPoint(channel,this,sKey); | |
779 LOG.debug("created {}",endp); | |
780 _endPoints.put(endp,this); | |
781 return endp; | |
782 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
783 |
865 | 784 public void destroyEndPoint(SelectChannelEndPoint endp) |
785 { | |
786 LOG.debug("destroyEndPoint {}",endp); | |
787 _endPoints.remove(endp); | |
788 endPointClosed(endp); | |
789 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
790 |
865 | 791 Selector getSelector() |
792 { | |
793 return _selector; | |
794 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
795 |
865 | 796 void stop() throws Exception |
797 { | |
798 // Spin for a while waiting for selector to complete | |
799 // to avoid unneccessary closed channel exceptions | |
800 try | |
801 { | |
802 for (int i=0;i<100 && _selecting!=null;i++) | |
803 { | |
804 wakeup(); | |
805 Thread.sleep(10); | |
806 } | |
807 } | |
808 catch(Exception e) | |
809 { | |
810 LOG.trace("",e); | |
811 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
812 |
865 | 813 // close endpoints and selector |
814 synchronized (this) | |
815 { | |
816 Selector selector=_selector; | |
817 for (SelectionKey key:selector.keys()) | |
818 { | |
819 if (key==null) | |
820 continue; | |
821 Object att=key.attachment(); | |
822 if (att instanceof EndPoint) | |
823 { | |
824 EndPoint endpoint = (EndPoint)att; | |
825 try | |
826 { | |
827 endpoint.close(); | |
828 } | |
829 catch(IOException e) | |
830 { | |
831 LOG.trace("",e); | |
832 } | |
833 } | |
834 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
835 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
836 |
865 | 837 _timeout.cancelAll(); |
838 try | |
839 { | |
840 selector=_selector; | |
841 if (selector != null) | |
842 selector.close(); | |
843 } | |
844 catch (IOException e) | |
845 { | |
846 LOG.trace("",e); | |
847 } | |
848 _selector=null; | |
849 } | |
850 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
851 |
865 | 852 public String dump() |
853 { | |
854 return AggregateLifeCycle.dump(this); | |
855 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
856 |
865 | 857 public void dump(Appendable out, String indent) throws IOException |
858 { | |
859 out.append(String.valueOf(this)).append(" id=").append(String.valueOf(_setID)).append("\n"); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
860 |
865 | 861 Thread selecting = _selecting; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
862 |
865 | 863 Object where = "not selecting"; |
864 StackTraceElement[] trace =selecting==null?null:selecting.getStackTrace(); | |
865 if (trace!=null) | |
866 { | |
867 for (StackTraceElement t:trace) | |
868 if (t.getClassName().startsWith("org.eclipse.jetty.")) | |
869 { | |
870 where=t; | |
871 break; | |
872 } | |
873 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
874 |
865 | 875 Selector selector=_selector; |
876 if (selector!=null) | |
877 { | |
878 final ArrayList<Object> dump = new ArrayList<Object>(selector.keys().size()*2); | |
879 dump.add(where); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
880 |
865 | 881 final CountDownLatch latch = new CountDownLatch(1); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
882 |
865 | 883 addChange(new ChangeTask() |
884 { | |
885 public void run() | |
886 { | |
887 dumpKeyState(dump); | |
888 latch.countDown(); | |
889 } | |
890 }); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
891 |
865 | 892 try |
893 { | |
894 latch.await(5,TimeUnit.SECONDS); | |
895 } | |
896 catch(InterruptedException e) | |
897 { | |
898 LOG.trace("",e); | |
899 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
900 |
865 | 901 AggregateLifeCycle.dump(out,indent,dump); |
902 } | |
903 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
904 |
865 | 905 public void dumpKeyState(List<Object> dumpto) |
906 { | |
907 Selector selector=_selector; | |
908 Set<SelectionKey> keys = selector.keys(); | |
909 dumpto.add(selector + " keys=" + keys.size()); | |
910 for (SelectionKey key: keys) | |
911 { | |
912 if (key.isValid()) | |
913 dumpto.add(key.attachment()+" iOps="+key.interestOps()+" rOps="+key.readyOps()); | |
914 else | |
915 dumpto.add(key.attachment()+" iOps=-1 rOps=-1"); | |
916 } | |
917 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
918 |
865 | 919 public String toString() |
920 { | |
921 Selector selector=_selector; | |
922 return String.format("%s keys=%d selected=%d", | |
923 super.toString(), | |
924 selector != null && selector.isOpen() ? selector.keys().size() : -1, | |
925 selector != null && selector.isOpen() ? selector.selectedKeys().size() : -1); | |
926 } | |
927 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
928 |
865 | 929 private static class ChannelAndAttachment |
930 { | |
931 final SelectableChannel _channel; | |
932 final Object _attachment; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
933 |
865 | 934 public ChannelAndAttachment(SelectableChannel channel, Object attachment) |
935 { | |
936 super(); | |
937 _channel = channel; | |
938 _attachment = attachment; | |
939 } | |
940 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
941 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
942 |
865 | 943 private interface ChangeTask extends Runnable |
944 {} | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
945 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
946 } |