forked from mrpi/redis-cplusplus-client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUnsyncedRpcTracker.cc
480 lines (426 loc) · 15.4 KB
/
UnsyncedRpcTracker.cc
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
/* Copyright (c) 2017 Stanford University
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR(S) DISCLAIM ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL AUTHORS BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "UnsyncedRpcTracker.h"
#include <cstring>
#include <iostream>
#include "anet.h"
#include "redisclient.h"
namespace RAMCloud {
/**
* Default constructor
*/
UnsyncedRpcTracker::UnsyncedRpcTracker()
: masters()
, mutex()
, lastOpNum(0)
{
}
/**
* Default destructor
*/
UnsyncedRpcTracker::~UnsyncedRpcTracker()
{
Lock _(mutex);
for (MasterMap::iterator it = masters.begin(); it != masters.end(); ++it) {
Master* master = it->second;
while (!master->rpcs.empty()) {
free(master->rpcs.front().data);
master->rpcs.pop();
}
delete master;
}
}
/**
* Saves the information of an non-durable RPC whose response is received.
* Any non-durable RPC should register itself before returning wait() call.
*
* \param session
* Transport session where this RPC is sent to.
* \param rpcRequest
* Pointer to RPC request which is previously sent to target master.
* This memory must be either from Allocator or malloc.
* Onwership of this memory is now in UnsyncedRpcTracker. No one should
* modify or free this memory.
* \param tableId
* The table containing the desired object.
* \param keyHash
* Key hash that identifies a particular tablet.
* \param objVer
* Version of object after applying modifications by this RPC.
* \param logPos
* Location of new value of object in master after applying this RPC.
* \param callback
* Callback desired to be invoked when the modifications on the object
* by this RPC becomes durable (by replicating to backups).
*/
void
UnsyncedRpcTracker::registerUnsynced(int socket, int dbindex,
const char* msg, int msgSize,
uint64_t opNumInServer,
uint64_t syncedInServer)
{
Lock lock(mutex);
Master* master = getOrInitMasterRecord(socket);
int size = msgSize;
char* data = new char[size];
std::memcpy(data, msg, size);
if (opNumInServer <= lastOpNum) {
printf("Error. duplicate request? opNumInServer %" PRIu64 ", lastOpNum %" PRIu64 "\n",
opNumInServer, lastOpNum);
}
lastOpNum = opNumInServer;
master->rpcs.emplace(dbindex, data, size, opNumInServer, [](){});
master->updateSyncState(syncedInServer);
}
inline static std::string&
rtrim(std::string & str, const std::string & ws = REDIS_WHITESPACE) {
std::string::size_type pos = str.find_last_not_of(ws);
str.erase(pos + 1);
return str;
}
// Reads a single line of character data from the given blocking socket.
// Returns the line that was read, not including EOL delimiter(s). Both LF
// ('\n') and CRLF ("\r\n") delimiters are supported. If there was an I/O
// error reading from the socket, connection_error is raised. If max_size
// bytes are read before finding an EOL delimiter, a blank string is
// returned.
static std::string
read_line(int socket, ssize_t max_size = 2048) {
assert(socket > 0);
assert(max_size > 0);
std::ostringstream oss;
enum {
buffer_size = 64
};
char buffer[buffer_size];
memset(buffer, 0, buffer_size);
ssize_t total_bytes_read = 0;
bool found_delimiter = false;
while (total_bytes_read < max_size && !found_delimiter) {
// Peek at what's available.
ssize_t bytes_received = redis::recv_or_throw(socket, buffer, buffer_size, MSG_PEEK);
// Some data is available; Length might be < buffer_size.
// Look for newline in whatever was read though.
char * eol = static_cast<char *> (memchr(buffer, '\n', bytes_received));
// If found, write data from the buffer to the output string.
// Else, write the entire buffer and continue reading more data.
ssize_t to_read = bytes_received;
if (eol) {
to_read = eol - buffer + 1;
oss.write(buffer, to_read);
found_delimiter = true;
} else
oss.write(buffer, bytes_received);
// Now read from the socket to remove the peeked data from the socket's
// read buffer. This will not block since we've peeked already and know
// there's data waiting. It might fail if we were interrupted however.
bytes_received = redis::recv_or_throw(socket, buffer, to_read, 0);
}
// Construct final line string. Remove trailing CRLF-based whitespace.
std::string line = oss.str();
return rtrim(line, REDIS_LBR);
}
static bool recv_ok_reply_(int socket) {
std::string line = read_line(socket);
if (line.empty())
throw redis::protocol_error("empty single line reply");
if (line.find(REDIS_PREFIX_STATUS_REPLY_LOADING) == 0) {
return false;
}
if (line.find(REDIS_PREFIX_STATUS_REPLY_ERROR) == 0) {
std::string error_msg = line.substr(strlen(REDIS_PREFIX_STATUS_REPLY_ERROR));
if (error_msg.empty())
error_msg = "unknown error";
throw redis::protocol_error(error_msg);
}
if (line.find(REDIS_PREFIX_STATUS_REPLY_RETRY) == 0) {
throw redis::retry_error();
}
if (line[0] != REDIS_PREFIX_STATUS_REPLY_VALUE &&
line[0] != REDIS_PREFIX_STATUS_REPLY_UNSYNCED &&
line[0] != REDIS_PREFIX_INT_REPLY) {
fprintf(stderr, "Unexpected prefix. Resp: %s", line.c_str());
throw redis::protocol_error("unexpected prefix for status reply");
}
if (line.substr(1,2) != REDIS_STATUS_REPLY_OK &&
line[0] != REDIS_PREFIX_INT_REPLY) {
fprintf(stderr, "Expected OK response. Resp: %s", line.c_str());
throw redis::protocol_error("expected OK response");
}
return true;
}
/**
* Invoked if there is a problem with a session to master, which hints
* a possible crash of the master. It will recover all possibly lost updates
* by retrying requests that are not known to be replicated to backups.
*
* \param sessionPtr
* A raw pointer to session which is being destroyed.
*/
void
UnsyncedRpcTracker::flushSession(int disconnectedSocket, std::string hostIp, uint16_t replayPort)
{
Lock lock(mutex);
Master* master;
MasterMap::iterator it = masters.find(disconnectedSocket);
if (it != masters.end()) {
master = it->second;
} else {
return;
}
fprintf(stderr, "Flushing session in UnsyncedRpcTracker. Total commands: %d\n",
static_cast<int>(master->rpcs.size()));
conn_err:
char err[ANET_ERR_LEN];
int socketForReplay = anetTcpConnect(err, const_cast<char*>(hostIp.c_str()), replayPort);
if (socketForReplay == ANET_ERR) {
std::cerr << err << " (redis_replay://" << hostIp << ':' << replayPort << ")";
sleep(1);
goto conn_err;
}
anetTcpNoDelay(NULL, socketForReplay);
int currentDbIndex = -1;
int sentCommands;
for (sentCommands = 0; !master->rpcs.empty(); ++sentCommands) {
UnsyncedRpc& rpc = master->rpcs.front();
if (rpc.dbindex != currentDbIndex) {
std::string selectCmd = (std::string)(redis::makecmd("SELECT") << rpc.dbindex);
if (anetWrite(socketForReplay, const_cast<char*>(selectCmd.data()), selectCmd.size()) == -1)
goto conn_err;
++sentCommands;
currentDbIndex = rpc.dbindex;
if (!recv_ok_reply_(socketForReplay)) continue;
}
if (anetWrite(socketForReplay, rpc.data, rpc.size) == -1)
goto conn_err;
if (!recv_ok_reply_(socketForReplay)) continue;
rpc.callback();
free(rpc.data);
master->rpcs.pop();
}
// TODO: pipelining for faster recovery...
// for (int i = 0; i < sentCommands; ++i) {
// uint64_t opNumInServer, syncNum;
// recv_unsynced_ok_reply_(socketForReplay, &opNumInServer, &syncNum);
// }
lastOpNum = 0;
}
/**
* Garbage collect RPC information for requests whose updates are made durable
* and invoke callbacks for those requests.
*
* \param sessionPtr
* Session which represents a target master.
* \param masterLogState
* Master's log state including the master's log position up to which
* all log is replicated to backups.
*/
void
UnsyncedRpcTracker::updateSyncState(int socket, uint64_t syncedInServer)
{
Lock lock(mutex);
Master* master;
MasterMap::iterator it = masters.find(socket);
if (it != masters.end()) {
master = it->second;
} else {
return;
}
master->updateSyncState(syncedInServer);
}
/**
* Check the liveness of the masters that has not been contacted recently.
*/
void
UnsyncedRpcTracker::pingMasterByTimeout()
{
/*
vector<ClientLease> victims;
{
Lock lock(mutex);
// Sweep table and pick candidates.
victims.reserve(Cleaner::maxIterPerPeriod / 10);
ClientMap::iterator it;
if (cleaner.nextClientToCheck) {
it = clients.find(cleaner.nextClientToCheck);
} else {
it = clients.begin();
}
for (int i = 0; i < Cleaner::maxIterPerPeriod && it != clients.end();
//&& victims.size() < Cleaner::maxCheckPerPeriod;
++i, ++it) {
Client* client = it->second;
ClientLease lease = {it->first,
client->leaseExpiration.getEncoded(),
0};
if (leaseValidator->needsValidation(lease)) {
victims.push_back(lease);
}
}
if (it == clients.end()) {
cleaner.nextClientToCheck = 0;
} else {
cleaner.nextClientToCheck = it->first;
}
}
// Check with coordinator whether the lease is expired.
// And erase entry if the lease is expired.
for (uint32_t i = 0; i < victims.size(); ++i) {
Lock lock(mutex);
// Do not clean if this client record is protected
if (clients[victims[i].leaseId]->doNotRemove)
continue;
// Do not clean if there are RPCs still in progress for this client.
if (clients[victims[i].leaseId]->numRpcsInProgress)
continue;
ClientLease lease = victims[i];
if (leaseValidator->validate(lease, &lease)) {
clients[victims[i].leaseId]->leaseExpiration =
ClusterTime(lease.leaseExpiration);
} else {
TabletManager::Protector tp(tabletManager);
if (tp.notReadyTabletExists()) {
// Since there is a NOT_READY tablet (eg. recovery/migration),
// we cannot garbage collect expired clients safely.
// Both RpcResult entries and participant list entry need to be
// recovered to make a correct GC decision, but with a tablet
// currently NOT_READY, it is possible to have only RpcResult
// recovered, not Transaction ParticipantList entry yet.
return;
}
// After preventing the start of tablet migration or recovery,
// check SingleClientProtector once more before deletion.
if (clients[victims[i].leaseId]->doNotRemove)
continue;
clients.erase(victims[i].leaseId);
}
}
*/
}
/**
* Wait for backup replication of all changes made by this client up to now.
*/
void
UnsyncedRpcTracker::sync()
{
Lock lock(mutex);
for (MasterMap::iterator it = masters.begin(); it != masters.end(); ++it) {
Master* master = it->second;
if (!master->rpcs.empty()) {
// Ask redis server to sync.
int syncNum = 0; // TODO: extract from response.
master->updateSyncState(syncNum);
// master->syncRpcHolder.construct(ramcloud->clientContext,
// master->session,
// master->lastestLogState);
}
}
}
/**
* Register a callback that will be invoked when all currently outstanding RPCs
* are made durable.
*
* \param callback
* Callback desired to be invoked.
*/
void
UnsyncedRpcTracker::sync(std::function<void()> callback)
{
Lock lock(mutex);
int numMasters = 0;
for (MasterMap::iterator it = masters.begin(); it != masters.end(); ++it) {
Master* master = it->second;
if (!master->rpcs.empty()) {
numMasters++;
}
}
if (numMasters == 0) {
return;
}
// TODO(seojin): Atomic necessary? think about cleaner thread more...
// Probably it is safe without atomic...
// Or it is already protected by mutex??
int* syncedMasterCounter = new int;
*syncedMasterCounter = 0;
for (MasterMap::iterator it = masters.begin(); it != masters.end(); ++it) {
Master* master = it->second;
if (master->rpcs.empty()) {
continue;
}
// TODO(seojin): chain the original callback.
// Use performant way to check previous value is empty.
//std::function<void()> callback = master->rpcs.back().callback;
master->rpcs.back().callback = [callback, numMasters,
syncedMasterCounter] {
(*syncedMasterCounter)++;
if (*syncedMasterCounter >= numMasters) {
callback();
// This is the last access to syncedMasterCounter.
delete syncedMasterCounter;
}
};
}
}
/**
* Return a pointer to the requested client record; create a new record if
* one does not already exist.
*
* \param session
* The boost_intrusive pointer to transport session
* \return
* Pointer to the existing or newly inserted master record.
*/
UnsyncedRpcTracker::Master*
UnsyncedRpcTracker::getOrInitMasterRecord(int socket)
{
Master* master = NULL;
MasterMap::iterator it = masters.find(socket);
if (it != masters.end()) {
master = it->second;
} else {
master = new Master();
masters[socket] = master;
}
return master;
}
/////////////////////////////////////////
// Master
/////////////////////////////////////////
/**
* Update the saved log state if the given one is newer, and
* garbage collect RPC information for requests whose updates are made durable
* and invoke callbacks for those requests.
*
* \param newLogState
* Master's log state including the master's log position up to which
* all log is replicated to backups.
*/
void
UnsyncedRpcTracker::Master::updateSyncState(uint64_t syncNum)
{
if (lastestSyncNum < syncNum) {
lastestSyncNum = syncNum;
}
while (!rpcs.empty()) {
UnsyncedRpc& rpc = rpcs.front();
if (rpc.opNum > syncNum) {
break;
}
rpc.callback();
free(rpc.data);
rpcs.pop();
}
}
} // namespace RAMCloud