ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova/* Licensed to the Apache Software Foundation (ASF) under one or more
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova * contributor license agreements. See the NOTICE file distributed with
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova * this work for additional information regarding copyright ownership.
b43458b4d81f7451112cecbd757f3a05216e7088Kristina Sojakova * The ASF licenses this file to You under the Apache License, Version 2.0
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova * (the "License"); you may not use this file except in compliance with
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova * the License. You may obtain a copy of the License at
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu Prodescu *
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova * http://www.apache.org/licenses/LICENSE-2.0
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova *
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova * Unless required by applicable law or agreed to in writing, software
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova * distributed under the License is distributed on an "AS IS" BASIS,
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova * See the License for the specific language governing permissions and
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova * limitations under the License.
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova */
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova#include "mod_proxy.h"
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova#include "scoreboard.h"
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova#include "ap_mpm.h"
9f85afecbd79b3df5a0bb17bd28cd0b288dc3213Kristina Sojakova#include "apr_version.h"
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova#include "ap_hooks.h"
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakovamodule AP_MODULE_DECLARE_DATA lbmethod_byrequests_module;
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakovastatic int (*ap_proxy_retry_worker_fn)(const char *proxy_function,
d2786879b4733fd4886a5b654f7c6de1d234f638Kristina Sojakova proxy_worker *worker, server_rec *s) = NULL;
d71bb9deea089887b4fd829c5b766e7e4de9f204Kristina Sojakova
d2786879b4733fd4886a5b654f7c6de1d234f638Kristina Sojakova/*
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova * The idea behind the find_best_byrequests scheduler is the following:
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova *
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova * lbfactor is "how much we expect this worker to work", or "the worker's
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova * normalized work quota".
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova *
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova * lbstatus is "how urgent this worker has to work to fulfill its quota
b43458b4d81f7451112cecbd757f3a05216e7088Kristina Sojakova * of work".
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova *
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova * We distribute each worker's work quota to the worker, and then look
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova * which of them needs to work most urgently (biggest lbstatus). This
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova * worker is then selected for work, and its lbstatus reduced by the
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova * total work quota we distributed to all workers. Thus the sum of all
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova * lbstatus does not change.(*)
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova *
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova * If some workers are disabled, the others will
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova * still be scheduled correctly.
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova *
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova * If a balancer is configured as follows:
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova *
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova * worker a b c d
d2786879b4733fd4886a5b654f7c6de1d234f638Kristina Sojakova * lbfactor 25 25 25 25
d2786879b4733fd4886a5b654f7c6de1d234f638Kristina Sojakova *
d2786879b4733fd4886a5b654f7c6de1d234f638Kristina Sojakova * And b gets disabled, the following schedule is produced:
d2786879b4733fd4886a5b654f7c6de1d234f638Kristina Sojakova *
d2786879b4733fd4886a5b654f7c6de1d234f638Kristina Sojakova * a c d a c d a c d ...
d2786879b4733fd4886a5b654f7c6de1d234f638Kristina Sojakova *
d2786879b4733fd4886a5b654f7c6de1d234f638Kristina Sojakova * Note that the above lbfactor setting is the *exact* same as:
d2786879b4733fd4886a5b654f7c6de1d234f638Kristina Sojakova *
b43458b4d81f7451112cecbd757f3a05216e7088Kristina Sojakova * worker a b c d
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova * lbfactor 1 1 1 1
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova *
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova * Asymmetric configurations work as one would expect. For
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova * example:
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova *
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova * worker a b c d
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova * lbfactor 1 1 1 2
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova *
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova * would have a, b and c all handling about the same
abd5fc85dc7e19b1614890182436940e922963a4Kristina Sojakova * amount of load with d handling twice what a or b
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova * or c handles individually. So we could see:
14650c9e129d8dc51ed55b2edc6ec27d9f0f6d00Kristina Sojakova *
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova * b a d c d a c d b d ...
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova *
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova */
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakovastatic proxy_worker *find_best_byrequests(proxy_balancer *balancer,
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova request_rec *r)
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova{
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova int i;
abd5fc85dc7e19b1614890182436940e922963a4Kristina Sojakova int total_factor = 0;
ccaa75089b23c0f043cdbd4001cba4e076ca4fd3Kristina Sojakova proxy_worker **worker;
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova proxy_worker *mycandidate = NULL;
80d2ec8f37d5ddec13c14b17b1bab01e9c94630aChristian Maeder int cur_lbset = 0;
80d2ec8f37d5ddec13c14b17b1bab01e9c94630aChristian Maeder int max_lbset = 0;
d71bb9deea089887b4fd829c5b766e7e4de9f204Kristina Sojakova int checking_standby;
168d206b4e5fd436c98239a1b6629c651f54c8eeKristina Sojakova int checked_standby;
168d206b4e5fd436c98239a1b6629c651f54c8eeKristina Sojakova
168d206b4e5fd436c98239a1b6629c651f54c8eeKristina Sojakova if (!ap_proxy_retry_worker_fn) {
168d206b4e5fd436c98239a1b6629c651f54c8eeKristina Sojakova ap_proxy_retry_worker_fn =
d2786879b4733fd4886a5b654f7c6de1d234f638Kristina Sojakova APR_RETRIEVE_OPTIONAL_FN(ap_proxy_retry_worker);
d2786879b4733fd4886a5b654f7c6de1d234f638Kristina Sojakova if (!ap_proxy_retry_worker_fn) {
d2786879b4733fd4886a5b654f7c6de1d234f638Kristina Sojakova /* can only happen if mod_proxy isn't loaded */
abd5fc85dc7e19b1614890182436940e922963a4Kristina Sojakova return NULL;
b3bacd257ffcdd346b70ab690f03b28ad5f33fdcKristina Sojakova }
b3bacd257ffcdd346b70ab690f03b28ad5f33fdcKristina Sojakova }
b3bacd257ffcdd346b70ab690f03b28ad5f33fdcKristina Sojakova
b3bacd257ffcdd346b70ab690f03b28ad5f33fdcKristina Sojakova ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, APLOGNO(01207)
b3bacd257ffcdd346b70ab690f03b28ad5f33fdcKristina Sojakova "proxy: Entering byrequests for BALANCER (%s)",
b3bacd257ffcdd346b70ab690f03b28ad5f33fdcKristina Sojakova balancer->s->name);
b3bacd257ffcdd346b70ab690f03b28ad5f33fdcKristina Sojakova
d71bb9deea089887b4fd829c5b766e7e4de9f204Kristina Sojakova /* First try to see if we have available candidate */
abd5fc85dc7e19b1614890182436940e922963a4Kristina Sojakova do {
168d206b4e5fd436c98239a1b6629c651f54c8eeKristina Sojakova checking_standby = checked_standby = 0;
80d2ec8f37d5ddec13c14b17b1bab01e9c94630aChristian Maeder while (!mycandidate && !checked_standby) {
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova worker = (proxy_worker **)balancer->workers->elts;
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova for (i = 0; i < balancer->workers->nelts; i++, worker++) {
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova if (!checking_standby) { /* first time through */
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova if ((*worker)->s->lbset > max_lbset)
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova max_lbset = (*worker)->s->lbset;
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova }
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova if (
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova ((*worker)->s->lbset != cur_lbset) ||
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova (checking_standby ? !PROXY_WORKER_IS_STANDBY(*worker) : PROXY_WORKER_IS_STANDBY(*worker)) ||
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova (PROXY_WORKER_IS_DRAINING(*worker))
abd5fc85dc7e19b1614890182436940e922963a4Kristina Sojakova ) {
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova continue;
e8dd447a2aa5fbac10668749dfe4142c05ec3d7dKristina Sojakova }
5e35940c3516ccea02caa0450d2b075de0106fa5Kristina Sojakova
5e35940c3516ccea02caa0450d2b075de0106fa5Kristina Sojakova /* If the worker is in error state run
5e35940c3516ccea02caa0450d2b075de0106fa5Kristina Sojakova * retry on that worker. It will be marked as
* operational if the retry timeout is elapsed.
* The worker might still be unusable, but we try
* anyway.
*/
if (!PROXY_WORKER_IS_USABLE(*worker))
ap_proxy_retry_worker_fn("BALANCER", *worker, r->server);
/* Take into calculation only the workers that are
* not in error state or not disabled.
*/
if (PROXY_WORKER_IS_USABLE(*worker)) {
(*worker)->s->lbstatus += (*worker)->s->lbfactor;
total_factor += (*worker)->s->lbfactor;
if (!mycandidate || (*worker)->s->lbstatus > mycandidate->s->lbstatus)
mycandidate = *worker;
}
}
checked_standby = checking_standby++;
}
cur_lbset++;
} while (cur_lbset <= max_lbset && !mycandidate);
if (mycandidate) {
mycandidate->s->lbstatus -= total_factor;
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server, APLOGNO(01208)
"proxy: byrequests selected worker \"%s\" : busy %" APR_SIZE_T_FMT " : lbstatus %d",
mycandidate->s->name, mycandidate->s->busy, mycandidate->s->lbstatus);
}
return mycandidate;
}
/* assumed to be mutex protected by caller */
static apr_status_t reset(proxy_balancer *balancer, server_rec *s)
{
int i;
proxy_worker **worker;
worker = (proxy_worker **)balancer->workers->elts;
for (i = 0; i < balancer->workers->nelts; i++, worker++) {
(*worker)->s->lbstatus = 0;
}
return APR_SUCCESS;
}
static apr_status_t age(proxy_balancer *balancer, server_rec *s)
{
return APR_SUCCESS;
}
/*
* How to add additional lbmethods:
* 1. Create func which determines "best" candidate worker
* (eg: find_best_bytraffic, above)
* 2. Register it as a provider.
*/
static const proxy_balancer_method byrequests =
{
"byrequests",
&find_best_byrequests,
NULL,
&reset,
&age
};
static void register_hook(apr_pool_t *p)
{
/* Only the mpm_winnt has child init hook handler.
* make sure that we are called after the mpm
* initializes and after the mod_proxy
*/
ap_register_provider(p, PROXY_LBMETHOD, "byrequests", "0", &byrequests);
}
AP_DECLARE_MODULE(lbmethod_byrequests) = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-directory config structure */
NULL, /* merge per-directory config structures */
NULL, /* create per-server config structure */
NULL, /* merge per-server config structures */
NULL, /* command apr_table_t */
register_hook /* register hooks */
};