Skip to content

Commit a6d6d38

Browse files
author
jizhuozhi
committed
VNSWRR limit the number of virtual peers when init
1 parent 740c186 commit a6d6d38

File tree

2 files changed

+123
-10
lines changed

2 files changed

+123
-10
lines changed

modules/ngx_http_upstream_vnswrr_module/ngx_http_upstream_vnswrr_module.c

+64-10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct ngx_http_upstream_vnswrr_srv_conf_s {
3030
ngx_uint_t vnumber;
3131
ngx_uint_t last_number;
3232
ngx_uint_t init_number;
33+
ngx_uint_t max_init;
3334
ngx_uint_t gcd;
3435
ngx_http_upstream_rr_peer_t *last_peer;
3536
ngx_http_upstream_rr_vpeers_t *vpeers;
@@ -138,9 +139,13 @@ ngx_http_upstream_vnswrr_create_srv_conf(ngx_conf_t *cf)
138139
static char *
139140
ngx_http_upstream_vnswrr(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
140141
{
141-
ngx_http_upstream_srv_conf_t *uscf;
142+
ngx_http_upstream_srv_conf_t *uscf;
143+
ngx_http_upstream_vnswrr_srv_conf_t *uvnscf;
144+
ngx_str_t *value;
145+
ngx_int_t max_init;
146+
ngx_uint_t i;
142147

143-
uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
148+
uscf= ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
144149

145150
if (uscf->peer.init_upstream) {
146151
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
@@ -159,7 +164,39 @@ ngx_http_upstream_vnswrr(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
159164
#endif
160165
|NGX_HTTP_UPSTREAM_DOWN;
161166

167+
uvnscf = ngx_http_conf_upstream_srv_conf(uscf,
168+
ngx_http_upstream_vnswrr_module);
169+
170+
value = cf->args->elts;
171+
172+
max_init = 0;
173+
174+
for (i = 2; i < cf->args->nelts; i++) {
175+
176+
if (ngx_strncmp(value[i].data, "max_init=", 9) == 0) {
177+
178+
max_init = ngx_atoi(&value[i].data[9], value[i].len - 9);
179+
180+
if (max_init == NGX_ERROR) {
181+
goto invalid;
182+
}
183+
184+
continue;
185+
}
186+
187+
goto invalid;
188+
}
189+
190+
uvnscf->max_init = max_init;
191+
162192
return NGX_CONF_OK;
193+
194+
invalid:
195+
196+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
197+
"invalid parameter \"%V\"", &value[i]);
198+
199+
return NGX_CONF_ERROR;
163200
}
164201

165202

@@ -170,7 +207,7 @@ ngx_http_upstream_init_vnswrr(ngx_conf_t *cf,
170207
ngx_http_upstream_rr_peers_t *peers, *backup;
171208
ngx_http_upstream_vnswrr_srv_conf_t *uvnscf, *ubvnscf;
172209
ngx_http_upstream_server_t *server;
173-
ngx_uint_t i, g, bg;
210+
ngx_uint_t i, g, bg, max_init;
174211

175212
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "init vnswrr");
176213

@@ -204,15 +241,24 @@ ngx_http_upstream_init_vnswrr(ngx_conf_t *cf,
204241
return NGX_ERROR;
205242
}
206243

244+
max_init = uvnscf->max_init;
245+
246+
peers = (ngx_http_upstream_rr_peers_t *) us->peer.data;
247+
207248
uvnscf->init_number = NGX_CONF_UNSET_UINT;
208249
uvnscf->last_number = NGX_CONF_UNSET_UINT;
209250
uvnscf->last_peer = NULL;
210251
uvnscf->next = NULL;
211252
uvnscf->gcd = g;
212253

254+
if (!max_init) {
255+
uvnscf->max_init = peers->number;
256+
} else if (max_init > peers->total_weight) {
257+
uvnscf->max_init = peers->total_weight;
258+
}
259+
213260
us->peer.init = ngx_http_upstream_init_vnswrr_peer;
214261

215-
peers = (ngx_http_upstream_rr_peers_t *) us->peer.data;
216262
if (peers->weighted) {
217263
uvnscf->vpeers = ngx_pcalloc(cf->pool,
218264
sizeof(ngx_http_upstream_rr_vpeers_t)
@@ -221,7 +267,7 @@ ngx_http_upstream_init_vnswrr(ngx_conf_t *cf,
221267
return NGX_ERROR;
222268
}
223269

224-
ngx_http_upstream_init_virtual_peers(peers, uvnscf, 0, peers->number);
270+
ngx_http_upstream_init_virtual_peers(peers, uvnscf, 0, uvnscf->max_init);
225271

226272
}
227273

@@ -238,6 +284,14 @@ ngx_http_upstream_init_vnswrr(ngx_conf_t *cf,
238284
ubvnscf->last_number = NGX_CONF_UNSET_UINT;
239285
ubvnscf->last_peer = NULL;
240286
ubvnscf->gcd = bg;
287+
288+
if (!max_init) {
289+
ubvnscf->max_init = backup->number;
290+
} else if (max_init > backup->total_weight) {
291+
ubvnscf->max_init = backup->total_weight;
292+
} else {
293+
ubvnscf->max_init = max_init;
294+
}
241295

242296
uvnscf->next = ubvnscf;
243297

@@ -252,7 +306,7 @@ ngx_http_upstream_init_vnswrr(ngx_conf_t *cf,
252306
return NGX_ERROR;
253307
}
254308

255-
ngx_http_upstream_init_virtual_peers(backup, ubvnscf, 0, backup->number);
309+
ngx_http_upstream_init_virtual_peers(backup, ubvnscf, 0, ubvnscf->max_init);
256310
}
257311

258312
return NGX_OK;
@@ -472,8 +526,8 @@ ngx_http_upstream_get_vnswrr(ngx_http_upstream_vnswrr_peer_data_t *vnsp)
472526
&& (uvnscf->last_number + 1 == uvnscf->vnumber))
473527
{
474528
n = peers->total_weight / uvnscf->gcd - uvnscf->vnumber;
475-
if (n > peers->number) {
476-
n = peers->number;
529+
if (n > uvnscf->max_init) {
530+
n = uvnscf->max_init;
477531
}
478532

479533
ngx_http_upstream_init_virtual_peers(peers, uvnscf, uvnscf->vnumber,
@@ -506,8 +560,8 @@ ngx_http_upstream_get_vnswrr(ngx_http_upstream_vnswrr_peer_data_t *vnsp)
506560
if (peers->weighted) {
507561

508562
n = peers->total_weight / uvnscf->gcd - uvnscf->vnumber;
509-
if (n > peers->number) {
510-
n = peers->number;
563+
if (n > uvnscf->max_init) {
564+
n = uvnscf->max_init;
511565
}
512566

513567
if (n > 0) {

tests/nginx-tests/tengine-tests/ngx_http_upstream_vnswrr.t

+59
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,27 @@ http {
7373
server 127.0.0.1:8083 weight=8;
7474
}
7575
76+
upstream h {
77+
vnswrr max_init=2;
78+
server 127.0.0.1:8081;
79+
server 127.0.0.1:8082;
80+
server 127.0.0.1:8083;
81+
}
82+
83+
upstream i {
84+
vnswrr max_init=1;
85+
server 127.0.0.1:8081 down;
86+
server 127.0.0.1:8082 backup;
87+
server 127.0.0.1:8083 backup;
88+
}
89+
90+
upstream j {
91+
vnswrr max_init=0;
92+
server 127.0.0.1:8081;
93+
server 127.0.0.1:8082;
94+
server 127.0.0.1:8083;
95+
}
96+
7697
server {
7798
listen 127.0.0.1:8081;
7899
listen 127.0.0.1:8082;
@@ -111,6 +132,18 @@ http {
111132
location /g {
112133
proxy_pass http://g;
113134
}
135+
136+
location /h {
137+
proxy_pass http://h;
138+
}
139+
140+
location /i {
141+
proxy_pass http://i;
142+
}
143+
144+
location /j {
145+
proxy_pass http://j;
146+
}
114147
}
115148
}
116149
@@ -178,6 +211,32 @@ $list{http_get_body('/g')} += 1;
178211
is($list{'8081'}, 2, 'weight 2');
179212
is($list{'8082'}, 4, 'weight 4');
180213
is($list{'8083'}, 8, 'weight 8');
214+
215+
%list = ();
216+
$list{http_get_body('/h')} += 1;
217+
$list{http_get_body('/h')} += 1;
218+
$list{http_get_body('/h')} += 1;
219+
220+
is($list{'8081'}, 1, 'weight 1');
221+
is($list{'8082'}, 1, 'weight 1');
222+
is($list{'8083'}, 1, 'weight 1');
223+
224+
%list = ();
225+
$list{http_get_body('/i')} += 1;
226+
$list{http_get_body('/i')} += 1;
227+
228+
is($list{'8082'}, 1, 'weight 1');
229+
is($list{'8083'}, 1, 'weight 1');
230+
231+
%list = ();
232+
$list{http_get_body('/j')} += 1;
233+
$list{http_get_body('/j')} += 1;
234+
$list{http_get_body('/j')} += 1;
235+
236+
is($list{'8081'}, 1, 'weight 1');
237+
is($list{'8082'}, 1, 'weight 1');
238+
is($list{'8083'}, 1, 'weight 1');
239+
181240
###############################################################################
182241

183242
sub http_get_body {

0 commit comments

Comments
 (0)