Beispiel #1
0
/**
 * swap the server connection with a connection from
 * the connection pool
 *
 * we can only switch backends if we have a authed connection in the pool.
 *
 * @return NULL if swapping failed
 *         the new backend on success
 */
network_socket *network_connection_pool_lua_swap(network_mysqld_con *con, int backend_ndx, GHashTable *pwd_table) {
	network_backend_t *backend = NULL;
	network_socket *send_sock;
	network_mysqld_con_lua_t *st = con->plugin_con_state;
//	GString empty_username = { "", 0, 0 };

	/*
	 * we can only change to another backend if the backend is already
	 * in the connection pool and connected
	 */

	backend = network_backends_get(con->srv->backends, backend_ndx);
	if (!backend) return NULL;


	/**
	 * get a connection from the pool which matches our basic requirements
	 * - username has to match
	 * - default_db should match
	 */
		
#ifdef DEBUG_CONN_POOL
	g_debug("%s: (swap) check if we have a connection for this user in the pool '%s'", G_STRLOC, con->client->response ? con->client->response->username->str: "empty_user");
#endif
       int flag = 0;
	network_connection_pool* pool = chassis_event_thread_pool(backend);
	if (NULL == (send_sock = network_connection_pool_get(pool))) {
		/**
		 * no connections in the pool
		 */
        flag = 1;
		if (NULL == (send_sock = self_connect(con, backend, pwd_table))) {
			st->backend_ndx = -1;
			return NULL;
		}
	}

	/* the backend is up and cool, take and move the current backend into the pool */
#ifdef DEBUG_CONN_POOL
	g_debug("%s: (swap) added the previous connection to the pool", G_STRLOC);
#endif
//	network_connection_pool_lua_add_connection(con);

	/* connect to the new backend */
	st->backend = backend;
//	st->backend->connected_clients++;
	st->backend_ndx = backend_ndx;
    
        if (flag == 0 && !g_atomic_int_compare_and_exchange(&st->backend->connected_clients, 0, 0)) {
            g_atomic_int_dec_and_test(&st->backend->connected_clients);
            //g_critical("pool_lua_swap:%08x's connected_clients is %d\n", backend,  backend->connected_clients);
        }

	return send_sock;
}
Beispiel #2
0
int backend_load_balance_get(network_mysqld_con* con) {
       int ret = -1;
       gint i, load_value, count, flag = 0;
       double cv, connect_value;
       network_backend_t* backend;
       network_connection_pool* pool;

       if(con->config->max_connections == 0) return ret;
       network_backends_t* backends = con->srv->priv->backends;
       count = network_backends_count(backends);
       load_value = con->config->max_connections;

       for(i = 0; i < count; i++) {
              backend = network_backends_get(backends, i);
              if (backend == NULL) continue;
              pool = chassis_event_thread_pool(backend);
              if (pool == NULL) continue;
              if (backend->type == BACKEND_TYPE_RO && backend->state == BACKEND_STATE_UP && backend->connected_clients >= load_value) {
                     flag = 1;
                     connect_value = backend->connected_clients / (double)backend->weight;
                     break;
              }
       }
       if(flag == 0) return ret;
       for(i = 0; i < count; i++) {
              backend = network_backends_get(backends, i);
              if (backend == NULL) continue;
              pool = chassis_event_thread_pool(backend);
              if (pool == NULL) continue;
              if (backend->type == BACKEND_TYPE_RO && backend->state == BACKEND_STATE_UP) {
                     cv = backend->connected_clients / (double)backend->weight;
                     if(cv <= connect_value) {
                            ret = i;
                            connect_value = cv;
                     }
              }
       }

       return ret;
}
Beispiel #3
0
/**
 * move the con->server into connection pool and disconnect the
 * proxy from its backend
 */
