PostgreSQL Source Code  git master
parallel.c File Reference
#include "postgres_fe.h"
#include <sys/select.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include "fe_utils/string_utils.h"
#include "parallel.h"
#include "pg_backup_utils.h"
#include "port/pg_bswap.h"
Include dependency graph for parallel.c:

Go to the source code of this file.

Data Structures

struct  ParallelSlot
 
struct  ShutdownInformation
 
struct  DumpSignalInformation
 

Macros

#define PIPE_READ   0
 
#define PIPE_WRITE   1
 
#define NO_SLOT   (-1) /* Failure result for GetIdleWorker() */
 
#define WORKER_IS_RUNNING(workerStatus)    ((workerStatus) == WRKR_IDLE || (workerStatus) == WRKR_WORKING)
 
#define pgpipe(a)   pipe(a)
 
#define piperead(a, b, c)   read(a,b,c)
 
#define pipewrite(a, b, c)   write(a,b,c)
 
#define write_stderr(str)
 
#define messageStartsWith(msg, prefix)    (strncmp(msg, prefix, strlen(prefix)) == 0)
 

Typedefs

typedef struct ShutdownInformation ShutdownInformation
 
typedef struct DumpSignalInformation DumpSignalInformation
 

Enumerations

enum  T_WorkerStatus { WRKR_NOT_STARTED = 0 , WRKR_IDLE , WRKR_WORKING , WRKR_TERMINATED }
 

Functions

static ParallelSlotGetMyPSlot (ParallelState *pstate)
 
static void archive_close_connection (int code, void *arg)
 
static void ShutdownWorkersHard (ParallelState *pstate)
 
static void WaitForTerminatingWorkers (ParallelState *pstate)
 
static void set_cancel_handler (void)
 
static void set_cancel_pstate (ParallelState *pstate)
 
static void set_cancel_slot_archive (ParallelSlot *slot, ArchiveHandle *AH)
 
static void RunWorker (ArchiveHandle *AH, ParallelSlot *slot)
 
static int GetIdleWorker (ParallelState *pstate)
 
static bool HasEveryWorkerTerminated (ParallelState *pstate)
 
static void lockTableForWorker (ArchiveHandle *AH, TocEntry *te)
 
static void WaitForCommands (ArchiveHandle *AH, int pipefd[2])
 
static bool ListenToWorkers (ArchiveHandle *AH, ParallelState *pstate, bool do_wait)
 
static char * getMessageFromLeader (int pipefd[2])
 
static void sendMessageToLeader (int pipefd[2], const char *str)
 
static int select_loop (int maxFd, fd_set *workerset)
 
static char * getMessageFromWorker (ParallelState *pstate, bool do_wait, int *worker)
 
static void sendMessageToWorker (ParallelState *pstate, int worker, const char *str)
 
static char * readMessageFromPipe (int fd)
 
void init_parallel_dump_utils (void)
 
void on_exit_close_archive (Archive *AHX)
 
static void sigTermHandler (SIGNAL_ARGS)
 
void set_archive_cancel_info (ArchiveHandle *AH, PGconn *conn)
 
ParallelStateParallelBackupStart (ArchiveHandle *AH)
 
void ParallelBackupEnd (ArchiveHandle *AH, ParallelState *pstate)
 
static void buildWorkerCommand (ArchiveHandle *AH, TocEntry *te, T_Action act, char *buf, int buflen)
 
static void parseWorkerCommand (ArchiveHandle *AH, TocEntry **te, T_Action *act, const char *msg)
 
static void buildWorkerResponse (ArchiveHandle *AH, TocEntry *te, T_Action act, int status, char *buf, int buflen)
 
static int parseWorkerResponse (ArchiveHandle *AH, TocEntry *te, const char *msg)
 
void DispatchJobForTocEntry (ArchiveHandle *AH, ParallelState *pstate, TocEntry *te, T_Action act, ParallelCompletionPtr callback, void *callback_data)
 
bool IsEveryWorkerIdle (ParallelState *pstate)
 
void WaitForWorkers (ArchiveHandle *AH, ParallelState *pstate, WFW_WaitOption mode)
 

Variables

static ShutdownInformation shutdown_info
 
static volatile DumpSignalInformation signal_info
 

Macro Definition Documentation

◆ messageStartsWith

#define messageStartsWith (   msg,
  prefix 
)     (strncmp(msg, prefix, strlen(prefix)) == 0)

Definition at line 226 of file parallel.c.

◆ NO_SLOT

#define NO_SLOT   (-1) /* Failure result for GetIdleWorker() */

Definition at line 72 of file parallel.c.

◆ pgpipe

#define pgpipe (   a)    pipe(a)

Definition at line 137 of file parallel.c.

◆ PIPE_READ

#define PIPE_READ   0

Definition at line 69 of file parallel.c.

◆ PIPE_WRITE

#define PIPE_WRITE   1

Definition at line 70 of file parallel.c.

◆ piperead

#define piperead (   a,
  b,
  c 
)    read(a,b,c)

Definition at line 138 of file parallel.c.

◆ pipewrite

#define pipewrite (   a,
  b,
  c 
)    write(a,b,c)

Definition at line 139 of file parallel.c.

◆ WORKER_IS_RUNNING

#define WORKER_IS_RUNNING (   workerStatus)     ((workerStatus) == WRKR_IDLE || (workerStatus) == WRKR_WORKING)

Definition at line 83 of file parallel.c.

◆ write_stderr

#define write_stderr (   str)
Value:
do { \
const char *str_ = (str); \
int rc_; \
rc_ = write(fileno(stderr), str_, strlen(str_)); \
(void) rc_; \
} while (0)
const char * str
#define write(a, b, c)
Definition: win32.h:14

Definition at line 184 of file parallel.c.

Typedef Documentation

◆ DumpSignalInformation

◆ ShutdownInformation

Enumeration Type Documentation

◆ T_WorkerStatus

Enumerator
WRKR_NOT_STARTED 
WRKR_IDLE 
WRKR_WORKING 
WRKR_TERMINATED 

Definition at line 75 of file parallel.c.

