nc_redis.c 92.1 KB
Newer Older
M
Manju Rajashekhar 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * twemproxy - A fast and lightweight proxy for memcached protocol.
 * Copyright (C) 2011 Twitter, Inc.
 *
 * 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.
 */

#include <stdio.h>
#include <ctype.h>
20
#include <math.h>
M
Manju Rajashekhar 已提交
21 22 23 24

#include <nc_core.h>
#include <nc_proto.h>

25 26 27 28 29 30
#define RSP_STRING(ACTION)                                                          \
    ACTION( ok,               "+OK\r\n"                                           ) \
    ACTION( pong,             "+PONG\r\n"                                         ) \
    ACTION( invalid_password, "-ERR invalid password\r\n"                         ) \
    ACTION( auth_required,    "-NOAUTH Authentication required\r\n"               ) \
    ACTION( no_password,      "-ERR Client sent AUTH, but no password is set\r\n" ) \
I
idning 已提交
31

32
#define DEFINE_ACTION(_var, _str) static struct string rsp_##_var = string(_str);
33 34
    RSP_STRING( DEFINE_ACTION )
#undef DEFINE_ACTION
C
charsyam 已提交
35

36
static rstatus_t redis_handle_auth_req(struct msg *request, struct msg *response);
C
charsyam 已提交
37

I
idning 已提交
38 39 40 41 42
/*
 * Return true, if the redis command take no key, otherwise
 * return false
 */
static bool
T
Tyson Andre 已提交
43
redis_argz(const struct msg *r)
I
idning 已提交
44 45
{
    switch (r->type) {
46
    /* TODO: PING has an optional argument, emulate that? */
I
idning 已提交
47 48
    case MSG_REQ_REDIS_PING:
    case MSG_REQ_REDIS_QUIT:
49
    case MSG_REQ_REDIS_COMMAND:
I
idning 已提交
50 51 52 53 54 55 56 57 58
        return true;

    default:
        break;
    }

    return false;
}

M
Manju Rajashekhar 已提交
59 60 61 62 63
/*
 * Return true, if the redis command accepts no arguments, otherwise
 * return false
 */
static bool
T
Tyson Andre 已提交
64
redis_arg0(const struct msg *r)
M
Manju Rajashekhar 已提交
65 66 67 68 69 70
{
    switch (r->type) {
    case MSG_REQ_REDIS_PERSIST:
    case MSG_REQ_REDIS_PTTL:
    case MSG_REQ_REDIS_TTL:
    case MSG_REQ_REDIS_TYPE:
71
    case MSG_REQ_REDIS_DUMP:
M
Manju Rajashekhar 已提交
72 73 74

    case MSG_REQ_REDIS_DECR:
    case MSG_REQ_REDIS_GET:
75
    case MSG_REQ_REDIS_GETDEL:
M
Manju Rajashekhar 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89
    case MSG_REQ_REDIS_INCR:
    case MSG_REQ_REDIS_STRLEN:

    case MSG_REQ_REDIS_HGETALL:
    case MSG_REQ_REDIS_HKEYS:
    case MSG_REQ_REDIS_HLEN:
    case MSG_REQ_REDIS_HVALS:

    case MSG_REQ_REDIS_LLEN:

    case MSG_REQ_REDIS_SCARD:
    case MSG_REQ_REDIS_SMEMBERS:

    case MSG_REQ_REDIS_ZCARD:
90
        /* TODO: Support emulating 2-arg username+password auth by just checking password? */
C
charsyam 已提交
91
    case MSG_REQ_REDIS_AUTH:
M
Manju Rajashekhar 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105
        return true;

    default:
        break;
    }

    return false;
}

/*
 * Return true, if the redis command accepts exactly 1 argument, otherwise
 * return false
 */
static bool
T
Tyson Andre 已提交
106
redis_arg1(const struct msg *r)
M
Manju Rajashekhar 已提交
107 108 109 110 111 112
{
    switch (r->type) {
    case MSG_REQ_REDIS_EXPIRE:
    case MSG_REQ_REDIS_EXPIREAT:
    case MSG_REQ_REDIS_PEXPIRE:
    case MSG_REQ_REDIS_PEXPIREAT:
113
    case MSG_REQ_REDIS_MOVE:
M
Manju Rajashekhar 已提交
114 115 116 117 118 119 120 121 122 123 124

    case MSG_REQ_REDIS_APPEND:
    case MSG_REQ_REDIS_DECRBY:
    case MSG_REQ_REDIS_GETBIT:
    case MSG_REQ_REDIS_GETSET:
    case MSG_REQ_REDIS_INCRBY:
    case MSG_REQ_REDIS_INCRBYFLOAT:
    case MSG_REQ_REDIS_SETNX:

    case MSG_REQ_REDIS_HEXISTS:
    case MSG_REQ_REDIS_HGET:
125
    case MSG_REQ_REDIS_HSTRLEN:
M
Manju Rajashekhar 已提交
126 127

    case MSG_REQ_REDIS_LINDEX:
128
    case MSG_REQ_REDIS_RPOPLPUSH:
M
Manju Rajashekhar 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

    case MSG_REQ_REDIS_SISMEMBER:

    case MSG_REQ_REDIS_ZRANK:
    case MSG_REQ_REDIS_ZREVRANK:
    case MSG_REQ_REDIS_ZSCORE:
        return true;

    default:
        break;
    }

    return false;
}

/*
 * Return true, if the redis command accepts exactly 2 arguments, otherwise
 * return false
 */
static bool
T
Tyson Andre 已提交
149
redis_arg2(const struct msg *r)
M
Manju Rajashekhar 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
{
    switch (r->type) {
    case MSG_REQ_REDIS_GETRANGE:
    case MSG_REQ_REDIS_PSETEX:
    case MSG_REQ_REDIS_SETBIT:
    case MSG_REQ_REDIS_SETEX:
    case MSG_REQ_REDIS_SETRANGE:

    case MSG_REQ_REDIS_HINCRBY:
    case MSG_REQ_REDIS_HINCRBYFLOAT:
    case MSG_REQ_REDIS_HSETNX:

    case MSG_REQ_REDIS_LRANGE:
    case MSG_REQ_REDIS_LREM:
    case MSG_REQ_REDIS_LSET:
    case MSG_REQ_REDIS_LTRIM:

167 168
    case MSG_REQ_REDIS_SMOVE:

M
Manju Rajashekhar 已提交
169
    case MSG_REQ_REDIS_ZCOUNT:
M
Matt Robenolt 已提交
170
    case MSG_REQ_REDIS_ZLEXCOUNT:
M
Manju Rajashekhar 已提交
171
    case MSG_REQ_REDIS_ZINCRBY:
M
Matt Robenolt 已提交
172
    case MSG_REQ_REDIS_ZREMRANGEBYLEX:
M
Manju Rajashekhar 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    case MSG_REQ_REDIS_ZREMRANGEBYRANK:
    case MSG_REQ_REDIS_ZREMRANGEBYSCORE:
        return true;

    default:
        break;
    }

    return false;
}

/*
 * Return true, if the redis command accepts exactly 3 arguments, otherwise
 * return false
 */
static bool
T
Tyson Andre 已提交
189
redis_arg3(const struct msg *r)
M
Manju Rajashekhar 已提交
190 191 192
{
    switch (r->type) {
    case MSG_REQ_REDIS_LINSERT:
193
    case MSG_REQ_REDIS_LMOVE:
M
Manju Rajashekhar 已提交
194 195 196 197 198 199 200 201 202 203
        return true;

    default:
        break;
    }

    return false;
}

/*
204
 * Return true, if the redis command operates on one key and accepts 0 or more arguments, otherwise
M
Manju Rajashekhar 已提交
205 206 207
 * return false
 */
static bool
T
Tyson Andre 已提交
208
redis_argn(const struct msg *r)
M
Manju Rajashekhar 已提交
209 210
{
    switch (r->type) {
211
    case MSG_REQ_REDIS_SORT:
212
    case MSG_REQ_REDIS_COPY:
213

M
Manju Rajashekhar 已提交
214
    case MSG_REQ_REDIS_BITCOUNT:
C
clark.kang 已提交
215
    case MSG_REQ_REDIS_BITPOS:
216
    case MSG_REQ_REDIS_BITFIELD:
217
        /* TODO: Support REDIS_BITOP operation destkey key ... and add tests - this requires handling key in a position other than the first one */
M
Manju Rajashekhar 已提交
218

219 220
    case MSG_REQ_REDIS_EXISTS:
    case MSG_REQ_REDIS_GETEX:
221
    case MSG_REQ_REDIS_SET:
222

M
Manju Rajashekhar 已提交
223 224 225
    case MSG_REQ_REDIS_HDEL:
    case MSG_REQ_REDIS_HMGET:
    case MSG_REQ_REDIS_HMSET:
226
    case MSG_REQ_REDIS_HSCAN:
227 228
    case MSG_REQ_REDIS_HSET:
    case MSG_REQ_REDIS_HRANDFIELD:
M
Manju Rajashekhar 已提交
229 230

    case MSG_REQ_REDIS_LPUSH:
231
    case MSG_REQ_REDIS_LPUSHX:
M
Manju Rajashekhar 已提交
232
    case MSG_REQ_REDIS_RPUSH:
233 234 235 236
    case MSG_REQ_REDIS_RPUSHX:
    case MSG_REQ_REDIS_LPOP:
    case MSG_REQ_REDIS_RPOP:
    case MSG_REQ_REDIS_LPOS:
M
Manju Rajashekhar 已提交
237 238

    case MSG_REQ_REDIS_SADD:
239 240 241 242
    case MSG_REQ_REDIS_SDIFF:
    case MSG_REQ_REDIS_SDIFFSTORE:
    case MSG_REQ_REDIS_SINTER:
    case MSG_REQ_REDIS_SINTERSTORE:
M
Manju Rajashekhar 已提交
243
    case MSG_REQ_REDIS_SREM:
244 245
    case MSG_REQ_REDIS_SUNION:
    case MSG_REQ_REDIS_SUNIONSTORE:
246
    case MSG_REQ_REDIS_SRANDMEMBER:
247
    case MSG_REQ_REDIS_SSCAN:
C
charsyam 已提交
248
    case MSG_REQ_REDIS_SPOP:
249
    case MSG_REQ_REDIS_SMISMEMBER:
M
Manju Rajashekhar 已提交
250

251 252
    case MSG_REQ_REDIS_PFADD:
    case MSG_REQ_REDIS_PFMERGE:
253 254
    case MSG_REQ_REDIS_PFCOUNT:

M
Manju Rajashekhar 已提交
255
    case MSG_REQ_REDIS_ZADD:
256 257 258
    case MSG_REQ_REDIS_ZDIFF:
    case MSG_REQ_REDIS_ZDIFFSTORE:
    case MSG_REQ_REDIS_ZINTER:
259
    case MSG_REQ_REDIS_ZINTERSTORE:
260 261 262 263
    case MSG_REQ_REDIS_ZMSCORE:
    case MSG_REQ_REDIS_ZPOPMAX:
    case MSG_REQ_REDIS_ZPOPMIN:
    case MSG_REQ_REDIS_ZRANDMEMBER:
M
Manju Rajashekhar 已提交
264
    case MSG_REQ_REDIS_ZRANGE:
265
    case MSG_REQ_REDIS_ZRANGEBYLEX:
M
Manju Rajashekhar 已提交
266
    case MSG_REQ_REDIS_ZRANGEBYSCORE:
267
    case MSG_REQ_REDIS_ZRANGESTORE:
M
Manju Rajashekhar 已提交
268 269
    case MSG_REQ_REDIS_ZREM:
    case MSG_REQ_REDIS_ZREVRANGE:
270
    case MSG_REQ_REDIS_ZREVRANGEBYLEX:
M
Manju Rajashekhar 已提交
271
    case MSG_REQ_REDIS_ZREVRANGEBYSCORE:
272
    case MSG_REQ_REDIS_ZSCAN:
273 274 275 276 277 278 279 280 281 282 283 284 285
    case MSG_REQ_REDIS_ZUNION:
    case MSG_REQ_REDIS_ZUNIONSTORE:

    case MSG_REQ_REDIS_GEODIST:
    case MSG_REQ_REDIS_GEOPOS:
    case MSG_REQ_REDIS_GEOHASH:
    case MSG_REQ_REDIS_GEOADD:
    case MSG_REQ_REDIS_GEORADIUS:
    case MSG_REQ_REDIS_GEORADIUSBYMEMBER:
    case MSG_REQ_REDIS_GEOSEARCH:
    case MSG_REQ_REDIS_GEOSEARCHSTORE:

    case MSG_REQ_REDIS_RESTORE:
M
Manju Rajashekhar 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299
        return true;

    default:
        break;
    }

    return false;
}

/*
 * Return true, if the redis command is a vector command accepting one or
 * more keys, otherwise return false
 */
static bool
T
Tyson Andre 已提交
300
redis_argx(const struct msg *r)
M
Manju Rajashekhar 已提交
301 302 303 304
{
    switch (r->type) {
    case MSG_REQ_REDIS_MGET:
    case MSG_REQ_REDIS_DEL:
305 306
    case MSG_REQ_REDIS_UNLINK:
    case MSG_REQ_REDIS_TOUCH:
M
Manju Rajashekhar 已提交
307 308 309 310 311 312 313 314 315
        return true;

    default:
        break;
    }

    return false;
}

I
idning 已提交
316 317 318 319
/*
 * Return true, if the redis command is a vector command accepting one or
 * more key-value pairs, otherwise return false
 */
320
static bool
T
Tyson Andre 已提交
321
redis_argkvx(const struct msg *r)
I
idning 已提交
322 323 324 325 326 327 328 329 330 331 332 333
{
    switch (r->type) {
    case MSG_REQ_REDIS_MSET:
        return true;

    default:
        break;
    }

    return false;
}

334 335 336 337 338 339 340
/*
 * Return true, if the redis command is either EVAL or EVALSHA. These commands
 * have a special format with exactly 2 arguments, followed by one or more keys,
 * followed by zero or more arguments (the documentation online seems to suggest
 * that at least one argument is required, but that shouldn't be the case).
 */
static bool
T
Tyson Andre 已提交
341
redis_argeval(const struct msg *r)
342 343 344 345 346 347 348 349 350 351 352 353 354
{
    switch (r->type) {
    case MSG_REQ_REDIS_EVAL:
    case MSG_REQ_REDIS_EVALSHA:
        return true;

    default:
        break;
    }

    return false;
}

