@@ -154,6 +154,7 @@ failed to get ngx.shared dict: error_shm_name
154154 },
155155 user = 'root',
156156 password = 'abc123',
157+ init_count = -1,
157158 })
158159
159160 local res, err = etcd:set("/trigger_unhealthy", { a='abc'})
@@ -187,6 +188,7 @@ http://127.0.0.1:42379: connection refused
187188 "http://127.0.0.1:22379",
188189 "http://127.0.0.1:32379",
189190 },
191+ init_count = -1,
190192 })
191193
192194 local body_chunk_fun, err = etcd:watch("/trigger_unhealthy")
@@ -224,10 +226,16 @@ http://127.0.0.1:42379: connection refused
224226 },
225227 user = 'root',
226228 password = 'abc123',
229+ init_count = -1,
227230 })
228231
229- etcd:set("/fault_count", { a='abc'})
230- etcd:set("/fault_count", { a='abc'})
232+ -- make sure to select http://127.0.0.1:42379 twice
233+ for i = 1, 4 do
234+ etcd:set("/fault_count", { a='abc'})
235+ end
236+
237+ -- here have actually been 5 reads and writes to etcd, including one to /auth/authenticate
238+
231239 local fails, err = ngx.shared["etcd_cluster_health_check"]:get("http://127.0.0.1:42379")
232240 if err then
233241 ngx.say(err)
@@ -264,6 +272,7 @@ GET /t
264272 },
265273 user = 'root',
266274 password = 'abc123',
275+ init_count = -1,
267276 })
268277
269278 etcd:set("/get_target_status", { a='abc'})
@@ -301,19 +310,21 @@ false
301310 },
302311 user = 'root',
303312 password = 'abc123',
313+ init_count = -1,
304314 })
305315
306- local res, err = etcd:set("/fail_timeout", "http://127.0.0.1:42379") -- trigger http://127.0.0.1:42379 to unhealthy
316+ local res, err
307317
308- res, err = etcd:set("/fail_timeout", "http://127.0.0.1:22379") -- choose http://127.0.0.1:22379 to set value
309- res, err = etcd:get("/fail_timeout")
310- assert(res.body.kvs[1].value == "http://127.0.0.1:22379")
311-
312- ngx.sleep(2)
318+ -- make sure to select http://127.0.0.1:42379 once and trigger it to unhealthy
319+ for i = 1, 3 do
320+ res, err = etcd:set("/fail_timeout", "value")
321+ end
313322
314- res, err = etcd:set("/fail_timeout", "http://127.0.0.1:42379") -- choose http://127.0.0.1:42379 to set value
315- res, err = etcd:get("/fail_timeout")
316- assert(res.body.kvs[1].value == "http://127.0.0.1:22379")
323+ -- ensure that unhealthy http://127.0.0.1:42379 are no longer selected
324+ for i = 1, 3 do
325+ res, err = etcd:get("/fail_timeout")
326+ assert(res.body.kvs[1].value == "value")
327+ end
317328
318329 ngx.say("done")
319330 }
@@ -323,6 +334,8 @@ GET /t
323334--- timeout: 5
324335--- response_body
325336done
337+ --- error_log
338+ update endpoint: http://127.0.0.1:42379 to unhealthy
326339--- no_error_log
327340[error]
328341
@@ -387,6 +400,7 @@ has no healthy etcd endpoint available
387400 },
388401 user = 'root',
389402 password = 'abc123',
403+ init_count = -1,
390404 })
391405
392406 local etcd2, err = require "resty.etcd" .new({
@@ -398,6 +412,7 @@ has no healthy etcd endpoint available
398412 },
399413 user = 'root',
400414 password = 'abc123',
415+ init_count = -1,
401416 })
402417
403418 assert(tostring(etcd1) ~= tostring(etcd2))
@@ -493,12 +508,21 @@ qr/update endpoint: http:\/\/localhost:1984 to unhealthy/
493508 },
494509 user = 'root',
495510 password = 'abc123',
511+ init_count = -1,
496512 })
497513
498- local res, err = etcd:set("/trigger_unhealthy", "abc")
514+ local res, err
515+ for i = 1, 3 do
516+ res, err = etcd:set("/trigger_unhealthy", "abc")
517+ end
499518 check_res(res, err)
500519 local res, err = etcd:get("/trigger_unhealthy")
501520 check_res(res, err, "abc")
521+
522+ -- There are 5 times read and write operations to etcd have occurred here
523+ -- 3 set, 1 get, 1 auth
524+ -- actual 8 times choose endpoint, retry every time 42379 is selected
525+ -- 42379 marked as unhealthy after 3 seleced
502526 }
503527 }
504528--- request
@@ -531,13 +555,16 @@ checked val as expect: abc
531555 },
532556 user = 'root',
533557 password = 'abc123',
558+ init_count = -1,
534559 })
535560
536561 local body_chunk_fun, err = etcd:watch("/trigger_unhealthy", {timeout = 0.5})
537562 check_res(body_chunk_fun, err)
538563
539564 ngx.timer.at(0.1, function ()
540- etcd:set("/trigger_unhealthy", "abc")
565+ for i = 1, 3 do
566+ etcd:set("/trigger_unhealthy", "abc")
567+ end
541568 end)
542569
543570 local idx = 0
@@ -563,6 +590,8 @@ qr/update endpoint: http:\/\/127.0.0.1:42379 to unhealthy/
563590--- response_body_like eval
564591qr/1:.*"created":true.*
5655922:.*"value":"abc".*
593+ 3:.*"value":"abc".*
594+ 4:.*"value":"abc".*
566595timeout/
567596--- timeout: 5
568597
@@ -681,6 +710,7 @@ has no healthy etcd endpoint available
681710 "http://127.0.0.1:22379",
682711 "http://127.0.0.1:32379",
683712 },
713+ init_count = -1,
684714 })
685715
686716 local res, err = etcd:set("/test/etcd/healthy", "hello")
@@ -749,6 +779,7 @@ qr/update endpoint: http:\/\/127.0.0.1:12379 to unhealthy/
749779 "http://127.0.0.1:22379",
750780 "http://127.0.0.1:32379",
751781 },
782+ init_count = -1,
752783 })
753784
754785 local res
@@ -827,6 +858,7 @@ healthy check use ngx.shared dict
827858 "http://127.0.0.1:22379",
828859 "http://127.0.0.1:32379",
829860 },
861+ init_count = -1,
830862 })
831863
832864 local res
@@ -844,7 +876,7 @@ healthy check use ngx.shared dict
844876GET /t
845877--- response_body
846878http://127.0.0.1:42379: connection refused
847- http://127.0.0.1:42379: connection refused
879+ http://127.0.0.1:22379: OK
848880--- no_error_log eval
849881qr/update endpoint: http:\/\/127.0.0.1:42379 to unhealthy/
850882
@@ -889,3 +921,39 @@ GET /t
889921--- response_body
890922passed
891923passed
924+
925+
926+
927+ === TEST 22: ring balancer with specific init_count
928+ --- http_config eval: $::HttpConfig
929+ --- config
930+ location /t {
931+ content_by_lua_block {
932+ local health_check = require("resty.etcd.health_check")
933+ health_check.disable()
934+ local etcd, err = require "resty.etcd" .new({
935+ protocol = "v3",
936+ http_host = {
937+ "http://127.0.0.1:12379",
938+ "http://127.0.0.1:22379",
939+ "http://127.0.0.1:32379",
940+ },
941+ init_count = 101,
942+ })
943+
944+ local res
945+ for i = 1, 3 do
946+ res, _ = etcd:set("/ring_balancer", "abc")
947+ end
948+
949+ ngx.say(etcd.init_count)
950+ }
951+ }
952+ --- request
953+ GET /t
954+ --- response_body
955+ 104
956+ --- error_log
957+ choose_endpoint(): choose endpoint: http://127.0.0.1:12379
958+ choose_endpoint(): choose endpoint: http://127.0.0.1:22379
959+ choose_endpoint(): choose endpoint: http://127.0.0.1:32379
0 commit comments