1
votes

I am using a java NIO based which accept connections from clients(configured non blocking) and only reads data sent by clients. Clients once connected will stick to server for a long time, so i used a single thread for "selector.select" and "Accept", but the clients connected will send messages every 15 sec and number of clients are 5000, each message is of size 150 Bytes.

Instead of creating a new thread for each read from clients i decided to have a thread pool of 100 threads, but server is not able to read data from all clients it simply hangs. When a new thread is created each time it is able to read data from all clients.

Here is my ReadEvent thread

class ReadEvent implements Runnable {

    private static Logger logger = Logger.getLogger(ReadEvent.class.getName());
    private SelectionKey key;

    /**
     * Constructor to initialize the thread with the key having read event
     * pending.
     * 
     * @param key
     *            SelectionKey having read event.
     **/
    public ReadEvent(SelectionKey key) {
        this.key = key;
    }

    /**
     * Method to read the data from the key.
     **/
    @Override
    public void run() {
        SocketChannel socketChannel = (SocketChannel) key.channel();
        synchronized (socketChannel) {
            if (socketChannel.isOpen()) {
                try {
                    ByteBuffer readBuffer = ByteBuffer.allocate(150);
                    int numRead = 0;
                    try {
                        /* ".read" is nonblocking */
                        numRead = socketChannel.read(readBuffer);
                        /*
                         * Some other IO error on reading socketChannel.
                         */
                    }
                    catch (IOException e) {
                        logger.debug(
                            "[run] Connection abruptly terminated from client",
                            e);
                        key.channel().close();
                        return;
                    }
                    if (numRead == -1) {// socket closed cleanly
                        key.channel().close();
                        return;
                    }
                    String data = null;
                    data = new String(readBuffer.array(),
                        Charset.forName("ASCII"));
                    /* Send the read data to the DataDispatcher Actor */
                    Main.getDataDispatcher().tell(data, ActorRef.noSender());
                }
                catch (IOException e) {
                    logger.debug("[run] ", e);
                    return;
                }
            }
            else {// socketChannel is closed
                try {
                    key.channel().close();// Sanitary close operation
                    return;
                }
                catch (IOException e) {
                }
            }
        }
    }
}

I can't figure out the overload on thread pool, any suggestions on implementation of ReadThread will help me.

UPDATE 1 : java.lang.OutOfMemoryError on fixed thread pool

Snippet of calling read event :

Thread per read:

try {
    if (!key.isValid()) {
        continue;
    }
    if (key.isAcceptable()) {
        this.accept(key);
    }
    else if (key.isReadable()) {
        new Thread(new ReadEvent(key)).start();
    }
}
catch (CancelledKeyException e) {// key has been canceled
}

The above snippet works fine for few thousands of clients.

Using Thread pool

ExecutorService executor = Executors.newFixedThreadPool(100);

try {
    if (!key.isValid()) {
        continue;
    }
    if (key.isAcceptable()) {
        this.accept(key);
    }
    else if (key.isReadable()) {
        executor.execute(new ReadEvent(key));
    }
}
catch (CancelledKeyException e) {// key has been canceled
}

The above snippet doesn't serves all clients and found the heap size is increasing gradually and most(almost 100%) of the CPU is used for GC and finally got java.lang.OutOfMemoryError: GC overhead limit exceeded exception

1
The read() method can only block if the channel is in blocking mode. But you can't select on a channel that's in blocking mode. Ergo as stated your question doesn't make sense, or lacks some critical information. - user207421
channel is configured to be in non blocking but, but the same works for thread per read event from clients - achuth
So where do these threads hang? - user207421
I can't figure it out, because i found in debugging that ReadEvent thread normally reads the data and finishes it's job, i am still working on it to find the problem - achuth
It's an immense waste of resources: CPU, threads, stacks, ... And it consumes them at an immense rate. The thread pool alleviates it but you're still introducing massive overhead into what is after all a non-blocking operation. - user207421

1 Answers

0
votes

6.7 years later but hey I had a similar issue. The problem is until the socket is actually read [socketChannel.read(...)] the operation is deemed as valid. By submitting the SelectionKey to the thread pool, remember that the thread will execute independently from the selecting thread meaning the operation is still ready as per the selecting thread and will keep submitting the operation to your thread pool until one of the submitted threads actually reads [socketChannel.read(...)] the channel and the operation is deemed as invalid and removed from the ready set. Solution as mentioned by others already is read the bytes within the selecting thread and then pass the channel and the read contents into the thread pool for post processing.

  case SelectionKey.OP_READ: {
       SocketChannel channel = (SocketChannel) key.channel();
       try {
            ByteBuffer buffer = ByteBuffer.allocate(1024 * 8);
            int read = channel.read(buffer);// read in selecting thread
            if (read <= -1) {
                key.cancel();//Signals the channel is closed
            } else {
               threadPool.execute(() -> {
                  //pass the channel & contents into the thread pool
               });
            }
       } catch (IOException e) {
           e.printStackTrace();
           key.cancel();
       }
  }
  break;