1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "mod_proxy.h"
18 #include "scoreboard.h"
20 #include "apr_version.h"
23 module AP_MODULE_DECLARE_DATA lbmethod_bybusyness_module;
25 static proxy_worker *find_best_bybusyness(proxy_balancer *balancer,
30 proxy_worker **worker;
31 proxy_worker *mycandidate = NULL;
39 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
40 "proxy: Entering bybusyness for BALANCER (%s)",
43 /* First try to see if we have available candidate */
46 checking_standby = checked_standby = 0;
47 while (!mycandidate && !checked_standby) {
49 worker = (proxy_worker **)balancer->workers->elts;
50 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
51 if (!checking_standby) { /* first time through */
52 if ((*worker)->s->lbset > max_lbset)
53 max_lbset = (*worker)->s->lbset;
56 ((*worker)->s->lbset != cur_lbset) ||
57 (checking_standby ? !PROXY_WORKER_IS_STANDBY(*worker) : PROXY_WORKER_IS_STANDBY(*worker)) ||
58 (PROXY_WORKER_IS_DRAINING(*worker))
63 /* If the worker is in error state run
64 * retry on that worker. It will be marked as
65 * operational if the retry timeout is elapsed.
66 * The worker might still be unusable, but we try
69 if (!PROXY_WORKER_IS_USABLE(*worker))
70 ap_proxy_retry_worker("BALANCER", *worker, r->server);
72 /* Take into calculation only the workers that are
73 * not in error state or not disabled.
75 if (PROXY_WORKER_IS_USABLE(*worker)) {
77 (*worker)->s->lbstatus += (*worker)->s->lbfactor;
78 total_factor += (*worker)->s->lbfactor;
81 || (*worker)->s->busy < mycandidate->s->busy
82 || ((*worker)->s->busy == mycandidate->s->busy && (*worker)->s->lbstatus > mycandidate->s->lbstatus))
83 mycandidate = *worker;
89 checked_standby = checking_standby++;
95 } while (cur_lbset <= max_lbset && !mycandidate);
98 mycandidate->s->lbstatus -= total_factor;
99 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
100 "proxy: bybusyness selected worker \"%s\" : busy %" APR_SIZE_T_FMT " : lbstatus %d",
101 mycandidate->s->name, mycandidate->s->busy, mycandidate->s->lbstatus);
109 /* assumed to be mutex protected by caller */
110 static apr_status_t reset(proxy_balancer *balancer, server_rec *s) {
112 proxy_worker **worker;
113 worker = (proxy_worker **)balancer->workers->elts;
114 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
115 (*worker)->s->lbstatus = 0;
116 (*worker)->s->busy = 0;
121 static apr_status_t age(proxy_balancer *balancer, server_rec *s) {
125 static const proxy_balancer_method bybusyness =
128 &find_best_bybusyness,
135 static void register_hook(apr_pool_t *p)
137 ap_register_provider(p, PROXY_LBMETHOD, "bybusyness", "0", &bybusyness);
140 AP_DECLARE_MODULE(lbmethod_bybusyness) = {
141 STANDARD20_MODULE_STUFF,
142 NULL, /* create per-directory config structure */
143 NULL, /* merge per-directory config structures */
144 NULL, /* create per-server config structure */
145 NULL, /* merge per-server config structures */
146 NULL, /* command apr_table_t */
147 register_hook /* register hooks */