e2e.cc 58.7 KB
Newer Older
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
// ----------------------------------------------------------------------
// File: e2e.cc
// Author: Georgios Bitzes - CERN
// ----------------------------------------------------------------------

/************************************************************************
 * quarkdb - a redis-like highly available key-value store              *
 * Copyright (C) 2016 CERN/Switzerland                                  *
 *                                                                      *
 * This program is free software: you can redistribute it and/or modify *
 * it under the terms of the GNU General Public License as published by *
 * the Free Software Foundation, either version 3 of the License, or    *
 * (at your option) any later version.                                  *
 *                                                                      *
 * This program is distributed in the hope that it will be useful,      *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
 * GNU General Public License for more details.                         *
 *                                                                      *
 * You should have received a copy of the GNU General Public License    *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.*
 ************************************************************************/

#include "raft/RaftDispatcher.hh"
#include "raft/RaftReplicator.hh"
#include "raft/RaftTimeouts.hh"
#include "raft/RaftCommitTracker.hh"
28
#include "raft/RaftConfig.hh"
29
#include "raft/RaftContactDetails.hh"
30
#include "Poller.hh"
31
32
#include "Configuration.hh"
#include "QuarkDBNode.hh"
33
34
35
#include "test-utils.hh"
#include "RedisParser.hh"
#include <gtest/gtest.h>
36
#include "test-reply-macros.hh"
37
#include "qclient/QScanner.hh"
Georgios Bitzes's avatar
Georgios Bitzes committed
38
#include "qclient/QSet.hh"
Georgios Bitzes's avatar
Georgios Bitzes committed
39
#include "qclient/ConnectionInitiator.hh"
Georgios Bitzes's avatar
Georgios Bitzes committed
40
#include "qclient/QHash.hh"
41
42
43

using namespace quarkdb;
#define ASSERT_OK(msg) ASSERT_TRUE(msg.ok())
44
45
class Raft_e2e : public TestCluster3NodesFixture {};
class Raft_e2e5 : public TestCluster5NodesFixture {};
46

Georgios Bitzes's avatar
Georgios Bitzes committed
47
48
49
TEST_F(Raft_e2e, coup) {
  spinup(0); spinup(1); spinup(2);

50
  RETRY_ASSERT_TRUE(checkStateConsensus(0, 1, 2));
Georgios Bitzes's avatar
Georgios Bitzes committed
51
52
53
54
55
56

  int leaderID = getLeaderID();
  ASSERT_GE(leaderID, 0);
  ASSERT_LE(leaderID, 2);

  int instigator = (leaderID+1)%3;
Georgios Bitzes's avatar
Georgios Bitzes committed
57
58
59
60
61
62
63
64
65
66
67
68
  for(int i = 1; i < 10; i++) {
    RaftTerm term = state(instigator)->getCurrentTerm();
    ASSERT_REPLY(tunnel(instigator)->exec("RAFT_ATTEMPT_COUP"), "vive la revolution");
    RETRY_ASSERT_TRUE(state(instigator)->getCurrentTerm() > term);
    RETRY_ASSERT_TRUE(checkStateConsensus(0, 1, 2));

    if(instigator == getLeaderID()) {
      qdb_info("Successful coup in " << i << " attempts");
      return; // pass test
    }
  }
  ASSERT_TRUE(false) << "Test has failed";
Georgios Bitzes's avatar
Georgios Bitzes committed
69
70
}

71
72
TEST_F(Raft_e2e, simultaneous_clients) {
  spinup(0); spinup(1); spinup(2);
73
  RETRY_ASSERT_TRUE(checkStateConsensus(0, 1, 2));
74

75
  int leaderID = getServerID(state(0)->getSnapshot()->leader);
76
77
78
  ASSERT_GE(leaderID, 0);
  ASSERT_LE(leaderID, 2);

79
  LogIndex lastEntry = journal(leaderID)->getLogSize() - 1;
80
81
82
  std::vector<std::future<redisReplyPtr>> futures;

  // send off many requests, pipeline them
83
84
85
86
  futures.emplace_back(tunnel(leaderID)->exec("get", "asdf"));
  futures.emplace_back(tunnel(leaderID)->exec("ping"));
  futures.emplace_back(tunnel(leaderID)->exec("set", "asdf", "1234"));
  futures.emplace_back(tunnel(leaderID)->exec("get", "asdf"));
87
  futures.emplace_back(tunnel(leaderID)->exec("raft-fetch", SSTR(lastEntry+1), "raw"));
88
89
90
91
92
93

  ASSERT_REPLY(futures[0], "");
  ASSERT_REPLY(futures[1], "PONG");
  ASSERT_REPLY(futures[2], "OK");
  ASSERT_REPLY(futures[3], "1234");

94
  RaftEntry entry;
95
  ASSERT_TRUE(RaftParser::fetchResponse(futures[4].get().get(), entry));
96
97
98
  ASSERT_EQ(entry.term, state(0)->getCurrentTerm());
  ASSERT_EQ(entry.request, make_req("set", "asdf", "1234"));

99
  futures.clear();
100
101
  futures.emplace_back(tunnel(leaderID)->exec("set", "asdf", "3456"));
  futures.emplace_back(tunnel(leaderID)->exec("get", "asdf"));
102
103
104
105
106
107
108

  ASSERT_REPLY(futures[0], "OK");
  ASSERT_REPLY(futures[1], "3456");

  // make sure the log entry has been propagated to all nodes
  for(size_t i = 0; i < 3; i++) {
    std::string value;
109
    RETRY_ASSERT_TRUE(stateMachine(i)->get("asdf", value).ok() && value == "3456");
110
111
112
113
114
115
  }

  ASSERT_REPLY(tunnel(leaderID)->exec("set", "qwerty", "789"), "OK");
  futures.clear();

  // interwine pipelined requests from three connections
116
117
  qclient::QClient tunnel2(myself(leaderID).hostname, myself(leaderID).port, makeNoRedirectOptions());
  qclient::QClient tunnel3(myself(leaderID).hostname, myself(leaderID).port, makeNoRedirectOptions());
118
119
120
121
122
123

  futures.emplace_back(tunnel2.exec("get", "qwerty"));
  futures.emplace_back(tunnel(leaderID)->exec("set", "client2", "val"));
  futures.emplace_back(tunnel(leaderID)->exec("get", "client2"));
  futures.emplace_back(tunnel(leaderID)->exec("sadd", "myset", "a"));
  futures.emplace_back(tunnel2.exec("sadd", "myset", "b"));
124
  futures.emplace_back(tunnel2.exec("sadd", "myset")); // malformed request
125
126
127
128
129
130
131
132
133
134
135
  futures.emplace_back(tunnel3.exec("set", "client3", "myval"));
  futures.emplace_back(tunnel3.exec("get", "client3"));

  // not guaranteed that response will be "myval" here, since it's on a different connection
  futures.emplace_back(tunnel2.exec("get", "client3"));

  ASSERT_REPLY(futures[0], "789");
  ASSERT_REPLY(futures[1], "OK");
  ASSERT_REPLY(futures[2], "val");
  ASSERT_REPLY(futures[3], 1);
  ASSERT_REPLY(futures[4], 1);
136
137
138
  ASSERT_REPLY(futures[5], "ERR wrong number of arguments for 'sadd' command");
  ASSERT_REPLY(futures[6], "OK");
  ASSERT_REPLY(futures[7], "myval");
139

140
  redisReplyPtr reply = futures[8].get();
141
142
143
144
145
146
147
148
149
150
151
  std::string str = std::string(reply->str, reply->len);
  qdb_info("Race-y request: GET client3 ==> " << str);
  ASSERT_TRUE(str == "myval" || str == "");

  ASSERT_REPLY(tunnel2.exec("scard", "myset"), 2);

  // but here we've received an ack - response _must_ be myval
  ASSERT_REPLY(tunnel2.exec("get", "client3"), "myval");

  RaftInfo info = dispatcher(leaderID)->info();
  ASSERT_EQ(info.blockedWrites, 0u);
152
  ASSERT_EQ(info.leader, myself(leaderID));
153
154
155
156

  std::string err;
  std::string checkpointPath = SSTR(commonState.testdir << "/checkpoint");

157
  // Before taking a checkpoint, ensure node #0 is caught up
158
  RETRY_ASSERT_TRUE(stateMachine(0)->getLastApplied() == stateMachine(leaderID)->getLastApplied());
159

160
161
162
163
  ASSERT_TRUE(dispatcher()->checkpoint(checkpointPath, err));
  ASSERT_FALSE(dispatcher()->checkpoint(checkpointPath, err)); // exists already

  // pretty expensive to open two extra databases, but necessary
164
  StateMachine checkpointSM(SSTR(checkpointPath << "/state-machine"));
165
166
167
168
169
170
171
172
173
174
175
176
177
178

  std::string tmp;
  ASSERT_OK(checkpointSM.get("client3", tmp));
  ASSERT_EQ(tmp, "myval");

  ASSERT_OK(checkpointSM.get("client2", tmp));
  ASSERT_EQ(tmp, "val");

  // TODO: verify checkpointSM last applied, once atomic commits are implemented

  // ensure the checkpoint journal is identical to the original
  RaftJournal checkpointJournal(SSTR(checkpointPath << "/raft-journal"));
  ASSERT_EQ(checkpointJournal.getLogSize(), journal()->getLogSize());
  for(LogIndex i = 0; i < journal()->getLogSize(); i++) {
179
    RaftEntry entry1, entry2;
180

181
182
    ASSERT_OK(checkpointJournal.fetch(i, entry1));
    ASSERT_OK(journal()->fetch(i, entry2));
183
184
185

    ASSERT_EQ(entry1, entry2);
  }
186
}
187

