server.c 187.7 KB
Newer Older
1
/*
A
antirez 已提交
2
 * Copyright (c) 2009-2016, Salvatore Sanfilippo <antirez at gmail dot com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of Redis nor the names of its contributors may be used
 *     to endorse or promote products derived from this software without
 *     specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

30
#include "server.h"
31
#include "cluster.h"
32
#include "slowlog.h"
33
#include "bio.h"
34
#include "latency.h"
35
#include "atomicvar.h"
36 37 38 39 40 41 42 43 44 45 46 47 48 49

#include <time.h>
#include <signal.h>
#include <sys/wait.h>
#include <errno.h>
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/uio.h>
50
#include <sys/un.h>
51 52 53
#include <limits.h>
#include <float.h>
#include <math.h>
54
#include <sys/resource.h>
55
#include <sys/utsname.h>
56
#include <locale.h>
C
clark.kang 已提交
57
#include <sys/socket.h>
58 59 60 61 62

/* Our shared "common" objects */

struct sharedObjectsStruct shared;

63
/* Global vars that are actually used as constants. The following double
64 65 66 67 68 69 70 71
 * values are used for double on-disk serialization, and are initialized
 * at runtime to avoid strange compiler optimizations. */

double R_Zero, R_PosInf, R_NegInf, R_Nan;

/*================================= Globals ================================= */

/* Global vars */
72 73
struct redisServer server; /* Server global state */
volatile unsigned long lru_clock; /* Server global current LRU time. */
74

75 76 77 78
/* Our command table.
 *
 * Every entry is composed of the following fields:
 *
79 80 81 82 83 84 85 86 87 88 89
 * name:        A string representing the command name.
 *
 * function:    Pointer to the C function implementing the command.
 *
 * arity:       Number of arguments, it is possible to use -N to say >= N
 *
 * sflags:      Command flags as string. See below for a table of flags.
 *
 * flags:       Flags as bitmask. Computed by Redis using the 'sflags' field.
 *
 * get_keys_proc: An optional function to get key arguments from a command.
90 91
 *                This is only used when the following three fields are not
 *                enough to specify what arguments are keys.
92 93 94 95 96 97 98 99 100 101 102 103 104 105
 *
 * first_key_index: First argument that is a key
 *
 * last_key_index: Last argument that is a key
 *
 * key_step:    Step to get all the keys from first to last argument.
 *              For instance in MSET the step is two since arguments
 *              are key,val,key,val,...
 *
 * microseconds: Microseconds of total execution time for this command.
 *
 * calls:       Total number of calls of this command.
 *
 * id:          Command bit identifier for ACLs or other goals.
106 107 108 109
 *
 * The flags, microseconds and calls fields are computed by Redis and should
 * always be set to zero.
 *
110 111
 * Command flags are expressed using space separated strings, that are turned
 * into actual flags by the populateCommandTable() function.
112 113 114
 *
 * This is the meaning of the flags:
 *
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
 * write:       Write command (may modify the key space).
 *
 * read-only:   All the non special commands just reading from keys without
 *              changing the content, or returning other informations like
 *              the TIME command. Special commands such administrative commands
 *              or transaction related commands (multi, exec, discard, ...)
 *              are not flagged as read-only commands, since they affect the
 *              server or the connection in other ways.
 *
 * use-memory:  May increase memory usage once called. Don't allow if out
 *              of memory.
 *
 * admin:       Administrative command, like SAVE or SHUTDOWN.
 *
 * pub-sub:     Pub/Sub related command.
 *
 * no-script:   Command not allowed in scripts.
 *
 * random:      Random command. Command is not deterministic, that is, the same
 *              command with the same arguments, with the same key space, may
 *              have different results. For instance SPOP and RANDOMKEY are
 *              two random commands.
 *
 * to-sort:     Sort command output array if called from script, so that the
 *              output is deterministic. When this flag is used (not always
 *              possible), then the "random" flag is not needed.
 *
 * ok-loading:  Allow the command while loading the database.
 *
 * ok-stale:    Allow the command while a slave has stale data but is not
 *              allowed to serve this data. Normally no command is accepted
 *              in this condition but just a few.
 *
 * no-monitor:  Do not automatically propagate the command on MONITOR.
A
antirez 已提交
149
 *
150
 * no-slowlog:  Do not automatically propagate the command to the slowlog.
151 152 153 154 155 156 157 158 159
 *
 * cluster-asking: Perform an implicit ASKING for this command, so the
 *              command will be accepted in cluster mode if the slot is marked
 *              as 'importing'.
 *
 * fast:        Fast command: O(1) or O(log(N)) command that should never
 *              delay its execution as long as the kernel scheduler is giving
 *              us time. Note that commands that may trigger a DEL as a side
 *              effect (like SET) are not fast commands.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
 *
 * The following additional flags are only used in order to put commands
 * in a specific ACL category. Commands can have multiple ACL categories.
 *
 * @keyspace, @read, @write, @set, @sortedset, @list, @hash, @string, @bitmap,
 * @hyperloglog, @stream, @admin, @fast, @slow, @pubsub, @blocking, @dangerous,
 * @connection, @transaction, @scripting, @geo.
 *
 * Note that:
 *
 * 1) The read-only flag implies the @read ACL category.
 * 2) The write flag implies the @write ACL category.
 * 3) The fast flag implies the @fast ACL category.
 * 4) The admin flag implies the @admin and @dangerous ACL category.
 * 5) The pub-sub flag implies the @pubsub ACL category.
 * 6) The lack of fast flag implies the @slow ACL category.
 * 7) The non obvious "keyspace" category includes the commands
 *    that interact with keys without having anything to do with
 *    specific data structures, such as: DEL, RENAME, MOVE, SELECT,
 *    TYPE, EXPIRE*, PEXPIRE*, TTL, PTTL, ...
180
 */
181

182
struct redisCommand redisCommandTable[] = {
183 184 185 186 187
    {"module",moduleCommand,-2,
     "admin no-script",
     0,NULL,0,0,0,0,0,0},

    {"get",getCommand,2,
188
     "read-only fast @string",
189 190 191 192 193
     0,NULL,1,1,1,0,0,0},

    /* Note that we can't flag set as fast, since it may perform an
     * implicit DEL of a large key. */
    {"set",setCommand,-3,
194
     "write use-memory @string",
195 196 197
     0,NULL,1,1,1,0,0,0},

    {"setnx",setnxCommand,3,
198
     "write use-memory fast @string",
199 200 201
     0,NULL,1,1,1,0,0,0},

    {"setex",setexCommand,4,
202
     "write use-memory @string",
203 204 205
     0,NULL,1,1,1,0,0,0},

    {"psetex",psetexCommand,4,
206
     "write use-memory @string",
207 208 209
     0,NULL,1,1,1,0,0,0},

    {"append",appendCommand,3,
210
     "write use-memory fast @string",
211 212 213
     0,NULL,1,1,1,0,0,0},

    {"strlen",strlenCommand,2,
214
     "read-only fast @string",
215 216
     0,NULL,1,1,1,0,0,0},

217 218
    {"del",delCommand,-2,
     "write @keyspace",
219 220 221
     0,NULL,1,-1,1,0,0,0},

    {"unlink",unlinkCommand,-2,
222
     "write fast @keyspace",
223 224 225
     0,NULL,1,-1,1,0,0,0},

    {"exists",existsCommand,-2,
226
     "read-only fast @keyspace",
227 228 229
     0,NULL,1,-1,1,0,0,0},

    {"setbit",setbitCommand,4,
230
     "write use-memory @bitmap",
231 232 233
     0,NULL,1,1,1,0,0,0},

    {"getbit",getbitCommand,3,
234
     "read-only fast @bitmap",
235 236 237
     0,NULL,1,1,1,0,0,0},

    {"bitfield",bitfieldCommand,-2,
238
     "write use-memory @bitmap",
239 240
     0,NULL,1,1,1,0,0,0},

241 242 243 244
    {"bitfield_ro",bitfieldroCommand,-2,
     "read-only fast @bitmap",
     0,NULL,1,1,1,0,0,0},

245
    {"setrange",setrangeCommand,4,
246
     "write use-memory @string",
247 248 249
     0,NULL,1,1,1,0,0,0},

    {"getrange",getrangeCommand,4,
250
     "read-only @string",
251 252 253
     0,NULL,1,1,1,0,0,0},

    {"substr",getrangeCommand,4,
254
     "read-only @string",
255 256 257
     0,NULL,1,1,1,0,0,0},

    {"incr",incrCommand,2,
258
     "write use-memory fast @string",
259 260 261
     0,NULL,1,1,1,0,0,0},

    {"decr",decrCommand,2,
262
     "write use-memory fast @string",
263 264 265
     0,NULL,1,1,1,0,0,0},

    {"mget",mgetCommand,-2,
266
     "read-only fast @string",
267 268 269
     0,NULL,1,-1,1,0,0,0},

    {"rpush",rpushCommand,-3,
270
     "write use-memory fast @list",
271 272 273
     0,NULL,1,1,1,0,0,0},

    {"lpush",lpushCommand,-3,
274
     "write use-memory fast @list",
275 276 277
     0,NULL,1,1,1,0,0,0},

    {"rpushx",rpushxCommand,-3,
278
     "write use-memory fast @list",
279 280 281
     0,NULL,1,1,1,0,0,0},

    {"lpushx",lpushxCommand,-3,
282
     "write use-memory fast @list",
283 284 285
     0,NULL,1,1,1,0,0,0},

    {"linsert",linsertCommand,5,
286
     "write use-memory @list",
287 288 289
     0,NULL,1,1,1,0,0,0},

    {"rpop",rpopCommand,2,
290
     "write fast @list",
291 292 293
     0,NULL,1,1,1,0,0,0},

    {"lpop",lpopCommand,2,
294
     "write fast @list",
295 296 297
     0,NULL,1,1,1,0,0,0},

    {"brpop",brpopCommand,-3,
298
     "write no-script @list @blocking",
299 300 301
     0,NULL,1,-2,1,0,0,0},

    {"brpoplpush",brpoplpushCommand,4,
302
     "write use-memory no-script @list @blocking",
303 304 305
     0,NULL,1,2,1,0,0,0},

    {"blpop",blpopCommand,-3,
306
     "write no-script @list @blocking",
307 308 309
     0,NULL,1,-2,1,0,0,0},

    {"llen",llenCommand,2,
310
     "read-only fast @list",
311 312 313
     0,NULL,1,1,1,0,0,0},

    {"lindex",lindexCommand,3,
314
     "read-only @list",
315 316 317
     0,NULL,1,1,1,0,0,0},

    {"lset",lsetCommand,4,
318
     "write use-memory @list",
319 320 321
     0,NULL,1,1,1,0,0,0},

    {"lrange",lrangeCommand,4,
322
     "read-only @list",
323 324 325
     0,NULL,1,1,1,0,0,0},

    {"ltrim",ltrimCommand,4,
326
     "write @list",
327 328
     0,NULL,1,1,1,0,0,0},

A
antirez 已提交
329 330
    {"lpos",lposCommand,-3,
     "read-only @list",
331 332
     0,NULL,1,1,1,0,0,0},

333
    {"lrem",lremCommand,4,
334
     "write @list",
335 336 337
     0,NULL,1,1,1,0,0,0},

    {"rpoplpush",rpoplpushCommand,3,
338
     "write use-memory @list",
339 340 341
     0,NULL,1,2,1,0,0,0},

    {"sadd",saddCommand,-3,
342
     "write use-memory fast @set",
343 344 345
     0,NULL,1,1,1,0,0,0},

    {"srem",sremCommand,-3,
346
     "write fast @set",
347 348 349
     0,NULL,1,1,1,0,0,0},

    {"smove",smoveCommand,4,
350
     "write fast @set",
351 352 353
     0,NULL,1,2,1,0,0,0},

    {"sismember",sismemberCommand,3,
354
     "read-only fast @set",
355 356 357
     0,NULL,1,1,1,0,0,0},

    {"scard",scardCommand,2,
358
     "read-only fast @set",
359 360 361
     0,NULL,1,1,1,0,0,0},

    {"spop",spopCommand,-2,
362
     "write random fast @set",
363 364 365
     0,NULL,1,1,1,0,0,0},

    {"srandmember",srandmemberCommand,-2,
366
     "read-only random @set",
367 368 369
     0,NULL,1,1,1,0,0,0},

    {"sinter",sinterCommand,-2,
370
     "read-only to-sort @set",
371 372 373
     0,NULL,1,-1,1,0,0,0},

    {"sinterstore",sinterstoreCommand,-3,
374
     "write use-memory @set",
375 376 377
     0,NULL,1,-1,1,0,0,0},

    {"sunion",sunionCommand,-2,
378
     "read-only to-sort @set",
379 380 381
     0,NULL,1,-1,1,0,0,0},

    {"sunionstore",sunionstoreCommand,-3,
382
     "write use-memory @set",
383 384 385
     0,NULL,1,-1,1,0,0,0},

    {"sdiff",sdiffCommand,-2,
386
     "read-only to-sort @set",
387 388 389
     0,NULL,1,-1,1,0,0,0},

    {"sdiffstore",sdiffstoreCommand,-3,
390
     "write use-memory @set",
391 392 393
     0,NULL,1,-1,1,0,0,0},

    {"smembers",sinterCommand,2,
394
     "read-only to-sort @set",
395 396 397
     0,NULL,1,1,1,0,0,0},

    {"sscan",sscanCommand,-3,
398
     "read-only random @set",
399 400 401
     0,NULL,1,1,1,0,0,0},

    {"zadd",zaddCommand,-4,
402
     "write use-memory fast @sortedset",
403 404 405
     0,NULL,1,1,1,0,0,0},

    {"zincrby",zincrbyCommand,4,
406
     "write use-memory fast @sortedset",
407 408 409
     0,NULL,1,1,1,0,0,0},

    {"zrem",zremCommand,-3,
410
     "write fast @sortedset",
411 412 413
     0,NULL,1,1,1,0,0,0},

    {"zremrangebyscore",zremrangebyscoreCommand,4,
414
     "write @sortedset",
415 416 417
     0,NULL,1,1,1,0,0,0},

    {"zremrangebyrank",zremrangebyrankCommand,4,
418
     "write @sortedset",
419 420 421
     0,NULL,1,1,1,0,0,0},

    {"zremrangebylex",zremrangebylexCommand,4,
422
     "write @sortedset",
423 424 425
     0,NULL,1,1,1,0,0,0},

    {"zunionstore",zunionstoreCommand,-4,
426
     "write use-memory @sortedset",
427 428 429
     0,zunionInterGetKeys,0,0,0,0,0,0},

    {"zinterstore",zinterstoreCommand,-4,
430
     "write use-memory @sortedset",
431 432 433
     0,zunionInterGetKeys,0,0,0,0,0,0},

    {"zrange",zrangeCommand,-4,
434
     "read-only @sortedset",
435 436 437
     0,NULL,1,1,1,0,0,0},

    {"zrangebyscore",zrangebyscoreCommand,-4,
438
     "read-only @sortedset",
439 440 441
     0,NULL,1,1,1,0,0,0},

    {"zrevrangebyscore",zrevrangebyscoreCommand,-4,
442
     "read-only @sortedset",
443 444 445
     0,NULL,1,1,1,0,0,0},

    {"zrangebylex",zrangebylexCommand,-4,
446
     "read-only @sortedset",
447 448 449
     0,NULL,1,1,1,0,0,0},

    {"zrevrangebylex",zrevrangebylexCommand,-4,
450
     "read-only @sortedset",
451 452 453
     0,NULL,1,1,1,0,0,0},

    {"zcount",zcountCommand,4,
454
     "read-only fast @sortedset",
455 456 457
     0,NULL,1,1,1,0,0,0},

    {"zlexcount",zlexcountCommand,4,
458
     "read-only fast @sortedset",
459 460 461
     0,NULL,1,1,1,0,0,0},

    {"zrevrange",zrevrangeCommand,-4,
462
     "read-only @sortedset",
463 464 465
     0,NULL,1,1,1,0,0,0},

    {"zcard",zcardCommand,2,
466
     "read-only fast @sortedset",
467 468 469
     0,NULL,1,1,1,0,0,0},

    {"zscore",zscoreCommand,3,
470
     "read-only fast @sortedset",
471 472 473
     0,NULL,1,1,1,0,0,0},

    {"zrank",zrankCommand,3,
474
     "read-only fast @sortedset",
475 476 477
     0,NULL,1,1,1,0,0,0},

    {"zrevrank",zrevrankCommand,3,
478
     "read-only fast @sortedset",
479 480 481
     0,NULL,1,1,1,0,0,0},

    {"zscan",zscanCommand,-3,
482
     "read-only random @sortedset",
483 484 485
     0,NULL,1,1,1,0,0,0},

    {"zpopmin",zpopminCommand,-2,
486
     "write fast @sortedset",
487 488 489
     0,NULL,1,1,1,0,0,0},

    {"zpopmax",zpopmaxCommand,-2,
490
     "write fast @sortedset",
491 492
     0,NULL,1,1,1,0,0,0},

I
Itamar Haber 已提交
493
    {"bzpopmin",bzpopminCommand,-3,
494
     "write no-script fast @sortedset @blocking",
495 496
     0,NULL,1,-2,1,0,0,0},

I
Itamar Haber 已提交
497
    {"bzpopmax",bzpopmaxCommand,-3,
498
     "write no-script fast @sortedset @blocking",
499 500 501
     0,NULL,1,-2,1,0,0,0},

    {"hset",hsetCommand,-4,
502
     "write use-memory fast @hash",
503 504 505
     0,NULL,1,1,1,0,0,0},

    {"hsetnx",hsetnxCommand,4,
506
     "write use-memory fast @hash",
507 508 509
     0,NULL,1,1,1,0,0,0},

    {"hget",hgetCommand,3,
510
     "read-only fast @hash",
511 512 513
     0,NULL,1,1,1,0,0,0},

    {"hmset",hsetCommand,-4,
514
     "write use-memory fast @hash",
515 516 517
     0,NULL,1,1,1,0,0,0},

    {"hmget",hmgetCommand,-3,
518
     "read-only fast @hash",
519 520 521
     0,NULL,1,1,1,0,0,0},

    {"hincrby",hincrbyCommand,4,
522
     "write use-memory fast @hash",
523 524 525
     0,NULL,1,1,1,0,0,0},

    {"hincrbyfloat",hincrbyfloatCommand,4,
526
     "write use-memory fast @hash",
527 528 529
     0,NULL,1,1,1,0,0,0},

    {"hdel",hdelCommand,-3,
530
     "write fast @hash",
531 532 533
     0,NULL,1,1,1,0,0,0},

    {"hlen",hlenCommand,2,
534
     "read-only fast @hash",
535 536 537
     0,NULL,1,1,1,0,0,0},

    {"hstrlen",hstrlenCommand,3,
538
     "read-only fast @hash",
539 540 541
     0,NULL,1,1,1,0,0,0},

    {"hkeys",hkeysCommand,2,
542
     "read-only to-sort @hash",
543 544 545
     0,NULL,1,1,1,0,0,0},

    {"hvals",hvalsCommand,2,
546
     "read-only to-sort @hash",
547 548 549
     0,NULL,1,1,1,0,0,0},

    {"hgetall",hgetallCommand,2,
550
     "read-only random @hash",
551 552 553
     0,NULL,1,1,1,0,0,0},

    {"hexists",hexistsCommand,3,
554
     "read-only fast @hash",
555 556 557
     0,NULL,1,1,1,0,0,0},

    {"hscan",hscanCommand,-3,
558
     "read-only random @hash",
559 560 561
     0,NULL,1,1,1,0,0,0},

    {"incrby",incrbyCommand,3,
562
     "write use-memory fast @string",
563 564 565
     0,NULL,1,1,1,0,0,0},

    {"decrby",decrbyCommand,3,
566
     "write use-memory fast @string",
567 568 569
     0,NULL,1,1,1,0,0,0},

    {"incrbyfloat",incrbyfloatCommand,3,
570
     "write use-memory fast @string",
571 572 573
     0,NULL,1,1,1,0,0,0},

    {"getset",getsetCommand,3,
574
     "write use-memory fast @string",
575 576 577
     0,NULL,1,1,1,0,0,0},

    {"mset",msetCommand,-3,
578
     "write use-memory @string",
579 580 581
     0,NULL,1,-1,2,0,0,0},

    {"msetnx",msetnxCommand,-3,
582
     "write use-memory @string",
583 584 585
     0,NULL,1,-1,2,0,0,0},

    {"randomkey",randomkeyCommand,1,
586
     "read-only random @keyspace",
587 588 589
     0,NULL,0,0,0,0,0,0},

    {"select",selectCommand,2,
590
     "ok-loading fast ok-stale @keyspace",
591 592 593
     0,NULL,0,0,0,0,0,0},

    {"swapdb",swapdbCommand,3,
594
     "write fast @keyspace @dangerous",
595 596 597
     0,NULL,0,0,0,0,0,0},

    {"move",moveCommand,3,
598
     "write fast @keyspace",
599 600 601 602 603
     0,NULL,1,1,1,0,0,0},

    /* Like for SET, we can't mark rename as a fast command because
     * overwriting the target key may result in an implicit slow DEL. */
    {"rename",renameCommand,3,
604
     "write @keyspace",
605 606 607
     0,NULL,1,2,1,0,0,0},

    {"renamenx",renamenxCommand,3,
608
     "write fast @keyspace",
609 610 611
     0,NULL,1,2,1,0,0,0},

    {"expire",expireCommand,3,
612
     "write fast @keyspace",
613 614 615
     0,NULL,1,1,1,0,0,0},

    {"expireat",expireatCommand,3,
616
     "write fast @keyspace",
617 618 619
     0,NULL,1,1,1,0,0,0},

    {"pexpire",pexpireCommand,3,
620
     "write fast @keyspace",
621 622 623
     0,NULL,1,1,1,0,0,0},

    {"pexpireat",pexpireatCommand,3,
624
     "write fast @keyspace",
625 626 627
     0,NULL,1,1,1,0,0,0},

    {"keys",keysCommand,2,
628
     "read-only to-sort @keyspace @dangerous",
629 630 631
     0,NULL,0,0,0,0,0,0},

    {"scan",scanCommand,-2,
632
     "read-only random @keyspace",
633 634 635
     0,NULL,0,0,0,0,0,0},

    {"dbsize",dbsizeCommand,1,
636
     "read-only fast @keyspace",
637 638 639
     0,NULL,0,0,0,0,0,0},

    {"auth",authCommand,-2,
640
     "no-auth no-script ok-loading ok-stale fast no-monitor no-slowlog @connection",
641 642 643 644 645 646
     0,NULL,0,0,0,0,0,0},

    /* We don't allow PING during loading since in Redis PING is used as
     * failure detection, and a loading server is considered to be
     * not available. */
    {"ping",pingCommand,-1,
647
     "ok-stale fast @connection",
648 649 650
     0,NULL,0,0,0,0,0,0},

    {"echo",echoCommand,2,
651
     "read-only fast @connection",
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
     0,NULL,0,0,0,0,0,0},

    {"save",saveCommand,1,
     "admin no-script",
     0,NULL,0,0,0,0,0,0},

    {"bgsave",bgsaveCommand,-1,
     "admin no-script",
     0,NULL,0,0,0,0,0,0},

    {"bgrewriteaof",bgrewriteaofCommand,1,
     "admin no-script",
     0,NULL,0,0,0,0,0,0},

    {"shutdown",shutdownCommand,-1,
     "admin no-script ok-loading ok-stale",
     0,NULL,0,0,0,0,0,0},

    {"lastsave",lastsaveCommand,1,
671
     "read-only random fast ok-loading ok-stale @admin @dangerous",
672 673 674
     0,NULL,0,0,0,0,0,0},

    {"type",typeCommand,2,
675
     "read-only fast @keyspace",
676 677 678
     0,NULL,1,1,1,0,0,0},

    {"multi",multiCommand,1,
679
     "no-script fast ok-loading ok-stale @transaction",
680 681 682
     0,NULL,0,0,0,0,0,0},

    {"exec",execCommand,1,
683
     "no-script no-monitor no-slowlog ok-loading ok-stale @transaction",
684 685 686
     0,NULL,0,0,0,0,0,0},

    {"discard",discardCommand,1,
687
     "no-script fast ok-loading ok-stale @transaction",
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
     0,NULL,0,0,0,0,0,0},

    {"sync",syncCommand,1,
     "admin no-script",
     0,NULL,0,0,0,0,0,0},

    {"psync",syncCommand,3,
     "admin no-script",
     0,NULL,0,0,0,0,0,0},

    {"replconf",replconfCommand,-1,
     "admin no-script ok-loading ok-stale",
     0,NULL,0,0,0,0,0,0},

    {"flushdb",flushdbCommand,-1,
703
     "write @keyspace @dangerous",
704 705 706
     0,NULL,0,0,0,0,0,0},

    {"flushall",flushallCommand,-1,
707
     "write @keyspace @dangerous",
708 709 710
     0,NULL,0,0,0,0,0,0},

    {"sort",sortCommand,-2,
711
     "write use-memory @list @set @sortedset @dangerous",
712 713 714
     0,sortGetKeys,1,1,1,0,0,0},

    {"info",infoCommand,-1,
715
     "ok-loading ok-stale random @dangerous",
716 717 718
     0,NULL,0,0,0,0,0,0},

    {"monitor",monitorCommand,1,
719
     "admin no-script ok-loading ok-stale",
720 721 722
     0,NULL,0,0,0,0,0,0},

    {"ttl",ttlCommand,2,
723
     "read-only fast random @keyspace",
724 725 726
     0,NULL,1,1,1,0,0,0},

    {"touch",touchCommand,-2,
727
     "read-only fast @keyspace",
728
     0,NULL,1,-1,1,0,0,0},
729 730

    {"pttl",pttlCommand,2,
731
     "read-only fast random @keyspace",
732 733 734
     0,NULL,1,1,1,0,0,0},

    {"persist",persistCommand,2,
735
     "write fast @keyspace",
736 737 738 739 740 741 742 743 744 745 746
     0,NULL,1,1,1,0,0,0},

    {"slaveof",replicaofCommand,3,
     "admin no-script ok-stale",
     0,NULL,0,0,0,0,0,0},

    {"replicaof",replicaofCommand,3,
     "admin no-script ok-stale",
     0,NULL,0,0,0,0,0,0},

    {"role",roleCommand,1,
747
     "ok-loading ok-stale no-script fast read-only @dangerous",
748 749 750
     0,NULL,0,0,0,0,0,0},

    {"debug",debugCommand,-2,
751
     "admin no-script ok-loading ok-stale",
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
     0,NULL,0,0,0,0,0,0},

    {"config",configCommand,-2,
     "admin ok-loading ok-stale no-script",
     0,NULL,0,0,0,0,0,0},

    {"subscribe",subscribeCommand,-2,
     "pub-sub no-script ok-loading ok-stale",
     0,NULL,0,0,0,0,0,0},

    {"unsubscribe",unsubscribeCommand,-1,
     "pub-sub no-script ok-loading ok-stale",
     0,NULL,0,0,0,0,0,0},

    {"psubscribe",psubscribeCommand,-2,
     "pub-sub no-script ok-loading ok-stale",
     0,NULL,0,0,0,0,0,0},

    {"punsubscribe",punsubscribeCommand,-1,
     "pub-sub no-script ok-loading ok-stale",
     0,NULL,0,0,0,0,0,0},

    {"publish",publishCommand,3,
     "pub-sub ok-loading ok-stale fast",
     0,NULL,0,0,0,0,0,0},

    {"pubsub",pubsubCommand,-2,
     "pub-sub ok-loading ok-stale random",
     0,NULL,0,0,0,0,0,0},

    {"watch",watchCommand,-2,
783
     "no-script fast ok-loading ok-stale @transaction",
784 785 786
     0,NULL,1,-1,1,0,0,0},

    {"unwatch",unwatchCommand,1,
787
     "no-script fast ok-loading ok-stale @transaction",
788 789 790 791 792 793 794
     0,NULL,0,0,0,0,0,0},

    {"cluster",clusterCommand,-2,
     "admin ok-stale random",
     0,NULL,0,0,0,0,0,0},

    {"restore",restoreCommand,-4,
795
     "write use-memory @keyspace @dangerous",
796 797 798
     0,NULL,1,1,1,0,0,0},

    {"restore-asking",restoreCommand,-4,
799
    "write use-memory cluster-asking @keyspace @dangerous",
800 801 802
    0,NULL,1,1,1,0,0,0},

    {"migrate",migrateCommand,-6,
803
     "write random @keyspace @dangerous",
804 805 806
     0,migrateGetKeys,0,0,0,0,0,0},

    {"asking",askingCommand,1,
807
     "fast @keyspace",
808 809 810
     0,NULL,0,0,0,0,0,0},

    {"readonly",readonlyCommand,1,
811
     "fast @keyspace",
812 813 814
     0,NULL,0,0,0,0,0,0},

    {"readwrite",readwriteCommand,1,
815
     "fast @keyspace",
816 817 818
     0,NULL,0,0,0,0,0,0},

    {"dump",dumpCommand,2,
819
     "read-only random @keyspace",
820 821 822
     0,NULL,1,1,1,0,0,0},

    {"object",objectCommand,-2,
823
     "read-only random @keyspace",
824 825 826
     0,NULL,2,2,1,0,0,0},

    {"memory",memoryCommand,-2,
827
     "random read-only",
828
     0,memoryGetKeys,0,0,0,0,0,0},
829 830

    {"client",clientCommand,-2,
831
     "admin no-script random ok-loading ok-stale @connection",
832 833 834
     0,NULL,0,0,0,0,0,0},

    {"hello",helloCommand,-2,
835
     "no-auth no-script fast no-monitor ok-loading ok-stale no-slowlog @connection",
836 837 838 839 840
     0,NULL,0,0,0,0,0,0},

