-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: persist replication state for replica PSYNC after restart #4451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: unstable
Are you sure you want to change the base?
Changes from all commits
9fed86b
1eac2be
ae949b0
c30c8d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7333,10 +7333,45 @@ int checkForSentinelMode(int argc, char **argv, char *exec_name) { | |
| void loadDataFromDisk(void) { | ||
| ustime_t start = ustime(); | ||
| if (server.aof_state == AOF_ON) { | ||
| int ret = loadAppendOnlyFiles(server.aof_manifest); | ||
| rdbSaveInfo rsi = RDB_SAVE_INFO_INIT; | ||
| int rsi_is_valid = 0; | ||
| int ret = loadAppendOnlyFiles(server.aof_manifest, &rsi); | ||
| if (ret == AOF_FAILED || ret == AOF_OPEN_ERR) exit(1); | ||
| if (ret != AOF_NOT_EXIST) | ||
| if (ret != AOF_NOT_EXIST) { | ||
| serverLog(LL_NOTICE, "DB loaded from append only file: %.3f seconds", (float)(ustime() - start) / 1000000); | ||
| /* Restore the replication ID / offset from the AOF file's RDB preamble. */ | ||
| if (rsi.repl_id_is_set && rsi.repl_offset != -1 && | ||
| rsi.repl_stream_db != -1) { | ||
| rsi_is_valid = 1; | ||
| if (!iAmPrimary()) { | ||
| memcpy(server.replid, rsi.repl_id, sizeof(server.replid)); | ||
| server.primary_repl_offset = rsi.repl_offset; | ||
| /* If this is a replica, create a cached primary from this | ||
| * information, in order to allow partial resynchronizations | ||
| * with primaries. */ | ||
| replicationCachePrimaryUsingMyself(); | ||
| selectDb(server.cached_primary, rsi.repl_stream_db); | ||
| } else { | ||
| /* If this is a primary, we can save the replication info | ||
| * as secondary ID and offset, in order to allow replicas | ||
| * to partial resynchronizations with primaries. */ | ||
| memcpy(server.replid2, rsi.repl_id, sizeof(server.replid)); | ||
| server.second_replid_offset = rsi.repl_offset + 1; | ||
| /* Rebase primary_repl_offset from rsi.repl_offset. */ | ||
| server.primary_repl_offset += rsi.repl_offset; | ||
| serverAssert(server.repl_backlog); | ||
| server.repl_backlog->offset = server.primary_repl_offset - server.repl_backlog->histlen + 1; | ||
| rebaseReplicationBuffer(rsi.repl_offset); | ||
| server.repl_no_replicas_since = time(NULL); | ||
| } | ||
| } | ||
| } | ||
| /* We always create replication backlog if server is a primary, we need | ||
| * it because we put DELs in it when loading expired keys in RDB, but | ||
| * if AOF doesn't have replication info or there is no AOF, it is not | ||
| * possible to support partial resynchronization, to avoid extra memory | ||
| * of replication backlog, we drop it. */ | ||
| if (!rsi_is_valid && server.repl_backlog) freeReplicationBacklog(); | ||
|
Comment on lines
+7336
to
+7374
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Add an AOF restart and PSYNC integration test. Add a Tcl integration test under As per coding guidelines, 🤖 Prompt for AI AgentsSource: Coding guidelines |
||
| } else { | ||
| rdbSaveInfo rsi = RDB_SAVE_INFO_INIT; | ||
| int rsi_is_valid = 0; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3316,7 +3316,7 @@ void flushAppendOnlyFile(int force); | |
| void feedAppendOnlyFile(int dictid, robj **argv, int argc); | ||
| void aofRemoveTempFile(pid_t childpid, int from_signal); | ||
| int rewriteAppendOnlyFileBackground(void); | ||
| int loadAppendOnlyFiles(aofManifest *am); | ||
| int loadAppendOnlyFiles(aofManifest *am, rdbSaveInfo *rsi); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This signature change leaves |
||
| void stopAppendOnly(void); | ||
| int startAppendOnly(void); | ||
| int restartAOFWithSyncRdb(void); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
Do not replace an unknown replication-stream DB with DB 0.
repl_stream_dbmust match the DB selected in the primary replication stream. Line 4299 sets it to 0 when the active and cached primary clients are absent.If the saved offset follows commands in another DB, startup restores
server.cached_primaryin DB 0. A partial resynchronization can then apply a later command before anySELECTcommand arrives. The replica will write that command to the wrong DB.Persist the last known primary stream DB outside the transient client. If that value is unavailable, return
NULLand use full synchronization.🤖 Prompt for AI Agents