1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
-- upstream_manager_v2.lua
-- 多租户动态 upstream 管理模块
-- 支持多个 namespace,每个网站可以有独立的后端节点池
local _M = {}
local cjson = require "cjson"
-- 共享内存
local upstream_nodes = ngx.shared.upstream_nodes
local healthcheck = ngx.shared.healthcheck
-- 常量
local DEFAULT_NS = "default"
local HEALTH_CHECK_PATH = "/api/status"
local HEALTH_CHECK_TIMEOUT = 2000 -- 2秒
-- 获取 namespace 的 key
local function get_ns_key(ns)
return "ns:" .. (ns or DEFAULT_NS)
end
-- 初始化
function _M.init()
ngx.log(ngx.INFO, "upstream_manager_v2 initialized (multi-tenant mode)")
end
-- 获取指定 namespace 的所有节点
function _M.get_all_nodes(ns)
local ns_key = get_ns_key(ns)
local nodes_json = upstream_nodes:get(ns_key)
if not nodes_json then
return {}
end
local ok, nodes = pcall(cjson.decode, nodes_json)
if not ok then
ngx.log(ngx.ERR, "failed to decode nodes for ", ns_key, ": ", nodes)
return {}
end
-- 添加健康状态
for _, node in ipairs(nodes) do
local key = ns_key .. ":" .. node.host .. ":" .. node.port
local health = healthcheck:get(key)
node.healthy = (health ~= "unhealthy")
end
return nodes
end
-- 获取所有 namespace 及其节点
function _M.get_all_namespaces()
local keys = upstream_nodes:get_keys(100) -- 最多获取 100 个
local result = {}
for _, key in ipairs(keys) do
if key:sub(1, 3) == "ns:" then
local ns = key:sub(4)
result[ns] = _M.get_all_nodes(ns)
end
end
return result
end
-- 保存节点列表
local function save_nodes(ns, nodes)
local ns_key = get_ns_key(ns)
local ok, json = pcall(cjson.encode, nodes)
if not ok then
return false, "failed to encode nodes"
end
local ok, err = upstream_nodes:set(ns_key, json)
if not ok then
return false, err
end
return true
end
-- 注册节点
function _M.register(ns, host, port, weight, health_path)
local nodes = _M.get_all_nodes(ns)
-- 检查是否已存在
for i, node in ipairs(nodes) do
if node.host == host and node.port == port then
-- 更新
nodes[i].weight = weight
nodes[i].health_path = health_path or HEALTH_CHECK_PATH
nodes[i].updated_at = ngx.time()
return save_nodes(ns, nodes)
end
end
-- 添加新节点
table.insert(nodes, {
host = host,
port = port,
weight = weight,
health_path = health_path or HEALTH_CHECK_PATH,
created_at = ngx.time(),
updated_at = ngx.time()
})
ngx.log(ngx.INFO, "registered node: ns=", ns or DEFAULT_NS, " ", host, ":", port, " weight=", weight)
return save_nodes(ns, nodes)
end
-- 注销节点
function _M.deregister(ns, host, port)
local nodes = _M.get_all_nodes(ns)
local new_nodes = {}
local found = false
for _, node in ipairs(nodes) do
if node.host == host and node.port == port then
found = true
ngx.log(ngx.INFO, "deregistered node: ns=", ns or DEFAULT_NS, " ", host, ":", port)
else
table.insert(new_nodes, node)
end
end
if not found then
return false, "node not found"
end
return save_nodes(ns, new_nodes)
end
-- 设置权重
function _M.set_weight(ns, host, port, weight)
local nodes = _M.get_all_nodes(ns)
for i, node in ipairs(nodes) do
if node.host == host and node.port == port then
nodes[i].weight = weight
nodes[i].updated_at = ngx.time()
ngx.log(ngx.INFO, "updated weight: ns=", ns or DEFAULT_NS, " ", host, ":", port, " -> ", weight)
return save_nodes(ns, nodes)
end
end
return false, "node not found"
end
-- 加权随机选择算法
local function weighted_random_select(ns, nodes)
local ns_key = get_ns_key(ns)
local total_weight = 0
local available_nodes = {}
-- 只选择健康且权重大于0的节点
for _, node in ipairs(nodes) do
local key = ns_key .. ":" .. node.host .. ":" .. node.port
local health = healthcheck:get(key)
if health ~= "unhealthy" and node.weight > 0 then
total_weight = total_weight + node.weight
table.insert(available_nodes, node)
end
end
if #available_nodes == 0 then
return nil
end
if #available_nodes == 1 then
return available_nodes[1]
end
-- 加权随机选择
local rand = math.random(1, total_weight)
local cumulative = 0
for _, node in ipairs(available_nodes) do
cumulative = cumulative + node.weight
if rand <= cumulative then
return node
end
end
return available_nodes[1]
end
-- 获取后端地址(指定 namespace)
function _M.get_backend(ns)
local nodes = _M.get_all_nodes(ns)
if #nodes == 0 then
ngx.log(ngx.WARN, "no nodes available for ns=", ns or DEFAULT_NS)
return nil
end
local node = weighted_random_select(ns, nodes)
if not node then
ngx.log(ngx.WARN, "no healthy nodes available for ns=", ns or DEFAULT_NS)
return nil
end
return node.host .. ":" .. node.port
end
-- 健康检查(检查所有 namespace 的节点)
function _M.health_check()
local all_ns = _M.get_all_namespaces()
for ns, nodes in pairs(all_ns) do
local ns_key = get_ns_key(ns)
for _, node in ipairs(nodes) do
local key = ns_key .. ":" .. node.host .. ":" .. node.port
local health_path = node.health_path or HEALTH_CHECK_PATH
local sock = ngx.socket.tcp()
sock:settimeout(HEALTH_CHECK_TIMEOUT)
local ok, err = sock:connect(node.host, node.port)
if ok then
local req = "GET " .. health_path .. " HTTP/1.0\r\nHost: " .. node.host .. ":" .. node.port .. "\r\n\r\n"
sock:send(req)
local line, err = sock:receive("*l")
if line and line:match("200") then
healthcheck:set(key, "healthy", 30)
ngx.log(ngx.DEBUG, "health check passed: ", key)
else
healthcheck:set(key, "unhealthy", 30)
ngx.log(ngx.WARN, "health check failed: ", key, " - ", err or "non-200")
end
sock:close()
else
healthcheck:set(key, "unhealthy", 30)
ngx.log(ngx.WARN, "health check failed: ", key, " - ", err)
end
end
end
end
-- 获取健康状态
function _M.get_health_status(ns)
local nodes
if ns then
nodes = _M.get_all_nodes(ns)
else
-- 返回所有 namespace 的状态
return _M.get_all_namespaces()
end
local ns_key = get_ns_key(ns)
local result = {
namespace = ns or DEFAULT_NS,
total = #nodes,
healthy = 0,
unhealthy = 0,
nodes = {}
}
for _, node in ipairs(nodes) do
local key = ns_key .. ":" .. node.host .. ":" .. node.port
local health = healthcheck:get(key)
local is_healthy = (health ~= "unhealthy")
if is_healthy then
result.healthy = result.healthy + 1
else
result.unhealthy = result.unhealthy + 1
end
table.insert(result.nodes, {
address = node.host .. ":" .. node.port,
weight = node.weight,
healthy = is_healthy,
health_path = node.health_path
})
end
return result
end
return _M
|