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"
21 #include "apr_hooks.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,
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;
97 if (worker->s->lbset > cur_lbset)
99 if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
101 /* If the worker is in error state run
102 * retry on that worker. It will be marked as
103 * operational if the retry timeout is elapsed.
104 * The worker might still be unusable, but we try
107 if (!PROXY_WORKER_IS_USABLE(worker))
108 ap_proxy_retry_worker("BALANCER", worker, r->server);
109 /* Take into calculation only the workers that are
110 * not in error state or not disabled.
112 if (PROXY_WORKER_IS_USABLE(worker)) {
113 worker->s->lbstatus += worker->s->lbfactor;
114 total_factor += worker->s->lbfactor;
115 if (!mycandidate || worker->s->lbstatus > mycandidate->s->lbstatus)
116 mycandidate = worker;
119 checked_standby = checking_standby++;
122 } while (cur_lbset <= max_lbset && !mycandidate);
125 mycandidate->s->lbstatus -= total_factor;
126 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
127 "proxy: byrequests selected worker \"%s\" : busy %" APR_SIZE_T_FMT " : lbstatus %d",
128 mycandidate->name, mycandidate->s->busy, mycandidate->s->lbstatus);
136 * How to add additional lbmethods:
137 * 1. Create func which determines "best" candidate worker
138 * (eg: find_best_bytraffic, above)
139 * 2. Register it as a provider.
141 static const proxy_balancer_method byrequests =
144 &find_best_byrequests,
148 static void register_hook(apr_pool_t *p)
150 /* Only the mpm_winnt has child init hook handler.
151 * make sure that we are called after the mpm
152 * initializes and after the mod_proxy
154 ap_register_provider(p, PROXY_LBMETHOD, "byrequests", "0", &byrequests);
157 module AP_MODULE_DECLARE_DATA lbmethod_byrequests_module = {
158 STANDARD20_MODULE_STUFF,
159 NULL, /* create per-directory config structure */
160 NULL, /* merge per-directory config structures */
161 NULL, /* create per-server config structure */
162 NULL, /* merge per-server config structures */
163 NULL, /* command apr_table_t */
164 register_hook /* register hooks */