Skip to content

Commit

Permalink
Merge pull request #64 from maralla/cluster-nodes
Browse files Browse the repository at this point in the history
Replace `CLUSTER SLOTS` with `CLUSTER NODES` to get slot map
  • Loading branch information
maralla committed Jun 2, 2016
2 parents bc8dbf4 + 1eba08f commit 3151c5f
Show file tree
Hide file tree
Showing 13 changed files with 373 additions and 245 deletions.
2 changes: 1 addition & 1 deletion src/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ struct command {

struct redirect_info {
uint16_t slot;
char addr[DSN_LEN + 1];
char addr[ADDRESS_LEN + 1];
int type;
};

Expand Down
35 changes: 19 additions & 16 deletions src/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static struct connection *conn_create_server(struct context *ctx,
server->info->readonly = true;
}

strncpy(info->dsn, key, DSN_LEN);
strncpy(info->dsn, key, ADDRESS_LEN);
dict_set(&ctx->server_table, info->dsn, (void*)server);
TAILQ_INSERT_TAIL(&ctx->servers, server, next);
return server;
Expand Down Expand Up @@ -282,9 +282,9 @@ struct connection *conn_get_server_from_pool(struct context *ctx,
struct address *addr, bool readonly)
{
struct connection *server = NULL;
char key[DSN_LEN + 1];
char key[ADDRESS_LEN];
snprintf(key, ADDRESS_LEN, "%s:%d", addr->ip, addr->port);

socket_get_key(addr, key);
server = dict_get(&ctx->server_table, key);
if (server != NULL) {
if (verify_server(server, readonly) == CORVUS_ERR) return NULL;
Expand Down Expand Up @@ -315,20 +315,23 @@ struct connection *conn_get_raw_server(struct context *ctx)
struct connection *conn_get_server(struct context *ctx, uint16_t slot,
int access)
{
struct address master, slave, *addr;
memset(&slave, 0, sizeof(slave));
bool readonly;

bool hitted = slot_get_node_addr(ctx, slot, &master, &slave);
if (hitted) {
if (!config.readslave || slave.port == 0 || access == CMD_ACCESS_WRITE) {
addr = &master;
readonly = false;
} else {
addr = &slave;
readonly = true;
struct address *addr;
struct node_info info;
bool readonly = false;

if (slot_get_node_addr(slot, &info)) {
addr = &info.nodes[0];
if (access != CMD_ACCESS_WRITE && config.readslave && info.index > 1) {
int r = rand_r(&ctx->seed);
if (!config.readmasterslave || r % info.index != 0) {
int i = r % (info.index - 1);
addr = &info.nodes[++i];
readonly = true;
}
}
if (addr->port > 0) {
return conn_get_server_from_pool(ctx, addr, readonly);
}
return conn_get_server_from_pool(ctx, addr, readonly);
}
return conn_get_raw_server(ctx);
}
Expand Down
2 changes: 1 addition & 1 deletion src/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct conn_info {
int refcount;

struct address addr;
char dsn[DSN_LEN + 1];
char dsn[ADDRESS_LEN + 1];

struct reader reader;

Expand Down
2 changes: 1 addition & 1 deletion src/corvus.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ int config_add(char *name, char *value)
val = atoi(value);
config.server_timeout = val < 0 ? 0 : val;
} else if (strcmp(name, "statsd") == 0) {
strncpy(config.statsd_addr, value, DSN_LEN);
strncpy(config.statsd_addr, value, ADDRESS_LEN);
} else if (strcmp(name, "metric_interval") == 0) {
config.metric_interval = atoi(value);
if (config.metric_interval <= 0) config.metric_interval = 10;
Expand Down
2 changes: 1 addition & 1 deletion src/corvus.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ struct {
int thread;
int loglevel;
bool syslog;
char statsd_addr[DSN_LEN + 1];
char statsd_addr[ADDRESS_LEN + 1];
int metric_interval;
bool stats;
bool readslave;
Expand Down
Loading

0 comments on commit 3151c5f

Please sign in to comment.