You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by me...@apache.org on 2020/06/19 04:46:28 UTC
[incubator-apisix] branch master updated: feature: support the use of independent files to implement the load a… (#1732)
This is an automated email from the ASF dual-hosted git repository.
membphis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-apisix.git
The following commit(s) were added to refs/heads/master by this push:
new 7307797 feature: support the use of independent files to implement the load a… (#1732)
7307797 is described below
commit 7307797f0e843aa17ac4604748287f707abadc7c
Author: YuanSheng Wang <me...@gmail.com>
AuthorDate: Fri Jun 19 12:46:20 2020 +0800
feature: support the use of independent files to implement the load a… (#1732)
* feature: support the use of independent files to implement the load algorithm,
which is convenient for expanding different algorithms in the future.
---
Makefile | 3 ++
apisix/balancer.lua | 73 ++++----------------------------------
apisix/balancer/chash.lua | 80 ++++++++++++++++++++++++++++++++++++++++++
apisix/balancer/roundrobin.lua | 34 ++++++++++++++++++
4 files changed, 124 insertions(+), 66 deletions(-)
diff --git a/Makefile b/Makefile
index ec2771b..5836e36 100644
--- a/Makefile
+++ b/Makefile
@@ -128,6 +128,9 @@ install: default
$(INSTALL) -d $(INST_LUADIR)/apisix/admin
$(INSTALL) apisix/admin/*.lua $(INST_LUADIR)/apisix/admin/
+ $(INSTALL) -d $(INST_LUADIR)/apisix/balancer
+ $(INSTALL) apisix/balancer/*.lua $(INST_LUADIR)/apisix/balancer/
+
$(INSTALL) -d $(INST_LUADIR)/apisix/core
$(INSTALL) apisix/core/*.lua $(INST_LUADIR)/apisix/core/
diff --git a/apisix/balancer.lua b/apisix/balancer.lua
index ce89b61..9ba7185 100644
--- a/apisix/balancer.lua
+++ b/apisix/balancer.lua
@@ -16,14 +16,10 @@
--
local healthcheck
local require = require
-local roundrobin = require("resty.roundrobin")
local discovery = require("apisix.discovery.init").discovery
-local resty_chash = require("resty.chash")
local balancer = require("ngx.balancer")
local core = require("apisix.core")
local error = error
-local str_char = string.char
-local str_gsub = string.gsub
local pairs = pairs
local ipairs = ipairs
local tostring = tostring
@@ -35,6 +31,10 @@ local upstreams_etcd
local module_name = "balancer"
+local pickers = {
+ roundrobin = require("apisix.balancer.roundrobin"),
+ chash = require("apisix.balancer.chash"),
+}
local lrucache_server_picker = core.lrucache.new({
@@ -134,72 +134,13 @@ local function fetch_healthchecker(upstream, healthcheck_parent, version)
end
-local function fetch_chash_hash_key(ctx, upstream)
- local key = upstream.key
- local hash_on = upstream.hash_on or "vars"
- local chash_key
-
- if hash_on == "consumer" then
- chash_key = ctx.consumer_id
- elseif hash_on == "vars" then
- chash_key = ctx.var[key]
- elseif hash_on == "header" then
- chash_key = ctx.var["http_" .. key]
- elseif hash_on == "cookie" then
- chash_key = ctx.var["cookie_" .. key]
- end
-
- if not chash_key then
- chash_key = ctx.var["remote_addr"]
- core.log.warn("chash_key fetch is nil, use default chash_key ",
- "remote_addr: ", chash_key)
- end
- core.log.info("upstream key: ", key)
- core.log.info("hash_on: ", hash_on)
- core.log.info("chash_key: ", core.json.delay_encode(chash_key))
-
- return chash_key
-end
-
-
local function create_server_picker(upstream, checker)
- if upstream.type == "roundrobin" then
- local up_nodes = fetch_health_nodes(upstream, checker)
- core.log.info("upstream nodes: ", core.json.delay_encode(up_nodes))
-
- local picker = roundrobin:new(up_nodes)
- return {
- upstream = upstream,
- get = function ()
- return picker:find()
- end
- }
- end
-
- if upstream.type == "chash" then
+ local picker = pickers[upstream.type]
+ if picker then
local up_nodes = fetch_health_nodes(upstream, checker)
core.log.info("upstream nodes: ", core.json.delay_encode(up_nodes))
- local str_null = str_char(0)
-
- local servers, nodes = {}, {}
- for serv, weight in pairs(up_nodes) do
- local id = str_gsub(serv, ":", str_null)
-
- servers[id] = serv
- nodes[id] = weight
- end
-
- local picker = resty_chash:new(nodes)
- return {
- upstream = upstream,
- get = function (ctx)
- local chash_key = fetch_chash_hash_key(ctx, upstream)
- local id = picker:find(chash_key)
- -- core.log.warn("chash id: ", id, " val: ", servers[id])
- return servers[id]
- end
- }
+ return picker.new(up_nodes, upstream)
end
return nil, "invalid balancer type: " .. upstream.type, 0
diff --git a/apisix/balancer/chash.lua b/apisix/balancer/chash.lua
new file mode 100644
index 0000000..38831cd
--- /dev/null
+++ b/apisix/balancer/chash.lua
@@ -0,0 +1,80 @@
+--
+-- 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 core = require("apisix.core")
+local resty_chash = require("resty.chash")
+local str_char = string.char
+local str_gsub = string.gsub
+local pairs = pairs
+
+
+local _M = {}
+
+
+local function fetch_chash_hash_key(ctx, upstream)
+ local key = upstream.key
+ local hash_on = upstream.hash_on or "vars"
+ local chash_key
+
+ if hash_on == "consumer" then
+ chash_key = ctx.consumer_id
+ elseif hash_on == "vars" then
+ chash_key = ctx.var[key]
+ elseif hash_on == "header" then
+ chash_key = ctx.var["http_" .. key]
+ elseif hash_on == "cookie" then
+ chash_key = ctx.var["cookie_" .. key]
+ end
+
+ if not chash_key then
+ chash_key = ctx.var["remote_addr"]
+ core.log.warn("chash_key fetch is nil, use default chash_key ",
+ "remote_addr: ", chash_key)
+ end
+ core.log.info("upstream key: ", key)
+ core.log.info("hash_on: ", hash_on)
+ core.log.info("chash_key: ", core.json.delay_encode(chash_key))
+
+ return chash_key
+end
+
+
+function _M.new(up_nodes, upstream)
+ local str_null = str_char(0)
+
+ local servers, nodes = {}, {}
+ for serv, weight in pairs(up_nodes) do
+ local id = str_gsub(serv, ":", str_null)
+
+ servers[id] = serv
+ nodes[id] = weight
+ end
+
+ local picker = resty_chash:new(nodes)
+ return {
+ upstream = upstream,
+ get = function (ctx)
+ local chash_key = fetch_chash_hash_key(ctx, upstream)
+ local id = picker:find(chash_key)
+ -- core.log.warn("chash id: ", id, " val: ", servers[id])
+ return servers[id]
+ end
+ }
+end
+
+
+return _M
diff --git a/apisix/balancer/roundrobin.lua b/apisix/balancer/roundrobin.lua
new file mode 100644
index 0000000..dac4f03
--- /dev/null
+++ b/apisix/balancer/roundrobin.lua
@@ -0,0 +1,34 @@
+--
+-- 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 roundrobin = require("resty.roundrobin")
+
+local _M = {}
+
+
+function _M.new(up_nodes, upstream)
+ local picker = roundrobin:new(up_nodes)
+ return {
+ upstream = upstream,
+ get = function ()
+ return picker:find()
+ end
+ }
+end
+
+
+return _M