Пример #1
0
static int proxy_backends_len(lua_State *L) {
	network_backends_t *bs = *(network_backends_t **)luaL_checkself(L);

	lua_pushinteger(L, network_backends_count(bs));

	return 1;
}
Пример #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;
}
Пример #3
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;
}
Пример #4
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;
}
Пример #5
0
network_backend_t *network_backends_get(network_backends_t *bs, guint ndx) {
	if (ndx >= network_backends_count(bs)) return NULL;

	/* FIXME: shouldn't we copy the backend or add ref-counting ? */	
	return bs->backends->pdata[ndx];
}
Пример #6
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;
}