188
189
TEST_F(Raft_e2e, hscan) {
  spinup(0); spinup(1); spinup(2);
190
  RETRY_ASSERT_TRUE(checkStateConsensus(0, 1, 2));
191
  int leaderID = getServerID(state(0)->getSnapshot()->leader);
192
193

  for(size_t i = 1; i < 10; i++) {
194
    ASSERT_REPLY(tunnel(leaderID)->exec("hset", "hash", SSTR("f" << i), SSTR("v" << i)), 1);
195
196
  }

197
  redisReplyPtr reply = tunnel(leaderID)->exec("hscan", "hash", "0", "cOUnT", "3").get();
198
  ASSERT_REPLY(reply, std::make_pair("next:f4", make_vec("f1", "v1", "f2", "v2", "f3", "v3")));
199

200
  reply = tunnel(leaderID)->exec("hscan", "hash", "0", "asdf", "123").get();
201
202
  ASSERT_ERR(reply, "ERR syntax error");

203
  reply = tunnel(leaderID)->exec("hscan", "hash", "next:f4", "COUNT", "3").get();
204
  ASSERT_REPLY(reply, std::make_pair("next:f7", make_vec("f4", "v4", "f5", "v5", "f6", "v6")));
205

206
  reply = tunnel(leaderID)->exec("hscan", "hash", "next:f7", "COUNT", "30").get();
207
  ASSERT_REPLY(reply, std::make_pair("0", make_vec("f7", "v7", "f8", "v8", "f9", "v9")));
208

209
  reply = tunnel(leaderID)->exec("hscan", "hash", "adfaf").get();
210
211
  ASSERT_ERR(reply, "ERR invalid cursor");

212
  reply = tunnel(leaderID)->exec("hscan", "hash", "next:zz").get();
213
  ASSERT_REPLY(reply, std::make_pair("0", make_vec()));
214
215
}

216
217
218
TEST_F(Raft_e2e, scan) {
  spinup(0); spinup(1); spinup(2);
  RETRY_ASSERT_TRUE(checkStateConsensus(0, 1, 2));
Georgios Bitzes's avatar
Georgios Bitzes committed
219
  int leaderID = getLeaderID();
220
221
222
223
224
225
226
227
228
229

  for(size_t i = 1; i < 10; i++) {
    ASSERT_REPLY(tunnel(leaderID)->exec("set", SSTR("f" << i), SSTR("v" << i)), "OK");
  }

  redisReplyPtr reply = tunnel(leaderID)->exec("scan", "0", "MATCH", "f[1-2]").get();
  ASSERT_REPLY(reply, std::make_pair("0", make_vec("f1", "f2")));

  reply = tunnel(leaderID)->exec("scan", "0", "MATCH", "f*", "COUNT", "3").get();
  ASSERT_REPLY(reply, std::make_pair("next:f4", make_vec("f1", "f2", "f3")));
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251

  // without MATCH
  reply = tunnel(leaderID)->exec("scan", "0", "COUNT", "3").get();
  ASSERT_REPLY(reply, std::make_pair("next:f4", make_vec("f1", "f2", "f3")));

  // with "*" MATCH pattern
  reply = tunnel(leaderID)->exec("scan", "0", "COUNT", "3", "MATCH", "*").get();
  ASSERT_REPLY(reply, std::make_pair("next:f4", make_vec("f1", "f2", "f3")));

  QScanner scanner(*tunnel(leaderID), "f*", 3);

  std::vector<std::string> ret;
  ASSERT_TRUE(scanner.next(ret));
  ASSERT_EQ(ret, make_vec("f1", "f2", "f3"));

  ASSERT_TRUE(scanner.next(ret));
  ASSERT_EQ(ret, make_vec("f4", "f5", "f6"));

  ASSERT_TRUE(scanner.next(ret));
  ASSERT_EQ(ret, make_vec("f7", "f8", "f9"));

  ASSERT_FALSE(scanner.next(ret));
252
253
}

Georgios Bitzes's avatar
Georgios Bitzes committed
254
255
256
257
258
TEST_F(Raft_e2e, test_qclient_convenience_classes) {
  spinup(0); spinup(1); spinup(2);
  RETRY_ASSERT_TRUE(checkStateConsensus(0, 1, 2));
  int leaderID = getLeaderID();

Georgios Bitzes's avatar
Georgios Bitzes committed
259
  // QHash iterator
Georgios Bitzes's avatar
Georgios Bitzes committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
  std::vector<std::future<redisReplyPtr>> replies;
  for(size_t i = 0; i < 9; i++) {
    replies.push_back(tunnel(leaderID)->exec("HSET", "myhash", SSTR("f" << i), SSTR("v" << i)));
  }

  for(size_t i = 0; i < 9; i++) {
    ASSERT_REPLY(replies[i], 1);
  }

  qclient::QHash qhash(*tunnel(leaderID), "myhash");
  qclient::QHash::Iterator it = qhash.getIterator(2);

  for(size_t i = 0; i < 9; i++) {
    ASSERT_TRUE(it.valid());
    ASSERT_EQ(it.getKey(), SSTR("f" << i));
    ASSERT_EQ(it.getValue(), SSTR("v" << i));
    it.next();
  }

  ASSERT_FALSE(it.valid());
  ASSERT_EQ(it.requestsSoFar(), 5u);

Georgios Bitzes's avatar
Georgios Bitzes committed
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
  // QSet iterator
  replies.clear();
  for(size_t i = 0; i < 9; i++) {
    replies.push_back(tunnel(leaderID)->exec("SADD", "myset", SSTR("item-" << i)));
  }

  for(size_t i = 0; i < 9; i++) {
    ASSERT_REPLY(replies[i], 1);
  }

  qclient::QSet qset(*tunnel(leaderID), "myset");

  for(size_t count = 1; count < 15; count++) {
    qclient::QSet::Iterator it = qset.getIterator(count);
    for(size_t i = 0; i < 9; i++) {
      ASSERT_TRUE(it.valid());
      ASSERT_EQ(it.getElement(), SSTR("item-" << i));
      it.next();
    }

    ASSERT_FALSE(it.valid());
    ASSERT_EQ(it.requestsSoFar(), (9 / count) + (9%count != 0) );
  }

  qclient::QSet::Iterator it2 = qset.getIterator(3, "next:item-4");
  for(size_t i = 4; i < 9; i++) {
    ASSERT_TRUE(it2.valid());
    ASSERT_EQ(it2.getElement(), SSTR("item-" << i));
    it2.next();
  }

  ASSERT_FALSE(it2.valid());
  ASSERT_EQ(it2.requestsSoFar(), 2u);
}
Georgios Bitzes's avatar
Georgios Bitzes committed
316