355
static bool
T
Tyson Andre 已提交
356
redis_nokey(const struct msg *r)
357 358 359 360 361 362 363 364 365 366 367 368
{
    switch (r->type) {
    case MSG_REQ_REDIS_LOLWUT:
        return true;

    default:
        break;
    }

    return false;
}

369 370 371 372 373
/*
 * Return true, if the redis response is an error response i.e. a simple
 * string whose first character is '-', otherwise return false.
 */
static bool
T
Tyson Andre 已提交
374
redis_error(const struct msg *r)
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
{
    switch (r->type) {
    case MSG_RSP_REDIS_ERROR:
    case MSG_RSP_REDIS_ERROR_ERR:
    case MSG_RSP_REDIS_ERROR_OOM:
    case MSG_RSP_REDIS_ERROR_BUSY:
    case MSG_RSP_REDIS_ERROR_NOAUTH:
    case MSG_RSP_REDIS_ERROR_LOADING:
    case MSG_RSP_REDIS_ERROR_BUSYKEY:
    case MSG_RSP_REDIS_ERROR_MISCONF:
    case MSG_RSP_REDIS_ERROR_NOSCRIPT:
    case MSG_RSP_REDIS_ERROR_READONLY:
    case MSG_RSP_REDIS_ERROR_WRONGTYPE:
    case MSG_RSP_REDIS_ERROR_EXECABORT:
    case MSG_RSP_REDIS_ERROR_MASTERDOWN:
    case MSG_RSP_REDIS_ERROR_NOREPLICAS:
        return true;

    default:
        break;
    }

    return false;
}

M
Manju Rajashekhar 已提交
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
/*
 * Reference: http://redis.io/topics/protocol
 *
 * Redis >= 1.2 uses the unified protocol to send requests to the Redis
 * server. In the unified protocol all the arguments sent to the server
 * are binary safe and every request has the following general form:
 *
 *   *<number of arguments> CR LF
 *   $<number of bytes of argument 1> CR LF
 *   <argument data> CR LF
 *   ...
 *   $<number of bytes of argument N> CR LF
 *   <argument data> CR LF
 *
 * Before the unified request protocol, redis protocol for requests supported
 * the following commands
 * 1). Inline commands: simple commands where arguments are just space
 *     separated strings. No binary safeness is possible.
 * 2). Bulk commands: bulk commands are exactly like inline commands, but
 *     the last argument is handled in a special way in order to allow for
 *     a binary-safe last argument.
 *
 * Nutcracker only supports the Redis unified protocol for requests.
 */
