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
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.
3f08db06526d6901aa08c110b5bc7dde6bc39905nd/* If we haven't seen a heartbeat in the last N seconds, don't count this IP
9ecf04e1256ba153c86e74edb77cfd705c55a042nd * as allive.
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjungmodule AP_MODULE_DECLARE_DATA lbmethod_heartbeat_module;
9a58dc6a2b26ec128b1270cf48810e705f1a90dbsftypedef struct lb_hb_ctx_t
9ecf04e1256ba153c86e74edb77cfd705c55a042nd const char *path;
9ecf04e1256ba153c86e74edb77cfd705c55a042ndtypedef struct hb_server_t {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd const char *ip;
9ecf04e1256ba153c86e74edb77cfd705c55a042ndstatic void
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedoohargstr_to_table(apr_pool_t *p, char *str, apr_table_t *parms)
9ecf04e1256ba153c86e74edb77cfd705c55a042nd ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd "Found query arg: %s = %s", key, value);
9ecf04e1256ba153c86e74edb77cfd705c55a042ndstatic apr_status_t read_heartbeats(const char *path, apr_hash_t *servers,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd rv = apr_file_open(&fp, path, APR_READ|APR_BINARY|APR_BUFFERED,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_bucket_brigade *tmpbb = apr_brigade_create(pool, ba);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd const char *ip;
9ecf04e1256ba153c86e74edb77cfd705c55a042nd if (bsize == 0) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd /* comment */
9ecf04e1256ba153c86e74edb77cfd705c55a042nd /* line format: <IP> <query_string>\n */
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_hash_set(servers, server->ip, APR_HASH_KEY_STRING, server);
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 } while (1);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd * Finding a random number in a range.
9ecf04e1256ba153c86e74edb77cfd705c55a042nd * n' = a + n(b-a+1)/(M+1)
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(__n) = (__min) + (long) ((double) ((__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0)))
9ecf04e1256ba153c86e74edb77cfd705c55a042nd apr_generate_random_bytes((void*)number, sizeof(apr_uint32_t));
9ecf04e1256ba153c86e74edb77cfd705c55a042ndstatic proxy_worker *find_best_hb(proxy_balancer *balancer,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd "lb_heartbeat: Unable to read heartbeats at '%s'",
9ecf04e1256ba153c86e74edb77cfd705c55a042nd up_servers = apr_array_make(tpool, apr_hash_count(servers), sizeof(hb_server_t *));
9ecf04e1256ba153c86e74edb77cfd705c55a042nd worker = &APR_ARRAY_IDX(balancer->workers, i, proxy_worker);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd server = apr_hash_get(servers, worker->hostname, APR_HASH_KEY_STRING);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd "lb_heartbeat: failed picking a random number. how random.");
9ecf04e1256ba153c86e74edb77cfd705c55a042ndstatic apr_status_t reset(proxy_balancer *balancer, request_rec *r) {
9ecf04e1256ba153c86e74edb77cfd705c55a042ndstatic apr_status_t age(proxy_balancer *balancer, request_rec *r) {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd "heartbeat",
9ecf04e1256ba153c86e74edb77cfd705c55a042nd ap_register_provider(p, PROXY_LBMETHOD, "heartbeat", "0", &heartbeat);
9ecf04e1256ba153c86e74edb77cfd705c55a042ndstatic void *lb_hb_create_config(apr_pool_t *p, server_rec *s)
9ecf04e1256ba153c86e74edb77cfd705c55a042nd lb_hb_ctx_t *ctx = (lb_hb_ctx_t *) apr_palloc(p, sizeof(lb_hb_ctx_t));
9ecf04e1256ba153c86e74edb77cfd705c55a042ndstatic void *lb_hb_merge_config(apr_pool_t *p, void *basev, void *overridesv)
9ecf04e1256ba153c86e74edb77cfd705c55a042nd (lb_hb_ctx_t *) ap_get_module_config(cmd->server->module_config,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
9ecf04e1256ba153c86e74edb77cfd705c55a042nd AP_INIT_TAKE1("HeartbeatStorage", cmd_lb_hb_storage, NULL, RSRC_CONF,
9ecf04e1256ba153c86e74edb77cfd705c55a042nd "Path to read heartbeat data."),
9ecf04e1256ba153c86e74edb77cfd705c55a042ndmodule AP_MODULE_DECLARE_DATA lbmethod_heartbeat_module = {
9ecf04e1256ba153c86e74edb77cfd705c55a042nd lb_hb_create_config, /* create per-server config structure */
9ecf04e1256ba153c86e74edb77cfd705c55a042nd lb_hb_merge_config, /* merge per-server config structures */