redox.cpp
7.44 KB
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
/**
* Redis C++11 wrapper.
*/
#include <signal.h>
#include "redox.hpp"
using namespace std;
namespace redox {
void Redox::connected(const redisAsyncContext *ctx, int status) {
if (status != REDIS_OK) {
cerr << "[ERROR] Connecting to Redis: " << ctx->errstr << endl;
return;
}
// Disable hiredis automatically freeing reply objects
ctx->c.reader->fn->freeObject = [](void* reply) {};
Redox* rdx = (Redox*) ctx->data;
rdx->connected_lock.unlock();
cout << "[INFO] Connected to Redis." << endl;
}
void Redox::disconnected(const redisAsyncContext *ctx, int status) {
if (status != REDIS_OK) {
cerr << "[ERROR] Disconnecting from Redis: " << ctx->errstr << endl;
return;
}
// Re-enable hiredis automatically freeing reply objects
ctx->c.reader->fn->freeObject = freeReplyObject;
Redox* rdx = (Redox*) ctx->data;
rdx->connected_lock.unlock();
cout << "[INFO] Disconnected from Redis." << endl;
}
Redox::Redox(const string& host, const int port)
: host(host), port(port) {
lock_guard<mutex> lg(queue_guard);
signal(SIGPIPE, SIG_IGN);
ctx = redisAsyncConnect(host.c_str(), port);
if (ctx->err) {
printf("Error: %s\n", ctx->errstr);
return;
}
evloop = ev_loop_new(EVFLAG_AUTO);
ev_set_userdata(evloop, (void*)this);
redisLibevAttach(evloop, ctx);
redisAsyncSetConnectCallback(ctx, Redox::connected);
redisAsyncSetDisconnectCallback(ctx, Redox::disconnected);
ctx->data = (void*)this;
connected_lock.lock();
}
Redox::~Redox() {
redisAsyncDisconnect(ctx);
stop();
if(event_loop_thread.joinable())
event_loop_thread.join();
ev_loop_destroy(evloop);
std::cout << "[INFO] Redox created " << commands_created
<< " Commands and freed " << commands_deleted << "." << std::endl;
}
void Redox::run_blocking() {
// Events to connect to Redox
ev_run(evloop, EVRUN_NOWAIT);
// Block until connected to Redis
connected_lock.lock();
connected_lock.unlock();
// Continuously create events and handle them
while (!to_exit) {
process_queued_commands();
ev_run(evloop, EVRUN_NOWAIT);
}
cout << "[INFO] Stop signal detected." << endl;
// Run a few more times to clear out canceled events
for(int i = 0; i < 100; i++) {
ev_run(evloop, EVRUN_NOWAIT);
}
if(commands_created != commands_deleted) {
cerr << "[ERROR] All commands were not freed! "
<< commands_created << "/" << commands_deleted << endl;
}
exited = true;
// Let go for block_until_stopped method
exit_waiter.notify_one();
cout << "[INFO] Event thread exited." << endl;
}
void Redox::run() {
event_loop_thread = thread([this] { run_blocking(); });
// Don't return until connected
lock_guard<mutex> lg(connected_lock);
}
void Redox::stop_signal() {
to_exit = true;
}
void Redox::block() {
unique_lock<mutex> ul(exit_waiter_lock);
exit_waiter.wait(ul, [this] { return exited.load(); });
}
void Redox::stop() {
stop_signal();
block();
}
template<class ReplyT>
Command<ReplyT>* Redox::find_command(long id) {
lock_guard<mutex> lg(command_map_guard);
auto& command_map = get_command_map<ReplyT>();
auto it = command_map.find(id);
if(it == command_map.end()) return nullptr;
return it->second;
}
template<class ReplyT>
void command_callback(redisAsyncContext *ctx, void *r, void *privdata) {
Redox* rdx = (Redox*) ctx->data;
long id = (long)privdata;
redisReply* reply_obj = (redisReply*) r;
Command<ReplyT>* c = rdx->find_command<ReplyT>(id);
if(c == nullptr) {
// cout << "[WARNING] Couldn't find Command " << id << " in command_map (command_callback)." << endl;
freeReplyObject(reply_obj);
return;
}
c->reply_obj = reply_obj;
c->process_reply();
// Increment the Redox object command counter
rdx->incr_cmd_count();
}
/**
* Submit an asynchronous command to the Redox server. Return
* true if succeeded, false otherwise.
*/
template<class ReplyT>
bool submit_to_server(Command<ReplyT>* c) {
c->pending++;
if (redisAsyncCommand(c->rdx->ctx, command_callback<ReplyT>, (void*)c->id, c->cmd.c_str()) != REDIS_OK) {
cerr << "[ERROR] Could not send \"" << c->cmd << "\": " << c->rdx->ctx->errstr << endl;
c->invoke_error(REDOX_SEND_ERROR);
return false;
}
return true;
}
template<class ReplyT>
void submit_command_callback(struct ev_loop* loop, ev_timer* timer, int revents) {
Redox* rdx = (Redox*) ev_userdata(loop);
long id = (long)timer->data;
Command<ReplyT>* c = rdx->find_command<ReplyT>(id);
if(c == nullptr) {
cout << "[ERROR] Couldn't find Command " << id
<< " in command_map (submit_command_callback)." << endl;
return;
}
if(c->is_completed()) {
// cout << "[INFO] Command " << c << " is completed, stopping event timer." << endl;
c->timer_guard.lock();
if((c->repeat != 0) || (c->after != 0))
ev_timer_stop(loop, &c->timer);
c->timer_guard.unlock();
// Mark for memory to be freed when all callbacks are received
c->timer.data = (void*)0;
return;
}
submit_to_server<ReplyT>(c);
}
template<class ReplyT>
bool Redox::process_queued_command(long id) {
Command<ReplyT>* c = find_command<ReplyT>(id);
if(c == nullptr) return false;
if((c->repeat == 0) && (c->after == 0)) {
submit_to_server<ReplyT>(c);
} else {
c->timer.data = (void*)c->id;
ev_timer_init(&c->timer, submit_command_callback<ReplyT>, c->after, c->repeat);
ev_timer_start(evloop, &c->timer);
c->timer_guard.unlock();
}
return true;
}
void Redox::process_queued_commands() {
lock_guard<mutex> lg(queue_guard);
while(!command_queue.empty()) {
long id = command_queue.front();
command_queue.pop();
if(process_queued_command<redisReply*>(id)) {}
else if(process_queued_command<string>(id)) {}
else if(process_queued_command<char*>(id)) {}
else if(process_queued_command<int>(id)) {}
else if(process_queued_command<long long int>(id)) {}
else if(process_queued_command<nullptr_t>(id)) {}
else throw runtime_error("[FATAL] Command pointer not found in any queue!");
}
}
// ----------------------------
template<> unordered_map<long, Command<redisReply*>*>&
Redox::get_command_map<redisReply*>() {
// cout << "redis reply command map at " << &commands_redis_reply << endl;
return commands_redis_reply; }
template<> unordered_map<long, Command<string>*>&
Redox::get_command_map<string>() {
// cout << "string command map at " << &commands_string_r << endl;
return commands_string_r; }
template<> unordered_map<long, Command<char*>*>&
Redox::get_command_map<char*>() {
// cout << "char* command map at " << &commands_char_p << endl;
return commands_char_p; }
template<> unordered_map<long, Command<int>*>&
Redox::get_command_map<int>() {
// cout << "int command map at " << &commands_int << " has size: " << commands_int.size() << endl;
return commands_int; }
template<> unordered_map<long, Command<long long int>*>&
Redox::get_command_map<long long int>() {
// cout << "long long int command map at " << &commands_long_long_int << endl;
return commands_long_long_int; }
template<> unordered_map<long, Command<nullptr_t>*>&
Redox::get_command_map<nullptr_t>() { return commands_null; }
// ----------------------------
// Helpers
// ----------------------------
void Redox::command(const string& cmd) {
command<redisReply*>(cmd);
}
bool Redox::command_blocking(const string& cmd) {
Command<redisReply*>* c = command_blocking<redisReply*>(cmd);
bool succeeded = (c->status() == REDOX_OK);
c->free();
return succeeded;
}
} // End namespace redis