Skip to content

Commit c0f0363

Browse files
committed
Make send timeout smaller than receive timeout
1 parent fbdc41a commit c0f0363

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

pg_query_state.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,8 @@ pg_query_state(PG_FUNCTION_ARGS)
481481
shm_mq_msg *msg;
482482
List *bg_worker_procs = NIL;
483483
List *msgs;
484-
int i;
484+
instr_time start_time;
485+
instr_time cur_time;
485486

486487
if (!module_initialized)
487488
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -514,14 +515,22 @@ pg_query_state(PG_FUNCTION_ARGS)
514515
init_lock_tag(&tag, PG_QUERY_STATE_KEY);
515516
LockAcquire(&tag, ExclusiveLock, false, false);
516517

517-
for (i = 0; pg_atomic_read_u32(&counterpart_userid->n_peers) != 0 && i <= MAX_TIMEOUT/1000; i++)
518+
INSTR_TIME_SET_CURRENT(start_time);
519+
520+
while (pg_atomic_read_u32(&counterpart_userid->n_peers) != 0)
518521
{
519522
pg_usleep(1000000); /* wait one second */
520523
CHECK_FOR_INTERRUPTS();
521-
}
522-
if (i > MAX_TIMEOUT/1000)
523-
elog(WARNING, "pg_query_state: last request was interrupted");
524524

525+
INSTR_TIME_SET_CURRENT(cur_time);
526+
INSTR_TIME_SUBTRACT(cur_time, start_time);
527+
528+
if (INSTR_TIME_GET_MILLISEC(cur_time) > MAX_RCV_TIMEOUT)
529+
{
530+
elog(WARNING, "pg_query_state: last request was interrupted");
531+
break;
532+
}
533+
}
525534
pg_atomic_write_u32(&counterpart_userid->n_peers, 1);
526535

527536
counterpart_user_id = GetRemoteBackendUserId(proc);
@@ -1023,7 +1032,7 @@ GetRemoteBackendQueryStates(PGPROC *leader,
10231032
mq_receive_result = shm_mq_receive_with_timeout(mqh,
10241033
&len,
10251034
(void **) &msg,
1026-
MAX_TIMEOUT);
1035+
MAX_RCV_TIMEOUT);
10271036
if (mq_receive_result != SHM_MQ_SUCCESS)
10281037
/* counterpart is died, not consider it */
10291038
continue;

pg_query_state.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
#define TIMINIG_OFF_WARNING 1
2323
#define BUFFERS_OFF_WARNING 2
2424

25-
#define MAX_TIMEOUT 5000 /* 5 seconds */
25+
/* Receive timeout should be larger than send timeout to let workers stop waiting before polling process */
26+
#define MAX_RCV_TIMEOUT 6000 /* 6 seconds */
27+
#define MAX_SND_TIMEOUT 3000 /* 3 seconds */
2628

2729

2830
/*

signal_handler.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ SendQueryState(void)
159159
shm_mq_handle *mqh;
160160
instr_time start_time;
161161
instr_time cur_time;
162-
long delay = MAX_TIMEOUT;
162+
long delay = MAX_SND_TIMEOUT;
163163

164164
INSTR_TIME_SET_CURRENT(start_time);
165165

@@ -179,7 +179,7 @@ SendQueryState(void)
179179
INSTR_TIME_SET_CURRENT(cur_time);
180180
INSTR_TIME_SUBTRACT(cur_time, start_time);
181181

182-
delay = MAX_TIMEOUT - (long) INSTR_TIME_GET_MILLISEC(cur_time);
182+
delay = MAX_SND_TIMEOUT - (long) INSTR_TIME_GET_MILLISEC(cur_time);
183183
if (delay <= 0)
184184
{
185185
elog(WARNING, "pg_query_state: failed to receive request from leader");

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy