X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/e65fdc78380761bc9b9531f92a8b78c42263286c..123d7c93cb73a0d09f2a8b34322f1a0f4acc71ba:/redis.c diff --git a/redis.c b/redis.c index 84e4be95..607c827a 100644 --- a/redis.c +++ b/redis.c @@ -27,9 +27,10 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#define REDIS_VERSION "0.101" +#define REDIS_VERSION "0.900" #include "fmacros.h" +#include "config.h" #include #include @@ -38,8 +39,12 @@ #include #define __USE_POSIX199309 #include + +#ifdef HAVE_BACKTRACE #include #include +#endif /* HAVE_BACKTRACE */ + #include #include #include @@ -52,7 +57,6 @@ #include #include #include -#include #include "redis.h" #include "ae.h" /* Event driven programming library */ @@ -64,8 +68,6 @@ #include "lzf.h" /* LZF compression library */ #include "pqsort.h" /* Partial qsort for SORT+LIMIT */ -#include "config.h" - /* Error codes */ #define REDIS_OK 0 #define REDIS_ERR -1 @@ -82,10 +84,10 @@ #define REDIS_MAX_SYNC_TIME 60 /* Slave can't take more to sync */ #define REDIS_EXPIRELOOKUPS_PER_CRON 100 /* try to expire 100 keys/second */ #define REDIS_MAX_WRITE_PER_EVENT (1024*64) +#define REDIS_REQUEST_MAX_SIZE (1024*1024*256) /* max bytes in inline command */ /* Hash table parameters */ #define REDIS_HT_MINFILL 10 /* Minimal hash table fill 10% */ -#define REDIS_HT_MINSLOTS 16384 /* Never resize the HT under this */ /* Command flags */ #define REDIS_CMD_BULK 1 /* Bulk write command */ @@ -327,11 +329,11 @@ static int deleteIfVolatile(redisDb *db, robj *key); static int deleteKey(redisDb *db, robj *key); static time_t getExpire(redisDb *db, robj *key); static int setExpire(redisDb *db, robj *key, time_t when); -static void updateSalvesWaitingBgsave(int bgsaveerr); +static void updateSlavesWaitingBgsave(int bgsaveerr); static void freeMemoryIfNeeded(void); static int processCommand(redisClient *c); -static void segvHandler(int sig, siginfo_t *info, void *secret); static void setupSigSegvAction(void); +static void rdbRemoveTempFile(pid_t childpid); static void authCommand(redisClient *c); static void pingCommand(redisClient *c); @@ -371,6 +373,7 @@ static void sremCommand(redisClient *c); static void smoveCommand(redisClient *c); static void sismemberCommand(redisClient *c); static void scardCommand(redisClient *c); +static void spopCommand(redisClient *c); static void sinterCommand(redisClient *c); static void sinterstoreCommand(redisClient *c); static void sunionCommand(redisClient *c); @@ -418,6 +421,7 @@ static struct redisCommand cmdTable[] = { {"smove",smoveCommand,4,REDIS_CMD_BULK}, {"sismember",sismemberCommand,3,REDIS_CMD_BULK}, {"scard",scardCommand,2,REDIS_CMD_INLINE}, + {"spop",spopCommand,2,REDIS_CMD_INLINE}, {"sinter",sinterCommand,-2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM}, {"sinterstore",sinterstoreCommand,-3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM}, {"sunion",sunionCommand,-2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM}, @@ -692,22 +696,28 @@ static void closeTimedoutClients(void) { } } +static int htNeedsResize(dict *dict) { + long long size, used; + + size = dictSlots(dict); + used = dictSize(dict); + return (size && used && size > DICT_HT_INITIAL_SIZE && + (used*100/size < REDIS_HT_MINFILL)); +} + /* If the percentage of used slots in the HT reaches REDIS_HT_MINFILL * we resize the hash table to save memory */ static void tryResizeHashTables(void) { int j; for (j = 0; j < server.dbnum; j++) { - long long size, used; - - size = dictSlots(server.db[j].dict); - used = dictSize(server.db[j].dict); - if (size && used && size > REDIS_HT_MINSLOTS && - (used*100/size < REDIS_HT_MINFILL)) { - redisLog(REDIS_NOTICE,"The hash table %d is too sparse, resize it...",j); + if (htNeedsResize(server.db[j].dict)) { + redisLog(REDIS_DEBUG,"The hash table %d is too sparse, resize it...",j); dictResize(server.db[j].dict); - redisLog(REDIS_NOTICE,"Hash table %d resized.",j); + redisLog(REDIS_DEBUG,"Hash table %d resized.",j); } + if (htNeedsResize(server.db[j].expires)) + dictResize(server.db[j].expires); } } @@ -727,8 +737,8 @@ static int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientD size = dictSlots(server.db[j].dict); used = dictSize(server.db[j].dict); vkeys = dictSize(server.db[j].expires); - if (!(loops % 5) && used > 0) { - redisLog(REDIS_DEBUG,"DB %d: %d keys (%d volatile) in %d slots HT.",j,used,vkeys,size); + if (!(loops % 5) && (used || vkeys)) { + redisLog(REDIS_DEBUG,"DB %d: %lld keys (%lld volatile) in %lld slots HT.",j,used,vkeys,size); /* dictPrintStats(server.dict); */ } } @@ -743,7 +753,7 @@ static int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientD /* Show information about connected clients */ if (!(loops % 5)) { - redisLog(REDIS_DEBUG,"%d clients connected (%d slaves), %zu bytes in use", + redisLog(REDIS_DEBUG,"%d clients connected (%d slaves), %zu bytes in use, %d shared objects", listLength(server.clients)-listLength(server.slaves), listLength(server.slaves), server.usedmemory, @@ -757,7 +767,6 @@ static int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientD /* Check if a background saving in progress terminated */ if (server.bgsaveinprogress) { int statloc; - /* XXX: TODO handle the case of the saving child killed */ if (wait4(-1,&statloc,WNOHANG,NULL)) { int exitcode = WEXITSTATUS(statloc); int bysignal = WIFSIGNALED(statloc); @@ -772,10 +781,11 @@ static int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientD } else { redisLog(REDIS_WARNING, "Background saving terminated by signal"); + rdbRemoveTempFile(server.bgsavechildpid); } server.bgsaveinprogress = 0; server.bgsavechildpid = -1; - updateSalvesWaitingBgsave(exitcode == 0 ? REDIS_OK : REDIS_ERR); + updateSlavesWaitingBgsave(exitcode == 0 ? REDIS_OK : REDIS_ERR); } } else { /* If there is not a background saving in progress check if @@ -892,6 +902,7 @@ static void initServerConfig() { server.dbfilename = "dump.rdb"; server.requirepass = NULL; server.shareobjects = 0; + server.sharingpoolsize = 1024; server.maxclients = 0; server.maxmemory = 0; ResetServerSaveParams(); @@ -922,7 +933,6 @@ static void initServer() { server.el = aeCreateEventLoop(); server.db = zmalloc(sizeof(redisDb)*server.dbnum); server.sharingpool = dictCreate(&setDictType,NULL); - server.sharingpoolsize = 1024; if (!server.db || !server.clients || !server.slaves || !server.monitors || !server.el || !server.objfreelist) oom("server initialization"); /* Fatal OOM */ server.fd = anetTcpServer(server.neterr, server.port, server.bindaddr); @@ -969,15 +979,20 @@ static int yesnotoi(char *s) { /* I agree, this is a very rudimental way to load a configuration... will improve later if the config gets more complex */ static void loadServerConfig(char *filename) { - FILE *fp = fopen(filename,"r"); + FILE *fp; char buf[REDIS_CONFIGLINE_MAX+1], *err = NULL; int linenum = 0; sds line = NULL; - - if (!fp) { - redisLog(REDIS_WARNING,"Fatal error, can't open config file"); - exit(1); + + if (filename[0] == '-' && filename[1] == '\0') + fp = stdin; + else { + if ((fp = fopen(filename,"r")) == NULL) { + redisLog(REDIS_WARNING,"Fatal error, can't open config file"); + exit(1); + } } + while(fgets(buf,REDIS_CONFIGLINE_MAX+1,fp) != NULL) { sds *argv; int argc, j; @@ -1031,7 +1046,7 @@ static void loadServerConfig(char *filename) { goto loaderr; } } else if (!strcasecmp(argv[0],"logfile") && argc == 2) { - FILE *fp; + FILE *logfp; server.logfile = zstrdup(argv[1]); if (!strcasecmp(server.logfile,"stdout")) { @@ -1041,13 +1056,13 @@ static void loadServerConfig(char *filename) { if (server.logfile) { /* Test if we are able to open the file. The server will not * be able to abort just for this problem later... */ - fp = fopen(server.logfile,"a"); - if (fp == NULL) { + logfp = fopen(server.logfile,"a"); + if (logfp == NULL) { err = sdscatprintf(sdsempty(), "Can't open the log file: %s", strerror(errno)); goto loaderr; } - fclose(fp); + fclose(logfp); } } else if (!strcasecmp(argv[0],"databases") && argc == 2) { server.dbnum = atoi(argv[1]); @@ -1093,7 +1108,7 @@ static void loadServerConfig(char *filename) { zfree(argv); sdsfree(line); } - fclose(fp); + if (fp != stdin) fclose(fp); return; loaderr: @@ -1431,6 +1446,7 @@ again: /* Read the first line of the query */ char *p = strchr(c->querybuf,'\n'); size_t querylen; + if (p) { sds query, *argv; int argc, j; @@ -1472,9 +1488,9 @@ again: /* Execute the command. If the client is still valid * after processCommand() return and there is something * on the query buffer try to process the next command. */ - if (processCommand(c) && sdslen(c->querybuf)) goto again; + if (c->argc && processCommand(c) && sdslen(c->querybuf)) goto again; return; - } else if (sdslen(c->querybuf) >= 1024*32) { + } else if (sdslen(c->querybuf) >= REDIS_REQUEST_MAX_SIZE) { redisLog(REDIS_DEBUG, "Client protocol error"); freeClient(c); return; @@ -1888,7 +1904,7 @@ static int rdbSave(char *filename) { int j; time_t now = time(NULL); - snprintf(tmpfile,256,"temp-%d.%ld.rdb",(int)time(NULL),(long int)random()); + snprintf(tmpfile,256,"temp-%d.rdb", (int) getpid()); fp = fopen(tmpfile,"w"); if (!fp) { redisLog(REDIS_WARNING, "Failed saving the DB: %s", strerror(errno)); @@ -2015,6 +2031,13 @@ static int rdbSaveBackground(char *filename) { return REDIS_OK; /* unreached */ } +static void rdbRemoveTempFile(pid_t childpid) { + char tmpfile[256]; + + snprintf(tmpfile,256,"temp-%d.rdb", (int) childpid); + unlink(tmpfile); +} + static int rdbLoadType(FILE *fp) { unsigned char type; if (fread(&type,1,1,fp) == 0) return -1; @@ -2532,11 +2555,15 @@ static void bgsaveCommand(redisClient *c) { static void shutdownCommand(redisClient *c) { redisLog(REDIS_WARNING,"User requested shutdown, saving DB..."); + /* Kill the saving child if there is a background saving in progress. + We want to avoid race conditions, for instance our saving child may + overwrite the synchronous saving did by SHUTDOWN. */ if (server.bgsaveinprogress) { redisLog(REDIS_WARNING,"There is a live saving child. Killing it!"); - signal(SIGCHLD, SIG_IGN); kill(server.bgsavechildpid,SIGKILL); + rdbRemoveTempFile(server.bgsavechildpid); } + /* SYNC SAVE */ if (rdbSave(server.dbfilename) == REDIS_OK) { if (server.daemonize) unlink(server.pidfile); @@ -2544,7 +2571,10 @@ static void shutdownCommand(redisClient *c) { redisLog(REDIS_WARNING,"Server exit now, bye bye..."); exit(1); } else { - signal(SIGCHLD, SIG_DFL); + /* Ooops.. error saving! The best we can do is to continue operating. + * Note that if there was a background saving process, in the next + * cron() Redis will be notified that the background saving aborted, + * handling special stuff like slaves pending for synchronization... */ redisLog(REDIS_WARNING,"Error trying to save the DB, can't exit"); addReplySds(c,sdsnew("-ERR can't quit, problems saving the DB\r\n")); } @@ -2880,8 +2910,8 @@ static void ltrimCommand(redisClient *c) { ln = listLast(list); listDelNode(list,ln); } - addReply(c,shared.ok); server.dirty++; + addReply(c,shared.ok); } } } @@ -2962,6 +2992,7 @@ static void sremCommand(redisClient *c) { } if (dictDelete(set->ptr,c->argv[2]) == DICT_OK) { server.dirty++; + if (htNeedsResize(set->ptr)) dictResize(set->ptr); addReply(c,shared.cone); } else { addReply(c,shared.czero); @@ -3041,6 +3072,34 @@ static void scardCommand(redisClient *c) { } } +static void spopCommand(redisClient *c) { + robj *set; + dictEntry *de; + + set = lookupKeyWrite(c->db,c->argv[1]); + if (set == NULL) { + addReply(c,shared.nullbulk); + } else { + if (set->type != REDIS_SET) { + addReply(c,shared.wrongtypeerr); + return; + } + de = dictGetRandomKey(set->ptr); + if (de == NULL) { + addReply(c,shared.nullbulk); + } else { + robj *ele = dictGetEntryKey(de); + + addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",sdslen(ele->ptr))); + addReply(c,ele); + addReply(c,shared.crlf); + dictDelete(set->ptr,ele); + if (htNeedsResize(set->ptr)) dictResize(set->ptr); + server.dirty++; + } + } +} + static int qsortCompareSetsByCardinality(const void *s1, const void *s2) { dict **d1 = (void*) s1, **d2 = (void*) s2; @@ -3561,6 +3620,7 @@ static void sortCommand(redisClient *c) { static void infoCommand(redisClient *c) { sds info; time_t uptime = time(NULL)-server.stat_starttime; + int j; info = sdscatprintf(sdsempty(), "redis_version:%s\r\n" @@ -3601,6 +3661,16 @@ static void infoCommand(redisClient *c) { (int)(time(NULL)-server.master->lastinteraction) ); } + for (j = 0; j < server.dbnum; j++) { + long long keys, vkeys; + + keys = dictSize(server.db[j].dict); + vkeys = dictSize(server.db[j].expires); + if (keys || vkeys) { + info = sdscatprintf(info, "db%d: keys=%lld,expires=%lld\r\n", + j, keys, vkeys); + } + } addReplySds(c,sdscatprintf(sdsempty(),"$%d\r\n",sdslen(info))); addReplySds(c,info); addReply(c,shared.crlf); @@ -3690,10 +3760,12 @@ static void expireCommand(redisClient *c) { return; } else { time_t when = time(NULL)+seconds; - if (setExpire(c->db,c->argv[1],when)) + if (setExpire(c->db,c->argv[1],when)) { addReply(c,shared.cone); - else + server.dirty++; + } else { addReply(c,shared.czero); + } return; } } @@ -3887,7 +3959,13 @@ static void sendBulkToSlave(aeEventLoop *el, int fd, void *privdata, int mask) { } } -static void updateSalvesWaitingBgsave(int bgsaveerr) { +/* This function is called at the end of every backgrond saving. + * The argument bgsaveerr is REDIS_OK if the background saving succeeded + * otherwise REDIS_ERR is passed to the function. + * + * The goal of this function is to handle slaves waiting for a successful + * background saving in order to perform non-blocking synchronization. */ +static void updateSlavesWaitingBgsave(int bgsaveerr) { listNode *ln; int startbgsave = 0; @@ -4108,7 +4186,7 @@ static void debugCommand(redisClient *c) { } } -#if defined(__APPLE__) || defined(__linux__) +#ifdef HAVE_BACKTRACE static struct redisFunctionSym symsTable[] = { {"freeStringObject", (unsigned long)freeStringObject}, {"freeListObject", (unsigned long)freeListObject}, @@ -4131,7 +4209,7 @@ static struct redisFunctionSym symsTable[] = { {"deleteKey", (unsigned long)deleteKey}, {"getExpire", (unsigned long)getExpire}, {"setExpire", (unsigned long)setExpire}, -{"updateSalvesWaitingBgsave", (unsigned long)updateSalvesWaitingBgsave}, +{"updateSlavesWaitingBgsave", (unsigned long)updateSlavesWaitingBgsave}, {"freeMemoryIfNeeded", (unsigned long)freeMemoryIfNeeded}, {"authCommand", (unsigned long)authCommand}, {"pingCommand", (unsigned long)pingCommand}, @@ -4171,6 +4249,7 @@ static struct redisFunctionSym symsTable[] = { {"smoveCommand", (unsigned long)smoveCommand}, {"sismemberCommand", (unsigned long)sismemberCommand}, {"scardCommand", (unsigned long)scardCommand}, +{"spopCommand", (unsigned long)spopCommand}, {"sinterCommand", (unsigned long)sinterCommand}, {"sinterstoreCommand", (unsigned long)sinterstoreCommand}, {"sunionCommand", (unsigned long)sunionCommand}, @@ -4192,8 +4271,8 @@ static struct redisFunctionSym symsTable[] = { {"debugCommand", (unsigned long)debugCommand}, {"processCommand", (unsigned long)processCommand}, {"setupSigSegvAction", (unsigned long)setupSigSegvAction}, -{"segvHandler", (unsigned long)segvHandler}, {"readQueryFromClient", (unsigned long)readQueryFromClient}, +{"rdbRemoveTempFile", (unsigned long)rdbRemoveTempFile}, {NULL,0} }; @@ -4226,10 +4305,20 @@ static void *getMcontextEip(ucontext_t *uc) { return (void*) uc->uc_mcontext.mc_eip; #elif defined(__dietlibc__) return (void*) uc->uc_mcontext.eip; -#elif defined(__APPLE__) +#elif defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_6) return (void*) uc->uc_mcontext->__ss.__eip; -#else /* Linux */ +#elif defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_6) + #ifdef _STRUCT_X86_THREAD_STATE64 + return (void*) uc->uc_mcontext->__ss.__rip; + #else + return (void*) uc->uc_mcontext->__ss.__eip; + #endif +#elif defined(__i386__) || defined(__X86_64__) /* Linux x86 */ return (void*) uc->uc_mcontext.gregs[REG_EIP]; +#elif defined(__ia64__) /* Linux IA64 */ + return (void*) uc->uc_mcontext.sc_ip; +#else + return NULL; #endif } @@ -4271,10 +4360,12 @@ static void segvHandler(int sig, siginfo_t *info, void *secret) { trace_size = backtrace(trace, 100); /* overwrite sigaction with caller's address */ - trace[1] = getMcontextEip(uc); + if (getMcontextEip(uc) != NULL) { + trace[1] = getMcontextEip(uc); + } messages = backtrace_symbols(trace, trace_size); - for (i=0; i /proc/sys/vm/overcommit_memory' in your init scripts."); + redisLog(REDIS_WARNING,"WARNING overcommit_memory is set to 0! Background save may fail under low condition memory. 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."); } } #endif /* __linux__ */ @@ -4355,10 +4448,6 @@ static void daemonize(void) { } int main(int argc, char **argv) { -#ifdef __linux__ - linuxOvercommitMemoryWarning(); -#endif - initServerConfig(); if (argc == 2) { ResetServerSaveParams(); @@ -4372,6 +4461,9 @@ int main(int argc, char **argv) { initServer(); if (server.daemonize) daemonize(); redisLog(REDIS_NOTICE,"Server started, Redis version " REDIS_VERSION); +#ifdef __linux__ + linuxOvercommitMemoryWarning(); +#endif if (rdbLoad(server.dbfilename) == REDIS_OK) redisLog(REDIS_NOTICE,"DB loaded from disk"); if (aeCreateFileEvent(server.el, server.fd, AE_READABLE,