Commit b7b8cc0c authored by Tom Lane's avatar Tom Lane

Redesign parallel dump/restore's wait-for-workers logic.

The ListenToWorkers/ReapWorkerStatus APIs were messy and hard to use.
Instead, make DispatchJobForTocEntry register a callback function that
will take care of state cleanup, doing whatever had been done by the caller
of ReapWorkerStatus in the old design.  (This callback is essentially just
the old mark_work_done function in the restore case, and a trivial test for
worker failure in the dump case.)  Then we can have ListenToWorkers call
the callback immediately on receipt of a status message, and return the
worker to WRKR_IDLE state; so the WRKR_FINISHED state goes away.

This allows us to design a unified wait-for-worker-messages loop:
WaitForWorkers replaces EnsureIdleWorker and EnsureWorkersFinished as well
as the mess in restore_toc_entries_parallel.  Also, we no longer need the
fragile API spec that the caller of DispatchJobForTocEntry is responsible
for ensuring there's an idle worker, since DispatchJobForTocEntry can just
wait until there is one.

In passing, I got rid of the ParallelArgs struct, which was a net negative
in terms of notational verboseness, and didn't seem to be providing any
noticeable amount of abstraction either.

Tom Lane, reviewed by Kevin Grittner

Discussion: <1188.1464544443@sss.pgh.pa.us>
parent f31a931f
This diff is collapsed.
...@@ -2,14 +2,11 @@ ...@@ -2,14 +2,11 @@
* *
* parallel.h * parallel.h
* *
* Parallel support header file for the pg_dump archiver * Parallel support for pg_dump and pg_restore
* *
* Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* The author is not responsible for loss or damages that may
* result from its use.
*
* IDENTIFICATION * IDENTIFICATION
* src/bin/pg_dump/parallel.h * src/bin/pg_dump/parallel.h
* *
...@@ -21,31 +18,53 @@ ...@@ -21,31 +18,53 @@
#include "pg_backup_archiver.h" #include "pg_backup_archiver.h"
/* Function to call in master process on completion of a worker task */
typedef void (*ParallelCompletionPtr) (ArchiveHandle *AH,
TocEntry *te,
int status,
void *callback_data);
/* Wait options for WaitForWorkers */
typedef enum
{
WFW_NO_WAIT,
WFW_GOT_STATUS,
WFW_ONE_IDLE,
WFW_ALL_IDLE
} WFW_WaitOption;
/* Worker process statuses */
typedef enum typedef enum
{ {
WRKR_TERMINATED = 0,
WRKR_IDLE, WRKR_IDLE,
WRKR_WORKING, WRKR_WORKING,
WRKR_FINISHED WRKR_TERMINATED
} T_WorkerStatus; } T_WorkerStatus;
/* Arguments needed for a worker process */ /*
typedef struct ParallelArgs * Per-parallel-worker state of parallel.c.
{ *
ArchiveHandle *AH; * Much of this is valid only in the master process (or, on Windows, should
TocEntry *te; * be touched only by the master thread). But the AH field should be touched
} ParallelArgs; * only by workers. The pipe descriptors are valid everywhere.
*/
/* State for each parallel activity slot */
typedef struct ParallelSlot typedef struct ParallelSlot
{ {
ParallelArgs *args; T_WorkerStatus workerStatus; /* see enum above */
T_WorkerStatus workerStatus;
int status; /* These fields are valid if workerStatus == WRKR_WORKING: */
TocEntry *te; /* item being worked on */
ParallelCompletionPtr callback; /* function to call on completion */
void *callback_data; /* passthru data for it */
ArchiveHandle *AH; /* Archive data worker is using */
int pipeRead; /* master's end of the pipes */ int pipeRead; /* master's end of the pipes */
int pipeWrite; int pipeWrite;
int pipeRevRead; /* child's end of the pipes */ int pipeRevRead; /* child's end of the pipes */
int pipeRevWrite; int pipeRevWrite;
/* Child process/thread identity info: */
#ifdef WIN32 #ifdef WIN32
uintptr_t hThread; uintptr_t hThread;
unsigned int threadId; unsigned int threadId;
...@@ -54,12 +73,11 @@ typedef struct ParallelSlot ...@@ -54,12 +73,11 @@ typedef struct ParallelSlot
#endif #endif
} ParallelSlot; } ParallelSlot;
#define NO_SLOT (-1) /* Overall state for parallel.c */
typedef struct ParallelState typedef struct ParallelState
{ {
int numWorkers; int numWorkers; /* allowed number of workers */
ParallelSlot *parallelSlot; ParallelSlot *parallelSlot; /* array of numWorkers slots */
} ParallelState; } ParallelState;
#ifdef WIN32 #ifdef WIN32
...@@ -69,17 +87,17 @@ extern DWORD mainThreadId; ...@@ -69,17 +87,17 @@ extern DWORD mainThreadId;
extern void init_parallel_dump_utils(void); extern void init_parallel_dump_utils(void);
extern int GetIdleWorker(ParallelState *pstate);
extern bool IsEveryWorkerIdle(ParallelState *pstate); extern bool IsEveryWorkerIdle(ParallelState *pstate);
extern void ListenToWorkers(ArchiveHandle *AH, ParallelState *pstate, bool do_wait); extern void WaitForWorkers(ArchiveHandle *AH, ParallelState *pstate,
extern int ReapWorkerStatus(ParallelState *pstate, int *status); WFW_WaitOption mode);
extern void EnsureIdleWorker(ArchiveHandle *AH, ParallelState *pstate);
extern void EnsureWorkersFinished(ArchiveHandle *AH, ParallelState *pstate);
extern ParallelState *ParallelBackupStart(ArchiveHandle *AH); extern ParallelState *ParallelBackupStart(ArchiveHandle *AH);
extern void DispatchJobForTocEntry(ArchiveHandle *AH, extern void DispatchJobForTocEntry(ArchiveHandle *AH,
ParallelState *pstate, ParallelState *pstate,
TocEntry *te, T_Action act); TocEntry *te,
T_Action act,
ParallelCompletionPtr callback,
void *callback_data);
extern void ParallelBackupEnd(ArchiveHandle *AH, ParallelState *pstate); extern void ParallelBackupEnd(ArchiveHandle *AH, ParallelState *pstate);
extern void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn); extern void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn);
......
...@@ -97,9 +97,14 @@ static void par_list_remove(TocEntry *te); ...@@ -97,9 +97,14 @@ static void par_list_remove(TocEntry *te);
static TocEntry *get_next_work_item(ArchiveHandle *AH, static TocEntry *get_next_work_item(ArchiveHandle *AH,
TocEntry *ready_list, TocEntry *ready_list,
ParallelState *pstate); ParallelState *pstate);
static void mark_work_done(ArchiveHandle *AH, TocEntry *ready_list, static void mark_dump_job_done(ArchiveHandle *AH,
int worker, int status, TocEntry *te,
ParallelState *pstate); int status,
void *callback_data);
static void mark_restore_job_done(ArchiveHandle *AH,
TocEntry *te,
int status,
void *callback_data);
static void fix_dependencies(ArchiveHandle *AH); static void fix_dependencies(ArchiveHandle *AH);
static bool has_lock_conflicts(TocEntry *te1, TocEntry *te2); static bool has_lock_conflicts(TocEntry *te1, TocEntry *te2);
static void repoint_table_dependencies(ArchiveHandle *AH); static void repoint_table_dependencies(ArchiveHandle *AH);
...@@ -2355,8 +2360,8 @@ WriteDataChunks(ArchiveHandle *AH, ParallelState *pstate) ...@@ -2355,8 +2360,8 @@ WriteDataChunks(ArchiveHandle *AH, ParallelState *pstate)
* If we are in a parallel backup, then we are always the master * If we are in a parallel backup, then we are always the master
* process. Dispatch each data-transfer job to a worker. * process. Dispatch each data-transfer job to a worker.
*/ */
EnsureIdleWorker(AH, pstate); DispatchJobForTocEntry(AH, pstate, te, ACT_DUMP,
DispatchJobForTocEntry(AH, pstate, te, ACT_DUMP); mark_dump_job_done, NULL);
} }
else else
WriteDataChunksForTocEntry(AH, te); WriteDataChunksForTocEntry(AH, te);
...@@ -2365,9 +2370,32 @@ WriteDataChunks(ArchiveHandle *AH, ParallelState *pstate) ...@@ -2365,9 +2370,32 @@ WriteDataChunks(ArchiveHandle *AH, ParallelState *pstate)
/* /*
* If parallel, wait for workers to finish. * If parallel, wait for workers to finish.
*/ */
EnsureWorkersFinished(AH, pstate); if (pstate && pstate->numWorkers > 1)
WaitForWorkers(AH, pstate, WFW_ALL_IDLE);
} }
/*
* Callback function that's invoked in the master process after a step has
* been parallel dumped.
*
* We don't need to do anything except check for worker failure.
*/
static void
mark_dump_job_done(ArchiveHandle *AH,
TocEntry *te,
int status,
void *callback_data)
{
ahlog(AH, 1, "finished item %d %s %s\n",
te->dumpId, te->desc, te->tag);
if (status != 0)
exit_horribly(modulename, "worker process failed: exit code %d\n",
status);
}
void void
WriteDataChunksForTocEntry(ArchiveHandle *AH, TocEntry *te) WriteDataChunksForTocEntry(ArchiveHandle *AH, TocEntry *te)
{ {
...@@ -2751,9 +2779,9 @@ _tocEntryRequired(TocEntry *te, teSection curSection, RestoreOptions *ropt) ...@@ -2751,9 +2779,9 @@ _tocEntryRequired(TocEntry *te, teSection curSection, RestoreOptions *ropt)
return 0; return 0;
} }
if (ropt->schemaExcludeNames.head != NULL if (ropt->schemaExcludeNames.head != NULL &&
&& te->namespace te->namespace &&
&& simple_string_list_member(&ropt->schemaExcludeNames, te->namespace)) simple_string_list_member(&ropt->schemaExcludeNames, te->namespace))
return 0; return 0;
if (ropt->selTypes) if (ropt->selTypes)
...@@ -3769,11 +3797,9 @@ static void ...@@ -3769,11 +3797,9 @@ static void
restore_toc_entries_parallel(ArchiveHandle *AH, ParallelState *pstate, restore_toc_entries_parallel(ArchiveHandle *AH, ParallelState *pstate,
TocEntry *pending_list) TocEntry *pending_list)
{ {
int work_status;
bool skipped_some; bool skipped_some;
TocEntry ready_list; TocEntry ready_list;
TocEntry *next_work_item; TocEntry *next_work_item;
int ret_child;
ahlog(AH, 2, "entering restore_toc_entries_parallel\n"); ahlog(AH, 2, "entering restore_toc_entries_parallel\n");
...@@ -3850,54 +3876,29 @@ restore_toc_entries_parallel(ArchiveHandle *AH, ParallelState *pstate, ...@@ -3850,54 +3876,29 @@ restore_toc_entries_parallel(ArchiveHandle *AH, ParallelState *pstate,
par_list_remove(next_work_item); par_list_remove(next_work_item);
DispatchJobForTocEntry(AH, pstate, next_work_item, ACT_RESTORE); DispatchJobForTocEntry(AH, pstate, next_work_item, ACT_RESTORE,
mark_restore_job_done, &ready_list);
} }
else else
{ {
/* at least one child is working and we have nothing ready. */ /* at least one child is working and we have nothing ready. */
} }
for (;;) /*
{ * Before dispatching another job, check to see if anything has
int nTerm = 0; * finished. We should check every time through the loop so as to
* reduce dependencies as soon as possible. If we were unable to
/* * dispatch any job this time through, wait until some worker finishes
* In order to reduce dependencies as soon as possible and * (and, hopefully, unblocks some pending item). If we did dispatch
* especially to reap the status of workers who are working on * something, continue as soon as there's at least one idle worker.
* items that pending items depend on, we do a non-blocking check * Note that in either case, there's guaranteed to be at least one
* for ended workers first. * idle worker when we return to the top of the loop. This ensures we
* * won't block inside DispatchJobForTocEntry, which would be
* However, if we do not have any other work items currently that * undesirable: we'd rather postpone dispatching until we see what's
* workers can work on, we do not busy-loop here but instead * been unblocked by finished jobs.
* really wait for at least one worker to terminate. Hence we call */
* ListenToWorkers(..., ..., do_wait = true) in this case. WaitForWorkers(AH, pstate,
*/ next_work_item ? WFW_ONE_IDLE : WFW_GOT_STATUS);
ListenToWorkers(AH, pstate, !next_work_item);
while ((ret_child = ReapWorkerStatus(pstate, &work_status)) != NO_SLOT)
{
nTerm++;
mark_work_done(AH, &ready_list, ret_child, work_status, pstate);
}
/*
* We need to make sure that we have an idle worker before
* re-running the loop. If nTerm > 0 we already have that (quick
* check).
*/
if (nTerm > 0)
break;
/* if nobody terminated, explicitly check for an idle worker */
if (GetIdleWorker(pstate) != NO_SLOT)
break;
/*
* If we have no idle worker, read the result of one or more
* workers and loop the loop to call ReapWorkerStatus() on them.
*/
ListenToWorkers(AH, pstate, true);
}
} }
ahlog(AH, 1, "finished main parallel loop\n"); ahlog(AH, 1, "finished main parallel loop\n");
...@@ -4025,9 +4026,11 @@ get_next_work_item(ArchiveHandle *AH, TocEntry *ready_list, ...@@ -4025,9 +4026,11 @@ get_next_work_item(ArchiveHandle *AH, TocEntry *ready_list,
int count = 0; int count = 0;
for (k = 0; k < pstate->numWorkers; k++) for (k = 0; k < pstate->numWorkers; k++)
if (pstate->parallelSlot[k].args->te != NULL && {
pstate->parallelSlot[k].args->te->section == SECTION_DATA) if (pstate->parallelSlot[k].workerStatus == WRKR_WORKING &&
pstate->parallelSlot[k].te->section == SECTION_DATA)
count++; count++;
}
if (pstate->numWorkers == 0 || count * 4 < pstate->numWorkers) if (pstate->numWorkers == 0 || count * 4 < pstate->numWorkers)
pref_non_data = false; pref_non_data = false;
} }
...@@ -4044,13 +4047,13 @@ get_next_work_item(ArchiveHandle *AH, TocEntry *ready_list, ...@@ -4044,13 +4047,13 @@ get_next_work_item(ArchiveHandle *AH, TocEntry *ready_list,
* that a currently running item also needs lock on, or vice versa. If * that a currently running item also needs lock on, or vice versa. If
* so, we don't want to schedule them together. * so, we don't want to schedule them together.
*/ */
for (i = 0; i < pstate->numWorkers && !conflicts; i++) for (i = 0; i < pstate->numWorkers; i++)
{ {
TocEntry *running_te; TocEntry *running_te;
if (pstate->parallelSlot[i].workerStatus != WRKR_WORKING) if (pstate->parallelSlot[i].workerStatus != WRKR_WORKING)
continue; continue;
running_te = pstate->parallelSlot[i].args->te; running_te = pstate->parallelSlot[i].te;
if (has_lock_conflicts(te, running_te) || if (has_lock_conflicts(te, running_te) ||
has_lock_conflicts(running_te, te)) has_lock_conflicts(running_te, te))
...@@ -4091,10 +4094,8 @@ get_next_work_item(ArchiveHandle *AH, TocEntry *ready_list, ...@@ -4091,10 +4094,8 @@ get_next_work_item(ArchiveHandle *AH, TocEntry *ready_list,
* our work is finished, the master process will assign us a new work item. * our work is finished, the master process will assign us a new work item.
*/ */
int int
parallel_restore(ParallelArgs *args) parallel_restore(ArchiveHandle *AH, TocEntry *te)
{ {
ArchiveHandle *AH = args->AH;
TocEntry *te = args->te;
int status; int status;
Assert(AH->connection != NULL); Assert(AH->connection != NULL);
...@@ -4110,22 +4111,18 @@ parallel_restore(ParallelArgs *args) ...@@ -4110,22 +4111,18 @@ parallel_restore(ParallelArgs *args)
/* /*
* Housekeeping to be done after a step has been parallel restored. * Callback function that's invoked in the master process after a step has
* been parallel restored.
* *
* Clear the appropriate slot, free all the extra memory we allocated, * Update status and reduce the dependency count of any dependent items.
* update status, and reduce the dependency count of any dependent items.
*/ */
static void static void
mark_work_done(ArchiveHandle *AH, TocEntry *ready_list, mark_restore_job_done(ArchiveHandle *AH,
int worker, int status, TocEntry *te,
ParallelState *pstate) int status,
void *callback_data)
{ {
TocEntry *te = NULL; TocEntry *ready_list = (TocEntry *) callback_data;
te = pstate->parallelSlot[worker].args->te;
if (te == NULL)
exit_horribly(modulename, "could not find slot of finished worker\n");
ahlog(AH, 1, "finished item %d %s %s\n", ahlog(AH, 1, "finished item %d %s %s\n",
te->dumpId, te->desc, te->tag); te->dumpId, te->desc, te->tag);
......
...@@ -111,7 +111,6 @@ typedef z_stream *z_streamp; ...@@ -111,7 +111,6 @@ typedef z_stream *z_streamp;
typedef struct _archiveHandle ArchiveHandle; typedef struct _archiveHandle ArchiveHandle;
typedef struct _tocEntry TocEntry; typedef struct _tocEntry TocEntry;
struct ParallelArgs;
struct ParallelState; struct ParallelState;
#define READ_ERROR_EXIT(fd) \ #define READ_ERROR_EXIT(fd) \
...@@ -375,7 +374,7 @@ struct _tocEntry ...@@ -375,7 +374,7 @@ struct _tocEntry
int nLockDeps; /* number of such dependencies */ int nLockDeps; /* number of such dependencies */
}; };
extern int parallel_restore(struct ParallelArgs *args); extern int parallel_restore(ArchiveHandle *AH, TocEntry *te);
extern void on_exit_close_archive(Archive *AHX); extern void on_exit_close_archive(Archive *AHX);
extern void warn_or_exit_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) pg_attribute_printf(3, 4); extern void warn_or_exit_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) pg_attribute_printf(3, 4);
......
...@@ -820,13 +820,9 @@ _WorkerJobRestoreCustom(ArchiveHandle *AH, TocEntry *te) ...@@ -820,13 +820,9 @@ _WorkerJobRestoreCustom(ArchiveHandle *AH, TocEntry *te)
*/ */
const int buflen = 64; const int buflen = 64;
char *buf = (char *) pg_malloc(buflen); char *buf = (char *) pg_malloc(buflen);
ParallelArgs pargs;
int status; int status;
pargs.AH = AH; status = parallel_restore(AH, te);
pargs.te = te;
status = parallel_restore(&pargs);
snprintf(buf, buflen, "OK RESTORE %d %d %d", te->dumpId, status, snprintf(buf, buflen, "OK RESTORE %d %d %d", te->dumpId, status,
status == WORKER_IGNORED_ERRORS ? AH->public.n_errors : 0); status == WORKER_IGNORED_ERRORS ? AH->public.n_errors : 0);
......
...@@ -826,13 +826,9 @@ _WorkerJobRestoreDirectory(ArchiveHandle *AH, TocEntry *te) ...@@ -826,13 +826,9 @@ _WorkerJobRestoreDirectory(ArchiveHandle *AH, TocEntry *te)
*/ */
const int buflen = 64; const int buflen = 64;
char *buf = (char *) pg_malloc(buflen); char *buf = (char *) pg_malloc(buflen);
ParallelArgs pargs;
int status; int status;
pargs.AH = AH; status = parallel_restore(AH, te);
pargs.te = te;
status = parallel_restore(&pargs);
snprintf(buf, buflen, "OK RESTORE %d %d %d", te->dumpId, status, snprintf(buf, buflen, "OK RESTORE %d %d %d", te->dumpId, status,
status == WORKER_IGNORED_ERRORS ? AH->public.n_errors : 0); status == WORKER_IGNORED_ERRORS ? AH->public.n_errors : 0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment