You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/05/06 23:42:14 UTC

[GitHub] [apisix] Yiyiyimu opened a new pull request #4191: fix: enable etcd health-check, so one etcd node failure would not make apisix fail

Yiyiyimu opened a new pull request #4191:
URL: https://github.com/apache/apisix/pull/4191


   ### What this PR does / why we need it:
   fix #3673 #3937.
   
   Import health check of lua-resty-etcd, so 
   1. when one etcd node disconnected, apisix would try to connect another one and perform as normal, while health-check would continue print warning log about disconnected etcd node
   2. when all etcd nodes disconnected, apisix would try to re-connect with etcd, with a cyclic backoff interval for 1s, 2s, 4s, ..., 512s, 1s, 2s ... so the error log would not been flushed when etcd are all down.
   
   TODO:
   - [ ] for etcd node deployed in kubernetes, since nodes are behind the same domain, we could not know if one of them is taint, as talked in https://github.com/apache/apisix/issues/3692
   - [ ] add test, but since the current health check does not work for kubernetes, need to implement a new CI pipeline for multiple etcd nodes
   
   ### Pre-submission checklist:
   
   * [x] Did you explain what problem does this PR solve? Or what new features have been added?
   * [ ] Have you added corresponding test cases?
   * [ ] Have you modified the corresponding document?
   * [ ] Is this PR backward compatible? **If it is not backward compatible, please discuss on the [mailing list](https://github.com/apache/apisix/tree/master#community) first**
   


-- 
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:
users@infra.apache.org



[GitHub] [apisix] Yiyiyimu commented on a change in pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r645176342



##########
File path: apisix/core/config_etcd.lua
##########
@@ -529,6 +531,17 @@ local function _automatic_fetch(premature, self)
         return
     end
 
+    if not health_check.conf then

Review comment:
       In lua-resty-etcd
   https://github.com/api7/lua-resty-etcd/blob/1fe01f77dda252f2d95eed0a7635f3479097b8ed/lib/resty/etcd/health_check.lua#L70-L83

##########
File path: apisix/core/config_etcd.lua
##########
@@ -529,6 +531,17 @@ local function _automatic_fetch(premature, self)
         return
     end
 
+    if not health_check.conf then

Review comment:
       In lua-resty-etcd
   https://github.com/api7/lua-resty-etcd/blob/1fe01f77dda252f2d95eed0a7635f3479097b8ed/lib/resty/etcd/health_check.lua#L70-L83
   
   And I think I didn't configure it correctly but failed to find the reason. The health check would renew its value in shared dict for every loop in `automatic_fetch`, so etcd health check would keep refreshing the tainted node status but not directly jump over it. Removing the error check imported in this PR still got the same problem.

##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,6 +558,33 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
+                if string.find(err, "connection refused")
+                    or string.find(err, "Service Unavailable") then
+                    local etcd_cli, err = get_etcd()

Review comment:
       Got it. Working on it

##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,6 +558,33 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
+                if string.find(err, "connection refused")
+                    or string.find(err, "Service Unavailable") then
+                    local etcd_cli, err = get_etcd()
+                    if not etcd_cli then
+                        error("all etcd endpoints are unhealthy: " .. err)
+                    end
+                    self.etcd_cli = etcd_cli
+                    log.warn("changed to new etcd endpoint")
+                end
+                if string.find(err, "has no healthy etcd endpoint available") then

Review comment:
       Fixed thanks!

##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,6 +558,33 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
+                if string.find(err, "connection refused")
+                    or string.find(err, "Service Unavailable") then
+                    local etcd_cli, err = get_etcd()

Review comment:
       Changed to forcibly get new etcd client

##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,7 +558,34 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
-                if err ~= "timeout" and err ~= "Key not found"
+                if string.find(err, "connection refused")
+                    or string.find(err, "Service Unavailable") then
+                    log.warn(err, ", ", tostring(self))

Review comment:
       Mainly to fulfill the needs of config-etcd test, and I think it's also good to inform users that etcd can not be connected
   https://github.com/apache/apisix/blob/86e168e711713531eeafdd0a75682b150e0d0109/t/core/config_etcd.t#L28-L49




-- 
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:
users@infra.apache.org



[GitHub] [apisix] Yiyiyimu commented on a change in pull request #4191: feat: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r660287165



##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,7 +564,24 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
-                if err ~= "timeout" and err ~= "Key not found"
+                if string.find(err, err_etcd_unhealthy_all) then
+                    local reconnected = false
+                    while err and not reconnected do
+                        local backoff_duration, backoff_factor, backoff_step = 1, 2, 10

Review comment:
       I'm not so familiar with the production environment. Do you have some recommendations, like 8?




-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

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



[GitHub] [apisix] Yiyiyimu edited a comment on pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu edited a comment on pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#issuecomment-837350315


   Things get a bit weird right now 😕 
   
   Before I work on this fix, I test that if killing one node out of an etcd cluster, apisix would fail, as those related issues talk about. However, right now when I test this scenario again on the master branch, apisix would **not** be affected by the closed etcd node and runs normally. I checked the recent commits and it seems no changes have applied to this problem.
   
   Could someone help me to reproduce the program again
   
   ---
   
   Test running apisix and etcd in Kubernetes, delete one etcd node, and apisix runs normally with master branch. So don't know if anything went wrong yet.


-- 
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:
users@infra.apache.org



[GitHub] [apisix] Yiyiyimu edited a comment on pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu edited a comment on pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#issuecomment-865406935


   Waiting for https://github.com/api7/lua-resty-etcd/pull/131
   
   ---
   
   Done


-- 
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:
users@infra.apache.org



[GitHub] [apisix] Yiyiyimu commented on a change in pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r655764147



##########
File path: apisix/core/config_etcd.lua
##########
@@ -529,6 +531,17 @@ local function _automatic_fetch(premature, self)
         return
     end
 
+    if not health_check.conf then

Review comment:
       Ah it is. Thank you so much!




-- 
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:
users@infra.apache.org



[GitHub] [apisix] Yiyiyimu commented on a change in pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r645176342



##########
File path: apisix/core/config_etcd.lua
##########
@@ -529,6 +531,17 @@ local function _automatic_fetch(premature, self)
         return
     end
 
+    if not health_check.conf then

Review comment:
       In lua-resty-etcd
   https://github.com/api7/lua-resty-etcd/blob/1fe01f77dda252f2d95eed0a7635f3479097b8ed/lib/resty/etcd/health_check.lua#L70-L83
   
   And I think I didn't configure it correctly but failed to find the reason. The health check would renew its value in shared dict for every loop in `automatic_fetch`, so etcd health check would keep refreshing the tainted node status but not directly jump over it. Removing the error check imported in this PR still got the same problem.




-- 
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:
users@infra.apache.org



[GitHub] [apisix] Yiyiyimu commented on pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#issuecomment-865406935


   Waiting for https://github.com/api7/lua-resty-etcd/pull/131


-- 
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:
users@infra.apache.org



[GitHub] [apisix] Yiyiyimu commented on a change in pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r660154147



##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,7 +564,24 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
-                if err ~= "timeout" and err ~= "Key not found"
+                if string.find(err, err_etcd_unhealthy_all) then
+                    local reconnected = false
+                    while err and not reconnected do
+                        local backoff_duration, backoff_factor, backoff_step = 1, 2, 10
+                        for _ = 0, backoff_step, 1 do

Review comment:
       Thanks for remind! Fixed

##########
File path: apisix/core/config_etcd.lua
##########
@@ -568,7 +568,7 @@ local function _automatic_fetch(premature, self)
                     local reconnected = false
                     while err and not reconnected do
                         local backoff_duration, backoff_factor, backoff_step = 1, 2, 10
-                        for _ = 0, backoff_step, 1 do
+                        for _ = 1, backoff_step, 1 do

Review comment:
       Thanks for reminding! Fixed

##########
File path: apisix/core/config_etcd.lua
##########
@@ -568,7 +568,7 @@ local function _automatic_fetch(premature, self)
                     local reconnected = false
                     while err and not reconnected do
                         local backoff_duration, backoff_factor, backoff_step = 1, 2, 10
-                        for _ = 0, backoff_step, 1 do
+                        for _ = 1, backoff_step, 1 do

Review comment:
       @tokers PTAL

##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,7 +564,24 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
-                if err ~= "timeout" and err ~= "Key not found"
+                if string.find(err, err_etcd_unhealthy_all) then
+                    local reconnected = false
+                    while err and not reconnected do
+                        local backoff_duration, backoff_factor, backoff_step = 1, 2, 10

Review comment:
       I'm not so familiar with the production environment. Do you have some recommendations, like 8?




-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

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



[GitHub] [apisix] Yiyiyimu commented on a change in pull request #4191: feat: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r660287165



##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,7 +564,24 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
-                if err ~= "timeout" and err ~= "Key not found"
+                if string.find(err, err_etcd_unhealthy_all) then
+                    local reconnected = false
+                    while err and not reconnected do
+                        local backoff_duration, backoff_factor, backoff_step = 1, 2, 10

Review comment:
       I'm not so familiar with the production environment. Do you have some recommendations, like 8, around 4 mins at most?




-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

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



[GitHub] [apisix] juzhiyuan commented on pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
juzhiyuan commented on pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#issuecomment-836072466


   ![image](https://user-images.githubusercontent.com/2106987/117597733-c5f08c00-b178-11eb-83f9-fef08f6c5209.png)
   
   We need another `fix` hh


-- 
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:
users@infra.apache.org



[GitHub] [apisix] Yiyiyimu commented on a change in pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r655764147



##########
File path: apisix/core/config_etcd.lua
##########
@@ -529,6 +531,17 @@ local function _automatic_fetch(premature, self)
         return
     end
 
+    if not health_check.conf then

Review comment:
       Ah it is. Thank you so much!

##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,6 +558,33 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
+                if string.find(err, "connection refused")
+                    or string.find(err, "Service Unavailable") then
+                    local etcd_cli, err = get_etcd()

Review comment:
       Removed since moved one-node-down retry to `lua-resty-etcd`




-- 
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:
users@infra.apache.org



[GitHub] [apisix] spacewander merged pull request #4191: feat: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
spacewander merged pull request #4191:
URL: https://github.com/apache/apisix/pull/4191


   


-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

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



[GitHub] [apisix] Yiyiyimu commented on pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#issuecomment-865406935


   Waiting for https://github.com/api7/lua-resty-etcd/pull/131


-- 
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:
users@infra.apache.org



[GitHub] [apisix] Yiyiyimu commented on a change in pull request #4191: feat: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r660231882



##########
File path: apisix/core/config_etcd.lua
##########
@@ -568,7 +568,7 @@ local function _automatic_fetch(premature, self)
                     local reconnected = false
                     while err and not reconnected do
                         local backoff_duration, backoff_factor, backoff_step = 1, 2, 10
-                        for _ = 0, backoff_step, 1 do
+                        for _ = 1, backoff_step, 1 do

Review comment:
       Thanks for reminding! Fixed




-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

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



[GitHub] [apisix] tokers commented on a change in pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
tokers commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r645242854



##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,7 +558,34 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
-                if err ~= "timeout" and err ~= "Key not found"
+                if string.find(err, "connection refused")
+                    or string.find(err, "Service Unavailable") then
+                    log.warn(err, ", ", tostring(self))

Review comment:
       Is this for debugging purpose?




-- 
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:
users@infra.apache.org



[GitHub] [apisix] Yiyiyimu commented on a change in pull request #4191: feat: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r661107427



##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,7 +564,24 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
-                if err ~= "timeout" and err ~= "Key not found"
+                if string.find(err, err_etcd_unhealthy_all) then
+                    local reconnected = false
+                    while err and not reconnected do
+                        local backoff_duration, backoff_factor, backoff_step = 1, 2, 10

Review comment:
       Thanks for suggestion!
   Changed to use outside counter till `32` to avoid keeping nginx timer too long. PTAL @tokers 




-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

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



[GitHub] [apisix] Yiyiyimu commented on pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#issuecomment-837379107


   Waiting for lua-resty-etcd new release: https://github.com/api7/lua-resty-etcd/pull/129


-- 
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:
users@infra.apache.org



[GitHub] [apisix] Yiyiyimu commented on a change in pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r658647753



##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,7 +558,34 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
-                if err ~= "timeout" and err ~= "Key not found"
+                if string.find(err, "connection refused")
+                    or string.find(err, "Service Unavailable") then
+                    log.warn(err, ", ", tostring(self))

Review comment:
       This part got removed

##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,7 +558,34 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
-                if err ~= "timeout" and err ~= "Key not found"
+                if string.find(err, "connection refused")
+                    or string.find(err, "Service Unavailable") then
+                    log.warn(err, ", ", tostring(self))
+                    local etcd_cli, err = get_etcd(true)
+                    if not etcd_cli then
+                        error("all etcd endpoints are unhealthy: " .. err)
+                    end
+                    self.etcd_cli = etcd_cli
+                    log.warn("changed to new etcd endpoint")
+                elseif string.find(err, "has no healthy etcd endpoint available") then
+                    log.warn(err, ", ", tostring(self))
+                    local reconnected = false
+                    while err and not reconnected do
+                        local backoff_duration, backoff_factor, backoff_step = 1, 2, 10
+                        for _ = 0, backoff_step, 1 do
+                            ngx_sleep(backoff_duration)
+                            _, err = sync_data(self)
+                            if not string.find(err, "has no healthy etcd endpoint available") then

Review comment:
       Fixed. Thanks!




-- 
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:
users@infra.apache.org



[GitHub] [apisix] spacewander commented on a change in pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r658775498



##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,7 +564,24 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
-                if err ~= "timeout" and err ~= "Key not found"
+                if string.find(err, err_etcd_unhealthy_all) then
+                    local reconnected = false
+                    while err and not reconnected do
+                        local backoff_duration, backoff_factor, backoff_step = 1, 2, 10
+                        for _ = 0, backoff_step, 1 do

Review comment:
       Should be `1, backoff_step, 1`?




-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

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



[GitHub] [apisix] spacewander commented on a change in pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r629778272



##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,6 +558,33 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
+                if string.find(err, "connection refused")
+                    or string.find(err, "Service Unavailable") then
+                    local etcd_cli, err = get_etcd()
+                    if not etcd_cli then
+                        error("all etcd endpoints are unhealthy: " .. err)
+                    end
+                    self.etcd_cli = etcd_cli
+                    log.warn("changed to new etcd endpoint")
+                end
+                if string.find(err, "has no healthy etcd endpoint available") then

Review comment:
       elseif

##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,6 +558,33 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
+                if string.find(err, "connection refused")
+                    or string.find(err, "Service Unavailable") then
+                    local etcd_cli, err = get_etcd()
+                    if not etcd_cli then
+                        error("all etcd endpoints are unhealthy: " .. err)
+                    end
+                    self.etcd_cli = etcd_cli
+                    log.warn("changed to new etcd endpoint")
+                end
+                if string.find(err, "has no healthy etcd endpoint available") then
+                    local reconnected = false
+                    while err and not reconnected do
+                        local backoff_duration, backoff_factor, backoff_step = 1, 2, 10
+                        for _ = 0, backoff_step, 1 do
+                            ngx_sleep(backoff_duration)
+                            _, err = sync_data(self)
+                            if not string.find(err, "has no healthy etcd endpoint available") then
+                                log.warn("reconnected to etcd")
+                                reconnected = true
+                                break
+                            end
+                            backoff_duration = backoff_duration * backoff_factor
+                            log.error("no healthy etcd endpoint available, next retry after "
+                                       .. backoff_duration .. "s")
+                        end
+                    end
+                end
                 if err ~= "timeout" and err ~= "Key not found"

Review comment:
       elseif

##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,6 +558,33 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
+                if string.find(err, "connection refused")
+                    or string.find(err, "Service Unavailable") then
+                    local etcd_cli, err = get_etcd()

Review comment:
       The `get_etcd` only gets the singleton of etcd client, it doesn't recreate it.

##########
File path: apisix/core/config_etcd.lua
##########
@@ -529,6 +531,17 @@ local function _automatic_fetch(premature, self)
         return
     end
 
+    if not health_check.conf then

Review comment:
       Where will `health_check.conf` be assigned?




-- 
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:
users@infra.apache.org



[GitHub] [apisix] tokers commented on a change in pull request #4191: feat: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
tokers commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r661078905



##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,7 +564,24 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
-                if err ~= "timeout" and err ~= "Key not found"
+                if string.find(err, err_etcd_unhealthy_all) then
+                    local reconnected = false
+                    while err and not reconnected do
+                        local backoff_duration, backoff_factor, backoff_step = 1, 2, 10

Review comment:
       Maybe `6` is enough, also, since we are in a timer, keep a Nginx timer living for a long while is not a good practice as it might cause the memory leaky.




-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

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



[GitHub] [apisix] tokers commented on a change in pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
tokers commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r645242854



##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,7 +558,34 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
-                if err ~= "timeout" and err ~= "Key not found"
+                if string.find(err, "connection refused")
+                    or string.find(err, "Service Unavailable") then
+                    log.warn(err, ", ", tostring(self))

Review comment:
       Is this for debugging purpose?




-- 
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:
users@infra.apache.org



[GitHub] [apisix] Yiyiyimu commented on a change in pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r645179104



##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,6 +558,33 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
+                if string.find(err, "connection refused")
+                    or string.find(err, "Service Unavailable") then
+                    local etcd_cli, err = get_etcd()

Review comment:
       Got it. Working on it




-- 
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:
users@infra.apache.org



[GitHub] [apisix] Yiyiyimu commented on pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#issuecomment-837350315


   Things get a bit weird right now 😕 
   
   Before I work on this fix, I test that if killing one node out of an etcd cluster, apisix would fail, as those related issues talk about. However, right now when I test this scenario again on the master branch, apisix would **not** be affected by the closed etcd node and runs normally. I checked the recent commits and it seems no changes have applied to this problem.
   
   Could someone help me to reproduce the program again


-- 
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:
users@infra.apache.org



[GitHub] [apisix] Yiyiyimu commented on a change in pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r660154147



##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,7 +564,24 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
-                if err ~= "timeout" and err ~= "Key not found"
+                if string.find(err, err_etcd_unhealthy_all) then
+                    local reconnected = false
+                    while err and not reconnected do
+                        local backoff_duration, backoff_factor, backoff_step = 1, 2, 10
+                        for _ = 0, backoff_step, 1 do

Review comment:
       Thanks for remind! Fixed




-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

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



[GitHub] [apisix] Yiyiyimu commented on a change in pull request #4191: feat: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r660287165



##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,7 +564,24 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
-                if err ~= "timeout" and err ~= "Key not found"
+                if string.find(err, err_etcd_unhealthy_all) then
+                    local reconnected = false
+                    while err and not reconnected do
+                        local backoff_duration, backoff_factor, backoff_step = 1, 2, 10

Review comment:
       I'm not so familiar with the production environment. Do you have some recommendations, like 8, around 4 mins at most? @tokers 




-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

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



[GitHub] [apisix] Yiyiyimu commented on a change in pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r645179104



##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,6 +558,33 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
+                if string.find(err, "connection refused")
+                    or string.find(err, "Service Unavailable") then
+                    local etcd_cli, err = get_etcd()

Review comment:
       Changed to forcibly get new etcd client




-- 
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:
users@infra.apache.org



[GitHub] [apisix] Yiyiyimu commented on a change in pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r645248271



##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,7 +558,34 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
-                if err ~= "timeout" and err ~= "Key not found"
+                if string.find(err, "connection refused")
+                    or string.find(err, "Service Unavailable") then
+                    log.warn(err, ", ", tostring(self))

Review comment:
       Mainly to fulfill the needs of config-etcd test, and I think it's also good to inform users that etcd can not be connected
   https://github.com/apache/apisix/blob/86e168e711713531eeafdd0a75682b150e0d0109/t/core/config_etcd.t#L28-L49




-- 
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:
users@infra.apache.org



[GitHub] [apisix] tokers commented on a change in pull request #4191: feat: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
tokers commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r660226319



##########
File path: apisix/core/config_etcd.lua
##########
@@ -568,7 +568,7 @@ local function _automatic_fetch(premature, self)
                     local reconnected = false
                     while err and not reconnected do
                         local backoff_duration, backoff_factor, backoff_step = 1, 2, 10
-                        for _ = 0, backoff_step, 1 do
+                        for _ = 1, backoff_step, 1 do

Review comment:
       We can ignore the pace parameter if it is `1`.




-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

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



[GitHub] [apisix] Yiyiyimu commented on a change in pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r655764393



##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,6 +558,33 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
+                if string.find(err, "connection refused")
+                    or string.find(err, "Service Unavailable") then
+                    local etcd_cli, err = get_etcd()

Review comment:
       Removed since moved one-node-down retry to `lua-resty-etcd`




-- 
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:
users@infra.apache.org



[GitHub] [apisix] Yiyiyimu commented on a change in pull request #4191: feat: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r660251010



##########
File path: apisix/core/config_etcd.lua
##########
@@ -568,7 +568,7 @@ local function _automatic_fetch(premature, self)
                     local reconnected = false
                     while err and not reconnected do
                         local backoff_duration, backoff_factor, backoff_step = 1, 2, 10
-                        for _ = 0, backoff_step, 1 do
+                        for _ = 1, backoff_step, 1 do

Review comment:
       @tokers PTAL




-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

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



[GitHub] [apisix] tokers commented on a change in pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
tokers commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r645476306



##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,7 +558,34 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
-                if err ~= "timeout" and err ~= "Key not found"
+                if string.find(err, "connection refused")
+                    or string.find(err, "Service Unavailable") then
+                    log.warn(err, ", ", tostring(self))

Review comment:
       Then the error message should be more semantic.




-- 
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:
users@infra.apache.org



[GitHub] [apisix] spacewander commented on a change in pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
spacewander commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r645384366



##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,7 +558,34 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
-                if err ~= "timeout" and err ~= "Key not found"
+                if string.find(err, "connection refused")
+                    or string.find(err, "Service Unavailable") then
+                    log.warn(err, ", ", tostring(self))
+                    local etcd_cli, err = get_etcd(true)
+                    if not etcd_cli then
+                        error("all etcd endpoints are unhealthy: " .. err)
+                    end
+                    self.etcd_cli = etcd_cli
+                    log.warn("changed to new etcd endpoint")
+                elseif string.find(err, "has no healthy etcd endpoint available") then
+                    log.warn(err, ", ", tostring(self))
+                    local reconnected = false
+                    while err and not reconnected do
+                        local backoff_duration, backoff_factor, backoff_step = 1, 2, 10
+                        for _ = 0, backoff_step, 1 do
+                            ngx_sleep(backoff_duration)
+                            _, err = sync_data(self)
+                            if not string.find(err, "has no healthy etcd endpoint available") then

Review comment:
       The `err` can be nil which will cause `find` to throw an error

##########
File path: apisix/core/config_etcd.lua
##########
@@ -529,6 +531,17 @@ local function _automatic_fetch(premature, self)
         return
     end
 
+    if not health_check.conf then

Review comment:
       Is it because fail_timeout takes effect?
   https://github.com/api7/lua-resty-etcd/blob/1fe01f77dda252f2d95eed0a7635f3479097b8ed/lib/resty/etcd/health_check.lua#L34




-- 
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:
users@infra.apache.org



[GitHub] [apisix] Yiyiyimu commented on a change in pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r645176342



##########
File path: apisix/core/config_etcd.lua
##########
@@ -529,6 +531,17 @@ local function _automatic_fetch(premature, self)
         return
     end
 
+    if not health_check.conf then

Review comment:
       In lua-resty-etcd
   https://github.com/api7/lua-resty-etcd/blob/1fe01f77dda252f2d95eed0a7635f3479097b8ed/lib/resty/etcd/health_check.lua#L70-L83




-- 
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:
users@infra.apache.org



[GitHub] [apisix] tokers commented on a change in pull request #4191: feat: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
tokers commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r660226319



##########
File path: apisix/core/config_etcd.lua
##########
@@ -568,7 +568,7 @@ local function _automatic_fetch(premature, self)
                     local reconnected = false
                     while err and not reconnected do
                         local backoff_duration, backoff_factor, backoff_step = 1, 2, 10
-                        for _ = 0, backoff_step, 1 do
+                        for _ = 1, backoff_step, 1 do

Review comment:
       We can ignore the pace parameter if it is `1`.

##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,7 +564,24 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
-                if err ~= "timeout" and err ~= "Key not found"
+                if string.find(err, err_etcd_unhealthy_all) then
+                    local reconnected = false
+                    while err and not reconnected do
+                        local backoff_duration, backoff_factor, backoff_step = 1, 2, 10

Review comment:
       Will step `10` too large? As 1024 seconds is too long.




-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

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



[GitHub] [apisix] tokers commented on a change in pull request #4191: feat: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
tokers commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r660281943



##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,7 +564,24 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
-                if err ~= "timeout" and err ~= "Key not found"
+                if string.find(err, err_etcd_unhealthy_all) then
+                    local reconnected = false
+                    while err and not reconnected do
+                        local backoff_duration, backoff_factor, backoff_step = 1, 2, 10

Review comment:
       Will step `10` too large? As 1024 seconds is too long.




-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

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



[GitHub] [apisix] Yiyiyimu commented on a change in pull request #4191: fix: enable etcd health-check

Posted by GitBox <gi...@apache.org>.
Yiyiyimu commented on a change in pull request #4191:
URL: https://github.com/apache/apisix/pull/4191#discussion_r645209292



##########
File path: apisix/core/config_etcd.lua
##########
@@ -545,6 +558,33 @@ local function _automatic_fetch(premature, self)
 
             local ok, err = sync_data(self)
             if err then
+                if string.find(err, "connection refused")
+                    or string.find(err, "Service Unavailable") then
+                    local etcd_cli, err = get_etcd()
+                    if not etcd_cli then
+                        error("all etcd endpoints are unhealthy: " .. err)
+                    end
+                    self.etcd_cli = etcd_cli
+                    log.warn("changed to new etcd endpoint")
+                end
+                if string.find(err, "has no healthy etcd endpoint available") then

Review comment:
       Fixed thanks!




-- 
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:
users@infra.apache.org