changeset 964:768414c16e10

remove SelectSet
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 14 Oct 2016 01:03:47 -0600
parents 4b6216fa9cec
children 866f2e801618
files src/org/eclipse/jetty/io/nio/SelectChannelEndPoint.java src/org/eclipse/jetty/io/nio/SelectorManager.java src/org/eclipse/jetty/server/nio/SelectChannelConnector.java
diffstat 3 files changed, 120 insertions(+), 189 deletions(-) [+]
line wrap: on
line diff
diff -r 4b6216fa9cec -r 768414c16e10 src/org/eclipse/jetty/io/nio/SelectChannelEndPoint.java
--- a/src/org/eclipse/jetty/io/nio/SelectChannelEndPoint.java	Fri Oct 14 00:15:28 2016 -0600
+++ b/src/org/eclipse/jetty/io/nio/SelectChannelEndPoint.java	Fri Oct 14 01:03:47 2016 -0600
@@ -32,7 +32,6 @@
 import org.eclipse.jetty.io.ConnectedEndPoint;
 import org.eclipse.jetty.io.Connection;
 import org.eclipse.jetty.io.EofException;
-import org.eclipse.jetty.io.nio.SelectorManager.SelectSet;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -45,7 +44,6 @@
 {
 	public static final Logger LOG=LoggerFactory.getLogger("org.eclipse.jetty.io.nio");
 
-	private final SelectorManager.SelectSet _selectSet;
 	private final SelectorManager _manager;
 	private final SelectionKey _key;
 	private final Runnable _handler = new Runnable()
@@ -79,13 +77,12 @@
 
 	private boolean _ishut = false;
 
-	public SelectChannelEndPoint(SocketChannel channel, SelectSet selectSet, SelectionKey key, int maxIdleTime)
+	public SelectChannelEndPoint(SocketChannel channel, SelectorManager manager, SelectionKey key, int maxIdleTime)
 		throws IOException
 	{
 		super(channel, maxIdleTime);
 
-		_manager = selectSet.getManager();
-		_selectSet = selectSet;
+		_manager = manager;
 		_key = key;
 	}
 
@@ -234,7 +231,7 @@
 		if (isInputShutdown())
 			throw new EofException();
 
-		long now = _selectSet.getNow();
+		long now = _manager.getNow();
 		long end = now+timeoutMs;
 		try
 		{
@@ -252,7 +249,7 @@
 				}
 				finally
 				{
-					now = _selectSet.getNow();
+					now = _manager.getNow();
 				}
 
 				if (_readBlocked && timeoutMs>0 && now>=end)
@@ -276,7 +273,7 @@
 		if (isOutputShutdown())
 			throw new EofException();
 
-		long now=_selectSet.getNow();
+		long now=_manager.getNow();
 		long end=now+timeoutMs;
 		try
 		{
@@ -294,7 +291,7 @@
 				}
 				finally
 				{
-					now = _selectSet.getNow();
+					now = _manager.getNow();
 				}
 				if (_writeBlocked && timeoutMs>0 && now>=end)
 					return false;
@@ -333,7 +330,7 @@
 			|   ((!_socket.isOutputShutdown()&& write_interest) ? SelectionKey.OP_WRITE : 0);
 			if( _key.interestOps() != interestOps ) {
 				_key.interestOps(interestOps);
-				_selectSet.getSelector().update();
+				_manager.getSelector().update();
 			}
 		} else {
 			_key.cancel();
diff -r 4b6216fa9cec -r 768414c16e10 src/org/eclipse/jetty/io/nio/SelectorManager.java
--- 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);
+	}
+
 }
diff -r 4b6216fa9cec -r 768414c16e10 src/org/eclipse/jetty/server/nio/SelectChannelConnector.java
--- a/src/org/eclipse/jetty/server/nio/SelectChannelConnector.java	Fri Oct 14 00:15:28 2016 -0600
+++ b/src/org/eclipse/jetty/server/nio/SelectChannelConnector.java	Fri Oct 14 01:03:47 2016 -0600
@@ -59,7 +59,6 @@
 	public SelectChannelConnector(Server server,int port)
 	{
 		super(server,port);
-		_manager.setMaxIdleTime(getMaxIdleTime());
 		addBean(_manager,true);
 	}
 	
@@ -91,9 +90,6 @@
 	@Override
 	protected synchronized void doStart() throws Exception
 	{
-//		_manager.setMaxIdleTime(getMaxIdleTime());
-		_manager.setLowResourcesConnections(0);
-
 		if (_acceptChannel == null)
 		{
 			// Create a new server socket
@@ -120,6 +116,10 @@
 
 	private final class ConnectorSelectorManager extends SelectorManager
 	{
+		ConnectorSelectorManager() {
+			super(_maxIdleTime);
+		}
+
 		@Override
 		public void execute(Runnable task)
 		{