简体   繁体   English

在并发测试下Java NIO的管道错误

[英]Broken pipe error of Java NIO under concurrent test

I am now building an event-driven server, using Java NIO 我现在正在使用Java NIO构建事件驱动的服务器

When I did ab -n 100 -c 100 localhost:8080/ , everything is fine. 当我执行ab -n 100 -c 100 localhost:8080/ ,一切都很好。 But when I did ab -n 10000 -c 1000 localhost:8080/ , error occurred!! 但是当我执行ab -n 10000 -c 1000 localhost:8080/ ,发生了错误!

The part of the code: 代码部分:

resHeader.append("Host: MyServer" + CRLF);
resHeader.append("Connection: close" + CRLF + CRLF);
ByteBuffer temp = ByteBuffer.wrap(resHeader.toString().getBytes());--(ClientHandler.java:372)
while(temp.hasRemaining()) {
    client.write(temp);
}

The definition of client: client = (SocketChannel) key.channel(); 客户client = (SocketChannel) key.channel();的定义: client = (SocketChannel) key.channel();

java.io.IOException: Broken pipe
    at sun.nio.ch.FileDispatcher.write0(Native Method)
    at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:29)
    at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:69)
    at sun.nio.ch.IOUtil.write(IOUtil.java:40)
    at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:336)
    at ClientHandler.sendResponse(ClientHandler.java:372)
    at ClientHandler.parseRequest(ClientHandler.java:218)
    at ClientHandler.writeKey(ClientHandler.java:136)
    at ClientHandler.execute(ClientHandler.java:51)
    at TcpServer.main(TcpServer.java:33)

After the error, I add this to the code: 错误后,我将其添加到代码中:

ByteBuffer temp = null;
try {
    temp = ByteBuffer.wrap(resHeader.toString().getBytes());
    while(temp.hasRemaining()) {
        client.write(temp);
    }
} catch (IOException e) {
    System.err.println("TEST: " + temp.hasRemaining());
    System.out.println("connect:" + client.isConnected()
    // TODO Auto-generated catch block
    e.printStackTrace();
}

And when the errors pop up again, it prints "TEST: true" and "connect:true" which means that it does not finish writing. 当错误再次弹出时,它会打印"TEST: true""connect:true" ,这表示它还没有完成写入。 Why this happened? 为什么会这样?

Also, I used ab -c 1000 -n 10000 -r http://localhost:8080/ 另外,我使用了ab -c 1000 -n 10000 -r http://localhost:8080/

-r : Don't exit on socket receive errors.

    Concurrency Level:      1000
    Time taken for tests:   18.700 seconds
    Complete requests:      10000
    Failed requests:        1878

   (Connect: 0, Receive: 626, Length: 626, Exceptions: 626)

(PS: Using this -r I did not get the errors: java.io.IOException: Broken pipe ) (PS:使用此-r我没有得到错误: java.io.IOException: Broken pipe

Does this provide some useful information? 这是否提供一些有用的信息?

'Broken pipe' means that the peer closed the connection prematurely. “管道中断”表示对等端过早关闭了连接。 It could do that under load for any number of reasons, none of which have anything to do with your code. 出于多种原因,它可以在负载下执行此操作,而这些原因均与您的代码无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM