Using RxNetty as proxy cannot scale caused by locking and waiting
Trying to use RxNetty as proxy is limited to 300 rps. trying to scale using more CPUs does not help. Experiment is a server using HttpClient to call a static json file on local nginx server. [(https://github.com/rotemfogel/RxNettyProxy)]
I am receiving 99% timeouts, getting blocked on waiting for java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject
Jprofiler Report can be found at [(https://github.com/rotemfogel/RxNettyProxy/blob/master/RxNettyProxy-Monitor.html)]
Apache Bench Test
$ ab -k -n 800 -c 8 http://localhost:80/static.json ... Benchmarking localhost (be patient) Completed 100 requests .... Finished 800 requests ... Document Path: /static.json Document Length: 1442 bytes
Concurrency Level: 8 Time taken for tests: 0.094 seconds Complete requests: 800 Failed requests: 0 Keep-Alive requests: 795 ... Requests per second: 8504.12 #/sec Time per request: 0.941 ms Time per request: 0.118 [ms](mean, across all concurrent requests) Transfer rate: 14151.14 [Kbytes/sec] received
$ ab -k -n 800 -c 8 http://localhost:9000/ Benchmarking localhost (be patient) Completed 100 requests ... Finished 800 requests ... Document Path: / Document Length: 5 bytes
Concurrency Level: 8 Time taken for tests: 2.237 seconds Complete requests: 800 Failed requests: 0 Non-2xx responses: 800 Keep-Alive requests: 800 Total transferred: 76000 bytes HTML transferred: 4000 bytes Requests per second: 357.56 #/sec Time per request: 22.374 ms Time per request: 2.797 [ms](mean, across all concurrent requests) Transfer rate: 33.17 [Kbytes/sec] received
Your example is using toBlocking for the client request. Adjusting it to just work with observables should give much better results. As a quick test adjusting your example:
class ProxyServer {
private final static Logger LOG = LoggerFactory.getLogger(ProxyServer.class);
private boolean isEmpty(CharSequence c) {
return c == null || c.length() == 0;
}
class RequestExecutor implements Transformer<ByteBuf, String> {
private final HttpClient<ByteBuf, ByteBuf> client;
public RequestExecutor(HttpClient<ByteBuf, ByteBuf> client) {
this.client = client;
}
@Override
public Observable<String> call(Observable<ByteBuf> t) {
return t.map(buf -> buf.toString(Charset.defaultCharset()))
.reduce((s1, s2) -> s1 + s2)
.flatMap(payload -> {
final HttpClientRequest<ByteBuf> request =
HttpClientRequest.<ByteBuf>create(HttpMethod.GET, "/static.json").withContent(payload);
return client.submit(request)
.timeout(10, TimeUnit.MILLISECONDS)
.flatMap(HttpClientResponse::getContent)
.map(buf -> buf.toString(Charset.defaultCharset()))
.reduce((s1, s2) -> s1 + s2);
});
}
}
private HttpServer<ByteBuf, ByteBuf> createServer(final int listenPort) {
HttpClient<ByteBuf, ByteBuf> client = RxNetty.<ByteBuf, ByteBuf> createHttpClient("127.0.0.1", 54321);
HttpServer<ByteBuf, ByteBuf> server = new HttpServerBuilder<ByteBuf, ByteBuf>(listenPort,
(request, response) -> {
return request.getContent().compose(new RequestExecutor(client)).flatMap(r -> {
if (isEmpty(r)) {
response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
}
else {
response.setStatus(HttpResponseStatus.OK);
response.writeString(r);
}
return response.close();
});
}).build();
LOG.info("Proxy Server started on port 9000");
return server;
}
public static void main(final String[] args) {
new ProxyServer().createServer(9000).startAndWait();
}
}
nginx directly:
$ ab -k -n 800 -c 8 http://127.0.0.1:54321/static.json
This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Finished 800 requests
Server Software: nginx/1.8.0
Server Hostname: 127.0.0.1
Server Port: 54321
Document Path: /static.json
Document Length: 3 bytes
Concurrency Level: 8
Time taken for tests: 0.064 seconds
Complete requests: 800
Failed requests: 0
Keep-Alive requests: 792
Total transferred: 194360 bytes
HTML transferred: 2400 bytes
Requests per second: 12476.41 [#/sec] (mean)
Time per request: 0.641 [ms] (mean)
Time per request: 0.080 [ms] (mean, across all concurrent requests)
Transfer rate: 2960.10 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.5 0 6
Processing: 0 1 0.1 1 2
Waiting: 0 1 0.1 1 2
Total: 0 1 0.5 1 7
Percentage of the requests served within a certain time (ms)
50% 1
66% 1
75% 1
80% 1
90% 1
95% 1
98% 1
99% 3
100% 7 (longest request)
proxy
$ ab -k -n 800 -c 8 http://127.0.0.1:9000/static.json
This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Finished 800 requests
Server Software:
Server Hostname: 127.0.0.1
Server Port: 9000
Document Path: /static.json
Document Length: 13 bytes
Concurrency Level: 8
Time taken for tests: 0.126 seconds
Complete requests: 800
Failed requests: 0
Keep-Alive requests: 800
Total transferred: 67200 bytes
HTML transferred: 10400 bytes
Requests per second: 6353.95 [#/sec] (mean)
Time per request: 1.259 [ms] (mean)
Time per request: 0.157 [ms] (mean, across all concurrent requests)
Transfer rate: 521.22 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.5 0 6
Processing: 1 1 0.3 1 5
Waiting: 1 1 0.3 1 5
Total: 1 1 0.6 1 7
Percentage of the requests served within a certain time (ms)
50% 1
66% 1
75% 1
80% 1
90% 1
95% 2
98% 2
99% 5
100% 7 (longest request)
@brharrington :+1: .toBlocking() blocks the eventloop.
How can i call the external service and still wait on the reply? בתאריך 19 בפבר' 2016 19:08, "Nitesh Kant" [email protected] כתב:
@brharrington https://github.com/brharrington [image: :+1:] .toBlocking() blocks the eventloop.
— Reply to this email directly or view it on GitHub https://github.com/ReactiveX/RxNetty/issues/481#issuecomment-186307806.
For your usecase, you do not need to wait for the reply. As @brharrington mentioned, you can modify the code to write the response back, as and when it arrives asynchronously.
Thanks guys. I'll try your method.
Using @brharrington 's code I was able to increase the throughput, however it is not predictable. When running several tests (Apache Bench) throughput is flactuating between 700 and 5000 rps. Is that normal?
@rotemfogel typically I would warm up the server and there will be some variance post warm up but not that much. We did some benchmarks a while back and you can find the results + steps here:
https://github.com/Netflix-Skunkworks/WsPerfLab
My case is actually more complex than a proxy server. I receive a json request and call an external service which receives query params. When converting to suggested code, I do not receive a single valid response. I always receive an empty response When stepping into the call (in debug mode) everything works. [(https://github.com/rotemfogel/RxNettyProxy/blob/master/src/main/java/io/proxy/transformers/ExternalRequestExecutor.java)]
Thanks
The code you have pointed to is incorrect as you expect that when the code gets here, the response is received and populated in sResp. This is a race and in most cases, it won't happen that the response is received in that time. You should compose the async response (eg: via a flatmap instead of map) in order to correctly read the response completely before parsing.
If you are new to Rx, I would recommend getting up to speed with RxJava first before trying RxNetty out.
refactored my code to transformers. on the rxclient side, using flatMap solved the problem. works like a charm. Thanks @NiteshKant @brharrington