mod_lbmethod_heartbeat.c revision b1e3273c2e70ad5ac64feef979b2ed1d98c09963
9ecf04e1256ba153c86e74edb77cfd705c55a042nd/* Licensed to the Apache Software Foundation (ASF) under one or more
9ecf04e1256ba153c86e74edb77cfd705c55a042nd * contributor license agreements. See the NOTICE file distributed with
fd9abdda70912b99b24e3bf1a38f26fde908a74cnd * this work for additional information regarding copyright ownership.
fd9abdda70912b99b24e3bf1a38f26fde908a74cnd * The ASF licenses this file to You under the Apache License, Version 2.0
fd9abdda70912b99b24e3bf1a38f26fde908a74cnd * (the "License"); you may not use this file except in compliance with
9ecf04e1256ba153c86e74edb77cfd705c55a042nd * the License. You may obtain a copy of the License at
9ecf04e1256ba153c86e74edb77cfd705c55a042nd *
9ecf04e1256ba153c86e74edb77cfd705c55a042nd * http://www.apache.org/licenses/LICENSE-2.0
9ecf04e1256ba153c86e74edb77cfd705c55a042nd *
96ad5d81ee4a2cc66a4ae19893efc8aa6d06fae7jailletc * Unless required by applicable law or agreed to in writing, software
9ecf04e1256ba153c86e74edb77cfd705c55a042nd * distributed under the License is distributed on an "AS IS" BASIS,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen * See the License for the specific language governing permissions and
2e545ce2450a9953665f701bb05350f0d3f26275nd * limitations under the License.
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen */
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen
9ecf04e1256ba153c86e74edb77cfd705c55a042nd#include "mod_proxy.h"
9ecf04e1256ba153c86e74edb77cfd705c55a042nd#include "scoreboard.h"
af33a4994ae2ff15bc67d19ff1a7feb906745bf8rbowen#include "ap_mpm.h"
3f08db06526d6901aa08c110b5bc7dde6bc39905nd#include "apr_version.h"
9ecf04e1256ba153c86e74edb77cfd705c55a042nd#include "apr_hooks.h"
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd#ifndef LBM_HEARTBEAT_MAX_LASTSEEN
3f08db06526d6901aa08c110b5bc7dde6bc39905nd/* If we haven't seen a heartbeat in the last N seconds, don't count this IP
9ecf04e1256ba153c86e74edb77cfd705c55a042nd * as allive.
9ecf04e1256ba153c86e74edb77cfd705c55a042nd */
c04f76acce77126cf88b09350e56ea8c6b4a064enilgun#define LBM_HEARTBEAT_MAX_LASTSEEN (10)
9ecf04e1256ba153c86e74edb77cfd705c55a042nd#endif
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjungmodule AP_MODULE_DECLARE_DATA lbmethod_heartbeat_module;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsftypedef struct lb_hb_ctx_t
4b575a6b6704b516f22d65a3ad35696d7b9ba372rpluem{
9ecf04e1256ba153c86e74edb77cfd705c55a042nd const char *path;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd} lb_hb_ctx_t;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042ndtypedef struct hb_server_t {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd const char *ip;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd int busy;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd int ready;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd int seen;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd proxy_worker *worker;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd} hb_server_t;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042ndstatic void
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedoohargstr_to_table(apr_pool_t *p, char *str, apr_table_t *parms)
9ecf04e1256ba153c86e74edb77cfd705c55a042nd{
9ecf04e1256ba153c86e74edb77cfd705c55a042nd char *key;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd char *value;
f5a398cc8880978754903f9ece8e4beb63a81cedrbowen char *strtok_state;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd key = apr_strtok(str, "&", &strtok_state);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd while (key) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd value = strchr(key, '=');
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (value) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd *value = '\0'; /* Split the string in two */
9ecf04e1256ba153c86e74edb77cfd705c55a042nd value++; /* Skip passed the = */
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd else {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd value = "1";
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd ap_unescape_url(key);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd ap_unescape_url(value);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_table_set(parms, key, value);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd /*
9ecf04e1256ba153c86e74edb77cfd705c55a042nd ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd "Found query arg: %s = %s", key, value);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd */
9ecf04e1256ba153c86e74edb77cfd705c55a042nd key = apr_strtok(NULL, "&", &strtok_state);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd}
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042ndstatic apr_status_t read_heartbeats(const char *path, apr_hash_t *servers,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_pool_t *pool)
9ecf04e1256ba153c86e74edb77cfd705c55a042nd{
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_finfo_t fi;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_status_t rv;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_file_t *fp;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (!path) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd return APR_SUCCESS;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd rv = apr_file_open(&fp, path, APR_READ|APR_BINARY|APR_BUFFERED,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd APR_OS_DEFAULT, pool);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (rv) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd return rv;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd rv = apr_file_info_get(&fi, APR_FINFO_SIZE, fp);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (rv) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd return rv;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd char *t;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd int lineno = 0;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_bucket_alloc_t *ba = apr_bucket_alloc_create(pool);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_bucket_brigade *bb = apr_brigade_create(pool, ba);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_bucket_brigade *tmpbb = apr_brigade_create(pool, ba);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_table_t *hbt = apr_table_make(pool, 10);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_brigade_insert_file(bb, fp, 0, fi.size, pool);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd do {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd hb_server_t *server;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd char buf[4096];
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_size_t bsize = sizeof(buf);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd const char *ip;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_brigade_cleanup(tmpbb);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (APR_BRIGADE_EMPTY(bb)) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd break;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd rv = apr_brigade_split_line(tmpbb, bb,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd APR_BLOCK_READ, sizeof(buf));
9ecf04e1256ba153c86e74edb77cfd705c55a042nd lineno++;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (rv) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd return rv;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_brigade_flatten(tmpbb, buf, &bsize);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (bsize == 0) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd break;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd buf[bsize - 1] = 0;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd /* comment */
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (buf[0] == '#') {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd continue;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd /* line format: <IP> <query_string>\n */
9ecf04e1256ba153c86e74edb77cfd705c55a042nd t = strchr(buf, ' ');
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (!t) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd continue;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd ip = apr_pstrndup(pool, buf, t - buf);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd t++;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd server = apr_hash_get(servers, ip, APR_HASH_KEY_STRING);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (server == NULL) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd server = apr_pcalloc(pool, sizeof(hb_server_t));
9ecf04e1256ba153c86e74edb77cfd705c55a042nd server->ip = ip;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd server->seen = -1;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_hash_set(servers, server->ip, APR_HASH_KEY_STRING, server);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_table_clear(hbt);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd argstr_to_table(pool, apr_pstrdup(pool, t), hbt);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (apr_table_get(hbt, "busy")) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd server->busy = atoi(apr_table_get(hbt, "busy"));
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (apr_table_get(hbt, "ready")) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd server->ready = atoi(apr_table_get(hbt, "ready"));
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (apr_table_get(hbt, "lastseen")) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd server->seen = atoi(apr_table_get(hbt, "lastseen"));
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (server->busy == 0 && server->ready != 0) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd /* Server has zero threads active, but lots of them ready,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd * it likely just started up, so lets /4 the number ready,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd * to prevent us from completely flooding it with all new
9ecf04e1256ba153c86e74edb77cfd705c55a042nd * requests.
9ecf04e1256ba153c86e74edb77cfd705c55a042nd */
9ecf04e1256ba153c86e74edb77cfd705c55a042nd server->ready = server->ready / 4;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd } while (1);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd return APR_SUCCESS;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd}
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd/*
9ecf04e1256ba153c86e74edb77cfd705c55a042nd * Finding a random number in a range.
9ecf04e1256ba153c86e74edb77cfd705c55a042nd * n' = a + n(b-a+1)/(M+1)
9ecf04e1256ba153c86e74edb77cfd705c55a042nd * where:
9ecf04e1256ba153c86e74edb77cfd705c55a042nd * n' = random number in range
9ecf04e1256ba153c86e74edb77cfd705c55a042nd * a = low end of range
9ecf04e1256ba153c86e74edb77cfd705c55a042nd * b = high end of range
9ecf04e1256ba153c86e74edb77cfd705c55a042nd * n = random number of 0..M
9ecf04e1256ba153c86e74edb77cfd705c55a042nd * M = maxint
9ecf04e1256ba153c86e74edb77cfd705c55a042nd * Algorithm 'borrowed' from PHP's rand() function.
9ecf04e1256ba153c86e74edb77cfd705c55a042nd */
9ecf04e1256ba153c86e74edb77cfd705c55a042nd#define RAND_RANGE(__n, __min, __max, __tmax) \
9ecf04e1256ba153c86e74edb77cfd705c55a042nd(__n) = (__min) + (long) ((double) ((__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0)))
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042ndstatic apr_status_t random_pick(apr_uint32_t *number,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_uint32_t min,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_uint32_t max)
9ecf04e1256ba153c86e74edb77cfd705c55a042nd{
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_status_t rv =
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_generate_random_bytes((void*)number, sizeof(apr_uint32_t));
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (rv) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd return rv;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd RAND_RANGE(*number, min, max, APR_UINT32_MAX);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd return APR_SUCCESS;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd}
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042ndstatic proxy_worker *find_best_hb(proxy_balancer *balancer,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd request_rec *r)
9ecf04e1256ba153c86e74edb77cfd705c55a042nd{
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_status_t rv;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd int i;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_uint32_t openslots = 0;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd proxy_worker *worker;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd hb_server_t *server;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_array_header_t *up_servers;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd proxy_worker *mycandidate = NULL;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_pool_t *tpool;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_hash_t *servers;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd lb_hb_ctx_t *ctx =
9ecf04e1256ba153c86e74edb77cfd705c55a042nd ap_get_module_config(r->server->module_config,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd &lbmethod_heartbeat_module);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_pool_create(&tpool, r->pool);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd servers = apr_hash_make(tpool);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd rv = read_heartbeats(ctx->path, servers, tpool);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (rv) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd "lb_heartbeat: Unable to read heartbeats at '%s'",
9ecf04e1256ba153c86e74edb77cfd705c55a042nd ctx->path);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_pool_destroy(tpool);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd return NULL;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd up_servers = apr_array_make(tpool, apr_hash_count(servers), sizeof(hb_server_t *));
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd for (i = 0; i < balancer->workers->nelts; i++) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd worker = &APR_ARRAY_IDX(balancer->workers, i, proxy_worker);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd server = apr_hash_get(servers, worker->hostname, APR_HASH_KEY_STRING);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (!server) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd continue;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (!PROXY_WORKER_IS_USABLE(worker)) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd ap_proxy_retry_worker("BALANCER", worker, r->server);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (PROXY_WORKER_IS_USABLE(worker)) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd server->worker = worker;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (server->seen < LBM_HEARTBEAT_MAX_LASTSEEN) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd openslots += server->ready;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd APR_ARRAY_PUSH(up_servers, hb_server_t *) = server;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (openslots > 0) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_uint32_t c = 0;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_uint32_t pick = 0;;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd rv = random_pick(&pick, 0, openslots);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (rv) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd "lb_heartbeat: failed picking a random number. how random.");
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_pool_destroy(tpool);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd return NULL;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd for (i = 0; i < up_servers->nelts; i++) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd server = APR_ARRAY_IDX(up_servers, i, hb_server_t *);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (pick > c && pick <= c + server->ready) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd mycandidate = server->worker;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd c += server->ready;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_pool_destroy(tpool);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd return mycandidate;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd}
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042ndstatic apr_status_t reset(proxy_balancer *balancer, request_rec *r) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd return APR_SUCCESS;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd}
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042ndstatic apr_status_t age(proxy_balancer *balancer, request_rec *r) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd return APR_SUCCESS;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd}
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042ndstatic const proxy_balancer_method heartbeat =
9ecf04e1256ba153c86e74edb77cfd705c55a042nd{
9ecf04e1256ba153c86e74edb77cfd705c55a042nd "heartbeat",
9ecf04e1256ba153c86e74edb77cfd705c55a042nd &find_best_hb,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd &reset,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd &age,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd NULL
9ecf04e1256ba153c86e74edb77cfd705c55a042nd};
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042ndstatic void register_hooks(apr_pool_t *p)
9ecf04e1256ba153c86e74edb77cfd705c55a042nd{
9ecf04e1256ba153c86e74edb77cfd705c55a042nd ap_register_provider(p, PROXY_LBMETHOD, "heartbeat", "0", &heartbeat);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd}
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042ndstatic void *lb_hb_create_config(apr_pool_t *p, server_rec *s)
9ecf04e1256ba153c86e74edb77cfd705c55a042nd{
9ecf04e1256ba153c86e74edb77cfd705c55a042nd lb_hb_ctx_t *ctx = (lb_hb_ctx_t *) apr_palloc(p, sizeof(lb_hb_ctx_t));
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd ctx->path = ap_server_root_relative(p, "logs/hb.dat");
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd return ctx;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd}
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042ndstatic void *lb_hb_merge_config(apr_pool_t *p, void *basev, void *overridesv)
9ecf04e1256ba153c86e74edb77cfd705c55a042nd{
9ecf04e1256ba153c86e74edb77cfd705c55a042nd lb_hb_ctx_t *ps = apr_pcalloc(p, sizeof(lb_hb_ctx_t));
9ecf04e1256ba153c86e74edb77cfd705c55a042nd lb_hb_ctx_t *base = (lb_hb_ctx_t *) basev;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd lb_hb_ctx_t *overrides = (lb_hb_ctx_t *) overridesv;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (overrides->path) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd ps->path = apr_pstrdup(p, overrides->path);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd else {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd ps->path = apr_pstrdup(p, base->path);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd return ps;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd}
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042ndstatic const char *cmd_lb_hb_storage(cmd_parms *cmd,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd void *dconf, const char *path)
9ecf04e1256ba153c86e74edb77cfd705c55a042nd{
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_pool_t *p = cmd->pool;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd lb_hb_ctx_t *ctx =
9ecf04e1256ba153c86e74edb77cfd705c55a042nd (lb_hb_ctx_t *) ap_get_module_config(cmd->server->module_config,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd &lbmethod_heartbeat_module);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (err != NULL) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd return err;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd }
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd ctx->path = ap_server_root_relative(p, path);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042nd return NULL;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd}
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042ndstatic const command_rec cmds[] = {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd AP_INIT_TAKE1("HeartbeatStorage", cmd_lb_hb_storage, NULL, RSRC_CONF,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd "Path to read heartbeat data."),
9ecf04e1256ba153c86e74edb77cfd705c55a042nd {NULL}
9ecf04e1256ba153c86e74edb77cfd705c55a042nd};
9ecf04e1256ba153c86e74edb77cfd705c55a042nd
9ecf04e1256ba153c86e74edb77cfd705c55a042ndmodule AP_MODULE_DECLARE_DATA lbmethod_heartbeat_module = {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd STANDARD20_MODULE_STUFF,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd NULL, /* create per-directory config structure */
c04f76acce77126cf88b09350e56ea8c6b4a064enilgun NULL, /* merge per-directory config structures */
9ecf04e1256ba153c86e74edb77cfd705c55a042nd lb_hb_create_config, /* create per-server config structure */
9ecf04e1256ba153c86e74edb77cfd705c55a042nd lb_hb_merge_config, /* merge per-server config structures */
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung cmds, /* command apr_table_t */
727872d18412fc021f03969b8641810d8896820bhumbedooh register_hooks /* register hooks */
0d0ba3a410038e179b695446bb149cce6264e0abnd};
727872d18412fc021f03969b8641810d8896820bhumbedooh