Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1f91118

Browse files
committed
Fix workers launch
1 parent 29e644f commit 1f91118

File tree

2 files changed

+45
-44
lines changed

2 files changed

+45
-44
lines changed

jsonbd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ typedef struct jsonbd_shm_hdr
3737
sem_t workers_sem[MAX_DATABASES];
3838
volatile int workers_ready;
3939
jsonbd_shm_worker launcher;
40+
Latch launcher_latch;
4041
} jsonbd_shm_hdr;
4142

4243
/* CACHE */

jsonbd_worker.c

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ Oid jsonbd_keys_indoid = InvalidOid;
4646
Oid jsonbd_id_indoid = InvalidOid;
4747

4848
void jsonbd_worker_main(Datum arg);
49-
void jsonbd_worker_launcher(void);
49+
void jsonbd_launcher_main(Datum arg);
5050
static Oid jsonbd_get_dictionary_relid(void);
51-
static bool jsonbd_register_worker(int worker_num, Oid dboid);
51+
static bool jsonbd_register_worker(int, Oid, volatile Latch *);
5252

5353
#define JSONBD_DICTIONARY_REL "jsonbd_dictionary"
5454

@@ -121,16 +121,25 @@ get_cached_compression_options(Oid cmoptoid)
121121
}
122122

123123
static void
124-
init_local_variables(int worker_num, Oid dboid)
124+
init_worker(dsm_segment *seg)
125125
{
126126
HASHCTL hash_ctl;
127+
jsonbd_worker_args *worker_args;
127128

128129
shm_toc *toc = shm_toc_attach(JSONBD_SHM_MQ_MAGIC, workers_data);
129130
jsonbd_shm_hdr *hdr = shm_toc_lookup(toc, 0, false);
131+
132+
worker_args = (jsonbd_worker_args *) dsm_segment_address(seg);
133+
134+
/* increase global count of started workers */
130135
hdr->workers_ready++;
131136

132-
worker_state = shm_toc_lookup(toc, worker_num, false);
137+
/* Connect to our database */
138+
BackgroundWorkerInitializeConnectionByOid(worker_args->dboid, InvalidOid);
139+
140+
worker_state = shm_toc_lookup(toc, worker_args->worker_num, false);
133141
worker_state->proc = MyProc;
142+
worker_state->dboid = worker_args->dboid;
134143

135144
/* input mq */
136145
shm_mq_set_receiver(worker_state->mqin, MyProc);
@@ -165,7 +174,13 @@ init_local_variables(int worker_num, Oid dboid)
165174
HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
166175

167176
elog(LOG, "jsonbd dictionary worker %d started with pid: %d",
168-
worker_num, MyProcPid);
177+
worker_args->worker_num, MyProcPid);
178+
179+
/* We don't need this segment anymore */
180+
dsm_detach(seg);
181+
182+
/* Set launcher free */
183+
SetLatch(&hdr->launcher_latch);
169184
}
170185

171186
static void
@@ -519,7 +534,7 @@ jsonbd_cmd_get_keys(int nkeys, Oid cmoptoid, uint32 *ids)
519534
}
520535

521536
void
522-
jsonbd_worker_launcher(void)
537+
jsonbd_launcher_main(Datum arg)
523538
{
524539
shm_toc *toc;
525540
jsonbd_shm_hdr *hdr;
@@ -534,15 +549,19 @@ jsonbd_worker_launcher(void)
534549
/* We're now ready to receive signals */
535550
BackgroundWorkerUnblockSignals();
536551

552+
/* Init this launcher as backend so workers can notify it */
553+
InitPostgres(NULL, InvalidOid, NULL, InvalidOid, NULL);
554+
537555
/* Create resource owner */
538-
CurrentResourceOwner = ResourceOwnerCreate(NULL, "jsonbd_worker_launcher");
556+
CurrentResourceOwner = ResourceOwnerCreate(NULL, "jsonbd_launcher_main");
539557

540558
/* Init launcher state */
541559
Assert(workers_data != NULL);
542560
toc = shm_toc_attach(JSONBD_SHM_MQ_MAGIC, workers_data);
543561
hdr = shm_toc_lookup(toc, 0, false);
544562
worker_state = &hdr->launcher;
545563

564+
InitLatch(&hdr->launcher_latch);
546565
shm_mq_set_receiver(worker_state->mqin, MyProc);
547566
shm_mq_set_sender(worker_state->mqout, MyProc);
548567

@@ -575,12 +594,18 @@ jsonbd_worker_launcher(void)
575594
dboid = *((Oid *) data);
576595

577596
/* start workers for specified database */
578-
for (i=0; i < jsonbd_nworkers; i++)
597+
for (i=0; i < jsonbd_nworkers; i++, worker_num++)
579598
{
580599
bool res;
581-
res = jsonbd_register_worker(worker_num++, dboid);
600+
jsonbd_shm_worker *wd;
601+
602+
res = jsonbd_register_worker(worker_num, dboid, &hdr->launcher_latch);
582603
if (res)
583604
started++;
605+
606+
/* point worker semaphore to database semaphore */
607+
wd = shm_toc_lookup(toc, worker_num, false);
608+
wd->dbsem = &hdr->workers_sem[database_num];
584609
}
585610
}
586611

