Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1b8XoR-0003Zf-To for pgsql-hackers@arkaria.postgresql.org; Thu, 02 Jun 2016 18:57:48 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1b8XoR-0007dX-8N for pgsql-hackers@arkaria.postgresql.org; Thu, 02 Jun 2016 18:57:47 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1b8XoN-0007by-Rn for pgsql-hackers@postgreSQL.org; Thu, 02 Jun 2016 18:57:44 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1b8XoG-0002Kg-4O for pgsql-hackers@postgreSQL.org; Thu, 02 Jun 2016 18:57:42 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id u52IvZdP006815 for ; Thu, 2 Jun 2016 14:57:35 -0400 From: Tom Lane To: pgsql-hackers@postgreSQL.org Subject: Re: Redesigning parallel dump/restore's wait-for-workers logic In-reply-to: <1188.1464544443@sss.pgh.pa.us> References: <1188.1464544443@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Sun, 29 May 2016 13:54:03 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <6784.1464893804.0@sss.pgh.pa.us> Date: Thu, 02 Jun 2016 14:57:35 -0400 Message-ID: <6814.1464893855@sss.pgh.pa.us> X-Pg-Spam-Score: -3.3 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <6784.1464893804.1@sss.pgh.pa.us> I wrote: > One of the things I do not like about the current coding of parallel > pg_dump/pg_restore is its baroque logic for handling worker completion > reports, specifically the ListenToWorkers/ReapWorkerStatus APIs. Here's a version of this patch rebased over e652273e073566b6. Since this is only refactoring, and doesn't (I think) fix any bugs, I'm setting it aside till the next commitfest. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="parallel-dump-waiting-2.patch"; charset="us-ascii" Content-ID: <6784.1464893804.2@sss.pgh.pa.us> Content-Description: parallel-dump-waiting-2.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/bin/pg_dump/parallel.c b/src/bin/pg_dump/parallel.c index 7647271..a9671ca 100644 *** a/src/bin/pg_dump/parallel.c --- b/src/bin/pg_dump/parallel.c *************** *** 35,43 **** * the required action (dump or restore) and returns a malloc'd status st= ring. * The status string is passed back to the master where it is interpreted= by * AH->MasterEndParallelItemPtr, another format-specific routine. That ! * function can update state or catalog information on the master's side, * depending on the reply from the worker process. In the end it returns= a ! * status code, which is 0 for successful execution. * * Remember that we have forked off the workers only after we have read in * the catalog. That's why our worker processes can also access the cata= log --- 35,45 ---- * the required action (dump or restore) and returns a malloc'd status st= ring. * The status string is passed back to the master where it is interpreted= by * AH->MasterEndParallelItemPtr, another format-specific routine. That ! * function can update format-specific information on the master's side, * depending on the reply from the worker process. In the end it returns= a ! * status code, which we pass to the ParallelCompletionPtr callback funct= ion ! * that was passed to DispatchJobForTocEntry(). The callback function do= es ! * state updating for the master control logic in pg_backup_archiver.c. * * Remember that we have forked off the workers only after we have read in * the catalog. That's why our worker processes can also access the cata= log *************** *** 48,60 **** * In the master process, the workerStatus field for each worker has one = of * the following values: * WRKR_IDLE: it's waiting for a command ! * WRKR_WORKING: it's been sent a command ! * WRKR_FINISHED: it's returned a result * WRKR_TERMINATED: process ended - * The FINISHED state indicates that the worker is idle, but we've not yet - * dealt with the status code it returned from the prior command. - * ReapWorkerStatus() extracts the unhandled command status value and sets - * the workerStatus back to WRKR_IDLE. */ =20=20 #include "postgres_fe.h" --- 50,57 ---- * In the master process, the workerStatus field for each worker has one = of * the following values: * WRKR_IDLE: it's waiting for a command ! * WRKR_WORKING: it's working on a command * WRKR_TERMINATED: process ended */ =20=20 #include "postgres_fe.h" *************** *** 75,80 **** --- 72,79 ---- #define PIPE_READ 0 #define PIPE_WRITE 1 =20=20 + #define NO_SLOT (-1) /* Failure result for GetIdleWorker() */ +=20 #ifdef WIN32 =20=20 /* *************** static void setup_cancel_handler(void); *** 161,169 **** --- 160,171 ---- 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 *getMessageFromMaster(int pipefd[2]); static void sendMessageToMaster(int pipefd[2], const char *str); static int select_loop(int maxFd, fd_set *workerset); *************** archive_close_connection(int code, void=20 *** 335,342 **** * fail to detect it because there would be no EOF condition on * the other end of the pipe.) */ ! if (slot->args->AH) ! DisconnectDatabase(&(slot->args->AH->public)); =20=20 #ifdef WIN32 closesocket(slot->pipeRevRead); --- 337,344 ---- * fail to detect it because there would be no EOF condition on * the other end of the pipe.) */ ! if (slot->AH) ! DisconnectDatabase(&(slot->AH->public)); =20=20 #ifdef WIN32 closesocket(slot->pipeRevRead); *************** ShutdownWorkersHard(ParallelState *pstat *** 393,399 **** EnterCriticalSection(&signal_info_lock); for (i =3D 0; i < pstate->numWorkers; i++) { ! ArchiveHandle *AH =3D pstate->parallelSlot[i].args->AH; char errbuf[1]; =20=20 if (AH !=3D NULL && AH->connCancel !=3D NULL) --- 395,401 ---- EnterCriticalSection(&signal_info_lock); for (i =3D 0; i < pstate->numWorkers; i++) { ! ArchiveHandle *AH =3D pstate->parallelSlot[i].AH; char errbuf[1]; =20=20 if (AH !=3D NULL && AH->connCancel !=3D NULL) *************** consoleHandler(DWORD dwCtrlType) *** 620,626 **** for (i =3D 0; i < signal_info.pstate->numWorkers; i++) { ParallelSlot *slot =3D &(signal_info.pstate->parallelSlot[i]); ! ArchiveHandle *AH =3D slot->args->AH; HANDLE hThread =3D (HANDLE) slot->hThread; =20=20 /* --- 622,628 ---- for (i =3D 0; i < signal_info.pstate->numWorkers; i++) { ParallelSlot *slot =3D &(signal_info.pstate->parallelSlot[i]); ! ArchiveHandle *AH =3D slot->AH; HANDLE hThread =3D (HANDLE) slot->hThread; =20=20 /* *************** set_cancel_slot_archive(ParallelSlot *sl *** 775,781 **** EnterCriticalSection(&signal_info_lock); #endif =20=20 ! slot->args->AH =3D AH; =20=20 #ifdef WIN32 LeaveCriticalSection(&signal_info_lock); --- 777,783 ---- EnterCriticalSection(&signal_info_lock); #endif =20=20 ! slot->AH =3D AH; =20=20 #ifdef WIN32 LeaveCriticalSection(&signal_info_lock); *************** ParallelBackupStart(ArchiveHandle *AH) *** 921,929 **** strerror(errno)); =20=20 slot->workerStatus =3D WRKR_IDLE; ! slot->args =3D (ParallelArgs *) pg_malloc(sizeof(ParallelArgs)); ! slot->args->AH =3D NULL; ! slot->args->te =3D NULL; =20=20 /* master's ends of the pipes */ slot->pipeRead =3D pipeWM[PIPE_READ]; --- 923,932 ---- strerror(errno)); =20=20 slot->workerStatus =3D WRKR_IDLE; ! slot->AH =3D NULL; ! slot->te =3D NULL; ! slot->callback =3D NULL; ! slot->callback_data =3D NULL; =20=20 /* master's ends of the pipes */ slot->pipeRead =3D pipeWM[PIPE_READ]; *************** ParallelBackupEnd(ArchiveHandle *AH, Par *** 1057,1076 **** } =20=20 /* ! * Dispatch a job to some free worker (caller must ensure there is one!) * * te is the TocEntry to be processed, act is the action to be taken on i= t. */ void ! DispatchJobForTocEntry(ArchiveHandle *AH, ParallelState *pstate, TocEntry= *te, ! T_Action act) { int worker; char *arg; =20=20 ! /* our caller makes sure that at least one worker is idle */ ! worker =3D GetIdleWorker(pstate); ! Assert(worker !=3D NO_SLOT); =20=20 /* Construct and send command string */ arg =3D (AH->MasterStartParallelItemPtr) (AH, te, act); --- 1060,1087 ---- } =20=20 /* ! * Dispatch a job to some free worker. * * te is the TocEntry to be processed, act is the action to be taken on i= t. + * callback is the function to call on completion of the job. + * + * If no worker is currently available, this will block, and previously + * registered callback functions may be called. */ void ! DispatchJobForTocEntry(ArchiveHandle *AH, ! ParallelState *pstate, ! TocEntry *te, ! T_Action act, ! ParallelCompletionPtr callback, ! void *callback_data) { int worker; char *arg; =20=20 ! /* Get a worker, waiting if none are idle */ ! while ((worker =3D GetIdleWorker(pstate)) =3D=3D NO_SLOT) ! WaitForWorkers(AH, pstate, WFW_ONE_IDLE); =20=20 /* Construct and send command string */ arg =3D (AH->MasterStartParallelItemPtr) (AH, te, act); *************** DispatchJobForTocEntry(ArchiveHandle *AH *** 1081,1094 **** =20=20 /* Remember worker is busy, and which TocEntry it's working on */ pstate->parallelSlot[worker].workerStatus =3D WRKR_WORKING; ! pstate->parallelSlot[worker].args->te =3D te; } =20=20 /* * Find an idle worker and return its slot number. * Return NO_SLOT if none are idle. */ ! int GetIdleWorker(ParallelState *pstate) { int i; --- 1092,1107 ---- =20=20 /* Remember worker is busy, and which TocEntry it's working on */ pstate->parallelSlot[worker].workerStatus =3D WRKR_WORKING; ! pstate->parallelSlot[worker].te =3D te; ! pstate->parallelSlot[worker].callback =3D callback; ! pstate->parallelSlot[worker].callback_data =3D callback_data; } =20=20 /* * Find an idle worker and return its slot number. * Return NO_SLOT if none are idle. */ ! static int GetIdleWorker(ParallelState *pstate) { int i; *************** WaitForCommands(ArchiveHandle *AH, int p *** 1260,1276 **** * immediately if there is none available. * * When we get a status message, we let MasterEndParallelItemPtr process = it, ! * then save the resulting status code and switch the worker's state to ! * WRKR_FINISHED. Later, caller must call ReapWorkerStatus() to verify ! * that the status was "OK" and push the worker back to IDLE state. * ! * XXX Rube Goldberg would be proud of this API, but no one else should b= e. * * XXX is it worth checking for more than one status message per call? * It seems somewhat unlikely that multiple workers would finish at exact= ly * the same time. */ ! void ListenToWorkers(ArchiveHandle *AH, ParallelState *pstate, bool do_wait) { int worker; --- 1273,1288 ---- * immediately if there is none available. * * When we get a status message, we let MasterEndParallelItemPtr process = it, ! * then pass the resulting status code to the callback function that was ! * specified to DispatchJobForTocEntry, then reset the worker status to I= DLE. * ! * Returns true if we collected a status message, else false. * * XXX is it worth checking for more than one status message per call? * It seems somewhat unlikely that multiple workers would finish at exact= ly * the same time. */ ! static bool ListenToWorkers(ArchiveHandle *AH, ParallelState *pstate, bool do_wait) { int worker; *************** ListenToWorkers(ArchiveHandle *AH, Paral *** 1284,1317 **** /* If do_wait is true, we must have detected EOF on some socket */ if (do_wait) exit_horribly(modulename, "a worker process died unexpectedly\n"); ! return; } =20=20 /* Process it and update our idea of the worker's status */ if (messageStartsWith(msg, "OK ")) { ! TocEntry *te =3D pstate->parallelSlot[worker].args->te; char *statusString; =20=20 if (messageStartsWith(msg, "OK RESTORE ")) { statusString =3D msg + strlen("OK RESTORE "); ! pstate->parallelSlot[worker].status =3D (AH->MasterEndParallelItemPtr) (AH, te, statusString, ACT_RESTORE); } else if (messageStartsWith(msg, "OK DUMP ")) { statusString =3D msg + strlen("OK DUMP "); ! pstate->parallelSlot[worker].status =3D (AH->MasterEndParallelItemPtr) (AH, te, statusString, ACT_DUMP); } else exit_horribly(modulename, "invalid message received from worker: \"%s\"\n", msg); ! pstate->parallelSlot[worker].workerStatus =3D WRKR_FINISHED; } else exit_horribly(modulename, --- 1296,1334 ---- /* If do_wait is true, we must have detected EOF on some socket */ if (do_wait) exit_horribly(modulename, "a worker process died unexpectedly\n"); ! return false; } =20=20 /* Process it and update our idea of the worker's status */ if (messageStartsWith(msg, "OK ")) { ! ParallelSlot *slot =3D &pstate->parallelSlot[worker]; ! TocEntry *te =3D slot->te; char *statusString; + int status; =20=20 if (messageStartsWith(msg, "OK RESTORE ")) { statusString =3D msg + strlen("OK RESTORE "); ! status =3D (AH->MasterEndParallelItemPtr) (AH, te, statusString, ACT_RESTORE); + slot->callback(AH, te, status, slot->callback_data); } else if (messageStartsWith(msg, "OK DUMP ")) { statusString =3D msg + strlen("OK DUMP "); ! status =3D (AH->MasterEndParallelItemPtr) (AH, te, statusString, ACT_DUMP); + slot->callback(AH, te, status, slot->callback_data); } else exit_horribly(modulename, "invalid message received from worker: \"%s\"\n", msg); ! slot->workerStatus =3D WRKR_IDLE; ! slot->te =3D NULL; } else exit_horribly(modulename, *************** ListenToWorkers(ArchiveHandle *AH, Paral *** 1320,1429 **** =20=20 /* Free the string returned from getMessageFromWorker */ free(msg); - } =20=20 ! /* ! * Check to see if any worker is in WRKR_FINISHED state. If so, ! * return its command status code into *status, reset it to IDLE state, ! * and return its slot number. Otherwise return NO_SLOT. ! * ! * This function is executed in the master process. ! */ ! int ! ReapWorkerStatus(ParallelState *pstate, int *status) ! { ! int i; !=20 ! for (i =3D 0; i < pstate->numWorkers; i++) ! { ! if (pstate->parallelSlot[i].workerStatus =3D=3D WRKR_FINISHED) ! { ! *status =3D pstate->parallelSlot[i].status; ! pstate->parallelSlot[i].status =3D 0; ! pstate->parallelSlot[i].workerStatus =3D WRKR_IDLE; ! return i; ! } ! } ! return NO_SLOT; } =20=20 /* ! * Wait, if necessary, until we have at least one idle worker. ! * Reap worker status as necessary to move FINISHED workers to IDLE state. * ! * We assume that no extra processing is required when reaping a finished ! * command, except for checking that the status was OK (zero). ! * Caution: that assumption means that this function can only be used in ! * parallel dump, not parallel restore, because the latter has a more ! * complex set of rules about handling status. * * This function is executed in the master process. */ void ! EnsureIdleWorker(ArchiveHandle *AH, ParallelState *pstate) { ! int ret_worker; ! int work_status; =20=20 ! for (;;) { ! int nTerm =3D 0; !=20 ! while ((ret_worker =3D ReapWorkerStatus(pstate, &work_status)) !=3D NO_= SLOT) ! { ! if (work_status !=3D 0) ! exit_horribly(modulename, "error processing a parallel work item\n"); !=20 ! nTerm++; ! } !=20 ! /* ! * We need to make sure that we have an idle worker before dispatching ! * the next item. If nTerm > 0 we already have that (quick check). ! */ ! if (nTerm > 0) ! return; !=20 ! /* explicit check for an idle worker */ ! if (GetIdleWorker(pstate) !=3D NO_SLOT) ! return; =20=20 /* ! * 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); ! } ! } !=20 ! /* ! * Wait for all workers to be idle. ! * Reap worker status as necessary to move FINISHED workers to IDLE state. ! * ! * We assume that no extra processing is required when reaping a finished ! * command, except for checking that the status was OK (zero). ! * Caution: that assumption means that this function can only be used in ! * parallel dump, not parallel restore, because the latter has a more ! * complex set of rules about handling status. ! * ! * This function is executed in the master process. ! */ ! void ! EnsureWorkersFinished(ArchiveHandle *AH, ParallelState *pstate) ! { ! int work_status; =20=20 ! if (!pstate || pstate->numWorkers =3D=3D 1) ! return; =20=20 ! /* Waiting for the remaining worker processes to finish */ ! while (!IsEveryWorkerIdle(pstate)) ! { ! if (ReapWorkerStatus(pstate, &work_status) =3D=3D NO_SLOT) ! ListenToWorkers(AH, pstate, true); ! else if (work_status !=3D 0) ! exit_horribly(modulename, ! "error processing a parallel work item\n"); } } =20=20 --- 1337,1415 ---- =20=20 /* Free the string returned from getMessageFromWorker */ free(msg); =20=20 ! return true; } =20=20 /* ! * Check for status results from workers, waiting if necessary. * ! * Available wait modes are: ! * WFW_NO_WAIT: reap any available status, but don't block ! * WFW_GOT_STATUS: wait for at least one more worker to finish ! * WFW_ONE_IDLE: wait for at least one worker to be idle ! * WFW_ALL_IDLE: wait for all workers to be idle ! * ! * Any received results are passed to MasterEndParallelItemPtr and then ! * to the callback specified to DispatchJobForTocEntry. * * This function is executed in the master process. */ void ! WaitForWorkers(ArchiveHandle *AH, ParallelState *pstate, WFW_WaitOption m= ode) { ! bool do_wait =3D false; =20=20 ! /* ! * In GOT_STATUS mode, always block waiting for a message, since we can't ! * return till we get something. In other modes, we don't block the fir= st ! * time through the loop. ! */ ! if (mode =3D=3D WFW_GOT_STATUS) { ! /* Assert that caller knows what it's doing */ ! Assert(!IsEveryWorkerIdle(pstate)); ! do_wait =3D true; ! } =20=20 + for (;;) + { /* ! * Check for status messages, even if we don't need to block. We do ! * not try very hard to reap all available messages, though, since ! * there's unlikely to be more than one. */ ! if (ListenToWorkers(AH, pstate, do_wait)) ! { ! /* ! * If we got a message, we are done by definition for GOT_STATUS ! * mode, and we can also be certain that there's at least one idle ! * worker. So we're done in all but ALL_IDLE mode. ! */ ! if (mode !=3D WFW_ALL_IDLE) ! return; ! } =20=20 ! /* Check whether we must wait for new status messages */ ! switch (mode) ! { ! case WFW_NO_WAIT: ! return; /* never wait */ ! case WFW_GOT_STATUS: ! Assert(false); /* can't get here, because we waited */ ! break; ! case WFW_ONE_IDLE: ! if (GetIdleWorker(pstate) !=3D NO_SLOT) ! return; ! break; ! case WFW_ALL_IDLE: ! if (IsEveryWorkerIdle(pstate)) ! return; ! break; ! } =20=20 ! /* Loop back, and this time wait for something to happen */ ! do_wait =3D true; } } =20=20 diff --git a/src/bin/pg_dump/parallel.h b/src/bin/pg_dump/parallel.h index 21739ca..8ee629b 100644 *** a/src/bin/pg_dump/parallel.h --- b/src/bin/pg_dump/parallel.h *************** *** 2,15 **** * * parallel.h * ! * Parallel support header file for the pg_dump archiver * * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group * 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 * src/bin/pg_dump/parallel.h * --- 2,12 ---- * * parallel.h * ! * Parallel support for pg_dump and pg_restore * * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION * src/bin/pg_dump/parallel.h * *************** *** 21,51 **** =20=20 #include "pg_backup_archiver.h" =20=20 typedef enum { - WRKR_TERMINATED =3D 0, WRKR_IDLE, WRKR_WORKING, ! WRKR_FINISHED } T_WorkerStatus; =20=20 ! /* Arguments needed for a worker process */ ! typedef struct ParallelArgs ! { ! ArchiveHandle *AH; ! TocEntry *te; ! } ParallelArgs; !=20 ! /* State for each parallel activity slot */ typedef struct ParallelSlot { ! ParallelArgs *args; ! T_WorkerStatus workerStatus; ! int status; int pipeRead; /* master's end of the pipes */ int pipeWrite; int pipeRevRead; /* child's end of the pipes */ int pipeRevWrite; #ifdef WIN32 uintptr_t hThread; unsigned int threadId; --- 18,70 ---- =20=20 #include "pg_backup_archiver.h" =20=20 + /* Function to call in master process on completion of a worker task */ + typedef void (*ParallelCompletionPtr) (ArchiveHandle *AH, + TocEntry *te, + int status, + void *callback_data); +=20 + /* Wait options for WaitForWorkers */ + typedef enum + { + WFW_NO_WAIT, + WFW_GOT_STATUS, + WFW_ONE_IDLE, + WFW_ALL_IDLE + } WFW_WaitOption; +=20 + /* Worker process statuses */ typedef enum { WRKR_IDLE, WRKR_WORKING, ! WRKR_TERMINATED } T_WorkerStatus; =20=20 ! /* ! * Per-parallel-worker state of parallel.c. ! * ! * Much of this is valid only in the master process (or, on Windows, shou= ld ! * be touched only by the master thread). But the AH field should be tou= ched ! * only by workers. The pipe descriptors are valid everywhere. ! */ typedef struct ParallelSlot { ! T_WorkerStatus workerStatus; /* see enum above */ !=20 ! /* These fields are valid if workerStatus =3D=3D WRKR_WORKING: */ ! TocEntry *te; /* item being worked on */ ! ParallelCompletionPtr callback; /* function to call on completion */ ! void *callback_data; /* passthru data for it */ !=20 ! ArchiveHandle *AH; /* Archive data worker is using */ !=20 int pipeRead; /* master's end of the pipes */ int pipeWrite; int pipeRevRead; /* child's end of the pipes */ int pipeRevWrite; +=20 + /* Child process/thread identity info: */ #ifdef WIN32 uintptr_t hThread; unsigned int threadId; *************** typedef struct ParallelSlot *** 54,65 **** #endif } ParallelSlot; =20=20 ! #define NO_SLOT (-1) !=20 typedef struct ParallelState { ! int numWorkers; ! ParallelSlot *parallelSlot; } ParallelState; =20=20 #ifdef WIN32 --- 73,83 ---- #endif } ParallelSlot; =20=20 ! /* Overall state for parallel.c */ typedef struct ParallelState { ! int numWorkers; /* allowed number of workers */ ! ParallelSlot *parallelSlot; /* array of numWorkers slots */ } ParallelState; =20=20 #ifdef WIN32 *************** extern DWORD mainThreadId; *** 69,85 **** =20=20 extern void init_parallel_dump_utils(void); =20=20 - extern int GetIdleWorker(ParallelState *pstate); extern bool IsEveryWorkerIdle(ParallelState *pstate); ! extern void ListenToWorkers(ArchiveHandle *AH, ParallelState *pstate, boo= l do_wait); ! extern int ReapWorkerStatus(ParallelState *pstate, int *status); ! extern void EnsureIdleWorker(ArchiveHandle *AH, ParallelState *pstate); ! extern void EnsureWorkersFinished(ArchiveHandle *AH, ParallelState *pstat= e); =20=20 extern ParallelState *ParallelBackupStart(ArchiveHandle *AH); extern void DispatchJobForTocEntry(ArchiveHandle *AH, ParallelState *pstate, ! TocEntry *te, T_Action act); extern void ParallelBackupEnd(ArchiveHandle *AH, ParallelState *pstate); =20=20 extern void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn); --- 87,103 ---- =20=20 extern void init_parallel_dump_utils(void); =20=20 extern bool IsEveryWorkerIdle(ParallelState *pstate); ! extern void WaitForWorkers(ArchiveHandle *AH, ParallelState *pstate, ! WFW_WaitOption mode); =20=20 extern ParallelState *ParallelBackupStart(ArchiveHandle *AH); extern void DispatchJobForTocEntry(ArchiveHandle *AH, ParallelState *pstate, ! TocEntry *te, ! T_Action act, ! ParallelCompletionPtr callback, ! void *callback_data); extern void ParallelBackupEnd(ArchiveHandle *AH, ParallelState *pstate); =20=20 extern void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn); diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_back= up_archiver.c index eadc874..9babfe2 100644 *** a/src/bin/pg_dump/pg_backup_archiver.c --- b/src/bin/pg_dump/pg_backup_archiver.c *************** static void par_list_remove(TocEntry *te *** 97,105 **** static TocEntry *get_next_work_item(ArchiveHandle *AH, TocEntry *ready_list, ParallelState *pstate); ! static void mark_work_done(ArchiveHandle *AH, TocEntry *ready_list, ! int worker, int status, ! ParallelState *pstate); static void fix_dependencies(ArchiveHandle *AH); static bool has_lock_conflicts(TocEntry *te1, TocEntry *te2); static void repoint_table_dependencies(ArchiveHandle *AH); --- 97,110 ---- static TocEntry *get_next_work_item(ArchiveHandle *AH, TocEntry *ready_list, ParallelState *pstate); ! static void mark_dump_job_done(ArchiveHandle *AH, ! TocEntry *te, ! 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 bool has_lock_conflicts(TocEntry *te1, TocEntry *te2); static void repoint_table_dependencies(ArchiveHandle *AH); *************** WriteDataChunks(ArchiveHandle *AH, Paral *** 2348,2355 **** * If we are in a parallel backup, then we are always the master * process. Dispatch each data-transfer job to a worker. */ ! EnsureIdleWorker(AH, pstate); ! DispatchJobForTocEntry(AH, pstate, te, ACT_DUMP); } else WriteDataChunksForTocEntry(AH, te); --- 2353,2360 ---- * If we are in a parallel backup, then we are always the master * process. Dispatch each data-transfer job to a worker. */ ! DispatchJobForTocEntry(AH, pstate, te, ACT_DUMP, ! mark_dump_job_done, NULL); } else WriteDataChunksForTocEntry(AH, te); *************** WriteDataChunks(ArchiveHandle *AH, Paral *** 2358,2366 **** /* * If parallel, wait for workers to finish. */ ! EnsureWorkersFinished(AH, pstate); } =20=20 void WriteDataChunksForTocEntry(ArchiveHandle *AH, TocEntry *te) { --- 2363,2394 ---- /* * If parallel, wait for workers to finish. */ ! if (pstate && pstate->numWorkers > 1) ! WaitForWorkers(AH, pstate, WFW_ALL_IDLE); ! } !=20 !=20 ! /* ! * 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); !=20 ! if (status !=3D 0) ! exit_horribly(modulename, "worker process failed: exit code %d\n", ! status); } =20=20 +=20 void WriteDataChunksForTocEntry(ArchiveHandle *AH, TocEntry *te) { *************** static void *** 3744,3754 **** restore_toc_entries_parallel(ArchiveHandle *AH, ParallelState *pstate, TocEntry *pending_list) { - int work_status; bool skipped_some; TocEntry ready_list; TocEntry *next_work_item; - int ret_child; =20=20 ahlog(AH, 2, "entering restore_toc_entries_parallel\n"); =20=20 --- 3772,3780 ---- *************** restore_toc_entries_parallel(ArchiveHand *** 3825,3878 **** =20=20 par_list_remove(next_work_item); =20=20 ! DispatchJobForTocEntry(AH, pstate, next_work_item, ACT_RESTORE); } else { /* at least one child is working and we have nothing ready. */ } =20=20 ! for (;;) ! { ! int nTerm =3D 0; !=20 ! /* ! * In order to reduce dependencies as soon as possible and ! * especially to reap the status of workers who are working on ! * items that pending items depend on, we do a non-blocking check ! * for ended workers first. ! * ! * However, if we do not have any other work items currently that ! * workers can work on, we do not busy-loop here but instead ! * really wait for at least one worker to terminate. Hence we call ! * ListenToWorkers(..., ..., do_wait =3D true) in this case. ! */ ! ListenToWorkers(AH, pstate, !next_work_item); !=20 ! while ((ret_child =3D ReapWorkerStatus(pstate, &work_status)) !=3D NO_= SLOT) ! { ! nTerm++; ! mark_work_done(AH, &ready_list, ret_child, work_status, pstate); ! } !=20 ! /* ! * 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; !=20 ! /* if nobody terminated, explicitly check for an idle worker */ ! if (GetIdleWorker(pstate) !=3D NO_SLOT) ! break; !=20 ! /* ! * 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); ! } } =20=20 ahlog(AH, 1, "finished main parallel loop\n"); --- 3851,3879 ---- =20=20 par_list_remove(next_work_item); =20=20 ! DispatchJobForTocEntry(AH, pstate, next_work_item, ACT_RESTORE, ! mark_restore_job_done, &ready_list); } else { /* at least one child is working and we have nothing ready. */ } =20=20 ! /* ! * Before dispatching another job, check to see if anything has ! * 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 ! * (and, hopefully, unblocks some pending item). If we did dispatch ! * something, continue as soon as there's at least one idle worker. ! * Note that in either case, there's guaranteed to be at least one ! * idle worker when we return to the top of the loop. This ensures we ! * won't block inside DispatchJobForTocEntry, which would be ! * undesirable: we'd rather postpone dispatching until we see what's ! * been unblocked by finished jobs. ! */ ! WaitForWorkers(AH, pstate, ! next_work_item ? WFW_ONE_IDLE : WFW_GOT_STATUS); } =20=20 ahlog(AH, 1, "finished main parallel loop\n"); *************** get_next_work_item(ArchiveHandle *AH, To *** 4000,4008 **** int count =3D 0; =20=20 for (k =3D 0; k < pstate->numWorkers; k++) ! if (pstate->parallelSlot[k].args->te !=3D NULL && ! pstate->parallelSlot[k].args->te->section =3D=3D SECTION_DATA) count++; if (pstate->numWorkers =3D=3D 0 || count * 4 < pstate->numWorkers) pref_non_data =3D false; } --- 4001,4011 ---- int count =3D 0; =20=20 for (k =3D 0; k < pstate->numWorkers; k++) ! { ! if (pstate->parallelSlot[k].workerStatus =3D=3D WRKR_WORKING && ! pstate->parallelSlot[k].te->section =3D=3D SECTION_DATA) count++; + } if (pstate->numWorkers =3D=3D 0 || count * 4 < pstate->numWorkers) pref_non_data =3D false; } *************** get_next_work_item(ArchiveHandle *AH, To *** 4019,4031 **** * that a currently running item also needs lock on, or vice versa. If * so, we don't want to schedule them together. */ ! for (i =3D 0; i < pstate->numWorkers && !conflicts; i++) { TocEntry *running_te; =20=20 if (pstate->parallelSlot[i].workerStatus !=3D WRKR_WORKING) continue; ! running_te =3D pstate->parallelSlot[i].args->te; =20=20 if (has_lock_conflicts(te, running_te) || has_lock_conflicts(running_te, te)) --- 4022,4034 ---- * that a currently running item also needs lock on, or vice versa. If * so, we don't want to schedule them together. */ ! for (i =3D 0; i < pstate->numWorkers; i++) { TocEntry *running_te; =20=20 if (pstate->parallelSlot[i].workerStatus !=3D WRKR_WORKING) continue; ! running_te =3D pstate->parallelSlot[i].te; =20=20 if (has_lock_conflicts(te, running_te) || has_lock_conflicts(running_te, te)) *************** get_next_work_item(ArchiveHandle *AH, To *** 4066,4075 **** * our work is finished, the master process will assign us a new work ite= m. */ int ! parallel_restore(ParallelArgs *args) { - ArchiveHandle *AH =3D args->AH; - TocEntry *te =3D args->te; int status; =20=20 Assert(AH->connection !=3D NULL); --- 4069,4076 ---- * our work is finished, the master process will assign us a new work ite= m. */ int ! parallel_restore(ArchiveHandle *AH, TocEntry *te) { int status; =20=20 Assert(AH->connection !=3D NULL); *************** parallel_restore(ParallelArgs *args) *** 4085,4106 **** =20=20 =20=20 /* ! * Housekeeping to be done 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. */ static void ! mark_work_done(ArchiveHandle *AH, TocEntry *ready_list, ! int worker, int status, ! ParallelState *pstate) { ! TocEntry *te =3D NULL; !=20 ! te =3D pstate->parallelSlot[worker].args->te; !=20 ! if (te =3D=3D NULL) ! exit_horribly(modulename, "could not find slot of finished worker\n"); =20=20 ahlog(AH, 1, "finished item %d %s %s\n", te->dumpId, te->desc, te->tag); --- 4086,4103 ---- =20=20 =20=20 /* ! * Callback function that's invoked in the master process after a step has ! * been parallel restored. * ! * Update status and reduce the dependency count of any dependent items. */ static void ! mark_restore_job_done(ArchiveHandle *AH, ! TocEntry *te, ! int status, ! void *callback_data) { ! TocEntry *ready_list =3D (TocEntry *) callback_data; =20=20 ahlog(AH, 1, "finished item %d %s %s\n", te->dumpId, te->desc, te->tag); diff --git a/src/bin/pg_dump/pg_backup_archiver.h b/src/bin/pg_dump/pg_back= up_archiver.h index 0376f2b..123aa5d 100644 *** a/src/bin/pg_dump/pg_backup_archiver.h --- b/src/bin/pg_dump/pg_backup_archiver.h *************** typedef z_stream *z_streamp; *** 111,117 **** =20=20 typedef struct _archiveHandle ArchiveHandle; typedef struct _tocEntry TocEntry; - struct ParallelArgs; struct ParallelState; =20=20 #define READ_ERROR_EXIT(fd) \ --- 111,116 ---- *************** struct _tocEntry *** 375,381 **** int nLockDeps; /* number of such dependencies */ }; =20=20 ! extern int parallel_restore(struct ParallelArgs *args); extern void on_exit_close_archive(Archive *AHX); =20=20 extern void warn_or_exit_horribly(ArchiveHandle *AH, const char *modulena= me, const char *fmt,...) pg_attribute_printf(3, 4); --- 374,380 ---- int nLockDeps; /* number of such dependencies */ }; =20=20 ! extern int parallel_restore(ArchiveHandle *AH, TocEntry *te); extern void on_exit_close_archive(Archive *AHX); =20=20 extern void warn_or_exit_horribly(ArchiveHandle *AH, const char *modulena= me, const char *fmt,...) pg_attribute_printf(3, 4); diff --git a/src/bin/pg_dump/pg_backup_custom.c b/src/bin/pg_dump/pg_backup= _custom.c index 66329dc..c4f487a 100644 *** a/src/bin/pg_dump/pg_backup_custom.c --- b/src/bin/pg_dump/pg_backup_custom.c *************** _WorkerJobRestoreCustom(ArchiveHandle *A *** 820,832 **** */ const int buflen =3D 64; char *buf =3D (char *) pg_malloc(buflen); - ParallelArgs pargs; int status; =20=20 ! pargs.AH =3D AH; ! pargs.te =3D te; !=20 ! status =3D parallel_restore(&pargs); =20=20 snprintf(buf, buflen, "OK RESTORE %d %d %d", te->dumpId, status, status =3D=3D WORKER_IGNORED_ERRORS ? AH->public.n_errors : 0); --- 820,828 ---- */ const int buflen =3D 64; char *buf =3D (char *) pg_malloc(buflen); int status; =20=20 ! status =3D parallel_restore(AH, te); =20=20 snprintf(buf, buflen, "OK RESTORE %d %d %d", te->dumpId, status, status =3D=3D WORKER_IGNORED_ERRORS ? AH->public.n_errors : 0); diff --git a/src/bin/pg_dump/pg_backup_directory.c b/src/bin/pg_dump/pg_bac= kup_directory.c index e52f122..8071259 100644 *** a/src/bin/pg_dump/pg_backup_directory.c --- b/src/bin/pg_dump/pg_backup_directory.c *************** _WorkerJobRestoreDirectory(ArchiveHandle *** 826,838 **** */ const int buflen =3D 64; char *buf =3D (char *) pg_malloc(buflen); - ParallelArgs pargs; int status; =20=20 ! pargs.AH =3D AH; ! pargs.te =3D te; !=20 ! status =3D parallel_restore(&pargs); =20=20 snprintf(buf, buflen, "OK RESTORE %d %d %d", te->dumpId, status, status =3D=3D WORKER_IGNORED_ERRORS ? AH->public.n_errors : 0); --- 826,834 ---- */ const int buflen =3D 64; char *buf =3D (char *) pg_malloc(buflen); int status; =20=20 ! status =3D parallel_restore(AH, te); =20=20 snprintf(buf, buflen, "OK RESTORE %d %d %d", te->dumpId, status, status =3D=3D WORKER_IGNORED_ERRORS ? AH->public.n_errors : 0); ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers ------- =_aaaaaaaaaa0--