    /* EVAL can modify the dataset, however it is not flagged as a write
     * command since we do the check while running commands from Lua. */
    {"eval",evalCommand,-3,
841
     "no-script @scripting",
842 843 844
     0,evalGetKeys,0,0,0,0,0,0},

    {"evalsha",evalShaCommand,-3,
845
     "no-script @scripting",
846 847 848
     0,evalGetKeys,0,0,0,0,0,0},

    {"slowlog",slowlogCommand,-2,
849
     "admin random ok-loading ok-stale",
850 851 852
     0,NULL,0,0,0,0,0,0},

    {"script",scriptCommand,-2,
853
     "no-script @scripting",
854 855 856
     0,NULL,0,0,0,0,0,0},

    {"time",timeCommand,1,
857
     "read-only random fast ok-loading ok-stale",
858 859 860
     0,NULL,0,0,0,0,0,0},

    {"bitop",bitopCommand,-4,
861
     "write use-memory @bitmap",
862 863 864
     0,NULL,2,-1,1,0,0,0},

    {"bitcount",bitcountCommand,-2,
865
     "read-only @bitmap",
866 867 868
     0,NULL,1,1,1,0,0,0},

    {"bitpos",bitposCommand,-3,
869
     "read-only @bitmap",
870 871 872
     0,NULL,1,1,1,0,0,0},

    {"wait",waitCommand,3,
873
     "no-script @keyspace",
874 875
     0,NULL,0,0,0,0,0,0},

876
    {"command",commandCommand,-1,
877
     "ok-loading ok-stale random @connection",
878 879 880
     0,NULL,0,0,0,0,0,0},

    {"geoadd",geoaddCommand,-5,
881
     "write use-memory @geo",
882 883 884 885
     0,NULL,1,1,1,0,0,0},

    /* GEORADIUS has store options that may write. */
    {"georadius",georadiusCommand,-6,
886
     "write @geo",
887 888 889
     0,georadiusGetKeys,1,1,1,0,0,0},

    {"georadius_ro",georadiusroCommand,-6,
890
     "read-only @geo",
891 892 893
     0,georadiusGetKeys,1,1,1,0,0,0},

    {"georadiusbymember",georadiusbymemberCommand,-5,
894
     "write @geo",
895 896 897
     0,georadiusGetKeys,1,1,1,0,0,0},

    {"georadiusbymember_ro",georadiusbymemberroCommand,-5,
898
     "read-only @geo",
899 900 901
     0,georadiusGetKeys,1,1,1,0,0,0},

    {"geohash",geohashCommand,-2,
902
     "read-only @geo",
903 904 905
     0,NULL,1,1,1,0,0,0},

    {"geopos",geoposCommand,-2,
906
     "read-only @geo",
907 908 909
     0,NULL,1,1,1,0,0,0},

    {"geodist",geodistCommand,-4,
910
     "read-only @geo",
911 912 913
     0,NULL,1,1,1,0,0,0},

    {"pfselftest",pfselftestCommand,1,
914
     "admin @hyperloglog",
915 916 917
      0,NULL,0,0,0,0,0,0},

    {"pfadd",pfaddCommand,-2,
918
     "write use-memory fast @hyperloglog",
919 920 921 922 923 924 925
     0,NULL,1,1,1,0,0,0},

    /* Technically speaking PFCOUNT may change the key since it changes the
     * final bytes in the HyperLogLog representation. However in this case
     * we claim that the representation, even if accessible, is an internal
     * affair, and the command is semantically read only. */
    {"pfcount",pfcountCommand,-2,
926
     "read-only @hyperloglog",
927 928 929
     0,NULL,1,-1,1,0,0,0},

    {"pfmerge",pfmergeCommand,-2,
930
     "write use-memory @hyperloglog",
931 932 933 934 935 936 937
     0,NULL,1,-1,1,0,0,0},

    {"pfdebug",pfdebugCommand,-3,
     "admin write",
     0,NULL,0,0,0,0,0,0},

    {"xadd",xaddCommand,-5,
938
     "write use-memory fast random @stream",
939 940 941
     0,NULL,1,1,1,0,0,0},

    {"xrange",xrangeCommand,-4,
942
     "read-only @stream",
943 944 945
     0,NULL,1,1,1,0,0,0},

    {"xrevrange",xrevrangeCommand,-4,
946
     "read-only @stream",
947 948 949
     0,NULL,1,1,1,0,0,0},

    {"xlen",xlenCommand,2,
950
     "read-only fast @stream",
951 952 953
     0,NULL,1,1,1,0,0,0},

    {"xread",xreadCommand,-4,
954
     "read-only @stream @blocking",
955 956 957
     0,xreadGetKeys,1,1,1,0,0,0},

    {"xreadgroup",xreadCommand,-7,
958
     "write @stream @blocking",
959 960 961
     0,xreadGetKeys,1,1,1,0,0,0},

    {"xgroup",xgroupCommand,-2,
962
     "write use-memory @stream",
963 964 965
     0,NULL,2,2,1,0,0,0},

    {"xsetid",xsetidCommand,3,
966
     "write use-memory fast @stream",
967 968 969
     0,NULL,1,1,1,0,0,0},

    {"xack",xackCommand,-4,
970
     "write fast random @stream",
971 972 973
     0,NULL,1,1,1,0,0,0},

    {"xpending",xpendingCommand,-3,
974
     "read-only random @stream",
975 976 977
     0,NULL,1,1,1,0,0,0},

    {"xclaim",xclaimCommand,-6,
978
     "write random fast @stream",
979 980 981
     0,NULL,1,1,1,0,0,0},

    {"xinfo",xinfoCommand,-2,
982
     "read-only random @stream",
983 984 985
     0,NULL,2,2,1,0,0,0},

    {"xdel",xdelCommand,-3,
986
     "write fast @stream",
987 988 989
     0,NULL,1,1,1,0,0,0},

    {"xtrim",xtrimCommand,-2,
990
     "write random @stream",
991 992 993
     0,NULL,1,1,1,0,0,0},

    {"post",securityWarningCommand,-1,
994
     "ok-loading ok-stale read-only",
995 996 997
     0,NULL,0,0,0,0,0,0},

    {"host:",securityWarningCommand,-1,
998
     "ok-loading ok-stale read-only",
999 1000 1001 1002 1003 1004 1005
     0,NULL,0,0,0,0,0,0},

    {"latency",latencyCommand,-2,
     "admin no-script ok-loading ok-stale",
     0,NULL,0,0,0,0,0,0},

    {"lolwut",lolwutCommand,-1,
1006
     "read-only fast",
1007 1008 1009
     0,NULL,0,0,0,0,0,0},

    {"acl",aclCommand,-2,
1010
     "admin no-script no-slowlog ok-loading ok-stale",
1011 1012
     0,NULL,0,0,0,0,0,0},

A
antirez 已提交
1013
    {"stralgo",stralgoCommand,-2,
A
antirez 已提交
1014
     "read-only @string",
1015
     0,lcsGetKeys,0,0,0,0,0,0}
1016 1017 1018 1019
};

/*============================ Utility functions ============================ */

1020 1021 1022 1023
/* We use a private localtime implementation which is fork-safe. The logging
 * function of Redis may be called from other threads. */
void nolocks_localtime(struct tm *tmp, time_t t, time_t tz, int dst);

1024
/* Low level logging. To use only for very big messages, otherwise
A
antirez 已提交
1025 1026
 * serverLog() is to prefer. */
void serverLogRaw(int level, const char *msg) {
J
Jonah H. Harris 已提交
1027 1028
    const int syslogLevelMap[] = { LOG_DEBUG, LOG_INFO, LOG_NOTICE, LOG_WARNING };
    const char *c = ".-*#";
1029
    FILE *fp;
1030
    char buf[64];
A
antirez 已提交
1031
    int rawmode = (level & LL_RAW);
1032
    int log_to_stdout = server.logfile[0] == '\0';
1033

A
antirez 已提交
1034
    level &= 0xff; /* clear flags */
1035
    if (level < server.verbosity) return;
1036

1037
    fp = log_to_stdout ? stdout : fopen(server.logfile,"a");
1038 1039
    if (!fp) return;

A
antirez 已提交
1040 1041 1042
    if (rawmode) {
        fprintf(fp,"%s",msg);
    } else {
1043 1044
        int off;
        struct timeval tv;
A
antirez 已提交
1045 1046
        int role_char;
        pid_t pid = getpid();
1047 1048

        gettimeofday(&tv,NULL);
1049 1050
        struct tm tm;
        nolocks_localtime(&tm,tv.tv_sec,server.timezone,server.daylight_active);
A
antirez 已提交
1051
        off = strftime(buf,sizeof(buf),"%d %b %Y %H:%M:%S.",&tm);
1052
        snprintf(buf+off,sizeof(buf)-off,"%03d",(int)tv.tv_usec/1000);
A
antirez 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061
        if (server.sentinel_mode) {
            role_char = 'X'; /* Sentinel. */
        } else if (pid != server.pid) {
            role_char = 'C'; /* RDB / AOF writing child. */
        } else {
            role_char = (server.masterhost ? 'S':'M'); /* Slave or Master. */
        }
        fprintf(fp,"%d:%c %s %c %s\n",
            (int)getpid(),role_char, buf,c[level],msg);
A
antirez 已提交
1062
    }
J
Jonah H. Harris 已提交
1063 1064
    fflush(fp);

1065
    if (!log_to_stdout) fclose(fp);
J
Jonah H. Harris 已提交
1066
    if (server.syslog_enabled) syslog(syslogLevelMap[level], "%s", msg);
1067 1068
}

A
antirez 已提交
1069
/* Like serverLogRaw() but with printf-alike support. This is the function that
1070 1071
 * is used across the code. The raw version is only used in order to dump
 * the INFO output on crash. */
A
antirez 已提交
1072
void serverLog(int level, const char *fmt, ...) {
1073
    va_list ap;
A
antirez 已提交
1074
    char msg[LOG_MAX_LEN];
1075

A
antirez 已提交
1076
    if ((level&0xff) < server.verbosity) return;
1077 1078 1079 1080 1081

    va_start(ap, fmt);
    vsnprintf(msg, sizeof(msg), fmt, ap);
    va_end(ap);

A
antirez 已提交
1082
    serverLogRaw(level,msg);
1083 1084
}

A
antirez 已提交
1085 1086 1087 1088 1089
/* Log a fixed message without printf-alike capabilities, in a way that is
 * safe to call from a signal handler.
 *
 * We actually use this only for signals that are not fatal from the point
 * of view of Redis. Signals that are going to kill the server anyway and
A
antirez 已提交
1090 1091
 * where we need printf-alike features are served by serverLog(). */
void serverLogFromHandler(int level, const char *msg) {
A
antirez 已提交
1092
    int fd;
1093
    int log_to_stdout = server.logfile[0] == '\0';
A
antirez 已提交
1094 1095
    char buf[64];

1096 1097
    if ((level&0xff) < server.verbosity || (log_to_stdout && server.daemonize))
        return;
1098
    fd = log_to_stdout ? STDOUT_FILENO :
1099
                         open(server.logfile, O_APPEND|O_CREAT|O_WRONLY, 0644);
A
antirez 已提交
1100 1101
    if (fd == -1) return;
    ll2string(buf,sizeof(buf),getpid());
1102
    if (write(fd,buf,strlen(buf)) == -1) goto err;
1103
    if (write(fd,":signal-handler (",17) == -1) goto err;
A
antirez 已提交
1104
    ll2string(buf,sizeof(buf),time(NULL));
1105 1106 1107 1108 1109
    if (write(fd,buf,strlen(buf)) == -1) goto err;
    if (write(fd,") ",2) == -1) goto err;
    if (write(fd,msg,strlen(msg)) == -1) goto err;
    if (write(fd,"\n",1) == -1) goto err;
err:
1110
    if (!log_to_stdout) close(fd);
A
antirez 已提交
1111 1112
}

A
antirez 已提交
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
/* Return the UNIX time in microseconds */
long long ustime(void) {
    struct timeval tv;
    long long ust;

    gettimeofday(&tv, NULL);
    ust = ((long long)tv.tv_sec)*1000000;
    ust += tv.tv_usec;
    return ust;
}

1124
/* Return the UNIX time in milliseconds */
1125
mstime_t mstime(void) {
1126 1127 1128
    return ustime()/1000;
}

1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
/* After an RDB dump or AOF rewrite we exit from children using _exit() instead of
 * exit(), because the latter may interact with the same file objects used by
 * the parent process. However if we are testing the coverage normal exit() is
 * used in order to obtain the right coverage information. */
void exitFromChild(int retcode) {
#ifdef COVERAGE_TEST
    exit(retcode);
#else
    _exit(retcode);
#endif
}

1141 1142
/*====================== Hash table type implementation  ==================== */

1143
/* This is a hash table type that uses the SDS dynamic strings library as
T
T.J. Schuck 已提交
1144
 * keys and redis objects as values (objects can hold SDS strings,
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
 * lists, sets). */

void dictVanillaFree(void *privdata, void *val)
{
    DICT_NOTUSED(privdata);
    zfree(val);
}

void dictListDestructor(void *privdata, void *val)
{
    DICT_NOTUSED(privdata);
    listRelease((list*)val);
}

int dictSdsKeyCompare(void *privdata, const void *key1,
        const void *key2)
{
    int l1,l2;
    DICT_NOTUSED(privdata);

    l1 = sdslen((sds)key1);
    l2 = sdslen((sds)key2);
    if (l1 != l2) return 0;
    return memcmp(key1, key2, l1) == 0;
}

A
antirez 已提交
1171 1172
/* A case insensitive version used for the command lookup table and other
 * places where case insensitive non binary-safe comparison is needed. */
1173 1174 1175 1176 1177 1178 1179 1180
int dictSdsKeyCaseCompare(void *privdata, const void *key1,
        const void *key2)
{
    DICT_NOTUSED(privdata);

    return strcasecmp(key1, key2) == 0;
}

1181
void dictObjectDestructor(void *privdata, void *val)
1182 1183 1184
{
    DICT_NOTUSED(privdata);

1185
    if (val == NULL) return; /* Lazy freeing will set value to NULL. */
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
    decrRefCount(val);
}

void dictSdsDestructor(void *privdata, void *val)
{
    DICT_NOTUSED(privdata);

    sdsfree(val);
}

int dictObjKeyCompare(void *privdata, const void *key1,
        const void *key2)
{
    const robj *o1 = key1, *o2 = key2;
    return dictSdsKeyCompare(privdata,o1->ptr,o2->ptr);
}

1203
uint64_t dictObjHash(const void *key) {
1204 1205 1206 1207
    const robj *o = key;
    return dictGenHashFunction(o->ptr, sdslen((sds)o->ptr));
}

1208
uint64_t dictSdsHash(const void *key) {
1209 1210 1211
    return dictGenHashFunction((unsigned char*)key, sdslen((char*)key));
}

1212
uint64_t dictSdsCaseHash(const void *key) {
1213 1214 1215
    return dictGenCaseHashFunction((unsigned char*)key, sdslen((char*)key));
}

1216 1217 1218 1219 1220 1221
int dictEncObjKeyCompare(void *privdata, const void *key1,
        const void *key2)
{
    robj *o1 = (robj*) key1, *o2 = (robj*) key2;
    int cmp;

1222 1223
    if (o1->encoding == OBJ_ENCODING_INT &&
        o2->encoding == OBJ_ENCODING_INT)
1224 1225
            return o1->ptr == o2->ptr;

1226 1227 1228 1229
    /* Due to OBJ_STATIC_REFCOUNT, we avoid calling getDecodedObject() without
     * good reasons, because it would incrRefCount() the object, which
     * is invalid. So we check to make sure dictFind() works with static
     * objects as well. */
O
Oran Agra 已提交
1230 1231
    if (o1->refcount != OBJ_STATIC_REFCOUNT) o1 = getDecodedObject(o1);
    if (o2->refcount != OBJ_STATIC_REFCOUNT) o2 = getDecodedObject(o2);
1232
    cmp = dictSdsKeyCompare(privdata,o1->ptr,o2->ptr);
O
Oran Agra 已提交
1233 1234
    if (o1->refcount != OBJ_STATIC_REFCOUNT) decrRefCount(o1);
    if (o2->refcount != OBJ_STATIC_REFCOUNT) decrRefCount(o2);
1235 1236 1237
    return cmp;
}

1238
uint64_t dictEncObjHash(const void *key) {
1239 1240
    robj *o = (robj*) key;

1241
    if (sdsEncodedObject(o)) {
1242 1243
        return dictGenHashFunction(o->ptr, sdslen((sds)o->ptr));
    } else {
1244
        if (o->encoding == OBJ_ENCODING_INT) {
1245 1246 1247 1248 1249 1250
            char buf[32];
            int len;

            len = ll2string(buf,32,(long)o->ptr);
            return dictGenHashFunction((unsigned char*)buf, len);
        } else {
1251
            uint64_t hash;
1252 1253 1254 1255 1256 1257 1258 1259 1260

            o = getDecodedObject(o);
            hash = dictGenHashFunction(o->ptr, sdslen((sds)o->ptr));
            decrRefCount(o);
            return hash;
        }
    }
}

1261 1262 1263
/* Generic hash table type where keys are Redis Objects, Values
 * dummy pointers. */
dictType objectKeyPointerValueDictType = {
1264 1265 1266 1267
    dictEncObjHash,            /* hash function */
    NULL,                      /* key dup */
    NULL,                      /* val dup */
    dictEncObjKeyCompare,      /* key compare */
1268
    dictObjectDestructor,      /* key destructor */
1269 1270 1271
    NULL                       /* val destructor */
};

1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
/* Like objectKeyPointerValueDictType(), but values can be destroyed, if
 * not NULL, calling zfree(). */
dictType objectKeyHeapPointerValueDictType = {
    dictEncObjHash,            /* hash function */
    NULL,                      /* key dup */
    NULL,                      /* val dup */
    dictEncObjKeyCompare,      /* key compare */
    dictObjectDestructor,      /* key destructor */
    dictVanillaFree            /* val destructor */
};

1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
/* Set dictionary type. Keys are SDS strings, values are ot used. */
dictType setDictType = {
    dictSdsHash,               /* hash function */
    NULL,                      /* key dup */
    NULL,                      /* val dup */
    dictSdsKeyCompare,         /* key compare */
    dictSdsDestructor,         /* key destructor */
    NULL                       /* val destructor */
};

1293 1294
/* Sorted sets hash (note: a skiplist is used in addition to the hash table) */
dictType zsetDictType = {
1295
    dictSdsHash,               /* hash function */
1296 1297
    NULL,                      /* key dup */
    NULL,                      /* val dup */
1298 1299
    dictSdsKeyCompare,         /* key compare */
    NULL,                      /* Note: SDS string shared & freed by skiplist */
1300
    NULL                       /* val destructor */
1301 1302 1303 1304 1305 1306 1307 1308 1309
};

/* Db->dict, keys are sds strings, vals are Redis objects. */
dictType dbDictType = {
    dictSdsHash,                /* hash function */
    NULL,                       /* key dup */
    NULL,                       /* val dup */
    dictSdsKeyCompare,          /* key compare */
    dictSdsDestructor,          /* key destructor */
1310
    dictObjectDestructor   /* val destructor */
1311 1312
};

A
antirez 已提交
1313 1314 1315 1316 1317 1318 1319
/* server.lua_scripts sha (as sds string) -> scripts (as robj) cache. */
dictType shaScriptObjectDictType = {
    dictSdsCaseHash,            /* hash function */
    NULL,                       /* key dup */
    NULL,                       /* val dup */
    dictSdsKeyCaseCompare,      /* key compare */
    dictSdsDestructor,          /* key destructor */
1320
    dictObjectDestructor        /* val destructor */
A
antirez 已提交
1321 1322
};

1323 1324
/* Db->expires */
dictType keyptrDictType = {
1325 1326 1327 1328 1329 1330
    dictSdsHash,                /* hash function */
    NULL,                       /* key dup */
    NULL,                       /* val dup */
    dictSdsKeyCompare,          /* key compare */
    NULL,                       /* key destructor */
    NULL                        /* val destructor */
1331 1332
};

1333 1334
/* Command table. sds string -> command struct pointer. */
dictType commandTableDictType = {
1335 1336 1337 1338 1339 1340
    dictSdsCaseHash,            /* hash function */
    NULL,                       /* key dup */
    NULL,                       /* val dup */
    dictSdsKeyCaseCompare,      /* key compare */
    dictSdsDestructor,          /* key destructor */
    NULL                        /* val destructor */
1341 1342
};

1343
/* Hash type hash table (note that small hashes are represented with ziplists) */
1344
dictType hashDictType = {
1345
    dictSdsHash,                /* hash function */
1346 1347
    NULL,                       /* key dup */
    NULL,                       /* val dup */
1348
    dictSdsKeyCompare,          /* key compare */
1349 1350
    dictSdsDestructor,          /* key destructor */
    dictSdsDestructor           /* val destructor */
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
};

/* Keylist hash table type has unencoded redis objects as keys and
 * lists as values. It's used for blocking operations (BLPOP) and to
 * map swapped keys to a list of clients waiting for this keys to be loaded. */
dictType keylistDictType = {
    dictObjHash,                /* hash function */
    NULL,                       /* key dup */
    NULL,                       /* val dup */
    dictObjKeyCompare,          /* key compare */
1361
    dictObjectDestructor,       /* key destructor */
1362 1363 1364
    dictListDestructor          /* val destructor */
};

A
antirez 已提交
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
/* Cluster nodes hash table, mapping nodes addresses 1.2.3.4:6379 to
 * clusterNode structures. */
dictType clusterNodesDictType = {
    dictSdsHash,                /* hash function */
    NULL,                       /* key dup */
    NULL,                       /* val dup */
    dictSdsKeyCompare,          /* key compare */
    dictSdsDestructor,          /* key destructor */
    NULL                        /* val destructor */
};

1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
/* Cluster re-addition blacklist. This maps node IDs to the time
 * we can re-add this node. The goal is to avoid readding a removed
 * node for some time. */
dictType clusterNodesBlackListDictType = {
    dictSdsCaseHash,            /* hash function */
    NULL,                       /* key dup */
    NULL,                       /* val dup */
    dictSdsKeyCaseCompare,      /* key compare */
    dictSdsDestructor,          /* key destructor */
    NULL                        /* val destructor */
};

A
antirez 已提交
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
/* Cluster re-addition blacklist. This maps node IDs to the time
 * we can re-add this node. The goal is to avoid readding a removed
 * node for some time. */
dictType modulesDictType = {
    dictSdsCaseHash,            /* hash function */
    NULL,                       /* key dup */
    NULL,                       /* val dup */
    dictSdsKeyCaseCompare,      /* key compare */
    dictSdsDestructor,          /* key destructor */
    NULL                        /* val destructor */
};

A
antirez 已提交
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
/* Migrate cache dict type. */
dictType migrateCacheDictType = {
    dictSdsHash,                /* hash function */
    NULL,                       /* key dup */
    NULL,                       /* val dup */
    dictSdsKeyCompare,          /* key compare */
    dictSdsDestructor,          /* key destructor */
    NULL                        /* val destructor */
};

1410 1411 1412 1413
/* Replication cached script dict (server.repl_scriptcache_dict).
 * Keys are sds SHA1 strings, while values are not used at all in the current
 * implementation. */
dictType replScriptCacheDictType = {
1414
    dictSdsCaseHash,            /* hash function */
1415 1416
    NULL,                       /* key dup */
    NULL,                       /* val dup */
1417
    dictSdsKeyCaseCompare,      /* key compare */
1418 1419 1420 1421
    dictSdsDestructor,          /* key destructor */
    NULL                        /* val destructor */
};

1422 1423 1424 1425 1426
int htNeedsResize(dict *dict) {
    long long size, used;

    size = dictSlots(dict);
    used = dictSize(dict);
1427
    return (size > DICT_HT_INITIAL_SIZE &&
A
antirez 已提交
1428
            (used*100/size < HASHTABLE_MIN_FILL));
1429 1430
}

A
antirez 已提交
1431
/* If the percentage of used slots in the HT reaches HASHTABLE_MIN_FILL
1432
 * we resize the hash table to save memory */
1433 1434 1435 1436 1437
void tryResizeHashTables(int dbid) {
    if (htNeedsResize(server.db[dbid].dict))
        dictResize(server.db[dbid].dict);
    if (htNeedsResize(server.db[dbid].expires))
        dictResize(server.db[dbid].expires);
1438 1439 1440 1441 1442
}

/* Our hash table implementation performs rehashing incrementally while
 * we write/read from the hash table. Still if the server is idle, the hash
 * table will use two tables for a long time. So we try to use 1 millisecond
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
 * of CPU time at every call of this function to perform some rehahsing.
 *
 * The function returns 1 if some rehashing was performed, otherwise 0
 * is returned. */
int incrementallyRehash(int dbid) {
    /* Keys dictionary */
    if (dictIsRehashing(server.db[dbid].dict)) {
        dictRehashMilliseconds(server.db[dbid].dict,1);
        return 1; /* already used our millisecond for this loop... */
    }
    /* Expires */
    if (dictIsRehashing(server.db[dbid].expires)) {
        dictRehashMilliseconds(server.db[dbid].expires,1);
        return 1; /* already used our millisecond for this loop... */
1457
    }
1458
    return 0;
1459 1460 1461 1462 1463 1464 1465 1466 1467
}

/* This function is called once a background process of some kind terminates,
 * as we want to avoid resizing the hash tables when there is a child in order
 * to play well with copy-on-write (otherwise when a resize happens lots of
 * memory pages are copied). The goal of this function is to update the ability
 * for dict.c to resize the hash tables accordingly to the fact we have o not
 * running childs. */
void updateDictResizePolicy(void) {
1468
    if (!hasActiveChildProcess())
1469 1470 1471 1472 1473
        dictEnableResize();
    else
        dictDisableResize();
}

1474 1475
/* Return true if there are no active children processes doing RDB saving,
 * AOF rewriting, or some side process spawned by a loaded module. */
1476
int hasActiveChildProcess() {
O
Oran Agra 已提交
1477 1478 1479 1480 1481
    return server.rdb_child_pid != -1 ||
           server.aof_child_pid != -1 ||
           server.module_child_pid != -1;
}

1482 1483 1484 1485 1486 1487
/* Return true if this instance has persistence completely turned off:
 * both RDB and AOF are disabled. */
int allPersistenceDisabled(void) {
    return server.saveparamslen == 0 && server.aof_state == AOF_OFF;
}

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
/* ======================= Cron: called every 100 ms ======================== */

/* Add a sample to the operations per second array of samples. */
void trackInstantaneousMetric(int metric, long long current_reading) {
    long long t = mstime() - server.inst_metric[metric].last_sample_time;
    long long ops = current_reading -
                    server.inst_metric[metric].last_sample_count;
    long long ops_sec;

    ops_sec = t > 0 ? (ops*1000/t) : 0;

    server.inst_metric[metric].samples[server.inst_metric[metric].idx] =
        ops_sec;
    server.inst_metric[metric].idx++;
    server.inst_metric[metric].idx %= STATS_METRIC_SAMPLES;
    server.inst_metric[metric].last_sample_time = mstime();
    server.inst_metric[metric].last_sample_count = current_reading;
}

/* Return the mean of all the samples. */
long long getInstantaneousMetric(int metric) {
    int j;
    long long sum = 0;

    for (j = 0; j < STATS_METRIC_SAMPLES; j++)
        sum += server.inst_metric[metric].samples[j];
    return sum / STATS_METRIC_SAMPLES;
}

1517
/* The client query buffer is an sds.c string that can end with a lot of
A
antirez 已提交
1518 1519
 * free space not used, this function reclaims space if needed.
 *
G
guiquanz 已提交
1520
 * The function always returns 0 as it never terminates the client. */
1521
int clientsCronResizeQueryBuffer(client *c) {
1522 1523 1524 1525 1526
    size_t querybuf_size = sdsAllocSize(c->querybuf);
    time_t idletime = server.unixtime - c->lastinteraction;

    /* There are two conditions to resize the query buffer:
     * 1) Query buffer is > BIG_ARG and too big for latest peak.
1527 1528 1529 1530
     * 2) Query buffer is > BIG_ARG and client is idle. */
    if (querybuf_size > PROTO_MBULK_BIG_ARG &&
         ((querybuf_size/(c->querybuf_peak+1)) > 2 ||
          idletime > 2))
1531
    {
1532 1533 1534
        /* Only resize the query buffer if it is actually wasting
         * at least a few kbytes. */
        if (sdsavail(c->querybuf) > 1024*4) {
1535 1536 1537 1538 1539 1540
            c->querybuf = sdsRemoveFreeSpace(c->querybuf);
        }
    }
    /* Reset the peak again to capture the peak memory usage in the next
     * cycle. */
    c->querybuf_peak = 0;
1541

A
antirez 已提交
1542 1543 1544 1545 1546
    /* Clients representing masters also use a "pending query buffer" that
     * is the yet not applied part of the stream we are reading. Such buffer
     * also needs resizing from time to time, otherwise after a very large
     * transfer (a huge value or a big MIGRATE operation) it will keep using
     * a lot of memory. */
1547 1548 1549
    if (c->flags & CLIENT_MASTER) {
        /* There are two conditions to resize the pending query buffer:
         * 1) Pending Query buffer is > LIMIT_PENDING_QUERYBUF.
A
antirez 已提交
1550
         * 2) Used length is smaller than pending_querybuf_size/2 */
1551 1552
        size_t pending_querybuf_size = sdsAllocSize(c->pending_querybuf);
        if(pending_querybuf_size > LIMIT_PENDING_QUERYBUF &&
A
antirez 已提交
1553 1554
           sdslen(c->pending_querybuf) < (pending_querybuf_size/2))
        {
1555 1556 1557
            c->pending_querybuf = sdsRemoveFreeSpace(c->pending_querybuf);
        }
    }
A
antirez 已提交
1558
    return 0;
1559 1560
}

1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
/* This function is used in order to track clients using the biggest amount
 * of memory in the latest few seconds. This way we can provide such information
 * in the INFO output (clients section), without having to do an O(N) scan for
 * all the clients.
 *
 * This is how it works. We have an array of CLIENTS_PEAK_MEM_USAGE_SLOTS slots
 * where we track, for each, the biggest client output and input buffers we
 * saw in that slot. Every slot correspond to one of the latest seconds, since
 * the array is indexed by doing UNIXTIME % CLIENTS_PEAK_MEM_USAGE_SLOTS.
 *
 * When we want to know what was recently the peak memory usage, we just scan
 * such few slots searching for the maximum value. */
1573
#define CLIENTS_PEAK_MEM_USAGE_SLOTS 8
1574 1575 1576 1577 1578 1579
size_t ClientsPeakMemInput[CLIENTS_PEAK_MEM_USAGE_SLOTS];
size_t ClientsPeakMemOutput[CLIENTS_PEAK_MEM_USAGE_SLOTS];

int clientsCronTrackExpansiveClients(client *c) {
    size_t in_usage = sdsAllocSize(c->querybuf);
    size_t out_usage = getClientOutputBufferMemoryUsage(c);
1580
    int i = server.unixtime % CLIENTS_PEAK_MEM_USAGE_SLOTS;
1581
    int zeroidx = (i+1) % CLIENTS_PEAK_MEM_USAGE_SLOTS;
1582 1583 1584

    /* Always zero the next sample, so that when we switch to that second, we'll
     * only register samples that are greater in that second without considering
1585 1586 1587 1588 1589 1590 1591 1592 1593
     * the history of such slot.
     *
     * Note: our index may jump to any random position if serverCron() is not
     * called for some reason with the normal frequency, for instance because
     * some slow command is called taking multiple seconds to execute. In that
     * case our array may end containing data which is potentially older
     * than CLIENTS_PEAK_MEM_USAGE_SLOTS seconds: however this is not a problem
     * since here we want just to track if "recently" there were very expansive
     * clients from the POV of memory usage. */
1594 1595
    ClientsPeakMemInput[zeroidx] = 0;
    ClientsPeakMemOutput[zeroidx] = 0;
1596 1597 1598 1599 1600

    /* Track the biggest values observed so far in this slot. */
    if (in_usage > ClientsPeakMemInput[i]) ClientsPeakMemInput[i] = in_usage;
    if (out_usage > ClientsPeakMemOutput[i]) ClientsPeakMemOutput[i] = out_usage;

1601 1602 1603
    return 0; /* This function never terminates the client. */
}

1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
/* Iterating all the clients in getMemoryOverheadData() is too slow and
 * in turn would make the INFO command too slow. So we perform this
 * computation incrementally and track the (not instantaneous but updated
 * to the second) total memory used by clients using clinetsCron() in
 * a more incremental way (depending on server.hz). */
int clientsCronTrackClientsMemUsage(client *c) {
    size_t mem = 0;
    int type = getClientType(c);
    mem += getClientOutputBufferMemoryUsage(c);
    mem += sdsAllocSize(c->querybuf);
    mem += sizeof(client);
    /* Now that we have the memory used by the client, remove the old
     * value from the old categoty, and add it back. */
    server.stat_clients_type_memory[c->client_cron_last_memory_type] -=
        c->client_cron_last_memory_usage;
    server.stat_clients_type_memory[type] += mem;
    /* Remember what we added and where, to remove it next time. */
    c->client_cron_last_memory_usage = mem;
    c->client_cron_last_memory_type = type;
    return 0;
}

1626 1627 1628 1629 1630 1631
/* Return the max samples in the memory usage of clients tracked by
 * the function clientsCronTrackExpansiveClients(). */
void getExpansiveClientsInfo(size_t *in_usage, size_t *out_usage) {
    size_t i = 0, o = 0;
    for (int j = 0; j < CLIENTS_PEAK_MEM_USAGE_SLOTS; j++) {
        if (ClientsPeakMemInput[j] > i) i = ClientsPeakMemInput[j];
1632
        if (ClientsPeakMemOutput[j] > o) o = ClientsPeakMemOutput[j];
1633 1634 1635 1636 1637
    }
    *in_usage = i;
    *out_usage = o;
}

A
antirez 已提交
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
/* This function is called by serverCron() and is used in order to perform
 * operations on clients that are important to perform constantly. For instance
 * we use this function in order to disconnect clients after a timeout, including
 * clients blocked in some blocking command with a non-zero timeout.
 *
 * The function makes some effort to process all the clients every second, even
 * if this cannot be strictly guaranteed, since serverCron() may be called with
 * an actual frequency lower than server.hz in case of latency events like slow
 * commands.
 *
 * It is very important for this function, and the functions it calls, to be
 * very fast: sometimes Redis has tens of hundreds of connected clients, and the
 * default server.hz value is 10, so sometimes here we need to process thousands
 * of clients per second, turning this function into a source of latency.
 */
A
antirez 已提交
1653
#define CLIENTS_CRON_MIN_ITERATIONS 5
1654
void clientsCron(void) {
A
antirez 已提交
1655 1656 1657 1658
    /* Try to process at least numclients/server.hz of clients
     * per call. Since normally (if there are no big latency events) this
     * function is called server.hz times per second, in the average case we
     * process all the clients in 1 second. */
1659
    int numclients = listLength(server.clients);
A
antirez 已提交
1660 1661 1662 1663 1664 1665 1666 1667 1668
    int iterations = numclients/server.hz;
    mstime_t now = mstime();

    /* Process at least a few clients while we are at it, even if we need
     * to process less than CLIENTS_CRON_MIN_ITERATIONS to meet our contract
     * of processing each client once per second. */
    if (iterations < CLIENTS_CRON_MIN_ITERATIONS)
        iterations = (numclients < CLIENTS_CRON_MIN_ITERATIONS) ?
                     numclients : CLIENTS_CRON_MIN_ITERATIONS;
1669 1670

    while(listLength(server.clients) && iterations--) {
1671
        client *c;
1672 1673 1674 1675 1676
        listNode *head;

        /* Rotate the list, take the current head, process.
         * This way if the client must be removed from the list it's the
         * first element and we don't incur into O(N) computation. */
1677
        listRotateTailToHead(server.clients);
1678 1679
        head = listFirst(server.clients);
        c = listNodeValue(head);
A
antirez 已提交
1680 1681 1682
        /* The following functions do different service checks on the client.
         * The protocol is that they return non-zero if the client was
         * terminated. */
A
antirez 已提交
1683
        if (clientsCronHandleTimeout(c,now)) continue;
A
antirez 已提交
1684
        if (clientsCronResizeQueryBuffer(c)) continue;
1685
        if (clientsCronTrackExpansiveClients(c)) continue;
1686
        if (clientsCronTrackClientsMemUsage(c)) continue;
1687 1688 1689
    }
}

1690 1691 1692 1693
/* This function handles 'background' operations we are required to do
 * incrementally in Redis databases, such as active key expiring, resizing,
 * rehashing. */
void databasesCron(void) {
1694 1695
    /* Expire keys by random sampling. Not required for slaves
     * as master will synthesize DELs for us. */
1696
    if (server.active_expire_enabled) {
1697
        if (iAmMaster()) {
1698 1699 1700 1701
            activeExpireCycle(ACTIVE_EXPIRE_CYCLE_SLOW);
        } else {
            expireSlaveKeys();
        }
1702
    }
1703

O
oranagra 已提交
1704
    /* Defrag keys gradually. */
1705
    activeDefragCycle();
O
oranagra 已提交
1706

1707 1708 1709
    /* Perform hash tables rehashing if needed, but only if there are no
     * other processes saving the DB on disk. Otherwise rehashing is bad
     * as will cause a lot of copy-on-write of memory pages. */
1710
    if (!hasActiveChildProcess()) {
1711 1712 1713
        /* We use global counters so if we stop the computation at a given
         * DB we'll be able to start from the successive in the next
         * cron loop iteration. */
1714 1715
        static unsigned int resize_db = 0;
        static unsigned int rehash_db = 0;
A
antirez 已提交
1716
        int dbs_per_call = CRON_DBS_PER_CALL;
1717
        int j;
1718

1719 1720 1721
        /* Don't test more DBs than we have. */
        if (dbs_per_call > server.dbnum) dbs_per_call = server.dbnum;

1722
        /* Resize */
1723
        for (j = 0; j < dbs_per_call; j++) {
1724 1725 1726 1727 1728 1729
            tryResizeHashTables(resize_db % server.dbnum);
            resize_db++;
        }

        /* Rehash */
        if (server.activerehashing) {
1730
            for (j = 0; j < dbs_per_call; j++) {
Z
zhaozhao.zz 已提交
1731
                int work_done = incrementallyRehash(rehash_db);
1732 1733 1734 1735
                if (work_done) {
                    /* If the function did some work, stop here, we'll do
                     * more at the next cron loop. */
                    break;
Z
zhaozhao.zz 已提交
1736 1737 1738 1739
                } else {
                    /* If this db didn't need rehash, we'll try the next one. */
                    rehash_db++;
                    rehash_db %= server.dbnum;
1740 1741 1742
                }
            }
        }
1743 1744 1745
    }
}

1746 1747 1748
/* We take a cached value of the unix time in the global state because with
 * virtual memory and aging there is to store the current time in objects at
 * every object access, and accuracy is not needed. To access a global var is
A
antirez 已提交
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
 * a lot faster than calling time(NULL).
 *
 * This function should be fast because it is called at every command execution
 * in call(), so it is possible to decide if to update the daylight saving
 * info or not using the 'update_daylight_info' argument. Normally we update
 * such info only when calling this function from serverCron() but not when
 * calling it from call(). */
void updateCachedTime(int update_daylight_info) {
    server.ustime = ustime();
    server.mstime = server.ustime / 1000;
    server.unixtime = server.mstime / 1000;
1760

A
antirez 已提交
1761 1762 1763 1764 1765
    /* To get information about daylight saving time, we need to call
     * localtime_r and cache the result. However calling localtime_r in this
     * context is safe since we will never fork() while here, in the main
     * thread. The logging function will call a thread safe version of
     * localtime that has no locks. */
A
antirez 已提交
1766 1767 1768 1769 1770 1771
    if (update_daylight_info) {
        struct tm tm;
        time_t ut = server.unixtime;
        localtime_r(&ut,&tm);
        server.daylight_active = tm.tm_isdst;
    }
1772 1773
}

1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
void checkChildrenDone(void) {
    int statloc;
    pid_t pid;

    /* If we have a diskless rdb child (note that we support only one concurrent
     * child), we want to avoid collecting it's exit status and acting on it
     * as long as we didn't finish to drain the pipe, since then we're at risk
     * of starting a new fork and a new pipe before we're done with the previous
     * one. */
    if (server.rdb_child_pid != -1 && server.rdb_pipe_conns)
        return;

    if ((pid = wait3(&statloc,WNOHANG,NULL)) != 0) {
        int exitcode = WEXITSTATUS(statloc);
        int bysignal = 0;

        if (WIFSIGNALED(statloc)) bysignal = WTERMSIG(statloc);

1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
        /* sigKillChildHandler catches the signal and calls exit(), but we
         * must make sure not to flag lastbgsave_status, etc incorrectly.
         * We could directly terminate the child process via SIGUSR1
         * without handling it, but in this case Valgrind will log an
         * annoying error. */
        if (exitcode == SERVER_CHILD_NOERROR_RETVAL) {
            bysignal = SIGUSR1;
            exitcode = 1;
        }

1802 1803
        if (pid == -1) {
            serverLog(LL_WARNING,"wait3() returned an error: %s. "
1804
                "rdb_child_pid = %d, aof_child_pid = %d, module_child_pid = %d",
1805 1806
                strerror(errno),
                (int) server.rdb_child_pid,
1807 1808
                (int) server.aof_child_pid,
                (int) server.module_child_pid);
1809 1810 1811 1812 1813 1814
        } else if (pid == server.rdb_child_pid) {
            backgroundSaveDoneHandler(exitcode,bysignal);
            if (!bysignal && exitcode == 0) receiveChildInfo();
        } else if (pid == server.aof_child_pid) {
            backgroundRewriteDoneHandler(exitcode,bysignal);
            if (!bysignal && exitcode == 0) receiveChildInfo();
1815 1816 1817
        } else if (pid == server.module_child_pid) {
            ModuleForkDoneHandler(exitcode,bysignal);
            if (!bysignal && exitcode == 0) receiveChildInfo();
1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
        } else {
            if (!ldbRemoveChild(pid)) {
                serverLog(LL_WARNING,
                    "Warning, detected child with unmatched pid: %ld",
                    (long)pid);
            }
        }
        updateDictResizePolicy();
        closeChildInfoPipe();
    }
}

1830
/* This is our timer interrupt, called server.hz times per second.
1831 1832 1833 1834 1835
 * Here is where we do a number of things that need to be done asynchronously.
 * For instance:
 *
 * - Active expired keys collection (it is also performed in a lazy way on
 *   lookup).
G
guiquanz 已提交
1836
 * - Software watchdog.
1837 1838 1839
 * - Update some statistic.
 * - Incremental rehashing of the DBs hash tables.
 * - Triggering BGSAVE / AOF rewrite, and handling of terminated children.
G
guiquanz 已提交
1840
 * - Clients timeout of different kinds.
1841 1842 1843
 * - Replication reconnection.
 * - Many more...
 *
1844
 * Everything directly called here will be called server.hz times per second,
1845 1846 1847 1848
 * so in order to throttle execution of things we want to do less frequently
 * a macro is used: run_with_period(milliseconds) { .... }
 */

1849
int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
1850
    int j;
A
antirez 已提交
1851 1852 1853
    UNUSED(eventLoop);
    UNUSED(id);
    UNUSED(clientData);
1854

A
antirez 已提交
1855 1856 1857 1858
    /* Software watchdog: deliver the SIGALRM that will reach the signal
     * handler if we don't return here fast enough. */
    if (server.watchdog_period) watchdogScheduleSignal(server.watchdog_period);

1859
    /* Update the time cache. */
A
antirez 已提交
1860
    updateCachedTime(1);
A
antirez 已提交
1861

1862
    server.hz = server.config_hz;
1863 1864
    /* Adapt the server.hz value to the number of configured clients. If we have
     * many clients, we want to call serverCron() with an higher frequency. */
1865 1866 1867 1868 1869 1870 1871 1872 1873
    if (server.dynamic_hz) {
        while (listLength(server.clients) / server.hz >
               MAX_CLIENTS_PER_CLOCK_TICK)
        {
            server.hz *= 2;
            if (server.hz > CONFIG_MAX_HZ) {
                server.hz = CONFIG_MAX_HZ;
                break;
            }
1874 1875 1876
        }
    }

1877
    run_with_period(100) {
A
antirez 已提交
1878 1879
        trackInstantaneousMetric(STATS_METRIC_COMMAND,server.stat_numcommands);
        trackInstantaneousMetric(STATS_METRIC_NET_INPUT,
1880
                server.stat_net_input_bytes);
A
antirez 已提交
1881
        trackInstantaneousMetric(STATS_METRIC_NET_OUTPUT,
1882 1883
                server.stat_net_output_bytes);
    }
1884

A
antirez 已提交
1885
    /* We have just LRU_BITS bits per object for LRU information.
1886
     * So we use an (eventually wrapping) LRU clock.
1887
     *
1888 1889 1890 1891 1892
     * Note that even if the counter wraps it's not a big problem,
     * everything will still work but some object will appear younger
     * to Redis. However for this to happen a given object should never be
     * touched for all the time needed to the counter to wrap, which is
     * not likely.
1893 1894
     *
     * Note that you can change the resolution altering the
A
antirez 已提交
1895
     * LRU_CLOCK_RESOLUTION define. */
A
antirez 已提交
1896
    server.lruclock = getLRUClock();
1897

1898 1899 1900 1901
    /* Record the max memory used since the server was started. */
    if (zmalloc_used_memory() > server.stat_peak_memory)
        server.stat_peak_memory = zmalloc_used_memory();

O
Oran Agra 已提交
1902
    run_with_period(100) {
1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928
        /* Sample the RSS and other metrics here since this is a relatively slow call.
         * We must sample the zmalloc_used at the same time we take the rss, otherwise
         * the frag ratio calculate may be off (ratio of two samples at different times) */
        server.cron_malloc_stats.process_rss = zmalloc_get_rss();
        server.cron_malloc_stats.zmalloc_used = zmalloc_used_memory();
        /* Sampling the allcator info can be slow too.
         * The fragmentation ratio it'll show is potentically more accurate
         * it excludes other RSS pages such as: shared libraries, LUA and other non-zmalloc
         * allocations, and allocator reserved pages that can be pursed (all not actual frag) */
        zmalloc_get_allocator_info(&server.cron_malloc_stats.allocator_allocated,
                                   &server.cron_malloc_stats.allocator_active,
                                   &server.cron_malloc_stats.allocator_resident);
        /* in case the allocator isn't providing these stats, fake them so that
         * fragmention info still shows some (inaccurate metrics) */
        if (!server.cron_malloc_stats.allocator_resident) {
            /* LUA memory isn't part of zmalloc_used, but it is part of the process RSS,
             * so we must desuct it in order to be able to calculate correct
             * "allocator fragmentation" ratio */
            size_t lua_memory = lua_gc(server.lua,LUA_GCCOUNT,0)*1024LL;
            server.cron_malloc_stats.allocator_resident = server.cron_malloc_stats.process_rss - lua_memory;
        }
        if (!server.cron_malloc_stats.allocator_active)
            server.cron_malloc_stats.allocator_active = server.cron_malloc_stats.allocator_resident;
        if (!server.cron_malloc_stats.allocator_allocated)
            server.cron_malloc_stats.allocator_allocated = server.cron_malloc_stats.zmalloc_used;
    }
A
antirez 已提交
1929

1930 1931 1932
    /* We received a SIGTERM, shutting down here in a safe way, as it is
     * not ok doing so inside the signal handler. */
    if (server.shutdown_asap) {
1933
        if (prepareForShutdown(SHUTDOWN_NOFLAGS) == C_OK) exit(0);
A
antirez 已提交
1934
        serverLog(LL_WARNING,"SIGTERM received but errors trying to shut down the server, check the logs for more information");
1935
        server.shutdown_asap = 0;
1936 1937 1938
    }

    /* Show some info about non-empty databases */
1939 1940 1941 1942 1943 1944 1945 1946
    run_with_period(5000) {
        for (j = 0; j < server.dbnum; j++) {
            long long size, used, vkeys;

            size = dictSlots(server.db[j].dict);
            used = dictSize(server.db[j].dict);
            vkeys = dictSize(server.db[j].expires);
            if (used || vkeys) {
A
antirez 已提交
1947
                serverLog(LL_VERBOSE,"DB %d: %lld keys (%lld volatile) in %lld slots HT.",j,used,vkeys,size);
1948 1949
                /* dictPrintStats(server.dict); */
            }
1950 1951 1952 1953
        }
    }

    /* Show information about connected clients */
1954 1955
    if (!server.sentinel_mode) {
        run_with_period(5000) {
1956
            serverLog(LL_DEBUG,
A
antirez 已提交
1957
                "%lu clients connected (%lu replicas), %zu bytes in use",
1958 1959 1960 1961
                listLength(server.clients)-listLength(server.slaves),
                listLength(server.slaves),
                zmalloc_used_memory());
        }
1962 1963
    }

1964 1965
    /* We need to do a few operations on clients asynchronously. */
    clientsCron();
1966

1967 1968 1969
    /* Handle background operations on Redis databases. */
    databasesCron();

1970 1971
    /* Start a scheduled AOF rewrite if this was requested by the user while
     * a BGSAVE was in progress. */
1972
    if (!hasActiveChildProcess() &&
1973
        server.aof_rewrite_scheduled)
1974 1975 1976 1977
    {
        rewriteAppendOnlyFileBackground();
    }

A
antirez 已提交
1978
    /* Check if a background saving or AOF rewrite in progress terminated. */
1979
    if (hasActiveChildProcess() || ldbPendingChildren())
1980
    {
1981
        checkChildrenDone();
A
antirez 已提交
1982
    } else {
1983
        /* If there is not a background saving/rewrite in progress check if
1984
         * we have to save/rewrite now. */
D
dejun.xdj 已提交
1985
        for (j = 0; j < server.saveparamslen; j++) {
1986 1987
            struct saveparam *sp = server.saveparams+j;

1988 1989 1990
            /* Save if we reached the given amount of changes,
             * the given amount of seconds, and if the latest bgsave was
             * successful or if, in case of an error, at least
A
antirez 已提交
1991
             * CONFIG_BGSAVE_RETRY_DELAY seconds already elapsed. */
1992
            if (server.dirty >= sp->changes &&
1993 1994
                server.unixtime-server.lastsave > sp->seconds &&
                (server.unixtime-server.lastbgsave_try >
A
antirez 已提交
1995
                 CONFIG_BGSAVE_RETRY_DELAY ||
1996
                 server.lastbgsave_status == C_OK))
1997
            {
A
antirez 已提交
1998
                serverLog(LL_NOTICE,"%d changes in %d seconds. Saving...",
1999
                    sp->changes, (int)sp->seconds);
2000 2001 2002
                rdbSaveInfo rsi, *rsiptr;
                rsiptr = rdbPopulateSaveInfo(&rsi);
                rdbSaveBackground(server.rdb_filename,rsiptr);
2003 2004
                break;
            }
D
dejun.xdj 已提交
2005 2006 2007 2008
        }

        /* Trigger an AOF rewrite if needed. */
        if (server.aof_state == AOF_ON &&
2009
            !hasActiveChildProcess() &&
D
dejun.xdj 已提交
2010 2011 2012
            server.aof_rewrite_perc &&
            server.aof_current_size > server.aof_rewrite_min_size)
        {
2013
            long long base = server.aof_rewrite_base_size ?
D
dejun.xdj 已提交
2014
                server.aof_rewrite_base_size : 1;
2015 2016
            long long growth = (server.aof_current_size*100/base) - 100;
            if (growth >= server.aof_rewrite_perc) {
A
antirez 已提交
2017
                serverLog(LL_NOTICE,"Starting automatic rewriting of AOF on %lld%% growth",growth);
2018 2019
                rewriteAppendOnlyFileBackground();
            }
D
dejun.xdj 已提交
2020
        }
2021 2022
    }

2023

2024 2025 2026 2027 2028 2029 2030 2031 2032
    /* AOF postponed flush: Try at every cron cycle if the slow fsync
     * completed. */
    if (server.aof_flush_postponed_start) flushAppendOnlyFile(0);

    /* AOF write errors: in this case we have a buffer to flush as well and
     * clear the AOF error in case of success to make the DB writable again,
     * however to try every second is enough in case of 'hz' is set to
     * an higher frequency. */
    run_with_period(1000) {
2033
        if (server.aof_last_write_status == C_ERR)
2034
            flushAppendOnlyFile(0);
2035
    }
2036

2037
    /* Clear the paused clients flag if needed. */
A
antirez 已提交
2038
    clientsArePaused(); /* Don't check return value, just use the side effect.*/
2039

2040 2041
    /* Replication cron function -- used to reconnect to master,
     * detect transfer failures, start background RDB transfers and so forth. */
2042
    run_with_period(1000) replicationCron();
2043

A
antirez 已提交
2044
    /* Run the Redis Cluster cron. */
2045
    run_with_period(100) {
2046 2047
        if (server.cluster_enabled) clusterCron();
    }
A
antirez 已提交
2048

A
antirez 已提交
2049
    /* Run the Sentinel timer if we are in sentinel mode. */
2050
    if (server.sentinel_mode) sentinelTimer();
2051

A
antirez 已提交
2052 2053 2054 2055 2056
    /* Cleanup expired MIGRATE cached sockets. */
    run_with_period(1000) {
        migrateCloseTimedoutSockets();
    }

2057 2058 2059
    /* Stop the I/O threads if we don't have enough pending work. */
    stopThreadedIOIfNeeded();

2060 2061 2062 2063 2064 2065
    /* Resize tracking keys table if needed. This is also done at every
     * command execution, but we want to be sure that if the last command
     * executed changes the value via CONFIG SET, the server will perform
     * the operation even if completely idle. */
    if (server.tracking_clients) trackingLimitUsedSlots();

2066 2067 2068 2069 2070 2071 2072
    /* Start a scheduled BGSAVE if the corresponding flag is set. This is
     * useful when we are forced to postpone a BGSAVE because an AOF
     * rewrite is in progress.
     *
     * Note: this code must be after the replicationCron() call above so
     * make sure when refactoring this file to keep this order. This is useful
     * because we want to give priority to RDB savings for replication. */
2073
    if (!hasActiveChildProcess() &&
2074 2075 2076 2077
        server.rdb_bgsave_scheduled &&
        (server.unixtime-server.lastbgsave_try > CONFIG_BGSAVE_RETRY_DELAY ||
         server.lastbgsave_status == C_OK))
    {
2078 2079 2080
        rdbSaveInfo rsi, *rsiptr;
        rsiptr = rdbPopulateSaveInfo(&rsi);
        if (rdbSaveBackground(server.rdb_filename,rsiptr) == C_OK)
2081 2082 2083
            server.rdb_bgsave_scheduled = 0;
    }

2084 2085 2086 2087 2088 2089
    /* Fire the cron loop modules event. */
    RedisModuleCronLoopV1 ei = {REDISMODULE_CRON_LOOP_VERSION,server.hz};
    moduleFireServerEvent(REDISMODULE_EVENT_CRON_LOOP,
                          0,
                          &ei);

2090
    server.cronloops++;
2091
    return 1000/server.hz;
2092 2093
}

2094 2095
extern int ProcessingEventsWhileBlocked;

2096 2097
/* This function gets called every time Redis is entering the
 * main loop of the event driven library, that is, before to sleep
2098
 * for ready file descriptors.
A
antirez 已提交
2099
 *
2100 2101 2102
 * Note: This function is (currently) called from two functions:
 * 1. aeMain - The main server loop
 * 2. processEventsWhileBlocked - Process clients during RDB/AOF load
A
antirez 已提交
2103
 *
2104 2105 2106
 * If it was called from processEventsWhileBlocked we don't want
 * to perform all actions (For example, we don't want to expire
 * keys), but we do need to perform some actions.
A
antirez 已提交
2107
 *
2108 2109
 * The most important is freeClientsInAsyncFreeQueue but we also
 * call some other low-risk functions. */
2110
void beforeSleep(struct aeEventLoop *eventLoop) {
A
antirez 已提交
2111
    UNUSED(eventLoop);
2112

A
antirez 已提交
2113
    /* Just call a subset of vital functions in case we are re-entering
2114 2115 2116 2117
     * the event loop from processEventsWhileBlocked(). Note that in this
     * case we keep track of the number of events we are processing, since
     * processEventsWhileBlocked() wants to stop ASAP if there are no longer
     * events to handle. */
A
antirez 已提交
2118
    if (ProcessingEventsWhileBlocked) {
2119 2120 2121 2122 2123 2124
        uint64_t processed = 0;
        processed += handleClientsWithPendingReadsUsingThreads();
        processed += tlsProcessPendingData();
        processed += handleClientsWithPendingWrites();
        processed += freeClientsInAsyncFreeQueue();
        server.events_processed_while_blocked += processed;
A
antirez 已提交
2125
        return;
2126
    }
2127 2128

    /* Handle precise timeouts of blocked clients. */
2129
    handleBlockedClientsTimeout();
2130

2131 2132 2133
    /* We should handle pending reads clients ASAP after event loop. */
    handleClientsWithPendingReadsUsingThreads();

2134 2135
    /* Handle TLS pending data. (must be done before flushAppendOnlyFile) */
    tlsProcessPendingData();
2136

2137
    /* If tls still has pending unread data don't sleep at all. */
2138
    aeSetDontWait(server.el, tlsHasPendingData());
2139

2140
    /* Call the Redis Cluster before sleep function. Note that this function
A
antirez 已提交
2141
     * may change the state of Redis Cluster (from ok to fail or vice versa),
2142 2143 2144 2145
     * so it's a good idea to call it before serving the unblocked clients
     * later in this function. */
    if (server.cluster_enabled) clusterBeforeSleep();

2146 2147 2148 2149
    /* Run a fast expire cycle (the called function will return
     * ASAP if a fast cycle is not needed). */
    if (server.active_expire_enabled && server.masterhost == NULL)
        activeExpireCycle(ACTIVE_EXPIRE_CYCLE_FAST);
2150

S
srzhao 已提交
2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163
    /* Unblock all the clients blocked for synchronous replication
     * in WAIT. */
    if (listLength(server.clients_waiting_acks))
        processClientsWaitingReplicas();

    /* Check if there are clients unblocked by modules that implement
     * blocking commands. */
    if (moduleCount()) moduleHandleBlockedClients();

    /* Try to process pending commands for clients that were just unblocked. */
    if (listLength(server.unblocked_clients))
        processUnblockedClients();

2164
    /* Send all the slaves an ACK request if at least one client blocked
2165 2166 2167 2168
     * during the previous event loop iteration. Note that we do this after
     * processUnblockedClients(), so if there are multiple pipelined WAITs
     * and the just unblocked WAIT gets blocked again, we don't have to wait
     * a server cron cycle in absence of other event loop events. See #6623. */
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181
    if (server.get_ack_from_slaves) {
        robj *argv[3];

        argv[0] = createStringObject("REPLCONF",8);
        argv[1] = createStringObject("GETACK",6);
        argv[2] = createStringObject("*",1); /* Not used argument. */
        replicationFeedSlaves(server.slaves, server.slaveseldb, argv, 3);
        decrRefCount(argv[0]);
        decrRefCount(argv[1]);
        decrRefCount(argv[2]);
        server.get_ack_from_slaves = 0;
    }

2182 2183 2184 2185
    /* Send the invalidation messages to clients participating to the
     * client side caching protocol in broadcasting (BCAST) mode. */
    trackingBroadcastInvalidationMessages();

2186
    /* Write the AOF buffer on disk */
2187
    flushAppendOnlyFile(0);
2188 2189

    /* Handle writes with pending output buffers. */
2190 2191 2192 2193
    handleClientsWithPendingWritesUsingThreads();

    /* Close clients that need to be closed asynchronous */
    freeClientsInAsyncFreeQueue();
2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205

    /* Before we are going to sleep, let the threads access the dataset by
     * releasing the GIL. Redis main thread will not touch anything at this
     * time. */
    if (moduleCount()) moduleReleaseGIL();
}

