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/03/15 11:22:30 UTC

[GitHub] [apisix] membphis commented on a change in pull request #6614: feat: support reading configuration form shdict(mvp)

membphis commented on a change in pull request #6614:
URL: https://github.com/apache/apisix/pull/6614#discussion_r826632359



##########
File path: apisix/core/config_shdict.lua
##########
@@ -0,0 +1,126 @@
+--
+-- 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.
+--
+
+--- Get configuration form ngx.shared.DICT.
+--
+-- @module core.config_shdict
+
+local base              = require("resty.core.base")
+local config_local      = require("apisix.core.config_local")
+local table             = table
+local is_http           = ngx.config.subsystem == "http"
+local string            = string
+local io                = io
+local package           = package
+local new_tab           = base.new_tab
+local ngx_timer_at      = ngx.timer.at
+local ffi               = require 'ffi'
+local C                 = ffi.C
+local router_config     = ngx.shared["router-config"]
+
+local process
+if is_http then
+    process = require("ngx.process")
+end
+
+
+ffi.cdef[[
+extern void CreateMock(void* writeRoute);

Review comment:
       change it to standard C style: `create_mock `

##########
File path: apisix/core/config_shdict.lua
##########
@@ -0,0 +1,126 @@
+--
+-- 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.
+--
+
+--- Get configuration form ngx.shared.DICT.
+--
+-- @module core.config_shdict
+
+local base              = require("resty.core.base")
+local config_local      = require("apisix.core.config_local")
+local table             = table
+local is_http           = ngx.config.subsystem == "http"
+local string            = string
+local io                = io
+local package           = package
+local new_tab           = base.new_tab
+local ngx_timer_at      = ngx.timer.at
+local ffi               = require 'ffi'
+local C                 = ffi.C
+local router_config     = ngx.shared["router-config"]
+
+local process
+if is_http then
+    process = require("ngx.process")
+end
+
+
+ffi.cdef[[
+extern void CreateMock(void* writeRoute);

Review comment:
       we should call a `init()` function here

##########
File path: apisix/core/config_shdict.lua
##########
@@ -0,0 +1,126 @@
+--
+-- 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.
+--
+
+--- Get configuration form ngx.shared.DICT.
+--
+-- @module core.config_shdict
+
+local base              = require("resty.core.base")
+local config_local      = require("apisix.core.config_local")
+local table             = table
+local is_http           = ngx.config.subsystem == "http"
+local string            = string
+local io                = io
+local package           = package
+local new_tab           = base.new_tab
+local ngx_timer_at      = ngx.timer.at
+local ffi               = require 'ffi'

Review comment:
       bad style, change to : `require("ffi")`

##########
File path: t/amesh-agent/config_shdict.t
##########
@@ -0,0 +1,84 @@
+#
+# 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';
+
+use Cwd qw(cwd);
+my $apisix_home = $ENV{APISIX_HOME} || cwd();
+
+repeat_each(1);
+no_long_string();
+no_root_location();
+log_level("info");
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if (!$block->request) {
+        $block->set_value("request", "GET /t");
+    }
+
+    if (!$block->no_error_log) {
+        $block->set_value("no_error_log", "[error]\n[alert]");
+    }
+
+    my $lua_deps_path = $block->lua_deps_path // <<_EOC_;
+        lua_package_path "$apisix_home/?.lua;$apisix_home/?/init.lua;$apisix_home/deps/share/lua/5.1/?/init.lua;$apisix_home/deps/share/lua/5.1/?.lua;$apisix_home/apisix/?.lua;$apisix_home/t/?.lua;;";
+        lua_package_cpath "$apisix_home/?.so;$apisix_home/t/amesh-agent/?.so;$apisix_home/deps/lib/lua/5.1/?.so;$apisix_home/deps/lib64/lua/5.1/?.so;;";
+_EOC_
+
+    $block->set_value("lua_deps_path", $lua_deps_path);
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: load amesh agent so successfully
+--- yaml_config
+apisix:
+  node_listen: 1984
+  config_center: shdict
+  enable_admin: false
+--- config
+    location /t {
+        content_by_lua_block {
+            ngx.say("ok")
+        }
+    }
+--- no_error_log eval
+qr/can not load amesh agent/

Review comment:
       it should be a `Amesh library`

##########
File path: apisix/core/config_shdict.lua
##########
@@ -0,0 +1,126 @@
+--
+-- 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.
+--
+
+--- Get configuration form ngx.shared.DICT.
+--
+-- @module core.config_shdict
+
+local base              = require("resty.core.base")
+local config_local      = require("apisix.core.config_local")
+local table             = table
+local is_http           = ngx.config.subsystem == "http"
+local string            = string
+local io                = io
+local package           = package
+local new_tab           = base.new_tab
+local ngx_timer_at      = ngx.timer.at
+local ffi               = require 'ffi'
+local C                 = ffi.C
+local router_config     = ngx.shared["router-config"]
+
+local process
+if is_http then
+    process = require("ngx.process")
+end
+
+
+ffi.cdef[[
+extern void CreateMock(void* writeRoute);
+]]
+
+
+local _M = {
+    version = 0.1,
+    local_conf = config_local.local_conf,
+}
+
+
+-- todo: refactor this function in chash.lua and radixtree.lua
+local function load_shared_lib(lib_name)
+    local string_gmatch = string.gmatch
+    local string_match = string.match
+    local io_open = io.open
+    local io_close = io.close
+
+    local cpath = package.cpath
+    local tried_paths = new_tab(32, 0)
+    local i = 1
+
+    for k, _ in string_gmatch(cpath, "[^;]+") do
+        local fpath = string_match(k, "(.*/)")
+        fpath = fpath .. lib_name
+
+        local f = io_open(fpath)
+        if f ~= nil then
+            io_close(f)
+            return ffi.load(fpath)
+        end
+        tried_paths[i] = fpath
+        i = i + 1
+    end
+
+    return nil, tried_paths
+end
+
+
+local function load_ameshagent(lib_name)
+    ngx_timer_at(0, function(premature)
+        if premature then
+            return
+        end
+
+        local ameshagent, tried_paths = load_shared_lib(lib_name)
+
+        if not ameshagent then
+            tried_paths[#tried_paths + 1] = 'tried above paths but can not load '
+                    .. lib_name
+            error("can not load amesh agent, tried paths: " ..
+                    table.concat(tried_paths, '\r\n', 1, #tried_paths))
+        end
+
+        local router_zone = C.ngx_http_lua_ffi_shdict_udata_to_zone(router_config[1])

Review comment:
       why do we need to call this function?

##########
File path: apisix/core/config_shdict.lua
##########
@@ -0,0 +1,126 @@
+--
+-- 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.
+--
+
+--- Get configuration form ngx.shared.DICT.
+--
+-- @module core.config_shdict
+
+local base              = require("resty.core.base")
+local config_local      = require("apisix.core.config_local")
+local table             = table
+local is_http           = ngx.config.subsystem == "http"
+local string            = string
+local io                = io
+local package           = package
+local new_tab           = base.new_tab
+local ngx_timer_at      = ngx.timer.at
+local ffi               = require 'ffi'
+local C                 = ffi.C
+local router_config     = ngx.shared["router-config"]
+
+local process
+if is_http then
+    process = require("ngx.process")
+end
+
+
+ffi.cdef[[
+extern void CreateMock(void* writeRoute);
+]]
+
+
+local _M = {
+    version = 0.1,
+    local_conf = config_local.local_conf,
+}
+
+
+-- todo: refactor this function in chash.lua and radixtree.lua
+local function load_shared_lib(lib_name)
+    local string_gmatch = string.gmatch
+    local string_match = string.match
+    local io_open = io.open
+    local io_close = io.close
+
+    local cpath = package.cpath
+    local tried_paths = new_tab(32, 0)
+    local i = 1
+
+    for k, _ in string_gmatch(cpath, "[^;]+") do
+        local fpath = string_match(k, "(.*/)")
+        fpath = fpath .. lib_name
+
+        local f = io_open(fpath)
+        if f ~= nil then
+            io_close(f)
+            return ffi.load(fpath)
+        end
+        tried_paths[i] = fpath
+        i = i + 1
+    end
+
+    return nil, tried_paths
+end
+
+
+local function load_ameshagent(lib_name)
+    ngx_timer_at(0, function(premature)
+        if premature then
+            return
+        end
+
+        local ameshagent, tried_paths = load_shared_lib(lib_name)
+
+        if not ameshagent then
+            tried_paths[#tried_paths + 1] = 'tried above paths but can not load '
+                    .. lib_name
+            error("can not load amesh agent, tried paths: " ..
+                    table.concat(tried_paths, '\r\n', 1, #tried_paths))
+        end
+
+        local router_zone = C.ngx_http_lua_ffi_shdict_udata_to_zone(router_config[1])
+        local router_shd_cdata = ffi.cast("void*", router_zone)
+        ameshagent.CreateMock(router_shd_cdata)
+    end)
+end
+
+
+
+function _M.init_worker()
+    local lib_name = "libameshagent"

Review comment:
       `libamesh` is fine




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