diff src/org/eclipse/jetty/io/nio/SelectorManager.java @ 964:768414c16e10

remove SelectSet
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 14 Oct 2016 01:03:47 -0600
parents 94498d6daf5b
children 0d20943cfea2
line wrap: on
line diff
--- a/src/org/eclipse/jetty/io/nio/SelectorManager.java	Fri Oct 14 00:15:28 2016 -0600
+++ b/src/org/eclipse/jetty/io/nio/SelectorManager.java	Fri Oct 14 01:03:47 2016 -0600
@@ -56,18 +56,18 @@
  */
 public abstract class SelectorManager extends AbstractLifeCycle implements Dumpable
 {
-	public static final Logger LOG=LoggerFactory.getLogger("org.eclipse.jetty.io.nio");
+	public static final Logger LOG = LoggerFactory.getLogger("org.eclipse.jetty.io.nio");
 
-	private int _maxIdleTime;
-	private long _lowResourcesConnections;
-	private SelectSet _selectSet;
+	private final int _maxIdleTime;
+	private volatile long _now = System.currentTimeMillis();
+	private SaneSelector _selector;
 
 	/* ------------------------------------------------------------ */
 	/**
 	 * @param maxIdleTime The maximum period in milli seconds that a connection may be idle before it is closed.
 	 * @see #setLowResourcesMaxIdleTime(long)
 	 */
-	public void setMaxIdleTime(int maxIdleTime)
+	public SelectorManager(int maxIdleTime)
 	{
 		_maxIdleTime = maxIdleTime;
 	}
@@ -76,47 +76,41 @@
 	/** Register a channel
 	 * @param channel
 	 */
-	public void register(SocketChannel channel)
+	public final void register(SocketChannel channel)
 	{
+/*
 		SelectSet set = _selectSet;
 		if (set!=null)
 		{
 			set.addChange(channel);
 		}
-	}
-
-	/* ------------------------------------------------------------ */
-	/**
-	 * @return the lowResourcesConnections
-	 */
-	public long getLowResourcesConnections()
-	{
-		return _lowResourcesConnections;
-	}
-
-	/* ------------------------------------------------------------ */
-	/**
-	 * Set the number of connections, which if exceeded places this manager in low resources state.
-	 * This is not an exact measure as the connection count is averaged over the select sets.
-	 * @param lowResourcesConnections the number of connections
-	 * @see #setLowResourcesMaxIdleTime(long)
-	 */
-	public void setLowResourcesConnections(long lowResourcesConnections)
-	{
-		_lowResourcesConnections = lowResourcesConnections;
+*/
+		try {
+			SelectionKey key = _selector.register(channel,0,null);
+			SelectChannelEndPoint endpoint = new SelectChannelEndPoint(channel,this,key, _maxIdleTime);
+			endpoint.setConnection(newConnection(channel,endpoint));
+			key.attach(endpoint);
+			_selector.update();
+//System.out.println("qqqqqqqqqqqqqqqqqqqqqqqqqqqqq b");
+			endpoint.schedule();
+		} catch(IOException e) {
+			LOG.warn("",e);
+			try {
+				channel.close();
+			} catch(IOException e2) {
+				LOG.warn("",e2);
+			}
+		}
 	}
 
 
 	public abstract void execute(Runnable task);
 
-	/* ------------------------------------------------------------ */
-	/* (non-Javadoc)
-	 * @see org.eclipse.component.AbstractLifeCycle#doStart()
-	 */
+
 	@Override
 	protected void doStart() throws Exception
 	{
-		_selectSet = new SelectSet();
+		_selector = new SaneSelector();
 
 		super.doStart();
 
@@ -128,8 +122,7 @@
 				String name=Thread.currentThread().getName();
 				try
 				{
-					SelectSet set = _selectSet;
-					if (set==null)
+					if (_selector==null)
 						return;
 
 					Thread.currentThread().setName(name+" Selector");
@@ -138,7 +131,7 @@
 					{
 						try
 						{
-							set.doSelect();
+							doSelect();
 						}
 						catch(IOException e)
 						{
@@ -152,6 +145,7 @@
 				}
 				finally
 				{
+					_selector = null;
 					LOG.debug("Stopped {} on {}",Thread.currentThread(),this);
 					Thread.currentThread().setName(name);
 				}
@@ -162,13 +156,32 @@
 
 
 	@Override
-	protected void doStop() throws Exception
+	protected synchronized void doStop() throws Exception
 	{
-		SelectSet set = _selectSet;
-		_selectSet = null;
-		if (set!=null)
+		if (_selector!=null)
 		{
-			set.stop();
+			// close endpoints and selector
+			for (SelectionKey key : _selector.keys())
+			{
+				EndPoint endpoint = (EndPoint)key.attachment();
+				try
+				{
+					endpoint.close();
+				}
+				catch(IOException e)
+				{
+					LOG.trace("",e);
+				}
+			}
+	
+			try
+			{
+				_selector.close();
+			}
+			catch (IOException e)
+			{
+				LOG.trace("",e);
+			}
 		}
 		super.doStop();
 	}
@@ -185,160 +198,81 @@
 	public void dump(Appendable out, String indent) throws IOException
 	{
 		AggregateLifeCycle.dumpObject(out,this);
-		AggregateLifeCycle.dump(out,indent,Collections.singletonList(_selectSet));
+//		AggregateLifeCycle.dump(out,indent,Collections.emptyList());
 	}
 
 
-	public final class SelectSet implements Dumpable
-	{
-		private volatile long _now = System.currentTimeMillis();
+	// from SelectSet
 
-		private final SaneSelector _selector;
-
-		private SelectSet() throws IOException
+	private void doSelect() throws IOException
+	{
+		try
 		{
-			_selector = new SaneSelector();
-		}
-
-		private void addChange(SocketChannel channel)
-		{
-			try {
-//System.out.println("qqqqqqqqqqqqqqqqqqqqqqqqqqqqq a");
-//				SelectionKey key = _selector.register(channel,SelectionKey.OP_READ,null);
-				SelectionKey key = _selector.register(channel,0,null);
-
-				SelectChannelEndPoint endpoint = new SelectChannelEndPoint(channel,this,key, _maxIdleTime);
-				endpoint.setConnection(newConnection(channel,endpoint));
+			_selector.select();
 
-				key.attach(endpoint);
-//				key.interestOps(SelectionKey.OP_READ);
-				_selector.update();
-//System.out.println("qqqqqqqqqqqqqqqqqqqqqqqqqqqqq b");
-				endpoint.schedule();
-			} catch(IOException e) {
-				LOG.warn("",e);
-				try {
-					channel.close();
-				} catch(IOException e2) {
-					LOG.warn("",e2);
-				}
-			}
-		}
-
-		private void doSelect() throws IOException
-		{
-			try
+			// Look for things to do
+			for (SelectionKey key: _selector.selectedKeys())
 			{
-				_selector.select();
+				try
+				{
+					if (!key.isValid())
+					{
+						key.cancel();
+						continue;
+					}
 
-				// Look for things to do
-				for (SelectionKey key: _selector.selectedKeys())
-				{
-					try
-					{
-						if (!key.isValid())
-						{
-							key.cancel();
-							continue;
-						}
-
-						if (key.isReadable()||key.isWritable()) {
-							SelectChannelEndPoint endpoint = (SelectChannelEndPoint)key.attachment();
-							endpoint.schedule();
-						}
-					}
-					catch (CancelledKeyException e)
-					{
-//						LOG.trace("",e);
-						LOG.warn("",e);
-					}
-					catch (Exception e)
-					{
-						LOG.warn("",e);
+					if (key.isReadable()||key.isWritable()) {
+						SelectChannelEndPoint endpoint = (SelectChannelEndPoint)key.attachment();
+						endpoint.schedule();
 					}
 				}
-
-				// Everything always handled
-				_selector.selectedKeys().clear();
-
-				_now = System.currentTimeMillis();
-			}
-			catch (ClosedSelectorException e)
-			{
-				if (isRunning())
+				catch (CancelledKeyException e)
+				{
+					// LOG.trace("",e);
 					LOG.warn("",e);
-				else
-					LOG.trace("",e);
-			}
-			catch (CancelledKeyException e)
-			{
-				LOG.trace("",e);
-			}
-		}
-
-		public SelectorManager getManager()
-		{
-			return SelectorManager.this;
-		}
-
-		public long getNow()
-		{
-			return _now;
-		}
-
-		SaneSelector getSelector()
-		{
-			return _selector;
-		}
-
-		private synchronized void stop() throws Exception
-		{
-			// close endpoints and selector
-			for (SelectionKey key : _selector.keys())
-			{
-				EndPoint endpoint = (EndPoint)key.attachment();
-				try
+				}
+				catch (Exception e)
 				{
-					endpoint.close();
-				}
-				catch(IOException e)
-				{
-					LOG.trace("",e);
+					LOG.warn("",e);
 				}
 			}
 
-			try
-			{
-				_selector.close();
-			}
-			catch (IOException e)
-			{
-				LOG.trace("",e);
-			}
-		}
-
-		@Override
-		public String dump()
-		{
-			return AggregateLifeCycle.dump(this);
-		}
+			// Everything always handled
+			_selector.selectedKeys().clear();
 
-		@Override
-		public void dump(Appendable out, String indent) throws IOException
-		{
-			out.append(String.valueOf(this)).append("\n");
-			AggregateLifeCycle.dump(out,indent,Collections.emptyList());
+			_now = System.currentTimeMillis();
 		}
-
-		@Override
-		public String toString()
+		catch (ClosedSelectorException e)
 		{
-			SaneSelector selector=_selector;
-			return String.format("%s keys=%d selected=%d",
-					super.toString(),
-					selector != null && selector.isOpen() ? selector.keys().size() : -1,
-					selector != null && selector.isOpen() ? selector.selectedKeys().size() : -1);
+			if (isRunning())
+				LOG.warn("",e);
+			else
+				LOG.trace("",e);
+		}
+		catch (CancelledKeyException e)
+		{
+			LOG.trace("",e);
 		}
 	}
 
+	public final long getNow()
+	{
+		return _now;
+	}
+
+	final SaneSelector getSelector()
+	{
+		return _selector;
+	}
+
+	@Override
+	public final String toString()
+	{
+		SaneSelector selector=_selector;
+		return String.format("%s keys=%d selected=%d",
+				super.toString(),
+				selector != null && selector.isOpen() ? selector.keys().size() : -1,
+				selector != null && selector.isOpen() ? selector.selectedKeys().size() : -1);
+	}
+
 }