Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion corvus.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bind 12345
port 12345
node localhost:8000,localhost:8001,localhost:8002
thread 4

Expand Down
17 changes: 14 additions & 3 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ static pthread_mutex_t lock_config_rewrite = PTHREAD_MUTEX_INITIALIZER;
const char * CONFIG_OPTIONS[] = {
"cluster",
"bind",
"port",
"node",
"thread",
"loglevel",
Expand All @@ -44,7 +45,8 @@ void config_init()
memset(config.cluster, 0, CLUSTER_NAME_SIZE + 1);
strncpy(config.cluster, "default", CLUSTER_NAME_SIZE);

config.bind = 12345;
strncpy(config.bind, "0.0.0.0", CONFIG_BINDADDR_MAX);
config.port = 12345;
config.node = cv_calloc(1, sizeof(struct node_conf));
config.node->refcount = 1;
config.thread = 4;
Expand Down Expand Up @@ -172,7 +174,14 @@ int config_add(char *name, char *value)
if (strlen(value) <= 0) return CORVUS_OK;
strncpy(config.cluster, value, CLUSTER_NAME_SIZE);
} else if (strcmp(name, "bind") == 0) {
if (socket_parse_port(value, &config.bind) == CORVUS_ERR) {
size_t len = strlen(value);
if (len == 0 || len > CONFIG_BINDADDR_MAX) {
return CORVUS_ERR;
}

strncpy(config.bind, value, CONFIG_BINDADDR_MAX);
} else if (strcmp(name, "port") == 0) {
if (socket_parse_port(value, &config.port) == CORVUS_ERR) {
return CORVUS_ERR;
}
} else if (strcmp(name, "syslog") == 0) {
Expand Down Expand Up @@ -289,7 +298,9 @@ int config_get(const char *name, char *value, size_t max_len)
if (strcmp(name, "cluster") == 0) {
strncpy(value, config.cluster, max_len);
} else if (strcmp(name, "bind") == 0) {
snprintf(value, max_len, "%u", config.bind);
snprintf(value, max_len, "%s", config.bind);
} else if (strcmp(name, "port") == 0) {
snprintf(value, max_len, "%u", config.port);
} else if (strcmp(name, "node") == 0) {
config_node_to_str(value, max_len);
} else if (strcmp(name, "thread") == 0) {
Expand Down
6 changes: 4 additions & 2 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#define CLUSTER_NAME_SIZE 127
#define CONFIG_FILE_PATH_SIZE 256
#define CONFIG_BINDADDR_MAX 46

struct node_conf {
struct address *addr;
Expand All @@ -15,7 +16,8 @@ struct node_conf {
struct corvus_config {
char config_file_path[CONFIG_FILE_PATH_SIZE + 1];
char cluster[CLUSTER_NAME_SIZE + 1];
uint16_t bind;
char bind[CONFIG_BINDADDR_MAX];
uint16_t port;
struct node_conf *node;
int thread;
int loglevel;
Expand Down Expand Up @@ -47,4 +49,4 @@ void config_node_dec_ref(struct node_conf *node);
int config_add(char *name, char *value);
bool config_option_changable(const char *option);

#endif /* end of include guard: CONFIG_H */
#endif /* end of include guard: CONFIG_H */
11 changes: 8 additions & 3 deletions src/corvus.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ void *main_loop(void *data)
exit(EXIT_FAILURE);
}

if (proxy_init(&ctx->proxy, ctx, "0.0.0.0", config.bind) == -1) {
if (proxy_init(&ctx->proxy, ctx, config.bind, config.port) == -1) {
LOG(ERROR, "Fatal: fail to create proxy.");
exit(EXIT_FAILURE);
}
Expand Down Expand Up @@ -312,6 +312,7 @@ static const struct option opts[] = {
{"threads", required_argument, NULL, 't'},
{"cluster", required_argument, NULL, 'c'},
{"bind", required_argument, NULL, 'b'},
{"port", required_argument, NULL, 'p'},
{"syslog", required_argument, NULL, 'l'},
{"read-strategy", required_argument, NULL, 's'},
{"bufsize", required_argument, NULL, 'B'},
Expand All @@ -331,6 +332,7 @@ static const char *opts_desc[] = {
"nodes of targeting cluster. eg: 192.168.123.2:6391,192.168.123.2:6392",
"thread num",
"cluster name",
"IP to listen on",
"port to listen",
"whether writing log to syslog. 0 or 1",
"whether sending all cmd to both masters and slaves. read-slave-only|both or \"\" by default to masters",
Expand Down Expand Up @@ -363,7 +365,7 @@ static int parameter_init(int argc, const char *argv[]) {
int ch;
opterr = optind = 0;
do {
ch = getopt_long(argc, (char * const *)argv, ":n:t:c:b:l:s:B:C:S:A:m:L:P:g:G:E:", opts, NULL);
ch = getopt_long(argc, (char * const *)argv, ":n:t:c:b:p:l:s:B:C:S:A:m:L:P:g:G:E:", opts, NULL);
if (ch < 0) {
break;
}
Expand All @@ -380,6 +382,9 @@ static int parameter_init(int argc, const char *argv[]) {
case 'b':
config_add("bind", optarg);
break;
case 'p':
config_add("port", optarg);
break;
case 'l':
config_add("syslog", optarg);
break;
Expand Down Expand Up @@ -503,7 +508,7 @@ int main(int argc, const char *argv[])
stats_init();
}

LOG(INFO, "serve at 0.0.0.0:%d", config.bind);
LOG(INFO, "serve at %s:%d", config.bind, config.port);

for (i = 0; i < config.thread; i++) {
if ((err = pthread_join(contexts[i].thread, NULL)) != 0) {
Expand Down
8 changes: 4 additions & 4 deletions src/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ void logger(const char *file, int line, int level, const char *fmt, ...)
va_end(ap);

if (config.syslog) {
syslog(SYSLOG_LEVEL_MAP[level], "[%s %d %d %d] %s", config.cluster,
(int)config.bind, (int)process_id, (int)thread_id, msg);
syslog(SYSLOG_LEVEL_MAP[level], "[%s %s:%d %d %d] %s", config.cluster,
config.bind, (int)config.port, (int)process_id, (int)thread_id, msg);
} else {
gettimeofday(&now, NULL);
int n = strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S,",
localtime(&now.tv_sec));
snprintf(timestamp + n, sizeof(timestamp) - n, "%03d", (int)now.tv_usec/1000);
fprintf(stderr, "%s %s [%s %d %d %d]: %s (%s:%d)\n", timestamp, LEVEL_MAP[level],
config.cluster, (int)config.bind, (int)process_id, (int)thread_id, msg, file, line);
fprintf(stderr, "%s %s [%s %s:%d %d %d]: %s (%s:%d)\n", timestamp, LEVEL_MAP[level],
config.cluster, config.bind, (int)config.port, (int)process_id, (int)thread_id, msg, file, line);
}
}
2 changes: 1 addition & 1 deletion src/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static int cv_getaddrinfo(const char *addr, int port, struct addrinfo **servinfo

snprintf(port_str, 6, "%d", port);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = socktype;

if ((err = getaddrinfo(addr, port_str, &hints, servinfo)) != 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ static void stats_send(char *metric, double value)

int n;
const char *fmt = "corvus.%s.%s-%d.%s:%f|g";
n = snprintf(NULL, 0, fmt, config.cluster, hostname, config.bind, metric, value);
n = snprintf(NULL, 0, fmt, config.cluster, hostname, config.port, metric, value);
char buf[n + 1];
snprintf(buf, sizeof(buf), fmt, config.cluster, hostname, config.bind, metric, value);
snprintf(buf, sizeof(buf), fmt, config.cluster, hostname, config.port, metric, value);
if (sendto(statsd_fd, buf, n, 0, (struct sockaddr*)&dest, sizeof(dest)) == -1) {
LOG(WARN, "fail to send metrics data: %s", strerror(errno));
}
Expand Down
8 changes: 4 additions & 4 deletions tests/test_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
#include "alloc.h"
#include "config.h"

TEST(test_config_bind) {
char n[] = "bind";
TEST(test_config_port) {
char n[] = "port";

ASSERT(config_add(n, "123456") == -1);
ASSERT(config_add(n, "123asf") == -1);
ASSERT(config_add(n, "-1243") == -1);
ASSERT(config_add(n, "") == -1);
ASSERT(config_add(n, "abc") == -1);
ASSERT(config_add(n, "2345") == 0);
ASSERT(config.bind == 2345);
ASSERT(config.port == 2345);

PASS(NULL);
}
Expand Down Expand Up @@ -165,7 +165,7 @@ TEST(test_parse_int) {
}

TEST_CASE(test_config) {
RUN_TEST(test_config_bind);
RUN_TEST(test_config_port);
RUN_TEST(test_config_syslog);
RUN_TEST(test_config_requirepass);
RUN_TEST(test_config_read_strategy);
Expand Down