Skip to content

Commit 3cd7ad6

Browse files
committed
Add support for HCTemplates to mod_lbmethod_cluster
1 parent 98aac5b commit 3cd7ad6

6 files changed

Lines changed: 108 additions & 4 deletions

File tree

native/balancers/mod_lbmethod_cluster.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "mod_watchdog.h"
77
#include "common.h"
88

9-
109
#define LB_CLUSTER_WATHCHDOG_NAME ("_lb_cluster_")
1110
static ap_watchdog_t *watchdog;
1211

@@ -16,6 +15,9 @@ static struct context_storage_method *context_storage = NULL;
1615
static struct balancer_storage_method *balancer_storage = NULL;
1716
static struct domain_storage_method *domain_storage = NULL;
1817

18+
static apr_table_t *proxyhctemplate;
19+
static void (*set_proxyhctemplate_f)(apr_pool_t *, apr_table_t *) = NULL;
20+
1921
static int use_alias = 0; /* 1 : Compare Alias with server_name */
2022
static int use_nocanon = 0;
2123
static apr_time_t lbstatus_recalc_time =
@@ -362,6 +364,13 @@ static int lbmethod_cluster_post_config(apr_pool_t *p, apr_pool_t *plog, apr_poo
362364
ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, "proxy_cluster_post_config: Can't find mod_manager for domains");
363365
return !OK;
364366
}
367+
set_proxyhctemplate_f = ap_lookup_provider("manager", "shared", "6");
368+
if (set_proxyhctemplate_f == NULL) {
369+
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
370+
"lbmethod_cluster_post_config: Can't find mod_manager for set_proxyhctemplate function");
371+
} else if (!apr_is_empty_table(proxyhctemplate)) {
372+
set_proxyhctemplate_f(p, proxyhctemplate);
373+
}
365374

366375
/* Add version information */
367376
ap_add_version_component(p, MOD_CLUSTER_EXPOSED_VERSION);
@@ -405,10 +414,30 @@ static const char *cmd_nocanon(cmd_parms *parms, void *mconfig, int on)
405414
return NULL;
406415
}
407416

417+
static const char *cmd_proxyhctemplate(cmd_parms *cmd, void *dummy, const char *arg)
418+
{
419+
const char *err = NULL;
420+
(void)dummy;
421+
422+
if (!proxyhctemplate) {
423+
proxyhctemplate = apr_table_make(cmd->pool, 8);
424+
}
425+
426+
err = parse_proxyhctemplate_params(cmd->pool, arg, proxyhctemplate);
427+
if (err != NULL) {
428+
return err;
429+
}
430+
return NULL;
431+
}
432+
408433
static const command_rec lbmethod_cmds[] = {
409434
AP_INIT_FLAG("UseNocanon", cmd_nocanon, NULL, OR_ALL,
410435
"UseNocanon - When no ProxyPass or ProxyMatch for the URL, passes the URL path \"raw\" to the backend "
411-
"(Default: Off)")};
436+
"(Default: Off)"),
437+
AP_INIT_RAW_ARGS(
438+
"ModProxyClusterHCTemplate", cmd_proxyhctemplate, NULL, OR_ALL,
439+
"ModProxyClusterHCTemplate - Set of health check parameters to use with mod_lbmethod_cluster workers."),
440+
{NULL}};
412441

413442
static void register_hooks(apr_pool_t *p)
414443
{

native/common/common.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,3 +670,19 @@ const node_context *context_host_ok(request_rec *r, const proxy_balancer *balanc
670670

671671
return best->node != -1 ? best : NULL;
672672
}
673+
674+
const char *parse_proxyhctemplate_params(apr_pool_t *pool, const char *arg, apr_table_t *params)
675+
{
676+
while (*arg) {
677+
char *key, *val;
678+
key = ap_getword_conf(pool, &arg);
679+
val = strchr(key, '=');
680+
if (!val) {
681+
return "Invalid ProxyHCTemplate parameter. Parameter must be in the form 'key=value'";
682+
}
683+
*val++ = '\0';
684+
685+
apr_table_add(params, key, val);
686+
}
687+
return NULL;
688+
}

native/include/balancer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,10 @@ struct balancer_storage_method
143143
*/
144144
int (*get_max_size_balancer)(void);
145145
};
146+
147+
/**
148+
* Helper function for translating hcheck template parameters to corresponding balancer parameters
149+
*/
150+
const char *translate_balancer_params(const char *param);
151+
146152
#endif /*BALANCER_H*/

native/include/common.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
* Common routines
88
*/
99

10-
1110
#include "mod_proxy_cluster.h"
1211

1312

@@ -17,7 +16,6 @@ struct counter
1716
int *values;
1817
};
1918