void
redis_parse_req(struct msg *r)
{
    struct mbuf *b;
    uint8_t *p, *m;
    uint8_t ch;
    enum {
        SW_START,
        SW_NARG,
        SW_NARG_LF,
        SW_REQ_TYPE_LEN,
        SW_REQ_TYPE_LEN_LF,
        SW_REQ_TYPE,
        SW_REQ_TYPE_LF,
        SW_KEY_LEN,
        SW_KEY_LEN_LF,
        SW_KEY,
        SW_KEY_LF,
        SW_ARG1_LEN,
        SW_ARG1_LEN_LF,
        SW_ARG1,
        SW_ARG1_LF,
        SW_ARG2_LEN,
        SW_ARG2_LEN_LF,
        SW_ARG2,
        SW_ARG2_LF,
        SW_ARG3_LEN,
        SW_ARG3_LEN_LF,
        SW_ARG3,
        SW_ARG3_LF,
        SW_ARGN_LEN,
        SW_ARGN_LEN_LF,
        SW_ARGN,
        SW_ARGN_LF,
        SW_SENTINEL
    } state;

    state = r->state;
    b = STAILQ_LAST(&r->mhdr, mbuf, next);

    ASSERT(r->request);
    ASSERT(state >= SW_START && state < SW_SENTINEL);
    ASSERT(b != NULL);
    ASSERT(b->pos <= b->last);

    /* validate the parsing maker */
    ASSERT(r->pos != NULL);
    ASSERT(r->pos >= b->pos && r->pos <= b->last);

    for (p = r->pos; p < b->last; p++) {
        ch = *p;

        switch (state) {

        case SW_START:
479 480 481 482 483 484 485 486 487 488 489 490 491
            ASSERT(r->token == NULL);
            if (ch != '*') {
                /* redis commands are always arrays */
                goto error;
            }
            r->token = p;
            /* req_start <- p */
            r->narg_start = p;
            r->rnarg = 0;
            state = SW_NARG;

            break;

M
Manju Rajashekhar 已提交
492
        case SW_NARG:
493 494 495
            /* SW_NARG: The number of arguments in the redis command array */
            ASSERT(r->token != NULL);
            if (isdigit(ch)) {
M
Manju Rajashekhar 已提交
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 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
                r->rnarg = r->rnarg * 10 + (uint32_t)(ch - '0');
            } else if (ch == CR) {
                if (r->rnarg == 0) {
                    goto error;
                }
                r->narg = r->rnarg;
                r->narg_end = p;
                r->token = NULL;
                state = SW_NARG_LF;
            } else {
                goto error;
            }

            break;

        case SW_NARG_LF:
            switch (ch) {
            case LF:
                state = SW_REQ_TYPE_LEN;
                break;

            default:
                goto error;
            }

            break;

        case SW_REQ_TYPE_LEN:
            if (r->token == NULL) {
                if (ch != '$') {
                    goto error;
                }
                r->token = p;
                r->rlen = 0;
            } else if (isdigit(ch)) {
                r->rlen = r->rlen * 10 + (uint32_t)(ch - '0');
            } else if (ch == CR) {
                if (r->rlen == 0 || r->rnarg == 0) {
                    goto error;
                }
                r->rnarg--;
                r->token = NULL;
                state = SW_REQ_TYPE_LEN_LF;
            } else {
                goto error;
            }

            break;

        case SW_REQ_TYPE_LEN_LF:
            switch (ch) {
            case LF:
                state = SW_REQ_TYPE;
                break;

            default:
                goto error;
            }

            break;

        case SW_REQ_TYPE:
            if (r->token == NULL) {
                r->token = p;
            }

            m = r->token + r->rlen;
            if (m >= b->last) {
                m = b->last - 1;
                p = m;
                break;
            }

            if (*m != CR) {
                goto error;
            }

            p = m; /* move forward by rlen bytes */
            r->rlen = 0;
            m = r->token;
            r->token = NULL;
            r->type = MSG_UNKNOWN;

            switch (p - m) {

            case 3:
                if (str3icmp(m, 'g', 'e', 't')) {
                    r->type = MSG_REQ_REDIS_GET;
                    break;
                }

                if (str3icmp(m, 's', 'e', 't')) {
                    r->type = MSG_REQ_REDIS_SET;
                    break;
                }

                if (str3icmp(m, 't', 't', 'l')) {
                    r->type = MSG_REQ_REDIS_TTL;
                    break;
                }

                if (str3icmp(m, 'd', 'e', 'l')) {
                    r->type = MSG_REQ_REDIS_DEL;
                    break;
                }

                break;

            case 4:
                if (str4icmp(m, 'p', 't', 't', 'l')) {
                    r->type = MSG_REQ_REDIS_PTTL;
                    break;
                }

                if (str4icmp(m, 'd', 'e', 'c', 'r')) {
                    r->type = MSG_REQ_REDIS_DECR;
                    break;
                }

615 616 617 618 619
                if (str4icmp(m, 'd', 'u', 'm', 'p')) {
                    r->type = MSG_REQ_REDIS_DUMP;
                    break;
                }

M
Manju Rajashekhar 已提交
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
                if (str4icmp(m, 'h', 'd', 'e', 'l')) {
                    r->type = MSG_REQ_REDIS_HDEL;
                    break;
                }

                if (str4icmp(m, 'h', 'g', 'e', 't')) {
                    r->type = MSG_REQ_REDIS_HGET;
                    break;
                }

                if (str4icmp(m, 'h', 'l', 'e', 'n')) {
                    r->type = MSG_REQ_REDIS_HLEN;
                    break;
                }

                if (str4icmp(m, 'h', 's', 'e', 't')) {
                    r->type = MSG_REQ_REDIS_HSET;
                    break;
                }

                if (str4icmp(m, 'i', 'n', 'c', 'r')) {
                    r->type = MSG_REQ_REDIS_INCR;
                    break;
                }

                if (str4icmp(m, 'l', 'l', 'e', 'n')) {
                    r->type = MSG_REQ_REDIS_LLEN;
                    break;
                }

                if (str4icmp(m, 'l', 'p', 'o', 'p')) {
                    r->type = MSG_REQ_REDIS_LPOP;
                    break;
                }

655 656 657 658 659
                if (str4icmp(m, 'l', 'p', 'o', 's')) {
                    r->type = MSG_REQ_REDIS_LPOS;
                    break;
                }

M
Manju Rajashekhar 已提交
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
                if (str4icmp(m, 'l', 'r', 'e', 'm')) {
                    r->type = MSG_REQ_REDIS_LREM;
                    break;
                }

                if (str4icmp(m, 'l', 's', 'e', 't')) {
                    r->type = MSG_REQ_REDIS_LSET;
                    break;
                }

                if (str4icmp(m, 'r', 'p', 'o', 'p')) {
                    r->type = MSG_REQ_REDIS_RPOP;
                    break;
                }

                if (str4icmp(m, 's', 'a', 'd', 'd')) {
                    r->type = MSG_REQ_REDIS_SADD;
                    break;
                }

                if (str4icmp(m, 's', 'p', 'o', 'p')) {
                    r->type = MSG_REQ_REDIS_SPOP;
                    break;
                }

                if (str4icmp(m, 's', 'r', 'e', 'm')) {
                    r->type = MSG_REQ_REDIS_SREM;
                    break;
                }

                if (str4icmp(m, 't', 'y', 'p', 'e')) {
                    r->type = MSG_REQ_REDIS_TYPE;
                    break;
                }

                if (str4icmp(m, 'm', 'g', 'e', 't')) {
                    r->type = MSG_REQ_REDIS_MGET;
                    break;
                }
I
idning 已提交
699 700 701 702
                if (str4icmp(m, 'm', 's', 'e', 't')) {
                    r->type = MSG_REQ_REDIS_MSET;
                    break;
                }
M
Manju Rajashekhar 已提交
703 704 705 706 707 708 709 710 711 712 713

                if (str4icmp(m, 'z', 'a', 'd', 'd')) {
                    r->type = MSG_REQ_REDIS_ZADD;
                    break;
                }

                if (str4icmp(m, 'z', 'r', 'e', 'm')) {
                    r->type = MSG_REQ_REDIS_ZREM;
                    break;
                }

714 715 716 717 718
                if (str4icmp(m, 'e', 'v', 'a', 'l')) {
                    r->type = MSG_REQ_REDIS_EVAL;
                    break;
                }

719 720 721 722 723
                if (str4icmp(m, 's', 'o', 'r', 't')) {
                    r->type = MSG_REQ_REDIS_SORT;
                    break;
                }

I
idning 已提交
724 725 726 727 728 729 730 731 732 733 734 735
                if (str4icmp(m, 'p', 'i', 'n', 'g')) {
                    r->type = MSG_REQ_REDIS_PING;
                    r->noforward = 1;
                    break;
                }

                if (str4icmp(m, 'q', 'u', 'i', 't')) {
                    r->type = MSG_REQ_REDIS_QUIT;
                    r->quit = 1;
                    break;
                }

C
charsyam 已提交
736 737
                if (str4icmp(m, 'a', 'u', 't', 'h')) {
                    r->type = MSG_REQ_REDIS_AUTH;
738
                    r->noforward = 1;
C
charsyam 已提交
739 740 741
                    break;
                }

742 743 744 745 746 747 748 749 750 751 752
                if (str4icmp(m, 'm', 'o', 'v', 'e')) {
                    r->type = MSG_REQ_REDIS_MOVE;
                    r->noforward = 1;
                    break;
                }

                if (str4icmp(m, 'c', 'o', 'p', 'y')) {
                    r->type = MSG_REQ_REDIS_COPY;
                    break;
                }

M
Manju Rajashekhar 已提交
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
                break;

            case 5:
                if (str5icmp(m, 'h', 'k', 'e', 'y', 's')) {
                    r->type = MSG_REQ_REDIS_HKEYS;
                    break;
                }

                if (str5icmp(m, 'h', 'm', 'g', 'e', 't')) {
                    r->type = MSG_REQ_REDIS_HMGET;
                    break;
                }

                if (str5icmp(m, 'h', 'm', 's', 'e', 't')) {
                    r->type = MSG_REQ_REDIS_HMSET;
                    break;
                }

                if (str5icmp(m, 'h', 'v', 'a', 'l', 's')) {
                    r->type = MSG_REQ_REDIS_HVALS;
                    break;
                }

776 777 778 779 780
                if (str5icmp(m, 'h', 's', 'c', 'a', 'n')) {
                    r->type = MSG_REQ_REDIS_HSCAN;
                    break;
                }

M
Manju Rajashekhar 已提交
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
                if (str5icmp(m, 'l', 'p', 'u', 's', 'h')) {
                    r->type = MSG_REQ_REDIS_LPUSH;
                    break;
                }

                if (str5icmp(m, 'l', 't', 'r', 'i', 'm')) {
                    r->type = MSG_REQ_REDIS_LTRIM;
                    break;
                }

                if (str5icmp(m, 'r', 'p', 'u', 's', 'h')) {
                    r->type = MSG_REQ_REDIS_RPUSH;
                    break;
                }

                if (str5icmp(m, 's', 'c', 'a', 'r', 'd')) {
                    r->type = MSG_REQ_REDIS_SCARD;
                    break;
                }

801 802 803 804 805
                if (str5icmp(m, 's', 'd', 'i', 'f', 'f')) {
                    r->type = MSG_REQ_REDIS_SDIFF;
                    break;
                }

M
Manju Rajashekhar 已提交
806 807 808 809 810 811 812 813 814 815
                if (str5icmp(m, 's', 'e', 't', 'e', 'x')) {
                    r->type = MSG_REQ_REDIS_SETEX;
                    break;
                }

                if (str5icmp(m, 's', 'e', 't', 'n', 'x')) {
                    r->type = MSG_REQ_REDIS_SETNX;
                    break;
                }

816 817 818 819 820
                if (str5icmp(m, 's', 'm', 'o', 'v', 'e')) {
                    r->type = MSG_REQ_REDIS_SMOVE;
                    break;
                }

821 822 823 824 825
                if (str5icmp(m, 's', 's', 'c', 'a', 'n')) {
                    r->type = MSG_REQ_REDIS_SSCAN;
                    break;
                }

M
Manju Rajashekhar 已提交
826 827 828 829 830
                if (str5icmp(m, 'z', 'c', 'a', 'r', 'd')) {
                    r->type = MSG_REQ_REDIS_ZCARD;
                    break;
                }

831 832 833 834 835
                if (str5icmp(m, 'z', 'd', 'i', 'f', 'f')) {
                    r->type = MSG_REQ_REDIS_ZDIFF;
                    break;
                }

M
Manju Rajashekhar 已提交
836 837 838 839 840
                if (str5icmp(m, 'z', 'r', 'a', 'n', 'k')) {
                    r->type = MSG_REQ_REDIS_ZRANK;
                    break;
                }

841 842 843 844 845
                if (str5icmp(m, 'z', 's', 'c', 'a', 'n')) {
                    r->type = MSG_REQ_REDIS_ZSCAN;
                    break;
                }

846 847 848 849 850
                if (str5icmp(m, 'p', 'f', 'a', 'd', 'd')) {
                    r->type = MSG_REQ_REDIS_PFADD;
                    break;
                }

851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
                if (str5icmp(m, 'g', 'e', 't', 'e', 'x')) {
                    r->type = MSG_REQ_REDIS_GETEX;
                    break;
                }

                if (str5icmp(m, 't', 'o', 'u', 'c', 'h')) {
                    r->type = MSG_REQ_REDIS_TOUCH;
                    break;
                }

                if (str5icmp(m, 'l', 'm', 'o', 'v', 'e')) {
                    r->type = MSG_REQ_REDIS_LMOVE;
                    break;
                }

M
Manju Rajashekhar 已提交
866 867 868 869 870 871 872 873
                break;

            case 6:
                if (str6icmp(m, 'a', 'p', 'p', 'e', 'n', 'd')) {
                    r->type = MSG_REQ_REDIS_APPEND;
                    break;
                }

C
clark.kang 已提交
874 875 876 877 878
                if (str6icmp(m, 'b', 'i', 't', 'p', 'o', 's')) {
                    r->type = MSG_REQ_REDIS_BITPOS;
                    break;
                }

M
Manju Rajashekhar 已提交
879 880 881 882 883 884 885 886 887 888 889 890 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
                if (str6icmp(m, 'd', 'e', 'c', 'r', 'b', 'y')) {
                    r->type = MSG_REQ_REDIS_DECRBY;
                    break;
                }

                if (str6icmp(m, 'e', 'x', 'i', 's', 't', 's')) {
                    r->type = MSG_REQ_REDIS_EXISTS;
                    break;
                }

                if (str6icmp(m, 'e', 'x', 'p', 'i', 'r', 'e')) {
                    r->type = MSG_REQ_REDIS_EXPIRE;
                    break;
                }

                if (str6icmp(m, 'g', 'e', 't', 'b', 'i', 't')) {
                    r->type = MSG_REQ_REDIS_GETBIT;
                    break;
                }

                if (str6icmp(m, 'g', 'e', 't', 's', 'e', 't')) {
                    r->type = MSG_REQ_REDIS_GETSET;
                    break;
                }

                if (str6icmp(m, 'p', 's', 'e', 't', 'e', 'x')) {
                    r->type = MSG_REQ_REDIS_PSETEX;
                    break;
                }

                if (str6icmp(m, 'h', 's', 'e', 't', 'n', 'x')) {
                    r->type = MSG_REQ_REDIS_HSETNX;
                    break;
                }

                if (str6icmp(m, 'i', 'n', 'c', 'r', 'b', 'y')) {
                    r->type = MSG_REQ_REDIS_INCRBY;
                    break;
                }

                if (str6icmp(m, 'l', 'i', 'n', 'd', 'e', 'x')) {
                    r->type = MSG_REQ_REDIS_LINDEX;
                    break;
                }

                if (str6icmp(m, 'l', 'p', 'u', 's', 'h', 'x')) {
                    r->type = MSG_REQ_REDIS_LPUSHX;
                    break;
                }

                if (str6icmp(m, 'l', 'r', 'a', 'n', 'g', 'e')) {
                    r->type = MSG_REQ_REDIS_LRANGE;
                    break;
                }

                if (str6icmp(m, 'r', 'p', 'u', 's', 'h', 'x')) {
                    r->type = MSG_REQ_REDIS_RPUSHX;
                    break;
                }

                if (str6icmp(m, 's', 'e', 't', 'b', 'i', 't')) {
                    r->type = MSG_REQ_REDIS_SETBIT;
                    break;
                }

944 945 946 947 948
                if (str6icmp(m, 's', 'i', 'n', 't', 'e', 'r')) {
                    r->type = MSG_REQ_REDIS_SINTER;
                    break;
                }

M
Manju Rajashekhar 已提交
949 950 951 952 953
                if (str6icmp(m, 's', 't', 'r', 'l', 'e', 'n')) {
                    r->type = MSG_REQ_REDIS_STRLEN;
                    break;
                }

954 955 956 957 958
                if (str6icmp(m, 's', 'u', 'n', 'i', 'o', 'n')) {
                    r->type = MSG_REQ_REDIS_SUNION;
                    break;
                }

M
Manju Rajashekhar 已提交
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
                if (str6icmp(m, 'z', 'c', 'o', 'u', 'n', 't')) {
                    r->type = MSG_REQ_REDIS_ZCOUNT;
                    break;
                }

                if (str6icmp(m, 'z', 'r', 'a', 'n', 'g', 'e')) {
                    r->type = MSG_REQ_REDIS_ZRANGE;
                    break;
                }

                if (str6icmp(m, 'z', 's', 'c', 'o', 'r', 'e')) {
                    r->type = MSG_REQ_REDIS_ZSCORE;
                    break;
                }

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
                if (str6icmp(m, 'g', 'e', 'o', 'p', 'o', 's')) {
                    r->type = MSG_REQ_REDIS_GEOPOS;
                    break;
                }

                if (str6icmp(m, 'g', 'e', 'o', 'a', 'd', 'd')) {
                    r->type = MSG_REQ_REDIS_GEOADD;
                    break;
                }

                if (str6icmp(m, 'g', 'e', 't', 'd', 'e', 'l')) {
                    r->type = MSG_REQ_REDIS_GETDEL;
                    break;
                }

                if (str6icmp(m, 'z', 'u', 'n', 'i', 'o', 'n')) {
                    r->type = MSG_REQ_REDIS_ZUNION;
                    break;
                }

                if (str6icmp(m, 'z', 'i', 'n', 't', 'e', 'r')) {
                    r->type = MSG_REQ_REDIS_ZINTER;
                    break;
                }

                if (str6icmp(m, 'u', 'n', 'l', 'i', 'n', 'k')) {
                    r->type = MSG_REQ_REDIS_UNLINK;
                    break;
                }

1004 1005
                if (str6icmp(m, 'l', 'o', 'l', 'w', 'u', 't')) {
                    r->type = MSG_REQ_REDIS_LOLWUT;
1006
                    if (!msg_set_placeholder_key(r)) {
1007 1008 1009 1010 1011
                        goto enomem;
                    }
                    break;
                }

M
Manju Rajashekhar 已提交
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
                break;

            case 7:
                if (str7icmp(m, 'p', 'e', 'r', 's', 'i', 's', 't')) {
                    r->type = MSG_REQ_REDIS_PERSIST;
                    break;
                }

                if (str7icmp(m, 'p', 'e', 'x', 'p', 'i', 'r', 'e')) {
                    r->type = MSG_REQ_REDIS_PEXPIRE;
                    break;
                }

                if (str7icmp(m, 'h', 'e', 'x', 'i', 's', 't', 's')) {
                    r->type = MSG_REQ_REDIS_HEXISTS;
                    break;
                }

                if (str7icmp(m, 'h', 'g', 'e', 't', 'a', 'l', 'l')) {
                    r->type = MSG_REQ_REDIS_HGETALL;
                    break;
                }

                if (str7icmp(m, 'h', 'i', 'n', 'c', 'r', 'b', 'y')) {
                    r->type = MSG_REQ_REDIS_HINCRBY;
                    break;
                }

                if (str7icmp(m, 'l', 'i', 'n', 's', 'e', 'r', 't')) {
                    r->type = MSG_REQ_REDIS_LINSERT;
                    break;
                }

                if (str7icmp(m, 'z', 'i', 'n', 'c', 'r', 'b', 'y')) {
                    r->type = MSG_REQ_REDIS_ZINCRBY;
                    break;
                }

1050 1051 1052 1053 1054
                if (str7icmp(m, 'e', 'v', 'a', 'l', 's', 'h', 'a')) {
                    r->type = MSG_REQ_REDIS_EVALSHA;
                    break;
                }

1055 1056 1057 1058 1059
                if (str7icmp(m, 'r', 'e', 's', 't', 'o', 'r', 'e')) {
                    r->type = MSG_REQ_REDIS_RESTORE;
                    break;
                }

1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
                if (str7icmp(m, 'p', 'f', 'c', 'o', 'u', 'n', 't')) {
                    r->type = MSG_REQ_REDIS_PFCOUNT;
                    break;
                }

                if (str7icmp(m, 'p', 'f', 'm', 'e', 'r', 'g', 'e')) {
                    r->type = MSG_REQ_REDIS_PFMERGE;
                    break;
                }

1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
                if (str7icmp(m, 'z', 'm', 's', 'c', 'o', 'r', 'e')) {
                    r->type = MSG_REQ_REDIS_ZMSCORE;
                    break;
                }

                if (str7icmp(m, 'z', 'p', 'o', 'p', 'm', 'i', 'n')) {
                    r->type = MSG_REQ_REDIS_ZPOPMIN;
                    break;
                }

                if (str7icmp(m, 'z', 'p', 'o', 'p', 'm', 'a', 'x')) {
                    r->type = MSG_REQ_REDIS_ZPOPMAX;
                    break;
                }

                if (str7icmp(m, 'g', 'e', 'o', 'd', 'i', 's', 't')) {
                    r->type = MSG_REQ_REDIS_GEODIST;
                    break;
                }

                if (str7icmp(m, 'g', 'e', 'o', 'h', 'a', 's', 'h')) {
                    r->type = MSG_REQ_REDIS_GEOHASH;
                    break;
                }

                if (str7icmp(m, 'h', 's', 't', 'r', 'l', 'e', 'n')) {
                    r->type = MSG_REQ_REDIS_HSTRLEN;
                    break;
                }

1100 1101
                if (str7icmp(m, 'c', 'o', 'm', 'm', 'a', 'n', 'd')) {
                    r->type = MSG_REQ_REDIS_COMMAND;
1102
                    if (!msg_set_placeholder_key(r)) {
1103 1104 1105 1106 1107
                        goto enomem;
                    }
                    break;
                }

M
Manju Rajashekhar 已提交
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
                break;

            case 8:
                if (str8icmp(m, 'e', 'x', 'p', 'i', 'r', 'e', 'a', 't')) {
                    r->type = MSG_REQ_REDIS_EXPIREAT;
                    break;
                }

                if (str8icmp(m, 'b', 'i', 't', 'c', 'o', 'u', 'n', 't')) {
                    r->type = MSG_REQ_REDIS_BITCOUNT;
                    break;
                }

                if (str8icmp(m, 'g', 'e', 't', 'r', 'a', 'n', 'g', 'e')) {
                    r->type = MSG_REQ_REDIS_GETRANGE;
                    break;
                }

                if (str8icmp(m, 's', 'e', 't', 'r', 'a', 'n', 'g', 'e')) {
                    r->type = MSG_REQ_REDIS_SETRANGE;
                    break;
                }

                if (str8icmp(m, 's', 'm', 'e', 'm', 'b', 'e', 'r', 's')) {
                    r->type = MSG_REQ_REDIS_SMEMBERS;
                    break;
                }

                if (str8icmp(m, 'z', 'r', 'e', 'v', 'r', 'a', 'n', 'k')) {
                    r->type = MSG_REQ_REDIS_ZREVRANK;
                    break;
                }

1141 1142 1143 1144 1145
                if (str8icmp(m, 'b', 'i', 't', 'f', 'i', 'e', 'l', 'd')) {
                    r->type = MSG_REQ_REDIS_BITFIELD;
                    break;
                }

M
Manju Rajashekhar 已提交
1146 1147 1148 1149 1150 1151 1152 1153
                break;

            case 9:
                if (str9icmp(m, 'p', 'e', 'x', 'p', 'i', 'r', 'e', 'a', 't')) {
                    r->type = MSG_REQ_REDIS_PEXPIREAT;
                    break;
                }

1154 1155 1156 1157 1158
                if (str9icmp(m, 'r', 'p', 'o', 'p', 'l', 'p', 'u', 's', 'h')) {
                    r->type = MSG_REQ_REDIS_RPOPLPUSH;
                    break;
                }

M
Manju Rajashekhar 已提交
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
                if (str9icmp(m, 's', 'i', 's', 'm', 'e', 'm', 'b', 'e', 'r')) {
                    r->type = MSG_REQ_REDIS_SISMEMBER;
                    break;
                }

                if (str9icmp(m, 'z', 'r', 'e', 'v', 'r', 'a', 'n', 'g', 'e')) {
                    r->type = MSG_REQ_REDIS_ZREVRANGE;
                    break;
                }

M
Matt Robenolt 已提交
1169 1170 1171 1172 1173
                if (str9icmp(m, 'z', 'l', 'e', 'x', 'c', 'o', 'u', 'n', 't')) {
                    r->type = MSG_REQ_REDIS_ZLEXCOUNT;
                    break;
                }

1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
                if (str9icmp(m, 'g', 'e', 'o', 's', 'e', 'a', 'r', 'c', 'h')) {
                    r->type = MSG_REQ_REDIS_GEOSEARCH;
                    break;
                }

                if (str9icmp(m, 'g', 'e', 'o', 'r', 'a', 'd', 'i', 'u', 's')) {
                    r->type = MSG_REQ_REDIS_GEORADIUS;
                    break;
                }

M
Manju Rajashekhar 已提交
1184 1185
                break;

1186 1187 1188 1189 1190 1191
            case 10:
                if (str10icmp(m, 's', 'd', 'i', 'f', 'f', 's', 't', 'o', 'r', 'e')) {
                    r->type = MSG_REQ_REDIS_SDIFFSTORE;
                    break;
                }

1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
                if (str10icmp(m, 'h', 'r', 'a', 'n', 'd', 'f', 'i', 'e', 'l', 'd')) {
                    r->type = MSG_REQ_REDIS_HRANDFIELD;
                    break;
                }

                if (str10icmp(m, 's', 'm', 'i', 's', 'm', 'e', 'm', 'b', 'e', 'r')) {
                    r->type = MSG_REQ_REDIS_SMISMEMBER;
                    break;
                }

                if (str10icmp(m, 'z', 'd', 'i', 'f', 'f', 's', 't', 'o', 'r', 'e')) {
                    r->type = MSG_REQ_REDIS_ZDIFFSTORE;
                    break;
                }


                break;

M
Manju Rajashekhar 已提交
1210 1211 1212 1213 1214 1215
            case 11:
                if (str11icmp(m, 'i', 'n', 'c', 'r', 'b', 'y', 'f', 'l', 'o', 'a', 't')) {
                    r->type = MSG_REQ_REDIS_INCRBYFLOAT;
                    break;
                }

1216 1217 1218 1219 1220
                if (str11icmp(m, 's', 'i', 'n', 't', 'e', 'r', 's', 't', 'o', 'r', 'e')) {
                    r->type = MSG_REQ_REDIS_SINTERSTORE;
                    break;
                }

M
Manju Rajashekhar 已提交
1221 1222 1223 1224 1225
                if (str11icmp(m, 's', 'r', 'a', 'n', 'd', 'm', 'e', 'm', 'b', 'e', 'r')) {
                    r->type = MSG_REQ_REDIS_SRANDMEMBER;
                    break;
                }

1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
                if (str11icmp(m, 's', 'u', 'n', 'i', 'o', 'n', 's', 't', 'o', 'r', 'e')) {
                    r->type = MSG_REQ_REDIS_SUNIONSTORE;
                    break;
                }

                if (str11icmp(m, 'z', 'i', 'n', 't', 'e', 'r', 's', 't', 'o', 'r', 'e')) {
                    r->type = MSG_REQ_REDIS_ZINTERSTORE;
                    break;
                }

                if (str11icmp(m, 'z', 'u', 'n', 'i', 'o', 'n', 's', 't', 'o', 'r', 'e')) {
                    r->type = MSG_REQ_REDIS_ZUNIONSTORE;
                    break;
                }

M
Matt Robenolt 已提交
1241 1242 1243 1244 1245
                if (str11icmp(m, 'z', 'r', 'a', 'n', 'g', 'e', 'b', 'y', 'l', 'e', 'x')) {
                    r->type = MSG_REQ_REDIS_ZRANGEBYLEX;
                    break;
                }

1246
                if (str11icmp(m, 'z', 'r', 'a', 'n', 'd', 'm', 'e', 'm', 'b', 'e', 'r')) {
1247
                    r->type = MSG_REQ_REDIS_ZRANDMEMBER;
1248 1249 1250 1251 1252 1253 1254 1255
                    break;
                }

                if (str11icmp(m, 'z', 'r', 'a', 'n', 'g', 'e', 's', 't', 'o', 'r', 'e')) {
                    r->type = MSG_REQ_REDIS_ZRANGESTORE;
                    break;
                }

M
Manju Rajashekhar 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
                break;

            case 12:
                if (str12icmp(m, 'h', 'i', 'n', 'c', 'r', 'b', 'y', 'f', 'l', 'o', 'a', 't')) {
                    r->type = MSG_REQ_REDIS_HINCRBYFLOAT;
                    break;
                }


                break;

            case 13:
                if (str13icmp(m, 'z', 'r', 'a', 'n', 'g', 'e', 'b', 'y', 's', 'c', 'o', 'r', 'e')) {
                    r->type = MSG_REQ_REDIS_ZRANGEBYSCORE;
                    break;
                }

                break;

M
Matt Robenolt 已提交
1275 1276 1277 1278 1279 1280
            case 14:
                if (str14icmp(m, 'z', 'r', 'e', 'm', 'r', 'a', 'n', 'g', 'e', 'b', 'y', 'l', 'e', 'x')) {
                    r->type = MSG_REQ_REDIS_ZREMRANGEBYLEX;
                    break;
                }

1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
                if (str14icmp(m, 'z', 'r', 'e', 'v', 'r', 'a', 'n', 'g', 'e', 'b', 'y', 'l', 'e', 'x')) {
                    r->type = MSG_REQ_REDIS_ZREVRANGEBYLEX;
                    break;
                }
                if (str14icmp(m, 'g', 'e', 'o', 's', 'e', 'a', 'r', 'c', 'h', 's', 't', 'o', 'r', 'e')) {
                    r->type = MSG_REQ_REDIS_GEOSEARCHSTORE;
                    break;
                }


M
Matt Robenolt 已提交
1291 1292
                break;

M
Manju Rajashekhar 已提交
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
            case 15:
                if (str15icmp(m, 'z', 'r', 'e', 'm', 'r', 'a', 'n', 'g', 'e', 'b', 'y', 'r', 'a', 'n', 'k')) {
                    r->type = MSG_REQ_REDIS_ZREMRANGEBYRANK;
                    break;
                }

                break;

            case 16:
                if (str16icmp(m, 'z', 'r', 'e', 'm', 'r', 'a', 'n', 'g', 'e', 'b', 'y', 's', 'c', 'o', 'r', 'e')) {
                    r->type = MSG_REQ_REDIS_ZREMRANGEBYSCORE;
                    break;
                }

                if (str16icmp(m, 'z', 'r', 'e', 'v', 'r', 'a', 'n', 'g', 'e', 'b', 'y', 's', 'c', 'o', 'r', 'e')) {
                    r->type = MSG_REQ_REDIS_ZREVRANGEBYSCORE;
                    break;
                }
                break;

1313 1314 1315 1316 1317 1318
            case 17:
                if (str17icmp(m, 'g', 'e', 'o', 'r', 'a', 'd', 'i', 'u', 's', 'b', 'y', 'm', 'e', 'm', 'b', 'e', 'r')) {
                    r->type = MSG_REQ_REDIS_GEORADIUSBYMEMBER;
                    break;
                }

M
Manju Rajashekhar 已提交
1319 1320 1321 1322 1323
            default:
                break;
            }

            if (r->type == MSG_UNKNOWN) {
1324
                log_error("parsed unsupported command '%.*s'", (int)(p - m), m);
M
Manju Rajashekhar 已提交
1325 1326 1327
                goto error;
            }

1328
            log_debug(LOG_VERB, "parsed command '%.*s'", (int)(p - m), m);
M
Manju Rajashekhar 已提交
1329 1330 1331 1332 1333 1334 1335

            state = SW_REQ_TYPE_LF;
            break;

        case SW_REQ_TYPE_LF:
            switch (ch) {
            case LF:
I
idning 已提交
1336
                if (redis_argz(r)) {
1337 1338 1339 1340
                    if (r->narg != 1) {
                        /* It's an error to provide more than one argument. */
                        goto error;
                    }
I
idning 已提交
1341
                    goto done;
1342 1343 1344 1345 1346
                } else if (redis_nokey(r)) {
                    if (r->narg == 1) {
                        goto done;
                    }
                    state = SW_ARGN_LEN;
C
clark.kang 已提交
1347 1348
                } else if (r->narg == 1) {
                    goto error;
I
idning 已提交
1349
                } else if (redis_argeval(r)) {
1350 1351 1352 1353
                    state = SW_ARG1_LEN;
                } else {
                    state = SW_KEY_LEN;
                }
M
Manju Rajashekhar 已提交
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
                break;

            default:
                goto error;
            }

            break;

        case SW_KEY_LEN:
            if (r->token == NULL) {
                if (ch != '$') {
                    goto error;
                }
                r->token = p;
                r->rlen = 0;
            } else if (isdigit(ch)) {
                r->rlen = r->rlen * 10 + (uint32_t)(ch - '0');
            } else if (ch == CR) {
1372
                if (r->rlen >= mbuf_data_size()) {
M
Manju Rajashekhar 已提交
1373
                    log_error("parsed bad req %"PRIu64" of type %d with key "
1374
                              "length %d that greater than or equal to maximum"
1375
                              " redis key length of %zu", r->id, r->type,
1376
                              r->rlen, mbuf_data_size());
M
Manju Rajashekhar 已提交
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
                    goto error;
                }
                if (r->rnarg == 0) {
                    goto error;
                }
                r->rnarg--;
                r->token = NULL;
                state = SW_KEY_LEN_LF;
            } else {
                goto error;
            }

            break;

        case SW_KEY_LEN_LF:
            switch (ch) {
            case LF:
                state = SW_KEY;
                break;

            default:
                goto error;
            }

            break;

        case SW_KEY:
            if (r->token == NULL) {
                r->token = p;
            }

            m = r->token + r->rlen;
            if (m >= b->last) {
                m = b->last - 1;
                p = m;
                break;
            }

            if (*m != CR) {
                goto error;
1417 1418
            } else {        /* got a key */
                struct keypos *kpos;
M
Manju Rajashekhar 已提交
1419

1420 1421 1422 1423
                p = m;      /* move forward by rlen bytes */
                r->rlen = 0;
                m = r->token;
                r->token = NULL;
M
Manju Rajashekhar 已提交
1424

1425 1426 1427 1428 1429 1430
                kpos = array_push(r->keys);
                if (kpos == NULL) {
                    goto enomem;
                }
                kpos->start = m;
                kpos->end = p;
M
Manju Rajashekhar 已提交
1431

1432 1433
                state = SW_KEY_LF;
            }
M
Manju Rajashekhar 已提交
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468

            break;

        case SW_KEY_LF:
            switch (ch) {
            case LF:
                if (redis_arg0(r)) {
                    if (r->rnarg != 0) {
                        goto error;
                    }
                    goto done;
                } else if (redis_arg1(r)) {
                    if (r->rnarg != 1) {
                        goto error;
                    }
                    state = SW_ARG1_LEN;
                } else if (redis_arg2(r)) {
                    if (r->rnarg != 2) {
                        goto error;
                    }
                    state = SW_ARG1_LEN;
                } else if (redis_arg3(r)) {
                    if (r->rnarg != 3) {
                        goto error;
                    }
                    state = SW_ARG1_LEN;
                } else if (redis_argn(r)) {
                    if (r->rnarg == 0) {
                        goto done;
                    }
                    state = SW_ARG1_LEN;
                } else if (redis_argx(r)) {
                    if (r->rnarg == 0) {
                        goto done;
                    }
1469
                    state = SW_KEY_LEN;
I
idning 已提交
1470
                } else if (redis_argkvx(r)) {
1471 1472 1473
                    if (r->narg % 2 == 0) {
                        goto error;
                    }
I
idning 已提交
1474
                    state = SW_ARG1_LEN;
1475 1476 1477 1478 1479
                } else if (redis_argeval(r)) {
                    if (r->rnarg == 0) {
                        goto done;
                    }
                    state = SW_ARGN_LEN;
M
Manju Rajashekhar 已提交
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
                } else {
                    goto error;
                }

                break;

            default:
                goto error;
            }

            break;

        case SW_ARG1_LEN:
            if (r->token == NULL) {
                if (ch != '$') {
                    goto error;
                }
                r->rlen = 0;
                r->token = p;
            } else if (isdigit(ch)) {
                r->rlen = r->rlen * 10 + (uint32_t)(ch - '0');
            } else if (ch == CR) {
                if ((p - r->token) <= 1 || r->rnarg == 0) {
                    goto error;
                }
                r->rnarg--;
                r->token = NULL;
                state = SW_ARG1_LEN_LF;
            } else {
                goto error;
            }

            break;

        case SW_ARG1_LEN_LF:
            switch (ch) {
            case LF:
                state = SW_ARG1;
                break;

            default:
                goto error;
            }

            break;

        case SW_ARG1:
            m = p + r->rlen;
            if (m >= b->last) {
                r->rlen -= (uint32_t)(b->last - p);
                m = b->last - 1;
                p = m;
                break;
            }

            if (*m != CR) {
                goto error;
            }

            p = m; /* move forward by rlen bytes */
            r->rlen = 0;

            state = SW_ARG1_LF;

            break;

        case SW_ARG1_LF:
            switch (ch) {
            case LF:
                if (redis_arg1(r)) {
                    if (r->rnarg != 0) {
                        goto error;
                    }
                    goto done;
                } else if (redis_arg2(r)) {
                    if (r->rnarg != 1) {
                        goto error;
                    }
                    state = SW_ARG2_LEN;
                } else if (redis_arg3(r)) {
                    if (r->rnarg != 2) {
                        goto error;
                    }
                    state = SW_ARG2_LEN;
                } else if (redis_argn(r)) {
                    if (r->rnarg == 0) {
                        goto done;
                    }
                    state = SW_ARGN_LEN;
1569
                } else if (redis_argeval(r)) {
1570
                    if (r->rnarg < 2) {
1571 1572 1573
                        goto error;
                    }
                    state = SW_ARG2_LEN;
I
idning 已提交
1574
                } else if (redis_argkvx(r)) {
I
idning 已提交
1575 1576 1577 1578
                    if (r->rnarg == 0) {
                        goto done;
                    }
                    state = SW_KEY_LEN;
M
Manju Rajashekhar 已提交
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
                } else {
                    goto error;
                }

                break;

            default:
                goto error;
            }

            break;

        case SW_ARG2_LEN:
            if (r->token == NULL) {
                if (ch != '$') {
                    goto error;
                }
                r->rlen = 0;
                r->token = p;
            } else if (isdigit(ch)) {
                r->rlen = r->rlen * 10 + (uint32_t)(ch - '0');
            } else if (ch == CR) {
                if ((p - r->token) <= 1 || r->rnarg == 0) {
                    goto error;
                }
                r->rnarg--;
                r->token = NULL;
                state = SW_ARG2_LEN_LF;
            } else {
                goto error;
            }

            break;

        case SW_ARG2_LEN_LF:
            switch (ch) {
            case LF:
                state = SW_ARG2;
                break;

            default:
                goto error;
            }

            break;

        case SW_ARG2:
1626 1627
            if (r->token == NULL && redis_argeval(r)) {
                /*
M
Manju Rajashekhar 已提交
1628
                 * For EVAL/EVALSHA, ARG2 represents the # key/arg pairs which must
1629 1630
                 * be tokenized and stored in contiguous memory.
                 */
1631 1632 1633
                r->token = p;
            }

M
Manju Rajashekhar 已提交
1634 1635
            m = p + r->rlen;
            if (m >= b->last) {
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
                /* 
                 * For EVAL/EVALHASH, the r->token has been assigned a value.  When
                 * m >= b->last happens will need to repair mbuf.  
                 * 
                 * At the end of redis_parse_req, r->token will be used to choose
                 * the start (p) for the next call to redis_parse_req and clear
                 * r->token when repairing this and adding more data.
                 * 
                 * So, only when r->token == NULL we need to calculate r->rlen again.
                 */
                if (r->token == NULL) {
                    r->rlen -= (uint32_t)(b->last - p);
                }
M
Manju Rajashekhar 已提交
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
                m = b->last - 1;
                p = m;
                break;
            }

            if (*m != CR) {
                goto error;
            }

            p = m; /* move forward by rlen bytes */
            r->rlen = 0;
1660 1661

            if (redis_argeval(r)) {
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
                uint32_t nkey;
                uint8_t *chp;

                /*
                 * For EVAL/EVALSHA, we need to find the integer value of this
                 * argument. It tells us the number of keys in the script, and
                 * we need to error out if number of keys is 0. At this point,
                 * both p and m point to the end of the argument and r->token
                 * points to the start.
                 */
                if (p - r->token < 1) {
1673
                    goto error;
1674 1675 1676 1677 1678 1679
                }

                for (nkey = 0, chp = r->token; chp < p; chp++) {
                    if (isdigit(*chp)) {
                        nkey = nkey * 10 + (uint32_t)(*chp - '0');
                    } else {
1680
                        goto error;
1681
                    }
1682
                }
1683
                if (nkey == 0) {
1684
                    goto error;
1685 1686 1687
                }

                r->token = NULL;
1688
            }
1689

M
Manju Rajashekhar 已提交
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711
            state = SW_ARG2_LF;

            break;

        case SW_ARG2_LF:
            switch (ch) {
            case LF:
                if (redis_arg2(r)) {
                    if (r->rnarg != 0) {
                        goto error;
                    }
                    goto done;
                } else if (redis_arg3(r)) {
                    if (r->rnarg != 1) {
                        goto error;
                    }
                    state = SW_ARG3_LEN;
                } else if (redis_argn(r)) {
                    if (r->rnarg == 0) {
                        goto done;
                    }
                    state = SW_ARGN_LEN;
1712
                } else if (redis_argeval(r)) {
1713
                    if (r->rnarg < 1) {
1714 1715 1716
                        goto error;
                    }
                    state = SW_KEY_LEN;
M
Manju Rajashekhar 已提交
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822
                } else {
                    goto error;
                }

                break;

            default:
                goto error;
            }

            break;

        case SW_ARG3_LEN:
            if (r->token == NULL) {
                if (ch != '$') {
                    goto error;
                }
                r->rlen = 0;
                r->token = p;
            } else if (isdigit(ch)) {
                r->rlen = r->rlen * 10 + (uint32_t)(ch - '0');
            } else if (ch == CR) {
                if ((p - r->token) <= 1 || r->rnarg == 0) {
                    goto error;
                }
                r->rnarg--;
                r->token = NULL;
                state = SW_ARG3_LEN_LF;
            } else {
                goto error;
            }

            break;

        case SW_ARG3_LEN_LF:
            switch (ch) {
            case LF:
                state = SW_ARG3;
                break;

            default:
                goto error;
            }

            break;

        case SW_ARG3:
            m = p + r->rlen;
            if (m >= b->last) {
                r->rlen -= (uint32_t)(b->last - p);
                m = b->last - 1;
                p = m;
                break;
            }

            if (*m != CR) {
                goto error;
            }

            p = m; /* move forward by rlen bytes */
            r->rlen = 0;
            state = SW_ARG3_LF;

            break;

        case SW_ARG3_LF:
            switch (ch) {
            case LF:
                if (redis_arg3(r)) {
                    if (r->rnarg != 0) {
                        goto error;
                    }
                    goto done;
                } else if (redis_argn(r)) {
                    if (r->rnarg == 0) {
                        goto done;
                    }
                    state = SW_ARGN_LEN;
                } else {
                    goto error;
                }

                break;

            default:
                goto error;
            }

            break;

        case SW_ARGN_LEN:
            if (r->token == NULL) {
                if (ch != '$') {
                    goto error;
                }
                r->rlen = 0;
                r->token = p;
            } else if (isdigit(ch)) {
                r->rlen = r->rlen * 10 + (uint32_t)(ch - '0');
            } else if (ch == CR) {
                if ((p - r->token) <= 1 || r->rnarg == 0) {
                    goto error;
                }
                r->rnarg--;
                r->token = NULL;
                state = SW_ARGN_LEN_LF;
I
idning 已提交
1823
            } else {
M
Manju Rajashekhar 已提交
1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
                goto error;
            }

            break;

        case SW_ARGN_LEN_LF:
            switch (ch) {
            case LF:
                state = SW_ARGN;
                break;

            default:
                goto error;
            }

            break;

        case SW_ARGN:
            m = p + r->rlen;
            if (m >= b->last) {
                r->rlen -= (uint32_t)(b->last - p);
                m = b->last - 1;
                p = m;
                break;
            }

            if (*m != CR) {
                goto error;
            }

            p = m; /* move forward by rlen bytes */
            r->rlen = 0;
            state = SW_ARGN_LF;

            break;

        case SW_ARGN_LF:
            switch (ch) {
            case LF:
1863
                if (redis_argn(r) || redis_argeval(r) || redis_nokey(r)) {
M
Manju Rajashekhar 已提交
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
                    if (r->rnarg == 0) {
                        goto done;
                    }
                    state = SW_ARGN_LEN;
                } else {
                    goto error;
                }

                break;

            default:
                goto error;
            }

            break;

        case SW_SENTINEL:
        default:
            NOT_REACHED();
            break;
        }
    }

    ASSERT(p == b->last);
    r->pos = p;
    r->state = state;

    if (b->last == b->end && r->token != NULL) {
        r->pos = r->token;
        r->token = NULL;
        r->result = MSG_PARSE_REPAIR;
    } else {
        r->result = MSG_PARSE_AGAIN;
    }

    log_hexdump(LOG_VERB, b->pos, mbuf_length(b), "parsed req %"PRIu64" res %d "
                "type %d state %d rpos %d of %d", r->id, r->result, r->type,
1901
                r->state, (int)(r->pos - b->pos), (int)(b->last - b->pos));
M
Manju Rajashekhar 已提交
1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913
    return;

done:
    ASSERT(r->type > MSG_UNKNOWN && r->type < MSG_SENTINEL);
    r->pos = p + 1;
    ASSERT(r->pos <= b->last);
    r->state = SW_START;
    r->token = NULL;
    r->result = MSG_PARSE_OK;

    log_hexdump(LOG_VERB, b->pos, mbuf_length(b), "parsed req %"PRIu64" res %d "
                "type %d state %d rpos %d of %d", r->id, r->result, r->type,
1914
                r->state, (int)(r->pos - b->pos), (int)(b->last - b->pos));
M
Manju Rajashekhar 已提交
1915 1916
    return;

1917 1918 1919 1920 1921 1922 1923 1924 1925
enomem:
    r->result = MSG_PARSE_ERROR;
    r->state = state;

    log_hexdump(LOG_INFO, b->pos, mbuf_length(b), "out of memory on parse req %"PRIu64" "
                "res %d type %d state %d", r->id, r->result, r->type, r->state);

    return;

M
Manju Rajashekhar 已提交
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
error:
    r->result = MSG_PARSE_ERROR;
    r->state = state;
    errno = EINVAL;

    log_hexdump(LOG_INFO, b->pos, mbuf_length(b), "parsed bad req %"PRIu64" "
                "res %d type %d state %d", r->id, r->result, r->type,
                r->state);
}

