From 95497d07d32090d095d02ca9a296518224b7b108 Mon Sep 17 00:00:00 2001 From: cca5507 Date: Mon, 19 Jun 2023 10:40:53 +0800 Subject: [PATCH] =?UTF-8?q?thread=20pool=20worker=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E9=80=80=E5=87=BA=E6=97=B6=E6=B8=85=E7=90=86=E5=85=B1=E4=BA=AB?= =?UTF-8?q?=E5=86=85=E5=AD=98=E6=95=B0=E7=BB=84=E4=B8=AD=E7=9A=84=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E6=A7=BD=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gausskernel/process/postmaster/pgstat.cpp | 74 ++++++++++++++----- 1 file changed, 54 insertions(+), 20 deletions(-) diff --git a/src/gausskernel/process/postmaster/pgstat.cpp b/src/gausskernel/process/postmaster/pgstat.cpp index 6b0b2f54a..5e21138ab 100644 --- a/src/gausskernel/process/postmaster/pgstat.cpp +++ b/src/gausskernel/process/postmaster/pgstat.cpp @@ -3302,6 +3302,33 @@ void pgstat_couple_decouple_session(bool is_couple) pgstat_increment_changecount_after(beentry); } +static void clear_backend_entry(volatile PgBackendStatus* beentry) +{ + /* + * Clear status entry, following the protocol of bumping st_changecount + * before and after. We use a volatile pointer here to ensure the + * compiler doesn't try to get cute. + */ + pgstat_increment_changecount_before(beentry); + + beentry->st_procpid = 0; /* mark pid invalid */ + beentry->st_sessionid = 0; /* mark sessionid invalid */ + beentry->globalSessionId.sessionId = 0; + beentry->globalSessionId.nodeId = 0; + beentry->globalSessionId.seq = 0; + + /* + * make sure st_changecount is an even before release it. + * + * In case some thread was interrupted by SIGTERM at any time with a mess st_changecount + * in PgBackendStatus, PgstatCollectorMain may hang-up in waiting its change to even and + * can not exit after receiving SIGTERM signal + */ + do { + pgstat_increment_changecount_after(beentry); + } while ((beentry->st_changecount & 1) != 0); +} + /* * Shut down a single backend's statistics reporting at process exit. * @@ -3328,29 +3355,18 @@ static void pgstat_beshutdown_hook(int code, Datum arg) if (OidIsValid(u_sess->proc_cxt.MyDatabaseId)) pgstat_report_stat(true); - /* - * Clear my status entry, following the protocol of bumping st_changecount - * before and after. We use a volatile pointer here to ensure the - * compiler doesn't try to get cute. - */ - pgstat_increment_changecount_before(beentry); - - beentry->st_procpid = 0; /* mark pid invalid */ - beentry->st_sessionid = 0; /* mark sessionid invalid */ - beentry->globalSessionId.sessionId = 0; - beentry->globalSessionId.nodeId = 0; - beentry->globalSessionId.seq = 0; + /* Clear my status entry */ + clear_backend_entry(beentry); /* - * make sure st_changecount is an even before release it. - * - * In case some thread was interrupted by SIGTERM at any time with a mess st_changecount - * in PgBackendStatus, PgstatCollectorMain may hang-up in waiting its change to even and - * can not exit after receiving SIGTERM signal + * Thread pool worker also needs to clear + * t_thrd.shemem_ptr_cxt.BackendStatusArray[t_thrd.proc_cxt.MyBackendId - 1] */ - do { - pgstat_increment_changecount_after(beentry); - } while ((beentry->st_changecount & 1) != 0); + if (IS_THREAD_POOL_WORKER && t_thrd.proc_cxt.MyBackendId != InvalidBackendId && + beentry != &t_thrd.shemem_ptr_cxt.BackendStatusArray[t_thrd.proc_cxt.MyBackendId - 1]) { + clear_backend_entry(&t_thrd.shemem_ptr_cxt.BackendStatusArray[t_thrd.proc_cxt.MyBackendId - 1]); + WaitUntilLWLockInfoNeverAccess(&t_thrd.shemem_ptr_cxt.BackendStatusArray[t_thrd.proc_cxt.MyBackendId - 1]); + } /* * handle below cases: @@ -8267,6 +8283,12 @@ static void endMySessionTimeEntry(int code, Datum arg) { DetachMySessionTimeEntry(t_thrd.shemem_ptr_cxt.mySessionTimeEntry); + if (IS_THREAD_POOL_WORKER && t_thrd.proc_cxt.MyBackendId != InvalidBackendId && + t_thrd.shemem_ptr_cxt.mySessionTimeEntry != + &t_thrd.shemem_ptr_cxt.sessionTimeArray[t_thrd.proc_cxt.MyBackendId - 1]) { + DetachMySessionTimeEntry(&t_thrd.shemem_ptr_cxt.sessionTimeArray[t_thrd.proc_cxt.MyBackendId - 1]); + } + /* don't bother this entry any more. */ t_thrd.shemem_ptr_cxt.mySessionTimeEntry = NULL; } @@ -8509,6 +8531,12 @@ static void endMySessionStatEntry(int code, Datum arg) /* mark my entry not active. */ t_thrd.shemem_ptr_cxt.mySessionStatEntry->isValid = false; + if (IS_THREAD_POOL_WORKER && t_thrd.proc_cxt.MyBackendId != InvalidBackendId && + t_thrd.shemem_ptr_cxt.mySessionStatEntry != + &t_thrd.shemem_ptr_cxt.sessionStatArray[t_thrd.proc_cxt.MyBackendId - 1]) { + t_thrd.shemem_ptr_cxt.sessionStatArray[t_thrd.proc_cxt.MyBackendId - 1].isValid = false; + } + /* don't bother this entry any more. */ t_thrd.shemem_ptr_cxt.mySessionStatEntry = NULL; } @@ -8744,6 +8772,12 @@ static void endMySessionMemoryEntry(int code, Datum arg) /* mark my entry not active. */ t_thrd.shemem_ptr_cxt.mySessionMemoryEntry->isValid = false; + if (IS_THREAD_POOL_WORKER && t_thrd.proc_cxt.MyBackendId != InvalidBackendId && + t_thrd.shemem_ptr_cxt.mySessionMemoryEntry != + &t_thrd.shemem_ptr_cxt.sessionMemoryArray[t_thrd.proc_cxt.MyBackendId - 1]) { + t_thrd.shemem_ptr_cxt.sessionMemoryArray[t_thrd.proc_cxt.MyBackendId - 1].isValid = false; + } + /* don't bother this entry any more. */ t_thrd.shemem_ptr_cxt.mySessionMemoryEntry = NULL; }