forked from mailru/tntlua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rima.lua
242 lines (197 loc) · 6.01 KB
/
rima.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
--
-- rima.lua
--
--
-- Task manager for imap collector.
-- Task's key is a user email address.
-- Rima can manage some tasks with the same key.
-- Tasks with identical keys will be groupped and managed as one bunch of tasks.
--
-- Producers can adds tasks by rima_put() calls.
-- Consumer request a bunch of tasks (with common key) by calling rima_get().
-- When Rima gives a task to worker it locks the key until worker calls rima_done(key).
-- Rima does not return task with already locked keys.
--
--
-- Space 0: Remote IMAP Collector Task Queue
-- Tuple: { task_id (NUM64), key (STR), task_description (NUM), add_time (NUM) }
-- Index 0: TREE { task_id }
-- Index 1: TREE { key, task_id }
--
-- Space 2: Task Priority
-- Tuple: { key (STR), priority (NUM), is_locked (NUM), lock_time (NUM), lock_source (STR) }
-- Index 0: TREE { key }
-- Index 1: TREE { priority, is_locked, lock_time }
--
-- Space 3: Mail Fetcher Queue (Special queue for fast single message loading)
-- Tuple: { task_id (NUM64), key (STR), task_description (NUM), add_time (NUM) }
-- Index 0: TREE { task_id }
-- Index 1: TREE { key }
--
local EXPIRATION_TIME = 30 * 60 -- seconds
--
-- Put task to the queue.
--
local function rima_put_impl(key, data, prio, ts)
-- insert task data into the queue
box.auto_increment(0, key, data, ts)
-- increase priority of the key
local pr = box.select_limit(2, 0, 0, 1, key)
if pr == nil then
box.insert(2, key, prio, 0, box.time())
elseif box.unpack('i', pr[1]) < prio then
box.update(2, key, "=p", 1, prio)
end
end
function rima_put(key, data) -- deprecated
rima_put_impl(key, data, 512, box.time())
end
function rima_put_with_prio(key, data, prio)
prio = box.unpack('i', prio)
rima_put_impl(key, data, prio, box.time())
end
function rima_put_with_prio_and_ts(key, data, prio, ts)
prio = box.unpack('i', prio)
ts = box.unpack('i', ts)
rima_put_impl(key, data, prio, ts)
end
--
-- Put fetch single mail task to the queue.
--
function rima_put_fetchmail(key, data)
box.auto_increment(3, key, data, box.time())
end
local function get_prio_key(prio, source)
local v = box.select_limit(2, 1, 0, 1, prio, 0)
if v == nil then return nil end
if source == nil then source = "" end
-- lock the key
local key = v[0]
box.update(2, key, "=p=p=p", 2, 1, 3, box.time(), 4, source)
return key
end
local function get_key_data(key)
local result = { key }
local tuples = { box.select_limit(0, 1, 0, 1000, key) }
for _, tuple in pairs(tuples) do
tuple = box.delete(0, box.unpack('l', tuple[0]))
if tuple ~= nil then
table.insert(result, { box.unpack('i', tuple[3]), tuple[2] } )
end
end
return result
end
--
-- Request tasks from the queue.
--
function rima_get_ex(prio, source)
prio = box.unpack('i', prio)
local key = get_prio_key(prio, source)
if key == nil then return end
return unpack(get_key_data(key))
end
--
-- Request fetch single mail tasks from the queue.
--
function rima_get_fetchmail()
local tuple = box.select_range(3, 0, 1)
if tuple == nil then return end
local key = tuple[1]
local result = {}
local n = 0
local tuples = { box.select_limit(3, 1, 0, 1000, key) }
for _, tuple in pairs(tuples) do
tuple = box.delete(3, box.unpack('l', tuple[0]))
if tuple ~= nil then
table.insert(result, { box.unpack('i', tuple[3]), tuple[2] })
n = 1
end
end
if n == 0 then return end
return key, unpack(result)
end
--
-- Request tasks from the queue for concrete user.
--
function rima_get_user_tasks(key, source)
local lock_acquired = rima_lock(key, source)
if lock_acquired == 0 then
local pr = box.select_limit(2, 0, 0, 1, key)
if pr[4] ~= source and source ~= "force_run" then return end
lock_acquired = 1
end
return unpack(get_key_data(key))
end
--
-- Notify manager that tasks for that key was completed.
-- Rima unlocks key and next rima_get() may returns tasks with such key.
-- In case of non-zero @unlock_delay user unlock is defered for @unlock_delay seconds (at least).
--
function rima_done(key, unlock_delay)
if unlock_delay ~= nil then unlock_delay = box.unpack('i', unlock_delay) end
local pr = box.select_limit(2, 0, 0, 1, key)
if pr == nil then return end
if unlock_delay ~= nil and unlock_delay > 0 then
box.update(2, key, "=p=p", 2, 1, 3, box.time() - EXPIRATION_TIME + unlock_delay)
elseif box.select_limit(0, 1, 0, 1, key) == nil then
-- no tasks for this key in the queue
box.delete(2, key)
else
box.update(2, key, "=p=p", 2, 0, 3, box.time())
end
end
--
-- Explicitly lock tasks for the key.
--
function rima_lock(key, source)
local pr = box.select_limit(2, 0, 0, 1, key)
if pr ~= nil and box.unpack('i', pr[2]) > 0 then return 0 end
if source == nil then source = "" end
-- lock the key
if pr ~= nil then
box.update(2, key, "=p=p=p", 2, 1, 3, box.time(), 4, source)
else
box.insert(2, key, 0, 1, box.time(), source)
end
return 1
end
--
-- Delete info and all tasks for user
--
function rima_delete_user(email)
local something_deleted = 0
repeat
something_deleted = 0
local tuple = box.delete(2, email)
if tuple ~= nil then something_deleted = 1 end
local tuples = { box.select_limit(3, 1, 0, 1000, email) }
for _, tuple in pairs(tuples) do
tuple = box.delete(3, box.unpack('l', tuple[0]))
something_deleted = 1
end
tuples = { box.select_limit(0, 1, 0, 1000, email) }
for _, tuple in pairs(tuples) do
tuple = box.delete(0, box.unpack('l', tuple[0]))
something_deleted = 1
end
until something_deleted == 0
end
--
-- Run expiration of tuples
--
local function is_expired(args, tuple)
if tuple == nil or #tuple <= args.fieldno then
return nil
end
-- expire only locked keys
if box.unpack('i', tuple[2]) == 0 then return false end
local field = tuple[args.fieldno]
local current_time = box.time()
local tuple_expire_time = box.unpack('i', field) + args.expiration_time
return current_time >= tuple_expire_time
end
local function delete_expired(spaceno, args, tuple)
rima_done(tuple[0])
end
dofile('expirationd.lua')
expirationd.run_task('expire_locks', 2, is_expired, delete_expired, {fieldno = 3, expiration_time = EXPIRATION_TIME})