/*
 * Reference: http://redis.io/topics/protocol
 *
 * Redis will reply to commands with different kinds of replies. It is
 * possible to check the kind of reply from the first byte sent by the
 * server:
 *  - with a single line reply the first byte of the reply will be "+"
 *  - with an error message the first byte of the reply will be "-"
 *  - with an integer number the first byte of the reply will be ":"
 *  - with bulk reply the first byte of the reply will be "$"
 *  - with multi-bulk reply the first byte of the reply will be "*"
 *
 * 1). Status reply (or single line reply) is in the form of a single line
 *     string starting with "+" terminated by "\r\n".
 * 2). Error reply are similar to status replies. The only difference is
 *     that the first byte is "-" instead of "+".
 * 3). Integer reply is just a CRLF terminated string representing an
 *     integer, and prefixed by a ":" byte.
 * 4). Bulk reply is used by server to return a single binary safe string.
 *     The first reply line is a "$" byte followed by the number of bytes
 *     of the actual reply, followed by CRLF, then the actual data bytes,
 *     followed by additional two bytes for the final CRLF. If the requested
 *     value does not exist the bulk reply will use the special value '-1'
 *     as the data length.
 * 5). Multi-bulk reply is used by the server to return many binary safe
 *     strings (bulks) with the initial line indicating how many bulks that
 *     will follow. The first byte of a multi bulk reply is always *.
 */
