-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdb.R
465 lines (411 loc) · 11.7 KB
/
db.R
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
#' @importFrom DBI dbGetQuery sqlInterpolate dbConnect dbDisconnect
#' @importFrom DBI dbExecute dbWithTransaction
#' @importFrom RSQLite SQLite
db_connect <- function(..., synchronous = NULL) {
con <- dbConnect(SQLite(), synchronous = synchronous, ...)
dbExecute(con, "PRAGMA busy_timeout = 1000")
con
}
#' The name of the default database
#'
#' If the queue database is not specified explicitly,
#' then `liteq` uses this file. Its location is determined via the
#' `rappdirs` package, see [rappdirs::user_data_dir()].
#'
#' @return A characater scalar, the name of the default database.
#'
#' @importFrom rappdirs user_data_dir
#' @export
default_db <- function() {
file.path(
user_data_dir(appname = "liteq"),
"liteq.db"
)
}
#' Ensure that the DB exists and has the right columns
#'
#' We try a query, and if it fails then we try to create the DB.
#'
#' @param db DB file.
#'
#' @keywords internal
ensure_db <- function(db) {
tryCatch(
do_db(db, "SELECT * FROM meta LIMIT 1"),
error = function(e) {
dir.create(dirname(db), recursive = TRUE, showWarnings = FALSE)
db_create_db(db)
}
)
}
db_queue_name <- function(name) {
paste0("qq", name)
}
db_query <- function(con, query, ...) {
dbGetQuery(con, sqlInterpolate(con, query, ...))
}
db_execute <- function(con, query, ...) {
dbExecute(con, sqlInterpolate(con, query, ...))
}
do_db <- function(db, query, ...) {
con <- db_connect(db)
on.exit(dbDisconnect(con))
db_query(con, query, ...)
}
do_db_execute <- function(db, query, ...) {
con <- db_connect(db)
on.exit(dbDisconnect(con))
db_execute(con, query, ...)
}
db_lock <- function(con) {
dbExecute(con, "PRAGMA busy_timeout = 1000")
done <- FALSE
while (!done) {
tryCatch(
{
dbExecute(con, "BEGIN EXCLUSIVE")
done <- TRUE
},
error = function(e) NULL
)
}
}
db_create_db <- function(db) {
do_db_execute(
db,
"CREATE TABLE meta (
name TEXT PRIMARY KEY,
created TIMESTAMP,
lockdir TEXT,
requeue TEXT DEFAULT \"fail\" -- fail/ requeue/ number of requeues
)"
)
}
#' @importFrom RSQLite dbExistsTable
db_ensure_queue <- function(name, db, crash_strategy) {
con <- db_connect(db)
on.exit(dbDisconnect(con), add = TRUE)
db_execute(con, "BEGIN EXCLUSIVE")
tablename <- db_queue_name(name)
if (!dbExistsTable(con, tablename)) {
db_create_queue_locked(db, con, name, crash_strategy)
}
}
#' Create a queue
#'
#' The database columns:
#' * id Id of the message, it is generated automatically by the database.
#' * title The title of the message, can be empty. In the future,
#' it can be used to filter messages.
#' * message The message, arbitrary text, can be empty.
#' * status Can be:
#' * `READY`, ready to be consumed
#' * `WORKING`, it is being consumed
#' * `FAILED`, failed.
#' * requeued How many times the message was requeued.
#'
#' @inheritParams create_queue
#' @importFrom rappdirs user_cache_dir
#' @keywords internal
db_create_queue <- function(name, db, crash_strategy) {
con <- db_connect(db)
on.exit(dbDisconnect(con), add = TRUE)
db_execute(con, "BEGIN EXCLUSIVE")
db_create_queue_locked(db, con, name, crash_strategy)
}
db_create_queue_locked <- function(db, con, name, crash_strategy) {
db_execute(
con,
'CREATE TABLE ?tablename (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
message TEXT NOT NULL,
status TEXT DEFAULT "READY",
requeued INTEGER DEFAULT 0)',
tablename = db_queue_name(name)
)
db_execute(
con,
'INSERT INTO meta (name, created, lockdir, requeue) VALUES
(?name, DATE("now"), ?lockdir, ?crash)',
name = name,
lockdir = db_lockdir(db),
crash = as.character(crash_strategy)
)
db_execute(con, "COMMIT")
}
db_lockdir <- function(db) {
file.path(
Sys.getenv("LITEQ_CACHE_DIR", user_cache_dir(appname = "liteq")),
paste0(basename(db), "-", random_lock_name())
)
}
db_list_queues <- function(db) {
do_db(db, "SELECT name FROM meta");
}
db_publish <- function(db, queue, title, message) {
do_db_execute(
db,
"INSERT INTO ?tablename (title, message)
VALUES (?title, ?message)",
tablename = db_queue_name(queue),
title = title,
message = message
)
invisible()
}
#' Try to consume a message from the queue
#'
#' If there is a message that it `READY`, it returns that. Otherwise it
#' checks for crashed workers.
#'
#' @section Details of the implementation:
#'
#' The database must be locked for the whole operation, including
#' checking on or creating the lock databases.
#'
#' 1. If there is a `READY` message, that one is taken.
#' 2. Otherwise if there are `WORKING` messages, then
#' we check them one by one. This might take a lot of
#' time, and the DB must be locked for the whole search,
#' so it is not ideal. But I don't have a better solution
#' right now.
#'
#' Taking a message means
#' 1. Updating its row.status to `WORKING`.
#' 2. Creating another database that serves as the lock for this message.
#'
#' @param db DB file name.
#' @param queue Name of the queue.
#' @keywords internal
db_try_consume <- function(db, queue, crashed = TRUE, con = NULL) {
if (is.null(con)) {
con <- db_connect(db)
on.exit(try_silent(dbDisconnect(con)), add = TRUE)
db_lock(con)
}
## See if there is a message to work on. If there is, we just return it.
msg <- db_query(
con, 'SELECT * FROM ?tablename WHERE status = "READY" LIMIT 1',
tablename = db_queue_name(queue)
)
if (nrow(msg) == 1) {
db_execute(
con, 'UPDATE ?tablename SET status = "WORKING" WHERE id = ?id',
tablename = db_queue_name(queue),
id = msg$id
)
lockdir <- db_query(
con, "SELECT lockdir FROM meta WHERE name = ?name",
name = queue
)$lockdir
db_execute(con, "COMMIT")
return(list(msg = msg, db = db, queue = queue, lockdir = lockdir))
}
## Otherwise we need to check on crashed workers
if (crashed && db_clean_crashed(con, queue)) {
mmsg <- db_try_consume(db, queue, crashed = FALSE, con = con)
tryCatch(db_execute(con, "COMMIT"), error = function(e) NULL)
return(mmsg)
} else {
tryCatch(db_execute(con, "COMMIT"), error = function(e) NULL)
NULL
}
}
db_clean_crashed <- function(con, queue) {
work <- db_query(
con, 'SELECT * FROM ?tablename WHERE status = "WORKING"',
tablename = db_queue_name(queue)
)
if (nrow(work) == 0) return(FALSE)
meta <- db_query(
con, "SELECT * FROM meta WHERE name = ?name",
name = queue
)
locks <- message_lock_file(meta$lockdir, queue, work$id)
for (i in seq_along(locks)) {
lock <- locks[[i]]
x <- tryCatch(
{
lcon <- db_connect(lock)
dbGetQuery(lcon, "SELECT * FROM foo")
},
error = function(x) "busy"
)
if (! identical(x, "busy")) {
try_silent(dbDisconnect(lcon))
if (meta$requeue == "fail" || meta$requeue == "requeue") {
## Always fail, or always requeue
status <- if (meta$requeue == "fail") "FAILED" else "READY"
db_clean_crashed_update(con, queue, work$id[i], status)
} else if (as.numeric(work$requeued[i]) >= as.numeric(meta$requeue)) {
## Requeued too many times
db_clean_crashed_update(con, queue, work$id[i], "FAILED")
} else {
## Can still requeue
db_clean_crashed_update(con, queue, work$id[i], "READY")
db_execute(
con,
'UPDATE ?tablename SET requeued = requeued + 1 WHERE id = ?id',
tablename = db_queue_name(queue),
id = work$id[i]
)
}
unlink(lock)
}
}
TRUE
}
db_clean_crashed_update <- function(con, queue, id, status) {
db_execute(
con, 'UPDATE ?tablename SET status = ?status WHERE id = ?id',
tablename = db_queue_name(queue),
id = id,
status = status
)
}
#' Consume a message from a message queue
#'
#' This is the blocking version of [try_consume()]. Currently it just
#' polls twice a second, and sleeps between the polls. Each poll will also
#' trigger a crash cleanup, if there are workers running.
#'
#' @inheritParams try_consume
#'
#' @keywords internal
db_consume <- function(db, queue, poll_interval = 500) {
while (TRUE) {
msg <- db_try_consume(db, queue)
if (!is.null(msg)) break
Sys.sleep(poll_interval / 1000)
}
msg
}
#' Positive or negative ackowledgement
#'
#' If positive, then we need to remove the message from the queue.
#' If negative, we just set the status to `FAILED`.
#'
#' @param db DB file.
#' @param queue Queue name.
#' @param id Message id.
#' @param lock Name of the message lock file.
#' @param success Whether this is a positive or negative ACK.
#'
#' @keywords internal
db_ack <- function(db, queue, id, lock, success) {
con <- db_connect(db)
on.exit(try_silent(dbDisconnect(con)), add = TRUE)
db_lock(con)
if (success) {
num <- db_execute(
con, "DELETE FROM ?tablename WHERE id = ?id",
tablename = db_queue_name(queue), id = id
)
} else {
num <- db_execute(
con, 'UPDATE ?tablename SET status = "FAILED" WHERE id = ?id',
tablename = db_queue_name(queue), id = id
)
}
if (num == 0) stop("Message does not exist, internal error?")
if (num > 1) stop("Multiple messages with the same id, internal error")
lockdir <- db_query(
con, "SELECT lockdir FROM meta WHERE name = ?name",
name = queue
)$lockdir
lock <- message_lock_file(lockdir, queue, id)
unlink(lock)
db_execute(con, "COMMIT")
invisible()
}
db_list_messages <- function(db, queue, failed = FALSE) {
q <- "SELECT id, title, status FROM ?tablename"
if (failed) q <- paste(q, "WHERE status = \"FAILED\"")
do_db(db, q, tablename = db_queue_name(queue))
}
db_requeue_failed_messages <- function(db, queue, id) {
if (is.null(id)) {
db_requeue_all_failed_messages(db, queue)
} else {
db_requeue_some_failed_messages(db, queue, id)
}
invisible()
}
db_requeue_all_failed_messages <- function(db, queue) {
do_db_execute(
db,
"UPDATE ?tablename SET status = \"READY\" WHERE status = \"FAILED\"",
tablename = db_queue_name(queue)
)
}
db_requeue_some_failed_messages <- function(db, queue, id) {
con <- db_connect(db)
on.exit(dbDisconnect(con))
dbWithTransaction(con, {
for (id1 in id) {
db_execute(
con,
"UPDATE ?tablename
SET status = \"READY\"
WHERE status = \"FAILED\" AND id = ?id",
tablename = db_queue_name(queue),
id = id1
)
}
})
}
db_remove_failed_messages <- function(db, queue, id) {
if (is.null(id)) {
db_remove_all_failed_messages(db, queue)
} else {
db_remove_some_failed_messages(db, queue, id)
}
invisible()
}
db_remove_all_failed_messages <- function(db, queue) {
do_db_execute(
db,
"DELETE FROM ?tablename WHERE status = \"FAILED\"",
tablename = db_queue_name(queue)
)
}
db_remove_some_failed_messages <- function(db, queue, id) {
con <- db_connect(db)
on.exit(dbDisconnect(con))
dbWithTransaction(con, {
for (id1 in id) {
db_execute(
con,
"DELETE FROM ?tablename
WHERE status = \"FAILED\" AND id = ?id",
tablename = db_queue_name(queue),
id = id1
)
}
})
}
db_delete_queue <- function(db, queue, force) {
con <- db_connect(db)
on.exit(dbDisconnect(con))
dbWithTransaction(con, {
num <- db_query(
con,
"SELECT COUNT(*) FROM ?tablename",
tablename = db_queue_name(queue)
)
if (num > 0 && ! force) {
stop("Unwilling to delete non-empty queue, consider 'force = TRUE'")
}
db_execute(
con,
"DELETE FROM meta WHERE name = ?name",
name = queue
)
db_execute(
con,
"DROP TABLE ?tablename",
tablename = db_queue_name(queue)
)
})
}