From 2de5bab368fc1ed7630935df8762e3a2679b3453 Mon Sep 17 00:00:00 2001 From: antirez Date: Sat, 5 Jul 2014 15:40:08 +0200 Subject: Better "final read from parent" algorithm in rewriteAppendOnlyFile*(. We now wait up to 1 second for diff data to come from the parent, however we use poll(2) to wait for more data, and use a counter of contiguous failures to get data for N times (set to 20 experimentally after different tests) as an early stop condition to avoid wasting 1 second when the write traffic is too low. --- src/aof.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/aof.c b/src/aof.c index 536238d86..f9bc75e8a 100644 --- a/src/aof.c +++ b/src/aof.c @@ -1059,17 +1059,20 @@ int rewriteAppendOnlyFile(char *filename) { /* Read again a few times to get more data from the parent. * We can't read forever (the server may receive data from clients * fater than it is able to send data to the child), so we try to read - * some more data in a loop and wait a bit if data is not available, but - * don't wait more than 100 ms total. */ + * some more data in a loop as soon as there is a good chance more data + * will come. If it looks like we are wasting time, we abort (this + * happens after 20 ms without new data). */ int nodata = 0; - for (j = 0; j < 100; j++) { - ssize_t nread_from_parent = aofReadDiffFromParent(); - printf("%lld\n", (long long) nread_from_parent); - if (nread_from_parent == 0) { + mstime_t start = mstime(); + while(mstime()-start < 1000 && nodata < 20) { + if (aeWait(server.aof_pipe_read_data_from_parent, AE_READABLE, 1) <= 0) + { nodata++; - if (nodata == 10) break; - usleep(10000); + continue; } + nodata = 0; /* Start counting from zero, we stop on N *contiguous* + timeouts. */ + aofReadDiffFromParent(); } /* Ask the master to stop sending diffs. */ -- cgit v1.2.1