317
318
319
TEST_F(Raft_e2e, test_many_redis_commands) {
  spinup(0); spinup(1); spinup(2);
  RETRY_ASSERT_TRUE(checkStateConsensus(0, 1, 2));
320
  int leaderID = getServerID(state(0)->getSnapshot()->leader);
321
322
323
324
325
326
327
328
329

  std::vector<std::future<redisReplyPtr>> futures;
  futures.emplace_back(tunnel(leaderID)->exec("SADD", "myset", "a", "b", "c"));
  futures.emplace_back(tunnel(leaderID)->exec("SCARD", "myset"));
  futures.emplace_back(tunnel(leaderID)->exec("Smembers", "myset"));
  futures.emplace_back(tunnel(leaderID)->exec("srem", "myset", "a", "b"));
  futures.emplace_back(tunnel(leaderID)->exec("srem", "myset", "b"));
  futures.emplace_back(tunnel(leaderID)->exec("scard", "myset"));
  futures.emplace_back(tunnel(leaderID)->exec("smembers", "myset"));
330
  futures.emplace_back(tunnel(leaderID)->exec("get", "empty_key"));
331
  futures.emplace_back(tunnel(leaderID)->exec("timestamped-lease-acquire", "123"));
332
333
  futures.emplace_back(tunnel(leaderID)->exec("timestamped-lease-get", "123"));
  futures.emplace_back(tunnel(leaderID)->exec("timestamped-lease-release", "123"));
334
335
336

  ASSERT_REPLY(futures[0], 3);
  ASSERT_REPLY(futures[1], 3);
337
  ASSERT_REPLY(futures[2], make_vec("a", "b", "c"));
338
339
340
  ASSERT_REPLY(futures[3], 2);
  ASSERT_REPLY(futures[4], 0);
  ASSERT_REPLY(futures[5], 1);
341
  ASSERT_REPLY(futures[6], make_vec("c"));
342
  ASSERT_NIL(futures[7]);
343
  ASSERT_REPLY(futures[8], "ERR unknown command 'timestamped-lease-acquire'" );
344
345
  ASSERT_REPLY(futures[9], "ERR unknown command 'timestamped-lease-get'" );
  ASSERT_REPLY(futures[10], "ERR unknown command 'timestamped-lease-release'" );
346
347
348
349
350
351
352
353
354

  futures.clear();

  futures.emplace_back(tunnel(leaderID)->exec("hset", "myhash", "a", "b"));
  futures.emplace_back(tunnel(leaderID)->exec("hset", "myhash", "b", "c"));
  futures.emplace_back(tunnel(leaderID)->exec("hset", "myhash", "c", "d"));
  futures.emplace_back(tunnel(leaderID)->exec("hset", "myhash", "a", "d"));
  futures.emplace_back(tunnel(leaderID)->exec("hdel", "myhash", "a", "b", "b"));
  futures.emplace_back(tunnel(leaderID)->exec("hdel", "myhash", "a"));
355
356
357
358
359
360
361
362
363
364
  futures.emplace_back(tunnel(leaderID)->exec("sadd", "myhash", "wrongtype"));
  futures.emplace_back(tunnel(leaderID)->exec("exists", "myhash"));
  futures.emplace_back(tunnel(leaderID)->exec("hdel", "myhash", "c"));
  futures.emplace_back(tunnel(leaderID)->exec("exists", "myhash"));
  futures.emplace_back(tunnel(leaderID)->exec("sadd", "myhash", "wrongtype"));
  futures.emplace_back(tunnel(leaderID)->exec("exists", "myhash"));
  futures.emplace_back(tunnel(leaderID)->exec("hset", "myhash", "a", "b"));
  futures.emplace_back(tunnel(leaderID)->exec("srem", "myhash", "wrongtype"));
  futures.emplace_back(tunnel(leaderID)->exec("exists", "myhash"));
  futures.emplace_back(tunnel(leaderID)->exec("hset", "myhash", "a", "b"));
365
  futures.emplace_back(tunnel(leaderID)->exec("exists", "myhash", "myhash", "asdf"));
366
367
368
369
370
  futures.emplace_back(tunnel(leaderID)->exec("hexists", "myhash", "a"));
  futures.emplace_back(tunnel(leaderID)->exec("hexists", "myhash", "b"));
  futures.emplace_back(tunnel(leaderID)->exec("sismember", "myhash", "b"));
  futures.emplace_back(tunnel(leaderID)->exec("scard", "myhash"));
  futures.emplace_back(tunnel(leaderID)->exec("scard", "does-not-exist"));
371
  futures.emplace_back(tunnel(leaderID)->exec("quarkdb_invalid_command"));
372
  futures.emplace_back(tunnel(leaderID)->exec("raft-fetch-last", "7", "raw"));
373

374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
  size_t count = 0;
  ASSERT_REPLY(futures[count++], 1);
  ASSERT_REPLY(futures[count++], 1);
  ASSERT_REPLY(futures[count++], 1);
  ASSERT_REPLY(futures[count++], 0);
  ASSERT_REPLY(futures[count++], 2);
  ASSERT_REPLY(futures[count++], 0);
  ASSERT_REPLY(futures[count++], "ERR Invalid argument: WRONGTYPE Operation against a key holding the wrong kind of value");
  ASSERT_REPLY(futures[count++], 1);
  ASSERT_REPLY(futures[count++], 1);
  ASSERT_REPLY(futures[count++], 0);
  ASSERT_REPLY(futures[count++], 1);
  ASSERT_REPLY(futures[count++], 1);
  ASSERT_REPLY(futures[count++], "ERR Invalid argument: WRONGTYPE Operation against a key holding the wrong kind of value");
  ASSERT_REPLY(futures[count++], 1);
  ASSERT_REPLY(futures[count++], 0);
  ASSERT_REPLY(futures[count++], 1);
  ASSERT_REPLY(futures[count++], 2);
  ASSERT_REPLY(futures[count++], 1);
  ASSERT_REPLY(futures[count++], 0);
  ASSERT_REPLY(futures[count++], "ERR Invalid argument: WRONGTYPE Operation against a key holding the wrong kind of value");
  ASSERT_REPLY(futures[count++], "ERR Invalid argument: WRONGTYPE Operation against a key holding the wrong kind of value");
  ASSERT_REPLY(futures[count++], 0);
397
  ASSERT_REPLY(futures[count++], "ERR internal dispatching error");
398

399
400
401
402
403
404
405
406
407
408
409
  redisReplyPtr entries = futures[count++].get();
  std::vector<RaftEntry> lastEntries;

  ASSERT_TRUE(RaftParser::fetchLastResponse(entries, lastEntries));
  for(size_t i = 1; i <= 7; i++) {
    RaftEntry comparison;
    LogIndex index = journal(leaderID)->getLogSize() - i;
    ASSERT_OK(journal(leaderID)->fetch(index, comparison));
    ASSERT_EQ(lastEntries[7 - i], comparison);
  }

410
411
  futures.clear();
  futures.emplace_back(tunnel(leaderID)->exec("set", "mystring", "asdf"));
412
413
  futures.emplace_back(tunnel(leaderID)->exec("keys", "*"));
  futures.emplace_back(tunnel(leaderID)->exec("exists", "mystring", "myset", "myhash", "adfa", "myhash"));
414
  futures.emplace_back(tunnel(leaderID)->exec("del", "myhash", "myset", "mystring"));
415
  futures.emplace_back(tunnel(leaderID)->exec("exists", "mystring", "myset", "myhash", "adfa", "myhash"));
416
  futures.emplace_back(tunnel(leaderID)->exec("del", "myhash", "myset"));
417
  futures.emplace_back(tunnel(leaderID)->exec("clock-get"));
418
419

  ASSERT_REPLY(futures[0], "OK");
420
  ASSERT_REPLY(futures[1], make_vec("myhash", "myset", "mystring"));
421
422
423
424
  ASSERT_REPLY(futures[2], 4);
  ASSERT_REPLY(futures[3], 3);
  ASSERT_REPLY(futures[4], 0);
  ASSERT_REPLY(futures[5], 0);
425
  qdb_info(qclient::describeRedisReply(futures[6].get()));
426
427
428
429
430
431
432

  futures.clear();
  futures.emplace_back(tunnel(leaderID)->exec("set", "a", "aa"));
  futures.emplace_back(tunnel(leaderID)->exec("set", "aa", "a"));
  futures.emplace_back(tunnel(leaderID)->exec("get", "a"));
  futures.emplace_back(tunnel(leaderID)->exec("del", "a"));
  futures.emplace_back(tunnel(leaderID)->exec("get", "aa"));
433
  futures.emplace_back(tunnel(leaderID)->exec("keys", "*"));
434
435
436
437
438
439

  ASSERT_REPLY(futures[0], "OK");
  ASSERT_REPLY(futures[1], "OK");
  ASSERT_REPLY(futures[2], "aa");
  ASSERT_REPLY(futures[3], 1);
  ASSERT_REPLY(futures[4], "a");
440
  ASSERT_REPLY(futures[5], make_vec("aa"));
441
442

  futures.clear();
443
444
  futures.emplace_back(tunnel(leaderID)->exec("config_getall"));
  futures.emplace_back(tunnel(leaderID)->exec("config_set", "some.config.value", "1234"));
445
446
  futures.emplace_back(tunnel(leaderID)->exec("flushall"));
  futures.emplace_back(tunnel(leaderID)->exec("del", "aa"));
447
448
449
  futures.emplace_back(tunnel(leaderID)->exec("config_get", "some.config.value", "1234"));
  futures.emplace_back(tunnel(leaderID)->exec("config_get", "some.config.value"));
  futures.emplace_back(tunnel(leaderID)->exec("config_getall"));
450

451
452
453
454
455
456
457
  ASSERT_REPLY(futures[0], "");
  ASSERT_REPLY(futures[1], "OK");
  ASSERT_REPLY(futures[2], "OK");
  ASSERT_REPLY(futures[3], 0);
  ASSERT_REPLY(futures[4], "ERR wrong number of arguments for 'config_get' command");
  ASSERT_REPLY(futures[5], "1234");
  ASSERT_REPLY(futures[6], make_vec("some.config.value", "1234"));
458
459
460
461
462
463

  futures.clear();
  futures.emplace_back(tunnel(leaderID)->exec("hset", "hash", "key1", "v1"));
  futures.emplace_back(tunnel(leaderID)->exec("hset", "hash2", "key1", "v1"));
  futures.emplace_back(tunnel(leaderID)->exec("exists", "hash", "hash2"));
  futures.emplace_back(tunnel(leaderID)->exec("del", "hash"));
464
465
  futures.emplace_back(tunnel(leaderID)->exec("raft_info"));
  futures.emplace_back(tunnel(leaderID)->exec("bad_command"));
466
467
  futures.emplace_back(tunnel(leaderID)->exec("exists", "hash"));
  futures.emplace_back(tunnel(leaderID)->exec("exists", "hash2"));
468
  futures.emplace_back(tunnel(leaderID)->exec("raft_info", "leader"));
469
  futures.emplace_back(tunnel(leaderID)->exec("recovery_get", "test"));
470
471
472
473
474

  ASSERT_REPLY(futures[0], 1);
  ASSERT_REPLY(futures[1], 1);
  ASSERT_REPLY(futures[2], 2);
  ASSERT_REPLY(futures[3], 1);
475
476
477
478
  // ignore futures[4]
  ASSERT_REPLY(futures[5], "ERR unknown command 'bad_command'");
  ASSERT_REPLY(futures[6], 0);
  ASSERT_REPLY(futures[7], 1);
479
  ASSERT_REPLY(futures[8], myself(leaderID).toString() );
480
  ASSERT_REPLY(futures[9], "ERR recovery commands not allowed, not in recovery mode");
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

  futures.clear();
  futures.emplace_back(tunnel(leaderID)->exec("hmset", "hmset_test", "f1", "v1", "f2", "v2"));
  futures.emplace_back(tunnel(leaderID)->exec("exists", "hmset_test"));
  futures.emplace_back(tunnel(leaderID)->exec("hmset", "test"));
  futures.emplace_back(tunnel(leaderID)->exec("hmset", "hmset_test", "f2", "v3", "f4"));
  futures.emplace_back(tunnel(leaderID)->exec("hget", "hmset_test", "f1"));
  futures.emplace_back(tunnel(leaderID)->exec("hlen", "hmset_test"));
  futures.emplace_back(tunnel(leaderID)->exec("hmset", "hmset_test", "f2", "value2", "f3", "value3"));
  futures.emplace_back(tunnel(leaderID)->exec("hlen", "hmset_test"));
  futures.emplace_back(tunnel(leaderID)->exec("hget", "hmset_test", "f2"));
  futures.emplace_back(tunnel(leaderID)->exec("hmset", "hmset_test", "f3", "v3"));
  futures.emplace_back(tunnel(leaderID)->exec("hget", "hmset_test", "f3"));
  futures.emplace_back(tunnel(leaderID)->exec("hlen", "hmset_test"));

  ASSERT_REPLY(futures[0], "OK");
  ASSERT_REPLY(futures[1], 1);
  ASSERT_REPLY(futures[2], "ERR wrong number of arguments for 'hmset' command");
  ASSERT_REPLY(futures[3], "ERR wrong number of arguments for 'hmset' command");
  ASSERT_REPLY(futures[4], "v1");
  ASSERT_REPLY(futures[5], 2);
  ASSERT_REPLY(futures[6], "OK");
  ASSERT_REPLY(futures[7], 3);
  ASSERT_REPLY(futures[8], "value2");
  ASSERT_REPLY(futures[9], "OK");
  ASSERT_REPLY(futures[10], "v3");
  ASSERT_REPLY(futures[11], 3);
508
509
510
511

  futures.clear();
  futures.emplace_back(tunnel(leaderID)->exec("lpush", "list_test", "i1", "i2", "i3", "i4"));
  futures.emplace_back(tunnel(leaderID)->exec("exists", "list_test"));
Georgios Bitzes's avatar
Georgios Bitzes committed
512
  futures.emplace_back(tunnel(leaderID)->exec("llen", "list_test"));
513
  futures.emplace_back(tunnel(leaderID)->exec("lpop", "list_test"));
Georgios Bitzes's avatar
Georgios Bitzes committed
514
  futures.emplace_back(tunnel(leaderID)->exec("llen", "list_test"));
515
  futures.emplace_back(tunnel(leaderID)->exec("rpop", "list_test"));
Georgios Bitzes's avatar
Georgios Bitzes committed
516
  futures.emplace_back(tunnel(leaderID)->exec("llen", "list_test"));
517
  futures.emplace_back(tunnel(leaderID)->exec("del", "list_test"));
Georgios Bitzes's avatar
Georgios Bitzes committed
518
  futures.emplace_back(tunnel(leaderID)->exec("llen", "list_test"));
519
520
521
522
523
524
525
526
527
528
  futures.emplace_back(tunnel(leaderID)->exec("lpop", "list_test"));
  futures.emplace_back(tunnel(leaderID)->exec("rpush", "list_test", "i5", "i6", "i7", "i8"));
  futures.emplace_back(tunnel(leaderID)->exec("set", "list_test", "asdf"));
  futures.emplace_back(tunnel(leaderID)->exec("lpop", "list_test"));
  futures.emplace_back(tunnel(leaderID)->exec("rpop", "list_test"));
  futures.emplace_back(tunnel(leaderID)->exec("rpop", "list_test"));
  futures.emplace_back(tunnel(leaderID)->exec("lpop", "list_test"));
  futures.emplace_back(tunnel(leaderID)->exec("set", "list_test", "asdf"));
  futures.emplace_back(tunnel(leaderID)->exec("lpop", "list_test"));

Georgios Bitzes's avatar
Georgios Bitzes committed
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
  int i = 0;
  ASSERT_REPLY(futures[i++], 4);
  ASSERT_REPLY(futures[i++], 1);
  ASSERT_REPLY(futures[i++], 4);
  ASSERT_REPLY(futures[i++], "i4");
  ASSERT_REPLY(futures[i++], 3);
  ASSERT_REPLY(futures[i++], "i1");
  ASSERT_REPLY(futures[i++], 2);
  ASSERT_REPLY(futures[i++], 1);
  ASSERT_REPLY(futures[i++], 0);
  ASSERT_NIL(futures[i++]);
  ASSERT_REPLY(futures[i++], 4);
  ASSERT_REPLY(futures[i++], "ERR Invalid argument: WRONGTYPE Operation against a key holding the wrong kind of value");
  ASSERT_REPLY(futures[i++], "i5");
  ASSERT_REPLY(futures[i++], "i8");
  ASSERT_REPLY(futures[i++], "i7");
  ASSERT_REPLY(futures[i++], "i6");
  ASSERT_REPLY(futures[i++], "OK");
  ASSERT_REPLY(futures[i++], "ERR Invalid argument: WRONGTYPE Operation against a key holding the wrong kind of value");
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580

  // Now test qclient callbacks, ensure things stay reasonable when we mix them
  // with futures.
  TrivialQCallback c1;
  tunnel(leaderID)->execCB(&c1, "set", "qcl-counter", "1");

  TrivialQCallback c2;
  tunnel(leaderID)->execCB(&c2, "get", "qcl-counter");

  std::future<redisReplyPtr> fut1 = tunnel(leaderID)->exec("get", "qcl-counter");
  std::future<redisReplyPtr> fut2 = tunnel(leaderID)->exec("set", "qcl-counter", "2");
  std::future<redisReplyPtr> fut3 = tunnel(leaderID)->exec("get", "qcl-counter");

  TrivialQCallback c3;
  tunnel(leaderID)->execCB(&c3, "get", "qcl-counter");

  TrivialQCallback c4;
  tunnel(leaderID)->execCB(&c4, "set", "qcl-counter", "3");

  TrivialQCallback c5;
  tunnel(leaderID)->execCB(&c5, "get", "qcl-counter");

  std::future<redisReplyPtr> fut4 = tunnel(leaderID)->exec("get", "qcl-counter");

  ASSERT_REPLY(c1.getFuture(), "OK");
  ASSERT_REPLY(c2.getFuture(), "1");
  ASSERT_REPLY(fut1, "1");
  ASSERT_REPLY(fut2, "OK");
  ASSERT_REPLY(fut3, "2");
  ASSERT_REPLY(c3.getFuture(), "2");
  ASSERT_REPLY(c4.getFuture(), "OK");
  ASSERT_REPLY(c5.getFuture(), "3");
  ASSERT_REPLY(fut4, "3");
581
582

  // Test lease commands.
583
  std::future<redisReplyPtr> l0 = tunnel(leaderID)->exec("lease-acquire", "qcl-counter", "holder1", "10000");
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
  std::future<redisReplyPtr> l1 = tunnel(leaderID)->exec("lease-acquire", "mykey", "holder1", "10000");
  std::future<redisReplyPtr> l2 = tunnel(leaderID)->exec("lease-acquire", "mykey", "holder2", "10000");
  std::future<redisReplyPtr> l3 = tunnel(leaderID)->exec("lease-acquire", "mykey", "holder1", "10000");

  ASSERT_REPLY(l0, "ERR Invalid Argument: WRONGTYPE Operation against a key holding the wrong kind of value");
  ASSERT_REPLY(l1, "ACQUIRED");

  redisReplyPtr replyL2 = l2.get();
  std::string reply = std::string(replyL2->str, replyL2->len);
  ASSERT_TRUE(StringUtils::startswith(reply, "ERR lease held by 'holder1', time remaining"));
  ASSERT_REPLY(l3, "RENEWED");

  std::future<redisReplyPtr> l4 = tunnel(leaderID)->exec("lease-get", "mykey");
  std::future<redisReplyPtr> l5 = tunnel(leaderID)->exec("lease-get", "mykey-2");

  redisReplyPtr replyL4 = l4.get();
  qdb_info(qclient::describeRedisReply(replyL4));
  ASSERT_TRUE(StringUtils::startswith(qclient::describeRedisReply(replyL4), "1) HOLDER: holder1\n2) REMAINING: "));
  ASSERT_NIL(l5);
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625

  std::future<redisReplyPtr> l6 = tunnel(leaderID)->exec("lease-release", "mykey");
  std::future<redisReplyPtr> l7 = tunnel(leaderID)->exec("lease-release", "mykey-2");
  std::future<redisReplyPtr> l8 = tunnel(leaderID)->exec("lease-release", "qcl-counter");
  std::future<redisReplyPtr> l9 = tunnel(leaderID)->exec("lease-release", "mykey");
  std::future<redisReplyPtr> l10 = tunnel(leaderID)->exec("lease-get", "mykey");

  ASSERT_REPLY(l6, "OK");
  ASSERT_NIL(l7);
  ASSERT_REPLY(l8, "ERR Invalid argument: WRONGTYPE Operation against a key holding the wrong kind of value");
  ASSERT_NIL(l9);
  ASSERT_NIL(l10);

  std::future<redisReplyPtr> l11 = tunnel(leaderID)->exec("lease-acquire", "mykey", "holder2", "10000");
  std::future<redisReplyPtr> l12 = tunnel(leaderID)->exec("lease-acquire", "mykey", "holder2", "10000");
  std::future<redisReplyPtr> l13 = tunnel(leaderID)->exec("lease-release", "mykey");
  std::future<redisReplyPtr> l14 = tunnel(leaderID)->exec("lease-acquire", "mykey", "holder2", "10000");

  ASSERT_REPLY(l11, "ACQUIRED");
  ASSERT_REPLY(l12, "RENEWED");
  ASSERT_REPLY(l13, "OK");
  ASSERT_REPLY(l14, "ACQUIRED");

626
627
628
  // Ensure the followers return the correct number of responses on MOVED for
  // pipelined writes.
  int follower1 = (leaderID+1) % 3;
629

630
631
632
633
634
635
636
637
638
639
640
  std::vector<std::future<redisReplyPtr>> moved;
  for(size_t i = 0; i < 10; i++) {
    moved.emplace_back(tunnel(follower1)->exec("set", "abc", "123"));
  }

  for(size_t i = 0; i < 10; i++) {
    ASSERT_REPLY(moved[i], SSTR("MOVED 0 " << myself(leaderID).toString()));
  }

  // Make sure the connection did not hang.
  ASSERT_REPLY(tunnel(follower1)->exec("ping", "zxcvbnm"), "zxcvbnm");
641
642
}

