-
Notifications
You must be signed in to change notification settings - Fork 23
/
rima.lua
306 lines (249 loc) · 8.07 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
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
--
-- 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: Task Queue (task's data)
-- 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 Queue (with priority, locks e.t.c.)
-- Tuple: { key (STR), priority (NUM), is_locked (NUM), lock_time (NUM), lock_source (STR), serial_num (NUM) }
-- Index 0: TREE { key }
-- Index 1: TREE { priority, is_locked, lock_time }
-- Index 2: TREE { priority, is_locked, serial_num }
-- Index 3: TREE { serial_num }
--
-- 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
local TASKS_BATCH = 1000
local FAST_TASKS_BATCH = 1000
local function next_queue_id()
local next_id = 1
local max_id = box.space[2].index[3]:max()
if max_id ~= nil then
next_id = box.unpack('i', max_id[5]) + 1
end
-- Tarantool field is only int32 :(, so it is defense from overflow
-- Overflow happens in case, where workers can't so fast do the tasks
-- We don't lose any tasks, because tasks data places into space[0]
if next_id >= 2^32 then
print("too big serrial, truncate space[2]")
box.space[2]:truncate()
next_id = 1
end
return next_id
end
--
-- Insert task data into the queue
--
local function insert_task_data(key, data, new_prio, ts)
local first_task = box.select_limit(0, 1, 0, 1, key)
if first_task == nil then
box.auto_increment(0, key, data, ts)
else
if new_prio == 0 and data == first_task[2] then
-- optimisation: no need another same task
else
box.auto_increment(0, key, data, ts)
end
end
end
--
-- Put task to the queue.
--
local function rima_put_impl(key, data, prio, ts)
-- first: insert task data
insert_task_data(key, data, prio, ts)
-- second: insert or update key into queue
local pr = box.select_limit(2, 0, 0, 1, key)
if pr == nil then
box.insert(2, key, prio, 0, box.time(), '', next_queue_id())
elseif box.unpack('i', pr[1]) < prio then
box.update(2, key, "=p", 1, prio)
else
end
return 1
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
function rima_put_sync(key, data, prio)
prio = box.unpack('i', prio)
return rima_put_impl(key, data, prio, box.time())
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_with_lock(prio, source)
local v = box.select_limit(2, 2, 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, TASKS_BATCH, 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_with_lock(prio, source)
if key == nil then return end
local tasks = get_key_data(key)
if table.getn(tasks) == 1 then
-- if only email in table, it means there are no tasks
rima_done(key)
return
end
return unpack(tasks)
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, FAST_TASKS_BATCH, 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
-- already locked, pr[2] - is_locked
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, next_queue_id())
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})