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_byrequests_module;
26 * The idea behind the find_best_byrequests scheduler is the following:
28 * lbfactor is "how much we expect this worker to work", or "the worker's
29 * normalized work quota".
31 * lbstatus is "how urgent this worker has to work to fulfill its quota
34 * We distribute each worker's work quota to the worker, and then look
35 * which of them needs to work most urgently (biggest lbstatus). This
36 * worker is then selected for work, and its lbstatus reduced by the
37 * total work quota we distributed to all workers. Thus the sum of all
38 * lbstatus does not change.(*)
40 * If some workers are disabled, the others will
41 * still be scheduled correctly.
43 * If a balancer is configured as follows:
46 * lbfactor 25 25 25 25
48 * And b gets disabled, the following schedule is produced:
50 * a c d a c d a c d ...
52 * Note that the above lbfactor setting is the *exact* same as:
57 * Asymmetric configurations work as one would expect. For
63 * would have a, b and c all handling about the same
64 * amount of load with d handling twice what a or b
65 * or c handles individually. So we could see:
67 * b a d c d a c d b d ...
71 static proxy_worker *find_best_byrequests(proxy_balancer *balancer,
76 proxy_worker **worker;
77 proxy_worker *mycandidate = NULL;
83 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
84 "proxy: Entering byrequests for BALANCER (%s)",
87 /* First try to see if we have available candidate */
89 checking_standby = checked_standby = 0;
90 while (!mycandidate && !checked_standby) {
91 worker = (proxy_worker **)balancer->workers->elts;
92 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
93 if (!checking_standby) { /* first time through */
94 if ((*worker)->s->lbset > max_lbset)
95 max_lbset = (*worker)->s->lbset;
98 ((*worker)->s->lbset != cur_lbset) ||
99 (checking_standby ? !PROXY_WORKER_IS_STANDBY(*worker) : PROXY_WORKER_IS_STANDBY(*worker)) ||
100 (PROXY_WORKER_IS_DRAINING(*worker))
105 /* If the worker is in error state run
106 * retry on that worker. It will be marked as
107 * operational if the retry timeout is elapsed.
108 * The worker might still be unusable, but we try
111 if (!PROXY_WORKER_IS_USABLE(*worker))
112 ap_proxy_retry_worker("BALANCER", *worker, r->server);
113 /* Take into calculation only the workers that are
114 * not in error state or not disabled.
116 if (PROXY_WORKER_IS_USABLE(*worker)) {
117 (*worker)->s->lbstatus += (*worker)->s->lbfactor;
118 total_factor += (*worker)->s->lbfactor;
119 if (!mycandidate || (*worker)->s->lbstatus > mycandidate->s->lbstatus)
120 mycandidate = *worker;
123 checked_standby = checking_standby++;
126 } while (cur_lbset <= max_lbset && !mycandidate);
129 mycandidate->s->lbstatus -= total_factor;
130 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
131 "proxy: byrequests selected worker \"%s\" : busy %" APR_SIZE_T_FMT " : lbstatus %d",
132 mycandidate->s->name, mycandidate->s->busy, mycandidate->s->lbstatus);
139 /* assumed to be mutex protected by caller */
140 static apr_status_t reset(proxy_balancer *balancer, server_rec *s) {
142 proxy_worker **worker;
143 worker = (proxy_worker **)balancer->workers->elts;
144 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
145 (*worker)->s->lbstatus = 0;
150 static apr_status_t age(proxy_balancer *balancer, server_rec *s) {
155 * How to add additional lbmethods:
156 * 1. Create func which determines "best" candidate worker
157 * (eg: find_best_bytraffic, above)
158 * 2. Register it as a provider.
160 static const proxy_balancer_method byrequests =
163 &find_best_byrequests,
169 static void register_hook(apr_pool_t *p)
171 /* Only the mpm_winnt has child init hook handler.
172 * make sure that we are called after the mpm
173 * initializes and after the mod_proxy
175 ap_register_provider(p, PROXY_LBMETHOD, "byrequests", "0", &byrequests);
178 AP_DECLARE_MODULE(lbmethod_byrequests) = {
179 STANDARD20_MODULE_STUFF,
180 NULL, /* create per-directory config structure */
181 NULL, /* merge per-directory config structures */
182 NULL, /* create per-server config structure */
183 NULL, /* merge per-server config structures */
184 NULL, /* command apr_table_t */
185 register_hook /* register hooks */