cdbdisp.h 5.9 KB
Newer Older
1 2 3 4 5 6
/*-------------------------------------------------------------------------
 *
 * cdbdisp.h
 * routines for dispatching commands from the dispatcher process
 * to the qExec processes.
 *
7 8 9 10 11 12
 * Portions Copyright (c) 2005-2008, Greenplum inc
 * Portions Copyright (c) 2012-Present Pivotal Software, Inc.
 *
 *
 * IDENTIFICATION
 *	    src/include/cdb/cdbdisp.h
13 14 15 16 17 18 19 20 21 22
 *
 *-------------------------------------------------------------------------
 */
#ifndef CDBDISP_H
#define CDBDISP_H

#include "cdb/cdbtm.h"

#define CDB_MOTION_LOST_CONTACT_STRING "Interconnect error master lost contact with segment."

23
struct CdbDispatchResults; /* #include "cdb/cdbdispatchresult.h" */
24
struct CdbPgResults;
25
struct Gang; /* #include "cdb/cdbgang.h" */
26 27
struct ResourceOwnerData;
enum GangType;
28 29 30 31 32 33 34 35 36 37 38 39 40

/*
 * Types of message to QE when we wait for it.
 */
typedef enum DispatchWaitMode
{
	DISPATCH_WAIT_NONE = 0,			/* wait until QE fully completes */
	DISPATCH_WAIT_FINISH,			/* send query finish */
	DISPATCH_WAIT_CANCEL			/* send query cancel */
} DispatchWaitMode;

typedef struct CdbDispatcherState
{
41 42
	bool isExtendedQuery;
	List *allocatedGangs;
43
	bool destroyGang;
44
	struct CdbDispatchResults *primaryResults;
45
	void *dispatchParams;
46
	int	largestGangSize;
47 48
} CdbDispatcherState;

49 50 51
typedef struct DispatcherInternalFuncs
{
	bool (*checkForCancel)(struct CdbDispatcherState *ds);
52
	int (*getWaitSocketFd)(struct CdbDispatcherState *ds);
53
	void* (*makeDispatchParams)(int maxSlices, int largestGangSize, char *queryText, int queryTextLen);
54
	void (*checkResults)(struct CdbDispatcherState *ds, DispatchWaitMode waitMode);
55
	void (*dispatchToGang)(struct CdbDispatcherState *ds, struct Gang *gp, int sliceIndex);
56 57
	void (*waitDispatchFinish)(struct CdbDispatcherState *ds);

58
}DispatcherInternalFuncs;
59

60 61 62 63
/*--------------------------------------------------------------------*/
/*
 * cdbdisp_dispatchToGang:
 * Send the strCommand SQL statement to the subset of all segdbs in the cluster
64
 * specified by the gang parameter. cancelOnError indicates whether an error
65
 * occurring on one of the qExec segdbs should cause all still-executing commands to cancel
66
 * on other qExecs. Normally this would be true. The commands are sent over the libpq
N
Ning Yu 已提交
67
 * connections that were established during cdblink_setup.
68 69 70
 *
 * The caller must provide a CdbDispatchResults object having available
 * resultArray slots sufficient for the number of QEs to be dispatched:
71
 * i.e., resultCapacity - resultCount >= gp->size. This function will
72
 * assign one resultArray slot per QE of the Gang, paralleling the Gang's
73
 * db_descriptors array. Success or failure of each QE will be noted in
74 75 76 77
 * the QE's CdbDispatchResult entry; but before examining the results, the
 * caller must wait for execution to end by calling CdbCheckDispatchResult().
 *
 * The CdbDispatchResults object owns some malloc'ed storage, so the caller
78
 * must make certain to free it by calling cdbdisp_destroyDispatcherState().
79 80 81 82 83 84 85 86 87 88 89
 *
 * When dispatchResults->cancelOnError is false, strCommand is to be
 * dispatched to every connected gang member if possible, despite any
 * cancellation requests, QE errors, connection failures, etc.
 *
 * NB: This function should return normally even if there is an error.
 * It should not longjmp out via elog(ERROR, ...), ereport(ERROR, ...),
 * PG_THROW, CHECK_FOR_INTERRUPTS, etc.
 */