20-
2119
/**
2220
* Read the virtual host table from shared memory
2321
* @param pool pool to use for memory allocation
@@ -222,3 +220,9 @@ apr_status_t loc_get_id(void *mem, void *data, apr_pool_t *pool);
222220
const node_context *context_host_ok(request_rec *r, const proxy_balancer *balancer, int node, int use_alias,
223221
const proxy_vhost_table *vhost_table, const proxy_context_table *context_table,
224222
const proxy_node_table *node_table);
223+
224+
/**
225+
* Parse ProxyHCTemplate parameters from @param arg into @param params using @param pool for allocations
226+
* @return error message or NULL if everything went well
227+
*/
228+
const char *parse_proxyhctemplate_params(apr_pool_t *pool, const char *arg, apr_table_t *params);

native/mod_manager/balancer.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,27 @@ mem_t *create_mem_balancer(char *string, unsigned *num, int persist, apr_pool_t
174174
{
175175
return create_attach_mem_balancer(string, num, persist, 1, p, storage);
176176
}
177+
178+
const char *translate_balancer_params(const char *param)
179+
{
180+
if (strcasecmp(param, "hcinterval") == 0) {
181+
return "w_hi";
182+
}
183+
if (strcasecmp(param, "hcpasses") == 0) {
184+
return "w_hp";
185+
}
186+
if (strcasecmp(param, "hcfails") == 0) {
187+
return "w_hf";
188+
}
189+
if (strcasecmp(param, "hcmethod") == 0) {
190+
return "w_hm";
191+
}
192+
if (strcasecmp(param, "hcuri") == 0) {
193+
return "w_hu";
194+
}
195+
if (strcasecmp(param, "hcexpr") == 0) {
196+
return "w_he";
197+
}
198+
199+
return NULL;
200+
}

native/mod_manager/mod_manager.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ static mem_t *hoststatsmem = NULL;
107107
static mem_t *balancerstatsmem = NULL;
108108
static mem_t *sessionidstatsmem = NULL;
109109
static mem_t *domainstatsmem = NULL;
110+
/* Used for HCExpr templates with lbmethod_cluster */
111+
static apr_table_t *proxyhctemplate = NULL;
112+
113+
static void set_proxyhctemplate(apr_pool_t *p, apr_table_t *t)
114+
{
115+
proxyhctemplate = apr_table_overlay(p, t, proxyhctemplate);
116+
}
110117

111118
static slotmem_storage_method *storage = NULL;
112119
static balancer_method *balancerhandler = NULL;
@@ -540,6 +547,7 @@ static int manager_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *p
540547
(void)ptemp;
541548
ap_mutex_register(pconf, node_mutex_type, NULL, APR_LOCK_DEFAULT, 0);
542549
ap_mutex_register(pconf, context_mutex_type, NULL, APR_LOCK_DEFAULT, 0);
550+
proxyhctemplate = apr_table_make(plog, 1);
543551
return OK;
544552
}
545553

@@ -910,6 +918,11 @@ static int is_same_worker_existing(const request_rec *r, const nodeinfo_t *node)
910918
static apr_status_t mod_manager_manage_worker(request_rec *r, const nodeinfo_t *node, const balancerinfo_t *bal)
911919
{
912920
apr_table_t *params;
921+
apr_status_t rv;
922+
int i;
923+
const apr_array_header_t *h;
924+
const apr_table_entry_t *entries;
925+
913926
params = apr_table_make(r->pool, 10);
914927
/* balancer */
915928
apr_table_set(params, "b", node->mess.balancer);
@@ -938,6 +951,17 @@ static apr_status_t mod_manager_manage_worker(request_rec *r, const nodeinfo_t *
938951

939952
/* Use 10 sec for the moment, the idea is to adjust it with the STATUS frequency */
940953
apr_table_set(params, "w_hi", "10000");
954+
955+
h = apr_table_elts(proxyhctemplate);
956+
entries = (const apr_table_entry_t *)h->elts;
957+
958+
for (i = 0; i < h->nelts; i++) {
959+
const char *key = translate_balancer_params(entries[i].key);
960+
if (key != NULL) {
961+
apr_table_set(params, key, entries[i].val);
962+
}
963+
}
964+
941965
return balancer_manage(r, params);
942966
}
943967

@@ -3935,6 +3959,7 @@ static void manager_hooks(apr_pool_t *p)
39353959
ap_register_provider(p, "manager", "shared", "3", &balancer_storage);
39363960
ap_register_provider(p, "manager", "shared", "4", &sessionid_storage);
39373961
ap_register_provider(p, "manager", "shared", "5", &domain_storage);
3962+
ap_register_provider(p, "manager", "shared", "6", &set_proxyhctemplate);
39383963
}
39393964

39403965
/*

0 commit comments

Comments
 (0)