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