Write a sequence of commands able to fully rebuild the dataset into * "filename". Used both by REWRITEAOF and BGREWRITEAOF. * * In order to minimize the number of commands needed in the rewritten * log Redis uses variadic commands when possible, such as RPUSH, SADD * and ZADD. However at max AOF_REWRITE_ITEMS_PER_CMD items per time * are inserted using a single command. */
| 1544 | * and ZADD. However at max AOF_REWRITE_ITEMS_PER_CMD items per time |
| 1545 | * are inserted using a single command. */ |
| 1546 | int rewriteAppendOnlyFile(char *filename) { |
| 1547 | rio aof; |
| 1548 | FILE *fp = NULL; |
| 1549 | char tmpfile[256]; |
| 1550 | char byte; |
| 1551 | |
| 1552 | /* Note that we have to use a different temp name here compared to the |
| 1553 | * one used by rewriteAppendOnlyFileBackground() function. */ |
| 1554 | snprintf(tmpfile,256,"temp-rewriteaof-%d.aof", (int) getpid()); |
| 1555 | fp = fopen(tmpfile,"w"); |
| 1556 | if (!fp) { |
| 1557 | serverLog(LL_WARNING, "Opening the temp file for AOF rewrite in rewriteAppendOnlyFile(): %s", strerror(errno)); |
| 1558 | return C_ERR; |
| 1559 | } |
| 1560 | |
| 1561 | server.aof_child_diff = sdsempty(); |
| 1562 | rioInitWithFile(&aof,fp); |
| 1563 | |
| 1564 | if (server.aof_rewrite_incremental_fsync) |
| 1565 | rioSetAutoSync(&aof,REDIS_AUTOSYNC_BYTES); |
| 1566 | |
| 1567 | startSaving(RDBFLAGS_AOF_PREAMBLE); |
| 1568 | |
| 1569 | if (server.aof_use_rdb_preamble) { |
| 1570 | int error; |
| 1571 | if (rdbSaveRio(&aof,&error,RDBFLAGS_AOF_PREAMBLE,NULL) == C_ERR) { |
| 1572 | errno = error; |
| 1573 | goto werr; |
| 1574 | } |
| 1575 | } else { |
| 1576 | if (rewriteAppendOnlyFileRio(&aof) == C_ERR) goto werr; |
| 1577 | } |
| 1578 | |
| 1579 | /* Do an initial slow fsync here while the parent is still sending |
| 1580 | * data, in order to make the next final fsync faster. */ |
| 1581 | if (fflush(fp) == EOF) goto werr; |
| 1582 | if (fsync(fileno(fp)) == -1) goto werr; |
| 1583 | |
| 1584 | /* Read again a few times to get more data from the parent. |
| 1585 | * We can't read forever (the server may receive data from clients |
| 1586 | * faster than it is able to send data to the child), so we try to read |
| 1587 | * some more data in a loop as soon as there is a good chance more data |
| 1588 | * will come. If it looks like we are wasting time, we abort (this |
| 1589 | * happens after 20 ms without new data). */ |
| 1590 | int nodata = 0; |
| 1591 | mstime_t start = mstime(); |
| 1592 | while(mstime()-start < 1000 && nodata < 20) { |
| 1593 | if (aeWait(server.aof_pipe_read_data_from_parent, AE_READABLE, 1) <= 0) |
| 1594 | { |
| 1595 | nodata++; |
| 1596 | continue; |
| 1597 | } |
| 1598 | nodata = 0; /* Start counting from zero, we stop on N *contiguous* |
| 1599 | timeouts. */ |
| 1600 | aofReadDiffFromParent(); |
| 1601 | } |
| 1602 | |
| 1603 | /* Ask the master to stop sending diffs. */ |
no test coverage detected