-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add config set
command to modify corresponding redis nodes dynamically
#95
Changes from all commits
f083bd2
a1642dd
88f54f7
0f42521
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
#include <pthread.h> | ||
#include <unistd.h> | ||
#include <execinfo.h> | ||
#include <assert.h> | ||
#include <getopt.h> | ||
#include "corvus.h" | ||
#include "mbuf.h" | ||
|
@@ -22,14 +23,16 @@ | |
#define MIN_BUFSIZE 64 | ||
|
||
static struct context *contexts; | ||
static pthread_mutex_t lock_conf_node = PTHREAD_MUTEX_INITIALIZER; | ||
|
||
void config_init() | ||
{ | ||
memset(config.cluster, 0, CLUSTER_NAME_SIZE + 1); | ||
strncpy(config.cluster, "default", CLUSTER_NAME_SIZE); | ||
|
||
config.bind = 12345; | ||
memset(&config.node, 0, sizeof(struct node_conf)); | ||
config.node = cv_calloc(1, sizeof(struct node_conf)); | ||
config.node->refcount = 1; | ||
config.thread = 4; | ||
config.loglevel = INFO; | ||
config.syslog = 0; | ||
|
@@ -135,30 +138,58 @@ int config_add(char *name, char *value) | |
memcpy(config.requirepass, value, strlen(value)); | ||
} | ||
} else if (strcmp(name, "node") == 0) { | ||
cv_free(config.node.addr); | ||
memset(&config.node, 0, sizeof(config.node)); | ||
|
||
struct address *addr = NULL; | ||
int addr_cnt = 0; | ||
char *p = strtok(value, ","); | ||
while (p) { | ||
config.node.addr = cv_realloc(config.node.addr, | ||
sizeof(struct address) * (config.node.len + 1)); | ||
|
||
if (socket_parse_ip(p, &config.node.addr[config.node.len]) == -1) { | ||
cv_free(config.node.addr); | ||
return -1; | ||
addr = cv_realloc(addr, sizeof(struct address) * (addr_cnt + 1)); | ||
if (socket_parse_ip(p, &addr[addr_cnt]) == -1) { | ||
cv_free(addr); | ||
return CORVUS_ERR; | ||
} | ||
|
||
config.node.len++; | ||
addr_cnt++; | ||
p = strtok(NULL, ","); | ||
} | ||
{ | ||
struct node_conf *newnode = cv_calloc(1, sizeof(struct node_conf)); | ||
memset(newnode, 0, sizeof(struct node_conf)); | ||
newnode->addr = addr; | ||
newnode->len = addr_cnt; | ||
newnode->refcount = 1; | ||
pthread_mutex_lock(&lock_conf_node); | ||
struct node_conf *oldnode = config.node; | ||
config.node = newnode; | ||
pthread_mutex_unlock(&lock_conf_node); | ||
conf_node_dec_ref(oldnode); | ||
} | ||
} else if (strcmp(name, "slowlog-log-slower-than") == 0) { | ||
config.slowlog_log_slower_than = atoi(value); | ||
} else if (strcmp(name, "slowlog-max-len") == 0) { | ||
config.slowlog_max_len = atoi(value); | ||
} else if (strcmp(name, "slowlog-statsd-enabled") == 0) { | ||
config.slowlog_statsd_enabled = atoi(value); | ||
} | ||
return 0; | ||
return CORVUS_OK; | ||
} | ||
|
||
struct node_conf *conf_node_inc_ref() | ||
{ | ||
pthread_mutex_lock(&lock_conf_node); | ||
struct node_conf *node = config.node; | ||
int refcount = ATOMIC_INC(node->refcount, 1); | ||
assert(refcount >= 0); | ||
pthread_mutex_unlock(&lock_conf_node); | ||
return node; | ||
} | ||
|
||
void conf_node_dec_ref(struct node_conf *node) | ||
{ | ||
int refcount = ATOMIC_DEC(node->refcount, 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can safely remove the lock here. Thanks to the atomic operation multiple calling of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, so just remove it |
||
assert(refcount >= 0); | ||
if (refcount == 0) { | ||
cv_free(node->addr); | ||
cv_free(node); | ||
} | ||
} | ||
|
||
int read_line(char **line, size_t *bytes, FILE *fp) | ||
|
@@ -595,7 +626,7 @@ int main(int argc, const char *argv[]) | |
usage(argv[0]); | ||
return EXIT_FAILURE; | ||
} | ||
if (config.node.len <= 0) { | ||
if (config.node->len <= 0) { | ||
fprintf(stderr, "Error: invalid upstream list, `node` should be set to a valid nodes list.\n"); | ||
return EXIT_FAILURE; | ||
} | ||
|
@@ -668,7 +699,8 @@ int main(int argc, const char *argv[]) | |
destroy_contexts(); | ||
cmd_map_destroy(); | ||
cv_free(config.requirepass); | ||
cv_free(config.node.addr); | ||
conf_node_dec_ref(config.node); | ||
pthread_mutex_destroy(&lock_conf_node); | ||
if (config.syslog) closelog(); | ||
return EXIT_SUCCESS; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
#include "test.h" | ||
#include "corvus.h" | ||
#include "slot.h" | ||
#include "alloc.h" | ||
|
||
static void usage(const char *name) | ||
{ | ||
|
@@ -83,7 +84,9 @@ int main(int argc, const char *argv[]) | |
build_contexts(); | ||
struct context *contexts = get_contexts(); | ||
|
||
memcpy(&config.node, &conf, sizeof(config.node)); | ||
config.node = cv_malloc(sizeof(struct node_conf)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, checked for newest version and change all malloc to calloc |
||
memset(config.node, 0, sizeof(struct node_conf)); | ||
config.node->refcount = 1; | ||
slot_start_manager(&contexts[config.thread]); | ||
|
||
RUN_CASE(test_slot); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'd better check
data->elements
here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aha, sorry about changes after merging.
I change the assert for elements judging ensure that.