int network_connection_pool_lua_add_connection(network_mysqld_con *con, int is_write_sql) {
	network_connection_pool_entry *pool_entry = NULL;
	network_mysqld_con_lua_t *st = con->plugin_con_state;
       guint thread_id;

	/* con-server is already disconnected, got out */
	if (!con->server) return 0;

    /* TODO bug fix */
    /* when mysql return unkonw packet, response is null, insert the socket into pool cause segment fault. */
    /* ? should init socket->challenge  ? */
    /* if response is null, conn has not been authed, use an invalid username. */
    if(!con->server->response)
    {
        g_warning("%s: (remove) remove socket from pool, response is NULL, src is %s, dst is %s",
            G_STRLOC, con->server->src->name->str, con->server->dst->name->str);

        con->server->response = network_mysqld_auth_response_new();
        g_string_assign_len(con->server->response->username, C("mysql_proxy_invalid_user"));
    }

	/* the server connection is still authed */
	con->server->is_authed = 1;

	/* insert the server socket into the connection pool */
       if(is_write_sql == 0) {
              network_connection_pool* pool = chassis_event_thread_pool(st->backend);
              pool_entry = network_connection_pool_add(pool, con->server);
       }else {
              network_connection_pool* pool = chassis_event_thread_secondpool(st->backend);
              pool_entry = network_connection_pool_time_add(pool, con->server, con);
       }

	if (pool_entry) {
		event_set(&(con->server->event), con->server->fd, EV_READ, network_mysqld_con_idle_handle, pool_entry);
		chassis_event_add_local(con->srv, &(con->server->event)); /* add a event, but stay in the same thread */
	}

	g_atomic_int_dec_and_test(&(st->backend->connected_clients));
	st->backend = NULL;
	st->backend_ndx = -1;
	con->server = NULL;
       thread_id = chassis_event_thread_index_get();
       chassis_event_thread_t *thread = g_ptr_array_index(con->srv->threads, thread_id);
       if(thread->block_con_queue->length) {
              if (write(thread->con_write_fd, "", 1) != 1) g_message("%s:pipes - write error: %s", G_STRLOC, g_strerror(errno));
       }

       return 0;
}
/**
 * swap the server connection with a connection from
 * the connection pool
 *
 * we can only switch backends if we have a authed connection in the pool.
 *
 * @return NULL if swapping failed
 *         the new backend on success
 */
network_socket *network_connection_pool_lua_swap(network_mysqld_con *con, int backend_ndx) {
	network_backend_t *backend = NULL;
	network_socket *send_sock;
	network_mysqld_con_lua_t *st = con->plugin_con_state;
	chassis_private *g = con->srv->priv;
//	GString empty_username = { "", 0, 0 };

	/*
	 * we can only change to another backend if the backend is already
	 * in the connection pool and connected
	 */

	backend = network_backends_get(g->backends, backend_ndx);
	if (!backend) return NULL;


	/**
	 * get a connection from the pool which matches our basic requirements
	 * - username has to match
	 * - default_db should match
	 */
		
#ifdef DEBUG_CONN_POOL
	g_debug("%s: (swap) check if we have a connection for this user in the pool '%s'", G_STRLOC, con->client->response ? con->client->response->username->str: "empty_user");
#endif
	network_connection_pool* pool = chassis_event_thread_pool(backend);
	if (NULL == (send_sock = network_connection_pool_get(pool))) {
		/**
		 * no connections in the pool
		 */
		if (NULL == (send_sock = self_connect(con, backend))) {
			st->backend_ndx = -1;
			return NULL;
		}
	}

	/* the backend is up and cool, take and move the current backend into the pool */
#ifdef DEBUG_CONN_POOL
	g_debug("%s: (swap) added the previous connection to the pool", G_STRLOC);
#endif
//	network_connection_pool_lua_add_connection(con);

	/* connect to the new backend */
	st->backend = backend;
//	st->backend->connected_clients++;
	st->backend_ndx = backend_ndx;

	return send_sock;
}
Beispiel #5
0
/**
 * move the con->server into connection pool and disconnect the 
 * proxy from its backend 
 */