643
644
TEST_F(Raft_e2e, replication_with_trimmed_journal) {
  spinup(0); spinup(1);
645
  RETRY_ASSERT_TRUE(checkStateConsensus(0, 1));
646

647
  int leaderID = getServerID(state(0)->getSnapshot()->leader);
648
649
650
651
  int firstSlaveID = (leaderID+1)%2;
  ASSERT_GE(leaderID, 0);
  ASSERT_LE(leaderID, 1);

652
  // First, disable automatic resilvering..
653
  EncodedConfigChange configChange = raftconfig(leaderID)->setResilveringEnabled(false);
654
655
  ASSERT_TRUE(configChange.error.empty());
  ASSERT_REPLY(tunnel(leaderID)->execute(configChange.request), "OK");
656
657

  // send off many requests, pipeline them
658
  std::vector<std::future<redisReplyPtr>> futures;
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
  for(size_t i = 0; i < testreqs.size(); i++) {
    futures.emplace_back(tunnel(leaderID)->execute(testreqs[i]));
  }

  for(size_t i = 0; i < 2; i++) {
    ASSERT_REPLY(futures[i], "OK");
  }

  for(size_t i = 2; i < futures.size(); i++) {
    ASSERT_REPLY(futures[i], 1);
  }

  // now let's trim leader's journal..
  journal(leaderID)->trimUntil(4);

  // and verify it's NOT possible to bring node #2 up to date
  spinup(2);
676
  RETRY_ASSERT_TRUE(checkStateConsensus(0, 1, 2));
677
678
679
680
681
682
683
684
685

  ASSERT_EQ(journal(2)->getLogSize(), 1);
  ASSERT_EQ(journal(2)->getLogStart(), 0);

  // a divine intervention fills up the missing entries in node #2 journal
  for(size_t i = 1; i < 5; i++) {
    RaftEntry entry;
    ASSERT_TRUE(journal(firstSlaveID)->fetch(i, entry).ok());

686
    journal(2)->append(i, entry);
687
688
689
  }

  // now verify node #2 can be brought up to date successfully
690
691
692
693
694
  RETRY_ASSERT_TRUE(
    journal(0)->getLogSize() == journal(1)->getLogSize() &&
    journal(1)->getLogSize() == journal(2)->getLogSize()
  );

695
696
  ASSERT_EQ(journal(2)->getLogSize(), journal(leaderID)->getLogSize());
  ASSERT_EQ(journal(2)->getLogSize(), journal(firstSlaveID)->getLogSize());
697
698
699

  // Verify resilvering didn't happen.
  ASSERT_EQ(journal(2)->getLogStart(), 0);
700
}
Georgios Bitzes's avatar
Georgios Bitzes committed
701
702
703