void
redis_parse_rsp(struct msg *r)
{
    struct mbuf *b;
    uint8_t *p, *m;
    uint8_t ch;
C
CharSyam 已提交
1970

M
Manju Rajashekhar 已提交
1971 1972 1973 1974 1975
    enum {
        SW_START,
        SW_STATUS,
        SW_ERROR,
        SW_INTEGER_START,
C
clark.kang 已提交
1976
        SW_SIMPLE,
M
Manju Rajashekhar 已提交
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
        SW_BULK,
        SW_BULK_LF,
        SW_BULK_ARG,
        SW_BULK_ARG_LF,
        SW_MULTIBULK,
        SW_MULTIBULK_NARG_LF,
        SW_MULTIBULK_ARGN_LEN,
        SW_MULTIBULK_ARGN_LEN_LF,
        SW_MULTIBULK_ARGN,
        SW_MULTIBULK_ARGN_LF,
        SW_RUNTO_CRLF,
        SW_ALMOST_DONE,
        SW_SENTINEL
    } state;

    state = r->state;
    b = STAILQ_LAST(&r->mhdr, mbuf, next);

    ASSERT(!r->request);
    ASSERT(state >= SW_START && state < SW_SENTINEL);
    ASSERT(b != NULL);
    ASSERT(b->pos <= b->last);

    /* validate the parsing marker */
    ASSERT(r->pos != NULL);
    ASSERT(r->pos >= b->pos && r->pos <= b->last);

    for (p = r->pos; p < b->last; p++) {
        ch = *p;

        switch (state) {
        case SW_START:
            r->type = MSG_UNKNOWN;
2010 2011 2012
            r->rnarg = 1;
            r->is_top_level = 1;

M
Manju Rajashekhar 已提交
2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027
            switch (ch) {
            case '+':
                p = p - 1; /* go back by 1 byte */
                r->type = MSG_RSP_REDIS_STATUS;
                state = SW_STATUS;
                break;

            case '-':
                r->type = MSG_RSP_REDIS_ERROR;
                p = p - 1; /* go back by 1 byte */
                state = SW_ERROR;
                break;

            case ':':
                r->type = MSG_RSP_REDIS_INTEGER;
2028 2029
                r->integer = 0;
                state = SW_INTEGER_START;
M
Manju Rajashekhar 已提交
2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055
                break;

            case '$':
                r->type = MSG_RSP_REDIS_BULK;
                p = p - 1; /* go back by 1 byte */
                state = SW_BULK;
                break;

            case '*':
                r->type = MSG_RSP_REDIS_MULTIBULK;
                p = p - 1; /* go back by 1 byte */
                state = SW_MULTIBULK;
                break;

            default:
                goto error;
            }

            break;

        case SW_STATUS:
            /* rsp_start <- p */
            state = SW_RUNTO_CRLF;
            break;

        case SW_ERROR:
2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171
            if (r->token == NULL) {
                if (ch != '-') {
                    goto error;
                }
              /* rsp_start <- p */
              r->token = p;
            }
            if (ch == ' ' || ch == CR) {
                m = r->token;
                r->token = NULL;
                switch (p - m) {

                case 4:
                    /*
                     * -ERR no such key\r\n
                     * -ERR syntax error\r\n
                     * -ERR source and destination objects are the same\r\n
                     * -ERR index out of range\r\n
                     */
                    if (str4cmp(m, '-', 'E', 'R', 'R')) {
                        r->type = MSG_RSP_REDIS_ERROR_ERR;
                        break;
                    }

                    /* -OOM command not allowed when used memory > 'maxmemory'.\r\n */
                    if (str4cmp(m, '-', 'O', 'O', 'M')) {
                        r->type = MSG_RSP_REDIS_ERROR_OOM;
                        break;
                    }

                    break;

                case 5:
                    /* -BUSY Redis is busy running a script. You can only call SCRIPT KILL or SHUTDOWN NOSAVE.\r\n" */
                    if (str5cmp(m, '-', 'B', 'U', 'S', 'Y')) {
                        r->type = MSG_RSP_REDIS_ERROR_BUSY;
                        break;
                    }

                    break;

                case 7:
                    /* -NOAUTH Authentication required.\r\n */
                    if (str7cmp(m, '-', 'N', 'O', 'A', 'U', 'T', 'H')) {
                        r->type = MSG_RSP_REDIS_ERROR_NOAUTH;
                        break;
                    }

                    break;

                case 8:
                    /* rsp: "-LOADING Redis is loading the dataset in memory\r\n" */
                    if (str8cmp(m, '-', 'L', 'O', 'A', 'D', 'I', 'N', 'G')) {
                        r->type = MSG_RSP_REDIS_ERROR_LOADING;
                        break;
                    }

                    /* -BUSYKEY Target key name already exists.\r\n */
                    if (str8cmp(m, '-', 'B', 'U', 'S', 'Y', 'K', 'E', 'Y')) {
                        r->type = MSG_RSP_REDIS_ERROR_BUSYKEY;
                        break;
                    }

                    /* "-MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.\r\n" */
                    if (str8cmp(m, '-', 'M', 'I', 'S', 'C', 'O', 'N', 'F')) {
                        r->type = MSG_RSP_REDIS_ERROR_MISCONF;
                        break;
                    }

                    break;

                case 9:
                    /* -NOSCRIPT No matching script. Please use EVAL.\r\n */
                    if (str9cmp(m, '-', 'N', 'O', 'S', 'C', 'R', 'I', 'P', 'T')) {
                        r->type = MSG_RSP_REDIS_ERROR_NOSCRIPT;
                        break;
                    }

                    /* -READONLY You can't write against a read only slave.\r\n */
                    if (str9cmp(m, '-', 'R', 'E', 'A', 'D', 'O', 'N', 'L', 'Y')) {
                        r->type = MSG_RSP_REDIS_ERROR_READONLY;
                        break;
                    }

                    break;

                case 10:
                    /* -WRONGTYPE Operation against a key holding the wrong kind of value\r\n */
                    if (str10cmp(m, '-', 'W', 'R', 'O', 'N', 'G', 'T', 'Y', 'P', 'E')) {
                        r->type = MSG_RSP_REDIS_ERROR_WRONGTYPE;
                        break;
                    }

                    /* -EXECABORT Transaction discarded because of previous errors.\r\n" */
                    if (str10cmp(m, '-', 'E', 'X', 'E', 'C', 'A', 'B', 'O', 'R', 'T')) {
                        r->type = MSG_RSP_REDIS_ERROR_EXECABORT;
                        break;
                    }

                    break;

                case 11:
                    /* -MASTERDOWN Link with MASTER is down and slave-serve-stale-data is set to 'no'.\r\n */
                    if (str11cmp(m, '-', 'M', 'A', 'S', 'T', 'E', 'R', 'D', 'O', 'W', 'N')) {
                        r->type = MSG_RSP_REDIS_ERROR_MASTERDOWN;
                        break;
                    }

                    /* -NOREPLICAS Not enough good slaves to write.\r\n */
                    if (str11cmp(m, '-', 'N', 'O', 'R', 'E', 'P', 'L', 'I', 'C', 'A', 'S')) {
                        r->type = MSG_RSP_REDIS_ERROR_NOREPLICAS;
                        break;
                    }

                    break;
                }
2172 2173 2174 2175 2176 2177
                if (ch == '\r') {
                    state = SW_ALMOST_DONE;
                } else {
                    /* Read remaining characters until '\r' */
                    state = SW_RUNTO_CRLF;
                }
2178 2179
            }

M
Manju Rajashekhar 已提交
2180 2181
            break;

C
clark.kang 已提交
2182 2183
        case SW_SIMPLE:
            if (ch == CR) {
2184
                ASSERT(r->rnarg > 0);
2185 2186
                r->rnarg--;
                state = SW_MULTIBULK_ARGN_LF;
C
clark.kang 已提交
2187 2188 2189
            }
            break;

M
Manju Rajashekhar 已提交
2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
        case SW_INTEGER_START:
            if (ch == CR) {
                state = SW_ALMOST_DONE;
            } else if (ch == '-') {
                ;
            } else if (isdigit(ch)) {
                r->integer = r->integer * 10 + (uint32_t)(ch - '0');
            } else {
                goto error;
            }
            break;

        case SW_RUNTO_CRLF:
            switch (ch) {
            case CR:
                state = SW_ALMOST_DONE;
                break;

            default:
                break;
            }

            break;

        case SW_ALMOST_DONE:
            switch (ch) {
            case LF:
                /* rsp_end <- p */
                goto done;

            default:
                goto error;
            }

            break;

        case SW_BULK:
2227 2228 2229 2230 2231
            /*
             * SW_BULK is used for top-level bulk string replies.
             * Within an array, SW_MULTIBULK_ARG... helpers are used
             * to parse bulk strings instead.
             */
M
Manju Rajashekhar 已提交
2232 2233 2234 2235 2236 2237 2238 2239
            if (r->token == NULL) {
                if (ch != '$') {
                    goto error;
                }
                /* rsp_start <- p */
                r->token = p;
                r->rlen = 0;
            } else if (ch == '-') {
M
Manju Rajashekhar 已提交
2240
                /* handles null bulk reply = '$-1' */
M
Manju Rajashekhar 已提交
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303
                state = SW_RUNTO_CRLF;
            } else if (isdigit(ch)) {
                r->rlen = r->rlen * 10 + (uint32_t)(ch - '0');
            } else if (ch == CR) {
                if ((p - r->token) <= 1) {
                    goto error;
                }
                r->token = NULL;
                state = SW_BULK_LF;
            } else {
                goto error;
            }

            break;

        case SW_BULK_LF:
            switch (ch) {
            case LF:
                state = SW_BULK_ARG;
                break;

            default:
                goto error;
            }

            break;

        case SW_BULK_ARG:
            m = p + r->rlen;
            if (m >= b->last) {
                r->rlen -= (uint32_t)(b->last - p);
                m = b->last - 1;
                p = m;
                break;
            }

            if (*m != CR) {
                goto error;
            }

            p = m; /* move forward by rlen bytes */
            r->rlen = 0;

            state = SW_BULK_ARG_LF;

            break;

        case SW_BULK_ARG_LF:
            switch (ch) {
            case LF:
                goto done;

            default:
                goto error;
            }

            break;

        case SW_MULTIBULK:
            if (r->token == NULL) {
                if (ch != '*') {
                    goto error;
                }
2304
                r->vlen = 0;
M
Manju Rajashekhar 已提交
2305 2306
                r->token = p;
                /* rsp_start <- p */
2307 2308
                if (r->is_top_level) {
                    r->narg_start = p;
2309
                }
C
CharSyam 已提交
2310
            } else if (ch == '-') {
2311 2312
                p = p-1;
                r->token = NULL;
2313 2314 2315 2316 2317
                /*
                 * This is a null array (e.g. from BLPOP). Don't increment rnarg
                 * https://redis.io/topics/protocol
                 */
                r->vlen = 1;
2318
                state = SW_MULTIBULK_ARGN_LEN;
M
Manju Rajashekhar 已提交
2319
            } else if (isdigit(ch)) {
2320
                r->vlen = r->vlen * 10 + (uint32_t)(ch - '0');
M
Manju Rajashekhar 已提交
2321 2322 2323 2324
            } else if (ch == CR) {
                if ((p - r->token) <= 1) {
                    goto error;
                }
C
CharSyam 已提交
2325

2326 2327 2328 2329 2330 2331 2332 2333
                if (r->is_top_level) {
                    /* For multiget responses, we may need to know the number of responses to combine them. */
                    r->narg = r->vlen;
                    r->narg_end = p;
                }
                r->is_top_level = 0;
                ASSERT(r->rnarg > 0);
                r->rnarg += r->vlen - 1;
M
Manju Rajashekhar 已提交
2334
                r->token = NULL;
2335

2336 2337 2338 2339
                /*
                 * The stack is always initialized before transitioning
                 * to another state.
                 */
M
Manju Rajashekhar 已提交
2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351
                state = SW_MULTIBULK_NARG_LF;
            } else {
                goto error;
            }

            break;

        case SW_MULTIBULK_NARG_LF:
            switch (ch) {
            case LF:
                if (r->rnarg == 0) {
                    /* response is '*0\r\n' */
2352
                    goto done;
M
Manju Rajashekhar 已提交
2353
                }
C
clark.kang 已提交
2354

M
Manju Rajashekhar 已提交
2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365
                state = SW_MULTIBULK_ARGN_LEN;
                break;

            default:
                goto error;
            }

            break;

        case SW_MULTIBULK_ARGN_LEN:
            if (r->token == NULL) {
M
Manju Rajashekhar 已提交
2366 2367 2368 2369 2370 2371 2372 2373
                /*
                 * From: http://redis.io/topics/protocol, a multi bulk reply
                 * is used to return an array of other replies. Every element
                 * of a multi bulk reply can be of any kind, including a
                 * nested multi bulk reply.
                 *
                 * Here, we only handle a multi bulk reply element that
                 * are either integer reply or bulk reply.
I
idning 已提交
2374 2375
                 *
                 * there is a special case for sscan/hscan/zscan, these command
I
idning 已提交
2376
                 * replay a nested multi-bulk with a number and a multi bulk like this:
I
idning 已提交
2377
                 *
2378
                 * - multi-bulk
I
idning 已提交
2379
                 *    - cursor
2380
                 *    - multi-bulk
I
idning 已提交
2381 2382 2383 2384 2385 2386 2387 2388
                 *       - val1
                 *       - val2
                 *       - val3
                 *
                 * in this case, there is only one sub-multi-bulk,
                 * and it's the last element of parent,
                 * we can handle it like tail-recursive.
                 *
M
Manju Rajashekhar 已提交
2389
                 */
I
idning 已提交
2390 2391 2392 2393 2394 2395
                if (ch == '*') {    /* for sscan/hscan/zscan only */
                    p = p - 1;      /* go back by 1 byte */
                    state = SW_MULTIBULK;
                    break;
                }

C
clark.kang 已提交
2396 2397 2398 2399 2400 2401 2402 2403
                if (ch == ':' || ch == '+' || ch == '-') {
                    /* handles not-found reply = '$-1' or integer reply = ':<num>' */
                    /* and *2\r\n$2\r\nr0\r\n+OK\r\n or *1\r\n+OK\r\n */
                    state = SW_SIMPLE;
                    break;
                }

                if (ch != '$') {
M
Manju Rajashekhar 已提交
2404 2405
                    goto error;
                }
C
clark.kang 已提交
2406

M
Manju Rajashekhar 已提交
2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417
                r->token = p;
                r->rlen = 0;
            } else if (isdigit(ch)) {
                r->rlen = r->rlen * 10 + (uint32_t)(ch - '0');
            } else if (ch == '-') {
                ;
            } else if (ch == CR) {
                if ((p - r->token) <= 1 || r->rnarg == 0) {
                    goto error;
                }

C
clark.kang 已提交
2418
                if ((r->rlen == 1 && (p - r->token) == 3)) {
M
Manju Rajashekhar 已提交
2419 2420 2421 2422 2423
                    r->rlen = 0;
                    state = SW_MULTIBULK_ARGN_LF;
                } else {
                    state = SW_MULTIBULK_ARGN_LEN_LF;
                }
2424
                ASSERT(r->rnarg > 0);
M
Manju Rajashekhar 已提交
2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467
                r->rnarg--;
                r->token = NULL;
            } else {
                goto error;
            }

            break;

        case SW_MULTIBULK_ARGN_LEN_LF:
            switch (ch) {
            case LF:
                state = SW_MULTIBULK_ARGN;
                break;

            default:
                goto error;
            }

            break;

        case SW_MULTIBULK_ARGN:
            m = p + r->rlen;
            if (m >= b->last) {
                r->rlen -= (uint32_t)(b->last - p);
                m = b->last - 1;
                p = m;
                break;
            }

            if (*m != CR) {
                goto error;
            }

            p += r->rlen; /* move forward by rlen bytes */
            r->rlen = 0;

            state = SW_MULTIBULK_ARGN_LF;

            break;

        case SW_MULTIBULK_ARGN_LF:
            switch (ch) {
            case LF:
2468
                if (r->rnarg == 0) {
M
Manju Rajashekhar 已提交
2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501
                    goto done;
                }

                state = SW_MULTIBULK_ARGN_LEN;
                break;

            default:
                goto error;
            }

            break;

        case SW_SENTINEL:
        default:
            NOT_REACHED();
            break;
        }
    }

    ASSERT(p == b->last);
    r->pos = p;
    r->state = state;

    if (b->last == b->end && r->token != NULL) {
        r->pos = r->token;
        r->token = NULL;
        r->result = MSG_PARSE_REPAIR;
    } else {
        r->result = MSG_PARSE_AGAIN;
    }

    log_hexdump(LOG_VERB, b->pos, mbuf_length(b), "parsed rsp %"PRIu64" res %d "
                "type %d state %d rpos %d of %d", r->id, r->result, r->type,
2502
                r->state, (int)(r->pos - b->pos), (int)(b->last - b->pos));
M
Manju Rajashekhar 已提交
2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514
    return;

done:
    ASSERT(r->type > MSG_UNKNOWN && r->type < MSG_SENTINEL);
    r->pos = p + 1;
    ASSERT(r->pos <= b->last);
    r->state = SW_START;
    r->token = NULL;
    r->result = MSG_PARSE_OK;

    log_hexdump(LOG_VERB, b->pos, mbuf_length(b), "parsed rsp %"PRIu64" res %d "
                "type %d state %d rpos %d of %d", r->id, r->result, r->type,
2515
                r->state, (int)(r->pos - b->pos), (int)(b->last - b->pos));
M
Manju Rajashekhar 已提交
2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527
    return;

error:
    r->result = MSG_PARSE_ERROR;
    r->state = state;
    errno = EINVAL;

    log_hexdump(LOG_INFO, b->pos, mbuf_length(b), "parsed bad rsp %"PRIu64" "
                "res %d type %d state %d", r->id, r->result, r->type,
                r->state);
}

