use of java.nio.channels.SelectionKey in project tomcat by apache.
the class SocketNioSend method main.
public static void main(String[] args) throws Exception {
Selector selector = Selector.open();
Member mbr = new MemberImpl("localhost", 9999, 0);
ChannelData data = new ChannelData();
data.setOptions(Channel.SEND_OPTIONS_BYTE_MESSAGE);
data.setAddress(mbr);
byte[] buf = new byte[8192 * 4];
data.setMessage(new XByteBuffer(buf, false));
buf = XByteBuffer.createDataPackage(data);
int len = buf.length;
BigDecimal total = new BigDecimal((double) 0);
BigDecimal bytes = new BigDecimal((double) len);
NioSender sender = new NioSender();
sender.setDestination(mbr);
sender.setDirectBuffer(true);
sender.setSelector(selector);
sender.setTxBufSize(1024 * 1024);
sender.connect();
sender.setMessage(buf);
System.out.println("Writing to 9999");
long start = 0;
double mb = 0;
boolean first = true;
int count = 0;
DecimalFormat df = new DecimalFormat("##.00");
while (count < 100000) {
if (first) {
first = false;
start = System.currentTimeMillis();
}
sender.setMessage(buf);
int selectedKeys = 0;
try {
selectedKeys = selector.select(0);
} catch (Exception e) {
e.printStackTrace();
continue;
}
if (selectedKeys == 0) {
continue;
}
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey sk = it.next();
it.remove();
try {
int readyOps = sk.readyOps();
sk.interestOps(sk.interestOps() & ~readyOps);
if (sender.process(sk, false)) {
total = total.add(bytes);
sender.reset();
sender.setMessage(buf);
mb += ((double) len) / 1024 / 1024;
if (((++count) % 10000) == 0) {
long time = System.currentTimeMillis();
double seconds = ((double) (time - start)) / 1000;
System.out.println("Throughput " + df.format(mb / seconds) + " MB/seconds, total " + mb + " MB, total " + total + " bytes.");
}
}
} catch (Throwable t) {
t.printStackTrace();
return;
}
}
selector.selectedKeys().clear();
}
System.out.println("Complete, sleeping 15 seconds");
Thread.sleep(15000);
}
use of java.nio.channels.SelectionKey in project tomcat by apache.
the class SocketNioValidateSend method main.
public static void main(String[] args) throws Exception {
Selector selector = Selector.open();
Member mbr = new MemberImpl("localhost", 9999, 0);
byte seq = 0;
byte[] buf = new byte[50000];
Arrays.fill(buf, seq);
int len = buf.length;
BigDecimal total = new BigDecimal((double) 0);
BigDecimal bytes = new BigDecimal((double) len);
NioSender sender = new NioSender();
sender.setDestination(mbr);
sender.setDirectBuffer(true);
sender.setSelector(selector);
sender.connect();
sender.setMessage(buf);
System.out.println("Writing to 9999");
long start = 0;
double mb = 0;
boolean first = true;
int count = 0;
DecimalFormat df = new DecimalFormat("##.00");
while (count < 100000) {
if (first) {
first = false;
start = System.currentTimeMillis();
}
sender.setMessage(buf);
int selectedKeys = 0;
try {
selectedKeys = selector.select(0);
} catch (Exception e) {
e.printStackTrace();
continue;
}
if (selectedKeys == 0) {
continue;
}
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey sk = it.next();
it.remove();
try {
int readyOps = sk.readyOps();
sk.interestOps(sk.interestOps() & ~readyOps);
if (sender.process(sk, false)) {
total = total.add(bytes);
sender.reset();
seq++;
Arrays.fill(buf, seq);
sender.setMessage(buf);
mb += ((double) len) / 1024 / 1024;
if (((++count) % 10000) == 0) {
long time = System.currentTimeMillis();
double seconds = ((double) (time - start)) / 1000;
System.out.println("Throughput " + df.format(mb / seconds) + " MB/seconds, total " + mb + " MB, total " + total + " bytes.");
}
}
} catch (Throwable t) {
t.printStackTrace();
return;
}
}
}
System.out.println("Complete, sleeping 15 seconds");
Thread.sleep(15000);
}
use of java.nio.channels.SelectionKey in project kafka by apache.
the class Selector method connect.
/**
* Begin connecting to the given address and add the connection to this nioSelector associated with the given id
* number.
* <p>
* Note that this call only initiates the connection, which will be completed on a future {@link #poll(long)}
* call. Check {@link #connected()} to see which (if any) connections have completed after a given poll call.
* @param id The id for the new connection
* @param address The address to connect to
* @param sendBufferSize The send buffer for the new connection
* @param receiveBufferSize The receive buffer for the new connection
* @throws IllegalStateException if there is already a connection for that id
* @throws IOException if DNS resolution fails on the hostname or if the broker is down
*/
@Override
public void connect(String id, InetSocketAddress address, int sendBufferSize, int receiveBufferSize) throws IOException {
if (this.channels.containsKey(id))
throw new IllegalStateException("There is already a connection for id " + id);
SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
Socket socket = socketChannel.socket();
socket.setKeepAlive(true);
if (sendBufferSize != Selectable.USE_DEFAULT_BUFFER_SIZE)
socket.setSendBufferSize(sendBufferSize);
if (receiveBufferSize != Selectable.USE_DEFAULT_BUFFER_SIZE)
socket.setReceiveBufferSize(receiveBufferSize);
socket.setTcpNoDelay(true);
boolean connected;
try {
connected = socketChannel.connect(address);
} catch (UnresolvedAddressException e) {
socketChannel.close();
throw new IOException("Can't resolve address: " + address, e);
} catch (IOException e) {
socketChannel.close();
throw e;
}
SelectionKey key = socketChannel.register(nioSelector, SelectionKey.OP_CONNECT);
KafkaChannel channel = channelBuilder.buildChannel(id, key, maxReceiveSize);
key.attach(channel);
this.channels.put(id, channel);
if (connected) {
// OP_CONNECT won't trigger for immediately connected channels
log.debug("Immediately connected to node {}", channel.id());
immediatelyConnectedKeys.add(key);
key.interestOps(0);
}
}
use of java.nio.channels.SelectionKey in project kafka by apache.
the class Selector method pollSelectionKeys.
private void pollSelectionKeys(Iterable<SelectionKey> selectionKeys, boolean isImmediatelyConnected, long currentTimeNanos) {
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
iterator.remove();
KafkaChannel channel = channel(key);
// register all per-connection metrics at once
sensors.maybeRegisterConnectionMetrics(channel.id());
if (idleExpiryManager != null)
idleExpiryManager.update(channel.id(), currentTimeNanos);
try {
/* complete any connections that have finished their handshake (either normally or immediately) */
if (isImmediatelyConnected || key.isConnectable()) {
if (channel.finishConnect()) {
this.connected.add(channel.id());
this.sensors.connectionCreated.record();
SocketChannel socketChannel = (SocketChannel) key.channel();
log.debug("Created socket with SO_RCVBUF = {}, SO_SNDBUF = {}, SO_TIMEOUT = {} to node {}", socketChannel.socket().getReceiveBufferSize(), socketChannel.socket().getSendBufferSize(), socketChannel.socket().getSoTimeout(), channel.id());
} else
continue;
}
/* if channel is not ready finish prepare */
if (channel.isConnected() && !channel.ready())
channel.prepare();
/* if channel is ready read from any connections that have readable data */
if (channel.ready() && key.isReadable() && !hasStagedReceive(channel)) {
NetworkReceive networkReceive;
while ((networkReceive = channel.read()) != null) addToStagedReceives(channel, networkReceive);
}
/* if channel is ready write to any sockets that have space in their buffer and for which we have data */
if (channel.ready() && key.isWritable()) {
Send send = channel.write();
if (send != null) {
this.completedSends.add(send);
this.sensors.recordBytesSent(channel.id(), send.size());
}
}
/* cancel any defunct sockets */
if (!key.isValid())
close(channel, true);
} catch (Exception e) {
String desc = channel.socketDescription();
if (e instanceof IOException)
log.debug("Connection with {} disconnected", desc, e);
else
log.warn("Unexpected error from {}; closing connection", desc, e);
close(channel, true);
}
}
}
use of java.nio.channels.SelectionKey in project kafka by apache.
the class Selector method register.
/**
* Register the nioSelector with an existing channel
* Use this on server-side, when a connection is accepted by a different thread but processed by the Selector
* Note that we are not checking if the connection id is valid - since the connection already exists
*/
public void register(String id, SocketChannel socketChannel) throws ClosedChannelException {
SelectionKey key = socketChannel.register(nioSelector, SelectionKey.OP_READ);
KafkaChannel channel = channelBuilder.buildChannel(id, key, maxReceiveSize);
key.attach(channel);
this.channels.put(id, channel);
}
Aggregations