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: allow getting health check status via control API #3345

Merged
merged 3 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
56 changes: 56 additions & 0 deletions apisix/control/v1.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
--
local core = require("apisix.core")
local plugin = require("apisix.plugin")
local get_routes = require("apisix.router").http_routes
local get_services = require("apisix.http.service").services
local upstream_mod = require("apisix.upstream")
local get_upstreams = upstream_mod.upstreams
local ipairs = ipairs


local _M = {}
Expand Down Expand Up @@ -49,11 +54,62 @@ function _M.schema()
end


local function iter_and_add_checker(infos, values, src)
if not values then
return
end

for _, value in core.config_util.iterate_values(values) do
if value.checker then
local checker = value.checker
local upstream = value.checker_upstream
local host = upstream.checks and upstream.checks.active and upstream.checks.active.host
local port = upstream.checks and upstream.checks.active and upstream.checks.active.port
local nodes = upstream.nodes
local health_nodes = core.table.new(#nodes, 0)
for _, node in ipairs(nodes) do
local ok = checker:get_target_status(node.host, port or node.port, host)
if ok then
core.table.insert(health_nodes, node)
end
end

local conf = value.value
core.table.insert(infos, {
name = upstream_mod.get_healthchecker_name(value),
src_id = conf.id,
src_type = src,
nodes = nodes,
health_nodes = health_nodes,
})
end
end
end


function _M.healthcheck()
local infos = {}
local routes = get_routes()
iter_and_add_checker(infos, routes, "routes")
local services = get_services()
iter_and_add_checker(infos, services, "services")
local upstreams = get_upstreams()
iter_and_add_checker(infos, upstreams, "upstreams")
return 200, infos
end


return {
-- /v1/schema
{
methods = {"GET"},
uris = {"/schema"},
handler = _M.schema,
},
-- /v1/healthcheck
{
methods = {"GET"},
tokers marked this conversation as resolved.
Show resolved Hide resolved
uris = {"/healthcheck"},
handler = _M.healthcheck,
}
}
8 changes: 7 additions & 1 deletion apisix/upstream.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ local function release_checker(healthcheck_parent)
end


local function get_healthchecker_name(value)
return "upstream#" .. value.key
end
_M.get_healthchecker_name = get_healthchecker_name


local function create_checker(upstream)
if healthcheck == nil then
healthcheck = require("resty.healthcheck")
Expand All @@ -71,7 +77,7 @@ local function create_checker(upstream)
end

local checker, err = healthcheck.new({
name = "upstream#" .. healthcheck_parent.key,
name = get_healthchecker_name(healthcheck_parent),
shm_name = "upstream-healthcheck",
checks = upstream.checks,
})
Expand Down
68 changes: 68 additions & 0 deletions doc/control-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,71 @@ Return the jsonschema used by this APISIX instance in the format below:
For `plugins` part, only enabled plugins will be returned. Some plugins may lack
of fields like `consumer_schema` or `type`, it is dependended by the plugin's
definition.

### GET /v1/healthcheck

Introduced since `v2.3`.

Return current [health check](health-check.md) status in the format below:

```json
[
{
"health_nodes": [
{
"host": "127.0.0.1",
"port": 1980,
"weight": 1
}
],
"name": "upstream#/upstreams/1",
"nodes": [
{
"host": "127.0.0.1",
"port": 1980,
"weight": 1
},
{
"host": "127.0.0.2",
"port": 1988,
"weight": 1
}
],
"src_id": "1",
"src_type": "upstreams"
},
{
"health_nodes": [
{
"host": "127.0.0.1",
"port": 1980,
"weight": 1
}
],
"name": "upstream#/routes/1",
"nodes": [
{
"host": "127.0.0.1",
"port": 1980,
"weight": 1
},
{
"host": "127.0.0.1",
"port": 1988,
"weight": 1
}
],
"src_id": "1",
"src_type": "routes"
}
]
```

Each entry contains fields below:

* src_type: where the health checker comes from. The value is one of `["routes", "services", "upstreams"]`.
* src_id: the id of object which creates the health checker. For example, if Upstream
object with id 1 creates a health checker, the `src_type` is `upstreams` and the `src_id` is `1`.
* name: the name of the health checker.
* nodes: the target nodes of the health checker.
* health_nodes: the health node known by the health checker.
226 changes: 226 additions & 0 deletions t/control/healthcheck.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
#
# 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.
#
use t::APISIX 'no_plan';

repeat_each(1);
no_long_string();
no_root_location();
no_shuffle();
log_level("info");

add_block_preprocessor(sub {
my ($block) = @_;

if (!$block->yaml_config) {
my $yaml_config = <<_EOC_;
apisix:
node_listen: 1984
config_center: yaml
enable_admin: false
_EOC_

$block->set_value("yaml_config", $yaml_config);
}

if (!$block->request) {
$block->set_value("request", "GET /t");
}
});

run_tests;

__DATA__

=== TEST 1: upstreams
--- apisix_yaml
routes:
-
uris:
- /hello
upstream_id: 1
upstreams:
- nodes:
"127.0.0.1:1980": 1
"127.0.0.2:1988": 1
type: roundrobin
id: 1
checks:
active:
http_path: "/status"
healthy:
interval: 1
successes: 1
unhealthy:
interval: 1
http_failures: 1
#END
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin")
local http = require "resty.http"
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
local httpc = http.new()
local res, err = httpc:request_uri(uri, {method = "GET"})

ngx.sleep(2)

local code, body, res = t.test('/v1/healthcheck',
ngx.HTTP_GET)
res = json.decode(res)
table.sort(res[1].nodes, function(a, b)
return a.host < b.host
end)
ngx.say(json.encode(res))
}
}
--- grep_error_log eval
qr/unhealthy TCP increment \(.+\) for '[^']+'/
--- grep_error_log_out
unhealthy TCP increment (1/2) for '(127.0.0.2:1988)'
unhealthy TCP increment (2/2) for '(127.0.0.2:1988)'
--- response_body
[{"health_nodes":[{"host":"127.0.0.1","port":1980,"weight":1}],"name":"upstream#/upstreams/1","nodes":[{"host":"127.0.0.1","port":1980,"weight":1},{"host":"127.0.0.2","port":1988,"weight":1}],"src_id":"1","src_type":"upstreams"}]



