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_bytraffic_module
;
26 * The idea behind the find_best_bytraffic scheduler is the following:
28 * We know the amount of traffic (bytes in and out) handled by each
29 * worker. We normalize that traffic by each workers' weight. So assuming
35 * the scheduler will allow worker c to handle 3 times the
36 * traffic of a and b. If each request/response results in the
37 * same amount of traffic, then c would be accessed 3 times as
38 * often as a or b. If, for example, a handled a request that
39 * resulted in a large i/o bytecount, then b and c would be
40 * chosen more often, to even things out.
42 static proxy_worker
*find_best_bytraffic(proxy_balancer
*balancer
,
46 apr_off_t mytraffic
= 0;
49 proxy_worker
*mycandidate
= NULL
;
55 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, 0, r
->server
,
56 "proxy: Entering bytraffic for BALANCER (%s)",
59 /* First try to see if we have available candidate */
61 checking_standby
= checked_standby
= 0;
62 while (!mycandidate
&& !checked_standby
) {
63 worker
= (proxy_worker
*)balancer
->workers
->elts
;
64 for (i
= 0; i
< balancer
->workers
->nelts
; i
++, worker
++) {
65 if (!checking_standby
) { /* first time through */
66 if (worker
->s
->lbset
> max_lbset
)
67 max_lbset
= worker
->s
->lbset
;
69 if (worker
->s
->lbset
!= cur_lbset
)
71 if ( (checking_standby
? !PROXY_WORKER_IS_STANDBY(worker
) : PROXY_WORKER_IS_STANDBY(worker
)) )
73 /* If the worker is in error state run
74 * retry on that worker. It will be marked as
75 * operational if the retry timeout is elapsed.
76 * The worker might still be unusable, but we try
79 if (!PROXY_WORKER_IS_USABLE(worker
))
80 ap_proxy_retry_worker("BALANCER", worker
, r
->server
);
81 /* Take into calculation only the workers that are
82 * not in error state or not disabled.
84 if (PROXY_WORKER_IS_USABLE(worker
)) {
85 mytraffic
= (worker
->s
->transferred
/worker
->s
->lbfactor
) +
86 (worker
->s
->read
/worker
->s
->lbfactor
);
87 if (!mycandidate
|| mytraffic
< curmin
) {
93 checked_standby
= checking_standby
++;
96 } while (cur_lbset
<= max_lbset
&& !mycandidate
);
99 ap_log_error(APLOG_MARK
, APLOG_DEBUG
, 0, r
->server
,
100 "proxy: bytraffic selected worker \"%s\" : busy %" APR_SIZE_T_FMT
,
101 mycandidate
->name
, mycandidate
->s
->busy
);
108 static const proxy_balancer_method bytraffic
=
111 &find_best_bytraffic
,
115 static void register_hook(apr_pool_t
*p
)
117 /* Only the mpm_winnt has child init hook handler.
118 * make sure that we are called after the mpm
119 * initializes and after the mod_proxy
121 ap_register_provider(p
, PROXY_LBMETHOD
, "bytraffic", "0", &bytraffic
);
124 module AP_MODULE_DECLARE_DATA lbmethod_bytraffic_module
= {
125 STANDARD20_MODULE_STUFF
,
126 NULL
, /* create per-directory config structure */
127 NULL
, /* merge per-directory config structures */
128 NULL
, /* create per-server config structure */
129 NULL
, /* merge per-server config structures */
130 NULL
, /* command apr_table_t */
131 register_hook
/* register hooks */