Skip to content

Commit 5905387

Browse files
committed
use rdb to startAppendonly after primary-replica full sync
Signed-off-by: Ray Cao <zisong.cw@alibaba-inc.com>
1 parent d56a1f7 commit 5905387

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

src/aof.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,81 @@ int startAppendOnly(void) {
10051005
return C_OK;
10061006
}
10071007

1008+
int startAppendOnlyWithRdb(void) {
1009+
serverAssert(server.aof_state == AOF_OFF);
1010+
1011+
int newfd = -1;
1012+
sds new_base_filename = NULL;
1013+
sds new_base_filepath = NULL;
1014+
sds new_incr_filename = NULL;
1015+
sds new_incr_filepath = NULL;
1016+
aofManifest *temp_am = NULL;
1017+
1018+
if (dirCreateIfMissing(server.aof_dirname) == -1) {
1019+
serverLog(LL_WARNING, "Can't open or create append-only dir %s: %s", server.aof_dirname, strerror(errno));
1020+
goto cleanup;
1021+
}
1022+
1023+
serverAssert(server.aof_manifest != NULL);
1024+
1025+
/* Dup a temporary aof_manifest for subsequent modifications. */
1026+
temp_am = aofManifestDup(server.aof_manifest);
1027+
1028+
/* Get a new BASE file name and mark the previous (if we have)
1029+
* as the HISTORY type. */
1030+
new_base_filename = getNewBaseFileNameAndMarkPreAsHistory(temp_am, server.aof_use_rdb_preamble);
1031+
serverAssert(new_base_filename != NULL);
1032+
new_base_filepath = makePath(server.aof_dirname, new_base_filename);
1033+
if (rename(server.rdb_filename, new_base_filepath) == -1) {
1034+
serverLog(LL_WARNING, "Error trying to rename the RDB file %s into %s: %s", server.rdb_filename,
1035+
new_base_filepath, strerror(errno));
1036+
goto cleanup;
1037+
}
1038+
1039+
/* Get next new incr aof name. */
1040+
new_incr_filename = getNewIncrAofName(temp_am);
1041+
new_incr_filepath = makePath(server.aof_dirname, new_incr_filename);
1042+
newfd = open(new_incr_filepath, O_WRONLY | O_TRUNC | O_CREAT, 0644);
1043+
sdsfree(new_incr_filepath);
1044+
if (newfd == -1) {
1045+
serverLog(LL_WARNING, "Can't open the append-only file %s: %s", new_incr_filename, strerror(errno));
1046+
goto cleanup;
1047+
}
1048+
1049+
/* Change the AOF file type in 'incr_aof_list' from AOF_FILE_TYPE_INCR
1050+
* to AOF_FILE_TYPE_HIST, and move them to the 'history_aof_list'. */
1051+
markRewrittenIncrAofAsHistory(temp_am);
1052+
1053+
/* Persist AOF Manifest. */
1054+
if (persistAofManifest(temp_am) == C_ERR) {
1055+
goto cleanup;
1056+
}
1057+
aofManifestFreeAndUpdate(temp_am);
1058+
1059+
aofDelHistoryFiles();
1060+
1061+
/* Reset the aof_last_incr_size. */
1062+
server.aof_last_incr_size = 0;
1063+
/* Reset the aof_last_incr_fsync_offset. */
1064+
server.aof_last_incr_fsync_offset = 0;
1065+
1066+
server.aof_fd = newfd;
1067+
server.aof_rewrite_base_size = getAppendOnlyFileSize(new_base_filename, NULL);
1068+
server.aof_current_size = server.aof_rewrite_base_size;
1069+
server.aof_state = AOF_ON;
1070+
1071+
return C_OK;
1072+
1073+
cleanup:
1074+
if (temp_am) aofManifestFree(temp_am);
1075+
if (new_base_filename) sdsfree(new_base_filename);
1076+
if (new_base_filepath) sdsfree(new_base_filepath);
1077+
if (new_incr_filename) sdsfree(new_incr_filename);
1078+
if (new_incr_filepath) sdsfree(new_incr_filepath);
1079+
if (newfd != -1) close(newfd);
1080+
return C_ERR;
1081+
}
1082+
10081083
/* This is a wrapper to the write syscall in order to retry on short writes
10091084
* or if the syscall gets interrupted. It could look strange that we retry
10101085
* on short writes given that we are writing to a block device: normally if

src/replication.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,8 +2473,12 @@ void readSyncBulkPayload(connection *conn) {
24732473
goto error;
24742474
}
24752475

2476+
if (server.aof_enabled && server.aof_use_rdb_preamble) {
2477+
startAppendOnlyWithRdb();
2478+
}
2479+
24762480
/* Cleanup. */
2477-
if (server.rdb_del_sync_files && allPersistenceDisabled()) {
2481+
if (server.aof_enabled && server.aof_state == AOF_OFF && server.rdb_del_sync_files && allPersistenceDisabled()) {
24782482
serverLog(LL_NOTICE, "Removing the RDB file obtained from "
24792483
"the primary. This replica has persistence "
24802484
"disabled");
@@ -2524,7 +2528,7 @@ void readSyncBulkPayload(connection *conn) {
25242528
/* Restart the AOF subsystem now that we finished the sync. This
25252529
* will trigger an AOF rewrite, and when done will start appending
25262530
* to the new file. */
2527-
if (server.aof_enabled) restartAOFAfterSYNC();
2531+
if (server.aof_enabled && server.aof_state == AOF_OFF) restartAOFAfterSYNC();
25282532

25292533
/* In case of dual channel replication sync we want to close the RDB connection
25302534
* once the connection is established */
@@ -3872,7 +3876,6 @@ int connectWithPrimary(void) {
38723876
return C_ERR;
38733877
}
38743878

3875-
38763879
server.repl_transfer_lastio = server.unixtime;
38773880
server.repl_state = REPL_STATE_CONNECTING;
38783881
serverLog(LL_NOTICE, "PRIMARY <-> REPLICA sync started");

src/server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2984,6 +2984,7 @@ int rewriteAppendOnlyFileBackground(void);
29842984
int loadAppendOnlyFiles(aofManifest *am);
29852985
void stopAppendOnly(void);
29862986
int startAppendOnly(void);
2987+
int startAppendOnlyWithRdb(void);
29872988
void backgroundRewriteDoneHandler(int exitcode, int bysignal);
29882989
void killAppendOnlyChild(void);
29892990
void restartAOFAfterSYNC(void);

0 commit comments

Comments
 (0)