]> granicus.if.org Git - apache/blob - modules/proxy/balancers/mod_lbmethod_byrequests.c
Add in independent byrequests lbmethod
[apache] / modules / proxy / balancers / mod_lbmethod_byrequests.c
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "mod_proxy.h"
18 #include "scoreboard.h"
19 #include "ap_mpm.h"
20 #include "apr_version.h"
21 #include "apr_hooks.h"
22
23 module AP_MODULE_DECLARE_DATA lbmethod_byrequests_module;
24
25 /*
26  * The idea behind the find_best_byrequests scheduler is the following:
27  *
28  * lbfactor is "how much we expect this worker to work", or "the worker's
29  * normalized work quota".
30  *
31  * lbstatus is "how urgent this worker has to work to fulfill its quota
32  * of work".
33  *
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.(*)
39  *
40  * If some workers are disabled, the others will
41  * still be scheduled correctly.
42  *
43  * If a balancer is configured as follows:
44  *
45  * worker     a    b    c    d
46  * lbfactor  25   25   25   25
47  *
48  * And b gets disabled, the following schedule is produced:
49  *
50  *    a c d a c d a c d ...
51  *
52  * Note that the above lbfactor setting is the *exact* same as:
53  *
54  * worker     a    b    c    d
55  * lbfactor   1    1    1    1
56  *
57  * Asymmetric configurations work as one would expect. For
58  * example:
59  *
60  * worker     a    b    c    d
61  * lbfactor   1    1    1    2
62  *
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:
66  *
67  *   b a d c d a c d b d ...
68  *
69  */
70
71 static proxy_worker *find_best_byrequests(proxy_balancer *balancer,
72                                 request_rec *r)
73 {
74     int i;
75     int total_factor = 0;
76     proxy_worker *worker;
77     proxy_worker *mycandidate = NULL;
78     int cur_lbset = 0;
79     int max_lbset = 0;
80     int checking_standby;
81     int checked_standby;
82     
83     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
84                  "proxy: Entering byrequests for BALANCER (%s)",
85                  balancer->name);
86
87     /* First try to see if we have available candidate */
88     do {
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;
96                 }
97                 if (worker->s->lbset > cur_lbset)
98                     continue;
99                 if ( (checking_standby ? !PROXY_WORKER_IS_STANDBY(worker) : PROXY_WORKER_IS_STANDBY(worker)) )
100                     continue;
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
105                  * anyway.
106                  */
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.
111                  */
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;
117                 }
118             }
119             checked_standby = checking_standby++;
120         }
121         cur_lbset++;
122     } while (cur_lbset <= max_lbset && !mycandidate);
123
124     if (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);
129
130     }
131
132     return mycandidate;
133 }
134
135 /*
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.
140  */
141 static const proxy_balancer_method byrequests =
142 {
143     "byrequests",
144     &find_best_byrequests,
145     NULL
146 };
147
148 static void register_hook(apr_pool_t *p)
149 {
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
153      */
154     ap_register_provider(p, PROXY_LBMETHOD, "byrequests", "0", &byrequests);
155 }
156
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 */
165 };