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 2022/04/04 16:30:56 UTC

[GitHub] [apisix] spacewander commented on a diff in pull request #6759: feat(xds): using data written by xds to control dp behavior

spacewander commented on code in PR #6759:
URL: https://github.com/apache/apisix/pull/6759#discussion_r841919504


##########
apisix/init.lua:
##########
@@ -116,18 +116,18 @@ function _M.http_init_worker()
 
     require("apisix.debug").init_worker()
 
+    if core.config == require("apisix.core.config_xds") then

Review Comment:
   Can we just put all the init_worker together?



##########
apisix/core/config_xds.lua:
##########
@@ -101,18 +128,254 @@ local function load_libxds(lib_name)
               table.concat(tried_paths, '\r\n', 1, #tried_paths))
     end
 
-    local route_zone = C.ngx_http_lua_ffi_shdict_udata_to_zone(route_config[1])
-    local route_shd_cdata = ffi.cast("void*", route_zone)
-    xdsagent.initial(route_shd_cdata)
+    local config_zone = C.ngx_http_lua_ffi_shdict_udata_to_zone(config[1])
+    local config_shd_cdata = ffi.cast("void*", config_zone)
+
+    local conf_ver_zone = C.ngx_http_lua_ffi_shdict_udata_to_zone(conf_ver[1])
+    local conf_ver_shd_cdata = ffi.cast("void*", conf_ver_zone)
+
+    xdsagent.initial(config_shd_cdata, conf_ver_shd_cdata)
+end
+
+
+
+local function sync_data(self)
+    if not latest_version then

Review Comment:
   As we already block for the version, we can skip this check?



##########
apisix/core/config_xds.lua:
##########
@@ -101,18 +128,254 @@ local function load_libxds(lib_name)
               table.concat(tried_paths, '\r\n', 1, #tried_paths))
     end
 
-    local route_zone = C.ngx_http_lua_ffi_shdict_udata_to_zone(route_config[1])
-    local route_shd_cdata = ffi.cast("void*", route_zone)
-    xdsagent.initial(route_shd_cdata)
+    local config_zone = C.ngx_http_lua_ffi_shdict_udata_to_zone(config[1])
+    local config_shd_cdata = ffi.cast("void*", config_zone)
+
+    local conf_ver_zone = C.ngx_http_lua_ffi_shdict_udata_to_zone(conf_ver[1])
+    local conf_ver_shd_cdata = ffi.cast("void*", conf_ver_zone)
+
+    xdsagent.initial(config_shd_cdata, conf_ver_shd_cdata)
+end
+
+
+
+local function sync_data(self)
+    if not latest_version then
+        return false, "wait for more time"
+    end
+
+    if self.conf_version == latest_version then
+        return true
+    end
+
+    if self.values then
+        for _, val in ipairs(self.values) do
+            if val and val.clean_handlers then
+                for _, clean_handler in ipairs(val.clean_handlers) do
+                    clean_handler(val)
+                end
+                val.clean_handlers = nil
+            end
+        end
+        self.values = nil
+        self.values_hash = nil
+    end
+
+    local keys = config:get_keys(0)
+
+    if not keys or #keys <= 0 then
+        -- xds did not write any data to shdict
+        return true, "no keys"
+    end
+
+    -- v1 version we only support route/upstream
+    local capacity = math_ceil(#keys / 2)
+
+    self.values = new_tab(capacity, 0)
+    self.values_hash = new_tab(0, capacity)
+
+    for _, key in ipairs(keys) do
+        if string.has_prefix(key, self.key) then
+            local data_valid = true
+            local conf_str = config:get(key, 0)
+            local conf, err = json.decode(conf_str)
+            if not conf then
+                data_valid = false
+                log.error("decode the conf of [", key, "] failed, err: ", err,
+                          ", conf_str: ", conf_str)
+            end
+
+            if not self.single_item and type(conf) ~= "table" then
+                data_valid = false
+                log.error("invalid conf of [", key, "], conf: ", conf,
+                          ", it should be an object")
+            end
+
+            if data_valid and self.item_schema then
+                local ok, err = check_schema(self.item_schema, conf)
+                if not ok then
+                    data_valid = false
+                    log.error("failed to check the conf of [", key, "] err:", err)
+                end
+            end
+
+            if data_valid and self.checker then
+                local ok, err = self.checker(conf)
+                if not ok then
+                    data_valid = false
+                    log.error("failed to check the conf of [", key, "] err:", err)
+                end
+            end
+
+            if data_valid then
+                if not conf.id then
+                    conf.id = sub_str(key, #self.key + 2, #key + 1)
+                    log.warn("the id of [", key, "] is nil, use the id: ", conf.id)
+                end
+
+                local conf_item = {value = conf, modifiedIndex = latest_version,
+                                   key = key}
+                insert_tab(self.values, conf_item)
+                self.values_hash[conf.id] = #self.values
+                conf_item.clean_handlers = {}
+
+                if self.filter then
+                    self.filter(conf_item)
+                end
+            end
+        end
+    end
+
+    self.conf_version = latest_version
+    return true
+end
+
+
+local function _automatic_fetch(premature, self)
+    if premature then
+        return
+    end
+
+    local i = 0
+    while not exiting() and self.running and i <= 32 do
+        i = i + 1
+        local ok, ok2, err = pcall(sync_data, self)
+        if not ok then
+            err = ok2
+            log.error("failed to fetch data from xds: ",
+                      err, ", ", tostring(self))
+            ngx_sleep(3)
+            break
+        elseif not ok2 and err then
+            -- todo: handler other error
+            if err ~= "wait for more time" and err ~= "no keys" and self.last_err ~= err then
+                log.error("failed to fetch data from xds, ", err, ", ", tostring(self))
+            end
+
+            if err ~= self.last_err then
+                self.last_err = err
+                self.last_err_time = ngx_time()
+            else
+                if ngx_time() - self.last_err_time >= 30 then
+                    self.last_err = nil
+                end
+            end
+            ngx_sleep(0.5)
+        elseif not ok2 then
+            ngx_sleep(0.05)
+        else
+            ngx_sleep(0.1)
+        end
+    end
+
+    if not exiting() and self.running then
+        ngx_timer_at(0, _automatic_fetch, self)
+    end
+end
+
+
+local function fetch_version(premature)
+    if premature then
+        return
+    end
+
+    local version = conf_ver:get("version")
+
+    if not version then
+        return
+    end
+
+    if version ~= latest_version then
+        latest_version = version
+    end
 end
 
 
+function _M.new(key, opts)
+    local automatic = opts and opts.automatic
+    local item_schema = opts and opts.item_schema
+    local filter_fun = opts and opts.filter
+    local single_item = opts and opts.single_item
+    local checker = opts and opts.checker
+
+
+    local obj = setmetatable({
+        automatic = automatic,
+        item_schema = item_schema,
+        checker = checker,
+        sync_times = 0,
+        running = true,
+        conf_version = 0,
+        values = nil,
+        routes_hash = nil,
+        prev_index = nil,
+        last_err = nil,
+        last_err_time = nil,
+        key = key,
+        single_item = single_item,
+        filter = filter_fun,
+    }, mt)
+
+    if automatic then
+        if not key then
+            return nil, "missing `key` argument"
+        end
+
+        -- blocking until xds completes initial configuration
+        while true do
+            fetch_version()

Review Comment:
   Need a sleep here?



##########
apisix/core/config_xds.lua:
##########
@@ -101,18 +128,254 @@ local function load_libxds(lib_name)
               table.concat(tried_paths, '\r\n', 1, #tried_paths))
     end
 
-    local route_zone = C.ngx_http_lua_ffi_shdict_udata_to_zone(route_config[1])
-    local route_shd_cdata = ffi.cast("void*", route_zone)
-    xdsagent.initial(route_shd_cdata)
+    local config_zone = C.ngx_http_lua_ffi_shdict_udata_to_zone(config[1])
+    local config_shd_cdata = ffi.cast("void*", config_zone)
+
+    local conf_ver_zone = C.ngx_http_lua_ffi_shdict_udata_to_zone(conf_ver[1])
+    local conf_ver_shd_cdata = ffi.cast("void*", conf_ver_zone)
+
+    xdsagent.initial(config_shd_cdata, conf_ver_shd_cdata)
+end
+
+
+
+local function sync_data(self)
+    if not latest_version then
+        return false, "wait for more time"
+    end
+
+    if self.conf_version == latest_version then
+        return true
+    end
+
+    if self.values then
+        for _, val in ipairs(self.values) do
+            if val and val.clean_handlers then
+                for _, clean_handler in ipairs(val.clean_handlers) do
+                    clean_handler(val)
+                end
+                val.clean_handlers = nil
+            end
+        end
+        self.values = nil
+        self.values_hash = nil
+    end
+
+    local keys = config:get_keys(0)
+
+    if not keys or #keys <= 0 then
+        -- xds did not write any data to shdict
+        return true, "no keys"

Review Comment:
   ```suggestion
           return false, "no keys"
   ```



##########
apisix/core/config_xds.lua:
##########
@@ -101,18 +128,254 @@ local function load_libxds(lib_name)
               table.concat(tried_paths, '\r\n', 1, #tried_paths))
     end
 
-    local route_zone = C.ngx_http_lua_ffi_shdict_udata_to_zone(route_config[1])
-    local route_shd_cdata = ffi.cast("void*", route_zone)
-    xdsagent.initial(route_shd_cdata)
+    local config_zone = C.ngx_http_lua_ffi_shdict_udata_to_zone(config[1])
+    local config_shd_cdata = ffi.cast("void*", config_zone)
+
+    local conf_ver_zone = C.ngx_http_lua_ffi_shdict_udata_to_zone(conf_ver[1])
+    local conf_ver_shd_cdata = ffi.cast("void*", conf_ver_zone)
+
+    xdsagent.initial(config_shd_cdata, conf_ver_shd_cdata)
+end
+
+
+
+local function sync_data(self)
+    if not latest_version then
+        return false, "wait for more time"
+    end
+
+    if self.conf_version == latest_version then
+        return true
+    end
+
+    if self.values then
+        for _, val in ipairs(self.values) do
+            if val and val.clean_handlers then
+                for _, clean_handler in ipairs(val.clean_handlers) do
+                    clean_handler(val)
+                end
+                val.clean_handlers = nil
+            end
+        end
+        self.values = nil
+        self.values_hash = nil
+    end
+
+    local keys = config:get_keys(0)
+
+    if not keys or #keys <= 0 then
+        -- xds did not write any data to shdict
+        return true, "no keys"
+    end
+
+    -- v1 version we only support route/upstream
+    local capacity = math_ceil(#keys / 2)

Review Comment:
   Why divide 2?



##########
t/xds-library/config_xds_2.t:
##########
@@ -76,3 +76,166 @@ __DATA__
     }
 --- response_body
 hello world
+
+
+
+=== TEST 2: proxy request using data written by xds(id = 2, upstream_id = 1)
+--- config
+    location /t {
+        content_by_lua_block {
+            local http = require "resty.http"
+            local httpc = http.new()
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello1"
+            local res, err = httpc:request_uri(uri, { method = "GET"})
+
+            if not res then
+                ngx.say(err)
+                return
+            end
+            ngx.print(res.body)
+        }
+    }
+--- response_body
+hello1 world
+
+
+
+=== TEST 3: proxy request using data written by xds(id = 3, upstream_id = 2)
+--- config
+    location /t {
+        content_by_lua_block {
+            ngx.sleep(1.5)
+            local core = require("apisix.core")
+            local value = ngx.shared["xds-config"]:flush_all()
+            ngx.sleep(1.5)
+            local http = require "resty.http"
+            local httpc = http.new()
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
+            local res, err = httpc:request_uri(uri, { method = "GET"})
+
+            if not res then
+                ngx.say(err)
+                return
+            end
+            ngx.print(res.body)
+        }
+    }
+--- response_body
+hello world
+
+
+
+=== TEST 4: flush all keys in xds config
+--- config
+    location /t {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            ngx.shared["xds-config"]:flush_all()
+            ngx.update_time()
+            ngx.shared["xds-conf-version"]:set("version", ngx.now())
+            ngx.sleep(1.5)
+
+            local http = require "resty.http"
+            local httpc = http.new()
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/hello"
+            local res, err = httpc:request_uri(uri, { method = "GET"})
+
+            if not res then
+                ngx.say(err)
+                return
+            end
+            ngx.status = res.status
+            ngx.print(res.body)
+        }
+    }
+--- error_code: 404
+--- response_body
+{"error_msg":"404 Route Not Found"}
+
+
+
+=== TEST 5: bad format json
+--- config
+    location /t {
+        content_by_lua_block {
+            local data = [[{
+                upstream = {
+                    type = "roundrobin"
+                    nodes = {
+                        ["127.0.0.1:1980"] = 1,
+                    }
+                },
+                uri = "/bad_json"
+            }]]
+            ngx.shared["xds-config"]:set("/routes/3", data)
+            ngx.update_time()
+            ngx.shared["xds-conf-version"]:set("version", ngx.now())
+            ngx.sleep(1.5)
+
+            local http = require "resty.http"
+            local httpc = http.new()
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/bad_json"
+            local res, err = httpc:request_uri(uri, { method = "GET"})
+
+            if not res then
+                ngx.say(err)
+                return
+            end
+            ngx.status = res.status
+        }
+    }
+--- error_code: 404
+--- no_error_log
+[alert]

Review Comment:
   We can safely remove it, right?



-- 
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.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org