]> granicus.if.org Git - apache/blob - modules/proxy/balancers/mod_lbmethod_byrequests.c
Move useful and shared balancer stuff to the shm slot...
[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 "ap_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->s->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 (
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))
101                     ) {
102                     continue;
103                 }
104
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
109                  * anyway.
110                  */
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.
115                  */
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;
121                 }
122             }
123             checked_standby = checking_standby++;
124         }
125         cur_lbset++;
126     } while (cur_lbset <= max_lbset && !mycandidate);
127
128     if (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);
133
134     }
135
136     return mycandidate;
137 }
138
139 /* assumed to be mutex protected by caller */
140 static apr_status_t reset(proxy_balancer *balancer, server_rec *s) {
141     int i;
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;
146     }
147     return APR_SUCCESS;
148 }
149
150 static apr_status_t age(proxy_balancer *balancer, server_rec *s) {
151         return APR_SUCCESS;
152 }
153
154 /*
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.
159  */
160 static const proxy_balancer_method byrequests =
161 {
162     "byrequests",
163     &find_best_byrequests,
164     NULL,
165     &reset,
166     &age
167 };
168
169 static void register_hook(apr_pool_t *p)
170 {
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
174      */
175     ap_register_provider(p, PROXY_LBMETHOD, "byrequests", "0", &byrequests);
176 }
177
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 */
186 };