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

feat: server info #2884

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 2 additions & 0 deletions apisix/admin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ local require = require
local core = require("apisix.core")
local route = require("resty.radixtree")
local plugin = require("apisix.plugin")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert this line

local ngx = ngx
local get_method = ngx.req.get_method
local ngx_time = ngx.time
Expand All @@ -29,6 +30,7 @@ local reload_event = "/apisix/admin/plugins/reload"
local ipairs = ipairs
local error = error
local events

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

not related to this title of PR

local MAX_REQ_BODY = 1024 * 1024 * 1.5 -- 1.5 MiB


Expand Down
1 change: 1 addition & 0 deletions apisix/cli/ngx_tpl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ http {
.. [=[$prefix/deps/lib/lua/5.1/?.so;;]=]
.. [=[{*lua_cpath*};";

lua_shared_dict internal_status 10m;
lua_shared_dict plugin-limit-req 10m;
lua_shared_dict plugin-limit-count 10m;
lua_shared_dict prometheus-metrics 10m;
Expand Down
46 changes: 37 additions & 9 deletions apisix/core/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,36 @@
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
local table = require("apisix.core.table")
local ngx_re = require("ngx.re")
local resolver = require("resty.dns.resolver")
local ipmatcher= require("resty.ipmatcher")
local ffi = require("ffi")
local base = require("resty.core.base")
local table = require("apisix.core.table")
local log = require("apisix.core.log")
local ngx_re = require("ngx.re")
local resolver = require("resty.dns.resolver")
local ipmatcher = require("resty.ipmatcher")
local ffi = require("ffi")
local base = require("resty.core.base")

local open = io.open
local math = math
local sub_str = string.sub
local str_byte = string.byte
local tonumber = tonumber
local type = type
local C = ffi.C
local ffi_string = ffi.string

local ffi_string = ffi.string
local get_string_buf = base.get_string_buf
local exiting = ngx.worker.exiting
local ngx_sleep = ngx.sleep
local exiting = ngx.worker.exiting
local ngx_sleep = ngx.sleep

local hostname
local max_sleep_interval = 1
local max_hostname_len = 256

ffi.cdef[[
int ngx_escape_uri(char *dst, const char *src,
size_t size, int type);
int gethostname(char *name, size_t len);
int strlen(const char *s);
]]


Expand Down Expand Up @@ -203,6 +211,26 @@ function _M.validate_header_value(value)
end


function _M.gethostname()
if hostname then
return hostname
end

local buf = get_string_buf(max_hostname_len)

if C.gethostname(buf, max_hostname_len) == 0 then
buf[max_hostname_len - 1] = str_byte('\0')
hostname = ffi_string(buf, ffi.C.strlen(buf))

else
log.alert("ffi.C.gethostname() failed")
hostname = "unknown"
end

return hostname
end


local function sleep(sec)
if sec <= max_sleep_interval then
return ngx_sleep(sec)
Expand Down
1 change: 1 addition & 0 deletions apisix/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ function _M.http_init_worker()

require("apisix.debug").init_worker()
require("apisix.upstream").init_worker()
require("apisix.server_info").init_worker()

local_conf = core.config.local_conf()
local dns_resolver_valid = local_conf and local_conf.apisix and
Expand Down
67 changes: 67 additions & 0 deletions apisix/plugins/server-info.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
--
-- 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 server_info = require("apisix.server_info")
local core = require("apisix.core")

local plugin_name = "server-info"
local schema = {
type = "object",
additionalProperties = false,
}


local _M = {
version = 0.1,
priority = 1000,
name = plugin_name,
schema = schema,
}


local function get_server_info()
local server_info, err = server_info.get()
if not server_info then
core.log.error("failed to get server_info: ", err)
return 500, err
end

return 200, core.json.encode(server_info)
end


function _M.check_schema(conf)
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
end

return true
end


function _M.api()
return {
{
methods = {"GET"},
uri = "/apisix/server_info",
handler = get_server_info,
},
}
end


return _M
168 changes: 168 additions & 0 deletions apisix/server_info.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
--
-- 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 require = require
local core = require("apisix.core")

local type = type
local ngx_time = ngx.time

local boot_time = os.time()
local internal_status = ngx.shared.internal_status

local _M = {}

if not internal_status then
error("lua_shared_dict \"internal_status\" not configured")
end


local function is_privileged()
local process_type = require("ngx.process").type()
return process_type == "privileged agent" or process_type == "single"
end

-- server information will be saved into shared memory only if the key
-- "server_info" not exist if excl is true.
local function save(data, excl)
local handler = excl and internal_status.add or internal_status.set

local ok, err = handler(internal_status, "server_info", data)
if not ok then
if excl and err == "exists" then
return true
end

return nil, err
end

return true
end


local function encode_and_save(server_info, excl)
local data, err = core.json.encode(server_info)
if not data then
return nil, err
end

return save(data, excl)
end


local function report()
local server_info, err = _M.get()
if not server_info then
core.log.error("failed to get server_info: ", err)
return nil, err
end

if server_info.etcd_version == "unknown" then
local res, err = core.etcd.server_version()
if not res then
core.log.error("failed to fetch etcd version: ", err)
return nil, err

elseif type(res.body) ~= "table" then
core.log.error("failed to fetch etcd version: bad version info")
return nil, "bad etcd version info"
else
server_info.etcd_version = res.body.etcdcluster
end
end

server_info.last_report_time = ngx_time()

local data, err = core.json.encode(server_info)
if not data then
core.log.error("failed to encode server_info: ", err)
return nil, err
end

local key = "/data_plane/server_info/" .. server_info.id
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can not use data_plane here. CP will write the server info too.

how about /nodes/server_info/{node_id}?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key /nodes/server_info/{node_id} is good, but why CP also writes the server info?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@membphis @tokers
I think /data_plane/ is better.
It would be better to limit the write of DP to certain prefix, so that we can grant the privilege strictly.

local ok, err = core.etcd.set(key, data, 180)
if not ok then
core.log.error("failed to report server info to etcd: ", err)
return nil, err
end

local ok, err = save(data, false)
if not ok then
core.log.error("failed to encode and save server info: ", err)
return nil, err
end
end


local function uninitialized_server_info()
return {
etcd_version = "unknown",
hostname = core.utils.gethostname(),
id = core.id.get(),
version = core.version.VERSION,
up_time = ngx_time() - boot_time,
last_report_time = -1,
}
end


function _M.init_worker()
if not is_privileged() then
return
end

local ok, err = encode_and_save(uninitialized_server_info(), true)
if not ok then
core.log.error("failed to encode and save server info: ", err)
end

local opts = {
check_interval = 5, -- in seconds
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5 seconds is short, I think 10 mins should be good

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and we should allow the user to specify this field

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think 10 mins is too long, also for now, all the basic server info are static (except the up_time and last_report_time), a relative short period is more suitable so we can easily judge whether a node is healthy or unstable when we are viewing the server info on Dashboard.

What about 1 min or 2 mins?

}

if core.config ~= require("apisix.core.config_etcd") then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need some comment for why call return here

return
end

-- only launch timer to report server info when config cener is etcd.
local timer, err = core.timer.new("server info", report, opts)
if not timer then
core.log.error("failed to create timer to report server info ", err)
end
end


function _M.get()
local data, err = internal_status:get("server_info")
if err ~= nil then
return nil, err
end

if not data then
return uninitialized_server_info()
end

local server_info, err = core.json.decode(data)
if not server_info then
return nil, err
end

server_info.up_time = ngx_time() - boot_time
return server_info
end


return _M
1 change: 1 addition & 0 deletions conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ plugins: # plugin list
- sls-logger
- hmac-auth
- api-breaker
- server-info

stream_plugins:
- mqtt-proxy
Expand Down
Loading