forked from jafarlihi/cppq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cppq.hpp
558 lines (499 loc) · 19.1 KB
/
cppq.hpp
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
#pragma once
#include <string>
#include <cstdint>
#include <chrono>
#include <thread>
#include <iostream>
#include <atomic>
#include <condition_variable>
#include <exception>
#include <functional>
#include <future>
#include <memory>
#include <mutex>
#include <queue>
#include <type_traits>
#include <utility>
#include <optional>
#include <map>
#include <hiredis/hiredis.h>
#include <uuid/uuid.h>
namespace cppq {
using concurrency_t = std::invoke_result_t<decltype(std::thread::hardware_concurrency)>;
// Retrofitted from https://github.com/bshoshany/thread-pool
class [[nodiscard]] thread_pool
{
public:
thread_pool(const concurrency_t thread_count_ = 0) :
thread_count(determine_thread_count(thread_count_)),
threads(std::make_unique<std::thread[]>(determine_thread_count(thread_count_))) {
create_threads();
}
~thread_pool() {
wait_for_tasks();
destroy_threads();
}
[[nodiscard]] concurrency_t get_thread_count() const {
return thread_count;
}
template <typename F, typename... A>
void push_task(F&& task, A&&... args) {
std::function<void()> task_function =
std::bind(std::forward<F>(task), std::forward<A>(args)...);
{
const std::scoped_lock tasks_lock(tasks_mutex);
tasks.push(task_function);
}
++tasks_total;
task_available_cv.notify_one();
}
void wait_for_tasks() {
waiting = true;
std::unique_lock<std::mutex> tasks_lock(tasks_mutex);
task_done_cv.wait(tasks_lock, [this] { return (tasks_total == 0); });
waiting = false;
}
private:
void create_threads() {
running = true;
for (concurrency_t i = 0; i < thread_count; ++i) {
threads[i] = std::thread(&thread_pool::worker, this);
}
}
void destroy_threads() {
running = false;
task_available_cv.notify_all();
for (concurrency_t i = 0; i < thread_count; ++i) {
threads[i].join();
}
}
[[nodiscard]] concurrency_t determine_thread_count(const concurrency_t thread_count_) {
if (thread_count_ > 0)
return thread_count_;
else {
if (std::thread::hardware_concurrency() > 0)
return std::thread::hardware_concurrency();
else
return 1;
}
}
void worker() {
while (running) {
std::function<void()> task;
std::unique_lock<std::mutex> tasks_lock(tasks_mutex);
task_available_cv.wait(tasks_lock, [this] { return !tasks.empty() || !running; });
if (running) {
task = std::move(tasks.front());
tasks.pop();
tasks_lock.unlock();
task();
tasks_lock.lock();
--tasks_total;
if (waiting)
task_done_cv.notify_one();
}
}
}
std::atomic<bool> running = false;
std::condition_variable task_available_cv = {};
std::condition_variable task_done_cv = {};
std::queue<std::function<void()>> tasks = {};
std::atomic<size_t> tasks_total = 0;
mutable std::mutex tasks_mutex = {};
concurrency_t thread_count = 0;
std::unique_ptr<std::thread[]> threads = nullptr;
std::atomic<bool> waiting = false;
};
enum class TaskState {
Unknown,
Pending,
Scheduled,
Active,
Failed,
Completed
};
std::string stateToString(TaskState state) {
switch (state) {
case TaskState::Unknown: return "Unknown";
case TaskState::Pending: return "Pending";
case TaskState::Scheduled: return "Scheduled";
case TaskState::Active: return "Active";
case TaskState::Failed: return "Failed";
case TaskState::Completed: return "Completed";
}
return "Unknown";
}
TaskState stringToState(std::string state) {
if (state.compare("Unknown") == 0) return TaskState::Unknown;
if (state.compare("Pending") == 0) return TaskState::Pending;
if (state.compare("Scheduled") == 0) return TaskState::Scheduled;
if (state.compare("Active") == 0) return TaskState::Active;
if (state.compare("Failed") == 0) return TaskState::Failed;
if (state.compare("Completed") == 0) return TaskState::Completed;
return TaskState::Unknown;
}
std::string uuidToString(uuid_t uuid) {
char uuid_str[37];
uuid_unparse_lower(uuid, uuid_str);
return uuid_str;
}
class Task {
public:
Task(std::string type, std::string payload, uint64_t maxRetry) {
uuid_generate(this->uuid);
this->type = type;
this->payload = payload;
this->state = TaskState::Unknown;
this->maxRetry = maxRetry;
this->retried = 0;
this->dequeuedAtMs = 0;
}
Task(
std::string uuid,
std::string type,
std::string payload,
std::string state,
uint64_t maxRetry,
uint64_t retried,
uint64_t dequeuedAtMs,
uint64_t schedule = 0,
std::string cron = ""
) {
uuid_t uuid_parsed;
uuid_parse(uuid.c_str(), uuid_parsed);
uuid_copy(this->uuid, uuid_parsed);
this->type = type;
this->payload = payload;
this->maxRetry = maxRetry;
this->retried = retried;
this->dequeuedAtMs = dequeuedAtMs;
this->state = stringToState(state);
this->schedule = schedule;
this->cron = cron;
}
uuid_t uuid;
std::string type;
std::string payload;
TaskState state;
uint64_t maxRetry;
uint64_t retried;
uint64_t dequeuedAtMs;
uint64_t schedule;
std::string cron;
std::string result;
};
using Handler = void (*)(Task&);
auto handlers = std::unordered_map<std::string, Handler>();
void registerHandler(std::string type, Handler handler) {
handlers[type] = handler;
}
typedef enum { Cron, TimePoint, None } ScheduleType;
typedef struct ScheduleOptions {
union {
const char *cron;
std::chrono::system_clock::time_point time;
};
ScheduleType type;
} ScheduleOptions;
ScheduleOptions scheduleOptions(std::chrono::system_clock::time_point t) {
return ScheduleOptions{ .time = t, .type = ScheduleType::TimePoint };
}
ScheduleOptions scheduleOptions(std::string c) {
return ScheduleOptions{ .cron = c.c_str(), .type = ScheduleType::Cron };
}
void enqueue(redisContext *c, Task task, std::string queue, ScheduleOptions s) {
if (s.type == ScheduleType::None)
task.state = TaskState::Pending;
else
task.state = TaskState::Scheduled;
redisCommand(c, "MULTI");
if (s.type == ScheduleType::None) {
redisCommand(c, "LPUSH cppq:%s:pending %s", queue.c_str(), uuidToString(task.uuid).c_str());
redisCommand(
c,
"HSET cppq:%s:task:%s type %s payload %s state %s maxRetry %d retried %d dequeuedAtMs %d",
queue.c_str(),
uuidToString(task.uuid).c_str(),
task.type.c_str(),
task.payload.c_str(),
stateToString(task.state).c_str(),
task.maxRetry,
task.retried,
task.dequeuedAtMs
);
} else if (s.type == ScheduleType::TimePoint) {
redisCommand(c, "LPUSH cppq:%s:scheduled %s", queue.c_str(), uuidToString(task.uuid).c_str());
redisCommand(
c,
"HSET cppq:%s:task:%s type %s payload %s state %s maxRetry %d retried %d dequeuedAtMs %d schedule %lu",
queue.c_str(),
uuidToString(task.uuid).c_str(),
task.type.c_str(),
task.payload.c_str(),
stateToString(task.state).c_str(),
task.maxRetry,
task.retried,
task.dequeuedAtMs,
std::chrono::duration_cast<std::chrono::milliseconds>(s.time.time_since_epoch()).count()
);
} else if (s.type == ScheduleType::Cron) {
redisCommand(c, "LPUSH cppq:%s:scheduled %s", queue.c_str(), uuidToString(task.uuid).c_str());
redisCommand(
c,
"HSET cppq:%s:task:%s type %s payload %s state %s maxRetry %d retried %d dequeuedAtMs %d cron %s",
queue.c_str(),
uuidToString(task.uuid).c_str(),
task.type.c_str(),
task.payload.c_str(),
stateToString(task.state).c_str(),
task.maxRetry,
task.retried,
task.dequeuedAtMs,
s.cron
);
}
redisReply *reply = (redisReply *)redisCommand(c, "EXEC");
if (reply->type == REDIS_REPLY_ERROR)
throw std::runtime_error("Failed to enqueue task");
}
void enqueue(redisContext *c, Task task, std::string queue) {
return enqueue(c, task, queue, ScheduleOptions{ .cron = "", .type = ScheduleType::None });
}
std::optional<Task> dequeue(redisContext *c, std::string queue) {
redisReply *reply = (redisReply *)redisCommand(c, "LRANGE cppq:%s:pending -1 -1", queue.c_str());
if (reply->type != REDIS_REPLY_ARRAY)
return {};
if (reply->elements == 0)
return {};
reply = reply->element[0];
std::string uuid = reply->str;
uint64_t dequeuedAtMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
redisCommand(c, "MULTI");
redisCommand(c, "LREM cppq:%s:pending 1 %s", queue.c_str(), uuid.c_str());
redisCommand(c, "HGET cppq:%s:task:%s type", queue.c_str(), uuid.c_str());
redisCommand(c, "HGET cppq:%s:task:%s payload", queue.c_str(), uuid.c_str());
redisCommand(c, "HGET cppq:%s:task:%s state", queue.c_str(), uuid.c_str());
redisCommand(c, "HGET cppq:%s:task:%s maxRetry", queue.c_str(), uuid.c_str());
redisCommand(c, "HGET cppq:%s:task:%s retried", queue.c_str(), uuid.c_str());
redisCommand(c, "HGET cppq:%s:task:%s dequeuedAtMs", queue.c_str(), uuid.c_str());
redisCommand(c, "HSET cppq:%s:task:%s dequeuedAtMs %lu", queue.c_str(), uuid.c_str(), dequeuedAtMs);
redisCommand(c, "HSET cppq:%s:task:%s state %s", queue.c_str(), uuid.c_str(), stateToString(TaskState::Active).c_str());
redisCommand(c, "LPUSH cppq:%s:active %s", queue.c_str(), uuid.c_str());
reply = (redisReply *)redisCommand(c, "EXEC");
if (reply->type != REDIS_REPLY_ARRAY || reply->elements != 10)
return {};
Task task = Task(
uuid,
reply->element[1]->str,
reply->element[2]->str,
stateToString(TaskState::Active),
strtoull(reply->element[4]->str, NULL, 0),
strtoull(reply->element[5]->str, NULL, 0),
dequeuedAtMs
);
return std::make_optional<Task>(task);
}
std::optional<Task> dequeueScheduled(redisContext *c, std::string queue, char *getScheduledScriptSHA) {
redisReply *reply = (redisReply *)redisCommand(c, "EVALSHA %s 0 %s", getScheduledScriptSHA, queue.c_str());
if (reply->type != REDIS_REPLY_STRING)
return {};
std::string uuid = reply->str;
uint64_t dequeuedAtMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
redisCommand(c, "MULTI");
redisCommand(c, "LREM cppq:%s:scheduled 1 %s", queue.c_str(), uuid.c_str());
redisCommand(c, "HGET cppq:%s:task:%s type", queue.c_str(), uuid.c_str());
redisCommand(c, "HGET cppq:%s:task:%s payload", queue.c_str(), uuid.c_str());
redisCommand(c, "HGET cppq:%s:task:%s state", queue.c_str(), uuid.c_str());
redisCommand(c, "HGET cppq:%s:task:%s maxRetry", queue.c_str(), uuid.c_str());
redisCommand(c, "HGET cppq:%s:task:%s retried", queue.c_str(), uuid.c_str());
redisCommand(c, "HGET cppq:%s:task:%s dequeuedAtMs", queue.c_str(), uuid.c_str());
redisCommand(c, "HGET cppq:%s:task:%s schedule", queue.c_str(), uuid.c_str());
redisCommand(c, "HSET cppq:%s:task:%s dequeuedAtMs %lu", queue.c_str(), uuid.c_str(), dequeuedAtMs);
redisCommand(c, "HSET cppq:%s:task:%s state %s", queue.c_str(), uuid.c_str(), stateToString(TaskState::Active).c_str());
redisCommand(c, "LPUSH cppq:%s:active %s", queue.c_str(), uuid.c_str());
reply = (redisReply *)redisCommand(c, "EXEC");
if (reply->type != REDIS_REPLY_ARRAY || reply->elements != 11)
return {};
Task task = Task(
uuid,
reply->element[1]->str,
reply->element[2]->str,
stateToString(TaskState::Active),
strtoull(reply->element[4]->str, NULL, 0),
strtoull(reply->element[5]->str, NULL, 0),
dequeuedAtMs,
strtoull(reply->element[6]->str, NULL, 0)
);
return std::make_optional<Task>(task);
}
void taskRunner(redisOptions redisOpts, Task task, std::string queue) {
redisContext *c = redisConnectWithOptions(&redisOpts);
if (c == NULL || c->err) {
std::cerr << "Failed to connect to Redis" << std::endl;
return;
}
Handler handler = handlers[task.type];
try {
handler(task);
} catch(const std::exception &e) {
task.retried++;
redisCommand(c, "MULTI");
redisCommand(c, "LREM cppq:%s:active 1 %s", queue.c_str(), uuidToString(task.uuid).c_str());
redisCommand(c, "HSET cppq:%s:task:%s retried %d", queue.c_str(), uuidToString(task.uuid).c_str(), task.retried);
if (task.retried >= task.maxRetry) {
task.state = TaskState::Failed;
redisCommand(
c,
"HSET cppq:%s:task:%s state %s",
queue.c_str(),
uuidToString(task.uuid).c_str(),
stateToString(task.state).c_str()
);
redisCommand(c, "LPUSH cppq:%s:failed %s", queue.c_str(), uuidToString(task.uuid).c_str());
} else {
task.state = TaskState::Pending;
redisCommand(
c,
"HSET cppq:%s:task:%s state %s",
queue.c_str(),
uuidToString(task.uuid).c_str(),
stateToString(task.state).c_str()
);
redisCommand(c, "LPUSH cppq:%s:pending %s", queue.c_str(), uuidToString(task.uuid).c_str());
}
redisCommand(c, "EXEC");
redisFree(c);
return;
}
task.state = TaskState::Completed;
redisCommand(c, "MULTI");
redisCommand(c, "LREM cppq:%s:active 1 %s", queue.c_str(), uuidToString(task.uuid).c_str());
redisCommand(
c,
"HSET cppq:%s:task:%s state %s",
queue.c_str(),
uuidToString(task.uuid).c_str(),
stateToString(task.state).c_str()
);
redisCommand(
c,
"HSET cppq:%s:task:%s result %s",
queue.c_str(),
uuidToString(task.uuid).c_str(),
task.result.c_str()
);
redisCommand(c, "LPUSH cppq:%s:completed %s", queue.c_str(), uuidToString(task.uuid).c_str());
redisCommand(c, "EXEC");
redisFree(c);
}
void recovery(redisOptions redisOpts, std::map<std::string, int> queues, uint64_t timeoutMs, uint64_t checkEveryMs) {
redisContext *c = redisConnectWithOptions(&redisOpts);
if (c == NULL || c->err) {
std::cerr << "Failed to connect to Redis" << std::endl;
return;
}
// TODO: Consider incrementing `retried` on recovery
while (true) {
std::this_thread::sleep_for(std::chrono::milliseconds(checkEveryMs));
for (std::map<std::string, int>::iterator it = queues.begin(); it != queues.end(); it++) {
redisReply *reply = (redisReply *)redisCommand(c, "LRANGE cppq:%s:active 0 -1", it->first.c_str());
for (int i = 0; i < reply->elements; i++) {
std::string uuid = reply->element[i]->str;
redisReply *dequeuedAtMsReply = (redisReply *)redisCommand(
c,
"HGET cppq:%s:task:%s dequeuedAtMs",
it->first.c_str(),
uuid.c_str()
);
redisReply *scheduleReply = (redisReply *)redisCommand(
c,
"HGET cppq:%s:task:%s schedule",
it->first.c_str(),
uuid.c_str()
);
uint64_t dequeuedAtMs = strtoull(dequeuedAtMsReply->str, NULL, 0);
if (
dequeuedAtMs + timeoutMs <
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()
).count()
) {
redisCommand(c, "MULTI");
redisCommand(c, "LREM cppq:%s:active 1 %s", it->first.c_str(), uuid.c_str());
redisCommand(
c,
"HSET cppq:%s:task:%s state %s",
it->first.c_str(),
uuid.c_str(),
stateToString(TaskState::Pending).c_str()
);
if (scheduleReply->type == REDIS_REPLY_NIL)
redisCommand(c, "LPUSH cppq:%s:pending %s", it->first.c_str(), uuid.c_str());
else
redisCommand(c, "LPUSH cppq:%s:scheduled %s", it->first.c_str(), uuid.c_str());
redisCommand(c, "EXEC");
}
}
}
}
}
void pause(redisContext *c, std::string queue) {
redisCommand(c, "SADD cppq:queues:paused %s", queue.c_str());
}
void unpause(redisContext *c, std::string queue) {
redisCommand(c, "SREM cppq:queues:paused %s", queue.c_str());
}
bool isPaused(redisContext *c, std::string queue) {
redisReply *reply = (redisReply *)redisCommand(c, "SMEMBERS cppq:queues:paused");
for (int i = 0; i < reply->elements; i++)
if (queue == reply->element[i]->str)
return true;
return false;
}
const char *getScheduledScript = R"DOC(
local timeCall = redis.call('time')
local time = timeCall[1] .. timeCall[2]
local scheduled = redis.call('LRANGE', 'cppq:' .. ARGV[1] .. ':scheduled', 0, -1)
for _, key in ipairs(scheduled) do
if (time > redis.call('HGET', 'cppq:' .. ARGV[1] .. ':task:' .. key, 'schedule')) then
return key
end
end)DOC";
void runServer(redisOptions redisOpts, std::map<std::string, int> queues, uint64_t recoveryTimeoutSecond) {
redisContext *c = redisConnectWithOptions(&redisOpts);
if (c == NULL || c->err) {
std::cerr << "Failed to connect to Redis" << std::endl;
return;
}
redisReply *reply = (redisReply *)redisCommand(c, "SCRIPT LOAD %s", getScheduledScript);
char *getScheduledScriptSHA = reply->str;
std::vector<std::pair<std::string, int>> queuesVector;
for (auto& it : queues) queuesVector.push_back(it);
sort(
queuesVector.begin(),
queuesVector.end(),
[](std::pair<std::string, int> const& a, std::pair<std::string, int> const& b) { return a.second > b.second; }
);
for (auto it = queuesVector.begin(); it != queuesVector.end(); it++)
redisCommand(c, "SADD cppq:queues %s:%d", it->first.c_str(), it->second);
thread_pool pool;
pool.push_task(recovery, redisOpts, queues, recoveryTimeoutSecond * 1000, 10000);
while (true) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
for (std::vector<std::pair<std::string, int>>::iterator it = queuesVector.begin(); it != queuesVector.end(); it++) {
if (isPaused(c, it->first))
continue;
std::optional<Task> task;
task = dequeueScheduled(c, it->first, getScheduledScriptSHA);
if (!task.has_value())
task = dequeue(c, it->first);
if (task.has_value()) {
pool.push_task(taskRunner, redisOpts, task.value(), it->first);
break;
}
}
}
}
}