/* This function is called immadiately after the event loop multiplexing
 * API returned, and the control is going to soon return to Redis by invoking
 * the different events callbacks. */
void afterSleep(struct aeEventLoop *eventLoop) {
    UNUSED(eventLoop);
2206 2207 2208 2209

    if (!ProcessingEventsWhileBlocked) {
        if (moduleCount()) moduleAcquireGIL();
    }
2210 2211 2212 2213 2214 2215 2216
}

/* =========================== Server initialization ======================== */

void createSharedObjects(void) {
    int j;

2217 2218 2219 2220 2221 2222
    shared.crlf = createObject(OBJ_STRING,sdsnew("\r\n"));
    shared.ok = createObject(OBJ_STRING,sdsnew("+OK\r\n"));
    shared.err = createObject(OBJ_STRING,sdsnew("-ERR\r\n"));
    shared.emptybulk = createObject(OBJ_STRING,sdsnew("$0\r\n\r\n"));
    shared.czero = createObject(OBJ_STRING,sdsnew(":0\r\n"));
    shared.cone = createObject(OBJ_STRING,sdsnew(":1\r\n"));
2223
    shared.emptyarray = createObject(OBJ_STRING,sdsnew("*0\r\n"));
2224 2225 2226 2227
    shared.pong = createObject(OBJ_STRING,sdsnew("+PONG\r\n"));
    shared.queued = createObject(OBJ_STRING,sdsnew("+QUEUED\r\n"));
    shared.emptyscan = createObject(OBJ_STRING,sdsnew("*2\r\n$1\r\n0\r\n*0\r\n"));
    shared.wrongtypeerr = createObject(OBJ_STRING,sdsnew(
2228
        "-WRONGTYPE Operation against a key holding the wrong kind of value\r\n"));
2229
    shared.nokeyerr = createObject(OBJ_STRING,sdsnew(
2230
        "-ERR no such key\r\n"));
2231
    shared.syntaxerr = createObject(OBJ_STRING,sdsnew(
2232
        "-ERR syntax error\r\n"));
2233
    shared.sameobjecterr = createObject(OBJ_STRING,sdsnew(
2234
        "-ERR source and destination objects are the same\r\n"));
2235
    shared.outofrangeerr = createObject(OBJ_STRING,sdsnew(
2236
        "-ERR index out of range\r\n"));
2237
    shared.noscripterr = createObject(OBJ_STRING,sdsnew(
A
antirez 已提交
2238
        "-NOSCRIPT No matching script. Please use EVAL.\r\n"));
2239
    shared.loadingerr = createObject(OBJ_STRING,sdsnew(
2240
        "-LOADING Redis is loading the dataset in memory\r\n"));
2241
    shared.slowscripterr = createObject(OBJ_STRING,sdsnew(
2242
        "-BUSY Redis is busy running a script. You can only call SCRIPT KILL or SHUTDOWN NOSAVE.\r\n"));
2243
    shared.masterdownerr = createObject(OBJ_STRING,sdsnew(
A
antirez 已提交
2244
        "-MASTERDOWN Link with MASTER is down and replica-serve-stale-data is set to 'no'.\r\n"));
2245
    shared.bgsaveerr = createObject(OBJ_STRING,sdsnew(
2246
        "-MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.\r\n"));
2247
    shared.roslaveerr = createObject(OBJ_STRING,sdsnew(
A
antirez 已提交
2248
        "-READONLY You can't write against a read only replica.\r\n"));
2249
    shared.noautherr = createObject(OBJ_STRING,sdsnew(
2250
        "-NOAUTH Authentication required.\r\n"));
2251
    shared.oomerr = createObject(OBJ_STRING,sdsnew(
2252
        "-OOM command not allowed when used memory > 'maxmemory'.\r\n"));
2253
    shared.execaborterr = createObject(OBJ_STRING,sdsnew(
A
antirez 已提交
2254
        "-EXECABORT Transaction discarded because of previous errors.\r\n"));
2255
    shared.noreplicaserr = createObject(OBJ_STRING,sdsnew(
A
antirez 已提交
2256
        "-NOREPLICAS Not enough good replicas to write.\r\n"));
2257
    shared.busykeyerr = createObject(OBJ_STRING,sdsnew(
2258
        "-BUSYKEY Target key name already exists.\r\n"));
2259 2260 2261
    shared.space = createObject(OBJ_STRING,sdsnew(" "));
    shared.colon = createObject(OBJ_STRING,sdsnew(":"));
    shared.plus = createObject(OBJ_STRING,sdsnew("+"));
2262

A
antirez 已提交
2263 2264 2265
    /* The shared NULL depends on the protocol version. */
    shared.null[0] = NULL;
    shared.null[1] = NULL;
2266
    shared.null[2] = createObject(OBJ_STRING,sdsnew("$-1\r\n"));
A
antirez 已提交
2267 2268
    shared.null[3] = createObject(OBJ_STRING,sdsnew("_\r\n"));

2269 2270 2271 2272 2273
    shared.nullarray[0] = NULL;
    shared.nullarray[1] = NULL;
    shared.nullarray[2] = createObject(OBJ_STRING,sdsnew("*-1\r\n"));
    shared.nullarray[3] = createObject(OBJ_STRING,sdsnew("_\r\n"));

2274 2275 2276 2277 2278 2279 2280 2281 2282 2283
    shared.emptymap[0] = NULL;
    shared.emptymap[1] = NULL;
    shared.emptymap[2] = createObject(OBJ_STRING,sdsnew("*0\r\n"));
    shared.emptymap[3] = createObject(OBJ_STRING,sdsnew("%0\r\n"));

    shared.emptyset[0] = NULL;
    shared.emptyset[1] = NULL;
    shared.emptyset[2] = createObject(OBJ_STRING,sdsnew("*0\r\n"));
    shared.emptyset[3] = createObject(OBJ_STRING,sdsnew("~0\r\n"));

A
antirez 已提交
2284
    for (j = 0; j < PROTO_SHARED_SELECT_CMDS; j++) {
2285 2286 2287 2288
        char dictid_str[64];
        int dictid_len;

        dictid_len = ll2string(dictid_str,sizeof(dictid_str),j);
2289
        shared.select[j] = createObject(OBJ_STRING,
2290 2291 2292
            sdscatprintf(sdsempty(),
                "*2\r\n$6\r\nSELECT\r\n$%d\r\n%s\r\n",
                dictid_len, dictid_str));
2293
    }
2294 2295 2296 2297 2298 2299
    shared.messagebulk = createStringObject("$7\r\nmessage\r\n",13);
    shared.pmessagebulk = createStringObject("$8\r\npmessage\r\n",14);
    shared.subscribebulk = createStringObject("$9\r\nsubscribe\r\n",15);
    shared.unsubscribebulk = createStringObject("$11\r\nunsubscribe\r\n",18);
    shared.psubscribebulk = createStringObject("$10\r\npsubscribe\r\n",17);
    shared.punsubscribebulk = createStringObject("$12\r\npunsubscribe\r\n",19);
2300
    shared.del = createStringObject("DEL",3);
2301
    shared.unlink = createStringObject("UNLINK",6);
2302 2303
    shared.rpop = createStringObject("RPOP",4);
    shared.lpop = createStringObject("LPOP",4);
2304
    shared.lpush = createStringObject("LPUSH",5);
2305
    shared.rpoplpush = createStringObject("RPOPLPUSH",9);
2306 2307
    shared.zpopmin = createStringObject("ZPOPMIN",7);
    shared.zpopmax = createStringObject("ZPOPMAX",7);
2308 2309
    shared.multi = createStringObject("MULTI",5);
    shared.exec = createStringObject("EXEC",4);
A
antirez 已提交
2310
    for (j = 0; j < OBJ_SHARED_INTEGERS; j++) {
2311 2312
        shared.integers[j] =
            makeObjectShared(createObject(OBJ_STRING,(void*)(long)j));
2313
        shared.integers[j]->encoding = OBJ_ENCODING_INT;
2314
    }
A
antirez 已提交
2315
    for (j = 0; j < OBJ_SHARED_BULKHDR_LEN; j++) {
2316
        shared.mbulkhdr[j] = createObject(OBJ_STRING,
2317
            sdscatprintf(sdsempty(),"*%d\r\n",j));
2318
        shared.bulkhdr[j] = createObject(OBJ_STRING,
2319 2320
            sdscatprintf(sdsempty(),"$%d\r\n",j));
    }
2321 2322 2323 2324
    /* The following two shared objects, minstring and maxstrings, are not
     * actually used for their value but as a special object meaning
     * respectively the minimum possible string and the maximum possible
     * string in string comparisons for the ZRANGEBYLEX command. */
2325 2326
    shared.minstring = sdsnew("minstring");
    shared.maxstring = sdsnew("maxstring");
2327 2328
}

2329
void initServerConfig(void) {
2330 2331
    int j;

A
antirez 已提交
2332
    updateCachedTime(1);
A
antirez 已提交
2333
    getRandomHexChars(server.runid,CONFIG_RUN_ID_SIZE);
2334 2335
    server.runid[CONFIG_RUN_ID_SIZE] = '\0';
    changeReplicationId();
A
antirez 已提交
2336
    clearReplicationId2();
A
antirez 已提交
2337 2338 2339 2340
    server.hz = CONFIG_DEFAULT_HZ; /* Initialize it ASAP, even if it may get
                                      updated later after loading the config.
                                      This value may be used before the server
                                      is initialized. */
D
David Carlier 已提交
2341
    server.timezone = getTimeZone(); /* Initialized by tzset(). */
2342
    server.configfile = NULL;
A
antirez 已提交
2343
    server.executable = NULL;
2344
    server.arch_bits = (sizeof(long) == 8) ? 64 : 32;
A
antirez 已提交
2345
    server.bindaddr_count = 0;
2346
    server.unixsocketperm = CONFIG_DEFAULT_UNIX_SOCKET_PERM;
2347
    server.ipfd_count = 0;
2348
    server.tlsfd_count = 0;
2349
    server.sofd = -1;
A
antirez 已提交
2350
    server.active_expire_enabled = 1;
A
antirez 已提交
2351
    server.client_max_querybuf_len = PROTO_MAX_QUERYBUF_LEN;
2352
    server.saveparams = NULL;
2353
    server.loading = 0;
2354
    server.logfile = zstrdup(CONFIG_DEFAULT_LOGFILE);
A
antirez 已提交
2355
    server.aof_state = AOF_OFF;
2356 2357
    server.aof_rewrite_base_size = 0;
    server.aof_rewrite_scheduled = 0;
2358
    server.aof_flush_sleep = 0;
A
antirez 已提交
2359
    server.aof_last_fsync = time(NULL);
2360 2361
    server.aof_rewrite_time_last = -1;
    server.aof_rewrite_time_start = -1;
2362
    server.aof_lastbgrewrite_status = C_OK;
2363
    server.aof_delayed_fsync = 0;
A
antirez 已提交
2364 2365
    server.aof_fd = -1;
    server.aof_selected_db = -1; /* Make sure the first time will not match */
2366
    server.aof_flush_postponed_start = 0;
2367
    server.pidfile = NULL;
O
oranagra 已提交
2368
    server.active_defrag_running = 0;
A
antirez 已提交
2369
    server.notify_keyspace_events = 0;
A
antirez 已提交
2370 2371 2372
    server.blocked_clients = 0;
    memset(server.blocked_clients_by_type,0,
           sizeof(server.blocked_clients_by_type));
2373
    server.shutdown_asap = 0;
2374
    server.cluster_configfile = zstrdup(CONFIG_DEFAULT_CLUSTER_CONFIG_FILE);
2375
    server.cluster_module_flags = CLUSTER_MODULE_FLAG_NONE;
A
antirez 已提交
2376
    server.migrate_cached_sockets = dictCreate(&migrateCacheDictType,NULL);
2377
    server.next_client_id = 1; /* Client IDs, start from 1 .*/
2378
    server.loading_process_events_interval_bytes = (1024*1024*2);
2379

A
antirez 已提交
2380
    server.lruclock = getLRUClock();
2381 2382 2383 2384 2385
    resetServerSaveParams();

    appendServerSaveParams(60*60,1);  /* save after 1 hour and 1 change */
    appendServerSaveParams(300,100);  /* save after 5 minutes and 100 changes */
    appendServerSaveParams(60,10000); /* save after 1 minute and 10000 changes */
2386

2387 2388 2389 2390 2391
    /* Replication related */
    server.masterauth = NULL;
    server.masterhost = NULL;
    server.masterport = 6379;
    server.master = NULL;
2392
    server.cached_master = NULL;
2393
    server.master_initial_offset = -1;
A
antirez 已提交
2394
    server.repl_state = REPL_STATE_NONE;
2395 2396
    server.repl_transfer_tmpfile = NULL;
    server.repl_transfer_fd = -1;
2397
    server.repl_transfer_s = NULL;
A
antirez 已提交
2398
    server.repl_syncio_timeout = CONFIG_REPL_SYNCIO_TIMEOUT;
2399
    server.repl_down_since = 0; /* Never connected, repl is down since EVER. */
2400 2401 2402 2403 2404 2405 2406 2407
    server.master_repl_offset = 0;

    /* Replication partial resync backlog */
    server.repl_backlog = NULL;
    server.repl_backlog_histlen = 0;
    server.repl_backlog_idx = 0;
    server.repl_backlog_off = 0;
    server.repl_no_slaves_since = time(NULL);
2408

2409
    /* Client output buffer limits */
A
antirez 已提交
2410
    for (j = 0; j < CLIENT_TYPE_OBUF_COUNT; j++)
2411
        server.client_obuf_limits[j] = clientBufferLimitsDefaults[j];
2412

2413 2414 2415 2416 2417
    /* Double constants initialization */
    R_Zero = 0.0;
    R_PosInf = 1.0/R_Zero;
    R_NegInf = -1.0/R_Zero;
    R_Nan = R_Zero/R_Zero;
2418

G
guiquanz 已提交
2419
    /* Command table -- we initiialize it here as it is part of the
2420 2421 2422
     * initial configuration, since command names may be changed via
     * redis.conf using the rename-command directive. */
    server.commands = dictCreate(&commandTableDictType,NULL);
2423
    server.orig_commands = dictCreate(&commandTableDictType,NULL);
2424 2425 2426
    populateCommandTable();
    server.delCommand = lookupCommandByCString("del");
    server.multiCommand = lookupCommandByCString("multi");
2427
    server.lpushCommand = lookupCommandByCString("lpush");
2428 2429
    server.lpopCommand = lookupCommandByCString("lpop");
    server.rpopCommand = lookupCommandByCString("rpop");
2430 2431
    server.zpopminCommand = lookupCommandByCString("zpopmin");
    server.zpopmaxCommand = lookupCommandByCString("zpopmax");
2432
    server.sremCommand = lookupCommandByCString("srem");
2433
    server.execCommand = lookupCommandByCString("exec");
2434 2435
    server.expireCommand = lookupCommandByCString("expire");
    server.pexpireCommand = lookupCommandByCString("pexpire");
2436
    server.xclaimCommand = lookupCommandByCString("xclaim");
2437
    server.xgroupCommand = lookupCommandByCString("xgroup");
2438
    server.rpoplpushCommand = lookupCommandByCString("rpoplpush");
2439

A
antirez 已提交
2440
    /* Debugging */
A
antirez 已提交
2441 2442 2443 2444
    server.assert_failed = "<no assertion failed>";
    server.assert_file = "<no file>";
    server.assert_line = 0;
    server.bug_report_start = 0;
A
antirez 已提交
2445
    server.watchdog_period = 0;
2446 2447 2448 2449 2450 2451

    /* By default we want scripts to be always replicated by effects
     * (single commands executed by the script), and not by sending the
     * script to the slave / AOF. This is the new way starting from
     * Redis 5. However it is possible to revert it via redis.conf. */
    server.lua_always_replicate_commands = 1;
2452 2453

    initConfigValues();
2454 2455
}

A
antirez 已提交
2456 2457 2458 2459 2460
extern char **environ;

/* Restart the server, executing the same executable that started this
 * instance, with the same arguments and configuration file.
 *
2461 2462 2463
 * The function is designed to directly call execve() so that the new
 * server instance will retain the PID of the previous one.
 *
A
antirez 已提交
2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477
 * The list of flags, that may be bitwise ORed together, alter the
 * behavior of this function:
 *
 * RESTART_SERVER_NONE              No flags.
 * RESTART_SERVER_GRACEFULLY        Do a proper shutdown before restarting.
 * RESTART_SERVER_CONFIG_REWRITE    Rewrite the config file before restarting.
 *
 * On success the function does not return, because the process turns into
 * a different process. On error C_ERR is returned. */
int restartServer(int flags, mstime_t delay) {
    int j;

    /* Check if we still have accesses to the executable that started this
     * server instance. */
2478 2479 2480 2481 2482
    if (access(server.executable,X_OK) == -1) {
        serverLog(LL_WARNING,"Can't restart: this process has no "
                             "permissions to execute %s", server.executable);
        return C_ERR;
    }
A
antirez 已提交
2483 2484 2485 2486

    /* Config rewriting. */
    if (flags & RESTART_SERVER_CONFIG_REWRITE &&
        server.configfile &&
2487 2488 2489 2490 2491 2492
        rewriteConfig(server.configfile) == -1)
    {
        serverLog(LL_WARNING,"Can't restart: configuration rewrite process "
                             "failed");
        return C_ERR;
    }
A
antirez 已提交
2493 2494 2495

    /* Perform a proper shutdown. */
    if (flags & RESTART_SERVER_GRACEFULLY &&
2496 2497 2498 2499 2500
        prepareForShutdown(SHUTDOWN_NOFLAGS) != C_OK)
    {
        serverLog(LL_WARNING,"Can't restart: error preparing for shutdown");
        return C_ERR;
    }
A
antirez 已提交
2501 2502 2503

    /* Close all file descriptors, with the exception of stdin, stdout, strerr
     * which are useful if we restart a Redis server which is not daemonized. */
2504 2505 2506 2507 2508
    for (j = 3; j < (int)server.maxclients + 1024; j++) {
        /* Test the descriptor validity before closing it, otherwise
         * Valgrind issues a warning on close(). */
        if (fcntl(j,F_GETFD) != -1) close(j);
    }
A
antirez 已提交
2509 2510 2511

    /* Execute the server with the original command line. */
    if (delay) usleep(delay*1000);
2512 2513
    zfree(server.exec_argv[0]);
    server.exec_argv[0] = zstrdup(server.executable);
A
antirez 已提交
2514 2515 2516 2517 2518 2519 2520 2521
    execve(server.executable,server.exec_argv,environ);

    /* If an error occurred here, there is nothing we can do, but exit. */
    _exit(1);

    return C_ERR; /* Never reached. */
}

2522
/* This function will try to raise the max number of open files accordingly to
2523
 * the configured max number of clients. It also reserves a number of file
A
antirez 已提交
2524
 * descriptors (CONFIG_MIN_RESERVED_FDS) for extra operations of
2525
 * persistence, listening sockets, log files and so forth.
2526 2527 2528 2529 2530
 *
 * If it will not be possible to set the limit accordingly to the configured
 * max number of clients, the function will do the reverse setting
 * server.maxclients to the value that we can actually handle. */
void adjustOpenFilesLimit(void) {
A
antirez 已提交
2531
    rlim_t maxfiles = server.maxclients+CONFIG_MIN_RESERVED_FDS;
2532 2533 2534
    struct rlimit limit;

    if (getrlimit(RLIMIT_NOFILE,&limit) == -1) {
A
antirez 已提交
2535
        serverLog(LL_WARNING,"Unable to obtain the current NOFILE limit (%s), assuming 1024 and setting the max clients configuration accordingly.",
2536
            strerror(errno));
A
antirez 已提交
2537
        server.maxclients = 1024-CONFIG_MIN_RESERVED_FDS;
2538 2539 2540 2541 2542 2543
    } else {
        rlim_t oldlimit = limit.rlim_cur;

        /* Set the max number of files if the current limit is not enough
         * for our needs. */
        if (oldlimit < maxfiles) {
2544
            rlim_t bestlimit;
A
antirez 已提交
2545 2546 2547 2548
            int setrlimit_error = 0;

            /* Try to set the file limit to match 'maxfiles' or at least
             * to the higher value supported less than maxfiles. */
2549 2550
            bestlimit = maxfiles;
            while(bestlimit > oldlimit) {
2551
                rlim_t decr_step = 16;
A
antirez 已提交
2552

2553 2554
                limit.rlim_cur = bestlimit;
                limit.rlim_max = bestlimit;
2555
                if (setrlimit(RLIMIT_NOFILE,&limit) != -1) break;
A
antirez 已提交
2556 2557
                setrlimit_error = errno;

2558
                /* We failed to set file limit to 'bestlimit'. Try with a
A
antirez 已提交
2559
                 * smaller limit decrementing by a few FDs per iteration. */
2560 2561
                if (bestlimit < decr_step) break;
                bestlimit -= decr_step;
2562
            }
A
antirez 已提交
2563 2564 2565

            /* Assume that the limit we get initially is still valid if
             * our last try was even lower. */
2566
            if (bestlimit < oldlimit) bestlimit = oldlimit;
A
antirez 已提交
2567

2568
            if (bestlimit < maxfiles) {
2569
                unsigned int old_maxclients = server.maxclients;
2570 2571 2572 2573
                server.maxclients = bestlimit-CONFIG_MIN_RESERVED_FDS;
                /* maxclients is unsigned so may overflow: in order
                 * to check if maxclients is now logically less than 1
                 * we test indirectly via bestlimit. */
2574
                if (bestlimit <= CONFIG_MIN_RESERVED_FDS) {
A
antirez 已提交
2575
                    serverLog(LL_WARNING,"Your current 'ulimit -n' "
2576
                        "of %llu is not enough for the server to start. "
2577
                        "Please increase your open file limit to at least "
A
antirez 已提交
2578 2579 2580
                        "%llu. Exiting.",
                        (unsigned long long) oldlimit,
                        (unsigned long long) maxfiles);
2581 2582
                    exit(1);
                }
A
antirez 已提交
2583
                serverLog(LL_WARNING,"You requested maxclients of %d "
2584
                    "requiring at least %llu max file descriptors.",
A
antirez 已提交
2585 2586
                    old_maxclients,
                    (unsigned long long) maxfiles);
2587
                serverLog(LL_WARNING,"Server can't set maximum open files "
2588
                    "to %llu because of OS error: %s.",
A
antirez 已提交
2589
                    (unsigned long long) maxfiles, strerror(setrlimit_error));
A
antirez 已提交
2590
                serverLog(LL_WARNING,"Current maximum open files is %llu. "
2591 2592 2593
                    "maxclients has been reduced to %d to compensate for "
                    "low ulimit. "
                    "If you need higher maxclients increase 'ulimit -n'.",
2594
                    (unsigned long long) bestlimit, server.maxclients);
2595
            } else {
A
antirez 已提交
2596
                serverLog(LL_NOTICE,"Increased maximum number of open files "
2597
                    "to %llu (it was originally set to %llu).",
A
antirez 已提交
2598 2599
                    (unsigned long long) maxfiles,
                    (unsigned long long) oldlimit);
2600 2601 2602 2603 2604
            }
        }
    }
}

2605 2606 2607 2608 2609 2610 2611 2612 2613 2614
/* Check that server.tcp_backlog can be actually enforced in Linux according
 * to the value of /proc/sys/net/core/somaxconn, or warn about it. */
void checkTcpBacklogSettings(void) {
#ifdef HAVE_PROC_SOMAXCONN
    FILE *fp = fopen("/proc/sys/net/core/somaxconn","r");
    char buf[1024];
    if (!fp) return;
    if (fgets(buf,sizeof(buf),fp) != NULL) {
        int somaxconn = atoi(buf);
        if (somaxconn > 0 && somaxconn < server.tcp_backlog) {
A
antirez 已提交
2615
            serverLog(LL_WARNING,"WARNING: The TCP backlog setting of %d cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of %d.", server.tcp_backlog, somaxconn);
2616 2617 2618 2619 2620 2621
        }
    }
    fclose(fp);
#endif
}

2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632
/* Initialize a set of file descriptors to listen to the specified 'port'
 * binding the addresses specified in the Redis server configuration.
 *
 * The listening file descriptors are stored in the integer array 'fds'
 * and their number is set in '*count'.
 *
 * The addresses to bind are specified in the global server.bindaddr array
 * and their number is server.bindaddr_count. If the server configuration
 * contains no specific addresses to bind, this function will try to
 * bind * (all addresses) for both the IPv4 and IPv6 protocols.
 *
2633
 * On success the function returns C_OK.
2634
 *
2635
 * On error the function returns C_ERR. For the function to be on
2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647
 * error, at least one of the server.bindaddr addresses was
 * impossible to bind, or no bind addresses were specified in the server
 * configuration but the function is not able to bind * for at least
 * one of the IPv4 or IPv6 protocols. */
int listenToPort(int port, int *fds, int *count) {
    int j;

    /* Force binding of 0.0.0.0 if no bind address is specified, always
     * entering the loop if j == 0. */
    if (server.bindaddr_count == 0) server.bindaddr[0] = NULL;
    for (j = 0; j < server.bindaddr_count || j == 0; j++) {
        if (server.bindaddr[j] == NULL) {
2648
            int unsupported = 0;
2649 2650
            /* Bind * for both IPv6 and IPv4, we enter here only if
             * server.bindaddr_count == 0. */
2651 2652
            fds[*count] = anetTcp6Server(server.neterr,port,NULL,
                server.tcp_backlog);
A
antirez 已提交
2653 2654 2655
            if (fds[*count] != ANET_ERR) {
                anetNonBlock(NULL,fds[*count]);
                (*count)++;
A
antirez 已提交
2656
            } else if (errno == EAFNOSUPPORT) {
2657
                unsupported++;
2658
                serverLog(LL_WARNING,"Not listening to IPv6: unsupported");
A
antirez 已提交
2659
            }
2660

A
antirez 已提交
2661 2662 2663 2664 2665 2666 2667 2668 2669
            if (*count == 1 || unsupported) {
                /* Bind the IPv4 address as well. */
                fds[*count] = anetTcpServer(server.neterr,port,NULL,
                    server.tcp_backlog);
                if (fds[*count] != ANET_ERR) {
                    anetNonBlock(NULL,fds[*count]);
                    (*count)++;
                } else if (errno == EAFNOSUPPORT) {
                    unsupported++;
2670
                    serverLog(LL_WARNING,"Not listening to IPv4: unsupported");
A
antirez 已提交
2671 2672
                }
            }
2673
            /* Exit the loop if we were able to bind * on IPv4 and IPv6,
2674 2675
             * otherwise fds[*count] will be ANET_ERR and we'll print an
             * error and return to the caller with an error. */
2676
            if (*count + unsupported == 2) break;
2677 2678
        } else if (strchr(server.bindaddr[j],':')) {
            /* Bind IPv6 address. */
2679 2680
            fds[*count] = anetTcp6Server(server.neterr,port,server.bindaddr[j],
                server.tcp_backlog);
2681 2682
        } else {
            /* Bind IPv4 address. */
2683 2684
            fds[*count] = anetTcpServer(server.neterr,port,server.bindaddr[j],
                server.tcp_backlog);
2685 2686
        }
        if (fds[*count] == ANET_ERR) {
A
antirez 已提交
2687
            serverLog(LL_WARNING,
2688
                "Could not create server TCP listening socket %s:%d: %s",
2689
                server.bindaddr[j] ? server.bindaddr[j] : "*",
2690
                port, server.neterr);
2691 2692 2693 2694
                if (errno == ENOPROTOOPT     || errno == EPROTONOSUPPORT ||
                    errno == ESOCKTNOSUPPORT || errno == EPFNOSUPPORT ||
                    errno == EAFNOSUPPORT    || errno == EADDRNOTAVAIL)
                    continue;
2695
            return C_ERR;
2696
        }
A
antirez 已提交
2697
        anetNonBlock(NULL,fds[*count]);
2698 2699
        (*count)++;
    }
2700
    return C_OK;
2701 2702
}

2703 2704 2705 2706
/* Resets the stats that we expose via INFO or other means that we want
 * to reset via CONFIG RESETSTAT. The function is also used in order to
 * initialize these fields in initServer() at server startup. */
void resetServerStats(void) {
2707 2708
    int j;

2709 2710 2711
    server.stat_numcommands = 0;
    server.stat_numconnections = 0;
    server.stat_expiredkeys = 0;
2712 2713
    server.stat_expired_stale_perc = 0;
    server.stat_expired_time_cap_reached_count = 0;
2714
    server.stat_expire_cycle_time_used = 0;
2715 2716 2717
    server.stat_evictedkeys = 0;
    server.stat_keyspace_misses = 0;
    server.stat_keyspace_hits = 0;
O
oranagra 已提交
2718 2719 2720 2721
    server.stat_active_defrag_hits = 0;
    server.stat_active_defrag_misses = 0;
    server.stat_active_defrag_key_hits = 0;
    server.stat_active_defrag_key_misses = 0;
O
Oran Agra 已提交
2722
    server.stat_active_defrag_scanned = 0;
2723
    server.stat_fork_time = 0;
2724
    server.stat_fork_rate = 0;
2725 2726 2727 2728
    server.stat_rejected_conn = 0;
    server.stat_sync_full = 0;
    server.stat_sync_partial_ok = 0;
    server.stat_sync_partial_err = 0;
A
antirez 已提交
2729
    for (j = 0; j < STATS_METRIC_COUNT; j++) {
2730 2731 2732 2733 2734 2735 2736 2737
        server.inst_metric[j].idx = 0;
        server.inst_metric[j].last_sample_time = mstime();
        server.inst_metric[j].last_sample_count = 0;
        memset(server.inst_metric[j].samples,0,
            sizeof(server.inst_metric[j].samples));
    }
    server.stat_net_input_bytes = 0;
    server.stat_net_output_bytes = 0;
2738
    server.stat_unexpected_error_replies = 0;
T
Tom Kiemes 已提交
2739
    server.aof_delayed_fsync = 0;
2740 2741
}

