Skip to content

Commit 7f5638b

Browse files
fix(limit-count): upgrade redis-cluster lib so NOSCRIPT is not treated as node failure (#13579)
1 parent cdcaea1 commit 7f5638b

4 files changed

Lines changed: 87 additions & 1 deletion

File tree

apisix-master-0.rockspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ dependencies = {
6363
"base64 = 1.5-3",
6464
"binaryheap = 0.4-1",
6565
"api7-dkjson = 0.1.1-0",
66-
"resty-redis-cluster = 1.05-1",
66+
"lua-resty-redis-cluster = 1.3.3-0",
6767
"lua-resty-expr = 1.3.2",
6868
"graphql = 0.0.2-1",
6969
"argparse = 0.7.1-1",

apisix/cli/ngx_tpl.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,11 @@ http {
331331
lua_shared_dict plugin-limit-count-reset-header {* http.lua_shared_dict["plugin-limit-count"] *};
332332
{% end %}
333333
334+
{% if enabled_plugins["limit-conn"] or enabled_plugins["limit-req"] or enabled_plugins["limit-count"] then %}
335+
# tracks unhealthy redis cluster nodes for fast-fail
336+
lua_shared_dict redis_cluster_health 10m;
337+
{% end %}
338+
334339
{% if enabled_plugins["graphql-limit-count"] then %}
335340
lua_shared_dict plugin-graphql-limit-count {* http.lua_shared_dict["plugin-graphql-limit-count"] *};
336341
lua_shared_dict plugin-graphql-limit-count-reset-header {* http.lua_shared_dict["plugin-graphql-limit-count-reset-header"] *};

apisix/init.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ function _M.http_init_worker()
168168
plugin.init_prometheus()
169169

170170
trusted_addresses_util.init_worker()
171+
172+
local process = require("ngx.process")
173+
if process.type() == "privileged agent" then
174+
-- start the redis cluster node health checker timer
175+
require("resty.rediscluster").init()
176+
end
171177
end
172178

173179

t/plugin/limit-count-redis-cluster.t

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,3 +546,78 @@ passed
546546
["GET /hello", "GET /hello", "GET /hello", "GET /hello"]
547547
--- error_code eval
548548
[200, 200, 503, 503]
549+
550+
551+
552+
=== TEST 17: set route and flush the cluster script cache
553+
--- config
554+
location /t {
555+
content_by_lua_block {
556+
local t = require("lib.test_admin").test
557+
local code, body = t('/apisix/admin/routes/1',
558+
ngx.HTTP_PUT,
559+
[[{
560+
"uri": "/hello",
561+
"plugins": {
562+
"limit-count": {
563+
"count": 9999,
564+
"time_window": 60,
565+
"key": "remote_addr",
566+
"policy": "redis-cluster",
567+
"redis_cluster_nodes": [
568+
"127.0.0.1:5000",
569+
"127.0.0.1:5001"
570+
],
571+
"redis_cluster_name": "redis-cluster-1"
572+
}
573+
},
574+
"upstream": {
575+
"nodes": {
576+
"127.0.0.1:1980": 1
577+
},
578+
"type": "roundrobin"
579+
}
580+
}]]
581+
)
582+
if code >= 300 then
583+
ngx.status = code
584+
ngx.say(body)
585+
return
586+
end
587+
588+
-- drop any cached script on every node so the next evalsha returns NOSCRIPT
589+
local redis = require("resty.redis")
590+
local seed = redis:new()
591+
seed:set_timeout(1000)
592+
local ok, err = seed:connect("127.0.0.1", 5000)
593+
if not ok then
594+
ngx.say("failed to connect seed: ", err)
595+
return
596+
end
597+
local nodes = seed:cluster("nodes")
598+
seed:set_keepalive(10000, 100)
599+
for addr in nodes:gmatch("(%d+%.%d+%.%d+%.%d+:%d+)@") do
600+
local ip, port = addr:match("([^:]+):(%d+)")
601+
local red = redis:new()
602+
red:set_timeout(1000)
603+
if red:connect(ip, tonumber(port)) then
604+
red:script("flush")
605+
red:set_keepalive(10000, 100)
606+
end
607+
end
608+
ngx.say("done")
609+
}
610+
}
611+
--- response_body
612+
done
613+
614+
615+
616+
=== TEST 18: cluster path falls back to eval on NOSCRIPT
617+
--- request
618+
GET /hello
619+
--- error_code: 200
620+
--- grep_error_log eval
621+
qr/redis evalsha failed:.*Falling back to eval/
622+
--- grep_error_log_out
623+
redis evalsha failed: NOSCRIPT No matching script. Please use EVAL.. Falling back to eval

0 commit comments

Comments
 (0)