forked from sonic-net/sonic-swss-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_piped_ut.cpp
442 lines (356 loc) · 10.5 KB
/
redis_piped_ut.cpp
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
#include <iostream>
#include <memory>
#include <thread>
#include <algorithm>
#include "gtest/gtest.h"
#include "common/dbconnector.h"
#include "common/producertable.h"
#include "common/consumertable.h"
#include "common/notificationconsumer.h"
#include "common/notificationproducer.h"
#include "common/select.h"
#include "common/selectableevent.h"
#include "common/table.h"
using namespace std;
using namespace swss;
#define NUMBER_OF_THREADS (64) // Spawning more than 256 threads causes libc++ to except
#define NUMBER_OF_OPS (1000)
#define MAX_FIELDS_DIV (30) // Testing up to 30 fields objects
#define PRINT_SKIP (10) // Print + for Producer and - for Consumer for every 100 ops
static int getMaxFields(int i)
{
return (i/MAX_FIELDS_DIV) + 1;
}
static string key(int i)
{
return string("key") + to_string(i);
}
static string field(int i)
{
return string("field") + to_string(i);
}
static string value(int i)
{
return string("value") + to_string(i);
}
static bool IsDigit(char ch)
{
return (ch >= '0') && (ch <= '9');
}
static int readNumberAtEOL(const string& str)
{
auto pos = find_if(str.begin(), str.end(), IsDigit);
istringstream is(str.substr(pos - str.begin()));
int ret;
is >> ret;
return ret;
}
static void validateFields(const string& key, const vector<FieldValueTuple>& f)
{
unsigned int maxNumOfFields = getMaxFields(readNumberAtEOL(key));
int i = 0;
EXPECT_EQ(maxNumOfFields, f.size());
for (auto fv : f)
{
EXPECT_EQ(i, readNumberAtEOL(fvField(fv)));
EXPECT_EQ(i, readNumberAtEOL(fvValue(fv)));
i++;
}
}
static void producerWorker(int index)
{
string tableName = "UT_REDIS_THREAD_" + to_string(index);
DBConnector db("TEST_DB", 0, true);
RedisPipeline pipeline(&db);
ProducerTable p(&pipeline, tableName, true);
for (int i = 0; i < NUMBER_OF_OPS; i++)
{
vector<FieldValueTuple> fields;
int maxNumOfFields = getMaxFields(i);
for (int j = 0; j < maxNumOfFields; j++)
{
FieldValueTuple t(field(j), value(j));
fields.push_back(t);
}
if ((i % 100) == 0)
cout << "+" << flush;
p.set(key(i), fields);
}
for (int i = 0; i < NUMBER_OF_OPS; i++)
{
p.del(key(i));
}
}
static void consumerWorker(int index)
{
string tableName = "UT_REDIS_THREAD_" + to_string(index);
DBConnector db("TEST_DB", 0, true);
ConsumerTable c(&db, tableName);
Select cs;
Selectable *selectcs;
int numberOfKeysSet = 0;
int numberOfKeyDeleted = 0;
int ret, i = 0;
KeyOpFieldsValuesTuple kco;
cs.addSelectable(&c);
while ((ret = cs.select(&selectcs)) == Select::OBJECT)
{
c.pop(kco);
if (kfvOp(kco) == "SET")
{
numberOfKeysSet++;
validateFields(kfvKey(kco), kfvFieldsValues(kco));
} else
{
numberOfKeyDeleted++;
}
if ((i++ % 100) == 0)
cout << "-" << flush;
if ((numberOfKeysSet == NUMBER_OF_OPS) &&
(numberOfKeyDeleted == NUMBER_OF_OPS))
break;
}
EXPECT_EQ(ret, Select::OBJECT);
}
static void clearDB()
{
DBConnector db("TEST_DB", 0, true);
RedisReply r(&db, "FLUSHALL", REDIS_REPLY_STATUS);
r.checkStatusOK();
}
TEST(DBConnector, piped_test)
{
thread *producerThreads[NUMBER_OF_THREADS];
thread *consumerThreads[NUMBER_OF_THREADS];
clearDB();
cout << "Starting " << NUMBER_OF_THREADS*2 << " producers and consumers on redis" << endl;
/* Starting the consumer before the producer */
for (int i = 0; i < NUMBER_OF_THREADS; i++)
{
consumerThreads[i] = new thread(consumerWorker, i);
producerThreads[i] = new thread(producerWorker, i);
}
cout << "Done. Waiting for all job to finish " << NUMBER_OF_OPS << " jobs." << endl;
for (int i = 0; i < NUMBER_OF_THREADS; i++)
{
producerThreads[i]->join();
delete producerThreads[i];
consumerThreads[i]->join();
delete consumerThreads[i];
}
cout << endl << "Done." << endl;
}
TEST(DBConnector, piped_multitable)
{
DBConnector db("TEST_DB", 0, true);
ConsumerTable *consumers[NUMBER_OF_THREADS];
thread *producerThreads[NUMBER_OF_THREADS];
KeyOpFieldsValuesTuple kco;
Select cs;
int numberOfKeysSet = 0;
int numberOfKeyDeleted = 0;
int ret = 0, i;
clearDB();
cout << "Starting " << NUMBER_OF_THREADS*2 << " producers and consumers on redis, using single thread for consumers and thread per producer" << endl;
/* Starting the consumer before the producer */
for (i = 0; i < NUMBER_OF_THREADS; i++)
{
consumers[i] = new ConsumerTable(&db, string("UT_REDIS_THREAD_") +
to_string(i));
producerThreads[i] = new thread(producerWorker, i);
}
for (i = 0; i < NUMBER_OF_THREADS; i++)
cs.addSelectable(consumers[i]);
while (1)
{
Selectable *is;
ret = cs.select(&is);
EXPECT_EQ(ret, Select::OBJECT);
((ConsumerTable *)is)->pop(kco);
if (kfvOp(kco) == "SET")
{
numberOfKeysSet++;
validateFields(kfvKey(kco), kfvFieldsValues(kco));
} else
{
numberOfKeyDeleted++;
if ((numberOfKeyDeleted % 100) == 0)
cout << "-" << flush;
}
if ((numberOfKeysSet == NUMBER_OF_OPS * NUMBER_OF_THREADS) &&
(numberOfKeyDeleted == NUMBER_OF_OPS * NUMBER_OF_THREADS))
break;
}
/* Making sure threads stops execution */
for (i = 0; i < NUMBER_OF_THREADS; i++)
{
producerThreads[i]->join();
delete consumers[i];
delete producerThreads[i];
}
cout << endl << "Done." << endl;
}
static void notificationProducer()
{
sleep(1);
DBConnector db("TEST_DB", 0, true);
NotificationProducer np(&db, "UT_REDIS_CHANNEL");
vector<FieldValueTuple> values;
FieldValueTuple tuple("foo", "bar");
values.push_back(tuple);
cout << "Starting sending notification producer" << endl;
np.send("a", "b", values);
}
TEST(DBConnector, piped_notifications)
{
DBConnector db("TEST_DB", 0, true);
NotificationConsumer nc(&db, "UT_REDIS_CHANNEL");
Select s;
s.addSelectable(&nc);
Selectable *sel;
int value = 1;
clearDB();
thread np(notificationProducer);
int result = s.select(&sel, 2000);
if (result == Select::OBJECT)
{
cout << "Got notification from producer" << endl;
value = 2;
string op, data;
vector<FieldValueTuple> values;
nc.pop(op, data, values);
EXPECT_EQ(op, "a");
EXPECT_EQ(data, "b");
auto v = values.at(0);
EXPECT_EQ(fvField(v), "foo");
EXPECT_EQ(fvValue(v), "bar");
}
np.join();
EXPECT_EQ(value, 2);
}
static void selectableEventThread(Selectable *ev, int *value)
{
Select s;
s.addSelectable(ev);
Selectable *sel;
cout << "Starting listening ... " << endl;
int result = s.select(&sel, 2000);
if (result == Select::OBJECT)
{
if (sel == ev)
{
cout << "Got notification" << endl;
*value = 2;
}
}
}
TEST(DBConnector, piped_selectableevent)
{
int value = 1;
SelectableEvent ev;
thread t(selectableEventThread, &ev, &value);
sleep(1);
EXPECT_EQ(value, 1);
ev.notify();
t.join();
EXPECT_EQ(value, 2);
}
TEST(Table, piped_test)
{
string tableName = "TABLE_UT_TEST";
DBConnector db("TEST_DB", 0, true);
RedisPipeline pipeline(&db);
Table t(&pipeline, tableName, true);
clearDB();
cout << "Starting table manipulations" << endl;
string key_1 = "a";
string key_2 = "b";
vector<FieldValueTuple> values;
for (int i = 1; i < 4; i++)
{
string field = "field_" + to_string(i);
string value = to_string(i);
values.push_back(make_pair(field, value));
}
cout << "- Step 1. SET" << endl;
cout << "Set key [a] field_1:1 field_2:2 field_3:3" << endl;
cout << "Set key [b] field_1:1 field_2:2 field_3:3" << endl;
t.set(key_1, values);
t.set(key_2, values);
t.flush();
cout << "- Step 2. GET_TABLE_CONTENT" << endl;
vector<KeyOpFieldsValuesTuple> tuples;
t.getContent(tuples);
cout << "Get total " << tuples.size() << " number of entries" << endl;
EXPECT_EQ(tuples.size(), (size_t)2);
for (auto tuple: tuples)
{
cout << "Get key [" << kfvKey(tuple) << "]" << flush;
unsigned int size_v = 3;
EXPECT_EQ(kfvFieldsValues(tuple).size(), size_v);
for (auto fv: kfvFieldsValues(tuple))
{
string value_1 = "1", value_2 = "2";
cout << " " << fvField(fv) << ":" << fvValue(fv) << flush;
if (fvField(fv) == "field_1")
{
EXPECT_EQ(fvValue(fv), value_1);
}
if (fvField(fv) == "field_2")
{
EXPECT_EQ(fvValue(fv), value_2);
}
}
cout << endl;
}
cout << "- Step 3. DEL" << endl;
cout << "Delete key [a]" << endl;
t.del(key_1);
cout << "- Step 4. GET" << endl;
cout << "Get key [a] and key [b]" << endl;
EXPECT_EQ(t.get(key_1, values), false);
t.get(key_2, values);
cout << "Get key [b]" << flush;
for (auto fv: values)
{
string value_1 = "1", value_2 = "2";
cout << " " << fvField(fv) << ":" << fvValue(fv) << flush;
if (fvField(fv) == "field_1")
{
EXPECT_EQ(fvValue(fv), value_1);
}
if (fvField(fv) == "field_2")
{
EXPECT_EQ(fvValue(fv), value_2);
}
}
cout << endl;
cout << "- Step 5. DEL and GET_TABLE_CONTENT" << endl;
cout << "Delete key [b]" << endl;
t.del(key_2);
t.getContent(tuples);
EXPECT_EQ(tuples.size(), unsigned(0));
cout << "Done." << endl;
}
TEST(ProducerConsumer, piped_Prefix)
{
string tableName = "tableName";
DBConnector db("TEST_DB", 0, true);
RedisPipeline pipeline(&db);
ProducerTable p(&pipeline, tableName, true);
vector<FieldValueTuple> values;
FieldValueTuple t("f", "v");
values.push_back(t);
p.set("key", values, "set", "prefix_");
p.flush();
ConsumerTable c(&db, tableName);
KeyOpFieldsValuesTuple kco;
c.pop(kco, "prefix_");
string key = kfvKey(kco);
string op = kfvOp(kco);
auto vs = kfvFieldsValues(kco);
EXPECT_EQ(key, "key");
EXPECT_EQ(op, "set");
EXPECT_EQ(fvField(vs[0]), "f");
EXPECT_EQ(fvValue(vs[0]), "v");
}