2742
void initServer(void) {
2743 2744 2745 2746
    int j;

    signal(SIGHUP, SIG_IGN);
    signal(SIGPIPE, SIG_IGN);
2747
    setupSignalHandlers();
2748

J
Jonah H. Harris 已提交
2749 2750 2751 2752 2753
    if (server.syslog_enabled) {
        openlog(server.syslog_ident, LOG_PID | LOG_NDELAY | LOG_NOWAIT,
            server.syslog_facility);
    }

2754 2755
    /* Initialization after setting defaults from the config system. */
    server.aof_state = server.aof_enabled ? AOF_ON : AOF_OFF;
2756
    server.hz = server.config_hz;
A
antirez 已提交
2757
    server.pid = getpid();
2758
    server.current_client = NULL;
2759
    server.fixed_time_expire = 0;
2760
    server.clients = listCreate();
2761
    server.clients_index = raxNew();
2762
    server.clients_to_close = listCreate();
2763 2764
    server.slaves = listCreate();
    server.monitors = listCreate();
2765
    server.clients_pending_write = listCreate();
A
antirez 已提交
2766
    server.clients_pending_read = listCreate();
2767
    server.clients_timeout_table = raxNew();
2768
    server.slaveseldb = -1; /* Force to emit the first SELECT command. */
2769
    server.unblocked_clients = listCreate();
2770
    server.ready_keys = listCreate();
2771 2772
    server.clients_waiting_acks = listCreate();
    server.get_ack_from_slaves = 0;
2773
    server.clients_paused = 0;
2774
    server.events_processed_while_blocked = 0;
2775
    server.system_memory_size = zmalloc_get_memory_size();
2776

Y
Yossi Gottlieb 已提交
2777
    if (server.tls_port && tlsConfigure(&server.tls_ctx_config) == C_ERR) {
2778 2779 2780 2781
        serverLog(LL_WARNING, "Failed to configure TLS. Check logs for more info.");
        exit(1);
    }

2782
    createSharedObjects();
2783
    adjustOpenFilesLimit();
A
antirez 已提交
2784
    server.el = aeCreateEventLoop(server.maxclients+CONFIG_FDSET_INCR);
2785 2786 2787 2788 2789 2790
    if (server.el == NULL) {
        serverLog(LL_WARNING,
            "Failed creating the event loop. Error message: '%s'",
            strerror(errno));
        exit(1);
    }
2791
    server.db = zmalloc(sizeof(redisDb)*server.dbnum);
2792

2793
    /* Open the TCP listening socket for the user commands. */
2794
    if (server.port != 0 &&
2795
        listenToPort(server.port,server.ipfd,&server.ipfd_count) == C_ERR)
2796
        exit(1);
2797 2798 2799
    if (server.tls_port != 0 &&
        listenToPort(server.tls_port,server.tlsfd,&server.tlsfd_count) == C_ERR)
        exit(1);
2800 2801

    /* Open the listening Unix domain socket. */
2802 2803
    if (server.unixsocket != NULL) {
        unlink(server.unixsocket); /* don't care if this fails */
2804 2805
        server.sofd = anetUnixServer(server.neterr,server.unixsocket,
            server.unixsocketperm, server.tcp_backlog);
2806
        if (server.sofd == ANET_ERR) {
A
antirez 已提交
2807
            serverLog(LL_WARNING, "Opening Unix socket: %s", server.neterr);
2808 2809
            exit(1);
        }
A
antirez 已提交
2810
        anetNonBlock(NULL,server.sofd);
2811
    }
2812 2813

    /* Abort if there are no listening sockets at all. */
2814
    if (server.ipfd_count == 0 && server.tlsfd_count == 0 && server.sofd < 0) {
A
antirez 已提交
2815
        serverLog(LL_WARNING, "Configured to not listen anywhere, exiting.");
2816 2817
        exit(1);
    }
2818 2819

    /* Create the Redis databases, and initialize other internal state. */
2820 2821 2822
    for (j = 0; j < server.dbnum; j++) {
        server.db[j].dict = dictCreate(&dbDictType,NULL);
        server.db[j].expires = dictCreate(&keyptrDictType,NULL);
2823
        server.db[j].expires_cursor = 0;
2824
        server.db[j].blocking_keys = dictCreate(&keylistDictType,NULL);
2825
        server.db[j].ready_keys = dictCreate(&objectKeyPointerValueDictType,NULL);
2826 2827
        server.db[j].watched_keys = dictCreate(&keylistDictType,NULL);
        server.db[j].id = j;
2828
        server.db[j].avg_ttl = 0;
O
Oran Agra 已提交
2829
        server.db[j].defrag_later = listCreate();
2830
        listSetFreeMethod(server.db[j].defrag_later,(void (*)(void*))sdsfree);
2831
    }
2832
    evictionPoolAlloc(); /* Initialize the LRU keys pool. */
2833 2834
    server.pubsub_channels = dictCreate(&keylistDictType,NULL);
    server.pubsub_patterns = listCreate();
2835
    server.pubsub_patterns_dict = dictCreate(&keylistDictType,NULL);
2836 2837 2838
    listSetFreeMethod(server.pubsub_patterns,freePubsubPattern);
    listSetMatchMethod(server.pubsub_patterns,listMatchPubsubPattern);
    server.cronloops = 0;
A
antirez 已提交
2839
    server.rdb_child_pid = -1;
A
antirez 已提交
2840
    server.aof_child_pid = -1;
O
Oran Agra 已提交
2841
    server.module_child_pid = -1;
A
antirez 已提交
2842
    server.rdb_child_type = RDB_CHILD_TYPE_NONE;
2843 2844 2845 2846 2847
    server.rdb_pipe_conns = NULL;
    server.rdb_pipe_numconns = 0;
    server.rdb_pipe_numconns_writing = 0;
    server.rdb_pipe_buff = NULL;
    server.rdb_pipe_bufflen = 0;
2848
    server.rdb_bgsave_scheduled = 0;
2849 2850 2851
    server.child_info_pipe[0] = -1;
    server.child_info_pipe[1] = -1;
    server.child_info_data.magic = 0;
2852
    aofRewriteBufferReset();
A
antirez 已提交
2853
    server.aof_buf = sdsempty();
2854 2855
    server.lastsave = time(NULL); /* At startup we consider the DB saved. */
    server.lastbgsave_try = 0;    /* At startup we never tried to BGSAVE. */
2856 2857
    server.rdb_save_time_last = -1;
    server.rdb_save_time_start = -1;
2858
    server.dirty = 0;
2859 2860
    resetServerStats();
    /* A few stats we don't want to reset: server startup time, and peak mem. */
2861
    server.stat_starttime = time(NULL);
2862
    server.stat_peak_memory = 0;
2863 2864
    server.stat_rdb_cow_bytes = 0;
    server.stat_aof_cow_bytes = 0;
O
Oran Agra 已提交
2865
    server.stat_module_cow_bytes = 0;
2866 2867
    for (int j = 0; j < CLIENT_TYPE_COUNT; j++)
        server.stat_clients_type_memory[j] = 0;
2868 2869 2870 2871 2872
    server.cron_malloc_stats.zmalloc_used = 0;
    server.cron_malloc_stats.process_rss = 0;
    server.cron_malloc_stats.allocator_allocated = 0;
    server.cron_malloc_stats.allocator_active = 0;
    server.cron_malloc_stats.allocator_resident = 0;
2873 2874
    server.lastbgsave_status = C_OK;
    server.aof_last_write_status = C_OK;
2875
    server.aof_last_write_errno = 0;
2876
    server.repl_good_slaves_count = 0;
2877

2878 2879 2880
    /* Create the timer callback, this is our way to process many background
     * operations incrementally, like clients timeout, eviction of unaccessed
     * expired keys and so forth. */
2881
    if (aeCreateTimeEvent(server.el, 1, serverCron, NULL, NULL) == AE_ERR) {
2882
        serverPanic("Can't create event loop timers.");
stamhe's avatar
stamhe 已提交
2883 2884
        exit(1);
    }
2885 2886 2887 2888 2889 2890 2891

    /* Create an event handler for accepting new connections in TCP and Unix
     * domain sockets. */
    for (j = 0; j < server.ipfd_count; j++) {
        if (aeCreateFileEvent(server.el, server.ipfd[j], AE_READABLE,
            acceptTcpHandler,NULL) == AE_ERR)
            {
A
antirez 已提交
2892
                serverPanic(
2893 2894 2895
                    "Unrecoverable error creating server.ipfd file event.");
            }
    }
2896 2897 2898 2899 2900 2901 2902 2903
    for (j = 0; j < server.tlsfd_count; j++) {
        if (aeCreateFileEvent(server.el, server.tlsfd[j], AE_READABLE,
            acceptTLSHandler,NULL) == AE_ERR)
            {
                serverPanic(
                    "Unrecoverable error creating server.tlsfd file event.");
            }
    }
2904
    if (server.sofd > 0 && aeCreateFileEvent(server.el,server.sofd,AE_READABLE,
A
antirez 已提交
2905
        acceptUnixHandler,NULL) == AE_ERR) serverPanic("Unrecoverable error creating server.sofd file event.");
2906

2907 2908 2909 2910 2911 2912 2913 2914 2915 2916

    /* Register a readable event for the pipe used to awake the event loop
     * when a blocked client in a module needs attention. */
    if (aeCreateFileEvent(server.el, server.module_blocked_pipe[0], AE_READABLE,
        moduleBlockedClientPipeReadable,NULL) == AE_ERR) {
            serverPanic(
                "Error registering the readable event for the module "
                "blocked clients subsystem.");
    }

2917 2918 2919 2920 2921
    /* Register before and after sleep handlers (note this needs to be done
     * before loading persistence since it is used by processEventsWhileBlocked. */
    aeSetBeforeSleepProc(server.el,beforeSleep);
    aeSetAfterSleepProc(server.el,afterSleep);

2922
    /* Open the AOF file if needed. */
A
antirez 已提交
2923
    if (server.aof_state == AOF_ON) {
A
antirez 已提交
2924
        server.aof_fd = open(server.aof_filename,
2925
                               O_WRONLY|O_APPEND|O_CREAT,0644);
A
antirez 已提交
2926
        if (server.aof_fd == -1) {
A
antirez 已提交
2927
            serverLog(LL_WARNING, "Can't open the append-only file: %s",
2928 2929 2930 2931 2932
                strerror(errno));
            exit(1);
        }
    }

2933 2934
    /* 32 bit instances are limited to 4GB of address space, so if there is
     * no explicit limit in the user provided configuration we set a limit
2935 2936
     * at 3 GB using maxmemory with 'noeviction' policy'. This avoids
     * useless crashes of the Redis instance for out of memory. */
2937
    if (server.arch_bits == 32 && server.maxmemory == 0) {
A
antirez 已提交
2938
        serverLog(LL_WARNING,"Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with 'noeviction' policy now.");
2939
        server.maxmemory = 3072LL*(1024*1024); /* 3 GB */
A
antirez 已提交
2940
        server.maxmemory_policy = MAXMEMORY_NO_EVICTION;
2941 2942
    }

A
antirez 已提交
2943
    if (server.cluster_enabled) clusterInit();
2944
    replicationScriptCacheInit();
2945
    scriptingInit(1);
2946
    slowlogInit();
2947
    latencyMonitorInit();
2948 2949 2950 2951 2952 2953 2954 2955
}

/* Some steps in server initialization need to be done last (after modules
 * are loaded).
 * Specifically, creation of threads due to a race bug in ld.so, in which
 * Thread Local Storage initialization collides with dlopen call.
 * see: https://sourceware.org/bugzilla/show_bug.cgi?id=19329 */
void InitServerLast() {
2956
    bioInit();
2957
    initThreadedIO();
2958
    set_jemalloc_bg_thread(server.jemalloc_bg_thread);
2959
    server.initial_memory_usage = zmalloc_used_memory();
2960 2961
}

2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974
/* Parse the flags string description 'strflags' and set them to the
 * command 'c'. If the flags are all valid C_OK is returned, otherwise
 * C_ERR is returned (yet the recognized flags are set in the command). */
int populateCommandTableParseFlags(struct redisCommand *c, char *strflags) {
    int argc;
    sds *argv;

    /* Split the line into arguments for processing. */
    argv = sdssplitargs(strflags,&argc);
    if (argv == NULL) return C_ERR;

    for (int j = 0; j < argc; j++) {
        char *flag = argv[j];
2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996
        if (!strcasecmp(flag,"write")) {
            c->flags |= CMD_WRITE|CMD_CATEGORY_WRITE;
        } else if (!strcasecmp(flag,"read-only")) {
            c->flags |= CMD_READONLY|CMD_CATEGORY_READ;
        } else if (!strcasecmp(flag,"use-memory")) {
            c->flags |= CMD_DENYOOM;
        } else if (!strcasecmp(flag,"admin")) {
            c->flags |= CMD_ADMIN|CMD_CATEGORY_ADMIN|CMD_CATEGORY_DANGEROUS;
        } else if (!strcasecmp(flag,"pub-sub")) {
            c->flags |= CMD_PUBSUB|CMD_CATEGORY_PUBSUB;
        } else if (!strcasecmp(flag,"no-script")) {
            c->flags |= CMD_NOSCRIPT;
        } else if (!strcasecmp(flag,"random")) {
            c->flags |= CMD_RANDOM;
        } else if (!strcasecmp(flag,"to-sort")) {
            c->flags |= CMD_SORT_FOR_SCRIPT;
        } else if (!strcasecmp(flag,"ok-loading")) {
            c->flags |= CMD_LOADING;
        } else if (!strcasecmp(flag,"ok-stale")) {
            c->flags |= CMD_STALE;
        } else if (!strcasecmp(flag,"no-monitor")) {
            c->flags |= CMD_SKIP_MONITOR;
2997 2998
        } else if (!strcasecmp(flag,"no-slowlog")) {
            c->flags |= CMD_SKIP_SLOWLOG;
2999 3000 3001 3002
        } else if (!strcasecmp(flag,"cluster-asking")) {
            c->flags |= CMD_ASKING;
        } else if (!strcasecmp(flag,"fast")) {
            c->flags |= CMD_FAST | CMD_CATEGORY_FAST;
3003 3004
        } else if (!strcasecmp(flag,"no-auth")) {
            c->flags |= CMD_NO_AUTH;
3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015
        } else {
            /* Parse ACL categories here if the flag name starts with @. */
            uint64_t catflag;
            if (flag[0] == '@' &&
                (catflag = ACLGetCommandCategoryFlagByName(flag+1)) != 0)
            {
                c->flags |= catflag;
            } else {
                sdsfreesplitres(argv,argc);
                return C_ERR;
            }
3016 3017
        }
    }
A
antirez 已提交
3018 3019 3020
    /* If it's not @fast is @slow in this binary world. */
    if (!(c->flags & CMD_CATEGORY_FAST)) c->flags |= CMD_CATEGORY_SLOW;

3021 3022 3023 3024
    sdsfreesplitres(argv,argc);
    return C_OK;
}

3025 3026 3027 3028
/* Populates the Redis Command Table starting from the hard coded list
 * we have on top of redis.c file. */
void populateCommandTable(void) {
    int j;
3029
    int numcommands = sizeof(redisCommandTable)/sizeof(struct redisCommand);
3030 3031

    for (j = 0; j < numcommands; j++) {
3032
        struct redisCommand *c = redisCommandTable+j;
3033
        int retval1, retval2;
3034

3035 3036
        /* Translate the command string flags description into an actual
         * set of flags. */
3037 3038
        if (populateCommandTableParseFlags(c,c->sflags) == C_ERR)
            serverPanic("Unsupported command flag");
3039

3040
        c->id = ACLGetCommandID(c->name); /* Assign the ID used for ACL. */
3041 3042 3043 3044
        retval1 = dictAdd(server.commands, sdsnew(c->name), c);
        /* Populate an additional dictionary that will be unaffected
         * by rename-command statements in redis.conf. */
        retval2 = dictAdd(server.orig_commands, sdsnew(c->name), c);
A
antirez 已提交
3045
        serverAssert(retval1 == DICT_OK && retval2 == DICT_OK);
3046
    }
3047 3048
}

3049
void resetCommandTableStats(void) {
3050 3051 3052
    struct redisCommand *c;
    dictEntry *de;
    dictIterator *di;
3053

3054 3055 3056
    di = dictGetSafeIterator(server.commands);
    while((de = dictNext(di)) != NULL) {
        c = (struct redisCommand *) dictGetVal(de);
3057 3058 3059
        c->microseconds = 0;
        c->calls = 0;
    }
3060 3061
    dictReleaseIterator(di);

3062 3063
}

3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100
/* ========================== Redis OP Array API ============================ */

void redisOpArrayInit(redisOpArray *oa) {
    oa->ops = NULL;
    oa->numops = 0;
}

int redisOpArrayAppend(redisOpArray *oa, struct redisCommand *cmd, int dbid,
                       robj **argv, int argc, int target)
{
    redisOp *op;

    oa->ops = zrealloc(oa->ops,sizeof(redisOp)*(oa->numops+1));
    op = oa->ops+oa->numops;
    op->cmd = cmd;
    op->dbid = dbid;
    op->argv = argv;
    op->argc = argc;
    op->target = target;
    oa->numops++;
    return oa->numops;
}

void redisOpArrayFree(redisOpArray *oa) {
    while(oa->numops) {
        int j;
        redisOp *op;

        oa->numops--;
        op = oa->ops+oa->numops;
        for (j = 0; j < op->argc; j++)
            decrRefCount(op->argv[j]);
        zfree(op->argv);
    }
    zfree(oa->ops);
}

3101 3102
/* ====================== Commands lookup and execution ===================== */

3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113
struct redisCommand *lookupCommand(sds name) {
    return dictFetchValue(server.commands, name);
}

struct redisCommand *lookupCommandByCString(char *s) {
    struct redisCommand *cmd;
    sds name = sdsnew(s);

    cmd = dictFetchValue(server.commands, name);
    sdsfree(name);
    return cmd;
3114 3115
}

3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129
/* Lookup the command in the current table, if not found also check in
 * the original table containing the original command names unaffected by
 * redis.conf rename-command statement.
 *
 * This is used by functions rewriting the argument vector such as
 * rewriteClientCommandVector() in order to set client->cmd pointer
 * correctly even if the command was renamed. */
struct redisCommand *lookupCommandOrOriginal(sds name) {
    struct redisCommand *cmd = dictFetchValue(server.commands, name);

    if (!cmd) cmd = dictFetchValue(server.orig_commands,name);
    return cmd;
}

A
antirez 已提交
3130
/* Propagate the specified command (in the context of the specified database id)
3131
 * to AOF and Slaves.
A
antirez 已提交
3132 3133
 *
 * flags are an xor between:
A
antirez 已提交
3134 3135 3136
 * + PROPAGATE_NONE (no propagation of command at all)
 * + PROPAGATE_AOF (propagate into the AOF file if is enabled)
 * + PROPAGATE_REPL (propagate into the replication link)
3137
 *
3138 3139 3140 3141 3142 3143 3144
 * This should not be used inside commands implementation since it will not
 * wrap the resulting commands in MULTI/EXEC. Use instead alsoPropagate(),
 * preventCommandPropagation(), forceCommandPropagation().
 *
 * However for functions that need to (also) propagate out of the context of a
 * command execution, for example when serving a blocked client, you
 * want to use propagate().
A
antirez 已提交
3145 3146 3147 3148
 */
void propagate(struct redisCommand *cmd, int dbid, robj **argv, int argc,
               int flags)
{
A
antirez 已提交
3149
    if (server.aof_state != AOF_OFF && flags & PROPAGATE_AOF)
A
antirez 已提交
3150
        feedAppendOnlyFile(cmd,dbid,argv,argc);
A
antirez 已提交
3151
    if (flags & PROPAGATE_REPL)
A
antirez 已提交
3152 3153 3154
        replicationFeedSlaves(server.slaves,dbid,argv,argc);
}

3155
/* Used inside commands to schedule the propagation of additional commands
3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166
 * after the current command is propagated to AOF / Replication.
 *
 * 'cmd' must be a pointer to the Redis command to replicate, dbid is the
 * database ID the command should be propagated into.
 * Arguments of the command to propagte are passed as an array of redis
 * objects pointers of len 'argc', using the 'argv' vector.
 *
 * The function does not take a reference to the passed 'argv' vector,
 * so it is up to the caller to release the passed argv (but it is usually
 * stack allocated).  The function autoamtically increments ref count of
 * passed objects, so the caller does not need to. */
3167 3168 3169
void alsoPropagate(struct redisCommand *cmd, int dbid, robj **argv, int argc,
                   int target)
{
3170
    robj **argvcopy;
3171 3172
    int j;

3173 3174 3175
    if (server.loading) return; /* No propagation during loading. */

    argvcopy = zmalloc(sizeof(robj*)*argc);
3176 3177 3178 3179 3180
    for (j = 0; j < argc; j++) {
        argvcopy[j] = argv[j];
        incrRefCount(argv[j]);
    }
    redisOpArrayAppend(&server.also_propagate,cmd,dbid,argvcopy,argc,target);
3181 3182
}

A
antirez 已提交
3183
/* It is possible to call the function forceCommandPropagation() inside a
J
Juarez Bochi 已提交
3184
 * Redis command implementation in order to to force the propagation of a
A
antirez 已提交
3185
 * specific command execution into AOF / Replication. */
3186
void forceCommandPropagation(client *c, int flags) {
A
antirez 已提交
3187 3188
    if (flags & PROPAGATE_REPL) c->flags |= CLIENT_FORCE_REPL;
    if (flags & PROPAGATE_AOF) c->flags |= CLIENT_FORCE_AOF;
A
antirez 已提交
3189 3190
}

3191 3192 3193
/* Avoid that the executed command is propagated at all. This way we
 * are free to just propagate what we want using the alsoPropagate()
 * API. */
3194
void preventCommandPropagation(client *c) {
A
antirez 已提交
3195
    c->flags |= CLIENT_PREVENT_PROP;
3196 3197
}

3198 3199 3200 3201 3202 3203 3204 3205 3206 3207
/* AOF specific version of preventCommandPropagation(). */
void preventCommandAOF(client *c) {
    c->flags |= CLIENT_PREVENT_AOF_PROP;
}

/* Replication specific version of preventCommandPropagation(). */
void preventCommandReplication(client *c) {
    c->flags |= CLIENT_PREVENT_REPL_PROP;
}

A
antirez 已提交
3208 3209 3210 3211 3212 3213 3214 3215
/* Call() is the core of Redis execution of a command.
 *
 * The following flags can be passed:
 * CMD_CALL_NONE        No flags.
 * CMD_CALL_SLOWLOG     Check command speed and log in the slow log if needed.
 * CMD_CALL_STATS       Populate command stats.
 * CMD_CALL_PROPAGATE_AOF   Append command to AOF if it modified the dataset
 *                          or if the client flags are forcing propagation.
W
Wander Hillen 已提交
3216
 * CMD_CALL_PROPAGATE_REPL  Send command to slaves if it modified the dataset
A
antirez 已提交
3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244
 *                          or if the client flags are forcing propagation.
 * CMD_CALL_PROPAGATE   Alias for PROPAGATE_AOF|PROPAGATE_REPL.
 * CMD_CALL_FULL        Alias for SLOWLOG|STATS|PROPAGATE.
 *
 * The exact propagation behavior depends on the client flags.
 * Specifically:
 *
 * 1. If the client flags CLIENT_FORCE_AOF or CLIENT_FORCE_REPL are set
 *    and assuming the corresponding CMD_CALL_PROPAGATE_AOF/REPL is set
 *    in the call flags, then the command is propagated even if the
 *    dataset was not affected by the command.
 * 2. If the client flags CLIENT_PREVENT_REPL_PROP or CLIENT_PREVENT_AOF_PROP
 *    are set, the propagation into AOF or to slaves is not performed even
 *    if the command modified the dataset.
 *
 * Note that regardless of the client flags, if CMD_CALL_PROPAGATE_AOF
 * or CMD_CALL_PROPAGATE_REPL are not set, then respectively AOF or
 * slaves propagation will never occur.
 *
 * Client flags are modified by the implementation of a given command
 * using the following API:
 *
 * forceCommandPropagation(client *c, int flags);
 * preventCommandPropagation(client *c);
 * preventCommandAOF(client *c);
 * preventCommandReplication(client *c);
 *
 */
3245
void call(client *c, int flags) {
A
antirez 已提交
3246 3247
    long long dirty;
    ustime_t start, duration;
A
antirez 已提交
3248
    int client_old_flags = c->flags;
3249
    struct redisCommand *real_cmd = c->cmd;
3250

3251
    server.fixed_time_expire++;
3252

3253 3254
    /* Send the command to clients in MONITOR mode if applicable.
     * Administrative commands are considered too dangerous to be shown. */
3255 3256
    if (listLength(server.monitors) &&
        !server.loading &&
A
antirez 已提交
3257
        !(c->cmd->flags & (CMD_SKIP_MONITOR|CMD_ADMIN)))
3258
    {
3259
        replicationFeedMonitors(c,server.monitors,c->db->id,c->argv,c->argc);
3260
    }
A
antirez 已提交
3261

3262 3263 3264
    /* Initialization: clear the flags that must be set by the command on
     * demand, and initialize the array for additional commands propagation. */
    c->flags &= ~(CLIENT_FORCE_AOF|CLIENT_FORCE_REPL|CLIENT_PREVENT_PROP);
A
antirez 已提交
3265
    redisOpArray prev_also_propagate = server.also_propagate;
3266
    redisOpArrayInit(&server.also_propagate);
3267 3268

    /* Call the command. */
3269
    dirty = server.dirty;
A
antirez 已提交
3270 3271
    updateCachedTime(0);
    start = server.ustime;
3272
    c->cmd->proc(c);
3273
    duration = ustime()-start;
3274
    dirty = server.dirty-dirty;
A
antirez 已提交
3275
    if (dirty < 0) dirty = 0;
3276 3277 3278

    /* When EVAL is called loading the AOF we don't want commands called
     * from Lua to go into the slowlog or to populate statistics. */
A
antirez 已提交
3279 3280
    if (server.loading && c->flags & CLIENT_LUA)
        flags &= ~(CMD_CALL_SLOWLOG | CMD_CALL_STATS);
3281

A
antirez 已提交
3282 3283 3284
    /* If the caller is Lua, we want to force the EVAL caller to propagate
     * the script if the command flag or client flag are forcing the
     * propagation. */
A
antirez 已提交
3285 3286 3287 3288 3289
    if (c->flags & CLIENT_LUA && server.lua_caller) {
        if (c->flags & CLIENT_FORCE_REPL)
            server.lua_caller->flags |= CLIENT_FORCE_REPL;
        if (c->flags & CLIENT_FORCE_AOF)
            server.lua_caller->flags |= CLIENT_FORCE_AOF;
A
antirez 已提交
3290 3291
    }

A
antirez 已提交
3292 3293
    /* Log the command into the Slow log if needed, and populate the
     * per-command statistics that we show in INFO commandstats. */
3294
    if (flags & CMD_CALL_SLOWLOG && !(c->cmd->flags & CMD_SKIP_SLOWLOG)) {
A
antirez 已提交
3295
        char *latency_event = (c->cmd->flags & CMD_FAST) ?
3296
                              "fast-command" : "command";
3297
        latencyAddSampleIfNeeded(latency_event,duration/1000);
3298
        slowlogPushEntryIfNeeded(c,c->argv,c->argc,duration);
3299
    }
3300

A
antirez 已提交
3301
    if (flags & CMD_CALL_STATS) {
3302 3303 3304 3305 3306
        /* use the real command that was executed (cmd and lastamc) may be
         * different, in case of MULTI-EXEC or re-written commands such as
         * EXPIRE, GEOADD, etc. */
        real_cmd->microseconds += duration;
        real_cmd->calls++;
3307
    }
A
antirez 已提交
3308 3309

    /* Propagate the command into the AOF and replication link */
A
antirez 已提交
3310 3311 3312 3313
    if (flags & CMD_CALL_PROPAGATE &&
        (c->flags & CLIENT_PREVENT_PROP) != CLIENT_PREVENT_PROP)
    {
        int propagate_flags = PROPAGATE_NONE;
A
antirez 已提交
3314

3315 3316
        /* Check if the command operated changes in the data set. If so
         * set for replication / AOF propagation. */
A
antirez 已提交
3317
        if (dirty) propagate_flags |= (PROPAGATE_AOF|PROPAGATE_REPL);
3318

A
antirez 已提交
3319
        /* If the client forced AOF / replication of the command, set
3320
         * the flags regardless of the command effects on the data set. */
A
antirez 已提交
3321 3322
        if (c->flags & CLIENT_FORCE_REPL) propagate_flags |= PROPAGATE_REPL;
        if (c->flags & CLIENT_FORCE_AOF) propagate_flags |= PROPAGATE_AOF;
3323

A
antirez 已提交
3324
        /* However prevent AOF / replication propagation if the command
J
Jack Drogon 已提交
3325
         * implementations called preventCommandPropagation() or similar,
A
antirez 已提交
3326 3327 3328 3329 3330 3331 3332
         * or if we don't have the call() flags to do so. */
        if (c->flags & CLIENT_PREVENT_REPL_PROP ||
            !(flags & CMD_CALL_PROPAGATE_REPL))
                propagate_flags &= ~PROPAGATE_REPL;
        if (c->flags & CLIENT_PREVENT_AOF_PROP ||
            !(flags & CMD_CALL_PROPAGATE_AOF))
                propagate_flags &= ~PROPAGATE_AOF;
3333 3334

        /* Call propagate() only if at least one of AOF / replication
3335 3336 3337
         * propagation is needed. Note that modules commands handle replication
         * in an explicit way, so we never replicate them automatically. */
        if (propagate_flags != PROPAGATE_NONE && !(c->cmd->flags & CMD_MODULE))
A
antirez 已提交
3338
            propagate(c->cmd,c->db->id,c->argv,c->argc,propagate_flags);
3339
    }
3340

3341
    /* Restore the old replication flags, since call() can be executed
A
antirez 已提交
3342
     * recursively. */
A
antirez 已提交
3343
    c->flags &= ~(CLIENT_FORCE_AOF|CLIENT_FORCE_REPL|CLIENT_PREVENT_PROP);
3344
    c->flags |= client_old_flags &
A
antirez 已提交
3345
        (CLIENT_FORCE_AOF|CLIENT_FORCE_REPL|CLIENT_PREVENT_PROP);
A
antirez 已提交
3346

3347
    /* Handle the alsoPropagate() API to handle commands that want to propagate
3348
     * multiple separated commands. Note that alsoPropagate() is not affected
A
antirez 已提交
3349
     * by CLIENT_PREVENT_PROP flag. */
3350
    if (server.also_propagate.numops) {
3351
        int j;
3352
        redisOp *rop;
3353

A
antirez 已提交
3354
        if (flags & CMD_CALL_PROPAGATE) {
3355 3356
            int multi_emitted = 0;
            /* Wrap the commands in server.also_propagate array,
3357
             * but don't wrap it if we are already in MULTI context,
3358
             * in case the nested MULTI/EXEC.
3359 3360 3361
             *
             * And if the array contains only one command, no need to
             * wrap it, since the single command is atomic. */
3362 3363 3364 3365 3366
            if (server.also_propagate.numops > 1 &&
                !(c->cmd->flags & CMD_MODULE) &&
                !(c->flags & CLIENT_MULTI) &&
                !(flags & CMD_CALL_NOWRAP))
            {
3367 3368 3369 3370
                execCommandPropagateMulti(c);
                multi_emitted = 1;
            }

3371 3372
            for (j = 0; j < server.also_propagate.numops; j++) {
                rop = &server.also_propagate.ops[j];
A
antirez 已提交
3373 3374 3375 3376 3377 3378
                int target = rop->target;
                /* Whatever the command wish is, we honor the call() flags. */
                if (!(flags&CMD_CALL_PROPAGATE_AOF)) target &= ~PROPAGATE_AOF;
                if (!(flags&CMD_CALL_PROPAGATE_REPL)) target &= ~PROPAGATE_REPL;
                if (target)
                    propagate(rop->cmd,rop->dbid,rop->argv,rop->argc,target);
3379
            }
3380 3381 3382 3383

            if (multi_emitted) {
                execCommandPropagateExec(c);
            }
3384 3385
        }
        redisOpArrayFree(&server.also_propagate);
3386
    }
A
antirez 已提交
3387
    server.also_propagate = prev_also_propagate;
3388 3389 3390 3391 3392 3393

    /* If the client has keys tracking enabled for client side caching,
     * make sure to remember the keys it fetched via this command. */
    if (c->cmd->flags & CMD_READONLY) {
        client *caller = (c->flags & CLIENT_LUA && server.lua_caller) ?
                            server.lua_caller : c;
3394 3395 3396
        if (caller->flags & CLIENT_TRACKING &&
            !(caller->flags & CLIENT_TRACKING_BCAST))
        {
3397
            trackingRememberKeys(caller);
3398
        }
3399 3400
    }

3401
    server.fixed_time_expire--;
3402 3403 3404 3405
    server.stat_numcommands++;
}

