From 88cd033ca60cfb09263011915813f6a476307b7e Mon Sep 17 00:00:00 2001 From: "roberto@mrspurr" Date: Thu, 16 Jun 2011 07:08:56 +0200 Subject: [PATCH] refactored clustering --- master.c | 3 +++ protocol.c | 6 ++++- uwsgi.c | 70 ++++++------------------------------------------------ uwsgi.h | 1 - 4 files changed, 15 insertions(+), 65 deletions(-) diff --git a/master.c b/master.c index 85831f28..5e6d50dc 100644 --- a/master.c +++ b/master.c @@ -110,7 +110,10 @@ void manage_cluster_announce(char *key, uint16_t keylen, char *val, uint16_t val char *tmpstr; struct uwsgi_cluster_node *ucn = (struct uwsgi_cluster_node *) data; + +#ifdef UWSGI_DEBUG uwsgi_log("%.*s = %.*s\n", keylen, key, vallen, val); +#endif if (!uwsgi_strncmp("hostname", 8, key, keylen)) { strncpy(ucn->nodename, val, UMIN(vallen, 255)); diff --git a/protocol.c b/protocol.c index 78145a1e..23b35edf 100644 --- a/protocol.c +++ b/protocol.c @@ -825,7 +825,9 @@ int uwsgi_get_dgram(int fd, struct wsgi_request *wsgi_req) { return -1; } - uwsgi_log("recevied request from %s\n", inet_ntoa(sin.sin_addr)); +#ifdef UWSGI_DEBUG + uwsgi_log("received request from %s\n", inet_ntoa(sin.sin_addr)); +#endif if (rlen < 4) { uwsgi_log("invalid uwsgi packet\n"); @@ -849,7 +851,9 @@ int uwsgi_get_dgram(int fd, struct wsgi_request *wsgi_req) { wsgi_req->buffer = buffer+4; +#ifdef UWSGI_DEBUG uwsgi_log("request received %d %d\n", wsgi_req->uh.modifier1, wsgi_req->uh.modifier2); +#endif return 0; diff --git a/uwsgi.c b/uwsgi.c index ad9f3fb2..ea1dcc4d 100644 --- a/uwsgi.c +++ b/uwsgi.c @@ -3141,76 +3141,15 @@ void manage_opt(int i, char *optarg) { } -void uwsgi_cluster_simple_add_node(char *nodename, int workers, int type) { - - int i; - struct uwsgi_cluster_node *ucn; - char *tcp_port; - - if (strlen(nodename) > 100) { - uwsgi_log("invalid cluster node name %s\n", nodename); - return; - } - - tcp_port = strchr(nodename, ':'); - if (tcp_port == NULL) { - fprintf(stdout, "invalid cluster node name %s\n", nodename); - return; - } - - // first check for already present node - for (i = 0; i < MAX_CLUSTER_NODES; i++) { - ucn = &uwsgi.shared->nodes[i]; - if (ucn->name[0] != 0) { - if (!strcmp(ucn->name, nodename)) { - ucn->status = UWSGI_NODE_OK; - ucn->last_seen = time(NULL); - return; - } - } - } - - for (i = 0; i < MAX_CLUSTER_NODES; i++) { - ucn = &uwsgi.shared->nodes[i]; - - if (ucn->name[0] == 0) { - memcpy(ucn->name, nodename, strlen(nodename) + 1); - ucn->workers = workers; - ucn->ucn_addr.sin_family = AF_INET; - ucn->ucn_addr.sin_port = htons(atoi(tcp_port + 1)); - tcp_port[0] = 0; - if (nodename[0] == 0) { - ucn->ucn_addr.sin_addr.s_addr = INADDR_ANY; - } - else { - uwsgi_log("%s\n", nodename); - ucn->ucn_addr.sin_addr.s_addr = inet_addr(nodename); - } - - ucn->type = type; - // here memory can be freed, as it is allocated by uwsgi_concat2n - if (type == CLUSTER_NODE_DYNAMIC) { - free(nodename); - } - else { - tcp_port[0] = ':'; - } - ucn->last_seen = time(NULL); - uwsgi_log("[uWSGI cluster] added node %s\n", ucn->name); - return; - } - } - - uwsgi_log("unable to add node %s\n", nodename); -} - void uwsgi_cluster_add_node(struct uwsgi_cluster_node *nucn, int type) { int i; struct uwsgi_cluster_node *ucn; char *tcp_port; +#ifdef UWSGI_DEBUG uwsgi_log("adding node\n"); +#endif tcp_port = strchr(nucn->name, ':'); if (tcp_port == NULL) { @@ -3246,7 +3185,9 @@ void uwsgi_cluster_add_node(struct uwsgi_cluster_node *nucn, int type) { ucn->ucn_addr.sin_addr.s_addr = INADDR_ANY; } else { +#ifdef UWSGI_DEBUG uwsgi_log("%s\n", nucn->name); +#endif ucn->ucn_addr.sin_addr.s_addr = inet_addr(nucn->name); } @@ -3483,7 +3424,9 @@ int uwsgi_cluster_add_me() { free(buf); +#ifdef UWSGI_DEBUG uwsgi_log("add_me() successfull\n"); +#endif return 0; } @@ -3495,6 +3438,7 @@ int uwsgi_cluster_join(char *name) { int broadcast = 0; + if (name[0] == ':') { fd = bind_to_udp(name, 0, 1); broadcast = 1; diff --git a/uwsgi.h b/uwsgi.h index a91fcbed..f533f8f6 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -1516,7 +1516,6 @@ int uwsgi_enqueue_message(char *, int, uint8_t, uint8_t, char *, int void manage_opt(int, char *); void uwsgi_cluster_add_node(struct uwsgi_cluster_node *, int); -void uwsgi_cluster_simple_add_node(char *, int, int); int uwsgi_ping_node(int, struct wsgi_request *); struct http_status_codes {