/
memcache_text.cc
584 lines (472 loc) · 12.8 KB
/
memcache_text.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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
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
581
582
583
//
// kumofs
//
// Copyright (C) 2009 FURUHASHI Sadayuki
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#define __STDC_LIMIT_MACROS
#define __STDC_FORMAT_MACROS
#include "gate/memcache_text.h"
#include "gate/memproto/memtext.h"
#include "log/mlogger.h"
#include "rpc/exception.h"
#include <mp/pthread.h>
#include <mp/stream_buffer.h>
#include <mp/object_callback.h>
#include <stdexcept>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <algorithm>
#include <memory>
#include <inttypes.h>
namespace kumo {
namespace {
using gate::wavy;
using gate::shared_zone;
using gate::auto_zone;
static const size_t MEMTEXT_INITIAL_ALLOCATION_SIZE = 32*1024;
static const size_t MEMTEXT_RESERVE_SIZE = 4*1024;
class handler : public wavy::handler {
public:
handler(int fd);
~handler();
public:
void read_event();
public:
typedef mp::shared_ptr<bool> shared_valid;
class context {
public:
context(int fd, mp::stream_buffer* buf, const shared_valid& valid);
~context();
public:
int fd() const { return m_fd; }
msgpack::zone* release_reference();
const shared_valid& valid() { return m_valid; }
private:
int m_fd;
mp::stream_buffer* m_buffer;
shared_valid m_valid;
private:
context();
context(const context&);
};
private:
mp::stream_buffer m_buffer;
memtext_parser m_memproto;
size_t m_off;
shared_valid m_valid;
context m_context;
private:
handler();
handler(const handler&);
};
struct entry {
int fd;
handler::shared_valid valid;
};
struct get_entry : entry {
bool require_cas;
};
struct get_multi_entry : entry {
bool require_cas;
size_t offset;
unsigned *count;
struct iovec* vhead;
wavy::request* rhead;
size_t veclen;
};
struct set_entry : entry {
};
struct delete_entry : entry {
};
inline void send_data(entry* e,
const char* buf, size_t buflen)
{
if(!*e->valid) { return; }
wavy::write(e->fd, buf, buflen);
}
inline void send_datav(entry* e,
struct iovec* vb, size_t count, auto_zone z)
{
if(!*e->valid) { return; }
wavy::request req(&mp::object_delete<msgpack::zone>, z.release());
wavy::writev(e->fd, vb, count, req);
}
inline void send_datav(entry* e,
struct iovec* vb, wavy::request* vr, size_t count)
{
if(!*e->valid) { return; }
wavy::writev(e->fd, vb, vr, count);
}
static const char* const NOT_SUPPORTED_REPLY = "CLIENT_ERROR supported\r\n";
static const char* const GET_FAILED_REPLY = "SERVER_ERROR get failed\r\n";
static const char* const STORE_FAILED_REPLY = "SERVER_ERROR store failed\r\n";
static const char* const DELETE_FAILED_REPLY = "SERVER_ERROR delete failed\r\n";
// "VALUE "+keylen+" 0 "+uint32+" "+uint64+"\r\n\0"
#define HEADER_SIZE(keylen) \
6 +(keylen)+ 3 + 10 + 1 + 20 + 3
void response_get(void* user,
gate::res_get& res, auto_zone z)
{
get_entry* e = reinterpret_cast<get_entry*>(user);
LOG_TRACE("get response");
if(res.error) {
send_data(e, GET_FAILED_REPLY, strlen(GET_FAILED_REPLY));
return;
}
if(!res.val) {
send_data(e, "END\r\n", 5);
return;
}
char* const header = (char*)z->malloc(HEADER_SIZE(res.keylen));
char* p = header;
memcpy(p, "VALUE ", 6); p += 6;
memcpy(p, res.key, res.keylen); p += res.keylen;
p += sprintf(p, " 0 %"PRIu32, res.vallen);
if(e->require_cas) {
p += sprintf(p, " %"PRIu64"\r\n", (uint64_t)0);
} else {
p[0] = '\r'; p[1] = '\n'; p += 2;
}
struct iovec vb[3];
vb[0].iov_base = header;
vb[0].iov_len = p - header;
vb[1].iov_base = const_cast<char*>(res.val);
vb[1].iov_len = res.vallen;
vb[2].iov_base = const_cast<char*>("\r\nEND\r\n");
vb[2].iov_len = 7;
send_datav(e, vb, 3, z);
}
void response_get_multi(void* user,
gate::res_get& res, auto_zone z)
{
get_multi_entry* e = reinterpret_cast<get_multi_entry*>(user);
LOG_TRACE("get multi response");
if(res.error || !res.val) {
goto filled;
}
{
// res.life is different from req.life and res.life includes req.life
char* const header = (char*)z->malloc(HEADER_SIZE(res.keylen)+2); // +2: \r\n
char* p = header;
memcpy(p, "\r\nVALUE ", 8); p += 8;
memcpy(p, res.key, res.keylen); p += res.keylen;
p += sprintf(p, " 0 %"PRIu32, res.vallen);
if(e->require_cas) {
p += sprintf(p, " %"PRIu64"\r\n", (uint64_t)0); // FIXME p64X casval
} else {
p[0] = '\r'; p[1] = '\n'; p += 2;
}
struct iovec* vb = &e->vhead[e->offset];
wavy::request* vr = &e->rhead[e->offset];
vb[0].iov_base = header;
vb[0].iov_len = p - header;
vb[1].iov_base = res.val;
vb[1].iov_len = res.vallen;
vr[1] = wavy::request(&mp::object_delete<msgpack::zone>, z.release());
}
filled:
if(__sync_sub_and_fetch(e->count, 1) == 0) {
for(struct iovec* v=e->vhead, * const ve = e->vhead+e->veclen;
v < ve; v+=2) {
if(v->iov_base) {
v->iov_base = (char*)v->iov_base + 2; // strip \r\n
v->iov_len -= 2;
break;
}
}
send_datav(e, e->vhead, e->rhead, e->veclen);
}
}
void response_set(void* user,
gate::res_set& res, auto_zone z)
{
set_entry* e = reinterpret_cast<set_entry*>(user);
LOG_TRACE("set response");
if(res.error) {
send_data(e, STORE_FAILED_REPLY, strlen(STORE_FAILED_REPLY));
return;
}
send_data(e, "STORED\r\n", 8);
}
void response_delete(void* user,
gate::res_delete& res, auto_zone z)
{
delete_entry* e = reinterpret_cast<delete_entry*>(user);
LOG_TRACE("delete response");
if(res.error) {
send_data(e, DELETE_FAILED_REPLY, strlen(DELETE_FAILED_REPLY));
return;
}
if(res.deleted) {
send_data(e, "DELETED\r\n", 9);
} else {
send_data(e, "NOT FOUND\r\n", 11);
}
}
void response_noreply_set(void* user,
gate::res_set& res, auto_zone z)
{ }
void response_noreply_delete(void* user,
gate::res_delete& res, auto_zone z)
{ }
#define RELEASE_REFERENCE(user, ctx, life) \
handler::context* ctx = static_cast<handler::context*>(user); \
shared_zone life(ctx->release_reference());
int request_get_single(void* user,
memtext_command cmd,
memtext_request_retrieval* r,
bool require_cas)
{
LOG_TRACE("get");
RELEASE_REFERENCE(user, ctx, life);
const char* const key = r->key[0];
size_t const key_len = r->key_len[0];
get_entry* e = life->allocate<get_entry>();
e->fd = ctx->fd();
e->valid = ctx->valid();
e->require_cas = require_cas;
gate::req_get req;
req.keylen = key_len;
req.key = key;
req.hash = gate::stdhash(req.key, req.keylen);
req.user = reinterpret_cast<void*>(e);
req.callback = &response_get;
req.life = life;
req.submit();
return 0;
}
int request_get_multi(void* user,
memtext_command cmd,
memtext_request_retrieval* r,
bool require_cas)
{
LOG_TRACE("get multi");
RELEASE_REFERENCE(user, ctx, life);
size_t const veclen = r->key_num * 2 + 1; // +1: \r\nEND\r\n
struct iovec* vhead = (struct iovec* )life->malloc(
sizeof(struct iovec )*veclen);
wavy::request* rhead = (wavy::request*)life->malloc(
sizeof(wavy::request)*veclen);
unsigned* share_count = (unsigned*)life->malloc(sizeof(unsigned));
*share_count = r->key_num;
memset(vhead, 0, sizeof(struct iovec )*(veclen-1));
vhead[veclen-1].iov_base = const_cast<char*>("\r\nEND\r\n");
vhead[veclen-1].iov_len = 7;
memset(rhead, 0, sizeof(wavy::request)*veclen);
get_multi_entry* me[r->key_num];
for(unsigned i=0; i < r->key_num; ++i) {
me[i] = life->allocate<get_multi_entry>();
me[i]->fd = ctx->fd();
me[i]->valid = ctx->valid();
me[i]->require_cas = require_cas;
me[i]->offset = i*2;
me[i]->count = share_count;
me[i]->vhead = vhead;
me[i]->rhead = rhead;
me[i]->veclen = veclen;
}
for(unsigned i=0; i < r->key_num; ++i) {
gate::req_get req;
req.keylen = r->key_len[i];
req.key = r->key[i];
req.user = reinterpret_cast<void*>(me[i]);
req.hash = gate::stdhash(req.key, req.keylen);
req.callback = &response_get_multi;
req.life = life;
req.submit();
}
return 0;
}
int request_get(void* user,
memtext_command cmd,
memtext_request_retrieval* r)
{
if(r->key_num == 1) {
return request_get_single(user, cmd, r, false);
} else {
return request_get_multi(user, cmd, r, false);
}
}
int request_gets(void* user,
memtext_command cmd,
memtext_request_retrieval* r)
{
if(r->key_num == 1) {
return request_get_single(user, cmd, r, true);
} else {
return request_get_multi(user, cmd, r, true);
}
}
int request_set(void* user,
memtext_command cmd,
memtext_request_storage* r)
{
LOG_TRACE("set");
RELEASE_REFERENCE(user, ctx, life);
if(r->flags || r->exptime) {
wavy::write(ctx->fd(), NOT_SUPPORTED_REPLY, strlen(NOT_SUPPORTED_REPLY));
return 0;
}
set_entry* e = life->allocate<set_entry>();
e->fd = ctx->fd();
e->valid = ctx->valid();
gate::req_set req;
req.keylen = r->key_len;
req.key = r->key;
req.vallen = r->data_len;
req.val = r->data;
req.hash = gate::stdhash(req.key, req.keylen);
req.user = reinterpret_cast<void*>(e);
if(r->noreply) {
req.async = true;
req.callback = &response_noreply_set;
} else {
req.callback = &response_set;
}
req.life = life;
req.submit();
return 0;
}
int request_delete(void* user,
memtext_command cmd,
memtext_request_delete* r)
{
LOG_TRACE("delete");
RELEASE_REFERENCE(user, ctx, life);
if(r->exptime) {
wavy::write(ctx->fd(), NOT_SUPPORTED_REPLY, strlen(NOT_SUPPORTED_REPLY));
return 0;
}
delete_entry* e = life->allocate<delete_entry>();
e->fd = ctx->fd();
e->valid = ctx->valid();
gate::req_delete req;
req.key = r->key;
req.keylen = r->key_len;
req.hash = gate::stdhash(req.key, req.keylen);
req.user = reinterpret_cast<void*>(e);
if(r->noreply) {
req.async = true;
req.callback = &response_noreply_delete;
} else {
req.callback = &response_delete;
}
req.life = life;
req.submit();
return 0;
}
handler::context::context(int fd, mp::stream_buffer* buf, const shared_valid& valid) :
m_fd(fd), m_buffer(buf), m_valid(valid) { }
handler::context::~context() { }
msgpack::zone* handler::context::release_reference()
{
auto_zone z(new msgpack::zone());
mp::stream_buffer::reference* ref =
z->allocate<mp::stream_buffer::reference>();
m_buffer->release_to(ref);
return z.release();
}
handler::handler(int fd) :
wavy::handler(fd),
m_buffer(MEMTEXT_INITIAL_ALLOCATION_SIZE),
m_off(0),
m_valid(new bool(true)),
m_context(fd, &m_buffer, m_valid)
{
memtext_callback cb = {
request_get, // get
request_gets, // gets
request_set, // set
NULL, // add
NULL, // replace
NULL, // append
NULL, // prepend
NULL, // cas
request_delete, // delete
NULL, // incr
NULL, // decr
};
memtext_init(&m_memproto, &cb, &m_context);
}
handler::~handler()
{
*m_valid = false;
}
void handler::read_event()
try {
m_buffer.reserve_buffer(MEMTEXT_RESERVE_SIZE);
ssize_t rl = ::read(fd(), m_buffer.buffer(), m_buffer.buffer_capacity());
if(rl <= 0) {
if(rl == 0) { throw rpc::connection_closed_error(); }
if(errno == EAGAIN || errno == EINTR) { return; }
else { throw rpc::connection_broken_error(); }
}
m_buffer.buffer_consumed(rl);
do {
int ret = memtext_execute(&m_memproto,
(char*)m_buffer.data(), m_buffer.data_size(), &m_off);
if(ret < 0) {
throw std::runtime_error("parse error");
} else if(ret == 0) {
return;
}
m_buffer.data_used(m_off);
m_off = 0;
} while(m_buffer.data_size() > 0);
} catch(rpc::connection_error& e) {
LOG_DEBUG(e.what());
throw;
} catch (std::exception& e) {
LOG_DEBUG("memcached text protocol error: ",e.what());
throw;
} catch (...) {
LOG_DEBUG("memcached text protocol error: unknown error");
throw;
}
void accepted(int fd, int err)
{
if(fd < 0) {
LOG_FATAL("accept failed: ",strerror(err));
gate::fatal_stop();
return;
}
#ifndef NO_TCP_NODELAY
int on = 1;
::setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); // ignore error
#endif
#ifndef NO_SO_LINGER
struct linger opt = {0, 0};
::setsockopt(fd, SOL_SOCKET, SO_LINGER, (void *)&opt, sizeof(opt)); // ignore error
#endif
LOG_DEBUG("accept MemcacheText gate fd=",fd);
wavy::add<handler>(fd);
}
} // noname namespace
MemcacheText::MemcacheText(int lsock) :
m_lsock(lsock) { }
MemcacheText::~MemcacheText() {}
void MemcacheText::run()
{
using namespace mp::placeholders;
wavy::listen(m_lsock,
mp::bind(&accepted, _1, _2));
}
} // namespace kumo