Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: skipped to init etcd if use local file as config center. #737

Merged
merged 6 commits into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bin/apisix
Original file line number Diff line number Diff line change
Expand Up @@ -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 "")

Expand Down
25 changes: 25 additions & 0 deletions conf/apisix.yaml
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion lua/apisix/admin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}


Expand Down
41 changes: 0 additions & 41 deletions lua/apisix/admin/node_status.lua

This file was deleted.

23 changes: 17 additions & 6 deletions lua/apisix/plugins/heartbeat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
membphis marked this conversation as resolved.
Show resolved Hide resolved
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(),
}

Expand All @@ -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)
Expand Down
47 changes: 5 additions & 42 deletions lua/apisix/plugins/node-status.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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


Expand All @@ -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
1 change: 1 addition & 0 deletions t/config-center-yaml/route-upstream.t
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 1 addition & 26 deletions t/plugin/node-status.t
Original file line number Diff line number Diff line change
Expand Up @@ -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