@@ -154,6 +154,7 @@ failed to get ngx.shared dict: error_shm_name
154
154
},
155
155
user = 'root',
156
156
password = 'abc123',
157
+ init_count = -1,
157
158
})
158
159
159
160
local res, err = etcd:set("/trigger_unhealthy", { a='abc'})
@@ -187,6 +188,7 @@ http://127.0.0.1:42379: connection refused
187
188
"http://127.0.0.1:22379",
188
189
"http://127.0.0.1:32379",
189
190
},
191
+ init_count = -1,
190
192
})
191
193
192
194
local body_chunk_fun, err = etcd:watch("/trigger_unhealthy")
@@ -224,10 +226,16 @@ http://127.0.0.1:42379: connection refused
224
226
},
225
227
user = 'root',
226
228
password = 'abc123',
229
+ init_count = -1,
227
230
})
228
231
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
+
231
239
local fails, err = ngx.shared["etcd_cluster_health_check"]:get("http://127.0.0.1:42379")
232
240
if err then
233
241
ngx.say(err)
@@ -264,6 +272,7 @@ GET /t
264
272
},
265
273
user = 'root',
266
274
password = 'abc123',
275
+ init_count = -1,
267
276
})
268
277
269
278
etcd:set("/get_target_status", { a='abc'})
@@ -301,19 +310,21 @@ false
301
310
},
302
311
user = 'root',
303
312
password = 'abc123',
313
+ init_count = -1,
304
314
})
305
315
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
307
317
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
313
322
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
317
328
318
329
ngx.say("done")
319
330
}
@@ -323,6 +334,8 @@ GET /t
323
334
--- timeout: 5
324
335
--- response_body
325
336
done
337
+ --- error_log
338
+ update endpoint: http://127.0.0.1:42379 to unhealthy
326
339
--- no_error_log
327
340
[error]
328
341
@@ -387,6 +400,7 @@ has no healthy etcd endpoint available
387
400
},
388
401
user = 'root',
389
402
password = 'abc123',
403
+ init_count = -1,
390
404
})
391
405
392
406
local etcd2, err = require "resty.etcd" .new({
@@ -398,6 +412,7 @@ has no healthy etcd endpoint available
398
412
},
399
413
user = 'root',
400
414
password = 'abc123',
415
+ init_count = -1,
401
416
})
402
417
403
418
assert(tostring(etcd1) ~= tostring(etcd2))
@@ -493,12 +508,21 @@ qr/update endpoint: http:\/\/localhost:1984 to unhealthy/
493
508
},
494
509
user = 'root',
495
510
password = 'abc123',
511
+ init_count = -1,
496
512
})
497
513
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
499
518
check_res(res, err)
500
519
local res, err = etcd:get("/trigger_unhealthy")
501
520
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
502
526
}
503
527
}
504
528
--- request
@@ -531,13 +555,16 @@ checked val as expect: abc
531
555
},
532
556
user = 'root',
533
557
password = 'abc123',
558
+ init_count = -1,
534
559
})
535
560
536
561
local body_chunk_fun, err = etcd:watch("/trigger_unhealthy", {timeout = 0.5})
537
562
check_res(body_chunk_fun, err)
538
563
539
564
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
541
568
end)
542
569
543
570
local idx = 0
@@ -563,6 +590,8 @@ qr/update endpoint: http:\/\/127.0.0.1:42379 to unhealthy/
563
590
--- response_body_like eval
564
591
qr/1:.*"created":true.*
565
592
2:.*"value":"abc".*
593
+ 3:.*"value":"abc".*
594
+ 4:.*"value":"abc".*
566
595
timeout/
567
596
--- timeout: 5
568
597
@@ -681,6 +710,7 @@ has no healthy etcd endpoint available
681
710
"http://127.0.0.1:22379",
682
711
"http://127.0.0.1:32379",
683
712
},
713
+ init_count = -1,
684
714
})
685
715
686
716
local res, err = etcd:set("/test/etcd/healthy", "hello")
@@ -749,6 +779,7 @@ qr/update endpoint: http:\/\/127.0.0.1:12379 to unhealthy/
749
779
"http://127.0.0.1:22379",
750
780
"http://127.0.0.1:32379",
751
781
},
782
+ init_count = -1,
752
783
})
753
784
754
785
local res
@@ -827,6 +858,7 @@ healthy check use ngx.shared dict
827
858
"http://127.0.0.1:22379",
828
859
"http://127.0.0.1:32379",
829
860
},
861
+ init_count = -1,
830
862
})
831
863
832
864
local res
@@ -844,7 +876,7 @@ healthy check use ngx.shared dict
844
876
GET /t
845
877
--- response_body
846
878
http://127.0.0.1:42379: connection refused
847
- http://127.0.0.1:42379: connection refused
879
+ http://127.0.0.1:22379: OK
848
880
--- no_error_log eval
849
881
qr/update endpoint: http:\/\/127.0.0.1:42379 to unhealthy/
850
882
@@ -889,3 +921,39 @@ GET /t
889
921
--- response_body
890
922
passed
891
923
passed
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