Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit46f63ec

Browse files
committed
Start dynamic pool workers
1 parent4f5d440 commit46f63ec

File tree

6 files changed

+69
-24
lines changed

6 files changed

+69
-24
lines changed

‎contrib/mmts/bgwpool.c‎

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,15 @@
77
#include"storage/spin.h"
88
#include"storage/pg_sema.h"
99
#include"storage/shmem.h"
10+
#include"datatype/timestamp.h"
1011

1112
#include"bgwpool.h"
1213

1314
boolMtmIsLogicalReceiver;
15+
intMtmMaxWorkers;
1416

15-
typedefstruct
17+
staticvoidBgwPoolMainLoop(BgwPool*pool)
1618
{
17-
BgwPoolConstructorconstructor;
18-
intid;
19-
}BgwPoolExecutorCtx;
20-
21-
staticvoidBgwPoolMainLoop(Datumarg)
22-
{
23-
BgwPoolExecutorCtx*ctx= (BgwPoolExecutorCtx*)arg;
24-
intid=ctx->id;
25-
BgwPool*pool=ctx->constructor();
2619
intsize;
2720
void*work;
2821

@@ -58,7 +51,7 @@ static void BgwPoolMainLoop(Datum arg)
5851
pool->lastPeakTime=0;
5952
}
6053
SpinLockRelease(&pool->lock);
61-
pool->executor(id,work,size);
54+
pool->executor(work,size);
6255
free(work);
6356
SpinLockAcquire(&pool->lock);
6457
pool->active-=1;
@@ -84,6 +77,7 @@ void BgwPoolInit(BgwPool* pool, BgwPoolExecutor executor, char const* dbname, c
8477
pool->pending=0;
8578
pool->nWorkers=nWorkers;
8679
pool->lastPeakTime=0;
80+
pool->lastDynamicWorkerStartTime=0;
8781
strncpy(pool->dbname,dbname,MAX_DBNAME_LEN);
8882
strncpy(pool->dbuser,dbuser,MAX_DBUSER_LEN);
8983
}
@@ -93,6 +87,17 @@ timestamp_t BgwGetLastPeekTime(BgwPool* pool)
9387
returnpool->lastPeakTime;
9488
}
9589

90+
staticvoidBgwPoolStaticWorkerMainLoop(Datumarg)
91+
{
92+
BgwPoolConstructorconstructor= (BgwPoolConstructor)DatumGetPointer(arg);
93+
BgwPoolMainLoop(constructor());
94+
}
95+
96+
staticvoidBgwPoolDynamicWorkerMainLoop(Datumarg)
97+
{
98+
BgwPoolMainLoop((BgwPool*)DatumGetPointer(arg));
99+
}
100+
96101
voidBgwPoolStart(intnWorkers,BgwPoolConstructorconstructor)
97102
{
98103
inti;
@@ -101,15 +106,12 @@ void BgwPoolStart(int nWorkers, BgwPoolConstructor constructor)
101106
MemSet(&worker,0,sizeof(BackgroundWorker));
102107
worker.bgw_flags=BGWORKER_SHMEM_ACCESS |BGWORKER_BACKEND_DATABASE_CONNECTION;
103108
worker.bgw_start_time=BgWorkerStart_ConsistentState;
104-
worker.bgw_main=BgwPoolMainLoop;
109+
worker.bgw_main=BgwPoolStaticWorkerMainLoop;
105110
worker.bgw_restart_time=MULTIMASTER_BGW_RESTART_TIMEOUT;
106111

107112
for (i=0;i<nWorkers;i++) {
108-
BgwPoolExecutorCtx*ctx= (BgwPoolExecutorCtx*)malloc(sizeof(BgwPoolExecutorCtx));
109113
snprintf(worker.bgw_name,BGW_MAXLEN,"bgw_pool_worker_%d",i+1);
110-
ctx->id=i;
111-
ctx->constructor=constructor;
112-
worker.bgw_main_arg= (Datum)ctx;
114+
worker.bgw_main_arg=PointerGetDatum(constructor);
113115
RegisterBackgroundWorker(&worker);
114116
}
115117
}
@@ -124,14 +126,36 @@ size_t BgwPoolGetQueueSize(BgwPool* pool)
124126
}
125127

126128

