mod_lbmethod_bybusyness.c revision 2c4ed36c350564e8bfb68e9d9ba58fe77b9bf3c0
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim/* Licensed to the Apache Software Foundation (ASF) under one or more
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim * contributor license agreements. See the NOTICE file distributed with
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim * this work for additional information regarding copyright ownership.
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim * The ASF licenses this file to You under the Apache License, Version 2.0
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim * (the "License"); you may not use this file except in compliance with
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim * the License. You may obtain a copy of the License at
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim *
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim * http://www.apache.org/licenses/LICENSE-2.0
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim *
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim * Unless required by applicable law or agreed to in writing, software
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim * distributed under the License is distributed on an "AS IS" BASIS,
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim * See the License for the specific language governing permissions and
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim * limitations under the License.
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim */
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim#include "mod_proxy.h"
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim#include "scoreboard.h"
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim#include "ap_mpm.h"
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim#include "apr_version.h"
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim#include "apr_hooks.h"
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jimmodule AP_MODULE_DECLARE_DATA lbmethod_bybusyness_module;
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jimstatic proxy_worker *find_best_bybusyness(proxy_balancer *balancer,
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim request_rec *r)
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim{
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim int i;
834fc281be8e0f7f2614961f12d8bbf603382a17jfclere proxy_worker **worker;
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim proxy_worker *mycandidate = NULL;
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim int cur_lbset = 0;
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim int max_lbset = 0;
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim int checking_standby;
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim int checked_standby;
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim int total_factor = 0;
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim "proxy: Entering bybusyness for BALANCER (%s)",
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim balancer->name);
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim /* First try to see if we have available candidate */
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim do {
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim checking_standby = checked_standby = 0;
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim while (!mycandidate && !checked_standby) {
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
834fc281be8e0f7f2614961f12d8bbf603382a17jfclere worker = (proxy_worker **)balancer->workers->elts;
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim for (i = 0; i < balancer->workers->nelts; i++, worker++) {
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim if (!checking_standby) { /* first time through */
834fc281be8e0f7f2614961f12d8bbf603382a17jfclere if ((*worker)->s->lbset > max_lbset)
834fc281be8e0f7f2614961f12d8bbf603382a17jfclere max_lbset = (*worker)->s->lbset;
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim }
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
834fc281be8e0f7f2614961f12d8bbf603382a17jfclere if ((*worker)->s->lbset != cur_lbset)
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim continue;
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
834fc281be8e0f7f2614961f12d8bbf603382a17jfclere if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(*worker) : PROXY_WORKER_IS_STANDBY(*worker)) )
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim continue;
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim /* If the worker is in error state run
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim * retry on that worker. It will be marked as
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim * operational if the retry timeout is elapsed.
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim * The worker might still be unusable, but we try
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim * anyway.
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim */
834fc281be8e0f7f2614961f12d8bbf603382a17jfclere if (!PROXY_WORKER_IS_USABLE(*worker))
834fc281be8e0f7f2614961f12d8bbf603382a17jfclere ap_proxy_retry_worker("BALANCER", *worker, r->server);
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim /* Take into calculation only the workers that are
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim * not in error state or not disabled.
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim */
834fc281be8e0f7f2614961f12d8bbf603382a17jfclere if (PROXY_WORKER_IS_USABLE(*worker)) {
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
834fc281be8e0f7f2614961f12d8bbf603382a17jfclere (*worker)->s->lbstatus += (*worker)->s->lbfactor;
834fc281be8e0f7f2614961f12d8bbf603382a17jfclere total_factor += (*worker)->s->lbfactor;
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim if (!mycandidate
834fc281be8e0f7f2614961f12d8bbf603382a17jfclere || (*worker)->s->busy < mycandidate->s->busy
834fc281be8e0f7f2614961f12d8bbf603382a17jfclere || ((*worker)->s->busy == mycandidate->s->busy && (*worker)->s->lbstatus > mycandidate->s->lbstatus))
834fc281be8e0f7f2614961f12d8bbf603382a17jfclere mycandidate = *worker;
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim }
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim }
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim checked_standby = checking_standby++;
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim }
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim cur_lbset++;
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim } while (cur_lbset <= max_lbset && !mycandidate);
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim if (mycandidate) {
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim mycandidate->s->lbstatus -= total_factor;
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim "proxy: bybusyness selected worker \"%s\" : busy %" APR_SIZE_T_FMT " : lbstatus %d",
23a31b10f869a72c9197b5f153f4f3e1a4c68f28jim mycandidate->s->name, mycandidate->s->busy, mycandidate->s->lbstatus);
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim }
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim return mycandidate;
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim}
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
2c4ed36c350564e8bfb68e9d9ba58fe77b9bf3c0jim/* assumed to be mutex protected by caller */
6bed231822ad2aaba16de74c4c01023e5982a5e2jfclerestatic apr_status_t reset(proxy_balancer *balancer, server_rec *s) {
2c4ed36c350564e8bfb68e9d9ba58fe77b9bf3c0jim int i;
2c4ed36c350564e8bfb68e9d9ba58fe77b9bf3c0jim proxy_worker **worker;
2c4ed36c350564e8bfb68e9d9ba58fe77b9bf3c0jim worker = (proxy_worker **)balancer->workers->elts;
2c4ed36c350564e8bfb68e9d9ba58fe77b9bf3c0jim for (i = 0; i < balancer->workers->nelts; i++, worker++) {
2c4ed36c350564e8bfb68e9d9ba58fe77b9bf3c0jim (*worker)->s->lbstatus = 0;
2c4ed36c350564e8bfb68e9d9ba58fe77b9bf3c0jim (*worker)->s->busy = 0;
2c4ed36c350564e8bfb68e9d9ba58fe77b9bf3c0jim }
2c4ed36c350564e8bfb68e9d9ba58fe77b9bf3c0jim return APR_SUCCESS;
b1e3273c2e70ad5ac64feef979b2ed1d98c09963jim}
b1e3273c2e70ad5ac64feef979b2ed1d98c09963jim
6bed231822ad2aaba16de74c4c01023e5982a5e2jfclerestatic apr_status_t age(proxy_balancer *balancer, server_rec *s) {
b1e3273c2e70ad5ac64feef979b2ed1d98c09963jim return APR_SUCCESS;
b1e3273c2e70ad5ac64feef979b2ed1d98c09963jim}
b1e3273c2e70ad5ac64feef979b2ed1d98c09963jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jimstatic const proxy_balancer_method bybusyness =
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim{
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim "bybusyness",
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim &find_best_bybusyness,
9a0e1ad272fdbe3fee989e93bdae8253418429edjfclere NULL,
b1e3273c2e70ad5ac64feef979b2ed1d98c09963jim &reset,
9a0e1ad272fdbe3fee989e93bdae8253418429edjfclere &age
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim};
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jimstatic void register_hook(apr_pool_t *p)
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim{
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim ap_register_provider(p, PROXY_LBMETHOD, "bybusyness", "0", &bybusyness);
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim}
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim
36ef8f77bffe75d1aa327882be1b5bdbe2ff567asfAP_DECLARE_MODULE(lbmethod_bybusyness) = {
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim STANDARD20_MODULE_STUFF,
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim NULL, /* create per-directory config structure */
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim NULL, /* merge per-directory config structures */
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim NULL, /* create per-server config structure */
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim NULL, /* merge per-server config structures */
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim NULL, /* command apr_table_t */
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim register_hook /* register hooks */
dfcadcd3e6bbdee594540b8f8e4e50586a2e3219jim};