From f3ffeaa77075917f64c3836476328fe52344a9e5 Mon Sep 17 00:00:00 2001 From: Binbin Date: Thu, 21 Nov 2024 15:22:05 +0800 Subject: [PATCH] Use goto to cleanup error handling in readSyncBulkPayload The goto error label is the same as the error return, use goto to reduce the references. ``` error: cancelReplicationHandshake(1); return; ``` Also this can make the log printing more continuous under the error, that is, we print the error log first, and then print the reconnecting log at the last (in cancelReplicationHandshake). Signed-off-by: Binbin --- src/replication.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/replication.c b/src/replication.c index a809c4c166..75f08c4c89 100644 --- a/src/replication.c +++ b/src/replication.c @@ -2091,8 +2091,7 @@ void readSyncBulkPayload(connection *conn) { } serverLog(LL_WARNING, "I/O error trying to sync with PRIMARY: %s", (nread == -1) ? connGetLastError(conn) : "connection lost"); - cancelReplicationHandshake(1); - return; + goto error; } server.stat_net_repl_input_bytes += nread; @@ -2257,7 +2256,6 @@ void readSyncBulkPayload(connection *conn) { if (loadingFailed) { stopLoading(0); - cancelReplicationHandshake(1); rioFreeConn(&rdb, NULL); if (server.repl_diskless_load == REPL_DISKLESS_LOAD_SWAPDB) { @@ -2277,7 +2275,7 @@ void readSyncBulkPayload(connection *conn) { /* Note that there's no point in restarting the AOF on SYNC * failure, it'll be restarted when sync succeeds or the replica * gets promoted. */ - return; + goto error; } /* RDB loading succeeded if we reach this point. */ @@ -2319,8 +2317,7 @@ void readSyncBulkPayload(connection *conn) { "Failed trying to sync the temp DB to disk in " "PRIMARY <-> REPLICA synchronization: %s", strerror(errno)); - cancelReplicationHandshake(1); - return; + goto error; } /* Rename rdb like renaming rewrite aof asynchronously. */ @@ -2330,9 +2327,8 @@ void readSyncBulkPayload(connection *conn) { "Failed trying to rename the temp DB into %s in " "PRIMARY <-> REPLICA synchronization: %s", server.rdb_filename, strerror(errno)); - cancelReplicationHandshake(1); if (old_rdb_fd != -1) close(old_rdb_fd); - return; + goto error; } /* Close old rdb asynchronously. */ if (old_rdb_fd != -1) bioCreateCloseJob(old_rdb_fd, 0, 0); @@ -2343,8 +2339,7 @@ void readSyncBulkPayload(connection *conn) { "Failed trying to sync DB directory %s in " "PRIMARY <-> REPLICA synchronization: %s", server.rdb_filename, strerror(errno)); - cancelReplicationHandshake(1); - return; + goto error; } /* We will soon start loading the RDB from disk, the replication history is changed, @@ -2361,7 +2356,6 @@ void readSyncBulkPayload(connection *conn) { if (rdbLoad(server.rdb_filename, &rsi, RDBFLAGS_REPLICATION) != RDB_OK) { serverLog(LL_WARNING, "Failed trying to load the PRIMARY synchronization " "DB from disk, check server logs."); - cancelReplicationHandshake(1); if (server.rdb_del_sync_files && allPersistenceDisabled()) { serverLog(LL_NOTICE, "Removing the RDB file obtained from " "the primary. This replica has persistence " @@ -2375,7 +2369,7 @@ void readSyncBulkPayload(connection *conn) { /* Note that there's no point in restarting the AOF on sync failure, it'll be restarted when sync succeeds or replica promoted. */ - return; + goto error; } /* Cleanup. */