-
Notifications
You must be signed in to change notification settings - Fork 37
/
picker.lua
300 lines (251 loc) · 7.25 KB
/
picker.lua
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
local _M = {}
local json = require "cjson"
local log = ngx.log
local ERR = ngx.ERR
local WARN = ngx.WARN
local INFO = ngx.INFO
local ngx_timer_at = ngx.timer.at
local ngx_time = ngx.time
_M.black_hole = {ip="127.0.0.1", port=2222, weight=0}
_M.data = {}
local function info(...)
log(INFO, "picker: ", ...)
end
local function warn(...)
log(WARN, "picker: ", ...)
end
local function errlog(...)
log(ERR, "picker: ", ...)
end
local function indexOf(t, e)
for i=1,#t do
if t[i].host == e.host and t[i].port == e.port then
return i
end
end
return nil
end
-- init(shm1, shm2, merge, hcshm)
-- if merge==true, all peers used in shm1 and shm2
-- if merge==false, peers in shm1 is prefered, if no peers found, use shm2
-- hcshm used to work with healthcheck, if not set, use shm1
function _M.init(...)
_M.storage = {}
_M.storage[1] = select(1, ...)
_M.storage[2] = select(2, ...)
_M.merge = select(3, ...) == true or false
_M.hcshm = select(4, ...) or _M.storage[1]
return
end
local function slowStart(premature, name, peer, t)
if premature then return end
if t < 1 then t = 1 end
local peers = _M.data[name].peers
-- we must confirm the index every time
-- if a peer disappear, index will change
local i = indexOf(peers, peer)
if not i then
return
end
local times = peers[i].slow_start
if t > times then
return
end
if t == times then
peers[i].cfg_weight = peer.weight
return
end
peers[i].cfg_weight = peer.weight * t / times
local ok, err = ngx_timer_at(1, slowStart, name, peer, t+1)
if not ok then
errlog("Error start slowStart: " .. err)
peers[i].cfg_weight = peer.weight
end
end
local function getVersion(name)
if _M.merge then
return tostring(_M.storage[1]:get(name .. "|version")) ..
tostring(_M.storage[2]:get(name .. "|version")), 1
else
local index = 1
local ver = _M.storage[1]:get(name .. "|version")
if not ver and _M.storage[2] then
ver = _M.storage[2]:get(name .. "|version")
index = 2
end
return ver, index
end
end
local function decode(pstr)
local ok, peers = pcall(json.decode, pstr)
if not ok or type(peers) ~= "table" then
errlog("get peers from shm format error:", pstr)
peers = {}
end
return peers
end
local function getPeers(name, index)
local pstr = _M.storage[index]:get(name .. "|peers")
local peers = decode(pstr)
if _M.merge then
pstr = _M.storage[3-index]:get(name .. "|peers")
if #peers == 0 then
peers = decode(pstr)
else
for i, p in ipairs(decode(pstr)) do
table.insert(peers, p)
end
end
end
return peers
end
local function update(name)
-- if the upstream version is same, do not update
local ver, index = getVersion(name)
if not ver then
_M.data[name] = nil
warn("cannot get version from shm, delete upstream: ", name)
return
end
if _M.data[name] and _M.data[name].version == ver then
return
end
local peers = getPeers(name, index)
if not peers then
_M.data[name] = nil
warn("cannot get peers from shm, delete upstream:", name)
return
end
_M.data[name] = { peers = peers, version = ver }
-- Check if there is a new peer that needs slow start.
local peers = _M.data[name].peers
if #peers <= 1 then
return
end
local now = ngx_time()
for i=1,#peers do
if peers[i].slow_start > 0 then
if peers[i].start_at and now - peers[i].start_at < 5 then
peers[i].weight = 0
local ok, err = ngx_timer_at(0, slowStart, name, peers[i], 1)
if not ok then
errlog("Error start slowStart: " .. err)
end
end
end
end
return nil
end
local function ischeckdown(name, host, port)
return _M.hcshm:get("checkdown:" .. name .. ":" .. host .. ":" .. port)
end
function _M.rr(name, ban_peer)
-- before pick check update
update(name)
if not _M.data[name] then
return nil
end
-- start to pick a peer
local peers = _M.data[name].peers
local total = 0
local pick = nil
for i=1,#peers do
if ban_peer and #peers > 1 then
if peers[i].host == ban_peer.host and peers[i].port == ban_peer.port then
goto continue
end
end
-- If no weight set, the default is 1.
if peers[i].cfg_weight == nil then
peers[i].cfg_weight = peers[i].weight or 1
end
if peers[i].run_weight == nil then
peers[i].run_weight = 0
end
if peers[i].cfg_weight == 0 then
goto continue
end
if peers[i].status ~= "up" then
goto continue
end
if peers[i].checkdown then
if ischeckdown(name, peers[i].host, peers[i].port) then
goto continue
else
peers[i].checkdown = false
end
end
peers[i].run_weight = peers[i].run_weight + peers[i].cfg_weight
total = total + peers[i].cfg_weight
if not peers[i].checkdown then
if pick == nil or pick.run_weight < peers[i].run_weight then
pick = peers[i]
end
end
::continue::
end
-- if all peers cfg_weight is 0, then reset.
if not pick and total == 0 then
for i=1,#peers do
peers[i].cfg_weight = peers[i].weight or 1
end
end
if pick then
pick.run_weight = pick.run_weight - total
pick.checkdown = ischeckdown(name, pick.host, pick.port)
end
return pick
end
function _M.show(name)
if not name then
return "{}"
end
return json.encode(_M.data[name])
end
function _M.cutdown(name, peer, percent)
local i = indexOf(_M.data[name], peer)
if not i then
return 'peer not exists'
end
_M.data[name].peers[i].cfg_weight = _M.data[name].peers[i].cfg_weight * percent
return nil
end
function _M.recover(name, peer)
local i = indexOf(_M.data[name], peer)
if not i then
return 'peer not exists'
end
_M.data[name].peers[i].cfg_weight = _M.data[name].peers[i].weight
return nil
end
function _M.setBlackHole(name, percent)
if percent >= 1 or percent < 0 then
return "not permitted"
end
local peers = _M.data[name].peers
local index = indexOf(peers, _M.black_hole)
if percent == 0 then
if index then
table.remove(_M.data[name].peers, index)
return nil
else
return nil
end
else
local total_weight = 0
for i=1,#peers do
total_weight = total_weight + peers[i].cfg_weight
end
local black_weight = total_weight / percent - total_weight
if index then
_M.data[name].peers[index].cfg_weight = black_weight
return nil
else
local black_peer = _M.black_hole
black_peer.cfg_weight = black_weight
table.insert(_M.data[name].peers, black_peer)
return nil
end
end
end
return _M