/* If this function gets called we already read a whole
3406
 * command, arguments are in the client argv/argc fields.
3407 3408 3409
 * processCommand() execute the command or prepare the
 * server for a bulk read from the client.
 *
3410
 * If C_OK is returned the client is still alive and valid and
G
guiquanz 已提交
3411
 * other operations can be performed by the caller. Otherwise
3412
 * if C_ERR is returned the client was destroyed (i.e. after QUIT). */
3413
int processCommand(client *c) {
3414 3415
    moduleCallCommandFilters(c);

P
Pieter Noordhuis 已提交
3416 3417 3418 3419
    /* The QUIT command is handled separately. Normal command procs will
     * go through checking for replication and QUIT will cause trouble
     * when FORCE_REPLICATION is enabled and would be implemented in
     * a regular command proc. */
3420
    if (!strcasecmp(c->argv[0]->ptr,"quit")) {
P
Pieter Noordhuis 已提交
3421
        addReply(c,shared.ok);
A
antirez 已提交
3422
        c->flags |= CLIENT_CLOSE_AFTER_REPLY;
3423
        return C_ERR;
3424 3425 3426
    }

    /* Now lookup the command and check ASAP about trivial error conditions
3427
     * such as wrong arity, bad command name and so forth. */
3428
    c->cmd = c->lastcmd = lookupCommand(c->argv[0]->ptr);
3429
    if (!c->cmd) {
A
antirez 已提交
3430
        flagTransaction(c);
3431 3432 3433 3434 3435 3436 3437
        sds args = sdsempty();
        int i;
        for (i=1; i < c->argc && sdslen(args) < 128; i++)
            args = sdscatprintf(args, "`%.*s`, ", 128-(int)sdslen(args), (char*)c->argv[i]->ptr);
        addReplyErrorFormat(c,"unknown command `%s`, with args beginning with: %s",
            (char*)c->argv[0]->ptr, args);
        sdsfree(args);
3438
        return C_OK;
3439 3440
    } else if ((c->cmd->arity > 0 && c->cmd->arity != c->argc) ||
               (c->argc < -c->cmd->arity)) {
A
antirez 已提交
3441
        flagTransaction(c);
3442
        addReplyErrorFormat(c,"wrong number of arguments for '%s' command",
3443
            c->cmd->name);
3444
        return C_OK;
3445 3446
    }

3447 3448
    /* Check if the user is authenticated. This check is skipped in case
     * the default user is flagged as "nopass" and is active. */
3449
    int auth_required = (!(DefaultUser->flags & USER_FLAG_NOPASS) ||
3450
                          (DefaultUser->flags & USER_FLAG_DISABLED)) &&
3451
                        !c->authenticated;
3452
    if (auth_required) {
3453 3454 3455
        /* AUTH and HELLO and no auth modules are valid even in
         * non-authenticated state. */
        if (!(c->cmd->flags & CMD_NO_AUTH)) {
3456 3457 3458 3459
            flagTransaction(c);
            addReply(c,shared.noautherr);
            return C_OK;
        }
3460 3461
    }

3462 3463
    /* Check if the user can run this command according to the current
     * ACLs. */
A
antirez 已提交
3464 3465
    int acl_keypos;
    int acl_retval = ACLCheckCommandPerm(c,&acl_keypos);
A
antirez 已提交
3466
    if (acl_retval != ACL_OK) {
A
antirez 已提交
3467
        addACLLogEntry(c,acl_retval,acl_keypos,NULL);
3468
        flagTransaction(c);
A
antirez 已提交
3469 3470 3471
        if (acl_retval == ACL_DENIED_CMD)
            addReplyErrorFormat(c,
                "-NOPERM this user has no permissions to run "
3472
                "the '%s' command or its subcommand", c->cmd->name);
A
antirez 已提交
3473 3474 3475 3476
        else
            addReplyErrorFormat(c,
                "-NOPERM this user has no permissions to access "
                "one of the keys used as arguments");
3477 3478 3479
        return C_OK;
    }

3480 3481 3482 3483
    /* If cluster is enabled perform the cluster redirection here.
     * However we don't perform the redirection if:
     * 1) The sender of this command is our master.
     * 2) The command has no key arguments. */
A
antirez 已提交
3484
    if (server.cluster_enabled &&
A
antirez 已提交
3485 3486 3487
        !(c->flags & CLIENT_MASTER) &&
        !(c->flags & CLIENT_LUA &&
          server.lua_caller->flags & CLIENT_MASTER) &&
3488 3489
        !(c->cmd->getkeys_proc == NULL && c->cmd->firstkey == 0 &&
          c->cmd->proc != execCommand))
3490
    {
A
antirez 已提交
3491
        int hashslot;
3492 3493 3494 3495 3496 3497 3498 3499
        int error_code;
        clusterNode *n = getNodeByQuery(c,c->cmd,c->argv,c->argc,
                                        &hashslot,&error_code);
        if (n == NULL || n != server.cluster->myself) {
            if (c->cmd->proc == execCommand) {
                discardTransaction(c);
            } else {
                flagTransaction(c);
A
antirez 已提交
3500
            }
3501 3502
            clusterRedirectClient(c,n,hashslot,error_code);
            return C_OK;
A
antirez 已提交
3503 3504 3505
        }
    }

3506
    /* Handle the maxmemory directive.
3507 3508 3509
     *
     * Note that we do not want to reclaim memory if we are here re-entering
     * the event loop since there is a busy Lua script running in timeout
3510 3511
     * condition, to avoid mixing the propagation of scripts with the
     * propagation of DELs due to eviction. */
3512
    if (server.maxmemory && !server.lua_timedout) {
A
antirez 已提交
3513
        int out_of_memory = freeMemoryIfNeededAndSafe() == C_ERR;
3514 3515 3516 3517 3518
        /* freeMemoryIfNeeded may flush slave output buffers. This may result
         * into a slave, that may be the active client, to be freed. */
        if (server.current_client == NULL) return C_ERR;

        /* It was impossible to free enough memory, and the command the client
3519 3520 3521 3522
         * is trying to execute is denied during OOM conditions or the client
         * is in MULTI/EXEC context? Error. */
        if (out_of_memory &&
            (c->cmd->flags & CMD_DENYOOM ||
A
antirez 已提交
3523 3524 3525 3526
             (c->flags & CLIENT_MULTI &&
              c->cmd->proc != execCommand &&
              c->cmd->proc != discardCommand)))
        {
A
antirez 已提交
3527
            flagTransaction(c);
3528
            addReply(c, shared.oomerr);
3529
            return C_OK;
3530
        }
3531 3532 3533 3534 3535 3536 3537

        /* Save out_of_memory result at script start, otherwise if we check OOM
         * untill first write within script, memory used by lua stack and
         * arguments might interfere. */
        if (c->cmd->proc == evalCommand || c->cmd->proc == evalShaCommand) {
            server.lua_oom = out_of_memory;
        }
3538 3539
    }

3540 3541 3542 3543
    /* Make sure to use a reasonable amount of memory for client side
     * caching metadata. */
    if (server.tracking_clients) trackingLimitUsedSlots();

3544 3545
    /* Don't accept write commands if there are problems persisting on disk
     * and if this is a master instance. */
3546 3547
    int deny_write_type = writeCommandsDeniedByDiskError();
    if (deny_write_type != DISK_ERROR_TYPE_NONE &&
3548
        server.masterhost == NULL &&
A
antirez 已提交
3549
        (c->cmd->flags & CMD_WRITE ||
3550
         c->cmd->proc == pingCommand))
3551
    {
A
antirez 已提交
3552
        flagTransaction(c);
3553
        if (deny_write_type == DISK_ERROR_TYPE_RDB)
3554 3555 3556 3557 3558 3559
            addReply(c, shared.bgsaveerr);
        else
            addReplySds(c,
                sdscatprintf(sdsempty(),
                "-MISCONF Errors writing to the AOF file: %s\r\n",
                strerror(server.aof_last_write_errno)));
3560
        return C_OK;
3561 3562
    }

3563
    /* Don't accept write commands if there are not enough good slaves and
3564
     * user configured the min-slaves-to-write option. */
3565 3566
    if (server.masterhost == NULL &&
        server.repl_min_slaves_to_write &&
3567
        server.repl_min_slaves_max_lag &&
A
antirez 已提交
3568
        c->cmd->flags & CMD_WRITE &&
3569 3570 3571 3572
        server.repl_good_slaves_count < server.repl_min_slaves_to_write)
    {
        flagTransaction(c);
        addReply(c, shared.noreplicaserr);
3573
        return C_OK;
3574 3575
    }

3576
    /* Don't accept write commands if this is a read only slave. But
3577 3578
     * accept write commands if this is our master. */
    if (server.masterhost && server.repl_slave_ro &&
A
antirez 已提交
3579 3580
        !(c->flags & CLIENT_MASTER) &&
        c->cmd->flags & CMD_WRITE)
3581
    {
3582
        flagTransaction(c);
3583
        addReply(c, shared.roslaveerr);
3584
        return C_OK;
3585 3586
    }

3587 3588 3589
    /* Only allow a subset of commands in the context of Pub/Sub if the
     * connection is in RESP2 mode. With RESP3 there are no limits. */
    if ((c->flags & CLIENT_PUBSUB && c->resp == 2) &&
3590
        c->cmd->proc != pingCommand &&
3591 3592 3593 3594
        c->cmd->proc != subscribeCommand &&
        c->cmd->proc != unsubscribeCommand &&
        c->cmd->proc != psubscribeCommand &&
        c->cmd->proc != punsubscribeCommand) {
3595
        addReplyErrorFormat(c,
A
antirez 已提交
3596 3597
            "Can't execute '%s': only (P)SUBSCRIBE / "
            "(P)UNSUBSCRIBE / PING / QUIT are allowed in this context",
3598
            c->cmd->name);
3599
        return C_OK;
3600 3601
    }

W
WuYunlong 已提交
3602 3603 3604
    /* Only allow commands with flag "t", such as INFO, SLAVEOF and so on,
     * when slave-serve-stale-data is no and we are a slave with a broken
     * link with master. */
A
antirez 已提交
3605
    if (server.masterhost && server.repl_state != REPL_STATE_CONNECTED &&
3606
        server.repl_serve_stale_data == 0 &&
A
antirez 已提交
3607
        !(c->cmd->flags & CMD_STALE))
3608
    {
A
antirez 已提交
3609
        flagTransaction(c);
3610
        addReply(c, shared.masterdownerr);
3611
        return C_OK;
3612 3613
    }

3614
    /* Loading DB? Return an error if the command has not the
A
antirez 已提交
3615 3616
     * CMD_LOADING flag. */
    if (server.loading && !(c->cmd->flags & CMD_LOADING)) {
3617
        addReply(c, shared.loadingerr);
3618
        return C_OK;
3619 3620
    }

3621 3622 3623 3624 3625 3626
    /* Lua script too slow? Only allow a limited number of commands.
     * Note that we need to allow the transactions commands, otherwise clients
     * sending a transaction with pipelining without error checking, may have
     * the MULTI plus a few initial commands refused, then the timeout
     * condition resolves, and the bottom-half of the transaction gets
     * executed, see Github PR #7022. */
3627
    if (server.lua_timedout &&
3628
          c->cmd->proc != authCommand &&
3629
          c->cmd->proc != helloCommand &&
A
antirez 已提交
3630
          c->cmd->proc != replconfCommand &&
3631 3632 3633
          c->cmd->proc != multiCommand &&
          c->cmd->proc != execCommand &&
          c->cmd->proc != discardCommand &&
3634 3635
          c->cmd->proc != watchCommand &&
          c->cmd->proc != unwatchCommand &&
3636
        !(c->cmd->proc == shutdownCommand &&
3637 3638 3639 3640 3641 3642
          c->argc == 2 &&
          tolower(((char*)c->argv[1]->ptr)[0]) == 'n') &&
        !(c->cmd->proc == scriptCommand &&
          c->argc == 2 &&
          tolower(((char*)c->argv[1]->ptr)[0]) == 'k'))
    {
A
antirez 已提交
3643
        flagTransaction(c);
3644
        addReply(c, shared.slowscripterr);
3645
        return C_OK;
3646 3647
    }

3648
    /* Exec the command */
A
antirez 已提交
3649
    if (c->flags & CLIENT_MULTI &&
3650 3651
        c->cmd->proc != execCommand && c->cmd->proc != discardCommand &&
        c->cmd->proc != multiCommand && c->cmd->proc != watchCommand)
3652
    {
3653
        queueMultiCommand(c);
3654 3655
        addReply(c,shared.queued);
    } else {
A
antirez 已提交
3656
        call(c,CMD_CALL_FULL);
3657
        c->woff = server.master_repl_offset;
3658
        if (listLength(server.ready_keys))
3659
            handleClientsBlockedOnKeys();
3660
    }
3661
    return C_OK;
3662 3663 3664 3665
}

/*================================== Shutdown =============================== */

3666 3667 3668 3669 3670 3671
/* Close listening sockets. Also unlink the unix domain socket if
 * unlink_unix_socket is non-zero. */
void closeListeningSockets(int unlink_unix_socket) {
    int j;

    for (j = 0; j < server.ipfd_count; j++) close(server.ipfd[j]);
3672
    for (j = 0; j < server.tlsfd_count; j++) close(server.tlsfd[j]);
3673 3674 3675 3676
    if (server.sofd != -1) close(server.sofd);
    if (server.cluster_enabled)
        for (j = 0; j < server.cfd_count; j++) close(server.cfd[j]);
    if (unlink_unix_socket && server.unixsocket) {
A
antirez 已提交
3677
        serverLog(LL_NOTICE,"Removing the unix socket file.");
3678 3679 3680 3681
        unlink(server.unixsocket); /* don't care if this fails */
    }
}

3682
int prepareForShutdown(int flags) {
3683 3684 3685 3686 3687 3688 3689 3690 3691
    /* When SHUTDOWN is called while the server is loading a dataset in
     * memory we need to make sure no attempt is performed to save
     * the dataset on shutdown (otherwise it could overwrite the current DB
     * with half-read data).
     *
     * Also when in Sentinel mode clear the SAVE flag and force NOSAVE. */
    if (server.loading || server.sentinel_mode)
        flags = (flags & ~SHUTDOWN_SAVE) | SHUTDOWN_NOSAVE;

A
antirez 已提交
3692 3693
    int save = flags & SHUTDOWN_SAVE;
    int nosave = flags & SHUTDOWN_NOSAVE;
3694

A
antirez 已提交
3695
    serverLog(LL_WARNING,"User requested shutdown...");
3696 3697
    if (server.supervised_mode == SUPERVISED_SYSTEMD)
        redisCommunicateSystemd("STOPPING=1\n");
3698

3699 3700 3701
    /* Kill all the Lua debugger forked sessions. */
    ldbKillForkedSessions();

3702 3703 3704
    /* Kill the saving child if there is a background saving in progress.
       We want to avoid race conditions, for instance our saving child may
       overwrite the synchronous saving did by SHUTDOWN. */
A
antirez 已提交
3705
    if (server.rdb_child_pid != -1) {
A
antirez 已提交
3706
        serverLog(LL_WARNING,"There is a child saving an .rdb. Killing it!");
3707
        killRDBChild();
3708
    }
3709

3710 3711 3712
    /* Kill module child if there is one. */
    if (server.module_child_pid != -1) {
        serverLog(LL_WARNING,"There is a module fork child. Killing it!");
3713
        TerminateModuleForkChild(server.module_child_pid,0);
3714 3715
    }

A
antirez 已提交
3716
    if (server.aof_state != AOF_OFF) {
3717 3718
        /* Kill the AOF saving child as the AOF we already have may be longer
         * but contains the full dataset anyway. */
A
antirez 已提交
3719
        if (server.aof_child_pid != -1) {
3720 3721
            /* If we have AOF enabled but haven't written the AOF yet, don't
             * shutdown or else the dataset will be lost. */
A
antirez 已提交
3722 3723
            if (server.aof_state == AOF_WAIT_REWRITE) {
                serverLog(LL_WARNING, "Writing initial AOF, can't exit.");
3724
                return C_ERR;
3725
            }
A
antirez 已提交
3726
            serverLog(LL_WARNING,
3727
                "There is a child rewriting the AOF. Killing it!");
3728
            killAppendOnlyChild();
3729
        }
3730
        /* Append only file: flush buffers and fsync() the AOF at exit */
A
antirez 已提交
3731
        serverLog(LL_NOTICE,"Calling fsync() on the AOF file.");
3732
        flushAppendOnlyFile(1);
3733
        redis_fsync(server.aof_fd);
3734
    }
3735 3736

    /* Create a new RDB file before exiting. */
3737
    if ((server.saveparamslen > 0 && !nosave) || save) {
A
antirez 已提交
3738
        serverLog(LL_NOTICE,"Saving the final RDB snapshot before exiting.");
3739 3740
        if (server.supervised_mode == SUPERVISED_SYSTEMD)
            redisCommunicateSystemd("STATUS=Saving the final RDB snapshot\n");
3741
        /* Snapshotting. Perform a SYNC SAVE and exit */
3742 3743 3744
        rdbSaveInfo rsi, *rsiptr;
        rsiptr = rdbPopulateSaveInfo(&rsi);
        if (rdbSave(server.rdb_filename,rsiptr) != C_OK) {
3745 3746 3747 3748 3749
            /* Ooops.. error saving! The best we can do is to continue
             * operating. Note that if there was a background saving process,
             * in the next cron() Redis will be notified that the background
             * saving aborted, handling special stuff like slaves pending for
             * synchronization... */
A
antirez 已提交
3750
            serverLog(LL_WARNING,"Error trying to save the DB, can't exit.");
3751 3752
            if (server.supervised_mode == SUPERVISED_SYSTEMD)
                redisCommunicateSystemd("STATUS=Error trying to save the DB, can't exit.\n");
3753
            return C_ERR;
3754 3755
        }
    }
3756

3757 3758 3759
    /* Fire the shutdown modules event. */
    moduleFireServerEvent(REDISMODULE_EVENT_SHUTDOWN,0,NULL);

3760
    /* Remove the pid file if possible and needed. */
R
rebx 已提交
3761
    if (server.daemonize || server.pidfile) {
A
antirez 已提交
3762
        serverLog(LL_NOTICE,"Removing the pid file.");
3763 3764
        unlink(server.pidfile);
    }
3765 3766 3767 3768 3769

    /* Best effort flush of slave output buffers, so that we hopefully
     * send them pending writes. */
    flushSlavesOutputBuffers();

3770
    /* Close the listening sockets. Apparently this allows faster restarts. */
3771
    closeListeningSockets(1);
A
antirez 已提交
3772
    serverLog(LL_WARNING,"%s is now ready to exit, bye bye...",
3773
        server.sentinel_mode ? "Sentinel" : "Redis");
3774
    return C_OK;
3775 3776 3777 3778
}

/*================================== Commands =============================== */

3779 3780 3781
/* Sometimes Redis cannot accept write commands because there is a perstence
 * error with the RDB or AOF file, and Redis is configured in order to stop
 * accepting writes in such situation. This function returns if such a
3782 3783 3784 3785 3786 3787 3788 3789
 * condition is active, and the type of the condition.
 *
 * Function return values:
 *
 * DISK_ERROR_TYPE_NONE:    No problems, we can accept writes.
 * DISK_ERROR_TYPE_AOF:     Don't accept writes: AOF errors.
 * DISK_ERROR_TYPE_RDB:     Don't accept writes: RDB errors.
 */
3790 3791 3792 3793 3794
int writeCommandsDeniedByDiskError(void) {
    if (server.stop_writes_on_bgsave_err &&
        server.saveparamslen > 0 &&
        server.lastbgsave_status == C_ERR)
    {
3795
        return DISK_ERROR_TYPE_RDB;
3796 3797 3798
    } else if (server.aof_state != AOF_OFF &&
               server.aof_last_write_status == C_ERR)
    {
3799
        return DISK_ERROR_TYPE_AOF;
3800 3801 3802 3803 3804
    } else {
        return DISK_ERROR_TYPE_NONE;
    }
}

3805 3806
/* The PING command. It works in a different way if the client is in
 * in Pub/Sub mode. */
3807
void pingCommand(client *c) {
3808 3809
    /* The command takes zero or one arguments. */
    if (c->argc > 2) {
3810 3811
        addReplyErrorFormat(c,"wrong number of arguments for '%s' command",
            c->cmd->name);
3812 3813 3814
        return;
    }

3815
    if (c->flags & CLIENT_PUBSUB && c->resp == 2) {
3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827
        addReply(c,shared.mbulkhdr[2]);
        addReplyBulkCBuffer(c,"pong",4);
        if (c->argc == 1)
            addReplyBulkCBuffer(c,"",0);
        else
            addReplyBulk(c,c->argv[1]);
    } else {
        if (c->argc == 1)
            addReply(c,shared.pong);
        else
            addReplyBulk(c,c->argv[1]);
    }
3828 3829
}

3830
void echoCommand(client *c) {
3831 3832 3833
    addReplyBulk(c,c->argv[1]);
}

3834
void timeCommand(client *c) {
A
antirez 已提交
3835 3836
    struct timeval tv;

G
guiquanz 已提交
3837
    /* gettimeofday() can only fail if &tv is a bad address so we
A
antirez 已提交
3838 3839
     * don't check for errors. */
    gettimeofday(&tv,NULL);
3840
    addReplyArrayLen(c,2);
A
antirez 已提交
3841 3842 3843 3844
    addReplyBulkLongLong(c,tv.tv_sec);
    addReplyBulkLongLong(c,tv.tv_usec);
}

A
antirez 已提交
3845
/* Helper function for addReplyCommand() to output flags. */
3846
int addReplyCommandFlag(client *c, struct redisCommand *cmd, int f, char *reply) {
M
Matt Stancliff 已提交
3847 3848 3849 3850 3851 3852
    if (cmd->flags & f) {
        addReplyStatus(c, reply);
        return 1;
    }
    return 0;
}
3853

A
antirez 已提交
3854
/* Output the representation of a Redis command. Used by the COMMAND command. */
3855
void addReplyCommand(client *c, struct redisCommand *cmd) {
M
Matt Stancliff 已提交
3856
    if (!cmd) {
A
antirez 已提交
3857
        addReplyNull(c);
M
Matt Stancliff 已提交
3858
    } else {
3859 3860
        /* We are adding: command name, arg count, flags, first, last, offset, categories */
        addReplyArrayLen(c, 7);
M
Matt Stancliff 已提交
3861 3862 3863 3864
        addReplyBulkCString(c, cmd->name);
        addReplyLongLong(c, cmd->arity);

        int flagcount = 0;
3865
        void *flaglen = addReplyDeferredLen(c);
A
antirez 已提交
3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876
        flagcount += addReplyCommandFlag(c,cmd,CMD_WRITE, "write");
        flagcount += addReplyCommandFlag(c,cmd,CMD_READONLY, "readonly");
        flagcount += addReplyCommandFlag(c,cmd,CMD_DENYOOM, "denyoom");
        flagcount += addReplyCommandFlag(c,cmd,CMD_ADMIN, "admin");
        flagcount += addReplyCommandFlag(c,cmd,CMD_PUBSUB, "pubsub");
        flagcount += addReplyCommandFlag(c,cmd,CMD_NOSCRIPT, "noscript");
        flagcount += addReplyCommandFlag(c,cmd,CMD_RANDOM, "random");
        flagcount += addReplyCommandFlag(c,cmd,CMD_SORT_FOR_SCRIPT,"sort_for_script");
        flagcount += addReplyCommandFlag(c,cmd,CMD_LOADING, "loading");
        flagcount += addReplyCommandFlag(c,cmd,CMD_STALE, "stale");
        flagcount += addReplyCommandFlag(c,cmd,CMD_SKIP_MONITOR, "skip_monitor");
3877
        flagcount += addReplyCommandFlag(c,cmd,CMD_SKIP_SLOWLOG, "skip_slowlog");
A
antirez 已提交
3878 3879
        flagcount += addReplyCommandFlag(c,cmd,CMD_ASKING, "asking");
        flagcount += addReplyCommandFlag(c,cmd,CMD_FAST, "fast");
O
Oran Agra 已提交
3880
        flagcount += addReplyCommandFlag(c,cmd,CMD_NO_AUTH, "no_auth");
3881 3882 3883
        if ((cmd->getkeys_proc && !(cmd->flags & CMD_MODULE)) ||
            cmd->flags & CMD_MODULE_GETKEYS)
        {
M
Matt Stancliff 已提交
3884 3885 3886
            addReplyStatus(c, "movablekeys");
            flagcount += 1;
        }
3887
        setDeferredSetLen(c, flaglen, flagcount);
M
Matt Stancliff 已提交
3888 3889 3890 3891

        addReplyLongLong(c, cmd->firstkey);
        addReplyLongLong(c, cmd->lastkey);
        addReplyLongLong(c, cmd->keystep);
3892 3893

        addReplyCommandCategories(c,cmd);
M
Matt Stancliff 已提交
3894 3895
    }
}
3896

