Skip to content

Commit 8a7529d

Browse files
authored
iwrr: a wrr with always O(1) time and O(n) memory (#1729)
1 parent cd2b982 commit 8a7529d

File tree

7 files changed

+838
-0
lines changed

7 files changed

+838
-0
lines changed

.github/workflows/ci-arm64.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ jobs:
7777
--add-module=./modules/ngx_http_upstream_consistent_hash_module \
7878
--add-module=./modules/ngx_http_upstream_dynamic_module \
7979
--add-module=./modules/ngx_http_upstream_dyups_module \
80+
--add-module=./modules/ngx_http_upstream_iwrr_module \
8081
--add-module=./modules/ngx_http_upstream_keepalive_module \
8182
--add-module=./modules/ngx_http_upstream_session_sticky_module \
8283
--add-module=./modules/ngx_http_upstream_vnswrr_module \

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ jobs:
9191
--add-module=./modules/ngx_http_upstream_consistent_hash_module \
9292
--add-module=./modules/ngx_http_upstream_dynamic_module \
9393
--add-module=./modules/ngx_http_upstream_dyups_module \
94+
--add-module=./modules/ngx_http_upstream_iwrr_module \
9495
--add-module=./modules/ngx_http_upstream_keepalive_module \
9596
--add-module=./modules/ngx_http_upstream_session_sticky_module \
9697
--add-module=./modules/ngx_http_upstream_vnswrr_module \
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
## Name
3+
4+
ngx_http_upstream_iwrr_module.
5+
6+
7+
## Introduction
8+
9+
The `IWRR` module is an efficient load balancing algorithm with `O(1)` time complexity, but `IWRR` is no need to incremental initialization.
10+
11+
Compared with Nginx's official `SWRR` algorithm and `VNSWRR`, `IWRR` abandons smoothness on the premise of ensuring the correctness of the weighted load balancing algorithm, ensuring that no matter how the total weight of the cluster changes, `IWRR` space The complexity is always `O(n)`.
12+
13+
## Example
14+
15+
```
16+
http {
17+
18+
upstream backend {
19+
iwrr; # enable IWRR load balancing algorithm.
20+
127.0.0.1 port=81;
21+
127.0.0.1 port=82 weight=2;
22+
127.0.0.1 port=83;
23+
127.0.0.1 port=84 backup;
24+
127.0.0.1 port=85 down;
25+
}
26+
27+
server {
28+
server_name localhost;
29+
30+
location / {
31+
proxy_pass http://backend;
32+
}
33+
}
34+
}
35+
36+
```
37+
38+
## Installation
39+
40+
Build Tengine with this module from source:
41+
42+
```
43+
44+
./configure --add-module=./modules/ngx_http_upstream_iwrr_module/
45+
make
46+
make install
47+
48+
```
49+
50+
51+
## Directive
52+
53+
iwrr
54+
=======
55+
```
56+
Syntax: iwrr [max_init=number]
57+
Default: none
58+
Context: upstream
59+
```
60+
61+
Enable `iwrr` load balancing algorithm.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
## 名称
3+
4+
ngx_http_upstream_iwrr_module.
5+
6+
7+
## 介绍
8+
9+
`IWRR`模块是一个高效的负载均衡算法,与`VNSWRR`相同,它具有`O(1)`的时间复杂度,但是`IWRR`不需要执行渐进式初始化操作。
10+
11+
同Nginx官方的加权轮询负载均衡算法及`VNSWRR`相比,`IWRR`在保证加权负载均衡算法正确性的前提下,牺牲了平滑的特点,保证无论集群总权重如何变化,`IWRR`空间复杂度总是`O(n)`的。
12+
13+
## 配置列子
14+
15+
```
16+
http {
17+
18+
upstream backend {
19+
iwrr; # enable IWRR load balancing algorithm.
20+
127.0.0.1 port=81;
21+
127.0.0.1 port=82 weight=2;
22+
127.0.0.1 port=83;
23+
127.0.0.1 port=84 backup;
24+
127.0.0.1 port=85 down;
25+
}
26+
27+
server {
28+
server_name localhost;
29+
30+
location / {
31+
proxy_pass http://backend;
32+
}
33+
}
34+
}
35+
36+
```
37+
38+
## 安装方法
39+
40+
在Tengine中,通过源码安装此模块:
41+
42+
43+
```
44+
45+
./configure --add-module=./modules/ngx_http_upstream_iwrr_module
46+
make
47+
make install
48+
49+
```
50+
51+
52+
## 指令描述
53+
54+
iwrr
55+
=======
56+
```
57+
Syntax: iwrr
58+
Default: none
59+
Context: upstream
60+
```
61+
62+
在upstream里面启用 `iwrr` 加权轮询算法。
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
ngx_addon_name=ngx_http_upstream_iwrr_module
2+
HTTP_UPSTREAM_IWRR_SRCS="$ngx_addon_dir/ngx_http_upstream_iwrr_module.c"
3+
4+
if test -n "$ngx_module_link"; then
5+
ngx_module_type=HTTP
6+
ngx_module_name=$ngx_addon_name
7+
ngx_module_deps=
8+
ngx_module_srcs="$HTTP_UPSTREAM_IWRR_SRCS"
9+
10+
. auto/module
11+
else
12+
HTTP_MODULES="$HTTP_MODULES ngx_http_upstream_iwrr_module"
13+
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $HTTP_UPSTREAM_IWRR_SRCS"
14+
fi

0 commit comments

Comments
 (0)