-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqstable.lua
More file actions
342 lines (304 loc) · 9.01 KB
/
Copy pathqstable.lua
File metadata and controls
342 lines (304 loc) · 9.01 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
local c = require "qstable.core"
local md5 = require "md5"
local setmetatable = setmetatable
local rawset = rawset
local pairs = pairs
local select = select
local assert = assert
local tonumber = tonumber
local c_index = c.index
local c_len = c.len
local c_next = c.next
local cache_config = {}
local config_hash = {}
local meta = {}
local weak_meta = {__mode = "v"}
local pid
local shm_key_serial = 1
local _M = {}
local function wrap_root(name, ud)
return setmetatable({__ud = assert(ud), __name = name}, meta)
end
local function wrap_table(ud)
return setmetatable({__ud = assert(ud)}, meta)
end
local function qstable_next(t, k)
local nextk,nextv,nextud = c_next(t.__ud, k)
if nextud then
return nextk,t[nextk]
else
return nextk,nextv
end
end
meta.__index = function(t, k)
local v,udata= c_index(t.__ud, k)
if udata then
local wrap_tablevalue = wrap_table(udata)
rawset(t, k, wrap_tablevalue)
return wrap_tablevalue
else
return v
end
end
meta.__newindex = function(t, k, v)
error("Cannot modify a read-only table")
end
meta.__len = function(t)
return c_len(t.__ud)
end
meta.__pairs = function(t)
return qstable_next,t,nil
end
meta.__ipairs = function(t)
return qstable_next,t,nil
end
for k,v in pairs(meta) do
weak_meta[k] = v
end
local function calc_hash(conf)
assert(conf)
local str = c.tostring(conf)
local h = md5.sumhexa(str)
return h
end
function _M.reload()
local ud_list = c.reload()
for name,ud in pairs(ud_list) do
local cache = cache_config[name]
if not cache or cache.__ud ~= ud then
cache_config[name] = wrap_root(name, ud)
local info = c.get_info(c.get_conf(ud))
config_hash[name] = info.hash
end
end
end
function _M.find(name, ...)
local conf = cache_config[name]
if not conf then
return
end
local val = conf
local n = select("#", ...)
for i =1,n do
val = val[select(i, ...)]
if val == nil then
return nil
end
end
return val
end
local function gen_shm_key()
local shm_key = math.floor(c.timemillis()/1000) + shm_key_serial
shm_key_serial = shm_key_serial + 1
return shm_key
end
local function get_shm_nattch(shm_key)
local info = c.shminfo(shm_key)
return info and info.shm_nattch or -1
end
local function get_pid()
if not pid then
local handle = io.popen("echo $$")
pid = tonumber(handle:read("*a"))
handle:close()
end
return pid
end
local function get_shm_infos(cache_infos)
local f = io.open("/proc/sysvipc/shm", "r")
local text = f:read("*a")
local shm_infos = {}
f:close()
local function get_name_infos(name)
local infos = shm_infos[name]
if not infos then
infos = {}
shm_infos[name] = infos
end
return infos
end
for line in text:gmatch("[^\r\n]+") do
local shm_key = line:match("%s*(%d+)%s+.*")
shm_key = tonumber(shm_key)
if shm_key and shm_key > 0 then
local info = cache_infos[shm_key]
if not info then
local shminfo = c.shminfo(shm_key)
if shminfo then
local shm_conf = c.shmat(shm_key)
if shm_conf then
info = c.get_info(shm_conf)
if info.size == shminfo.shm_segsz then
info.shm_key = shm_key
table.insert(get_name_infos(info.name), info)
cache_infos[shm_key] = info
end
c.shmdt(shm_conf)
end
end
else
table.insert(get_name_infos(info.name), info)
end
end
end
for name,infos in pairs(shm_infos) do
table.sort(infos, function(a, b)
if a.time ~= b.time then
return a.time < b.time
else
return a.pid < b.pid
end
end)
end
return shm_infos
end
function _M.update(confs)
_M.reload()
local random_confs = {}
for name,data in pairs(confs) do
table.insert(random_confs, {name = name, data = data})
end
local count = #random_confs
for i=1,count-1 do
local index = math.random(i, count)
if i ~= index then
local tmp = random_confs[i]
random_confs[i] = random_confs[index]
random_confs[index] = tmp
end
end
local pid = get_pid()
local shm_confs = {}
local cache_infos = {}
local shm_infos = get_shm_infos(cache_infos)
local new_shm_infos = {}
for index,v in pairs(random_confs) do
local name = v.name
local data = v.data
local conf = c.new(name, pid, c.timemillis(), data)
local hash = calc_hash(conf)
c.set_hash(conf, hash)
--query form local
if config_hash[name] == hash then
c.delete(conf)
else
--query from shared memory
local found
config_hash[name] = hash
local infos = shm_infos[name]
if infos then
for _,info in pairs(infos) do
if info.hash == hash then
local shm_conf = c.shmat(info.shm_key)
if shm_conf then
if c.get_info(shm_conf).hash == hash then
shm_confs[name] = shm_conf
c.delete(conf)
found = true
break
else
c.shmdt(shm_conf)
end
end
end
end
end
--new config
if not found then
local new_info,shm_conf
for i=1,65535 do
local shm_key = gen_shm_key()
shm_conf = c.shmsave(conf, shm_key)
if shm_conf then
shm_key = shm_key
new_info = {
hash = hash,
shm_key = shm_key,
shm_conf = shm_conf,
pid = pid,
}
new_shm_infos[name] = new_info
break
end
end
c.delete(conf)
if not shm_conf then
error("shmsave fail " .. name)
end
new_info.time = c.timemillis()
end
end
end
--concurrent config loading processing
shm_infos = get_shm_infos(cache_infos)
for name,new_info in pairs(new_shm_infos) do
local found
local infos = shm_infos[name]
if infos then
for _,info in pairs(infos) do
if info.hash == new_info.hash then
local shm_conf = c.shmat(info.shm_key)
if shm_conf then
if c.get_info(shm_conf).hash == new_info.hash then
if info.time <= new_info.time then
if info.time < new_info.time or info.pid < new_info.pid then
found = true
shm_confs[name] = shm_conf
break
else
c.shmdt(shm_conf)
end
else
c.shmdt(shm_conf)
end
else
c.shmdt(shm_conf)
end
end
end
end
end
if found then
c.shmdt(new_info.shm_conf)
else
shm_confs[name] = new_info.shm_conf
end
end
if next(shm_confs) then
c.update(shm_confs)
end
--release shared memory when reference equals 0
shm_infos = get_shm_infos(cache_infos)
for name,infos in pairs(shm_infos) do
for _,info in pairs(infos) do
if get_shm_nattch(info.shm_key) == 0 then
c.shmrmid(info.shm_key)
end
end
end
end
function _M.md5(tb)
if type(tb) == "string" then
return md5.sumhexa(tb)
elseif type(tb) == "table" then
local conf = c.new("tmp", get_pid(), c.timemillis(), tb)
local hash = calc_hash(conf)
c.delete(conf)
return hash
else
error("invalid type table " .. type(tb))
end
end
function _M.memory()
return c.memory()
end
function _M.gc()
for _,cache in pairs(cache_config) do
setmetatable(cache, weak_meta)
end
collectgarbage("collect")
for _,cache in pairs(cache_config) do
setmetatable(cache, meta)
end
end
return _M