forked from kohler/masstree-beta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtclient.hh
358 lines (348 loc) · 10.7 KB
/
mtclient.hh
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
/* Masstree
* Eddie Kohler, Yandong Mao, Robert Morris
* Copyright (c) 2012-2013 President and Fellows of Harvard College
* Copyright (c) 2012-2013 Massachusetts Institute of Technology
*
* 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, subject to the conditions
* listed in the Masstree LICENSE file. These conditions include: you must
* preserve this copyright notice, and you cannot mention the copyright
* holders in advertising related to the Software without their permission.
* The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
* notice is a summary of the Masstree LICENSE file; the license in that file
* is legally binding.
*/
#ifndef KVC_HH
#define KVC_HH 1
#include "kvproto.hh"
#include "kvrow.hh"
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <string>
#include <queue>
#include <vector>
using namespace std;
class KVCallback {
public:
KVCallback() {
_ref = 1;
}
virtual ~KVCallback() {
}
virtual void incref() {
_ref ++;
}
virtual void decref() {
_ref --;
if (_ref == 0)
delete this;
}
virtual bool callback_get(int r, vector<string> &val) {
(void) r, (void) val;
return true;
}
virtual bool callback_put(int r) {
(void) r;
return true;
}
virtual bool callback_remove(int r) {
(void) r;
return true;
}
virtual bool callback_scan(int r, vector<string> &keys,
vector< vector<string> > &vals) {
(void) r, (void) keys, (void) vals;
return true;
}
int _cmd;
unsigned int _wantedlen;
private:
int _ref;
};
class KVConn {
public:
KVConn(const char *server, int port, int target_core = -1) {
kvbuf = new_bufkvout();
struct hostent *ent = gethostbyname(server);
mandatory_assert(ent);
int fd = socket(AF_INET, SOCK_STREAM, 0);
mandatory_assert(fd > 0);
this->fdtoclose = fd;
int yes = 1;
mandatory_assert(fd >= 0);
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
memcpy(&sin.sin_addr.s_addr, ent->h_addr, ent->h_length);
int r = connect(fd, (const struct sockaddr *)&sin, sizeof(sin));
if (r) {
perror("connect");
exit(EXIT_FAILURE);
}
in = new_kvin(fd, 64*1024);
out = new_kvout(fd, 64*1024);
KVW(out, target_core);
handshake();
}
KVConn(int fd, bool tcp) {
kvbuf = new_bufkvout();
in = new_kvin(fd, 64*1024);
out = new_kvout(fd, 64*1024);
this->fdtoclose = -1;
if (tcp) {
KVW(out, -1);
handshake();
}
}
~KVConn() {
if (fdtoclose >= 0)
close(fdtoclose);
free_kvin(in);
free_kvout(out);
free_kvout(kvbuf);
}
void aput(const Str &key, row_type::change_t &c, KVCallback *cb) {
cb->incref();
cb->_cmd = Cmd_Put;
reqs.push(cb);
sendput(key, c, 0);
}
void aget(const Str &key, row_type::fields_t &f, KVCallback *cb) {
cb->incref();
cb->_cmd = Cmd_Get;
reqs.push(cb);
sendget(key, f, 0);
}
void aget(const char *key, row_type::fields_t &f, KVCallback *cb) {
aget(Str(key, strlen(key)), f, cb);
}
void ascan(int numpairs, const char *key, row_type::fields_t &f,
KVCallback *cb) {
cb->incref();
cb->_cmd = Cmd_Scan;
cb->_wantedlen = numpairs;
reqs.push(cb);
sendscan(numpairs, key, f, 0);
}
void run() {
if (reqs.size() == 0)
return;
flush();
kvcheck(in, 2);
receive();
}
bool drain() {
if (reqs.size() == 0)
return true;
flush();
run();
return false;
}
void sendgetwhole(const Str &key, unsigned int seq) {
row_type::fields_t f;
row_type::make_get1_fields(f);
sendget(key, f, seq);
}
void sendget(const Str &key, row_type::fields_t &f, unsigned int seq) {
KVW(out, (int)Cmd_Get);
KVW(out, seq);
if (key.len > MaxKeyLen) {
fprintf(stderr, "maxkeylen %d, %*.s\n", key.len, key.len, key.s);
exit(EXIT_FAILURE);
}
kvwrite_str(out, key);
// Write fields
kvout_reset(kvbuf);
row_type::kvwrite_fields(kvbuf, f);
kvwrite_str(out, Str(kvbuf->buf, kvbuf->n));
}
void sendget(const char *key, row_type::fields_t &f, unsigned int seq) {
sendget(Str(key, strlen(key)), f, seq);
}
// return the length of the value if successful (>=0);
// -1 if I/O error
// -2 if the key is not found
int recvget(vector<string> &row, unsigned int *seq, bool gotseq) {
if(!gotseq && kvread(in, (char*)seq, sizeof(*seq)) != sizeof(*seq))
return -1;
if (kvread_row(in, row) < 0)
return -1;
return 0;
}
void sendputwhole(const Str &key, const Str &val, unsigned int seq,
bool need_status = false) {
row_type::change_t c;
row_type::make_put1_change(c, val);
sendput(key, c, seq, need_status);
}
void sendput(const Str &key, row_type::change_t &c, unsigned int seq,
bool need_status = false) {
row_type::sort(c);
KVW(out, (int) (need_status ? Cmd_Put_Status : Cmd_Put));
KVW(out, seq);
if (key.len > MaxKeyLen) {
fprintf(stderr, "maxkeylen %d, %.*s\n", key.len, key.len, key.s);
exit(EXIT_FAILURE);
}
kvwrite_str(out, key);
// write change
kvout_reset(kvbuf);
row_type::kvwrite_change(kvbuf, c);
kvwrite_str(out, Str(kvbuf->buf, kvbuf->n));
}
void sendremove(const Str &key, unsigned int seq) {
KVW(out, (int)Cmd_Remove);
KVW(out, seq);
if (key.len > MaxKeyLen) {
fprintf(stderr, "maxkeylen %d, %.*s\n", key.len, key.len, key.s);
exit(EXIT_FAILURE);
}
kvwrite_str(out, key);
}
int recvput(unsigned int *seq, bool gotseq) {
if(!gotseq && kvread(in, (char*)seq, sizeof(*seq)) != sizeof(*seq))
return -1;
return 0;
}
int recvputstatus(int *status, unsigned int *seq, bool gotseq) {
if(!gotseq && kvread(in, (char*)seq, sizeof(*seq)) != sizeof(*seq))
return -1;
if (kvread(in, (char *) status, sizeof(*status)) != sizeof(*status))
return -1;
return 0;
}
int recvremove(int *status, unsigned int *seq, bool gotseq) {
if (!gotseq && kvread(in, (char*)seq, sizeof(*seq)) != sizeof(*seq))
return -1;
if (kvread(in, (char *) status, sizeof(*status)) != sizeof(*status))
return -1;
return 0;
}
void sendscanwhole(int numpairs, const char *key, unsigned int seq) {
row_type::fields_t f;
row_type::make_get1_fields(f);
sendscan(numpairs, key, f, seq);
}
void sendscan(int numpairs, const char *key, row_type::fields_t &f,
unsigned int seq) {
KVW(out, (int)Cmd_Scan);
KVW(out, seq);
kvwrite_str(out, Str(key, strlen(key)));
// write fields
kvout_reset(kvbuf);
row_type::kvwrite_fields(kvbuf, f);
kvwrite_str(out, Str(kvbuf->buf, kvbuf->n));
// write numpairs
KVW(out, numpairs);
}
int recvscan(vector<string> &keys, vector< vector<string> > &rows,
unsigned int *seq, bool gotseq) {
if(!gotseq) {
unsigned int tmpseq;
if (!seq)
seq = &tmpseq;
if (kvread(in, (char*)seq, sizeof(*seq)) != sizeof(*seq))
return -1;
}
while(1){
char keybuf[MaxKeyLen];
int keylen;
if (kvread_str(in, keybuf, MaxKeyLen, keylen) < 0)
return -1;
if (keylen == 0)
return 0;
keys.push_back(string(keybuf, keylen));
vector<string> row;
if (kvread_row(in, row) < 0)
return -1;
rows.push_back(row);
}
}
void cp(int childno) {
mandatory_assert(childno == 0);
fprintf(stderr, "asking for a checkpoint\n");
KVW(out, (int)Cmd_Checkpoint);
KVW(out, (int)0);
flush();
printf("sent\n");
}
void flush() {
kvflush(out);
}
void readseq(unsigned int *seq) {
ssize_t r = kvread(in, (char*)seq, sizeof(*seq));
mandatory_assert((size_t) r == sizeof(*seq));
}
int getPartition() {
return partition;
}
struct kvin *in;
struct kvout *out;
private:
int fdtoclose;
int partition;
void handshake() {
struct kvproto p;
KVW(out, p);
kvflush(out);
bool ok;
int r = KVR(in, ok);
r = KVR(in, partition);
if (r <= 0) {
fprintf(stderr, "Connection closed by a crashed server?\n");
exit(EXIT_FAILURE);
}
if (!ok) {
fprintf(stderr, "Incompatible kvdb protocol. Make sure the "
"client uses the same row type as the kvd.\n");
exit(EXIT_FAILURE);
}
}
struct kvout *kvbuf;
void receive() {
while (kvcheck(in, 0)) {
unsigned int seq;
readseq(&seq);
KVCallback *cb = reqs.front();
reqs.pop();
if(cb->_cmd == Cmd_Get){
vector<string> row;
int r = recvget(row, NULL, true);
mandatory_assert(r == 0);
cb->callback_get(0, row);
} else if(cb->_cmd == Cmd_Put){
int r = recvput(NULL, true);
mandatory_assert(r >= 0);
cb->callback_put(0);
} else if (cb->_cmd == Cmd_Put_Status) {
int status;
int r = recvputstatus(&status, NULL, true);
mandatory_assert(r >= 0);
cb->callback_put(status);
} else if(cb->_cmd == Cmd_Scan){
vector<string> keys;
vector< vector<string> > rows;
int r = recvscan(keys, rows, NULL, true);
mandatory_assert(r == 0);
mandatory_assert(keys.size() <= cb->_wantedlen);
cb->callback_scan(0, keys, rows);
} else if (cb->_cmd == Cmd_Remove) {
int status;
int r = recvremove(&status, NULL, true);
mandatory_assert(r >= 0);
cb->callback_remove(0);
} else {
mandatory_assert(0 && "Unknown request");
}
cb->decref();
}
}
std::queue<KVCallback *> reqs;
};
#endif