TEST_F(Raft_e2e, membership_updates) {
  spinup(0); spinup(1); spinup(2);
704
  RETRY_ASSERT_TRUE(checkStateConsensus(0, 1, 2));
705
  int leaderID = getServerID(state(0)->getSnapshot()->leader);
Georgios Bitzes's avatar
Georgios Bitzes committed
706
707
708
709
  ASSERT_REPLY(tunnel(leaderID)->exec("set", "pi", "3.141516"), "OK");

  // throw a node out of the cluster
  int victim = (leaderID+1) % 3;
710
  RETRY_ASSERT_TRUE(checkFullConsensus(0, 1, 2));
711
  int index = journal(leaderID)->getLogSize() - 1;
Georgios Bitzes's avatar
Georgios Bitzes committed
712
  ASSERT_REPLY(tunnel(leaderID)->exec("RAFT_REMOVE_MEMBER", myself(victim).toString()), "OK");
713
  RETRY_ASSERT_TRUE(dispatcher(leaderID)->info().commitIndex == index + 1);
Georgios Bitzes's avatar
Georgios Bitzes committed
714
715

  // verify the cluster has not been disrupted
716
  ASSERT_EQ(state(leaderID)->getSnapshot()->leader, myself(leaderID));
Georgios Bitzes's avatar
Georgios Bitzes committed
717
718
719

  // add it back as an observer, verify consensus
  ASSERT_REPLY(tunnel(leaderID)->exec("RAFT_ADD_OBSERVER", myself(victim).toString()), "OK");
720

721
722
723
  RETRY_ASSERT_TRUE(dispatcher(0)->info().commitIndex == index + 2);
  RETRY_ASSERT_TRUE(dispatcher(1)->info().commitIndex == index + 2);
  RETRY_ASSERT_TRUE(dispatcher(2)->info().commitIndex == index + 2);
724

725
  ASSERT_EQ(state(victim)->getSnapshot()->status, RaftStatus::FOLLOWER);
Georgios Bitzes's avatar
Georgios Bitzes committed
726

727
728
  ASSERT_EQ(state(0)->getSnapshot()->leader, state(1)->getSnapshot()->leader);
  ASSERT_EQ(state(1)->getSnapshot()->leader, state(2)->getSnapshot()->leader);
Georgios Bitzes's avatar
Georgios Bitzes committed
729
730
731
732
733

  ASSERT_EQ(journal(0)->getLogSize(), journal(1)->getLogSize());
  ASSERT_EQ(journal(1)->getLogSize(), journal(2)->getLogSize());

  // cannot be a leader, it's an observer
734
  ASSERT_NE(state(0)->getSnapshot()->leader, myself(victim));
Georgios Bitzes's avatar
Georgios Bitzes committed
735
736

  // add back as a full voting member
737
  leaderID = getServerID(state(0)->getSnapshot()->leader);
Georgios Bitzes's avatar
Georgios Bitzes committed
738
  ASSERT_REPLY(tunnel(leaderID)->exec("RAFT_PROMOTE_OBSERVER", myself(victim).toString()), "OK");
739
  RETRY_ASSERT_TRUE(dispatcher(leaderID)->info().commitIndex == index + 3);
740
  RETRY_ASSERT_TRUE(checkStateConsensus(0, 1, 2));
Georgios Bitzes's avatar
Georgios Bitzes committed
741
742
}