A
antirez 已提交
3897
/* COMMAND <subcommand> <args> */
3898
void commandCommand(client *c) {
M
Matt Stancliff 已提交
3899 3900 3901
    dictIterator *di;
    dictEntry *de;

I
Itamar Haber 已提交
3902 3903
    if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"help")) {
        const char *help[] = {
3904
"(no subcommand) -- Return details about all Redis commands.",
3905 3906 3907
"COUNT -- Return the total number of commands in this Redis server.",
"GETKEYS <full-command> -- Return the keys from a full Redis command.",
"INFO [command-name ...] -- Return details about multiple Redis commands.",
3908
NULL
I
Itamar Haber 已提交
3909 3910 3911
        };
        addReplyHelp(c, help);
    } else if (c->argc == 1) {
3912
        addReplyArrayLen(c, dictSize(server.commands));
A
antirez 已提交
3913 3914 3915 3916 3917 3918
        di = dictGetIterator(server.commands);
        while ((de = dictNext(di)) != NULL) {
            addReplyCommand(c, dictGetVal(de));
        }
        dictReleaseIterator(di);
    } else if (!strcasecmp(c->argv[1]->ptr, "info")) {
M
Matt Stancliff 已提交
3919
        int i;
3920
        addReplyArrayLen(c, c->argc-2);
M
Matt Stancliff 已提交
3921
        for (i = 2; i < c->argc; i++) {
3922
            addReplyCommand(c, dictFetchValue(server.commands, c->argv[i]->ptr));
M
Matt Stancliff 已提交
3923
        }
A
antirez 已提交
3924 3925
    } else if (!strcasecmp(c->argv[1]->ptr, "count") && c->argc == 2) {
        addReplyLongLong(c, dictSize(server.commands));
3926 3927 3928 3929 3930
    } else if (!strcasecmp(c->argv[1]->ptr,"getkeys") && c->argc >= 3) {
        struct redisCommand *cmd = lookupCommand(c->argv[2]->ptr);
        int *keys, numkeys, j;

        if (!cmd) {
3931 3932 3933 3934
            addReplyError(c,"Invalid command specified");
            return;
        } else if (cmd->getkeys_proc == NULL && cmd->firstkey == 0) {
            addReplyError(c,"The command has no key arguments");
3935 3936 3937 3938 3939 3940 3941 3942 3943
            return;
        } else if ((cmd->arity > 0 && cmd->arity != c->argc-2) ||
                   ((c->argc-2) < -cmd->arity))
        {
            addReplyError(c,"Invalid number of arguments specified for command");
            return;
        }

        keys = getKeysFromCommand(cmd,c->argv+2,c->argc-2,&numkeys);
3944 3945 3946
        if (!keys) {
            addReplyError(c,"Invalid arguments specified for command");
        } else {
3947
            addReplyArrayLen(c,numkeys);
3948 3949 3950
            for (j = 0; j < numkeys; j++) addReplyBulk(c,c->argv[keys[j]+2]);
            getKeysFreeResult(keys);
        }
A
antirez 已提交
3951
    } else {
3952
        addReplySubcommandSyntaxError(c);
M
Matt Stancliff 已提交
3953 3954 3955
    }
}

3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972
/* Convert an amount of bytes into a human readable string in the form
 * of 100B, 2G, 100M, 4K, and so forth. */
void bytesToHuman(char *s, unsigned long long n) {
    double d;

    if (n < 1024) {
        /* Bytes */
        sprintf(s,"%lluB",n);
    } else if (n < (1024*1024)) {
        d = (double)n/(1024);
        sprintf(s,"%.2fK",d);
    } else if (n < (1024LL*1024*1024)) {
        d = (double)n/(1024*1024);
        sprintf(s,"%.2fM",d);
    } else if (n < (1024LL*1024*1024*1024)) {
        d = (double)n/(1024LL*1024*1024);
        sprintf(s,"%.2fG",d);
3973 3974 3975 3976 3977 3978 3979 3980 3981
    } else if (n < (1024LL*1024*1024*1024*1024)) {
        d = (double)n/(1024LL*1024*1024*1024);
        sprintf(s,"%.2fT",d);
    } else if (n < (1024LL*1024*1024*1024*1024*1024)) {
        d = (double)n/(1024LL*1024*1024*1024*1024);
        sprintf(s,"%.2fP",d);
    } else {
        /* Let's hope we never need this */
        sprintf(s,"%lluB",n);
3982 3983 3984 3985 3986 3987
    }
}

/* Create the string returned by the INFO command. This is decoupled
 * by the INFO command itself as we need to report the same information
 * on memory corruption problems. */
3988
sds genRedisInfoString(const char *section) {
3989
    sds info = sdsempty();
3990
    time_t uptime = server.unixtime-server.stat_starttime;
3991
    int j;
3992
    struct rusage self_ru, c_ru;
3993
    int allsections = 0, defsections = 0, everything = 0, modules = 0;
3994
    int sections = 0;
3995

3996 3997 3998
    if (section == NULL) section = "default";
    allsections = strcasecmp(section,"all") == 0;
    defsections = strcasecmp(section,"default") == 0;
3999 4000 4001
    everything = strcasecmp(section,"everything") == 0;
    modules = strcasecmp(section,"modules") == 0;
    if (everything) allsections = 1;
4002 4003 4004

    getrusage(RUSAGE_SELF, &self_ru);
    getrusage(RUSAGE_CHILDREN, &c_ru);
4005 4006 4007

    /* Server */
    if (allsections || defsections || !strcasecmp(section,"server")) {
4008 4009
        static int call_uname = 1;
        static struct utsname name;
4010
        char *mode;
4011

4012 4013 4014
        if (server.cluster_enabled) mode = "cluster";
        else if (server.sentinel_mode) mode = "sentinel";
        else mode = "standalone";
4015

4016
        if (sections++) info = sdscat(info,"\r\n");
4017 4018 4019 4020 4021 4022 4023

        if (call_uname) {
            /* Uname can be slow and is always the same output. Cache it. */
            uname(&name);
            call_uname = 0;
        }

A
antirez 已提交
4024
        info = sdscatfmt(info,
4025 4026 4027
            "# Server\r\n"
            "redis_version:%s\r\n"
            "redis_git_sha1:%s\r\n"
A
antirez 已提交
4028 4029
            "redis_git_dirty:%i\r\n"
            "redis_build_id:%s\r\n"
4030
            "redis_mode:%s\r\n"
4031
            "os:%s %s %s\r\n"
A
antirez 已提交
4032
            "arch_bits:%i\r\n"
4033
            "multiplexing_api:%s\r\n"
4034
            "atomicvar_api:%s\r\n"
A
antirez 已提交
4035 4036
            "gcc_version:%i.%i.%i\r\n"
            "process_id:%I\r\n"
A
antirez 已提交
4037
            "run_id:%s\r\n"
A
antirez 已提交
4038 4039 4040 4041 4042 4043
            "tcp_port:%i\r\n"
            "uptime_in_seconds:%I\r\n"
            "uptime_in_days:%I\r\n"
            "hz:%i\r\n"
            "configured_hz:%i\r\n"
            "lru_clock:%u\r\n"
A
antirez 已提交
4044
            "executable:%s\r\n"
4045
            "config_file:%s\r\n",
4046 4047 4048
            REDIS_VERSION,
            redisGitSHA1(),
            strtol(redisGitDirty(),NULL,10) > 0,
A
antirez 已提交
4049
            redisBuildIdString(),
4050
            mode,
4051
            name.sysname, name.release, name.machine,
4052
            server.arch_bits,
4053
            aeGetApiName(),
4054
            REDIS_ATOMIC_API,
A
antirez 已提交
4055 4056 4057 4058 4059
#ifdef __GNUC__
            __GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__,
#else
            0,0,0,
#endif
A
antirez 已提交
4060
            (int64_t) getpid(),
A
antirez 已提交
4061
            server.runid,
Y
Yossi Gottlieb 已提交
4062
            server.port ? server.port : server.tls_port,
A
antirez 已提交
4063 4064
            (int64_t)uptime,
            (int64_t)(uptime/(3600*24)),
4065
            server.hz,
4066
            server.config_hz,
A
antirez 已提交
4067
            server.lruclock,
A
antirez 已提交
4068
            server.executable ? server.executable : "",
4069
            server.configfile ? server.configfile : "");
4070 4071 4072 4073
    }

    /* Clients */
    if (allsections || defsections || !strcasecmp(section,"clients")) {
4074 4075
        size_t maxin, maxout;
        getExpansiveClientsInfo(&maxin,&maxout);
4076 4077 4078
        if (sections++) info = sdscat(info,"\r\n");
        info = sdscatprintf(info,
            "# Clients\r\n"
4079
            "connected_clients:%lu\r\n"
4080 4081
            "client_recent_max_input_buffer:%zu\r\n"
            "client_recent_max_output_buffer:%zu\r\n"
4082
            "blocked_clients:%d\r\n"
4083
            "tracking_clients:%d\r\n"
A
antirez 已提交
4084
            "clients_in_timeout_table:%llu\r\n",
4085
            listLength(server.clients)-listLength(server.slaves),
4086
            maxin, maxout,
4087
            server.blocked_clients,
4088
            server.tracking_clients,
4089
            (unsigned long long) raxSize(server.clients_timeout_table));
4090 4091 4092 4093
    }

    /* Memory */
    if (allsections || defsections || !strcasecmp(section,"memory")) {
4094 4095
        char hmem[64];
        char peak_hmem[64];
4096
        char total_system_hmem[64];
4097
        char used_memory_lua_hmem[64];
4098
        char used_memory_scripts_hmem[64];
4099
        char used_memory_rss_hmem[64];
4100
        char maxmemory_hmem[64];
4101
        size_t zmalloc_used = zmalloc_used_memory();
4102
        size_t total_system_mem = server.system_memory_size;
T
therealbill 已提交
4103
        const char *evict_policy = evictPolicyToString();
4104
        long long memory_lua = server.lua ? (long long)lua_gc(server.lua,LUA_GCCOUNT,0)*1024 : 0;
4105
        struct redisMemOverhead *mh = getMemoryOverheadData();
4106

4107 4108 4109 4110 4111
        /* Peak memory is updated from time to time by serverCron() so it
         * may happen that the instantaneous value is slightly bigger than
         * the peak value. This may confuse users, so we update the peak
         * if found smaller than the current memory usage. */
        if (zmalloc_used > server.stat_peak_memory)
4112 4113 4114
            server.stat_peak_memory = zmalloc_used;

        bytesToHuman(hmem,zmalloc_used);
4115
        bytesToHuman(peak_hmem,server.stat_peak_memory);
4116
        bytesToHuman(total_system_hmem,total_system_mem);
4117
        bytesToHuman(used_memory_lua_hmem,memory_lua);
4118
        bytesToHuman(used_memory_scripts_hmem,mh->lua_caches);
4119
        bytesToHuman(used_memory_rss_hmem,server.cron_malloc_stats.process_rss);
4120
        bytesToHuman(maxmemory_hmem,server.maxmemory);
4121

4122 4123 4124 4125 4126 4127
        if (sections++) info = sdscat(info,"\r\n");
        info = sdscatprintf(info,
            "# Memory\r\n"
            "used_memory:%zu\r\n"
            "used_memory_human:%s\r\n"
            "used_memory_rss:%zu\r\n"
4128
            "used_memory_rss_human:%s\r\n"
4129 4130
            "used_memory_peak:%zu\r\n"
            "used_memory_peak_human:%s\r\n"
4131
            "used_memory_peak_perc:%.2f%%\r\n"
4132 4133 4134 4135
            "used_memory_overhead:%zu\r\n"
            "used_memory_startup:%zu\r\n"
            "used_memory_dataset:%zu\r\n"
            "used_memory_dataset_perc:%.2f%%\r\n"
4136 4137 4138
            "allocator_allocated:%zu\r\n"
            "allocator_active:%zu\r\n"
            "allocator_resident:%zu\r\n"
4139 4140
            "total_system_memory:%lu\r\n"
            "total_system_memory_human:%s\r\n"
A
antirez 已提交
4141
            "used_memory_lua:%lld\r\n"
4142
            "used_memory_lua_human:%s\r\n"
4143 4144 4145
            "used_memory_scripts:%lld\r\n"
            "used_memory_scripts_human:%s\r\n"
            "number_of_cached_scripts:%lu\r\n"
4146 4147 4148
            "maxmemory:%lld\r\n"
            "maxmemory_human:%s\r\n"
            "maxmemory_policy:%s\r\n"
4149 4150 4151
            "allocator_frag_ratio:%.2f\r\n"
            "allocator_frag_bytes:%zu\r\n"
            "allocator_rss_ratio:%.2f\r\n"
4152
            "allocator_rss_bytes:%zd\r\n"
4153
            "rss_overhead_ratio:%.2f\r\n"
4154
            "rss_overhead_bytes:%zd\r\n"
4155
            "mem_fragmentation_ratio:%.2f\r\n"
4156
            "mem_fragmentation_bytes:%zd\r\n"
4157 4158 4159 4160 4161
            "mem_not_counted_for_evict:%zu\r\n"
            "mem_replication_backlog:%zu\r\n"
            "mem_clients_slaves:%zu\r\n"
            "mem_clients_normal:%zu\r\n"
            "mem_aof_buffer:%zu\r\n"
4162
            "mem_allocator:%s\r\n"
O
oranagra 已提交
4163
            "active_defrag_running:%d\r\n"
4164
            "lazyfree_pending_objects:%zu\r\n",
4165
            zmalloc_used,
4166
            hmem,
4167
            server.cron_malloc_stats.process_rss,
4168
            used_memory_rss_hmem,
4169 4170
            server.stat_peak_memory,
            peak_hmem,
4171
            mh->peak_perc,
4172 4173 4174
            mh->overhead_total,
            mh->startup_allocated,
            mh->dataset,
4175
            mh->dataset_perc,
4176 4177 4178
            server.cron_malloc_stats.allocator_allocated,
            server.cron_malloc_stats.allocator_active,
            server.cron_malloc_stats.allocator_resident,
4179 4180
            (unsigned long)total_system_mem,
            total_system_hmem,
4181 4182
            memory_lua,
            used_memory_lua_hmem,
4183
            (long long) mh->lua_caches,
4184 4185
            used_memory_scripts_hmem,
            dictSize(server.lua_scripts),
4186 4187 4188
            server.maxmemory,
            maxmemory_hmem,
            evict_policy,
4189 4190 4191 4192 4193 4194
            mh->allocator_frag,
            mh->allocator_frag_bytes,
            mh->allocator_rss,
            mh->allocator_rss_bytes,
            mh->rss_extra,
            mh->rss_extra_bytes,
4195 4196 4197 4198 4199
            mh->total_frag,       /* This is the total RSS overhead, including
                                     fragmentation, but not just it. This field
                                     (and the next one) is named like that just
                                     for backward compatibility. */
            mh->total_frag_bytes,
4200 4201 4202 4203 4204
            freeMemoryGetNotCountedMemory(),
            mh->repl_backlog,
            mh->clients_slaves,
            mh->clients_normal,
            mh->aof_buffer,
4205
            ZMALLOC_LIB,
O
oranagra 已提交
4206
            server.active_defrag_running,
4207
            lazyfreeGetPendingObjectsCount()
4208 4209
        );
        freeMemoryOverheadData(mh);
4210 4211
    }

4212 4213 4214
    /* Persistence */
    if (allsections || defsections || !strcasecmp(section,"persistence")) {
        if (sections++) info = sdscat(info,"\r\n");
4215
        info = sdscatprintf(info,
4216 4217
            "# Persistence\r\n"
            "loading:%d\r\n"
4218 4219
            "rdb_changes_since_last_save:%lld\r\n"
            "rdb_bgsave_in_progress:%d\r\n"
Y
YAMAMOTO Takashi 已提交
4220
            "rdb_last_save_time:%jd\r\n"
4221
            "rdb_last_bgsave_status:%s\r\n"
Y
YAMAMOTO Takashi 已提交
4222 4223
            "rdb_last_bgsave_time_sec:%jd\r\n"
            "rdb_current_bgsave_time_sec:%jd\r\n"
4224
            "rdb_last_cow_size:%zu\r\n"
4225
            "aof_enabled:%d\r\n"
4226 4227
            "aof_rewrite_in_progress:%d\r\n"
            "aof_rewrite_scheduled:%d\r\n"
Y
YAMAMOTO Takashi 已提交
4228 4229
            "aof_last_rewrite_time_sec:%jd\r\n"
            "aof_current_rewrite_time_sec:%jd\r\n"
4230
            "aof_last_bgrewrite_status:%s\r\n"
4231
            "aof_last_write_status:%s\r\n"
O
Oran Agra 已提交
4232 4233 4234
            "aof_last_cow_size:%zu\r\n"
            "module_fork_in_progress:%d\r\n"
            "module_fork_last_cow_size:%zu\r\n",
4235 4236
            server.loading,
            server.dirty,
A
antirez 已提交
4237
            server.rdb_child_pid != -1,
Y
YAMAMOTO Takashi 已提交
4238
            (intmax_t)server.lastsave,
4239
            (server.lastbgsave_status == C_OK) ? "ok" : "err",
Y
YAMAMOTO Takashi 已提交
4240 4241 4242
            (intmax_t)server.rdb_save_time_last,
            (intmax_t)((server.rdb_child_pid == -1) ?
                -1 : time(NULL)-server.rdb_save_time_start),
4243
            server.stat_rdb_cow_bytes,
A
antirez 已提交
4244
            server.aof_state != AOF_OFF,
4245
            server.aof_child_pid != -1,
4246
            server.aof_rewrite_scheduled,
Y
YAMAMOTO Takashi 已提交
4247 4248 4249
            (intmax_t)server.aof_rewrite_time_last,
            (intmax_t)((server.aof_child_pid == -1) ?
                -1 : time(NULL)-server.aof_rewrite_time_start),
4250
            (server.aof_lastbgrewrite_status == C_OK) ? "ok" : "err",
4251
            (server.aof_last_write_status == C_OK) ? "ok" : "err",
O
Oran Agra 已提交
4252 4253 4254
            server.stat_aof_cow_bytes,
            server.module_child_pid != -1,
            server.stat_module_cow_bytes);
4255

4256
        if (server.aof_enabled) {
4257 4258 4259
            info = sdscatprintf(info,
                "aof_current_size:%lld\r\n"
                "aof_base_size:%lld\r\n"
4260 4261
                "aof_pending_rewrite:%d\r\n"
                "aof_buffer_length:%zu\r\n"
4262
                "aof_rewrite_buffer_length:%lu\r\n"
4263 4264
                "aof_pending_bio_fsync:%llu\r\n"
                "aof_delayed_fsync:%lu\r\n",
4265 4266 4267
                (long long) server.aof_current_size,
                (long long) server.aof_rewrite_base_size,
                server.aof_rewrite_scheduled,
A
antirez 已提交
4268
                sdslen(server.aof_buf),
4269
                aofRewriteBufferSize(),
A
antirez 已提交
4270
                bioPendingJobsOfType(BIO_AOF_FSYNC),
4271
                server.aof_delayed_fsync);
4272 4273
        }

4274 4275 4276 4277 4278 4279 4280
        if (server.loading) {
            double perc;
            time_t eta, elapsed;
            off_t remaining_bytes = server.loading_total_bytes-
                                    server.loading_loaded_bytes;

            perc = ((double)server.loading_loaded_bytes /
A
antirez 已提交
4281
                   (server.loading_total_bytes+1)) * 100;
4282

A
antirez 已提交
4283
            elapsed = time(NULL)-server.loading_start_time;
4284 4285 4286 4287
            if (elapsed == 0) {
                eta = 1; /* A fake 1 second figure if we don't have
                            enough info */
            } else {
A
antirez 已提交
4288
                eta = (elapsed*remaining_bytes)/(server.loading_loaded_bytes+1);
4289 4290 4291
            }

            info = sdscatprintf(info,
Y
YAMAMOTO Takashi 已提交
4292
                "loading_start_time:%jd\r\n"
4293 4294 4295
                "loading_total_bytes:%llu\r\n"
                "loading_loaded_bytes:%llu\r\n"
                "loading_loaded_perc:%.2f\r\n"
Y
YAMAMOTO Takashi 已提交
4296 4297
                "loading_eta_seconds:%jd\r\n",
                (intmax_t) server.loading_start_time,
4298 4299 4300
                (unsigned long long) server.loading_total_bytes,
                (unsigned long long) server.loading_loaded_bytes,
                perc,
Y
YAMAMOTO Takashi 已提交
4301
                (intmax_t)eta
4302 4303
            );
        }
4304
    }
4305 4306 4307 4308

    /* Stats */
    if (allsections || defsections || !strcasecmp(section,"stats")) {
        if (sections++) info = sdscat(info,"\r\n");
4309
        info = sdscatprintf(info,
4310 4311 4312
            "# Stats\r\n"
            "total_connections_received:%lld\r\n"
            "total_commands_processed:%lld\r\n"
4313
            "instantaneous_ops_per_sec:%lld\r\n"
4314 4315 4316 4317
            "total_net_input_bytes:%lld\r\n"
            "total_net_output_bytes:%lld\r\n"
            "instantaneous_input_kbps:%.2f\r\n"
            "instantaneous_output_kbps:%.2f\r\n"
4318
            "rejected_connections:%lld\r\n"
4319 4320 4321
            "sync_full:%lld\r\n"
            "sync_partial_ok:%lld\r\n"
            "sync_partial_err:%lld\r\n"
4322
            "expired_keys:%lld\r\n"
4323 4324
            "expired_stale_perc:%.2f\r\n"
            "expired_time_cap_reached_count:%lld\r\n"
4325
            "expire_cycle_cpu_milliseconds:%lld\r\n"
4326 4327 4328 4329
            "evicted_keys:%lld\r\n"
            "keyspace_hits:%lld\r\n"
            "keyspace_misses:%lld\r\n"
            "pubsub_channels:%ld\r\n"
4330
            "pubsub_patterns:%lu\r\n"
4331
            "latest_fork_usec:%lld\r\n"
4332
            "migrate_cached_sockets:%ld\r\n"
O
oranagra 已提交
4333 4334 4335 4336
            "slave_expires_tracked_keys:%zu\r\n"
            "active_defrag_hits:%lld\r\n"
            "active_defrag_misses:%lld\r\n"
            "active_defrag_key_hits:%lld\r\n"
4337
            "active_defrag_key_misses:%lld\r\n"
4338
            "tracking_total_keys:%lld\r\n"
4339
            "tracking_total_items:%lld\r\n"
4340
            "tracking_total_prefixes:%lld\r\n"
4341
            "unexpected_error_replies:%lld\r\n",
4342 4343
            server.stat_numconnections,
            server.stat_numcommands,
A
antirez 已提交
4344
            getInstantaneousMetric(STATS_METRIC_COMMAND),
4345 4346
            server.stat_net_input_bytes,
            server.stat_net_output_bytes,
A
antirez 已提交
4347 4348
            (float)getInstantaneousMetric(STATS_METRIC_NET_INPUT)/1024,
            (float)getInstantaneousMetric(STATS_METRIC_NET_OUTPUT)/1024,
4349
            server.stat_rejected_conn,
4350 4351 4352
            server.stat_sync_full,
            server.stat_sync_partial_ok,
            server.stat_sync_partial_err,
4353
            server.stat_expiredkeys,
4354 4355
            server.stat_expired_stale_perc*100,
            server.stat_expired_time_cap_reached_count,
4356
            server.stat_expire_cycle_time_used/1000,
4357 4358 4359 4360
            server.stat_evictedkeys,
            server.stat_keyspace_hits,
            server.stat_keyspace_misses,
            dictSize(server.pubsub_channels),
4361
            listLength(server.pubsub_patterns),
4362
            server.stat_fork_time,
4363
            dictSize(server.migrate_cached_sockets),
O
oranagra 已提交
4364 4365 4366 4367
            getSlaveKeyWithExpireCount(),
            server.stat_active_defrag_hits,
            server.stat_active_defrag_misses,
            server.stat_active_defrag_key_hits,
4368
            server.stat_active_defrag_key_misses,
4369
            (unsigned long long) trackingGetTotalKeys(),
4370
            (unsigned long long) trackingGetTotalItems(),
4371
            (unsigned long long) trackingGetTotalPrefixes(),
4372
            server.stat_unexpected_error_replies);
4373
    }
A
antirez 已提交
4374

4375 4376 4377 4378 4379 4380 4381 4382
    /* Replication */
    if (allsections || defsections || !strcasecmp(section,"replication")) {
        if (sections++) info = sdscat(info,"\r\n");
        info = sdscatprintf(info,
            "# Replication\r\n"
            "role:%s\r\n",
            server.masterhost == NULL ? "master" : "slave");
        if (server.masterhost) {
4383 4384 4385 4386 4387 4388 4389
            long long slave_repl_offset = 1;

            if (server.master)
                slave_repl_offset = server.master->reploff;
            else if (server.cached_master)
                slave_repl_offset = server.cached_master->reploff;

4390 4391 4392 4393 4394 4395
            info = sdscatprintf(info,
                "master_host:%s\r\n"
                "master_port:%d\r\n"
                "master_link_status:%s\r\n"
                "master_last_io_seconds_ago:%d\r\n"
                "master_sync_in_progress:%d\r\n"
4396
                "slave_repl_offset:%lld\r\n"
4397 4398
                ,server.masterhost,
                server.masterport,
A
antirez 已提交
4399
                (server.repl_state == REPL_STATE_CONNECTED) ?
4400 4401
                    "up" : "down",
                server.master ?
4402
                ((int)(server.unixtime-server.master->lastinteraction)) : -1,
A
antirez 已提交
4403
                server.repl_state == REPL_STATE_TRANSFER,
4404
                slave_repl_offset
4405 4406
            );

A
antirez 已提交
4407
            if (server.repl_state == REPL_STATE_TRANSFER) {
4408
                info = sdscatprintf(info,
4409
                    "master_sync_left_bytes:%lld\r\n"
4410
                    "master_sync_last_io_seconds_ago:%d\r\n"
4411 4412
                    , (long long)
                        (server.repl_transfer_size - server.repl_transfer_read),
4413
                    (int)(server.unixtime-server.repl_transfer_lastio)
4414 4415
                );
            }
4416

A
antirez 已提交
4417
            if (server.repl_state != REPL_STATE_CONNECTED) {
4418
                info = sdscatprintf(info,
Y
YAMAMOTO Takashi 已提交
4419
                    "master_link_down_since_seconds:%jd\r\n",
O
Oran Agra 已提交
4420
                    (intmax_t)(server.unixtime-server.repl_down_since));
4421
            }
4422
            info = sdscatprintf(info,
4423 4424 4425 4426
                "slave_priority:%d\r\n"
                "slave_read_only:%d\r\n",
                server.slave_priority,
                server.repl_slave_ro);
A
antirez 已提交
4427
        }
4428

4429
        info = sdscatprintf(info,
4430
            "connected_slaves:%lu\r\n",
4431
            listLength(server.slaves));
4432 4433 4434 4435 4436 4437 4438 4439 4440 4441

        /* If min-slaves-to-write is active, write the number of slaves
         * currently considered 'good'. */
        if (server.repl_min_slaves_to_write &&
            server.repl_min_slaves_max_lag) {
            info = sdscatprintf(info,
                "min_slaves_good_slaves:%d\r\n",
                server.repl_good_slaves_count);
        }

4442 4443 4444 4445 4446 4447 4448
        if (listLength(server.slaves)) {
            int slaveid = 0;
            listNode *ln;
            listIter li;

            listRewind(server.slaves,&li);
            while((ln = listNext(&li))) {
4449
                client *slave = listNodeValue(ln);
4450
                char *state = NULL;
4451
                char ip[NET_IP_STR_LEN], *slaveip = slave->slave_ip;
4452
                int port;
4453
                long lag = 0;
4454

4455
                if (slaveip[0] == '\0') {
4456
                    if (connPeerToString(slave->conn,ip,sizeof(ip),&port) == -1)
4457 4458 4459
                        continue;
                    slaveip = ip;
                }
4460
                switch(slave->replstate) {
A
antirez 已提交
4461 4462
                case SLAVE_STATE_WAIT_BGSAVE_START:
                case SLAVE_STATE_WAIT_BGSAVE_END:
4463 4464
                    state = "wait_bgsave";
                    break;
A
antirez 已提交
4465
                case SLAVE_STATE_SEND_BULK:
4466 4467
                    state = "send_bulk";
                    break;
A
antirez 已提交
4468
                case SLAVE_STATE_ONLINE:
4469 4470 4471 4472
                    state = "online";
                    break;
                }
                if (state == NULL) continue;
A
antirez 已提交
4473
                if (slave->replstate == SLAVE_STATE_ONLINE)
4474 4475 4476 4477
                    lag = time(NULL) - slave->repl_ack_time;

                info = sdscatprintf(info,
                    "slave%d:ip=%s,port=%d,state=%s,"
4478
                    "offset=%lld,lag=%ld\r\n",
4479
                    slaveid,slaveip,slave->slave_listening_port,state,
4480
                    slave->repl_ack_off, lag);
4481 4482 4483
                slaveid++;
            }
        }
4484
        info = sdscatprintf(info,
4485 4486
            "master_replid:%s\r\n"
            "master_replid2:%s\r\n"
4487
            "master_repl_offset:%lld\r\n"
4488
            "second_repl_offset:%lld\r\n"
4489 4490 4491 4492
            "repl_backlog_active:%d\r\n"
            "repl_backlog_size:%lld\r\n"
            "repl_backlog_first_byte_offset:%lld\r\n"
            "repl_backlog_histlen:%lld\r\n",
4493 4494
            server.replid,
            server.replid2,
4495
            server.master_repl_offset,
4496
            server.second_replid_offset,
4497 4498 4499 4500
            server.repl_backlog != NULL,
            server.repl_backlog_size,
            server.repl_backlog_off,
            server.repl_backlog_histlen);
A
antirez 已提交
4501 4502
    }

4503 4504
    /* CPU */
    if (allsections || defsections || !strcasecmp(section,"cpu")) {
4505 4506
        if (sections++) info = sdscat(info,"\r\n");
        info = sdscatprintf(info,
4507
        "# CPU\r\n"
4508 4509 4510 4511 4512 4513 4514 4515
        "used_cpu_sys:%ld.%06ld\r\n"
        "used_cpu_user:%ld.%06ld\r\n"
        "used_cpu_sys_children:%ld.%06ld\r\n"
        "used_cpu_user_children:%ld.%06ld\r\n",
        (long)self_ru.ru_stime.tv_sec, (long)self_ru.ru_stime.tv_usec,
        (long)self_ru.ru_utime.tv_sec, (long)self_ru.ru_utime.tv_usec,
        (long)c_ru.ru_stime.tv_sec, (long)c_ru.ru_stime.tv_usec,
        (long)c_ru.ru_utime.tv_sec, (long)c_ru.ru_utime.tv_usec);
4516
    }
4517

4518 4519 4520 4521 4522 4523 4524
    /* Modules */
    if (allsections || defsections || !strcasecmp(section,"modules")) {
        if (sections++) info = sdscat(info,"\r\n");
        info = sdscatprintf(info,"# Modules\r\n");
        info = genModulesInfoString(info);
    }

4525
    /* Command statistics */
4526 4527 4528 4529
    if (allsections || !strcasecmp(section,"commandstats")) {
        if (sections++) info = sdscat(info,"\r\n");
        info = sdscatprintf(info, "# Commandstats\r\n");

4530 4531 4532 4533 4534 4535
        struct redisCommand *c;
        dictEntry *de;
        dictIterator *di;
        di = dictGetSafeIterator(server.commands);
        while((de = dictNext(di)) != NULL) {
            c = (struct redisCommand *) dictGetVal(de);
4536 4537 4538 4539 4540
            if (!c->calls) continue;
            info = sdscatprintf(info,
                "cmdstat_%s:calls=%lld,usec=%lld,usec_per_call=%.2f\r\n",
                c->name, c->calls, c->microseconds,
                (c->calls == 0) ? 0 : ((float)c->microseconds/c->calls));
4541
        }
4542
        dictReleaseIterator(di);
A
antirez 已提交
4543 4544
    }

4545
    /* Cluster */
4546 4547 4548 4549 4550 4551 4552 4553
    if (allsections || defsections || !strcasecmp(section,"cluster")) {
        if (sections++) info = sdscat(info,"\r\n");
        info = sdscatprintf(info,
        "# Cluster\r\n"
        "cluster_enabled:%d\r\n",
        server.cluster_enabled);
    }

4554 4555 4556 4557 4558 4559
    /* Key space */
    if (allsections || defsections || !strcasecmp(section,"keyspace")) {
        if (sections++) info = sdscat(info,"\r\n");
        info = sdscatprintf(info, "# Keyspace\r\n");
        for (j = 0; j < server.dbnum; j++) {
            long long keys, vkeys;
4560

4561 4562 4563
            keys = dictSize(server.db[j].dict);
            vkeys = dictSize(server.db[j].expires);
            if (keys || vkeys) {
4564 4565 4566
                info = sdscatprintf(info,
                    "db%d:keys=%lld,expires=%lld,avg_ttl=%lld\r\n",
                    j, keys, vkeys, server.db[j].avg_ttl);
4567
            }
4568 4569
        }
    }
4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580

    /* Get info from modules.
     * if user asked for "everything" or "modules", or a specific section
     * that's not found yet. */
    if (everything || modules ||
        (!allsections && !defsections && sections==0)) {
        info = modulesCollectInfo(info,
                                  everything || modules ? NULL: section,
                                  0, /* not a crash report */
                                  sections);
    }
4581 4582 4583
    return info;
}