int network_connection_pool_lua_add_connection(network_mysqld_con *con) {
	network_connection_pool_entry *pool_entry = NULL;
	network_mysqld_con_lua_t *st = con->plugin_con_state;

	/* con-server is already disconnected, got out */
	if (!con->server) return 0;

    /* TODO bug fix */
    /* when mysql return unkonw packet, response is null, insert the socket into pool cause segment fault. */
    /* ? should init socket->challenge  ? */
    /* if response is null, conn has not been authed, use an invalid username. */
    if(!con->server->response)
    {
        g_warning("%s: (remove) remove socket from pool, response is NULL, src is %s, dst is %s",
            G_STRLOC, con->server->src->name->str, con->server->dst->name->str);

        con->server->response = network_mysqld_auth_response_new();
        g_string_assign_len(con->server->response->username, C("mysql_proxy_invalid_user"));
    }

	/* the server connection is still authed */
	con->server->is_authed = 1;

	/* insert the server socket into the connection pool */
	network_connection_pool* pool = chassis_event_thread_pool(st->backend);
	pool_entry = network_connection_pool_add(pool, con->server);

	if (pool_entry) {
		event_set(&(con->server->event), con->server->fd, EV_READ, network_mysqld_con_idle_handle, pool_entry);
		chassis_event_add_local(con->srv, &(con->server->event)); /* add a event, but stay in the same thread */
	}

    if (!g_atomic_int_compare_and_exchange(&st->backend->connected_clients, 0, 0)) {
        g_atomic_int_dec_and_test(&st->backend->connected_clients);    
        //g_critical("add_connection: %08x's connected_clients is %d\n", backend,  backend->connected_clients);
    }

//	st->backend->connected_clients--;
	st->backend = NULL;
	st->backend_ndx = -1;
	
	con->server = NULL;

	return 0;
}
Beispiel #6
0
int idle_rw(network_mysqld_con* con) {
       int ret = -1;
       guint i;

       network_backends_t* backends = con->srv->priv->backends;

       guint count = network_backends_count(backends);
       for (i = 0; i < count; ++i) {
              network_backend_t* backend = network_backends_get(backends, i);
              if (backend == NULL) continue;

              network_connection_pool* pool = chassis_event_thread_pool(backend);
              if (pool == NULL) continue;

              if (backend->type == BACKEND_TYPE_RW && backend->state == BACKEND_STATE_UP) {
                     ret = i;
                     break;
              }
       }

       return ret;
}
Beispiel #7
0
int idle_ro(network_mysqld_con* con) {
       int max_conns = -1;
       guint i;

       network_backends_t* backends = con->srv->priv->backends;

       guint count = network_backends_count(backends);
       for(i = 0; i < count; ++i) {
              network_backend_t* backend = network_backends_get(backends, i);
              if (backend == NULL) continue;

              network_connection_pool* pool = chassis_event_thread_pool(backend);
              if (pool == NULL) continue;

              if (backend->type == BACKEND_TYPE_RO && backend->state == BACKEND_STATE_UP) {
                     if (max_conns == -1 || backend->connected_clients < max_conns) {
                            max_conns = backend->connected_clients;
                     }
              }
       }

       return max_conns;
}
Beispiel #8
0
int wrr_ro(network_mysqld_con *con) {
       guint i, j;
       network_backends_t* backends = con->srv->priv->backends;
       g_wrr_poll* rwsplit = backends->global_wrr;
       guint ndx_num = network_backends_count(backends);
       if (rwsplit->next_ndx >= ndx_num)
              rwsplit->next_ndx = 0;
       gint backend_ndx = backend_load_balance_get(con);
       if(0 < backend_ndx) return backend_ndx;
       // set max weight if no init
       if (rwsplit->max_weight == 0) {
              for(i = 0; i < ndx_num; ++i) {
                     network_backend_t* backend = network_backends_get(backends, i);
                     if (backend == NULL || backend->state != BACKEND_STATE_UP) continue;
                     if (rwsplit->max_weight < backend->weight) {
                            rwsplit->max_weight = backend->weight;
                            rwsplit->cur_weight = backend->weight;
                     }
              }
       }

       guint max_weight = rwsplit->max_weight;
       gint cur_weight = rwsplit->cur_weight;
       guint next_ndx   = rwsplit->next_ndx;

       // get backend index by slave wrr
       gint ndx = -1;
       for(i = 0; i < ndx_num; ++i) {
              network_backend_t* backend = network_backends_get(backends, next_ndx);
              if (backend == NULL) goto next;

              network_connection_pool* pool = chassis_event_thread_pool(backend);
              if (pool == NULL) goto next;

              if (backend->type == BACKEND_TYPE_RO && backend->weight >= cur_weight) {
                     if(backend->state == BACKEND_STATE_UP) {
                            ndx = next_ndx;
                     }else {
                            for(j = 0; j < ndx_num; ++j) {
                                   network_backend_t* b = network_backends_get(backends, j);
                                   if(b == NULL) continue;
                                   network_connection_pool* p = chassis_event_thread_pool(b);
                                   if(p == NULL) continue;
                                   if(b->type == BACKEND_TYPE_RO && b->state == BACKEND_STATE_UP) ndx = j;
                            }
                     }
              }
next:
              if (next_ndx >= ndx_num - 1) {
                     --cur_weight;
                     next_ndx = 0;

                     if (cur_weight <= 0) cur_weight = max_weight;
              } else {
                     ++next_ndx;
              }

              if (ndx != -1) break;
       }
       rwsplit->cur_weight = cur_weight;
       rwsplit->next_ndx = next_ndx;
       return ndx;
}
Beispiel #9
0
/**
 * swap the server connection with a connection from
 * the connection pool
 *
 * we can only switch backends if we have a authed connection in the pool.
 *
 * @return NULL if swapping failed
 *         the new backend on success
 */
