]> git.saurik.com Git - redis.git/blame - src/redis.c
in kill_server send the signal once, then wait for up to 5sec before sending lethal...
[redis.git] / src / redis.c
CommitLineData
e2641e09 1/*
2 * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of Redis nor the names of its contributors may be used
14 * to endorse or promote products derived from this software without
15 * specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "redis.h"
daa70b17 31#include "slowlog.h"
8f61a72f 32#include "bio.h"
e2641e09 33
e2641e09 34#include <time.h>
35#include <signal.h>
36#include <sys/wait.h>
37#include <errno.h>
38#include <assert.h>
39#include <ctype.h>
40#include <stdarg.h>
e2641e09 41#include <arpa/inet.h>
42#include <sys/stat.h>
43#include <fcntl.h>
44#include <sys/time.h>
45#include <sys/resource.h>
46#include <sys/uio.h>
47#include <limits.h>
48#include <float.h>
49#include <math.h>
2b00385d 50#include <sys/resource.h>
fa2a27cf 51#include <sys/utsname.h>
e2641e09 52
53/* Our shared "common" objects */
54
55struct sharedObjectsStruct shared;
56
c74b7c77 57/* Global vars that are actually used as constants. The following double
e2641e09 58 * values are used for double on-disk serialization, and are initialized
59 * at runtime to avoid strange compiler optimizations. */
60
61double R_Zero, R_PosInf, R_NegInf, R_Nan;
62
63/*================================= Globals ================================= */
64
65/* Global vars */
66struct redisServer server; /* server global state */
67struct redisCommand *commandTable;
5d02b00f 68
7501c66f 69/* Our command table.
70 *
71 * Every entry is composed of the following fields:
72 *
73 * name: a string representing the command name.
74 * function: pointer to the C function implementing the command.
75 * arity: number of arguments, it is possible to use -N to say >= N
76 * sflags: command flags as string. See below for a table of flags.
77 * flags: flags as bitmask. Computed by Redis using the 'sflags' field.
78 * get_keys_proc: an optional function to get key arguments from a command.
79 * This is only used when the following three fields are not
80 * enough to specify what arguments are keys.
81 * first_key_index: first argument that is a key
82 * last_key_index: last argument that is a key
83 * key_step: step to get all the keys from first to last argument. For instance
84 * in MSET the step is two since arguments are key,val,key,val,...
85 * microseconds: microseconds of total execution time for this command.
86 * calls: total number of calls of this command.
87 *
88 * The flags, microseconds and calls fields are computed by Redis and should
89 * always be set to zero.
90 *
91 * Command flags are expressed using strings where every character represents
92 * a flag. Later the populateCommandTable() function will take care of
93 * populating the real 'flags' field using this characters.
5d02b00f 94 *
95 * This is the meaning of the flags:
96 *
97 * w: write command (may modify the key space).
98 * r: read command (will never modify the key space).
99 * m: may increase memory usage once called. Don't allow if out of memory.
100 * a: admin command, like SAVE or SHUTDOWN.
101 * p: Pub/Sub related command.
b60ed6e8 102 * f: force replication of this command, regarless of server.dirty.
103 * s: command not allowed in scripts.
7e14a208 104 * R: random command. Command is not deterministic, that is, the same command
b60ed6e8 105 * with the same arguments, with the same key space, may have different
548efd91 106 * results. For instance SPOP and RANDOMKEY are two random commands.
107 * S: Sort command output array if called from script, so that the output
108 * is deterministic.
109 */
d7ed7fd2 110struct redisCommand redisCommandTable[] = {
5d02b00f 111 {"get",getCommand,2,"r",0,NULL,1,1,1,0,0},
112 {"set",setCommand,3,"wm",0,noPreloadGetKeys,1,1,1,0,0},
113 {"setnx",setnxCommand,3,"wm",0,noPreloadGetKeys,1,1,1,0,0},
39da5d1f 114 {"setex",setexCommand,4,"wm",0,noPreloadGetKeys,1,1,1,0,0},
115 {"psetex",psetexCommand,4,"wm",0,noPreloadGetKeys,1,1,1,0,0},
5d02b00f 116 {"append",appendCommand,3,"wm",0,NULL,1,1,1,0,0},
117 {"strlen",strlenCommand,2,"r",0,NULL,1,1,1,0,0},
118 {"del",delCommand,-2,"w",0,noPreloadGetKeys,1,-1,1,0,0},
119 {"exists",existsCommand,2,"r",0,NULL,1,1,1,0,0},
120 {"setbit",setbitCommand,4,"wm",0,NULL,1,1,1,0,0},
121 {"getbit",getbitCommand,3,"r",0,NULL,1,1,1,0,0},
122 {"setrange",setrangeCommand,4,"wm",0,NULL,1,1,1,0,0},
123 {"getrange",getrangeCommand,4,"r",0,NULL,1,1,1,0,0},
124 {"substr",getrangeCommand,4,"r",0,NULL,1,1,1,0,0},
125 {"incr",incrCommand,2,"wm",0,NULL,1,1,1,0,0},
126 {"decr",decrCommand,2,"wm",0,NULL,1,1,1,0,0},
127 {"mget",mgetCommand,-2,"r",0,NULL,1,-1,1,0,0},
128 {"rpush",rpushCommand,-3,"wm",0,NULL,1,1,1,0,0},
129 {"lpush",lpushCommand,-3,"wm",0,NULL,1,1,1,0,0},
130 {"rpushx",rpushxCommand,3,"wm",0,NULL,1,1,1,0,0},
131 {"lpushx",lpushxCommand,3,"wm",0,NULL,1,1,1,0,0},
132 {"linsert",linsertCommand,5,"wm",0,NULL,1,1,1,0,0},
133 {"rpop",rpopCommand,2,"w",0,NULL,1,1,1,0,0},
134 {"lpop",lpopCommand,2,"w",0,NULL,1,1,1,0,0},
e41677b4 135 {"brpop",brpopCommand,-3,"ws",0,NULL,1,1,1,0,0},
136 {"brpoplpush",brpoplpushCommand,4,"wms",0,NULL,1,2,1,0,0},
137 {"blpop",blpopCommand,-3,"ws",0,NULL,1,-2,1,0,0},
5d02b00f 138 {"llen",llenCommand,2,"r",0,NULL,1,1,1,0,0},
139 {"lindex",lindexCommand,3,"r",0,NULL,1,1,1,0,0},
140 {"lset",lsetCommand,4,"wm",0,NULL,1,1,1,0,0},
141 {"lrange",lrangeCommand,4,"r",0,NULL,1,1,1,0,0},
142 {"ltrim",ltrimCommand,4,"w",0,NULL,1,1,1,0,0},
143 {"lrem",lremCommand,4,"w",0,NULL,1,1,1,0,0},
144 {"rpoplpush",rpoplpushCommand,3,"wm",0,NULL,1,2,1,0,0},
145 {"sadd",saddCommand,-3,"wm",0,NULL,1,1,1,0,0},
146 {"srem",sremCommand,-3,"w",0,NULL,1,1,1,0,0},
147 {"smove",smoveCommand,4,"w",0,NULL,1,2,1,0,0},
148 {"sismember",sismemberCommand,3,"r",0,NULL,1,1,1,0,0},
149 {"scard",scardCommand,2,"r",0,NULL,1,1,1,0,0},
15ef6053 150 {"spop",spopCommand,2,"wRs",0,NULL,1,1,1,0,0},
b60ed6e8 151 {"srandmember",srandmemberCommand,2,"rR",0,NULL,1,1,1,0,0},
548efd91 152 {"sinter",sinterCommand,-2,"rS",0,NULL,1,-1,1,0,0},
1bcfa0f6 153 {"sinterstore",sinterstoreCommand,-3,"wm",0,NULL,1,-1,1,0,0},
548efd91 154 {"sunion",sunionCommand,-2,"rS",0,NULL,1,-1,1,0,0},
1bcfa0f6 155 {"sunionstore",sunionstoreCommand,-3,"wm",0,NULL,1,-1,1,0,0},
548efd91 156 {"sdiff",sdiffCommand,-2,"rS",0,NULL,1,-1,1,0,0},
1bcfa0f6 157 {"sdiffstore",sdiffstoreCommand,-3,"wm",0,NULL,1,-1,1,0,0},
548efd91 158 {"smembers",sinterCommand,2,"rS",0,NULL,1,1,1,0,0},
5d02b00f 159 {"zadd",zaddCommand,-4,"wm",0,NULL,1,1,1,0,0},
160 {"zincrby",zincrbyCommand,4,"wm",0,NULL,1,1,1,0,0},
161 {"zrem",zremCommand,-3,"w",0,NULL,1,1,1,0,0},
162 {"zremrangebyscore",zremrangebyscoreCommand,4,"w",0,NULL,1,1,1,0,0},
163 {"zremrangebyrank",zremrangebyrankCommand,4,"w",0,NULL,1,1,1,0,0},
164 {"zunionstore",zunionstoreCommand,-4,"wm",0,zunionInterGetKeys,0,0,0,0,0},
165 {"zinterstore",zinterstoreCommand,-4,"wm",0,zunionInterGetKeys,0,0,0,0,0},
166 {"zrange",zrangeCommand,-4,"r",0,NULL,1,1,1,0,0},
167 {"zrangebyscore",zrangebyscoreCommand,-4,"r",0,NULL,1,1,1,0,0},
168 {"zrevrangebyscore",zrevrangebyscoreCommand,-4,"r",0,NULL,1,1,1,0,0},
169 {"zcount",zcountCommand,4,"r",0,NULL,1,1,1,0,0},
170 {"zrevrange",zrevrangeCommand,-4,"r",0,NULL,1,1,1,0,0},
171 {"zcard",zcardCommand,2,"r",0,NULL,1,1,1,0,0},
172 {"zscore",zscoreCommand,3,"r",0,NULL,1,1,1,0,0},
173 {"zrank",zrankCommand,3,"r",0,NULL,1,1,1,0,0},
174 {"zrevrank",zrevrankCommand,3,"r",0,NULL,1,1,1,0,0},
175 {"hset",hsetCommand,4,"wm",0,NULL,1,1,1,0,0},
176 {"hsetnx",hsetnxCommand,4,"wm",0,NULL,1,1,1,0,0},
177 {"hget",hgetCommand,3,"r",0,NULL,1,1,1,0,0},
178 {"hmset",hmsetCommand,-4,"wm",0,NULL,1,1,1,0,0},
179 {"hmget",hmgetCommand,-3,"r",0,NULL,1,1,1,0,0},
180 {"hincrby",hincrbyCommand,4,"wm",0,NULL,1,1,1,0,0},
68bfe993 181 {"hincrbyfloat",hincrbyfloatCommand,4,"wm",0,NULL,1,1,1,0,0},
5d02b00f 182 {"hdel",hdelCommand,-3,"w",0,NULL,1,1,1,0,0},
183 {"hlen",hlenCommand,2,"r",0,NULL,1,1,1,0,0},
548efd91 184 {"hkeys",hkeysCommand,2,"rS",0,NULL,1,1,1,0,0},
185 {"hvals",hvalsCommand,2,"rS",0,NULL,1,1,1,0,0},
5d02b00f 186 {"hgetall",hgetallCommand,2,"r",0,NULL,1,1,1,0,0},
187 {"hexists",hexistsCommand,3,"r",0,NULL,1,1,1,0,0},
188 {"incrby",incrbyCommand,3,"wm",0,NULL,1,1,1,0,0},
189 {"decrby",decrbyCommand,3,"wm",0,NULL,1,1,1,0,0},
5574b53e 190 {"incrbyfloat",incrbyfloatCommand,3,"wm",0,NULL,1,1,1,0,0},
5d02b00f 191 {"getset",getsetCommand,3,"wm",0,NULL,1,1,1,0,0},
192 {"mset",msetCommand,-3,"wm",0,NULL,1,-1,2,0,0},
193 {"msetnx",msetnxCommand,-3,"wm",0,NULL,1,-1,2,0,0},
b60ed6e8 194 {"randomkey",randomkeyCommand,1,"rR",0,NULL,0,0,0,0,0},
5d02b00f 195 {"select",selectCommand,2,"r",0,NULL,0,0,0,0,0},
196 {"move",moveCommand,3,"w",0,NULL,1,1,1,0,0},
197 {"rename",renameCommand,3,"w",0,renameGetKeys,1,2,1,0,0},
198 {"renamenx",renamenxCommand,3,"w",0,renameGetKeys,1,2,1,0,0},
12d293ca 199 {"expire",expireCommand,3,"w",0,NULL,1,1,1,0,0},
200 {"expireat",expireatCommand,3,"w",0,NULL,1,1,1,0,0},
201 {"pexpire",pexpireCommand,3,"w",0,NULL,1,1,1,0,0},
202 {"pexpireat",pexpireatCommand,3,"w",0,NULL,1,1,1,0,0},
548efd91 203 {"keys",keysCommand,2,"rS",0,NULL,0,0,0,0,0},
5d02b00f 204 {"dbsize",dbsizeCommand,1,"r",0,NULL,0,0,0,0,0},
e41677b4 205 {"auth",authCommand,2,"rs",0,NULL,0,0,0,0,0},
5d02b00f 206 {"ping",pingCommand,1,"r",0,NULL,0,0,0,0,0},
207 {"echo",echoCommand,2,"r",0,NULL,0,0,0,0,0},
e41677b4 208 {"save",saveCommand,1,"ars",0,NULL,0,0,0,0,0},
5d02b00f 209 {"bgsave",bgsaveCommand,1,"ar",0,NULL,0,0,0,0,0},
210 {"bgrewriteaof",bgrewriteaofCommand,1,"ar",0,NULL,0,0,0,0,0},
4ab8695d 211 {"shutdown",shutdownCommand,-1,"ar",0,NULL,0,0,0,0,0},
5d02b00f 212 {"lastsave",lastsaveCommand,1,"r",0,NULL,0,0,0,0,0},
213 {"type",typeCommand,2,"r",0,NULL,1,1,1,0,0},
b60ed6e8 214 {"multi",multiCommand,1,"rs",0,NULL,0,0,0,0,0},
05406168 215 {"exec",execCommand,1,"s",0,NULL,0,0,0,0,0},
b60ed6e8 216 {"discard",discardCommand,1,"rs",0,NULL,0,0,0,0,0},
217 {"sync",syncCommand,1,"ars",0,NULL,0,0,0,0,0},
5d02b00f 218 {"flushdb",flushdbCommand,1,"w",0,NULL,0,0,0,0,0},
219 {"flushall",flushallCommand,1,"w",0,NULL,0,0,0,0,0},
2c861050 220 {"sort",sortCommand,-2,"wmS",0,NULL,1,1,1,0,0},
5d02b00f 221 {"info",infoCommand,-1,"r",0,NULL,0,0,0,0,0},
b60ed6e8 222 {"monitor",monitorCommand,1,"ars",0,NULL,0,0,0,0,0},
12d293ca 223 {"ttl",ttlCommand,2,"r",0,NULL,1,1,1,0,0},
224 {"pttl",pttlCommand,2,"r",0,NULL,1,1,1,0,0},
5d02b00f 225 {"persist",persistCommand,2,"w",0,NULL,1,1,1,0,0},
a5b75e9a 226 {"slaveof",slaveofCommand,3,"as",0,NULL,0,0,0,0,0},
38bb4522 227 {"debug",debugCommand,-2,"as",0,NULL,0,0,0,0,0},
5d02b00f 228 {"config",configCommand,-2,"ar",0,NULL,0,0,0,0,0},
b60ed6e8 229 {"subscribe",subscribeCommand,-2,"rps",0,NULL,0,0,0,0,0},
230 {"unsubscribe",unsubscribeCommand,-1,"rps",0,NULL,0,0,0,0,0},
231 {"psubscribe",psubscribeCommand,-2,"rps",0,NULL,0,0,0,0,0},
232 {"punsubscribe",punsubscribeCommand,-1,"rps",0,NULL,0,0,0,0,0},
6e6bbac7 233 {"publish",publishCommand,3,"pf",0,NULL,0,0,0,0,0},
b60ed6e8 234 {"watch",watchCommand,-2,"rs",0,noPreloadGetKeys,1,-1,1,0,0},
235 {"unwatch",unwatchCommand,1,"rs",0,NULL,0,0,0,0,0},
7afc3a96 236 {"restore",restoreCommand,4,"awm",0,NULL,1,1,1,0,0},
5d02b00f 237 {"migrate",migrateCommand,6,"aw",0,NULL,0,0,0,0,0},
1bcfa0f6 238 {"dump",dumpCommand,2,"ar",0,NULL,1,1,1,0,0},
239 {"object",objectCommand,-2,"r",0,NULL,2,2,2,0,0},
5d02b00f 240 {"client",clientCommand,-2,"ar",0,NULL,0,0,0,0,0},
05406168 241 {"eval",evalCommand,-3,"s",0,zunionInterGetKeys,0,0,0,0,0},
242 {"evalsha",evalShaCommand,-3,"s",0,zunionInterGetKeys,0,0,0,0,0},
070e3945 243 {"slowlog",slowlogCommand,-2,"r",0,NULL,0,0,0,0,0},
9494f1f1 244 {"script",scriptCommand,-2,"ras",0,NULL,0,0,0,0,0},
245 {"time",timeCommand,1,"rR",0,NULL,0,0,0,0,0}
e2641e09 246};
247
248/*============================ Utility functions ============================ */
249
9c104c68 250/* Low level logging. To use only for very big messages, otherwise
251 * redisLog() is to prefer. */
252void redisLogRaw(int level, const char *msg) {
e1a586ee
JH
253 const int syslogLevelMap[] = { LOG_DEBUG, LOG_INFO, LOG_NOTICE, LOG_WARNING };
254 const char *c = ".-*#";
e2641e09 255 FILE *fp;
23072961 256 char buf[64];
996d503d 257 int rawmode = (level & REDIS_LOG_RAW);
23072961 258
996d503d 259 level &= 0xff; /* clear flags */
23072961 260 if (level < server.verbosity) return;
e2641e09 261
262 fp = (server.logfile == NULL) ? stdout : fopen(server.logfile,"a");
263 if (!fp) return;
264
996d503d 265 if (rawmode) {
266 fprintf(fp,"%s",msg);
267 } else {
df35d873 268 int off;
269 struct timeval tv;
270
271 gettimeofday(&tv,NULL);
272 off = strftime(buf,sizeof(buf),"%d %b %H:%M:%S.",localtime(&tv.tv_sec));
273 snprintf(buf+off,sizeof(buf)-off,"%03d",(int)tv.tv_usec/1000);
996d503d 274 fprintf(fp,"[%d] %s %c %s\n",(int)getpid(),buf,c[level],msg);
275 }
e1a586ee
JH
276 fflush(fp);
277
e2641e09 278 if (server.logfile) fclose(fp);
e1a586ee
JH
279
280 if (server.syslog_enabled) syslog(syslogLevelMap[level], "%s", msg);
e2641e09 281}
282
9c104c68 283/* Like redisLogRaw() but with printf-alike support. This is the funciton that
284 * is used across the code. The raw version is only used in order to dump
285 * the INFO output on crash. */
286void redisLog(int level, const char *fmt, ...) {
287 va_list ap;
288 char msg[REDIS_MAX_LOGMSG_LEN];
289
996d503d 290 if ((level&0xff) < server.verbosity) return;
9c104c68 291
292 va_start(ap, fmt);
293 vsnprintf(msg, sizeof(msg), fmt, ap);
294 va_end(ap);
295
296 redisLogRaw(level,msg);
297}
298
be4f8ccc 299/* Log a fixed message without printf-alike capabilities, in a way that is
300 * safe to call from a signal handler.
301 *
302 * We actually use this only for signals that are not fatal from the point
303 * of view of Redis. Signals that are going to kill the server anyway and
304 * where we need printf-alike features are served by redisLog(). */
305void redisLogFromHandler(int level, const char *msg) {
306 int fd;
307 char buf[64];
308
309 if ((level&0xff) < server.verbosity ||
310 (server.logfile == NULL && server.daemonize)) return;
311 fd = server.logfile ?
312 open(server.logfile, O_APPEND|O_CREAT|O_WRONLY, 0644) :
e51f7d2c 313 STDOUT_FILENO;
be4f8ccc 314 if (fd == -1) return;
315 ll2string(buf,sizeof(buf),getpid());
316 write(fd,"[",1);
317 write(fd,buf,strlen(buf));
318 write(fd," | signal handler] (",20);
319 ll2string(buf,sizeof(buf),time(NULL));
320 write(fd,buf,strlen(buf));
321 write(fd,") ",2);
322 write(fd,msg,strlen(msg));
323 write(fd,"\n",1);
e51f7d2c 324 if (server.logfile) close(fd);
be4f8ccc 325}
326
e2641e09 327/* Redis generally does not try to recover from out of memory conditions
328 * when allocating objects or strings, it is not clear if it will be possible
329 * to report this condition to the client since the networking layer itself
330 * is based on heap allocation for send buffers, so we simply abort.
331 * At least the code will be simpler to read... */
332void oom(const char *msg) {
333 redisLog(REDIS_WARNING, "%s: Out of memory\n",msg);
334 sleep(1);
335 abort();
336}
337
d9cb288c 338/* Return the UNIX time in microseconds */
339long long ustime(void) {
340 struct timeval tv;
341 long long ust;
342
343 gettimeofday(&tv, NULL);
344 ust = ((long long)tv.tv_sec)*1000000;
345 ust += tv.tv_usec;
346 return ust;
347}
348
2c2b2085 349/* Return the UNIX time in milliseconds */
350long long mstime(void) {
351 return ustime()/1000;
352}
353
e2641e09 354/*====================== Hash table type implementation ==================== */
355
356/* This is an hash table type that uses the SDS dynamic strings libary as
357 * keys and radis objects as values (objects can hold SDS strings,
358 * lists, sets). */
359
360void dictVanillaFree(void *privdata, void *val)
361{
362 DICT_NOTUSED(privdata);
363 zfree(val);
364}
365
366void dictListDestructor(void *privdata, void *val)
367{
368 DICT_NOTUSED(privdata);
369 listRelease((list*)val);
370}
371
372int dictSdsKeyCompare(void *privdata, const void *key1,
373 const void *key2)
374{
375 int l1,l2;
376 DICT_NOTUSED(privdata);
377
378 l1 = sdslen((sds)key1);
379 l2 = sdslen((sds)key2);
380 if (l1 != l2) return 0;
381 return memcmp(key1, key2, l1) == 0;
382}
383
1b1f47c9 384/* A case insensitive version used for the command lookup table. */
385int dictSdsKeyCaseCompare(void *privdata, const void *key1,
386 const void *key2)
387{
388 DICT_NOTUSED(privdata);
389
390 return strcasecmp(key1, key2) == 0;
391}
392
e2641e09 393void dictRedisObjectDestructor(void *privdata, void *val)
394{
395 DICT_NOTUSED(privdata);
396
397 if (val == NULL) return; /* Values of swapped out keys as set to NULL */
398 decrRefCount(val);
399}
400
401void dictSdsDestructor(void *privdata, void *val)
402{
403 DICT_NOTUSED(privdata);
404
405 sdsfree(val);
406}
407
408int dictObjKeyCompare(void *privdata, const void *key1,
409 const void *key2)
410{
411 const robj *o1 = key1, *o2 = key2;
412 return dictSdsKeyCompare(privdata,o1->ptr,o2->ptr);
413}
414
415unsigned int dictObjHash(const void *key) {
416 const robj *o = key;
417 return dictGenHashFunction(o->ptr, sdslen((sds)o->ptr));
418}
419
420unsigned int dictSdsHash(const void *key) {
421 return dictGenHashFunction((unsigned char*)key, sdslen((char*)key));
422}
423
1b1f47c9 424unsigned int dictSdsCaseHash(const void *key) {
425 return dictGenCaseHashFunction((unsigned char*)key, sdslen((char*)key));
426}
427
e2641e09 428int dictEncObjKeyCompare(void *privdata, const void *key1,
429 const void *key2)
430{
431 robj *o1 = (robj*) key1, *o2 = (robj*) key2;
432 int cmp;
433
434 if (o1->encoding == REDIS_ENCODING_INT &&
435 o2->encoding == REDIS_ENCODING_INT)
436 return o1->ptr == o2->ptr;
437
438 o1 = getDecodedObject(o1);
439 o2 = getDecodedObject(o2);
440 cmp = dictSdsKeyCompare(privdata,o1->ptr,o2->ptr);
441 decrRefCount(o1);
442 decrRefCount(o2);
443 return cmp;
444}
445
446unsigned int dictEncObjHash(const void *key) {
447 robj *o = (robj*) key;
448
449 if (o->encoding == REDIS_ENCODING_RAW) {
450 return dictGenHashFunction(o->ptr, sdslen((sds)o->ptr));
451 } else {
452 if (o->encoding == REDIS_ENCODING_INT) {
453 char buf[32];
454 int len;
455
456 len = ll2string(buf,32,(long)o->ptr);
457 return dictGenHashFunction((unsigned char*)buf, len);
458 } else {
459 unsigned int hash;
460
461 o = getDecodedObject(o);
462 hash = dictGenHashFunction(o->ptr, sdslen((sds)o->ptr));
463 decrRefCount(o);
464 return hash;
465 }
466 }
467}
468
4dd444bb 469/* Sets type hash table */
e2641e09 470dictType setDictType = {
471 dictEncObjHash, /* hash function */
472 NULL, /* key dup */
473 NULL, /* val dup */
474 dictEncObjKeyCompare, /* key compare */
475 dictRedisObjectDestructor, /* key destructor */
476 NULL /* val destructor */
477};
478
479/* Sorted sets hash (note: a skiplist is used in addition to the hash table) */
480dictType zsetDictType = {
481 dictEncObjHash, /* hash function */
482 NULL, /* key dup */
483 NULL, /* val dup */
484 dictEncObjKeyCompare, /* key compare */
485 dictRedisObjectDestructor, /* key destructor */
69ef89f2 486 NULL /* val destructor */
e2641e09 487};
488
489/* Db->dict, keys are sds strings, vals are Redis objects. */
490dictType dbDictType = {
491 dictSdsHash, /* hash function */
492 NULL, /* key dup */
493 NULL, /* val dup */
494 dictSdsKeyCompare, /* key compare */
495 dictSdsDestructor, /* key destructor */
496 dictRedisObjectDestructor /* val destructor */
497};
498
499/* Db->expires */
500dictType keyptrDictType = {
501 dictSdsHash, /* hash function */
502 NULL, /* key dup */
503 NULL, /* val dup */
504 dictSdsKeyCompare, /* key compare */
505 NULL, /* key destructor */
506 NULL /* val destructor */
507};
508
1b1f47c9 509/* Command table. sds string -> command struct pointer. */
510dictType commandTableDictType = {
511 dictSdsCaseHash, /* hash function */
512 NULL, /* key dup */
513 NULL, /* val dup */
514 dictSdsKeyCaseCompare, /* key compare */
515 dictSdsDestructor, /* key destructor */
516 NULL /* val destructor */
517};
518
e2641e09 519/* Hash type hash table (note that small hashes are represented with zimpaps) */
520dictType hashDictType = {
521 dictEncObjHash, /* hash function */
522 NULL, /* key dup */
523 NULL, /* val dup */
524 dictEncObjKeyCompare, /* key compare */
525 dictRedisObjectDestructor, /* key destructor */
526 dictRedisObjectDestructor /* val destructor */
527};
528
529/* Keylist hash table type has unencoded redis objects as keys and
530 * lists as values. It's used for blocking operations (BLPOP) and to
531 * map swapped keys to a list of clients waiting for this keys to be loaded. */
532dictType keylistDictType = {
533 dictObjHash, /* hash function */
534 NULL, /* key dup */
535 NULL, /* val dup */
536 dictObjKeyCompare, /* key compare */
537 dictRedisObjectDestructor, /* key destructor */
538 dictListDestructor /* val destructor */
539};
540
541int htNeedsResize(dict *dict) {
542 long long size, used;
543
544 size = dictSlots(dict);
545 used = dictSize(dict);
546 return (size && used && size > DICT_HT_INITIAL_SIZE &&
547 (used*100/size < REDIS_HT_MINFILL));
548}
549
550/* If the percentage of used slots in the HT reaches REDIS_HT_MINFILL
551 * we resize the hash table to save memory */
552void tryResizeHashTables(void) {
553 int j;
554
555 for (j = 0; j < server.dbnum; j++) {
556 if (htNeedsResize(server.db[j].dict))
557 dictResize(server.db[j].dict);
558 if (htNeedsResize(server.db[j].expires))
559 dictResize(server.db[j].expires);
560 }
561}
562
563/* Our hash table implementation performs rehashing incrementally while
564 * we write/read from the hash table. Still if the server is idle, the hash
565 * table will use two tables for a long time. So we try to use 1 millisecond
566 * of CPU time at every serverCron() loop in order to rehash some key. */
567void incrementallyRehash(void) {
568 int j;
569
570 for (j = 0; j < server.dbnum; j++) {
571 if (dictIsRehashing(server.db[j].dict)) {
572 dictRehashMilliseconds(server.db[j].dict,1);
573 break; /* already used our millisecond for this loop... */
574 }
575 }
576}
577
578/* This function is called once a background process of some kind terminates,
579 * as we want to avoid resizing the hash tables when there is a child in order
580 * to play well with copy-on-write (otherwise when a resize happens lots of
581 * memory pages are copied). The goal of this function is to update the ability
582 * for dict.c to resize the hash tables accordingly to the fact we have o not
583 * running childs. */
584void updateDictResizePolicy(void) {
f48cd4b9 585 if (server.rdb_child_pid == -1 && server.aof_child_pid == -1)
e2641e09 586 dictEnableResize();
587 else
588 dictDisableResize();
589}
590
591/* ======================= Cron: called every 100 ms ======================== */
592
bcf2995c 593/* Try to expire a few timed out keys. The algorithm used is adaptive and
594 * will use few CPU cycles if there are few expiring keys, otherwise
595 * it will get more aggressive to avoid that too much memory is used by
596 * keys that can be removed from the keyspace. */
597void activeExpireCycle(void) {
598 int j;
599
600 for (j = 0; j < server.dbnum; j++) {
601 int expired;
602 redisDb *db = server.db+j;
603
604 /* Continue to expire if at the end of the cycle more than 25%
605 * of the keys were expired. */
606 do {
607 long num = dictSize(db->expires);
4be855e7 608 long long now = mstime();
bcf2995c 609
610 expired = 0;
611 if (num > REDIS_EXPIRELOOKUPS_PER_CRON)
612 num = REDIS_EXPIRELOOKUPS_PER_CRON;
613 while (num--) {
614 dictEntry *de;
4be855e7 615 long long t;
bcf2995c 616
617 if ((de = dictGetRandomKey(db->expires)) == NULL) break;
4be855e7 618 t = dictGetSignedIntegerVal(de);
bcf2995c 619 if (now > t) {
c0ba9ebe 620 sds key = dictGetKey(de);
bcf2995c 621 robj *keyobj = createStringObject(key,sdslen(key));
622
623 propagateExpire(db,keyobj);
624 dbDelete(db,keyobj);
625 decrRefCount(keyobj);
626 expired++;
627 server.stat_expiredkeys++;
628 }
629 }
630 } while (expired > REDIS_EXPIRELOOKUPS_PER_CRON/4);
631 }
632}
633
165346ca 634void updateLRUClock(void) {
56ff70f8 635 server.lruclock = (server.unixtime/REDIS_LRU_CLOCK_RESOLUTION) &
165346ca 636 REDIS_LRU_CLOCK_MAX;
637}
bcf2995c 638
250e7f69 639
640/* Add a sample to the operations per second array of samples. */
641void trackOperationsPerSecond(void) {
642 long long t = mstime() - server.ops_sec_last_sample_time;
643 long long ops = server.stat_numcommands - server.ops_sec_last_sample_ops;
644 long long ops_sec;
645
646 ops_sec = t > 0 ? (ops*1000/t) : 0;
647
648 server.ops_sec_samples[server.ops_sec_idx] = ops_sec;
649 server.ops_sec_idx = (server.ops_sec_idx+1) % REDIS_OPS_SEC_SAMPLES;
650 server.ops_sec_last_sample_time = mstime();
651 server.ops_sec_last_sample_ops = server.stat_numcommands;
652}
653
654/* Return the mean of all the samples. */
655long long getOperationsPerSecond(void) {
656 int j;
657 long long sum = 0;
658
659 for (j = 0; j < REDIS_OPS_SEC_SAMPLES; j++)
660 sum += server.ops_sec_samples[j];
661 return sum / REDIS_OPS_SEC_SAMPLES;
662}
663
f1eaf572 664/* Check for timeouts. Returns non-zero if the client was terminated */
665int clientsCronHandleTimeout(redisClient *c) {
9fa9ccb0 666 time_t now = server.unixtime;
cfa4b57c 667
668 if (server.maxidletime &&
669 !(c->flags & REDIS_SLAVE) && /* no timeout for slaves */
670 !(c->flags & REDIS_MASTER) && /* no timeout for masters */
671 !(c->flags & REDIS_BLOCKED) && /* no timeout for BLPOP */
672 dictSize(c->pubsub_channels) == 0 && /* no timeout for pubsub */
673 listLength(c->pubsub_patterns) == 0 &&
674 (now - c->lastinteraction > server.maxidletime))
675 {
676 redisLog(REDIS_VERBOSE,"Closing idle client");
677 freeClient(c);
f1eaf572 678 return 1;
cfa4b57c 679 } else if (c->flags & REDIS_BLOCKED) {
680 if (c->bpop.timeout != 0 && c->bpop.timeout < now) {
681 addReply(c,shared.nullmultibulk);
682 unblockClientWaitingData(c);
683 }
684 }
f1eaf572 685 return 0;
cfa4b57c 686}
687
9fa9ccb0 688/* The client query buffer is an sds.c string that can end with a lot of
f1eaf572 689 * free space not used, this function reclaims space if needed.
690 *
691 * The funciton always returns 0 as it never terminates the client. */
692int clientsCronResizeQueryBuffer(redisClient *c) {
9fa9ccb0 693 size_t querybuf_size = sdsAllocSize(c->querybuf);
694 time_t idletime = server.unixtime - c->lastinteraction;
695
696 /* There are two conditions to resize the query buffer:
697 * 1) Query buffer is > BIG_ARG and too big for latest peak.
698 * 2) Client is inactive and the buffer is bigger than 1k. */
699 if (((querybuf_size > REDIS_MBULK_BIG_ARG) &&
700 (querybuf_size/(c->querybuf_peak+1)) > 2) ||
701 (querybuf_size > 1024 && idletime > 2))
702 {
703 /* Only resize the query buffer if it is actually wasting space. */
704 if (sdsavail(c->querybuf) > 1024) {
705 c->querybuf = sdsRemoveFreeSpace(c->querybuf);
706 }
707 }
708 /* Reset the peak again to capture the peak memory usage in the next
709 * cycle. */
710 c->querybuf_peak = 0;
f1eaf572 711 return 0;
9fa9ccb0 712}
713
cfa4b57c 714void clientsCron(void) {
715 /* Make sure to process at least 1/100 of clients per call.
716 * Since this function is called 10 times per second we are sure that
717 * in the worst case we process all the clients in 10 seconds.
718 * In normal conditions (a reasonable number of clients) we process
719 * all the clients in a shorter time. */
9fa9ccb0 720 int numclients = listLength(server.clients);
721 int iterations = numclients/100;
cfa4b57c 722
9fa9ccb0 723 if (iterations < 50)
724 iterations = (numclients < 50) ? numclients : 50;
cfa4b57c 725 while(listLength(server.clients) && iterations--) {
726 redisClient *c;
727 listNode *head;
728
729 /* Rotate the list, take the current head, process.
730 * This way if the client must be removed from the list it's the
731 * first element and we don't incur into O(N) computation. */
732 listRotate(server.clients);
733 head = listFirst(server.clients);
734 c = listNodeValue(head);
f1eaf572 735 /* The following functions do different service checks on the client.
736 * The protocol is that they return non-zero if the client was
737 * terminated. */
738 if (clientsCronHandleTimeout(c)) continue;
739 if (clientsCronResizeQueryBuffer(c)) continue;
cfa4b57c 740 }
741}
742
e2641e09 743int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
89a1433e 744 int j, loops = server.cronloops;
e2641e09 745 REDIS_NOTUSED(eventLoop);
746 REDIS_NOTUSED(id);
747 REDIS_NOTUSED(clientData);
748
fc030ac7 749 /* Software watchdog: deliver the SIGALRM that will reach the signal
750 * handler if we don't return here fast enough. */
751 if (server.watchdog_period) watchdogScheduleSignal(server.watchdog_period);
752
e2641e09 753 /* We take a cached value of the unix time in the global state because
754 * with virtual memory and aging there is to store the current time
755 * in objects at every object access, and accuracy is not needed.
756 * To access a global var is faster than calling time(NULL) */
757 server.unixtime = time(NULL);
4f06867a 758
250e7f69 759 trackOperationsPerSecond();
760
ef59a8bc 761 /* We have just 22 bits per object for LRU information.
165346ca 762 * So we use an (eventually wrapping) LRU clock with 10 seconds resolution.
763 * 2^22 bits with 10 seconds resoluton is more or less 1.5 years.
e2641e09 764 *
165346ca 765 * Note that even if this will wrap after 1.5 years it's not a problem,
ef59a8bc 766 * everything will still work but just some object will appear younger
165346ca 767 * to Redis. But for this to happen a given object should never be touched
768 * for 1.5 years.
769 *
770 * Note that you can change the resolution altering the
771 * REDIS_LRU_CLOCK_RESOLUTION define.
e2641e09 772 */
165346ca 773 updateLRUClock();
e2641e09 774
17b24ff3 775 /* Record the max memory used since the server was started. */
776 if (zmalloc_used_memory() > server.stat_peak_memory)
777 server.stat_peak_memory = zmalloc_used_memory();
778
e2641e09 779 /* We received a SIGTERM, shutting down here in a safe way, as it is
780 * not ok doing so inside the signal handler. */
781 if (server.shutdown_asap) {
4ab8695d 782 if (prepareForShutdown(0) == REDIS_OK) exit(0);
e2641e09 783 redisLog(REDIS_WARNING,"SIGTERM received but errors trying to shut down the server, check the logs for more information");
784 }
785
786 /* Show some info about non-empty databases */
787 for (j = 0; j < server.dbnum; j++) {
788 long long size, used, vkeys;
789
790 size = dictSlots(server.db[j].dict);
791 used = dictSize(server.db[j].dict);
792 vkeys = dictSize(server.db[j].expires);
793 if (!(loops % 50) && (used || vkeys)) {
794 redisLog(REDIS_VERBOSE,"DB %d: %lld keys (%lld volatile) in %lld slots HT.",j,used,vkeys,size);
795 /* dictPrintStats(server.dict); */
796 }
797 }
798
799 /* We don't want to resize the hash tables while a bacground saving
800 * is in progress: the saving child is created using fork() that is
801 * implemented with a copy-on-write semantic in most modern systems, so
802 * if we resize the HT while there is the saving child at work actually
803 * a lot of memory movements in the parent will cause a lot of pages
804 * copied. */
f48cd4b9 805 if (server.rdb_child_pid == -1 && server.aof_child_pid == -1) {
e2641e09 806 if (!(loops % 10)) tryResizeHashTables();
807 if (server.activerehashing) incrementallyRehash();
808 }
809
810 /* Show information about connected clients */
811 if (!(loops % 50)) {
812 redisLog(REDIS_VERBOSE,"%d clients connected (%d slaves), %zu bytes in use",
813 listLength(server.clients)-listLength(server.slaves),
814 listLength(server.slaves),
ca734d17 815 zmalloc_used_memory());
e2641e09 816 }
817
cfa4b57c 818 /* We need to do a few operations on clients asynchronously. */
819 clientsCron();
e2641e09 820
b333e239 821 /* Start a scheduled AOF rewrite if this was requested by the user while
822 * a BGSAVE was in progress. */
f48cd4b9 823 if (server.rdb_child_pid == -1 && server.aof_child_pid == -1 &&
2c915bcf 824 server.aof_rewrite_scheduled)
b333e239 825 {
826 rewriteAppendOnlyFileBackground();
827 }
828
f03fe802 829 /* Check if a background saving or AOF rewrite in progress terminated. */
f48cd4b9 830 if (server.rdb_child_pid != -1 || server.aof_child_pid != -1) {
e2641e09 831 int statloc;
832 pid_t pid;
833
834 if ((pid = wait3(&statloc,WNOHANG,NULL)) != 0) {
36c17a53 835 int exitcode = WEXITSTATUS(statloc);
836 int bysignal = 0;
837
838 if (WIFSIGNALED(statloc)) bysignal = WTERMSIG(statloc);
839
f48cd4b9 840 if (pid == server.rdb_child_pid) {
36c17a53 841 backgroundSaveDoneHandler(exitcode,bysignal);
e2641e09 842 } else {
36c17a53 843 backgroundRewriteDoneHandler(exitcode,bysignal);
e2641e09 844 }
845 updateDictResizePolicy();
846 }
c9d0c362 847 } else {
b333e239 848 /* If there is not a background saving/rewrite in progress check if
849 * we have to save/rewrite now */
e2641e09 850 for (j = 0; j < server.saveparamslen; j++) {
851 struct saveparam *sp = server.saveparams+j;
852
853 if (server.dirty >= sp->changes &&
56ff70f8 854 server.unixtime-server.lastsave > sp->seconds) {
e2641e09 855 redisLog(REDIS_NOTICE,"%d changes in %d seconds. Saving...",
856 sp->changes, sp->seconds);
f48cd4b9 857 rdbSaveBackground(server.rdb_filename);
e2641e09 858 break;
859 }
860 }
b333e239 861
862 /* Trigger an AOF rewrite if needed */
f48cd4b9 863 if (server.rdb_child_pid == -1 &&
ff2145ad 864 server.aof_child_pid == -1 &&
2c915bcf 865 server.aof_rewrite_perc &&
866 server.aof_current_size > server.aof_rewrite_min_size)
b333e239 867 {
2c915bcf 868 long long base = server.aof_rewrite_base_size ?
869 server.aof_rewrite_base_size : 1;
870 long long growth = (server.aof_current_size*100/base) - 100;
871 if (growth >= server.aof_rewrite_perc) {
19b46c9a 872 redisLog(REDIS_NOTICE,"Starting automatic rewriting of AOF on %lld%% growth",growth);
b333e239 873 rewriteAppendOnlyFileBackground();
874 }
875 }
e2641e09 876 }
877
db3c2a4f 878
879 /* If we postponed an AOF buffer flush, let's try to do it every time the
880 * cron function is called. */
881 if (server.aof_flush_postponed_start) flushAppendOnlyFile(0);
882
bcf2995c 883 /* Expire a few keys per cycle, only if this is a master.
884 * On slaves we wait for DEL operations synthesized by the master
885 * in order to guarantee a strict consistency. */
886 if (server.masterhost == NULL) activeExpireCycle();
e2641e09 887
8c43e663 888 /* Close clients that need to be closed asynchronous */
889 freeClientsInAsyncFreeQueue();
890
f4aa600b 891 /* Replication cron function -- used to reconnect to master and
892 * to detect transfer failures. */
62ec599c 893 if (!(loops % 10)) replicationCron();
f4aa600b 894
89a1433e 895 server.cronloops++;
e2641e09 896 return 100;
897}
898
899/* This function gets called every time Redis is entering the
900 * main loop of the event driven library, that is, before to sleep
901 * for ready file descriptors. */
902void beforeSleep(struct aeEventLoop *eventLoop) {
903 REDIS_NOTUSED(eventLoop);
a4ce7581
PN
904 listNode *ln;
905 redisClient *c;
e2641e09 906
a4ce7581
PN
907 /* Try to process pending commands for clients that were just unblocked. */
908 while (listLength(server.unblocked_clients)) {
909 ln = listFirst(server.unblocked_clients);
910 redisAssert(ln != NULL);
911 c = ln->value;
912 listDelNode(server.unblocked_clients,ln);
3bcffcbe 913 c->flags &= ~REDIS_UNBLOCKED;
a4ce7581
PN
914
915 /* Process remaining data in the input buffer. */
00010fa9 916 if (c->querybuf && sdslen(c->querybuf) > 0) {
917 server.current_client = c;
a4ce7581 918 processInputBuffer(c);
00010fa9 919 server.current_client = NULL;
920 }
a4ce7581
PN
921 }
922
e2641e09 923 /* Write the AOF buffer on disk */
db3c2a4f 924 flushAppendOnlyFile(0);
e2641e09 925}
926
927/* =========================== Server initialization ======================== */
928
929void createSharedObjects(void) {
930 int j;
931
932 shared.crlf = createObject(REDIS_STRING,sdsnew("\r\n"));
933 shared.ok = createObject(REDIS_STRING,sdsnew("+OK\r\n"));
934 shared.err = createObject(REDIS_STRING,sdsnew("-ERR\r\n"));
935 shared.emptybulk = createObject(REDIS_STRING,sdsnew("$0\r\n\r\n"));
936 shared.czero = createObject(REDIS_STRING,sdsnew(":0\r\n"));
937 shared.cone = createObject(REDIS_STRING,sdsnew(":1\r\n"));
938 shared.cnegone = createObject(REDIS_STRING,sdsnew(":-1\r\n"));
939 shared.nullbulk = createObject(REDIS_STRING,sdsnew("$-1\r\n"));
940 shared.nullmultibulk = createObject(REDIS_STRING,sdsnew("*-1\r\n"));
941 shared.emptymultibulk = createObject(REDIS_STRING,sdsnew("*0\r\n"));
942 shared.pong = createObject(REDIS_STRING,sdsnew("+PONG\r\n"));
943 shared.queued = createObject(REDIS_STRING,sdsnew("+QUEUED\r\n"));
944 shared.wrongtypeerr = createObject(REDIS_STRING,sdsnew(
945 "-ERR Operation against a key holding the wrong kind of value\r\n"));
946 shared.nokeyerr = createObject(REDIS_STRING,sdsnew(
947 "-ERR no such key\r\n"));
948 shared.syntaxerr = createObject(REDIS_STRING,sdsnew(
949 "-ERR syntax error\r\n"));
950 shared.sameobjecterr = createObject(REDIS_STRING,sdsnew(
951 "-ERR source and destination objects are the same\r\n"));
952 shared.outofrangeerr = createObject(REDIS_STRING,sdsnew(
953 "-ERR index out of range\r\n"));
7229d60d 954 shared.noscripterr = createObject(REDIS_STRING,sdsnew(
955 "-NOSCRIPT No matching script. Please use EVAL.\r\n"));
97e7f8ae 956 shared.loadingerr = createObject(REDIS_STRING,sdsnew(
957 "-LOADING Redis is loading the dataset in memory\r\n"));
115e3ff3 958 shared.slowscripterr = createObject(REDIS_STRING,sdsnew(
4ab8695d 959 "-BUSY Redis is busy running a script. You can only call SCRIPT KILL or SHUTDOWN NOSAVE.\r\n"));
c25e7eaf 960 shared.bgsaveerr = createObject(REDIS_STRING,sdsnew(
05406168 961 "-MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.\r\n"));
962 shared.roslaveerr = createObject(REDIS_STRING,sdsnew(
963 "-READONLY You can't write against a read only slave.\r\n"));
3f7ad833 964 shared.oomerr = createObject(REDIS_STRING,sdsnew(
965 "-OOM command not allowed when used memory > 'maxmemory'.\r\n"));
e2641e09 966 shared.space = createObject(REDIS_STRING,sdsnew(" "));
967 shared.colon = createObject(REDIS_STRING,sdsnew(":"));
968 shared.plus = createObject(REDIS_STRING,sdsnew("+"));
ae15f750
JJ
969
970 for (j = 0; j < REDIS_SHARED_SELECT_CMDS; j++) {
971 shared.select[j] = createObject(REDIS_STRING,
972 sdscatprintf(sdsempty(),"select %d\r\n", j));
973 }
e2641e09 974 shared.messagebulk = createStringObject("$7\r\nmessage\r\n",13);
975 shared.pmessagebulk = createStringObject("$8\r\npmessage\r\n",14);
976 shared.subscribebulk = createStringObject("$9\r\nsubscribe\r\n",15);
977 shared.unsubscribebulk = createStringObject("$11\r\nunsubscribe\r\n",18);
978 shared.psubscribebulk = createStringObject("$10\r\npsubscribe\r\n",17);
979 shared.punsubscribebulk = createStringObject("$12\r\npunsubscribe\r\n",19);
355f8591 980 shared.del = createStringObject("DEL",3);
c1db214e 981 shared.rpop = createStringObject("RPOP",4);
982 shared.lpop = createStringObject("LPOP",4);
e2641e09 983 for (j = 0; j < REDIS_SHARED_INTEGERS; j++) {
984 shared.integers[j] = createObject(REDIS_STRING,(void*)(long)j);
985 shared.integers[j]->encoding = REDIS_ENCODING_INT;
986 }
355f8591 987 for (j = 0; j < REDIS_SHARED_BULKHDR_LEN; j++) {
988 shared.mbulkhdr[j] = createObject(REDIS_STRING,
989 sdscatprintf(sdsempty(),"*%d\r\n",j));
990 shared.bulkhdr[j] = createObject(REDIS_STRING,
991 sdscatprintf(sdsempty(),"$%d\r\n",j));
992 }
e2641e09 993}
994
995void initServerConfig() {
91d664d6 996 getRandomHexChars(server.runid,REDIS_RUN_ID_SIZE);
997 server.runid[REDIS_RUN_ID_SIZE] = '\0';
75eaac5c 998 server.arch_bits = (sizeof(long) == 8) ? 64 : 32;
e2641e09 999 server.port = REDIS_SERVERPORT;
a5639e7d 1000 server.bindaddr = NULL;
5d10923f 1001 server.unixsocket = NULL;
85238765 1002 server.unixsocketperm = 0;
a5639e7d
PN
1003 server.ipfd = -1;
1004 server.sofd = -1;
1005 server.dbnum = REDIS_DEFAULT_DBNUM;
c6f9ee88 1006 server.verbosity = REDIS_NOTICE;
e2641e09 1007 server.maxidletime = REDIS_MAXIDLETIME;
becf5fdb 1008 server.client_max_querybuf_len = REDIS_MAX_QUERYBUF_LEN;
e2641e09 1009 server.saveparams = NULL;
97e7f8ae 1010 server.loading = 0;
e2641e09 1011 server.logfile = NULL; /* NULL = log on standard output */
e1a586ee
JH
1012 server.syslog_enabled = 0;
1013 server.syslog_ident = zstrdup("redis");
1014 server.syslog_facility = LOG_LOCAL0;
e2641e09 1015 server.daemonize = 0;
e394114d 1016 server.aof_state = REDIS_AOF_OFF;
2c915bcf 1017 server.aof_fsync = AOF_FSYNC_EVERYSEC;
1018 server.aof_no_fsync_on_rewrite = 0;
1019 server.aof_rewrite_perc = REDIS_AOF_REWRITE_PERC;
1020 server.aof_rewrite_min_size = REDIS_AOF_REWRITE_MIN_SIZE;
1021 server.aof_rewrite_base_size = 0;
1022 server.aof_rewrite_scheduled = 0;
ff2145ad 1023 server.aof_last_fsync = time(NULL);
81f32c7b 1024 server.aof_delayed_fsync = 0;
ff2145ad 1025 server.aof_fd = -1;
1026 server.aof_selected_db = -1; /* Make sure the first time will not match */
db3c2a4f 1027 server.aof_flush_postponed_start = 0;
e2641e09 1028 server.pidfile = zstrdup("/var/run/redis.pid");
f48cd4b9 1029 server.rdb_filename = zstrdup("dump.rdb");
2c915bcf 1030 server.aof_filename = zstrdup("appendonly.aof");
e2641e09 1031 server.requirepass = NULL;
f48cd4b9 1032 server.rdb_compression = 1;
e2641e09 1033 server.activerehashing = 1;
58732c23 1034 server.maxclients = REDIS_MAX_CLIENTS;
5fa95ad7 1035 server.bpop_blocked_clients = 0;
e2641e09 1036 server.maxmemory = 0;
165346ca 1037 server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_LRU;
1038 server.maxmemory_samples = 3;
ebd85e9a
PN
1039 server.hash_max_ziplist_entries = REDIS_HASH_MAX_ZIPLIST_ENTRIES;
1040 server.hash_max_ziplist_value = REDIS_HASH_MAX_ZIPLIST_VALUE;
e2641e09 1041 server.list_max_ziplist_entries = REDIS_LIST_MAX_ZIPLIST_ENTRIES;
1042 server.list_max_ziplist_value = REDIS_LIST_MAX_ZIPLIST_VALUE;
96ffb2fe 1043 server.set_max_intset_entries = REDIS_SET_MAX_INTSET_ENTRIES;
3ea204e1
PN
1044 server.zset_max_ziplist_entries = REDIS_ZSET_MAX_ZIPLIST_ENTRIES;
1045 server.zset_max_ziplist_value = REDIS_ZSET_MAX_ZIPLIST_VALUE;
e2641e09 1046 server.shutdown_asap = 0;
aeecbdfa 1047 server.repl_ping_slave_period = REDIS_REPL_PING_SLAVE_PERIOD;
1048 server.repl_timeout = REDIS_REPL_TIMEOUT;
4ab8695d 1049 server.lua_caller = NULL;
eeffcf38 1050 server.lua_time_limit = REDIS_LUA_TIME_LIMIT;
070e3945 1051 server.lua_client = NULL;
115e3ff3 1052 server.lua_timedout = 0;
e2641e09 1053
95506e46 1054 updateLRUClock();
e2641e09 1055 resetServerSaveParams();
1056
1057 appendServerSaveParams(60*60,1); /* save after 1 hour and 1 change */
1058 appendServerSaveParams(300,100); /* save after 5 minutes and 100 changes */
1059 appendServerSaveParams(60,10000); /* save after 1 minute and 10000 changes */
1060 /* Replication related */
e2641e09 1061 server.masterauth = NULL;
1062 server.masterhost = NULL;
1063 server.masterport = 6379;
1064 server.master = NULL;
1844f990 1065 server.repl_state = REDIS_REPL_NONE;
890a2ed9 1066 server.repl_syncio_timeout = REDIS_REPL_SYNCIO_TIMEOUT;
4ebfc455 1067 server.repl_serve_stale_data = 1;
05406168 1068 server.repl_slave_ro = 1;
09eb4487 1069 server.repl_down_since = time(NULL);
e2641e09 1070
7eac2a75 1071 /* Client output buffer limits */
1072 server.client_obuf_limits[REDIS_CLIENT_LIMIT_CLASS_NORMAL].hard_limit_bytes = 0;
1073 server.client_obuf_limits[REDIS_CLIENT_LIMIT_CLASS_NORMAL].soft_limit_bytes = 0;
1074 server.client_obuf_limits[REDIS_CLIENT_LIMIT_CLASS_NORMAL].soft_limit_seconds = 0;
c8a607f2 1075 server.client_obuf_limits[REDIS_CLIENT_LIMIT_CLASS_SLAVE].hard_limit_bytes = 1024*1024*256;
1076 server.client_obuf_limits[REDIS_CLIENT_LIMIT_CLASS_SLAVE].soft_limit_bytes = 1024*1024*64;
7eac2a75 1077 server.client_obuf_limits[REDIS_CLIENT_LIMIT_CLASS_SLAVE].soft_limit_seconds = 60;
c8a607f2 1078 server.client_obuf_limits[REDIS_CLIENT_LIMIT_CLASS_PUBSUB].hard_limit_bytes = 1024*1024*32;
1079 server.client_obuf_limits[REDIS_CLIENT_LIMIT_CLASS_PUBSUB].soft_limit_bytes = 1024*1024*8;
7eac2a75 1080 server.client_obuf_limits[REDIS_CLIENT_LIMIT_CLASS_PUBSUB].soft_limit_seconds = 60;
1081
e2641e09 1082 /* Double constants initialization */
1083 R_Zero = 0.0;
1084 R_PosInf = 1.0/R_Zero;
1085 R_NegInf = -1.0/R_Zero;
1086 R_Nan = R_Zero/R_Zero;
8d3e063a 1087
1088 /* Command table -- we intiialize it here as it is part of the
1089 * initial configuration, since command names may be changed via
1090 * redis.conf using the rename-command directive. */
1091 server.commands = dictCreate(&commandTableDictType,NULL);
1092 populateCommandTable();
1093 server.delCommand = lookupCommandByCString("del");
1094 server.multiCommand = lookupCommandByCString("multi");
eeb34eff 1095 server.lpushCommand = lookupCommandByCString("lpush");
daa70b17 1096
1097 /* Slow log */
1098 server.slowlog_log_slower_than = REDIS_SLOWLOG_LOG_SLOWER_THAN;
1099 server.slowlog_max_len = REDIS_SLOWLOG_MAX_LEN;
fa5af017 1100
fc030ac7 1101 /* Debugging */
fa5af017 1102 server.assert_failed = "<no assertion failed>";
1103 server.assert_file = "<no file>";
1104 server.assert_line = 0;
1105 server.bug_report_start = 0;
fc030ac7 1106 server.watchdog_period = 0;
e2641e09 1107}
1108
e074416b 1109/* This function will try to raise the max number of open files accordingly to
1110 * the configured max number of clients. It will also account for 32 additional
1111 * file descriptors as we need a few more for persistence, listening
1112 * sockets, log files and so forth.
1113 *
1114 * If it will not be possible to set the limit accordingly to the configured
1115 * max number of clients, the function will do the reverse setting
1116 * server.maxclients to the value that we can actually handle. */
1117void adjustOpenFilesLimit(void) {
1118 rlim_t maxfiles = server.maxclients+32;
1119 struct rlimit limit;
1120
1121 if (maxfiles < 1024) maxfiles = 1024;
1122 if (getrlimit(RLIMIT_NOFILE,&limit) == -1) {
1123 redisLog(REDIS_WARNING,"Unable to obtain the current NOFILE limit (%s), assuming 1024 and setting the max clients configuration accordingly.",
1124 strerror(errno));
1125 server.maxclients = 1024-32;
1126 } else {
1127 rlim_t oldlimit = limit.rlim_cur;
1128
1129 /* Set the max number of files if the current limit is not enough
1130 * for our needs. */
1131 if (oldlimit < maxfiles) {
fbce4752 1132 rlim_t f;
1133
1134 f = maxfiles;
1135 while(f > oldlimit) {
1136 limit.rlim_cur = f;
1137 limit.rlim_max = f;
1138 if (setrlimit(RLIMIT_NOFILE,&limit) != -1) break;
1139 f -= 128;
1140 }
1141 if (f < oldlimit) f = oldlimit;
1142 if (f != maxfiles) {
1143 server.maxclients = f-32;
e074416b 1144 redisLog(REDIS_WARNING,"Unable to set the max number of files limit to %d (%s), setting the max clients configuration to %d.",
1145 (int) maxfiles, strerror(errno), (int) server.maxclients);
1146 } else {
1147 redisLog(REDIS_NOTICE,"Max number of open files set to %d",
1148 (int) maxfiles);
1149 }
1150 }
1151 }
1152}
1153
e2641e09 1154void initServer() {
1155 int j;
1156
1157 signal(SIGHUP, SIG_IGN);
1158 signal(SIGPIPE, SIG_IGN);
633a9410 1159 setupSignalHandlers();
e2641e09 1160
e1a586ee
JH
1161 if (server.syslog_enabled) {
1162 openlog(server.syslog_ident, LOG_PID | LOG_NDELAY | LOG_NOWAIT,
1163 server.syslog_facility);
1164 }
1165
00010fa9 1166 server.current_client = NULL;
e2641e09 1167 server.clients = listCreate();
7eac2a75 1168 server.clients_to_close = listCreate();
e2641e09 1169 server.slaves = listCreate();
1170 server.monitors = listCreate();
a4ce7581 1171 server.unblocked_clients = listCreate();
cea8c5cd 1172
e2641e09 1173 createSharedObjects();
e074416b 1174 adjustOpenFilesLimit();
1175 server.el = aeCreateEventLoop(server.maxclients+1024);
e2641e09 1176 server.db = zmalloc(sizeof(redisDb)*server.dbnum);
68d6345d 1177
a53b4c24 1178 if (server.port != 0) {
68d6345d 1179 server.ipfd = anetTcpServer(server.neterr,server.port,server.bindaddr);
a53b4c24 1180 if (server.ipfd == ANET_ERR) {
eef17490 1181 redisLog(REDIS_WARNING, "Opening port %d: %s",
1182 server.port, server.neterr);
a53b4c24 1183 exit(1);
1184 }
a5639e7d 1185 }
5d10923f
PN
1186 if (server.unixsocket != NULL) {
1187 unlink(server.unixsocket); /* don't care if this fails */
85238765 1188 server.sofd = anetUnixServer(server.neterr,server.unixsocket,server.unixsocketperm);
a5639e7d
PN
1189 if (server.sofd == ANET_ERR) {
1190 redisLog(REDIS_WARNING, "Opening socket: %s", server.neterr);
1191 exit(1);
1192 }
c61e6925 1193 }
a5639e7d
PN
1194 if (server.ipfd < 0 && server.sofd < 0) {
1195 redisLog(REDIS_WARNING, "Configured to not listen anywhere, exiting.");
e2641e09 1196 exit(1);
1197 }
1198 for (j = 0; j < server.dbnum; j++) {
1199 server.db[j].dict = dictCreate(&dbDictType,NULL);
1200 server.db[j].expires = dictCreate(&keyptrDictType,NULL);
1201 server.db[j].blocking_keys = dictCreate(&keylistDictType,NULL);
1202 server.db[j].watched_keys = dictCreate(&keylistDictType,NULL);
e2641e09 1203 server.db[j].id = j;
1204 }
1205 server.pubsub_channels = dictCreate(&keylistDictType,NULL);
1206 server.pubsub_patterns = listCreate();
1207 listSetFreeMethod(server.pubsub_patterns,freePubsubPattern);
1208 listSetMatchMethod(server.pubsub_patterns,listMatchPubsubPattern);
1209 server.cronloops = 0;
f48cd4b9 1210 server.rdb_child_pid = -1;
ff2145ad 1211 server.aof_child_pid = -1;
1212 server.aof_rewrite_buf = sdsempty();
1213 server.aof_buf = sdsempty();
e2641e09 1214 server.lastsave = time(NULL);
1215 server.dirty = 0;
1216 server.stat_numcommands = 0;
1217 server.stat_numconnections = 0;
1218 server.stat_expiredkeys = 0;
f21779ff 1219 server.stat_evictedkeys = 0;
e2641e09 1220 server.stat_starttime = time(NULL);
53eeeaff 1221 server.stat_keyspace_misses = 0;
1222 server.stat_keyspace_hits = 0;
17b24ff3 1223 server.stat_peak_memory = 0;
615e414c 1224 server.stat_fork_time = 0;
3c95e721 1225 server.stat_rejected_conn = 0;
250e7f69 1226 memset(server.ops_sec_samples,0,sizeof(server.ops_sec_samples));
1227 server.ops_sec_idx = 0;
1228 server.ops_sec_last_sample_time = mstime();
1229 server.ops_sec_last_sample_ops = 0;
e2641e09 1230 server.unixtime = time(NULL);
c25e7eaf 1231 server.lastbgsave_status = REDIS_OK;
4d3bbf35 1232 server.stop_writes_on_bgsave_err = 1;
e2641e09 1233 aeCreateTimeEvent(server.el, 1, serverCron, NULL, NULL);
a5639e7d 1234 if (server.ipfd > 0 && aeCreateFileEvent(server.el,server.ipfd,AE_READABLE,
ab17b909 1235 acceptTcpHandler,NULL) == AE_ERR) oom("creating file event");
a5639e7d 1236 if (server.sofd > 0 && aeCreateFileEvent(server.el,server.sofd,AE_READABLE,
ab17b909 1237 acceptUnixHandler,NULL) == AE_ERR) oom("creating file event");
e2641e09 1238
e394114d 1239 if (server.aof_state == REDIS_AOF_ON) {
ff2145ad 1240 server.aof_fd = open(server.aof_filename,
e394114d 1241 O_WRONLY|O_APPEND|O_CREAT,0644);
ff2145ad 1242 if (server.aof_fd == -1) {
e2641e09 1243 redisLog(REDIS_WARNING, "Can't open the append-only file: %s",
1244 strerror(errno));
1245 exit(1);
1246 }
1247 }
1248
d876678b 1249 /* 32 bit instances are limited to 4GB of address space, so if there is
1250 * no explicit limit in the user provided configuration we set a limit
1251 * at 3.5GB using maxmemory with 'noeviction' policy'. This saves
1252 * useless crashes of the Redis instance. */
1253 if (server.arch_bits == 32 && server.maxmemory == 0) {
1254 redisLog(REDIS_WARNING,"Warning: 32 bit instance detected but no memory limit set. Setting 3.5 GB maxmemory limit with 'noeviction' policy now.");
1255 server.maxmemory = 3584LL*(1024*1024); /* 3584 MB = 3.5 GB */
1256 server.maxmemory_policy = REDIS_MAXMEMORY_NO_EVICTION;
1257 }
1258
7585836e 1259 scriptingInit();
daa70b17 1260 slowlogInit();
8f61a72f 1261 bioInit();
e2641e09 1262}
1263
1b1f47c9 1264/* Populates the Redis Command Table starting from the hard coded list
1265 * we have on top of redis.c file. */
1266void populateCommandTable(void) {
1267 int j;
d7ed7fd2 1268 int numcommands = sizeof(redisCommandTable)/sizeof(struct redisCommand);
1b1f47c9 1269
1270 for (j = 0; j < numcommands; j++) {
d7ed7fd2 1271 struct redisCommand *c = redisCommandTable+j;
5d02b00f 1272 char *f = c->sflags;
1b1f47c9 1273 int retval;
e2641e09 1274
5d02b00f 1275 while(*f != '\0') {
1276 switch(*f) {
1277 case 'w': c->flags |= REDIS_CMD_WRITE; break;
1278 case 'r': c->flags |= REDIS_CMD_READONLY; break;
1279 case 'm': c->flags |= REDIS_CMD_DENYOOM; break;
1280 case 'a': c->flags |= REDIS_CMD_ADMIN; break;
1281 case 'p': c->flags |= REDIS_CMD_PUBSUB; break;
1282 case 'f': c->flags |= REDIS_CMD_FORCE_REPLICATION; break;
b60ed6e8 1283 case 's': c->flags |= REDIS_CMD_NOSCRIPT; break;
1284 case 'R': c->flags |= REDIS_CMD_RANDOM; break;
548efd91 1285 case 'S': c->flags |= REDIS_CMD_SORT_FOR_SCRIPT; break;
5d02b00f 1286 default: redisPanic("Unsupported command flag"); break;
1287 }
1288 f++;
1289 }
1290
1b1f47c9 1291 retval = dictAdd(server.commands, sdsnew(c->name), c);
1292 assert(retval == DICT_OK);
1293 }
e2641e09 1294}
1295
d7ed7fd2 1296void resetCommandTableStats(void) {
1297 int numcommands = sizeof(redisCommandTable)/sizeof(struct redisCommand);
1298 int j;
1299
1300 for (j = 0; j < numcommands; j++) {
1301 struct redisCommand *c = redisCommandTable+j;
1302
1303 c->microseconds = 0;
1304 c->calls = 0;
1305 }
1306}
1307
78d6a22d 1308/* ========================== Redis OP Array API ============================ */
1309
1310void redisOpArrayInit(redisOpArray *oa) {
1311 oa->ops = NULL;
1312 oa->numops = 0;
1313}
1314
1315int redisOpArrayAppend(redisOpArray *oa, struct redisCommand *cmd, int dbid,
1316 robj **argv, int argc, int target)
1317{
1318 redisOp *op;
1319
1320 oa->ops = zrealloc(oa->ops,sizeof(redisOp)*(oa->numops+1));
1321 op = oa->ops+oa->numops;
1322 op->cmd = cmd;
1323 op->dbid = dbid;
1324 op->argv = argv;
1325 op->argc = argc;
1326 op->target = target;
1327 oa->numops++;
1328 return oa->numops;
1329}
1330
1331void redisOpArrayFree(redisOpArray *oa) {
1332 while(oa->numops) {
1333 int j;
1334 redisOp *op;
1335
1336 oa->numops--;
1337 op = oa->ops+oa->numops;
1338 for (j = 0; j < op->argc; j++)
1339 decrRefCount(op->argv[j]);
1340 zfree(op->argv);
1341 }
1342 zfree(oa->ops);
1343}
1344
e2641e09 1345/* ====================== Commands lookup and execution ===================== */
1346
1b1f47c9 1347struct redisCommand *lookupCommand(sds name) {
1348 return dictFetchValue(server.commands, name);
1349}
1350
1351struct redisCommand *lookupCommandByCString(char *s) {
1352 struct redisCommand *cmd;
1353 sds name = sdsnew(s);
1354
1355 cmd = dictFetchValue(server.commands, name);
1356 sdsfree(name);
1357 return cmd;
e2641e09 1358}
1359
ad08d059 1360/* Propagate the specified command (in the context of the specified database id)
1361 * to AOF, Slaves and Monitors.
1362 *
1363 * flags are an xor between:
1364 * + REDIS_PROPAGATE_NONE (no propagation of command at all)
1365 * + REDIS_PROPAGATE_AOF (propagate into the AOF file if is enabled)
1366 * + REDIS_PROPAGATE_REPL (propagate into the replication link)
1367 */
1368void propagate(struct redisCommand *cmd, int dbid, robj **argv, int argc,
1369 int flags)
1370{
1371 if (server.aof_state != REDIS_AOF_OFF && flags & REDIS_PROPAGATE_AOF)
1372 feedAppendOnlyFile(cmd,dbid,argv,argc);
1373 if (flags & REDIS_PROPAGATE_REPL && listLength(server.slaves))
1374 replicationFeedSlaves(server.slaves,dbid,argv,argc);
1375}
1376
78d6a22d 1377/* Used inside commands to schedule the propagation of additional commands
1378 * after the current command is propagated to AOF / Replication. */
eeb34eff 1379void alsoPropagate(struct redisCommand *cmd, int dbid, robj **argv, int argc,
1380 int target)
1381{
78d6a22d 1382 redisOpArrayAppend(&server.also_propagate,cmd,dbid,argv,argc,target);
eeb34eff 1383}
1384
e2641e09 1385/* Call() is the core of Redis execution of a command */
ce8b772b 1386void call(redisClient *c, int flags) {
daa70b17 1387 long long dirty, start = ustime(), duration;
e2641e09 1388
ad08d059 1389 /* Sent the command to clients in MONITOR mode, only if the commands are
1390 * not geneated from reading an AOF. */
1391 if (listLength(server.monitors) && !server.loading)
e31b615e 1392 replicationFeedMonitors(c,server.monitors,c->db->id,c->argv,c->argc);
ad08d059 1393
1394 /* Call the command. */
78d6a22d 1395 redisOpArrayInit(&server.also_propagate);
e2641e09 1396 dirty = server.dirty;
09e2d9ee 1397 c->cmd->proc(c);
e2641e09 1398 dirty = server.dirty-dirty;
daa70b17 1399 duration = ustime()-start;
ce8b772b 1400
1401 /* When EVAL is called loading the AOF we don't want commands called
1402 * from Lua to go into the slowlog or to populate statistics. */
1403 if (server.loading && c->flags & REDIS_LUA_CLIENT)
1404 flags &= ~(REDIS_CALL_SLOWLOG | REDIS_CALL_STATS);
1405
ad08d059 1406 /* Log the command into the Slow log if needed, and populate the
1407 * per-command statistics that we show in INFO commandstats. */
ce8b772b 1408 if (flags & REDIS_CALL_SLOWLOG)
1409 slowlogPushEntryIfNeeded(c->argv,c->argc,duration);
1410 if (flags & REDIS_CALL_STATS) {
1411 c->cmd->microseconds += duration;
1412 c->cmd->calls++;
1413 }
ad08d059 1414
1415 /* Propagate the command into the AOF and replication link */
ce8b772b 1416 if (flags & REDIS_CALL_PROPAGATE) {
ad08d059 1417 int flags = REDIS_PROPAGATE_NONE;
1418
1419 if (c->cmd->flags & REDIS_CMD_FORCE_REPLICATION)
1420 flags |= REDIS_PROPAGATE_REPL;
1421 if (dirty)
1422 flags |= (REDIS_PROPAGATE_REPL | REDIS_PROPAGATE_AOF);
1423 if (flags != REDIS_PROPAGATE_NONE)
1424 propagate(c->cmd,c->db->id,c->argv,c->argc,flags);
ce8b772b 1425 }
eeb34eff 1426 /* Commands such as LPUSH or BRPOPLPUSH may propagate an additional
1427 * PUSH command. */
78d6a22d 1428 if (server.also_propagate.numops) {
eeb34eff 1429 int j;
78d6a22d 1430 redisOp *rop;
eeb34eff 1431
78d6a22d 1432 for (j = 0; j < server.also_propagate.numops; j++) {
1433 rop = &server.also_propagate.ops[j];
1434 propagate(rop->cmd, rop->dbid, rop->argv, rop->argc, rop->target);
1435 }
1436 redisOpArrayFree(&server.also_propagate);
eeb34eff 1437 }
e2641e09 1438 server.stat_numcommands++;
1439}
1440
1441/* If this function gets called we already read a whole
1442 * command, argments are in the client argv/argc fields.
1443 * processCommand() execute the command or prepare the
1444 * server for a bulk read from the client.
1445 *
1446 * If 1 is returned the client is still alive and valid and
1447 * and other operations can be performed by the caller. Otherwise
1448 * if 0 is returned the client was destroied (i.e. after QUIT). */
1449int processCommand(redisClient *c) {
941c9fa2
PN
1450 /* The QUIT command is handled separately. Normal command procs will
1451 * go through checking for replication and QUIT will cause trouble
1452 * when FORCE_REPLICATION is enabled and would be implemented in
1453 * a regular command proc. */
e2641e09 1454 if (!strcasecmp(c->argv[0]->ptr,"quit")) {
941c9fa2 1455 addReply(c,shared.ok);
5e78edb3 1456 c->flags |= REDIS_CLOSE_AFTER_REPLY;
cd8788f2 1457 return REDIS_ERR;
e2641e09 1458 }
1459
1460 /* Now lookup the command and check ASAP about trivial error conditions
09e2d9ee 1461 * such as wrong arity, bad command name and so forth. */
2c74a9f9 1462 c->cmd = c->lastcmd = lookupCommand(c->argv[0]->ptr);
09e2d9ee 1463 if (!c->cmd) {
3ab20376
PN
1464 addReplyErrorFormat(c,"unknown command '%s'",
1465 (char*)c->argv[0]->ptr);
cd8788f2 1466 return REDIS_OK;
09e2d9ee 1467 } else if ((c->cmd->arity > 0 && c->cmd->arity != c->argc) ||
1468 (c->argc < -c->cmd->arity)) {
3ab20376 1469 addReplyErrorFormat(c,"wrong number of arguments for '%s' command",
09e2d9ee 1470 c->cmd->name);
cd8788f2 1471 return REDIS_OK;
e2641e09 1472 }
e2641e09 1473
1474 /* Check if the user is authenticated */
09e2d9ee 1475 if (server.requirepass && !c->authenticated && c->cmd->proc != authCommand)
1476 {
3ab20376 1477 addReplyError(c,"operation not permitted");
cd8788f2 1478 return REDIS_OK;
e2641e09 1479 }
1480
1dd10ca2 1481 /* Handle the maxmemory directive.
1482 *
1483 * First we try to free some memory if possible (if there are volatile
1484 * keys in the dataset). If there are not the only thing we can do
1485 * is returning an error. */
f6b32c14 1486 if (server.maxmemory) {
1487 int retval = freeMemoryIfNeeded();
1488 if ((c->cmd->flags & REDIS_CMD_DENYOOM) && retval == REDIS_ERR) {
05406168 1489 addReply(c, shared.oomerr);
f6b32c14 1490 return REDIS_OK;
1491 }
e2641e09 1492 }
1493
c25e7eaf 1494 /* Don't accept write commands if there are problems persisting on disk. */
4d3bbf35 1495 if (server.stop_writes_on_bgsave_err &&
1496 server.saveparamslen > 0
1497 && server.lastbgsave_status == REDIS_ERR &&
c25e7eaf 1498 c->cmd->flags & REDIS_CMD_WRITE)
1499 {
1500 addReply(c, shared.bgsaveerr);
1501 return REDIS_OK;
05406168 1502 }
1503
1504 /* Don't accept wirte commands if this is a read only slave. But
1505 * accept write commands if this is our master. */
1506 if (server.masterhost && server.repl_slave_ro &&
1507 !(c->flags & REDIS_MASTER) &&
1508 c->cmd->flags & REDIS_CMD_WRITE)
1509 {
1510 addReply(c, shared.roslaveerr);
1511 return REDIS_OK;
c25e7eaf 1512 }
1513
e2641e09 1514 /* Only allow SUBSCRIBE and UNSUBSCRIBE in the context of Pub/Sub */
1515 if ((dictSize(c->pubsub_channels) > 0 || listLength(c->pubsub_patterns) > 0)
1516 &&
09e2d9ee 1517 c->cmd->proc != subscribeCommand &&
1518 c->cmd->proc != unsubscribeCommand &&
1519 c->cmd->proc != psubscribeCommand &&
1520 c->cmd->proc != punsubscribeCommand) {
3ab20376 1521 addReplyError(c,"only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context");
cd8788f2 1522 return REDIS_OK;
e2641e09 1523 }
1524
4ebfc455 1525 /* Only allow INFO and SLAVEOF when slave-serve-stale-data is no and
1526 * we are a slave with a broken link with master. */
1844f990 1527 if (server.masterhost && server.repl_state != REDIS_REPL_CONNECTED &&
4ebfc455 1528 server.repl_serve_stale_data == 0 &&
09e2d9ee 1529 c->cmd->proc != infoCommand && c->cmd->proc != slaveofCommand)
4ebfc455 1530 {
1531 addReplyError(c,
1532 "link with MASTER is down and slave-serve-stale-data is set to no");
1533 return REDIS_OK;
1534 }
1535
97e7f8ae 1536 /* Loading DB? Return an error if the command is not INFO */
09e2d9ee 1537 if (server.loading && c->cmd->proc != infoCommand) {
97e7f8ae 1538 addReply(c, shared.loadingerr);
1539 return REDIS_OK;
1540 }
1541
4ab8695d 1542 /* Lua script too slow? Only allow SHUTDOWN NOSAVE and SCRIPT KILL. */
1543 if (server.lua_timedout &&
1544 !(c->cmd->proc != shutdownCommand &&
1545 c->argc == 2 &&
1546 tolower(((char*)c->argv[1]->ptr)[0]) == 'n') &&
1547 !(c->cmd->proc == scriptCommand &&
1548 c->argc == 2 &&
1549 tolower(((char*)c->argv[1]->ptr)[0]) == 'k'))
1550 {
115e3ff3 1551 addReply(c, shared.slowscripterr);
1552 return REDIS_OK;
1553 }
1554
e2641e09 1555 /* Exec the command */
1556 if (c->flags & REDIS_MULTI &&
09e2d9ee 1557 c->cmd->proc != execCommand && c->cmd->proc != discardCommand &&
1558 c->cmd->proc != multiCommand && c->cmd->proc != watchCommand)
e2641e09 1559 {
09e2d9ee 1560 queueMultiCommand(c);
e2641e09 1561 addReply(c,shared.queued);
1562 } else {
ce8b772b 1563 call(c,REDIS_CALL_FULL);
e2641e09 1564 }
cd8788f2 1565 return REDIS_OK;
e2641e09 1566}
1567
1568/*================================== Shutdown =============================== */
1569
4ab8695d 1570int prepareForShutdown(int flags) {
1571 int save = flags & REDIS_SHUTDOWN_SAVE;
1572 int nosave = flags & REDIS_SHUTDOWN_NOSAVE;
1573
adae85cd 1574 redisLog(REDIS_WARNING,"User requested shutdown...");
e2641e09 1575 /* Kill the saving child if there is a background saving in progress.
1576 We want to avoid race conditions, for instance our saving child may
1577 overwrite the synchronous saving did by SHUTDOWN. */
f48cd4b9 1578 if (server.rdb_child_pid != -1) {
adae85cd 1579 redisLog(REDIS_WARNING,"There is a child saving an .rdb. Killing it!");
f48cd4b9 1580 kill(server.rdb_child_pid,SIGKILL);
1581 rdbRemoveTempFile(server.rdb_child_pid);
e2641e09 1582 }
e394114d 1583 if (server.aof_state != REDIS_AOF_OFF) {
adae85cd 1584 /* Kill the AOF saving child as the AOF we already have may be longer
1585 * but contains the full dataset anyway. */
ff2145ad 1586 if (server.aof_child_pid != -1) {
adae85cd 1587 redisLog(REDIS_WARNING,
1588 "There is a child rewriting the AOF. Killing it!");
ff2145ad 1589 kill(server.aof_child_pid,SIGKILL);
adae85cd 1590 }
e2641e09 1591 /* Append only file: fsync() the AOF and exit */
adae85cd 1592 redisLog(REDIS_NOTICE,"Calling fsync() on the AOF file.");
ff2145ad 1593 aof_fsync(server.aof_fd);
adae85cd 1594 }
4ab8695d 1595 if ((server.saveparamslen > 0 && !nosave) || save) {
adae85cd 1596 redisLog(REDIS_NOTICE,"Saving the final RDB snapshot before exiting.");
e2641e09 1597 /* Snapshotting. Perform a SYNC SAVE and exit */
f48cd4b9 1598 if (rdbSave(server.rdb_filename) != REDIS_OK) {
e2641e09 1599 /* Ooops.. error saving! The best we can do is to continue
1600 * operating. Note that if there was a background saving process,
1601 * in the next cron() Redis will be notified that the background
1602 * saving aborted, handling special stuff like slaves pending for
1603 * synchronization... */
adae85cd 1604 redisLog(REDIS_WARNING,"Error trying to save the DB, can't exit.");
e2641e09 1605 return REDIS_ERR;
1606 }
1607 }
adae85cd 1608 if (server.daemonize) {
1609 redisLog(REDIS_NOTICE,"Removing the pid file.");
1610 unlink(server.pidfile);
1611 }
80e87a46 1612 /* Close the listening sockets. Apparently this allows faster restarts. */
1613 if (server.ipfd != -1) close(server.ipfd);
1614 if (server.sofd != -1) close(server.sofd);
56209f72
NF
1615 if (server.unixsocket) {
1616 redisLog(REDIS_NOTICE,"Removing the unix socket file.");
1617 unlink(server.unixsocket); /* don't care if this fails */
1618 }
80e87a46 1619
adae85cd 1620 redisLog(REDIS_WARNING,"Redis is now ready to exit, bye bye...");
e2641e09 1621 return REDIS_OK;
1622}
1623
1624/*================================== Commands =============================== */
1625
1626void authCommand(redisClient *c) {
ab52d1f4 1627 if (!server.requirepass) {
1628 addReplyError(c,"Client sent AUTH, but no password is set");
1629 } else if (!strcmp(c->argv[1]->ptr, server.requirepass)) {
e2641e09 1630 c->authenticated = 1;
1631 addReply(c,shared.ok);
1632 } else {
1633 c->authenticated = 0;
3ab20376 1634 addReplyError(c,"invalid password");
e2641e09 1635 }
1636}
1637
1638void pingCommand(redisClient *c) {
1639 addReply(c,shared.pong);
1640}
1641
1642void echoCommand(redisClient *c) {
1643 addReplyBulk(c,c->argv[1]);
1644}
1645
9494f1f1 1646void timeCommand(redisClient *c) {
1647 struct timeval tv;
1648
1649 /* gettimeofday() can only fail if &tv is a bad addresss so we
1650 * don't check for errors. */
1651 gettimeofday(&tv,NULL);
1652 addReplyMultiBulkLen(c,2);
1653 addReplyBulkLongLong(c,tv.tv_sec);
1654 addReplyBulkLongLong(c,tv.tv_usec);
1655}
1656
e2641e09 1657/* Convert an amount of bytes into a human readable string in the form
1658 * of 100B, 2G, 100M, 4K, and so forth. */
1659void bytesToHuman(char *s, unsigned long long n) {
1660 double d;
1661
1662 if (n < 1024) {
1663 /* Bytes */
1664 sprintf(s,"%lluB",n);
1665 return;
1666 } else if (n < (1024*1024)) {
1667 d = (double)n/(1024);
1668 sprintf(s,"%.2fK",d);
1669 } else if (n < (1024LL*1024*1024)) {
1670 d = (double)n/(1024*1024);
1671 sprintf(s,"%.2fM",d);
1672 } else if (n < (1024LL*1024*1024*1024)) {
1673 d = (double)n/(1024LL*1024*1024);
1674 sprintf(s,"%.2fG",d);
1675 }
1676}
1677
1678/* Create the string returned by the INFO command. This is decoupled
1679 * by the INFO command itself as we need to report the same information
1680 * on memory corruption problems. */
1b085c9f 1681sds genRedisInfoString(char *section) {
1682 sds info = sdsempty();
56ff70f8 1683 time_t uptime = server.unixtime-server.stat_starttime;
d9cb288c 1684 int j, numcommands;
2b00385d 1685 struct rusage self_ru, c_ru;
7a1fd61e 1686 unsigned long lol, bib;
1b085c9f 1687 int allsections = 0, defsections = 0;
1688 int sections = 0;
1689
1690 if (section) {
1691 allsections = strcasecmp(section,"all") == 0;
0d808ef2 1692 defsections = strcasecmp(section,"default") == 0;
1b085c9f 1693 }
2b00385d 1694
1695 getrusage(RUSAGE_SELF, &self_ru);
1696 getrusage(RUSAGE_CHILDREN, &c_ru);
7a1fd61e 1697 getClientsMaxBuffers(&lol,&bib);
1b085c9f 1698
1699 /* Server */
1700 if (allsections || defsections || !strcasecmp(section,"server")) {
fa2a27cf 1701 struct utsname name;
1702
1b085c9f 1703 if (sections++) info = sdscat(info,"\r\n");
fa2a27cf 1704 uname(&name);
e2641e09 1705 info = sdscatprintf(info,
1b085c9f 1706 "# Server\r\n"
1707 "redis_version:%s\r\n"
1708 "redis_git_sha1:%s\r\n"
1709 "redis_git_dirty:%d\r\n"
fa2a27cf 1710 "os:%s %s %s\r\n"
75eaac5c 1711 "arch_bits:%d\r\n"
1b085c9f 1712 "multiplexing_api:%s\r\n"
5db904bd 1713 "gcc_version:%d.%d.%d\r\n"
1b085c9f 1714 "process_id:%ld\r\n"
91d664d6 1715 "run_id:%s\r\n"
1b085c9f 1716 "tcp_port:%d\r\n"
1717 "uptime_in_seconds:%ld\r\n"
1718 "uptime_in_days:%ld\r\n"
1719 "lru_clock:%ld\r\n",
1720 REDIS_VERSION,
1721 redisGitSHA1(),
1722 strtol(redisGitDirty(),NULL,10) > 0,
fa2a27cf 1723 name.sysname, name.release, name.machine,
75eaac5c 1724 server.arch_bits,
1b085c9f 1725 aeGetApiName(),
5db904bd 1726#ifdef __GNUC__
1727 __GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__,
1728#else
1729 0,0,0,
1730#endif
1b085c9f 1731 (long) getpid(),
91d664d6 1732 server.runid,
1b085c9f 1733 server.port,
1734 uptime,
1735 uptime/(3600*24),
1736 (unsigned long) server.lruclock);
1737 }
1738
1739 /* Clients */
1740 if (allsections || defsections || !strcasecmp(section,"clients")) {
1741 if (sections++) info = sdscat(info,"\r\n");
1742 info = sdscatprintf(info,
1743 "# Clients\r\n"
3c08fdae 1744 "connected_clients:%lu\r\n"
1b085c9f 1745 "client_longest_output_list:%lu\r\n"
1746 "client_biggest_input_buf:%lu\r\n"
1747 "blocked_clients:%d\r\n",
1748 listLength(server.clients)-listLength(server.slaves),
1749 lol, bib,
1750 server.bpop_blocked_clients);
1751 }
1752
1753 /* Memory */
1754 if (allsections || defsections || !strcasecmp(section,"memory")) {
17b24ff3 1755 char hmem[64];
1756 char peak_hmem[64];
1757
1758 bytesToHuman(hmem,zmalloc_used_memory());
1759 bytesToHuman(peak_hmem,server.stat_peak_memory);
1b085c9f 1760 if (sections++) info = sdscat(info,"\r\n");
1761 info = sdscatprintf(info,
1762 "# Memory\r\n"
1763 "used_memory:%zu\r\n"
1764 "used_memory_human:%s\r\n"
1765 "used_memory_rss:%zu\r\n"
17b24ff3 1766 "used_memory_peak:%zu\r\n"
1767 "used_memory_peak_human:%s\r\n"
8c3402df 1768 "used_memory_lua:%lld\r\n"
1b085c9f 1769 "mem_fragmentation_ratio:%.2f\r\n"
32f99c51 1770 "mem_allocator:%s\r\n",
1b085c9f 1771 zmalloc_used_memory(),
1772 hmem,
1773 zmalloc_get_rss(),
17b24ff3 1774 server.stat_peak_memory,
1775 peak_hmem,
8c3402df 1776 ((long long)lua_gc(server.lua,LUA_GCCOUNT,0))*1024LL,
1b085c9f 1777 zmalloc_get_fragmentation_ratio(),
fec5a664 1778 ZMALLOC_LIB
12ebe2ac 1779 );
0d808ef2 1780 }
1781
1b085c9f 1782 /* Persistence */
1783 if (allsections || defsections || !strcasecmp(section,"persistence")) {
1784 if (sections++) info = sdscat(info,"\r\n");
e2641e09 1785 info = sdscatprintf(info,
1b085c9f 1786 "# Persistence\r\n"
1787 "loading:%d\r\n"
1788 "aof_enabled:%d\r\n"
1789 "changes_since_last_save:%lld\r\n"
1790 "bgsave_in_progress:%d\r\n"
1791 "last_save_time:%ld\r\n"
c25e7eaf 1792 "last_bgsave_status:%s\r\n"
1b085c9f 1793 "bgrewriteaof_in_progress:%d\r\n",
1794 server.loading,
e394114d 1795 server.aof_state != REDIS_AOF_OFF,
1b085c9f 1796 server.dirty,
f48cd4b9 1797 server.rdb_child_pid != -1,
1b085c9f 1798 server.lastsave,
c25e7eaf 1799 server.lastbgsave_status == REDIS_OK ? "ok" : "err",
ff2145ad 1800 server.aof_child_pid != -1);
1b085c9f 1801
e394114d 1802 if (server.aof_state != REDIS_AOF_OFF) {
d630abcd 1803 info = sdscatprintf(info,
1804 "aof_current_size:%lld\r\n"
1805 "aof_base_size:%lld\r\n"
e7ef418c 1806 "aof_pending_rewrite:%d\r\n"
1807 "aof_buffer_length:%zu\r\n"
81f32c7b 1808 "aof_pending_bio_fsync:%llu\r\n"
1809 "aof_delayed_fsync:%lu\r\n",
2c915bcf 1810 (long long) server.aof_current_size,
1811 (long long) server.aof_rewrite_base_size,
1812 server.aof_rewrite_scheduled,
ff2145ad 1813 sdslen(server.aof_buf),
81f32c7b 1814 bioPendingJobsOfType(REDIS_BIO_AOF_FSYNC),
1815 server.aof_delayed_fsync);
d630abcd 1816 }
1817
1b085c9f 1818 if (server.loading) {
1819 double perc;
1820 time_t eta, elapsed;
1821 off_t remaining_bytes = server.loading_total_bytes-
1822 server.loading_loaded_bytes;
1823
1824 perc = ((double)server.loading_loaded_bytes /
1825 server.loading_total_bytes) * 100;
1826
56ff70f8 1827 elapsed = server.unixtime-server.loading_start_time;
1b085c9f 1828 if (elapsed == 0) {
1829 eta = 1; /* A fake 1 second figure if we don't have
1830 enough info */
1831 } else {
1832 eta = (elapsed*remaining_bytes)/server.loading_loaded_bytes;
1833 }
1834
1835 info = sdscatprintf(info,
1836 "loading_start_time:%ld\r\n"
1837 "loading_total_bytes:%llu\r\n"
1838 "loading_loaded_bytes:%llu\r\n"
1839 "loading_loaded_perc:%.2f\r\n"
1840 "loading_eta_seconds:%ld\r\n"
1841 ,(unsigned long) server.loading_start_time,
1842 (unsigned long long) server.loading_total_bytes,
1843 (unsigned long long) server.loading_loaded_bytes,
1844 perc,
1845 eta
1846 );
1847 }
e2641e09 1848 }
1b085c9f 1849
1b085c9f 1850 /* Stats */
1851 if (allsections || defsections || !strcasecmp(section,"stats")) {
1852 if (sections++) info = sdscat(info,"\r\n");
97e7f8ae 1853 info = sdscatprintf(info,
1b085c9f 1854 "# Stats\r\n"
1855 "total_connections_received:%lld\r\n"
1856 "total_commands_processed:%lld\r\n"
250e7f69 1857 "instantaneous_ops_per_sec:%lld\r\n"
3c95e721 1858 "rejected_connections:%lld\r\n"
1b085c9f 1859 "expired_keys:%lld\r\n"
1860 "evicted_keys:%lld\r\n"
1861 "keyspace_hits:%lld\r\n"
1862 "keyspace_misses:%lld\r\n"
1863 "pubsub_channels:%ld\r\n"
3c08fdae 1864 "pubsub_patterns:%lu\r\n"
615e414c 1865 "latest_fork_usec:%lld\r\n",
1b085c9f 1866 server.stat_numconnections,
1867 server.stat_numcommands,
250e7f69 1868 getOperationsPerSecond(),
3c95e721 1869 server.stat_rejected_conn,
1b085c9f 1870 server.stat_expiredkeys,
1871 server.stat_evictedkeys,
1872 server.stat_keyspace_hits,
1873 server.stat_keyspace_misses,
1874 dictSize(server.pubsub_channels),
615e414c 1875 listLength(server.pubsub_patterns),
1876 server.stat_fork_time);
97e7f8ae 1877 }
67a1810b 1878
1b085c9f 1879 /* Replication */
1880 if (allsections || defsections || !strcasecmp(section,"replication")) {
1881 if (sections++) info = sdscat(info,"\r\n");
1882 info = sdscatprintf(info,
1883 "# Replication\r\n"
1884 "role:%s\r\n",
1885 server.masterhost == NULL ? "master" : "slave");
1886 if (server.masterhost) {
1887 info = sdscatprintf(info,
1888 "master_host:%s\r\n"
1889 "master_port:%d\r\n"
1890 "master_link_status:%s\r\n"
1891 "master_last_io_seconds_ago:%d\r\n"
1892 "master_sync_in_progress:%d\r\n"
1893 ,server.masterhost,
1894 server.masterport,
1844f990 1895 (server.repl_state == REDIS_REPL_CONNECTED) ?
1b085c9f 1896 "up" : "down",
1897 server.master ?
56ff70f8 1898 ((int)(server.unixtime-server.master->lastinteraction)) : -1,
1844f990 1899 server.repl_state == REDIS_REPL_TRANSFER
1b085c9f 1900 );
1901
1844f990 1902 if (server.repl_state == REDIS_REPL_TRANSFER) {
1b085c9f 1903 info = sdscatprintf(info,
1904 "master_sync_left_bytes:%ld\r\n"
1905 "master_sync_last_io_seconds_ago:%d\r\n"
1906 ,(long)server.repl_transfer_left,
56ff70f8 1907 (int)(server.unixtime-server.repl_transfer_lastio)
1b085c9f 1908 );
1909 }
07486df6 1910
1844f990 1911 if (server.repl_state != REDIS_REPL_CONNECTED) {
07486df6 1912 info = sdscatprintf(info,
1913 "master_link_down_since_seconds:%ld\r\n",
56ff70f8 1914 (long)server.unixtime-server.repl_down_since);
07486df6 1915 }
67a1810b 1916 }
1b085c9f 1917 info = sdscatprintf(info,
3c08fdae 1918 "connected_slaves:%lu\r\n",
1b085c9f 1919 listLength(server.slaves));
503d87a8 1920 if (listLength(server.slaves)) {
1921 int slaveid = 0;
1922 listNode *ln;
1923 listIter li;
1924
1925 listRewind(server.slaves,&li);
1926 while((ln = listNext(&li))) {
1927 redisClient *slave = listNodeValue(ln);
1928 char *state = NULL;
1929 char ip[32];
1930 int port;
1931
1932 if (anetPeerToString(slave->fd,ip,&port) == -1) continue;
1933 switch(slave->replstate) {
1934 case REDIS_REPL_WAIT_BGSAVE_START:
1935 case REDIS_REPL_WAIT_BGSAVE_END:
1936 state = "wait_bgsave";
1937 break;
1938 case REDIS_REPL_SEND_BULK:
1939 state = "send_bulk";
1940 break;
1941 case REDIS_REPL_ONLINE:
1942 state = "online";
1943 break;
1944 }
1945 if (state == NULL) continue;
1946 info = sdscatprintf(info,"slave%d:%s,%d,%s\r\n",
1947 slaveid,ip,port,state);
1948 slaveid++;
1949 }
1950 }
67a1810b 1951 }
67a1810b 1952
0d808ef2 1953 /* CPU */
1954 if (allsections || defsections || !strcasecmp(section,"cpu")) {
1b085c9f 1955 if (sections++) info = sdscat(info,"\r\n");
1956 info = sdscatprintf(info,
0d808ef2 1957 "# CPU\r\n"
1b085c9f 1958 "used_cpu_sys:%.2f\r\n"
1959 "used_cpu_user:%.2f\r\n"
5a9dd97c 1960 "used_cpu_sys_children:%.2f\r\n"
1961 "used_cpu_user_children:%.2f\r\n",
1b085c9f 1962 (float)self_ru.ru_stime.tv_sec+(float)self_ru.ru_stime.tv_usec/1000000,
d83eda48 1963 (float)self_ru.ru_utime.tv_sec+(float)self_ru.ru_utime.tv_usec/1000000,
1964 (float)c_ru.ru_stime.tv_sec+(float)c_ru.ru_stime.tv_usec/1000000,
1965 (float)c_ru.ru_utime.tv_sec+(float)c_ru.ru_utime.tv_usec/1000000);
0d808ef2 1966 }
1b085c9f 1967
0d808ef2 1968 /* cmdtime */
1969 if (allsections || !strcasecmp(section,"commandstats")) {
1970 if (sections++) info = sdscat(info,"\r\n");
1971 info = sdscatprintf(info, "# Commandstats\r\n");
d7ed7fd2 1972 numcommands = sizeof(redisCommandTable)/sizeof(struct redisCommand);
1b085c9f 1973 for (j = 0; j < numcommands; j++) {
d7ed7fd2 1974 struct redisCommand *c = redisCommandTable+j;
0d808ef2 1975
d7ed7fd2 1976 if (!c->calls) continue;
1977 info = sdscatprintf(info,
1978 "cmdstat_%s:calls=%lld,usec=%lld,usec_per_call=%.2f\r\n",
1979 c->name, c->calls, c->microseconds,
1980 (c->calls == 0) ? 0 : ((float)c->microseconds/c->calls));
1b085c9f 1981 }
d9cb288c 1982 }
1983
1b085c9f 1984 /* Key space */
1985 if (allsections || defsections || !strcasecmp(section,"keyspace")) {
1986 if (sections++) info = sdscat(info,"\r\n");
1987 info = sdscatprintf(info, "# Keyspace\r\n");
1988 for (j = 0; j < server.dbnum; j++) {
1989 long long keys, vkeys;
e2641e09 1990
1b085c9f 1991 keys = dictSize(server.db[j].dict);
1992 vkeys = dictSize(server.db[j].expires);
1993 if (keys || vkeys) {
1994 info = sdscatprintf(info, "db%d:keys=%lld,expires=%lld\r\n",
1995 j, keys, vkeys);
1996 }
e2641e09 1997 }
1998 }
1999 return info;
2000}
2001
2002void infoCommand(redisClient *c) {
1b085c9f 2003 char *section = c->argc == 2 ? c->argv[1]->ptr : "default";
2004
2005 if (c->argc > 2) {
2006 addReply(c,shared.syntaxerr);
2007 return;
2008 }
2009 sds info = genRedisInfoString(section);
e2641e09 2010 addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n",
2011 (unsigned long)sdslen(info)));
2012 addReplySds(c,info);
2013 addReply(c,shared.crlf);
2014}
2015
2016void monitorCommand(redisClient *c) {
2017 /* ignore MONITOR if aleady slave or in monitor mode */
2018 if (c->flags & REDIS_SLAVE) return;
2019
2020 c->flags |= (REDIS_SLAVE|REDIS_MONITOR);
2021 c->slaveseldb = 0;
2022 listAddNodeTail(server.monitors,c);
2023 addReply(c,shared.ok);
2024}
2025
2026/* ============================ Maxmemory directive ======================== */
2027
e2641e09 2028/* This function gets called when 'maxmemory' is set on the config file to limit
f6b32c14 2029 * the max memory used by the server, before processing a command.
e2641e09 2030 *
f6b32c14 2031 * The goal of the function is to free enough memory to keep Redis under the
2032 * configured memory limit.
e2641e09 2033 *
f6b32c14 2034 * The function starts calculating how many bytes should be freed to keep
2035 * Redis under the limit, and enters a loop selecting the best keys to
2036 * evict accordingly to the configured policy.
2037 *
2038 * If all the bytes needed to return back under the limit were freed the
2039 * function returns REDIS_OK, otherwise REDIS_ERR is returned, and the caller
2040 * should block the execution of commands that will result in more memory
2041 * used by the server.
e2641e09 2042 */
f6b32c14 2043int freeMemoryIfNeeded(void) {
2044 size_t mem_used, mem_tofree, mem_freed;
2045 int slaves = listLength(server.slaves);
2046
c1ef6ffe 2047 /* Remove the size of slaves output buffers and AOF buffer from the
2048 * count of used memory. */
f6b32c14 2049 mem_used = zmalloc_used_memory();
2050 if (slaves) {
2051 listIter li;
2052 listNode *ln;
2053
2054 listRewind(server.slaves,&li);
2055 while((ln = listNext(&li))) {
2056 redisClient *slave = listNodeValue(ln);
2057 unsigned long obuf_bytes = getClientOutputBufferMemoryUsage(slave);
2058 if (obuf_bytes > mem_used)
2059 mem_used = 0;
2060 else
2061 mem_used -= obuf_bytes;
2062 }
2063 }
c1ef6ffe 2064 if (server.aof_state != REDIS_AOF_OFF) {
2065 mem_used -= sdslen(server.aof_buf);
2066 mem_used -= sdslen(server.aof_rewrite_buf);
2067 }
5402c426 2068
f6b32c14 2069 /* Check if we are over the memory limit. */
2070 if (mem_used <= server.maxmemory) return REDIS_OK;
2071
2072 if (server.maxmemory_policy == REDIS_MAXMEMORY_NO_EVICTION)
2073 return REDIS_ERR; /* We need to free memory, but policy forbids. */
2074
2075 /* Compute how much memory we need to free. */
2076 mem_tofree = mem_used - server.maxmemory;
f6b32c14 2077 mem_freed = 0;
2078 while (mem_freed < mem_tofree) {
2079 int j, k, keys_freed = 0;
e2641e09 2080
165346ca 2081 for (j = 0; j < server.dbnum; j++) {
10c12171 2082 long bestval = 0; /* just to prevent warning */
165346ca 2083 sds bestkey = NULL;
2084 struct dictEntry *de;
2085 redisDb *db = server.db+j;
2086 dict *dict;
2087
2088 if (server.maxmemory_policy == REDIS_MAXMEMORY_ALLKEYS_LRU ||
2089 server.maxmemory_policy == REDIS_MAXMEMORY_ALLKEYS_RANDOM)
2090 {
2091 dict = server.db[j].dict;
2092 } else {
2093 dict = server.db[j].expires;
2094 }
2095 if (dictSize(dict) == 0) continue;
2096
2097 /* volatile-random and allkeys-random policy */
2098 if (server.maxmemory_policy == REDIS_MAXMEMORY_ALLKEYS_RANDOM ||
2099 server.maxmemory_policy == REDIS_MAXMEMORY_VOLATILE_RANDOM)
2100 {
2101 de = dictGetRandomKey(dict);
c0ba9ebe 2102 bestkey = dictGetKey(de);
165346ca 2103 }
2104
2105 /* volatile-lru and allkeys-lru policy */
2106 else if (server.maxmemory_policy == REDIS_MAXMEMORY_ALLKEYS_LRU ||
2107 server.maxmemory_policy == REDIS_MAXMEMORY_VOLATILE_LRU)
2108 {
2109 for (k = 0; k < server.maxmemory_samples; k++) {
2110 sds thiskey;
2111 long thisval;
2112 robj *o;
2113
2114 de = dictGetRandomKey(dict);
c0ba9ebe 2115 thiskey = dictGetKey(de);
0c2f75c6 2116 /* When policy is volatile-lru we need an additonal lookup
2117 * to locate the real key, as dict is set to db->expires. */
2118 if (server.maxmemory_policy == REDIS_MAXMEMORY_VOLATILE_LRU)
2119 de = dictFind(db->dict, thiskey);
c0ba9ebe 2120 o = dictGetVal(de);
165346ca 2121 thisval = estimateObjectIdleTime(o);
2122
2123 /* Higher idle time is better candidate for deletion */
2124 if (bestkey == NULL || thisval > bestval) {
2125 bestkey = thiskey;
2126 bestval = thisval;
2127 }
2128 }
2129 }
2130
2131 /* volatile-ttl */
2132 else if (server.maxmemory_policy == REDIS_MAXMEMORY_VOLATILE_TTL) {
2133 for (k = 0; k < server.maxmemory_samples; k++) {
2134 sds thiskey;
2135 long thisval;
2136
2137 de = dictGetRandomKey(dict);
c0ba9ebe 2138 thiskey = dictGetKey(de);
2139 thisval = (long) dictGetVal(de);
165346ca 2140
2141 /* Expire sooner (minor expire unix timestamp) is better
2142 * candidate for deletion */
2143 if (bestkey == NULL || thisval < bestval) {
2144 bestkey = thiskey;
2145 bestval = thisval;
2146 }
2147 }
2148 }
2149
2150 /* Finally remove the selected key. */
2151 if (bestkey) {
f6b32c14 2152 long long delta;
2153
165346ca 2154 robj *keyobj = createStringObject(bestkey,sdslen(bestkey));
452229b6 2155 propagateExpire(db,keyobj);
f6b32c14 2156 /* We compute the amount of memory freed by dbDelete() alone.
2157 * It is possible that actually the memory needed to propagate
2158 * the DEL in AOF and replication link is greater than the one
2159 * we are freeing removing the key, but we can't account for
2160 * that otherwise we would never exit the loop.
2161 *
2162 * AOF and Output buffer memory will be freed eventually so
2163 * we only care about memory used by the key space. */
2164 delta = (long long) zmalloc_used_memory();
165346ca 2165 dbDelete(db,keyobj);
f6b32c14 2166 delta -= (long long) zmalloc_used_memory();
f6b32c14 2167 mem_freed += delta;
f21779ff 2168 server.stat_evictedkeys++;
165346ca 2169 decrRefCount(keyobj);
f6b32c14 2170 keys_freed++;
2171
2172 /* When the memory to free starts to be big enough, we may
2173 * start spending so much time here that is impossible to
2174 * deliver data to the slaves fast enough, so we force the
2175 * transmission here inside the loop. */
8b7c3455 2176 if (slaves) flushSlavesOutputBuffers();
165346ca 2177 }
2178 }
b129c6df 2179 if (!keys_freed) return REDIS_ERR; /* nothing to free... */
165346ca 2180 }
f6b32c14 2181 return REDIS_OK;
e2641e09 2182}
2183
2184/* =================================== Main! ================================ */
2185
2186#ifdef __linux__
2187int linuxOvercommitMemoryValue(void) {
2188 FILE *fp = fopen("/proc/sys/vm/overcommit_memory","r");
2189 char buf[64];
2190
2191 if (!fp) return -1;
2192 if (fgets(buf,64,fp) == NULL) {
2193 fclose(fp);
2194 return -1;
2195 }
2196 fclose(fp);
2197
2198 return atoi(buf);
2199}
2200
2201void linuxOvercommitMemoryWarning(void) {
2202 if (linuxOvercommitMemoryValue() == 0) {
2203 redisLog(REDIS_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.");
2204 }
2205}
2206#endif /* __linux__ */
2207
695fe874 2208void createPidFile(void) {
2209 /* Try to write the pid file in a best-effort way. */
2210 FILE *fp = fopen(server.pidfile,"w");
2211 if (fp) {
8ce39260 2212 fprintf(fp,"%d\n",(int)getpid());
695fe874 2213 fclose(fp);
2214 }
2215}
2216
e2641e09 2217void daemonize(void) {
2218 int fd;
e2641e09 2219
2220 if (fork() != 0) exit(0); /* parent exits */
2221 setsid(); /* create a new session */
2222
2223 /* Every output goes to /dev/null. If Redis is daemonized but
2224 * the 'logfile' is set to 'stdout' in the configuration file
2225 * it will not log at all. */
2226 if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
2227 dup2(fd, STDIN_FILENO);
2228 dup2(fd, STDOUT_FILENO);
2229 dup2(fd, STDERR_FILENO);
2230 if (fd > STDERR_FILENO) close(fd);
2231 }
e2641e09 2232}
2233
2234void version() {
188a17ed 2235 printf("Redis server v=%s sha=%s:%d malloc=%s\n", REDIS_VERSION,
2236 redisGitSHA1(), atoi(redisGitDirty()) > 0, ZMALLOC_LIB);
e2641e09 2237 exit(0);
2238}
2239
2240void usage() {
9268a5b5 2241 fprintf(stderr,"Usage: ./redis-server [/path/to/redis.conf] [options]\n");
e2641e09 2242 fprintf(stderr," ./redis-server - (read config from stdin)\n");
9268a5b5 2243 fprintf(stderr," ./redis-server -v or --version\n");
78d6a02b 2244 fprintf(stderr," ./redis-server -h or --help\n");
2245 fprintf(stderr," ./redis-server --test-memory <megabytes>\n\n");
9268a5b5 2246 fprintf(stderr,"Examples:\n");
2247 fprintf(stderr," ./redis-server (run the server with default conf)\n");
2248 fprintf(stderr," ./redis-server /etc/redis/6379.conf\n");
2249 fprintf(stderr," ./redis-server --port 7777\n");
2250 fprintf(stderr," ./redis-server --port 7777 --slaveof 127.0.0.1 8888\n");
2251 fprintf(stderr," ./redis-server /etc/myredis.conf --loglevel verbose\n");
e2641e09 2252 exit(1);
2253}
2254
996d503d 2255void redisAsciiArt(void) {
2256#include "asciilogo.h"
2257 char *buf = zmalloc(1024*16);
2258
2259 snprintf(buf,1024*16,ascii_logo,
2260 REDIS_VERSION,
2261 redisGitSHA1(),
2262 strtol(redisGitDirty(),NULL,10) > 0,
2263 (sizeof(long) == 8) ? "64" : "32",
571e257d 2264 "stand alone",
996d503d 2265 server.port,
2266 (long) getpid()
2267 );
2268 redisLogRaw(REDIS_NOTICE|REDIS_LOG_RAW,buf);
2269 zfree(buf);
2270}
2271
633a9410 2272static void sigtermHandler(int sig) {
e2641e09 2273 REDIS_NOTUSED(sig);
2274
be4f8ccc 2275 redisLogFromHandler(REDIS_WARNING,"Received SIGTERM, scheduling shutdown...");
e2641e09 2276 server.shutdown_asap = 1;
2277}
2278
633a9410 2279void setupSignalHandlers(void) {
e2641e09 2280 struct sigaction act;
2281
633a9410
PN
2282 /* When the SA_SIGINFO flag is set in sa_flags then sa_sigaction is used.
2283 * Otherwise, sa_handler is used. */
2284 sigemptyset(&act.sa_mask);
e2641e09 2285 act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND;
2286 act.sa_handler = sigtermHandler;
633a9410 2287 sigaction(SIGTERM, &act, NULL);
e2641e09 2288
633a9410
PN
2289#ifdef HAVE_BACKTRACE
2290 sigemptyset(&act.sa_mask);
2291 act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND | SA_SIGINFO;
2292 act.sa_sigaction = sigsegvHandler;
2293 sigaction(SIGSEGV, &act, NULL);
2294 sigaction(SIGBUS, &act, NULL);
2295 sigaction(SIGFPE, &act, NULL);
2296 sigaction(SIGILL, &act, NULL);
2297#endif
2298 return;
e2641e09 2299}
e2641e09 2300
78d6a02b 2301void memtest(size_t megabytes, int passes);
2302
39ca1713 2303int main(int argc, char **argv) {
2304 long long start;
a48c8d87 2305 struct timeval tv;
39ca1713 2306
06c5523a 2307 /* We need to initialize our libraries, and the server configuration. */
39ca1713 2308 zmalloc_enable_thread_safeness();
a48c8d87 2309 srand(time(NULL)^getpid());
2310 gettimeofday(&tv,NULL);
2311 dictSetHashFunctionSeed(tv.tv_sec^tv.tv_usec^getpid());
39ca1713 2312 initServerConfig();
a48c8d87 2313
67c6f0f6 2314 if (argc >= 2) {
2315 int j = 1; /* First option to parse in argv[] */
2316 sds options = sdsempty();
2317 char *configfile = NULL;
2318
2319 /* Handle special options --help and --version */
39ca1713 2320 if (strcmp(argv[1], "-v") == 0 ||
2321 strcmp(argv[1], "--version") == 0) version();
4c8bd905 2322 if (strcmp(argv[1], "--help") == 0 ||
2323 strcmp(argv[1], "-h") == 0) usage();
78d6a02b 2324 if (strcmp(argv[1], "--test-memory") == 0) {
2325 if (argc == 3) {
ea693f02 2326 memtest(atoi(argv[2]),50);
78d6a02b 2327 exit(0);
2328 } else {
2329 fprintf(stderr,"Please specify the amount of memory to test in megabytes.\n");
2330 fprintf(stderr,"Example: ./redis-server --test-memory 4096\n\n");
2331 exit(1);
2332 }
2333 }
2334
67c6f0f6 2335 /* First argument is the config file name? */
2336 if (argv[j][0] != '-' || argv[j][1] != '-')
2337 configfile = argv[j++];
2338 /* All the other options are parsed and conceptually appended to the
2339 * configuration file. For instance --port 6380 will generate the
2340 * string "port 6380\n" to be parsed after the actual file name
2341 * is parsed, if any. */
2342 while(j != argc) {
2343 if (argv[j][0] == '-' && argv[j][1] == '-') {
2344 /* Option name */
2345 if (sdslen(options)) options = sdscat(options,"\n");
2346 options = sdscat(options,argv[j]+2);
2347 options = sdscat(options," ");
2348 } else {
2349 /* Option argument */
2350 options = sdscatrepr(options,argv[j],strlen(argv[j]));
2351 options = sdscat(options," ");
2352 }
2353 j++;
2354 }
39ca1713 2355 resetServerSaveParams();
67c6f0f6 2356 loadServerConfig(configfile,options);
2357 sdsfree(options);
39ca1713 2358 } else {
2359 redisLog(REDIS_WARNING,"Warning: no config file specified, using the default config. In order to specify a config file use 'redis-server /path/to/redis.conf'");
2360 }
2361 if (server.daemonize) daemonize();
2362 initServer();
2363 if (server.daemonize) createPidFile();
2364 redisAsciiArt();
27ccb94a 2365 redisLog(REDIS_WARNING,"Server started, Redis version " REDIS_VERSION);
39ca1713 2366#ifdef __linux__
2367 linuxOvercommitMemoryWarning();
2368#endif
2369 start = ustime();
e394114d 2370 if (server.aof_state == REDIS_AOF_ON) {
2c915bcf 2371 if (loadAppendOnlyFile(server.aof_filename) == REDIS_OK)
39ca1713 2372 redisLog(REDIS_NOTICE,"DB loaded from append only file: %.3f seconds",(float)(ustime()-start)/1000000);
2373 } else {
f48cd4b9 2374 if (rdbLoad(server.rdb_filename) == REDIS_OK) {
39ca1713 2375 redisLog(REDIS_NOTICE,"DB loaded from disk: %.3f seconds",
2376 (float)(ustime()-start)/1000000);
2377 } else if (errno != ENOENT) {
2378 redisLog(REDIS_WARNING,"Fatal error loading the DB. Exiting.");
2379 exit(1);
2380 }
2381 }
2382 if (server.ipfd > 0)
2383 redisLog(REDIS_NOTICE,"The server is now ready to accept connections on port %d", server.port);
2384 if (server.sofd > 0)
2385 redisLog(REDIS_NOTICE,"The server is now ready to accept connections at %s", server.unixsocket);
2386 aeSetBeforeSleepProc(server.el,beforeSleep);
2387 aeMain(server.el);
2388 aeDeleteEventLoop(server.el);
2389 return 0;
2390}
2391
e2641e09 2392/* The End */