@@ -60,7 +60,7 @@ static uint32 decode_varbyte(unsigned char *ptr);
60
60
static size_t
61
61
jsonbd_get_queue_size (void )
62
62
{
63
- return (Size ) (jsonbd_queue_size * 1024 );
63
+ return (Size ) (shm_mq_minimum_size + jsonbd_queue_size * 1024 );
64
64
}
65
65
66
66
static size_t
@@ -74,7 +74,9 @@ jsonbd_shmem_size(void)
74
74
shm_toc_initialize_estimator (& e );
75
75
76
76
shm_toc_estimate_chunk (& e , sizeof (jsonbd_shm_hdr ));
77
- shm_toc_estimate_chunk (& e , PGSemaphoreShmemSize (1 ));
77
+
78
+ /* two queues for launcher */
79
+ shm_toc_estimate_chunk (& e , shm_mq_minimum_size * 2 );
78
80
79
81
for (i = 0 ; i < MAX_JSONBD_WORKERS ; i ++ )
80
82
{
@@ -83,8 +85,8 @@ jsonbd_shmem_size(void)
83
85
shm_toc_estimate_chunk (& e , jsonbd_get_queue_size ());
84
86
}
85
87
86
- /* 3 keys each worker + 3 for header (header itself, launcher and its two queues) */
87
- shm_toc_estimate_keys (& e , MAX_JSONBD_WORKERS * 3 + 4 );
88
+ /* 3 keys each worker + 3 for header (header itself and two queues) */
89
+ shm_toc_estimate_keys (& e , MAX_JSONBD_WORKERS * 3 + 3 );
88
90
size = shm_toc_estimate (& e );
89
91
return size ;
90
92
}
@@ -95,14 +97,13 @@ jsonbd_shmem_size(void)
95
97
* About keys in toc:
96
98
* 0 - for header
97
99
* 1..MAX_JSONBD_WORKERS - workers
98
- * MAX_JSONBD_WORKERS + 1 - launcher
99
- * MAX_JSONBD_WORKERS + 2 .. - queues
100
+ * MAX_JSONBD_WORKERS + 1 .. - queues
100
101
*/
101
102
static void
102
103
jsonbd_init_worker (shm_toc * toc , jsonbd_shm_worker * wd , int worker_num ,
103
104
size_t queue_size )
104
105
{
105
- static int mqkey = MAX_JSONBD_WORKERS + 2 ;
106
+ static int mqkey = MAX_JSONBD_WORKERS + 1 ;
106
107
107
108
/* each worker will have two mq, for input and output */
108
109
wd -> mqin = shm_mq_create (shm_toc_allocate (toc , queue_size ), queue_size );
@@ -146,15 +147,11 @@ jsonbd_shmem_startup_hook(void)
146
147
147
148
toc = shm_toc_create (JSONBD_SHM_MQ_MAGIC , workers_data , size );
148
149
149
- /* initialize header */
150
+ /* Initialize header */
150
151
hdr = shm_toc_allocate (toc , sizeof (jsonbd_shm_hdr ));
151
152
hdr -> workers_ready = 0 ;
152
- hdr -> launcher_sem = PGSemaphoreCreate ();
153
- jsonbd_init_worker (toc , & hdr -> launcher , MAX_JSONBD_WORKERS + 1 , sizeof (Oid ));
154
-
155
- for (i = 0 ; i < MAX_DATABASES ; i ++ )
156
- sem_init (& hdr -> workers_sem [i ], 1 , jsonbd_nworkers );
157
-
153
+ sem_init (& hdr -> launcher_sem , 1 , 1 );
154
+ jsonbd_init_worker (toc , & hdr -> launcher , 0 , shm_mq_minimum_size );
158
155
shm_toc_insert (toc , 0 , hdr );
159
156
160
157
for (i = 0 ; i < MAX_JSONBD_WORKERS ; i ++ )
@@ -285,6 +282,7 @@ jsonbd_communicate(shm_mq_iovec *iov, int iov_len,
285
282
int i ,
286
283
j ;
287
284
bool detached = false;
285
+ bool launch_failed = false;
288
286
bool callback_succeded = false;
289
287
shm_mq_result resmq ;
290
288
shm_mq_handle * mqh ;
@@ -321,8 +319,8 @@ jsonbd_communicate(shm_mq_iovec *iov, int iov_len,
321
319
wd = shm_toc_lookup (toc , j + 1 , false);
322
320
323
321
if (wd -> dboid != MyDatabaseId )
324
- /* somehow not all workers started for this database */
325
- break ;
322
+ /* somehow not all workers started for this database, try next */
323
+ continue ;
326
324
327
325
if (pg_atomic_test_set_flag (& wd -> busy ))
328
326
goto comm ;
@@ -337,7 +335,7 @@ jsonbd_communicate(shm_mq_iovec *iov, int iov_len,
337
335
* there are no workers for our database,
338
336
* so we should launch them using our jsonbd workers launcher
339
337
*/
340
- PGSemaphoreLock ( hdr -> launcher_sem );
338
+ sem_wait ( & hdr -> launcher_sem );
341
339
shm_mq_set_sender (hdr -> launcher .mqin , MyProc );
342
340
shm_mq_set_receiver (hdr -> launcher .mqout , MyProc );
343
341
@@ -355,15 +353,21 @@ jsonbd_communicate(shm_mq_iovec *iov, int iov_len,
355
353
if (resmq != SHM_MQ_SUCCESS )
356
354
detached = true;
357
355
356
+ if (reslen != 2 || res [0 ] == 'n' )
357
+ launch_failed = true;
358
+
358
359
shm_mq_detach (mqh );
359
360
}
360
361
361
362
shm_mq_clean_sender (hdr -> launcher .mqin );
362
363
shm_mq_clean_receiver (hdr -> launcher .mqout );
363
- PGSemaphoreUnlock ( hdr -> launcher_sem );
364
+ sem_post ( & hdr -> launcher_sem );
364
365
365
366
if (detached )
366
367
elog (ERROR , "jsonbd: workers launcher was detached" );
368
+
369
+ if (launch_failed )
370
+ elog (ERROR , "jsonbd: could not launch dictionary worker, see logs" );
367
371
}
368
372
369
373
comm :
0 commit comments