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_bybusyness_module;
25 static int (*ap_proxy_retry_worker_fn)(const char *proxy_function,
26 proxy_worker *worker, server_rec *s) = NULL;
28 static proxy_worker *find_best_bybusyness(proxy_balancer *balancer,
33 proxy_worker **worker;
34 proxy_worker *mycandidate = NULL;
42 if (!ap_proxy_retry_worker_fn) {
43 ap_proxy_retry_worker_fn =
44 APR_RETRIEVE_OPTIONAL_FN(ap_proxy_retry_worker);
45 if (!ap_proxy_retry_worker_fn) {
46 /* can only happen if mod_proxy isn't loaded */
51 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
52 "proxy: Entering bybusyness for BALANCER (%s)",
55 /* First try to see if we have available candidate */
58 checking_standby = checked_standby = 0;
59 while (!mycandidate && !checked_standby) {
61 worker = (proxy_worker **)balancer->workers->elts;
62 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
63 if (!checking_standby) { /* first time through */
64 if ((*worker)->s->lbset > max_lbset)
65 max_lbset = (*worker)->s->lbset;
68 ((*worker)->s->lbset != cur_lbset) ||
69 (checking_standby ? !PROXY_WORKER_IS_STANDBY(*worker) : PROXY_WORKER_IS_STANDBY(*worker)) ||
70 (PROXY_WORKER_IS_DRAINING(*worker))
75 /* If the worker is in error state run
76 * retry on that worker. It will be marked as
77 * operational if the retry timeout is elapsed.
78 * The worker might still be unusable, but we try
81 if (!PROXY_WORKER_IS_USABLE(*worker)) {
82 ap_proxy_retry_worker_fn("BALANCER", *worker, r->server);
85 /* Take into calculation only the workers that are
86 * not in error state or not disabled.
88 if (PROXY_WORKER_IS_USABLE(*worker)) {
90 (*worker)->s->lbstatus += (*worker)->s->lbfactor;
91 total_factor += (*worker)->s->lbfactor;
94 || (*worker)->s->busy < mycandidate->s->busy
95 || ((*worker)->s->busy == mycandidate->s->busy && (*worker)->s->lbstatus > mycandidate->s->lbstatus))
96 mycandidate = *worker;
102 checked_standby = checking_standby++;
108 } while (cur_lbset <= max_lbset && !mycandidate);
111 mycandidate->s->lbstatus -= total_factor;
112 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
113 "proxy: bybusyness selected worker \"%s\" : busy %" APR_SIZE_T_FMT " : lbstatus %d",
114 mycandidate->s->name, mycandidate->s->busy, mycandidate->s->lbstatus);
122 /* assumed to be mutex protected by caller */
123 static apr_status_t reset(proxy_balancer *balancer, server_rec *s) {
125 proxy_worker **worker;
126 worker = (proxy_worker **)balancer->workers->elts;
127 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
128 (*worker)->s->lbstatus = 0;
129 (*worker)->s->busy = 0;
134 static apr_status_t age(proxy_balancer *balancer, server_rec *s) {
138 static const proxy_balancer_method bybusyness =
141 &find_best_bybusyness,
148 static void register_hook(apr_pool_t *p)
150 ap_register_provider(p, PROXY_LBMETHOD, "bybusyness", "0", &bybusyness);
153 AP_DECLARE_MODULE(lbmethod_bybusyness) = {
154 STANDARD20_MODULE_STUFF,
155 NULL, /* create per-directory config structure */
156 NULL, /* merge per-directory config structures */
157 NULL, /* create per-server config structure */
158 NULL, /* merge per-server config structures */
159 NULL, /* command apr_table_t */
160 register_hook /* register hooks */