743
744
TEST_F(Raft_e2e, reject_dangerous_membership_update) {
  spinup(0); spinup(1);
Georgios Bitzes's avatar
Georgios Bitzes committed
745
  RETRY_ASSERT_TRUE(checkFullConsensus(0, 1));
746
747
748
749
750
751
  int leaderID = getLeaderID();

  // make sure dangerous node removal is prevented
  int victim = (leaderID+1) % 2;
  redisReplyPtr reply = tunnel(leaderID)->exec("RAFT_REMOVE_MEMBER", myself(victim).toString()).get();
  ASSERT_ERR(reply, "ERR membership update blocked, new cluster would not have an up-to-date quorum");
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770

  // Try to remove a non-existent node
  ASSERT_REPLY(tunnel(leaderID)->exec("RAFT_REMOVE_MEMBER", RaftServer("random_host", 123).toString()),
    "ERR random_host:123 is neither an observer nor a full node.");

  // Make sure we can remove the third node
  ASSERT_REPLY(tunnel(leaderID)->exec("RAFT_REMOVE_MEMBER", myself(2).toString()), "OK");
  RaftMembership membership = journal(leaderID)->getMembership();
  RETRY_ASSERT_TRUE(journal(leaderID)->getCommitIndex() == membership.epoch);

  // Add it back as observer
  ASSERT_REPLY(tunnel(leaderID)->exec("RAFT_ADD_OBSERVER", myself(2).toString()), "OK");
  membership = journal(leaderID)->getMembership();
  RETRY_ASSERT_TRUE(journal(leaderID)->getCommitIndex() == membership.epoch);

  // Remove it again
  ASSERT_REPLY(tunnel(leaderID)->exec("RAFT_REMOVE_MEMBER", myself(2).toString()), "OK");
  membership = journal(leaderID)->getMembership();
  RETRY_ASSERT_TRUE(journal(leaderID)->getCommitIndex() == membership.epoch);
771
772
}

Georgios Bitzes's avatar
Georgios Bitzes committed
773
774
775
TEST_F(Raft_e2e5, membership_updates_with_disruptions) {
  // let's get this party started
  spinup(0); spinup(1); spinup(2); spinup(3);
776
  RETRY_ASSERT_TRUE(checkStateConsensus(0, 1, 2, 3));
Georgios Bitzes's avatar
Georgios Bitzes committed
777
778

  // throw node #4 out of the cluster
779
  int leaderID = getServerID(state(0)->getSnapshot()->leader);
Georgios Bitzes's avatar
Georgios Bitzes committed
780
  ASSERT_REPLY(tunnel(leaderID)->exec("RAFT_REMOVE_MEMBER", myself(4).toString()), "OK");
781
782
783
784
785
786

  // wait until membership update has been committed
  RaftMembership membership = journal(leaderID)->getMembership();
  ASSERT_GT(membership.epoch, 0u);
  ASSERT_EQ(membership.nodes.size(), 4u);
  RETRY_ASSERT_TRUE(journal(leaderID)->getCommitIndex() == membership.epoch);
Georgios Bitzes's avatar
Georgios Bitzes committed
787
788
789

  // .. and now spinup node #4 :> Ensure it doesn't disrupt the current leader
  spinup(4);
790
  std::this_thread::sleep_for(raftclock()->getTimeouts().getHigh()*2);
791
  ASSERT_EQ(leaderID, getServerID(state(0)->getSnapshot()->leader));
Georgios Bitzes's avatar
Georgios Bitzes committed
792
793

  // verify the cluster has not been disrupted
794
  ASSERT_EQ(state(leaderID)->getSnapshot()->leader, myself(leaderID));
Georgios Bitzes's avatar
Georgios Bitzes committed
795
796
797
798
799
800

  // remove one more node
  int victim = (leaderID+1) % 5;
  if(victim == 4) victim = 2;

  ASSERT_REPLY(tunnel(leaderID)->exec("RAFT_REMOVE_MEMBER", myself(victim).toString()), "OK");
801
  std::this_thread::sleep_for(raftclock()->getTimeouts().getHigh()*2);
Georgios Bitzes's avatar
Georgios Bitzes committed
802
803

  // verify the cluster has not been disrupted
804
  ASSERT_EQ(state(leaderID)->getSnapshot()->leader, myself(leaderID));
Georgios Bitzes's avatar
Georgios Bitzes committed
805
806
807
808
809

  // issue a bunch of writes and reads
  ASSERT_REPLY(tunnel(leaderID)->exec("set", "123", "abc"), "OK");
  ASSERT_REPLY(tunnel(leaderID)->exec("get", "123"), "abc");
}
810

Georgios Bitzes's avatar
Georgios Bitzes committed
811
TEST_F(Raft_e2e, leader_steps_down_after_follower_loss) {
812
813
814
815
816
817
818
819
820
821
822
823
824
825
  // cluster with 2 nodes
  spinup(0); spinup(1);
  RETRY_ASSERT_TRUE(checkStateConsensus(0, 1));

  int leaderID = getLeaderID();
  ASSERT_GE(leaderID, 0);
  ASSERT_LE(leaderID, 1);

  RaftTerm term = state(leaderID)->getCurrentTerm();

  int followerID = (leaderID + 1)%2;
  spindown(followerID);

  RETRY_ASSERT_TRUE(term < state(leaderID)->getCurrentTerm());
826
  ASSERT_TRUE(state(leaderID)->getSnapshot()->leader.empty());
827
}
828
829
830
831
832
833
834
835
836
837
838

TEST_F(Raft_e2e, stale_reads) {
  spinup(0); spinup(1); spinup(2);
  RETRY_ASSERT_TRUE(checkStateConsensus(0, 1, 2));

  int leaderID = getLeaderID();
  int follower = (getLeaderID() + 1) % 3;

  ASSERT_REPLY(tunnel(leaderID)->exec("set", "abc", "1234"), "OK");
  ASSERT_REPLY(tunnel(follower)->exec("get", "abc"), SSTR("MOVED 0 " << myself(leaderID).toString()));

Georgios Bitzes's avatar
Georgios Bitzes committed
839
  ASSERT_REPLY(tunnel(follower)->exec("activate-stale-reads"), "OK");
840
841
842
843
844
845
846

  redisReplyPtr reply = tunnel(follower)->exec("get", "abc").get();
  qdb_info("Race-y read: " << std::string(reply->str, reply->len));

  RETRY_ASSERT_TRUE(checkFullConsensus(0, 1, 2));
  ASSERT_REPLY(tunnel(follower)->exec("get", "abc"), "1234");
}
Georgios Bitzes's avatar
Georgios Bitzes committed
847
848
849
850
851
852
853

TEST_F(Raft_e2e, monitor) {
  spinup(0); spinup(1); spinup(2);
  RETRY_ASSERT_TRUE(checkStateConsensus(0, 1, 2));

  int leaderID = getLeaderID();

854
855
856
857
858
  // Get connection ID
  redisReplyPtr connIDReply = tunnel(leaderID)->exec("client-id").get();
  std::string connID(connIDReply->str, connIDReply->len);
  qdb_info("Connection ID: " << connID);

Georgios Bitzes's avatar
Georgios Bitzes committed
859
860
861
862
863
864
865
  // We can't use QClient for this, it can't handle the output of MONITOR
  qclient::ConnectionInitiator initiator("localhost", myself(leaderID).port);
  ASSERT_TRUE(initiator.ok());

  Link link(initiator.getFd());
  BufferedReader reader(&link);

866
867
868
869
870
  ASSERT_EQ(link.Send(SSTR("*2\r\n$4\r\nAUTH\r\n$" << contactDetails()->getPassword().size() << "\r\n" << contactDetails()->getPassword() << "\r\n")), 56);
  std::string response;
  RETRY_ASSERT_TRUE(reader.consume(5, response));
  ASSERT_EQ(response, "+OK\r\n");

Georgios Bitzes's avatar
Georgios Bitzes committed
871
872
873
874
875
876
  ASSERT_EQ(link.Send("*1\r\n$7\r\nMONITOR\r\n"), 17);
  ASSERT_EQ(link.Send("random string"), 13);

  RETRY_ASSERT_TRUE(reader.consume(5, response));
  ASSERT_EQ(response, "+OK\r\n");

877
  tunnel(leaderID)->exec("set", "abc", "aaaa" "\xab" "bbb");
Georgios Bitzes's avatar
Georgios Bitzes committed
878
  response.clear();
879
880
881
882

  std::string expectedReply = SSTR("+ [" << connID << "]: \"set\" \"abc\" \"aaaa\\xABbbb\"\r\n");
  RETRY_ASSERT_TRUE(reader.consume(expectedReply.size(), response));
  ASSERT_EQ(response, expectedReply);
Georgios Bitzes's avatar
Georgios Bitzes committed
883
884
885

  tunnel(leaderID)->exec("get", "abc");
  response.clear();
886
887
888
889

  expectedReply = SSTR("+ [" << connID << "]: \"get\" \"abc\"\r\n");
  RETRY_ASSERT_TRUE(reader.consume(expectedReply.size(), response));
  ASSERT_EQ(response, expectedReply);
Georgios Bitzes's avatar
Georgios Bitzes committed
890
}
Georgios Bitzes's avatar
Georgios Bitzes committed
891

