You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/03/17 10:12:43 UTC
[GitHub] [apisix] spacewander commented on a change in pull request #3848: feat: add dump for consul_kv
spacewander commented on a change in pull request #3848:
URL: https://github.com/apache/apisix/pull/3848#discussion_r595868872
##########
File path: apisix/discovery/consul_kv.lua
##########
@@ -319,7 +397,7 @@ local function format_consul_params(consul_conf)
if scheme ~= "http" then
return nil, "only support consul http schema address, eg: http://address:port"
elseif path ~= "/" or core.string.has_suffix(v, '/') then
- return nil, "invalid consul server address, the valid format: http://address:port"
+ return nil, "invald consul server address, the valid format: http://address:port"
Review comment:
Should be `invalid`
##########
File path: apisix/discovery/consul_kv.lua
##########
@@ -356,6 +434,20 @@ function _M.init_worker()
return
end
+ if consul_conf.dump then
+ local dump = consul_conf.dump
+ if not dump.path or #dump.path == 0 then
Review comment:
We can use `required` & `minLength` to validate the path in the schema.
##########
File path: apisix/control/router.lua
##########
@@ -31,6 +31,41 @@ local get_method = ngx.req.get_method
local _M = {}
+local function format_dismod_uri(mod_name, uri)
+ if core.string.has_prefix(uri, "/v1/") then
+ return uri
+ end
+
+ local tmp = {"/v1/discovery/", mod_name}
+ if not core.string.has_prefix(uri, "/") then
+ core.table.insert(tmp, "/")
+ end
+ core.table.insert(tmp, uri)
+
+ return core.table.concat(tmp, "")
+end
+
+-- we do not hardcode the discovery module's control api uri
+local function format_dismod_controlapi_uris(mod_name, api_route)
Review comment:
Better to use `format_dismod_control_api_uris`
##########
File path: t/discovery/consul_kv_dump.t
##########
@@ -0,0 +1,346 @@
+#
+# 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) = @_;
+
+ my $http_config = $block->http_config // <<_EOC_;
+
+ server {
+ listen 30511;
+
+ location /hello {
+ content_by_lua_block {
+ ngx.say("server 1")
+ }
+ }
+ }
+_EOC_
+
+ $block->set_value("http_config", $http_config);
+});
+
+our $yaml_config = <<_EOC_;
+apisix:
+ node_listen: 1984
+ config_center: yaml
+ enable_admin: false
+ enable_control: true
+
+discovery:
+ consul_kv:
+ servers:
+ - "http://127.0.0.1:8500"
+ dump:
+ path: "consul_kv.dump"
+ load_on_init: true
+_EOC_
+
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: prepare nodes
+--- config
+location /v1/kv {
+ proxy_pass http://127.0.0.1:8500;
+}
+--- request eval
+[
+ "DELETE /v1/kv/upstreams/?recurse=true",
+ "PUT /v1/kv/upstreams/webpages/127.0.0.1:30511\n" . "{\"weight\": 1, \"max_fails\": 1, \"fail_timeout\": 1}",
+]
+--- response_body eval
+[
+ 'true',
+ 'true',
+ 'true',
+]
+
+
+
+=== TEST 2: show dump services
+--- yaml_config eval: $::yaml_config
+--- config
+ location /t {
+ content_by_lua_block {
+ local json = require("toolkit.json")
+ local t = require("lib.test_admin")
+ ngx.sleep(2)
+
+ local code, body, res = t.test('/v1/discovery/consul_kv/show_dump_file',
+ ngx.HTTP_GET)
+ local entity = json.decode(res)
+ ngx.say(json.encode(entity.services))
+ }
+ }
+--- timeout: 12
Review comment:
Do we need so long timeout?
##########
File path: docs/en/latest/discovery/consul_kv.md
##########
@@ -73,6 +76,30 @@ The `keepalive` has two optional values:
- `true`, default and recommend value, use the long pull way to query consul servers
- `false`, not recommend, it would use the short pull way to query consul servers, then you can set the `fetch_interval` for fetch interval
+#### Dump Data
+
+When we need reload `apisix` online, as the `consul_kv` module maybe loads data from CONSUL slower than load routes from ETCD, and would get the log at the moment before load successfully from consul:
+
+```
+ http_access_phase(): failed to set upstream: no valid upstream node
+```
+
+So, we import the `dump` function for `consul_kv` module. When reload, would load the dump file before from consul; when the registered nodes in consul been updated, would dump the upstream nodes into file automatically.
+
+The `dump` has three optional values now:
+
+- `path`, the dump file save path
+ - support relative path, eg: `logs/consul_kv.dump`
+ - support absolute path, eg: `/tmp/consul_kv.bin`
+ - make sure the dump file's parent path exist
+ - make sure the `apisix` has the dump file's read-write access permission,eg: `chown www:root conf/upstream.d/`
+- `load_on_init`, default value is `true`
+ - if `true`, load the data from dump file before load data from consul
Review comment:
Look like we need to prepare a dump file for a fresh installed APISIX? Maybe we need to write down this in the docs?
##########
File path: apisix/discovery/consul_kv.lua
##########
@@ -230,6 +240,70 @@ local function update_application(server_name_prefix, data)
end
+local function read_dump_srvs()
+ local data, err = util.read_file(dump_params.path)
+ if not data then
+ log.warn("read dump file get error: ", err)
+ return
+ end
+
+ core.log.info("read dump file: ", data)
+ data = util.trim(data)
+ if #data == 0 then
+ core.log.error("dump file is empty")
+ return
+ end
+
+ local entity, err = core.json.decode(data)
+ if err then
+ core.log.error("decoded dump data got error: ", err, ", file content: ", data)
+ return
+ end
+
+ if not entity.services or not entity.last_update then
+ core.log.error("decoded dump data miss fields, file content: ", data)
+ return
+ end
+
+ if dump_params.expire ~= 0 and (entity.last_update + dump_params.expire) < ngx.time() then
+ core.log.warn("dump file: ", dump_params.path,
+ " had expired, ignored it")
+ return
+ end
+
+ applications = entity.services
+ core.log.info("load dump file into memory success")
+end
+
+
+local function write_dump_srvs()
+ local entity = {
+ services = applications,
+ last_update = ngx.time(),
+ expire = dump_params.expire, -- later need handle it
+ }
+ local data = core.json.encode(entity)
+ local succ, err = util.write_file(dump_params.path, data)
+ if not succ then
+ core.log.error("write dump into file got error: ", err)
+ end
+end
+
+
+local function show_dump_file()
+ if not dump_params then
+ return 200, "dump params is nil"
Review comment:
Should we use a non-200 status code when an error happened?
And I think we need to test for error in this function.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org