Skip to content

Commit 7d61a02

Browse files
committed
Merge pull request #4 from hamishforbes/master
Only return healthy slaves
2 parents 5db29e0 + 485efe6 commit 7d61a02

File tree

3 files changed

+66
-16
lines changed

3 files changed

+66
-16
lines changed

Makefile

+7-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ REDIS_LOG = /redis.log
1414
REDIS_PREFIX = /tmp/redis-
1515

1616
# Overrideable redis test variables
17-
TEST_REDIS_PORTS ?= 6379 6380
17+
TEST_REDIS_PORTS ?= 6379 6380 6378
1818
TEST_REDIS_DATABASE ?= 1
1919

2020
REDIS_FIRST_PORT := $(firstword $(TEST_REDIS_PORTS))
@@ -65,7 +65,7 @@ INSTALL ?= install
6565
.PHONY: all install test test_all start_redis_instances stop_redis_instances \
6666
start_redis_instance stop_redis_instance cleanup_redis_instance flush_db \
6767
create_sentinel_config delete_sentinel_config check_ports test_redis \
68-
test_sentinel
68+
test_sentinel sleep
6969

7070
all: ;
7171

@@ -74,7 +74,10 @@ install: all
7474
$(INSTALL) lib/resty/redis/*.lua $(DESTDIR)/$(LUA_LIB_DIR)/resty/redis
7575

7676
test: test_redis
77-
test_all: start_redis_instances test_redis test_sentinel stop_redis_instances
77+
test_all: start_redis_instances sleep test_redis stop_redis_instances
78+
79+
sleep:
80+
sleep 3
7881

7982
start_redis_instances: check_ports create_sentinel_config
8083
@$(foreach port,$(TEST_REDIS_PORTS), \
@@ -91,6 +94,7 @@ start_redis_instances: check_ports create_sentinel_config
9194
prefix=$(REDIS_PREFIX)$(port) && \
9295
) true
9396

97+
9498
stop_redis_instances: delete_sentinel_config
9599
-@$(foreach port,$(TEST_REDIS_PORTS) $(TEST_SENTINEL_PORTS), \
96100
$(MAKE) stop_redis_instance cleanup_redis_instance port=$(port) \
@@ -140,12 +144,5 @@ test_redis: flush_db
140144
$(TEST_REDIS_VARS) $(PROVE) $(TEST_FILE)
141145
util/lua-releng
142146

143-
test_sentinel: flush_db
144-
$(TEST_SENTINEL_VARS) $(PROVE) $(SENTINEL_TEST_FILE)/01-master_up.t
145-
$(REDIS_CLI) shutdown
146-
$(TEST_SENTINEL_VARS) $(PROVE) $(SENTINEL_TEST_FILE)/02-master_down.t
147-
sleep $(TEST_SENTINEL_PROMOTION_TIME)
148-
$(TEST_SENTINEL_VARS) $(PROVE) $(SENTINEL_TEST_FILE)/03-slave_promoted.t
149-
150147
test_leak: flush_db
151148
$(TEST_REDIS_VARS) TEST_NGINX_CHECK_LEAK=1 $(PROVE) $(TEST_FILE)

lib/resty/redis/sentinel.lua

+5-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ function _M.get_slaves(sentinel, master_name)
3636
for i = 1, num_recs, 2 do
3737
host[slave[i]] = slave[i + 1]
3838
end
39-
host.host = host.ip -- for parity with other functions
40-
tbl_insert(hosts, host)
39+
40+
if host["master-link-status"] == "ok" then
41+
host.host = host.ip -- for parity with other functions
42+
tbl_insert(hosts, host)
43+
end
4144
end
4245
if hosts[1] ~= nil then
4346
return hosts

t/sentinel.t

+54-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Test::Nginx::Socket::Lua;
44
use Cwd qw(cwd);
55

6-
repeat_each(2);
6+
#repeat_each(2);
77

88
plan tests => repeat_each() * (3 * blocks());
99

@@ -38,7 +38,7 @@ __DATA__
3838
end
3939
4040
local redis_sentinel = require "resty.redis.sentinel"
41-
41+
4242
local master, err = redis_sentinel.get_master(sentinel, "mymaster")
4343
if not master then
4444
ngx.say(err)
@@ -74,7 +74,57 @@ port: 6379
7474
end
7575
7676
local redis_sentinel = require "resty.redis.sentinel"
77-
77+
78+
local slaves, err = redis_sentinel.get_slaves(sentinel, "mymaster")
79+
if not slaves then
80+
ngx.say(err)
81+
else
82+
-- order is undefined
83+
local all = {}
84+
for i,slave in ipairs(slaves) do
85+
all[i] = tonumber(slave.port)
86+
end
87+
table.sort(all)
88+
for _,p in ipairs(all) do
89+
ngx.say(p)
90+
end
91+
end
92+
93+
sentinel:close()
94+
';
95+
}
96+
--- request
97+
GET /t
98+
--- response_body
99+
6378
100+
6380
101+
--- no_error_log
102+
[error]
103+
104+
=== TEST 3: Get only healthy slaves
105+
--- http_config eval: $::HttpConfig
106+
--- config
107+
location /t {
108+
content_by_lua '
109+
110+
local redis = require "resty.redis"
111+
local r = redis.new()
112+
r:connect("127.0.0.1", 6378)
113+
r:slaveof("127.0.0.1", 7000)
114+
115+
ngx.sleep(9)
116+
117+
local redis_connector = require "resty.redis.connector"
118+
local rc = redis_connector.new()
119+
120+
local sentinel, err = rc:connect{ url = "redis://127.0.0.1:6381" }
121+
if not sentinel then
122+
ngx.say("failed to connect: ", err)
123+
return
124+
end
125+
126+
local redis_sentinel = require "resty.redis.sentinel"
127+
78128
local slaves, err = redis_sentinel.get_slaves(sentinel, "mymaster")
79129
if not slaves then
80130
ngx.say(err)
@@ -90,9 +140,9 @@ port: 6379
90140
}
91141
--- request
92142
GET /t
143+
--- timeout: 10
93144
--- response_body
94145
host: 127.0.0.1
95146
port: 6380
96147
--- no_error_log
97148
[error]
98-

0 commit comments

Comments
 (0)