From 957e99ada0306ec1f8e5df4938ff4add25888b27 Mon Sep 17 00:00:00 2001 From: chenxiaobin19 <1025221611@qq.com> Date: Wed, 6 Dec 2023 15:18:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbuild=E8=BF=9B=E7=A8=8Bhang?= =?UTF-8?q?=E4=BD=8F=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bin/pg_basebackup/pg_basebackup.cpp | 7 ++++++- src/bin/pg_basebackup/receivelog.cpp | 13 +++++++++---- src/bin/pg_ctl/backup.cpp | 7 ++++++- src/bin/pg_ctl/receivelog.cpp | 13 +++++++++---- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/bin/pg_basebackup/pg_basebackup.cpp b/src/bin/pg_basebackup/pg_basebackup.cpp index 9a84d71e411..47613121d54 100644 --- a/src/bin/pg_basebackup/pg_basebackup.cpp +++ b/src/bin/pg_basebackup/pg_basebackup.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include "tool_common.h" #include "getopt_long.h" @@ -461,7 +462,11 @@ static void StartLogStreamer(const char *startpos, uint32 timeline, char *syside #ifndef WIN32 bgchild = fork(); if (bgchild == 0) { - /* in child process */ + /* + * In child process. + * Receive SIGKILL when main process exits. + */ + prctl(PR_SET_PDEATHSIG, SIGKILL); exit(LogStreamerMain(g_childParam)); } else if (bgchild < 0) { fprintf(stderr, _("%s: could not create background process: %s\n"), progname, strerror(errno)); diff --git a/src/bin/pg_basebackup/receivelog.cpp b/src/bin/pg_basebackup/receivelog.cpp index 7b24d987e2a..d8556b10099 100644 --- a/src/bin/pg_basebackup/receivelog.cpp +++ b/src/bin/pg_basebackup/receivelog.cpp @@ -53,6 +53,10 @@ static XLogRecPtr lastFlushPosition = InvalidXLogRecPtr; extern char* basedir; extern int standby_message_timeout; +#define HEART_BEAT_INIT 0 +#define HEART_BEAT_RUN 1 +#define HEART_BEAT_STOP 2 + /* * The max size for single data file. copy from custorage.cpp. */ @@ -64,7 +68,7 @@ const int HEART_BEAT = 5; PGconn* xlogconn = NULL; pthread_t hearbeatTimerId; volatile uint32 timerFlag = 0; -volatile uint32 heartbeatRunning = 0; +volatile uint32 heartbeatRunning = HEART_BEAT_INIT; pthread_mutex_t heartbeatMutex; pthread_mutex_t heartbeatQuitMutex; pthread_cond_t heartbeatQuitCV; @@ -235,8 +239,9 @@ void* heartbeatTimerHandler(void* data) if (xlogconn == NULL) { return NULL; } - heartbeatRunning = 1; - while (heartbeatRunning) { + uint32 expected = HEART_BEAT_INIT; + (void)pg_atomic_compare_exchange_u32(&heartbeatRunning, &expected, HEART_BEAT_RUN); + while (pg_atomic_read_u32(&heartbeatRunning) == HEART_BEAT_RUN) { pthread_mutex_lock(&heartbeatMutex); (void)checkForReceiveTimeout(xlogconn); ping_sent = false; @@ -280,7 +285,7 @@ void suspendHeartBeatTimer(void) void closeHearBeatTimer(void) { - heartbeatRunning = 0; + pg_atomic_write_u32(&heartbeatRunning, HEART_BEAT_STOP); pthread_mutex_unlock(&heartbeatMutex); pthread_mutex_lock(&heartbeatQuitMutex); pthread_cond_signal(&heartbeatQuitCV); diff --git a/src/bin/pg_ctl/backup.cpp b/src/bin/pg_ctl/backup.cpp index 377176f9dad..c342f6d3bc9 100755 --- a/src/bin/pg_ctl/backup.cpp +++ b/src/bin/pg_ctl/backup.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #ifdef HAVE_LIBZ #include "zlib.h" @@ -562,7 +563,11 @@ bool StartLogStreamer( fflush(stderr); bgchild = fork(); if (bgchild == 0) { - /* in child process */ + /* + * In child process. + * Receive SIGKILL when main process exits. + */ + prctl(PR_SET_PDEATHSIG, SIGKILL); exit(LogStreamerMain(param)); } else if (bgchild < 0) { pg_log(PG_WARNING, _(" could not create background process: %s.\n"), strerror(errno)); diff --git a/src/bin/pg_ctl/receivelog.cpp b/src/bin/pg_ctl/receivelog.cpp index 14b62847cc0..a172d1154a4 100644 --- a/src/bin/pg_ctl/receivelog.cpp +++ b/src/bin/pg_ctl/receivelog.cpp @@ -51,6 +51,10 @@ static bool reportFlushPosition = false; static XLogRecPtr lastFlushPosition = InvalidXLogRecPtr; extern char* basedir; +#define HEART_BEAT_INIT 0 +#define HEART_BEAT_RUN 1 +#define HEART_BEAT_STOP 2 + /* * The max size for single data file. copy from custorage.cpp. */ @@ -62,7 +66,7 @@ const int HEART_BEAT = 5; PGconn* xlogconn = NULL; pthread_t hearbeatTimerId; volatile uint32 timerFlag = 0; -volatile uint32 heartbeatRunning = 0; +volatile uint32 heartbeatRunning = HEART_BEAT_INIT; pthread_mutex_t heartbeatMutex; typedef enum { DO_WAL_DATA_WRITE_DONE, DO_WAL_DATA_WRITE_STOP, DO_WAL_DATA_WRITE_ERROR } DoWalDataWriteResult; @@ -211,8 +215,9 @@ void* heartbeatTimerHandler(void* data) if (xlogconn == NULL) { return NULL; } - heartbeatRunning = 1; - while (heartbeatRunning) { + uint32 expected = HEART_BEAT_INIT; + (void)pg_atomic_compare_exchange_u32(&heartbeatRunning, &expected, HEART_BEAT_RUN); + while (pg_atomic_read_u32(&heartbeatRunning) == HEART_BEAT_RUN) { pthread_mutex_lock(&heartbeatMutex); (void)checkForReceiveTimeout(xlogconn); ping_sent = false; @@ -256,7 +261,7 @@ void suspendHeartBeatTimer(void) void closeHearBeatTimer(void) { - heartbeatRunning = 0; + pg_atomic_write_u32(&heartbeatRunning, HEART_BEAT_STOP); pthread_mutex_unlock(&heartbeatMutex); (void)pthread_join(hearbeatTimerId, NULL); return; -- Gitee