4584
void infoCommand(client *c) {
4585 4586 4587 4588 4589 4590
    char *section = c->argc == 2 ? c->argv[1]->ptr : "default";

    if (c->argc > 2) {
        addReply(c,shared.syntaxerr);
        return;
    }
A
antirez 已提交
4591 4592 4593
    sds info = genRedisInfoString(section);
    addReplyVerbatim(c,info,sdslen(info),"txt");
    sdsfree(info);
4594 4595
}

4596
void monitorCommand(client *c) {
4597
    /* ignore MONITOR if already slave or in monitor mode */
A
antirez 已提交
4598
    if (c->flags & CLIENT_SLAVE) return;
4599

A
antirez 已提交
4600
    c->flags |= (CLIENT_SLAVE|CLIENT_MONITOR);
4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621
    listAddNodeTail(server.monitors,c);
    addReply(c,shared.ok);
}

/* =================================== Main! ================================ */

#ifdef __linux__
int linuxOvercommitMemoryValue(void) {
    FILE *fp = fopen("/proc/sys/vm/overcommit_memory","r");
    char buf[64];

    if (!fp) return -1;
    if (fgets(buf,64,fp) == NULL) {
        fclose(fp);
        return -1;
    }
    fclose(fp);

    return atoi(buf);
}

4622
void linuxMemoryWarnings(void) {
4623
    if (linuxOvercommitMemoryValue() == 0) {
A
antirez 已提交
4624
        serverLog(LL_WARNING,"WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.");
4625
    }
4626
    if (THPIsEnabled()) {
A
antirez 已提交
4627
        serverLog(LL_WARNING,"WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.");
4628
    }
4629 4630 4631
}
#endif /* __linux__ */

4632
void createPidFile(void) {
4633 4634
    /* If pidfile requested, but no pidfile defined, use
     * default pidfile path */
4635
    if (!server.pidfile) server.pidfile = zstrdup(CONFIG_DEFAULT_PID_FILE);
4636

4637 4638 4639
    /* Try to write the pid file in a best-effort way. */
    FILE *fp = fopen(server.pidfile,"w");
    if (fp) {
4640
        fprintf(fp,"%d\n",(int)getpid());
4641 4642 4643 4644
        fclose(fp);
    }
}

4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661
void daemonize(void) {
    int fd;

    if (fork() != 0) exit(0); /* parent exits */
    setsid(); /* create a new session */

    /* Every output goes to /dev/null. If Redis is daemonized but
     * the 'logfile' is set to 'stdout' in the configuration file
     * it will not log at all. */
    if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
        dup2(fd, STDIN_FILENO);
        dup2(fd, STDOUT_FILENO);
        dup2(fd, STDERR_FILENO);
        if (fd > STDERR_FILENO) close(fd);
    }
}

4662
void version(void) {
4663
    printf("Redis server v=%s sha=%s:%d malloc=%s bits=%d build=%llx\n",
A
antirez 已提交
4664 4665 4666 4667
        REDIS_VERSION,
        redisGitSHA1(),
        atoi(redisGitDirty()) > 0,
        ZMALLOC_LIB,
4668
        sizeof(long) == 4 ? 32 : 64,
4669
        (unsigned long long) redisBuildId());
4670 4671 4672
    exit(0);
}

4673
void usage(void) {
4674
    fprintf(stderr,"Usage: ./redis-server [/path/to/redis.conf] [options]\n");
4675
    fprintf(stderr,"       ./redis-server - (read config from stdin)\n");
4676
    fprintf(stderr,"       ./redis-server -v or --version\n");
4677 4678
    fprintf(stderr,"       ./redis-server -h or --help\n");
    fprintf(stderr,"       ./redis-server --test-memory <megabytes>\n\n");
4679 4680 4681 4682
    fprintf(stderr,"Examples:\n");
    fprintf(stderr,"       ./redis-server (run the server with default conf)\n");
    fprintf(stderr,"       ./redis-server /etc/redis/6379.conf\n");
    fprintf(stderr,"       ./redis-server --port 7777\n");
A
antirez 已提交
4683
    fprintf(stderr,"       ./redis-server --port 7777 --replicaof 127.0.0.1 8888\n");
4684 4685 4686
    fprintf(stderr,"       ./redis-server /etc/myredis.conf --loglevel verbose\n\n");
    fprintf(stderr,"Sentinel mode:\n");
    fprintf(stderr,"       ./redis-server /etc/sentinel.conf --sentinel\n");
4687 4688 4689
    exit(1);
}

A
antirez 已提交
4690 4691 4692
void redisAsciiArt(void) {
#include "asciilogo.h"
    char *buf = zmalloc(1024*16);
4693
    char *mode;
4694 4695 4696

    if (server.cluster_enabled) mode = "cluster";
    else if (server.sentinel_mode) mode = "sentinel";
4697
    else mode = "standalone";
A
antirez 已提交
4698

4699 4700 4701 4702 4703 4704 4705 4706 4707
    /* Show the ASCII logo if: log file is stdout AND stdout is a
     * tty AND syslog logging is disabled. Also show logo if the user
     * forced us to do so via redis.conf. */
    int show_logo = ((!server.syslog_enabled &&
                      server.logfile[0] == '\0' &&
                      isatty(fileno(stdout))) ||
                     server.always_show_logo);

    if (!show_logo) {
A
antirez 已提交
4708
        serverLog(LL_NOTICE,
4709
            "Running mode=%s, port=%d.",
Y
Yossi Gottlieb 已提交
4710
            mode, server.port ? server.port : server.tls_port
4711 4712 4713 4714 4715 4716 4717
        );
    } else {
        snprintf(buf,1024*16,ascii_logo,
            REDIS_VERSION,
            redisGitSHA1(),
            strtol(redisGitDirty(),NULL,10) > 0,
            (sizeof(long) == 8) ? "64" : "32",
4718
            mode, server.port ? server.port : server.tls_port,
4719 4720
            (long) getpid()
        );
A
antirez 已提交
4721
        serverLogRaw(LL_NOTICE|LL_RAW,buf);
4722
    }
A
antirez 已提交
4723 4724 4725
    zfree(buf);
}

4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739
static void sigShutdownHandler(int sig) {
    char *msg;

    switch (sig) {
    case SIGINT:
        msg = "Received SIGINT scheduling shutdown...";
        break;
    case SIGTERM:
        msg = "Received SIGTERM scheduling shutdown...";
        break;
    default:
        msg = "Received shutdown signal, scheduling shutdown...";
    };

4740 4741 4742 4743 4744
    /* SIGINT is often delivered via Ctrl+C in an interactive session.
     * If we receive the signal the second time, we interpret this as
     * the user really wanting to quit ASAP without waiting to persist
     * on disk. */
    if (server.shutdown_asap && sig == SIGINT) {
A
antirez 已提交
4745
        serverLogFromHandler(LL_WARNING, "You insist... exiting now.");
4746 4747 4748
        rdbRemoveTempFile(getpid());
        exit(1); /* Exit with an error since this was not a clean shutdown. */
    } else if (server.loading) {
4749
        serverLogFromHandler(LL_WARNING, "Received shutdown signal during loading, exiting now.");
4750 4751 4752
        exit(0);
    }

A
antirez 已提交
4753
    serverLogFromHandler(LL_WARNING, msg);
4754 4755 4756
    server.shutdown_asap = 1;
}

4757
void setupSignalHandlers(void) {
4758 4759
    struct sigaction act;

4760 4761 4762
    /* When the SA_SIGINFO flag is set in sa_flags then sa_sigaction is used.
     * Otherwise, sa_handler is used. */
    sigemptyset(&act.sa_mask);
4763
    act.sa_flags = 0;
4764
    act.sa_handler = sigShutdownHandler;
4765
    sigaction(SIGTERM, &act, NULL);
4766
    sigaction(SIGINT, &act, NULL);
4767

4768 4769
#ifdef HAVE_BACKTRACE
    sigemptyset(&act.sa_mask);
4770
    act.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
4771 4772 4773 4774 4775 4776 4777
    act.sa_sigaction = sigsegvHandler;
    sigaction(SIGSEGV, &act, NULL);
    sigaction(SIGBUS, &act, NULL);
    sigaction(SIGFPE, &act, NULL);
    sigaction(SIGILL, &act, NULL);
#endif
    return;
4778 4779
}

4780 4781 4782 4783
/* This is the signal handler for children process. It is currently useful
 * in order to track the SIGUSR1, that we send to a child in order to terminate
 * it in a clean way, without the parent detecting an error and stop
 * accepting writes because of a write error condition. */
O
Oran Agra 已提交
4784 4785 4786
static void sigKillChildHandler(int sig) {
    UNUSED(sig);
    serverLogFromHandler(LL_WARNING, "Received SIGUSR1 in child, exiting now.");
4787
    exitFromChild(SERVER_CHILD_NOERROR_RETVAL);
O
Oran Agra 已提交
4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834
}

void setupChildSignalHandlers(void) {
    struct sigaction act;

    /* When the SA_SIGINFO flag is set in sa_flags then sa_sigaction is used.
     * Otherwise, sa_handler is used. */
    sigemptyset(&act.sa_mask);
    act.sa_flags = 0;
    act.sa_handler = sigKillChildHandler;
    sigaction(SIGUSR1, &act, NULL);
    return;
}

int redisFork() {
    int childpid;
    long long start = ustime();
    if ((childpid = fork()) == 0) {
        /* Child */
        closeListeningSockets(0);
        setupChildSignalHandlers();
    } else {
        /* Parent */
        server.stat_fork_time = ustime()-start;
        server.stat_fork_rate = (double) zmalloc_used_memory() * 1000000 / server.stat_fork_time / (1024*1024*1024); /* GB per second. */
        latencyAddSampleIfNeeded("fork",server.stat_fork_time/1000);
        if (childpid == -1) {
            return -1;
        }
        updateDictResizePolicy();
    }
    return childpid;
}

void sendChildCOWInfo(int ptype, char *pname) {
    size_t private_dirty = zmalloc_get_private_dirty(-1);

    if (private_dirty) {
        serverLog(LL_NOTICE,
            "%s: %zu MB of memory used by copy-on-write",
            pname, private_dirty/(1024*1024));
    }

    server.child_info_data.cow_size = private_dirty;
    sendChildInfo(ptype);
}

4835 4836
void memtest(size_t megabytes, int passes);

4837
/* Returns 1 if there is --sentinel among the arguments or if
4838
 * argv[0] contains "redis-sentinel". */
4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850
int checkForSentinelMode(int argc, char **argv) {
    int j;

    if (strstr(argv[0],"redis-sentinel") != NULL) return 1;
    for (j = 1; j < argc; j++)
        if (!strcmp(argv[j],"--sentinel")) return 1;
    return 0;
}

/* Function called at startup to load RDB or AOF file in memory. */
void loadDataFromDisk(void) {
    long long start = ustime();
A
antirez 已提交
4851
    if (server.aof_state == AOF_ON) {
4852
        if (loadAppendOnlyFile(server.aof_filename) == C_OK)
A
antirez 已提交
4853
            serverLog(LL_NOTICE,"DB loaded from append only file: %.3f seconds",(float)(ustime()-start)/1000000);
4854
    } else {
4855
        rdbSaveInfo rsi = RDB_SAVE_INFO_INIT;
4856
        if (rdbLoad(server.rdb_filename,&rsi,RDBFLAGS_NONE) == C_OK) {
A
antirez 已提交
4857
            serverLog(LL_NOTICE,"DB loaded from disk: %.3f seconds",
4858
                (float)(ustime()-start)/1000000);
4859 4860

            /* Restore the replication ID / offset from the RDB file. */
4861 4862 4863
            if ((server.masterhost ||
                (server.cluster_enabled &&
                nodeIsSlave(server.cluster->myself))) &&
4864 4865 4866
                rsi.repl_id_is_set &&
                rsi.repl_offset != -1 &&
                /* Note that older implementations may save a repl_stream_db
4867 4868
                 * of -1 inside the RDB file in a wrong way, see more
                 * information in function rdbPopulateSaveInfo. */
4869 4870
                rsi.repl_stream_db != -1)
            {
4871 4872 4873 4874 4875
                memcpy(server.replid,rsi.repl_id,sizeof(server.replid));
                server.master_repl_offset = rsi.repl_offset;
                /* If we are a slave, create a cached master from this
                 * information, in order to allow partial resynchronizations
                 * with masters. */
4876 4877
                replicationCacheMasterUsingMyself();
                selectDb(server.cached_master,rsi.repl_stream_db);
4878
            }
4879
        } else if (errno != ENOENT) {
A
antirez 已提交
4880
            serverLog(LL_WARNING,"Fatal error loading the DB: %s. Exiting.",strerror(errno));
4881 4882 4883 4884 4885
            exit(1);
        }
    }
}

A
antirez 已提交
4886
void redisOutOfMemoryHandler(size_t allocation_size) {
A
antirez 已提交
4887
    serverLog(LL_WARNING,"Out Of Memory allocating %zu bytes!",
A
antirez 已提交
4888
        allocation_size);
A
antirez 已提交
4889
    serverPanic("Redis aborting for OUT OF MEMORY");
A
antirez 已提交
4890 4891
}

4892
void redisSetProcTitle(char *title) {
4893
#ifdef USE_SETPROCTITLE
4894 4895 4896 4897 4898
    char *server_mode = "";
    if (server.cluster_enabled) server_mode = " [cluster]";
    else if (server.sentinel_mode) server_mode = " [sentinel]";

    setproctitle("%s %s:%d%s",
4899
        title,
A
antirez 已提交
4900
        server.bindaddr_count ? server.bindaddr[0] : "*",
4901
        server.port ? server.port : server.tls_port,
4902
        server_mode);
4903
#else
A
antirez 已提交
4904
    UNUSED(title);
4905
#endif
4906 4907
}

Z
zhenwei pi 已提交
4908 4909 4910 4911 4912 4913 4914 4915
void redisSetCpuAffinity(const char *cpulist) {
#ifdef USE_SETCPUAFFINITY
    setcpuaffinity(cpulist);
#else
    UNUSED(cpulist);
#endif
}

4916 4917 4918
/*
 * Check whether systemd or upstart have been used to start redis.
 */
4919 4920

int redisSupervisedUpstart(void) {
4921
    const char *upstart_job = getenv("UPSTART_JOB");
4922 4923

    if (!upstart_job) {
A
antirez 已提交
4924
        serverLog(LL_WARNING,
4925 4926 4927 4928
                "upstart supervision requested, but UPSTART_JOB not found");
        return 0;
    }

I
Itamar Haber 已提交
4929
    serverLog(LL_NOTICE, "supervised by upstart, will stop to signal readiness");
4930 4931 4932 4933 4934
    raise(SIGSTOP);
    unsetenv("UPSTART_JOB");
    return 1;
}

4935
int redisCommunicateSystemd(const char *sd_notify_msg) {
4936
    const char *notify_socket = getenv("NOTIFY_SOCKET");
4937
    if (!notify_socket) {
A
antirez 已提交
4938
        serverLog(LL_WARNING,
4939
                "systemd supervision requested, but NOTIFY_SOCKET not found");
4940 4941
    }

4942 4943 4944 4945 4946
    #ifdef HAVE_LIBSYSTEMD
    (void) sd_notify(0, sd_notify_msg);
    #else
    UNUSED(sd_notify_msg);
    #endif
4947
    return 0;
4948 4949
}

4950
int redisIsSupervised(int mode) {
A
antirez 已提交
4951
    if (mode == SUPERVISED_AUTODETECT) {
4952 4953 4954 4955 4956 4957
        const char *upstart_job = getenv("UPSTART_JOB");
        const char *notify_socket = getenv("NOTIFY_SOCKET");

        if (upstart_job) {
            redisSupervisedUpstart();
        } else if (notify_socket) {
4958 4959 4960 4961
            server.supervised_mode = SUPERVISED_SYSTEMD;
            serverLog(LL_WARNING,
                "WARNING auto-supervised by systemd - you MUST set appropriate values for TimeoutStartSec and TimeoutStopSec in your service unit.");
            return redisCommunicateSystemd("STATUS=Redis is loading...\n");
4962
        }
A
antirez 已提交
4963
    } else if (mode == SUPERVISED_UPSTART) {
4964
        return redisSupervisedUpstart();
A
antirez 已提交
4965
    } else if (mode == SUPERVISED_SYSTEMD) {
4966 4967 4968
        serverLog(LL_WARNING,
            "WARNING supervised by systemd - you MUST set appropriate values for TimeoutStartSec and TimeoutStopSec in your service unit.");
        return redisCommunicateSystemd("STATUS=Redis is loading...\n");
4969 4970 4971 4972 4973
    }

    return 0;
}

4974 4975 4976 4977 4978
int iAmMaster(void) {
    return ((!server.cluster_enabled && server.masterhost == NULL) ||
            (server.cluster_enabled && nodeIsMaster(server.cluster->myself)));
}

4979

4980
int main(int argc, char **argv) {
4981
    struct timeval tv;
A
antirez 已提交
4982
    int j;
4983

4984 4985 4986 4987
#ifdef REDIS_TEST
    if (argc == 3 && !strcasecmp(argv[1], "test")) {
        if (!strcasecmp(argv[2], "ziplist")) {
            return ziplistTest(argc, argv);
M
Matt Stancliff 已提交
4988 4989
        } else if (!strcasecmp(argv[2], "quicklist")) {
            quicklistTest(argc, argv);
4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001
        } else if (!strcasecmp(argv[2], "intset")) {
            return intsetTest(argc, argv);
        } else if (!strcasecmp(argv[2], "zipmap")) {
            return zipmapTest(argc, argv);
        } else if (!strcasecmp(argv[2], "sha1test")) {
            return sha1Test(argc, argv);
        } else if (!strcasecmp(argv[2], "util")) {
            return utilTest(argc, argv);
        } else if (!strcasecmp(argv[2], "endianconv")) {
            return endianconvTest(argc, argv);
        } else if (!strcasecmp(argv[2], "crc64")) {
            return crc64Test(argc, argv);
5002 5003
        } else if (!strcasecmp(argv[2], "zmalloc")) {
            return zmalloc_test(argc, argv);
5004 5005 5006 5007 5008 5009
        }

        return -1; /* test not found */
    }
#endif

A
antirez 已提交
5010
    /* We need to initialize our libraries, and the server configuration. */
5011 5012 5013
#ifdef INIT_SETPROCTITLE_REPLACEMENT
    spt_init(argc, argv);
#endif
5014
    setlocale(LC_COLLATE,"");
5015
    tzset(); /* Populates 'timezone' global. */
A
antirez 已提交
5016
    zmalloc_set_oom_handler(redisOutOfMemoryHandler);
5017 5018
    srand(time(NULL)^getpid());
    gettimeofday(&tv,NULL);
A
antirez 已提交
5019
    crc64_init();
5020

M
Mike A. Owens 已提交
5021 5022 5023
    uint8_t hashseed[16];
    getRandomBytes(hashseed,sizeof(hashseed));
    dictSetHashFunctionSeed(hashseed);
5024
    server.sentinel_mode = checkForSentinelMode(argc,argv);
5025
    initServerConfig();
A
antirez 已提交
5026 5027
    ACLInit(); /* The ACL subsystem must be initialized ASAP because the
                  basic networking code and client creation depends on it. */
A
antirez 已提交
5028
    moduleInitModulesSystem();
5029
    tlsInit();
5030

A
antirez 已提交
5031 5032 5033 5034 5035 5036 5037
    /* Store the executable path and arguments in a safe place in order
     * to be able to restart the server later. */
    server.executable = getAbsolutePath(argv[0]);
    server.exec_argv = zmalloc(sizeof(char*)*(argc+1));
    server.exec_argv[argc] = NULL;
    for (j = 0; j < argc; j++) server.exec_argv[j] = zstrdup(argv[j]);

5038 5039 5040 5041 5042 5043 5044 5045
    /* We need to init sentinel right now as parsing the configuration file
     * in sentinel mode will have the effect of populating the sentinel
     * data structures with master nodes to monitor. */
    if (server.sentinel_mode) {
        initSentinelConfig();
        initSentinel();
    }

5046
    /* Check if we need to start in redis-check-rdb/aof mode. We just execute
5047 5048 5049
     * the program main. However the program is part of the Redis executable
     * so that we can easily execute an RDB check on loading errors. */
    if (strstr(argv[0],"redis-check-rdb") != NULL)
5050 5051 5052
        redis_check_rdb_main(argc,argv,NULL);
    else if (strstr(argv[0],"redis-check-aof") != NULL)
        redis_check_aof_main(argc,argv);
5053

5054
    if (argc >= 2) {
A
antirez 已提交
5055
        j = 1; /* First option to parse in argv[] */
5056 5057 5058 5059
        sds options = sdsempty();
        char *configfile = NULL;

        /* Handle special options --help and --version */
5060 5061
        if (strcmp(argv[1], "-v") == 0 ||
            strcmp(argv[1], "--version") == 0) version();
5062 5063
        if (strcmp(argv[1], "--help") == 0 ||
            strcmp(argv[1], "-h") == 0) usage();
5064 5065
        if (strcmp(argv[1], "--test-memory") == 0) {
            if (argc == 3) {
5066
                memtest(atoi(argv[2]),50);
5067 5068 5069 5070 5071 5072 5073 5074
                exit(0);
            } else {
                fprintf(stderr,"Please specify the amount of memory to test in megabytes.\n");
                fprintf(stderr,"Example: ./redis-server --test-memory 4096\n\n");
                exit(1);
            }
        }

5075
        /* First argument is the config file name? */
A
antirez 已提交
5076 5077 5078 5079
        if (argv[j][0] != '-' || argv[j][1] != '-') {
            configfile = argv[j];
            server.configfile = getAbsolutePath(configfile);
            /* Replace the config file in server.exec_argv with
J
Jack Drogon 已提交
5080
             * its absolute path. */
A
antirez 已提交
5081 5082 5083 5084 5085
            zfree(server.exec_argv[j]);
            server.exec_argv[j] = zstrdup(server.configfile);
            j++;
        }

5086 5087 5088 5089 5090 5091 5092
        /* All the other options are parsed and conceptually appended to the
         * configuration file. For instance --port 6380 will generate the
         * string "port 6380\n" to be parsed after the actual file name
         * is parsed, if any. */
        while(j != argc) {
            if (argv[j][0] == '-' && argv[j][1] == '-') {
                /* Option name */
5093 5094 5095 5096 5097
                if (!strcmp(argv[j], "--check-rdb")) {
                    /* Argument has no options, need to skip for parsing. */
                    j++;
                    continue;
                }
5098 5099 5100 5101 5102 5103 5104 5105 5106 5107
                if (sdslen(options)) options = sdscat(options,"\n");
                options = sdscat(options,argv[j]+2);
                options = sdscat(options," ");
            } else {
                /* Option argument */
                options = sdscatrepr(options,argv[j],strlen(argv[j]));
                options = sdscat(options," ");
            }
            j++;
        }
5108
        if (server.sentinel_mode && configfile && *configfile == '-') {
A
antirez 已提交
5109
            serverLog(LL_WARNING,
5110
                "Sentinel config from STDIN not allowed.");
A
antirez 已提交
5111
            serverLog(LL_WARNING,
5112 5113 5114
                "Sentinel needs config file on disk to save state.  Exiting...");
            exit(1);
        }
5115
        resetServerSaveParams();
5116 5117
        loadServerConfig(configfile,options);
        sdsfree(options);
5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129
    }

    serverLog(LL_WARNING, "oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo");
    serverLog(LL_WARNING,
        "Redis version=%s, bits=%d, commit=%s, modified=%d, pid=%d, just started",
            REDIS_VERSION,
            (sizeof(long) == 8) ? 64 : 32,
            redisGitSHA1(),
            strtol(redisGitDirty(),NULL,10) > 0,
            (int)getpid());

    if (argc == 1) {
A
antirez 已提交
5130
        serverLog(LL_WARNING, "Warning: no config file specified, using the default config. In order to specify a config file use %s /path/to/%s.conf", argv[0], server.sentinel_mode ? "sentinel" : "redis");
5131 5132
    } else {
        serverLog(LL_WARNING, "Configuration loaded");
5133
    }
5134

5135 5136
    server.supervised = redisIsSupervised(server.supervised_mode);
    int background = server.daemonize && !server.supervised;
R
rebx 已提交
5137
    if (background) daemonize();
5138

5139
    initServer();
R
rebx 已提交
5140
    if (background || server.pidfile) createPidFile();
5141
    redisSetProcTitle(argv[0]);
5142
    redisAsciiArt();
5143
    checkTcpBacklogSettings();
5144 5145

    if (!server.sentinel_mode) {
A
antirez 已提交
5146
        /* Things not needed when running in Sentinel mode. */
5147
        serverLog(LL_WARNING,"Server initialized");
5148
    #ifdef __linux__
5149
        linuxMemoryWarnings();
5150
    #endif
A
antirez 已提交
5151
        moduleLoadFromQueue();
5152
        ACLLoadUsersAtStartup();
5153
        InitServerLast();
5154
        loadDataFromDisk();
5155
        if (server.cluster_enabled) {
5156
            if (verifyClusterConfigWithData() == C_ERR) {
A
antirez 已提交
5157
                serverLog(LL_WARNING,
5158 5159 5160 5161 5162
                    "You can't have keys in a DB different than DB 0 when in "
                    "Cluster mode. Exiting.");
                exit(1);
            }
        }
5163
        if (server.ipfd_count > 0 || server.tlsfd_count > 0)
5164
            serverLog(LL_NOTICE,"Ready to accept connections");
5165
        if (server.sofd > 0)
A
antirez 已提交
5166
            serverLog(LL_NOTICE,"The server is now ready to accept connections at %s", server.unixsocket);
5167 5168 5169 5170 5171 5172 5173 5174
        if (server.supervised_mode == SUPERVISED_SYSTEMD) {
            if (!server.masterhost) {
                redisCommunicateSystemd("STATUS=Ready to accept connections\n");
                redisCommunicateSystemd("READY=1\n");
            } else {
                redisCommunicateSystemd("STATUS=Waiting for MASTER <-> REPLICA sync\n");
            }
        }
5175
    } else {
5176
        InitServerLast();
A
antirez 已提交
5177
        sentinelIsRunning();
5178
    }
5179

5180 5181
    /* Warning the user about suspicious maxmemory setting. */
    if (server.maxmemory > 0 && server.maxmemory < 1024*1024) {
A
antirez 已提交
5182
        serverLog(LL_WARNING,"WARNING: You specified a maxmemory value that is less than 1MB (current value is %llu bytes). Are you sure this is what you really want?", server.maxmemory);
5183 5184
    }

Z
zhenwei pi 已提交
5185
    redisSetCpuAffinity(server.server_cpulist);
5186 5187 5188 5189 5190
    aeMain(server.el);
    aeDeleteEventLoop(server.el);
    return 0;
}

5191
/* The End */