Author: jfrederic.clere(a)jboss.com
Date: 2008-10-06 03:51:14 -0400 (Mon, 06 Oct 2008)
New Revision: 1935
Modified:
trunk/mod_cluster/native/mod_proxy_cluster/mod_proxy_cluster.c
Log:
Remove duplicate code.
Modified: trunk/mod_cluster/native/mod_proxy_cluster/mod_proxy_cluster.c
===================================================================
--- trunk/mod_cluster/native/mod_proxy_cluster/mod_proxy_cluster.c 2008-10-03 11:24:07 UTC
(rev 1934)
+++ trunk/mod_cluster/native/mod_proxy_cluster/mod_proxy_cluster.c 2008-10-06 07:51:14 UTC
(rev 1935)
@@ -519,15 +519,36 @@
return NULL;
}
-/*
+/**
* Check that the request has a sessionid (even invalid)
+ * @param r the request_rec.
+ * @param balancer_name name of the balancer. (to find the balancer)
+ * @param conf the proxy configuration.
+ * @param balance the balancer (balancer to use).
+ * @return 1 is it finds a sessionid 0 otherwise.
*/
-static int hassession(request_rec *r, proxy_balancer *balancer)
+static int hassession_byname(request_rec *r, char *balancer_name, proxy_server_conf
*conf, proxy_balancer *balance)
{
+ proxy_balancer *balancer = balance;
char *route;
char *uri = r->filename + 6;
char *sticky_path, *sticky, *path;
+ int i;
+ if (balancer == NULL) {
+ balancer = (proxy_balancer *)conf->balancers->elts;
+ for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
+ if (strcasecmp(balancer->name, uri) == 0)
+ break;
+ }
+ if (i == conf->balancers->nelts)
+ balancer = NULL;
+ }
+
+ /* XXX: We don't find the balancer, that is BAD */
+ if (balancer == NULL)
+ return 0;
+
if (balancer->sticky == NULL)
return 0;
@@ -548,20 +569,24 @@
return 0;
}
-/*
- * Check that the worker will handle the host/context.
- * The id of the worker is used to find the (slot) node in the shared
- * memory.
- * (See get_context_host_balancer too).
- */
-static int iscontext_host_ok(request_rec *r, proxy_balancer *balancer,
- proxy_worker *worker)
-{
+/**
+ * Find/Check the balancer corresponding to the request and the node.
+ * @param r the request_rec.
+ * @param node the node to use.
+ * @param conf the proxy configuration.
+ * @param balance the balancer (balancer to use in that case we check it).
+ * @return the name of the balancer or NULL if not found/not corresponding
+ */
+
+
+static char *get_balancer_by_node(request_rec *r, int nodeid, proxy_server_conf *conf,
proxy_balancer *balance)
+{
nodeinfo_t *node;
- int sizevhost, i;
+ int i;
+ int sizevhost;
int *vhosts;
- node_storage->read_node(worker->id, &node);
+ node_storage->read_node(nodeid, &node);
/*
* check the hosts and contexts
@@ -571,6 +596,8 @@
sizevhost = host_storage->get_max_size_host();
vhosts = apr_palloc(r->pool, sizeof(int)*sizevhost);
sizevhost = host_storage->get_ids_used_host(vhosts);
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
+ "get_balancer_by_node testing node %s for %s",
node->mess.JVMRoute, r->uri);
for (i=0; i<sizevhost; i++) {
hostinfo_t *vhost;
host_storage->read_host(vhosts[i], &vhost);
@@ -578,101 +605,57 @@
/* XXX Check the virtual host */
/* Check the contexts */
- int j, len;
+ int j;
int sizecontext = context_storage->get_max_size_context();
int *contexts = apr_palloc(r->pool, sizeof(int)*sizecontext);
sizecontext = context_storage->get_ids_used_context(contexts);
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
+ "get_balancer_by_node testing host %s",
vhost->host);
for (j=0; j<sizecontext; j++) {
contextinfo_t *context;
+ int len;
context_storage->read_context(contexts[j], &context);
- if (context->vhost != vhost->vhost)
+ if (context->vhost != vhost->vhost || (context->node !=
node->mess.id))
continue;
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
+ "get_balancer_by_node testing context %s",
context->context);
/* check for /context[/] in the URL */
len = strlen(context->context);
if (strncmp(r->uri, context->context, len) == 0) {
- if (r->uri[len] == '\0' || r->uri[len] == '/'
|| len == 1) {
+ if (r->uri[len] == '\0' || r->uri[len] == '/'
|| len==1) {
/* Check status */
switch (context->status)
{
case ENABLED:
- return 1;
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
+ "get_balancer_by_node found context
%s", context->context);
+ return node->mess.balancer;
break;
case DISABLED:
/* Only the request with sessionid ok for it */
- if (hassession(r, balancer))
- return 1;
+ if (hassession_byname(r, node->mess.balancer, conf,
NULL))
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
+ "get_balancer_by_node found (DISABLED)
context %s", context->context);
+ return node->mess.balancer;
break;
}
}
}
}
-
+
}
}
- return 0;
-}
-
-/*
- * Check that the worker corresponds to a node that belongs to the same domain according
to the JVMRoute.
- */
-static int isnode_domain_ok(request_rec *r, proxy_worker *worker,
- const char *domain)
-{
- nodeinfo_t *ou;
- node_storage->read_node(worker->id, &ou);
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
- "isnode_domain_ok: domain %s:%s", domain,
ou->mess.Domain);
- if (domain == NULL)
- return 1; /* OK no domain in the corresponding to the SESSIONID */
- if (strcmp(ou->mess.Domain, domain) == 0)
- return 1; /* OK */
- return 0;
-}
-/*
- * Check that the request has a sessionid (even invalid)
- * Use the name of the balancer.
- */
-static int hassession_byname(request_rec *r, char *balancer_name, proxy_server_conf
*conf)
-{
- proxy_balancer *balancer;
- char *route;
- char *uri = r->filename + 6;
- char *sticky_path, *sticky, *path;
- int i;
+ "get_balancer_by_node balancer NOT found");
+ return NULL;
- balancer = (proxy_balancer *)conf->balancers->elts;
- for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
- if (strcasecmp(balancer->name, uri) == 0)
- break;
- }
-
- /* XXX: We don't find the balancer, that is BAD */
- if (i == conf->balancers->nelts)
- return 0;
-
- if (balancer->sticky == NULL)
- return 0;
-
- /* for 2.2.x the sticky parameter may contain 2 values */
- sticky = sticky_path = apr_pstrdup(r->pool, balancer->sticky);
- if ((path = strchr(sticky, '|'))) {
- *path++ = '\0';
- sticky_path = path;
- }
- route = get_path_param(r->pool, uri , sticky_path);
- if (route == NULL)
- route = get_cookie_param(r, sticky);
- if (route) {
- ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
- "mod_proxy_cluster: found route %s", route);
- return 1;
- }
- return 0;
}
-/*
+/**
* Search the balancer that corresponds to the pair context/host
- * (See iscontext_host_ok()).
+ * @param r the request_rec.
+ * @balancer proxy_ ARF....
+ * @return the balancer or NULL if not found.
*/
static char *get_context_host_balancer(request_rec *r)
{
@@ -685,78 +668,46 @@
int *nodes = apr_palloc(r->pool, sizeof(int)*sizenode);
sizenode = node_storage->get_ids_used_node(nodes);
for (n=0; n<sizenode; n++) {
- nodeinfo_t *node;
- int i;
- int sizevhost;
- int *vhosts;
-
- node_storage->read_node(nodes[n], &node);
-
- /*
- * check the hosts and contexts
- * A node may have several virtual hosts and
- * each virtual hosts may have several context
- */
- sizevhost = host_storage->get_max_size_host();
- vhosts = apr_palloc(r->pool, sizeof(int)*sizevhost);
- sizevhost = host_storage->get_ids_used_host(vhosts);
- ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
- "get_context_host_balancer testing node %s for %s",
node->mess.JVMRoute, r->uri);
- for (i=0; i<sizevhost; i++) {
- hostinfo_t *vhost;
- host_storage->read_host(vhosts[i], &vhost);
- if (vhost->node == node->mess.id) {
- /* XXX Check the virtual host */
-
- /* Check the contexts */
- int j;
- int sizecontext = context_storage->get_max_size_context();
- int *contexts = apr_palloc(r->pool, sizeof(int)*sizecontext);
- sizecontext = context_storage->get_ids_used_context(contexts);
- ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
- "get_context_host_balancer testing host %s",
vhost->host);
- for (j=0; j<sizecontext; j++) {
- contextinfo_t *context;
- int len;
- context_storage->read_context(contexts[j], &context);
- if (context->vhost != vhost->vhost)
- continue;
- ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
- "get_context_host_balancer testing context
%s", context->context);
-
- /* check for /context[/] in the URL */
- len = strlen(context->context);
- if (strncmp(r->uri, context->context, len) == 0) {
- if (r->uri[len] == '\0' || r->uri[len] ==
'/' || len==1) {
- /* Check status */
- switch (context->status)
- {
- case ENABLED:
- ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
- "get_context_host_balancer found
context %s", context->context);
- return node->mess.balancer;
- break;
- case DISABLED:
- /* Only the request with sessionid ok for it */
- if (hassession_byname(r, node->mess.balancer, conf))
- ap_log_error(APLOG_MARK, APLOG_DEBUG, 0,
r->server,
- "get_context_host_balancer found
(DISABLED) context %s", context->context);
- return node->mess.balancer;
- break;
- }
- }
- }
- }
-
- }
+ char *ret = get_balancer_by_node(r, nodes[n], conf, NULL);
+ if (ret != NULL) {
+ return ret;
}
}
- ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
- "get_context_host_balancer NOT found");
return NULL;
}
+/*
+ * Check that the worker will handle the host/context.
+ * The id of the worker is used to find the (slot) node in the shared
+ * memory.
+ * (See get_context_host_balancer too).
+ */
+static int iscontext_host_ok(request_rec *r, proxy_balancer *balancer, int node)
+{
+ char *balancername = get_balancer_by_node(r, node, NULL, balancer);
+ if (balancername != NULL) {
+ return 1; /* Found */
+ }
+ return 0;
+}
/*
+ * Check that the worker corresponds to a node that belongs to the same domain according
to the JVMRoute.
+ */
+static int isnode_domain_ok(request_rec *r, proxy_worker *worker,
+ const char *domain)
+{
+ nodeinfo_t *ou;
+ node_storage->read_node(worker->id, &ou);
+ ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
+ "isnode_domain_ok: domain %s:%s", domain,
ou->mess.Domain);
+ if (domain == NULL)
+ return 1; /* OK no domain in the corresponding to the SESSIONID */
+ if (strcmp(ou->mess.Domain, domain) == 0)
+ return 1; /* OK */
+ return 0;
+}
+
+/*
* The ModClusterService from the cluster fills the lbfactor values.
* Our logic is a bit different the mod_balancer one. We check the
* context and host to prevent to route to application beeing redeploy or
@@ -811,7 +762,7 @@
* not in error state or not disabled.
* and that can map the context.
*/
- if (PROXY_WORKER_IS_USABLE(worker) && iscontext_host_ok(r, balancer,
worker)) {
+ if (PROXY_WORKER_IS_USABLE(worker) && iscontext_host_ok(r, balancer,
worker->id)) {
if (!checked_domain) {
/* First try only nodes in the domain */
if (!isnode_domain_ok(r, worker, domain)) {