summaryrefslogtreecommitdiff
path: root/src/include/pgxc/poolmgr.h
blob: c7f93433937453fcc5ca4c187a360b73962af77b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*-------------------------------------------------------------------------
 *
 * poolmgr.h
 *	  Definitions for the built-in Postgres-XL connection pool.
 *
 *
 * Portions Copyright (c) 2012-2014, TransLattice, Inc.
 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
 * Portions Copyright (c) 2010-2012 Postgres-XC Development Group
 *
 *
 * XXX Some function take list of nodes, others accept array + nitems.
 * We should make this more consistent.
 *
 * XXX PoolPingNodes is defined on a number of places, including some .c
 * files. We should define it on one place (pgxcnode.h?) and then include
 * the header wherever needed.
 *
 *
 * IDENTIFICATION
 *	  src/include/pgxc/poolmgr.h
 *
 *-------------------------------------------------------------------------
 */

#ifndef POOLMGR_H
#define POOLMGR_H
#include <sys/time.h>
#include "nodes/nodes.h"
#include "pgxcnode.h"
#include "poolcomm.h"
#include "storage/pmsignal.h"
#include "utils/guc.h"
#include "utils/hsearch.h"

#define MAX_IDLE_TIME 60

/* Connection to nodes maintained by Pool Manager */
typedef struct PGconn NODE_CONNECTION;
typedef struct PGcancel NODE_CANCEL;

/*
 * One connection in the pool (to datanode or coordinator).
 *
 * Essentially a PGconn+PGcancel, so that we can talk to the remote node
 * and also forward a cancel request if needed.
 *
 * XXX rename to PooledConnection.
 */
typedef struct
{
	time_t		released;
	NODE_CONNECTION *conn;
	NODE_CANCEL	*xc_cancelConn;
} PGXCNodePoolSlot;

/*
 * Pool of open connections to single node (datanode or coordinator).
 *
 * All the connections share the same connection string, and are tracked
 * in a simple array of connections.
 *
 * XXX rename to NodePool.
 * XXX not sure if "size" means "valid entries" or "maximum entries".
 * XXX use FLEXIBLE_ARRAY_MEMBER
 * XXX or maybe use simple lists of available/free connections instead?
 */
typedef struct
{
	Oid			nodeoid;	/* node Oid related to this pool */
	char	   *connstr;	/* connection string for all the connections */
	int			freeSize;	/* available connections */
	int			size;  		/* total pool size (available slots) */

	/* array of open connections (with freeSize available connections) */
	PGXCNodePoolSlot **slot;
} PGXCNodePool;

/*
 * A group of per-node connection pools (PGXCNodePool), for a particular
 * database/user combination. We have one PGXCNodePool for each remote
 * node (datanode or coordinator).
 *
 * If there are multiple such combinations (e.g. when there are multiple
 * users accessing the same database), there will be multiple DatabasePool
 * entries, organized in a linked list.
 */
typedef struct databasepool
{
	char	   *database;
	char	   *user_name;
	char	   *pgoptions;		/* Connection options */
	HTAB	   *nodePools; 		/* hashtable, one entry per remote node */
	MemoryContext mcxt;
	struct databasepool *next; 	/* Reference to next to organize linked list */
	time_t		oldest_idle;
} DatabasePool;

/*
 * Agent, managing a single client session on PoolManager side.
 *
 * Is responsible for:
 *
 * - tracking which connections are assigned to the session
 * - managing parameters (GUCs) set in the session
 */
typedef struct
{
	/* Process ID of postmaster child process associated to pool agent */
	int				pid;
	/* communication channel */
	PoolPort		port;
	DatabasePool   *pool;
	MemoryContext	mcxt;
	int				num_dn_connections;
	int				num_coord_connections;
	Oid		   	   *dn_conn_oids;		/* one for each Datanode */
	Oid		   	   *coord_conn_oids;	/* one for each Coordinator */
	PGXCNodePoolSlot **dn_connections;	/* one for each Datanode */
	PGXCNodePoolSlot **coord_connections; /* one for each Coordinator */
} PoolAgent;

/*
 * Configuration parameters (GUCs).
 */
extern int	PoolConnKeepAlive;
extern int	PoolMaintenanceTimeout;
extern int	MaxPoolSize;
extern int	PoolerPort;
extern bool PersistentConnections;

/* Status inquiry functions */
extern void PGXCPoolerProcessIam(void);
extern bool IsPGXCPoolerProcess(void);

/* Initialize internal structures */
extern int	PoolManagerInit(void);

/*
 * Gracefully close the PoolManager connection.
 */
extern void PoolManagerDisconnect(void);

/*
 * Returns list of options to be propagated to the remote node(s).
 */
extern char *session_options(void);

/* Get pooled connections to specified nodes */
extern int *PoolManagerGetConnections(List *datanodelist, List *coordlist,
		int **pids);

/* Clean connections for the specified nodes (for dbname/user). */
extern void PoolManagerCleanConnection(List *datanodelist, List *coordlist,
		char *dbname, char *username);

/* Check that connections cached in the connection poole match catalogs. */
extern bool PoolManagerCheckConnectionInfo(void);

/* Reload connection data in pooler (and close all existing connections). */
extern void PoolManagerReloadConnectionInfo(void);

/* Reload connection data in pooler and close connections to modified nodes). */
extern int PoolManagerRefreshConnectionInfo(void);

/* Return all connections (for the session) back to the pool. */
extern void PoolManagerReleaseConnections(bool destroy);

/* Send "abort transaction" signal to transactions being run */
extern int	PoolManagerAbortTransactions(char *dbname, char *username,
		int **proc_pids);

/* Cancel a running query on all participating nodes (pg_cancel_backend). */
extern void PoolManagerCancelQuery(int dn_count, int* dn_list,
								   int co_count, int* co_list);

/* Check health of nodes in the connection pool. */
extern void PoolPingNodes(void);

extern bool check_persistent_connections(bool *newval, void **extra,
		GucSource source);

#endif