892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
class PingCallback : qclient::QCallback {
public:
  PingCallback(qclient::QClient &q) : qcl(q) {
    flag = prom.get_future();
    qcl.execCB(this, "PING", SSTR(pingCounter));
  }

  void finalize(bool result) {
    isOk = result;
    prom.set_value();
  }

  virtual void handleResponse(redisReplyPtr &&reply) {
    if(!reply) finalize(false);
    if(reply->type != REDIS_REPLY_STRING) finalize(false);
    if(std::string(reply->str, reply->len) != SSTR(pingCounter)) finalize(false);
    qdb_info("Received successful ping response: " << pingCounter);

    pingCounter++;
    if(pingCounter == 5) return finalize(true);

    qcl.execCB(this, "PING", SSTR(pingCounter));
  }

  bool ok() {
    return isOk;
  }

  void wait() {
    flag.get();
  }

private:
  size_t pingCounter = 0;

  std::promise<void> prom;
  std::future<void> flag;
  bool isOk = true;
  qclient::QClient &qcl;
};

TEST_F(Raft_e2e, PingExtravaganza) {
  // A most efficient and sophisticated ping machinery.
  spinup(0); spinup(1); spinup(2);
  RETRY_ASSERT_TRUE(checkStateConsensus(0, 1, 2));
  int leaderID = getLeaderID();

  PingCallback pinger(*tunnel(leaderID));
  pinger.wait();
  ASSERT_TRUE(pinger.ok());
}

Georgios Bitzes's avatar
Georgios Bitzes committed
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
TEST_F(Raft_e2e, hincrbymulti) {
  spinup(0); spinup(1); spinup(2);
  RETRY_ASSERT_TRUE(checkStateConsensus(0, 1, 2));

  int leaderID = getLeaderID();

  ASSERT_REPLY(tunnel(leaderID)->exec("hincrbymulti", "h1", "h2", "3", "h2", "h3", "4"), 7);
  ASSERT_REPLY(tunnel(leaderID)->exec("hget", "h1", "h2"), "3");
  ASSERT_REPLY(tunnel(leaderID)->exec("hget", "h2", "h3"), "4");

  ASSERT_REPLY(tunnel(leaderID)->exec("hincrbymulti", "h1", "h2", "-5", "h2", "h3", "20", "h4", "h8"), "ERR wrong number of arguments for 'hincrbymulti' command");
  ASSERT_REPLY(tunnel(leaderID)->exec("hincrbymulti", "h1", "h2", "-5", "h2", "h3", "20", "h4", "h8", "13"), 35);

  ASSERT_REPLY(tunnel(leaderID)->exec("hget", "h1", "h2"), "-2");
  ASSERT_REPLY(tunnel(leaderID)->exec("hget", "h2", "h3"), "24");
  ASSERT_REPLY(tunnel(leaderID)->exec("hget", "h4", "h8"), "13");
}
Georgios Bitzes's avatar
Georgios Bitzes committed
961

Georgios Bitzes's avatar
Georgios Bitzes committed
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
TEST_F(Raft_e2e, smove) {
  spinup(0); spinup(1); spinup(2);
  RETRY_ASSERT_TRUE(checkStateConsensus(0, 1, 2));
  int leaderID = getLeaderID();

  ASSERT_REPLY(tunnel(leaderID)->exec("sadd", "set1", "i1", "i2", "i3", "i4", "i5"), 5);
  ASSERT_REPLY(tunnel(leaderID)->exec("scard", "set1"), 5);

  ASSERT_REPLY(tunnel(leaderID)->exec("sadd", "set2", "t1", "t2", "t3", "t4", "t5"), 5);
  ASSERT_REPLY(tunnel(leaderID)->exec("scard", "set2"), 5);

  ASSERT_REPLY(tunnel(leaderID)->exec("set", "mykey", "myval"), "OK");

  ASSERT_REPLY(tunnel(leaderID)->exec("smove", "set1", "mykey", "i1"), "ERR Invalid argument: WRONGTYPE Operation against a key holding the wrong kind of value");
  ASSERT_REPLY(tunnel(leaderID)->exec("smove", "mykey", "set1", "i1"), "ERR Invalid argument: WRONGTYPE Operation against a key holding the wrong kind of value");

  ASSERT_REPLY(tunnel(leaderID)->exec("scard", "set1"), 5);
  ASSERT_REPLY(tunnel(leaderID)->exec("scard", "set2"), 5);

  ASSERT_REPLY(tunnel(leaderID)->exec("smove", "set1", "set2", "i1"), 1);
  ASSERT_REPLY(tunnel(leaderID)->exec("scard", "set1"), 4);
  ASSERT_REPLY(tunnel(leaderID)->exec("scard", "set2"), 6);

  ASSERT_REPLY(tunnel(leaderID)->exec("smembers", "set1"), make_vec("i2", "i3", "i4", "i5"));
  ASSERT_REPLY(tunnel(leaderID)->exec("smembers", "set2"), make_vec("i1", "t1", "t2", "t3", "t4", "t5"));

  ASSERT_REPLY(tunnel(leaderID)->exec("smove", "set1", "set2", "not-existing"), 0);
  ASSERT_REPLY(tunnel(leaderID)->exec("scard", "set1"), 4);
  ASSERT_REPLY(tunnel(leaderID)->exec("scard", "set2"), 6);

  ASSERT_REPLY(tunnel(leaderID)->exec("sadd", "set1", "i1"), 1);

  ASSERT_REPLY(tunnel(leaderID)->exec("smembers", "set1"), make_vec("i1", "i2", "i3", "i4", "i5"));
  ASSERT_REPLY(tunnel(leaderID)->exec("smembers", "set2"), make_vec("i1", "t1", "t2", "t3", "t4", "t5"));

  ASSERT_REPLY(tunnel(leaderID)->exec("scard", "set1"), 5);
  ASSERT_REPLY(tunnel(leaderID)->exec("scard", "set2"), 6);

  ASSERT_REPLY(tunnel(leaderID)->exec("smove", "set1", "set2", "i1"), 1);
  ASSERT_REPLY(tunnel(leaderID)->exec("scard", "set1"), 4);
  ASSERT_REPLY(tunnel(leaderID)->exec("scard", "set2"), 6);

  ASSERT_REPLY(tunnel(leaderID)->exec("smembers", "set1"), make_vec("i2", "i3", "i4", "i5"));
  ASSERT_REPLY(tunnel(leaderID)->exec("smembers", "set2"), make_vec("i1", "t1", "t2", "t3", "t4", "t5"));
1006
  ASSERT_REPLY(tunnel(leaderID)->exec("quarkdb-manual-compaction"), "OK");
1007
  qdb_info(qclient::describeRedisReply(tunnel(leaderID)->exec("quarkdb-level-stats").get()));
1008
  qdb_info(qclient::describeRedisReply(tunnel(leaderID)->exec("quarkdb-compression-stats").get()));
Georgios Bitzes's avatar
Georgios Bitzes committed
1009
1010
}

Georgios Bitzes's avatar
Georgios Bitzes committed
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
TEST_F(Raft_e2e, sscan) {
  spinup(0); spinup(1); spinup(2);
  RETRY_ASSERT_TRUE(checkStateConsensus(0, 1, 2));

  int leaderID = getLeaderID();

  redisReplyPtr reply = tunnel(leaderID)->exec("sscan", "myset", "0", "asdf", "123").get();
  ASSERT_ERR(reply, "ERR syntax error");

  ASSERT_REPLY(tunnel(leaderID)->exec("sadd", "myset", "a", "b", "c", "d", "e", "f", "g"), 7);

  reply = tunnel(leaderID)->exec("sscan", "myset", "0", "COUNT", "3").get();
  ASSERT_REPLY(reply, std::make_pair("next:d", make_vec("a", "b", "c")));

  reply = tunnel(leaderID)->exec("sscan", "myset", "next:d", "COUNT", "2").get();
  ASSERT_REPLY(reply, std::make_pair("next:f", make_vec("d", "e")));

  reply = tunnel(leaderID)->exec("sscan", "myset", "next:f", "COUNT", "2").get();
  ASSERT_REPLY(reply, std::make_pair("0", make_vec("f", "g")));

  reply = tunnel(leaderID)->exec("sscan", "myset", "next:zz").get();
  ASSERT_REPLY(reply, std::make_pair("0", make_vec()));

  reply = tunnel(leaderID)->exec("sscan", "not-existing", "next:zz").get();
  ASSERT_REPLY(reply, std::make_pair("0", make_vec()));
Georgios Bitzes's avatar
Georgios Bitzes committed
1036
1037
1038
1039
1040
1041
1042

  QSet qset(*tunnel(leaderID), "myset");
  auto pair = qset.sscan("0", 2);

  ASSERT_EQ(pair.first, "next:c");
  ASSERT_EQ(pair.second, make_vec("a", "b"));

1043
1044
1045
1046
  pair = qset.sscan(pair.first, 2);
  ASSERT_EQ(pair.first, "next:e");
  ASSERT_EQ(pair.second, make_vec("c", "d"));

Georgios Bitzes's avatar
Georgios Bitzes committed
1047
1048
1049
1050
1051
  QSet qset2(*tunnel(leaderID), "not-existing");
  pair = qset2.sscan("0", 2);

  ASSERT_EQ(pair.first, "0");
  ASSERT_EQ(pair.second, make_vec());
Georgios Bitzes's avatar
Georgios Bitzes committed
1052
}
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065

