comparison src/http/modules/ngx_http_upstream_least_conn_module.c @ 4654:a2ca3a6ee680

Upstream: least_conn balancer module.
author Maxim Dounin <mdounin@mdounin.ru>
date Sun, 03 Jun 2012 23:21:26 +0000
parents
children 90ddd4abfdd2
comparison
equal deleted inserted replaced
4653:134ccdf44647 4654:a2ca3a6ee680
1
2 /*
3 * Copyright (C) Maxim Dounin
4 * Copyright (C) Nginx, Inc.
5 */
6
7
8 #include <ngx_config.h>
9 #include <ngx_core.h>
10 #include <ngx_http.h>
11
12
13 typedef struct {
14 ngx_uint_t *conns;
15 } ngx_http_upstream_least_conn_conf_t;
16
17
18 typedef struct {
19 /* the round robin data must be first */
20 ngx_http_upstream_rr_peer_data_t rrp;
21
22 ngx_uint_t *conns;
23
24 ngx_event_get_peer_pt get_rr_peer;
25 ngx_event_free_peer_pt free_rr_peer;
26 } ngx_http_upstream_lc_peer_data_t;
27
28
29 static ngx_int_t ngx_http_upstream_init_least_conn_peer(ngx_http_request_t *r,
30 ngx_http_upstream_srv_conf_t *us);
31 static ngx_int_t ngx_http_upstream_get_least_conn_peer(
32 ngx_peer_connection_t *pc, void *data);
33 static void ngx_http_upstream_free_least_conn_peer(ngx_peer_connection_t *pc,
34 void *data, ngx_uint_t state);
35 static void *ngx_http_upstream_least_conn_create_conf(ngx_conf_t *cf);
36 static char *ngx_http_upstream_least_conn(ngx_conf_t *cf, ngx_command_t *cmd,
37 void *conf);
38
39
40 static ngx_command_t ngx_http_upstream_least_conn_commands[] = {
41
42 { ngx_string("least_conn"),
43 NGX_HTTP_UPS_CONF|NGX_CONF_NOARGS,
44 ngx_http_upstream_least_conn,
45 0,
46 0,
47 NULL },
48
49 ngx_null_command
50 };
51
52
53 static ngx_http_module_t ngx_http_upstream_least_conn_module_ctx = {
54 NULL, /* preconfiguration */
55 NULL, /* postconfiguration */
56
57 NULL, /* create main configuration */
58 NULL, /* init main configuration */
59
60 ngx_http_upstream_least_conn_create_conf, /* create server configuration */
61 NULL, /* merge server configuration */
62
63 NULL, /* create location configuration */
64 NULL /* merge location configuration */
65 };
66
67
68 ngx_module_t ngx_http_upstream_least_conn_module = {
69 NGX_MODULE_V1,
70 &ngx_http_upstream_least_conn_module_ctx, /* module context */
71 ngx_http_upstream_least_conn_commands, /* module directives */
72 NGX_HTTP_MODULE, /* module type */
73 NULL, /* init master */
74 NULL, /* init module */
75 NULL, /* init process */
76 NULL, /* init thread */
77 NULL, /* exit thread */
78 NULL, /* exit process */
79 NULL, /* exit master */
80 NGX_MODULE_V1_PADDING
81 };
82
83
84 ngx_int_t
85 ngx_http_upstream_init_least_conn(ngx_conf_t *cf,
86 ngx_http_upstream_srv_conf_t *us)
87 {
88 ngx_uint_t n;
89 ngx_http_upstream_rr_peers_t *peers;
90 ngx_http_upstream_least_conn_conf_t *lcf;
91
92 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0,
93 "init least conn");
94
95 if (ngx_http_upstream_init_round_robin(cf, us) != NGX_OK) {
96 return NGX_ERROR;
97 }
98
99 peers = us->peer.data;
100
101 n = peers->number;
102
103 if (peers->next) {
104 n += peers->next->number;
105 }
106
107 lcf = ngx_http_conf_upstream_srv_conf(us,
108 ngx_http_upstream_least_conn_module);
109
110 lcf->conns = ngx_pcalloc(cf->pool, sizeof(ngx_uint_t) * n);
111 if (lcf->conns == NULL) {
112 return NGX_ERROR;
113 }
114
115 us->peer.init = ngx_http_upstream_init_least_conn_peer;
116
117 return NGX_OK;
118 }
119
120
121 static ngx_int_t
122 ngx_http_upstream_init_least_conn_peer(ngx_http_request_t *r,
123 ngx_http_upstream_srv_conf_t *us)
124 {
125 ngx_int_t rc;
126 ngx_http_upstream_lc_peer_data_t *lcp;
127 ngx_http_upstream_least_conn_conf_t *lcf;
128
129 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
130 "init least conn peer");
131
132 lcf = ngx_http_conf_upstream_srv_conf(us,
133 ngx_http_upstream_least_conn_module);
134
135 lcp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_lc_peer_data_t));
136 if (lcp == NULL) {
137 return NGX_ERROR;
138 }
139
140 lcp->conns = lcf->conns;
141
142 r->upstream->peer.data = &lcp->rrp;
143
144 rc = ngx_http_upstream_init_round_robin_peer(r, us);
145
146 if (ngx_http_upstream_init_round_robin_peer(r, us) != NGX_OK) {
147 return NGX_ERROR;
148 }
149
150 r->upstream->peer.get = ngx_http_upstream_get_least_conn_peer;
151 r->upstream->peer.free = ngx_http_upstream_free_least_conn_peer;
152
153 lcp->get_rr_peer = ngx_http_upstream_get_round_robin_peer;
154 lcp->free_rr_peer = ngx_http_upstream_free_round_robin_peer;
155
156 return NGX_OK;
157 }
158
159
160 static ngx_int_t
161 ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
162 {
163 ngx_http_upstream_lc_peer_data_t *lcp = data;
164
165 time_t now;
166 uintptr_t m;
167 ngx_int_t rc, total;
168 ngx_uint_t i, n, p, many;
169 ngx_http_upstream_rr_peer_t *peer, *best;
170 ngx_http_upstream_rr_peers_t *peers;
171
172 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
173 "get least conn peer, try: %ui", pc->tries);
174
175 if (lcp->rrp.peers->single) {
176 return lcp->get_rr_peer(pc, &lcp->rrp);
177 }
178
179 pc->cached = 0;
180 pc->connection = NULL;
181
182 now = ngx_time();
183
184 peers = lcp->rrp.peers;
185
186 best = NULL;
187 total = 0;
188
189 #if (NGX_SUPPRESS_WARN)
190 many = 0;
191 p = 0;
192 #endif
193
194 for (i = 0; i < peers->number; i++) {
195
196 n = i / (8 * sizeof(uintptr_t));
197 m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
198
199 if (lcp->rrp.tried[n] & m) {
200 continue;
201 }
202
203 peer = &peers->peer[i];
204
205 if (peer->down) {
206 continue;
207 }
208
209 if (peer->max_fails
210 && peer->fails >= peer->max_fails
211 && now - peer->checked <= peer->fail_timeout)
212 {
213 continue;
214 }
215
216 /*
217 * select peer with least number of connections; if there are
218 * multiple peers with the same number of connections, select
219 * based on round-robin
220 */
221
222 if (best == NULL
223 || lcp->conns[i] * best->weight < lcp->conns[p] * peer->weight)
224 {
225 best = peer;
226 many = 0;
227 p = i;
228
229 } else if (lcp->conns[i] * best->weight
230 == lcp->conns[p] * peer->weight)
231 {
232 many = 1;
233 }
234 }
235
236 if (best == NULL) {
237 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
238 "get least conn peer, no peer found");
239
240 goto failed;
241 }
242
243 if (many) {
244 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
245 "get least conn peer, many");
246
247 for (i = p; i < peers->number; i++) {
248
249 n = i / (8 * sizeof(uintptr_t));
250 m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
251
252 if (lcp->rrp.tried[n] & m) {
253 continue;
254 }
255
256 peer = &peers->peer[i];
257
258 if (peer->down) {
259 continue;
260 }
261
262 if (lcp->conns[i] * best->weight != lcp->conns[p] * peer->weight) {
263 continue;
264 }
265
266 if (peer->max_fails
267 && peer->fails >= peer->max_fails
268 && now - peer->checked <= peer->fail_timeout)
269 {
270 continue;
271 }
272
273 peer->current_weight += peer->effective_weight;
274 total += peer->effective_weight;
275
276 if (peer->effective_weight < peer->weight) {
277 peer->effective_weight++;
278 }
279
280 if (peer->current_weight > best->current_weight) {
281 best = peer;
282 p = i;
283 }
284 }
285 }
286
287 best->current_weight -= total;
288 best->checked = now;
289
290 pc->sockaddr = best->sockaddr;
291 pc->socklen = best->socklen;
292 pc->name = &best->name;
293
294 lcp->rrp.current = p;
295
296 n = p / (8 * sizeof(uintptr_t));
297 m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t));
298
299 lcp->rrp.tried[n] |= m;
300 lcp->conns[p]++;
301
302 if (pc->tries == 1 && peers->next) {
303 pc->tries += peers->next->number;
304 }
305
306 return NGX_OK;
307
308 failed:
309
310 if (peers->next) {
311 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
312 "get least conn peer, backup servers");
313
314 lcp->conns += peers->number;
315
316 lcp->rrp.peers = peers->next;
317 pc->tries = lcp->rrp.peers->number;
318
319 n = lcp->rrp.peers->number / (8 * sizeof(uintptr_t)) + 1;
320 for (i = 0; i < n; i++) {
321 lcp->rrp.tried[i] = 0;
322 }
323
324 rc = ngx_http_upstream_get_least_conn_peer(pc, lcp);
325
326 if (rc != NGX_BUSY) {
327 return rc;
328 }
329 }
330
331 /* all peers failed, mark them as live for quick recovery */
332
333 for (i = 0; i < peers->number; i++) {
334 peers->peer[i].fails = 0;
335 }
336
337 pc->name = peers->name;
338
339 return NGX_BUSY;
340 }
341
342
343 static void
344 ngx_http_upstream_free_least_conn_peer(ngx_peer_connection_t *pc,
345 void *data, ngx_uint_t state)
346 {
347 ngx_http_upstream_lc_peer_data_t *lcp = data;
348
349 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
350 "free least conn peer %ui %ui", pc->tries, state);
351
352 if (lcp->rrp.peers->single) {
353 return lcp->free_rr_peer(pc, &lcp->rrp, state);
354 }
355
356 if (state == 0 && pc->tries == 0) {
357 return;
358 }
359
360 lcp->conns[lcp->rrp.current]--;
361
362 return lcp->free_rr_peer(pc, &lcp->rrp, state);
363 }
364
365
366 static void *
367 ngx_http_upstream_least_conn_create_conf(ngx_conf_t *cf)
368 {
369 ngx_http_upstream_least_conn_conf_t *conf;
370
371 conf = ngx_pcalloc(cf->pool,
372 sizeof(ngx_http_upstream_least_conn_conf_t));
373 if (conf == NULL) {
374 return NULL;
375 }
376
377 /*
378 * set by ngx_pcalloc():
379 *
380 * conf->conns = NULL;
381 */
382
383 return conf;
384 }
385
386
387 static char *
388 ngx_http_upstream_least_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
389 {
390 ngx_http_upstream_srv_conf_t *uscf;
391
392 uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
393
394 uscf->peer.init_upstream = ngx_http_upstream_init_least_conn;
395
396 uscf->flags = NGX_HTTP_UPSTREAM_CREATE
397 |NGX_HTTP_UPSTREAM_WEIGHT
398 |NGX_HTTP_UPSTREAM_MAX_FAILS
399 |NGX_HTTP_UPSTREAM_FAIL_TIMEOUT
400 |NGX_HTTP_UPSTREAM_DOWN
401 |NGX_HTTP_UPSTREAM_BACKUP;
402
403 return NGX_CONF_OK;
404 }