void
cdbdisp_dispatchToGang(struct CdbDispatcherState *ds,
90
					   struct Gang *gp,
91
					   int sliceIndex);
92

93 94 95 96 97
/*
 * cdbdisp_waitDispatchFinish:
 *
 * For asynchronous dispatcher, we have to wait all dispatch to finish before we move on to query execution,
 * otherwise we may get into a deadlock situation, e.g, gather motion node waiting for data,
N
Ning Yu 已提交
98
 * while segments waiting for plan.
99 100 101 102
 */
void
cdbdisp_waitDispatchFinish(struct CdbDispatcherState *ds);

103 104 105 106 107 108 109 110 111
/*
 * CdbCheckDispatchResult:
 *
 * Waits for completion of threads launched by cdbdisp_dispatchToGang().
 *
 * QEs that were dispatched with 'cancelOnError' true and are not yet idle
 * will be canceled/finished according to waitMode.
 */
void
112
cdbdisp_checkDispatchResult(struct CdbDispatcherState *ds, DispatchWaitMode waitMode);
113

114 115 116 117 118 119 120
/*
 * cdbdisp_getDispatchResults:
 *
 * Block until all QEs return results or report errors.
 *
 * Return Values:
 *   Return NULL If one or more QEs got Error in which case qeErrorMsg contain
121
 *   QE error messages and qeErrorCode the thrown ERRCODE.
122 123
 */
struct CdbDispatchResults *
124
cdbdisp_getDispatchResults(struct CdbDispatcherState *ds, ErrorData **qeError);
125

126
/*
127
 * CdbDispatchHandleError
128 129 130 131 132 133 134 135 136 137 138 139 140
 *
 * When caller catches an error, the PG_CATCH handler can use this
 * function instead of cdbdisp_finishCommand to wait for all QEs
 * to finish, clean up, and report QE errors if appropriate.
 * This function should be called only from PG_CATCH handlers.
 *
 * This function destroys and frees the given CdbDispatchResults objects.
 * It is a no-op if both CdbDispatchResults ptrs are NULL.
 *
 * On return, the caller is expected to finish its own cleanup and
 * exit via PG_RE_THROW().
 */
void
141
CdbDispatchHandleError(struct CdbDispatcherState *ds);
142

143 144 145
void
cdbdisp_cancelDispatch(CdbDispatcherState *ds);

146
/*
147
 * Allocate memory and initialize CdbDispatcherState.
148
 *
149
 * Call cdbdisp_destroyDispatcherState to free it.
150
 */
151
CdbDispatcherState * cdbdisp_makeDispatcherState(bool isExtendedQuery);
152

153 154
/*
 * Free memory in CdbDispatcherState
155
 *
156 157
 * Free the PQExpBufferData allocated in libpq.
 * Free dispatcher memory context.
158
 */
159
void cdbdisp_destroyDispatcherState(CdbDispatcherState *ds);
160

161 162 163 164 165
void
cdbdisp_makeDispatchParams(CdbDispatcherState *ds,
						   int maxSlices,
						   char *queryText,
						   int queryTextLen);
166

167
bool cdbdisp_checkForCancel(CdbDispatcherState * ds);
168
int cdbdisp_getWaitSocketFd(CdbDispatcherState *ds);
169

170 171
void cdbdisp_markNamedPortalGangsDestroyed(void);

172
void cdbdisp_cleanupDispatcherHandle(const struct ResourceOwnerData * owner);
173 174 175 176

void AtAbort_DispatcherState(void);

void AtSubAbort_DispatcherState(void);
177 178 179 180

char *
segmentsToContentStr(List *segments);

181
#endif   /* CDBDISP_H */