=== TEST 2: routes
--- apisix_yaml
routes:
-
id: 1
uris:
- /hello
upstream:
nodes:
"127.0.0.1:1980": 1
"127.0.0.1:1988": 1
type: roundrobin
checks:
active:
http_path: "/status"
host: "127.0.0.1"
healthy:
interval: 1
successes: 1
unhealthy:
interval: 1
http_failures: 1
#END
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin")
local http = require "resty.http"
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
local httpc = http.new()
local res, err = httpc:request_uri(uri, {method = "GET"})

ngx.sleep(2)

local code, body, res = t.test('/v1/healthcheck',
ngx.HTTP_GET)
res = json.decode(res)
table.sort(res[1].nodes, function(a, b)
return a.port < b.port
end)
ngx.say(json.encode(res))
}
}
--- grep_error_log eval
qr/unhealthy TCP increment \(.+\) for '[^']+'/
--- grep_error_log_out
unhealthy TCP increment (1/2) for '127.0.0.1(127.0.0.1:1988)'
unhealthy TCP increment (2/2) for '127.0.0.1(127.0.0.1:1988)'
--- response_body
[{"health_nodes":[{"host":"127.0.0.1","port":1980,"weight":1}],"name":"upstream#/routes/1","nodes":[{"host":"127.0.0.1","port":1980,"weight":1},{"host":"127.0.0.1","port":1988,"weight":1}],"src_id":"1","src_type":"routes"}]



=== TEST 3: services
--- apisix_yaml
routes:
- id: 1
service_id: 1
uris:
- /hello

services:
-
id: 1
upstream:
nodes:
"127.0.0.1:1980": 1
"127.0.0.1:1988": 1
type: roundrobin
checks:
active:
http_path: "/status"
host: "127.0.0.1"
port: 1988
healthy:
interval: 1
successes: 1
unhealthy:
interval: 1
http_failures: 1
#END
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin")
local http = require "resty.http"
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
local httpc = http.new()
local res, err = httpc:request_uri(uri, {method = "GET"})

ngx.sleep(2)

local code, body, res = t.test('/v1/healthcheck',
ngx.HTTP_GET)
res = json.decode(res)
table.sort(res[1].nodes, function(a, b)
return a.port < b.port
end)
ngx.say(json.encode(res))
}
}
--- grep_error_log eval
qr/unhealthy TCP increment \(.+\) for '[^']+'/
--- grep_error_log_out
unhealthy TCP increment (1/2) for '127.0.0.1(127.0.0.1:1988)'
unhealthy TCP increment (2/2) for '127.0.0.1(127.0.0.1:1988)'
--- response_body
[{"health_nodes":{},"name":"upstream#/services/1","nodes":[{"host":"127.0.0.1","port":1980,"weight":1},{"host":"127.0.0.1","port":1988,"weight":1}],"src_id":"1","src_type":"services"}]



=== TEST 4: no checkers
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin")
local code, body, res = t.test('/v1/healthcheck',
ngx.HTTP_GET)
ngx.print(res)
}
}
--- response_body
{}