pgpool2.git / summary / log / commit / refs

commit    edc871810dca34dc8af56f23a9abc150580b8eb2
Author:   Yoshiyuki Asaba <y-asaba at pgfoundry.org>
Date:     Fri Jul 06 08:32:57 2007 +0000

    Load balancing node was chosen before sending "SELECT" statement.
    But this way had the following problems.
    
      psql>\d accounts
      Did not find any relation with OID XXX
    
    pgpool chooses it after receiving AuthenticationOK('R') message. So
    pgpool load balance to the same node inside a same session.


child.c | 42 ++++++++++++++++++++++++++++++++++++++++++ pool_process_query.c | 30 ++---------------------------- pool_type.h | 1 + 3 files changed, 45 insertions(+), 28 deletions(-) diff --git a/child.c b/child.c index 38d4b2bb7..86a47e174 100644 --- a/child.c +++ b/child.c @@ -67,6 +67,7 @@ static RETSIGTYPE wakeup_handler(int sig); static int send_params(POOL_CONNECTION *frontend, POOL_CONNECTION_POOL *backend); static void send_frontend_exits(void); static int s_do_auth(POOL_CONNECTION_POOL_SLOT *cp, char *password); +static int select_load_balancing_node(void); /* * non 0 means SIGTERM(smart shutdown) or SIGINT(fast shutdown) has arrived @@ -412,6 +413,9 @@ void do_child(int unix_fd, int inet_fd) if (MAJOR(backend) == PROTO_MAJOR_V2) TSTATE(backend) = 'I'; + /* select load balancing node */ + backend->info->load_balancing_node = select_load_balancing_node(); + /* query process loop */ for (;;) { @@ -1668,3 +1672,41 @@ void connection_count_down(void) static RETSIGTYPE wakeup_handler(int sig) { } + + +/* + * + */ +static int select_load_balancing_node(void) +{ + double total_weight,r; + int i; + + /* choose a backend in random manner with weight */ + selected_slot = 0; + total_weight = 0.0; + + for (i=0;i<NUM_BACKENDS;i++) + { + if (VALID_BACKEND(i)) + { + total_weight += BACKEND_INFO(i).backend_weight; + } + } + r = (((double)random())/RAND_MAX) * total_weight; + total_weight = 0.0; + for (i=0;i<NUM_BACKENDS;i++) + { + if (VALID_BACKEND(i)) + { + if(r >= total_weight) + selected_slot = i; + else + break; + total_weight += BACKEND_INFO(i).backend_weight; + } + } + + pool_debug("select_load_balancing_node: selected backend id is %d", selected_slot); + return selected_slot; +} diff --git a/pool_process_query.c b/pool_process_query.c index f1bcd9360..91bc56eb4 100644 --- a/pool_process_query.c +++ b/pool_process_query.c @@ -3758,10 +3758,10 @@ static int is_commit_query(Node *node) */ static void start_load_balance(POOL_CONNECTION_POOL *backend) { +#ifdef NOT_USED double total_weight,r; int i; -#ifdef NOT_USED /* save backend connection slots */ for (i=0;i<NUM_BACKENDS;i++) { @@ -3772,31 +3772,6 @@ static void start_load_balance(POOL_CONNECTION_POOL *backend) } #endif - /* choose a backend in random manner with weight */ - selected_slot = 0; - total_weight = 0.0; - - for (i=0;i<NUM_BACKENDS;i++) - { - if (VALID_BACKEND(i)) - { - total_weight += BACKEND_INFO(i).backend_weight; - } - } - r = (((double)random())/RAND_MAX) * total_weight; - total_weight = 0.0; - for (i=0;i<NUM_BACKENDS;i++) - { - if (VALID_BACKEND(i)) - { - if(r >= total_weight) - selected_slot = i; - else - break; - total_weight += BACKEND_INFO(i).backend_weight; - } - } - /* temporarily turn off replication mode */ if (REPLICATION) replication_was_enabled = 1; @@ -3809,8 +3784,7 @@ static void start_load_balance(POOL_CONNECTION_POOL *backend) #ifdef NOTUSED backend->slots[0] = slots[selected_slot]; #endif - LOAD_BALANCE_STATUS(selected_slot) = LOAD_SELECTED; - pool_debug("start_load_balance: selected backend id is %d", selected_slot); + LOAD_BALANCE_STATUS(backend->info->load_balancing_node) = LOAD_SELECTED; /* start load balancing */ in_load_balance = 1; diff --git a/pool_type.h b/pool_type.h index c559c0da0..52a6d2d1a 100644 --- a/pool_type.h +++ b/pool_type.h @@ -109,6 +109,7 @@ typedef struct { int minor; /* protocol minor version */ int counter; /* used counter */ time_t create_time; /* connection creation time */ + int load_balancing_node; /* load balancing node */ } ConnectionInfo; [parent: 790b231318ea]