network_socket* network_connection_pool_lua_swap(network_mysqld_con *con, int backend_ndx, int need_keep_conn, int *err) {
	network_backend_t *backend = NULL;
	network_socket *send_sock;
       network_connection_pool *pool, *second_pool;
	network_mysqld_con_lua_t *st = con->plugin_con_state;
	chassis_private *g = con->srv->priv;

	/*
	 * we can only change to another backend if the backend is already
	 * in the connection pool and connected
	 */

	backend = network_backends_get(g->backends, backend_ndx);
	if (!backend) return NULL;


	/**
	 * get a connection from the pool which matches our basic requirements
	 * - username has to match
	 * - default_db should match
	 */

#ifdef DEBUG_CONN_POOL
	g_debug("%s: (swap) check if we have a connection for this user in the pool '%s'", G_STRLOC, con->client->response ? con->client->response->username->str: "empty_user");
#endif
       if(need_keep_conn == 0) {
              pool = chassis_event_thread_pool(backend);
              send_sock = network_connection_pool_get(pool);
              if(send_sock == NULL) {
                     second_pool = chassis_event_thread_secondpool(backend);
                     send_sock = network_expiretime_connection_pool_get(second_pool, con);
              }
       }else {
              second_pool = chassis_event_thread_secondpool(backend);
              send_sock = network_connection_secondpool_get(second_pool, con);
              if(send_sock == NULL) g_message("%s:the connection need keep, but it's not in the pool now.", G_STRLOC);
       }
	if (NULL == send_sock ) {
		/**
		 * no connections in the pool
		 */
              if ((con->config->max_connections <= 0) || (backend->connected_clients < con->config->max_connections)) {
                     if (NULL == (send_sock = self_connect(con, backend))) {
                            st->backend_ndx = -1;
                            return NULL;
                     }
              } else {
                     st->backend_ndx = -1;
                     *err = -1;
                     return NULL;
              }
	}

	/* the backend is up and cool, take and move the current backend into the pool */
#ifdef DEBUG_CONN_POOL
	g_debug("%s: (swap) added the previous connection to the pool", G_STRLOC);
#endif

	/* connect to the new backend */
	st->backend = backend;
	g_atomic_int_inc(&(st->backend->connected_clients));
	st->backend_ndx = backend_ndx;

	return send_sock;
}