HTTP Download Server takes very long to open large file
I tried to setup two HTTP Servers. Server1 is user facing, accepting REST request to download large file. Server1 then forward request to Server2 to get file. Server2 will open file from filesystem and send it back.
Results: 100MB file -> It takes 2 ~ 5 seconds to start download. 1GB file -> It takes 5 ~ 10 seconds to start download. 10GB file -> It takes 70 ~ 100 seconds to start download.
Code: vertx 3.5.1 Server1:
public HttpVerticle() {
vertx = Vertx.vertx();
Router router = Router.router(vertx);
WebClient webClient = WebClient.create(vertx,
new WebClientOptions().setDefaultPort(8081).setDefaultHost("localhost"));
router.route("/").handler(routingContext ->
routingContext.response().end("server1")
);
router.get("/file").handler(ctx -> {
HttpServerResponse response = ctx.response();
response.setChunked(true);
response.putHeader("Content-Type", "application/octet-stream");
response.putHeader("Content-Disposition", "attachment; filename=test.tar");
webClient.get("/file2").as(BodyCodec.pipe(response))
.followRedirects(false)
.timeout(600000)
.rxSend()
.subscribe(ctx1 -> {
System.out.println("subscribed");
});
});
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
Server2
public HttpVerticle() {
vertx = Vertx.vertx();
Router router = Router.router(vertx);
router.route("/").handler(routingContext ->
routingContext.response().end("server2")
);
router.get("/file2").handler(ctx -> {
HttpServerResponse response = ctx.response();
String path = "test.jar";
vertx.fileSystem()
.rxOpen(path, new OpenOptions().setRead(true))
.doOnSuccess(file -> {
response.setChunked(true);
Pump pump = Pump.pump(file, response);
pump.start();
file.endHandler(h -> response.end());
})
.subscribe(v -> {
System.out.println("Completed");
});
});
vertx.createHttpServer().requestHandler(router::accept).listen(8081);
}
can you provide a project that reproduces it ?
@vietj I just pushed the code to my git. https://github.com/qizhengyu/vertx-practise
You can run "Main.java" to start both HttpServer. http server1 bootstrap successful, listening on port: 8083 http server2 bootstrap successful, listening on port: 8084
Do a GET http://localhost/:8083/file1 This will download the test file. The checked in file is a small test.tar file under main/resources/test.tar
Replace it with a 10GB file, you will notice opening the file takes significantly long.
What surprised me is, when I'm using WebClient, I do not experience the problem even to send a 30GB file. It opens 30GB file within 10 seconds.
thanks @qizhengyu
it is not clear what should be observed and how, can you clarify ?
Server1 is behaving as a proxy, which download data from Server2 and forward to HTTP Response. Try to download a file with size 100MB, 1GB and 10GB.
Expected Result:
100MB file -> It takes 2 ~ 5 seconds to start download. 1GB file -> 2 ~5 seconds to start download. 10GB file -> 2 ~ 5 seconds to start download.
Actual Result:
100MB file -> It takes 2 ~ 5 seconds to start download. 1GB file -> It takes 5 ~ 10 seconds to start download. 10GB file -> It takes 70 ~ 100 seconds to start download.
where is your file located ?
can you tell me what you are measuring exactly and how ?
here are my experiments with your code:
First time server2: the 7 seconds are taken to copy the file from src/main/resources/test.tar to the .vertx/file-cache-02e0a89c-d7f5-461a-80b6-3fd388a68141 cache directory (because it is served from classpath) which is a file copy operation.
curl http://localhost:8084/file2
time_connect: 0.005031
time_appconnect: 0.000000
time_pretransfer: 0.005061
time_starttransfer: 7.478691
time_total: 23.644636
the second time it is faster, specially the starttransfer since the file is already copied
curl http://localhost:8084/file2
time_connect: 0.004726
time_appconnect: 0.000000
time_pretransfer: 0.004777
time_starttransfer: 0.010173
time_total: 14.042477
now trying the proxy server : almost the same time
curl http://localhost:8083/file1
time_connect: 0.005551
time_appconnect: 0.000000
time_pretransfer: 0.005601
time_starttransfer: 0.076903
time_total: 16.209920
so I'm going to close this issue, reopen if you think there is a real issue
@vietj You file is too small.
Try a 10GB test.tar file with curl http://localhost:8083/file1.
You will notice, server1 takes a very long time to start stream the file.
my file was 3GB
I will try with 10GB but I doubt it will make a difference
MacBook-Pro-de-julien:resources julien$ ls -l
total 8
-rw------- 1 julien staff 3221225472 May 7 08:56 test.tar
You should observe that time_starttransfer: 7.478691 will increase linearly. But 3GB for 7s is really fast, 1GB initially took almost 5s for Chrome to start download during my test.
Also, I tested this on Chrome. I doubt if browser will make any difference(maybe memory usage) compare to curl
with 10GB:
MacBook-Pro-de-julien:vertx-practise julien$ curl -w "@curl-format.txt" -o /dev/null -s "http://localhost:8084/file2"
time_connect: 0.004789
time_appconnect: 0.000000
time_pretransfer: 0.004889
time_starttransfer: 22.791036
----------
time_total: 75.680637
MacBook-Pro-de-julien:vertx-practise julien$ curl -w "@curl-format.txt" -o /dev/null -s "http://localhost:8084/file2"
time_connect: 0.005075
time_appconnect: 0.000000
time_pretransfer: 0.005117
time_starttransfer: 0.009983
----------
time_total: 55.827024
MacBook-Pro-de-julien:vertx-practise julien$ curl -w "@curl-format.txt" -o /dev/null -s "http://localhost:8083/file1"
time_connect: 0.004350
time_appconnect: 0.000000
time_pretransfer: 0.004380
time_starttransfer: 0.078352
----------
time_total: 59.958323
- 3GB
- time to copy file from classpath: 7.478691
- total time : 16.209920
- 10GB
- time to copy file from classpath: 22.791036
- total time : 59.958323
if you want to avoid to pay the copy then don't use classpath resource resolver and use real files with an absolute path.
This is my testing project to use classpath. In my product, I'm pulling data from an absolute path. I have a 60s timeout to start streaming on proxy server. It still timeouts for a 10GB file.
Just wondering, should we expect the time to start streaming grows linearly? From your testing result, 3G -> 7.5s and 10G -> 22.8s. That's linear. I'm expected 10G to start within a little more than 7.5s, not 22s.
but I think that you are seing this in a browser because you are using chunked encoding maybe ? have you look in Chrome at the "time to first byte" TTFB ?
you should also give a try to cURL.
Should I set chunked = true or false for streaming?
I am still not able to download large file using webclient.