Fixes a bug with the accept thread busy waiting in select(), simplifies code.

This commit is contained in:
boris 2014-09-05 16:46:48 +00:00
parent eccf1346c0
commit 6bc0b7c4de

View File

@ -74,6 +74,11 @@ public class MultiplexingTcpHostHarvester
*/ */
private ReadThread readThread; private ReadThread readThread;
/**
* The <tt>Selector</tt> used by {@link #readThread}.
*/
private Selector readSelector = Selector.open();
/** /**
* Triggers the termination of the threads of this instance. * Triggers the termination of the threads of this instance.
*/ */
@ -101,12 +106,6 @@ public class MultiplexingTcpHostHarvester
private final Map<String, WeakReference<Component>> components private final Map<String, WeakReference<Component>> components
= new HashMap<String, WeakReference<Component>>(); = new HashMap<String, WeakReference<Component>>();
/**
* The <tt>Pipe</tt> on which {@link #acceptThread} notifies
* {@link #readThread} about the existence of new accepted channels.
*/
private Pipe pipe;
/** /**
* Initializes a new <tt>MultiplexingTcpHostHarvester</tt>, which is to * Initializes a new <tt>MultiplexingTcpHostHarvester</tt>, which is to
* listen on port number <tt>port</tt> on all IP addresses on all * listen on port number <tt>port</tt> on all IP addresses on all
@ -203,8 +202,6 @@ public class MultiplexingTcpHostHarvester
serverSocketChannels.add(channel); serverSocketChannels.add(channel);
} }
pipe = Pipe.open();
acceptThread = new AcceptThread(); acceptThread = new AcceptThread();
acceptThread.start(); acceptThread.start();
@ -361,12 +358,6 @@ public class MultiplexingTcpHostHarvester
* new socket has been <tt>accept</tt>ed in order to notify * new socket has been <tt>accept</tt>ed in order to notify
* {@link #readThread}. * {@link #readThread}.
*/ */
private final Pipe.SinkChannel pipeSink;
/**
* A <tt>ByteBuffer</tt> used to write to {@link #pipeSink}.
*/
private final ByteBuffer buffer;
/** /**
* Initializes a new <tt>AcceptThread</tt>. * Initializes a new <tt>AcceptThread</tt>.
@ -376,9 +367,6 @@ public class MultiplexingTcpHostHarvester
{ {
setName("MultiplexingTcpHostHarvester AcceptThread"); setName("MultiplexingTcpHostHarvester AcceptThread");
pipeSink = pipe.sink();
buffer = ByteBuffer.allocate(1);
selector = Selector.open(); selector = Selector.open();
for (ServerSocketChannel channel : serverSocketChannels) for (ServerSocketChannel channel : serverSocketChannels)
{ {
@ -393,13 +381,9 @@ public class MultiplexingTcpHostHarvester
@Override @Override
public void run() public void run()
{ {
int readyChannels;
SocketChannel channel;
IOException exception;
boolean added;
while (true) while (true)
{ {
int readyChannels;
if (close) if (close)
{ {
break; break;
@ -421,42 +405,49 @@ public class MultiplexingTcpHostHarvester
{ {
synchronized (newChannels) synchronized (newChannels)
{ {
exception = null; }
added = false; IOException exception = null;
List<SocketChannel> channelsToAdd
= new LinkedList<SocketChannel>();
for (SelectionKey key : selector.selectedKeys()) for (SelectionKey key : selector.selectedKeys())
{
SocketChannel channel;
if (key.isAcceptable())
{ {
if (key.isAcceptable()) try
{ {
try channel = ((ServerSocketChannel)
{ key.channel()).accept();
channel = ((ServerSocketChannel)
key.channel()).accept();
}
catch (IOException ioe)
{
exception = ioe;
break;
}
// Add the accepted socket to newChannels, so
// the 'read' thread can pick it up.
newChannels.add(channel);
added = true;
} }
catch (IOException ioe)
{
exception = ioe;
break;
}
// Add the accepted socket to newChannels, so
// the 'read' thread can pick it up.
channelsToAdd.add(channel);
} }
selector.selectedKeys().clear(); }
selector.selectedKeys().clear();
if (added) if (!channelsToAdd.isEmpty())
notifyReadThread(); {
synchronized (newChannels)
if (exception != null)
{ {
logger.info("Failed to accept a socket, which" newChannels.addAll(channelsToAdd);
+ "should have been ready to accept: "
+ exception);
break;
} }
notifyReadThread();
}
if (exception != null)
{
logger.info("Failed to accept a socket, which"
+ "should have been ready to accept: "
+ exception);
break;
} }
} }
} // while(true) } // while(true)
@ -484,23 +475,10 @@ public class MultiplexingTcpHostHarvester
/** /**
* Notifies {@link #readThread} that new channels have been added to * Notifies {@link #readThread} that new channels have been added to
* {@link #newChannels} by writing to {@link #pipe}.
*/ */
private void notifyReadThread() private void notifyReadThread()
{ {
try readSelector.wakeup();
{
//XXX do we have to fill it each time?
buffer.clear();
buffer.put((byte) 0);
buffer.flip();
pipeSink.write(buffer);
buffer.flip();
}
catch (IOException ioe)
{
logger.info("Failed to write to pipe.");
}
} }
} }
@ -524,12 +502,6 @@ public class MultiplexingTcpHostHarvester
* The channel on which we will be notified when new channels are * The channel on which we will be notified when new channels are
* available in {@link #newChannels}. * available in {@link #newChannels}.
*/ */
private final Pipe.SourceChannel pipeSource;
/**
* A buffer into which we will read away from {@link #pipeSource}.
*/
private final ByteBuffer buffer;
/** /**
* Used in {@link #cleanup()}, defined here to avoid allocating on * Used in {@link #cleanup()}, defined here to avoid allocating on
@ -551,11 +523,7 @@ public class MultiplexingTcpHostHarvester
private ReadThread() private ReadThread()
throws IOException throws IOException
{ {
selector = Selector.open(); selector = MultiplexingTcpHostHarvester.this.readSelector;
pipeSource = pipe.source();
pipeSource.configureBlocking(false);
pipeSource.register(selector, SelectionKey.OP_READ);
buffer = ByteBuffer.allocate(1);
datagramPacket = new DatagramPacket(new byte[1500], 1500); datagramPacket = new DatagramPacket(new byte[1500], 1500);
} }
@ -599,39 +567,16 @@ public class MultiplexingTcpHostHarvester
if (key.isReadable()) if (key.isReadable())
{ {
selectedChannel = key.channel(); selectedChannel = key.channel();
if (selectedChannel == pipeSource) key.cancel();
{
// That's just the nudge that new channels are
// available. Just read from the pipe, so it
// doesn't get hogged.
try
{
pipeSource.read(buffer);
}
catch (IOException ioe)
{
logger.info("Failed to read from pipe.");
}
}
else
{
/*
* We need this in order to de-register the
* channel from the selector.
*/
key.cancel();
readFromChannel((SocketChannel) selectedChannel); readFromChannel((SocketChannel) selectedChannel);
channels.remove(selectedChannel); channels.remove(selectedChannel);
}
} }
} }
selectedKeys.clear(); selectedKeys.clear();
} }
} //while(true) } //while(true)
//we are all done, clean up. //we are all done, clean up.
synchronized (newChannels) synchronized (newChannels)
{ {