@@ -596,6 +621,8 @@ jsonbd_worker_launcher(void)
596621

597622
/* we report ok if only one worker has started */
598623
resmq = shm_mq_sendv(mqh, &((shm_mq_iovec) {"y", 2}), 1, false);
624+
625+
database_num += 1;
599626
}
600627
else
601628
resmq = shm_mq_sendv(mqh, &((shm_mq_iovec) {"n", 2}), 1, false);
@@ -604,7 +631,6 @@ jsonbd_worker_launcher(void)
604631
elog(NOTICE, "jsonbd: backend detached early");
605632

606633
shm_mq_detach(mqh);
607-
MemoryContextReset(worker_context);
608634

609635
/* mark we need new handle */
610636
mqh = NULL;
@@ -629,12 +655,7 @@ jsonbd_worker_launcher(void)
629655
void
630656
jsonbd_worker_main(Datum arg)
631657
{
632-
Oid dboid;
633-
int worker_num;
634-
635658
dsm_segment *seg;
636-
jsonbd_worker_args *worker_args;
637-
PGPROC *starter;
638659
shm_mq_handle *mqh = NULL;
639660

640661
/* Establish signal handlers before unblocking signals */
@@ -643,31 +664,12 @@ jsonbd_worker_main(Datum arg)
643664
/* We're now ready to receive signals */
644665
BackgroundWorkerUnblockSignals();
645666

646-
/* Initialize connection */
647-
seg = dsm_attach((dsm_handle) DatumGetInt32(arg));
648-
worker_args = (jsonbd_worker_args *) dsm_segment_address(seg);
649-
worker_num = worker_args->worker_num;
650-
dboid = worker_args->dboid;
651-
652-
/* Connect to our database */
653-
BackgroundWorkerInitializeConnectionByOid(dboid, InvalidOid);
654-
655667
/* Create resource owner */
656668
CurrentResourceOwner = ResourceOwnerCreate(NULL, "jsonbd_worker");
657-
init_local_variables(worker_num, dboid);
658669

659-
/* We don't need this segment anymore */
660-
dsm_detach(seg);
661-
662-
starter = BackendPidGetProc(MyBgworkerEntry->bgw_notify_pid);
663-
if (starter == NULL)
664-
{
665-
elog(LOG, "launcher has exited prematurely");
666-
goto end;
667-
}
668-
669-
/* Set launcher free */
670-
SetLatch(&starter->procLatch);
670+
/* Initialize connection and local variables */
671+
seg = dsm_attach((dsm_handle) DatumGetInt32(arg));
672+
init_worker(seg);
671673

672674
MemoryContextSwitchTo(worker_context);
673675

@@ -758,13 +760,12 @@ jsonbd_worker_main(Datum arg)
758760
ResetLatch(&MyProc->procLatch);
759761
}
760762

761-
end:
762763
elog(LOG, "jsonbd dictionary worker has ended its work");
763764
proc_exit(0);
764765
}
765766

766767
static bool
767-
jsonbd_register_worker(int worker_num, Oid dboid)
768+
jsonbd_register_worker(int worker_num, Oid dboid, volatile Latch *launcher_latch)
768769
{
769770
pid_t pid;
770771
dsm_segment *seg;
@@ -811,13 +812,12 @@ jsonbd_register_worker(int worker_num, Oid dboid)
811812

812813
/* Wait to be signalled. */
813814
#if PG_VERSION_NUM >= 100000
814-
WaitLatch(MyLatch, WL_LATCH_SET, 0, PG_WAIT_EXTENSION);
815+
WaitLatch(launcher_latch, WL_LATCH_SET, 0, PG_WAIT_EXTENSION);
815816
#else
816-
WaitLatch(MyLatch, WL_LATCH_SET, 0);
817+
WaitLatch(launcher_latch, WL_LATCH_SET, 0);
817818
#endif
818819

819-
/* Reset the latch so we don't spin. */
820-
ResetLatch(MyLatch);
820+
ResetLatch(launcher_latch);
821821

822822
/* Remove the segment */
823823
dsm_detach(seg);
@@ -839,7 +839,7 @@ jsonbd_register_launcher(void)
839839
worker.bgw_restart_time = 0;
840840
worker.bgw_notify_pid = 0;
841841
memcpy(worker.bgw_library_name, "jsonbd", BGW_MAXLEN);
842-
memcpy(worker.bgw_function_name, CppAsString(jsonbd_worker_launcher), BGW_MAXLEN);
842+
memcpy(worker.bgw_function_name, CppAsString(jsonbd_launcher_main), BGW_MAXLEN);
843843
snprintf(worker.bgw_name, BGW_MAXLEN, "jsonbd launcher");
844844
worker.bgw_main_arg = (Datum) Int32GetDatum(0);
845845
RegisterBackgroundWorker(&worker);

0 commit comments

Comments
 (0)