From 0fc45691a19f3ee83cf321e1d4baf22c2f4ed8bd Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Thu, 24 Oct 2019 19:19:44 -0400 Subject: [PATCH 1/6] bugfix: skipped to init etcd if use local file as config center. --- bin/apisix | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bin/apisix b/bin/apisix index a9010a3bc528..3f69dd2a62c4 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 "") From 844a80e3a17730fd900f613425a48d28f2278456 Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Thu, 24 Oct 2019 20:27:14 -0400 Subject: [PATCH 2/6] bug: avoided to call etcd when use local yaml file. --- conf/apisix.yaml | 9 ++++++ lua/apisix/admin/init.lua | 1 - lua/apisix/admin/node_status.lua | 41 -------------------------- lua/apisix/plugins/heartbeat.lua | 10 +++++-- lua/apisix/plugins/node-status.lua | 47 ++++-------------------------- 5 files changed, 21 insertions(+), 87 deletions(-) create mode 100644 conf/apisix.yaml delete mode 100644 lua/apisix/admin/node_status.lua diff --git a/conf/apisix.yaml b/conf/apisix.yaml new file mode 100644 index 000000000000..9757b9aad57d --- /dev/null +++ b/conf/apisix.yaml @@ -0,0 +1,9 @@ +# 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 2c035c064af0..128340f3cc96 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 a5d42afe26e0..000000000000 --- 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 fca021ddaf7f..2c0cfa5271f6 100644 --- a/lua/apisix/plugins/heartbeat.lua +++ b/lua/apisix/plugins/heartbeat.lua @@ -63,9 +63,13 @@ 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 = {} + if core.config.local_conf().apisix.config_center == "etcd" then + local err + etcd_version, err = core.etcd.server_version() + if not etcd_version then + core.log.error("failed to fetch etcd version: ", err) + end end local info = { diff --git a/lua/apisix/plugins/node-status.lua b/lua/apisix/plugins/node-status.lua index d7254b020ea0..d34d271c1d9b 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 From 83b5a842c396e8c61ca69736e98d2cfbd95b1ae5 Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Thu, 24 Oct 2019 22:28:52 -0400 Subject: [PATCH 3/6] plugin(heartbeat): add new field `etcd_version_err` and `config_center`. --- lua/apisix/plugins/heartbeat.lua | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lua/apisix/plugins/heartbeat.lua b/lua/apisix/plugins/heartbeat.lua index 2c0cfa5271f6..ee4503f53091 100644 --- a/lua/apisix/plugins/heartbeat.lua +++ b/lua/apisix/plugins/heartbeat.lua @@ -63,19 +63,22 @@ end local function report() -- ngx.sleep(3) - local etcd_version = {} - if core.config.local_conf().apisix.config_center == "etcd" then - local err - etcd_version, err = core.etcd.server_version() + 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: ", err) + core.log.error("failed to fetch etcd version: ", etcd_version_err) end end 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 and etcd_version.body, + etcd_version_err = etcd_version_err, uuid = core.id.get(), } From bd7d831db80b0d1151839bedbd72fed9bfa04ccb Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Thu, 24 Oct 2019 22:34:21 -0400 Subject: [PATCH 4/6] bugfix: only collect etcd version. --- lua/apisix/plugins/heartbeat.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lua/apisix/plugins/heartbeat.lua b/lua/apisix/plugins/heartbeat.lua index ee4503f53091..b9275c3366bb 100644 --- a/lua/apisix/plugins/heartbeat.lua +++ b/lua/apisix/plugins/heartbeat.lua @@ -70,14 +70,18 @@ local function report() 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 = local_conf.plugins, config_center = local_conf.apisix.config_center, - etcd_version = etcd_version and etcd_version.body, + etcd_version = etcd_version, etcd_version_err = etcd_version_err, uuid = core.id.get(), } @@ -88,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) From 935c181072c45f416bf162c299ffbfa64408d8a0 Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Fri, 1 Nov 2019 02:20:52 -0400 Subject: [PATCH 5/6] added ASF license. --- conf/apisix.yaml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/conf/apisix.yaml b/conf/apisix.yaml index 9757b9aad57d..1a406aea2948 100644 --- a/conf/apisix.yaml +++ b/conf/apisix.yaml @@ -1,3 +1,19 @@ +# +# 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: - From dadf28729a95b5187fe8d8d530ecb0dd09dbe0bb Mon Sep 17 00:00:00 2001 From: Yuansheng Date: Fri, 1 Nov 2019 02:49:37 -0400 Subject: [PATCH 6/6] test: removed useless test case. --- t/config-center-yaml/route-upstream.t | 1 + t/plugin/node-status.t | 27 +-------------------------- 2 files changed, 2 insertions(+), 26 deletions(-) diff --git a/t/config-center-yaml/route-upstream.t b/t/config-center-yaml/route-upstream.t index 7da4850a6fb4..b3a9cdb11796 100644 --- a/t/config-center-yaml/route-upstream.t +++ b/t/config-center-yaml/route-upstream.t @@ -84,6 +84,7 @@ GET /hello failed to find upstream by id: 1111 + === TEST 3: upstream_id priority upstream --- yaml_config eval: $::yaml_config --- apisix_yaml diff --git a/t/plugin/node-status.t b/t/plugin/node-status.t index 9abde21f7a40..d73dcc1c4fd2 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