-
Notifications
You must be signed in to change notification settings - Fork 2
/
eq.lua
569 lines (487 loc) · 14.4 KB
/
eq.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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
local object = require 'box.oop'
local log = require 'box.log'
local rst = require 'box.rst'
local R = setmetatable({}, { __mode = "kv" })
local M = object:define({ __name = 'eq' })
M.DEF_PRI = 0x7fff
--[[
box.fiber.wrap(function()
box.fiber.name("eq:gc")
while true do
collectgarbage()
for k,v in pairs(R) do
print(k,": ",v)
end
box.fiber.sleep(1)
end
end)
]]
--[[
1. Queue required index on [status, prio]
]]
function M:on_disconnect(sid)
print(self, " received disconnect for ",sid)
if self.bysid[sid] then
for kfi in pairs(self.bysid[sid]) do
self.taken[kfi] = nil
local kf = {self:keyunpack(kfi)}
print("rm ", box.tuple.new(kf))
local t = box.select(self.space,0,kf)
if t then
if t[ self.f_stt ] == 'T' then
t = box.update(self.space, kf, '=p', self.f_stt, 'R' )
print("returned ", t, " to ",t[self.f_stt] )
self:wakeup(t)
else
print("rm ", box.tuple.new(kf), " -> ", t," have wrong status: ",t[self.f_stt] )
end
else
print("found no record for ",box.tuple.new(kf),", was marked as taken by ",sid)
end
end
self.bysid[sid] = nil
end
end
function M:init(opts)
R[self.__id] = self
rst:register( self, self.on_disconnect )
self.space = opts.space
self.index = opts.index
self.zombie = opts.zombie
-- self.pri = opts.pri or self.DEF_PRI
if opts.f_id then
self.f_id = opts.f_id
self.auto_id = true
end
self.f_stt = opts.f_stt or 0
self.f_pri = opts.f_pri
self.f_cnt = opts.f_cnt
self.f_cnb = opts.f_cnb
self.f_runat = opts.f_runat
self.auto_increment = opts.auto_increment
self.def = {
f_stt = 'R',
f_pri = opts.pri or self.DEF_PRI,
}
self.wseq = 0
self.wait = {}
self.taken = {}
self.bysid = {}
assert(box.space[self.space],"unknown space "..self.space)
if not self.index then
for idx,index in pairs(box.space[self.space].index) do
if index.key_field[0] and index.key_field[0].fieldno == self.f_stt then
if self.f_pri then
if index.key_field[1] and index.key_field[1].fieldno == self.f_pri then
print("match index ",idx," for status and pri")
assert(not self.index, "Ambiguous decision on index: "..tostring(self.index).." vs "..tostring(idx))
self.index = idx
end
else
print("match index ",idx," for status")
assert(not self.index, "Ambiguous decision on index: "..tostring(self.index).." vs "..tostring(idx))
self.index = idx
end
end
end
end
assert(box.space[self.space].index[self.index],"unknown index "..self.index.." in space "..self.space)
if self.f_runat then
for idx,index in pairs(box.space[self.space].index) do
if index.key_field[0] and index.key_field[0].fieldno == self.f_runat then
print("match index ",idx," for runat")
self.index_runat = idx
break
end
end
elseif self.zombie then
print(".zombie is useless without runat")
self.zombie = nil
end
-- print("kf = ",#box.space[self.space].index[self.index].key_field)
-- if true then return end
do
local fbody = "return function(self,t,val)\n\tif not val then val = {} end\n"
local cbody = "return function(self,tuple)\n"
cbody = cbody .. "\tlocal t if type(tuple) == 'userdata' then t = tuple:totable() else t = { tuple:unpack() } end\n"
local s = {}
local max = 1
for k,v in pairs({"f_id","f_stt","f_pri","f_cnt","f_cnb", "f_runat"}) do
-- print(k," ",v, " ",max)
if self[v] then
s[ self[v]+1 ] = v
if max < self[v]+1 then max = self[v]+1 end
-- print("set max to ", max, " for ",v)
end
end
-- print ("max = ",max)
for i = 1,max,1 do
-- print("check ",i," ",s[i])
if not s[i] then s[i] = false end
end
--table.sort(s)
for k,v in ipairs(s) do
-- print(k," ",v)
if v then
fbody = fbody .. "\tif #t < "..(k-1).." then box.raise(11,'tuple too short '..(#t)..' / '.."..(k-1)..") end\n"
fbody = fbody .. "\ttable.insert(t, ".. (k) ..", val['"..v.."'] or self.def['".. v .."'])\n"
cbody = cbody .. "\tt["..(k).."] = nil\n"
-- fbody = fbody .. "\tprint(box.tuple.new(t))\n"
end
end
fbody = fbody .. "\treturn t\nend\n"
cbody = cbody .. "\treturn box.tuple.new(t)\nend\n"
-- print("\n"..fbody)
-- print("\n"..cbody)
self.extend = box.dostring(fbody)
self.collapse = box.dostring(cbody)
end
do
local fbody = "return function(self,x) return "
local count = 0
for k,v in pairs( box.space[self.space].index[0].key_field ) do
fbody = fbody .. 'x['.. v.fieldno ..'],'
count = count + 1
end
fbody = fbody:sub(1,#fbody - 1) .. " end\n"
--print(fbody)
self.keyfield = box.dostring(fbody)
self.keypack = box.dostring("return function(self,x) return box.pack('"..string.rep("p",count).."',unpack(x)) end")
self.keyunpack = box.dostring("return function(self,x) return box.unpack('"..string.rep("p",count).."',x) end")
end
do
local weak = setmetatable({}, { __mode = "kv" })
weak.self = self
self.watcher = box.fiber.wrap(function(w)
box.fiber.name("q"..tostring(w.self.space)..".grd")
while true do
collectgarbage()
if not w.self then break end
local r,e = pcall(function(self)
--print("loop 1 ",box.time64())
local c = 0
for kf,sid in pairs(self.taken) do
-- print(k," -> ",v)
if sid > 0 then
if box.session.exists(sid) == 1 then
else
print("session ",sid, "was disconnected, but task ",box.tuple.new(kf)," was not released")
self:on_disconnect(sid)
end
end
end
end, w.self)
if not r then
print(e)
end
collectgarbage()
if not w.self then break end
box.fiber.sleep(1)
end
end,weak)
end
if self.f_runat then
local weak = setmetatable({}, { __mode = "kv" })
weak.self = self
local chan = box.ipc.channel(1)
local maxwait = self.pause or 1
self.runat_chan = chan
self.runat = box.fiber.wrap(function(w)
box.fiber.name("q"..tostring(w.self.space)..".run")
--if true then return end
local idx = box.space[w.self.space].index[ w.self.index_runat ]
while true do
collectgarbage()
if not w.self then break end
local r,e = pcall(function(self)
--print("loop 2 ",box.time64())
local collect = {}
local it = idx:iterator(box.index.GT,0)
for t in it do
-- print("checking ",t)
if box.unpack('l',t[ self.f_runat ]) <= box.time64() then
table.insert(collect,t)
else
break
end
if #collect >= 1000 then break end
end
for _,t in ipairs(collect) do
print("collect ",t)
if t[self.f_stt] == 'W' then
-- turn Wait into Ready
print("Put task to ready ",box.tuple.new(self:keyfield(t)))
local u = box.update(self.space, { self:keyfield(t) }, '=p=p', self.f_stt, 'R', self.f_runat, -1ULL)
self:wakeup(u)
elseif t[self.f_stt] == 'R' then
print("Drop old task ",box.tuple.new(self:keyfield(t)))
box.delete(self.space, { self:keyfield(t) } )
elseif t[self.f_stt] == 'Z' then
print("Kill zombie ",box.tuple.new(self:keyfield(t)))
box.delete(self.space, { self:keyfield(t) } )
elseif t[self.f_stt] == 'T' then
-- Autorelease (TODO)
print("Task taken too long ",box.tuple.new(self:keyfield(t)))
box.update(self.space, { self:keyfield(t) }, '=p', self.f_runat, -1ULL)
else
print("Unsupported status ",t[self.f_stt], " in runat")
box.update(self.space, { self:keyfield(t) }, '=p', self.f_runat, -1ULL)
end
end
if #collect < 1000 then
it = idx:iterator(box.index.GT,0)
local n = it()
if n then
local v = box.unpack('l',n[ self.f_runat ])
if v < -1ULL then
print("Have next task after ",tonumber(v - box.time64())/1e6)
return tonumber(v - box.time64())/1e6
end
end
end
return 1 -- no next task. sleep 1 second at most
end, w.self)
if not r then
print(e)
end
collectgarbage()
if not w.self then break end
local z = chan:get(r and e or maxwait)
if z then
print("awake from channel")
end
end
end,weak)
end
self:starttest()
print ("Configured EQ on ",self,": sp:",self.space)
end
function M:starttest()
local c = 0
for v in box.space[self.space].index[self.index]:iterator(box.index.EQ, 'T') do
c = c + 1
local kf = { self:keyfield( v ) }
box.update(self.space,kf,'=p',self.f_stt, 'R')
end
if c > 0 then
print("Reset ",c," records from T ro R")
end
end
function M:insert(...)
local arg = {...}
if #arg == 1 then arg = arg[1] end
local t = box.insert(self.space, box.tuple.new(self:extend(arg)))
self:wakeup(t)
return t -- self:collapse(t)
end
function M:replace(...)
local arg = {...}
if #arg == 1 then arg = arg[0] end
-- TODO: select, check taken, ...
local t = box.tuple.new(self:extend(arg));
box.replace(self.space,t)
end
function M:put(arg,opts)
--local arg = {...}
--if type(arg[1]) == 'table' then arg = arg[1] end
opts = opts or {}
local id
if self.auto_id then
local maxt = box.space[self.space].index[0]:max()
if maxt then
local maxv = box.unpack('l',maxt[ self.f_id ])
local now = box.time64()
if now > maxv then
id = now
else
id = maxv+1
end
else
id = box.time64()
end
end
local ext = {
f_id = id;
f_stt = opts.status;
f_pri = opts.pri or opts.priority;
f_runat = -1ULL;
}
if opts.delay then
ext.f_stt = 'W'
ext.f_runat = box.time64() + tonumber64( opts.delay * 1E6 )
elseif opts.ttl then
ext.f_runat = box.time64() + tonumber64( opts.ttl * 1E6 )
end
--print(box.tuple.new(arg))
local tbl = self:extend(arg,ext)
-- print("inserting ",box.tuple.new(tbl))
local t = box.insert(self.space,box.tuple.new(tbl))
if t[self.f_stt] == 'R' then
self:wakeup(t)
end
if self.runat_chan and ext.f_runat ~= -1ULL then
self.runat_chan:put(true,0)
end
return t
end
--[[
touch (complete_task_tuple, new_status)
turn task to any state
]]
function M:touch(task,status)
local t = box.select(self.space,0,{ self:keyfield(task) })
if t[self.f_stt] == 'T' then
box.raise(box.error.ER_PROC_LUA,"Touching taken task not allowed")
end
t = box.update(self.space, { self:keyfield(task) }, '=p', self.f_stt, status)
self:wakeup(t)
return t
end
--[[
internal
wake waiting fibers
]]
function M:wakeup(t)
if t[self.f_stt] ~= 'R' then return end
for _,v in pairs(self.wait) do
v:put(t,0)
return
end
print("No waits")
end
--[[
take task with timeout
]]
function M:take(to)
assert(to, "Timeout required")
to = tonumber(to)
local now = box.time()
local _,t
while true do
_,t = box.space[self.space].index[self.index]:next_equal( 'R' )
if t then
break
else
local left = (now + to) - box.time()
--print("left: ",left)
if left <= 0 then
return
end
local wseq = self.wseq
self.wseq = wseq + 1
local ch = box.ipc.channel(1)
self.wait[wseq] = ch
t = ch:get( left )
self.wait[wseq] = nil
if t then
print("Got from channel, left: ",(now + to) - box.time(),": ",t)
break
end
end
end
local sid = box.session.id()
local kf = { self:keyfield( t ) }
local kfi = self:keypack(kf)
print("register ",box.tuple.new(kf)," for ",sid)
t = box.update(self.space, kf, '=p', self.f_stt, 'T' )
if not self.bysid[ sid ] then self.bysid[ sid ] = {} end
self.taken[ kfi ] = sid
self.bysid[ sid ][ kfi ] = true
return t -- self:collapse(t)
end
function M:check_owner(kfi)
if not self.taken[kfi] then
local id = tostring(box.tuple.new({self:keyunpack(kfi)}))
box.raise(11,string.format( "Task %s not taken by any", id ))
end
if self.taken[kfi] ~= box.session.id() then
local id = tostring(box.tuple.new({self:keyunpack(kfi)}))
box.raise(11,string.format( "Task %s taken by %d. Not you (%d)", id, self.taken[kfi], box.session.id() ))
end
return true
end
--[[
ack - confirm that task is done
arg1: full tuple or table with key
arg2: table with optional params
]]
function M:ack(key,opt)
local kf = type(key) == 'userdata' and { self:keyfield(key) } or key
local kfi = self:keypack(kf)
self:check_owner(kfi)
local sid = self.taken[ kfi ]
self.taken[ kfi ] = nil
self.bysid[ sid ][ kfi ] = nil
-- return self:collapse( box.delete(self.space, kf) )
if self.zombie and self.f_runat then
self.runat_chan:put(true,0)
return box.update(self.space, kf, '=p=p', self.f_stt, 'Z', self.f_runat, box.time64() + tonumber64( self.zombie*1e6 ) )
else
return box.delete(self.space, kf)
end
end
function M:release(key,opt)
local kf = type(key) == 'userdata' and { self:keyfield(key) } or key
local kfi = self:keypack(kf)
self:check_owner(kfi)
opt = opt or {}
if not opt.update then opt.update = { "" } end
local update = {unpack(opt.update)}
local up = table.remove(update,1)
local t
if opt.delay and self.runat then
t = box.update(self.space, kf, '=p=p'..up, self.f_stt, 'W', self.f_runat, box.time64() + tonumber64( opt.delay*1e6 ), unpack(update) )
self.runat_chan:put(true,0)
else
t = box.update(self.space, kf, '=p'..up, self.f_stt, 'R', unpack(update) )
self:wakeup(t)
end
local sid = self.taken[ kfi ]
self.taken[ kfi ] = nil
self.bysid[ sid ][ kfi ] = nil
return t -- self:collapse( t )
end
function M:done(key,opt)
local kf = type(key) == 'userdata' and { self:keyfield(key) } or key
local kfi = self:keypack(kf)
self:check_owner(kfi)
opt = opt or {}
if not opt.update then opt.update = { "" } end
local update = {unpack(opt.update)}
local up = table.remove(update,1)
--return self:collapse( box.update(self.space, kf, '=p'..up, self.f_stt, 'D', ... ) )
local t = box.update(self.space, kf, '=p'..up, self.f_stt, 'D', unpack(update) )
local sid = self.taken[ kfi ]
self.taken[ kfi ] = nil
self.bysid[ sid ][ kfi ] = nil
return t
end
--function M:bury(kf, t, up, ...)
function M:bury(key,opt)
local kf = type(key) == 'userdata' and { self:keyfield(key) } or key
local kfi = self:keypack(kf)
self:check_owner(kfi)
opt = opt or {}
if not opt.update then opt.update = { "" } end
local update = {unpack(opt.update)}
local up = table.remove(update,1)
if opt.status and (
#opt.status ~= 1 or
opt.status == "R" or
opt.status == "T" or
opt.status == "W" or
opt.status == "Z" or
opt.status == "D"
) then
box.raise(box.error.ER_PROC_LUA,"Bad status for bury: "..opt.status)
elseif not opt.status then
opt.status = "B"
end
-- return self:collapse( box.update(self.space, kf, '=p'..up, self.f_stt, t, ... ) )
local t = box.update(self.space, kf, '=p'..up, self.f_stt, opt.status, unpack(update) )
local sid = self.taken[ kfi ]
self.taken[ kfi ] = nil
self.bysid[ sid ][ kfi ] = nil
return t
end
return M