2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541
/*
 * Return true, if redis replies with a transient server failure response,
 * otherwise return false
 *
 * Transient failures on redis are scenarios when it is temporarily
 * unresponsive and responds with the following protocol specific error
 * reply:
 * -OOM, when redis is out-of-memory
 * -BUSY, when redis is busy
 * -LOADING when redis is loading dataset into memory
 *
 * See issue: https://github.com/twitter/twemproxy/issues/369
 */
bool
T
Tyson Andre 已提交
2542
redis_failure(const struct msg *r)
2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558
{
    ASSERT(!r->request);

    switch (r->type) {
    case MSG_RSP_REDIS_ERROR_OOM:
    case MSG_RSP_REDIS_ERROR_BUSY:
    case MSG_RSP_REDIS_ERROR_LOADING:
        return true;

    default:
        break;
    }

    return false;
}

M
Manju Rajashekhar 已提交
2559
/*
2560 2561 2562 2563
 * copy one bulk from src to dst
 *
 * if dst == NULL, we just eat the bulk
 *
2564
 * */
I
idning 已提交
2565
static rstatus_t
2566
redis_copy_bulk(struct msg *dst, struct msg *src)
M
Manju Rajashekhar 已提交
2567
{
2568 2569 2570 2571
    struct mbuf *mbuf, *nbuf;
    uint8_t *p;
    uint32_t len = 0;
    uint32_t bytes = 0;
2572
    rstatus_t status;
2573

I
idning 已提交
2574
    for (mbuf = STAILQ_FIRST(&src->mhdr);
I
idning 已提交
2575
         mbuf && mbuf_empty(mbuf);
I
idning 已提交
2576 2577
         mbuf = STAILQ_FIRST(&src->mhdr)) {

2578 2579
        mbuf_remove(&src->mhdr, mbuf);
        mbuf_put(mbuf);
M
Manju Rajashekhar 已提交
2580 2581
    }

2582 2583
    mbuf = STAILQ_FIRST(&src->mhdr);
    if (mbuf == NULL) {
I
idning 已提交
2584
        return NC_ERROR;
M
Manju Rajashekhar 已提交
2585 2586
    }

2587 2588 2589
    p = mbuf->pos;
    ASSERT(*p == '$');
    p++;
M
Manju Rajashekhar 已提交
2590

2591
    if (p[0] == '-' && p[1] == '1') {
2592
        len = 1 + 2 + CRLF_LEN;             /* $-1\r\n */
2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603
        p = mbuf->pos + len;
    } else {
        len = 0;
        for (; p < mbuf->last && isdigit(*p); p++) {
            len = len * 10 + (uint32_t)(*p - '0');
        }
        len += CRLF_LEN * 2;
        len += (p - mbuf->pos);
    }
    bytes = len;

I
idning 已提交
2604
    /* copy len bytes to dst */
2605
    for (; mbuf;) {
I
idning 已提交
2606
        if (mbuf_length(mbuf) <= len) {     /* steal this buf from src to dst */
2607 2608
            nbuf = STAILQ_NEXT(mbuf, next);
            mbuf_remove(&src->mhdr, mbuf);
2609 2610
            if (dst != NULL) {
                mbuf_insert(&dst->mhdr, mbuf);
D
deep 已提交
2611 2612
            } else {
                mbuf_put(mbuf);
2613
            }
2614 2615
            len -= mbuf_length(mbuf);
            mbuf = nbuf;
I
idning 已提交
2616
        } else {                             /* split it */
2617
            if (dst != NULL) {
2618 2619 2620
                status = msg_append(dst, mbuf->pos, len);
                if (status != NC_OK) {
                    return status;
2621
                }
2622 2623 2624 2625 2626
            }
            mbuf->pos += len;
            break;
        }
    }
M
Manju Rajashekhar 已提交
2627

2628 2629 2630
    if (dst != NULL) {
        dst->mlen += bytes;
    }
I
idning 已提交
2631 2632
    src->mlen -= bytes;
    log_debug(LOG_VVERB, "redis_copy_bulk copy bytes: %d", bytes);
M
Manju Rajashekhar 已提交
2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653
    return NC_OK;
}

