user1462089 user1462089 - 4 months ago 13
Java Question

NIO blocking write not working

I have recently been writing a Java NIO based server with non-blocking sockets and I've bumped in to some problems in regarding writing the data out. I know by now that there are conditions where non-blocking write fails to write some, or all of the bytes in a ByteBuffer.

The way I currently handle such scenario is by either rewinding or compacting the buffer and later trying to send it again in next selection iteration. This how ever results in significant performance loss and it is imperative that I get the data sent fast.

I have attempted using something like:

ByteBuffer bb = ...;
SocketChannel sc = ...;
while(bb.remaining() > 0) {
sc.write(bb);
}


But the problem with this is the fact that it might write 0 bytes and still quit the while loop. I'm not sure why, but it seems like write() - method will hit ByteBuffer's limit regardless of whether it actually sent all the bytes or not.

Another problem I've had with this writing method is when it sometimes under heavy load will cause a buffer overflow exception even when I'm not attempting a blocking write.

I desperately need some advice on how to properly perform blocking write and what condition might cause SocketChannel.write(ByteBuffer) to overflow the buffer(should it not stop when limit is hit?).

Thanks in advance.

Edit: I still have not found the reason why sc.write(bb) would set position in buffer to bb.limit() even if it wrote 0 bytes. My only resort remain to be rewinding the buffer after failed write attempt.

Answer

When using non blocking IO you typically either after low latency or high throughput.

Do not move data inside the buffer by compacting it, rather allocate buffers of the required lengths (to fit one message typically, message header separately). And dispose them after having fully written the contents to the socket. You can use pools of buffers of 2x growing sizes if you cannot accept GC.

If throughput is your major concern then do not try writing to the socket directly, but rather register for socket writability with Selector and try writing when socket is writable. To achieve this you need to maintain a queue of buffers pending to be sent. Stay registered for the socket writability until this queue becomes empty. You should preferrably be using scatter/gather type IO in this case as it would minimize number of syscalls in the application.

write requester thread:
  ioloop.submit(buffer[]{msgheaderbuf, msgbodybuf}, sock);
selector thread (ioloop):
  submit(buffer[] bufs, socket sock):
    queue.enqueue(bufs);
    selector.register(sock, WRITABLE);
    selector.wakeup();

If latency is important then first try to write to the socket directly and only if write fails enqueue the data and register for the socket writability as I described above. This will require additional mutex to protect the socket from being written both directly and from within the selector thread (as a result of writability event triggering).

write requester thread:
  ioloop.submit(buffer[]{msgheaderbuf, msgbodybuf}, sock);
selector thread (ioloop):
  submit(buffer[] bufs, socket sock):
    size = sock.write(bufs);
    while (!bufs.empty() && !bufs[0].remaining()): bufs.pop_front();
    if (bufs.empty()) return;

    queue.enqueue(bufs);
    selector.register(sock, WRITABLE);
    selector.wakeup();