Jetty9 - Now calling SelectorManager.newConnection() from a single call site rather than having to duplicate the call in every newEndPoint() implementation.

This commit is contained in:
Simone Bordet 2012-07-19 18:43:23 +02:00
parent 796e240545
commit dfeb761306
7 changed files with 28 additions and 39 deletions

View File

@ -39,7 +39,6 @@ import org.eclipse.jetty.util.component.Dumpable;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.log.Logger;
/** /**
* The Selector Manager manages and number of SelectSets to allow * The Selector Manager manages and number of SelectSets to allow
* NIO scheduling to scale to large numbers of connections. * NIO scheduling to scale to large numbers of connections.
@ -49,8 +48,8 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
{ {
public static final Logger LOG = Log.getLogger(SelectorManager.class); public static final Logger LOG = Log.getLogger(SelectorManager.class);
private final ManagedSelector[] _selectSets; private final ManagedSelector[] _selectors;
private long _selectSetIndex; private long _selectorIndex;
private volatile long _maxIdleTime; private volatile long _maxIdleTime;
protected SelectorManager() protected SelectorManager()
@ -60,7 +59,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
protected SelectorManager(@Name("selectors") int selectors) protected SelectorManager(@Name("selectors") int selectors)
{ {
this._selectSets = new ManagedSelector[selectors]; this._selectors = new ManagedSelector[selectors];
} }
/** /**
@ -81,19 +80,19 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
/** /**
* @return the number of select sets in use * @return the number of select sets in use
*/ */
public int getSelectSets() public int getSelectorCount()
{ {
return _selectSets.length; return _selectors.length;
} }
private ManagedSelector chooseSelectSet() private ManagedSelector chooseSelector()
{ {
// The ++ increment here is not atomic, but it does not matter. // The ++ increment here is not atomic, but it does not matter.
// so long as the value changes sometimes, then connections will // so long as the value changes sometimes, then connections will
// be distributed over the available sets. // be distributed over the available sets.
long s = _selectSetIndex++; long s = _selectorIndex++;
int index = (int)(s % getSelectSets()); int index = (int)(s % getSelectorCount());
return _selectSets[index]; return _selectors[index];
} }
/** /**
@ -103,7 +102,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
*/ */
public void connect(SocketChannel channel, Object attachment) public void connect(SocketChannel channel, Object attachment)
{ {
ManagedSelector set = chooseSelectSet(); ManagedSelector set = chooseSelector();
set.submit(set.new Connect(channel, attachment)); set.submit(set.new Connect(channel, attachment));
} }
@ -113,7 +112,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
*/ */
public void accept(final SocketChannel channel) public void accept(final SocketChannel channel)
{ {
final ManagedSelector set = chooseSelectSet(); final ManagedSelector set = chooseSelector();
set.submit(set.new Accept(channel)); set.submit(set.new Accept(channel));
} }
@ -121,17 +120,17 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
protected void doStart() throws Exception protected void doStart() throws Exception
{ {
super.doStart(); super.doStart();
for (int i=0;i< _selectSets.length;i++) for (int i=0;i< _selectors.length;i++)
{ {
ManagedSelector selectSet = newSelectSet(i); ManagedSelector selectSet = newSelector(i);
_selectSets[i] = selectSet; _selectors[i] = selectSet;
selectSet.start(); selectSet.start();
execute(selectSet); execute(selectSet);
execute(new Expirer()); execute(new Expirer());
} }
} }
protected ManagedSelector newSelectSet(int id) protected ManagedSelector newSelector(int id)
{ {
return new ManagedSelector(id); return new ManagedSelector(id);
} }
@ -139,7 +138,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
@Override @Override
protected void doStop() throws Exception protected void doStop() throws Exception
{ {
for (ManagedSelector set : _selectSets) for (ManagedSelector set : _selectors)
set.stop(); set.stop();
super.doStop(); super.doStop();
} }
@ -149,6 +148,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
*/ */
protected void endPointOpened(AsyncEndPoint endpoint) protected void endPointOpened(AsyncEndPoint endpoint)
{ {
endpoint.onOpen();
endpoint.getAsyncConnection().onOpen(); endpoint.getAsyncConnection().onOpen();
} }
@ -200,7 +200,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
public void dump(Appendable out, String indent) throws IOException public void dump(Appendable out, String indent) throws IOException
{ {
AggregateLifeCycle.dumpObject(out,this); AggregateLifeCycle.dumpObject(out,this);
AggregateLifeCycle.dump(out, indent, TypeUtil.asList(_selectSets)); AggregateLifeCycle.dump(out, indent, TypeUtil.asList(_selectors));
} }
private class Expirer implements Runnable private class Expirer implements Runnable
@ -210,7 +210,7 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
{ {
while (isRunning()) while (isRunning())
{ {
for (ManagedSelector selector : _selectSets) for (ManagedSelector selector : _selectors)
if (selector!=null) if (selector!=null)
selector.timeoutCheck(); selector.timeoutCheck();
sleep(1000); sleep(1000);
@ -452,14 +452,14 @@ public abstract class SelectorManager extends AbstractLifeCycle implements Dumpa
private AsyncEndPoint createEndPoint(SocketChannel channel, SelectionKey sKey) throws IOException private AsyncEndPoint createEndPoint(SocketChannel channel, SelectionKey sKey) throws IOException
{ {
Selectable endp = newEndPoint(channel, this, sKey); Selectable asyncEndPoint = newEndPoint(channel, this, sKey);
_endPoints.put(endp, this); asyncEndPoint.setAsyncConnection(newConnection(channel, asyncEndPoint, sKey.attachment()));
LOG.debug("Created {}", endp); _endPoints.put(asyncEndPoint, this);
endPointOpened(endp); LOG.debug("Created {}", asyncEndPoint);
return endp; endPointOpened(asyncEndPoint);
return asyncEndPoint;
} }
public void destroyEndPoint(Selectable endp) public void destroyEndPoint(Selectable endp)
{ {
LOG.debug("Destroyed {}", endp); LOG.debug("Destroyed {}", endp);

View File

@ -53,7 +53,6 @@ public class SelectChannelEndPointTest
protected SelectChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException protected SelectChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException
{ {
SelectChannelEndPoint endp = new SelectChannelEndPoint(channel,selectSet,key,getMaxIdleTime()); SelectChannelEndPoint endp = new SelectChannelEndPoint(channel,selectSet,key,getMaxIdleTime());
endp.setAsyncConnection(selectSet.getManager().newConnection(channel,endp, key.attachment()));
_lastEndp=endp; _lastEndp=endp;
return endp; return endp;
} }

View File

@ -58,7 +58,6 @@ public class SslConnectionTest
protected SelectChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException protected SelectChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException
{ {
SelectChannelEndPoint endp = new SelectChannelEndPoint(channel,selectSet,key,getMaxIdleTime()); SelectChannelEndPoint endp = new SelectChannelEndPoint(channel,selectSet,key,getMaxIdleTime());
endp.setAsyncConnection(selectSet.getManager().newConnection(channel,endp, key.attachment()));
_lastEndp=endp; _lastEndp=endp;
// System.err.println("newEndPoint "+endp); // System.err.println("newEndPoint "+endp);
return endp; return endp;

View File

@ -202,9 +202,7 @@ public class SelectChannelConnector extends HttpConnector implements NetConnecto
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
protected SelectChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException protected SelectChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException
{ {
SelectChannelEndPoint endp= new SelectChannelEndPoint(channel,selectSet,key, SelectChannelConnector.this._maxIdleTime); return new SelectChannelEndPoint(channel,selectSet,key, this._maxIdleTime);
endp.setAsyncConnection(selectSet.getManager().newConnection(channel,endp, key.attachment()));
return endp;
} }
/* ------------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------------- */

View File

@ -56,7 +56,6 @@ public class NetworkTrafficSelectChannelConnector extends SelectChannelConnector
protected SelectChannelEndPoint newEndPoint(SocketChannel channel, SelectorManager.ManagedSelector selectSet, SelectionKey key) throws IOException protected SelectChannelEndPoint newEndPoint(SocketChannel channel, SelectorManager.ManagedSelector selectSet, SelectionKey key) throws IOException
{ {
NetworkTrafficSelectChannelEndPoint endPoint = new NetworkTrafficSelectChannelEndPoint(channel, selectSet, key, _maxIdleTime, listeners); NetworkTrafficSelectChannelEndPoint endPoint = new NetworkTrafficSelectChannelEndPoint(channel, selectSet, key, _maxIdleTime, listeners);
endPoint.setAsyncConnection(selectSet.getManager().newConnection(channel,endPoint, key.attachment()));
endPoint.notifyOpened(); endPoint.notifyOpened();
return endPoint; return endPoint;
} }

View File

@ -279,12 +279,8 @@ public class SPDYClient
long maxIdleTime = attachment.client.getMaxIdleTime(); long maxIdleTime = attachment.client.getMaxIdleTime();
if (maxIdleTime < 0) if (maxIdleTime < 0)
maxIdleTime = getMaxIdleTime(); maxIdleTime = getMaxIdleTime();
SelectChannelEndPoint result = new SelectChannelEndPoint(channel, selectSet, key, (int)maxIdleTime);
AsyncConnection connection = newConnection(channel, result, attachment); return new SelectChannelEndPoint(channel, selectSet, key, maxIdleTime);
result.setAsyncConnection(connection);
return result;
} }
@Override @Override

View File

@ -133,9 +133,7 @@ public class WebSocketClientSelectorManager extends SelectorManager
@Override @Override
protected SelectChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException protected SelectChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException
{ {
SelectChannelEndPoint endp = new SelectChannelEndPoint(channel,selectSet,key,getMaxIdleTime()); return new SelectChannelEndPoint(channel,selectSet,key,getMaxIdleTime());
endp.setAsyncConnection(selectSet.getManager().newConnection(channel,endp,key.attachment()));
return endp;
} }
public SSLEngine newSSLEngine(SslContextFactory sslContextFactory, SocketChannel channel) public SSLEngine newSSLEngine(SslContextFactory sslContextFactory, SocketChannel channel)