Apache HTTPD
mod_lbmethod_byrequests.c
Go to the documentation of this file.
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
23module AP_MODULE_DECLARE_DATA lbmethod_byrequests_module;
24
27
29{
30 int *total_factor = (int *)baton;
31
32 current->s->lbstatus += current->s->lbfactor;
33 *total_factor += current->s->lbfactor;
34
35 return (!prev_best || (current->s->lbstatus > prev_best->s->lbstatus));
36}
37
38/*
39 * The idea behind the find_best_byrequests scheduler is the following:
40 *
41 * lbfactor is "how much we expect this worker to work", or "the worker's
42 * normalized work quota".
43 *
44 * lbstatus is "how urgent this worker has to work to fulfill its quota
45 * of work".
46 *
47 * We distribute each worker's work quota to the worker, and then look
48 * which of them needs to work most urgently (biggest lbstatus). This
49 * worker is then selected for work, and its lbstatus reduced by the
50 * total work quota we distributed to all workers. Thus the sum of all
51 * lbstatus does not change.(*)
52 *
53 * If some workers are disabled, the others will
54 * still be scheduled correctly.
55 *
56 * If a balancer is configured as follows:
57 *
58 * worker a b c d
59 * lbfactor 25 25 25 25
60 *
61 * And b gets disabled, the following schedule is produced:
62 *
63 * a c d a c d a c d ...
64 *
65 * Note that the above lbfactor setting is the *exact* same as:
66 *
67 * worker a b c d
68 * lbfactor 1 1 1 1
69 *
70 * Asymmetric configurations work as one would expect. For
71 * example:
72 *
73 * worker a b c d
74 * lbfactor 1 1 1 2
75 *
76 * would have a, b and c all handling about the same
77 * amount of load with d handling twice what a or b
78 * or c handles individually. So we could see:
79 *
80 * b a d c d a c d b d ...
81 *
82 */
85{
86 int total_factor = 0;
88
89 if (worker) {
90 worker->s->lbstatus -= total_factor;
91 }
92
93 return worker;
94}
95
96/* assumed to be mutex protected by caller */
98{
99 int i;
100 proxy_worker **worker;
101 worker = (proxy_worker **)balancer->workers->elts;
102 for (i = 0; i < balancer->workers->nelts; i++, worker++) {
103 (*worker)->s->lbstatus = 0;
104 }
105 return APR_SUCCESS;
106}
107
109{
110 return APR_SUCCESS;
111}
112
113/*
114 * How to add additional lbmethods:
115 * 1. Create func which determines "best" candidate worker
116 * (eg: find_best_bytraffic, above)
117 * 2. Register it as a provider.
118 */
120{
121 "byrequests",
123 NULL,
124 &reset,
125 &age,
126 NULL
127};
128
129/* post_config hook: */
131 apr_pool_t *ptemp, server_rec *s)
132{
133
134 /* lbmethod_byrequests_post_config() will be called twice during startup. So, don't
135 * set up the static data the 1st time through. */
137 return OK;
138 }
139
144 "mod_proxy must be loaded for mod_lbmethod_byrequests");
145 return !OK;
146 }
147
148 return OK;
149}
150
156
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};
ap hook functions and macros
Apache Multi-Processing Module library.
APR Versioning Interface.
static apr_pool_t * pconf
Definition event.c:441
void ap_hook_post_config(ap_HOOK_post_config_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition config.c:105
#define AP_DECLARE_MODULE(foo)
request_rec * r
#define OK
Definition httpd.h:456
#define AP_SQ_MS_CREATE_PRE_CONFIG
Definition http_core.h:1047
int ap_state_query(int query_code)
Definition core.c:5378
#define AP_SQ_MAIN_STATE
Definition http_core.h:1030
#define APLOGNO(n)
Definition http_log.h:117
#define ap_log_error
Definition http_log.h:370
#define APLOG_MARK
Definition http_log.h:283
#define APLOG_EMERG
Definition http_log.h:64
apr_status_t ap_register_provider(apr_pool_t *pool, const char *provider_group, const char *provider_name, const char *provider_version, const void *provider)
Definition provider.c:35
ap_vhost_iterate_conn_cb void * baton
Definition http_vhost.h:87
#define APR_HOOK_MIDDLE
Definition apr_hooks.h:303
#define APR_RETRIEVE_OPTIONAL_FN(name)
#define APR_OPTIONAL_FN_TYPE(name)
#define PROXY_LBMETHOD
Definition mod_proxy.h:1444
#define STANDARD20_MODULE_STUFF
apr_size_t size
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
const char * s
Definition apr_strings.h:95
apr_pool_t * p
Definition md_event.c:32
static apr_status_t age(proxy_balancer *balancer, server_rec *s)
static const proxy_balancer_method byrequests
static void register_hook(apr_pool_t *p)
static apr_OFN_proxy_balancer_get_best_worker_t * ap_proxy_balancer_get_best_worker_fn
static proxy_worker * find_best_byrequests(proxy_balancer *balancer, request_rec *r)
static int lbmethod_byrequests_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
static apr_status_t reset(proxy_balancer *balancer, server_rec *s)
static int is_best_byrequests(proxy_worker *current, proxy_worker *prev_best, void *baton)
Proxy Extension Module for Apache.
return NULL
Definition mod_so.c:359
int i
Definition mod_so.c:347
static proxy_worker * proxy_balancer_get_best_worker(proxy_balancer *balancer, request_rec *r, proxy_is_best_callback_fn_t *is_best, void *baton)
Apache scoreboard library.
apr_array_header_t * workers
Definition mod_proxy.h:559
proxy_worker_shared * s
Definition mod_proxy.h:505
A structure that represents the current request.
Definition httpd.h:845
A structure to store information for each virtual server.
Definition httpd.h:1322