spacewander commented on a change in pull request #4092:
URL: https://github.com/apache/apisix/pull/4092#discussion_r617334177



##########
File path: docs/en/latest/plugins/traffic-split.md
##########
@@ -482,6 +483,98 @@ Content-Type: text/html; charset=utf-8
 hello 1980
 ```
 
+### Matching rules correspond to upstream
+
+How to make different matching rules correspond to different upstreams. We can 
achieve this by configuring multiple `rules.match` + `rules.weighted_upstreams`.

Review comment:
       We can directly say "configuring multiple `rules`"?
   
   > How to make different matching rules correspond to different upstreams.
   
   This sentence is confusing. It looks like a question at first glance but it 
isn't...

##########
File path: docs/en/latest/plugins/traffic-split.md
##########
@@ -482,6 +483,98 @@ Content-Type: text/html; charset=utf-8
 hello 1980
 ```
 
+### Matching rules correspond to upstream
+
+How to make different matching rules correspond to different upstreams. We can 
achieve this by configuring multiple `rules.match` + `rules.weighted_upstreams`.
+
+**Example:**
+
+When the request header `x-api-id` is equal to 1, it hits the upstream of port 
1981; when `x-api-id` is equal to 2, it hits the upstream of port 1982; 
otherwise, it hits the upstream of port 1980 (the upstream response data is the 
corresponding port number).

Review comment:
       "the upstream of port" => "the upstream with port"
   
   Please fix other similar places.

##########
File path: t/plugin/traffic-split2.t
##########
@@ -408,3 +408,340 @@ hash_on: header
 chash_key: "world"
 hash_on: header
 chash_key: "hello"
+
+
+
+=== TEST 12: the plugin has multiple weighted_upstreams(upstream method)
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PATCH,
+                [=[{
+                    "uri": "/server_port",
+                    "plugins": {
+                        "traffic-split": {
+                            "rules": [
+                                {
+                                    "match": [
+                                        {
+                                            "vars": [["arg_id","==","1"]]
+                                        }
+                                    ],
+                                    "weighted_upstreams": [
+                                        {
+                                            "upstream": {
+                                                "name": "upstream_A",
+                                                "type": "roundrobin",
+                                                "nodes": {
+                                                    "127.0.0.1:1981":1
+                                                }
+                                            },
+                                            "weight": 1
+                                        }
+                                    ]
+                                },
+                                {
+                                    "match": [
+                                        {
+                                            "vars": [["arg_id","==","2"]]
+                                        }
+                                    ],
+                                    "weighted_upstreams": [
+                                        {
+                                            "upstream": {
+                                                "name": "upstream_B",
+                                                "type": "roundrobin",
+                                                "nodes": {
+                                                    "127.0.0.1:1982":1
+                                                }
+                                            },
+                                            "weight": 1
+                                        }
+                                    ]
+                                }
+                            ]
+                        }
+                    },
+                    "upstream": {
+                            "type": "roundrobin",
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            }
+                    }
+                }]=]
+            )
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 13: hit each upstream separately
+--- config
+location /t {
+    content_by_lua_block {
+        local t = require("lib.test_admin").test
+        local bodys = {}
+        for i = 1, 9, 3 do
+            local _, _, body = t('/server_port', ngx.HTTP_GET)
+            local _, _, body2 = t('/server_port?id=1', ngx.HTTP_GET)
+            local _, _, body3 = t('/server_port?id=2', ngx.HTTP_GET)
+            bodys[i] = body
+            bodys[i+1] = body2
+            bodys[i+2] = body3
+        end
+
+        ngx.say(table.concat(bodys, ", "))
+    }
+}
+--- response_body eval
+qr/1980, 1981, 1982, 1980, 1981, 1982, 1980, 1981, 1982/
+--- no_error_log
+[error]
+
+
+
+=== TEST 14: the plugin has multiple weighted_upstreams and has a default 
routing weight in weighted_upstreams
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PATCH,
+                [=[{
+                    "uri": "/server_port",
+                    "plugins": {
+                        "traffic-split": {
+                            "rules": [
+                                {
+                                    "match": [
+                                        {
+                                            "vars": [["arg_id","==","1"]]
+                                        }
+                                    ],
+                                    "weighted_upstreams": [
+                                        {
+                                            "upstream": {
+                                                "name": "upstream_A",
+                                                "type": "roundrobin",
+                                                "nodes": {
+                                                    "127.0.0.1:1981":1
+                                                }
+                                            },
+                                            "weight": 1
+                                        },
+                                        {
+                                            "weight": 1
+                                        }
+                                    ]
+                                },
+                                {
+                                    "match": [
+                                        {
+                                            "vars": [["arg_id","==","2"]]
+                                        }
+                                    ],
+                                    "weighted_upstreams": [
+                                        {
+                                            "upstream": {
+                                                "name": "upstream_B",
+                                                "type": "roundrobin",
+                                                "nodes": {
+                                                    "127.0.0.1:1982":1
+                                                }
+                                            },
+                                            "weight": 1
+                                        },
+                                        {
+                                            "weight": 1
+                                        }
+                                    ]
+                                }
+                            ]
+                        }
+                    },
+                    "upstream": {
+                            "type": "roundrobin",
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            }
+                    }
+                }]=]
+            )
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 15: every weighted_upstreams in the plugin is hit
+--- config
+location /t {
+    content_by_lua_block {
+        local t = require("lib.test_admin").test
+        local bodys = {}
+        for i = 1, 8, 2 do
+            local _, _, body = t('/server_port?id=1', ngx.HTTP_GET)
+            local _, _, body2 = t('/server_port?id=2', ngx.HTTP_GET)
+            bodys[i] = body
+            bodys[i+1] = body2
+        end
+
+        table.sort(bodys)
+        ngx.say(table.concat(bodys, ", "))
+    }
+}
+--- response_body eval
+qr/1980, 1980, 1980, 1980, 1981, 1981, 1982, 1982/
+--- no_error_log
+[error]
+
+
+
+=== TEST 16: set upstream(upstream_id: 1, upstream_id: 2)
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/upstreams/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                    "nodes": {
+                        "127.0.0.1:1981": 1
+                    },
+                    "type": "roundrobin",
+                    "desc": "new upstream A"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+                ngx.say(body)
+            end
+
+            code, body = t('/apisix/admin/upstreams/2',
+                 ngx.HTTP_PUT,
+                 [[{
+                    "nodes": {
+                        "127.0.0.1:1982": 1
+                    },
+                    "type": "roundrobin",
+                    "desc": "new upstream B"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 17: the plugin has multiple weighted_upstreams(upstream_id method)

Review comment:
       We can merge TEST 17 into TEST 16




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to