forked from rrfeng/lua-resty-upstream-etcd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpicker.lua
More file actions
228 lines (182 loc) · 5.04 KB
/
picker.lua
File metadata and controls
228 lines (182 loc) · 5.04 KB
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
local _M = {}
local json = require "cjson"
local ngx_timer_at = ngx.timer.at
local ngx_log = ngx.log
local ngx_ERR = ngx.ERR
local ngx_time = ngx.time
_M.ready = false
_M.black_hole = {ip="127.0.0.1", port=2222, weight=0}
_M.data = {}
local function log(c)
ngx_log(ngx_ERR, c)
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
function _M.init(shm)
_M.storage = shm
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
log("Error start slowStart: " .. err)
peers[i].cfg_weight = peer.weight
end
end
local function update(name)
-- if the version is same, no update
local ver = _M.storage:get(name .. "|version")
if _M.data[name] and _M.data[name].version == ver then
return nil
end
local ver = _M.storage:get(name .. "|version")
local data = _M.storage:get(name .. "|peers")
if not ver and not data then
_M.data[name] = nil
end
local ok, value = pcall(json.decode, data)
if not ok or type(value) ~= "table" then
return "data format error"
end
if not _M.data[name] then
_M.data[name] = {}
end
_M.data[name].peers = value
_M.data[name].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
local ok, err = ngx_timer_at(0, slowStart, name, peers[i], 1)
if not ok then
log("Error start slowStart: " .. err)
end
end
end
end
return nil
end
function _M.rr(name)
-- 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 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 == "down" then
goto continue
end
peers[i].run_weight = peers[i].run_weight + peers[i].cfg_weight
total = total + peers[i].cfg_weight
if pick == nil or pick.run_weight < peers[i].run_weight then
pick = peers[i]
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
pick = peers[1]
end
if pick then
pick.run_weight = pick.run_weight - total
end
return pick
end
function _M.show(name)
if not name then
return {}
end
return _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