/*
 * Pre-coalesce handler is invoked when the message is a response to
 * the fragmented multi vector request - 'mget' or 'del' and all the
 * responses to the fragmented request vector hasn't been received
 */
void
redis_pre_coalesce(struct msg *r)
{
    struct msg *pr = r->peer; /* peer request */
    struct mbuf *mbuf;

    ASSERT(!r->request);
    ASSERT(pr->request);

    if (pr->frag_id == 0) {
        /* do nothing, if not a response to a fragmented request */
        return;
    }
I
idning 已提交
2654
    pr->frag_owner->nfrag_done++;
M
Manju Rajashekhar 已提交
2655 2656 2657 2658

    switch (r->type) {
    case MSG_RSP_REDIS_INTEGER:
        /* only redis 'del' fragmented request sends back integer reply */
2659
        ASSERT(pr->type == MSG_REQ_REDIS_DEL || pr->type == MSG_REQ_REDIS_TOUCH || pr->type == MSG_REQ_REDIS_UNLINK);
M
Manju Rajashekhar 已提交
2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685

        mbuf = STAILQ_FIRST(&r->mhdr);
        /*
         * Our response parser guarantees that the integer reply will be
         * completely encapsulated in a single mbuf and we should skip over
         * all the mbuf contents and discard it as the parser has already
         * parsed the integer reply and stored it in msg->integer
         */
        ASSERT(mbuf == STAILQ_LAST(&r->mhdr, mbuf, next));
        ASSERT(r->mlen == mbuf_length(mbuf));

        r->mlen -= mbuf_length(mbuf);
        mbuf_rewind(mbuf);

        /* accumulate the integer value in frag_owner of peer request */
        pr->frag_owner->integer += r->integer;
        break;

    case MSG_RSP_REDIS_MULTIBULK:
        /* only redis 'mget' fragmented request sends back multi-bulk reply */
        ASSERT(pr->type == MSG_REQ_REDIS_MGET);

        mbuf = STAILQ_FIRST(&r->mhdr);
        /*
         * Muti-bulk reply can span over multiple mbufs and in each reply
         * we should skip over the narg token. Our response parser
2686
         * guarantees that the narg token and the immediately following
M
Manju Rajashekhar 已提交
2687 2688 2689 2690 2691 2692 2693 2694 2695 2696
         * '\r\n' will exist in a contiguous region in the first mbuf
         */
        ASSERT(r->narg_start == mbuf->pos);
        ASSERT(r->narg_start < r->narg_end);

        r->narg_end += CRLF_LEN;
        r->mlen -= (uint32_t)(r->narg_end - r->narg_start);
        mbuf->pos = r->narg_end;

        break;
I
idning 已提交
2697 2698

    case MSG_RSP_REDIS_STATUS:
I
idning 已提交
2699
        if (pr->type == MSG_REQ_REDIS_MSET) {       /* MSET segments */
I
idning 已提交
2700 2701 2702
            mbuf = STAILQ_FIRST(&r->mhdr);
            r->mlen -= mbuf_length(mbuf);
            mbuf_rewind(mbuf);
M
Manju Rajashekhar 已提交
2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720
        }
        break;

    default:
        /*
         * Valid responses for a fragmented request are MSG_RSP_REDIS_INTEGER or,
         * MSG_RSP_REDIS_MULTIBULK. For an invalid response, we send out -ERR
         * with EINVAL errno
         */
        mbuf = STAILQ_FIRST(&r->mhdr);
        log_hexdump(LOG_ERR, mbuf->pos, mbuf_length(mbuf), "rsp fragment "
                    "with unknown type %d", r->type);
        pr->error = 1;
        pr->err = EINVAL;
        break;
    }
}

2721
static rstatus_t
T
Tyson Andre 已提交
2722
redis_append_key(struct msg *r, const uint8_t *key, uint32_t keylen)
2723 2724 2725 2726
{
    uint32_t len;
    struct mbuf *mbuf;
    uint8_t printbuf[32];
2727
    struct keypos *kpos;
2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742

    /* 1. keylen */
    len = (uint32_t)nc_snprintf(printbuf, sizeof(printbuf), "$%d\r\n", keylen);
    mbuf = msg_ensure_mbuf(r, len);
    if (mbuf == NULL) {
        return NC_ENOMEM;
    }
    mbuf_copy(mbuf, printbuf, len);
    r->mlen += len;

    /* 2. key */
    mbuf = msg_ensure_mbuf(r, keylen);
    if (mbuf == NULL) {
        return NC_ENOMEM;
    }
2743 2744 2745 2746 2747 2748 2749 2750

    kpos = array_push(r->keys);
    if (kpos == NULL) {
        return NC_ENOMEM;
    }

    kpos->start = mbuf->last;
    kpos->end = mbuf->last + keylen;
2751 2752 2753 2754 2755 2756 2757 2758 2759 2760
    mbuf_copy(mbuf, key, keylen);
    r->mlen += keylen;

    /* 3. CRLF */
    mbuf = msg_ensure_mbuf(r, CRLF_LEN);
    if (mbuf == NULL) {
        return NC_ENOMEM;
    }
    mbuf_copy(mbuf, (uint8_t *)CRLF, CRLF_LEN);
    r->mlen += (uint32_t)CRLF_LEN;
2761

2762 2763 2764
    return NC_OK;
}

I
idning 已提交
2765
/*
2766
 * input a msg, return a msg chain.
2767
 * nserver is the number of backend redis/memcache server
2768
 *
2769
 * the original msg will be fragmented into at most nserver fragments.
2770 2771 2772 2773 2774 2775 2776 2777 2778 2779
 * all the keys map to the same backend will group into one fragment.
 *
 * frag_id:
 * a unique fragment id for all fragments of the message vector. including the orig msg.
 *
 * frag_owner:
 * All fragments of the message use frag_owner point to the orig msg
 *
 * frag_seq:
 * the map from each key to it's fragment, (only in the orig msg)
I
idning 已提交
2780
 *
2781
 * For example, a message vector with 3 keys:
I
idning 已提交
2782
 *
2783
 *     get key1 key2 key3
I
idning 已提交
2784
 *
2785
 * suppose we have 2 backend server, and the map is:
I
idning 已提交
2786
 *
2787 2788 2789
 *     key1  => backend 0
 *     key2  => backend 1
 *     key3  => backend 0
I
idning 已提交
2790
 *
2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814
 * it will fragment like this:
 *
 *   +-----------------+
 *   |  msg vector     |
 *   |(original msg)   |
 *   |key1, key2, key3 |
 *   +-----------------+
 *
 *                                             frag_owner
 *                        /--------------------------------------+
 *       frag_owner      /                                       |
 *     /-----------+    | /------------+ frag_owner              |
 *     |           |    | |            |                         |
 *     |           v    v v            |                         |
 *   +--------------------+     +---------------------+     +----+----------------+
 *   |   frag_id = 10     |     |   frag_id = 10      |     |   frag_id = 10      |
 *   |     nfrag = 3      |     |      nfrag = 0      |     |      nfrag = 0      |
 *   | frag_seq = x x x   |     |     key1, key3      |     |         key2        |
 *   +------------|-|-|---+     +---------------------+     +---------------------+
 *                | | |          ^    ^                          ^
 *                | \ \          |    |                          |
 *                |  \ ----------+    |                          |
 *                +---\---------------+                          |
 *                     ------------------------------------------+
I
idning 已提交
2815
 *
2816 2817
 */
static rstatus_t
2818
redis_fragment_argx(struct msg *r, uint32_t nserver, struct msg_tqh *frag_msgq,
I
idning 已提交
2819
                    uint32_t key_step)
2820
{
I
idning 已提交
2821
    struct mbuf *mbuf;
2822 2823
    struct msg **sub_msgs;
    uint32_t i;
I
idning 已提交
2824
    rstatus_t status;
2825
    struct array *keys = r->keys;
2826

2827
    ASSERT(array_n(keys) == (r->narg - 1) / key_step);
2828

2829
    sub_msgs = nc_zalloc(nserver * sizeof(*sub_msgs));
I
idning 已提交
2830 2831 2832 2833
    if (sub_msgs == NULL) {
        return NC_ENOMEM;
    }

I
idning 已提交
2834
    ASSERT(r->frag_seq == NULL);
2835
    r->frag_seq = nc_alloc(array_n(keys) * sizeof(*r->frag_seq));
I
idning 已提交
2836 2837 2838 2839
    if (r->frag_seq == NULL) {
        nc_free(sub_msgs);
        return NC_ENOMEM;
    }
2840 2841 2842 2843

    mbuf = STAILQ_FIRST(&r->mhdr);
    mbuf->pos = mbuf->start;

I
idning 已提交
2844 2845 2846 2847 2848 2849
    /*
     * This code is based on the assumption that '*narg\r\n$4\r\nMGET\r\n' is located
     * in a contiguous location.
     * This is always true because we have capped our MBUF_MIN_SIZE at 512 and
     * whenever we have multiple messages, we copy the tail message into a new mbuf
     */
2850 2851 2852
    for (i = 0; i < 3; i++) {                 /* eat *narg\r\n$4\r\nMGET\r\n */
        for (; *(mbuf->pos) != '\n';) {
            mbuf->pos++;
2853
        }
2854
        mbuf->pos++;
2855 2856
    }

2857 2858 2859 2860
    r->frag_id = msg_gen_frag_id();
    r->nfrag = 0;
    r->frag_owner = r;

2861
    /* Build up the key1 key2 ... to be sent to a given server at index idx */
2862
    for (i = 0; i < array_n(keys); i++) {        /* for each key */
2863
        struct msg *sub_msg;
2864
        struct keypos *kpos = array_get(keys, i);
2865
        uint32_t idx = msg_backend_idx(r, kpos->start, kpos->end - kpos->start);
2866
        ASSERT(idx < nserver);
2867

I
idning 已提交
2868 2869
        if (sub_msgs[idx] == NULL) {
            sub_msgs[idx] = msg_get(r->owner, r->request, r->redis);
I
idning 已提交
2870 2871 2872 2873
            if (sub_msgs[idx] == NULL) {
                nc_free(sub_msgs);
                return NC_ENOMEM;
            }
I
idning 已提交
2874
        }
2875
        r->frag_seq[i] = sub_msg = sub_msgs[idx];
2876 2877

        sub_msg->narg++;
2878
        status = redis_append_key(sub_msg, kpos->start, kpos->end - kpos->start);
I
idning 已提交
2879
        if (status != NC_OK) {
2880
            nc_free(sub_msgs);
I
idning 已提交
2881
            return status;
2882 2883
        }

2884
        if (key_step == 1) {                            /* mget,del */
2885
            continue;
2886 2887 2888 2889 2890 2891 2892
        } else {                                        /* mset */
            status = redis_copy_bulk(NULL, r);          /* eat key */
            if (status != NC_OK) {
                nc_free(sub_msgs);
                return status;
            }

I
idning 已提交
2893 2894 2895 2896 2897 2898
            status = redis_copy_bulk(sub_msg, r);
            if (status != NC_OK) {
                nc_free(sub_msgs);
                return status;
            }

2899
            sub_msg->narg++;
2900 2901
        }
    }
2902

T
Tyson Andre 已提交
2903
    /*
2904 2905 2906 2907
     * prepend mget header, and forward the command (command type+key(s)+suffix)
     * to the corresponding server(s)
     */
    for (i = 0; i < nserver; i++) {
2908
        struct msg *sub_msg = sub_msgs[i];
I
idning 已提交
2909
        if (sub_msg == NULL) {
2910 2911 2912 2913
            continue;
        }

        if (r->type == MSG_REQ_REDIS_MGET) {
I
idning 已提交
2914 2915
            status = msg_prepend_format(sub_msg, "*%d\r\n$4\r\nmget\r\n",
                                        sub_msg->narg + 1);
2916
        } else if (r->type == MSG_REQ_REDIS_DEL) {
I
idning 已提交
2917 2918
            status = msg_prepend_format(sub_msg, "*%d\r\n$3\r\ndel\r\n",
                                        sub_msg->narg + 1);
2919
        } else if (r->type == MSG_REQ_REDIS_MSET) {
I
idning 已提交
2920 2921
            status = msg_prepend_format(sub_msg, "*%d\r\n$4\r\nmset\r\n",
                                        sub_msg->narg + 1);
2922 2923 2924 2925 2926 2927
        } else if (r->type == MSG_REQ_REDIS_TOUCH) {
            status = msg_prepend_format(sub_msg, "*%d\r\n$5\r\ntouch\r\n",
                                        sub_msg->narg + 1);
        } else if (r->type == MSG_REQ_REDIS_UNLINK) {
            status = msg_prepend_format(sub_msg, "*%d\r\n$6\r\nunlink\r\n",
                                        sub_msg->narg + 1);
I
idning 已提交
2928 2929
        } else {
            NOT_REACHED();
I
idning 已提交
2930 2931 2932 2933
        }
        if (status != NC_OK) {
            nc_free(sub_msgs);
            return status;
2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948
        }

        sub_msg->type = r->type;
        sub_msg->frag_id = r->frag_id;
        sub_msg->frag_owner = r->frag_owner;

        TAILQ_INSERT_TAIL(frag_msgq, sub_msg, m_tqe);
        r->nfrag++;
    }

    nc_free(sub_msgs);
    return NC_OK;
}

