Skip to content

Commit

Permalink
Don't clear 'remotes' field from 'info' command during updating slot map
Browse files Browse the repository at this point in the history
  • Loading branch information
doyoubi committed Feb 14, 2017
1 parent 0318b8b commit 6164134
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
24 changes: 18 additions & 6 deletions src/slot.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ static inline void node_list_init()
conf_node_dec_ref(node);
}

static inline void node_list_add(struct node_info *node)
static inline void node_list_add(struct address *dst, size_t *len, struct node_info *node)
{
pthread_rwlock_wrlock(&node_list.lock);
for (size_t i = 0; i < node->index && node_list.len < MAX_NODE_LIST; i++) {
memcpy(&node_list.nodes[node_list.len++], &node->nodes[i],
for (size_t i = 0; i < node->index && *len < MAX_NODE_LIST; i++) {
memcpy(&dst[(*len)++], &node->nodes[i],
sizeof(struct address));
}
}

static inline void node_list_replace(struct address *nodes, size_t len)
{
pthread_rwlock_wrlock(&node_list.lock);
node_list.len = len;
memcpy(node_list.nodes, nodes, sizeof(struct address) * len);
pthread_rwlock_unlock(&node_list.lock);
}

Expand Down Expand Up @@ -111,6 +117,8 @@ int parse_slots()
char *p;
int start, stop, slot_count = 0;
struct node_info *node;
struct address tmp_nodes[MAX_NODE_LIST];
size_t tmp_nodes_len = 0;

struct dict_iter iter = DICT_ITER_INITIALIZER;
DICT_FOREACH(&slot_map.node_map, &iter) {
Expand All @@ -122,7 +130,7 @@ int parse_slots()
continue;
}

node_list_add(n);
node_list_add(tmp_nodes, &tmp_nodes_len, n);

for (int i = 8; i < n->spec_length + 8; i++) {
if (n->slot_spec[i].data[0] == '[') {
Expand Down Expand Up @@ -158,6 +166,10 @@ int parse_slots()
node_map_free(&slot_map.free_nodes);
pthread_rwlock_unlock(&slot_map.lock);

if (slot_count == REDIS_CLUSTER_SLOTS) {
node_list_replace(tmp_nodes, tmp_nodes_len);
}

dict_clear(&slot_map.free_nodes);
return slot_count;
}
Expand Down Expand Up @@ -288,7 +300,6 @@ void slot_map_update(struct context *ctx)
struct address nodes[MAX_NODE_LIST];
memcpy(nodes, node_list.nodes, sizeof(node_list.nodes));
int len = node_list.len;
node_list.len = 0;

for (i = len; i > 0; i--) {
int r = rand_r(&ctx->seed) % i;
Expand Down Expand Up @@ -318,6 +329,7 @@ void slot_map_update(struct context *ctx)
conn_recycle(ctx, server);

if (count == CORVUS_ERR) {
node_list.len = 0; // clear it if we can't update slot map
LOG(WARN, "can not update slot map");
} else {
LOG(INFO, "slot map updated: corverd %d slots", count);
Expand Down
3 changes: 2 additions & 1 deletion src/slot.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ struct node_desc {

struct node_info {
char name[64];
// contains master and slaves of one shard
struct address nodes[MAX_SLAVE_NODES + 1];
size_t index;
size_t index; // length of `nodes` above
int refcount;
// for parsing slots of a master node
struct desc_part *slot_spec;
Expand Down

0 comments on commit 6164134

Please sign in to comment.