You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2021/06/10 06:40:56 UTC

[apisix] branch master updated: feat: add etcd retry when init (#4361)

This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 66bd993  feat: add etcd retry when init (#4361)
66bd993 is described below

commit 66bd993f87b4157b2c40d1ee445f5391c2aa6557
Author: Joshua Yang <yx...@gmail.com>
AuthorDate: Thu Jun 10 14:40:48 2021 +0800

    feat: add etcd retry when init (#4361)
---
 apisix/cli/etcd.lua | 80 ++++++++++++++++++++++++++++++++++++++---------------
 t/cli/test_etcd.sh  | 18 ++++++++++++
 2 files changed, 75 insertions(+), 23 deletions(-)

diff --git a/apisix/cli/etcd.lua b/apisix/cli/etcd.lua
index 2bea28f..3cdaaa8 100644
--- a/apisix/cli/etcd.lua
+++ b/apisix/cli/etcd.lua
@@ -191,10 +191,21 @@ function _M.init(env, args)
         local version_url = host .. "/version"
         local errmsg
 
-        local res, err = request(version_url, yaml_conf)
-        -- In case of failure, request returns nil followed by an error message.
-        -- Else the first return value is the response body
-        -- and followed by the response status code.
+        local res, err
+        local retry_time = 0
+        while retry_time < 2 do
+            res, err = request(version_url, yaml_conf)
+            -- In case of failure, request returns nil followed by an error message.
+            -- Else the first return value is the response body
+            -- and followed by the response status code.
+            if res then
+                break
+            end
+            retry_time = retry_time + 1
+            print(str_format("Warning! Request etcd endpoint \'%s\' error, %s, retry time=%s",
+                             version_url, err, retry_time))
+        end
+
         if not res then
             errmsg = str_format("request etcd endpoint \'%s\' error, %s\n", version_url, err)
             util.die(errmsg)
@@ -233,18 +244,30 @@ function _M.init(env, args)
 
             local post_json_auth = dkjson.encode(json_auth)
             local response_body = {}
-            local res, err = request({
-                url = auth_url,
-                method = "POST",
-                source = ltn12.source.string(post_json_auth),
-                sink = ltn12.sink.table(response_body),
-                headers = {
-                    ["Content-Length"] = #post_json_auth
-                }
-            }, yaml_conf)
-            -- In case of failure, request returns nil followed by an error message.
-            -- Else the first return value is just the number 1
-            -- and followed by the response status code.
+
+            local res, err
+            local retry_time = 0
+            while retry_time < 2 do
+                res, err = request({
+                    url = auth_url,
+                    method = "POST",
+                    source = ltn12.source.string(post_json_auth),
+                    sink = ltn12.sink.table(response_body),
+                    headers = {
+                        ["Content-Length"] = #post_json_auth
+                    }
+                }, yaml_conf)
+                -- In case of failure, request returns nil followed by an error message.
+                -- Else the first return value is just the number 1
+                -- and followed by the response status code.
+                if res then
+                    break
+                end
+                retry_time = retry_time + 1
+                print(str_format("Warning! Request etcd endpoint \'%s\' error, %s, retry time=%s",
+                                 auth_url, err, retry_time))
+            end
+
             if not res then
                 errmsg = str_format("request etcd endpoint \"%s\" error, %s\n", auth_url, err)
                 util.die(errmsg)
@@ -282,13 +305,24 @@ function _M.init(env, args)
                 headers["Authorization"] = auth_token
             end
 
-            local res, err = request({
-                url = put_url,
-                method = "POST",
-                source = ltn12.source.string(post_json),
-                sink = ltn12.sink.table(response_body),
-                headers = headers
-            }, yaml_conf)
+            local res, err
+            local retry_time = 0
+            while retry_time < 2 do
+                res, err = request({
+                    url = put_url,
+                    method = "POST",
+                    source = ltn12.source.string(post_json),
+                    sink = ltn12.sink.table(response_body),
+                    headers = headers
+                }, yaml_conf)
+                retry_time = retry_time + 1
+                if res then
+                    break
+                end
+                print(str_format("Warning! Request etcd endpoint \'%s\' error, %s, retry time=%s",
+                                 put_url, err, retry_time))
+            end
+
             if not res then
                 errmsg = str_format("request etcd endpoint \"%s\" error, %s\n", put_url, err)
                 util.die(errmsg)
diff --git a/t/cli/test_etcd.sh b/t/cli/test_etcd.sh
index 2e20a39..30f5e4d 100755
--- a/t/cli/test_etcd.sh
+++ b/t/cli/test_etcd.sh
@@ -80,6 +80,24 @@ fi
 
 echo "passed: properly handle the error when connecting to etcd without auth"
 
+# Check etcd retry if connect failed
+git checkout conf/config.yaml
+
+echo '
+etcd:
+  host:
+    - "http://127.0.0.1:2389"
+  prefix: "/apisix"
+' > conf/config.yaml
+
+out=$(make init 2>&1 || true)
+if ! echo "$out" | grep "retry time"; then
+    echo "failed: apisix should echo \"retry time\""
+    exit 1
+fi
+
+echo "passed: Show retry time info successfully"
+
 # Check etcd connect refused
 git checkout conf/config.yaml