pgpool2.git / summary / log / commit / refs
commit dcefdc260e0d2d2aa73749cf58933c8fc59d9ed9
Author: Tatsuo Ishii <ishii at sraoss.co.jp>
Date: Thu Jun 10 10:05:32 2010 +0000
Fix MASTER* macros not to use global variable in_load_balance.
pool.h | 20 +++++++++++++++++---
pool_connection_pool.c | 19 ++++++++++++++++---
pool_process_query.c | 12 ++++++++++--
pool_query_context.c | 47 ++++++++++++++++++++++++++++++++++++++++++-----
pool_query_context.h | 1 +
pool_session_context.c | 4 +++-
6 files changed, 89 insertions(+), 14 deletions(-)
diff --git a/pool.h b/pool.h
index fca378fee..54c06f396 100644
--- a/pool.h
+++ b/pool.h
@@ -253,6 +253,7 @@ typedef struct {
* no query is in progress and the DB node is healthy
*/
extern bool pool_is_node_to_be_sent_in_current_query(int node_id);
+extern int pool_virtual_master_db_node_id(void);
#define VALID_BACKEND(backend_id) \
(pool_is_node_to_be_sent_in_current_query((backend_id)) && \
@@ -261,15 +262,28 @@ extern bool pool_is_node_to_be_sent_in_current_query(int node_id);
#define CONNECTION_SLOT(p, slot) ((p)->slots[(slot)])
#define CONNECTION(p, slot) (CONNECTION_SLOT(p, slot)->con)
-#define MASTER_CONNECTION(p) ((p)->slots[MASTER_NODE_ID])
-#define MASTER_NODE_ID (in_load_balance? selected_slot : Req_info->master_node_id)
+
+/*
+ * The first DB ndoe id appears in pgpool.conf or the first "live" DB
+ * node otherwise.
+ */
+#define REAL_MASTER_NODE_ID (Req_info->master_node_id)
+
+/*
+ * "Virtual" master node id. It's same as REAL_MASTER_NODE_ID if not
+ * in load balance mode. If in load balance, it's the first load
+ * balance node.
+ */
+#define MASTER_NODE_ID (pool_virtual_master_db_node_id())
#define IS_MASTER_NODE_ID(node_id) (MASTER_NODE_ID == (node_id))
+#define MASTER_CONNECTION(p) ((p)->slots[MASTER_NODE_ID])
+#define MASTER(p) MASTER_CONNECTION(p)->con
+
#define REPLICATION (pool_config->replication_mode)
#define MASTER_SLAVE (pool_config->master_slave_mode)
#define DUAL_MODE (REPLICATION || MASTER_SLAVE)
#define PARALLEL_MODE (pool_config->parallel_mode)
#define RAW_MODE (!REPLICATION && !PARALLEL_MODE && !MASTER_SLAVE)
-#define MASTER(p) MASTER_CONNECTION(p)->con
#define MAJOR(p) MASTER_CONNECTION(p)->sp->major
#define TSTATE(p) MASTER(p)->tstate
#define SYSDB_INFO (system_db_info->info)
diff --git a/pool_connection_pool.c b/pool_connection_pool.c
index b1667c119..8498635c0 100644
--- a/pool_connection_pool.c
+++ b/pool_connection_pool.c
@@ -217,7 +217,9 @@ POOL_CONNECTION_POOL *pool_create_cp(void)
int i, freed = 0;
time_t closetime;
POOL_CONNECTION_POOL *oldestp;
+ POOL_CONNECTION_POOL *ret;
ConnectionInfo *info;
+ int pool_index;
POOL_CONNECTION_POOL *p = pool_connection_pool;
@@ -230,7 +232,12 @@ POOL_CONNECTION_POOL *pool_create_cp(void)
for (i=0;i<pool_config->max_pool;i++)
{
if (MASTER_CONNECTION(p) == NULL)
- return new_connection(p);
+ {
+ ret = new_connection(p);
+ if (ret)
+ ret->pool_index = i;
+ return ret;
+ }
p++;
}
@@ -241,6 +248,8 @@ POOL_CONNECTION_POOL *pool_create_cp(void)
*/
oldestp = p = pool_connection_pool;
closetime = MASTER_CONNECTION(p)->closetime;
+ pool_index = 0;
+
for (i=0;i<pool_config->max_pool;i++)
{
pool_debug("user: %s database: %s closetime: %ld",
@@ -251,6 +260,7 @@ POOL_CONNECTION_POOL *pool_create_cp(void)
{
closetime = MASTER_CONNECTION(p)->closetime;
oldestp = p;
+ pool_index = i;
}
p++;
}
@@ -283,7 +293,11 @@ POOL_CONNECTION_POOL *pool_create_cp(void)
p->info = info;
memset(p->info, 0, sizeof(ConnectionInfo) * MAX_NUM_BACKENDS);
- return new_connection(p);
+ ret = new_connection(p);
+ if (ret)
+ ret->pool_index = pool_index;
+
+ return ret;
}
/*
@@ -633,7 +647,6 @@ static POOL_CONNECTION_POOL *new_connection(POOL_CONNECTION_POOL *p)
if (active_backend_count > 0)
{
p->info->create_time = time(NULL);
- p->pool_index = i;
return p;
}
diff --git a/pool_process_query.c b/pool_process_query.c
index 991dbd86c..bc9968591 100644
--- a/pool_process_query.c
+++ b/pool_process_query.c
@@ -3597,6 +3597,9 @@ POOL_STATUS read_kind_from_backend(POOL_CONNECTION *frontend, POOL_CONNECTION_PO
POOL_SESSION_CONTEXT *session_context = pool_get_session_context();
POOL_QUERY_CONTEXT *query_context = session_context->query_context;
+ int num_executed_nodes = 0;
+ int first_node = -1;
+
memset(kind_map, 0, sizeof(kind_map));
for (i=0;i<NUM_BACKENDS;i++)
@@ -3606,6 +3609,11 @@ POOL_STATUS read_kind_from_backend(POOL_CONNECTION *frontend, POOL_CONNECTION_PO
if (VALID_BACKEND(i))
{
+ num_executed_nodes++;
+
+ if (first_node < 0)
+ first_node = i;
+
do
{
char *p, *value;
@@ -3691,7 +3699,7 @@ POOL_STATUS read_kind_from_backend(POOL_CONNECTION *frontend, POOL_CONNECTION_PO
}
#endif
- if (max_count != NUM_BACKENDS)
+ if (max_count != num_executed_nodes)
{
/*
* not all backends agree with kind. We need to do "decide by majority"
@@ -3723,7 +3731,7 @@ POOL_STATUS read_kind_from_backend(POOL_CONNECTION *frontend, POOL_CONNECTION_PO
}
}
else
- trust_kind = kind_list[MASTER_NODE_ID];
+ trust_kind = kind_list[first_node];
*decided_kind = trust_kind;
diff --git a/pool_query_context.c b/pool_query_context.c
index 3dda8b255..6a591ad51 100644
--- a/pool_query_context.c
+++ b/pool_query_context.c
@@ -143,12 +143,20 @@ void pool_clear_node_to_be_sent(POOL_QUERY_CONTEXT *query_context)
*/
void pool_setall_node_to_be_sent(POOL_QUERY_CONTEXT *query_context)
{
+ int i;
+
if (!query_context)
{
pool_error("pool_setall_node_to_be_sent: no query context");
return;
}
- memset(query_context->where_to_send, true, sizeof(query_context->where_to_send));
+
+ for (i=0;i<NUM_BACKENDS;i++)
+ {
+ if ((BACKEND_INFO(i)).backend_status == CON_UP ||
+ (BACKEND_INFO((i)).backend_status == CON_CONNECT_WAIT))
+ query_context->where_to_send[i] = true;
+ }
return;
}
@@ -173,7 +181,7 @@ bool pool_is_node_to_be_sent(POOL_QUERY_CONTEXT *query_context, int node_id)
}
/*
- * Return true if the DB node is needed to send query.
+ * Returns true if the DB node is needed to send query.
* Intended to be called from VALID_BACKEND
*/
bool pool_is_node_to_be_sent_in_current_query(int node_id)
@@ -191,6 +199,24 @@ bool pool_is_node_to_be_sent_in_current_query(int node_id)
return true;
}
+/*
+ * Returns virtual master DB node id,
+ */
+int pool_virtual_master_db_node_id(void)
+{
+ POOL_SESSION_CONTEXT *sc;
+
+ sc = pool_get_session_context();
+ if (!sc)
+ return 0;
+
+ if (sc->query_context)
+ {
+ return sc->query_context->virtual_master_node_id;
+ }
+ return REAL_MASTER_NODE_ID;
+}
+
/*
* Decide where to send queries(thus expecting response)
*/
@@ -198,6 +224,7 @@ void pool_where_to_send(POOL_QUERY_CONTEXT *query_context, char *query, Node *no
{
POOL_SESSION_CONTEXT *session_context;
POOL_CONNECTION_POOL *backend;
+ int i;
if (!query_context)
{
@@ -218,7 +245,7 @@ void pool_where_to_send(POOL_QUERY_CONTEXT *query_context, char *query, Node *no
*/
if (RAW_MODE)
{
- pool_set_node_to_be_sent(query_context, MASTER_NODE_ID);
+ pool_set_node_to_be_sent(query_context, REAL_MASTER_NODE_ID);
}
else if (MASTER_SLAVE)
{
@@ -246,7 +273,7 @@ void pool_where_to_send(POOL_QUERY_CONTEXT *query_context, char *query, Node *no
else
{
/* only send to master node */
- pool_set_node_to_be_sent(query_context, MASTER_NODE_ID);
+ pool_set_node_to_be_sent(query_context, REAL_MASTER_NODE_ID);
}
}
}
@@ -268,7 +295,7 @@ void pool_where_to_send(POOL_QUERY_CONTEXT *query_context, char *query, Node *no
!is_sequence_query(node))
{
/* only send to master node */
- pool_set_node_to_be_sent(query_context, MASTER_NODE_ID);
+ pool_set_node_to_be_sent(query_context, REAL_MASTER_NODE_ID);
}
else
{
@@ -279,6 +306,16 @@ void pool_where_to_send(POOL_QUERY_CONTEXT *query_context, char *query, Node *no
else
{
pool_error("pool_where_to_send: unknown mode");
+ return;
+ }
+
+ for (i=0;i<NUM_BACKENDS;i++)
+ {
+ if (query_context->where_to_send[i])
+ {
+ query_context->virtual_master_node_id = i;
+ break;
+ }
}
return;
}
diff --git a/pool_query_context.h b/pool_query_context.h
index 0954493d4..324c8dcfc 100644
--- a/pool_query_context.h
+++ b/pool_query_context.h
@@ -42,6 +42,7 @@ typedef struct {
Node *parse_tree; /* raw parser output if any */
Node *rewritten_parse_tree; /* rewritten raw parser output if any */
bool where_to_send[MAX_NUM_BACKENDS]; /* DB node map to send query */
+ int virtual_master_node_id; /* the first DB node to send query */
bool is_extended; /* true if extended query */
diff --git a/pool_session_context.c b/pool_session_context.c
index 81c78589e..2a941aa86 100644
--- a/pool_session_context.c
+++ b/pool_session_context.c
@@ -63,7 +63,9 @@ void pool_init_session_context(POOL_CONNECTION *frontend, POOL_CONNECTION_POOL *
return;
}
- process_info[backend->pool_index].connection_info->load_balancing_node = select_load_balancing_node();
+ session_context->load_balance_node_id =
+ process_info[backend->pool_index].connection_info->load_balancing_node =
+ select_load_balancing_node();
pool_debug("selected load balancing node: %d", backend->info->load_balancing_node);
}
[parent: b6dc4fb4d03a]