From: Tomas Vondra Date: Sat, 4 Nov 2017 14:52:13 +0000 (+0100) Subject: Move several functions from pgxcnode.c to poolmgr.c X-Git-Tag: XL_10_R1BETA1~90 X-Git-Url: http://git.postgresql.org/gitweb/?a=commitdiff_plain;h=a59901be0f488c2071aea53e3f6d3f2eb9a86f03;p=postgres-xl.git Move several functions from pgxcnode.c to poolmgr.c A number of functions were defined in pgxcnode.h/pgxnnode.h, but only ever used in poolmgr.c. Those are: - PGXCNodeConnect - open libpq connection using conn. string - PGXCNodePing - ping node using connection string - PGXCNodeClose - close libpq connection - PGXCNodeConnected - verify connection status - PGXCNodeConnStr - build connection string So move them to poolmgr.c and make them static, so that poolmgr is the only part dealing with libpq connections directly. --- diff --git a/src/backend/pgxc/pool/pgxcnode.c b/src/backend/pgxc/pool/pgxcnode.c index a664cc22da..f6dfd2a48c 100644 --- a/src/backend/pgxc/pool/pgxcnode.c +++ b/src/backend/pgxc/pool/pgxcnode.c @@ -24,15 +24,6 @@ * release_handles - release all connection (back to pool) * * - * connection functions (TODO move to poolmgr.c) - * -------------------- - * PGXCNodeConnect - open libpq connection using connection string - * PGXCNodePing - ping node using connection string - * PGXCNodeClose - close libpq connection - * PGXCNodeConnected - verify connection status - * PGXCNodeConnStr - build connection string - * - * * node handle management * ---------------------- * PGXCNodeGetNodeOid - OID for node by index in handle array @@ -356,109 +347,6 @@ InitMultinodeExecutor(bool is_force) } } -/* - * PGXCNodeConnStr - * Builds a connection string for the provided connection parameters. - * - * Aside from the usual connection parameters (host, port, ...) we also - * pass information about type of the parent node and remote node type. - * - * XXX Shouldn't this rather throw an ERROR instead of returning NULL? - */ -char * -PGXCNodeConnStr(char *host, int port, char *dbname, - char *user, char *pgoptions, char *remote_type, char *parent_node) -{ - char *out, - connstr[1024]; - int num; - - /* - * Build up connection string - * remote type can be Coordinator, Datanode or application. - * - * XXX What's application remote type? - */ - num = snprintf(connstr, sizeof(connstr), - "host=%s port=%d dbname=%s user=%s application_name='pgxc:%s' sslmode=disable options='-c remotetype=%s -c parentnode=%s %s'", - host, port, dbname, user, parent_node, remote_type, parent_node, - pgoptions); - - /* Check for overflow */ - if (num > 0 && num < sizeof(connstr)) - { - /* Output result */ - out = (char *) palloc(num + 1); - strcpy(out, connstr); - return out; - } - - /* return NULL if we have problem */ - return NULL; -} - - -/* - * PGXCNodeConnect - * Connect to a Datanode using a constructed connection string. - */ -NODE_CONNECTION * -PGXCNodeConnect(char *connstr) -{ - PGconn *conn; - - /* Delegate call to the pglib */ - conn = PQconnectdb(connstr); - return (NODE_CONNECTION *) conn; -} - -/* - * PGXCNodePing - * Check that a node (identified the connstring) responds correctly. - */ -int -PGXCNodePing(const char *connstr) -{ - if (connstr[0]) - { - PGPing status = PQping(connstr); - if (status == PQPING_OK) - return 0; - else - return 1; - } - else - return -1; -} - -/* - * PGXCNodeClose - * Close connection connection. - */ -void -PGXCNodeClose(NODE_CONNECTION *conn) -{ - /* Delegate call to the libpq */ - PQfinish((PGconn *) conn); -} - -/* - * PGXCNodeConnected - * Check if the provided connection is open and valid. - */ -int -PGXCNodeConnected(NODE_CONNECTION *conn) -{ - PGconn *pgconn = (PGconn *) conn; - - /* - * Simple check, want to do more comprehencive - - * check if it is ready for guery - */ - return pgconn && PQstatus(pgconn) == CONNECTION_OK; -} - - /* * pgxc_node_free * Close the socket handle (local copy) and free occupied memory. diff --git a/src/backend/pgxc/pool/poolmgr.c b/src/backend/pgxc/pool/poolmgr.c index 3722e9e04d..768ed80d7e 100644 --- a/src/backend/pgxc/pool/poolmgr.c +++ b/src/backend/pgxc/pool/poolmgr.c @@ -184,7 +184,15 @@ * - PoolManagerReloadConnectionInfo close all connections * * There's a number of additional helper functions, but those are mostly - * internal and marked as static. + * internal and marked as static. Example of such functions are functions + * constructing connection strings, opening/closing connections, pinging + * nodes, etc. + * + * - PGXCNodeConnect - open libpq connection using connection string + * - PGXCNodePing - ping node using connection string + * - PGXCNodeClose - close libpq connection + * - PGXCNodeConnected - verify connection status + * - PGXCNodeConnStr - build connection string * * * XXX Why do we even need a separate connection pool manager? Can't we @@ -206,6 +214,11 @@ * constants (e.g. PoolManagerAbortTransactions uses 'a'). Perhaps * define this somewhere in a clear manner, e.g. like a #define. * + * XXX The PGXCNode* functions were originally placed in pgxcnode.c, but + * were moved into poolmgr as that's the only place using them. But the + * name still reflects the original location, so perhaps rename them? + * + * * Portions Copyright (c) 2012-2014, TransLattice, Inc. * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group * Portions Copyright (c) 2010-2012 Postgres-XC Development Group @@ -382,6 +395,16 @@ static void pooler_sighup(SIGNAL_ARGS); static void TryPingUnhealthyNode(Oid nodeoid); +/* Open/close connection routines (invoked from Pool Manager) */ +static char *PGXCNodeConnStr(char *host, int port, char *dbname, char *user, + char *pgoptions, + char *remote_type, char *parent_node); +static NODE_CONNECTION *PGXCNodeConnect(char *connstr); +static void PGXCNodeClose(NODE_CONNECTION * conn); +static int PGXCNodeConnected(NODE_CONNECTION * conn); +static int PGXCNodePing(const char *connstr); + + /* * Flags set by interrupt handlers for later service in the main loop. */ @@ -3651,3 +3674,105 @@ check_persistent_connections(bool *newval, void **extra, GucSource source) } return true; } + +/* + * PGXCNodeConnStr + * Builds a connection string for the provided connection parameters. + * + * Aside from the usual connection parameters (host, port, ...) we also + * pass information about type of the parent node and remote node type. + * + * XXX Shouldn't this rather throw an ERROR instead of returning NULL? + */ +static char * +PGXCNodeConnStr(char *host, int port, char *dbname, + char *user, char *pgoptions, char *remote_type, char *parent_node) +{ + char *out, + connstr[1024]; + int num; + + /* + * Build up connection string + * remote type can be Coordinator, Datanode or application. + * + * XXX What's application remote type? + */ + num = snprintf(connstr, sizeof(connstr), + "host=%s port=%d dbname=%s user=%s application_name='pgxc:%s' sslmode=disable options='-c remotetype=%s -c parentnode=%s %s'", + host, port, dbname, user, parent_node, remote_type, parent_node, + pgoptions); + + /* Check for overflow */ + if (num > 0 && num < sizeof(connstr)) + { + /* Output result */ + out = (char *) palloc(num + 1); + strcpy(out, connstr); + return out; + } + + /* return NULL if we have problem */ + return NULL; +} + + +/* + * PGXCNodeConnect + * Connect to a Datanode using a constructed connection string. + */ +static NODE_CONNECTION * +PGXCNodeConnect(char *connstr) +{ + PGconn *conn; + + /* Delegate call to the pglib */ + conn = PQconnectdb(connstr); + return (NODE_CONNECTION *) conn; +} + +/* + * PGXCNodePing + * Check that a node (identified the connstring) responds correctly. + */ +static int +PGXCNodePing(const char *connstr) +{ + if (connstr[0]) + { + PGPing status = PQping(connstr); + if (status == PQPING_OK) + return 0; + else + return 1; + } + else + return -1; +} + +/* + * PGXCNodeClose + * Close connection connection. + */ +static void +PGXCNodeClose(NODE_CONNECTION *conn) +{ + /* Delegate call to the libpq */ + PQfinish((PGconn *) conn); +} + +/* + * PGXCNodeConnected + * Check if the provided connection is open and valid. + */ +static int +PGXCNodeConnected(NODE_CONNECTION *conn) +{ + PGconn *pgconn = (PGconn *) conn; + + /* + * Simple check, want to do more comprehencive - + * check if it is ready for guery + */ + return pgconn && PQstatus(pgconn) == CONNECTION_OK; +} diff --git a/src/include/pgxc/pgxcnode.h b/src/include/pgxc/pgxcnode.h index 13b52e802c..9f002ce3c3 100644 --- a/src/include/pgxc/pgxcnode.h +++ b/src/include/pgxc/pgxcnode.h @@ -25,10 +25,6 @@ #define NO_SOCKET -1 -/* Connection to Datanode maintained by Pool Manager */ -typedef struct PGconn NODE_CONNECTION; -typedef struct PGcancel NODE_CANCEL; - /* Helper structure to access Datanode from Session */ typedef enum { @@ -113,15 +109,7 @@ typedef struct extern void InitMultinodeExecutor(bool is_force); /* Open/close connection routines (invoked from Pool Manager) */ -extern char *PGXCNodeConnStr(char *host, int port, char *dbname, char *user, - char *pgoptions, - char *remote_type, char *parent_node); -extern NODE_CONNECTION *PGXCNodeConnect(char *connstr); -extern void PGXCNodeClose(NODE_CONNECTION * conn); -extern int PGXCNodeConnected(NODE_CONNECTION * conn); -extern int PGXCNodeConnClean(NODE_CONNECTION * conn); extern void PGXCNodeCleanAndRelease(int code, Datum arg); -extern int PGXCNodePing(const char *connstr); extern PGXCNodeHandle *get_any_handle(List *datanodelist); /* Look at information cached in node handles */ diff --git a/src/include/pgxc/poolmgr.h b/src/include/pgxc/poolmgr.h index 3c2d1f4eb2..c7f9343393 100644 --- a/src/include/pgxc/poolmgr.h +++ b/src/include/pgxc/poolmgr.h @@ -35,6 +35,10 @@ #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). *