rstatus_t
2949
redis_fragment(struct msg *r, uint32_t nserver, struct msg_tqh *frag_msgq)
2950
{
R
root 已提交
2951 2952 2953 2954
    if (1 == array_n(r->keys)){
        return NC_OK;
    }

I
idning 已提交
2955 2956 2957
    switch (r->type) {
    case MSG_REQ_REDIS_MGET:
    case MSG_REQ_REDIS_DEL:
2958 2959
    case MSG_REQ_REDIS_TOUCH:
    case MSG_REQ_REDIS_UNLINK:
2960
        return redis_fragment_argx(r, nserver, frag_msgq, 1);
2961

2962
        /* TODO: MSETNX - instead of responding with OK, respond with 1 if all fragments respond with 1 */
I
idning 已提交
2963
    case MSG_REQ_REDIS_MSET:
2964
        return redis_fragment_argx(r, nserver, frag_msgq, 2);
2965

I
idning 已提交
2966 2967
    default:
        return NC_OK;
2968
    }
2969 2970
}

I
idning 已提交
2971 2972 2973
rstatus_t
redis_reply(struct msg *r)
{
C
charsyam 已提交
2974
    struct conn *c_conn;
I
idning 已提交
2975 2976
    struct msg *response = r->peer;

C
charsyam 已提交
2977 2978 2979
    ASSERT(response != NULL && response->owner != NULL);

    c_conn = response->owner;
2980 2981 2982 2983
    if (r->type == MSG_REQ_REDIS_AUTH) {
        return redis_handle_auth_req(r, response);
    }

2984
    if (!conn_authenticated(c_conn)) {
2985
        return msg_append(response, rsp_auth_required.data, rsp_auth_required.len);
C
charsyam 已提交
2986
    }
I
idning 已提交
2987 2988 2989

    switch (r->type) {
    case MSG_REQ_REDIS_PING:
2990
        return msg_append(response, rsp_pong.data, rsp_pong.len);
I
idning 已提交
2991 2992 2993 2994 2995 2996 2997

    default:
        NOT_REACHED();
        return NC_ERROR;
    }
}

I
idning 已提交
2998
void
I
idning 已提交
2999 3000
redis_post_coalesce_mset(struct msg *request)
{
I
idning 已提交
3001
    rstatus_t status;
3002
    struct msg *response = request->peer;
I
idning 已提交
3003

3004
    status = msg_append(response, rsp_ok.data, rsp_ok.len);
I
idning 已提交
3005 3006 3007 3008
    if (status != NC_OK) {
        response->error = 1;        /* mark this msg as err */
        response->err = errno;
    }
I
idning 已提交
3009 3010
}

3011
void
3012
redis_post_coalesce_del_or_touch(struct msg *request)
I
idning 已提交
3013
{
3014
    struct msg *response = request->peer;
I
idning 已提交
3015
    rstatus_t status;
I
idning 已提交
3016

I
idning 已提交
3017 3018 3019 3020 3021
    status = msg_prepend_format(response, ":%d\r\n", request->integer);
    if (status != NC_OK) {
        response->error = 1;
        response->err = errno;
    }
3022 3023
}

I
idning 已提交
3024
static void
I
idning 已提交
3025 3026
redis_post_coalesce_mget(struct msg *request)
{
3027 3028
    struct msg *response = request->peer;
    struct msg *sub_msg;
I
idning 已提交
3029
    rstatus_t status;
I
idning 已提交
3030
    uint32_t i;
3031

I
idning 已提交
3032 3033 3034 3035 3036 3037 3038
    status = msg_prepend_format(response, "*%d\r\n", request->narg - 1);
    if (status != NC_OK) {
        /*
         * the fragments is still in c_conn->omsg_q, we have to discard all of them,
         * we just close the conn here
         */
        response->owner->err = 1;
I
idning 已提交
3039
        return;
I
idning 已提交
3040
    }
3041

3042
    for (i = 0; i < array_n(request->keys); i++) {      /* for each key */
I
idning 已提交
3043
        sub_msg = request->frag_seq[i]->peer;           /* get it's peer response */
I
idning 已提交
3044
        if (sub_msg == NULL) {
I
idning 已提交
3045
            response->owner->err = 1;
I
idning 已提交
3046
            return;
I
idning 已提交
3047 3048 3049 3050
        }
        status = redis_copy_bulk(response, sub_msg);
        if (status != NC_OK) {
            response->owner->err = 1;
I
idning 已提交
3051
            return;
I
idning 已提交
3052
        }
3053 3054 3055
    }
}

M
Manju Rajashekhar 已提交
3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066
/*
 * Post-coalesce handler is invoked when the message is a response to
 * the fragmented multi vector request - 'mget' or 'del' and all the
 * responses to the fragmented request vector has been received and
 * the fragmented request is consider to be done
 */
void
redis_post_coalesce(struct msg *r)
{
    struct msg *pr = r->peer; /* peer response */

3067
    ASSERT(!pr->request);
I
idning 已提交
3068
    ASSERT(r->request && (r->frag_owner == r));
M
Manju Rajashekhar 已提交
3069 3070 3071 3072 3073
    if (r->error || r->ferror) {
        /* do nothing, if msg is in error */
        return;
    }

I
idning 已提交
3074 3075 3076
    switch (r->type) {
    case MSG_REQ_REDIS_MGET:
        return redis_post_coalesce_mget(r);
3077

I
idning 已提交
3078
    case MSG_REQ_REDIS_DEL:
3079 3080 3081
    case MSG_REQ_REDIS_TOUCH:
    case MSG_REQ_REDIS_UNLINK:
        return redis_post_coalesce_del_or_touch(r);
3082

I
idning 已提交
3083 3084
    case MSG_REQ_REDIS_MSET:
        return redis_post_coalesce_mset(r);
3085

M
Manju Rajashekhar 已提交
3086 3087 3088 3089
    default:
        NOT_REACHED();
    }
}
C
charsyam 已提交
3090 3091

static rstatus_t
3092
redis_handle_auth_req(struct msg *req, struct msg *rsp)
C
charsyam 已提交
3093
{
3094
    struct conn *conn = (struct conn *)rsp->owner;
T
Tyson Andre 已提交
3095 3096 3097
    const struct server_pool *pool;
    const struct keypos *kpos;
    const uint8_t *key;
3098 3099
    uint32_t keylen;
    bool valid;
3100

3101
    ASSERT(conn->client && !conn->proxy);
C
charsyam 已提交
3102

T
Tyson Andre 已提交
3103
    pool = (const struct server_pool *)conn->owner;
C
charsyam 已提交
3104

3105 3106 3107 3108 3109 3110
    if (!pool->require_auth) {
        /*
         * AUTH command from the client in absence of a redis_auth:
         * directive should be treated as an error
         */
        return msg_append(rsp, rsp_no_password.data, rsp_no_password.len);
C
charsyam 已提交
3111 3112
    }

3113 3114 3115 3116
    kpos = array_get(req->keys, 0);
    key = kpos->start;
    keylen = (uint32_t)(kpos->end - kpos->start);
    valid = (keylen == pool->redis_auth.len) &&
T
Tyson Andre 已提交
3117
            (memcmp(pool->redis_auth.data, key, keylen) == 0);
3118 3119 3120
    if (valid) {
        conn->authenticated = 1;
        return msg_append(rsp, rsp_ok.data, rsp_ok.len);
C
charsyam 已提交
3121 3122
    }

3123 3124 3125 3126 3127 3128 3129 3130 3131
    /*
     * Password in the AUTH command doesn't match the one configured in
     * redis_auth: directive
     *
     * We mark the connection has unauthenticated until the client
     * reauthenticates with the correct password
     */
    conn->authenticated = 0;
    return msg_append(rsp, rsp_invalid_password.data, rsp_invalid_password.len);
C
charsyam 已提交
3132 3133
}

3134
rstatus_t
3135
redis_add_auth(struct context *ctx, struct conn *c_conn, struct conn *s_conn)
C
charsyam 已提交
3136
{
3137
    rstatus_t status;
C
charsyam 已提交
3138 3139 3140
    struct msg *msg;
    struct server_pool *pool;

3141
    ASSERT(!s_conn->client && !s_conn->proxy);
3142
    ASSERT(!conn_authenticated(s_conn));
C
charsyam 已提交
3143

3144
    pool = c_conn->owner;
C
charsyam 已提交
3145 3146 3147 3148

    msg = msg_get(c_conn, true, c_conn->redis);
    if (msg == NULL) {
        c_conn->err = errno;
3149
        return NC_ENOMEM;
C
charsyam 已提交
3150 3151
    }

3152 3153 3154
    status = msg_prepend_format(msg, "*2\r\n$4\r\nAUTH\r\n$%d\r\n%s\r\n",
                                pool->redis_auth.len, pool->redis_auth.data);
    if (status != NC_OK) {
C
charsyam 已提交
3155
        msg_put(msg);
3156
        return status;
C
charsyam 已提交
3157 3158
    }

3159
    msg->swallow = 1;
C
charsyam 已提交
3160
    s_conn->enqueue_inq(ctx, s_conn, msg);
3161
    s_conn->authenticated = 1;
C
charsyam 已提交
3162

3163
    return NC_OK;
C
charsyam 已提交
3164
}
3165 3166

void
3167
redis_post_connect(struct context *ctx, struct conn *conn, struct server *server)
3168
{
3169 3170 3171 3172
    rstatus_t status;
    struct server_pool *pool = server->owner;
    struct msg *msg;
    int digits;
3173

3174 3175
    ASSERT(!conn->client && conn->connected);
    ASSERT(conn->redis);
3176

3177 3178 3179 3180 3181 3182 3183 3184 3185
    /*
     * By default, every connection to redis uses the database DB 0. You
     * can select a different one on a per-connection basis by sending
     * a request 'SELECT <redis_db>', where <redis_db> is the configured
     * on a per pool basis in the configuration
     */
    if (pool->redis_db <= 0) {
        return;
    }
3186

3187 3188 3189 3190 3191 3192 3193 3194 3195
    /*
     * Create a fake client message and add it to the pipeline. We force this
     * message to be head of queue as it might already contain a command
     * that triggered the connect.
     */
    msg = msg_get(conn, true, conn->redis);
    if (msg == NULL) {
        return;
    }
3196

3197 3198 3199 3200 3201
    digits = (pool->redis_db >= 10) ? (int)log10(pool->redis_db) + 1 : 1;
    status = msg_prepend_format(msg, "*2\r\n$6\r\nSELECT\r\n$%d\r\n%d\r\n", digits, pool->redis_db);
    if (status != NC_OK) {
        msg_put(msg);
        return;
3202
    }
3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213
    msg->type = MSG_REQ_REDIS_SELECT;
    msg->result = MSG_PARSE_OK;
    msg->swallow = 1;
    msg->owner = NULL;

    /* enqueue as head and send */
    req_server_enqueue_imsgq_head(ctx, conn, msg);
    msg_send(ctx, conn);

    log_debug(LOG_NOTICE, "sent 'SELECT %d' to %s | %s", pool->redis_db,
              pool->name.data, server->name.data);
3214
}
A
Arne Claus 已提交
3215 3216 3217 3218 3219

void
redis_swallow_msg(struct conn *conn, struct msg *pmsg, struct msg *msg)
{
    if (pmsg != NULL && pmsg->type == MSG_REQ_REDIS_SELECT &&
3220
        msg != NULL && redis_error(msg)) {
A
Arne Claus 已提交
3221 3222 3223 3224 3225 3226 3227
        struct server* conn_server;
        struct server_pool* conn_pool;
        struct mbuf* rsp_buffer;
        uint8_t message[128];
        size_t copy_len;

        /*
C
charsyam 已提交
3228
         * Get a substring from the message so that the initial - and the trailing
A
Arne Claus 已提交
3229 3230 3231
         * \r\n is removed.
         */
        conn_server = (struct server*)conn->owner;
M
Manju Rajashekhar 已提交
3232 3233 3234
        conn_pool = conn_server->owner;
        rsp_buffer = STAILQ_LAST(&msg->mhdr, mbuf, next);
        copy_len = MIN(mbuf_length(rsp_buffer) - 3, sizeof(message) - 1);
A
Arne Claus 已提交
3235 3236 3237 3238 3239

        nc_memcpy(message, &rsp_buffer->start[1], copy_len);
        message[copy_len] = 0;

        log_warn("SELECT %d failed on %s | %s: %s",
M
Manju Rajashekhar 已提交
3240 3241
                 conn_pool->redis_db, conn_pool->name.data,
                 conn_server->name.data, message);
A
Arne Claus 已提交
3242 3243
    }
}