org.apache.kafka.common.network
Class Selector

java.lang.Object
  extended by org.apache.kafka.common.network.Selector
All Implemented Interfaces:
Selectable

public class Selector
extends java.lang.Object
implements Selectable

A selector interface for doing non-blocking multi-connection network I/O.

This class works with NetworkSend and NetworkReceive to transmit size-delimited network requests and responses.

A connection can be added to the selector associated with an integer id by doing

 selector.connect(42, new InetSocketAddress("google.com", server.port), 64000, 64000);
 
The connect call does not block on the creation of the TCP connection, so the connect method only begins initiating the connection. The successful invocation of this method does not mean a valid connection has been established. Sending requests, receiving responses, processing connection completions, and disconnections on the existing connections are all done using the poll() call.
 List<NetworkRequest> requestsToSend = Arrays.asList(new NetworkRequest(0, myBytes), new NetworkRequest(1, myOtherBytes));
 selector.poll(TIMEOUT_MS, requestsToSend);
 
The selector maintains several lists that are reset by each call to poll() which are available via various getters. These are reset by each call to poll(). This class is not thread safe!


Constructor Summary
Selector(Metrics metrics, Time time)
          Create a new selector
 
Method Summary
 void close()
          Close this selector and all associated connections
 java.util.List<NetworkReceive> completedReceives()
          The list of receives that completed on the last poll() call.
 java.util.List<NetworkSend> completedSends()
          The list of sends that completed on the last poll() call.
 void connect(int id, java.net.InetSocketAddress address, int sendBufferSize, int receiveBufferSize)
          Begin connecting to the given address and add the connection to this selector associated with the given id number.
 java.util.List<java.lang.Integer> connected()
          The list of connections that completed their connection on the last poll() call.
 void disconnect(int id)
          Disconnect any connections for the given id (if there are any).
 java.util.List<java.lang.Integer> disconnected()
          The list of connections that finished disconnecting on the last poll() call.
 void poll(long timeout, java.util.List<NetworkSend> sends)
          Do whatever I/O can be done on each connection without blocking.
 void wakeup()
          Interrupt the selector if it is blocked waiting to do I/O.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Selector

public Selector(Metrics metrics,
                Time time)
Create a new selector

Method Detail

connect

public void connect(int id,
                    java.net.InetSocketAddress address,
                    int sendBufferSize,
                    int receiveBufferSize)
             throws java.io.IOException
Begin connecting to the given address and add the connection to this selector associated with the given id number.

Note that this call only initiates the connection, which will be completed on a future poll(long, List) call. Check connected() to see which (if any) connections have completed after a given poll call.

Specified by:
connect in interface Selectable
Parameters:
id - The id for the new connection
address - The address to connect to
sendBufferSize - The send buffer for the new connection
receiveBufferSize - The receive buffer for the new connection
Throws:
java.lang.IllegalStateException - if there is already a connection for that id
java.io.IOException - if DNS resolution fails on the hostname or if the broker is down

disconnect

public void disconnect(int id)
Disconnect any connections for the given id (if there are any). The disconnection is asynchronous and will not be processed until the next poll() call.

Specified by:
disconnect in interface Selectable

wakeup

public void wakeup()
Interrupt the selector if it is blocked waiting to do I/O.

Specified by:
wakeup in interface Selectable

close

public void close()
Close this selector and all associated connections

Specified by:
close in interface Selectable

poll

public void poll(long timeout,
                 java.util.List<NetworkSend> sends)
          throws java.io.IOException
Do whatever I/O can be done on each connection without blocking. This includes completing connections, completing disconnections, initiating new sends, or making progress on in-progress sends or receives.

The provided network sends will be started. When this call is completed the user can check for completed sends, receives, connections or disconnects using completedSends(), completedReceives(), connected(), disconnected(). These lists will be cleared at the beginning of each poll(long, List) call and repopulated by the call if any completed I/O.

Specified by:
poll in interface Selectable
Parameters:
timeout - The amount of time to wait, in milliseconds. If negative, wait indefinitely.
sends - The list of new sends to begin
Throws:
java.lang.IllegalStateException - If a send is given for which we have no existing connection or for which there is already an in-progress send
java.io.IOException

completedSends

public java.util.List<NetworkSend> completedSends()
Description copied from interface: Selectable
The list of sends that completed on the last poll() call.

Specified by:
completedSends in interface Selectable

completedReceives

public java.util.List<NetworkReceive> completedReceives()
Description copied from interface: Selectable
The list of receives that completed on the last poll() call.

Specified by:
completedReceives in interface Selectable

disconnected

public java.util.List<java.lang.Integer> disconnected()
Description copied from interface: Selectable
The list of connections that finished disconnecting on the last poll() call.

Specified by:
disconnected in interface Selectable

connected

public java.util.List<java.lang.Integer> connected()
Description copied from interface: Selectable
The list of connections that completed their connection on the last poll() call.

Specified by:
connected in interface Selectable