Blame modules/proxy/balancers/mod_lbmethod_byrequests.c

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#include "mod_proxy.h"
Packit 90a5c9
#include "scoreboard.h"
Packit 90a5c9
#include "ap_mpm.h"
Packit 90a5c9
#include "apr_version.h"
Packit 90a5c9
#include "ap_hooks.h"
Packit 90a5c9
Packit 90a5c9
module AP_MODULE_DECLARE_DATA lbmethod_byrequests_module;
Packit 90a5c9
Packit 90a5c9
static APR_OPTIONAL_FN_TYPE(proxy_balancer_get_best_worker)
Packit 90a5c9
                            *ap_proxy_balancer_get_best_worker_fn = NULL;
Packit 90a5c9
Packit 90a5c9
static int is_best_byrequests(proxy_worker *current, proxy_worker *prev_best, void *baton)
Packit 90a5c9
{
Packit 90a5c9
    int *total_factor = (int *)baton;
Packit 90a5c9
Packit 90a5c9
    current->s->lbstatus += current->s->lbfactor;
Packit 90a5c9
    *total_factor += current->s->lbfactor;
Packit 90a5c9
Packit 90a5c9
    return (!prev_best || (current->s->lbstatus > prev_best->s->lbstatus));
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * The idea behind the find_best_byrequests scheduler is the following:
Packit 90a5c9
 *
Packit 90a5c9
 * lbfactor is "how much we expect this worker to work", or "the worker's
Packit 90a5c9
 * normalized work quota".
Packit 90a5c9
 *
Packit 90a5c9
 * lbstatus is "how urgent this worker has to work to fulfill its quota
Packit 90a5c9
 * of work".
Packit 90a5c9
 *
Packit 90a5c9
 * We distribute each worker's work quota to the worker, and then look
Packit 90a5c9
 * which of them needs to work most urgently (biggest lbstatus).  This
Packit 90a5c9
 * worker is then selected for work, and its lbstatus reduced by the
Packit 90a5c9
 * total work quota we distributed to all workers.  Thus the sum of all
Packit 90a5c9
 * lbstatus does not change.(*)
Packit 90a5c9
 *
Packit 90a5c9
 * If some workers are disabled, the others will
Packit 90a5c9
 * still be scheduled correctly.
Packit 90a5c9
 *
Packit 90a5c9
 * If a balancer is configured as follows:
Packit 90a5c9
 *
Packit 90a5c9
 * worker     a    b    c    d
Packit 90a5c9
 * lbfactor  25   25   25   25
Packit 90a5c9
 *
Packit 90a5c9
 * And b gets disabled, the following schedule is produced:
Packit 90a5c9
 *
Packit 90a5c9
 *    a c d a c d a c d ...
Packit 90a5c9
 *
Packit 90a5c9
 * Note that the above lbfactor setting is the *exact* same as:
Packit 90a5c9
 *
Packit 90a5c9
 * worker     a    b    c    d
Packit 90a5c9
 * lbfactor   1    1    1    1
Packit 90a5c9
 *
Packit 90a5c9
 * Asymmetric configurations work as one would expect. For
Packit 90a5c9
 * example:
Packit 90a5c9
 *
Packit 90a5c9
 * worker     a    b    c    d
Packit 90a5c9
 * lbfactor   1    1    1    2
Packit 90a5c9
 *
Packit 90a5c9
 * would have a, b and c all handling about the same
Packit 90a5c9
 * amount of load with d handling twice what a or b
Packit 90a5c9
 * or c handles individually. So we could see:
Packit 90a5c9
 *
Packit 90a5c9
 *   b a d c d a c d b d ...
Packit 90a5c9
 *
Packit 90a5c9
 */
Packit 90a5c9
static proxy_worker *find_best_byrequests(proxy_balancer *balancer,
Packit 90a5c9
                                request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    int total_factor = 0;
Packit 90a5c9
    proxy_worker *worker = ap_proxy_balancer_get_best_worker_fn(balancer, r, is_best_byrequests, &total_factor);
Packit 90a5c9
Packit 90a5c9
    if (worker) {
Packit 90a5c9
        worker->s->lbstatus -= total_factor;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return worker;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/* assumed to be mutex protected by caller */
Packit 90a5c9
static apr_status_t reset(proxy_balancer *balancer, server_rec *s)
Packit 90a5c9
{
Packit 90a5c9
    int i;
Packit 90a5c9
    proxy_worker **worker;
Packit 90a5c9
    worker = (proxy_worker **)balancer->workers->elts;
Packit 90a5c9
    for (i = 0; i < balancer->workers->nelts; i++, worker++) {
Packit 90a5c9
        (*worker)->s->lbstatus = 0;
Packit 90a5c9
    }
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static apr_status_t age(proxy_balancer *balancer, server_rec *s)
Packit 90a5c9
{
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * How to add additional lbmethods:
Packit 90a5c9
 *   1. Create func which determines "best" candidate worker
Packit 90a5c9
 *      (eg: find_best_bytraffic, above)
Packit 90a5c9
 *   2. Register it as a provider.
Packit 90a5c9
 */
Packit 90a5c9
static const proxy_balancer_method byrequests =
Packit 90a5c9
{
Packit 90a5c9
    "byrequests",
Packit 90a5c9
    &find_best_byrequests,
Packit 90a5c9
    NULL,
Packit 90a5c9
    &reset,
Packit 90a5c9
    &age,
Packit 90a5c9
    NULL
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
/* post_config hook: */
Packit 90a5c9
static int lbmethod_byrequests_post_config(apr_pool_t *pconf, apr_pool_t *plog,
Packit 90a5c9
        apr_pool_t *ptemp, server_rec *s)
Packit 90a5c9
{
Packit 90a5c9
Packit 90a5c9
    /* lbmethod_byrequests_post_config() will be called twice during startup.  So, don't
Packit 90a5c9
     * set up the static data the 1st time through. */
Packit 90a5c9
    if (ap_state_query(AP_SQ_MAIN_STATE) == AP_SQ_MS_CREATE_PRE_CONFIG) {
Packit 90a5c9
        return OK;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    ap_proxy_balancer_get_best_worker_fn =
Packit 90a5c9
                 APR_RETRIEVE_OPTIONAL_FN(proxy_balancer_get_best_worker);
Packit 90a5c9
    if (!ap_proxy_balancer_get_best_worker_fn) {
Packit 90a5c9
        ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(10152)
Packit 90a5c9
                     "mod_proxy must be loaded for mod_lbmethod_byrequests");
Packit 90a5c9
        return !OK;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void register_hook(apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    ap_register_provider(p, PROXY_LBMETHOD, "byrequests", "0", &byrequests);
Packit 90a5c9
    ap_hook_post_config(lbmethod_byrequests_post_config, NULL, NULL, APR_HOOK_MIDDLE);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_MODULE(lbmethod_byrequests) = {
Packit 90a5c9
    STANDARD20_MODULE_STUFF,
Packit 90a5c9
    NULL,       /* create per-directory config structure */
Packit 90a5c9
    NULL,       /* merge per-directory config structures */
Packit 90a5c9
    NULL,       /* create per-server config structure */
Packit 90a5c9
    NULL,       /* merge per-server config structures */
Packit 90a5c9
    NULL,       /* command apr_table_t */
Packit 90a5c9
    register_hook /* register hooks */
Packit 90a5c9
};