129+
staticvoidBgwStartExtraWorker(BgwPool*pool)
130+
{
131+
if (pool->nWorkers<MtmMaxWorkers) {
132+
timestamp_tnow=MtmGetSystemTime();
133+
if (pool->lastDynamicWorkerStartTime+MULTIMASTER_BGW_RESTART_TIMEOUT*USECS_PER_SEC<now) {
134+
BackgroundWorkerworker;
135+
BackgroundWorkerHandle*handle;
136+
MemSet(&worker,0,sizeof(BackgroundWorker));
137+
worker.bgw_flags=BGWORKER_SHMEM_ACCESS |BGWORKER_BACKEND_DATABASE_CONNECTION;
138+
worker.bgw_start_time=BgWorkerStart_ConsistentState;
139+
worker.bgw_main=BgwPoolDynamicWorkerMainLoop;
140+
worker.bgw_restart_time=MULTIMASTER_BGW_RESTART_TIMEOUT;
141+
snprintf(worker.bgw_name,BGW_MAXLEN,"bgw_pool_dynworker_%d", (int)++pool->nWorkers);
142+
worker.bgw_main_arg=PointerGetDatum(pool);
143+
pool->lastDynamicWorkerStartTime=now;
144+
if (!RegisterDynamicBackgroundWorker(&worker,&handle)) {
145+
elog(WARNING,"Failed to start dynamic background worker");
146+
}
147+
}
148+
}
149+
}
150+
127151
voidBgwPoolExecute(BgwPool*pool,void*work,size_tsize)
128152
{
129153
if (size+4>pool->size) {
130154
/*
131155
* Size of work is larger than size of shared buffer:
132156
* run it immediately
133157
*/
134-
pool->executor(0,work,size);
158+
pool->executor(work,size);
135159
return;
136160
}
137161

@@ -149,6 +173,9 @@ void BgwPoolExecute(BgwPool* pool, void* work, size_t size)
149173
SpinLockAcquire(&pool->lock);
150174
}else {
151175
pool->pending+=1;
176+
if (pool->active==pool->nWorkers) {
177+
BgwStartExtraWorker(pool);
178+
}
152179
if (pool->lastPeakTime==0&&pool->active==pool->nWorkers&&pool->pending!=0) {
153180
pool->lastPeakTime=MtmGetSystemTime();
154181
}

‎contrib/mmts/bgwpool.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include"storage/spin.h"
66
#include"storage/pg_sema.h"
77

8-
typedefvoid(*BgwPoolExecutor)(intid,void*work,size_tsize);
8+
typedefvoid(*BgwPoolExecutor)(void*work,size_tsize);
99

1010
typedefuint64timestamp_t;
1111

@@ -16,7 +16,8 @@ typedef uint64 timestamp_t;
1616
externtimestamp_tMtmGetSystemTime(void);/* non-adjusted current system time */
1717
externtimestamp_tMtmGetCurrentTime(void);/* adjusted current system time */
1818

19-
externboolMtmIsLogicalReceiver;
19+
externboolMtmIsLogicalReceiver;
20+
externintMtmMaxWorkers;
2021

2122
typedefstruct
2223
{
@@ -31,6 +32,7 @@ typedef struct
3132
size_tpending;
3233
size_tnWorkers;
3334
time_tlastPeakTime;
35+
timestamp_tlastDynamicWorkerStartTime;
3436
boolproducerBlocked;
3537
chardbname[MAX_DBNAME_LEN];
3638
chardbuser[MAX_DBUSER_LEN];

‎contrib/mmts/multimaster.c‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,7 +2371,7 @@ _PG_init(void)
23712371

23722372
DefineCustomIntVariable(
23732373
"multimaster.workers",
2374-
"Number of multimaster executor workers per node",
2374+
"Number of multimaster executor workers",
23752375
NULL,
23762376
&MtmWorkers,
23772377
8,
@@ -2384,6 +2384,21 @@ _PG_init(void)
23842384
NULL
23852385
);
23862386

2387+
DefineCustomIntVariable(
2388+
"multimaster.max_workers",
2389+
"Maximal number of multimaster dynamic executor workers",
2390+
NULL,
2391+
&MtmMaxWorkers,
2392+
100,
2393+
0,
2394+
INT_MAX,
2395+
PGC_BACKEND,
2396+
0,
2397+
NULL,
2398+
NULL,
2399+
NULL
2400+
);
2401+
23872402
DefineCustomIntVariable(
23882403
"multimaster.vacuum_delay",
23892404
"Minimal age of records which can be vacuumed (seconds)",
@@ -2920,6 +2935,7 @@ mtm_get_snapshot(PG_FUNCTION_ARGS)
29202935
PG_RETURN_INT64(MtmTx.snapshot);
29212936
}
29222937

2938+
29232939
Datum
29242940
mtm_get_last_csn(PG_FUNCTION_ARGS)
29252941
{
@@ -3786,7 +3802,7 @@ void MtmExecute(void* work, int size)
37863802
{
37873803
if (Mtm->status==MTM_RECOVERY) {
37883804
/* During recovery apply changes sequentially to preserve commit order */
3789-
MtmExecutor(0,work,size);
3805+
MtmExecutor(work,size);
37903806
}else {
37913807
BgwPoolExecute(&Mtm->pool,work,size);
37923808
}

‎contrib/mmts/multimaster.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ extern void MtmJoinTransaction(GlobalTransactionId* gtid, csn_t snapshot);
301301
externvoidMtmReceiverStarted(intnodeId);
302302
externMtmReplicationModeMtmGetReplicationMode(intnodeId,sig_atomic_tvolatile*shutdown);
303303
externvoidMtmExecute(void*work,intsize);
304-
externvoidMtmExecutor(intid,void*work,size_tsize);
304+
externvoidMtmExecutor(void*work,size_tsize);
305305
externvoidMtmSend2PCMessage(MtmTransState*ts,MtmMessageCodecmd);
306306
externvoidMtmSendMessage(MtmArbiterMessage*msg);
307307
externvoidMtmAdjustSubtransactions(MtmTransState*ts);

‎contrib/mmts/pglogical_apply.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ process_remote_delete(StringInfo s, Relation rel)
953953

954954
staticMemoryContextApplyContext;
955955

956-
voidMtmExecutor(intid,void*work,size_tsize)
956+
voidMtmExecutor(void*work,size_tsize)
957957
{
958958
StringInfoDatas;
959959
Relationrel=NULL;

‎contrib/mmts/pglogical_receiver.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ pglogical_receiver_main(Datum main_arg)
529529
}else {
530530
if (MtmPreserveCommitOrder&&buf.used==rc-hdr_len) {
531531
/* Perform commit-prepared and rollback-prepared requested directly in receiver */
532-
MtmExecutor(nodeId,buf.data,buf.used);
532+
MtmExecutor(buf.data,buf.used);
533533
}else {
534534
MtmExecute(buf.data,buf.used);
535535
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp