You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by we...@apache.org on 2019/11/02 00:00:05 UTC

[incubator-apisix] branch master updated: bugfix: skipped to init etcd if use local file as config center. (#737)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new a1c56ce  bugfix: skipped to init etcd if use local file as config center. (#737)
a1c56ce is described below

commit a1c56ceb5364ae21705f031afde186b6e08223e8
Author: YuanSheng Wang <me...@gmail.com>
AuthorDate: Sat Nov 2 07:59:59 2019 +0800

    bugfix: skipped to init etcd if use local file as config center. (#737)
---
 bin/apisix                         |  4 ++++
 conf/apisix.yaml                   | 25 ++++++++++++++++++++
 lua/apisix/admin/init.lua          |  1 -
 lua/apisix/admin/node_status.lua   | 41 ---------------------------------
 lua/apisix/plugins/heartbeat.lua   | 23 ++++++++++++++-----
 lua/apisix/plugins/node-status.lua | 47 ++++----------------------------------
 t/plugin/node-status.t             | 27 +---------------------
 7 files changed, 52 insertions(+), 116 deletions(-)

diff --git a/bin/apisix b/bin/apisix
index a9010a3..3f69dd2 100755
--- a/bin/apisix
+++ b/bin/apisix
@@ -536,6 +536,10 @@ local function init_etcd(show_output)
         error("failed to read local yaml config of apisix: " .. err)
     end
 
+    if yaml_conf.apisix.config_center ~= "etcd" then
+        return true
+    end
+
     local etcd_conf = yaml_conf.etcd
     local uri = etcd_conf.host .. "/v2/keys" .. (etcd_conf.prefix or "")
 
diff --git a/conf/apisix.yaml b/conf/apisix.yaml
new file mode 100644
index 0000000..1a406ae
--- /dev/null
+++ b/conf/apisix.yaml
@@ -0,0 +1,25 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# example
+routes:
+  -
+    uri: /hello
+    upstream:
+        nodes:
+            "127.0.0.1:1980": 1
+        type: roundrobin
+#END
diff --git a/lua/apisix/admin/init.lua b/lua/apisix/admin/init.lua
index 2c035c0..128340f 100644
--- a/lua/apisix/admin/init.lua
+++ b/lua/apisix/admin/init.lua
@@ -36,7 +36,6 @@ local resources = {
     proto           = require("apisix.admin.proto"),
     global_rules    = require("apisix.admin.global_rules"),
     stream_routes   = require("apisix.admin.stream_routes"),
-    node_status     = require("apisix.admin.node_status"),
 }
 
 
diff --git a/lua/apisix/admin/node_status.lua b/lua/apisix/admin/node_status.lua
deleted file mode 100644
index a5d42af..0000000
--- a/lua/apisix/admin/node_status.lua
+++ /dev/null
@@ -1,41 +0,0 @@
---
--- Licensed to the Apache Software Foundation (ASF) under one or more
--- contributor license agreements.  See the NOTICE file distributed with
--- this work for additional information regarding copyright ownership.
--- The ASF licenses this file to You under the Apache License, Version 2.0
--- (the "License"); you may not use this file except in compliance with
--- the License.  You may obtain a copy of the License at
---
---     http://www.apache.org/licenses/LICENSE-2.0
---
--- Unless required by applicable law or agreed to in writing, software
--- distributed under the License is distributed on an "AS IS" BASIS,
--- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--- See the License for the specific language governing permissions and
--- limitations under the License.
---
-local core = require("apisix.core")
-
-
-local _M = {
-    version = 0.1,
-}
-
-
-function _M.get(id)
-    local key = "/node_status"
-    if id then
-        key = key .. "/" .. id
-    end
-
-    local res, err = core.etcd.get(key)
-    if not res then
-        core.log.error("failed to get route[", key, "]: ", err)
-        return 500, {error_msg = err}
-    end
-
-    return res.status, res.body
-end
-
-
-return _M
diff --git a/lua/apisix/plugins/heartbeat.lua b/lua/apisix/plugins/heartbeat.lua
index fca021d..b9275c3 100644
--- a/lua/apisix/plugins/heartbeat.lua
+++ b/lua/apisix/plugins/heartbeat.lua
@@ -63,15 +63,26 @@ end
 
 local function report()
     -- ngx.sleep(3)
-    local etcd_version, err = core.etcd.server_version()
-    if not etcd_version then
-        core.log.error("failed to fetch etcd version: ", err)
+    local etcd_version, etcd_version_err
+    local local_conf = core.config.local_conf()
+
+    if local_conf.apisix.config_center == "etcd" then
+        etcd_version, etcd_version_err = core.etcd.server_version()
+        if not etcd_version then
+            core.log.error("failed to fetch etcd version: ", etcd_version_err)
+        else
+            etcd_version = etcd_version.body and etcd_version.body.etcdserver
+        end
     end
 
+    core.log.warn(core.json.encode(etcd_version))
+
     local info = {
         version = core.version,
-        plugins = core.config.local_conf().plugins,
-        etcd_version = etcd_version.body,
+        plugins = local_conf.plugins,
+        config_center = local_conf.apisix.config_center,
+        etcd_version = etcd_version,
+        etcd_version_err = etcd_version_err,
         uuid = core.id.get(),
     }
 
@@ -81,7 +92,7 @@ local function report()
         core.log.error("failed to encode hearbeat information: ", err)
         return
     end
-    core.log.debug("heartbeat body: ", args)
+    core.log.warn("heartbeat body: ", args)
 
     local res
     res, err = request_apisix_svr(args)
diff --git a/lua/apisix/plugins/node-status.lua b/lua/apisix/plugins/node-status.lua
index d7254b0..d34d271 100644
--- a/lua/apisix/plugins/node-status.lua
+++ b/lua/apisix/plugins/node-status.lua
@@ -24,7 +24,7 @@ local ipairs = ipairs
 
 local _M = {
     version = 0.1,
-    priority = 1000,        -- TODO: add a type field, may be a good idea
+    priority = 1000,
     name = plugin_name,
 }
 
@@ -37,32 +37,15 @@ local ngx_statu_items = {
 
 
 local function collect()
-    core.log.info("try to collect node status from etcd: ",
-                  "/node_status/" .. apisix_id)
-    local res, err = core.etcd.get("/node_status/" .. apisix_id)
-    if not res then
-        return 500, {error = err}
-    end
-
-    return res.status, res.body
-end
-
-
-local function run_loop()
     local res, err = core.http.request_self("/apisix/nginx_status", {
                                                 keepalive = false,
                                             })
     if not res then
-        if err then
-            return core.log.error("failed to fetch nginx status: ", err)
-        end
-        return
+        return 500, "failed to fetch nginx status: " .. err
     end
 
     if res.status ~= 200 then
-        core.log.error("failed to fetch nginx status, response code: ",
-                       res.status)
-        return
+        return res.status
     end
 
     -- Active connections: 2
@@ -72,8 +55,7 @@ local function run_loop()
 
     local iterator, err = re_gmatch(res.body, [[(\d+)]], "jmo")
     if not iterator then
-        core.log.error("failed to re.gmatch Nginx status: ", err)
-        return
+        return 500, "failed to re.gmatch Nginx status: " .. err
     end
 
     core.table.clear(ngx_status)
@@ -86,17 +68,7 @@ local function run_loop()
         ngx_status[name] = val[0]
     end
 
-    local res, err = core.etcd.set("/node_status/" .. apisix_id, ngx_status)
-    if not res then
-        core.log.error("failed to create etcd client: ", err)
-        return
-    end
-
-    if res.status >= 300 then
-        core.log.error("failed to update node status, code: ", res.status,
-                       " body: ", core.json.encode(res.body, true))
-        return
-    end
+    return 200, core.json.encode({id = apisix_id, status = ngx_status})
 end
 
 
@@ -111,13 +83,4 @@ function _M.api()
 end
 
 
-    local timer
-function _M.init()
-    if timer or ngx.worker.id() ~= 0 then
-        return
-    end
-    timer = core.timer.new(plugin_name, run_loop, {check_interval = 5 * 60})
-end
-
-
 return _M
diff --git a/t/plugin/node-status.t b/t/plugin/node-status.t
index 9abde21..d73dcc1 100644
--- a/t/plugin/node-status.t
+++ b/t/plugin/node-status.t
@@ -49,32 +49,7 @@ qr/"accepted":/
 
 
 
-=== TEST 2: get node status
---- config
-    location /t {
-        content_by_lua_block {
-            ngx.sleep(0.5)
-            local t = require("lib.test_admin").test
-            local code, body, body_org = t('/apisix/admin/node_status',
-                ngx.HTTP_GET
-                )
-
-            if code >= 300 then
-                ngx.status = code
-            end
-            ngx.say(body_org)
-        }
-    }
---- request
-GET /t
---- response_body eval
-qr/"accepted"/
---- no_error_log
-[error]
-
-
-
-=== TEST 3: test for unsupported method
+=== TEST 2: test for unsupported method
 --- request
 PATCH /apisix/status
 --- error_code: 404