76 {
77  WRKR_NOT_STARTED = 0,
78  WRKR_IDLE,
T_WorkerStatus
Definition: parallel.c:76
@ WRKR_WORKING
Definition: parallel.c:79
@ WRKR_IDLE
Definition: parallel.c:78
@ WRKR_TERMINATED
Definition: parallel.c:80
@ WRKR_NOT_STARTED
Definition: parallel.c:77

Function Documentation

◆ archive_close_connection()

static void archive_close_connection ( int  code,
void *  arg 
)
static

Definition at line 339 of file parallel.c.

340 {
342 
343  if (si->pstate)
344  {
345  /* In parallel mode, must figure out who we are */
346  ParallelSlot *slot = GetMyPSlot(si->pstate);
347 
348  if (!slot)
349  {
350  /*
351  * We're the leader. Forcibly shut down workers, then close our
352  * own database connection, if any.
353  */
355 
356  if (si->AHX)
357  DisconnectDatabase(si->AHX);
358  }
359  else
360  {
361  /*
362  * We're a worker. Shut down our own DB connection if any. On
363  * Windows, we also have to close our communication sockets, to
364  * emulate what will happen on Unix when the worker process exits.
365  * (Without this, if this is a premature exit, the leader would
366  * fail to detect it because there would be no EOF condition on
367  * the other end of the pipe.)
368  */
369  if (slot->AH)
370  DisconnectDatabase(&(slot->AH->public));
371 
372 #ifdef WIN32
373  closesocket(slot->pipeRevRead);
374  closesocket(slot->pipeRevWrite);
375 #endif
376  }
377  }
378  else
379  {
380  /* Non-parallel operation: just kill the leader DB connection */
381  if (si->AHX)
382  DisconnectDatabase(si->AHX);
383  }
384 }
static ParallelSlot * GetMyPSlot(ParallelState *pstate)
Definition: parallel.c:264
static void ShutdownWorkersHard(ParallelState *pstate)
Definition: parallel.c:395
void DisconnectDatabase(Archive *AHX)
Definition: pg_backup_db.c:225
void * arg
#define closesocket
Definition: port.h:349
ArchiveHandle * AH
Definition: parallel.c:101
int pipeRevRead
Definition: parallel.c:105
int pipeRevWrite
Definition: parallel.c:106
ParallelState * pstate
Definition: parallel.c:148

References ParallelSlot::AH, ShutdownInformation::AHX, arg, closesocket, DisconnectDatabase(), GetMyPSlot(), ParallelSlot::pipeRevRead, ParallelSlot::pipeRevWrite, ShutdownInformation::pstate, _archiveHandle::public, and ShutdownWorkersHard().

Referenced by on_exit_close_archive().

◆ buildWorkerCommand()

static void buildWorkerCommand ( ArchiveHandle AH,
TocEntry te,
T_Action  act,
char *  buf,
int  buflen 
)
static

Definition at line 1108 of file parallel.c.

1110 {
1111  if (act == ACT_DUMP)
1112  snprintf(buf, buflen, "DUMP %d", te->dumpId);
1113  else if (act == ACT_RESTORE)
1114  snprintf(buf, buflen, "RESTORE %d", te->dumpId);
1115  else
1116  Assert(false);
1117 }
#define Assert(condition)
Definition: c.h:858
@ ACT_RESTORE
@ ACT_DUMP
static char * buf
Definition: pg_test_fsync.c:73
#define snprintf
Definition: port.h:238

References ACT_DUMP, ACT_RESTORE, Assert, buf, _tocEntry::dumpId, and snprintf.

Referenced by DispatchJobForTocEntry().

◆ buildWorkerResponse()

static void buildWorkerResponse ( ArchiveHandle AH,
TocEntry te,
T_Action  act,
int  status,
char *  buf,
int  buflen 
)
static

Definition at line 1156 of file parallel.c.

1158 {
1159  snprintf(buf, buflen, "OK %d %d %d",
1160  te->dumpId,
1161  status,
1162  status == WORKER_IGNORED_ERRORS ? AH->public.n_errors : 0);
1163 }
#define WORKER_IGNORED_ERRORS
int n_errors
Definition: pg_backup.h:238

References buf, _tocEntry::dumpId, Archive::n_errors, _archiveHandle::public, snprintf, and WORKER_IGNORED_ERRORS.

Referenced by WaitForCommands().

◆ DispatchJobForTocEntry()

void DispatchJobForTocEntry ( ArchiveHandle AH,
ParallelState pstate,
TocEntry te,
T_Action  act,
ParallelCompletionPtr  callback,
void *  callback_data 
)

Definition at line 1205 of file parallel.c.

1211 {
1212  int worker;
1213  char buf[256];
1214 
1215  /* Get a worker, waiting if none are idle */
1216  while ((worker = GetIdleWorker(pstate)) == NO_SLOT)
1217  WaitForWorkers(AH, pstate, WFW_ONE_IDLE);
1218 
1219  /* Construct and send command string */
1220  buildWorkerCommand(AH, te, act, buf, sizeof(buf));
1221 
1222  sendMessageToWorker(pstate, worker, buf);
1223 
1224  /* Remember worker is busy, and which TocEntry it's working on */
1225  pstate->parallelSlot[worker].workerStatus = WRKR_WORKING;
1226  pstate->parallelSlot[worker].callback = callback;
1227  pstate->parallelSlot[worker].callback_data = callback_data;
1228  pstate->te[worker] = te;
1229 }
void WaitForWorkers(ArchiveHandle *AH, ParallelState *pstate, WFW_WaitOption mode)
Definition: parallel.c:1451
static int GetIdleWorker(ParallelState *pstate)
Definition: parallel.c:1236
static void buildWorkerCommand(ArchiveHandle *AH, TocEntry *te, T_Action act, char *buf, int buflen)
Definition: parallel.c:1108
#define NO_SLOT
Definition: parallel.c:72
static void sendMessageToWorker(ParallelState *pstate, int worker, const char *str)
Definition: parallel.c:1644
@ WFW_ONE_IDLE
Definition: parallel.h:34
ParallelCompletionPtr callback
Definition: parallel.c:98
void * callback_data
Definition: parallel.c:99
T_WorkerStatus workerStatus
Definition: parallel.c:95
TocEntry ** te
Definition: parallel.h:59
ParallelSlot * parallelSlot
Definition: parallel.h:60
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:46

References buf, buildWorkerCommand(), ParallelSlot::callback, callback(), ParallelSlot::callback_data, GetIdleWorker(), NO_SLOT, ParallelState::parallelSlot, sendMessageToWorker(), ParallelState::te, WaitForWorkers(), WFW_ONE_IDLE, ParallelSlot::workerStatus, and WRKR_WORKING.

Referenced by restore_toc_entries_parallel(), and WriteDataChunks().

◆ GetIdleWorker()

static int GetIdleWorker ( ParallelState pstate)
static

Definition at line 1236 of file parallel.c.

1237 {
1238  int i;
1239 
1240  for (i = 0; i < pstate->numWorkers; i++)
1241  {
1242  if (pstate->parallelSlot[i].workerStatus == WRKR_IDLE)
1243  return i;
1244  }
1245  return NO_SLOT;
1246 }
int i
Definition: isn.c:73
int numWorkers
Definition: parallel.h:57

References i, NO_SLOT, ParallelState::numWorkers, ParallelState::parallelSlot, ParallelSlot::workerStatus, and WRKR_IDLE.

Referenced by DispatchJobForTocEntry(), and WaitForWorkers().

◆ getMessageFromLeader()

static char * getMessageFromLeader ( int  pipefd[2])
static

Definition at line 1516 of file parallel.c.

1517 {
1518  return readMessageFromPipe(pipefd[PIPE_READ]);
1519 }
#define PIPE_READ
Definition: parallel.c:69
static char * readMessageFromPipe(int fd)
Definition: parallel.c:1662

References PIPE_READ, and readMessageFromPipe().

Referenced by WaitForCommands().

◆ getMessageFromWorker()

static char * getMessageFromWorker ( ParallelState pstate,
bool  do_wait,
int *  worker 
)
static

Definition at line 1579 of file parallel.c.

1580 {
1581  int i;
1582  fd_set workerset;
1583  int maxFd = -1;
1584  struct timeval nowait = {0, 0};
1585 
1586  /* construct bitmap of socket descriptors for select() */
1587  FD_ZERO(&workerset);
1588  for (i = 0; i < pstate->numWorkers; i++)
1589  {
1591  continue;
1592  FD_SET(pstate->parallelSlot[i].pipeRead, &workerset);
1593  if (pstate->parallelSlot[i].pipeRead > maxFd)
1594  maxFd = pstate->parallelSlot[i].pipeRead;
1595  }
1596 
1597  if (do_wait)
1598  {
1599  i = select_loop(maxFd, &workerset);
1600  Assert(i != 0);
1601  }
1602  else
1603  {
1604  if ((i = select(maxFd + 1, &workerset, NULL, NULL, &nowait)) == 0)
1605  return NULL;
1606  }
1607 
1608  if (i < 0)
1609  pg_fatal("%s() failed: %m", "select");
1610 
1611  for (i = 0; i < pstate->numWorkers; i++)
1612  {
1613  char *msg;
1614 
1616  continue;
1617  if (!FD_ISSET(pstate->parallelSlot[i].pipeRead, &workerset))
1618  continue;
1619 
1620  /*
1621  * Read the message if any. If the socket is ready because of EOF,
1622  * we'll return NULL instead (and the socket will stay ready, so the
1623  * condition will persist).
1624  *
1625  * Note: because this is a blocking read, we'll wait if only part of
1626  * the message is available. Waiting a long time would be bad, but
1627  * since worker status messages are short and are always sent in one
1628  * operation, it shouldn't be a problem in practice.
1629  */
1630  msg = readMessageFromPipe(pstate->parallelSlot[i].pipeRead);
1631  *worker = i;
1632  return msg;
1633  }
1634  Assert(false);
1635  return NULL;
1636 }
static int select_loop(int maxFd, fd_set *workerset)
Definition: parallel.c:1540
#define WORKER_IS_RUNNING(workerStatus)
Definition: parallel.c:83
#define pg_fatal(...)
static bool do_wait
Definition: pg_ctl.c:74
int pipeRead
Definition: parallel.c:103
#define select(n, r, w, e, timeout)
Definition: win32_port.h:495

References Assert, do_wait, i, ParallelState::numWorkers, ParallelState::parallelSlot, pg_fatal, ParallelSlot::pipeRead, readMessageFromPipe(), select, select_loop(), WORKER_IS_RUNNING, and ParallelSlot::workerStatus.

Referenced by ListenToWorkers().

◆ GetMyPSlot()

static ParallelSlot * GetMyPSlot ( ParallelState pstate)
static

Definition at line 264 of file parallel.c.

265 {
266  int i;
267 
268  for (i = 0; i < pstate->numWorkers; i++)
269  {
270 #ifdef WIN32
271  if (pstate->parallelSlot[i].threadId == GetCurrentThreadId())
272 #else
273  if (pstate->parallelSlot[i].pid == getpid())
274 #endif
275  return &(pstate->parallelSlot[i]);
276  }
277 
278  return NULL;
279 }
pid_t pid
Definition: parallel.c:113

References i, ParallelState::numWorkers, ParallelState::parallelSlot, and ParallelSlot::pid.

Referenced by archive_close_connection().

◆ HasEveryWorkerTerminated()

static bool HasEveryWorkerTerminated ( ParallelState pstate)
static

Definition at line 1252 of file parallel.c.

1253 {
1254  int i;
1255 
1256  for (i = 0; i < pstate->numWorkers; i++)
1257  {
1259  return false;
1260  }
1261  return true;
1262 }

References i, ParallelState::numWorkers, ParallelState::parallelSlot, WORKER_IS_RUNNING, and ParallelSlot::workerStatus.

Referenced by WaitForTerminatingWorkers().

◆ init_parallel_dump_utils()

void init_parallel_dump_utils ( void  )

Definition at line 236 of file parallel.c.

237 {
238 #ifdef WIN32
239  if (!parallel_init_done)
240  {
241  WSADATA wsaData;
242  int err;
243 
244  /* Prepare for threaded operation */
245  tls_index = TlsAlloc();
246  mainThreadId = GetCurrentThreadId();
247 
248  /* Initialize socket access */
249  err = WSAStartup(MAKEWORD(2, 2), &wsaData);
250  if (err != 0)
251  pg_fatal("%s() failed: error code %d", "WSAStartup", err);
252 
253  parallel_init_done = true;
254  }
255 #endif
256 }
void err(int eval, const char *fmt,...)
Definition: err.c:43

References err(), and pg_fatal.

Referenced by main().

◆ IsEveryWorkerIdle()

bool IsEveryWorkerIdle ( ParallelState pstate)

Definition at line 1268 of file parallel.c.

1269 {
1270  int i;
1271 
1272  for (i = 0; i < pstate->numWorkers; i++)
1273  {
1274  if (pstate->parallelSlot[i].workerStatus != WRKR_IDLE)
1275  return false;
1276  }
1277  return true;
1278 }

References i, ParallelState::numWorkers, ParallelState::parallelSlot, ParallelSlot::workerStatus, and WRKR_IDLE.

Referenced by ParallelBackupEnd(), restore_toc_entries_parallel(), and WaitForWorkers().

◆ ListenToWorkers()

static bool ListenToWorkers ( ArchiveHandle AH,
ParallelState pstate,
bool  do_wait 
)
static

Definition at line 1398 of file parallel.c.

1399 {
1400  int worker;
1401  char *msg;
1402 
1403  /* Try to collect a status message */
1404  msg = getMessageFromWorker(pstate, do_wait, &worker);
1405 
1406  if (!msg)
1407  {
1408  /* If do_wait is true, we must have detected EOF on some socket */
1409  if (do_wait)
1410  pg_fatal("a worker process died unexpectedly");
1411  return false;
1412  }
1413 
1414  /* Process it and update our idea of the worker's status */
1415  if (messageStartsWith(msg, "OK "))
1416  {
1417  ParallelSlot *slot = &pstate->parallelSlot[worker];
1418  TocEntry *te = pstate->te[worker];
1419  int status;
1420 
1421  status = parseWorkerResponse(AH, te, msg);
1422  slot->callback(AH, te, status, slot->callback_data);
1423  slot->workerStatus = WRKR_IDLE;
1424  pstate->te[worker] = NULL;
1425  }
1426  else
1427  pg_fatal("invalid message received from worker: \"%s\"",
1428  msg);
1429 
1430  /* Free the string returned from getMessageFromWorker */
1431  free(msg);
1432 
1433  return true;
1434 }
static int parseWorkerResponse(ArchiveHandle *AH, TocEntry *te, const char *msg)
Definition: parallel.c:1171
static char * getMessageFromWorker(ParallelState *pstate, bool do_wait, int *worker)
Definition: parallel.c:1579
#define messageStartsWith(msg, prefix)
Definition: parallel.c:226
#define free(a)
Definition: header.h:65

References ParallelSlot::callback, ParallelSlot::callback_data, do_wait, free, getMessageFromWorker(), messageStartsWith, ParallelState::parallelSlot, parseWorkerResponse(), pg_fatal, ParallelState::te, ParallelSlot::workerStatus, and WRKR_IDLE.

Referenced by WaitForWorkers().

◆ lockTableForWorker()

static void lockTableForWorker ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 1301 of file parallel.c.

1302 {
1303  const char *qualId;
1304  PQExpBuffer query;
1305  PGresult *res;
1306 
1307  /* Nothing to do for BLOBS */
1308  if (strcmp(te->desc, "BLOBS") == 0)
1309  return;
1310 
1311  query = createPQExpBuffer();
1312 
1313  qualId = fmtQualifiedId(te->namespace, te->tag);
1314 
1315  appendPQExpBuffer(query, "LOCK TABLE %s IN ACCESS SHARE MODE NOWAIT",
1316  qualId);
1317 
1318  res = PQexec(AH->connection, query->data);
1319 
1320  if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
1321  pg_fatal("could not obtain lock on relation \"%s\"\n"
1322  "This usually means that someone requested an ACCESS EXCLUSIVE lock "
1323  "on the table after the pg_dump parent process had gotten the "
1324  "initial ACCESS SHARE lock on the table.", qualId);
1325 
1326  PQclear(res);
1327  destroyPQExpBuffer(query);
1328 }
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2262
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:100
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:72
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:114
const char * fmtQualifiedId(const char *schema, const char *id)
Definition: string_utils.c:145

References appendPQExpBuffer(), _archiveHandle::connection, createPQExpBuffer(), PQExpBufferData::data, _tocEntry::desc, destroyPQExpBuffer(), fmtQualifiedId(), pg_fatal, PGRES_COMMAND_OK, PQclear(), PQexec(), PQresultStatus(), res, and _tocEntry::tag.

Referenced by WaitForCommands().

◆ on_exit_close_archive()

void on_exit_close_archive ( Archive AHX)

Definition at line 328 of file parallel.c.

329 {
330  shutdown_info.AHX = AHX;
332 }
static void archive_close_connection(int code, void *arg)
Definition: parallel.c:339
static ShutdownInformation shutdown_info
Definition: parallel.c:152
void on_exit_nicely(on_exit_nicely_callback function, void *arg)

References ShutdownInformation::AHX, archive_close_connection(), on_exit_nicely(), and shutdown_info.

Referenced by main().

◆ ParallelBackupEnd()

void ParallelBackupEnd ( ArchiveHandle AH,
ParallelState pstate 
)

Definition at line 1059 of file parallel.c.

1060 {
1061  int i;
1062 
1063  /* No work if non-parallel */
1064  if (pstate->numWorkers == 1)
1065  return;
1066 
1067  /* There should not be any unfinished jobs */
1068  Assert(IsEveryWorkerIdle(pstate));
1069 
1070  /* Close the sockets so that the workers know they can exit */
1071  for (i = 0; i < pstate->numWorkers; i++)
1072  {
1073  closesocket(pstate->parallelSlot[i].pipeRead);
1074  closesocket(pstate->parallelSlot[i].pipeWrite);
1075  }
1076 
1077  /* Wait for them to exit */
1078  WaitForTerminatingWorkers(pstate);
1079 
1080  /*
1081  * Unlink pstate from shutdown_info, so the exit handler will not try to
1082  * use it; and likewise unlink from signal_info.
1083  */
1084  shutdown_info.pstate = NULL;
1085  set_cancel_pstate(NULL);
1086 
1087  /* Release state (mere neatnik-ism, since we're about to terminate) */
1088  free(pstate->te);
1089  free(pstate->parallelSlot);
1090  free(pstate);
1091 }
static void set_cancel_pstate(ParallelState *pstate)
Definition: parallel.c:789
bool IsEveryWorkerIdle(ParallelState *pstate)
Definition: parallel.c:1268
static void WaitForTerminatingWorkers(ParallelState *pstate)
Definition: parallel.c:446
int pipeWrite
Definition: parallel.c:104

References Assert, closesocket, free, i, IsEveryWorkerIdle(), ParallelState::numWorkers, ParallelState::parallelSlot, ParallelSlot::pipeRead, ParallelSlot::pipeWrite, ShutdownInformation::pstate, set_cancel_pstate(), shutdown_info, ParallelState::te, and WaitForTerminatingWorkers().

Referenced by _CloseArchive(), and RestoreArchive().

◆ ParallelBackupStart()

ParallelState* ParallelBackupStart ( ArchiveHandle AH)

Definition at line 897 of file parallel.c.

898 {
899  ParallelState *pstate;
900  int i;
901 
902  Assert(AH->public.numWorkers > 0);
903 
904  pstate = (ParallelState *) pg_malloc(sizeof(ParallelState));
905 
906  pstate->numWorkers = AH->public.numWorkers;
907  pstate->te = NULL;
908  pstate->parallelSlot = NULL;
909 
910  if (AH->public.numWorkers == 1)
911  return pstate;
912 
913  /* Create status arrays, being sure to initialize all fields to 0 */
914  pstate->te = (TocEntry **)
915  pg_malloc0(pstate->numWorkers * sizeof(TocEntry *));
916  pstate->parallelSlot = (ParallelSlot *)
917  pg_malloc0(pstate->numWorkers * sizeof(ParallelSlot));
918 
919 #ifdef WIN32
920  /* Make fmtId() and fmtQualifiedId() use thread-local storage */
921  getLocalPQExpBuffer = getThreadLocalPQExpBuffer;
922 #endif
923 
924  /*
925  * Set the pstate in shutdown_info, to tell the exit handler that it must
926  * clean up workers as well as the main database connection. But we don't
927  * set this in signal_info yet, because we don't want child processes to
928  * inherit non-NULL signal_info.pstate.
929  */
930  shutdown_info.pstate = pstate;
931 
932  /*
933  * Temporarily disable query cancellation on the leader connection. This
934  * ensures that child processes won't inherit valid AH->connCancel
935  * settings and thus won't try to issue cancels against the leader's
936  * connection. No harm is done if we fail while it's disabled, because
937  * the leader connection is idle at this point anyway.
938  */
939  set_archive_cancel_info(AH, NULL);
940 
941  /* Ensure stdio state is quiesced before forking */
942  fflush(NULL);
943 
944  /* Create desired number of workers */
945  for (i = 0; i < pstate->numWorkers; i++)
946  {
947 #ifdef WIN32
948  WorkerInfo *wi;
949  uintptr_t handle;
950 #else
951  pid_t pid;
952 #endif
953  ParallelSlot *slot = &(pstate->parallelSlot[i]);
954  int pipeMW[2],
955  pipeWM[2];
956 
957  /* Create communication pipes for this worker */
958  if (pgpipe(pipeMW) < 0 || pgpipe(pipeWM) < 0)
959  pg_fatal("could not create communication channels: %m");
960 
961  /* leader's ends of the pipes */
962  slot->pipeRead = pipeWM[PIPE_READ];
963  slot->pipeWrite = pipeMW[PIPE_WRITE];
964  /* child's ends of the pipes */
965  slot->pipeRevRead = pipeMW[PIPE_READ];
966  slot->pipeRevWrite = pipeWM[PIPE_WRITE];
967 
968 #ifdef WIN32
969  /* Create transient structure to pass args to worker function */
970  wi = (WorkerInfo *) pg_malloc(sizeof(WorkerInfo));
971 
972  wi->AH = AH;
973  wi->slot = slot;
974 
975  handle = _beginthreadex(NULL, 0, (void *) &init_spawned_worker_win32,
976  wi, 0, &(slot->threadId));
977  slot->hThread = handle;
978  slot->workerStatus = WRKR_IDLE;
979 #else /* !WIN32 */
980  pid = fork();
981  if (pid == 0)
982  {
983  /* we are the worker */
984  int j;
985 
986  /* this is needed for GetMyPSlot() */
987  slot->pid = getpid();
988 
989  /* instruct signal handler that we're in a worker now */
990  signal_info.am_worker = true;
991 
992  /* close read end of Worker -> Leader */
993  closesocket(pipeWM[PIPE_READ]);
994  /* close write end of Leader -> Worker */
995  closesocket(pipeMW[PIPE_WRITE]);
996 
997  /*
998  * Close all inherited fds for communication of the leader with
999  * previously-forked workers.
1000  */
1001  for (j = 0; j < i; j++)
1002  {
1003  closesocket(pstate->parallelSlot[j].pipeRead);
1004  closesocket(pstate->parallelSlot[j].pipeWrite);
1005  }
1006 
1007  /* Run the worker ... */
1008  RunWorker(AH, slot);
1009 
1010  /* We can just exit(0) when done */
1011  exit(0);
1012  }
1013  else if (pid < 0)
1014  {
1015  /* fork failed */
1016  pg_fatal("could not create worker process: %m");
1017  }
1018 
1019  /* In Leader after successful fork */
1020  slot->pid = pid;
1021  slot->workerStatus = WRKR_IDLE;
1022 
1023  /* close read end of Leader -> Worker */
1024  closesocket(pipeMW[PIPE_READ]);
1025  /* close write end of Worker -> Leader */
1026  closesocket(pipeWM[PIPE_WRITE]);
1027 #endif /* WIN32 */
1028  }
1029 
1030  /*
1031  * Having forked off the workers, disable SIGPIPE so that leader isn't
1032  * killed if it tries to send a command to a dead worker. We don't want
1033  * the workers to inherit this setting, though.
1034  */
1035 #ifndef WIN32
1037 #endif
1038 
1039  /*
1040  * Re-establish query cancellation on the leader connection.
1041  */
1043 
1044  /*
1045  * Tell the cancel signal handler to forward signals to worker processes,
1046  * too. (As with query cancel, we did not need this earlier because the
1047  * workers have not yet been given anything to do; if we die before this
1048  * point, any already-started workers will see EOF and quit promptly.)
1049  */
1050  set_cancel_pstate(pstate);
1051 
1052  return pstate;
1053 }
#define pgpipe(a)
Definition: parallel.c:137
static void RunWorker(ArchiveHandle *AH, ParallelSlot *slot)
Definition: parallel.c:829
#define PIPE_WRITE
Definition: parallel.c:70
static volatile DumpSignalInformation signal_info
Definition: parallel.c:173
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:730
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
int j
Definition: isn.c:74
static void const char fflush(stdout)
exit(1)
pqsigfunc pqsignal(int signo, pqsigfunc func)
PQExpBuffer(* getLocalPQExpBuffer)(void)
Definition: string_utils.c:27
int numWorkers
Definition: pg_backup.h:225
#define SIGPIPE
Definition: win32_port.h:173
#define SIG_IGN
Definition: win32_port.h:165

References DumpSignalInformation::am_worker, Assert, closesocket, _archiveHandle::connection, exit(), fflush(), getLocalPQExpBuffer, i, j, ParallelState::numWorkers, Archive::numWorkers, ParallelState::parallelSlot, pg_fatal, pg_malloc(), pg_malloc0(), pgpipe, ParallelSlot::pid, PIPE_READ, PIPE_WRITE, ParallelSlot::pipeRead, ParallelSlot::pipeRevRead, ParallelSlot::pipeRevWrite, ParallelSlot::pipeWrite, pqsignal(), ShutdownInformation::pstate, _archiveHandle::public, RunWorker(), set_archive_cancel_info(), set_cancel_pstate(), shutdown_info, SIG_IGN, signal_info, SIGPIPE, ParallelState::te, ParallelSlot::workerStatus, and WRKR_IDLE.

Referenced by _CloseArchive(), and RestoreArchive().

◆ parseWorkerCommand()

static void parseWorkerCommand ( ArchiveHandle AH,
TocEntry **  te,
T_Action act,
const char *  msg 
)
static

Definition at line 1123 of file parallel.c.

1125 {
1126  DumpId dumpId;
1127  int nBytes;
1128 
1129  if (messageStartsWith(msg, "DUMP "))
1130  {
1131  *act = ACT_DUMP;
1132  sscanf(msg, "DUMP %d%n", &dumpId, &nBytes);
1133  Assert(nBytes == strlen(msg));
1134  *te = getTocEntryByDumpId(AH, dumpId);
1135  Assert(*te != NULL);
1136  }
1137  else if (messageStartsWith(msg, "RESTORE "))
1138  {
1139  *act = ACT_RESTORE;
1140  sscanf(msg, "RESTORE %d%n", &dumpId, &nBytes);
1141  Assert(nBytes == strlen(msg));
1142  *te = getTocEntryByDumpId(AH, dumpId);
1143  Assert(*te != NULL);
1144  }
1145  else
1146  pg_fatal("unrecognized command received from leader: \"%s\"",
1147  msg);
1148 }
int DumpId
Definition: pg_backup.h:270
TocEntry * getTocEntryByDumpId(ArchiveHandle *AH, DumpId id)

References ACT_DUMP, ACT_RESTORE, Assert, getTocEntryByDumpId(), messageStartsWith, and pg_fatal.

Referenced by WaitForCommands().

◆ parseWorkerResponse()

static int parseWorkerResponse ( ArchiveHandle AH,
TocEntry te,
const char *  msg 
)
static

Definition at line 1171 of file parallel.c.

1173 {
1174  DumpId dumpId;
1175  int nBytes,
1176  n_errors;
1177  int status = 0;
1178 
1179  if (messageStartsWith(msg, "OK "))
1180  {
1181  sscanf(msg, "OK %d %d %d%n", &dumpId, &status, &n_errors, &nBytes);
1182 
1183  Assert(dumpId == te->dumpId);
1184  Assert(nBytes == strlen(msg));
1185 
1186  AH->public.n_errors += n_errors;
1187  }
1188  else
1189  pg_fatal("invalid message received from worker: \"%s\"",
1190  msg);
1191 
1192  return status;
1193 }

References Assert, _tocEntry::dumpId, messageStartsWith, Archive::n_errors, pg_fatal, and _archiveHandle::public.

Referenced by ListenToWorkers().

◆ readMessageFromPipe()

static char * readMessageFromPipe ( int  fd)
static

Definition at line 1662 of file parallel.c.

1663 {
1664  char *msg;
1665  int msgsize,
1666  bufsize;
1667  int ret;
1668 
1669  /*
1670  * In theory, if we let piperead() read multiple bytes, it might give us
1671  * back fragments of multiple messages. (That can't actually occur, since
1672  * neither leader nor workers send more than one message without waiting
1673  * for a reply, but we don't wish to assume that here.) For simplicity,
1674  * read a byte at a time until we get the terminating '\0'. This method
1675  * is a bit inefficient, but since this is only used for relatively short
1676  * command and status strings, it shouldn't matter.
1677  */
1678  bufsize = 64; /* could be any number */
1679  msg = (char *) pg_malloc(bufsize);
1680  msgsize = 0;
1681  for (;;)
1682  {
1683  Assert(msgsize < bufsize);
1684  ret = piperead(fd, msg + msgsize, 1);
1685  if (ret <= 0)
1686  break; /* error or connection closure */
1687 
1688  Assert(ret == 1);
1689 
1690  if (msg[msgsize] == '\0')
1691  return msg; /* collected whole message */
1692 
1693  msgsize++;
1694  if (msgsize == bufsize) /* enlarge buffer if needed */
1695  {
1696  bufsize += 16; /* could be any number */
1697  msg = (char *) pg_realloc(msg, bufsize);
1698  }
1699  }
1700 
1701  /* Other end has closed the connection */
1702  pg_free(msg);
1703  return NULL;
1704 }
#define piperead(a, b, c)
Definition: parallel.c:138
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
void pg_free(void *ptr)
Definition: fe_memutils.c:105
#define bufsize
Definition: indent_globs.h:36
static int fd(const char *x, int i)
Definition: preproc-init.c:105

References Assert, bufsize, fd(), pg_free(), pg_malloc(), pg_realloc(), and piperead.

Referenced by getMessageFromLeader(), and getMessageFromWorker().

◆ RunWorker()

static void RunWorker ( ArchiveHandle AH,
ParallelSlot slot 
)
static

Definition at line 829 of file parallel.c.

830 {
831  int pipefd[2];
832 
833  /* fetch child ends of pipes */
834  pipefd[PIPE_READ] = slot->pipeRevRead;
835  pipefd[PIPE_WRITE] = slot->pipeRevWrite;
836 
837  /*
838  * Clone the archive so that we have our own state to work with, and in
839  * particular our own database connection.
840  *
841  * We clone on Unix as well as Windows, even though technically we don't
842  * need to because fork() gives us a copy in our own address space
843  * already. But CloneArchive resets the state information and also clones
844  * the database connection which both seem kinda helpful.
845  */
846  AH = CloneArchive(AH);
847 
848  /* Remember cloned archive where signal handler can find it */
849  set_cancel_slot_archive(slot, AH);
850 
851  /*
852  * Call the setup worker function that's defined in the ArchiveHandle.
853  */
854  (AH->SetupWorkerPtr) ((Archive *) AH);
855 
856  /*
857  * Execute commands until done.
858  */
859  WaitForCommands(AH, pipefd);
860 
861  /*
862  * Disconnect from database and clean up.
863  */
864  set_cancel_slot_archive(slot, NULL);
865  DisconnectDatabase(&(AH->public));
866  DeCloneArchive(AH);
867 }
static void WaitForCommands(ArchiveHandle *AH, int pipefd[2])
Definition: parallel.c:1336
static void set_cancel_slot_archive(ParallelSlot *slot, ArchiveHandle *AH)
Definition: parallel.c:809
void DeCloneArchive(ArchiveHandle *AH)
ArchiveHandle * CloneArchive(ArchiveHandle *AH)
SetupWorkerPtrType SetupWorkerPtr

References CloneArchive(), DeCloneArchive(), DisconnectDatabase(), PIPE_READ, PIPE_WRITE, ParallelSlot::pipeRevRead, ParallelSlot::pipeRevWrite, _archiveHandle::public, set_cancel_slot_archive(), _archiveHandle::SetupWorkerPtr, and WaitForCommands().

Referenced by ParallelBackupStart().

◆ select_loop()

static int select_loop ( int  maxFd,
fd_set *  workerset 
)
static

Definition at line 1540 of file parallel.c.

1541 {
1542  int i;
1543  fd_set saveSet = *workerset;
1544 
1545  for (;;)
1546  {
1547  *workerset = saveSet;
1548  i = select(maxFd + 1, workerset, NULL, NULL, NULL);
1549 
1550 #ifndef WIN32
1551  if (i < 0 && errno == EINTR)
1552  continue;
1553 #else
1554  if (i == SOCKET_ERROR && WSAGetLastError() == WSAEINTR)
1555  continue;
1556 #endif
1557  break;
1558  }
1559 
1560  return i;
1561 }
#define EINTR
Definition: win32_port.h:374

References EINTR, i, and select.

Referenced by getMessageFromWorker().

◆ sendMessageToLeader()

static void sendMessageToLeader ( int  pipefd[2],
const char *  str 
)
static

Definition at line 1527 of file parallel.c.

1528 {
1529  int len = strlen(str) + 1;
1530 
1531  if (pipewrite(pipefd[PIPE_WRITE], str, len) != len)
1532  pg_fatal("could not write to the communication channel: %m");
1533 }
#define pipewrite(a, b, c)
Definition: parallel.c:139
const void size_t len

References len, pg_fatal, PIPE_WRITE, pipewrite, and str.

Referenced by WaitForCommands().

◆ sendMessageToWorker()

static void sendMessageToWorker ( ParallelState pstate,
int  worker,
const char *  str 
)
static

Definition at line 1644 of file parallel.c.

1645 {
1646  int len = strlen(str) + 1;
1647 
1648  if (pipewrite(pstate->parallelSlot[worker].pipeWrite, str, len) != len)
1649  {
1650  pg_fatal("could not write to the communication channel: %m");
1651  }
1652 }

References len, ParallelState::parallelSlot, pg_fatal, ParallelSlot::pipeWrite, pipewrite, and str.

Referenced by DispatchJobForTocEntry().

◆ set_archive_cancel_info()

void set_archive_cancel_info ( ArchiveHandle AH,
PGconn conn 
)

Definition at line 730 of file parallel.c.

731 {
732  PGcancel *oldConnCancel;
733 
734  /*
735  * Activate the interrupt handler if we didn't yet in this process. On
736  * Windows, this also initializes signal_info_lock; therefore it's
737  * important that this happen at least once before we fork off any
738  * threads.
739  */
741 
742  /*
743  * On Unix, we assume that storing a pointer value is atomic with respect
744  * to any possible signal interrupt. On Windows, use a critical section.
745  */
746 
747 #ifdef WIN32
748  EnterCriticalSection(&signal_info_lock);
749 #endif
750 
751  /* Free the old one if we have one */
752  oldConnCancel = AH->connCancel;
753  /* be sure interrupt handler doesn't use pointer while freeing */
754  AH->connCancel = NULL;
755 
756  if (oldConnCancel != NULL)
757  PQfreeCancel(oldConnCancel);
758 
759  /* Set the new one if specified */
760  if (conn)
761  AH->connCancel = PQgetCancel(conn);
762 
763  /*
764  * On Unix, there's only ever one active ArchiveHandle per process, so we
765  * can just set signal_info.myAH unconditionally. On Windows, do that
766  * only in the main thread; worker threads have to make sure their
767  * ArchiveHandle appears in the pstate data, which is dealt with in
768  * RunWorker().
769  */
770 #ifndef WIN32
771  signal_info.myAH = AH;
772 #else
773  if (mainThreadId == GetCurrentThreadId())
774  signal_info.myAH = AH;
775 #endif
776 
777 #ifdef WIN32
778  LeaveCriticalSection(&signal_info_lock);
779 #endif
780 }
static void set_cancel_handler(void)
Definition: parallel.c:608
PGcancel * PQgetCancel(PGconn *conn)
Definition: fe-cancel.c:348
void PQfreeCancel(PGcancel *cancel)
Definition: fe-cancel.c:416
PGconn * conn
Definition: streamutil.c:55
ArchiveHandle * myAH
Definition: parallel.c:165
PGcancel *volatile connCancel

References conn, _archiveHandle::connCancel, DumpSignalInformation::myAH, PQfreeCancel(), PQgetCancel(), set_cancel_handler(), and signal_info.

Referenced by ConnectDatabase(), DisconnectDatabase(), and ParallelBackupStart().

◆ set_cancel_handler()

static void set_cancel_handler ( void  )
static

Definition at line 608 of file parallel.c.

609 {
610  /*
611  * When forking, signal_info.handler_set will propagate into the new
612  * process, but that's fine because the signal handler state does too.
613  */
615  {
616  signal_info.handler_set = true;
617 
618  pqsignal(SIGINT, sigTermHandler);
619  pqsignal(SIGTERM, sigTermHandler);
621  }
622 }
static void sigTermHandler(SIGNAL_ARGS)
Definition: parallel.c:545
#define SIGQUIT
Definition: win32_port.h:169

References DumpSignalInformation::handler_set, pqsignal(), signal_info, SIGQUIT, and sigTermHandler().

Referenced by set_archive_cancel_info().

◆ set_cancel_pstate()

static void set_cancel_pstate ( ParallelState pstate)
static

Definition at line 789 of file parallel.c.

790 {
791 #ifdef WIN32
792  EnterCriticalSection(&signal_info_lock);
793 #endif
794 
795  signal_info.pstate = pstate;
796 
797 #ifdef WIN32
798  LeaveCriticalSection(&signal_info_lock);
799 #endif
800 }
ParallelState * pstate
Definition: parallel.c:166

References DumpSignalInformation::pstate, and signal_info.

Referenced by ParallelBackupEnd(), and ParallelBackupStart().

◆ set_cancel_slot_archive()

static void set_cancel_slot_archive ( ParallelSlot slot,
ArchiveHandle AH 
)
static

Definition at line 809 of file parallel.c.

810 {
811 #ifdef WIN32
812  EnterCriticalSection(&signal_info_lock);
813 #endif
814 
815  slot->AH = AH;
816 
817 #ifdef WIN32
818  LeaveCriticalSection(&signal_info_lock);
819 #endif
820 }

References ParallelSlot::AH.

Referenced by RunWorker().

◆ ShutdownWorkersHard()

static void ShutdownWorkersHard ( ParallelState pstate)
static

Definition at line 395 of file parallel.c.

396 {
397  int i;
398 
399  /*
400  * Close our write end of the sockets so that any workers waiting for
401  * commands know they can exit. (Note: some of the pipeWrite fields might
402  * still be zero, if we failed to initialize all the workers. Hence, just
403  * ignore errors here.)
404  */
405  for (i = 0; i < pstate->numWorkers; i++)
407 
408  /*
409  * Force early termination of any commands currently in progress.
410  */
411 #ifndef WIN32
412  /* On non-Windows, send SIGTERM to each worker process. */
413  for (i = 0; i < pstate->numWorkers; i++)
414  {
415  pid_t pid = pstate->parallelSlot[i].pid;
416 
417  if (pid != 0)
418  kill(pid, SIGTERM);
419  }
420 #else
421 
422  /*
423  * On Windows, send query cancels directly to the workers' backends. Use
424  * a critical section to ensure worker threads don't change state.
425  */
426  EnterCriticalSection(&signal_info_lock);
427  for (i = 0; i < pstate->numWorkers; i++)
428  {
429  ArchiveHandle *AH = pstate->parallelSlot[i].AH;
430  char errbuf[1];
431 
432  if (AH != NULL && AH->connCancel != NULL)
433  (void) PQcancel(AH->connCancel, errbuf, sizeof(errbuf));
434  }
435  LeaveCriticalSection(&signal_info_lock);
436 #endif
437 
438  /* Now wait for them to terminate. */
440 }
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
Definition: fe-cancel.c:462
#define kill(pid, sig)
Definition: win32_port.h:485

References ParallelSlot::AH, closesocket, _archiveHandle::connCancel, i, kill, ParallelState::numWorkers, ParallelState::parallelSlot, ParallelSlot::pid, ParallelSlot::pipeWrite, PQcancel(), and WaitForTerminatingWorkers().

Referenced by archive_close_connection().

◆ sigTermHandler()

static void sigTermHandler ( SIGNAL_ARGS  )
static

Definition at line 545 of file parallel.c.

546 {
547  int i;
548  char errbuf[1];
549 
550  /*
551  * Some platforms allow delivery of new signals to interrupt an active
552  * signal handler. That could muck up our attempt to send PQcancel, so
553  * disable the signals that set_cancel_handler enabled.
554  */
555  pqsignal(SIGINT, SIG_IGN);
556  pqsignal(SIGTERM, SIG_IGN);
558 
559  /*
560  * If we're in the leader, forward signal to all workers. (It seems best
561  * to do this before PQcancel; killing the leader transaction will result
562  * in invalid-snapshot errors from active workers, which maybe we can
563  * quiet by killing workers first.) Ignore any errors.
564  */
565  if (signal_info.pstate != NULL)
566  {
567  for (i = 0; i < signal_info.pstate->numWorkers; i++)
568  {
569  pid_t pid = signal_info.pstate->parallelSlot[i].pid;
570 
571  if (pid != 0)
572  kill(pid, SIGTERM);
573  }
574  }
575 
576  /*
577  * Send QueryCancel if we have a connection to send to. Ignore errors,
578  * there's not much we can do about them anyway.
579  */
580  if (signal_info.myAH != NULL && signal_info.myAH->connCancel != NULL)
581  (void) PQcancel(signal_info.myAH->connCancel, errbuf, sizeof(errbuf));
582 
583  /*
584  * Report we're quitting, using nothing more complicated than write(2).
585  * When in parallel operation, only the leader process should do this.
586  */
587  if (!signal_info.am_worker)
588  {
589  if (progname)
590  {
592  write_stderr(": ");
593  }
594  write_stderr("terminated by user\n");
595  }
596 
597  /*
598  * And die, using _exit() not exit() because the latter will invoke atexit
599  * handlers that can fail if we interrupted related code.
600  */
601  _exit(1);
602 }
#define write_stderr(str)
Definition: parallel.c:184
const char * progname
Definition: main.c:44

References DumpSignalInformation::am_worker, _archiveHandle::connCancel, i, kill, DumpSignalInformation::myAH, ParallelState::numWorkers, ParallelState::parallelSlot, ParallelSlot::pid, PQcancel(), pqsignal(), progname, DumpSignalInformation::pstate, SIG_IGN, signal_info, SIGQUIT, and write_stderr.

Referenced by set_cancel_handler().

◆ WaitForCommands()

static void WaitForCommands ( ArchiveHandle AH,
int  pipefd[2] 
)
static

Definition at line 1336 of file parallel.c.

1337 {
1338  char *command;
1339  TocEntry *te;
1340  T_Action act;
1341  int status = 0;
1342  char buf[256];
1343 
1344  for (;;)
1345  {
1346  if (!(command = getMessageFromLeader(pipefd)))
1347  {
1348  /* EOF, so done */
1349  return;
1350  }
1351 
1352  /* Decode the command */
1353  parseWorkerCommand(AH, &te, &act, command);
1354 
1355  if (act == ACT_DUMP)
1356  {
1357  /* Acquire lock on this table within the worker's session */
1358  lockTableForWorker(AH, te);
1359 
1360  /* Perform the dump command */
1361  status = (AH->WorkerJobDumpPtr) (AH, te);
1362  }
1363  else if (act == ACT_RESTORE)
1364  {
1365  /* Perform the restore command */
1366  status = (AH->WorkerJobRestorePtr) (AH, te);
1367  }
1368  else
1369  Assert(false);
1370 
1371  /* Return status to leader */
1372  buildWorkerResponse(AH, te, act, status, buf, sizeof(buf));
1373 
1374  sendMessageToLeader(pipefd, buf);
1375 
1376  /* command was pg_malloc'd and we are responsible for free()ing it. */
1377  free(command);
1378  }
1379 }
static void sendMessageToLeader(int pipefd[2], const char *str)
Definition: parallel.c:1527
static char * getMessageFromLeader(int pipefd[2])
Definition: parallel.c:1516
static void lockTableForWorker(ArchiveHandle *AH, TocEntry *te)
Definition: parallel.c:1301
static void buildWorkerResponse(ArchiveHandle *AH, TocEntry *te, T_Action act, int status, char *buf, int buflen)
Definition: parallel.c:1156
static void parseWorkerCommand(ArchiveHandle *AH, TocEntry **te, T_Action *act, const char *msg)
Definition: parallel.c:1123
WorkerJobDumpPtrType WorkerJobDumpPtr
WorkerJobRestorePtrType WorkerJobRestorePtr

References ACT_DUMP, ACT_RESTORE, Assert, buf, buildWorkerResponse(), free, getMessageFromLeader(), lockTableForWorker(), parseWorkerCommand(), sendMessageToLeader(), _archiveHandle::WorkerJobDumpPtr, and _archiveHandle::WorkerJobRestorePtr.

Referenced by RunWorker().

◆ WaitForTerminatingWorkers()

static void WaitForTerminatingWorkers ( ParallelState pstate)
static

Definition at line 446 of file parallel.c.

447 {
448  while (!HasEveryWorkerTerminated(pstate))
449  {
450  ParallelSlot *slot = NULL;
451  int j;
452 
453 #ifndef WIN32
454  /* On non-Windows, use wait() to wait for next worker to end */
455  int status;
456  pid_t pid = wait(&status);
457 
458  /* Find dead worker's slot, and clear the PID field */
459  for (j = 0; j < pstate->numWorkers; j++)
460  {
461  slot = &(pstate->parallelSlot[j]);
462  if (slot->pid == pid)
463  {
464  slot->pid = 0;
465  break;
466  }
467  }
468 #else /* WIN32 */
469  /* On Windows, we must use WaitForMultipleObjects() */
470  HANDLE *lpHandles = pg_malloc(sizeof(HANDLE) * pstate->numWorkers);
471  int nrun = 0;
472  DWORD ret;
473  uintptr_t hThread;
474 
475  for (j = 0; j < pstate->numWorkers; j++)
476  {
478  {
479  lpHandles[nrun] = (HANDLE) pstate->parallelSlot[j].hThread;
480  nrun++;
481  }
482  }
483  ret = WaitForMultipleObjects(nrun, lpHandles, false, INFINITE);
484  Assert(ret != WAIT_FAILED);
485  hThread = (uintptr_t) lpHandles[ret - WAIT_OBJECT_0];
486  free(lpHandles);
487 
488  /* Find dead worker's slot, and clear the hThread field */
489  for (j = 0; j < pstate->numWorkers; j++)
490  {
491  slot = &(pstate->parallelSlot[j]);
492  if (slot->hThread == hThread)
493  {
494  /* For cleanliness, close handles for dead threads */
495  CloseHandle((HANDLE) slot->hThread);
496  slot->hThread = (uintptr_t) INVALID_HANDLE_VALUE;
497  break;
498  }
499  }
500 #endif /* WIN32 */
501 
502  /* On all platforms, update workerStatus and te[] as well */
503  Assert(j < pstate->numWorkers);
505  pstate->te[j] = NULL;
506  }
507 }
static bool HasEveryWorkerTerminated(ParallelState *pstate)
Definition: parallel.c:1252

References Assert, free, HasEveryWorkerTerminated(), j, ParallelState::numWorkers, ParallelState::parallelSlot, pg_malloc(), ParallelSlot::pid, ParallelState::te, WORKER_IS_RUNNING, ParallelSlot::workerStatus, and WRKR_TERMINATED.

Referenced by ParallelBackupEnd(), and ShutdownWorkersHard().

◆ WaitForWorkers()

void WaitForWorkers ( ArchiveHandle AH,
ParallelState pstate,
WFW_WaitOption  mode 
)

Definition at line 1451 of file parallel.c.

1452 {
1453  bool do_wait = false;
1454 
1455  /*
1456  * In GOT_STATUS mode, always block waiting for a message, since we can't
1457  * return till we get something. In other modes, we don't block the first
1458  * time through the loop.
1459  */
1460  if (mode == WFW_GOT_STATUS)
1461  {
1462  /* Assert that caller knows what it's doing */
1463  Assert(!IsEveryWorkerIdle(pstate));
1464  do_wait = true;
1465  }
1466 
1467  for (;;)
1468  {
1469  /*
1470  * Check for status messages, even if we don't need to block. We do
1471  * not try very hard to reap all available messages, though, since
1472  * there's unlikely to be more than one.
1473  */
1474  if (ListenToWorkers(AH, pstate, do_wait))
1475  {
1476  /*
1477  * If we got a message, we are done by definition for GOT_STATUS
1478  * mode, and we can also be certain that there's at least one idle
1479  * worker. So we're done in all but ALL_IDLE mode.
1480  */
1481  if (mode != WFW_ALL_IDLE)
1482  return;
1483  }
1484 
1485  /* Check whether we must wait for new status messages */
1486  switch (mode)
1487  {
1488  case WFW_NO_WAIT:
1489  return; /* never wait */
1490  case WFW_GOT_STATUS:
1491  Assert(false); /* can't get here, because we waited */
1492  break;
1493  case WFW_ONE_IDLE:
1494  if (GetIdleWorker(pstate) != NO_SLOT)
1495  return;
1496  break;
1497  case WFW_ALL_IDLE:
1498  if (IsEveryWorkerIdle(pstate))
1499  return;
1500  break;
1501  }
1502 
1503  /* Loop back, and this time wait for something to happen */
1504  do_wait = true;
1505  }
1506 }
static bool ListenToWorkers(ArchiveHandle *AH, ParallelState *pstate, bool do_wait)
Definition: parallel.c:1398
@ WFW_ALL_IDLE
Definition: parallel.h:35
@ WFW_GOT_STATUS
Definition: parallel.h:33
@ WFW_NO_WAIT
Definition: parallel.h:32
static PgChecksumMode mode
Definition: pg_checksums.c:56

References Assert, do_wait, GetIdleWorker(), IsEveryWorkerIdle(), ListenToWorkers(), mode, NO_SLOT, WFW_ALL_IDLE, WFW_GOT_STATUS, WFW_NO_WAIT, and WFW_ONE_IDLE.

Referenced by DispatchJobForTocEntry(), restore_toc_entries_parallel(), and WriteDataChunks().

Variable Documentation

◆ shutdown_info

ShutdownInformation shutdown_info
static

Definition at line 152 of file parallel.c.

Referenced by on_exit_close_archive(), ParallelBackupEnd(), and ParallelBackupStart().

◆ signal_info