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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// -*- mode: c++; c-file-style: "k&r"; c-basic-offset: 4 -*-
/***********************************************************************
*
* ir-test.cc:
* test cases for Inconsistent Replication Protocol
*
* Copyright 2013 Dan R. K. Ports <drkp@cs.washington.edu>
* Copyright 2015 Irene Zhang Ports <iyzhang@cs.washington.edu>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
**********************************************************************/
#include "lib/configuration.h"
#include "lib/message.h"
#include "lib/transport.h"
#include "lib/simtransport.h"
#include "replication/common/client.h"
#include "replication/common/replica.h"
#include "replication/ir/client.h"
#include "replication/ir/replica.h"
#include <stdlib.h>
#include <stdio.h>
#include <gtest/gtest.h>
#include <vector>
#include <set>
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
#include <sstream>
static string replicaLastOp;
static string clientLastOp;
static string clientLastReply;
using google::protobuf::Message;
using namespace replication;
using namespace replication::ir;
using namespace replication::ir::proto;
class IRApp : public IRAppReplica {
std::vector<string> *iOps;
std::vector<string> *cOps;
std::vector<string> *unloggedOps;
public:
IRApp(std::vector<string> *i, std::vector<string> *c, std::vector<string> *u) : iOps(i), cOps(c), unloggedOps(u) { }
void ExecInconsistentUpcall(const string &req) {
iOps->push_back(req);
}
void ExecConsensusUpcall(const string &req, string &reply) {
cOps->push_back(req);
reply = "reply: " + req;
}
void UnloggedUpcall(const string &req, string &reply) {
unloggedOps->push_back(req);
reply = "unlreply: " + req;
}
};
class IRTest : public ::testing::Test
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
{
protected:
std::vector<IRReplica *> replicas;
IRClient *client;
SimulatedTransport *transport;
transport::Configuration *config;
std::vector<std::vector<string> > iOps;
std::vector<std::vector<string> > cOps;
std::vector<std::vector<string> > unloggedOps;
int requestNum;
virtual void SetUp() {
std::vector<transport::ReplicaAddress> replicaAddrs =
{ { "localhost", "12345" },
{ "localhost", "12346" },
{ "localhost", "12347" }};
config = new transport::Configuration(3, 1, replicaAddrs);
transport = new SimulatedTransport();
iOps.resize(config->n);
cOps.resize(config->n);
unloggedOps.resize(config->n);
for (int i = 0; i < config->n; i++) {
replicas.push_back(new IRReplica(*config, i, transport,
new IRApp(&iOps[i], &cOps[i], &unloggedOps[i])));
}
client = new IRClient(*config, transport);
requestNum = -1;
// Only let tests run for a simulated minute. This prevents
// infinite retry loops, etc.
// transport->Timer(60000, [&]() {
// transport->CancelAllTimers();
// });
}
virtual string RequestOp(int n) {
std::ostringstream stream;
stream << "test: " << n;
return stream.str();
}
virtual string LastRequestOp() {
return RequestOp(requestNum);
}
virtual void ClientSendNextInconsistent(Client::continuation_t upcall) {
requestNum++;
client->InvokeInconsistent(LastRequestOp(), upcall);
}
virtual void ClientSendNextConsensus(Client::continuation_t upcall, IRClient::decide_t decide) {
requestNum++;
client->InvokeConsensus(LastRequestOp(), decide, upcall);
}
virtual void ClientSendNextUnlogged(int idx, Client::continuation_t upcall,
Client::timeout_continuation_t timeoutContinuation = nullptr,
uint32_t timeout = Client::DEFAULT_UNLOGGED_OP_TIMEOUT) {
requestNum++;
client->InvokeUnlogged(idx, LastRequestOp(), upcall, timeoutContinuation, timeout);
}
virtual void TearDown() {
for (auto x : replicas) {
delete x;
}
replicas.clear();
iOps.clear();
cOps.clear();
unloggedOps.clear();
delete client;
delete transport;
delete config;
}
};
TEST_F(IRTest, OneInconsistentOp)
{
auto upcall = [this](const string &req, const string &reply) {
EXPECT_EQ(req, LastRequestOp());
// Inconsistent ops do not return a value
EXPECT_EQ(reply, "");
transport->CancelAllTimers();
};
ClientSendNextInconsistent(upcall);
transport->Run();
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
// By now, they all should have executed the last request.
for (int i = 0; i < config->n; i++) {
EXPECT_EQ(iOps[i].size(), 1);
EXPECT_EQ(iOps[i].back(), LastRequestOp());
}
}
TEST_F(IRTest, OneConsensusOp)
{
auto upcall = [this](const string &req, const string &reply) {
EXPECT_EQ(req, LastRequestOp());
EXPECT_EQ(reply, "reply: "+LastRequestOp());
transport->CancelAllTimers();
};
auto decide = [this](const std::set<string> &results) {
// shouldn't ever get called
EXPECT_FALSE(true);
return "";
};
ClientSendNextConsensus(upcall, decide);
transport->Run();
// By now, they all should have executed the last request.
for (int i = 0; i < config->n; i++) {
EXPECT_EQ(cOps[i].size(), 1);
EXPECT_EQ(cOps[i].back(), LastRequestOp());
}
}
TEST_F(IRTest, Unlogged)
{
auto upcall = [this](const string &req, const string &reply) {
EXPECT_EQ(req, LastRequestOp());
EXPECT_EQ(reply, "unlreply: "+LastRequestOp());
EXPECT_EQ(unloggedOps[1].back(), req);
transport->CancelAllTimers();
};
int timeouts = 0;
auto timeout = [&](const string &req) {
timeouts++;
};
ClientSendNextUnlogged(1, upcall, timeout);
transport->Run();
for (unsigned int i = 0; i < iOps.size(); i++) {
EXPECT_EQ(0, iOps[i].size());
EXPECT_EQ((i == 1 ? 1 : 0), unloggedOps[i].size());
}
EXPECT_EQ(0, timeouts);
}
TEST_F(IRTest, UnloggedTimeout)
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
{
auto upcall = [this](const string &req, const string &reply) {
FAIL();
transport->CancelAllTimers();
};
int timeouts = 0;
auto timeout = [&](const string &req) {
timeouts++;
};
// Drop messages to or from replica 1
transport->AddFilter(10, [](TransportReceiver *src, int srcIdx,
TransportReceiver *dst, int dstIdx,
Message &m, uint64_t &delay) {
if ((srcIdx == 1) || (dstIdx == 1)) {
return false;
}
return true;
});
// Run for 10 seconds
transport->Timer(10000, [&]() {
transport->CancelAllTimers();
});
ClientSendNextUnlogged(1, upcall, timeout);
transport->Run();
for (unsigned int i = 0; i < iOps.size(); i++) {
EXPECT_EQ(0, iOps[i].size());
EXPECT_EQ(0, unloggedOps[i].size());
}
EXPECT_EQ(1, timeouts);
}
// TEST_F(IRTest, ManyOps)
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
// {
// Client::continuation_t upcall = [&](const string &req, const string &reply) {
// EXPECT_EQ(req, LastRequestOp());
// EXPECT_EQ(reply, "reply: "+LastRequestOp());
// // Not guaranteed that any replicas except the leader have
// // executed this request.
// EXPECT_EQ(ops[0].back(), req);
// if (requestNum < 9) {
// ClientSendNext(upcall);
// } else {
// transport->CancelAllTimers();
// }
// };
// ClientSendNext(upcall);
// transport->Run();
// // By now, they all should have executed the last request.
// for (int i = 0; i < config->n; i++) {
// EXPECT_EQ(10, ops[i].size());
// for (int j = 0; j < 10; j++) {
// EXPECT_EQ(RequestOp(j), ops[i][j]);
// }
// }
// }