TEST_F(Raft_e2e, LocalityHash) {
  spinup(0); spinup(1); spinup(2);
  RETRY_ASSERT_TRUE(checkStateConsensus(0, 1, 2));

  int leaderID = getLeaderID();

  // Insert new field.
  ASSERT_REPLY(tunnel(leaderID)->exec("lhlen", "mykey"), 0);
  ASSERT_REPLY(tunnel(leaderID)->exec("lhset", "mykey", "f1", "hint1", "v1"), 1);
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f1"), "v1");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f1", "hint1"), "v1");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f1", "ayy-lmao"), "v1");
1066
1067
1068
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f1", "emptykey"), "v1");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f1", "hint1", "emptykey"), "v1");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f1", "ayy-lmao", "emptykey"), "v1");
1069
1070

  // Update old field, no changes to locality hint.
1071
  ASSERT_REPLY(tunnel(leaderID)->exec("lhset-and-del-fallback", "mykey", "f1", "hint1", "v2", "fallback"), 0);
1072
1073
1074
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f1"), "v2");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f1", "hint1"), "v2");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f1", "ayy-lmao"), "v2");
1075
1076
1077
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f1", "emptykey"), "v2");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f1", "hint1", "emptykey"), "v2");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f1", "ayy-lmao", "emptykey"), "v2");
1078
1079
1080
1081
1082
1083
1084
  ASSERT_REPLY(tunnel(leaderID)->exec("lhlen", "mykey"), 1);

  // Insert one more field.
  ASSERT_REPLY(tunnel(leaderID)->exec("lhset", "mykey", "f2", "hint2", "v3"), 1);
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f2"), "v3");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f2", "hint2"), "v3");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f2", "hint1"), "v3");
1085
1086
1087
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f2", "emptykey"), "v3");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f2", "hint2", "emptykey"), "v3");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f2", "hint1", "emptykey"), "v3");
1088
1089
1090
1091
1092
1093
1094
  ASSERT_REPLY(tunnel(leaderID)->exec("lhlen", "mykey"), 2);

  // Update locality hint of first field.
  ASSERT_REPLY(tunnel(leaderID)->exec("lhset", "mykey", "f1", "hint2", "v2"), 0);
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f1"), "v2");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f1", "hint2"), "v2");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f1", "hint1"), "v2");
1095
1096
1097
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f1", "emptykey"), "v2");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f1", "hint2", "emptykey"), "v2");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f1", "hint1", "emptykey"), "v2");
1098
1099
1100
  ASSERT_REPLY(tunnel(leaderID)->exec("lhlen", "mykey"), 2);

  // Update value and locality hint of second field.
1101
  ASSERT_REPLY(tunnel(leaderID)->exec("lhset-and-del-fallback", "mykey", "f2", "hint3", "v4", "fallback"), 0);
1102
1103
1104
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f2"), "v4");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f2", "hint3"), "v4");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f2", "hint1"), "v4");
1105
1106
1107
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f2", "emptykey"), "v4");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f2", "hint3", "emptykey"), "v4");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f2", "hint1", "emptykey"), "v4");
1108
1109
1110
1111
1112
1113
1114
  ASSERT_REPLY(tunnel(leaderID)->exec("lhlen", "mykey"), 2);

  // Insert one more field.
  ASSERT_REPLY(tunnel(leaderID)->exec("lhset", "mykey", "f3", "aaaaa", "v5"), 1);
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f3"), "v5");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f3", "aaaaa"), "v5");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f3", "wrong-hint"), "v5");
1115
1116
1117
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f3", "emptykey"), "v5");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f3", "aaaaa", "emptykey"), "v5");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f3", "wrong-hint"), "v5");
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
  ASSERT_REPLY(tunnel(leaderID)->exec("lhlen", "mykey"), 3);

  // Re-read everything.
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f2"), "v4");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f2", "hint3"), "v4");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f2", "hint1"), "v4");

  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f1"), "v2");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f1", "hint2"), "v2");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f1", "hint1"), "v2");
1128

1129
1130
1131
1132
1133
1134
1135
1136
1137
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f2", "emptykey"), "v4");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f2", "hint3", "emptykey"), "v4");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f2", "hint1", "emptykey"), "v4");

  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f1", "emptykey"), "v2");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f1", "hint2", "emptykey"), "v2");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f1", "hint1", "emptykey"), "v2");


1138
  // Delete key.
1139
1140
  ASSERT_REPLY(tunnel(leaderID)->exec("exists", "mykey"), 1);
  ASSERT_REPLY(tunnel(leaderID)->exec("exists", "mykey", "mykey"), 2);
1141
  ASSERT_REPLY(tunnel(leaderID)->exec("del", "mykey"), 1);
1142
  ASSERT_REPLY(tunnel(leaderID)->exec("exists", "mykey"), 0);
1143
1144
1145
  ASSERT_REPLY(tunnel(leaderID)->exec("lhlen", "mykey"), 0);
  ASSERT_REPLY(tunnel(leaderID)->exec("del", "mykey"), 0);
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f3", "aaaaa"), "");
1146
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f3", "aaaaa", "emptykey"), "");
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160

  // Recreate with five fields.
  ASSERT_REPLY(tunnel(leaderID)->exec("lhset", "mykey", "f1", "hint1", "v1"), 1);
  ASSERT_REPLY(tunnel(leaderID)->exec("lhset", "mykey", "f2", "hint2", "v2"), 1);
  ASSERT_REPLY(tunnel(leaderID)->exec("lhset", "mykey", "f3", "hint3", "v3"), 1);
  ASSERT_REPLY(tunnel(leaderID)->exec("lhset", "mykey", "f4", "hint4", "v4"), 1);
  ASSERT_REPLY(tunnel(leaderID)->exec("lhset", "mykey", "f5", "hint5", "v5"), 1);
  ASSERT_REPLY(tunnel(leaderID)->exec("exists", "mykey"), 1);
  ASSERT_REPLY(tunnel(leaderID)->exec("lhlen", "mykey"), 5);

  ASSERT_REPLY(tunnel(leaderID)->exec("lhdel", "mykey", "f2", "hint1"), 1);
  ASSERT_REPLY(tunnel(leaderID)->exec("lhlen", "mykey"), 4);
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f2"), "");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f2", "hint2"), "");
1161
1162
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f2", "emptykey"), "");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f2", "hint2", "emptykey"), "");
1163
1164
1165
1166
1167
1168
  ASSERT_REPLY(tunnel(leaderID)->exec("lhdel", "mykey", "f2", "hint1"), 0);
  ASSERT_REPLY(tunnel(leaderID)->exec("lhdel", "mykey", "f1", "f3"), 2);
  ASSERT_REPLY(tunnel(leaderID)->exec("lhlen", "mykey"), 2);

  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f4"), "v4");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f5"), "v5");
1169
1170
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f4", "emptykey"), "v4");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f5", "emptykey"), "v5");
1171
1172
1173

  ASSERT_REPLY(tunnel(leaderID)->exec("lhdel", "mykey", "f4", "f4", "f4", "f4"), 1);
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f4"), "");
1174
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget-with-fallback", "mykey", "f4", "emptykey"), "");
1175
1176
1177
1178
1179
1180
  ASSERT_REPLY(tunnel(leaderID)->exec("lhlen", "mykey"), 1);

  ASSERT_REPLY(tunnel(leaderID)->exec("get", "mykey"), "ERR Invalid argument: WRONGTYPE Operation against a key holding the wrong kind of value");
  ASSERT_REPLY(tunnel(leaderID)->exec("lhdel", "mykey", "f4"), 0);
  ASSERT_REPLY(tunnel(leaderID)->exec("lhlen", "mykey"), 1);
  ASSERT_REPLY(tunnel(leaderID)->exec("lhget", "mykey", "f5", "hint5"), "v5");
Georgios Bitzes's avatar