diff src/org/eclipse/jetty/io/nio/SelectorManager.java @ 944:1d24b6e422fa

simplify SelectorManager
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 11 Oct 2016 20:16:03 -0600
parents 96f60ce98949
children f5aefdc4a81a
line wrap: on
line diff
--- a/src/org/eclipse/jetty/io/nio/SelectorManager.java	Tue Oct 11 02:02:48 2016 -0600
+++ b/src/org/eclipse/jetty/io/nio/SelectorManager.java	Tue Oct 11 20:16:03 2016 -0600
@@ -29,6 +29,7 @@
 import java.nio.channels.SocketChannel;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Collections;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentLinkedQueue;
@@ -121,31 +122,6 @@
 	/* ------------------------------------------------------------ */
 	/** Register a channel
 	 * @param channel
-	 * @param att Attached Object
-	 */
-	public void register(SocketChannel channel, Object att)
-	{
-		// The ++ increment here is not atomic, but it does not matter.
-		// so long as the value changes sometimes, then connections will
-		// be distributed over the available sets.
-
-		int s=_set++;
-		if (s<0)
-			s=-s;
-		s=s%_selectSets;
-		SelectSet[] sets=_selectSet;
-		if (sets!=null)
-		{
-			SelectSet set=sets[s];
-			set.addChange(channel,att);
-			set.wakeup();
-		}
-	}
-
-
-	/* ------------------------------------------------------------ */
-	/** Register a channel
-	 * @param channel
 	 */
 	public void register(SocketChannel channel)
 	{
@@ -343,7 +319,6 @@
 		private volatile long _idleTick;
 		private ConcurrentMap<SelectChannelEndPoint,Object> _endPoints = new ConcurrentHashMap<SelectChannelEndPoint, Object>();
 
-		/* ------------------------------------------------------------ */
 		SelectSet(int acceptorID) throws Exception
 		{
 			_setID=acceptorID;
@@ -355,24 +330,12 @@
 			_monitorNext=System.currentTimeMillis()+__MONITOR_PERIOD;
 		}
 
-		/* ------------------------------------------------------------ */
 		public void addChange(Object change)
 		{
 			_changes.add(change);
 		}
 
 		/* ------------------------------------------------------------ */
-		public void addChange(SelectableChannel channel, Object att)
-		{
-			if (att==null)
-				addChange(channel);
-			else if (att instanceof EndPoint)
-				addChange(att);
-			else
-				addChange(new ChannelAndAttachment(channel,att));
-		}
-
-		/* ------------------------------------------------------------ */
 		/**
 		 * Select and dispatch tasks found from changes and the selector.
 		 *
@@ -405,26 +368,6 @@
 							ch=endpoint.getChannel();
 							endpoint.doUpdateKey();
 						}
-						else if (change instanceof ChannelAndAttachment)
-						{
-							// finish accepting/connecting this connection
-							final ChannelAndAttachment asc = (ChannelAndAttachment)change;
-							final SelectableChannel channel=asc._channel;
-							ch=channel;
-							final Object att = asc._attachment;
-
-							if ((channel instanceof SocketChannel) && ((SocketChannel)channel).isConnected())
-							{
-								key = channel.register(selector,SelectionKey.OP_READ,att);
-								SelectChannelEndPoint endpoint = createEndPoint((SocketChannel)channel,key);
-								key.attach(endpoint);
-								endpoint.schedule();
-							}
-							else if (channel.isOpen())
-							{
-								key = channel.register(selector,SelectionKey.OP_CONNECT,att);
-							}
-						}
 						else if (change instanceof SocketChannel)
 						{
 							// Newly registered channel
@@ -435,10 +378,6 @@
 							key.attach(endpoint);
 							endpoint.schedule();
 						}
-						else if (change instanceof ChangeTask)
-						{
-							((Runnable)change).run();
-						}
 						else if (change instanceof Runnable)
 						{
 							execute((Runnable)change);
@@ -670,40 +609,6 @@
 			}
 		}
 
-
-		private void renewSelector()
-		{
-			try
-			{
-				synchronized (this)
-				{
-					Selector selector=_selector;
-					if (selector==null)
-						return;
-					final Selector new_selector = Selector.open();
-					for (SelectionKey k: selector.keys())
-					{
-						if (!k.isValid() || k.interestOps()==0)
-							continue;
-
-						final SelectableChannel channel = k.channel();
-						final Object attachment = k.attachment();
-
-						if (attachment==null)
-							addChange(channel);
-						else
-							addChange(channel,attachment);
-					}
-					_selector.close();
-					_selector=new_selector;
-				}
-			}
-			catch(IOException e)
-			{
-				throw new RuntimeException("recreating selector",e);
-			}
-		}
-
 		public SelectorManager getManager()
 		{
 			return SelectorManager.this;
@@ -716,24 +621,9 @@
 
 		public void wakeup()
 		{
-			try
-			{
-				Selector selector = _selector;
-				if (selector!=null)
-					selector.wakeup();
-			}
-			catch(Exception e)
-			{
-				addChange(new ChangeTask()
-				{
-					public void run()
-					{
-						renewSelector();
-					}
-				});
-
-				renewSelector();
-			}
+			Selector selector = _selector;
+			if (selector!=null)
+				selector.wakeup();
 		}
 
 		private SelectChannelEndPoint createEndPoint(SocketChannel channel, SelectionKey sKey) throws IOException
@@ -770,7 +660,7 @@
 			}
 			catch(Exception e)
 			{
-				LOG.trace("",e);
+				LOG.warn("",e);
 			}
 
 			// close endpoints and selector
@@ -806,7 +696,7 @@
 				{
 					LOG.trace("",e);
 				}
-				_selector=null;
+				_selector = null;
 			}
 		}
 
@@ -818,63 +708,7 @@
 		public void dump(Appendable out, String indent) throws IOException
 		{
 			out.append(String.valueOf(this)).append(" id=").append(String.valueOf(_setID)).append("\n");
-
-			Thread selecting = _selecting;
-
-			Object where = "not selecting";
-			StackTraceElement[] trace =selecting==null?null:selecting.getStackTrace();
-			if (trace!=null)
-			{
-				for (StackTraceElement t:trace)
-					if (t.getClassName().startsWith("org.eclipse.jetty."))
-					{
-						where=t;
-						break;
-					}
-			}
-
-			Selector selector=_selector;
-			if (selector!=null)
-			{
-				final ArrayList<Object> dump = new ArrayList<Object>(selector.keys().size()*2);
-				dump.add(where);
-
-				final CountDownLatch latch = new CountDownLatch(1);
-
-				addChange(new ChangeTask()
-				{
-					public void run()
-					{
-						dumpKeyState(dump);
-						latch.countDown();
-					}
-				});
-
-				try
-				{
-					latch.await(5,TimeUnit.SECONDS);
-				}
-				catch(InterruptedException e)
-				{
-					LOG.trace("",e);
-				}
-
-				AggregateLifeCycle.dump(out,indent,dump);
-			}
-		}
-
-		public void dumpKeyState(List<Object> dumpto)
-		{
-			Selector selector=_selector;
-			Set<SelectionKey> keys = selector.keys();
-			dumpto.add(selector + " keys=" + keys.size());
-			for (SelectionKey key: keys)
-			{
-				if (key.isValid())
-					dumpto.add(key.attachment()+" iOps="+key.interestOps()+" rOps="+key.readyOps());
-				else
-					dumpto.add(key.attachment()+" iOps=-1 rOps=-1");
-			}
+			AggregateLifeCycle.dump(out,indent,Collections.emptyList());
 		}
 
 		public String toString()
@@ -887,21 +721,4 @@
 		}
 	}
 
-	private static class ChannelAndAttachment
-	{
-		final SelectableChannel _channel;
-		final Object _attachment;
-
-		public ChannelAndAttachment(SelectableChannel channel, Object attachment)
-		{
-			super();
-			_channel = channel;
-			_attachment = attachment;
-		}
-	}
-
-
-	private interface ChangeTask extends Runnable
-	{}
-
 }