From a30047736806e54af0459a2c41767bf0ebc6a572 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Thu, 9 Dec 2010 10:37:35 +0100 Subject: [PATCH 001/146] Undo rename of function names where something went wrong --- src/t_zset.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/t_zset.c b/src/t_zset.c index 27522367..9539357c 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -263,7 +263,7 @@ zskiplistNode *zslFirstWithScore(zskiplist *zsl, double score) { * Returns 0 when the element cannot be found, rank otherwise. * Note that the rank is 1-based due to the span of zsl->header to the * first element. */ -unsigned long zslistTypeGetRank(zskiplist *zsl, double score, robj *o) { +unsigned long zslGetRank(zskiplist *zsl, double score, robj *o) { zskiplistNode *x; unsigned long rank = 0; int i; @@ -287,7 +287,7 @@ unsigned long zslistTypeGetRank(zskiplist *zsl, double score, robj *o) { } /* Finds an element by its rank. The rank argument needs to be 1-based. */ -zskiplistNode* zslistTypeGetElementByRank(zskiplist *zsl, unsigned long rank) { +zskiplistNode* zslGetElementByRank(zskiplist *zsl, unsigned long rank) { zskiplistNode *x; unsigned long traversed = 0; int i; @@ -810,10 +810,10 @@ void zrangeGenericCommand(redisClient *c, int reverse) { /* check if starting point is trivial, before searching * the element in log(N) time */ if (reverse) { - ln = start == 0 ? zsl->tail : zslistTypeGetElementByRank(zsl, llen-start); + ln = start == 0 ? zsl->tail : zslGetElementByRank(zsl, llen-start); } else { ln = start == 0 ? - zsl->header->level[0].forward : zslistTypeGetElementByRank(zsl, start+1); + zsl->header->level[0].forward : zslGetElementByRank(zsl, start+1); } /* Return the result in form of a multi-bulk reply */ @@ -1039,7 +1039,7 @@ void zrankGenericCommand(redisClient *c, int reverse) { } score = dictGetEntryVal(de); - rank = zslistTypeGetRank(zsl, *score, c->argv[2]); + rank = zslGetRank(zsl, *score, c->argv[2]); if (rank) { if (reverse) { addReplyLongLong(c, zsl->length - rank); From 22b9bf15949933b351525ae658dc32e40e5784ab Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Tue, 7 Dec 2010 23:21:05 +0100 Subject: [PATCH 002/146] Move logic concerned with zset ranges This also optimizes ZREVRANGEBYSCORE for pathological cases where a sorted set contains many elements with the same score. Previously, it would traverse the list from back to front in such a case. --- src/t_zset.c | 163 ++++++++++++++++++++++----------------- tests/unit/type/zset.tcl | 16 ++++ 2 files changed, 110 insertions(+), 69 deletions(-) diff --git a/src/t_zset.c b/src/t_zset.c index 9539357c..01b1d035 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -180,6 +180,78 @@ typedef struct { int minex, maxex; /* are min or max exclusive? */ } zrangespec; +static inline int zslValueInMinRange(double value, zrangespec *spec) { + return spec->minex ? (value > spec->min) : (value >= spec->min); +} + +static inline int zslValueInMaxRange(double value, zrangespec *spec) { + return spec->maxex ? (value < spec->max) : (value <= spec->max); +} + +static inline int zslValueInRange(double value, zrangespec *spec) { + return zslValueInMinRange(value,spec) && zslValueInMaxRange(value,spec); +} + +/* Returns if there is a part of the zset is in range. */ +int zslIsInRange(zskiplist *zsl, zrangespec *range) { + zskiplistNode *x; + + x = zsl->tail; + if (x == NULL || !zslValueInMinRange(x->score,range)) + return 0; + x = zsl->header->level[0].forward; + if (x == NULL || !zslValueInMaxRange(x->score,range)) + return 0; + return 1; +} + +/* Find the first node that is contained in the specified range. + * Returns NULL when no element is contained in the range. */ +zskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec range) { + zskiplistNode *x; + int i; + + /* If everything is out of range, return early. */ + if (!zslIsInRange(zsl,&range)) return NULL; + + x = zsl->header; + for (i = zsl->level-1; i >= 0; i--) { + /* Go forward while *OUT* of range. */ + while (x->level[i].forward && + !zslValueInMinRange(x->level[i].forward->score,&range)) + x = x->level[i].forward; + } + + /* The tail is in range, so the previous block should always return a + * node that is non-NULL and the last one to be out of range. */ + x = x->level[0].forward; + redisAssert(x != NULL && zslValueInRange(x->score,&range)); + return x; +} + +/* Find the last node that is contained in the specified range. + * Returns NULL when no element is contained in the range. */ +zskiplistNode *zslLastInRange(zskiplist *zsl, zrangespec range) { + zskiplistNode *x; + int i; + + /* If everything is out of range, return early. */ + if (!zslIsInRange(zsl,&range)) return NULL; + + x = zsl->header; + for (i = zsl->level-1; i >= 0; i--) { + /* Go forward while *IN* range. */ + while (x->level[i].forward && + zslValueInMaxRange(x->level[i].forward->score,&range)) + x = x->level[i].forward; + } + + /* The header is in range, so the previous block should always return a + * node that is non-NULL and in range. */ + redisAssert(x != NULL && zslValueInRange(x->score,&range)); + return x; +} + /* Delete all the elements with score between min and max from the skiplist. * Min and mx are inclusive, so a score >= min || score <= max is deleted. * Note that this function takes the reference to the hash table view of the @@ -243,22 +315,6 @@ unsigned long zslDeleteRangeByRank(zskiplist *zsl, unsigned int start, unsigned return removed; } -/* Find the first node having a score equal or greater than the specified one. - * Returns NULL if there is no match. */ -zskiplistNode *zslFirstWithScore(zskiplist *zsl, double score) { - zskiplistNode *x; - int i; - - x = zsl->header; - for (i = zsl->level-1; i >= 0; i--) { - while (x->level[i].forward && x->level[i].forward->score < score) - x = x->level[i].forward; - } - /* We may have multiple elements with the same score, what we need - * is to find the element with both the right score and object. */ - return x->level[0].forward; -} - /* Find the rank for an element by both score and key. * Returns 0 when the element cannot be found, rank otherwise. * Note that the rank is 1-based due to the span of zsl->header to the @@ -847,9 +903,18 @@ void genericZrangebyscoreCommand(redisClient *c, int reverse, int justcount) { int withscores = 0; unsigned long rangelen = 0; void *replylen = NULL; + int minidx, maxidx; /* Parse the range arguments. */ - if (zslParseRange(c->argv[2],c->argv[3],&range) != REDIS_OK) { + if (reverse) { + /* Range is given as [max,min] */ + maxidx = 2; minidx = 3; + } else { + /* Range is given as [min,max] */ + minidx = 2; maxidx = 3; + } + + if (zslParseRange(c->argv[minidx],c->argv[maxidx],&range) != REDIS_OK) { addReplyError(c,"min or max is not a double"); return; } @@ -882,33 +947,11 @@ void genericZrangebyscoreCommand(redisClient *c, int reverse, int justcount) { zsetobj = o->ptr; zsl = zsetobj->zsl; - /* If reversed, assume the elements are sorted from high to low score. */ - ln = zslFirstWithScore(zsl,range.min); + /* If reversed, get the last node in range as starting point. */ if (reverse) { - /* If range.min is out of range, ln will be NULL and we need to use - * the tail of the skiplist as first node of the range. */ - if (ln == NULL) ln = zsl->tail; - - /* zslFirstWithScore returns the first element with where with - * score >= range.min, so backtrack to make sure the element we use - * here has score <= range.min. */ - while (ln && ln->score > range.min) ln = ln->backward; - - /* Move to the right element according to the range spec. */ - if (range.minex) { - /* Find last element with score < range.min */ - while (ln && ln->score == range.min) ln = ln->backward; - } else { - /* Find last element with score <= range.min */ - while (ln && ln->level[0].forward && - ln->level[0].forward->score == range.min) - ln = ln->level[0].forward; - } + ln = zslLastInRange(zsl,range); } else { - if (range.minex) { - /* Find first element with score > range.min */ - while (ln && ln->score == range.min) ln = ln->level[0].forward; - } + ln = zslFirstInRange(zsl,range); } /* No "first" element in the specified interval. */ @@ -917,40 +960,24 @@ void genericZrangebyscoreCommand(redisClient *c, int reverse, int justcount) { return; } - /* We don't know in advance how many matching elements there - * are in the list, so we push this object that will represent - * the multi-bulk length in the output buffer, and will "fix" - * it later */ + /* We don't know in advance how many matching elements there are in the + * list, so we push this object that will represent the multi-bulk length + * in the output buffer, and will "fix" it later */ if (!justcount) replylen = addDeferredMultiBulkLength(c); /* If there is an offset, just traverse the number of elements without * checking the score because that is done in the next loop. */ while(ln && offset--) { - if (reverse) - ln = ln->backward; - else - ln = ln->level[0].forward; + ln = reverse ? ln->backward : ln->level[0].forward; } while (ln && limit--) { - /* Check if this this element is in range. */ + /* Abort when the node is no longer in range. */ if (reverse) { - if (range.maxex) { - /* Element should have score > range.max */ - if (ln->score <= range.max) break; - } else { - /* Element should have score >= range.max */ - if (ln->score < range.max) break; - } + if (!zslValueInMinRange(ln->score,&range)) break; } else { - if (range.maxex) { - /* Element should have score < range.max */ - if (ln->score >= range.max) break; - } else { - /* Element should have score <= range.max */ - if (ln->score > range.max) break; - } + if (!zslValueInMaxRange(ln->score,&range)) break; } /* Do our magic */ @@ -961,10 +988,8 @@ void genericZrangebyscoreCommand(redisClient *c, int reverse, int justcount) { addReplyDouble(c,ln->score); } - if (reverse) - ln = ln->backward; - else - ln = ln->level[0].forward; + /* Move to next node */ + ln = reverse ? ln->backward : ln->level[0].forward; } if (justcount) { diff --git a/tests/unit/type/zset.tcl b/tests/unit/type/zset.tcl index 6b8fc54a..a54ff37b 100644 --- a/tests/unit/type/zset.tcl +++ b/tests/unit/type/zset.tcl @@ -227,6 +227,22 @@ start_server {tags {"zset"}} { assert_equal {f e} [r zrevrangebyscore zset (6 (3] assert_equal {f} [r zrevrangebyscore zset (+inf (4] assert_equal 2 [r zcount zset (0 (3] + + # test empty ranges + r zrem zset a + r zrem zset g + + # inclusive + assert_equal {} [r zrangebyscore zset 6 +inf] + assert_equal {} [r zrangebyscore zset -inf -6] + assert_equal {} [r zrevrangebyscore zset +inf 6] + assert_equal {} [r zrevrangebyscore zset -6 -inf] + + # exclusive + assert_equal {} [r zrangebyscore zset (6 (+inf] + assert_equal {} [r zrangebyscore zset (-inf (-6] + assert_equal {} [r zrevrangebyscore zset (+inf (6] + assert_equal {} [r zrevrangebyscore zset (-6 (-inf] } test "ZRANGEBYSCORE with WITHSCORES" { From df278b8b0b4bd54b83840d4a151e538c60dc92e9 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Thu, 13 Jan 2011 16:06:03 +0100 Subject: [PATCH 003/146] Compiler should decide on inlining --- src/t_zset.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/t_zset.c b/src/t_zset.c index 01b1d035..0e7d726b 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -180,15 +180,15 @@ typedef struct { int minex, maxex; /* are min or max exclusive? */ } zrangespec; -static inline int zslValueInMinRange(double value, zrangespec *spec) { +static int zslValueInMinRange(double value, zrangespec *spec) { return spec->minex ? (value > spec->min) : (value >= spec->min); } -static inline int zslValueInMaxRange(double value, zrangespec *spec) { +static int zslValueInMaxRange(double value, zrangespec *spec) { return spec->maxex ? (value < spec->max) : (value <= spec->max); } -static inline int zslValueInRange(double value, zrangespec *spec) { +static int zslValueInRange(double value, zrangespec *spec) { return zslValueInMinRange(value,spec) && zslValueInMaxRange(value,spec); } From 8e1b327706ca39f9efb8967653b365ca81a52425 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Mon, 17 Jan 2011 11:10:30 +0100 Subject: [PATCH 004/146] Test for ranges where min > max --- src/t_zset.c | 4 ++++ tests/unit/type/zset.tcl | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/t_zset.c b/src/t_zset.c index 0e7d726b..99fe6a8b 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -196,6 +196,10 @@ static int zslValueInRange(double value, zrangespec *spec) { int zslIsInRange(zskiplist *zsl, zrangespec *range) { zskiplistNode *x; + /* Test for ranges that will always be empty. */ + if (range->min > range->max || + (range->min == range->max && (range->minex || range->maxex))) + return 0; x = zsl->tail; if (x == NULL || !zslValueInMinRange(x->score,range)) return 0; diff --git a/tests/unit/type/zset.tcl b/tests/unit/type/zset.tcl index a54ff37b..fdeebd2f 100644 --- a/tests/unit/type/zset.tcl +++ b/tests/unit/type/zset.tcl @@ -233,12 +233,16 @@ start_server {tags {"zset"}} { r zrem zset g # inclusive + assert_equal {} [r zrangebyscore zset 4 2] assert_equal {} [r zrangebyscore zset 6 +inf] assert_equal {} [r zrangebyscore zset -inf -6] assert_equal {} [r zrevrangebyscore zset +inf 6] assert_equal {} [r zrevrangebyscore zset -6 -inf] # exclusive + assert_equal {} [r zrangebyscore zset (4 (2] + assert_equal {} [r zrangebyscore zset 2 (2] + assert_equal {} [r zrangebyscore zset (2 2] assert_equal {} [r zrangebyscore zset (6 (+inf] assert_equal {} [r zrangebyscore zset (-inf (-6] assert_equal {} [r zrevrangebyscore zset (+inf (6] From 45290ad9bb9c535d6447852327ceb458573afff6 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Thu, 10 Feb 2011 11:49:59 +0100 Subject: [PATCH 005/146] Rename zset range functions --- src/t_zset.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/t_zset.c b/src/t_zset.c index 99fe6a8b..563b0134 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -180,16 +180,16 @@ typedef struct { int minex, maxex; /* are min or max exclusive? */ } zrangespec; -static int zslValueInMinRange(double value, zrangespec *spec) { +static int zslValueGteMin(double value, zrangespec *spec) { return spec->minex ? (value > spec->min) : (value >= spec->min); } -static int zslValueInMaxRange(double value, zrangespec *spec) { +static int zslValueLteMax(double value, zrangespec *spec) { return spec->maxex ? (value < spec->max) : (value <= spec->max); } static int zslValueInRange(double value, zrangespec *spec) { - return zslValueInMinRange(value,spec) && zslValueInMaxRange(value,spec); + return zslValueGteMin(value,spec) && zslValueLteMax(value,spec); } /* Returns if there is a part of the zset is in range. */ @@ -201,10 +201,10 @@ int zslIsInRange(zskiplist *zsl, zrangespec *range) { (range->min == range->max && (range->minex || range->maxex))) return 0; x = zsl->tail; - if (x == NULL || !zslValueInMinRange(x->score,range)) + if (x == NULL || !zslValueGteMin(x->score,range)) return 0; x = zsl->header->level[0].forward; - if (x == NULL || !zslValueInMaxRange(x->score,range)) + if (x == NULL || !zslValueLteMax(x->score,range)) return 0; return 1; } @@ -222,7 +222,7 @@ zskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec range) { for (i = zsl->level-1; i >= 0; i--) { /* Go forward while *OUT* of range. */ while (x->level[i].forward && - !zslValueInMinRange(x->level[i].forward->score,&range)) + !zslValueGteMin(x->level[i].forward->score,&range)) x = x->level[i].forward; } @@ -246,7 +246,7 @@ zskiplistNode *zslLastInRange(zskiplist *zsl, zrangespec range) { for (i = zsl->level-1; i >= 0; i--) { /* Go forward while *IN* range. */ while (x->level[i].forward && - zslValueInMaxRange(x->level[i].forward->score,&range)) + zslValueLteMax(x->level[i].forward->score,&range)) x = x->level[i].forward; } @@ -979,9 +979,9 @@ void genericZrangebyscoreCommand(redisClient *c, int reverse, int justcount) { while (ln && limit--) { /* Abort when the node is no longer in range. */ if (reverse) { - if (!zslValueInMinRange(ln->score,&range)) break; + if (!zslValueGteMin(ln->score,&range)) break; } else { - if (!zslValueInMaxRange(ln->score,&range)) break; + if (!zslValueLteMax(ln->score,&range)) break; } /* Do our magic */ From 672b0a1b25023fd6df826af6c7570e23d3ac3443 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Tue, 8 Mar 2011 12:30:01 +0100 Subject: [PATCH 006/146] Fast conversion of double when representable as long long --- src/redis.h | 1 + src/util.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/redis.h b/src/redis.h index cdddb601..19403218 100644 --- a/src/redis.h +++ b/src/redis.h @@ -887,6 +887,7 @@ int stringmatchlen(const char *pattern, int patternLen, int stringmatch(const char *pattern, const char *string, int nocase); long long memtoll(const char *p, int *err); int ll2string(char *s, size_t len, long long value); +int d2string(char *s, size_t len, double value); int isStringRepresentableAsLong(sds s, long *longval); int isStringRepresentableAsLongLong(sds s, long long *longval); int isObjectRepresentableAsLongLong(robj *o, long long *llongval); diff --git a/src/util.c b/src/util.c index e304ff83..5cffa072 100644 --- a/src/util.c +++ b/src/util.c @@ -1,6 +1,7 @@ #include "redis.h" #include #include +#include /* Glob-style pattern matching. */ int stringmatchlen(const char *pattern, int patternLen, @@ -200,6 +201,45 @@ int ll2string(char *s, size_t len, long long value) { return l; } +/* Convert a double to a string representation. Returns the number of bytes + * required. The representation should always be parsable by stdtod(3). */ +int d2string(char *buf, size_t len, double value) { + if (isnan(value)) { + len = snprintf(buf,len,"nan"); + } else if (isinf(value)) { + if (value < 0) + len = snprintf(buf,len,"-inf"); + else + len = snprintf(buf,len,"inf"); + } else if (value == 0) { + /* See: http://en.wikipedia.org/wiki/Signed_zero, "Comparisons". */ + if (1.0/value < 0) + len = snprintf(buf,len,"-0"); + else + len = snprintf(buf,len,"0"); + } else { +#if (DBL_MANT_DIG >= 52) && (LLONG_MAX == 0x7fffffffffffffffLL) + /* Check if the float is in a safe range to be casted into a + * long long. We are assuming that long long is 64 bit here. + * Also we are assuming that there are no implementations around where + * double has precision < 52 bit. + * + * Under this assumptions we test if a double is inside an interval + * where casting to long long is safe. Then using two castings we + * make sure the decimal part is zero. If all this is true we use + * integer printing function that is much faster. */ + double min = -4503599627370495; /* (2^52)-1 */ + double max = 4503599627370496; /* -(2^52) */ + if (val > min && val < max && value == ((double)((long long)value))) + len = ll2string(buf,len,(long long)value); + else +#endif + len = snprintf(buf,len,"%.17g",value); + } + + return len; +} + /* Check if the sds string 's' can be represented by a long long * (that is, is a number that fits into long without any other space or * character before or after the digits, so that converting this number From 9e7cee0ed01246e898eac500330c6a16e9dbfddb Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Tue, 8 Mar 2011 16:08:52 +0100 Subject: [PATCH 007/146] Add function to create ziplist-backed sorted set --- src/object.c | 8 +++++++- src/redis.h | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/object.c b/src/object.c index e6b02da8..a6bf8a20 100644 --- a/src/object.c +++ b/src/object.c @@ -93,12 +93,18 @@ robj *createHashObject(void) { robj *createZsetObject(void) { zset *zs = zmalloc(sizeof(*zs)); - zs->dict = dictCreate(&zsetDictType,NULL); zs->zsl = zslCreate(); return createObject(REDIS_ZSET,zs); } +robj *createZsetZiplistObject(void) { + unsigned char *zl = ziplistNew(); + robj *o = createObject(REDIS_ZSET,zl); + o->encoding = REDIS_ENCODING_ZIPLIST; + return o; +} + void freeStringObject(robj *o) { if (o->encoding == REDIS_ENCODING_RAW) { sdsfree(o->ptr); diff --git a/src/redis.h b/src/redis.h index 19403218..78294d0d 100644 --- a/src/redis.h +++ b/src/redis.h @@ -730,6 +730,7 @@ robj *createSetObject(void); robj *createIntsetObject(void); robj *createHashObject(void); robj *createZsetObject(void); +robj *createZsetZiplistObject(void); int getLongFromObjectOrReply(redisClient *c, robj *o, long *target, const char *msg); int checkType(redisClient *c, robj *o, int type); int getLongLongFromObjectOrReply(redisClient *c, robj *o, long long *target, const char *msg); From 21c5b508a48270257567ea223fb0766553501303 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Tue, 8 Mar 2011 16:44:22 +0100 Subject: [PATCH 008/146] Initial work for ziplist backed sorted sets --- src/t_zset.c | 314 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 244 insertions(+), 70 deletions(-) diff --git a/src/t_zset.c b/src/t_zset.c index 563b0134..e1c61772 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -403,6 +403,157 @@ static int zslParseRange(robj *min, robj *max, zrangespec *spec) { return REDIS_OK; } +/*----------------------------------------------------------------------------- + * Ziplist-backed sorted set API + *----------------------------------------------------------------------------*/ + +double zzlGetScore(unsigned char *sptr) { + unsigned char *vstr; + unsigned int vlen; + long long vlong; + char buf[128]; + double score; + + redisAssert(sptr != NULL); + redisAssert(ziplistGet(sptr,&vstr,&vlen,&vlong)); + + if (vstr) { + memcpy(buf,vstr,vlen); + buf[vlen] = '\0'; + score = strtod(buf,NULL); + } else { + score = vlong; + } + + return score; +} + +/* Compare element in sorted set with given element. */ +int zzlCompareElements(unsigned char *eptr, unsigned char *cstr, unsigned int clen) { + unsigned char *vstr; + unsigned int vlen; + long long vlong; + unsigned char vbuf[32]; + int minlen, cmp; + + redisAssert(ziplistGet(eptr,&vstr,&vlen,&vlong)); + if (vstr == NULL) { + /* Store string representation of long long in buf. */ + vlen = ll2string((char*)vbuf,sizeof(vbuf),vlong); + vstr = vbuf; + } + + minlen = (vlen < clen) ? vlen : clen; + cmp = memcmp(vstr,cstr,minlen); + if (cmp == 0) return vlen-clen; + return cmp; +} + +unsigned char *zzlFind(robj *zobj, robj *ele, double *score) { + unsigned char *zl = zobj->ptr; + unsigned char *eptr = ziplistIndex(zl,0), *sptr; + + ele = getDecodedObject(ele); + while (eptr != NULL) { + sptr = ziplistNext(zl,eptr); + redisAssert(sptr != NULL); + + if (ziplistCompare(eptr,ele->ptr,sdslen(ele->ptr))) { + /* Matching element, pull out score. */ + *score = zzlGetScore(sptr); + decrRefCount(ele); + return eptr; + } + + /* Move to next element. */ + eptr = ziplistNext(zl,sptr); + } + + decrRefCount(ele); + return NULL; +} + +/* Delete (element,score) pair from ziplist. Use local copy of eptr because we + * don't want to modify the one given as argument. */ +int zzlDelete(robj *zobj, unsigned char *eptr) { + unsigned char *zl = zobj->ptr; + unsigned char *p = eptr; + + /* TODO: add function to ziplist API to delete N elements from offset. */ + zl = ziplistDelete(zl,&p); + zl = ziplistDelete(zl,&p); + zobj->ptr = zl; + return REDIS_OK; +} + +int zzlInsertAt(robj *zobj, robj *ele, double score, unsigned char *eptr) { + unsigned char *zl = zobj->ptr; + unsigned char *sptr; + char scorebuf[128]; + int scorelen; + int offset; + + redisAssert(ele->encoding == REDIS_ENCODING_RAW); + scorelen = d2string(scorebuf,sizeof(scorebuf),score); + if (eptr == NULL) { + zl = ziplistPush(zl,ele->ptr,sdslen(ele->ptr),ZIPLIST_TAIL); + zl = ziplistPush(zl,(unsigned char*)scorebuf,scorelen,ZIPLIST_TAIL); + } else { + /* Keep offset relative to zl, as it might be re-allocated. */ + offset = eptr-zl; + zl = ziplistInsert(zl,eptr,ele->ptr,sdslen(ele->ptr)); + eptr = zl+offset; + + /* Insert score after the element. */ + redisAssert((sptr = ziplistNext(zl,eptr)) != NULL); + zl = ziplistInsert(zl,sptr,(unsigned char*)scorebuf,scorelen); + } + + zobj->ptr = zl; + return REDIS_OK; +} + +/* Insert (element,score) pair in ziplist. This function assumes the element is + * not yet present in the list. */ +int zzlInsert(robj *zobj, robj *ele, double score) { + unsigned char *zl = zobj->ptr; + unsigned char *eptr = ziplistIndex(zl,0), *sptr; + double s; + int insert = 0; + + ele = getDecodedObject(ele); + while (eptr != NULL) { + sptr = ziplistNext(zl,eptr); + redisAssert(sptr != NULL); + s = zzlGetScore(sptr); + + if (s > score) { + /* First element with score larger than score for element to be + * inserted. This means we should take its spot in the list to + * maintain ordering. */ + insert = 1; + } else if (s == score) { + /* Ensure lexicographical ordering for elements. */ + if (zzlCompareElements(eptr,ele->ptr,sdslen(ele->ptr)) < 0) + insert = 1; + } + + if (insert) { + zzlInsertAt(zobj,ele,score,eptr); + break; + } + + /* Move to next element. */ + eptr = ziplistNext(zl,sptr); + } + + /* Push on tail of list when it was not yet inserted. */ + if (!insert) + zzlInsertAt(zobj,ele,score,eptr); + + decrRefCount(ele); + return REDIS_OK; +} /*----------------------------------------------------------------------------- * Sorted set commands @@ -410,90 +561,113 @@ static int zslParseRange(robj *min, robj *max, zrangespec *spec) { /* This generic command implements both ZADD and ZINCRBY. */ void zaddGenericCommand(redisClient *c, robj *key, robj *ele, double score, int incr) { - robj *zsetobj; - zset *zs; - zskiplistNode *znode; + static char *nanerr = "resulting score is not a number (NaN)"; + robj *zobj; + robj *curobj; + double curscore = 0.0; - zsetobj = lookupKeyWrite(c->db,key); - if (zsetobj == NULL) { - zsetobj = createZsetObject(); - dbAdd(c->db,key,zsetobj); + zobj = lookupKeyWrite(c->db,key); + if (zobj == NULL) { + zobj = createZsetZiplistObject(); + dbAdd(c->db,key,zobj); } else { - if (zsetobj->type != REDIS_ZSET) { + if (zobj->type != REDIS_ZSET) { addReply(c,shared.wrongtypeerr); return; } } - zs = zsetobj->ptr; - /* Since both ZADD and ZINCRBY are implemented here, we need to increment - * the score first by the current score if ZINCRBY is called. */ - if (incr) { - /* Read the old score. If the element was not present starts from 0 */ - dictEntry *de = dictFind(zs->dict,ele); - if (de != NULL) - score += *(double*)dictGetEntryVal(de); + if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { + unsigned char *eptr; - if (isnan(score)) { - addReplyError(c,"resulting score is not a number (NaN)"); - /* Note that we don't need to check if the zset may be empty and - * should be removed here, as we can only obtain Nan as score if - * there was already an element in the sorted set. */ - return; - } - } + if ((eptr = zzlFind(zobj,ele,&curscore)) != NULL) { + if (incr) { + score += curscore; + if (isnan(score)) { + addReplyError(c,nanerr); + /* Don't need to check if the sorted set is empty, because + * we know it has at least one element. */ + return; + } + } - /* We need to remove and re-insert the element when it was already present - * in the dictionary, to update the skiplist. Note that we delay adding a - * pointer to the score because we want to reference the score in the - * skiplist node. */ - if (dictAdd(zs->dict,ele,NULL) == DICT_OK) { - dictEntry *de; + /* Remove and re-insert when score changed. */ + if (score != curscore) { + redisAssert(zzlDelete(zobj,eptr) == REDIS_OK); + redisAssert(zzlInsert(zobj,ele,score) == REDIS_OK); - /* New element */ - incrRefCount(ele); /* added to hash */ - znode = zslInsert(zs->zsl,score,ele); - incrRefCount(ele); /* added to skiplist */ + signalModifiedKey(c->db,key); + server.dirty++; + } - /* Update the score in the dict entry */ - de = dictFind(zs->dict,ele); - redisAssert(de != NULL); - dictGetEntryVal(de) = &znode->score; - signalModifiedKey(c->db,c->argv[1]); - server.dirty++; - if (incr) - addReplyDouble(c,score); - else - addReply(c,shared.cone); - } else { - dictEntry *de; - robj *curobj; - double *curscore; - int deleted; + if (incr) /* ZINCRBY */ + addReplyDouble(c,score); + else /* ZADD */ + addReply(c,shared.czero); + } else { + redisAssert(zzlInsert(zobj,ele,score) == REDIS_OK); - /* Update score */ - de = dictFind(zs->dict,ele); - redisAssert(de != NULL); - curobj = dictGetEntryKey(de); - curscore = dictGetEntryVal(de); - - /* When the score is updated, reuse the existing string object to - * prevent extra alloc/dealloc of strings on ZINCRBY. */ - if (score != *curscore) { - deleted = zslDelete(zs->zsl,*curscore,curobj); - redisAssert(deleted != 0); - znode = zslInsert(zs->zsl,score,curobj); - incrRefCount(curobj); - - /* Update the score in the current dict entry */ - dictGetEntryVal(de) = &znode->score; - signalModifiedKey(c->db,c->argv[1]); + signalModifiedKey(c->db,key); server.dirty++; + + if (incr) /* ZINCRBY */ + addReplyDouble(c,score); + else /* ZADD */ + addReply(c,shared.cone); } - if (incr) - addReplyDouble(c,score); - else - addReply(c,shared.czero); + } else if (zobj->encoding == REDIS_ENCODING_RAW) { + zset *zs = zobj->ptr; + zskiplistNode *znode; + dictEntry *de; + + de = dictFind(zs->dict,ele); + if (de != NULL) { + curobj = dictGetEntryKey(de); + curscore = *(double*)dictGetEntryVal(de); + + if (incr) { + score += curscore; + if (isnan(score)) { + addReplyError(c,nanerr); + /* Don't need to check if the sorted set is empty, because + * we know it has at least one element. */ + return; + } + } + + /* Remove and re-insert when score changed. We can safely delete + * the key object from the skiplist, since the dictionary still has + * a reference to it. */ + if (score != curscore) { + redisAssert(zslDelete(zs->zsl,curscore,curobj)); + znode = zslInsert(zs->zsl,score,curobj); + incrRefCount(curobj); /* Re-inserted in skiplist. */ + dictGetEntryVal(de) = &znode->score; /* Update score ptr. */ + + signalModifiedKey(c->db,key); + server.dirty++; + } + + if (incr) /* ZINCRBY */ + addReplyDouble(c,score); + else /* ZADD */ + addReply(c,shared.czero); + } else { + znode = zslInsert(zs->zsl,score,ele); + incrRefCount(ele); /* Inserted in skiplist. */ + redisAssert(dictAdd(zs->dict,ele,&znode->score) == DICT_OK); + incrRefCount(ele); /* Added to dictionary. */ + + signalModifiedKey(c->db,key); + server.dirty++; + + if (incr) /* ZINCRBY */ + addReplyDouble(c,score); + else /* ZADD */ + addReply(c,shared.cone); + } + } else { + redisPanic("Unknown sorted set encoding"); } } From 3ca7532a2d20ea88109cd4f0c3c527c37e3fe52f Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Tue, 8 Mar 2011 16:51:41 +0100 Subject: [PATCH 009/146] Don't encode element argument when dealing with ziplist --- src/t_zset.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/t_zset.c b/src/t_zset.c index e1c61772..9fd524c3 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -560,11 +560,16 @@ int zzlInsert(robj *zobj, robj *ele, double score) { *----------------------------------------------------------------------------*/ /* This generic command implements both ZADD and ZINCRBY. */ -void zaddGenericCommand(redisClient *c, robj *key, robj *ele, double score, int incr) { +void zaddGenericCommand(redisClient *c, int incr) { static char *nanerr = "resulting score is not a number (NaN)"; + robj *key = c->argv[1]; + robj *ele; robj *zobj; robj *curobj; - double curscore = 0.0; + double score, curscore = 0.0; + + if (getDoubleFromObjectOrReply(c,c->argv[2],&score,NULL) != REDIS_OK) + return; zobj = lookupKeyWrite(c->db,key); if (zobj == NULL) { @@ -580,6 +585,8 @@ void zaddGenericCommand(redisClient *c, robj *key, robj *ele, double score, int if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { unsigned char *eptr; + /* Prefer non-encoded element when dealing with ziplists. */ + ele = c->argv[3]; if ((eptr = zzlFind(zobj,ele,&curscore)) != NULL) { if (incr) { score += curscore; @@ -620,6 +627,7 @@ void zaddGenericCommand(redisClient *c, robj *key, robj *ele, double score, int zskiplistNode *znode; dictEntry *de; + ele = c->argv[3] = tryObjectEncoding(c->argv[3]); de = dictFind(zs->dict,ele); if (de != NULL) { curobj = dictGetEntryKey(de); @@ -672,17 +680,11 @@ void zaddGenericCommand(redisClient *c, robj *key, robj *ele, double score, int } void zaddCommand(redisClient *c) { - double scoreval; - if (getDoubleFromObjectOrReply(c,c->argv[2],&scoreval,NULL) != REDIS_OK) return; - c->argv[3] = tryObjectEncoding(c->argv[3]); - zaddGenericCommand(c,c->argv[1],c->argv[3],scoreval,0); + zaddGenericCommand(c,0); } void zincrbyCommand(redisClient *c) { - double scoreval; - if (getDoubleFromObjectOrReply(c,c->argv[2],&scoreval,NULL) != REDIS_OK) return; - c->argv[3] = tryObjectEncoding(c->argv[3]); - zaddGenericCommand(c,c->argv[1],c->argv[3],scoreval,1); + zaddGenericCommand(c,1); } void zremCommand(redisClient *c) { From 0b10e1044496699585b401196dd86e19a9f87cc4 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Tue, 8 Mar 2011 17:11:15 +0100 Subject: [PATCH 010/146] Support dual encoding in ZREM --- src/t_zset.c | 66 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/src/t_zset.c b/src/t_zset.c index 9fd524c3..790fb573 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -449,6 +449,11 @@ int zzlCompareElements(unsigned char *eptr, unsigned char *cstr, unsigned int cl return cmp; } +unsigned int *zzlLength(robj *zobj) { + unsigned char *zl = zobj->ptr; + return ziplistLen(zl)/2; +} + unsigned char *zzlFind(robj *zobj, robj *ele, double *score) { unsigned char *zl = zobj->ptr; unsigned char *eptr = ziplistIndex(zl,0), *sptr; @@ -460,7 +465,7 @@ unsigned char *zzlFind(robj *zobj, robj *ele, double *score) { if (ziplistCompare(eptr,ele->ptr,sdslen(ele->ptr))) { /* Matching element, pull out score. */ - *score = zzlGetScore(sptr); + if (score != NULL) *score = zzlGetScore(sptr); decrRefCount(ele); return eptr; } @@ -688,32 +693,47 @@ void zincrbyCommand(redisClient *c) { } void zremCommand(redisClient *c) { - robj *zsetobj; - zset *zs; - dictEntry *de; - double curscore; - int deleted; + robj *key = c->argv[1]; + robj *ele = c->argv[2]; + robj *zobj; - if ((zsetobj = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL || - checkType(c,zsetobj,REDIS_ZSET)) return; + if ((zobj = lookupKeyWriteOrReply(c,key,shared.czero)) == NULL || + checkType(c,zobj,REDIS_ZSET)) return; - zs = zsetobj->ptr; - c->argv[2] = tryObjectEncoding(c->argv[2]); - de = dictFind(zs->dict,c->argv[2]); - if (de == NULL) { - addReply(c,shared.czero); - return; + if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { + unsigned char *eptr; + + if ((eptr = zzlFind(zobj,ele,NULL)) != NULL) { + redisAssert(zzlDelete(zobj,eptr) == REDIS_OK); + if (zzlLength(zobj) == 0) dbDelete(c->db,key); + } else { + addReply(c,shared.czero); + return; + } + } else if (zobj->encoding == REDIS_ENCODING_RAW) { + zset *zs = zobj->ptr; + dictEntry *de; + double score; + + de = dictFind(zs->dict,ele); + if (de != NULL) { + /* Delete from the skiplist */ + score = *(double*)dictGetEntryVal(de); + redisAssert(zslDelete(zs->zsl,score,ele)); + + /* Delete from the hash table */ + dictDelete(zs->dict,ele); + if (htNeedsResize(zs->dict)) dictResize(zs->dict); + if (dictSize(zs->dict) == 0) dbDelete(c->db,key); + } else { + addReply(c,shared.czero); + return; + } + } else { + redisPanic("Unknown sorted set encoding"); } - /* Delete from the skiplist */ - curscore = *(double*)dictGetEntryVal(de); - deleted = zslDelete(zs->zsl,curscore,c->argv[2]); - redisAssert(deleted != 0); - /* Delete from the hash table */ - dictDelete(zs->dict,c->argv[2]); - if (htNeedsResize(zs->dict)) dictResize(zs->dict); - if (dictSize(zs->dict) == 0) dbDelete(c->db,c->argv[1]); - signalModifiedKey(c->db,c->argv[1]); + signalModifiedKey(c->db,key); server.dirty++; addReply(c,shared.cone); } From 8218db3dae220409f239a5a0adb47d106b178bf9 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Tue, 8 Mar 2011 21:36:43 +0100 Subject: [PATCH 011/146] Little less obfuscation --- src/t_zset.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/t_zset.c b/src/t_zset.c index 790fb573..1aab4b12 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -524,7 +524,6 @@ int zzlInsert(robj *zobj, robj *ele, double score) { unsigned char *zl = zobj->ptr; unsigned char *eptr = ziplistIndex(zl,0), *sptr; double s; - int insert = 0; ele = getDecodedObject(ele); while (eptr != NULL) { @@ -536,16 +535,14 @@ int zzlInsert(robj *zobj, robj *ele, double score) { /* First element with score larger than score for element to be * inserted. This means we should take its spot in the list to * maintain ordering. */ - insert = 1; - } else if (s == score) { - /* Ensure lexicographical ordering for elements. */ - if (zzlCompareElements(eptr,ele->ptr,sdslen(ele->ptr)) < 0) - insert = 1; - } - - if (insert) { zzlInsertAt(zobj,ele,score,eptr); break; + } else if (s == score) { + /* Ensure lexicographical ordering for elements. */ + if (zzlCompareElements(eptr,ele->ptr,sdslen(ele->ptr)) < 0) { + zzlInsertAt(zobj,ele,score,eptr); + break; + } } /* Move to next element. */ @@ -553,8 +550,8 @@ int zzlInsert(robj *zobj, robj *ele, double score) { } /* Push on tail of list when it was not yet inserted. */ - if (!insert) - zzlInsertAt(zobj,ele,score,eptr); + if (eptr == NULL) + zzlInsertAt(zobj,ele,score,NULL); decrRefCount(ele); return REDIS_OK; From 9f9b60f974cb57923c88a585a75db4b82711288b Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Tue, 8 Mar 2011 22:14:46 +0100 Subject: [PATCH 012/146] Typo --- src/t_zset.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/t_zset.c b/src/t_zset.c index 1aab4b12..9119e592 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -449,7 +449,7 @@ int zzlCompareElements(unsigned char *eptr, unsigned char *cstr, unsigned int cl return cmp; } -unsigned int *zzlLength(robj *zobj) { +unsigned int zzlLength(robj *zobj) { unsigned char *zl = zobj->ptr; return ziplistLen(zl)/2; } From 4a14dbbac2eb6148c50212222beb2639ecbc8760 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Tue, 8 Mar 2011 22:23:56 +0100 Subject: [PATCH 013/146] Look up and remove elements by range --- src/t_zset.c | 136 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 126 insertions(+), 10 deletions(-) diff --git a/src/t_zset.c b/src/t_zset.c index 9119e592..8c417ac7 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -454,6 +454,87 @@ unsigned int zzlLength(robj *zobj) { return ziplistLen(zl)/2; } +/* Returns if there is a part of the zset is in range. Should only be used + * internally by zzlFirstInRange and zzlLastInRange. */ +int zzlIsInRange(unsigned char *zl, zrangespec *range) { + unsigned char *p; + double score; + + /* Test for ranges that will always be empty. */ + if (range->min > range->max || + (range->min == range->max && (range->minex || range->maxex))) + return 0; + + p = ziplistIndex(zl,-1); /* Last score. */ + redisAssert(p != NULL); + score = zzlGetScore(p); + if (!zslValueGteMin(score,range)) + return 0; + + p = ziplistIndex(zl,1); /* First score. */ + redisAssert(p != NULL); + score = zzlGetScore(p); + if (!zslValueLteMax(score,range)) + return 0; + + return 1; +} + +/* Find pointer to the first element contained in the specified range. + * Returns NULL when no element is contained in the range. */ +unsigned char *zzlFirstInRange(robj *zobj, zrangespec range) { + unsigned char *zl = zobj->ptr; + unsigned char *eptr = ziplistIndex(zl,0), *sptr; + double score; + + /* If everything is out of range, return early. */ + if (!zzlIsInRange(zl,&range)) return NULL; + + while (eptr != NULL) { + sptr = ziplistNext(zl,eptr); + redisAssert(sptr != NULL); + + score = zzlGetScore(sptr); + if (zslValueGteMin(score,&range)) + return eptr; + + /* Move to next element. */ + eptr = ziplistNext(zl,sptr); + } + + return NULL; +} + +/* Find pointer to the last element contained in the specified range. + * Returns NULL when no element is contained in the range. */ +unsigned char *zzlLastInRange(robj *zobj, zrangespec range) { + unsigned char *zl = zobj->ptr; + unsigned char *eptr = ziplistIndex(zl,-2), *sptr; + double score; + + /* If everything is out of range, return early. */ + if (!zzlIsInRange(zl,&range)) return NULL; + + while (eptr != NULL) { + sptr = ziplistNext(zl,eptr); + redisAssert(sptr != NULL); + + score = zzlGetScore(sptr); + if (zslValueLteMax(score,&range)) + return eptr; + + /* Move to previous element by moving to the score of previous element. + * When this returns NULL, we know there also is no element. */ + sptr = ziplistPrev(zl,eptr); + if (sptr != NULL) + redisAssert((eptr = ziplistPrev(zl,sptr)) != NULL); + else + eptr = NULL; + } + + return NULL; +} + unsigned char *zzlFind(robj *zobj, robj *ele, double *score) { unsigned char *zl = zobj->ptr; unsigned char *eptr = ziplistIndex(zl,0), *sptr; @@ -557,6 +638,34 @@ int zzlInsert(robj *zobj, robj *ele, double score) { return REDIS_OK; } +unsigned long zzlDeleteRangeByScore(robj *zobj, zrangespec range) { + unsigned char *zl = zobj->ptr; + unsigned char *eptr, *sptr; + double score; + unsigned long deleted = 0; + + eptr = zzlFirstInRange(zobj,range); + if (eptr == NULL) return deleted; + + + /* When the tail of the ziplist is deleted, eptr will point to the sentinel + * byte and ziplistNext will return NULL. */ + while ((sptr = ziplistNext(zl,eptr)) != NULL) { + score = zzlGetScore(sptr); + if (zslValueLteMax(score,&range)) { + /* Delete both the element and the score. */ + zl = ziplistDelete(zl,&eptr); + zl = ziplistDelete(zl,&eptr); + deleted++; + } else { + /* No longer in range. */ + break; + } + } + + return deleted; +} + /*----------------------------------------------------------------------------- * Sorted set commands *----------------------------------------------------------------------------*/ @@ -736,10 +845,10 @@ void zremCommand(redisClient *c) { } void zremrangebyscoreCommand(redisClient *c) { + robj *key = c->argv[1]; + robj *zobj; zrangespec range; - long deleted; - robj *o; - zset *zs; + unsigned long deleted; /* Parse the range arguments. */ if (zslParseRange(c->argv[2],c->argv[3],&range) != REDIS_OK) { @@ -747,14 +856,21 @@ void zremrangebyscoreCommand(redisClient *c) { return; } - if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL || - checkType(c,o,REDIS_ZSET)) return; + if ((zobj = lookupKeyWriteOrReply(c,key,shared.czero)) == NULL || + checkType(c,zobj,REDIS_ZSET)) return; - zs = o->ptr; - deleted = zslDeleteRangeByScore(zs->zsl,range,zs->dict); - if (htNeedsResize(zs->dict)) dictResize(zs->dict); - if (dictSize(zs->dict) == 0) dbDelete(c->db,c->argv[1]); - if (deleted) signalModifiedKey(c->db,c->argv[1]); + if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { + deleted = zzlDeleteRangeByScore(zobj,range); + } else if (zobj->encoding == REDIS_ENCODING_RAW) { + zset *zs = zobj->ptr; + deleted = zslDeleteRangeByScore(zs->zsl,range,zs->dict); + if (htNeedsResize(zs->dict)) dictResize(zs->dict); + if (dictSize(zs->dict) == 0) dbDelete(c->db,key); + } else { + redisPanic("Unknown sorted set encoding"); + } + + if (deleted) signalModifiedKey(c->db,key); server.dirty += deleted; addReplyLongLong(c,deleted); } From 0f23eb3b10402bd7166c09583333c29aa3c3f55f Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Tue, 8 Mar 2011 23:56:59 +0100 Subject: [PATCH 014/146] Properly free encoded sorted set --- src/object.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/object.c b/src/object.c index a6bf8a20..c384d600 100644 --- a/src/object.c +++ b/src/object.c @@ -138,11 +138,20 @@ void freeSetObject(robj *o) { } void freeZsetObject(robj *o) { - zset *zs = o->ptr; - - dictRelease(zs->dict); - zslFree(zs->zsl); - zfree(zs); + zset *zs; + switch (o->encoding) { + case REDIS_ENCODING_RAW: + zs = o->ptr; + dictRelease(zs->dict); + zslFree(zs->zsl); + zfree(zs); + break; + case REDIS_ENCODING_ZIPLIST: + zfree(o->ptr); + break; + default: + redisPanic("Unknown sorted set encoding"); + } } void freeHashObject(robj *o) { From 5d1b4fb6983f5acd9cfb6ee5f5715547688448d2 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 9 Mar 2011 00:00:19 +0100 Subject: [PATCH 015/146] Support dual encoding for ZRANGE --- src/t_zset.c | 125 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 96 insertions(+), 29 deletions(-) diff --git a/src/t_zset.c b/src/t_zset.c index 8c417ac7..6b2b6d9f 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -666,6 +666,22 @@ unsigned long zzlDeleteRangeByScore(robj *zobj, zrangespec range) { return deleted; } +/*----------------------------------------------------------------------------- + * Common sorted set API + *----------------------------------------------------------------------------*/ + +int zsLength(robj *zobj) { + int length = -1; + if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { + length = zzlLength(zobj); + } else if (zobj->encoding == REDIS_ENCODING_RAW) { + length = ((zset*)zobj->ptr)->zsl->length; + } else { + redisPanic("Unknown sorted set encoding"); + } + return length; +} + /*----------------------------------------------------------------------------- * Sorted set commands *----------------------------------------------------------------------------*/ @@ -1135,16 +1151,13 @@ void zinterstoreCommand(redisClient *c) { } void zrangeGenericCommand(redisClient *c, int reverse) { - robj *o; + robj *key = c->argv[1]; + robj *zobj; + int withscores = 0; long start; long end; - int withscores = 0; int llen; - int rangelen, j; - zset *zsetobj; - zskiplist *zsl; - zskiplistNode *ln; - robj *ele; + int rangelen; if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != REDIS_OK) || (getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != REDIS_OK)) return; @@ -1156,13 +1169,11 @@ void zrangeGenericCommand(redisClient *c, int reverse) { return; } - if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptymultibulk)) == NULL - || checkType(c,o,REDIS_ZSET)) return; - zsetobj = o->ptr; - zsl = zsetobj->zsl; - llen = zsl->length; + if ((zobj = lookupKeyReadOrReply(c,key,shared.emptymultibulk)) == NULL + || checkType(c,zobj,REDIS_ZSET)) return; - /* convert negative indexes */ + /* Sanitize indexes. */ + llen = zsLength(zobj); if (start < 0) start = llen+start; if (end < 0) end = llen+end; if (start < 0) start = 0; @@ -1176,23 +1187,79 @@ void zrangeGenericCommand(redisClient *c, int reverse) { if (end >= llen) end = llen-1; rangelen = (end-start)+1; - /* check if starting point is trivial, before searching - * the element in log(N) time */ - if (reverse) { - ln = start == 0 ? zsl->tail : zslGetElementByRank(zsl, llen-start); - } else { - ln = start == 0 ? - zsl->header->level[0].forward : zslGetElementByRank(zsl, start+1); - } - /* Return the result in form of a multi-bulk reply */ - addReplyMultiBulkLen(c,withscores ? (rangelen*2) : rangelen); - for (j = 0; j < rangelen; j++) { - ele = ln->obj; - addReplyBulk(c,ele); - if (withscores) - addReplyDouble(c,ln->score); - ln = reverse ? ln->backward : ln->level[0].forward; + addReplyMultiBulkLen(c, withscores ? (rangelen*2) : rangelen); + + if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { + unsigned char *zl = zobj->ptr; + unsigned char *eptr, *sptr; + unsigned char *vstr; + unsigned int vlen; + long long vlong; + + if (reverse) + eptr = ziplistIndex(zl,-2-(2*start)); + else + eptr = ziplistIndex(zl,2*start); + + while (rangelen--) { + redisAssert(eptr != NULL); + redisAssert(ziplistGet(eptr,&vstr,&vlen,&vlong)); + if (vstr == NULL) + addReplyBulkLongLong(c,vlong); + else + addReplyBulkCBuffer(c,vstr,vlen); + + if (withscores) { + sptr = ziplistNext(zl,eptr); + redisAssert(sptr != NULL); + addReplyDouble(c,zzlGetScore(sptr)); + } + + if (reverse) { + /* Move to previous element by moving to the score of previous + * element. When NULL, we know there also is no element. */ + sptr = ziplistPrev(zl,eptr); + if (sptr != NULL) { + eptr = ziplistPrev(zl,sptr); + redisAssert(eptr != NULL); + } else { + eptr = NULL; + } + } else { + sptr = ziplistNext(zl,eptr); + redisAssert(sptr != NULL); + eptr = ziplistNext(zl,sptr); + } + } + + } else if (zobj->encoding == REDIS_ENCODING_RAW) { + zset *zs = zobj->ptr; + zskiplist *zsl = zs->zsl; + zskiplistNode *ln; + robj *ele; + + /* Check if starting point is trivial, before doing log(N) lookup. */ + if (reverse) { + ln = zsl->tail; + if (start > 0) + ln = zslGetElementByRank(zsl,llen-start); + } else { + ln = zsl->header->level[0].forward; + if (start > 0) + ln = zslGetElementByRank(zsl,start+1); + } + + while(rangelen--) { + redisAssert(ln != NULL); + ele = ln->obj; + addReplyBulk(c,ele); + if (withscores) + addReplyDouble(c,ln->score); + ln = reverse ? ln->backward : ln->level[0].forward; + } + } else { + redisPanic("Unknown sorted set encoding"); } } From 63b7b7fb34984b47971a4ced65ae49b6c7a350fa Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 9 Mar 2011 10:30:55 +0100 Subject: [PATCH 016/146] Support dual encoding for ZREMRANGEBYRANK --- src/t_zset.c | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/src/t_zset.c b/src/t_zset.c index 6b2b6d9f..c71be90a 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -666,6 +666,14 @@ unsigned long zzlDeleteRangeByScore(robj *zobj, zrangespec range) { return deleted; } +/* Delete all the elements with rank between start and end from the skiplist. + * Start and end are inclusive. Note that start and end need to be 1-based */ +unsigned long zzlDeleteRangeByRank(robj *zobj, unsigned int start, unsigned int end) { + unsigned int num = (end-start)+1; + zobj->ptr = ziplistDeleteRange(zobj->ptr,2*(start-1),2*num); + return num; +} + /*----------------------------------------------------------------------------- * Common sorted set API *----------------------------------------------------------------------------*/ @@ -892,22 +900,21 @@ void zremrangebyscoreCommand(redisClient *c) { } void zremrangebyrankCommand(redisClient *c) { + robj *key = c->argv[1]; + robj *zobj; long start; long end; int llen; - long deleted; - robj *zsetobj; - zset *zs; + unsigned long deleted; if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != REDIS_OK) || (getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != REDIS_OK)) return; - if ((zsetobj = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL || - checkType(c,zsetobj,REDIS_ZSET)) return; - zs = zsetobj->ptr; - llen = zs->zsl->length; + if ((zobj = lookupKeyWriteOrReply(c,key,shared.czero)) == NULL || + checkType(c,zobj,REDIS_ZSET)) return; - /* convert negative indexes */ + /* Sanitize indexes. */ + llen = zsLength(zobj); if (start < 0) start = llen+start; if (end < 0) end = llen+end; if (start < 0) start = 0; @@ -920,14 +927,23 @@ void zremrangebyrankCommand(redisClient *c) { } if (end >= llen) end = llen-1; - /* increment start and end because zsl*Rank functions - * use 1-based rank */ - deleted = zslDeleteRangeByRank(zs->zsl,start+1,end+1,zs->dict); - if (htNeedsResize(zs->dict)) dictResize(zs->dict); - if (dictSize(zs->dict) == 0) dbDelete(c->db,c->argv[1]); - if (deleted) signalModifiedKey(c->db,c->argv[1]); + if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { + /* Correct for 1-based rank. */ + deleted = zzlDeleteRangeByRank(zobj,start+1,end+1); + } else if (zobj->encoding == REDIS_ENCODING_RAW) { + zset *zs = zobj->ptr; + + /* Correct for 1-based rank. */ + deleted = zslDeleteRangeByRank(zs->zsl,start+1,end+1,zs->dict); + if (htNeedsResize(zs->dict)) dictResize(zs->dict); + if (dictSize(zs->dict) == 0) dbDelete(c->db,key); + } else { + redisPanic("Unknown sorted set encoding"); + } + + if (deleted) signalModifiedKey(c->db,key); server.dirty += deleted; - addReplyLongLong(c, deleted); + addReplyLongLong(c,deleted); } typedef struct { From 4c5f0966b2e582981d9fdaf3b511c6cf4ac4d4d5 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 9 Mar 2011 11:06:25 +0100 Subject: [PATCH 017/146] Helpers to move around in encoded sorted set --- src/t_zset.c | 67 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/src/t_zset.c b/src/t_zset.c index c71be90a..8665aa14 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -454,6 +454,44 @@ unsigned int zzlLength(robj *zobj) { return ziplistLen(zl)/2; } +/* Move to next entry based on the values in eptr and sptr. Both are set to + * NULL when there is no next entry. */ +void zzlNext(unsigned char *zl, unsigned char **eptr, unsigned char **sptr) { + unsigned char *_eptr, *_sptr; + redisAssert(*eptr != NULL && *sptr != NULL); + + _eptr = ziplistNext(zl,*sptr); + if (_eptr != NULL) { + _sptr = ziplistNext(zl,_eptr); + redisAssert(_sptr != NULL); + } else { + /* No next entry. */ + _sptr = NULL; + } + + *eptr = _eptr; + *sptr = _sptr; +} + +/* Move to the previous entry based on the values in eptr and sptr. Both are + * set to NULL when there is no next entry. */ +void zzlPrev(unsigned char *zl, unsigned char **eptr, unsigned char **sptr) { + unsigned char *_eptr, *_sptr; + redisAssert(*eptr != NULL && *sptr != NULL); + + _sptr = ziplistPrev(zl,*eptr); + if (_sptr != NULL) { + _eptr = ziplistPrev(zl,_sptr); + redisAssert(_eptr != NULL); + } else { + /* No previous entry. */ + _eptr = NULL; + } + + *eptr = _eptr; + *sptr = _sptr; +} + /* Returns if there is a part of the zset is in range. Should only be used * internally by zzlFirstInRange and zzlLastInRange. */ int zzlIsInRange(unsigned char *zl, zrangespec *range) { @@ -1218,35 +1256,24 @@ void zrangeGenericCommand(redisClient *c, int reverse) { else eptr = ziplistIndex(zl,2*start); + redisAssert(eptr != NULL); + sptr = ziplistNext(zl,eptr); + while (rangelen--) { - redisAssert(eptr != NULL); + redisAssert(eptr != NULL && sptr != NULL); redisAssert(ziplistGet(eptr,&vstr,&vlen,&vlong)); if (vstr == NULL) addReplyBulkLongLong(c,vlong); else addReplyBulkCBuffer(c,vstr,vlen); - if (withscores) { - sptr = ziplistNext(zl,eptr); - redisAssert(sptr != NULL); + if (withscores) addReplyDouble(c,zzlGetScore(sptr)); - } - if (reverse) { - /* Move to previous element by moving to the score of previous - * element. When NULL, we know there also is no element. */ - sptr = ziplistPrev(zl,eptr); - if (sptr != NULL) { - eptr = ziplistPrev(zl,sptr); - redisAssert(eptr != NULL); - } else { - eptr = NULL; - } - } else { - sptr = ziplistNext(zl,eptr); - redisAssert(sptr != NULL); - eptr = ziplistNext(zl,sptr); - } + if (reverse) + zzlPrev(zl,&eptr,&sptr); + else + zzlNext(zl,&eptr,&sptr); } } else if (zobj->encoding == REDIS_ENCODING_RAW) { From aff255c81df8bd27ff53a21dd7ece5595f2ed8a9 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 9 Mar 2011 11:29:21 +0100 Subject: [PATCH 018/146] Support dual encoding for ZRANGEBYSCORE et al --- src/t_zset.c | 173 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 121 insertions(+), 52 deletions(-) diff --git a/src/t_zset.c b/src/t_zset.c index 8665aa14..5fe10883 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -1318,10 +1318,8 @@ void zrevrangeCommand(redisClient *c) { * If "justcount", only the number of elements in the range is returned. */ void genericZrangebyscoreCommand(redisClient *c, int reverse, int justcount) { zrangespec range; - robj *o, *emptyreply; - zset *zsetobj; - zskiplist *zsl; - zskiplistNode *ln; + robj *key = c->argv[1]; + robj *emptyreply, *zobj; int offset = 0, limit = -1; int withscores = 0; unsigned long rangelen = 0; @@ -1365,61 +1363,132 @@ void genericZrangebyscoreCommand(redisClient *c, int reverse, int justcount) { /* Ok, lookup the key and get the range */ emptyreply = justcount ? shared.czero : shared.emptymultibulk; - if ((o = lookupKeyReadOrReply(c,c->argv[1],emptyreply)) == NULL || - checkType(c,o,REDIS_ZSET)) return; - zsetobj = o->ptr; - zsl = zsetobj->zsl; + if ((zobj = lookupKeyReadOrReply(c,key,emptyreply)) == NULL || + checkType(c,zobj,REDIS_ZSET)) return; - /* If reversed, get the last node in range as starting point. */ - if (reverse) { - ln = zslLastInRange(zsl,range); + if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { + unsigned char *zl = zobj->ptr; + unsigned char *eptr, *sptr; + unsigned char *vstr; + unsigned int vlen; + long long vlong; + double score; + + /* If reversed, get the last node in range as starting point. */ + if (reverse) + eptr = zzlLastInRange(zobj,range); + else + eptr = zzlFirstInRange(zobj,range); + + /* No "first" element in the specified interval. */ + if (eptr == NULL) { + addReply(c,emptyreply); + return; + } + + /* Get score pointer for the first element. */ + redisAssert(eptr != NULL); + sptr = ziplistNext(zl,eptr); + + /* We don't know in advance how many matching elements there are in the + * list, so we push this object that will represent the multi-bulk + * length in the output buffer, and will "fix" it later */ + if (!justcount) + replylen = addDeferredMultiBulkLength(c); + + /* If there is an offset, just traverse the number of elements without + * checking the score because that is done in the next loop. */ + while (eptr && offset--) + if (reverse) + zzlPrev(zl,&eptr,&sptr); + else + zzlNext(zl,&eptr,&sptr); + + while (eptr && limit--) { + score = zzlGetScore(sptr); + + /* Abort when the node is no longer in range. */ + if (reverse) { + if (!zslValueGteMin(score,&range)) break; + } else { + if (!zslValueLteMax(score,&range)) break; + } + + /* Do our magic */ + rangelen++; + if (!justcount) { + redisAssert(ziplistGet(eptr,&vstr,&vlen,&vlong)); + if (vstr == NULL) + addReplyBulkLongLong(c,vlong); + else + addReplyBulkCBuffer(c,vstr,vlen); + + if (withscores) + addReplyDouble(c,score); + } + + /* Move to next node */ + if (reverse) + zzlPrev(zl,&eptr,&sptr); + else + zzlNext(zl,&eptr,&sptr); + } + } else if (zobj->encoding == REDIS_ENCODING_RAW) { + zset *zs = zobj->ptr; + zskiplist *zsl = zs->zsl; + zskiplistNode *ln; + + /* If reversed, get the last node in range as starting point. */ + if (reverse) + ln = zslLastInRange(zsl,range); + else + ln = zslFirstInRange(zsl,range); + + /* No "first" element in the specified interval. */ + if (ln == NULL) { + addReply(c,emptyreply); + return; + } + + /* We don't know in advance how many matching elements there are in the + * list, so we push this object that will represent the multi-bulk + * length in the output buffer, and will "fix" it later */ + if (!justcount) + replylen = addDeferredMultiBulkLength(c); + + /* If there is an offset, just traverse the number of elements without + * checking the score because that is done in the next loop. */ + while (ln && offset--) + ln = reverse ? ln->backward : ln->level[0].forward; + + while (ln && limit--) { + /* Abort when the node is no longer in range. */ + if (reverse) { + if (!zslValueGteMin(ln->score,&range)) break; + } else { + if (!zslValueLteMax(ln->score,&range)) break; + } + + /* Do our magic */ + rangelen++; + if (!justcount) { + addReplyBulk(c,ln->obj); + if (withscores) + addReplyDouble(c,ln->score); + } + + /* Move to next node */ + ln = reverse ? ln->backward : ln->level[0].forward; + } } else { - ln = zslFirstInRange(zsl,range); - } - - /* No "first" element in the specified interval. */ - if (ln == NULL) { - addReply(c,emptyreply); - return; - } - - /* We don't know in advance how many matching elements there are in the - * list, so we push this object that will represent the multi-bulk length - * in the output buffer, and will "fix" it later */ - if (!justcount) - replylen = addDeferredMultiBulkLength(c); - - /* If there is an offset, just traverse the number of elements without - * checking the score because that is done in the next loop. */ - while(ln && offset--) { - ln = reverse ? ln->backward : ln->level[0].forward; - } - - while (ln && limit--) { - /* Abort when the node is no longer in range. */ - if (reverse) { - if (!zslValueGteMin(ln->score,&range)) break; - } else { - if (!zslValueLteMax(ln->score,&range)) break; - } - - /* Do our magic */ - rangelen++; - if (!justcount) { - addReplyBulk(c,ln->obj); - if (withscores) - addReplyDouble(c,ln->score); - } - - /* Move to next node */ - ln = reverse ? ln->backward : ln->level[0].forward; + redisPanic("Unknown sorted set encoding"); } if (justcount) { addReplyLongLong(c,(long)rangelen); } else { - setDeferredMultiBulkLength(c,replylen, - withscores ? (rangelen*2) : rangelen); + if (withscores) rangelen *= 2; + setDeferredMultiBulkLength(c,replylen,rangelen); } } From d1c920c53869907debf8a0f81ea320218fe95214 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 9 Mar 2011 12:37:59 +0100 Subject: [PATCH 019/146] Support dual encoding for more commands --- src/t_zset.c | 121 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 79 insertions(+), 42 deletions(-) diff --git a/src/t_zset.c b/src/t_zset.c index 5fe10883..25da7871 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -658,7 +658,7 @@ int zzlInsert(robj *zobj, robj *ele, double score) { break; } else if (s == score) { /* Ensure lexicographical ordering for elements. */ - if (zzlCompareElements(eptr,ele->ptr,sdslen(ele->ptr)) < 0) { + if (zzlCompareElements(eptr,ele->ptr,sdslen(ele->ptr)) > 0) { zzlInsertAt(zobj,ele,score,eptr); break; } @@ -1505,66 +1505,103 @@ void zcountCommand(redisClient *c) { } void zcardCommand(redisClient *c) { - robj *o; - zset *zs; + robj *key = c->argv[1]; + robj *zobj; - if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL || - checkType(c,o,REDIS_ZSET)) return; + if ((zobj = lookupKeyReadOrReply(c,key,shared.czero)) == NULL || + checkType(c,zobj,REDIS_ZSET)) return; - zs = o->ptr; - addReplyLongLong(c,zs->zsl->length); + addReplyLongLong(c,zzlLength(zobj)); } void zscoreCommand(redisClient *c) { - robj *o; - zset *zs; - dictEntry *de; + robj *key = c->argv[1]; + robj *zobj; + double score; - if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL || - checkType(c,o,REDIS_ZSET)) return; + if ((zobj = lookupKeyReadOrReply(c,key,shared.nullbulk)) == NULL || + checkType(c,zobj,REDIS_ZSET)) return; - zs = o->ptr; - c->argv[2] = tryObjectEncoding(c->argv[2]); - de = dictFind(zs->dict,c->argv[2]); - if (!de) { - addReply(c,shared.nullbulk); + if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { + if (zzlFind(zobj,c->argv[2],&score) != NULL) + addReplyDouble(c,score); + else + addReply(c,shared.nullbulk); + } else if (zobj->encoding == REDIS_ENCODING_RAW) { + zset *zs = zobj->ptr; + dictEntry *de; + + c->argv[2] = tryObjectEncoding(c->argv[2]); + de = dictFind(zs->dict,c->argv[2]); + if (de != NULL) { + score = *(double*)dictGetEntryVal(de); + addReplyDouble(c,score); + } else { + addReply(c,shared.nullbulk); + } } else { - double *score = dictGetEntryVal(de); - - addReplyDouble(c,*score); + redisPanic("Unknown sorted set encoding"); } } void zrankGenericCommand(redisClient *c, int reverse) { - robj *o; - zset *zs; - zskiplist *zsl; - dictEntry *de; + robj *key = c->argv[1]; + robj *ele = c->argv[2]; + robj *zobj; + unsigned long llen; unsigned long rank; - double *score; - if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL || - checkType(c,o,REDIS_ZSET)) return; + if ((zobj = lookupKeyReadOrReply(c,key,shared.nullbulk)) == NULL || + checkType(c,zobj,REDIS_ZSET)) return; + llen = zsLength(zobj); - zs = o->ptr; - zsl = zs->zsl; - c->argv[2] = tryObjectEncoding(c->argv[2]); - de = dictFind(zs->dict,c->argv[2]); - if (!de) { - addReply(c,shared.nullbulk); - return; - } + redisAssert(ele->encoding == REDIS_ENCODING_RAW); + if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { + unsigned char *zl = zobj->ptr; + unsigned char *eptr, *sptr; - score = dictGetEntryVal(de); - rank = zslGetRank(zsl, *score, c->argv[2]); - if (rank) { - if (reverse) { - addReplyLongLong(c, zsl->length - rank); + eptr = ziplistIndex(zl,0); + redisAssert(eptr != NULL); + sptr = ziplistNext(zl,eptr); + redisAssert(sptr != NULL); + + rank = 1; + while(eptr != NULL) { + if (ziplistCompare(eptr,ele->ptr,sdslen(ele->ptr))) + break; + rank++; + zzlNext(zl,&eptr,&sptr); + } + + if (eptr != NULL) { + if (reverse) + addReplyLongLong(c,llen-rank); + else + addReplyLongLong(c,rank-1); } else { - addReplyLongLong(c, rank-1); + addReply(c,shared.nullbulk); + } + } else if (zobj->encoding == REDIS_ENCODING_RAW) { + zset *zs = zobj->ptr; + zskiplist *zsl = zs->zsl; + dictEntry *de; + double score; + + ele = c->argv[2] = tryObjectEncoding(c->argv[2]); + de = dictFind(zs->dict,ele); + if (de != NULL) { + score = *(double*)dictGetEntryVal(de); + rank = zslGetRank(zsl,score,ele); + redisAssert(rank); /* Existing elements always have a rank. */ + if (reverse) + addReplyLongLong(c,llen-rank); + else + addReplyLongLong(c,rank-1); + } else { + addReply(c,shared.nullbulk); } } else { - addReply(c,shared.nullbulk); + redisPanic("Unknown sorted set encoding"); } } From e12b27acf72ee7e40a6a39ffc7c109914c584cd9 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 9 Mar 2011 13:16:38 +0100 Subject: [PATCH 020/146] Persistence code for encoded sorted sets --- src/rdb.c | 46 +++++++++++++++++++++++++++++++--------------- src/redis.h | 2 ++ 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/rdb.c b/src/rdb.c index c9fb3e83..62762bee 100644 --- a/src/rdb.c +++ b/src/rdb.c @@ -302,24 +302,33 @@ int rdbSaveObject(FILE *fp, robj *o) { redisPanic("Unknown set encoding"); } } else if (o->type == REDIS_ZSET) { - /* Save a set value */ - zset *zs = o->ptr; - dictIterator *di = dictGetIterator(zs->dict); - dictEntry *de; + /* Save a sorted set value */ + if (o->encoding == REDIS_ENCODING_ZIPLIST) { + size_t l = ziplistBlobLen((unsigned char*)o->ptr); - if ((n = rdbSaveLen(fp,dictSize(zs->dict))) == -1) return -1; - nwritten += n; - - while((de = dictNext(di)) != NULL) { - robj *eleobj = dictGetEntryKey(de); - double *score = dictGetEntryVal(de); - - if ((n = rdbSaveStringObject(fp,eleobj)) == -1) return -1; + if ((n = rdbSaveRawString(fp,o->ptr,l)) == -1) return -1; nwritten += n; - if ((n = rdbSaveDoubleValue(fp,*score)) == -1) return -1; + } else if (o->encoding == REDIS_ENCODING_RAW) { + zset *zs = o->ptr; + dictIterator *di = dictGetIterator(zs->dict); + dictEntry *de; + + if ((n = rdbSaveLen(fp,dictSize(zs->dict))) == -1) return -1; nwritten += n; + + while((de = dictNext(di)) != NULL) { + robj *eleobj = dictGetEntryKey(de); + double *score = dictGetEntryVal(de); + + if ((n = rdbSaveStringObject(fp,eleobj)) == -1) return -1; + nwritten += n; + if ((n = rdbSaveDoubleValue(fp,*score)) == -1) return -1; + nwritten += n; + } + dictReleaseIterator(di); + } else { + redisPanic("Unknown sorted set enoding"); } - dictReleaseIterator(di); } else if (o->type == REDIS_HASH) { /* Save a hash value */ if (o->encoding == REDIS_ENCODING_ZIPMAP) { @@ -386,6 +395,8 @@ int rdbSaveKeyValuePair(FILE *fp, robj *key, robj *val, vtype = REDIS_LIST_ZIPLIST; else if (vtype == REDIS_SET && val->encoding == REDIS_ENCODING_INTSET) vtype = REDIS_SET_INTSET; + else if (vtype == REDIS_ZSET && val->encoding == REDIS_ENCODING_ZIPLIST) + vtype = REDIS_ZSET_ZIPLIST; /* Save type, key, value */ if (rdbSaveType(fp,vtype) == -1) return -1; if (rdbSaveStringObject(fp,key) == -1) return -1; @@ -811,7 +822,8 @@ robj *rdbLoadObject(int type, FILE *fp) { } } else if (type == REDIS_HASH_ZIPMAP || type == REDIS_LIST_ZIPLIST || - type == REDIS_SET_INTSET) + type == REDIS_SET_INTSET || + type == REDIS_ZSET_ZIPLIST) { robj *aux = rdbLoadStringObject(fp); @@ -846,6 +858,10 @@ robj *rdbLoadObject(int type, FILE *fp) { if (intsetLen(o->ptr) > server.set_max_intset_entries) setTypeConvert(o,REDIS_ENCODING_HT); break; + case REDIS_ZSET_ZIPLIST: + o->type = REDIS_ZSET; + o->encoding = REDIS_ENCODING_ZIPLIST; + break; default: redisPanic("Unknown enoding"); break; diff --git a/src/redis.h b/src/redis.h index 78294d0d..9924cee7 100644 --- a/src/redis.h +++ b/src/redis.h @@ -70,10 +70,12 @@ #define REDIS_ZSET 3 #define REDIS_HASH 4 #define REDIS_VMPOINTER 8 + /* Object types only used for persistence in .rdb files */ #define REDIS_HASH_ZIPMAP 9 #define REDIS_LIST_ZIPLIST 10 #define REDIS_SET_INTSET 11 +#define REDIS_ZSET_ZIPLIST 12 /* Objects encoding. Some kind of objects like Strings and Hashes can be * internally represented in multiple ways. The 'encoding' field of the object From 3ea204e1031a94dafca7f7e4eed2f79ec3bd7fd0 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 9 Mar 2011 14:01:57 +0100 Subject: [PATCH 021/146] Configurable thresholds for encoded sorted sets --- redis.conf | 6 ++++++ src/config.c | 20 ++++++++++++++++++++ src/redis.c | 2 ++ src/redis.h | 4 ++++ 4 files changed, 32 insertions(+) diff --git a/redis.conf b/redis.conf index a470c98c..e7a01eec 100644 --- a/redis.conf +++ b/redis.conf @@ -340,6 +340,12 @@ list-max-ziplist-value 64 # set in order to use this special memory saving encoding. set-max-intset-entries 512 +# Similarly to hashes and lists, sorted sets are also specially encoded in +# order to save a lot of space. This encoding is only used when the length and +# elements of a sorted set are below the following limits: +zset-max-ziplist-entries 128 +zset-max-ziplist-value 64 + # Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in # order to help rehashing the main Redis hash table (the one mapping top-level # keys to values). The hash table implementation redis uses (see dict.c) diff --git a/src/config.c b/src/config.c index e0bf1574..fec72a45 100644 --- a/src/config.c +++ b/src/config.c @@ -261,6 +261,10 @@ void loadServerConfig(char *filename) { server.list_max_ziplist_value = memtoll(argv[1], NULL); } else if (!strcasecmp(argv[0],"set-max-intset-entries") && argc == 2) { server.set_max_intset_entries = memtoll(argv[1], NULL); + } else if (!strcasecmp(argv[0],"zset-max-ziplist-entries") && argc == 2) { + server.zset_max_ziplist_entries = memtoll(argv[1], NULL); + } else if (!strcasecmp(argv[0],"zset-max-ziplist-value") && argc == 2) { + server.zset_max_ziplist_value = memtoll(argv[1], NULL); } else if (!strcasecmp(argv[0],"rename-command") && argc == 3) { struct redisCommand *cmd = lookupCommand(argv[1]); int retval; @@ -443,6 +447,12 @@ void configSetCommand(redisClient *c) { } else if (!strcasecmp(c->argv[2]->ptr,"set-max-intset-entries")) { if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt; server.set_max_intset_entries = ll; + } else if (!strcasecmp(c->argv[2]->ptr,"zset-max-ziplist-entries")) { + if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt; + server.zset_max_ziplist_entries = ll; + } else if (!strcasecmp(c->argv[2]->ptr,"zset-max-ziplist-value")) { + if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt; + server.zset_max_ziplist_value = ll; } else { addReplyErrorFormat(c,"Unsupported CONFIG parameter: %s", (char*)c->argv[2]->ptr); @@ -594,6 +604,16 @@ void configGetCommand(redisClient *c) { addReplyBulkLongLong(c,server.set_max_intset_entries); matches++; } + if (stringmatch(pattern,"zset-max-ziplist-entries",0)) { + addReplyBulkCString(c,"zset-max-ziplist-entries"); + addReplyBulkLongLong(c,server.zset_max_ziplist_entries); + matches++; + } + if (stringmatch(pattern,"zset-max-ziplist-value",0)) { + addReplyBulkCString(c,"zset-max-ziplist-value"); + addReplyBulkLongLong(c,server.zset_max_ziplist_value); + matches++; + } setDeferredMultiBulkLength(c,replylen,matches*2); } diff --git a/src/redis.c b/src/redis.c index 866ac360..2b98d40c 100644 --- a/src/redis.c +++ b/src/redis.c @@ -821,6 +821,8 @@ void initServerConfig() { server.list_max_ziplist_entries = REDIS_LIST_MAX_ZIPLIST_ENTRIES; server.list_max_ziplist_value = REDIS_LIST_MAX_ZIPLIST_VALUE; server.set_max_intset_entries = REDIS_SET_MAX_INTSET_ENTRIES; + server.zset_max_ziplist_entries = REDIS_ZSET_MAX_ZIPLIST_ENTRIES; + server.zset_max_ziplist_value = REDIS_ZSET_MAX_ZIPLIST_VALUE; server.shutdown_asap = 0; server.cache_flush_delay = 0; diff --git a/src/redis.h b/src/redis.h index 9924cee7..9f53583e 100644 --- a/src/redis.h +++ b/src/redis.h @@ -197,6 +197,8 @@ #define REDIS_LIST_MAX_ZIPLIST_ENTRIES 512 #define REDIS_LIST_MAX_ZIPLIST_VALUE 64 #define REDIS_SET_MAX_INTSET_ENTRIES 512 +#define REDIS_ZSET_MAX_ZIPLIST_ENTRIES 128 +#define REDIS_ZSET_MAX_ZIPLIST_VALUE 64 /* Sets operations codes */ #define REDIS_OP_UNION 0 @@ -470,6 +472,8 @@ struct redisServer { size_t list_max_ziplist_entries; size_t list_max_ziplist_value; size_t set_max_intset_entries; + size_t zset_max_ziplist_entries; + size_t zset_max_ziplist_value; time_t unixtime; /* Unix time sampled every second. */ /* Virtual memory I/O threads stuff */ /* An I/O thread process an element taken from the io_jobs queue and From a669d5e99945b873279eadfcf289181956cb62c3 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 9 Mar 2011 16:13:06 +0100 Subject: [PATCH 022/146] Convert encoding when thresholds overflow --- src/t_zset.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/src/t_zset.c b/src/t_zset.c index 25da7871..35d95ba7 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -728,6 +728,85 @@ int zsLength(robj *zobj) { return length; } +void zsConvert(robj *zobj, int encoding) { + zset *zs; + zskiplistNode *node, *next; + robj *ele; + double score; + + if (zobj->encoding == encoding) return; + if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { + unsigned char *zl = zobj->ptr; + unsigned char *eptr, *sptr; + unsigned char *vstr; + unsigned int vlen; + long long vlong; + + if (encoding != REDIS_ENCODING_RAW) + redisPanic("Unknown target encoding"); + + zs = zmalloc(sizeof(*zs)); + zs->dict = dictCreate(&zsetDictType,NULL); + zs->zsl = zslCreate(); + + eptr = ziplistIndex(zl,0); + redisAssert(eptr != NULL); + sptr = ziplistNext(zl,eptr); + redisAssert(sptr != NULL); + + while (eptr != NULL) { + score = zzlGetScore(sptr); + redisAssert(ziplistGet(eptr,&vstr,&vlen,&vlong)); + if (vstr == NULL) + ele = createStringObjectFromLongLong(vlong); + else + ele = createStringObject((char*)vstr,vlen); + + /* Has incremented refcount since it was just created. */ + node = zslInsert(zs->zsl,score,ele); + redisAssert(dictAdd(zs->dict,ele,&node->score) == DICT_OK); + incrRefCount(ele); /* Added to dictionary. */ + zzlNext(zl,&eptr,&sptr); + } + + zfree(zobj->ptr); + zobj->ptr = zs; + zobj->encoding = REDIS_ENCODING_RAW; + } else if (zobj->encoding == REDIS_ENCODING_RAW) { + unsigned char *zl = ziplistNew(); + + if (encoding != REDIS_ENCODING_ZIPLIST) + redisPanic("Unknown target encoding"); + + /* Approach similar to zslFree(), since we want to free the skiplist at + * the same time as creating the ziplist. */ + zs = zobj->ptr; + dictRelease(zs->dict); + node = zs->zsl->header->level[0].forward; + zfree(zs->zsl->header); + zfree(zs->zsl); + + /* Immediately store pointer to ziplist in object because it will + * change because of reallocations when pushing to the ziplist. */ + zobj->ptr = zl; + + while (node) { + ele = getDecodedObject(node->obj); + redisAssert(zzlInsertAt(zobj,ele,node->score,NULL) == REDIS_OK); + decrRefCount(ele); + + next = node->level[0].forward; + zslFreeNode(node); + node = next; + } + + zfree(zs); + zobj->encoding = REDIS_ENCODING_ZIPLIST; + } else { + redisPanic("Unknown sorted set encoding"); + } +} + /*----------------------------------------------------------------------------- * Sorted set commands *----------------------------------------------------------------------------*/ @@ -746,7 +825,13 @@ void zaddGenericCommand(redisClient *c, int incr) { zobj = lookupKeyWrite(c->db,key); if (zobj == NULL) { - zobj = createZsetZiplistObject(); + if (server.zset_max_ziplist_entries == 0 || + server.zset_max_ziplist_value < sdslen(c->argv[3]->ptr)) + { + zobj = createZsetObject(); + } else { + zobj = createZsetZiplistObject(); + } dbAdd(c->db,key,zobj); } else { if (zobj->type != REDIS_ZSET) { @@ -785,7 +870,13 @@ void zaddGenericCommand(redisClient *c, int incr) { else /* ZADD */ addReply(c,shared.czero); } else { + /* Optimize: check if the element is too large or the list becomes + * too long *before* executing zzlInsert. */ redisAssert(zzlInsert(zobj,ele,score) == REDIS_OK); + if (zzlLength(zobj) > server.zset_max_ziplist_entries) + zsConvert(zobj,REDIS_ENCODING_RAW); + if (sdslen(ele->ptr) > server.zset_max_ziplist_value) + zsConvert(zobj,REDIS_ENCODING_RAW); signalModifiedKey(c->db,key); server.dirty++; From cc4c964b33ac108c9541e31eb2e2420addf9a82e Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 9 Mar 2011 16:13:39 +0100 Subject: [PATCH 023/146] Fix used function in ZCARD --- src/t_zset.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/t_zset.c b/src/t_zset.c index 35d95ba7..3c9ede1c 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -451,6 +451,7 @@ int zzlCompareElements(unsigned char *eptr, unsigned char *cstr, unsigned int cl unsigned int zzlLength(robj *zobj) { unsigned char *zl = zobj->ptr; + redisAssert(zobj->encoding == REDIS_ENCODING_ZIPLIST); return ziplistLen(zl)/2; } @@ -1602,7 +1603,7 @@ void zcardCommand(redisClient *c) { if ((zobj = lookupKeyReadOrReply(c,key,shared.czero)) == NULL || checkType(c,zobj,REDIS_ZSET)) return; - addReplyLongLong(c,zzlLength(zobj)); + addReplyLongLong(c,zsLength(zobj)); } void zscoreCommand(redisClient *c) { From d4e07f171472a99a63c47b1fb6ab8a0a071ae945 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Thu, 10 Mar 2011 16:16:27 +0100 Subject: [PATCH 024/146] Add new string to long long function --- src/redis.h | 1 + src/util.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/src/redis.h b/src/redis.h index 9f53583e..e32c1807 100644 --- a/src/redis.h +++ b/src/redis.h @@ -894,6 +894,7 @@ int stringmatchlen(const char *pattern, int patternLen, int stringmatch(const char *pattern, const char *string, int nocase); long long memtoll(const char *p, int *err); int ll2string(char *s, size_t len, long long value); +int string2ll(char *s, size_t len, long long *value); int d2string(char *s, size_t len, double value); int isStringRepresentableAsLong(sds s, long *longval); int isStringRepresentableAsLongLong(sds s, long long *longval); diff --git a/src/util.c b/src/util.c index 5cffa072..599dc690 100644 --- a/src/util.c +++ b/src/util.c @@ -201,6 +201,63 @@ int ll2string(char *s, size_t len, long long value) { return l; } +/* Convert a string into a long long. Returns 1 if the string could be parsed + * into a (non-overflowing) long long, 0 otherwise. The value will be set to + * the parsed value when appropriate. */ +int string2ll(char *s, size_t slen, long long *value) { + char *p = s; + size_t plen = 0; + int negative = 0; + unsigned long long v; + + if (plen == slen) + return 0; + + if (p[0] == '-') { + negative = 1; + p++; plen++; + + /* Abort on only a negative sign. */ + if (plen == slen) + return 0; + } + + /* First digit should be 1-9. */ + if (p[0] >= '1' && p[0] <= '9') { + v = p[0]-'0'; + p++; plen++; + } else { + return 0; + } + + while (plen < slen && p[0] >= '0' && p[0] <= '9') { + if (v > (ULLONG_MAX / 10)) /* Overflow. */ + return 0; + v *= 10; + + if (v > (ULLONG_MAX - (p[0]-'0'))) /* Overflow. */ + return 0; + v += p[0]-'0'; + + p++; plen++; + } + + /* Return if not all bytes were used. */ + if (plen < slen) + return 0; + + if (negative) { + if (v > (-(unsigned long long)LLONG_MIN)) /* Overflow. */ + return 0; + if (value != NULL) *value = -v; + } else { + if (v > LLONG_MAX) /* Overflow. */ + return 0; + if (value != NULL) *value = v; + } + return 1; +} + /* Convert a double to a string representation. Returns the number of bytes * required. The representation should always be parsable by stdtod(3). */ int d2string(char *buf, size_t len, double value) { From bbfe232f607f10655b6f9bf1d8f91830bb3ba413 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Thu, 10 Mar 2011 16:17:14 +0100 Subject: [PATCH 025/146] Make zzlLength take a ziplist argument --- src/t_zset.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/t_zset.c b/src/t_zset.c index 3c9ede1c..0a35f392 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -449,9 +449,7 @@ int zzlCompareElements(unsigned char *eptr, unsigned char *cstr, unsigned int cl return cmp; } -unsigned int zzlLength(robj *zobj) { - unsigned char *zl = zobj->ptr; - redisAssert(zobj->encoding == REDIS_ENCODING_ZIPLIST); +unsigned int zzlLength(unsigned char *zl) { return ziplistLen(zl)/2; } @@ -720,7 +718,7 @@ unsigned long zzlDeleteRangeByRank(robj *zobj, unsigned int start, unsigned int int zsLength(robj *zobj) { int length = -1; if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { - length = zzlLength(zobj); + length = zzlLength(zobj->ptr); } else if (zobj->encoding == REDIS_ENCODING_RAW) { length = ((zset*)zobj->ptr)->zsl->length; } else { @@ -874,7 +872,7 @@ void zaddGenericCommand(redisClient *c, int incr) { /* Optimize: check if the element is too large or the list becomes * too long *before* executing zzlInsert. */ redisAssert(zzlInsert(zobj,ele,score) == REDIS_OK); - if (zzlLength(zobj) > server.zset_max_ziplist_entries) + if (zzlLength(zobj->ptr) > server.zset_max_ziplist_entries) zsConvert(zobj,REDIS_ENCODING_RAW); if (sdslen(ele->ptr) > server.zset_max_ziplist_value) zsConvert(zobj,REDIS_ENCODING_RAW); @@ -965,7 +963,7 @@ void zremCommand(redisClient *c) { if ((eptr = zzlFind(zobj,ele,NULL)) != NULL) { redisAssert(zzlDelete(zobj,eptr) == REDIS_OK); - if (zzlLength(zobj) == 0) dbDelete(c->db,key); + if (zzlLength(zobj->ptr) == 0) dbDelete(c->db,key); } else { addReply(c,shared.czero); return; From 56ce42faf168cafeb9dee681ab269b1fb98b197d Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Thu, 10 Mar 2011 16:34:52 +0100 Subject: [PATCH 026/146] Generic iterator code for usage in ZUNIONSTORE/ZINTERSTORE --- src/t_zset.c | 431 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 374 insertions(+), 57 deletions(-) diff --git a/src/t_zset.c b/src/t_zset.c index 0a35f392..1ef5e991 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -810,6 +810,12 @@ void zsConvert(robj *zobj, int encoding) { * Sorted set commands *----------------------------------------------------------------------------*/ +// if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { +// } else if (zobj->encoding == REDIS_ENCODING_RAW) { +// } else { +// redisPanic("Unknown sorted set encoding"); +// } + /* This generic command implements both ZADD and ZINCRBY. */ void zaddGenericCommand(redisClient *c, int incr) { static char *nanerr = "resulting score is not a number (NaN)"; @@ -1075,16 +1081,327 @@ void zremrangebyrankCommand(redisClient *c) { } typedef struct { - dict *dict; + robj *subject; + int type; /* Set, sorted set */ + int encoding; double weight; + + union { + /* Set iterators. */ + union _iterset { + struct { + intset *is; + int ii; + } is; + struct { + dict *dict; + dictIterator *di; + dictEntry *de; + } ht; + } set; + + /* Sorted set iterators. */ + union _iterzset { + struct { + unsigned char *zl; + unsigned char *eptr, *sptr; + } zl; + struct { + zset *zs; + zskiplistNode *node; + } sl; + } zset; + } iter; } zsetopsrc; -int qsortCompareZsetopsrcByCardinality(const void *s1, const void *s2) { - zsetopsrc *d1 = (void*) s1, *d2 = (void*) s2; - unsigned long size1, size2; - size1 = d1->dict ? dictSize(d1->dict) : 0; - size2 = d2->dict ? dictSize(d2->dict) : 0; - return size1 - size2; + +/* Use dirty flags for pointers that need to be cleaned up in the next + * iteration over the zsetopval. The dirty flag for the long long value is + * special, since long long values don't need cleanup. Instead, it means that + * we already checked that "ell" holds a long long, or tried to convert another + * representation into a long long value. When this was successful, + * OPVAL_VALID_LL is set as well. */ +#define OPVAL_DIRTY_ROBJ 1 +#define OPVAL_DIRTY_LL 2 +#define OPVAL_VALID_LL 4 + +/* Store value retrieved from the iterator. */ +typedef struct { + int flags; + unsigned char _buf[32]; /* Private buffer. */ + robj *ele; + unsigned char *estr; + unsigned int elen; + long long ell; + double score; +} zsetopval; + +typedef union _iterset iterset; +typedef union _iterzset iterzset; + +void zuiInitIterator(zsetopsrc *op) { + if (op->subject == NULL) + return; + + if (op->type == REDIS_SET) { + iterset *it = &op->iter.set; + if (op->encoding == REDIS_ENCODING_INTSET) { + it->is.is = op->subject->ptr; + it->is.ii = 0; + } else if (op->encoding == REDIS_ENCODING_HT) { + it->ht.dict = op->subject->ptr; + it->ht.di = dictGetIterator(op->subject->ptr); + it->ht.de = dictNext(it->ht.di); + } else { + redisPanic("Unknown set encoding"); + } + } else if (op->type == REDIS_ZSET) { + iterzset *it = &op->iter.zset; + if (op->encoding == REDIS_ENCODING_ZIPLIST) { + it->zl.zl = op->subject->ptr; + it->zl.eptr = ziplistIndex(it->zl.zl,0); + if (it->zl.eptr != NULL) { + it->zl.sptr = ziplistNext(it->zl.zl,it->zl.eptr); + redisAssert(it->zl.sptr != NULL); + } + } else if (op->encoding == REDIS_ENCODING_RAW) { + it->sl.zs = op->subject->ptr; + it->sl.node = it->sl.zs->zsl->header->level[0].forward; + } else { + redisPanic("Unknown sorted set encoding"); + } + } else { + redisPanic("Unsupported type"); + } +} + +void zuiClearIterator(zsetopsrc *op) { + if (op->subject == NULL) + return; + + if (op->type == REDIS_SET) { + iterset *it = &op->iter.set; + if (op->encoding == REDIS_ENCODING_INTSET) { + REDIS_NOTUSED(it); /* skip */ + } else if (op->encoding == REDIS_ENCODING_HT) { + dictReleaseIterator(it->ht.di); + } else { + redisPanic("Unknown set encoding"); + } + } else if (op->type == REDIS_ZSET) { + iterzset *it = &op->iter.zset; + if (op->encoding == REDIS_ENCODING_ZIPLIST) { + REDIS_NOTUSED(it); /* skip */ + } else if (op->encoding == REDIS_ENCODING_RAW) { + REDIS_NOTUSED(it); /* skip */ + } else { + redisPanic("Unknown sorted set encoding"); + } + } else { + redisPanic("Unsupported type"); + } +} + +int zuiLength(zsetopsrc *op) { + if (op->subject == NULL) + return 0; + + if (op->type == REDIS_SET) { + iterset *it = &op->iter.set; + if (op->encoding == REDIS_ENCODING_INTSET) { + return intsetLen(it->is.is); + } else if (op->encoding == REDIS_ENCODING_HT) { + return dictSize(it->ht.dict); + } else { + redisPanic("Unknown set encoding"); + } + } else if (op->type == REDIS_ZSET) { + iterzset *it = &op->iter.zset; + if (op->encoding == REDIS_ENCODING_ZIPLIST) { + return zzlLength(it->zl.zl); + } else if (op->encoding == REDIS_ENCODING_RAW) { + return it->sl.zs->zsl->length; + } else { + redisPanic("Unknown sorted set encoding"); + } + } else { + redisPanic("Unsupported type"); + } +} + +/* Check if the current value is valid. If so, store it in the passed structure + * and move to the next element. If not valid, this means we have reached the + * end of the structure and can abort. */ +int zuiNext(zsetopsrc *op, zsetopval *val) { + if (op->subject == NULL) + return 0; + + if (val->flags & OPVAL_DIRTY_ROBJ) + decrRefCount(val->ele); + + bzero(val,sizeof(zsetopval)); + + if (op->type == REDIS_SET) { + iterset *it = &op->iter.set; + if (op->encoding == REDIS_ENCODING_INTSET) { + if (!intsetGet(it->is.is,it->is.ii,&val->ell)) + return 0; + val->score = 1.0; + + /* Move to next element. */ + it->is.ii++; + } else if (op->encoding == REDIS_ENCODING_HT) { + if (it->ht.de == NULL) + return 0; + val->ele = dictGetEntryKey(it->ht.de); + val->score = 1.0; + + /* Move to next element. */ + it->ht.de = dictNext(it->ht.di); + } else { + redisPanic("Unknown set encoding"); + } + } else if (op->type == REDIS_ZSET) { + iterzset *it = &op->iter.zset; + if (op->encoding == REDIS_ENCODING_ZIPLIST) { + /* No need to check both, but better be explicit. */ + if (it->zl.eptr == NULL || it->zl.sptr == NULL) + return 0; + redisAssert(ziplistGet(it->zl.eptr,&val->estr,&val->elen,&val->ell)); + val->score = zzlGetScore(it->zl.sptr); + + /* Move to next element. */ + zzlNext(it->zl.zl,&it->zl.eptr,&it->zl.sptr); + } else if (op->encoding == REDIS_ENCODING_RAW) { + if (it->sl.node == NULL) + return 0; + val->ele = it->sl.node->obj; + val->score = it->sl.node->score; + + /* Move to next element. */ + it->sl.node = it->sl.node->level[0].forward; + } else { + redisPanic("Unknown sorted set encoding"); + } + } else { + redisPanic("Unsupported type"); + } + return 1; +} + +int zuiLongLongFromValue(zsetopval *val) { + if (!(val->flags & OPVAL_DIRTY_LL)) { + val->flags |= OPVAL_DIRTY_LL; + + if (val->ele != NULL) { + if (val->ele->encoding == REDIS_ENCODING_INT) { + val->ell = (long)val->ele->ptr; + val->flags |= OPVAL_VALID_LL; + } else if (val->ele->encoding == REDIS_ENCODING_RAW) { + if (string2ll(val->ele->ptr,sdslen(val->ele->ptr),&val->ell)) + val->flags |= OPVAL_VALID_LL; + } else { + redisPanic("Unsupported element encoding"); + } + } else if (val->estr != NULL) { + if (string2ll((char*)val->estr,val->elen,&val->ell)) + val->flags |= OPVAL_VALID_LL; + } else { + /* The long long was already set, flag as valid. */ + val->flags |= OPVAL_VALID_LL; + } + } + return val->flags & OPVAL_VALID_LL; +} + +robj *zuiObjectFromValue(zsetopval *val) { + if (val->ele == NULL) { + if (val->estr != NULL) { + val->ele = createStringObject((char*)val->estr,val->elen); + } else { + val->ele = createStringObjectFromLongLong(val->ell); + } + val->flags |= OPVAL_DIRTY_ROBJ; + } + return val->ele; +} + +int zuiBufferFromValue(zsetopval *val) { + if (val->estr == NULL) { + if (val->ele != NULL) { + if (val->ele->encoding == REDIS_ENCODING_INT) { + val->elen = ll2string((char*)val->_buf,sizeof(val->_buf),(long)val->ele->ptr); + val->estr = val->_buf; + } else if (val->ele->encoding == REDIS_ENCODING_RAW) { + val->elen = sdslen(val->ele->ptr); + val->estr = val->ele->ptr; + } else { + redisPanic("Unsupported element encoding"); + } + } else { + val->elen = ll2string((char*)val->_buf,sizeof(val->_buf),val->ell); + val->estr = val->_buf; + } + } + return 1; +} + +/* Find value pointed to by val in the source pointer to by op. When found, + * return 1 and store its score in target. Return 0 otherwise. */ +int zuiFind(zsetopsrc *op, zsetopval *val, double *score) { + if (op->subject == NULL) + return 0; + + if (op->type == REDIS_SET) { + iterset *it = &op->iter.set; + + if (op->encoding == REDIS_ENCODING_INTSET) { + if (zuiLongLongFromValue(val) && intsetFind(it->is.is,val->ell)) { + *score = 1.0; + return 1; + } else { + return 0; + } + } else if (op->encoding == REDIS_ENCODING_HT) { + zuiObjectFromValue(val); + if (dictFind(it->ht.dict,val->ele) != NULL) { + *score = 1.0; + return 1; + } else { + return 0; + } + } else { + redisPanic("Unknown set encoding"); + } + } else if (op->type == REDIS_ZSET) { + iterzset *it = &op->iter.zset; + zuiObjectFromValue(val); + + if (op->encoding == REDIS_ENCODING_ZIPLIST) { + if (zzlFind(op->subject,val->ele,score) != NULL) { + /* Score is already set by zzlFind. */ + return 1; + } else { + return 0; + } + } else if (op->encoding == REDIS_ENCODING_RAW) { + dictEntry *de; + if ((de = dictFind(it->sl.zs->dict,val->ele)) != NULL) { + *score = *(double*)dictGetEntryVal(de); + return 1; + } else { + return 0; + } + } else { + redisPanic("Unknown sorted set encoding"); + } + } else { + redisPanic("Unsupported type"); + } +} + +int zuiCompareByCardinality(const void *s1, const void *s2) { + return zuiLength((zsetopsrc*)s1) - zuiLength((zsetopsrc*)s2); } #define REDIS_AGGR_SUM 1 @@ -1113,11 +1430,11 @@ void zunionInterGenericCommand(redisClient *c, robj *dstkey, int op) { int i, j, setnum; int aggregate = REDIS_AGGR_SUM; zsetopsrc *src; + zsetopval zval; + robj *tmp; robj *dstobj; zset *dstzset; zskiplistNode *znode; - dictIterator *di; - dictEntry *de; int touched = 0; /* expect setnum input keys to be given */ @@ -1135,24 +1452,24 @@ void zunionInterGenericCommand(redisClient *c, robj *dstkey, int op) { } /* read keys to be used for input */ - src = zmalloc(sizeof(zsetopsrc) * setnum); + src = zcalloc(sizeof(zsetopsrc) * setnum); for (i = 0, j = 3; i < setnum; i++, j++) { robj *obj = lookupKeyWrite(c->db,c->argv[j]); - if (!obj) { - src[i].dict = NULL; - } else { - if (obj->type == REDIS_ZSET) { - src[i].dict = ((zset*)obj->ptr)->dict; - } else if (obj->type == REDIS_SET) { - src[i].dict = (obj->ptr); - } else { + if (obj != NULL) { + if (obj->type != REDIS_ZSET && obj->type != REDIS_SET) { zfree(src); addReply(c,shared.wrongtypeerr); return; } + + src[i].subject = obj; + src[i].type = obj->type; + src[i].encoding = obj->encoding; + } else { + src[i].subject = NULL; } - /* default all weights to 1 */ + /* Default all weights to 1. */ src[i].weight = 1.0; } @@ -1193,82 +1510,82 @@ void zunionInterGenericCommand(redisClient *c, robj *dstkey, int op) { } } + for (i = 0; i < setnum; i++) + zuiInitIterator(&src[i]); + /* sort sets from the smallest to largest, this will improve our * algorithm's performance */ - qsort(src,setnum,sizeof(zsetopsrc),qsortCompareZsetopsrcByCardinality); + qsort(src,setnum,sizeof(zsetopsrc),zuiCompareByCardinality); dstobj = createZsetObject(); dstzset = dstobj->ptr; if (op == REDIS_OP_INTER) { - /* skip going over all entries if the smallest zset is NULL or empty */ - if (src[0].dict && dictSize(src[0].dict) > 0) { - /* precondition: as src[0].dict is non-empty and the zsets are ordered - * from small to large, all src[i > 0].dict are non-empty too */ - di = dictGetIterator(src[0].dict); - while((de = dictNext(di)) != NULL) { + /* Skip everything if the smallest input is empty. */ + if (zuiLength(&src[0]) > 0) { + /* Precondition: as src[0] is non-empty and the inputs are ordered + * by size, all src[i > 0] are non-empty too. */ + while (zuiNext(&src[0],&zval)) { double score, value; - score = src[0].weight * zunionInterDictValue(de); + score = src[0].weight * zval.score; for (j = 1; j < setnum; j++) { - dictEntry *other = dictFind(src[j].dict,dictGetEntryKey(de)); - if (other) { - value = src[j].weight * zunionInterDictValue(other); + if (zuiFind(&src[j],&zval,&value)) { + value *= src[j].weight; zunionInterAggregate(&score,value,aggregate); } else { break; } } - /* Only continue when present in every source dict. */ + /* Only continue when present in every input. */ if (j == setnum) { - robj *o = dictGetEntryKey(de); - znode = zslInsert(dstzset->zsl,score,o); - incrRefCount(o); /* added to skiplist */ - dictAdd(dstzset->dict,o,&znode->score); - incrRefCount(o); /* added to dictionary */ + tmp = zuiObjectFromValue(&zval); + znode = zslInsert(dstzset->zsl,score,tmp); + incrRefCount(tmp); /* added to skiplist */ + dictAdd(dstzset->dict,tmp,&znode->score); + incrRefCount(tmp); /* added to dictionary */ } } - dictReleaseIterator(di); } } else if (op == REDIS_OP_UNION) { for (i = 0; i < setnum; i++) { - if (!src[i].dict) continue; + if (zuiLength(&src[0]) == 0) + continue; - di = dictGetIterator(src[i].dict); - while((de = dictNext(di)) != NULL) { + while (zuiNext(&src[i],&zval)) { double score, value; - /* skip key when already processed */ - if (dictFind(dstzset->dict,dictGetEntryKey(de)) != NULL) + /* Skip key when already processed */ + if (dictFind(dstzset->dict,zuiObjectFromValue(&zval)) != NULL) continue; - /* initialize score */ - score = src[i].weight * zunionInterDictValue(de); + /* Initialize score */ + score = src[i].weight * zval.score; - /* because the zsets are sorted by size, its only possible - * for sets at larger indices to hold this entry */ + /* Because the inputs are sorted by size, it's only possible + * for sets at larger indices to hold this element. */ for (j = (i+1); j < setnum; j++) { - dictEntry *other = dictFind(src[j].dict,dictGetEntryKey(de)); - if (other) { - value = src[j].weight * zunionInterDictValue(other); + if (zuiFind(&src[j],&zval,&value)) { + value *= src[j].weight; zunionInterAggregate(&score,value,aggregate); } } - robj *o = dictGetEntryKey(de); - znode = zslInsert(dstzset->zsl,score,o); - incrRefCount(o); /* added to skiplist */ - dictAdd(dstzset->dict,o,&znode->score); - incrRefCount(o); /* added to dictionary */ + tmp = zuiObjectFromValue(&zval); + znode = zslInsert(dstzset->zsl,score,tmp); + incrRefCount(zval.ele); /* added to skiplist */ + dictAdd(dstzset->dict,tmp,&znode->score); + incrRefCount(zval.ele); /* added to dictionary */ } - dictReleaseIterator(di); } } else { - /* unknown operator */ - redisAssert(op == REDIS_OP_INTER || op == REDIS_OP_UNION); + redisPanic("Unknown operator"); } + for (i = 0; i < setnum; i++) + zuiClearIterator(&src[i]); + if (dbDelete(c->db,dstkey)) { signalModifiedKey(c->db,dstkey); touched = 1; From dba3a153a7ac9a1ec81e8f3034714d4900235a00 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Thu, 10 Mar 2011 16:53:20 +0100 Subject: [PATCH 027/146] Remove comment --- src/t_zset.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/t_zset.c b/src/t_zset.c index 1ef5e991..b77a3424 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -810,12 +810,6 @@ void zsConvert(robj *zobj, int encoding) { * Sorted set commands *----------------------------------------------------------------------------*/ -// if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { -// } else if (zobj->encoding == REDIS_ENCODING_RAW) { -// } else { -// redisPanic("Unknown sorted set encoding"); -// } - /* This generic command implements both ZADD and ZINCRBY. */ void zaddGenericCommand(redisClient *c, int incr) { static char *nanerr = "resulting score is not a number (NaN)"; From 255eebe22167e00f74e359bc71718225d6bd70c8 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Thu, 10 Mar 2011 17:02:05 +0100 Subject: [PATCH 028/146] Convert encoding of result when in limits --- src/t_zset.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/t_zset.c b/src/t_zset.c index b77a3424..a2882702 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -1426,6 +1426,7 @@ void zunionInterGenericCommand(redisClient *c, robj *dstkey, int op) { zsetopsrc *src; zsetopval zval; robj *tmp; + unsigned int maxelelen = 0; robj *dstobj; zset *dstzset; zskiplistNode *znode; @@ -1539,6 +1540,10 @@ void zunionInterGenericCommand(redisClient *c, robj *dstkey, int op) { incrRefCount(tmp); /* added to skiplist */ dictAdd(dstzset->dict,tmp,&znode->score); incrRefCount(tmp); /* added to dictionary */ + + if (tmp->encoding == REDIS_ENCODING_RAW) + if (sdslen(tmp->ptr) > maxelelen) + maxelelen = sdslen(tmp->ptr); } } } @@ -1571,6 +1576,10 @@ void zunionInterGenericCommand(redisClient *c, robj *dstkey, int op) { incrRefCount(zval.ele); /* added to skiplist */ dictAdd(dstzset->dict,tmp,&znode->score); incrRefCount(zval.ele); /* added to dictionary */ + + if (tmp->encoding == REDIS_ENCODING_RAW) + if (sdslen(tmp->ptr) > maxelelen) + maxelelen = sdslen(tmp->ptr); } } } else { @@ -1586,13 +1595,18 @@ void zunionInterGenericCommand(redisClient *c, robj *dstkey, int op) { server.dirty++; } if (dstzset->zsl->length) { + /* Convert to ziplist when in limits. */ + if (dstzset->zsl->length <= server.zset_max_ziplist_entries && + maxelelen <= server.zset_max_ziplist_value) + zsConvert(dstobj,REDIS_ENCODING_ZIPLIST); + dbAdd(c->db,dstkey,dstobj); - addReplyLongLong(c, dstzset->zsl->length); + addReplyLongLong(c,zsLength(dstobj)); if (!touched) signalModifiedKey(c->db,dstkey); server.dirty++; } else { decrRefCount(dstobj); - addReply(c, shared.czero); + addReply(c,shared.czero); } zfree(src); } From df26a0ae0b419124efad82df148b84c6c6164615 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Thu, 10 Mar 2011 17:50:13 +0100 Subject: [PATCH 029/146] Encode sorted set after loading from dump --- src/rdb.c | 15 +++++++++++++++ src/redis.h | 3 +++ src/t_zset.c | 20 ++++++++++---------- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/rdb.c b/src/rdb.c index 62762bee..d2c902d7 100644 --- a/src/rdb.c +++ b/src/rdb.c @@ -756,11 +756,13 @@ robj *rdbLoadObject(int type, FILE *fp) { } else if (type == REDIS_ZSET) { /* Read list/set value */ size_t zsetlen; + size_t maxelelen = 0; zset *zs; if ((zsetlen = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL; o = createZsetObject(); zs = o->ptr; + /* Load every single element of the list/set */ while(zsetlen--) { robj *ele; @@ -770,10 +772,21 @@ robj *rdbLoadObject(int type, FILE *fp) { if ((ele = rdbLoadEncodedStringObject(fp)) == NULL) return NULL; ele = tryObjectEncoding(ele); if (rdbLoadDoubleValue(fp,&score) == -1) return NULL; + + /* Don't care about integer-encoded strings. */ + if (ele->encoding == REDIS_ENCODING_RAW && + sdslen(ele->ptr) > maxelelen) + maxelelen = sdslen(ele->ptr); + znode = zslInsert(zs->zsl,score,ele); dictAdd(zs->dict,ele,&znode->score); incrRefCount(ele); /* added to skiplist */ } + + /* Convert *after* loading, since sorted sets are not stored ordered. */ + if (zsetLength(o) <= server.zset_max_ziplist_entries && + maxelelen <= server.zset_max_ziplist_value) + zsetConvert(o,REDIS_ENCODING_ZIPLIST); } else if (type == REDIS_HASH) { size_t hashlen; @@ -861,6 +874,8 @@ robj *rdbLoadObject(int type, FILE *fp) { case REDIS_ZSET_ZIPLIST: o->type = REDIS_ZSET; o->encoding = REDIS_ENCODING_ZIPLIST; + if (zsetLength(o) > server.zset_max_ziplist_entries) + zsetConvert(o,REDIS_ENCODING_RAW); break; default: redisPanic("Unknown enoding"); diff --git a/src/redis.h b/src/redis.h index e32c1807..73ac1b60 100644 --- a/src/redis.h +++ b/src/redis.h @@ -799,6 +799,9 @@ void backgroundRewriteDoneHandler(int exitcode, int bysignal); zskiplist *zslCreate(void); void zslFree(zskiplist *zsl); zskiplistNode *zslInsert(zskiplist *zsl, double score, robj *obj); +int zzlInsert(robj *zobj, robj *ele, double score); +unsigned int zsetLength(robj *zobj); +void zsetConvert(robj *zobj, int encoding); /* Core functions */ void freeMemoryIfNeeded(void); diff --git a/src/t_zset.c b/src/t_zset.c index a2882702..93fb93bf 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -715,7 +715,7 @@ unsigned long zzlDeleteRangeByRank(robj *zobj, unsigned int start, unsigned int * Common sorted set API *----------------------------------------------------------------------------*/ -int zsLength(robj *zobj) { +unsigned int zsetLength(robj *zobj) { int length = -1; if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { length = zzlLength(zobj->ptr); @@ -727,7 +727,7 @@ int zsLength(robj *zobj) { return length; } -void zsConvert(robj *zobj, int encoding) { +void zsetConvert(robj *zobj, int encoding) { zset *zs; zskiplistNode *node, *next; robj *ele; @@ -873,9 +873,9 @@ void zaddGenericCommand(redisClient *c, int incr) { * too long *before* executing zzlInsert. */ redisAssert(zzlInsert(zobj,ele,score) == REDIS_OK); if (zzlLength(zobj->ptr) > server.zset_max_ziplist_entries) - zsConvert(zobj,REDIS_ENCODING_RAW); + zsetConvert(zobj,REDIS_ENCODING_RAW); if (sdslen(ele->ptr) > server.zset_max_ziplist_value) - zsConvert(zobj,REDIS_ENCODING_RAW); + zsetConvert(zobj,REDIS_ENCODING_RAW); signalModifiedKey(c->db,key); server.dirty++; @@ -1042,7 +1042,7 @@ void zremrangebyrankCommand(redisClient *c) { checkType(c,zobj,REDIS_ZSET)) return; /* Sanitize indexes. */ - llen = zsLength(zobj); + llen = zsetLength(zobj); if (start < 0) start = llen+start; if (end < 0) end = llen+end; if (start < 0) start = 0; @@ -1598,10 +1598,10 @@ void zunionInterGenericCommand(redisClient *c, robj *dstkey, int op) { /* Convert to ziplist when in limits. */ if (dstzset->zsl->length <= server.zset_max_ziplist_entries && maxelelen <= server.zset_max_ziplist_value) - zsConvert(dstobj,REDIS_ENCODING_ZIPLIST); + zsetConvert(dstobj,REDIS_ENCODING_ZIPLIST); dbAdd(c->db,dstkey,dstobj); - addReplyLongLong(c,zsLength(dstobj)); + addReplyLongLong(c,zsetLength(dstobj)); if (!touched) signalModifiedKey(c->db,dstkey); server.dirty++; } else { @@ -1642,7 +1642,7 @@ void zrangeGenericCommand(redisClient *c, int reverse) { || checkType(c,zobj,REDIS_ZSET)) return; /* Sanitize indexes. */ - llen = zsLength(zobj); + llen = zsetLength(zobj); if (start < 0) start = llen+start; if (end < 0) end = llen+end; if (start < 0) start = 0; @@ -1926,7 +1926,7 @@ void zcardCommand(redisClient *c) { if ((zobj = lookupKeyReadOrReply(c,key,shared.czero)) == NULL || checkType(c,zobj,REDIS_ZSET)) return; - addReplyLongLong(c,zsLength(zobj)); + addReplyLongLong(c,zsetLength(zobj)); } void zscoreCommand(redisClient *c) { @@ -1968,7 +1968,7 @@ void zrankGenericCommand(redisClient *c, int reverse) { if ((zobj = lookupKeyReadOrReply(c,key,shared.nullbulk)) == NULL || checkType(c,zobj,REDIS_ZSET)) return; - llen = zsLength(zobj); + llen = zsetLength(zobj); redisAssert(ele->encoding == REDIS_ENCODING_RAW); if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { From 8588bfa370749b24922c0c8f477c562736626421 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Fri, 11 Mar 2011 17:06:07 +0100 Subject: [PATCH 030/146] Make zzl API unaware of the robj where the ziplist is stored --- src/redis.h | 2 +- src/t_zset.c | 85 +++++++++++++++++++++++----------------------------- 2 files changed, 39 insertions(+), 48 deletions(-) diff --git a/src/redis.h b/src/redis.h index 73ac1b60..8ed7216d 100644 --- a/src/redis.h +++ b/src/redis.h @@ -799,7 +799,7 @@ void backgroundRewriteDoneHandler(int exitcode, int bysignal); zskiplist *zslCreate(void); void zslFree(zskiplist *zsl); zskiplistNode *zslInsert(zskiplist *zsl, double score, robj *obj); -int zzlInsert(robj *zobj, robj *ele, double score); +unsigned char *zzlInsert(unsigned char *zl, robj *ele, double score); unsigned int zsetLength(robj *zobj); void zsetConvert(robj *zobj, int encoding); diff --git a/src/t_zset.c b/src/t_zset.c index 93fb93bf..65c99622 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -519,8 +519,7 @@ int zzlIsInRange(unsigned char *zl, zrangespec *range) { /* Find pointer to the first element contained in the specified range. * Returns NULL when no element is contained in the range. */ -unsigned char *zzlFirstInRange(robj *zobj, zrangespec range) { - unsigned char *zl = zobj->ptr; +unsigned char *zzlFirstInRange(unsigned char *zl, zrangespec range) { unsigned char *eptr = ziplistIndex(zl,0), *sptr; double score; @@ -544,8 +543,7 @@ unsigned char *zzlFirstInRange(robj *zobj, zrangespec range) { /* Find pointer to the last element contained in the specified range. * Returns NULL when no element is contained in the range. */ -unsigned char *zzlLastInRange(robj *zobj, zrangespec range) { - unsigned char *zl = zobj->ptr; +unsigned char *zzlLastInRange(unsigned char *zl, zrangespec range) { unsigned char *eptr = ziplistIndex(zl,-2), *sptr; double score; @@ -572,8 +570,7 @@ unsigned char *zzlLastInRange(robj *zobj, zrangespec range) { return NULL; } -unsigned char *zzlFind(robj *zobj, robj *ele, double *score) { - unsigned char *zl = zobj->ptr; +unsigned char *zzlFind(unsigned char *zl, robj *ele, double *score) { unsigned char *eptr = ziplistIndex(zl,0), *sptr; ele = getDecodedObject(ele); @@ -598,19 +595,16 @@ unsigned char *zzlFind(robj *zobj, robj *ele, double *score) { /* Delete (element,score) pair from ziplist. Use local copy of eptr because we * don't want to modify the one given as argument. */ -int zzlDelete(robj *zobj, unsigned char *eptr) { - unsigned char *zl = zobj->ptr; +unsigned char *zzlDelete(unsigned char *zl, unsigned char *eptr) { unsigned char *p = eptr; /* TODO: add function to ziplist API to delete N elements from offset. */ zl = ziplistDelete(zl,&p); zl = ziplistDelete(zl,&p); - zobj->ptr = zl; - return REDIS_OK; + return zl; } -int zzlInsertAt(robj *zobj, robj *ele, double score, unsigned char *eptr) { - unsigned char *zl = zobj->ptr; +unsigned char *zzlInsertAt(unsigned char *zl, unsigned char *eptr, robj *ele, double score) { unsigned char *sptr; char scorebuf[128]; int scorelen; @@ -632,14 +626,12 @@ int zzlInsertAt(robj *zobj, robj *ele, double score, unsigned char *eptr) { zl = ziplistInsert(zl,sptr,(unsigned char*)scorebuf,scorelen); } - zobj->ptr = zl; - return REDIS_OK; + return zl; } /* Insert (element,score) pair in ziplist. This function assumes the element is * not yet present in the list. */ -int zzlInsert(robj *zobj, robj *ele, double score) { - unsigned char *zl = zobj->ptr; +unsigned char *zzlInsert(unsigned char *zl, robj *ele, double score) { unsigned char *eptr = ziplistIndex(zl,0), *sptr; double s; @@ -653,12 +645,12 @@ int zzlInsert(robj *zobj, robj *ele, double score) { /* First element with score larger than score for element to be * inserted. This means we should take its spot in the list to * maintain ordering. */ - zzlInsertAt(zobj,ele,score,eptr); + zl = zzlInsertAt(zl,eptr,ele,score); break; } else if (s == score) { /* Ensure lexicographical ordering for elements. */ if (zzlCompareElements(eptr,ele->ptr,sdslen(ele->ptr)) > 0) { - zzlInsertAt(zobj,ele,score,eptr); + zl = zzlInsertAt(zl,eptr,ele,score); break; } } @@ -669,21 +661,21 @@ int zzlInsert(robj *zobj, robj *ele, double score) { /* Push on tail of list when it was not yet inserted. */ if (eptr == NULL) - zzlInsertAt(zobj,ele,score,NULL); + zl = zzlInsertAt(zl,NULL,ele,score); decrRefCount(ele); - return REDIS_OK; + return zl; } -unsigned long zzlDeleteRangeByScore(robj *zobj, zrangespec range) { - unsigned char *zl = zobj->ptr; +unsigned char *zzlDeleteRangeByScore(unsigned char *zl, zrangespec range, unsigned long *deleted) { unsigned char *eptr, *sptr; double score; - unsigned long deleted = 0; + unsigned long num = 0; - eptr = zzlFirstInRange(zobj,range); - if (eptr == NULL) return deleted; + if (deleted != NULL) *deleted = 0; + eptr = zzlFirstInRange(zl,range); + if (eptr == NULL) return zl; /* When the tail of the ziplist is deleted, eptr will point to the sentinel * byte and ziplistNext will return NULL. */ @@ -693,22 +685,24 @@ unsigned long zzlDeleteRangeByScore(robj *zobj, zrangespec range) { /* Delete both the element and the score. */ zl = ziplistDelete(zl,&eptr); zl = ziplistDelete(zl,&eptr); - deleted++; + num++; } else { /* No longer in range. */ break; } } - return deleted; + if (deleted != NULL) *deleted = num; + return zl; } /* Delete all the elements with rank between start and end from the skiplist. * Start and end are inclusive. Note that start and end need to be 1-based */ -unsigned long zzlDeleteRangeByRank(robj *zobj, unsigned int start, unsigned int end) { +unsigned char *zzlDeleteRangeByRank(unsigned char *zl, unsigned int start, unsigned int end, unsigned long *deleted) { unsigned int num = (end-start)+1; - zobj->ptr = ziplistDeleteRange(zobj->ptr,2*(start-1),2*num); - return num; + if (deleted) *deleted = num; + zl = ziplistDeleteRange(zl,2*(start-1),2*num); + return zl; } /*----------------------------------------------------------------------------- @@ -785,13 +779,9 @@ void zsetConvert(robj *zobj, int encoding) { zfree(zs->zsl->header); zfree(zs->zsl); - /* Immediately store pointer to ziplist in object because it will - * change because of reallocations when pushing to the ziplist. */ - zobj->ptr = zl; - while (node) { ele = getDecodedObject(node->obj); - redisAssert(zzlInsertAt(zobj,ele,node->score,NULL) == REDIS_OK); + zl = zzlInsertAt(zl,NULL,ele,node->score); decrRefCount(ele); next = node->level[0].forward; @@ -800,6 +790,7 @@ void zsetConvert(robj *zobj, int encoding) { } zfree(zs); + zobj->ptr = zl; zobj->encoding = REDIS_ENCODING_ZIPLIST; } else { redisPanic("Unknown sorted set encoding"); @@ -844,7 +835,7 @@ void zaddGenericCommand(redisClient *c, int incr) { /* Prefer non-encoded element when dealing with ziplists. */ ele = c->argv[3]; - if ((eptr = zzlFind(zobj,ele,&curscore)) != NULL) { + if ((eptr = zzlFind(zobj->ptr,ele,&curscore)) != NULL) { if (incr) { score += curscore; if (isnan(score)) { @@ -857,8 +848,8 @@ void zaddGenericCommand(redisClient *c, int incr) { /* Remove and re-insert when score changed. */ if (score != curscore) { - redisAssert(zzlDelete(zobj,eptr) == REDIS_OK); - redisAssert(zzlInsert(zobj,ele,score) == REDIS_OK); + zobj->ptr = zzlDelete(zobj->ptr,eptr); + zobj->ptr = zzlInsert(zobj->ptr,ele,score); signalModifiedKey(c->db,key); server.dirty++; @@ -871,7 +862,7 @@ void zaddGenericCommand(redisClient *c, int incr) { } else { /* Optimize: check if the element is too large or the list becomes * too long *before* executing zzlInsert. */ - redisAssert(zzlInsert(zobj,ele,score) == REDIS_OK); + zobj->ptr = zzlInsert(zobj->ptr,ele,score); if (zzlLength(zobj->ptr) > server.zset_max_ziplist_entries) zsetConvert(zobj,REDIS_ENCODING_RAW); if (sdslen(ele->ptr) > server.zset_max_ziplist_value) @@ -961,8 +952,8 @@ void zremCommand(redisClient *c) { if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { unsigned char *eptr; - if ((eptr = zzlFind(zobj,ele,NULL)) != NULL) { - redisAssert(zzlDelete(zobj,eptr) == REDIS_OK); + if ((eptr = zzlFind(zobj->ptr,ele,NULL)) != NULL) { + zobj->ptr = zzlDelete(zobj->ptr,eptr); if (zzlLength(zobj->ptr) == 0) dbDelete(c->db,key); } else { addReply(c,shared.czero); @@ -1012,7 +1003,7 @@ void zremrangebyscoreCommand(redisClient *c) { checkType(c,zobj,REDIS_ZSET)) return; if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { - deleted = zzlDeleteRangeByScore(zobj,range); + zobj->ptr = zzlDeleteRangeByScore(zobj->ptr,range,&deleted); } else if (zobj->encoding == REDIS_ENCODING_RAW) { zset *zs = zobj->ptr; deleted = zslDeleteRangeByScore(zs->zsl,range,zs->dict); @@ -1057,7 +1048,7 @@ void zremrangebyrankCommand(redisClient *c) { if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { /* Correct for 1-based rank. */ - deleted = zzlDeleteRangeByRank(zobj,start+1,end+1); + zobj->ptr = zzlDeleteRangeByRank(zobj->ptr,start+1,end+1,&deleted); } else if (zobj->encoding == REDIS_ENCODING_RAW) { zset *zs = zobj->ptr; @@ -1372,7 +1363,7 @@ int zuiFind(zsetopsrc *op, zsetopval *val, double *score) { zuiObjectFromValue(val); if (op->encoding == REDIS_ENCODING_ZIPLIST) { - if (zzlFind(op->subject,val->ele,score) != NULL) { + if (zzlFind(it->zl.zl,val->ele,score) != NULL) { /* Score is already set by zzlFind. */ return 1; } else { @@ -1791,9 +1782,9 @@ void genericZrangebyscoreCommand(redisClient *c, int reverse, int justcount) { /* If reversed, get the last node in range as starting point. */ if (reverse) - eptr = zzlLastInRange(zobj,range); + eptr = zzlLastInRange(zl,range); else - eptr = zzlFirstInRange(zobj,range); + eptr = zzlFirstInRange(zl,range); /* No "first" element in the specified interval. */ if (eptr == NULL) { @@ -1938,7 +1929,7 @@ void zscoreCommand(redisClient *c) { checkType(c,zobj,REDIS_ZSET)) return; if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { - if (zzlFind(zobj,c->argv[2],&score) != NULL) + if (zzlFind(zobj->ptr,c->argv[2],&score) != NULL) addReplyDouble(c,score); else addReply(c,shared.nullbulk); From e53ca04b50b86ef158a75c54ae9ee8b17e31719c Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Fri, 11 Mar 2011 18:17:53 +0100 Subject: [PATCH 031/146] Test for empty inner range when looking for elements in range --- src/t_zset.c | 32 ++++++++++++++++++++++---------- tests/unit/type/zset.tcl | 6 ++++++ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/t_zset.c b/src/t_zset.c index 65c99622..b8a961eb 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -226,10 +226,12 @@ zskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec range) { x = x->level[i].forward; } - /* The tail is in range, so the previous block should always return a - * node that is non-NULL and the last one to be out of range. */ + /* This is an inner range, so the next node cannot be NULL. */ x = x->level[0].forward; - redisAssert(x != NULL && zslValueInRange(x->score,&range)); + redisAssert(x != NULL); + + /* Check if score <= max. */ + if (!zslValueLteMax(x->score,&range)) return NULL; return x; } @@ -250,9 +252,11 @@ zskiplistNode *zslLastInRange(zskiplist *zsl, zrangespec range) { x = x->level[i].forward; } - /* The header is in range, so the previous block should always return a - * node that is non-NULL and in range. */ - redisAssert(x != NULL && zslValueInRange(x->score,&range)); + /* This is an inner range, so this node cannot be NULL. */ + redisAssert(x != NULL); + + /* Check if score >= min. */ + if (!zslValueGteMin(x->score,&range)) return NULL; return x; } @@ -531,8 +535,12 @@ unsigned char *zzlFirstInRange(unsigned char *zl, zrangespec range) { redisAssert(sptr != NULL); score = zzlGetScore(sptr); - if (zslValueGteMin(score,&range)) - return eptr; + if (zslValueGteMin(score,&range)) { + /* Check if score <= max. */ + if (zslValueLteMax(score,&range)) + return eptr; + return NULL; + } /* Move to next element. */ eptr = ziplistNext(zl,sptr); @@ -555,8 +563,12 @@ unsigned char *zzlLastInRange(unsigned char *zl, zrangespec range) { redisAssert(sptr != NULL); score = zzlGetScore(sptr); - if (zslValueLteMax(score,&range)) - return eptr; + if (zslValueLteMax(score,&range)) { + /* Check if score >= min. */ + if (zslValueGteMin(score,&range)) + return eptr; + return NULL; + } /* Move to previous element by moving to the score of previous element. * When this returns NULL, we know there also is no element. */ diff --git a/tests/unit/type/zset.tcl b/tests/unit/type/zset.tcl index fdeebd2f..6218a95c 100644 --- a/tests/unit/type/zset.tcl +++ b/tests/unit/type/zset.tcl @@ -247,6 +247,12 @@ start_server {tags {"zset"}} { assert_equal {} [r zrangebyscore zset (-inf (-6] assert_equal {} [r zrevrangebyscore zset (+inf (6] assert_equal {} [r zrevrangebyscore zset (-6 (-inf] + + # empty inner range + assert_equal {} [r zrangebyscore zset 2.4 2.6] + assert_equal {} [r zrangebyscore zset (2.4 2.6] + assert_equal {} [r zrangebyscore zset 2.4 (2.6] + assert_equal {} [r zrangebyscore zset (2.4 (2.6] } test "ZRANGEBYSCORE with WITHSCORES" { From 72690afdd26b118589ddec3915cdb4790b9d4812 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Sat, 12 Mar 2011 14:48:29 +0100 Subject: [PATCH 032/146] Remove unused function --- src/t_zset.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/t_zset.c b/src/t_zset.c index b8a961eb..63657890 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -188,10 +188,6 @@ static int zslValueLteMax(double value, zrangespec *spec) { return spec->maxex ? (value < spec->max) : (value <= spec->max); } -static int zslValueInRange(double value, zrangespec *spec) { - return zslValueGteMin(value,spec) && zslValueLteMax(value,spec); -} - /* Returns if there is a part of the zset is in range. */ int zslIsInRange(zskiplist *zsl, zrangespec *range) { zskiplistNode *x; From 69298a05eb23cbbf60f9008faa2e11866ab4352a Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Sun, 13 Mar 2011 18:15:57 +0100 Subject: [PATCH 033/146] Offset should be size_t --- src/t_zset.c | 2 +- src/ziplist.c | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/t_zset.c b/src/t_zset.c index 63657890..03a79ce9 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -616,7 +616,7 @@ unsigned char *zzlInsertAt(unsigned char *zl, unsigned char *eptr, robj *ele, do unsigned char *sptr; char scorebuf[128]; int scorelen; - int offset; + size_t offset; redisAssert(ele->encoding == REDIS_ENCODING_RAW); scorelen = d2string(scorebuf,sizeof(scorebuf),score); diff --git a/src/ziplist.c b/src/ziplist.c index 524f7238..44d63c78 100644 --- a/src/ziplist.c +++ b/src/ziplist.c @@ -375,8 +375,8 @@ static unsigned char *ziplistResize(unsigned char *zl, unsigned int len) { * The pointer "p" points to the first entry that does NOT need to be * updated, i.e. consecutive fields MAY need an update. */ static unsigned char *__ziplistCascadeUpdate(unsigned char *zl, unsigned char *p) { - unsigned int curlen = ZIPLIST_BYTES(zl), rawlen, rawlensize; - unsigned int offset, noffset, extra; + size_t curlen = ZIPLIST_BYTES(zl), rawlen, rawlensize; + size_t offset, noffset, extra; unsigned char *np; zlentry cur, next; @@ -431,7 +431,8 @@ static unsigned char *__ziplistCascadeUpdate(unsigned char *zl, unsigned char *p /* Delete "num" entries, starting at "p". Returns pointer to the ziplist. */ static unsigned char *__ziplistDelete(unsigned char *zl, unsigned char *p, unsigned int num) { unsigned int i, totlen, deleted = 0; - int offset, nextdiff = 0; + size_t offset; + int nextdiff = 0; zlentry first, tail; first = zipEntry(p); @@ -483,8 +484,9 @@ static unsigned char *__ziplistDelete(unsigned char *zl, unsigned char *p, unsig /* Insert item at "p". */ static unsigned char *__ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) { - unsigned int curlen = ZIPLIST_BYTES(zl), reqlen, prevlen = 0; - unsigned int offset, nextdiff = 0; + size_t curlen = ZIPLIST_BYTES(zl), reqlen, prevlen = 0; + size_t offset; + int nextdiff = 0; unsigned char encoding = 0; long long value; zlentry entry, tail; @@ -668,7 +670,7 @@ unsigned char *ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char * Also update *p in place, to be able to iterate over the * ziplist, while deleting entries. */ unsigned char *ziplistDelete(unsigned char *zl, unsigned char **p) { - unsigned int offset = *p-zl; + size_t offset = *p-zl; zl = __ziplistDelete(zl,*p,1); /* Store pointer to current element in p, because ziplistDelete will From 9ec4ea20a71f075a7fc9a2e1d613c339353ee553 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Mon, 14 Mar 2011 10:50:35 +0100 Subject: [PATCH 034/146] Test both sorted set encodings for every test --- tests/unit/type/zset.tcl | 999 +++++++++++++++++++++------------------ 1 file changed, 530 insertions(+), 469 deletions(-) diff --git a/tests/unit/type/zset.tcl b/tests/unit/type/zset.tcl index 6218a95c..31604412 100644 --- a/tests/unit/type/zset.tcl +++ b/tests/unit/type/zset.tcl @@ -6,292 +6,540 @@ start_server {tags {"zset"}} { } } - test {ZSET basic ZADD and score update} { - r zadd ztmp 10 x - r zadd ztmp 20 y - r zadd ztmp 30 z - set aux1 [r zrange ztmp 0 -1] - r zadd ztmp 1 y - set aux2 [r zrange ztmp 0 -1] - list $aux1 $aux2 - } {{x y z} {y x z}} - - test {ZCARD basics} { - r zcard ztmp - } {3} - - test {ZCARD non existing key} { - r zcard ztmp-blabla - } {0} - - test "ZRANGE basics" { - r del ztmp - r zadd ztmp 1 a - r zadd ztmp 2 b - r zadd ztmp 3 c - r zadd ztmp 4 d - - assert_equal {a b c d} [r zrange ztmp 0 -1] - assert_equal {a b c} [r zrange ztmp 0 -2] - assert_equal {b c d} [r zrange ztmp 1 -1] - assert_equal {b c} [r zrange ztmp 1 -2] - assert_equal {c d} [r zrange ztmp -2 -1] - assert_equal {c} [r zrange ztmp -2 -2] - - # out of range start index - assert_equal {a b c} [r zrange ztmp -5 2] - assert_equal {a b} [r zrange ztmp -5 1] - assert_equal {} [r zrange ztmp 5 -1] - assert_equal {} [r zrange ztmp 5 -2] - - # out of range end index - assert_equal {a b c d} [r zrange ztmp 0 5] - assert_equal {b c d} [r zrange ztmp 1 5] - assert_equal {} [r zrange ztmp 0 -5] - assert_equal {} [r zrange ztmp 1 -5] - - # withscores - assert_equal {a 1 b 2 c 3 d 4} [r zrange ztmp 0 -1 withscores] - } - - test "ZREVRANGE basics" { - r del ztmp - r zadd ztmp 1 a - r zadd ztmp 2 b - r zadd ztmp 3 c - r zadd ztmp 4 d - - assert_equal {d c b a} [r zrevrange ztmp 0 -1] - assert_equal {d c b} [r zrevrange ztmp 0 -2] - assert_equal {c b a} [r zrevrange ztmp 1 -1] - assert_equal {c b} [r zrevrange ztmp 1 -2] - assert_equal {b a} [r zrevrange ztmp -2 -1] - assert_equal {b} [r zrevrange ztmp -2 -2] - - # out of range start index - assert_equal {d c b} [r zrevrange ztmp -5 2] - assert_equal {d c} [r zrevrange ztmp -5 1] - assert_equal {} [r zrevrange ztmp 5 -1] - assert_equal {} [r zrevrange ztmp 5 -2] - - # out of range end index - assert_equal {d c b a} [r zrevrange ztmp 0 5] - assert_equal {c b a} [r zrevrange ztmp 1 5] - assert_equal {} [r zrevrange ztmp 0 -5] - assert_equal {} [r zrevrange ztmp 1 -5] - - # withscores - assert_equal {d 4 c 3 b 2 a 1} [r zrevrange ztmp 0 -1 withscores] - } - - test {ZRANK basics} { - r zadd zranktmp 10 x - r zadd zranktmp 20 y - r zadd zranktmp 30 z - list [r zrank zranktmp x] [r zrank zranktmp y] [r zrank zranktmp z] - } {0 1 2} - - test {ZREVRANK basics} { - list [r zrevrank zranktmp x] [r zrevrank zranktmp y] [r zrevrank zranktmp z] - } {2 1 0} - - test {ZRANK - after deletion} { - r zrem zranktmp y - list [r zrank zranktmp x] [r zrank zranktmp z] - } {0 1} - - test {ZSCORE} { - set aux {} - set err {} - for {set i 0} {$i < 1000} {incr i} { - set score [expr rand()] - lappend aux $score - r zadd zscoretest $score $i + proc basics {encoding} { + if {$encoding == "ziplist"} { + r config set zset-max-ziplist-entries 128 + r config set zset-max-ziplist-value 64 + } elseif {$encoding == "raw"} { + r config set zset-max-ziplist-entries 0 + r config set zset-max-ziplist-value 0 + } else { + puts "Unknown sorted set encoding" + exit } - for {set i 0} {$i < 1000} {incr i} { - if {[r zscore zscoretest $i] != [lindex $aux $i]} { - set err "Expected score was [lindex $aux $i] but got [r zscore zscoretest $i] for element $i" - break + + test "Check encoding - $encoding" { + r del ztmp + r zadd ztmp 10 x + assert_encoding $encoding ztmp + } + + test "ZSET basic ZADD and score update - $encoding" { + r del ztmp + r zadd ztmp 10 x + r zadd ztmp 20 y + r zadd ztmp 30 z + assert_equal {x y z} [r zrange ztmp 0 -1] + + r zadd ztmp 1 y + assert_equal {y x z} [r zrange ztmp 0 -1] + } + + test "ZSET element can't be set to NaN with ZADD - $encoding" { + assert_error "*not a double*" {r zadd myzset nan abc} + } + + test "ZSET element can't be set to NaN with ZINCRBY" { + assert_error "*not a double*" {r zadd myzset nan abc} + } + + test "ZINCRBY calls leading to NaN result in error" { + r zincrby myzset +inf abc + assert_error "*NaN*" {r zincrby myzset -inf abc} + } + + test "ZCARD basics - $encoding" { + assert_equal 3 [r zcard ztmp] + assert_equal 0 [r zcard zdoesntexist] + } + + test "ZRANGE basics - $encoding" { + r del ztmp + r zadd ztmp 1 a + r zadd ztmp 2 b + r zadd ztmp 3 c + r zadd ztmp 4 d + + assert_equal {a b c d} [r zrange ztmp 0 -1] + assert_equal {a b c} [r zrange ztmp 0 -2] + assert_equal {b c d} [r zrange ztmp 1 -1] + assert_equal {b c} [r zrange ztmp 1 -2] + assert_equal {c d} [r zrange ztmp -2 -1] + assert_equal {c} [r zrange ztmp -2 -2] + + # out of range start index + assert_equal {a b c} [r zrange ztmp -5 2] + assert_equal {a b} [r zrange ztmp -5 1] + assert_equal {} [r zrange ztmp 5 -1] + assert_equal {} [r zrange ztmp 5 -2] + + # out of range end index + assert_equal {a b c d} [r zrange ztmp 0 5] + assert_equal {b c d} [r zrange ztmp 1 5] + assert_equal {} [r zrange ztmp 0 -5] + assert_equal {} [r zrange ztmp 1 -5] + + # withscores + assert_equal {a 1 b 2 c 3 d 4} [r zrange ztmp 0 -1 withscores] + } + + test "ZREVRANGE basics - $encoding" { + r del ztmp + r zadd ztmp 1 a + r zadd ztmp 2 b + r zadd ztmp 3 c + r zadd ztmp 4 d + + assert_equal {d c b a} [r zrevrange ztmp 0 -1] + assert_equal {d c b} [r zrevrange ztmp 0 -2] + assert_equal {c b a} [r zrevrange ztmp 1 -1] + assert_equal {c b} [r zrevrange ztmp 1 -2] + assert_equal {b a} [r zrevrange ztmp -2 -1] + assert_equal {b} [r zrevrange ztmp -2 -2] + + # out of range start index + assert_equal {d c b} [r zrevrange ztmp -5 2] + assert_equal {d c} [r zrevrange ztmp -5 1] + assert_equal {} [r zrevrange ztmp 5 -1] + assert_equal {} [r zrevrange ztmp 5 -2] + + # out of range end index + assert_equal {d c b a} [r zrevrange ztmp 0 5] + assert_equal {c b a} [r zrevrange ztmp 1 5] + assert_equal {} [r zrevrange ztmp 0 -5] + assert_equal {} [r zrevrange ztmp 1 -5] + + # withscores + assert_equal {d 4 c 3 b 2 a 1} [r zrevrange ztmp 0 -1 withscores] + } + + test "ZRANK/ZREVRANK basics - $encoding" { + r del zranktmp + r zadd zranktmp 10 x + r zadd zranktmp 20 y + r zadd zranktmp 30 z + assert_equal 0 [r zrank zranktmp x] + assert_equal 1 [r zrank zranktmp y] + assert_equal 2 [r zrank zranktmp z] + assert_equal "" [r zrank zranktmp foo] + assert_equal 2 [r zrevrank zranktmp x] + assert_equal 1 [r zrevrank zranktmp y] + assert_equal 0 [r zrevrank zranktmp z] + assert_equal "" [r zrevrank zranktmp foo] + } + + test "ZRANK - after deletion - $encoding" { + r zrem zranktmp y + assert_equal 0 [r zrank zranktmp x] + assert_equal 1 [r zrank zranktmp z] + } + + test "ZINCRBY - can create a new sorted set - $encoding" { + r del zset + r zincrby zset 1 foo + assert_equal {foo} [r zrange zset 0 -1] + assert_equal 1 [r zscore zset foo] + } + + test "ZINCRBY - increment and decrement - $encoding" { + r zincrby zset 2 foo + r zincrby zset 1 bar + assert_equal {bar foo} [r zrange zset 0 -1] + + r zincrby zset 10 bar + r zincrby zset -5 foo + r zincrby zset -5 bar + assert_equal {foo bar} [r zrange zset 0 -1] + + assert_equal -2 [r zscore zset foo] + assert_equal 6 [r zscore zset bar] + } + + proc create_default_zset {} { + create_zset zset {-inf a 1 b 2 c 3 d 4 e 5 f +inf g} + } + + test "ZRANGEBYSCORE/ZREVRANGEBYSCORE/ZCOUNT basics" { + create_default_zset + + # inclusive range + assert_equal {a b c} [r zrangebyscore zset -inf 2] + assert_equal {b c d} [r zrangebyscore zset 0 3] + assert_equal {d e f} [r zrangebyscore zset 3 6] + assert_equal {e f g} [r zrangebyscore zset 4 +inf] + assert_equal {c b a} [r zrevrangebyscore zset 2 -inf] + assert_equal {d c b} [r zrevrangebyscore zset 3 0] + assert_equal {f e d} [r zrevrangebyscore zset 6 3] + assert_equal {g f e} [r zrevrangebyscore zset +inf 4] + assert_equal 3 [r zcount zset 0 3] + + # exclusive range + assert_equal {b} [r zrangebyscore zset (-inf (2] + assert_equal {b c} [r zrangebyscore zset (0 (3] + assert_equal {e f} [r zrangebyscore zset (3 (6] + assert_equal {f} [r zrangebyscore zset (4 (+inf] + assert_equal {b} [r zrevrangebyscore zset (2 (-inf] + assert_equal {c b} [r zrevrangebyscore zset (3 (0] + assert_equal {f e} [r zrevrangebyscore zset (6 (3] + assert_equal {f} [r zrevrangebyscore zset (+inf (4] + assert_equal 2 [r zcount zset (0 (3] + + # test empty ranges + r zrem zset a + r zrem zset g + + # inclusive + assert_equal {} [r zrangebyscore zset 4 2] + assert_equal {} [r zrangebyscore zset 6 +inf] + assert_equal {} [r zrangebyscore zset -inf -6] + assert_equal {} [r zrevrangebyscore zset +inf 6] + assert_equal {} [r zrevrangebyscore zset -6 -inf] + + # exclusive + assert_equal {} [r zrangebyscore zset (4 (2] + assert_equal {} [r zrangebyscore zset 2 (2] + assert_equal {} [r zrangebyscore zset (2 2] + assert_equal {} [r zrangebyscore zset (6 (+inf] + assert_equal {} [r zrangebyscore zset (-inf (-6] + assert_equal {} [r zrevrangebyscore zset (+inf (6] + assert_equal {} [r zrevrangebyscore zset (-6 (-inf] + + # empty inner range + assert_equal {} [r zrangebyscore zset 2.4 2.6] + assert_equal {} [r zrangebyscore zset (2.4 2.6] + assert_equal {} [r zrangebyscore zset 2.4 (2.6] + assert_equal {} [r zrangebyscore zset (2.4 (2.6] + } + + test "ZRANGEBYSCORE with WITHSCORES" { + create_default_zset + assert_equal {b 1 c 2 d 3} [r zrangebyscore zset 0 3 withscores] + assert_equal {d 3 c 2 b 1} [r zrevrangebyscore zset 3 0 withscores] + } + + test "ZRANGEBYSCORE with LIMIT" { + create_default_zset + assert_equal {b c} [r zrangebyscore zset 0 10 LIMIT 0 2] + assert_equal {d e f} [r zrangebyscore zset 0 10 LIMIT 2 3] + assert_equal {d e f} [r zrangebyscore zset 0 10 LIMIT 2 10] + assert_equal {} [r zrangebyscore zset 0 10 LIMIT 20 10] + assert_equal {f e} [r zrevrangebyscore zset 10 0 LIMIT 0 2] + assert_equal {d c b} [r zrevrangebyscore zset 10 0 LIMIT 2 3] + assert_equal {d c b} [r zrevrangebyscore zset 10 0 LIMIT 2 10] + assert_equal {} [r zrevrangebyscore zset 10 0 LIMIT 20 10] + } + + test "ZRANGEBYSCORE with LIMIT and WITHSCORES" { + create_default_zset + assert_equal {e 4 f 5} [r zrangebyscore zset 2 5 LIMIT 2 3 WITHSCORES] + assert_equal {d 3 c 2} [r zrevrangebyscore zset 5 2 LIMIT 2 3 WITHSCORES] + } + + test "ZRANGEBYSCORE with non-value min or max" { + assert_error "*not a double*" {r zrangebyscore fooz str 1} + assert_error "*not a double*" {r zrangebyscore fooz 1 str} + assert_error "*not a double*" {r zrangebyscore fooz 1 NaN} + } + + test "ZREMRANGEBYSCORE basics" { + proc remrangebyscore {min max} { + create_zset zset {1 a 2 b 3 c 4 d 5 e} + r zremrangebyscore zset $min $max } - } - set _ $err - } {} - test {ZSCORE after a DEBUG RELOAD} { - set aux {} - set err {} - r del zscoretest - for {set i 0} {$i < 1000} {incr i} { - set score [expr rand()] - lappend aux $score - r zadd zscoretest $score $i + # inner range + assert_equal 3 [remrangebyscore 2 4] + assert_equal {a e} [r zrange zset 0 -1] + + # start underflow + assert_equal 1 [remrangebyscore -10 1] + assert_equal {b c d e} [r zrange zset 0 -1] + + # end overflow + assert_equal 1 [remrangebyscore 5 10] + assert_equal {a b c d} [r zrange zset 0 -1] + + # switch min and max + assert_equal 0 [remrangebyscore 4 2] + assert_equal {a b c d e} [r zrange zset 0 -1] + + # -inf to mid + assert_equal 3 [remrangebyscore -inf 3] + assert_equal {d e} [r zrange zset 0 -1] + + # mid to +inf + assert_equal 3 [remrangebyscore 3 +inf] + assert_equal {a b} [r zrange zset 0 -1] + + # -inf to +inf + assert_equal 5 [remrangebyscore -inf +inf] + assert_equal {} [r zrange zset 0 -1] + + # exclusive min + assert_equal 4 [remrangebyscore (1 5] + assert_equal {a} [r zrange zset 0 -1] + assert_equal 3 [remrangebyscore (2 5] + assert_equal {a b} [r zrange zset 0 -1] + + # exclusive max + assert_equal 4 [remrangebyscore 1 (5] + assert_equal {e} [r zrange zset 0 -1] + assert_equal 3 [remrangebyscore 1 (4] + assert_equal {d e} [r zrange zset 0 -1] + + # exclusive min and max + assert_equal 3 [remrangebyscore (1 (5] + assert_equal {a e} [r zrange zset 0 -1] } - r debug reload - for {set i 0} {$i < 1000} {incr i} { - if {[r zscore zscoretest $i] != [lindex $aux $i]} { - set err "Expected score was [lindex $aux $i] but got [r zscore zscoretest $i] for element $i" - break + + test "ZREMRANGEBYSCORE with non-value min or max" { + assert_error "*not a double*" {r zremrangebyscore fooz str 1} + assert_error "*not a double*" {r zremrangebyscore fooz 1 str} + assert_error "*not a double*" {r zremrangebyscore fooz 1 NaN} + } + + test "ZREMRANGEBYRANK basics" { + proc remrangebyrank {min max} { + create_zset zset {1 a 2 b 3 c 4 d 5 e} + r zremrangebyrank zset $min $max } - } - set _ $err - } {} - test {ZSETs stress tester - sorting is working well?} { - set delta 0 - for {set test 0} {$test < 2} {incr test} { - unset -nocomplain auxarray - array set auxarray {} - set auxlist {} - r del myzset - for {set i 0} {$i < 1000} {incr i} { - if {$test == 0} { - set score [expr rand()] - } else { - set score [expr int(rand()*10)] + # inner range + assert_equal 3 [remrangebyrank 1 3] + assert_equal {a e} [r zrange zset 0 -1] + + # start underflow + assert_equal 1 [remrangebyrank -10 0] + assert_equal {b c d e} [r zrange zset 0 -1] + + # start overflow + assert_equal 0 [remrangebyrank 10 -1] + assert_equal {a b c d e} [r zrange zset 0 -1] + + # end underflow + assert_equal 0 [remrangebyrank 0 -10] + assert_equal {a b c d e} [r zrange zset 0 -1] + + # end overflow + assert_equal 5 [remrangebyrank 0 10] + assert_equal {} [r zrange zset 0 -1] + } + + test "ZUNIONSTORE against non-existing key doesn't set destination - $encoding" { + r del zseta + assert_equal 0 [r zunionstore dst_key 1 zseta] + assert_equal 0 [r exists dst_key] + } + + test "ZUNIONSTORE basics - $encoding" { + r del zseta zsetb zsetc + r zadd zseta 1 a + r zadd zseta 2 b + r zadd zseta 3 c + r zadd zsetb 1 b + r zadd zsetb 2 c + r zadd zsetb 3 d + + assert_equal 4 [r zunionstore zsetc 2 zseta zsetb] + assert_equal {a 1 b 3 d 3 c 5} [r zrange zsetc 0 -1 withscores] + } + + test "ZUNIONSTORE with weights - $encoding" { + assert_equal 4 [r zunionstore zsetc 2 zseta zsetb weights 2 3] + assert_equal {a 2 b 7 d 9 c 12} [r zrange zsetc 0 -1 withscores] + } + + test "ZUNIONSTORE with a regular set and weights - $encoding" { + r del seta + r sadd seta a + r sadd seta b + r sadd seta c + + assert_equal 4 [r zunionstore zsetc 2 seta zsetb weights 2 3] + assert_equal {a 2 b 5 c 8 d 9} [r zrange zsetc 0 -1 withscores] + } + + test "ZUNIONSTORE with AGGREGATE MIN - $encoding" { + assert_equal 4 [r zunionstore zsetc 2 zseta zsetb aggregate min] + assert_equal {a 1 b 1 c 2 d 3} [r zrange zsetc 0 -1 withscores] + } + + test "ZUNIONSTORE with AGGREGATE MAX - $encoding" { + assert_equal 4 [r zunionstore zsetc 2 zseta zsetb aggregate max] + assert_equal {a 1 b 2 c 3 d 3} [r zrange zsetc 0 -1 withscores] + } + + test "ZINTERSTORE basics - $encoding" { + assert_equal 2 [r zinterstore zsetc 2 zseta zsetb] + assert_equal {b 3 c 5} [r zrange zsetc 0 -1 withscores] + } + + test "ZINTERSTORE with weights - $encoding" { + assert_equal 2 [r zinterstore zsetc 2 zseta zsetb weights 2 3] + assert_equal {b 7 c 12} [r zrange zsetc 0 -1 withscores] + } + + test "ZINTERSTORE with a regular set and weights - $encoding" { + r del seta + r sadd seta a + r sadd seta b + r sadd seta c + assert_equal 2 [r zinterstore zsetc 2 seta zsetb weights 2 3] + assert_equal {b 5 c 8} [r zrange zsetc 0 -1 withscores] + } + + test "ZINTERSTORE with AGGREGATE MIN - $encoding" { + assert_equal 2 [r zinterstore zsetc 2 zseta zsetb aggregate min] + assert_equal {b 1 c 2} [r zrange zsetc 0 -1 withscores] + } + + test "ZINTERSTORE with AGGREGATE MAX - $encoding" { + assert_equal 2 [r zinterstore zsetc 2 zseta zsetb aggregate max] + assert_equal {b 2 c 3} [r zrange zsetc 0 -1 withscores] + } + + foreach cmd {ZUNIONSTORE ZINTERSTORE} { + test "$cmd with +inf/-inf scores - $encoding" { + r del zsetinf1 zsetinf2 + + r zadd zsetinf1 +inf key + r zadd zsetinf2 +inf key + r $cmd zsetinf3 2 zsetinf1 zsetinf2 + assert_equal inf [r zscore zsetinf3 key] + + r zadd zsetinf1 -inf key + r zadd zsetinf2 +inf key + r $cmd zsetinf3 2 zsetinf1 zsetinf2 + assert_equal 0 [r zscore zsetinf3 key] + + r zadd zsetinf1 +inf key + r zadd zsetinf2 -inf key + r $cmd zsetinf3 2 zsetinf1 zsetinf2 + assert_equal 0 [r zscore zsetinf3 key] + + r zadd zsetinf1 -inf key + r zadd zsetinf2 -inf key + r $cmd zsetinf3 2 zsetinf1 zsetinf2 + assert_equal -inf [r zscore zsetinf3 key] + } + + test "$cmd with NaN weights $encoding" { + r del zsetinf1 zsetinf2 + + r zadd zsetinf1 1.0 key + r zadd zsetinf2 1.0 key + assert_error "*weight value is not a double*" { + r $cmd zsetinf3 2 zsetinf1 zsetinf2 weights nan nan } - set auxarray($i) $score - r zadd myzset $score $i - # Random update - if {[expr rand()] < .2} { - set j [expr int(rand()*1000)] + } + } + } + + basics ziplist + basics raw + + proc stressers {encoding} { + if {$encoding == "ziplist"} { + # Little extra to allow proper fuzzing in the sorting stresser + r config set zset-max-ziplist-entries 256 + r config set zset-max-ziplist-value 64 + set elements 128 + } elseif {$encoding == "raw"} { + r config set zset-max-ziplist-entries 0 + r config set zset-max-ziplist-value 0 + set elements 1000 + } else { + puts "Unknown sorted set encoding" + exit + } + + test "ZSCORE - $encoding" { + r del zscoretest + set aux {} + for {set i 0} {$i < $elements} {incr i} { + set score [expr rand()] + lappend aux $score + r zadd zscoretest $score $i + } + + assert_encoding $encoding zscoretest + for {set i 0} {$i < $elements} {incr i} { + assert_equal [lindex $aux $i] [r zscore zscoretest $i] + } + } + + test "ZSCORE after a DEBUG RELOAD - $encoding" { + r del zscoretest + set aux {} + for {set i 0} {$i < $elements} {incr i} { + set score [expr rand()] + lappend aux $score + r zadd zscoretest $score $i + } + + r debug reload + assert_encoding $encoding zscoretest + for {set i 0} {$i < $elements} {incr i} { + assert_equal [lindex $aux $i] [r zscore zscoretest $i] + } + } + + test "ZSET sorting stresser - $encoding" { + set delta 0 + for {set test 0} {$test < 2} {incr test} { + unset -nocomplain auxarray + array set auxarray {} + set auxlist {} + r del myzset + for {set i 0} {$i < $elements} {incr i} { if {$test == 0} { set score [expr rand()] } else { set score [expr int(rand()*10)] } - set auxarray($j) $score - r zadd myzset $score $j - } - } - foreach {item score} [array get auxarray] { - lappend auxlist [list $score $item] - } - set sorted [lsort -command zlistAlikeSort $auxlist] - set auxlist {} - foreach x $sorted { - lappend auxlist [lindex $x 1] - } - set fromredis [r zrange myzset 0 -1] - set delta 0 - for {set i 0} {$i < [llength $fromredis]} {incr i} { - if {[lindex $fromredis $i] != [lindex $auxlist $i]} { - incr delta + set auxarray($i) $score + r zadd myzset $score $i + # Random update + if {[expr rand()] < .2} { + set j [expr int(rand()*1000)] + if {$test == 0} { + set score [expr rand()] + } else { + set score [expr int(rand()*10)] + } + set auxarray($j) $score + r zadd myzset $score $j + } + } + foreach {item score} [array get auxarray] { + lappend auxlist [list $score $item] + } + set sorted [lsort -command zlistAlikeSort $auxlist] + set auxlist {} + foreach x $sorted { + lappend auxlist [lindex $x 1] + } + + assert_encoding $encoding myzset + set fromredis [r zrange myzset 0 -1] + set delta 0 + for {set i 0} {$i < [llength $fromredis]} {incr i} { + if {[lindex $fromredis $i] != [lindex $auxlist $i]} { + incr delta + } } } + assert_equal 0 $delta } - format $delta - } {0} - test {ZINCRBY - can create a new sorted set} { - r del zset - r zincrby zset 1 foo - list [r zrange zset 0 -1] [r zscore zset foo] - } {foo 1} - - test {ZINCRBY - increment and decrement} { - r zincrby zset 2 foo - r zincrby zset 1 bar - set v1 [r zrange zset 0 -1] - r zincrby zset 10 bar - r zincrby zset -5 foo - r zincrby zset -5 bar - set v2 [r zrange zset 0 -1] - list $v1 $v2 [r zscore zset foo] [r zscore zset bar] - } {{bar foo} {foo bar} -2 6} - - proc create_default_zset {} { - create_zset zset {-inf a 1 b 2 c 3 d 4 e 5 f +inf g} - } - - test "ZRANGEBYSCORE/ZREVRANGEBYSCORE/ZCOUNT basics" { - create_default_zset - - # inclusive range - assert_equal {a b c} [r zrangebyscore zset -inf 2] - assert_equal {b c d} [r zrangebyscore zset 0 3] - assert_equal {d e f} [r zrangebyscore zset 3 6] - assert_equal {e f g} [r zrangebyscore zset 4 +inf] - assert_equal {c b a} [r zrevrangebyscore zset 2 -inf] - assert_equal {d c b} [r zrevrangebyscore zset 3 0] - assert_equal {f e d} [r zrevrangebyscore zset 6 3] - assert_equal {g f e} [r zrevrangebyscore zset +inf 4] - assert_equal 3 [r zcount zset 0 3] - - # exclusive range - assert_equal {b} [r zrangebyscore zset (-inf (2] - assert_equal {b c} [r zrangebyscore zset (0 (3] - assert_equal {e f} [r zrangebyscore zset (3 (6] - assert_equal {f} [r zrangebyscore zset (4 (+inf] - assert_equal {b} [r zrevrangebyscore zset (2 (-inf] - assert_equal {c b} [r zrevrangebyscore zset (3 (0] - assert_equal {f e} [r zrevrangebyscore zset (6 (3] - assert_equal {f} [r zrevrangebyscore zset (+inf (4] - assert_equal 2 [r zcount zset (0 (3] - - # test empty ranges - r zrem zset a - r zrem zset g - - # inclusive - assert_equal {} [r zrangebyscore zset 4 2] - assert_equal {} [r zrangebyscore zset 6 +inf] - assert_equal {} [r zrangebyscore zset -inf -6] - assert_equal {} [r zrevrangebyscore zset +inf 6] - assert_equal {} [r zrevrangebyscore zset -6 -inf] - - # exclusive - assert_equal {} [r zrangebyscore zset (4 (2] - assert_equal {} [r zrangebyscore zset 2 (2] - assert_equal {} [r zrangebyscore zset (2 2] - assert_equal {} [r zrangebyscore zset (6 (+inf] - assert_equal {} [r zrangebyscore zset (-inf (-6] - assert_equal {} [r zrevrangebyscore zset (+inf (6] - assert_equal {} [r zrevrangebyscore zset (-6 (-inf] - - # empty inner range - assert_equal {} [r zrangebyscore zset 2.4 2.6] - assert_equal {} [r zrangebyscore zset (2.4 2.6] - assert_equal {} [r zrangebyscore zset 2.4 (2.6] - assert_equal {} [r zrangebyscore zset (2.4 (2.6] - } - - test "ZRANGEBYSCORE with WITHSCORES" { - create_default_zset - assert_equal {b 1 c 2 d 3} [r zrangebyscore zset 0 3 withscores] - assert_equal {d 3 c 2 b 1} [r zrevrangebyscore zset 3 0 withscores] - } - - test "ZRANGEBYSCORE with LIMIT" { - create_default_zset - assert_equal {b c} [r zrangebyscore zset 0 10 LIMIT 0 2] - assert_equal {d e f} [r zrangebyscore zset 0 10 LIMIT 2 3] - assert_equal {d e f} [r zrangebyscore zset 0 10 LIMIT 2 10] - assert_equal {} [r zrangebyscore zset 0 10 LIMIT 20 10] - assert_equal {f e} [r zrevrangebyscore zset 10 0 LIMIT 0 2] - assert_equal {d c b} [r zrevrangebyscore zset 10 0 LIMIT 2 3] - assert_equal {d c b} [r zrevrangebyscore zset 10 0 LIMIT 2 10] - assert_equal {} [r zrevrangebyscore zset 10 0 LIMIT 20 10] - } - - test "ZRANGEBYSCORE with LIMIT and WITHSCORES" { - create_default_zset - assert_equal {e 4 f 5} [r zrangebyscore zset 2 5 LIMIT 2 3 WITHSCORES] - assert_equal {d 3 c 2} [r zrevrangebyscore zset 5 2 LIMIT 2 3 WITHSCORES] - } - - test "ZRANGEBYSCORE with non-value min or max" { - assert_error "*not a double*" {r zrangebyscore fooz str 1} - assert_error "*not a double*" {r zrangebyscore fooz 1 str} - assert_error "*not a double*" {r zrangebyscore fooz 1 NaN} - } - - tags {"slow"} { - test {ZRANGEBYSCORE fuzzy test, 100 ranges in 1000 elements sorted set} { + test "ZRANGEBYSCORE fuzzy test, 100 ranges in $elements element sorted set - $encoding" { set err {} r del zset - for {set i 0} {$i < 1000} {incr i} { + for {set i 0} {$i < $elements} {incr i} { r zadd zset [expr rand()] $i } + + assert_encoding $encoding zset for {set i 0} {$i < 100} {incr i} { set min [expr rand()] set max [expr rand()] @@ -363,198 +611,17 @@ start_server {tags {"zset"}} { } } } - set _ $err - } {} - } - - test "ZREMRANGEBYSCORE basics" { - proc remrangebyscore {min max} { - create_zset zset {1 a 2 b 3 c 4 d 5 e} - r zremrangebyscore zset $min $max + assert_equal {} $err } - # inner range - assert_equal 3 [remrangebyscore 2 4] - assert_equal {a e} [r zrange zset 0 -1] - - # start underflow - assert_equal 1 [remrangebyscore -10 1] - assert_equal {b c d e} [r zrange zset 0 -1] - - # end overflow - assert_equal 1 [remrangebyscore 5 10] - assert_equal {a b c d} [r zrange zset 0 -1] - - # switch min and max - assert_equal 0 [remrangebyscore 4 2] - assert_equal {a b c d e} [r zrange zset 0 -1] - - # -inf to mid - assert_equal 3 [remrangebyscore -inf 3] - assert_equal {d e} [r zrange zset 0 -1] - - # mid to +inf - assert_equal 3 [remrangebyscore 3 +inf] - assert_equal {a b} [r zrange zset 0 -1] - - # -inf to +inf - assert_equal 5 [remrangebyscore -inf +inf] - assert_equal {} [r zrange zset 0 -1] - - # exclusive min - assert_equal 4 [remrangebyscore (1 5] - assert_equal {a} [r zrange zset 0 -1] - assert_equal 3 [remrangebyscore (2 5] - assert_equal {a b} [r zrange zset 0 -1] - - # exclusive max - assert_equal 4 [remrangebyscore 1 (5] - assert_equal {e} [r zrange zset 0 -1] - assert_equal 3 [remrangebyscore 1 (4] - assert_equal {d e} [r zrange zset 0 -1] - - # exclusive min and max - assert_equal 3 [remrangebyscore (1 (5] - assert_equal {a e} [r zrange zset 0 -1] - } - - test "ZREMRANGEBYSCORE with non-value min or max" { - assert_error "*not a double*" {r zremrangebyscore fooz str 1} - assert_error "*not a double*" {r zremrangebyscore fooz 1 str} - assert_error "*not a double*" {r zremrangebyscore fooz 1 NaN} - } - - test "ZREMRANGEBYRANK basics" { - proc remrangebyrank {min max} { - create_zset zset {1 a 2 b 3 c 4 d 5 e} - r zremrangebyrank zset $min $max - } - - # inner range - assert_equal 3 [remrangebyrank 1 3] - assert_equal {a e} [r zrange zset 0 -1] - - # start underflow - assert_equal 1 [remrangebyrank -10 0] - assert_equal {b c d e} [r zrange zset 0 -1] - - # start overflow - assert_equal 0 [remrangebyrank 10 -1] - assert_equal {a b c d e} [r zrange zset 0 -1] - - # end underflow - assert_equal 0 [remrangebyrank 0 -10] - assert_equal {a b c d e} [r zrange zset 0 -1] - - # end overflow - assert_equal 5 [remrangebyrank 0 10] - assert_equal {} [r zrange zset 0 -1] - } - - test {ZUNIONSTORE against non-existing key doesn't set destination} { - r del zseta - list [r zunionstore dst_key 1 zseta] [r exists dst_key] - } {0 0} - - test {ZUNIONSTORE basics} { - r del zseta zsetb zsetc - r zadd zseta 1 a - r zadd zseta 2 b - r zadd zseta 3 c - r zadd zsetb 1 b - r zadd zsetb 2 c - r zadd zsetb 3 d - list [r zunionstore zsetc 2 zseta zsetb] [r zrange zsetc 0 -1 withscores] - } {4 {a 1 b 3 d 3 c 5}} - - test {ZUNIONSTORE with weights} { - list [r zunionstore zsetc 2 zseta zsetb weights 2 3] [r zrange zsetc 0 -1 withscores] - } {4 {a 2 b 7 d 9 c 12}} - - test {ZUNIONSTORE with a regular set and weights} { - r del seta - r sadd seta a - r sadd seta b - r sadd seta c - list [r zunionstore zsetc 2 seta zsetb weights 2 3] [r zrange zsetc 0 -1 withscores] - } {4 {a 2 b 5 c 8 d 9}} - - test {ZUNIONSTORE with AGGREGATE MIN} { - list [r zunionstore zsetc 2 zseta zsetb aggregate min] [r zrange zsetc 0 -1 withscores] - } {4 {a 1 b 1 c 2 d 3}} - - test {ZUNIONSTORE with AGGREGATE MAX} { - list [r zunionstore zsetc 2 zseta zsetb aggregate max] [r zrange zsetc 0 -1 withscores] - } {4 {a 1 b 2 c 3 d 3}} - - test {ZINTERSTORE basics} { - list [r zinterstore zsetc 2 zseta zsetb] [r zrange zsetc 0 -1 withscores] - } {2 {b 3 c 5}} - - test {ZINTERSTORE with weights} { - list [r zinterstore zsetc 2 zseta zsetb weights 2 3] [r zrange zsetc 0 -1 withscores] - } {2 {b 7 c 12}} - - test {ZINTERSTORE with a regular set and weights} { - r del seta - r sadd seta a - r sadd seta b - r sadd seta c - list [r zinterstore zsetc 2 seta zsetb weights 2 3] [r zrange zsetc 0 -1 withscores] - } {2 {b 5 c 8}} - - test {ZINTERSTORE with AGGREGATE MIN} { - list [r zinterstore zsetc 2 zseta zsetb aggregate min] [r zrange zsetc 0 -1 withscores] - } {2 {b 1 c 2}} - - test {ZINTERSTORE with AGGREGATE MAX} { - list [r zinterstore zsetc 2 zseta zsetb aggregate max] [r zrange zsetc 0 -1 withscores] - } {2 {b 2 c 3}} - - foreach cmd {ZUNIONSTORE ZINTERSTORE} { - test "$cmd with +inf/-inf scores" { - r del zsetinf1 zsetinf2 - - r zadd zsetinf1 +inf key - r zadd zsetinf2 +inf key - r $cmd zsetinf3 2 zsetinf1 zsetinf2 - assert_equal inf [r zscore zsetinf3 key] - - r zadd zsetinf1 -inf key - r zadd zsetinf2 +inf key - r $cmd zsetinf3 2 zsetinf1 zsetinf2 - assert_equal 0 [r zscore zsetinf3 key] - - r zadd zsetinf1 +inf key - r zadd zsetinf2 -inf key - r $cmd zsetinf3 2 zsetinf1 zsetinf2 - assert_equal 0 [r zscore zsetinf3 key] - - r zadd zsetinf1 -inf key - r zadd zsetinf2 -inf key - r $cmd zsetinf3 2 zsetinf1 zsetinf2 - assert_equal -inf [r zscore zsetinf3 key] - } - - test "$cmd with NaN weights" { - r del zsetinf1 zsetinf2 - - r zadd zsetinf1 1.0 key - r zadd zsetinf2 1.0 key - assert_error "*weight value is not a double*" { - r $cmd zsetinf3 2 zsetinf1 zsetinf2 weights nan nan - } - } - } - - tags {"slow"} { - test {ZSETs skiplist implementation backlink consistency test} { + test "ZSETs skiplist implementation backlink consistency test - $encoding" { set diff 0 - set elements 10000 for {set j 0} {$j < $elements} {incr j} { r zadd myzset [expr rand()] "Element-$j" r zrem myzset "Element-[expr int(rand()*$elements)]" } + + assert_encoding $encoding myzset set l1 [r zrange myzset 0 -1] set l2 [r zrevrange myzset 0 -1] for {set j 0} {$j < [llength $l1]} {incr j} { @@ -562,20 +629,22 @@ start_server {tags {"zset"}} { incr diff } } - format $diff - } {0} + assert_equal 0 $diff + } - test {ZSETs ZRANK augmented skip list stress testing} { + test "ZSETs ZRANK augmented skip list stress testing - $encoding" { set err {} r del myzset - for {set k 0} {$k < 10000} {incr k} { - set i [expr {$k%1000}] + for {set k 0} {$k < 2000} {incr k} { + set i [expr {$k % $elements}] if {[expr rand()] < .2} { r zrem myzset $i } else { set score [expr rand()] r zadd myzset $score $i + assert_encoding $encoding myzset } + set card [r zcard myzset] if {$card > 0} { set index [randomInt $card] @@ -587,20 +656,12 @@ start_server {tags {"zset"}} { } } } - set _ $err - } {} + assert_equal {} $err + } } - test {ZSET element can't be set to NaN with ZADD} { - assert_error "*not a double*" {r zadd myzset nan abc} - } - - test {ZSET element can't be set to NaN with ZINCRBY} { - assert_error "*not a double*" {r zadd myzset nan abc} - } - - test {ZINCRBY calls leading to NaN result in error} { - r zincrby myzset +inf abc - assert_error "*NaN*" {r zincrby myzset -inf abc} + tags {"slow"} { + stressers ziplist + stressers raw } } From dddf5335f47d62d5e74308faff8dcb5d0575286c Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Mon, 14 Mar 2011 13:30:06 +0100 Subject: [PATCH 035/146] Fix DEBUG DIGEST, SORT and AOF rewrite --- src/aof.c | 58 +++++++++++++++++++++++++++++++++++++++----------- src/debug.c | 61 +++++++++++++++++++++++++++++++++++++++++------------ src/redis.h | 3 +++ src/sort.c | 3 +++ 4 files changed, 100 insertions(+), 25 deletions(-) diff --git a/src/aof.c b/src/aof.c index 8ce6cd12..cd7c48d3 100644 --- a/src/aof.c +++ b/src/aof.c @@ -429,21 +429,55 @@ int rewriteAppendOnlyFile(char *filename) { } } else if (o->type == REDIS_ZSET) { /* Emit the ZADDs needed to rebuild the sorted set */ - zset *zs = o->ptr; - dictIterator *di = dictGetIterator(zs->dict); - dictEntry *de; + char cmd[]="*4\r\n$4\r\nZADD\r\n"; - while((de = dictNext(di)) != NULL) { - char cmd[]="*4\r\n$4\r\nZADD\r\n"; - robj *eleobj = dictGetEntryKey(de); - double *score = dictGetEntryVal(de); + if (o->encoding == REDIS_ENCODING_ZIPLIST) { + unsigned char *zl = o->ptr; + unsigned char *eptr, *sptr; + unsigned char *vstr; + unsigned int vlen; + long long vll; + double score; - if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr; - if (fwriteBulkObject(fp,&key) == 0) goto werr; - if (fwriteBulkDouble(fp,*score) == 0) goto werr; - if (fwriteBulkObject(fp,eleobj) == 0) goto werr; + eptr = ziplistIndex(zl,0); + redisAssert(eptr != NULL); + sptr = ziplistNext(zl,eptr); + redisAssert(sptr != NULL); + + while (eptr != NULL) { + redisAssert(ziplistGet(eptr,&vstr,&vlen,&vll)); + score = zzlGetScore(sptr); + + if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr; + if (fwriteBulkObject(fp,&key) == 0) goto werr; + if (fwriteBulkDouble(fp,score) == 0) goto werr; + if (vstr != NULL) { + if (fwriteBulkString(fp,(char*)vstr,vlen) == 0) + goto werr; + } else { + if (fwriteBulkLongLong(fp,vll) == 0) + goto werr; + } + zzlNext(zl,&eptr,&sptr); + } + } else if (o->encoding == REDIS_ENCODING_RAW) { + zset *zs = o->ptr; + dictIterator *di = dictGetIterator(zs->dict); + dictEntry *de; + + while((de = dictNext(di)) != NULL) { + robj *eleobj = dictGetEntryKey(de); + double *score = dictGetEntryVal(de); + + if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr; + if (fwriteBulkObject(fp,&key) == 0) goto werr; + if (fwriteBulkDouble(fp,*score) == 0) goto werr; + if (fwriteBulkObject(fp,eleobj) == 0) goto werr; + } + dictReleaseIterator(di); + } else { + redisPanic("Unknown sorted set encoding"); } - dictReleaseIterator(di); } else if (o->type == REDIS_HASH) { char cmd[]="*4\r\n$4\r\nHSET\r\n"; diff --git a/src/debug.c b/src/debug.c index c1fc26cf..940a0380 100644 --- a/src/debug.c +++ b/src/debug.c @@ -127,22 +127,57 @@ void computeDatasetDigest(unsigned char *final) { } setTypeReleaseIterator(si); } else if (o->type == REDIS_ZSET) { - zset *zs = o->ptr; - dictIterator *di = dictGetIterator(zs->dict); - dictEntry *de; + unsigned char eledigest[20]; - while((de = dictNext(di)) != NULL) { - robj *eleobj = dictGetEntryKey(de); - double *score = dictGetEntryVal(de); - unsigned char eledigest[20]; + if (o->encoding == REDIS_ENCODING_ZIPLIST) { + unsigned char *zl = o->ptr; + unsigned char *eptr, *sptr; + unsigned char *vstr; + unsigned int vlen; + long long vll; + double score; - snprintf(buf,sizeof(buf),"%.17g",*score); - memset(eledigest,0,20); - mixObjectDigest(eledigest,eleobj); - mixDigest(eledigest,buf,strlen(buf)); - xorDigest(digest,eledigest,20); + eptr = ziplistIndex(zl,0); + redisAssert(eptr != NULL); + sptr = ziplistNext(zl,eptr); + redisAssert(sptr != NULL); + + while (eptr != NULL) { + redisAssert(ziplistGet(eptr,&vstr,&vlen,&vll)); + score = zzlGetScore(sptr); + + memset(eledigest,0,20); + if (vstr != NULL) { + mixDigest(eledigest,vstr,vlen); + } else { + ll2string(buf,sizeof(buf),vll); + mixDigest(eledigest,buf,strlen(buf)); + } + + snprintf(buf,sizeof(buf),"%.17g",score); + mixDigest(eledigest,buf,strlen(buf)); + xorDigest(digest,eledigest,20); + zzlNext(zl,&eptr,&sptr); + } + } else if (o->encoding == REDIS_ENCODING_RAW) { + zset *zs = o->ptr; + dictIterator *di = dictGetIterator(zs->dict); + dictEntry *de; + + while((de = dictNext(di)) != NULL) { + robj *eleobj = dictGetEntryKey(de); + double *score = dictGetEntryVal(de); + + snprintf(buf,sizeof(buf),"%.17g",*score); + memset(eledigest,0,20); + mixObjectDigest(eledigest,eleobj); + mixDigest(eledigest,buf,strlen(buf)); + xorDigest(digest,eledigest,20); + } + dictReleaseIterator(di); + } else { + redisPanic("Unknown sorted set encoding"); } - dictReleaseIterator(di); } else if (o->type == REDIS_HASH) { hashTypeIterator *hi; robj *obj; diff --git a/src/redis.h b/src/redis.h index 8ed7216d..1d6d49dc 100644 --- a/src/redis.h +++ b/src/redis.h @@ -800,6 +800,9 @@ zskiplist *zslCreate(void); void zslFree(zskiplist *zsl); zskiplistNode *zslInsert(zskiplist *zsl, double score, robj *obj); unsigned char *zzlInsert(unsigned char *zl, robj *ele, double score); +double zzlGetScore(unsigned char *sptr); +void zzlNext(unsigned char *zl, unsigned char **eptr, unsigned char **sptr); +void zzlPrev(unsigned char *zl, unsigned char **eptr, unsigned char **sptr); unsigned int zsetLength(robj *zobj); void zsetConvert(robj *zobj, int encoding); diff --git a/src/sort.c b/src/sort.c index 1cf8932e..1a3fecb6 100644 --- a/src/sort.c +++ b/src/sort.c @@ -199,6 +199,9 @@ void sortCommand(redisClient *c) { j++; } + /* Destructively convert encoded sorted sets for SORT. */ + if (sortval->type == REDIS_ZSET) zsetConvert(sortval, REDIS_ENCODING_RAW); + /* Load the sorting vector with all the objects to sort */ switch(sortval->type) { case REDIS_LIST: vectorlen = listTypeLength(sortval); break; From 48991620f71485a5fa056736796b620eab1387a9 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Mon, 21 Mar 2011 23:54:35 +0100 Subject: [PATCH 036/146] Remove sorted set when empty after ZREMRANGEBY* --- src/t_zset.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/t_zset.c b/src/t_zset.c index 03a79ce9..f630ff02 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -1012,6 +1012,7 @@ void zremrangebyscoreCommand(redisClient *c) { if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { zobj->ptr = zzlDeleteRangeByScore(zobj->ptr,range,&deleted); + if (zzlLength(zobj->ptr) == 0) dbDelete(c->db,key); } else if (zobj->encoding == REDIS_ENCODING_RAW) { zset *zs = zobj->ptr; deleted = zslDeleteRangeByScore(zs->zsl,range,zs->dict); @@ -1057,6 +1058,7 @@ void zremrangebyrankCommand(redisClient *c) { if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { /* Correct for 1-based rank. */ zobj->ptr = zzlDeleteRangeByRank(zobj->ptr,start+1,end+1,&deleted); + if (zzlLength(zobj->ptr) == 0) dbDelete(c->db,key); } else if (zobj->encoding == REDIS_ENCODING_RAW) { zset *zs = zobj->ptr; From 04a10b1a6d372bd7a1105150194881eccbfd5620 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Tue, 22 Mar 2011 09:28:40 +0100 Subject: [PATCH 037/146] Test that sorted sets are removed when empty --- tests/unit/type/zset.tcl | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/unit/type/zset.tcl b/tests/unit/type/zset.tcl index 31604412..6e32a4b8 100644 --- a/tests/unit/type/zset.tcl +++ b/tests/unit/type/zset.tcl @@ -53,6 +53,18 @@ start_server {tags {"zset"}} { assert_equal 0 [r zcard zdoesntexist] } + test "ZREM removes key after last element is removed" { + r del ztmp + r zadd ztmp 10 x + r zadd ztmp 20 y + + assert_equal 1 [r exists ztmp] + assert_equal 0 [r zrem ztmp z] + assert_equal 1 [r zrem ztmp y] + assert_equal 1 [r zrem ztmp x] + assert_equal 0 [r exists ztmp] + } + test "ZRANGE basics - $encoding" { r del ztmp r zadd ztmp 1 a @@ -244,6 +256,7 @@ start_server {tags {"zset"}} { test "ZREMRANGEBYSCORE basics" { proc remrangebyscore {min max} { create_zset zset {1 a 2 b 3 c 4 d 5 e} + assert_equal 1 [r exists zset] r zremrangebyscore zset $min $max } @@ -290,6 +303,10 @@ start_server {tags {"zset"}} { # exclusive min and max assert_equal 3 [remrangebyscore (1 (5] assert_equal {a e} [r zrange zset 0 -1] + + # destroy when empty + assert_equal 5 [remrangebyscore 1 5] + assert_equal 0 [r exists zset] } test "ZREMRANGEBYSCORE with non-value min or max" { @@ -301,6 +318,7 @@ start_server {tags {"zset"}} { test "ZREMRANGEBYRANK basics" { proc remrangebyrank {min max} { create_zset zset {1 a 2 b 3 c 4 d 5 e} + assert_equal 1 [r exists zset] r zremrangebyrank zset $min $max } @@ -323,6 +341,10 @@ start_server {tags {"zset"}} { # end overflow assert_equal 5 [remrangebyrank 0 10] assert_equal {} [r zrange zset 0 -1] + + # destroy when empty + assert_equal 5 [remrangebyrank 0 4] + assert_equal 0 [r exists zset] } test "ZUNIONSTORE against non-existing key doesn't set destination - $encoding" { From 03af999cb0befed9d2556c2df1eb0511de2d96d2 Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 22 Mar 2011 19:35:42 +0100 Subject: [PATCH 038/146] command table altered for the new API to get position of keys arguments in commands --- src/redis.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/redis.c b/src/redis.c index 866ac360..51e58f58 100644 --- a/src/redis.c +++ b/src/redis.c @@ -70,12 +70,12 @@ struct redisServer server; /* server global state */ struct redisCommand *commandTable; struct redisCommand redisCommandTable[] = { {"get",getCommand,2,0,NULL,1,1,1,0,0}, - {"set",setCommand,3,REDIS_CMD_DENYOOM,NULL,0,0,0,0,0}, - {"setnx",setnxCommand,3,REDIS_CMD_DENYOOM,NULL,0,0,0,0,0}, - {"setex",setexCommand,4,REDIS_CMD_DENYOOM,NULL,0,0,0,0,0}, + {"set",setCommand,3,REDIS_CMD_DENYOOM,noPreloadGetKeys,1,1,1,0,0}, + {"setnx",setnxCommand,3,REDIS_CMD_DENYOOM,noPreloadGetKeys,1,1,1,0,0}, + {"setex",setexCommand,4,REDIS_CMD_DENYOOM,noPreloadGetKeys,2,2,1,0,0}, {"append",appendCommand,3,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0}, {"strlen",strlenCommand,2,0,NULL,1,1,1,0,0}, - {"del",delCommand,-2,0,NULL,0,0,0,0,0}, + {"del",delCommand,-2,0,noPreloadGetKeys,1,-1,1,0,0}, {"exists",existsCommand,2,0,NULL,1,1,1,0,0}, {"setbit",setbitCommand,4,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0}, {"getbit",getbitCommand,3,0,NULL,1,1,1,0,0}, @@ -94,7 +94,7 @@ struct redisCommand redisCommandTable[] = { {"lpop",lpopCommand,2,0,NULL,1,1,1,0,0}, {"brpop",brpopCommand,-3,0,NULL,1,1,1,0,0}, {"brpoplpush",brpoplpushCommand,4,REDIS_CMD_DENYOOM,NULL,1,2,1,0,0}, - {"blpop",blpopCommand,-3,0,NULL,1,1,1,0,0}, + {"blpop",blpopCommand,-3,0,NULL,1,-2,1,0,0}, {"llen",llenCommand,2,0,NULL,1,1,1,0,0}, {"lindex",lindexCommand,3,0,NULL,1,1,1,0,0}, {"lset",lsetCommand,4,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0}, @@ -121,8 +121,8 @@ struct redisCommand redisCommandTable[] = { {"zrem",zremCommand,3,0,NULL,1,1,1,0,0}, {"zremrangebyscore",zremrangebyscoreCommand,4,0,NULL,1,1,1,0,0}, {"zremrangebyrank",zremrangebyrankCommand,4,0,NULL,1,1,1,0,0}, - {"zunionstore",zunionstoreCommand,-4,REDIS_CMD_DENYOOM,zunionInterBlockClientOnSwappedKeys,0,0,0,0,0}, - {"zinterstore",zinterstoreCommand,-4,REDIS_CMD_DENYOOM,zunionInterBlockClientOnSwappedKeys,0,0,0,0,0}, + {"zunionstore",zunionstoreCommand,-4,REDIS_CMD_DENYOOM,zunionInterGetKeys,0,0,0,0,0}, + {"zinterstore",zinterstoreCommand,-4,REDIS_CMD_DENYOOM,zunionInterGetKeys,0,0,0,0,0}, {"zrange",zrangeCommand,-4,0,NULL,1,1,1,0,0}, {"zrangebyscore",zrangebyscoreCommand,-4,0,NULL,1,1,1,0,0}, {"zrevrangebyscore",zrevrangebyscoreCommand,-4,0,NULL,1,1,1,0,0}, @@ -152,10 +152,10 @@ struct redisCommand redisCommandTable[] = { {"randomkey",randomkeyCommand,1,0,NULL,0,0,0,0,0}, {"select",selectCommand,2,0,NULL,0,0,0,0,0}, {"move",moveCommand,3,0,NULL,1,1,1,0,0}, - {"rename",renameCommand,3,0,NULL,1,1,1,0,0}, - {"renamenx",renamenxCommand,3,0,NULL,1,1,1,0,0}, - {"expire",expireCommand,3,0,NULL,0,0,0,0,0}, - {"expireat",expireatCommand,3,0,NULL,0,0,0,0,0}, + {"rename",renameCommand,3,0,renameGetKeys,1,2,1,0,0}, + {"renamenx",renamenxCommand,3,0,renameGetKeys,1,2,1,0,0}, + {"expire",expireCommand,3,0,NULL,1,1,1,0,0}, + {"expireat",expireatCommand,3,0,NULL,1,1,1,0,0}, {"keys",keysCommand,2,0,NULL,0,0,0,0,0}, {"dbsize",dbsizeCommand,1,0,NULL,0,0,0,0,0}, {"auth",authCommand,2,0,NULL,0,0,0,0,0}, @@ -168,7 +168,7 @@ struct redisCommand redisCommandTable[] = { {"lastsave",lastsaveCommand,1,0,NULL,0,0,0,0,0}, {"type",typeCommand,2,0,NULL,1,1,1,0,0}, {"multi",multiCommand,1,0,NULL,0,0,0,0,0}, - {"exec",execCommand,1,REDIS_CMD_DENYOOM,execBlockClientOnSwappedKeys,0,0,0,0,0}, + {"exec",execCommand,1,REDIS_CMD_DENYOOM,execGetKeys,0,0,0,0,0}, {"discard",discardCommand,1,0,NULL,0,0,0,0,0}, {"sync",syncCommand,1,0,NULL,0,0,0,0,0}, {"flushdb",flushdbCommand,1,0,NULL,0,0,0,0,0}, @@ -186,7 +186,7 @@ struct redisCommand redisCommandTable[] = { {"psubscribe",psubscribeCommand,-2,0,NULL,0,0,0,0,0}, {"punsubscribe",punsubscribeCommand,-1,0,NULL,0,0,0,0,0}, {"publish",publishCommand,3,REDIS_CMD_FORCE_REPLICATION,NULL,0,0,0,0,0}, - {"watch",watchCommand,-2,0,NULL,0,0,0,0,0}, + {"watch",watchCommand,-2,0,noPreloadGetKeys,1,-1,1,0,0}, {"unwatch",unwatchCommand,1,0,NULL,0,0,0,0,0} }; From 9791f0f8ceb18e2e3c297a8c479307b7af0f9830 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 23 Mar 2011 18:09:17 +0100 Subject: [PATCH 039/146] new preloading implemented, still EXEC not handled correctly, everything to test --- src/db.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/dscache.c | 14 ++++++---- src/redis.c | 2 +- src/redis.h | 26 ++++++++++++------- 4 files changed, 98 insertions(+), 16 deletions(-) diff --git a/src/db.c b/src/db.c index 9daa5ddb..9bc299ca 100644 --- a/src/db.c +++ b/src/db.c @@ -616,3 +616,75 @@ void persistCommand(redisClient *c) { } } } + +/* ----------------------------------------------------------------------------- + * API to get key arguments from commands + * ---------------------------------------------------------------------------*/ + +int *getKeysUsingCommandTable(struct redisCommand *cmd,robj **argv, int argc, int *numkeys) { + int j, i = 0, last, *keys; + REDIS_NOTUSED(argv); + + if (cmd->firstkey == 0) { + *numkeys = 0; + return NULL; + } + last = cmd->lastkey; + if (last < 0) last = argc+last; + keys = zmalloc(sizeof(int)*((last - cmd->firstkey)+1)); + for (j = cmd->firstkey; j <= last; j += cmd->keystep) { + redisAssert(j < argc); + keys[i] = j; + } + return keys; +} + +int *getKeysFromCommand(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags) { + if (cmd->getkeys_proc) { + return cmd->getkeys_proc(cmd,argv,argc,numkeys,flags); + } else { + return getKeysUsingCommandTable(cmd,argv,argc,numkeys); + } +} + +void getKeysFreeResult(int *result) { + zfree(result); +} + +int *noPreloadGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags) { + if (flags & REDIS_GETKEYS_PRELOAD) { + *numkeys = 0; + return NULL; + } else { + return getKeysUsingCommandTable(cmd,argv,argc,numkeys); + } +} + +int *renameGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags) { + if (flags & REDIS_GETKEYS_PRELOAD) { + int *keys = zmalloc(sizeof(int)); + *numkeys = 1; + keys[0] = 1; + return NULL; + } else { + return getKeysUsingCommandTable(cmd,argv,argc,numkeys); + } +} + +int *zunionInterGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags) { + int i, num, *keys; + REDIS_NOTUSED(cmd); + REDIS_NOTUSED(flags); + + num = atoi(argv[2]->ptr); + /* Sanity check. Don't return any key if the command is going to + * reply with syntax error. */ + if (num > (argc-3)) { + *numkeys = 0; + return NULL; + } + keys = zmalloc(num); + for (i = 0; i < num; i++) keys[i] = 3+i; + *numkeys = num; + return keys; +} diff --git a/src/dscache.c b/src/dscache.c index cbe9bb01..5813052e 100644 --- a/src/dscache.c +++ b/src/dscache.c @@ -903,6 +903,7 @@ int waitForSwappedKey(redisClient *c, robj *key) { return 1; } +#if 0 /* Preload keys for any command with first, last and step values for * the command keys prototype, as defined in the command table. */ void waitForMultipleSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv) { @@ -955,6 +956,7 @@ void execBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int } } } +#endif /* Is this client attempting to run a command against swapped keys? * If so, block it ASAP, load the keys in background, then resume it. @@ -967,11 +969,13 @@ void execBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int * Return 1 if the client is marked as blocked, 0 if the client can * continue as the keys it is going to access appear to be in memory. */ int blockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd) { - if (cmd->vm_preload_proc != NULL) { - cmd->vm_preload_proc(c,cmd,c->argc,c->argv); - } else { - waitForMultipleSwappedKeys(c,cmd,c->argc,c->argv); - } + int *keyindex, numkeys, j; + + keyindex = getKeysFromCommand(cmd,c->argv,c->argc,&numkeys,REDIS_GETKEYS_PRELOAD); + for (j = 0; j < numkeys; j++) waitForSwappedKey(c,c->argv[keyindex[j]]); + getKeysFreeResult(keyindex); + +#warning "Handle EXEC here" /* If the client was blocked for at least one key, mark it as blocked. */ if (listLength(c->io_keys)) { diff --git a/src/redis.c b/src/redis.c index 51e58f58..19fd912c 100644 --- a/src/redis.c +++ b/src/redis.c @@ -168,7 +168,7 @@ struct redisCommand redisCommandTable[] = { {"lastsave",lastsaveCommand,1,0,NULL,0,0,0,0,0}, {"type",typeCommand,2,0,NULL,1,1,1,0,0}, {"multi",multiCommand,1,0,NULL,0,0,0,0,0}, - {"exec",execCommand,1,REDIS_CMD_DENYOOM,execGetKeys,0,0,0,0,0}, + {"exec",execCommand,1,REDIS_CMD_DENYOOM,NULL,0,0,0,0,0}, {"discard",discardCommand,1,0,NULL,0,0,0,0,0}, {"sync",syncCommand,1,0,NULL,0,0,0,0,0}, {"flushdb",flushdbCommand,1,0,NULL,0,0,0,0,0}, diff --git a/src/redis.h b/src/redis.h index cdddb601..b5188330 100644 --- a/src/redis.h +++ b/src/redis.h @@ -507,20 +507,19 @@ typedef struct pubsubPattern { } pubsubPattern; typedef void redisCommandProc(redisClient *c); -typedef void redisVmPreloadProc(redisClient *c, struct redisCommand *cmd, int argc, robj **argv); +typedef int *redisGetKeysProc(struct redisCommand *cmd, robj **argv, int argc, int *numkeys, int flags); struct redisCommand { char *name; redisCommandProc *proc; int arity; int flags; - /* Use a function to determine which keys need to be loaded - * in the background prior to executing this command. Takes precedence - * over vm_firstkey and others, ignored when NULL */ - redisVmPreloadProc *vm_preload_proc; + /* Use a function to determine keys arguments in a command line. + * Used both for diskstore preloading and Redis Cluster. */ + redisGetKeysProc *getkeys_proc; /* What keys should be loaded in background when calling this command? */ - int vm_firstkey; /* The first argument that's a key (0 = no keys) */ - int vm_lastkey; /* THe last argument that's a key */ - int vm_keystep; /* The step between first and last key */ + int firstkey; /* The first argument that's a key (0 = no keys) */ + int lastkey; /* THe last argument that's a key */ + int keystep; /* The step between first and last key */ long long microseconds, calls; }; @@ -829,8 +828,6 @@ void freeIOJob(iojob *j); void queueIOJob(iojob *j); void waitEmptyIOJobsQueue(void); void processAllPendingIOJobs(void); -void zunionInterBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv); -void execBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv); int blockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd); int dontWaitForSwappedKey(redisClient *c, robj *key); void handleClientsBlockedOnSwappedKey(redisDb *db, robj *key); @@ -917,6 +914,15 @@ int selectDb(redisClient *c, int id); void signalModifiedKey(redisDb *db, robj *key); void signalFlushedDb(int dbid); +/* API to get key arguments from commands */ +#define REDIS_GETKEYS_ALL 0 +#define REDIS_GETKEYS_PRELOAD 1 +int *getKeysFromCommand(struct redisCommand *cmd, robj **argv, int argc, int *numkeys, int flags); +void getKeysFreeResult(int *result); +int *noPreloadGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags); +int *renameGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags); +int *zunionInterGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags); + /* Git SHA1 */ char *redisGitSHA1(void); char *redisGitDirty(void); From a3cf9041218518f235a297b2f2d0d08ef9a5c4e5 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 23 Mar 2011 23:13:15 +0100 Subject: [PATCH 040/146] minor style change --- src/dscache.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/dscache.c b/src/dscache.c index 5813052e..faf87e84 100644 --- a/src/dscache.c +++ b/src/dscache.c @@ -971,7 +971,8 @@ void execBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int int blockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd) { int *keyindex, numkeys, j; - keyindex = getKeysFromCommand(cmd,c->argv,c->argc,&numkeys,REDIS_GETKEYS_PRELOAD); + keyindex = getKeysFromCommand(cmd,c->argv,c->argc,&numkeys, + REDIS_GETKEYS_PRELOAD); for (j = 0; j < numkeys; j++) waitForSwappedKey(c,c->argv[keyindex[j]]); getKeysFreeResult(keyindex); From 42b2621cdc4dfc47f90cf1085761ad89171a6a60 Mon Sep 17 00:00:00 2001 From: antirez Date: Mon, 28 Mar 2011 17:29:26 +0200 Subject: [PATCH 041/146] implemented preload of EXEC using the new getKeys() system --- src/dscache.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/dscache.c b/src/dscache.c index faf87e84..45865264 100644 --- a/src/dscache.c +++ b/src/dscache.c @@ -969,14 +969,33 @@ void execBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int * Return 1 if the client is marked as blocked, 0 if the client can * continue as the keys it is going to access appear to be in memory. */ int blockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd) { - int *keyindex, numkeys, j; + int *keyindex, numkeys, j, i; - keyindex = getKeysFromCommand(cmd,c->argv,c->argc,&numkeys, - REDIS_GETKEYS_PRELOAD); - for (j = 0; j < numkeys; j++) waitForSwappedKey(c,c->argv[keyindex[j]]); - getKeysFreeResult(keyindex); + /* EXEC is a special case, we need to preload all the commands + * queued into the transaction */ + if (cmd->proc == execCommand) { + struct redisCommand *mcmd; + robj **margv; + int margc; -#warning "Handle EXEC here" + if (!(c->flags & REDIS_MULTI)) return 0; + for (i = 0; i < c->mstate.count; i++) { + mcmd = c->mstate.commands[i].cmd; + margc = c->mstate.commands[i].argc; + margv = c->mstate.commands[i].argv; + + keyindex = getKeysFromCommand(mcmd,margv,margc,&numkeys, + REDIS_GETKEYS_PRELOAD); + for (j = 0; j < numkeys; j++) + waitForSwappedKey(c,margv[keyindex[j]]); + getKeysFreeResult(keyindex); + } + } else { + keyindex = getKeysFromCommand(cmd,c->argv,c->argc,&numkeys, + REDIS_GETKEYS_PRELOAD); + for (j = 0; j < numkeys; j++) waitForSwappedKey(c,c->argv[keyindex[j]]); + getKeysFreeResult(keyindex); + } /* If the client was blocked for at least one key, mark it as blocked. */ if (listLength(c->io_keys)) { From b4b5144694a4134544221a5587b81a98aec608be Mon Sep 17 00:00:00 2001 From: antirez Date: Mon, 28 Mar 2011 17:54:42 +0200 Subject: [PATCH 042/146] Fixes to the new preloading / key discovery APIs --- src/db.c | 3 ++- src/dscache.c | 66 +++++++-------------------------------------------- 2 files changed, 11 insertions(+), 58 deletions(-) diff --git a/src/db.c b/src/db.c index 9bc299ca..5bab42af 100644 --- a/src/db.c +++ b/src/db.c @@ -634,8 +634,9 @@ int *getKeysUsingCommandTable(struct redisCommand *cmd,robj **argv, int argc, in keys = zmalloc(sizeof(int)*((last - cmd->firstkey)+1)); for (j = cmd->firstkey; j <= last; j += cmd->keystep) { redisAssert(j < argc); - keys[i] = j; + keys[i++] = j; } + *numkeys = i; return keys; } diff --git a/src/dscache.c b/src/dscache.c index 45865264..31498358 100644 --- a/src/dscache.c +++ b/src/dscache.c @@ -903,61 +903,6 @@ int waitForSwappedKey(redisClient *c, robj *key) { return 1; } -#if 0 -/* Preload keys for any command with first, last and step values for - * the command keys prototype, as defined in the command table. */ -void waitForMultipleSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv) { - int j, last; - if (cmd->vm_firstkey == 0) return; - last = cmd->vm_lastkey; - if (last < 0) last = argc+last; - for (j = cmd->vm_firstkey; j <= last; j += cmd->vm_keystep) { - redisAssert(j < argc); - waitForSwappedKey(c,argv[j]); - } -} - -/* Preload keys needed for the ZUNIONSTORE and ZINTERSTORE commands. - * Note that the number of keys to preload is user-defined, so we need to - * apply a sanity check against argc. */ -void zunionInterBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv) { - int i, num; - REDIS_NOTUSED(cmd); - - num = atoi(argv[2]->ptr); - if (num > (argc-3)) return; - for (i = 0; i < num; i++) { - waitForSwappedKey(c,argv[3+i]); - } -} - -/* Preload keys needed to execute the entire MULTI/EXEC block. - * - * This function is called by blockClientOnSwappedKeys when EXEC is issued, - * and will block the client when any command requires a swapped out value. */ -void execBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv) { - int i, margc; - struct redisCommand *mcmd; - robj **margv; - REDIS_NOTUSED(cmd); - REDIS_NOTUSED(argc); - REDIS_NOTUSED(argv); - - if (!(c->flags & REDIS_MULTI)) return; - for (i = 0; i < c->mstate.count; i++) { - mcmd = c->mstate.commands[i].cmd; - margc = c->mstate.commands[i].argc; - margv = c->mstate.commands[i].argv; - - if (mcmd->vm_preload_proc != NULL) { - mcmd->vm_preload_proc(c,mcmd,margc,margv); - } else { - waitForMultipleSwappedKeys(c,mcmd,margc,margv); - } - } -} -#endif - /* Is this client attempting to run a command against swapped keys? * If so, block it ASAP, load the keys in background, then resume it. * @@ -986,14 +931,21 @@ int blockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd) { keyindex = getKeysFromCommand(mcmd,margv,margc,&numkeys, REDIS_GETKEYS_PRELOAD); - for (j = 0; j < numkeys; j++) + for (j = 0; j < numkeys; j++) { + redisLog(REDIS_WARNING,"Preloading %s", + (char*)margv[keyindex[j]]->ptr); waitForSwappedKey(c,margv[keyindex[j]]); + } getKeysFreeResult(keyindex); } } else { keyindex = getKeysFromCommand(cmd,c->argv,c->argc,&numkeys, REDIS_GETKEYS_PRELOAD); - for (j = 0; j < numkeys; j++) waitForSwappedKey(c,c->argv[keyindex[j]]); + for (j = 0; j < numkeys; j++) { + redisLog(REDIS_WARNING,"Preloading %s", + (char*)c->argv[keyindex[j]]->ptr); + waitForSwappedKey(c,c->argv[keyindex[j]]); + } getKeysFreeResult(keyindex); } From 6e1b9b58ec6ae895da686015d82d4f121f1bfb85 Mon Sep 17 00:00:00 2001 From: antirez Date: Mon, 28 Mar 2011 18:21:06 +0200 Subject: [PATCH 043/146] bug fixed in zunionstore specific getKeys() implementation --- src/db.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/db.c b/src/db.c index 5bab42af..7a9c9c4c 100644 --- a/src/db.c +++ b/src/db.c @@ -684,7 +684,7 @@ int *zunionInterGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *num *numkeys = 0; return NULL; } - keys = zmalloc(num); + keys = zmalloc(sizeof(int)*num); for (i = 0; i < num; i++) keys[i] = 3+i; *numkeys = num; return keys; From 4b61ca460c5d043b9d199a4e4991bfac5dde66c5 Mon Sep 17 00:00:00 2001 From: antirez Date: Mon, 28 Mar 2011 18:46:22 +0200 Subject: [PATCH 044/146] fixed a bug in RENAME getKeys() function --- src/db.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/db.c b/src/db.c index 7a9c9c4c..fea2f12e 100644 --- a/src/db.c +++ b/src/db.c @@ -666,7 +666,7 @@ int *renameGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int *keys = zmalloc(sizeof(int)); *numkeys = 1; keys[0] = 1; - return NULL; + return keys; } else { return getKeysUsingCommandTable(cmd,argv,argc,numkeys); } From a07bc9269234bba1f51211bff304ce9caa15a9fb Mon Sep 17 00:00:00 2001 From: antirez Date: Mon, 28 Mar 2011 19:03:57 +0200 Subject: [PATCH 045/146] Preloading messages log level changed from WARNING to DEBUG --- src/dscache.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dscache.c b/src/dscache.c index 31498358..46300e63 100644 --- a/src/dscache.c +++ b/src/dscache.c @@ -932,7 +932,7 @@ int blockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd) { keyindex = getKeysFromCommand(mcmd,margv,margc,&numkeys, REDIS_GETKEYS_PRELOAD); for (j = 0; j < numkeys; j++) { - redisLog(REDIS_WARNING,"Preloading %s", + redisLog(REDIS_DEBUG,"Preloading %s", (char*)margv[keyindex[j]]->ptr); waitForSwappedKey(c,margv[keyindex[j]]); } @@ -942,7 +942,7 @@ int blockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd) { keyindex = getKeysFromCommand(cmd,c->argv,c->argc,&numkeys, REDIS_GETKEYS_PRELOAD); for (j = 0; j < numkeys; j++) { - redisLog(REDIS_WARNING,"Preloading %s", + redisLog(REDIS_DEBUG,"Preloading %s", (char*)c->argv[keyindex[j]]->ptr); waitForSwappedKey(c,c->argv[keyindex[j]]); } From b46251d92970468f5e55e06db1f580acf1fab1c4 Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 29 Mar 2011 11:04:55 +0200 Subject: [PATCH 046/146] TODO modified --- TODO | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO b/TODO index 580adda6..e7943307 100644 --- a/TODO +++ b/TODO @@ -12,6 +12,7 @@ WARNING: are you a possible Redis contributor? DISKSTORE TODO ============== +* Fix FLUSHALL/FLUSHDB: the queue of pending reads/writes should be handled. * Check that 00/00 and ff/ff exist at startup, otherwise exit with error. * Implement sync flush option, where data is written synchronously on disk when a command is executed. * Implement MULTI/EXEC as transaction abstract API to diskstore.c, with transaction_start, transaction_end, and a journal to recover. From ecc9109434002d4667cd01a3b7c067a508c876eb Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 29 Mar 2011 17:51:15 +0200 Subject: [PATCH 047/146] Cluster branch merged to unstable. --- CLUSTER | 131 +++++ src/Makefile | 4 +- src/cluster.c | 1299 +++++++++++++++++++++++++++++++++++++++++++++++ src/config.c | 4 + src/crc16.c | 74 +++ src/db.c | 9 + src/rdb.c | 2 +- src/redis-cli.c | 21 +- src/redis.c | 42 +- src/redis.h | 136 ++++- src/syncio.c | 14 + 11 files changed, 1727 insertions(+), 9 deletions(-) create mode 100644 CLUSTER create mode 100644 src/cluster.c create mode 100644 src/crc16.c diff --git a/CLUSTER b/CLUSTER new file mode 100644 index 00000000..bfe5234a --- /dev/null +++ b/CLUSTER @@ -0,0 +1,131 @@ +TODO + +- disconnect FAIL clients after some pong idle time. + +--------------------------------- + +* Majority rule: the cluster con continue when there are all the hash slots covered AND when there are the majority of masters. +* Shutdown on request rule: when a node sees many connections closed or even a timeout longer than usual on almost all the other nodes, it will usually wait for the normal timeout before to change the state, unless it receives a query from a client: in such a case it will put itself into error status. + +-------------------------------- + +* When asked for a key that is not in a node's business it will reply: + + -ASK 1.2.3.4:6379 (in case we want the client to ask just one time) + -MOVED 1.2.3.4:6379 (in case the hash slot is permanently moved) + +So with -ASK a client should just retry the query against this new node, a single time. + +With -MOVED the client should update its hash slots table to reflect the fact that now the specified node is the one to contact for the specified hash slot. + +* Nodes communicate using a binary protocol. + +* Node failure detection. + + 1) Every node contains information about all the other nodes: + - If this node is believed to work ok or not + - The hash slots for which this node is responsible + - If the node is a master or a slave + - If it is a slave, the slave of which node + - if it is a master, the list of slave nodes + - The slaves are ordered for ":" string from lower to higher + ordered lexicographically. When a master is down, the cluster will + try to elect the first slave in the list. + + 2) Every node also contains the unix time where every other node was + reported to work properly (that is, it replied to a ping or any other + protocol request correctly). For every node we also store the timestamp + at which we sent the latest ping, so we can easily compute the current + lag. + + 3) From time to time a node pings a random node, selected among the nodes + with the least recent "alive" time stamp. Three random nodes are selected + and the one with lower alive time stamp is pinged. + + 4) The ping packet contains also information about a few random nodes + alive time stamp. So that the receiver of the ping will update the + alive table if the received alive timestamp is more recent the + one present in the node local table. + + In the ping packet every node "gossip" information is somethig like + this: + + :::: + + status is OK, POSSIBLE_FAILURE, FAILURE. + + 5) The node replies to ping with a pong packet, that also contains a random + selections of nodes timestamps. + +A given node thinks another node may be in a failure state once there is a +ping timeout bigger than 30 seconds (configurable). + +When a possible failure is detected the node performs the following action: + + 1) Is the average between all the other nodes big? For instance bigger + than 30 seconds / 2 = 15 seconds? Probably *we* are disconnected. + In such a case we don't trust our lag data, and reset all the + timestamps of sent ping to zero. This way when we'll reconnect there + is no risk that we'll claim many nodes are down, taking inappropriate + actions. + + 2) Messages from nodes marked as failed are *always* ignored by the other + nodes. A new node needs to be "introduced" by a good online node. + + 3) If we are well connected (that is, condition "1" is not true) and a + node timeout is > 30 seconds, we mark the node as POSSIBLE_FAILURE + (a flat in the cluster node structure). Every time we sent a ping + to another node we inform this other nodes that we detected this + condition, as already stated. + + 4) Once a node receives a POSSIBLE_FAILURE status for a node that is + already marked as POSSIBLE_FAILURE locally, it sends a message + to all the other nodes of type NODE_FAILURE_DETECTED, communicating the + ip/port of the specified node. + + All the nodes need to update the status of this node setting it into + FAILURE. + + 5) If the computer in FAILURE state is a master node, what is needed is + to perform a Slave Election. + +SLAVE ELECTION + + 1) The slave election is performed by the first slave (with slaves ordered + lexicographically). Actually it is the first functioning slave, so if + the first slave is marked as failing the next slave will perform the + election and so forth. Such a slave is called the "Successor". + + 2) The Successor starts checking that all the nodes in the cluster already + marked the master in FAILURE state. If at least one node does not agree + no action is performed. + + 3) If all the nodes agree that the master is failing, the Successor does + the following: + + a) It will send a SUCCESSION message to all the other nodes, that will + upgrade the hash slot tables accordingly. It will make sure that all + the nodes are updated and if some node did not received the message + it will keep trying. + b) Once all nodes not marked as FAILURE accepted the SUCCESSION message + it will update his own table and will start acting as a master + accepting write queries. + c) Every node receiving the succession message, if not already informed + of the change will broadcast the same message to other three random + nodes. No action is performed if the specified host was already marked + as the master node. + d) A node that was a slave of the original master that failed will + switch master to the new one once the SUCCESSION message is received. + +RANDOM + + 1) When selecting a slave, the system will try to pick one with an IP different than the master and other slaves, if possible. + + 2) The PING packet also contains information about the local configuration checksum. This is the SHA1 of the current configuration, without the bits that normally change form one node to another (like latest ping reply, failure status of nodes, and so forth). From time to time the local config SHA1 is checked against the list of the other nodes, and if there is a mismatch between our configuration and the most common one that lasts for more than N seconds, the most common configuration is asked and retrieved from another node. The event is logged. + + 3) Every time a node updates its internal cluster configuration, it dumps such a config in the cluster.conf file. On startup the configuration is reloaded. + Nodes can share the cluster configuration when needed (for instance if SHA1 does not match) using this exact same format. + +CLIENTS + + - Clients may be configured to use slaves to perform reads, when read-after-write consistency is not required. diff --git a/src/Makefile b/src/Makefile index acbc9b5f..f99be4b9 100644 --- a/src/Makefile +++ b/src/Makefile @@ -25,7 +25,7 @@ PREFIX= /usr/local INSTALL_BIN= $(PREFIX)/bin INSTALL= cp -p -OBJ = adlist.o ae.o anet.o dict.o redis.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o dscache.o pubsub.o multi.o debug.o sort.o intset.o syncio.o diskstore.o endian.o +OBJ = adlist.o ae.o anet.o dict.o redis.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o dscache.o pubsub.o multi.o debug.o sort.o intset.o syncio.o diskstore.o cluster.o crc16.o endian.o BENCHOBJ = ae.o anet.o redis-benchmark.o sds.o adlist.o zmalloc.o CLIOBJ = anet.o sds.o adlist.o redis-cli.o zmalloc.o release.o CHECKDUMPOBJ = redis-check-dump.o lzf_c.o lzf_d.o @@ -105,6 +105,8 @@ t_zset.o: t_zset.c redis.h fmacros.h config.h ae.h sds.h dict.h adlist.h \ zmalloc.h anet.h zipmap.h ziplist.h intset.h version.h util.o: util.c redis.h fmacros.h config.h ae.h sds.h dict.h adlist.h \ zmalloc.h anet.h zipmap.h ziplist.h intset.h version.h +cluster.o: redis.h fmacros.h config.h ae.h sds.h dict.h adlist.h \ + zmalloc.h anet.h zipmap.h ziplist.h intset.h version.h ziplist.o: ziplist.c zmalloc.h ziplist.h zipmap.o: zipmap.c zmalloc.h zmalloc.o: zmalloc.c config.h diff --git a/src/cluster.c b/src/cluster.c new file mode 100644 index 00000000..aac52578 --- /dev/null +++ b/src/cluster.c @@ -0,0 +1,1299 @@ +#include "redis.h" + +#include + +void clusterAcceptHandler(aeEventLoop *el, int fd, void *privdata, int mask); +void clusterReadHandler(aeEventLoop *el, int fd, void *privdata, int mask); +void clusterSendPing(clusterLink *link, int type); +void clusterSendFail(char *nodename); +void clusterUpdateState(void); +int clusterNodeGetSlotBit(clusterNode *n, int slot); + +/* ----------------------------------------------------------------------------- + * Initialization + * -------------------------------------------------------------------------- */ + +void clusterGetRandomName(char *p) { + FILE *fp = fopen("/dev/urandom","r"); + char *charset = "0123456789abcdef"; + int j; + + if (!fp) { + redisLog(REDIS_WARNING, + "Unrecovarable error: can't open /dev/urandom:%s" ,strerror(errno)); + exit(1); + } + fread(p,REDIS_CLUSTER_NAMELEN,1,fp); + for (j = 0; j < REDIS_CLUSTER_NAMELEN; j++) + p[j] = charset[p[j] & 0x0F]; + fclose(fp); +} + +int clusterLoadConfig(char *filename) { + FILE *fp = fopen(filename,"r"); + + if (fp == NULL) return REDIS_ERR; + fclose(fp); + + redisLog(REDIS_NOTICE,"Node configuration loaded, I'm %.40s", + server.cluster.myself->name); + return REDIS_OK; + +fmterr: + redisLog(REDIS_WARNING,"Unrecovarable error: corrupted cluster.conf file."); + fclose(fp); + exit(1); +} + +void clusterInit(void) { + server.cluster.myself = createClusterNode(NULL,REDIS_NODE_MYSELF); + server.cluster.state = REDIS_CLUSTER_FAIL; + server.cluster.nodes = dictCreate(&clusterNodesDictType,NULL); + server.cluster.node_timeout = 15; + memset(server.cluster.migrating_slots_to,0, + sizeof(server.cluster.migrating_slots_to)); + memset(server.cluster.importing_slots_from,0, + sizeof(server.cluster.importing_slots_from)); + memset(server.cluster.slots,0, + sizeof(server.cluster.slots)); + if (clusterLoadConfig("cluster.conf") == REDIS_ERR) { + /* No configuration found. We will just use the random name provided + * by the createClusterNode() function. */ + redisLog(REDIS_NOTICE,"No cluster configuration found, I'm %.40s", + server.cluster.myself->name); + } + clusterAddNode(server.cluster.myself); + /* We need a listening TCP port for our cluster messaging needs */ + server.cfd = anetTcpServer(server.neterr, + server.port+REDIS_CLUSTER_PORT_INCR, server.bindaddr); + if (server.cfd == -1) { + redisLog(REDIS_WARNING, "Opening cluster TCP port: %s", server.neterr); + exit(1); + } + if (aeCreateFileEvent(server.el, server.cfd, AE_READABLE, + clusterAcceptHandler, NULL) == AE_ERR) oom("creating file event"); +} + +/* ----------------------------------------------------------------------------- + * CLUSTER communication link + * -------------------------------------------------------------------------- */ + +clusterLink *createClusterLink(clusterNode *node) { + clusterLink *link = zmalloc(sizeof(*link)); + link->sndbuf = sdsempty(); + link->rcvbuf = sdsempty(); + link->node = node; + link->fd = -1; + return link; +} + +/* Free a cluster link, but does not free the associated node of course. + * Just this function will make sure that the original node associated + * with this link will have the 'link' field set to NULL. */ +void freeClusterLink(clusterLink *link) { + if (link->fd != -1) { + aeDeleteFileEvent(server.el, link->fd, AE_WRITABLE); + aeDeleteFileEvent(server.el, link->fd, AE_READABLE); + } + sdsfree(link->sndbuf); + sdsfree(link->rcvbuf); + if (link->node) + link->node->link = NULL; + close(link->fd); + zfree(link); +} + +void clusterAcceptHandler(aeEventLoop *el, int fd, void *privdata, int mask) { + int cport, cfd; + char cip[128]; + clusterLink *link; + REDIS_NOTUSED(el); + REDIS_NOTUSED(mask); + REDIS_NOTUSED(privdata); + + cfd = anetTcpAccept(server.neterr, fd, cip, &cport); + if (cfd == AE_ERR) { + redisLog(REDIS_VERBOSE,"Accepting cluster node: %s", server.neterr); + return; + } + redisLog(REDIS_VERBOSE,"Accepted cluster node %s:%d", cip, cport); + /* We need to create a temporary node in order to read the incoming + * packet in a valid contest. This node will be released once we + * read the packet and reply. */ + link = createClusterLink(NULL); + link->fd = cfd; + aeCreateFileEvent(server.el,cfd,AE_READABLE,clusterReadHandler,link); +} + +/* ----------------------------------------------------------------------------- + * Key space handling + * -------------------------------------------------------------------------- */ + +/* We have 4096 hash slots. The hash slot of a given key is obtained + * as the least significant 12 bits of the crc16 of the key. */ +unsigned int keyHashSlot(char *key, int keylen) { + return crc16(key,keylen) & 0x0FFF; +} + +/* ----------------------------------------------------------------------------- + * CLUSTER node API + * -------------------------------------------------------------------------- */ + +/* Create a new cluster node, with the specified flags. + * If "nodename" is NULL this is considered a first handshake and a random + * node name is assigned to this node (it will be fixed later when we'll + * receive the first pong). + * + * The node is created and returned to the user, but it is not automatically + * added to the nodes hash table. */ +clusterNode *createClusterNode(char *nodename, int flags) { + clusterNode *node = zmalloc(sizeof(*node)); + + if (nodename) + memcpy(node->name, nodename, REDIS_CLUSTER_NAMELEN); + else + clusterGetRandomName(node->name); + node->flags = flags; + memset(node->slots,0,sizeof(node->slots)); + node->numslaves = 0; + node->slaves = NULL; + node->slaveof = NULL; + node->ping_sent = node->pong_received = 0; + node->configdigest = NULL; + node->configdigest_ts = 0; + node->link = NULL; + return node; +} + +int clusterNodeRemoveSlave(clusterNode *master, clusterNode *slave) { + int j; + + for (j = 0; j < master->numslaves; j++) { + if (master->slaves[j] == slave) { + memmove(master->slaves+j,master->slaves+(j+1), + (master->numslaves-1)-j); + master->numslaves--; + return REDIS_OK; + } + } + return REDIS_ERR; +} + +int clusterNodeAddSlave(clusterNode *master, clusterNode *slave) { + int j; + + /* If it's already a slave, don't add it again. */ + for (j = 0; j < master->numslaves; j++) + if (master->slaves[j] == slave) return REDIS_ERR; + master->slaves = zrealloc(master->slaves, + sizeof(clusterNode*)*(master->numslaves+1)); + master->slaves[master->numslaves] = slave; + master->numslaves++; + return REDIS_OK; +} + +void clusterNodeResetSlaves(clusterNode *n) { + zfree(n->slaves); + n->numslaves = 0; +} + +void freeClusterNode(clusterNode *n) { + sds nodename; + + nodename = sdsnewlen(n->name, REDIS_CLUSTER_NAMELEN); + redisAssert(dictDelete(server.cluster.nodes,nodename) == DICT_OK); + sdsfree(nodename); + if (n->slaveof) clusterNodeRemoveSlave(n->slaveof, n); + if (n->link) freeClusterLink(n->link); + zfree(n); +} + +/* Add a node to the nodes hash table */ +int clusterAddNode(clusterNode *node) { + int retval; + + retval = dictAdd(server.cluster.nodes, + sdsnewlen(node->name,REDIS_CLUSTER_NAMELEN), node); + return (retval == DICT_OK) ? REDIS_OK : REDIS_ERR; +} + +/* Node lookup by name */ +clusterNode *clusterLookupNode(char *name) { + sds s = sdsnewlen(name, REDIS_CLUSTER_NAMELEN); + struct dictEntry *de; + + de = dictFind(server.cluster.nodes,s); + sdsfree(s); + if (de == NULL) return NULL; + return dictGetEntryVal(de); +} + +/* This is only used after the handshake. When we connect a given IP/PORT + * as a result of CLUSTER MEET we don't have the node name yet, so we + * pick a random one, and will fix it when we receive the PONG request using + * this function. */ +void clusterRenameNode(clusterNode *node, char *newname) { + int retval; + sds s = sdsnewlen(node->name, REDIS_CLUSTER_NAMELEN); + + redisLog(REDIS_DEBUG,"Renaming node %.40s into %.40s", + node->name, newname); + retval = dictDelete(server.cluster.nodes, s); + sdsfree(s); + redisAssert(retval == DICT_OK); + memcpy(node->name, newname, REDIS_CLUSTER_NAMELEN); + clusterAddNode(node); +} + +/* ----------------------------------------------------------------------------- + * CLUSTER messages exchange - PING/PONG and gossip + * -------------------------------------------------------------------------- */ + +/* Process the gossip section of PING or PONG packets. + * Note that this function assumes that the packet is already sanity-checked + * by the caller, not in the content of the gossip section, but in the + * length. */ +void clusterProcessGossipSection(clusterMsg *hdr, clusterLink *link) { + uint16_t count = ntohs(hdr->count); + clusterMsgDataGossip *g = (clusterMsgDataGossip*) hdr->data.ping.gossip; + clusterNode *sender = link->node ? link->node : clusterLookupNode(hdr->sender); + + while(count--) { + sds ci = sdsempty(); + uint16_t flags = ntohs(g->flags); + clusterNode *node; + + if (flags == 0) ci = sdscat(ci,"noflags,"); + if (flags & REDIS_NODE_MYSELF) ci = sdscat(ci,"myself,"); + if (flags & REDIS_NODE_MASTER) ci = sdscat(ci,"master,"); + if (flags & REDIS_NODE_SLAVE) ci = sdscat(ci,"slave,"); + if (flags & REDIS_NODE_PFAIL) ci = sdscat(ci,"fail?,"); + if (flags & REDIS_NODE_FAIL) ci = sdscat(ci,"fail,"); + if (flags & REDIS_NODE_HANDSHAKE) ci = sdscat(ci,"handshake,"); + if (flags & REDIS_NODE_NOADDR) ci = sdscat(ci,"noaddr,"); + if (ci[sdslen(ci)-1] == ',') ci[sdslen(ci)-1] = ' '; + + redisLog(REDIS_DEBUG,"GOSSIP %.40s %s:%d %s", + g->nodename, + g->ip, + ntohs(g->port), + ci); + sdsfree(ci); + + /* Update our state accordingly to the gossip sections */ + node = clusterLookupNode(g->nodename); + if (node != NULL) { + /* We already know this node. Let's start updating the last + * time PONG figure if it is newer than our figure. + * Note that it's not a problem if we have a PING already + * in progress against this node. */ + if (node->pong_received < ntohl(g->pong_received)) { + redisLog(REDIS_DEBUG,"Node pong_received updated by gossip"); + node->pong_received = ntohl(g->pong_received); + } + /* Mark this node as FAILED if we think it is possibly failing + * and another node also thinks it's failing. */ + if (node->flags & REDIS_NODE_PFAIL && + (flags & (REDIS_NODE_FAIL|REDIS_NODE_PFAIL))) + { + redisLog(REDIS_NOTICE,"Received a PFAIL acknowledge from node %.40s, marking node %.40s as FAIL!", hdr->sender, node->name); + node->flags &= ~REDIS_NODE_PFAIL; + node->flags |= REDIS_NODE_FAIL; + /* Broadcast the failing node name to everybody */ + clusterSendFail(node->name); + clusterUpdateState(); + } + } else { + /* If it's not in NOADDR state and we don't have it, we + * start an handshake process against this IP/PORT pairs. + * + * Note that we require that the sender of this gossip message + * is a well known node in our cluster, otherwise we risk + * joining another cluster. */ + if (sender && !(flags & REDIS_NODE_NOADDR)) { + clusterNode *newnode; + + redisLog(REDIS_DEBUG,"Adding the new node"); + newnode = createClusterNode(NULL,REDIS_NODE_HANDSHAKE); + memcpy(newnode->ip,g->ip,sizeof(g->ip)); + newnode->port = ntohs(g->port); + clusterAddNode(newnode); + } + } + + /* Next node */ + g++; + } +} + +/* IP -> string conversion. 'buf' is supposed to at least be 16 bytes. */ +void nodeIp2String(char *buf, clusterLink *link) { + struct sockaddr_in sa; + socklen_t salen = sizeof(sa); + + if (getpeername(link->fd, (struct sockaddr*) &sa, &salen) == -1) + redisPanic("getpeername() failed."); + strncpy(buf,inet_ntoa(sa.sin_addr),sizeof(link->node->ip)); +} + + +/* Update the node address to the IP address that can be extracted + * from link->fd, and at the specified port. */ +void nodeUpdateAddress(clusterNode *node, clusterLink *link, int port) { +} + +/* When this function is called, there is a packet to process starting + * at node->rcvbuf. Releasing the buffer is up to the caller, so this + * function should just handle the higher level stuff of processing the + * packet, modifying the cluster state if needed. + * + * The function returns 1 if the link is still valid after the packet + * was processed, otherwise 0 if the link was freed since the packet + * processing lead to some inconsistency error (for instance a PONG + * received from the wrong sender ID). */ +int clusterProcessPacket(clusterLink *link) { + clusterMsg *hdr = (clusterMsg*) link->rcvbuf; + uint32_t totlen = ntohl(hdr->totlen); + uint16_t type = ntohs(hdr->type); + clusterNode *sender; + + redisLog(REDIS_DEBUG,"--- packet to process %lu bytes (%lu) ---", + (unsigned long) totlen, sdslen(link->rcvbuf)); + if (totlen < 8) return 1; + if (totlen > sdslen(link->rcvbuf)) return 1; + if (type == CLUSTERMSG_TYPE_PING || type == CLUSTERMSG_TYPE_PONG || + type == CLUSTERMSG_TYPE_MEET) + { + uint16_t count = ntohs(hdr->count); + uint32_t explen; /* expected length of this packet */ + + explen = sizeof(clusterMsg)-sizeof(union clusterMsgData); + explen += (sizeof(clusterMsgDataGossip)*count); + if (totlen != explen) return 1; + } + if (type == CLUSTERMSG_TYPE_FAIL) { + uint32_t explen = sizeof(clusterMsg)-sizeof(union clusterMsgData); + + explen += sizeof(clusterMsgDataFail); + if (totlen != explen) return 1; + } + + sender = clusterLookupNode(hdr->sender); + if (type == CLUSTERMSG_TYPE_PING || type == CLUSTERMSG_TYPE_MEET) { + redisLog(REDIS_DEBUG,"Ping packet received: %p", link->node); + + /* Add this node if it is new for us and the msg type is MEET. + * In this stage we don't try to add the node with the right + * flags, slaveof pointer, and so forth, as this details will be + * resolved when we'll receive PONGs from the server. */ + if (!sender && type == CLUSTERMSG_TYPE_MEET) { + clusterNode *node; + + node = createClusterNode(NULL,REDIS_NODE_HANDSHAKE); + nodeIp2String(node->ip,link); + node->port = ntohs(hdr->port); + clusterAddNode(node); + } + + /* Get info from the gossip section */ + clusterProcessGossipSection(hdr,link); + + /* Anyway reply with a PONG */ + clusterSendPing(link,CLUSTERMSG_TYPE_PONG); + } else if (type == CLUSTERMSG_TYPE_PONG) { + int update = 0; + + redisLog(REDIS_DEBUG,"Pong packet received: %p", link->node); + if (link->node) { + if (link->node->flags & REDIS_NODE_HANDSHAKE) { + /* If we already have this node, try to change the + * IP/port of the node with the new one. */ + if (sender) { + redisLog(REDIS_WARNING, + "Handshake error: we already know node %.40s, updating the address if needed.", sender->name); + nodeUpdateAddress(sender,link,ntohs(hdr->port)); + freeClusterNode(link->node); /* will free the link too */ + return 0; + } + + /* First thing to do is replacing the random name with the + * right node name if this was an handshake stage. */ + clusterRenameNode(link->node, hdr->sender); + redisLog(REDIS_DEBUG,"Handshake with node %.40s completed.", + link->node->name); + link->node->flags &= ~REDIS_NODE_HANDSHAKE; + } else if (memcmp(link->node->name,hdr->sender, + REDIS_CLUSTER_NAMELEN) != 0) + { + /* If the reply has a non matching node ID we + * disconnect this node and set it as not having an associated + * address. */ + redisLog(REDIS_DEBUG,"PONG contains mismatching sender ID"); + link->node->flags |= REDIS_NODE_NOADDR; + freeClusterLink(link); + /* FIXME: remove this node if we already have it. + * + * If we already have it but the IP is different, use + * the new one if the old node is in FAIL, PFAIL, or NOADDR + * status... */ + return 0; + } + } + /* Update our info about the node */ + link->node->pong_received = time(NULL); + + /* Update master/slave info */ + if (sender) { + if (!memcmp(hdr->slaveof,REDIS_NODE_NULL_NAME, + sizeof(hdr->slaveof))) + { + sender->flags &= ~REDIS_NODE_SLAVE; + sender->flags |= REDIS_NODE_MASTER; + sender->slaveof = NULL; + } else { + clusterNode *master = clusterLookupNode(hdr->slaveof); + + sender->flags &= ~REDIS_NODE_MASTER; + sender->flags |= REDIS_NODE_SLAVE; + if (sender->numslaves) clusterNodeResetSlaves(sender); + if (master) clusterNodeAddSlave(master,sender); + } + } + + /* Update our info about served slots if this new node is serving + * slots that are not served from our point of view. */ + if (sender && sender->flags & REDIS_NODE_MASTER) { + int newslots, j; + + newslots = + memcmp(sender->slots,hdr->myslots,sizeof(hdr->myslots)) != 0; + memcpy(sender->slots,hdr->myslots,sizeof(hdr->myslots)); + if (newslots) { + for (j = 0; j < REDIS_CLUSTER_SLOTS; j++) { + if (clusterNodeGetSlotBit(sender,j)) { + if (server.cluster.slots[j] == sender) continue; + if (server.cluster.slots[j] == NULL || + server.cluster.slots[j]->flags & REDIS_NODE_FAIL) + { + server.cluster.slots[j] = sender; + update = 1; + } + } + } + } + } + + /* Get info from the gossip section */ + clusterProcessGossipSection(hdr,link); + + /* Update the cluster state if needed */ + if (update) clusterUpdateState(); + } else if (type == CLUSTERMSG_TYPE_FAIL && sender) { + clusterNode *failing; + + failing = clusterLookupNode(hdr->data.fail.about.nodename); + if (failing && !(failing->flags & REDIS_NODE_FAIL)) { + redisLog(REDIS_NOTICE, + "FAIL message received from %.40s about %.40s", + hdr->sender, hdr->data.fail.about.nodename); + failing->flags |= REDIS_NODE_FAIL; + failing->flags &= ~REDIS_NODE_PFAIL; + clusterUpdateState(); + } + } else { + redisLog(REDIS_NOTICE,"Received unknown packet type: %d", type); + } + return 1; +} + +/* This function is called when we detect the link with this node is lost. + We set the node as no longer connected. The Cluster Cron will detect + this connection and will try to get it connected again. + + Instead if the node is a temporary node used to accept a query, we + completely free the node on error. */ +void handleLinkIOError(clusterLink *link) { + freeClusterLink(link); +} + +/* Send data. This is handled using a trivial send buffer that gets + * consumed by write(). We don't try to optimize this for speed too much + * as this is a very low traffic channel. */ +void clusterWriteHandler(aeEventLoop *el, int fd, void *privdata, int mask) { + clusterLink *link = (clusterLink*) privdata; + ssize_t nwritten; + REDIS_NOTUSED(el); + REDIS_NOTUSED(mask); + + nwritten = write(fd, link->sndbuf, sdslen(link->sndbuf)); + if (nwritten <= 0) { + redisLog(REDIS_NOTICE,"I/O error writing to node link: %s", + strerror(errno)); + handleLinkIOError(link); + return; + } + link->sndbuf = sdsrange(link->sndbuf,nwritten,-1); + if (sdslen(link->sndbuf) == 0) + aeDeleteFileEvent(server.el, link->fd, AE_WRITABLE); +} + +/* Read data. Try to read the first field of the header first to check the + * full length of the packet. When a whole packet is in memory this function + * will call the function to process the packet. And so forth. */ +void clusterReadHandler(aeEventLoop *el, int fd, void *privdata, int mask) { + char buf[1024]; + ssize_t nread; + clusterMsg *hdr; + clusterLink *link = (clusterLink*) privdata; + int readlen; + REDIS_NOTUSED(el); + REDIS_NOTUSED(mask); + +again: + if (sdslen(link->rcvbuf) >= 4) { + hdr = (clusterMsg*) link->rcvbuf; + readlen = ntohl(hdr->totlen) - sdslen(link->rcvbuf); + } else { + readlen = 4 - sdslen(link->rcvbuf); + } + + nread = read(fd,buf,readlen); + if (nread == -1 && errno == EAGAIN) return; /* Just no data */ + + if (nread <= 0) { + /* I/O error... */ + redisLog(REDIS_NOTICE,"I/O error reading from node link: %s", + (nread == 0) ? "connection closed" : strerror(errno)); + handleLinkIOError(link); + return; + } else { + /* Read data and recast the pointer to the new buffer. */ + link->rcvbuf = sdscatlen(link->rcvbuf,buf,nread); + hdr = (clusterMsg*) link->rcvbuf; + } + + /* Total length obtained? read the payload now instead of burning + * cycles waiting for a new event to fire. */ + if (sdslen(link->rcvbuf) == 4) goto again; + + /* Whole packet in memory? We can process it. */ + if (sdslen(link->rcvbuf) == ntohl(hdr->totlen)) { + if (clusterProcessPacket(link)) { + sdsfree(link->rcvbuf); + link->rcvbuf = sdsempty(); + } + } +} + +/* Put stuff into the send buffer. */ +void clusterSendMessage(clusterLink *link, unsigned char *msg, size_t msglen) { + if (sdslen(link->sndbuf) == 0 && msglen != 0) + aeCreateFileEvent(server.el,link->fd,AE_WRITABLE, + clusterWriteHandler,link); + + link->sndbuf = sdscatlen(link->sndbuf, msg, msglen); +} + +/* Build the message header */ +void clusterBuildMessageHdr(clusterMsg *hdr, int type) { + int totlen; + + memset(hdr,0,sizeof(*hdr)); + hdr->type = htons(type); + memcpy(hdr->sender,server.cluster.myself->name,REDIS_CLUSTER_NAMELEN); + memcpy(hdr->myslots,server.cluster.myself->slots, + sizeof(hdr->myslots)); + memset(hdr->slaveof,0,REDIS_CLUSTER_NAMELEN); + if (server.cluster.myself->slaveof != NULL) { + memcpy(hdr->slaveof,server.cluster.myself->slaveof->name, + REDIS_CLUSTER_NAMELEN); + } + hdr->port = htons(server.port); + hdr->state = server.cluster.state; + memset(hdr->configdigest,0,32); /* FIXME: set config digest */ + + if (type == CLUSTERMSG_TYPE_FAIL) { + totlen = sizeof(clusterMsg)-sizeof(union clusterMsgData); + totlen += sizeof(clusterMsgDataFail); + } + hdr->totlen = htonl(totlen); + /* For PING, PONG, and MEET, fixing the totlen field is up to the caller */ +} + +/* Send a PING or PONG packet to the specified node, making sure to add enough + * gossip informations. */ +void clusterSendPing(clusterLink *link, int type) { + unsigned char buf[1024]; + clusterMsg *hdr = (clusterMsg*) buf; + int gossipcount = 0, totlen; + /* freshnodes is the number of nodes we can still use to populate the + * gossip section of the ping packet. Basically we start with the nodes + * we have in memory minus two (ourself and the node we are sending the + * message to). Every time we add a node we decrement the counter, so when + * it will drop to <= zero we know there is no more gossip info we can + * send. */ + int freshnodes = dictSize(server.cluster.nodes)-2; + + if (link->node && type == CLUSTERMSG_TYPE_PING) + link->node->ping_sent = time(NULL); + clusterBuildMessageHdr(hdr,type); + + /* Populate the gossip fields */ + while(freshnodes > 0 && gossipcount < 3) { + struct dictEntry *de = dictGetRandomKey(server.cluster.nodes); + clusterNode *this = dictGetEntryVal(de); + clusterMsgDataGossip *gossip; + int j; + + /* Not interesting to gossip about ourself. + * Nor to send gossip info about HANDSHAKE state nodes (zero info). */ + if (this == server.cluster.myself || + this->flags & REDIS_NODE_HANDSHAKE) { + freshnodes--; /* otherwise we may loop forever. */ + continue; + } + + /* Check if we already added this node */ + for (j = 0; j < gossipcount; j++) { + if (memcmp(hdr->data.ping.gossip[j].nodename,this->name, + REDIS_CLUSTER_NAMELEN) == 0) break; + } + if (j != gossipcount) continue; + + /* Add it */ + freshnodes--; + gossip = &(hdr->data.ping.gossip[gossipcount]); + memcpy(gossip->nodename,this->name,REDIS_CLUSTER_NAMELEN); + gossip->ping_sent = htonl(this->ping_sent); + gossip->pong_received = htonl(this->pong_received); + memcpy(gossip->ip,this->ip,sizeof(this->ip)); + gossip->port = htons(this->port); + gossip->flags = htons(this->flags); + gossipcount++; + } + totlen = sizeof(clusterMsg)-sizeof(union clusterMsgData); + totlen += (sizeof(clusterMsgDataGossip)*gossipcount); + hdr->count = htons(gossipcount); + hdr->totlen = htonl(totlen); + clusterSendMessage(link,buf,totlen); +} + +/* Send a message to all the nodes with a reliable link */ +void clusterBroadcastMessage(void *buf, size_t len) { + dictIterator *di; + dictEntry *de; + + di = dictGetIterator(server.cluster.nodes); + while((de = dictNext(di)) != NULL) { + clusterNode *node = dictGetEntryVal(de); + + if (!node->link) continue; + if (node->flags & (REDIS_NODE_MYSELF|REDIS_NODE_NOADDR)) continue; + clusterSendMessage(node->link,buf,len); + } + dictReleaseIterator(di); +} + +/* Send a FAIL message to all the nodes we are able to contact. + * The FAIL message is sent when we detect that a node is failing + * (REDIS_NODE_PFAIL) and we also receive a gossip confirmation of this: + * we switch the node state to REDIS_NODE_FAIL and ask all the other + * nodes to do the same ASAP. */ +void clusterSendFail(char *nodename) { + unsigned char buf[1024]; + clusterMsg *hdr = (clusterMsg*) buf; + + clusterBuildMessageHdr(hdr,CLUSTERMSG_TYPE_FAIL); + memcpy(hdr->data.fail.about.nodename,nodename,REDIS_CLUSTER_NAMELEN); + clusterBroadcastMessage(buf,ntohl(hdr->totlen)); +} + +/* ----------------------------------------------------------------------------- + * CLUSTER cron job + * -------------------------------------------------------------------------- */ + +/* This is executed 1 time every second */ +void clusterCron(void) { + dictIterator *di; + dictEntry *de; + int j; + time_t min_ping_sent = 0; + clusterNode *min_ping_node = NULL; + + /* Check if we have disconnected nodes and reestablish the connection. */ + di = dictGetIterator(server.cluster.nodes); + while((de = dictNext(di)) != NULL) { + clusterNode *node = dictGetEntryVal(de); + + if (node->flags & (REDIS_NODE_MYSELF|REDIS_NODE_NOADDR)) continue; + if (node->link == NULL) { + int fd; + clusterLink *link; + + fd = anetTcpNonBlockConnect(server.neterr, node->ip, + node->port+REDIS_CLUSTER_PORT_INCR); + if (fd == -1) continue; + link = createClusterLink(node); + link->fd = fd; + node->link = link; + aeCreateFileEvent(server.el,link->fd,AE_READABLE,clusterReadHandler,link); + /* If the node is flagged as MEET, we send a MEET message instead + * of a PING one, to force the receiver to add us in its node + * table. */ + clusterSendPing(link, node->flags & REDIS_NODE_MEET ? + CLUSTERMSG_TYPE_MEET : CLUSTERMSG_TYPE_PING); + /* We can clear the flag after the first packet is sent. + * If we'll never receive a PONG, we'll never send new packets + * to this node. Instead after the PONG is received and we + * are no longer in meet/handshake status, we want to send + * normal PING packets. */ + node->flags &= ~REDIS_NODE_MEET; + + redisLog(REDIS_NOTICE,"Connecting with Node %.40s at %s:%d\n", node->name, node->ip, node->port+REDIS_CLUSTER_PORT_INCR); + } + } + dictReleaseIterator(di); + + /* Ping some random node. Check a few random nodes and ping the one with + * the oldest ping_sent time */ + for (j = 0; j < 5; j++) { + de = dictGetRandomKey(server.cluster.nodes); + clusterNode *this = dictGetEntryVal(de); + + if (this->link == NULL) continue; + if (this->flags & (REDIS_NODE_MYSELF|REDIS_NODE_HANDSHAKE)) continue; + if (min_ping_node == NULL || min_ping_sent > this->ping_sent) { + min_ping_node = this; + min_ping_sent = this->ping_sent; + } + } + if (min_ping_node) { + redisLog(REDIS_DEBUG,"Pinging node %40s", min_ping_node->name); + clusterSendPing(min_ping_node->link, CLUSTERMSG_TYPE_PING); + } + + /* Iterate nodes to check if we need to flag something as failing */ + di = dictGetIterator(server.cluster.nodes); + while((de = dictNext(di)) != NULL) { + clusterNode *node = dictGetEntryVal(de); + int delay; + + if (node->flags & + (REDIS_NODE_MYSELF|REDIS_NODE_NOADDR|REDIS_NODE_HANDSHAKE| + REDIS_NODE_FAIL)) continue; + /* Check only if we already sent a ping and did not received + * a reply yet. */ + if (node->ping_sent == 0 || + node->ping_sent <= node->pong_received) continue; + + delay = time(NULL) - node->pong_received; + if (node->flags & REDIS_NODE_PFAIL) { + /* The PFAIL condition can be reversed without external + * help if it is not transitive (that is, if it does not + * turn into a FAIL state). */ + if (delay < server.cluster.node_timeout) + node->flags &= ~REDIS_NODE_PFAIL; + } else { + if (delay >= server.cluster.node_timeout) { + redisLog(REDIS_DEBUG,"*** NODE %.40s possibly failing", + node->name); + node->flags |= REDIS_NODE_PFAIL; + } + } + } + dictReleaseIterator(di); +} + +/* ----------------------------------------------------------------------------- + * Slots management + * -------------------------------------------------------------------------- */ + +/* Set the slot bit and return the old value. */ +int clusterNodeSetSlotBit(clusterNode *n, int slot) { + off_t byte = slot/8; + int bit = slot&7; + int old = (n->slots[byte] & (1<slots[byte] |= 1<slots[byte] & (1<slots[byte] &= ~(1<slots[byte] & (1<name); + return REDIS_OK; +} + +/* ----------------------------------------------------------------------------- + * Cluster state evaluation function + * -------------------------------------------------------------------------- */ +void clusterUpdateState(void) { + int ok = 1; + int j; + + for (j = 0; j < REDIS_CLUSTER_SLOTS; j++) { + if (server.cluster.slots[j] == NULL || + server.cluster.slots[j]->flags & (REDIS_NODE_FAIL)) + { + ok = 0; + break; + } + } + if (ok) { + if (server.cluster.state == REDIS_CLUSTER_NEEDHELP) { + server.cluster.state = REDIS_CLUSTER_NEEDHELP; + } else { + server.cluster.state = REDIS_CLUSTER_OK; + } + } else { + server.cluster.state = REDIS_CLUSTER_FAIL; + } +} + +/* ----------------------------------------------------------------------------- + * CLUSTER command + * -------------------------------------------------------------------------- */ + +void clusterCommand(redisClient *c) { + if (server.cluster_enabled == 0) { + addReplyError(c,"This instance has cluster support disabled"); + return; + } + + if (!strcasecmp(c->argv[1]->ptr,"meet") && c->argc == 4) { + clusterNode *n; + struct sockaddr_in sa; + long port; + + /* Perform sanity checks on IP/port */ + if (inet_aton(c->argv[2]->ptr,&sa.sin_addr) == 0) { + addReplyError(c,"Invalid IP address in MEET"); + return; + } + if (getLongFromObjectOrReply(c, c->argv[3], &port, NULL) != REDIS_OK || + port < 0 || port > (65535-REDIS_CLUSTER_PORT_INCR)) + { + addReplyError(c,"Invalid TCP port specified"); + return; + } + + /* Finally add the node to the cluster with a random name, this + * will get fixed in the first handshake (ping/pong). */ + n = createClusterNode(NULL,REDIS_NODE_HANDSHAKE|REDIS_NODE_MEET); + strncpy(n->ip,inet_ntoa(sa.sin_addr),sizeof(n->ip)); + n->port = port; + clusterAddNode(n); + addReply(c,shared.ok); + } else if (!strcasecmp(c->argv[1]->ptr,"nodes") && c->argc == 2) { + sds ci = sdsempty(); + dictIterator *di; + dictEntry *de; + robj *o; + + di = dictGetIterator(server.cluster.nodes); + while((de = dictNext(di)) != NULL) { + clusterNode *node = dictGetEntryVal(de); + + /* Node coordinates */ + ci = sdscatprintf(ci,"%.40s %s:%d ", + node->name, + node->ip, + node->port); + + /* Flags */ + if (node->flags == 0) ci = sdscat(ci,"noflags,"); + if (node->flags & REDIS_NODE_MYSELF) ci = sdscat(ci,"myself,"); + if (node->flags & REDIS_NODE_MASTER) ci = sdscat(ci,"master,"); + if (node->flags & REDIS_NODE_SLAVE) ci = sdscat(ci,"slave,"); + if (node->flags & REDIS_NODE_PFAIL) ci = sdscat(ci,"fail?,"); + if (node->flags & REDIS_NODE_FAIL) ci = sdscat(ci,"fail,"); + if (node->flags & REDIS_NODE_HANDSHAKE) ci =sdscat(ci,"handshake,"); + if (node->flags & REDIS_NODE_NOADDR) ci = sdscat(ci,"noaddr,"); + if (ci[sdslen(ci)-1] == ',') ci[sdslen(ci)-1] = ' '; + + /* Slave of... or just "-" */ + if (node->slaveof) + ci = sdscatprintf(ci,"%.40s ",node->slaveof->name); + else + ci = sdscatprintf(ci,"- "); + + /* Latency from the POV of this node, link status */ + ci = sdscatprintf(ci,"%ld %ld %s\n", + (long) node->ping_sent, + (long) node->pong_received, + node->link ? "connected" : "disconnected"); + } + dictReleaseIterator(di); + o = createObject(REDIS_STRING,ci); + addReplyBulk(c,o); + decrRefCount(o); + } else if (!strcasecmp(c->argv[1]->ptr,"addslots") && c->argc >= 3) { + int j; + long long slot; + unsigned char *slots = zmalloc(REDIS_CLUSTER_SLOTS); + + memset(slots,0,REDIS_CLUSTER_SLOTS); + /* Check that all the arguments are parsable and that all the + * slots are not already busy. */ + for (j = 2; j < c->argc; j++) { + if (getLongLongFromObject(c->argv[j],&slot) != REDIS_OK || + slot < 0 || slot > REDIS_CLUSTER_SLOTS) + { + addReplyError(c,"Invalid or out of range slot index"); + zfree(slots); + return; + } + if (server.cluster.slots[slot]) { + addReplyErrorFormat(c,"Slot %lld is already busy", slot); + zfree(slots); + return; + } + if (slots[slot]++ == 1) { + addReplyErrorFormat(c,"Slot %d specified multiple times", + (int)slot); + zfree(slots); + return; + } + } + for (j = 0; j < REDIS_CLUSTER_SLOTS; j++) { + if (slots[j]) { + int retval = clusterAddSlot(server.cluster.myself,j); + + redisAssert(retval == REDIS_OK); + } + } + zfree(slots); + clusterUpdateState(); + addReply(c,shared.ok); + } else if (!strcasecmp(c->argv[1]->ptr,"info") && c->argc == 2) { + char *statestr[] = {"ok","fail","needhelp"}; + int slots_assigned = 0, slots_ok = 0, slots_pfail = 0, slots_fail = 0; + int j; + + for (j = 0; j < REDIS_CLUSTER_SLOTS; j++) { + clusterNode *n = server.cluster.slots[j]; + + if (n == NULL) continue; + slots_assigned++; + if (n->flags & REDIS_NODE_FAIL) { + slots_fail++; + } else if (n->flags & REDIS_NODE_PFAIL) { + slots_pfail++; + } else { + slots_ok++; + } + } + + sds info = sdscatprintf(sdsempty(), + "cluster_state:%s\r\n" + "cluster_slots_assigned:%d\r\n" + "cluster_slots_ok:%d\r\n" + "cluster_slots_pfail:%d\r\n" + "cluster_slots_fail:%d\r\n" + , statestr[server.cluster.state], + slots_assigned, + slots_ok, + slots_pfail, + slots_fail + ); + addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n", + (unsigned long)sdslen(info))); + addReplySds(c,info); + addReply(c,shared.crlf); + } else { + addReplyError(c,"Wrong CLUSTER subcommand or number of arguments"); + } +} + +/* ----------------------------------------------------------------------------- + * RESTORE and MIGRATE commands + * -------------------------------------------------------------------------- */ + +/* RESTORE key ttl serialized-value */ +void restoreCommand(redisClient *c) { + FILE *fp; + char buf[64]; + robj *o; + unsigned char *data; + long ttl; + + /* Make sure this key does not already exist here... */ + if (dbExists(c->db,c->argv[1])) { + addReplyError(c,"Target key name is busy."); + return; + } + + /* Check if the TTL value makes sense */ + if (getLongFromObjectOrReply(c,c->argv[2],&ttl,NULL) != REDIS_OK) { + return; + } else if (ttl < 0) { + addReplyError(c,"Invalid TTL value, must be >= 0"); + return; + } + + /* rdbLoadObject() only works against file descriptors so we need to + * dump the serialized object into a file and reload. */ + snprintf(buf,sizeof(buf),"redis-restore-%d.tmp",getpid()); + fp = fopen(buf,"w+"); + if (!fp) { + redisLog(REDIS_WARNING,"Can't open tmp file for RESTORE: %s", + strerror(errno)); + addReplyErrorFormat(c,"RESTORE failed, tmp file creation error: %s", + strerror(errno)); + return; + } + unlink(buf); + + /* Write the actual data and rewind the file */ + data = (unsigned char*) c->argv[3]->ptr; + if (fwrite(data+1,sdslen((sds)data)-1,1,fp) != 1) { + redisLog(REDIS_WARNING,"Can't write against tmp file for RESTORE: %s", + strerror(errno)); + addReplyError(c,"RESTORE failed, tmp file I/O error."); + fclose(fp); + return; + } + rewind(fp); + + /* Finally create the object from the serialized dump and + * store it at the specified key. */ + o = rdbLoadObject(data[0],fp); + if (o == NULL) { + addReplyError(c,"Bad data format."); + fclose(fp); + return; + } + fclose(fp); + + /* Create the key and set the TTL if any */ + dbAdd(c->db,c->argv[1],o); + if (ttl) setExpire(c->db,c->argv[1],time(NULL)+ttl); + addReply(c,shared.ok); +} + +/* MIGRATE host port key dbid timeout */ +void migrateCommand(redisClient *c) { + int fd; + long timeout; + long dbid; + char buf[64]; + FILE *fp; + time_t ttl; + robj *o; + unsigned char type; + off_t payload_len; + + /* Sanity check */ + if (getLongFromObjectOrReply(c,c->argv[5],&timeout,NULL) != REDIS_OK) + return; + if (getLongFromObjectOrReply(c,c->argv[4],&dbid,NULL) != REDIS_OK) + return; + if (timeout <= 0) timeout = 1; + + /* Check if the key is here. If not we reply with success as there is + * nothing to migrate (for instance the key expired in the meantime), but + * we include such information in the reply string. */ + if ((o = lookupKeyRead(c->db,c->argv[3])) == NULL) { + addReplySds(c,sdsnew("+NOKEY")); + return; + } + + /* Connect */ + fd = anetTcpNonBlockConnect(server.neterr,c->argv[1]->ptr, + atoi(c->argv[2]->ptr)); + if (fd == -1) { + addReplyErrorFormat(c,"Can't connect to target node: %s", + server.neterr); + return; + } + if ((aeWait(fd,AE_WRITABLE,timeout*1000) & AE_WRITABLE) == 0) { + addReplyError(c,"Timeout connecting to the client"); + return; + } + + /* Create temp file */ + snprintf(buf,sizeof(buf),"redis-migrate-%d.tmp",getpid()); + fp = fopen(buf,"w+"); + if (!fp) { + redisLog(REDIS_WARNING,"Can't open tmp file for MIGRATE: %s", + strerror(errno)); + addReplyErrorFormat(c,"MIGRATE failed, tmp file creation error: %s.", + strerror(errno)); + return; + } + unlink(buf); + + /* Build the SELECT + RESTORE query writing it in our temp file. */ + if (fwriteBulkCount(fp,'*',2) == 0) goto file_wr_err; + if (fwriteBulkString(fp,"SELECT",6) == 0) goto file_wr_err; + if (fwriteBulkLongLong(fp,dbid) == 0) goto file_wr_err; + + ttl = getExpire(c->db,c->argv[3]); + type = o->type; + if (fwriteBulkCount(fp,'*',4) == 0) goto file_wr_err; + if (fwriteBulkString(fp,"RESTORE",7) == 0) goto file_wr_err; + if (fwriteBulkObject(fp,c->argv[3]) == 0) goto file_wr_err; + if (fwriteBulkLongLong(fp, (ttl == -1) ? 0 : ttl) == 0) goto file_wr_err; + + /* Finally the last argument that is the serailized object payload + * in the form: . */ + payload_len = rdbSavedObjectLen(o); + if (fwriteBulkCount(fp,'$',payload_len+1) == 0) goto file_wr_err; + if (fwrite(&type,1,1,fp) == 0) goto file_wr_err; + if (rdbSaveObject(fp,o) == -1) goto file_wr_err; + if (fwrite("\r\n",2,1,fp) == 0) goto file_wr_err; + + /* Tranfer the query to the other node */ + rewind(fp); + { + char buf[4096]; + size_t nread; + + while ((nread = fread(buf,1,sizeof(buf),fp)) != 0) { + int nwritten; + + nwritten = syncWrite(fd,buf,nread,timeout); + if (nwritten != (signed)nread) goto socket_wr_err; + } + if (ferror(fp)) goto file_rd_err; + } + + /* Read back the reply */ + { + char buf1[1024]; + char buf2[1024]; + + /* Read the two replies */ + if (syncReadLine(fd, buf1, sizeof(buf1), timeout) <= 0) + goto socket_rd_err; + if (syncReadLine(fd, buf2, sizeof(buf2), timeout) <= 0) + goto socket_rd_err; + if (buf1[0] == '-' || buf2[0] == '-') { + addReplyErrorFormat(c,"Target instance replied with error: %s", + (buf1[0] == '-') ? buf1+1 : buf2+1); + } else { + dbDelete(c->db,c->argv[3]); + addReply(c,shared.ok); + } + } + fclose(fp); + close(fd); + return; + +file_wr_err: + redisLog(REDIS_WARNING,"Can't write on tmp file for MIGRATE: %s", + strerror(errno)); + addReplyErrorFormat(c,"MIGRATE failed, tmp file write error: %s.", + strerror(errno)); + fclose(fp); + close(fd); + +file_rd_err: + redisLog(REDIS_WARNING,"Can't read from tmp file for MIGRATE: %s", + strerror(errno)); + addReplyErrorFormat(c,"MIGRATE failed, tmp file read error: %s.", + strerror(errno)); + fclose(fp); + close(fd); + +socket_wr_err: + redisLog(REDIS_NOTICE,"Can't write to target node for MIGRATE: %s", + strerror(errno)); + addReplyErrorFormat(c,"MIGRATE failed, writing to target node: %s.", + strerror(errno)); + fclose(fp); + close(fd); + +socket_rd_err: + redisLog(REDIS_NOTICE,"Can't read from target node for MIGRATE: %s", + strerror(errno)); + addReplyErrorFormat(c,"MIGRATE failed, reading from target node: %s.", + strerror(errno)); + fclose(fp); + close(fd); +} + +/* ----------------------------------------------------------------------------- + * Cluster functions related to serving / redirecting clients + * -------------------------------------------------------------------------- */ + +/* Return the pointer to the cluster node that is able to serve the query + * as all the keys belong to hash slots for which the node is in charge. + * + * If keys in query spawn multiple nodes NULL is returned. */ +clusterNode *getNodeByQuery(redisClient *c, struct redisCommand *cmd, robj **argv, int argc, int *hashslot) { + clusterNode *n = NULL; + multiState *ms, _ms; + multiCmd mc; + int i; + + /* We handle all the cases as if they were EXEC commands, so we have + * a common code path for everything */ + if (cmd->proc == execCommand) { + /* If REDIS_MULTI flag is not set EXEC is just going to return an + * error. */ + if (!(c->flags & REDIS_MULTI)) return server.cluster.myself; + ms = &c->mstate; + } else { + /* Create a fake Multi State structure, with just one command */ + ms = &_ms; + _ms.commands = &mc; + _ms.count = 1; + mc.argv = argv; + mc.argc = argc; + mc.cmd = cmd; + } + + for (i = 0; i < ms->count; i++) { + struct redisCommand *mcmd; + robj **margv; + int margc, *keyindex, numkeys, j; + + mcmd = ms->commands[i].cmd; + margc = ms->commands[i].argc; + margv = ms->commands[i].argv; + + keyindex = getKeysFromCommand(mcmd,margv,margc,&numkeys, + REDIS_GETKEYS_PRELOAD); + for (j = 0; j < numkeys; j++) { + int slot = keyHashSlot((char*)margv[keyindex[j]]->ptr, + sdslen(margv[keyindex[j]]->ptr)); + struct clusterNode *slotnode; + + slotnode = server.cluster.slots[slot]; + if (hashslot) *hashslot = slot; + /* Node not assigned? (Should never happen actually + * if we reached this function). + * Different node than the previous one? + * Return NULL, the cluster can't serve multi-node requests */ + if (slotnode == NULL || (n && slotnode != n)) { + getKeysFreeResult(keyindex); + return NULL; + } else { + n = slotnode; + } + } + getKeysFreeResult(keyindex); + } + return (n == NULL) ? server.cluster.myself : n; +} diff --git a/src/config.c b/src/config.c index e0bf1574..e40fdeda 100644 --- a/src/config.c +++ b/src/config.c @@ -285,6 +285,10 @@ void loadServerConfig(char *filename) { err = "Target command name already exists"; goto loaderr; } } + } else if (!strcasecmp(argv[0],"cluster-enabled") && argc == 2) { + if ((server.cluster_enabled = yesnotoi(argv[1])) == -1) { + err = "argument must be 'yes' or 'no'"; goto loaderr; + } } else { err = "Bad directive or wrong number of arguments"; goto loaderr; } diff --git a/src/crc16.c b/src/crc16.c new file mode 100644 index 00000000..37fb4ca7 --- /dev/null +++ b/src/crc16.c @@ -0,0 +1,74 @@ +#include "redis.h" + +/* + * Copyright 2001-2010 Georges Menie (www.menie.org) + * Copyright 2010 Salvatore Sanfilippo (adapted to Redis coding style) + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the University of California, Berkeley nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* CRC16 implementation acording to CCITT standards */ + +static const uint16_t crc16tab[256]= { + 0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7, + 0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef, + 0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6, + 0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de, + 0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485, + 0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d, + 0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4, + 0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc, + 0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823, + 0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b, + 0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12, + 0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a, + 0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41, + 0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49, + 0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70, + 0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78, + 0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f, + 0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067, + 0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e, + 0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256, + 0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d, + 0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405, + 0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c, + 0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634, + 0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab, + 0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3, + 0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a, + 0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92, + 0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9, + 0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1, + 0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8, + 0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0 +}; + +uint16_t crc16(const char *buf, int len) { + int counter; + uint16_t crc = 0; + for (counter = 0; counter < len; counter++) + crc = (crc<<8) ^ crc16tab[((crc>>8) ^ *buf++)&0x00FF]; + return crc; +} diff --git a/src/db.c b/src/db.c index fea2f12e..85a22062 100644 --- a/src/db.c +++ b/src/db.c @@ -307,6 +307,10 @@ void existsCommand(redisClient *c) { void selectCommand(redisClient *c) { int id = atoi(c->argv[1]->ptr); + if (server.cluster_enabled) { + addReplyError(c,"SELECT is not allowed in cluster mode"); + return; + } if (selectDb(c,id) == REDIS_ERR) { addReplyError(c,"invalid DB index"); } else { @@ -428,6 +432,11 @@ void moveCommand(redisClient *c) { redisDb *src, *dst; int srcid; + if (server.cluster_enabled) { + addReplyError(c,"MOVE is not allowed in cluster mode"); + return; + } + /* Obtain source and target DB pointers */ src = c->db; srcid = c->db->id; diff --git a/src/rdb.c b/src/rdb.c index c9fb3e83..f14467d1 100644 --- a/src/rdb.c +++ b/src/rdb.c @@ -245,7 +245,7 @@ int rdbSaveDoubleValue(FILE *fp, double val) { return rdbWriteRaw(fp,buf,len); } -/* Save a Redis object. */ +/* Save a Redis object. Returns -1 on error, 0 on success. */ int rdbSaveObject(FILE *fp, robj *o) { int n, nwritten = 0; diff --git a/src/redis-cli.c b/src/redis-cli.c index 123231d4..1b23c0b1 100644 --- a/src/redis-cli.c +++ b/src/redis-cli.c @@ -394,15 +394,18 @@ static sds cliFormatReplyRaw(redisReply *r) { switch (r->type) { case REDIS_REPLY_NIL: /* Nothing... */ - break; + break; case REDIS_REPLY_ERROR: + out = sdscatlen(out,r->str,r->len); + out = sdscatlen(out,"\n",1); + break; case REDIS_REPLY_STATUS: case REDIS_REPLY_STRING: out = sdscatlen(out,r->str,r->len); - break; + break; case REDIS_REPLY_INTEGER: out = sdscatprintf(out,"%lld",r->integer); - break; + break; case REDIS_REPLY_ARRAY: for (i = 0; i < r->elements; i++) { if (i > 0) out = sdscat(out,config.mb_delim); @@ -410,7 +413,7 @@ static sds cliFormatReplyRaw(redisReply *r) { out = sdscatlen(out,tmp,sdslen(tmp)); sdsfree(tmp); } - break; + break; default: fprintf(stderr,"Unknown reply type: %d\n", r->type); exit(1); @@ -464,7 +467,15 @@ static int cliSendCommand(int argc, char **argv, int repeat) { return REDIS_OK; } - output_raw = !strcasecmp(command,"info"); + output_raw = 0; + if (!strcasecmp(command,"info") || + (argc == 2 && !strcasecmp(command,"cluster") && + (!strcasecmp(argv[1],"nodes") || + !strcasecmp(argv[1],"info")))) + { + output_raw = 1; + } + if (!strcasecmp(command,"help") || !strcasecmp(command,"?")) { cliOutputHelp(--argc, ++argv); return REDIS_OK; diff --git a/src/redis.c b/src/redis.c index 19fd912c..25205752 100644 --- a/src/redis.c +++ b/src/redis.c @@ -187,7 +187,10 @@ struct redisCommand redisCommandTable[] = { {"punsubscribe",punsubscribeCommand,-1,0,NULL,0,0,0,0,0}, {"publish",publishCommand,3,REDIS_CMD_FORCE_REPLICATION,NULL,0,0,0,0,0}, {"watch",watchCommand,-2,0,noPreloadGetKeys,1,-1,1,0,0}, - {"unwatch",unwatchCommand,1,0,NULL,0,0,0,0,0} + {"unwatch",unwatchCommand,1,0,NULL,0,0,0,0,0}, + {"cluster",clusterCommand,-2,0,NULL,0,0,0,0,0}, + {"restore",restoreCommand,3,0,NULL,0,0,0,0,0}, + {"migrate",migrateCommand,6,0,NULL,0,0,0,0,0} }; /*============================ Utility functions ============================ */ @@ -440,6 +443,17 @@ dictType keylistDictType = { dictListDestructor /* val destructor */ }; +/* Cluster nodes hash table, mapping nodes addresses 1.2.3.4:6379 to + * clusterNode structures. */ +dictType clusterNodesDictType = { + dictSdsHash, /* hash function */ + NULL, /* key dup */ + NULL, /* val dup */ + dictSdsKeyCompare, /* key compare */ + dictSdsDestructor, /* key destructor */ + NULL /* val destructor */ +}; + int htNeedsResize(dict *dict) { long long size, used; @@ -669,6 +683,9 @@ int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) { * to detect transfer failures. */ if (!(loops % 10)) replicationCron(); + /* Run other sub-systems specific cron jobs */ + if (server.cluster_enabled && !(loops % 10)) clusterCron(); + server.cronloops++; return 100; } @@ -823,6 +840,7 @@ void initServerConfig() { server.set_max_intset_entries = REDIS_SET_MAX_INTSET_ENTRIES; server.shutdown_asap = 0; server.cache_flush_delay = 0; + server.cluster_enabled = 0; updateLRUClock(); resetServerSaveParams(); @@ -945,6 +963,7 @@ void initServer() { } if (server.ds_enabled) dsInit(); + if (server.cluster_enabled) clusterInit(); srand(time(NULL)^getpid()); } @@ -1051,6 +1070,27 @@ int processCommand(redisClient *c) { return REDIS_OK; } + /* If cluster is enabled, redirect here */ + if (server.cluster_enabled && + !(cmd->getkeys_proc == NULL && cmd->firstkey == 0)) { + int hashslot; + + if (server.cluster.state != REDIS_CLUSTER_OK) { + addReplyError(c,"The cluster is down. Check with CLUSTER INFO for more information"); + return REDIS_OK; + } else { + clusterNode *n = getNodeByQuery(c,cmd,c->argv,c->argc,&hashslot); + if (n == NULL) { + addReplyError(c,"Invalid cross-node request"); + return REDIS_OK; + } else if (n != server.cluster.myself) { + addReplySds(c,sdscatprintf(sdsempty(), + "-MOVED %d %s:%d\r\n",hashslot,n->ip,n->port)); + return REDIS_OK; + } + } + } + /* Handle the maxmemory directive. * * First we try to free some memory if possible (if there are volatile diff --git a/src/redis.h b/src/redis.h index b5188330..0ddd4730 100644 --- a/src/redis.h +++ b/src/redis.h @@ -18,6 +18,7 @@ #include #include #include +#include #include "ae.h" /* Event driven programming library */ #include "sds.h" /* Dynamic safe strings */ @@ -360,7 +361,123 @@ struct sharedObjectsStruct { *integers[REDIS_SHARED_INTEGERS]; }; -/* Global server state structure */ +/*----------------------------------------------------------------------------- + * Redis cluster data structures + *----------------------------------------------------------------------------*/ + +#define REDIS_CLUSTER_SLOTS 4096 +#define REDIS_CLUSTER_OK 0 /* Everything looks ok */ +#define REDIS_CLUSTER_FAIL 1 /* The cluster can't work */ +#define REDIS_CLUSTER_NEEDHELP 2 /* The cluster works, but needs some help */ +#define REDIS_CLUSTER_NAMELEN 40 /* sha1 hex length */ +#define REDIS_CLUSTER_PORT_INCR 10000 /* Cluster port = baseport + PORT_INCR */ + +struct clusterNode; + +/* clusterLink encapsulates everything needed to talk with a remote node. */ +typedef struct clusterLink { + int fd; /* TCP socket file descriptor */ + sds sndbuf; /* Packet send buffer */ + sds rcvbuf; /* Packet reception buffer */ + struct clusterNode *node; /* Node related to this link if any, or NULL */ +} clusterLink; + +/* Node flags */ +#define REDIS_NODE_MASTER 1 /* The node is a master */ +#define REDIS_NODE_SLAVE 2 /* The node is a slave */ +#define REDIS_NODE_PFAIL 4 /* Failure? Need acknowledge */ +#define REDIS_NODE_FAIL 8 /* The node is believed to be malfunctioning */ +#define REDIS_NODE_MYSELF 16 /* This node is myself */ +#define REDIS_NODE_HANDSHAKE 32 /* We have still to exchange the first ping */ +#define REDIS_NODE_NOADDR 64 /* We don't know the address of this node */ +#define REDIS_NODE_MEET 128 /* Send a MEET message to this node */ +#define REDIS_NODE_NULL_NAME "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" + +struct clusterNode { + char name[REDIS_CLUSTER_NAMELEN]; /* Node name, hex string, sha1-size */ + int flags; /* REDIS_NODE_... */ + unsigned char slots[REDIS_CLUSTER_SLOTS/8]; /* slots handled by this node */ + int numslaves; /* Number of slave nodes, if this is a master */ + struct clusterNode **slaves; /* pointers to slave nodes */ + struct clusterNode *slaveof; /* pointer to the master node */ + time_t ping_sent; /* Unix time we sent latest ping */ + time_t pong_received; /* Unix time we received the pong */ + char *configdigest; /* Configuration digest of this node */ + time_t configdigest_ts; /* Configuration digest timestamp */ + char ip[16]; /* Latest known IP address of this node */ + int port; /* Latest known port of this node */ + clusterLink *link; /* TCP/IP link with this node */ +}; +typedef struct clusterNode clusterNode; + +typedef struct { + clusterNode *myself; /* This node */ + int state; /* REDIS_CLUSTER_OK, REDIS_CLUSTER_FAIL, ... */ + int node_timeout; + dict *nodes; /* Hash table of name -> clusterNode structures */ + clusterNode *migrating_slots_to[REDIS_CLUSTER_SLOTS]; + clusterNode *importing_slots_from[REDIS_CLUSTER_SLOTS]; + clusterNode *slots[REDIS_CLUSTER_SLOTS]; +} clusterState; + +/* Redis cluster messages header */ + +/* Note that the PING, PONG and MEET messages are actually the same exact + * kind of packet. PONG is the reply to ping, in the extact format as a PING, + * while MEET is a special PING that forces the receiver to add the sender + * as a node (if it is not already in the list). */ +#define CLUSTERMSG_TYPE_PING 0 /* Ping */ +#define CLUSTERMSG_TYPE_PONG 1 /* Pong (reply to Ping) */ +#define CLUSTERMSG_TYPE_MEET 2 /* Meet "let's join" message */ +#define CLUSTERMSG_TYPE_FAIL 3 /* Mark node xxx as failing */ + +/* Initially we don't know our "name", but we'll find it once we connect + * to the first node, using the getsockname() function. Then we'll use this + * address for all the next messages. */ +typedef struct { + char nodename[REDIS_CLUSTER_NAMELEN]; + uint32_t ping_sent; + uint32_t pong_received; + char ip[16]; /* IP address last time it was seen */ + uint16_t port; /* port last time it was seen */ + uint16_t flags; + uint32_t notused; /* for 64 bit alignment */ +} clusterMsgDataGossip; + +typedef struct { + char nodename[REDIS_CLUSTER_NAMELEN]; +} clusterMsgDataFail; + +union clusterMsgData { + /* PING, MEET and PONG */ + struct { + /* Array of N clusterMsgDataGossip structures */ + clusterMsgDataGossip gossip[1]; + } ping; + /* FAIL */ + struct { + clusterMsgDataFail about; + } fail; +}; + +typedef struct { + uint32_t totlen; /* Total length of this message */ + uint16_t type; /* Message type */ + uint16_t count; /* Only used for some kind of messages. */ + char sender[REDIS_CLUSTER_NAMELEN]; /* Name of the sender node */ + unsigned char myslots[REDIS_CLUSTER_SLOTS/8]; + char slaveof[REDIS_CLUSTER_NAMELEN]; + char configdigest[32]; + uint16_t port; /* Sender TCP base port */ + unsigned char state; /* Cluster state from the POV of the sender */ + unsigned char notused[5]; /* Reserved for future use. For alignment. */ + union clusterMsgData data; +} clusterMsg; + +/*----------------------------------------------------------------------------- + * Global server state + *----------------------------------------------------------------------------*/ + struct redisServer { /* General */ pthread_t mainthread; @@ -373,6 +490,7 @@ struct redisServer { char *unixsocket; int ipfd; int sofd; + int cfd; list *clients; list *slaves, *monitors; char neterr[ANET_ERR_LEN]; @@ -499,6 +617,8 @@ struct redisServer { /* Misc */ unsigned lruclock:22; /* clock incrementing every minute, for LRU */ unsigned lruclock_padding:10; + int cluster_enabled; + clusterState cluster; }; typedef struct pubsubPattern { @@ -633,6 +753,7 @@ extern struct redisServer server; extern struct sharedObjectsStruct shared; extern dictType setDictType; extern dictType zsetDictType; +extern dictType clusterNodesDictType; extern double R_Zero, R_PosInf, R_NegInf, R_Nan; dictType hashDictType; @@ -747,6 +868,7 @@ int fwriteBulkString(FILE *fp, char *s, unsigned long len); int fwriteBulkDouble(FILE *fp, double d); int fwriteBulkLongLong(FILE *fp, long long l); int fwriteBulkObject(FILE *fp, robj *obj); +int fwriteBulkCount(FILE *fp, char prefix, int count); /* Replication */ void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc); @@ -923,6 +1045,15 @@ int *noPreloadGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numke int *renameGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags); int *zunionInterGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags); +/* Cluster */ +void clusterInit(void); +unsigned short crc16(const char *buf, int len); +unsigned int keyHashSlot(char *key, int keylen); +clusterNode *createClusterNode(char *nodename, int flags); +int clusterAddNode(clusterNode *node); +void clusterCron(void); +clusterNode *getNodeByQuery(redisClient *c, struct redisCommand *cmd, robj **argv, int argc, int *hashslot); + /* Git SHA1 */ char *redisGitSHA1(void); char *redisGitDirty(void); @@ -1045,6 +1176,9 @@ void punsubscribeCommand(redisClient *c); void publishCommand(redisClient *c); void watchCommand(redisClient *c); void unwatchCommand(redisClient *c); +void clusterCommand(redisClient *c); +void restoreCommand(redisClient *c); +void migrateCommand(redisClient *c); #if defined(__GNUC__) void *calloc(size_t count, size_t size) __attribute__ ((deprecated)); diff --git a/src/syncio.c b/src/syncio.c index 28ac1811..3d0e0451 100644 --- a/src/syncio.c +++ b/src/syncio.c @@ -107,6 +107,7 @@ int syncReadLine(int fd, char *ptr, ssize_t size, int timeout) { int fwriteBulkString(FILE *fp, char *s, unsigned long len) { char cbuf[128]; int clen; + cbuf[0] = '$'; clen = 1+ll2string(cbuf+1,sizeof(cbuf)-1,len); cbuf[clen++] = '\r'; @@ -117,6 +118,19 @@ int fwriteBulkString(FILE *fp, char *s, unsigned long len) { return 1; } +/* Write a multi bulk count in the form "*\r\n" */ +int fwriteBulkCount(FILE *fp, char prefix, int count) { + char cbuf[128]; + int clen; + + cbuf[0] = prefix; + clen = 1+ll2string(cbuf+1,sizeof(cbuf)-1,count); + cbuf[clen++] = '\r'; + cbuf[clen++] = '\n'; + if (fwrite(cbuf,clen,1,fp) == 0) return 0; + return 1; +} + /* Write a double value in bulk format $\r\n\r\n */ int fwriteBulkDouble(FILE *fp, double d) { char buf[128], dbuf[128]; From 143d0077ba07fba8d662092c309d51b99270e648 Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 29 Mar 2011 17:52:02 +0200 Subject: [PATCH 048/146] CLUSTER file updated with warning --- CLUSTER | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CLUSTER b/CLUSTER index bfe5234a..0e836cb1 100644 --- a/CLUSTER +++ b/CLUSTER @@ -1,5 +1,9 @@ TODO +*** WARNING: all the following problably has some meaning only for +*** me (antirez), most info are not updated, so please consider this file +*** as a private TODO list / brainstorming. + - disconnect FAIL clients after some pong idle time. --------------------------------- From e6f0a7b237b19881d73b145960ed68202f4bde49 Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 29 Mar 2011 18:02:34 +0200 Subject: [PATCH 049/146] todo list for a first cluster howto guide for early testers --- CLUSTER | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/CLUSTER b/CLUSTER index 0e836cb1..be903711 100644 --- a/CLUSTER +++ b/CLUSTER @@ -1,4 +1,25 @@ +CLUSTER README +============== + +Redis Cluster is currenty a work in progress, however there are a few things +that you can do already with it to see how it works. + +The following guide show you how to setup a three nodes cluster and issue some +basic command against it. + +... WORK IN PROGRESS ... + +1) Show MIGRATE +2) Show CLUSTER MEET +3) Show link status detection with CLUSTER NODES +4) Show how to add slots with CLUSTER ADDSLOTS +5) Show redirection +6) Show cluster down + +... WORK IN PROGRESS ... + TODO +==== *** WARNING: all the following problably has some meaning only for *** me (antirez), most info are not updated, so please consider this file From c7c7cfbddca1325e600e5c0fd4cfcfde38cc0758 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 30 Mar 2011 14:58:19 +0200 Subject: [PATCH 050/146] cluster configuration saving --- src/cluster.c | 109 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 71 insertions(+), 38 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index aac52578..b3fcd1ea 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1,6 +1,8 @@ #include "redis.h" #include +#include +#include void clusterAcceptHandler(aeEventLoop *el, int fd, void *privdata, int mask); void clusterReadHandler(aeEventLoop *el, int fd, void *privdata, int mask); @@ -8,6 +10,7 @@ void clusterSendPing(clusterLink *link, int type); void clusterSendFail(char *nodename); void clusterUpdateState(void); int clusterNodeGetSlotBit(clusterNode *n, int slot); +sds clusterGenNodesDescription(void); /* ----------------------------------------------------------------------------- * Initialization @@ -31,7 +34,8 @@ void clusterGetRandomName(char *p) { int clusterLoadConfig(char *filename) { FILE *fp = fopen(filename,"r"); - + + return REDIS_ERR; if (fp == NULL) return REDIS_ERR; fclose(fp); @@ -45,6 +49,25 @@ fmterr: exit(1); } +/* Cluster node configuration is exactly the same as CLUSTER NODES output. + * + * This function writes the node config and returns 0, on error -1 + * is returned. */ +int clusterSaveConfig(char *filename) { + sds ci = clusterGenNodesDescription(); + int fd; + + if ((fd = open(filename,O_WRONLY|O_CREAT,0644)) == -1) goto err; + if (write(fd,ci,sdslen(ci)) != (ssize_t)sdslen(ci)) goto err; + close(fd); + sdsfree(ci); + return 0; + +err: + sdsfree(ci); + return -1; +} + void clusterInit(void) { server.cluster.myself = createClusterNode(NULL,REDIS_NODE_MYSELF); server.cluster.state = REDIS_CLUSTER_FAIL; @@ -61,6 +84,10 @@ void clusterInit(void) { * by the createClusterNode() function. */ redisLog(REDIS_NOTICE,"No cluster configuration found, I'm %.40s", server.cluster.myself->name); + if (clusterSaveConfig("cluster.conf") == -1) { + redisLog(REDIS_WARNING,"Fatal: can't update cluster config file."); + exit(1); + } } clusterAddNode(server.cluster.myself); /* We need a listening TCP port for our cluster messaging needs */ @@ -874,6 +901,48 @@ void clusterUpdateState(void) { * CLUSTER command * -------------------------------------------------------------------------- */ +sds clusterGenNodesDescription(void) { + sds ci = sdsempty(); + dictIterator *di; + dictEntry *de; + + di = dictGetIterator(server.cluster.nodes); + while((de = dictNext(di)) != NULL) { + clusterNode *node = dictGetEntryVal(de); + + /* Node coordinates */ + ci = sdscatprintf(ci,"%.40s %s:%d ", + node->name, + node->ip, + node->port); + + /* Flags */ + if (node->flags == 0) ci = sdscat(ci,"noflags,"); + if (node->flags & REDIS_NODE_MYSELF) ci = sdscat(ci,"myself,"); + if (node->flags & REDIS_NODE_MASTER) ci = sdscat(ci,"master,"); + if (node->flags & REDIS_NODE_SLAVE) ci = sdscat(ci,"slave,"); + if (node->flags & REDIS_NODE_PFAIL) ci = sdscat(ci,"fail?,"); + if (node->flags & REDIS_NODE_FAIL) ci = sdscat(ci,"fail,"); + if (node->flags & REDIS_NODE_HANDSHAKE) ci =sdscat(ci,"handshake,"); + if (node->flags & REDIS_NODE_NOADDR) ci = sdscat(ci,"noaddr,"); + if (ci[sdslen(ci)-1] == ',') ci[sdslen(ci)-1] = ' '; + + /* Slave of... or just "-" */ + if (node->slaveof) + ci = sdscatprintf(ci,"%.40s ",node->slaveof->name); + else + ci = sdscatprintf(ci,"- "); + + /* Latency from the POV of this node, link status */ + ci = sdscatprintf(ci,"%ld %ld %s\n", + (long) node->ping_sent, + (long) node->pong_received, + node->link ? "connected" : "disconnected"); + } + dictReleaseIterator(di); + return ci; +} + void clusterCommand(redisClient *c) { if (server.cluster_enabled == 0) { addReplyError(c,"This instance has cluster support disabled"); @@ -905,45 +974,9 @@ void clusterCommand(redisClient *c) { clusterAddNode(n); addReply(c,shared.ok); } else if (!strcasecmp(c->argv[1]->ptr,"nodes") && c->argc == 2) { - sds ci = sdsempty(); - dictIterator *di; - dictEntry *de; robj *o; + sds ci = clusterGenNodesDescription(); - di = dictGetIterator(server.cluster.nodes); - while((de = dictNext(di)) != NULL) { - clusterNode *node = dictGetEntryVal(de); - - /* Node coordinates */ - ci = sdscatprintf(ci,"%.40s %s:%d ", - node->name, - node->ip, - node->port); - - /* Flags */ - if (node->flags == 0) ci = sdscat(ci,"noflags,"); - if (node->flags & REDIS_NODE_MYSELF) ci = sdscat(ci,"myself,"); - if (node->flags & REDIS_NODE_MASTER) ci = sdscat(ci,"master,"); - if (node->flags & REDIS_NODE_SLAVE) ci = sdscat(ci,"slave,"); - if (node->flags & REDIS_NODE_PFAIL) ci = sdscat(ci,"fail?,"); - if (node->flags & REDIS_NODE_FAIL) ci = sdscat(ci,"fail,"); - if (node->flags & REDIS_NODE_HANDSHAKE) ci =sdscat(ci,"handshake,"); - if (node->flags & REDIS_NODE_NOADDR) ci = sdscat(ci,"noaddr,"); - if (ci[sdslen(ci)-1] == ',') ci[sdslen(ci)-1] = ' '; - - /* Slave of... or just "-" */ - if (node->slaveof) - ci = sdscatprintf(ci,"%.40s ",node->slaveof->name); - else - ci = sdscatprintf(ci,"- "); - - /* Latency from the POV of this node, link status */ - ci = sdscatprintf(ci,"%ld %ld %s\n", - (long) node->ping_sent, - (long) node->pong_received, - node->link ? "connected" : "disconnected"); - } - dictReleaseIterator(di); o = createObject(REDIS_STRING,ci); addReplyBulk(c,o); decrRefCount(o); From 4b72c5617f1b9a53daad5bb58bfc7faf05c8169a Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 30 Mar 2011 16:51:28 +0200 Subject: [PATCH 051/146] fix to configuration saving on first start --- src/cluster.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index b3fcd1ea..1ffe0cdc 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -44,7 +44,7 @@ int clusterLoadConfig(char *filename) { return REDIS_OK; fmterr: - redisLog(REDIS_WARNING,"Unrecovarable error: corrupted cluster.conf file."); + redisLog(REDIS_WARNING,"Unrecovarable error: corrupted redis-cluster.conf file."); fclose(fp); exit(1); } @@ -69,6 +69,8 @@ err: } void clusterInit(void) { + int saveconf = 0; + server.cluster.myself = createClusterNode(NULL,REDIS_NODE_MYSELF); server.cluster.state = REDIS_CLUSTER_FAIL; server.cluster.nodes = dictCreate(&clusterNodesDictType,NULL); @@ -79,17 +81,20 @@ void clusterInit(void) { sizeof(server.cluster.importing_slots_from)); memset(server.cluster.slots,0, sizeof(server.cluster.slots)); - if (clusterLoadConfig("cluster.conf") == REDIS_ERR) { + if (clusterLoadConfig("redis-cluster.conf") == REDIS_ERR) { /* No configuration found. We will just use the random name provided * by the createClusterNode() function. */ redisLog(REDIS_NOTICE,"No cluster configuration found, I'm %.40s", server.cluster.myself->name); - if (clusterSaveConfig("cluster.conf") == -1) { + saveconf = 1; + } + clusterAddNode(server.cluster.myself); + if (saveconf) { + if (clusterSaveConfig("redis-cluster.conf") == -1) { redisLog(REDIS_WARNING,"Fatal: can't update cluster config file."); exit(1); } } - clusterAddNode(server.cluster.myself); /* We need a listening TCP port for our cluster messaging needs */ server.cfd = anetTcpServer(server.neterr, server.port+REDIS_CLUSTER_PORT_INCR, server.bindaddr); From ef21ab960e061e84ee0e8ba81900750e32179215 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 30 Mar 2011 17:41:13 +0200 Subject: [PATCH 052/146] centralized cluster config file name. Assigned slots in CLUSTER NODES output and in cluster config file. --- src/cluster.c | 47 ++++++++++++++++++++++++++++++++++++----------- src/redis.c | 1 + src/redis.h | 1 + 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index 1ffe0cdc..225fcc54 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -44,7 +44,7 @@ int clusterLoadConfig(char *filename) { return REDIS_OK; fmterr: - redisLog(REDIS_WARNING,"Unrecovarable error: corrupted redis-cluster.conf file."); + redisLog(REDIS_WARNING,"Unrecovarable error: corrupted cluster config file."); fclose(fp); exit(1); } @@ -53,11 +53,12 @@ fmterr: * * This function writes the node config and returns 0, on error -1 * is returned. */ -int clusterSaveConfig(char *filename) { +int clusterSaveConfig(void) { sds ci = clusterGenNodesDescription(); int fd; - if ((fd = open(filename,O_WRONLY|O_CREAT,0644)) == -1) goto err; + if ((fd = open(server.cluster.configfile,O_WRONLY|O_CREAT,0644)) == -1) + goto err; if (write(fd,ci,sdslen(ci)) != (ssize_t)sdslen(ci)) goto err; close(fd); sdsfree(ci); @@ -68,6 +69,13 @@ err: return -1; } +void clusterSaveConfigOrDie(void) { + if (clusterSaveConfig() == -1) { + redisLog(REDIS_WARNING,"Fatal: can't update cluster config file."); + exit(1); + } +} + void clusterInit(void) { int saveconf = 0; @@ -81,7 +89,7 @@ void clusterInit(void) { sizeof(server.cluster.importing_slots_from)); memset(server.cluster.slots,0, sizeof(server.cluster.slots)); - if (clusterLoadConfig("redis-cluster.conf") == REDIS_ERR) { + if (clusterLoadConfig(server.cluster.configfile) == REDIS_ERR) { /* No configuration found. We will just use the random name provided * by the createClusterNode() function. */ redisLog(REDIS_NOTICE,"No cluster configuration found, I'm %.40s", @@ -89,12 +97,7 @@ void clusterInit(void) { saveconf = 1; } clusterAddNode(server.cluster.myself); - if (saveconf) { - if (clusterSaveConfig("redis-cluster.conf") == -1) { - redisLog(REDIS_WARNING,"Fatal: can't update cluster config file."); - exit(1); - } - } + if (saveconf) clusterSaveConfigOrDie(); /* We need a listening TCP port for our cluster messaging needs */ server.cfd = anetTcpServer(server.neterr, server.port+REDIS_CLUSTER_PORT_INCR, server.bindaddr); @@ -910,6 +913,7 @@ sds clusterGenNodesDescription(void) { sds ci = sdsempty(); dictIterator *di; dictEntry *de; + int j, start; di = dictGetIterator(server.cluster.nodes); while((de = dictNext(di)) != NULL) { @@ -939,11 +943,32 @@ sds clusterGenNodesDescription(void) { ci = sdscatprintf(ci,"- "); /* Latency from the POV of this node, link status */ - ci = sdscatprintf(ci,"%ld %ld %s\n", + ci = sdscatprintf(ci,"%ld %ld %s", (long) node->ping_sent, (long) node->pong_received, node->link ? "connected" : "disconnected"); + + /* Slots served by this instance */ + start = -1; + for (j = 0; j < REDIS_CLUSTER_SLOTS; j++) { + int bit; + + if ((bit = clusterNodeGetSlotBit(node,j)) != 0) { + if (start == -1) start = j; + } + if (start != -1 && (!bit || j == REDIS_CLUSTER_SLOTS-1)) { + if (j == REDIS_CLUSTER_SLOTS-1) j++; + + if (start == j-1) { + ci = sdscatprintf(ci," %d",start); + } else { + ci = sdscatprintf(ci," %d-%d",start,j-1); + } + start = -1; + } + } } + ci = sdscatlen(ci,"\n",1); dictReleaseIterator(di); return ci; } diff --git a/src/redis.c b/src/redis.c index 25205752..8b8c49eb 100644 --- a/src/redis.c +++ b/src/redis.c @@ -841,6 +841,7 @@ void initServerConfig() { server.shutdown_asap = 0; server.cache_flush_delay = 0; server.cluster_enabled = 0; + server.cluster.configfile = zstrdup("nodes.conf"); updateLRUClock(); resetServerSaveParams(); diff --git a/src/redis.h b/src/redis.h index 0ddd4730..5b403798 100644 --- a/src/redis.h +++ b/src/redis.h @@ -411,6 +411,7 @@ struct clusterNode { typedef struct clusterNode clusterNode; typedef struct { + char *configfile; clusterNode *myself; /* This node */ int state; /* REDIS_CLUSTER_OK, REDIS_CLUSTER_FAIL, ... */ int node_timeout; From 6c390c0b2303247c16f42160fec3fd609cb99cb7 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 30 Mar 2011 18:12:51 +0200 Subject: [PATCH 053/146] minor change with no actual effects til the loading node config code is in place --- src/cluster.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cluster.c b/src/cluster.c index 225fcc54..76ea894d 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -94,9 +94,9 @@ void clusterInit(void) { * by the createClusterNode() function. */ redisLog(REDIS_NOTICE,"No cluster configuration found, I'm %.40s", server.cluster.myself->name); + clusterAddNode(server.cluster.myself); saveconf = 1; } - clusterAddNode(server.cluster.myself); if (saveconf) clusterSaveConfigOrDie(); /* We need a listening TCP port for our cluster messaging needs */ server.cfd = anetTcpServer(server.neterr, From 25ef31920adad5c52a1f5dbe7fd307d4abec5862 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 31 Mar 2011 16:44:43 +0200 Subject: [PATCH 054/146] Fixed issue #503. MONITOR + QUIT could crash the server, there are actually other interactions that could have the same effect (for instance Pub/Sub). --- src/networking.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/networking.c b/src/networking.c index 166b44c9..7f42a4cb 100644 --- a/src/networking.c +++ b/src/networking.c @@ -60,9 +60,6 @@ redisClient *createClient(int fd) { /* Set the event loop to listen for write events on the client's socket. * Typically gets called every time a reply is built. */ int _installWriteEvent(redisClient *c) { - /* When CLOSE_AFTER_REPLY is set, no more replies may be added! */ - redisAssert(!(c->flags & REDIS_CLOSE_AFTER_REPLY)); - if (c->fd <= 0) return REDIS_ERR; if (c->bufpos == 0 && listLength(c->reply) == 0 && (c->replstate == REDIS_REPL_NONE || @@ -88,9 +85,15 @@ robj *dupLastObjectIfNeeded(list *reply) { return listNodeValue(ln); } +/* ----------------------------------------------------------------------------- + * Low level functions to add more data to output buffers. + * -------------------------------------------------------------------------- */ + int _addReplyToBuffer(redisClient *c, char *s, size_t len) { size_t available = sizeof(c->buf)-c->bufpos; + if (c->flags & REDIS_CLOSE_AFTER_REPLY) return REDIS_OK; + /* If there already are entries in the reply list, we cannot * add anything more to the static buffer. */ if (listLength(c->reply) > 0) return REDIS_ERR; @@ -105,6 +108,9 @@ int _addReplyToBuffer(redisClient *c, char *s, size_t len) { void _addReplyObjectToList(redisClient *c, robj *o) { robj *tail; + + if (c->flags & REDIS_CLOSE_AFTER_REPLY) return; + if (listLength(c->reply) == 0) { incrRefCount(o); listAddNodeTail(c->reply,o); @@ -128,6 +134,9 @@ void _addReplyObjectToList(redisClient *c, robj *o) { * needed it will be free'd, otherwise it ends up in a robj. */ void _addReplySdsToList(redisClient *c, sds s) { robj *tail; + + if (c->flags & REDIS_CLOSE_AFTER_REPLY) return; + if (listLength(c->reply) == 0) { listAddNodeTail(c->reply,createObject(REDIS_STRING,s)); } else { @@ -148,6 +157,9 @@ void _addReplySdsToList(redisClient *c, sds s) { void _addReplyStringToList(redisClient *c, char *s, size_t len) { robj *tail; + + if (c->flags & REDIS_CLOSE_AFTER_REPLY) return; + if (listLength(c->reply) == 0) { listAddNodeTail(c->reply,createStringObject(s,len)); } else { @@ -165,6 +177,11 @@ void _addReplyStringToList(redisClient *c, char *s, size_t len) { } } +/* ----------------------------------------------------------------------------- + * Higher level functions to queue data on the client output buffer. + * The following functions are the ones that commands implementations will call. + * -------------------------------------------------------------------------- */ + void addReply(redisClient *c, robj *obj) { if (_installWriteEvent(c) != REDIS_OK) return; From 5b94b8ac5d10dded6bb79909c660fe8d3f3ba677 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 31 Mar 2011 19:52:15 +0200 Subject: [PATCH 055/146] fixed memory leak introduced with the previous commit. Many thanks to Pieter Noordhuis for spotting it in no time --- src/networking.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/networking.c b/src/networking.c index 7f42a4cb..32c06306 100644 --- a/src/networking.c +++ b/src/networking.c @@ -135,7 +135,10 @@ void _addReplyObjectToList(redisClient *c, robj *o) { void _addReplySdsToList(redisClient *c, sds s) { robj *tail; - if (c->flags & REDIS_CLOSE_AFTER_REPLY) return; + if (c->flags & REDIS_CLOSE_AFTER_REPLY) { + sdsfree(s); + return; + } if (listLength(c->reply) == 0) { listAddNodeTail(c->reply,createObject(REDIS_STRING,s)); From 626f6b2d644b2458184edf9318f24ce95ba83532 Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 1 Apr 2011 18:59:28 +0200 Subject: [PATCH 056/146] DUMP implemented, RESTORE and MIGRATE fixed. Use zcalloc() instead of zmalloc()+memset() in sds.c when a new string is created with NULL initialization pointer. --- src/cluster.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/redis.c | 5 ++-- src/redis.h | 1 + src/sds.c | 12 ++++---- 4 files changed, 89 insertions(+), 7 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index 76ea894d..b57dd48c 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1270,6 +1270,7 @@ file_wr_err: strerror(errno)); fclose(fp); close(fd); + return; file_rd_err: redisLog(REDIS_WARNING,"Can't read from tmp file for MIGRATE: %s", @@ -1278,6 +1279,7 @@ file_rd_err: strerror(errno)); fclose(fp); close(fd); + return; socket_wr_err: redisLog(REDIS_NOTICE,"Can't write to target node for MIGRATE: %s", @@ -1286,6 +1288,7 @@ socket_wr_err: strerror(errno)); fclose(fp); close(fd); + return; socket_rd_err: redisLog(REDIS_NOTICE,"Can't read from target node for MIGRATE: %s", @@ -1294,6 +1297,81 @@ socket_rd_err: strerror(errno)); fclose(fp); close(fd); + return; +} + +/* DUMP keyname + * DUMP is actually not used by Redis Cluster but it is the obvious + * complement of RESTORE and can be useful for different applications. */ +void dumpCommand(redisClient *c) { + char buf[64]; + FILE *fp; + robj *o, *dumpobj; + sds dump = NULL; + off_t payload_len; + unsigned int type; + + /* Check if the key is here. */ + if ((o = lookupKeyRead(c->db,c->argv[1])) == NULL) { + addReply(c,shared.nullbulk); + return; + } + + /* Create temp file */ + snprintf(buf,sizeof(buf),"redis-dump-%d.tmp",getpid()); + fp = fopen(buf,"w+"); + if (!fp) { + redisLog(REDIS_WARNING,"Can't open tmp file for MIGRATE: %s", + strerror(errno)); + addReplyErrorFormat(c,"DUMP failed, tmp file creation error: %s.", + strerror(errno)); + return; + } + unlink(buf); + + /* Dump the serailized object and read it back in memory. + * We prefix it with a one byte containing the type ID. + * This is the serialization format understood by RESTORE. */ + if (rdbSaveObject(fp,o) == -1) goto file_wr_err; + payload_len = ftello(fp); + if (fseeko(fp,0,SEEK_SET) == -1) goto file_rd_err; + dump = sdsnewlen(NULL,payload_len+1); + if (payload_len && fread(dump+1,payload_len,1,fp) != 1) goto file_rd_err; + fclose(fp); + type = o->type; + if (type == REDIS_LIST && o->encoding == REDIS_ENCODING_ZIPLIST) + type = REDIS_LIST_ZIPLIST; + else if (type == REDIS_HASH && o->encoding == REDIS_ENCODING_ZIPMAP) + type = REDIS_HASH_ZIPMAP; + else if (type == REDIS_SET && o->encoding == REDIS_ENCODING_INTSET) + type = REDIS_SET_INTSET; + else + type = o->type; + dump[0] = type; + + /* Transfer to the client */ + dumpobj = createObject(REDIS_STRING,dump); + addReplyBulk(c,dumpobj); + decrRefCount(dumpobj); + return; + +file_wr_err: + redisLog(REDIS_WARNING,"Can't write on tmp file for DUMP: %s", + strerror(errno)); + addReplyErrorFormat(c,"DUMP failed, tmp file write error: %s.", + strerror(errno)); + sdsfree(dump); + fclose(fp); + return; + +file_rd_err: + redisLog(REDIS_WARNING,"Can't read from tmp file for DUMP: %s", + strerror(errno)); + addReplyErrorFormat(c,"DUMP failed, tmp file read error: %s.", + strerror(errno)); + sdsfree(dump); + fclose(fp); + return; } /* ----------------------------------------------------------------------------- diff --git a/src/redis.c b/src/redis.c index 8b8c49eb..1cc8cc75 100644 --- a/src/redis.c +++ b/src/redis.c @@ -189,8 +189,9 @@ struct redisCommand redisCommandTable[] = { {"watch",watchCommand,-2,0,noPreloadGetKeys,1,-1,1,0,0}, {"unwatch",unwatchCommand,1,0,NULL,0,0,0,0,0}, {"cluster",clusterCommand,-2,0,NULL,0,0,0,0,0}, - {"restore",restoreCommand,3,0,NULL,0,0,0,0,0}, - {"migrate",migrateCommand,6,0,NULL,0,0,0,0,0} + {"restore",restoreCommand,4,0,NULL,0,0,0,0,0}, + {"migrate",migrateCommand,6,0,NULL,0,0,0,0,0}, + {"dump",dumpCommand,2,0,NULL,0,0,0,0,0} }; /*============================ Utility functions ============================ */ diff --git a/src/redis.h b/src/redis.h index 5b403798..91a64ecf 100644 --- a/src/redis.h +++ b/src/redis.h @@ -1180,6 +1180,7 @@ void unwatchCommand(redisClient *c); void clusterCommand(redisClient *c); void restoreCommand(redisClient *c); void migrateCommand(redisClient *c); +void dumpCommand(redisClient *c); #if defined(__GNUC__) void *calloc(size_t count, size_t size) __attribute__ ((deprecated)); diff --git a/src/sds.c b/src/sds.c index 7cb43962..8c754099 100644 --- a/src/sds.c +++ b/src/sds.c @@ -51,7 +51,11 @@ static void sdsOomAbort(void) { sds sdsnewlen(const void *init, size_t initlen) { struct sdshdr *sh; - sh = zmalloc(sizeof(struct sdshdr)+initlen+1); + if (init) { + sh = zmalloc(sizeof(struct sdshdr)+initlen+1); + } else { + sh = zcalloc(sizeof(struct sdshdr)+initlen+1); + } #ifdef SDS_ABORT_ON_OOM if (sh == NULL) sdsOomAbort(); #else @@ -59,10 +63,8 @@ sds sdsnewlen(const void *init, size_t initlen) { #endif sh->len = initlen; sh->free = 0; - if (initlen) { - if (init) memcpy(sh->buf, init, initlen); - else memset(sh->buf,0,initlen); - } + if (initlen && init) + memcpy(sh->buf, init, initlen); sh->buf[initlen] = '\0'; return (char*)sh->buf; } From f797c7dc176c833e6aa412c557d7fedd59dc1124 Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 5 Apr 2011 13:57:28 +0200 Subject: [PATCH 057/146] bad data on RESTORE can no longer crash the server but create a memory leak with some input string --- src/cluster.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index b57dd48c..783c658d 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1140,8 +1140,10 @@ void restoreCommand(redisClient *c) { /* Finally create the object from the serialized dump and * store it at the specified key. */ - o = rdbLoadObject(data[0],fp); - if (o == NULL) { + if ((data[0] > 4 && data[0] < 9) || + data[0] > 11 || + (o = rdbLoadObject(data[0],fp)) == NULL) + { addReplyError(c,"Bad data format."); fclose(fp); return; From ece74202bbb2ff6b97b407755d5b9206db82cb17 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 6 Apr 2011 12:19:45 +0200 Subject: [PATCH 058/146] OBJECT command implemented --- src/object.c | 39 +++++++++++++++++++++++++++++++++++++++ src/redis.c | 3 ++- src/redis.h | 1 + 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/object.c b/src/object.c index e6b02da8..4de9dbaa 100644 --- a/src/object.c +++ b/src/object.c @@ -416,3 +416,42 @@ unsigned long estimateObjectIdleTime(robj *o) { REDIS_LRU_CLOCK_RESOLUTION; } } + +/* This is an helper function for the DEBUG command. We need to lookup keys + * without any modification of LRU or other parameters. */ +robj *objectCommandLookup(redisClient *c, robj *key) { + dictEntry *de; + + if ((de = dictFind(c->db->dict,key->ptr)) == NULL) return NULL; + return (robj*) dictGetEntryVal(de); +} + +robj *objectCommandLookupOrReply(redisClient *c, robj *key, robj *reply) { + robj *o = objectCommandLookup(c,key); + + if (!o) addReply(c, reply); + return o; +} + +/* Object command allows to inspect the internals of an Redis Object. + * Usage: OBJECT ... arguments ... */ +void objectCommand(redisClient *c) { + robj *o; + + if (!strcasecmp(c->argv[1]->ptr,"refcount") && c->argc == 3) { + if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nullbulk)) + == NULL) return; + addReplyLongLong(c,o->refcount); + } else if (!strcasecmp(c->argv[1]->ptr,"encoding") && c->argc == 3) { + if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nullbulk)) + == NULL) return; + addReplyBulkCString(c,strEncoding(o->encoding)); + } else if (!strcasecmp(c->argv[1]->ptr,"idletime") && c->argc == 3) { + if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nullbulk)) + == NULL) return; + addReplyLongLong(c,estimateObjectIdleTime(o)); + } else { + addReplyError(c,"Syntax error. Try OBJECT (refcount|encoding|idletime)"); + } +} + diff --git a/src/redis.c b/src/redis.c index 1cc8cc75..9c726151 100644 --- a/src/redis.c +++ b/src/redis.c @@ -191,7 +191,8 @@ struct redisCommand redisCommandTable[] = { {"cluster",clusterCommand,-2,0,NULL,0,0,0,0,0}, {"restore",restoreCommand,4,0,NULL,0,0,0,0,0}, {"migrate",migrateCommand,6,0,NULL,0,0,0,0,0}, - {"dump",dumpCommand,2,0,NULL,0,0,0,0,0} + {"dump",dumpCommand,2,0,NULL,0,0,0,0,0}, + {"object",objectCommand,-2,0,NULL,0,0,0,0,0} }; /*============================ Utility functions ============================ */ diff --git a/src/redis.h b/src/redis.h index 91a64ecf..3ceffd42 100644 --- a/src/redis.h +++ b/src/redis.h @@ -1181,6 +1181,7 @@ void clusterCommand(redisClient *c); void restoreCommand(redisClient *c); void migrateCommand(redisClient *c); void dumpCommand(redisClient *c); +void objectCommand(redisClient *c); #if defined(__GNUC__) void *calloc(size_t count, size_t size) __attribute__ ((deprecated)); From 0b7f6d0913f96f2fe8280afc681c6e0a65ca81c9 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 6 Apr 2011 15:36:10 +0200 Subject: [PATCH 059/146] make sure that OBJECT ENCODING returns skiplist for sorted sets, and not raw, so that once we will merge specially encoded sorted sets everything will make sense. --- src/object.c | 6 +++++- src/redis.h | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/object.c b/src/object.c index 4de9dbaa..6a9b0214 100644 --- a/src/object.c +++ b/src/object.c @@ -93,10 +93,13 @@ robj *createHashObject(void) { robj *createZsetObject(void) { zset *zs = zmalloc(sizeof(*zs)); + robj *o; zs->dict = dictCreate(&zsetDictType,NULL); zs->zsl = zslCreate(); - return createObject(REDIS_ZSET,zs); + o = createObject(REDIS_ZSET,zs); + o->encoding = REDIS_ENCODING_SKIPLIST; + return o; } void freeStringObject(robj *o) { @@ -402,6 +405,7 @@ char *strEncoding(int encoding) { case REDIS_ENCODING_LINKEDLIST: return "linkedlist"; case REDIS_ENCODING_ZIPLIST: return "ziplist"; case REDIS_ENCODING_INTSET: return "intset"; + case REDIS_ENCODING_SKIPLIST: return "skiplist"; default: return "unknown"; } } diff --git a/src/redis.h b/src/redis.h index 3ceffd42..26f33451 100644 --- a/src/redis.h +++ b/src/redis.h @@ -86,6 +86,7 @@ #define REDIS_ENCODING_LINKEDLIST 4 /* Encoded as regular linked list */ #define REDIS_ENCODING_ZIPLIST 5 /* Encoded as ziplist */ #define REDIS_ENCODING_INTSET 6 /* Encoded as intset */ +#define REDIS_ENCODING_SKIPLIST 7 /* Encoded as skiplist */ /* Object types only used for dumping to disk */ #define REDIS_EXPIRETIME 253 From 4cc4d1648b3b4c01bf7568694a88e2ef3f70b2bf Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 6 Apr 2011 16:15:15 +0200 Subject: [PATCH 060/146] Typo --- src/rdb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rdb.c b/src/rdb.c index 27390b9c..d186a9c5 100644 --- a/src/rdb.c +++ b/src/rdb.c @@ -327,7 +327,7 @@ int rdbSaveObject(FILE *fp, robj *o) { } dictReleaseIterator(di); } else { - redisPanic("Unknown sorted set enoding"); + redisPanic("Unknown sorted set encoding"); } } else if (o->type == REDIS_HASH) { /* Save a hash value */ From 100ed062c0e3fa7d1a369de083aee619e27c8b2b Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 6 Apr 2011 16:17:07 +0200 Subject: [PATCH 061/146] Test for ENCODING_SKIPLIST instead of ENCODING_RAW --- src/aof.c | 2 +- src/debug.c | 2 +- src/object.c | 2 +- src/rdb.c | 2 +- src/sort.c | 2 +- src/t_zset.c | 38 +++++++++++++++++++------------------- tests/unit/type/zset.tcl | 8 ++++---- 7 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/aof.c b/src/aof.c index cd7c48d3..5d75c374 100644 --- a/src/aof.c +++ b/src/aof.c @@ -460,7 +460,7 @@ int rewriteAppendOnlyFile(char *filename) { } zzlNext(zl,&eptr,&sptr); } - } else if (o->encoding == REDIS_ENCODING_RAW) { + } else if (o->encoding == REDIS_ENCODING_SKIPLIST) { zset *zs = o->ptr; dictIterator *di = dictGetIterator(zs->dict); dictEntry *de; diff --git a/src/debug.c b/src/debug.c index 940a0380..080e2b2e 100644 --- a/src/debug.c +++ b/src/debug.c @@ -159,7 +159,7 @@ void computeDatasetDigest(unsigned char *final) { xorDigest(digest,eledigest,20); zzlNext(zl,&eptr,&sptr); } - } else if (o->encoding == REDIS_ENCODING_RAW) { + } else if (o->encoding == REDIS_ENCODING_SKIPLIST) { zset *zs = o->ptr; dictIterator *di = dictGetIterator(zs->dict); dictEntry *de; diff --git a/src/object.c b/src/object.c index 4bf1df01..c7c90c54 100644 --- a/src/object.c +++ b/src/object.c @@ -144,7 +144,7 @@ void freeSetObject(robj *o) { void freeZsetObject(robj *o) { zset *zs; switch (o->encoding) { - case REDIS_ENCODING_RAW: + case REDIS_ENCODING_SKIPLIST: zs = o->ptr; dictRelease(zs->dict); zslFree(zs->zsl); diff --git a/src/rdb.c b/src/rdb.c index d186a9c5..353620f2 100644 --- a/src/rdb.c +++ b/src/rdb.c @@ -308,7 +308,7 @@ int rdbSaveObject(FILE *fp, robj *o) { if ((n = rdbSaveRawString(fp,o->ptr,l)) == -1) return -1; nwritten += n; - } else if (o->encoding == REDIS_ENCODING_RAW) { + } else if (o->encoding == REDIS_ENCODING_SKIPLIST) { zset *zs = o->ptr; dictIterator *di = dictGetIterator(zs->dict); dictEntry *de; diff --git a/src/sort.c b/src/sort.c index 1a3fecb6..ff275c95 100644 --- a/src/sort.c +++ b/src/sort.c @@ -200,7 +200,7 @@ void sortCommand(redisClient *c) { } /* Destructively convert encoded sorted sets for SORT. */ - if (sortval->type == REDIS_ZSET) zsetConvert(sortval, REDIS_ENCODING_RAW); + if (sortval->type == REDIS_ZSET) zsetConvert(sortval, REDIS_ENCODING_SKIPLIST); /* Load the sorting vector with all the objects to sort */ switch(sortval->type) { diff --git a/src/t_zset.c b/src/t_zset.c index f630ff02..929714ca 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -721,7 +721,7 @@ unsigned int zsetLength(robj *zobj) { int length = -1; if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { length = zzlLength(zobj->ptr); - } else if (zobj->encoding == REDIS_ENCODING_RAW) { + } else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) { length = ((zset*)zobj->ptr)->zsl->length; } else { redisPanic("Unknown sorted set encoding"); @@ -743,7 +743,7 @@ void zsetConvert(robj *zobj, int encoding) { unsigned int vlen; long long vlong; - if (encoding != REDIS_ENCODING_RAW) + if (encoding != REDIS_ENCODING_SKIPLIST) redisPanic("Unknown target encoding"); zs = zmalloc(sizeof(*zs)); @@ -772,8 +772,8 @@ void zsetConvert(robj *zobj, int encoding) { zfree(zobj->ptr); zobj->ptr = zs; - zobj->encoding = REDIS_ENCODING_RAW; - } else if (zobj->encoding == REDIS_ENCODING_RAW) { + zobj->encoding = REDIS_ENCODING_SKIPLIST; + } else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) { unsigned char *zl = ziplistNew(); if (encoding != REDIS_ENCODING_ZIPLIST) @@ -872,9 +872,9 @@ void zaddGenericCommand(redisClient *c, int incr) { * too long *before* executing zzlInsert. */ zobj->ptr = zzlInsert(zobj->ptr,ele,score); if (zzlLength(zobj->ptr) > server.zset_max_ziplist_entries) - zsetConvert(zobj,REDIS_ENCODING_RAW); + zsetConvert(zobj,REDIS_ENCODING_SKIPLIST); if (sdslen(ele->ptr) > server.zset_max_ziplist_value) - zsetConvert(zobj,REDIS_ENCODING_RAW); + zsetConvert(zobj,REDIS_ENCODING_SKIPLIST); signalModifiedKey(c->db,key); server.dirty++; @@ -884,7 +884,7 @@ void zaddGenericCommand(redisClient *c, int incr) { else /* ZADD */ addReply(c,shared.cone); } - } else if (zobj->encoding == REDIS_ENCODING_RAW) { + } else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) { zset *zs = zobj->ptr; zskiplistNode *znode; dictEntry *de; @@ -967,7 +967,7 @@ void zremCommand(redisClient *c) { addReply(c,shared.czero); return; } - } else if (zobj->encoding == REDIS_ENCODING_RAW) { + } else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) { zset *zs = zobj->ptr; dictEntry *de; double score; @@ -1013,7 +1013,7 @@ void zremrangebyscoreCommand(redisClient *c) { if (zobj->encoding == REDIS_ENCODING_ZIPLIST) { zobj->ptr = zzlDeleteRangeByScore(zobj->ptr,range,&deleted); if (zzlLength(zobj->ptr) == 0) dbDelete(c->db,key); - } else if (zobj->encoding == REDIS_ENCODING_RAW) { + } else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) { zset *zs = zobj->ptr; deleted = zslDeleteRangeByScore(zs->zsl,range,zs->dict); if (htNeedsResize(zs->dict)) dictResize(zs->dict); @@ -1059,7 +1059,7 @@ void zremrangebyrankCommand(redisClient *c) { /* Correct for 1-based rank. */ zobj->ptr = zzlDeleteRangeByRank(zobj->ptr,start+1,end+1,&deleted); if (zzlLength(zobj->ptr) == 0) dbDelete(c->db,key); - } else if (zobj->encoding == REDIS_ENCODING_RAW) { + } else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) { zset *zs = zobj->ptr; /* Correct for 1-based rank. */ @@ -1159,7 +1159,7 @@ void zuiInitIterator(zsetopsrc *op) { it->zl.sptr = ziplistNext(it->zl.zl,it->zl.eptr); redisAssert(it->zl.sptr != NULL); } - } else if (op->encoding == REDIS_ENCODING_RAW) { + } else if (op->encoding == REDIS_ENCODING_SKIPLIST) { it->sl.zs = op->subject->ptr; it->sl.node = it->sl.zs->zsl->header->level[0].forward; } else { @@ -1187,7 +1187,7 @@ void zuiClearIterator(zsetopsrc *op) { iterzset *it = &op->iter.zset; if (op->encoding == REDIS_ENCODING_ZIPLIST) { REDIS_NOTUSED(it); /* skip */ - } else if (op->encoding == REDIS_ENCODING_RAW) { + } else if (op->encoding == REDIS_ENCODING_SKIPLIST) { REDIS_NOTUSED(it); /* skip */ } else { redisPanic("Unknown sorted set encoding"); @@ -1214,7 +1214,7 @@ int zuiLength(zsetopsrc *op) { iterzset *it = &op->iter.zset; if (op->encoding == REDIS_ENCODING_ZIPLIST) { return zzlLength(it->zl.zl); - } else if (op->encoding == REDIS_ENCODING_RAW) { + } else if (op->encoding == REDIS_ENCODING_SKIPLIST) { return it->sl.zs->zsl->length; } else { redisPanic("Unknown sorted set encoding"); @@ -1267,7 +1267,7 @@ int zuiNext(zsetopsrc *op, zsetopval *val) { /* Move to next element. */ zzlNext(it->zl.zl,&it->zl.eptr,&it->zl.sptr); - } else if (op->encoding == REDIS_ENCODING_RAW) { + } else if (op->encoding == REDIS_ENCODING_SKIPLIST) { if (it->sl.node == NULL) return 0; val->ele = it->sl.node->obj; @@ -1379,7 +1379,7 @@ int zuiFind(zsetopsrc *op, zsetopval *val, double *score) { } else { return 0; } - } else if (op->encoding == REDIS_ENCODING_RAW) { + } else if (op->encoding == REDIS_ENCODING_SKIPLIST) { dictEntry *de; if ((de = dictFind(it->sl.zs->dict,val->ele)) != NULL) { *score = *(double*)dictGetEntryVal(de); @@ -1692,7 +1692,7 @@ void zrangeGenericCommand(redisClient *c, int reverse) { zzlNext(zl,&eptr,&sptr); } - } else if (zobj->encoding == REDIS_ENCODING_RAW) { + } else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) { zset *zs = zobj->ptr; zskiplist *zsl = zs->zsl; zskiplistNode *ln; @@ -1849,7 +1849,7 @@ void genericZrangebyscoreCommand(redisClient *c, int reverse, int justcount) { else zzlNext(zl,&eptr,&sptr); } - } else if (zobj->encoding == REDIS_ENCODING_RAW) { + } else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) { zset *zs = zobj->ptr; zskiplist *zsl = zs->zsl; zskiplistNode *ln; @@ -1943,7 +1943,7 @@ void zscoreCommand(redisClient *c) { addReplyDouble(c,score); else addReply(c,shared.nullbulk); - } else if (zobj->encoding == REDIS_ENCODING_RAW) { + } else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) { zset *zs = zobj->ptr; dictEntry *de; @@ -1997,7 +1997,7 @@ void zrankGenericCommand(redisClient *c, int reverse) { } else { addReply(c,shared.nullbulk); } - } else if (zobj->encoding == REDIS_ENCODING_RAW) { + } else if (zobj->encoding == REDIS_ENCODING_SKIPLIST) { zset *zs = zobj->ptr; zskiplist *zsl = zs->zsl; dictEntry *de; diff --git a/tests/unit/type/zset.tcl b/tests/unit/type/zset.tcl index 6e32a4b8..4fa7af1b 100644 --- a/tests/unit/type/zset.tcl +++ b/tests/unit/type/zset.tcl @@ -10,7 +10,7 @@ start_server {tags {"zset"}} { if {$encoding == "ziplist"} { r config set zset-max-ziplist-entries 128 r config set zset-max-ziplist-value 64 - } elseif {$encoding == "raw"} { + } elseif {$encoding == "skiplist"} { r config set zset-max-ziplist-entries 0 r config set zset-max-ziplist-value 0 } else { @@ -458,7 +458,7 @@ start_server {tags {"zset"}} { } basics ziplist - basics raw + basics skiplist proc stressers {encoding} { if {$encoding == "ziplist"} { @@ -466,7 +466,7 @@ start_server {tags {"zset"}} { r config set zset-max-ziplist-entries 256 r config set zset-max-ziplist-value 64 set elements 128 - } elseif {$encoding == "raw"} { + } elseif {$encoding == "skiplist"} { r config set zset-max-ziplist-entries 0 r config set zset-max-ziplist-value 0 set elements 1000 @@ -684,6 +684,6 @@ start_server {tags {"zset"}} { tags {"slow"} { stressers ziplist - stressers raw + stressers skiplist } } From d4d3a70da2c9be4c5aa67a0be735568dbe436568 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 6 Apr 2011 16:38:29 +0200 Subject: [PATCH 062/146] Update target encoding for sorted set from rdb --- src/rdb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rdb.c b/src/rdb.c index 353620f2..2557f5b8 100644 --- a/src/rdb.c +++ b/src/rdb.c @@ -875,10 +875,10 @@ robj *rdbLoadObject(int type, FILE *fp) { o->type = REDIS_ZSET; o->encoding = REDIS_ENCODING_ZIPLIST; if (zsetLength(o) > server.zset_max_ziplist_entries) - zsetConvert(o,REDIS_ENCODING_RAW); + zsetConvert(o,REDIS_ENCODING_SKIPLIST); break; default: - redisPanic("Unknown enoding"); + redisPanic("Unknown encoding"); break; } } else { From 02e600653216cebc0746bfbb86b353667a843591 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 6 Apr 2011 16:39:18 +0200 Subject: [PATCH 063/146] Explicitly zero zval since it is stored on the stack --- src/t_zset.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/t_zset.c b/src/t_zset.c index 929714ca..7ce60349 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -1515,6 +1515,7 @@ void zunionInterGenericCommand(redisClient *c, robj *dstkey, int op) { dstobj = createZsetObject(); dstzset = dstobj->ptr; + memset(&zval, 0, sizeof(zval)); if (op == REDIS_OP_INTER) { /* Skip everything if the smallest input is empty. */ From 726a39c15ff5d89f0d44a32cca8869cf0589f929 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 7 Apr 2011 12:55:02 +0200 Subject: [PATCH 064/146] initial cluster config load code --- src/cluster.c | 35 +++++++++++++++++++++++++++++++---- src/sds.c | 7 +++++++ src/sds.h | 1 + 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index 783c658d..46350cc2 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -34,11 +34,32 @@ void clusterGetRandomName(char *p) { int clusterLoadConfig(char *filename) { FILE *fp = fopen(filename,"r"); + char *line; + int maxline; - return REDIS_ERR; if (fp == NULL) return REDIS_ERR; + + /* Parse the file. Note that single liens of the cluster config file can + * be really long as they include all the hash slots of the node. + * This means in the worst possible case REDIS_CLUSTER_SLOTS/2 integers. + * To simplify we allocate 1024+REDIS_CLUSTER_SLOTS*16 bytes per line. */ + maxline = 1024+REDIS_CLUSTER_SLOTS*16; + line = zmalloc(maxline); + while(fgets(line,maxline,fp) != NULL) { + int argc; + sds *argv = sdssplitargs(line,&argc); + + printf("Node: %s\n", argv[0]); + + sdssplitargs_free(argv,argc); + } + zfree(line); fclose(fp); + /* Config sanity check */ + /* TODO: check that myself is set. */ + return REDIS_ERR; + redisLog(REDIS_NOTICE,"Node configuration loaded, I'm %.40s", server.cluster.myself->name); return REDIS_OK; @@ -57,8 +78,8 @@ int clusterSaveConfig(void) { sds ci = clusterGenNodesDescription(); int fd; - if ((fd = open(server.cluster.configfile,O_WRONLY|O_CREAT,0644)) == -1) - goto err; + if ((fd = open(server.cluster.configfile,O_WRONLY|O_CREAT|O_TRUNC,0644)) + == -1) goto err; if (write(fd,ci,sdslen(ci)) != (ssize_t)sdslen(ci)) goto err; close(fd); sdsfree(ci); @@ -337,6 +358,7 @@ void clusterProcessGossipSection(clusterMsg *hdr, clusterLink *link) { /* Broadcast the failing node name to everybody */ clusterSendFail(node->name); clusterUpdateState(); + clusterSaveConfigOrDie(); } } else { /* If it's not in NOADDR state and we don't have it, we @@ -522,7 +544,10 @@ int clusterProcessPacket(clusterLink *link) { clusterProcessGossipSection(hdr,link); /* Update the cluster state if needed */ - if (update) clusterUpdateState(); + if (update) { + clusterUpdateState(); + clusterSaveConfigOrDie(); + } } else if (type == CLUSTERMSG_TYPE_FAIL && sender) { clusterNode *failing; @@ -534,6 +559,7 @@ int clusterProcessPacket(clusterLink *link) { failing->flags |= REDIS_NODE_FAIL; failing->flags &= ~REDIS_NODE_PFAIL; clusterUpdateState(); + clusterSaveConfigOrDie(); } } else { redisLog(REDIS_NOTICE,"Received unknown packet type: %d", type); @@ -1047,6 +1073,7 @@ void clusterCommand(redisClient *c) { } zfree(slots); clusterUpdateState(); + clusterSaveConfigOrDie(); addReply(c,shared.ok); } else if (!strcasecmp(c->argv[1]->ptr,"info") && c->argc == 2) { char *statestr[] = {"ok","fail","needhelp"}; diff --git a/src/sds.c b/src/sds.c index 8c754099..37c63b05 100644 --- a/src/sds.c +++ b/src/sds.c @@ -554,6 +554,13 @@ err: return NULL; } +void sdssplitargs_free(sds *argv, int argc) { + int j; + + for (j = 0 ;j < argc; j++) sdsfree(argv[j]); + zfree(argv); +} + #ifdef SDS_TEST_MAIN #include #include "testhelp.h" diff --git a/src/sds.h b/src/sds.h index 91a38782..6a317868 100644 --- a/src/sds.h +++ b/src/sds.h @@ -74,5 +74,6 @@ void sdstoupper(sds s); sds sdsfromlonglong(long long value); sds sdscatrepr(sds s, char *p, size_t len); sds *sdssplitargs(char *line, int *argc); +void sdssplitargs_free(sds *argv, int argc); #endif From 92690d29fee4822a0e6280c0fe8afb8a6ff04671 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 7 Apr 2011 17:46:28 +0200 Subject: [PATCH 065/146] first version of cluster config loading code --- src/cluster.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 6 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index 46350cc2..ef61ff4c 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -11,6 +11,8 @@ void clusterSendFail(char *nodename); void clusterUpdateState(void); int clusterNodeGetSlotBit(clusterNode *n, int slot); sds clusterGenNodesDescription(void); +clusterNode *clusterLookupNode(char *name); +int clusterNodeAddSlave(clusterNode *master, clusterNode *slave); /* ----------------------------------------------------------------------------- * Initialization @@ -35,7 +37,7 @@ void clusterGetRandomName(char *p) { int clusterLoadConfig(char *filename) { FILE *fp = fopen(filename,"r"); char *line; - int maxline; + int maxline, j; if (fp == NULL) return REDIS_ERR; @@ -48,8 +50,73 @@ int clusterLoadConfig(char *filename) { while(fgets(line,maxline,fp) != NULL) { int argc; sds *argv = sdssplitargs(line,&argc); + clusterNode *n, *master; + char *p, *s; - printf("Node: %s\n", argv[0]); + /* Create this node if it does not exist */ + n = clusterLookupNode(argv[0]); + if (!n) { + n = createClusterNode(argv[0],0); + clusterAddNode(n); + } + /* Address and port */ + if ((p = strchr(argv[1],':')) == NULL) goto fmterr; + *p = '\0'; + memcpy(n->ip,argv[1],strlen(argv[1])+1); + n->port = atoi(p+1); + + /* Parse flags */ + p = s = argv[2]; + while(p) { + p = strchr(s,','); + if (p) *p = '\0'; + if (!strcasecmp(s,"myself")) { + redisAssert(server.cluster.myself == NULL); + server.cluster.myself = n; + n->flags |= REDIS_NODE_MYSELF; + } else if (!strcasecmp(s,"master")) { + n->flags |= REDIS_NODE_MASTER; + } else if (!strcasecmp(s,"slave")) { + n->flags |= REDIS_NODE_SLAVE; + } else if (!strcasecmp(s,"fail?")) { + n->flags |= REDIS_NODE_PFAIL; + } else if (!strcasecmp(s,"fail")) { + n->flags |= REDIS_NODE_FAIL; + } else if (!strcasecmp(s,"handshake")) { + n->flags |= REDIS_NODE_HANDSHAKE; + } else if (!strcasecmp(s,"noaddr")) { + n->flags |= REDIS_NODE_NOADDR; + } else { + redisPanic("Unknown flag in redis cluster config file"); + } + if (p) s = p+1; + } + + /* Get master if any. Set the master and populate master's + * slave list. */ + if (argv[3][0] != '-') { + master = clusterLookupNode(argv[3]); + if (!master) { + master = createClusterNode(argv[3],0); + clusterAddNode(master); + } + n->slaveof = master; + clusterNodeAddSlave(master,n); + } + + /* Populate hash slots served by this instance. */ + for (j = 7; j < argc; j++) { + int start, stop; + + if ((p = strchr(argv[j],'-')) != NULL) { + *p = '\0'; + start = atoi(argv[j]); + stop = atoi(p+1); + } else { + start = stop = atoi(argv[j]); + } + while(start <= stop) clusterAddSlot(n, start++); + } sdssplitargs_free(argv,argc); } @@ -57,9 +124,7 @@ int clusterLoadConfig(char *filename) { fclose(fp); /* Config sanity check */ - /* TODO: check that myself is set. */ - return REDIS_ERR; - + redisAssert(server.cluster.myself != NULL); redisLog(REDIS_NOTICE,"Node configuration loaded, I'm %.40s", server.cluster.myself->name); return REDIS_OK; @@ -100,7 +165,7 @@ void clusterSaveConfigOrDie(void) { void clusterInit(void) { int saveconf = 0; - server.cluster.myself = createClusterNode(NULL,REDIS_NODE_MYSELF); + server.cluster.myself = NULL; server.cluster.state = REDIS_CLUSTER_FAIL; server.cluster.nodes = dictCreate(&clusterNodesDictType,NULL); server.cluster.node_timeout = 15; @@ -113,6 +178,7 @@ void clusterInit(void) { if (clusterLoadConfig(server.cluster.configfile) == REDIS_ERR) { /* No configuration found. We will just use the random name provided * by the createClusterNode() function. */ + server.cluster.myself = createClusterNode(NULL,REDIS_NODE_MYSELF); redisLog(REDIS_NOTICE,"No cluster configuration found, I'm %.40s", server.cluster.myself->name); clusterAddNode(server.cluster.myself); From d01a6bb3f92d96747dad229c3d28244cb42bf7e9 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 7 Apr 2011 19:04:16 +0200 Subject: [PATCH 066/146] fixes to configuration loading and saving. However there is to still fix the logic for reconnection/behavior of nodes after a restart. --- src/cluster.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index ef61ff4c..f2a0bd75 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -86,6 +86,8 @@ int clusterLoadConfig(char *filename) { n->flags |= REDIS_NODE_HANDSHAKE; } else if (!strcasecmp(s,"noaddr")) { n->flags |= REDIS_NODE_NOADDR; + } else if (!strcasecmp(s,"noflags")) { + /* nothing to do */ } else { redisPanic("Unknown flag in redis cluster config file"); } @@ -524,7 +526,8 @@ int clusterProcessPacket(clusterLink *link) { /* Anyway reply with a PONG */ clusterSendPing(link,CLUSTERMSG_TYPE_PONG); } else if (type == CLUSTERMSG_TYPE_PONG) { - int update = 0; + int update_state = 0; + int update_config = 0; redisLog(REDIS_DEBUG,"Pong packet received: %p", link->node); if (link->node) { @@ -545,6 +548,7 @@ int clusterProcessPacket(clusterLink *link) { redisLog(REDIS_DEBUG,"Handshake with node %.40s completed.", link->node->name); link->node->flags &= ~REDIS_NODE_HANDSHAKE; + update_config = 1; } else if (memcmp(link->node->name,hdr->sender, REDIS_CLUSTER_NAMELEN) != 0) { @@ -554,6 +558,7 @@ int clusterProcessPacket(clusterLink *link) { redisLog(REDIS_DEBUG,"PONG contains mismatching sender ID"); link->node->flags |= REDIS_NODE_NOADDR; freeClusterLink(link); + update_config = 1; /* FIXME: remove this node if we already have it. * * If we already have it but the IP is different, use @@ -599,7 +604,7 @@ int clusterProcessPacket(clusterLink *link) { server.cluster.slots[j]->flags & REDIS_NODE_FAIL) { server.cluster.slots[j] = sender; - update = 1; + update_state = update_config = 1; } } } @@ -610,10 +615,8 @@ int clusterProcessPacket(clusterLink *link) { clusterProcessGossipSection(hdr,link); /* Update the cluster state if needed */ - if (update) { - clusterUpdateState(); - clusterSaveConfigOrDie(); - } + if (update_state) clusterUpdateState(); + if (update_config) clusterSaveConfigOrDie(); } else if (type == CLUSTERMSG_TYPE_FAIL && sender) { clusterNode *failing; @@ -1059,8 +1062,8 @@ sds clusterGenNodesDescription(void) { start = -1; } } + ci = sdscatlen(ci,"\n",1); } - ci = sdscatlen(ci,"\n",1); dictReleaseIterator(di); return ci; } From 2bc52b2c028c8b1b1d22052ecd689439a488fbf7 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 7 Apr 2011 19:22:24 +0200 Subject: [PATCH 067/146] useless newline removed from log message --- src/cluster.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cluster.c b/src/cluster.c index f2a0bd75..27630561 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -505,6 +505,7 @@ int clusterProcessPacket(clusterLink *link) { sender = clusterLookupNode(hdr->sender); if (type == CLUSTERMSG_TYPE_PING || type == CLUSTERMSG_TYPE_MEET) { + int update_config = 0; redisLog(REDIS_DEBUG,"Ping packet received: %p", link->node); /* Add this node if it is new for us and the msg type is MEET. @@ -518,6 +519,7 @@ int clusterProcessPacket(clusterLink *link) { nodeIp2String(node->ip,link); node->port = ntohs(hdr->port); clusterAddNode(node); + update_config = 1; } /* Get info from the gossip section */ @@ -525,6 +527,9 @@ int clusterProcessPacket(clusterLink *link) { /* Anyway reply with a PONG */ clusterSendPing(link,CLUSTERMSG_TYPE_PONG); + + /* Update config if needed */ + if (update_config) clusterSaveConfigOrDie(); } else if (type == CLUSTERMSG_TYPE_PONG) { int update_state = 0; int update_config = 0; @@ -879,7 +884,7 @@ void clusterCron(void) { * normal PING packets. */ node->flags &= ~REDIS_NODE_MEET; - redisLog(REDIS_NOTICE,"Connecting with Node %.40s at %s:%d\n", node->name, node->ip, node->port+REDIS_CLUSTER_PORT_INCR); + redisLog(REDIS_NOTICE,"Connecting with Node %.40s at %s:%d", node->name, node->ip, node->port+REDIS_CLUSTER_PORT_INCR); } } dictReleaseIterator(di); From 1793752d97d72c82ce237b461165d5a06c44587e Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 7 Apr 2011 21:34:41 +0200 Subject: [PATCH 068/146] node cluster configuration file configurable via redis.conf --- src/cluster.c | 1 + src/config.c | 3 +++ 2 files changed, 4 insertions(+) diff --git a/src/cluster.c b/src/cluster.c index 27630561..6d117aca 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -13,6 +13,7 @@ int clusterNodeGetSlotBit(clusterNode *n, int slot); sds clusterGenNodesDescription(void); clusterNode *clusterLookupNode(char *name); int clusterNodeAddSlave(clusterNode *master, clusterNode *slave); +int clusterAddSlot(clusterNode *n, int slot); /* ----------------------------------------------------------------------------- * Initialization diff --git a/src/config.c b/src/config.c index e40fdeda..48f90349 100644 --- a/src/config.c +++ b/src/config.c @@ -289,6 +289,9 @@ void loadServerConfig(char *filename) { if ((server.cluster_enabled = yesnotoi(argv[1])) == -1) { err = "argument must be 'yes' or 'no'"; goto loaderr; } + } else if (!strcasecmp(argv[0],"cluster-config-file") && argc == 2) { + zfree(server.cluster.configfile); + server.cluster.configfile = zstrdup(argv[1]); } else { err = "Bad directive or wrong number of arguments"; goto loaderr; } From 152d937b8ca02ab8d6ccd038d6f49d3b0ab51a42 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 7 Apr 2011 23:06:01 +0200 Subject: [PATCH 069/146] when loading config set the ping/pong timestaps to the current value. Even a node in FAIL state can be accepted again if there are no slaves for this node so election was not possible --- src/cluster.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index 6d117aca..ab101d43 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -107,6 +107,10 @@ int clusterLoadConfig(char *filename) { clusterNodeAddSlave(master,n); } + /* Set ping sent / pong received timestamps */ + if (atoi(argv[4])) n->ping_sent = time(NULL); + if (atoi(argv[5])) n->pong_received = time(NULL); + /* Populate hash slots served by this instance. */ for (j = 7; j < argc; j++) { int start, stop; @@ -923,14 +927,25 @@ void clusterCron(void) { node->ping_sent <= node->pong_received) continue; delay = time(NULL) - node->pong_received; - if (node->flags & REDIS_NODE_PFAIL) { + if (delay < server.cluster.node_timeout) { /* The PFAIL condition can be reversed without external * help if it is not transitive (that is, if it does not - * turn into a FAIL state). */ - if (delay < server.cluster.node_timeout) + * turn into a FAIL state). + * + * The FAIL condition is also reversible if there are no slaves + * for this host, so no slave election should be in progress. + * + * TODO: consider all the implications of resurrecting a + * FAIL node. */ + if (node->flags & REDIS_NODE_PFAIL) { node->flags &= ~REDIS_NODE_PFAIL; + } else if (node->flags & REDIS_NODE_FAIL && !node->numslaves) { + node->flags &= ~REDIS_NODE_FAIL; + } } else { - if (delay >= server.cluster.node_timeout) { + /* Timeout reached. Set the noad se possibly failing if it is + * not already in this state. */ + if (!(node->flags & REDIS_NODE_PFAIL)) { redisLog(REDIS_DEBUG,"*** NODE %.40s possibly failing", node->name); node->flags |= REDIS_NODE_PFAIL; From 93666e583cf344ff97e87503c77d9b3020ab357b Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 7 Apr 2011 23:10:32 +0200 Subject: [PATCH 070/146] master node without slaves rejoin fixed --- src/cluster.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index ab101d43..6a693d6e 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -919,8 +919,8 @@ void clusterCron(void) { int delay; if (node->flags & - (REDIS_NODE_MYSELF|REDIS_NODE_NOADDR|REDIS_NODE_HANDSHAKE| - REDIS_NODE_FAIL)) continue; + (REDIS_NODE_MYSELF|REDIS_NODE_NOADDR|REDIS_NODE_HANDSHAKE)) + continue; /* Check only if we already sent a ping and did not received * a reply yet. */ if (node->ping_sent == 0 || @@ -945,7 +945,7 @@ void clusterCron(void) { } else { /* Timeout reached. Set the noad se possibly failing if it is * not already in this state. */ - if (!(node->flags & REDIS_NODE_PFAIL)) { + if (!(node->flags & (REDIS_NODE_PFAIL|REDIS_NODE_FAIL))) { redisLog(REDIS_DEBUG,"*** NODE %.40s possibly failing", node->name); node->flags |= REDIS_NODE_PFAIL; From 5a547b27f7e4a12ac6ecd4e0b7f7c25841018deb Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 7 Apr 2011 23:23:27 +0200 Subject: [PATCH 071/146] evaluate cluster state after config load. Still bugs in the slot allocation after nodes config load to fix. --- src/cluster.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cluster.c b/src/cluster.c index 6a693d6e..2ea1b9cb 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -134,6 +134,7 @@ int clusterLoadConfig(char *filename) { redisAssert(server.cluster.myself != NULL); redisLog(REDIS_NOTICE,"Node configuration loaded, I'm %.40s", server.cluster.myself->name); + clusterUpdateState(); return REDIS_OK; fmterr: From a55c7868c1d66c491399895c1274cb300c983a95 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 7 Apr 2011 23:33:18 +0200 Subject: [PATCH 072/146] fixed lame error in slot assignment --- src/cluster.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cluster.c b/src/cluster.c index 2ea1b9cb..0c3b142a 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -991,7 +991,7 @@ int clusterNodeGetSlotBit(clusterNode *n, int slot) { * an error and REDIS_ERR is returned. */ int clusterAddSlot(clusterNode *n, int slot) { redisAssert(clusterNodeSetSlotBit(n,slot) == 0); - server.cluster.slots[slot] = server.cluster.myself; + server.cluster.slots[slot] = n; printf("SLOT %d added to %.40s\n", slot, n->name); return REDIS_OK; } From fd7a584f7ab9a796338e3bd6cacf01ce5476bb91 Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 8 Apr 2011 10:17:41 +0200 Subject: [PATCH 073/146] do not process node failure messages about yourself --- src/cluster.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cluster.c b/src/cluster.c index 0c3b142a..e2b82044 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -632,7 +632,8 @@ int clusterProcessPacket(clusterLink *link) { clusterNode *failing; failing = clusterLookupNode(hdr->data.fail.about.nodename); - if (failing && !(failing->flags & REDIS_NODE_FAIL)) { + if (failing && !(failing->flags & (REDIS_NODE_FAIL|REDIS_NODE_MYSELF))) + { redisLog(REDIS_NOTICE, "FAIL message received from %.40s about %.40s", hdr->sender, hdr->data.fail.about.nodename); From 8d727af8c4d309463cd463689b50ffb299b61964 Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 8 Apr 2011 14:19:52 +0200 Subject: [PATCH 074/146] update state when FAIL is cleared from a node --- src/cluster.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cluster.c b/src/cluster.c index e2b82044..70c34aab 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -943,6 +943,7 @@ void clusterCron(void) { node->flags &= ~REDIS_NODE_PFAIL; } else if (node->flags & REDIS_NODE_FAIL && !node->numslaves) { node->flags &= ~REDIS_NODE_FAIL; + clusterUpdateState(); } } else { /* Timeout reached. Set the noad se possibly failing if it is From 1c708b25ee663985bcfb91082f8acdd6c3cb7489 Mon Sep 17 00:00:00 2001 From: Salvatore Sanfilippo Date: Mon, 11 Apr 2011 16:39:39 +0200 Subject: [PATCH 075/146] added minimal cluster section in INFO output. This is only useful to check if the instance is or not configured as a cluster node, all the other informations are accessible using the CLUSTER command. --- src/redis.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/redis.c b/src/redis.c index 9c726151..22c65354 100644 --- a/src/redis.c +++ b/src/redis.c @@ -1498,6 +1498,15 @@ sds genRedisInfoString(char *section) { } } + /* Clusetr */ + if (allsections || defsections || !strcasecmp(section,"cluster")) { + if (sections++) info = sdscat(info,"\r\n"); + info = sdscatprintf(info, + "# Cluster\r\n" + "cluster_enabled:%d\r\n", + server.cluster_enabled); + } + /* Key space */ if (allsections || defsections || !strcasecmp(section,"keyspace")) { if (sections++) info = sdscat(info,"\r\n"); From 407798c1e1552e1f5e583ead63ed6b7c8ea20299 Mon Sep 17 00:00:00 2001 From: Salvatore Sanfilippo Date: Mon, 11 Apr 2011 16:41:06 +0200 Subject: [PATCH 076/146] Redis-trib initial implementation (currently can not do any actual work) --- src/redis-trib.rb | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 src/redis-trib.rb diff --git a/src/redis-trib.rb b/src/redis-trib.rb new file mode 100755 index 00000000..d83c9f8e --- /dev/null +++ b/src/redis-trib.rb @@ -0,0 +1,70 @@ +#!/usr/bin/env ruby + +require 'rubygems' +require 'redis' + +class RedisTrib + def xputs(s) + printf s + STDOUT.flush + end + + def check_arity(req_args, num_args) + if ((req_args > 0 and num_args != req_args) || + (req_args < 0 and num_args < req_args.abs)) + puts "Wrong number of arguments for specified sub command" + exit 1 + end + end + + def parse_node(node) + s = node.split(":") + if s.length != 2 + puts "Invalid node name #{node}" + exit 1 + end + return {:host => s[0], :port => s[1].to_i} + end + + def connect_to_node(naddr) + xputs "Connecting to node #{naddr[:host]}:#{naddr[:port]}: " + begin + r = Redis.new(:host => naddr[:host], :port => naddr[:port]) + r.ping + rescue + puts "ERROR" + puts "Sorry, can't connect to node #{naddr[:host]}:#{naddr[:port]}" + exit 1 + end + puts "OK" + end + + def create_cluster + puts "Creating cluster" + ARGV[1..-1].each{|node| + naddr = parse_node(node) + r = connect_to_node(naddr) + } + end +end + +COMMANDS={ + "create-cluster" => ["create_cluster", -2] +} + +# Sanity check +if ARGV.length == 0 + puts "Usage: redis-trib " + exit 1 +end + +rt = RedisTrib.new +cmd_spec = COMMANDS[ARGV[0].downcase] +if !cmd_spec + puts "Unknown redis-trib subcommand '#{ARGV[0]}'" + exit 1 +end +rt.check_arity(cmd_spec[1],ARGV.length) + +# Dispatch +rt.send(cmd_spec[0]) From b800a3ab20440fdd55947f52f3efcc689ef858d3 Mon Sep 17 00:00:00 2001 From: Salvatore Sanfilippo Date: Mon, 11 Apr 2011 16:58:47 +0200 Subject: [PATCH 077/146] a first refactoring of redis-trib.rb --- src/redis-trib.rb | 78 +++++++++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 29 deletions(-) diff --git a/src/redis-trib.rb b/src/redis-trib.rb index d83c9f8e..8456ed29 100755 --- a/src/redis-trib.rb +++ b/src/redis-trib.rb @@ -3,12 +3,52 @@ require 'rubygems' require 'redis' -class RedisTrib - def xputs(s) - printf s - STDOUT.flush +def xputs(s) + printf s + STDOUT.flush +end + +class ClusterNode + def initialize(addr) + s = addr.split(":") + if s.length != 2 + puts "Invalid node name #{node}" + exit 1 + end + @host = s[0] + @port = s[1] end + def to_s + "#{@host}:#{@port}" + end + + def connect + xputs "Connecting to node #{self}: " + begin + @r = Redis.new(:host => @ost, :port => @port) + @r.ping + rescue + puts "ERROR" + puts "Sorry, can't connect to node #{self}" + end + puts "OK" + end + + def assert_cluster + info = @r.info + if !info["cluster_enabled"] || info["cluster_enabled"].to_i == 0 + puts "Error: Node #{self} is not configured as a cluster node." + exit 1 + end + end + + def r + @r + end +end + +class RedisTrib def check_arity(req_args, num_args) if ((req_args > 0 and num_args != req_args) || (req_args < 0 and num_args < req_args.abs)) @@ -17,33 +57,13 @@ class RedisTrib end end - def parse_node(node) - s = node.split(":") - if s.length != 2 - puts "Invalid node name #{node}" - exit 1 - end - return {:host => s[0], :port => s[1].to_i} - end - - def connect_to_node(naddr) - xputs "Connecting to node #{naddr[:host]}:#{naddr[:port]}: " - begin - r = Redis.new(:host => naddr[:host], :port => naddr[:port]) - r.ping - rescue - puts "ERROR" - puts "Sorry, can't connect to node #{naddr[:host]}:#{naddr[:port]}" - exit 1 - end - puts "OK" - end - def create_cluster puts "Creating cluster" - ARGV[1..-1].each{|node| - naddr = parse_node(node) - r = connect_to_node(naddr) + ARGV[1..-1].each{|n| + node = ClusterNode.new(n) + node.connect + node.assert_cluster + # node.assert_empty } end end From 8c4c50906cc91d8dffbb22cb4a520d11a1c6344b Mon Sep 17 00:00:00 2001 From: Salvatore Sanfilippo Date: Mon, 11 Apr 2011 17:40:35 +0200 Subject: [PATCH 078/146] added known nodes info in CLUSTER INFO --- src/cluster.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cluster.c b/src/cluster.c index e2b82044..0a580fa7 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1192,11 +1192,13 @@ void clusterCommand(redisClient *c) { "cluster_slots_ok:%d\r\n" "cluster_slots_pfail:%d\r\n" "cluster_slots_fail:%d\r\n" + "cluster_known_nodes:%lu\r\n" , statestr[server.cluster.state], slots_assigned, slots_ok, slots_pfail, - slots_fail + slots_fail, + dictSize(server.cluster.nodes) ); addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n", (unsigned long)sdslen(info))); From f29d1fb0b20d31c39ea4d092ecd4eb1e4d2f5e1b Mon Sep 17 00:00:00 2001 From: Salvatore Sanfilippo Date: Mon, 11 Apr 2011 18:26:00 +0200 Subject: [PATCH 079/146] assert_empty in redis-trib --- src/redis-trib.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/redis-trib.rb b/src/redis-trib.rb index 8456ed29..98ed0a0e 100755 --- a/src/redis-trib.rb +++ b/src/redis-trib.rb @@ -43,6 +43,14 @@ class ClusterNode end end + def assert_empty + if !(@r.cluster("info").split("\r\n").index("cluster_known_nodes:1")) || + (@r.info['db0']) + puts "Error: Node #{self} is not empty. Either the node already knows other nodes (check with nodes-info) or contains some key in database 0." + exit 1 + end + end + def r @r end @@ -63,7 +71,7 @@ class RedisTrib node = ClusterNode.new(n) node.connect node.assert_cluster - # node.assert_empty + node.assert_empty } end end From e9ee513b6a940ecd3e30a152d6ad2e8fb25147a5 Mon Sep 17 00:00:00 2001 From: antirez Date: Mon, 11 Apr 2011 21:47:45 +0200 Subject: [PATCH 080/146] TODO modified --- TODO | 2 ++ 1 file changed, 2 insertions(+) diff --git a/TODO b/TODO index e7943307..3032392e 100644 --- a/TODO +++ b/TODO @@ -36,6 +36,8 @@ OPTIMIZATIONS * SORT: Don't copy the list into a vector when BY argument is constant. * Write the hash table size of every db in the dump, so that Redis can resize the hash table just one time when loading a big DB. * Read-only mode for slaves. +* Redis big lists as linked lists of small ziplists? + Possibly a simple heuristic that join near nodes when some node gets smaller than the low_level, and split it into two if gets bigger than high_level. REPORTING ========= From 744f34d834df8424fd201305fb469e540bb29020 Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 12 Apr 2011 17:06:33 +0200 Subject: [PATCH 081/146] redis-trib can now assign hash slots to initialize a cluster. Next step is to join the cluster. --- src/redis-trib.rb | 92 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/src/redis-trib.rb b/src/redis-trib.rb index 98ed0a0e..5a0fe363 100755 --- a/src/redis-trib.rb +++ b/src/redis-trib.rb @@ -3,6 +3,8 @@ require 'rubygems' require 'redis' +ClusterHashSlots = 4096 + def xputs(s) printf s STDOUT.flush @@ -17,6 +19,8 @@ class ClusterNode end @host = s[0] @port = s[1] + @slots = {} + @dirty = false end def to_s @@ -51,12 +55,55 @@ class ClusterNode end end + def add_slots(slots) + slots.each{|s| + @slots[s] = :new + } + @dirty = true + end + + def flush_node_config + return if !@dirty + new = [] + @slots.each{|s,val| + if val == :new + new << s + @slots[s] = true + end + } + @r.cluster("addslots",*new) + @dirty = false + end + + def info + slots = @slots.map{|k,v| k}.reduce{|a,b| + a = [(a..a)] if !a.is_a?(Array) + if b == (a[-1].last)+1 + a[-1] = (a[-1].first)..b + a + else + a << (b..b) + end + }.map{|x| + (x.first == x.last) ? x.first.to_s : "#{x.first}-#{x.last}" + }.join(",") + "#{self.to_s.ljust(25)} slots:#{slots}" + end + + def is_dirty? + @dirty + end + def r @r end end class RedisTrib + def initialize + @nodes = [] + end + def check_arity(req_args, num_args) if ((req_args > 0 and num_args != req_args) || (req_args < 0 and num_args < req_args.abs)) @@ -72,7 +119,52 @@ class RedisTrib node.connect node.assert_cluster node.assert_empty + @nodes << node } + puts "Performing hash slots allocation on #{@nodes.length} nodes..." + alloc_slots + show_nodes + yes_or_die "Can I set the above configuration?" + flush_nodes_config + puts "** Nodes configuration updated" + puts "Sending CLUSTER MEET messages to join the cluster" + join_cluster + end + + def alloc_slots + slots_per_node = ClusterHashSlots/@nodes.length + i = 0 + @nodes.each{|n| + first = i*slots_per_node + last = first+slots_per_node-1 + last = ClusterHashSlots-1 if i == @nodes.length-1 + n.add_slots first..last + i += 1 + } + end + + def flush_nodes_config + @nodes.each{|n| + n.flush_node_config + } + end + + def show_nodes + @nodes.each{|n| + puts n.info + } + end + + def join_cluster + end + + def yes_or_die(msg) + print "#{msg} (type 'yes' to accept): " + STDOUT.flush + if !(STDIN.gets.chomp.downcase == "yes") + puts "Aborting..." + exit 1 + end end end From 4e17be0ef542cea7c13291126cb35bc25d3af83d Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 12 Apr 2011 19:21:03 +0200 Subject: [PATCH 082/146] TODO update --- TODO | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/TODO b/TODO index 3032392e..92b6ea93 100644 --- a/TODO +++ b/TODO @@ -9,19 +9,13 @@ WARNING: are you a possible Redis contributor? us, and *how* exactly this can be implemented to have good changes of a merge. Otherwise it is probably wasted work! Thank you -DISKSTORE TODO -============== -* Fix FLUSHALL/FLUSHDB: the queue of pending reads/writes should be handled. -* Check that 00/00 and ff/ff exist at startup, otherwise exit with error. -* Implement sync flush option, where data is written synchronously on disk when a command is executed. -* Implement MULTI/EXEC as transaction abstract API to diskstore.c, with transaction_start, transaction_end, and a journal to recover. -* Stop BGSAVE thread on shutdown and any other condition where the child is killed during normal bgsave. -* Fix RANDOMKEY to really do something interesting -* Fix DBSIZE to really do something interesting -* Add a DEBUG command to check if an entry is or not in memory currently +API CHANGES +=========== -* dscache.c near 236, kobj = createStringObject... we could use static obj. +* Turn commands into variadic versions when it makes sense, that is, when + the variable number of arguments represent values, and there is no conflict + with the return value of the command. APPEND ONLY FILE ================ @@ -61,3 +55,15 @@ KNOWN BUGS or alike) too many time passes? We should prevent expires while the AOF is loading. +DISKSTORE TODO +============== + +* Fix FLUSHALL/FLUSHDB: the queue of pending reads/writes should be handled. +* Check that 00/00 and ff/ff exist at startup, otherwise exit with error. +* Implement sync flush option, where data is written synchronously on disk when a command is executed. +* Implement MULTI/EXEC as transaction abstract API to diskstore.c, with transaction_start, transaction_end, and a journal to recover. +* Stop BGSAVE thread on shutdown and any other condition where the child is killed during normal bgsave. +* Fix RANDOMKEY to really do something interesting +* Fix DBSIZE to really do something interesting +* Add a DEBUG command to check if an entry is or not in memory currently +* dscache.c near 236, kobj = createStringObject... we could use static obj. From 1087227d4140a3bf5f0248e411fa084ba3fe2bf4 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 13 Apr 2011 10:38:56 +0200 Subject: [PATCH 083/146] redis-trib: show list of commands --- src/redis-trib.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/redis-trib.rb b/src/redis-trib.rb index 98ed0a0e..f2e14010 100755 --- a/src/redis-trib.rb +++ b/src/redis-trib.rb @@ -77,12 +77,17 @@ class RedisTrib end COMMANDS={ - "create-cluster" => ["create_cluster", -2] + "create-cluster" => ["create_cluster", -2, "node1_addr node2_addr ..."] } # Sanity check if ARGV.length == 0 puts "Usage: redis-trib " + puts + COMMANDS.each{|k,v| + puts " #{k.ljust(20)} #{v[2]}" + } + puts exit 1 end From 996d503d1aab68bf0220951ab71a1ef9b8678b4a Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 13 Apr 2011 10:58:21 +0200 Subject: [PATCH 084/146] ASCII ART FTW --- src/Makefile | 2 +- src/asciilogo.h | 18 ++++++++++++++++++ src/redis.c | 30 +++++++++++++++++++++++++++--- src/redis.h | 1 + 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 src/asciilogo.h diff --git a/src/Makefile b/src/Makefile index f99be4b9..a566d4ea 100644 --- a/src/Makefile +++ b/src/Makefile @@ -83,7 +83,7 @@ redis-check-dump.o: redis-check-dump.c lzf.h redis-cli.o: redis-cli.c fmacros.h version.h ../deps/hiredis/hiredis.h \ sds.h zmalloc.h ../deps/linenoise/linenoise.h help.h redis.o: redis.c redis.h fmacros.h config.h ae.h sds.h dict.h adlist.h \ - zmalloc.h anet.h zipmap.h ziplist.h intset.h version.h + zmalloc.h anet.h zipmap.h ziplist.h intset.h version.h asciilogo.h release.o: release.c release.h replication.o: replication.c redis.h fmacros.h config.h ae.h sds.h dict.h \ adlist.h zmalloc.h anet.h zipmap.h ziplist.h intset.h version.h diff --git a/src/asciilogo.h b/src/asciilogo.h new file mode 100644 index 00000000..878a1070 --- /dev/null +++ b/src/asciilogo.h @@ -0,0 +1,18 @@ +char *ascii_logo = +" _._ \n" +" _.-``__ ''-._ \n" +" _.-`` `. `_. ''-._ Redis %s (%s/%d) %s bit\n" +" .-`` .-```. ```\\/ _.,_ ''-._ \n" +" ( ' , .-` | `, ) Running in %s mode\n" +" |`-._`-...-` __...-.``-._|'` _.-'| Port: %d\n" +" | `-._ `._ / _.-' | PID: %ld\n" +" `-._ `-._ `-./ _.-' _.-' \n" +" |`-._`-._ `-.__.-' _.-'_.-'| \n" +" | `-._`-._ _.-'_.-' | http://redis.io \n" +" `-._ `-._`-.__.-'_.-' _.-' \n" +" |`-._`-._ `-.__.-' _.-'_.-'| \n" +" | `-._`-._ _.-'_.-' | \n" +" `-._ `-._`-.__.-'_.-' _.-' \n" +" `-._ `-.__.-' _.-' \n" +" `-._ _.-' \n" +" `-.__.-' \n\n"; diff --git a/src/redis.c b/src/redis.c index 22c65354..9112adb5 100644 --- a/src/redis.c +++ b/src/redis.c @@ -205,14 +205,20 @@ void redisLogRaw(int level, const char *msg) { time_t now = time(NULL); FILE *fp; char buf[64]; + int rawmode = (level & REDIS_LOG_RAW); + level &= 0xff; /* clear flags */ if (level < server.verbosity) return; fp = (server.logfile == NULL) ? stdout : fopen(server.logfile,"a"); if (!fp) return; - strftime(buf,sizeof(buf),"%d %b %H:%M:%S",localtime(&now)); - fprintf(fp,"[%d] %s %c %s\n",(int)getpid(),buf,c[level],msg); + if (rawmode) { + fprintf(fp,"%s",msg); + } else { + strftime(buf,sizeof(buf),"%d %b %H:%M:%S",localtime(&now)); + fprintf(fp,"[%d] %s %c %s\n",(int)getpid(),buf,c[level],msg); + } fflush(fp); if (server.logfile) fclose(fp); @@ -227,7 +233,7 @@ void redisLog(int level, const char *fmt, ...) { va_list ap; char msg[REDIS_MAX_LOGMSG_LEN]; - if (level < server.verbosity) return; + if ((level&0xff) < server.verbosity) return; va_start(ap, fmt); vsnprintf(msg, sizeof(msg), fmt, ap); @@ -1715,6 +1721,23 @@ void usage() { exit(1); } +void redisAsciiArt(void) { +#include "asciilogo.h" + char *buf = zmalloc(1024*16); + + snprintf(buf,1024*16,ascii_logo, + REDIS_VERSION, + redisGitSHA1(), + strtol(redisGitDirty(),NULL,10) > 0, + (sizeof(long) == 8) ? "64" : "32", + server.cluster_enabled ? "cluster" : "stand alone", + server.port, + (long) getpid() + ); + redisLogRaw(REDIS_NOTICE|REDIS_LOG_RAW,buf); + zfree(buf); +} + int main(int argc, char **argv) { long long start; @@ -1733,6 +1756,7 @@ int main(int argc, char **argv) { if (server.daemonize) daemonize(); initServer(); if (server.daemonize) createPidFile(); + redisAsciiArt(); redisLog(REDIS_NOTICE,"Server started, Redis version " REDIS_VERSION); #ifdef __linux__ linuxOvercommitMemoryWarning(); diff --git a/src/redis.h b/src/redis.h index 26f33451..7e3a8c8f 100644 --- a/src/redis.h +++ b/src/redis.h @@ -179,6 +179,7 @@ #define REDIS_VERBOSE 1 #define REDIS_NOTICE 2 #define REDIS_WARNING 3 +#define REDIS_LOG_RAW (1<<10) /* Modifier to log without timestamp */ /* Anti-warning macro... */ #define REDIS_NOTUSED(V) ((void) V) From 9d89e99c9946ab199d295db6faa1953252d7b80c Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 13 Apr 2011 11:42:05 +0200 Subject: [PATCH 085/146] no longer useful debugging printf removed --- src/cluster.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cluster.c b/src/cluster.c index 4e3cf746..d7e76ed4 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -994,7 +994,6 @@ int clusterNodeGetSlotBit(clusterNode *n, int slot) { int clusterAddSlot(clusterNode *n, int slot) { redisAssert(clusterNodeSetSlotBit(n,slot) == 0); server.cluster.slots[slot] = n; - printf("SLOT %d added to %.40s\n", slot, n->name); return REDIS_OK; } From 8231b1ef60e5b4068137c47765ca3907002fc530 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 13 Apr 2011 15:39:43 +0200 Subject: [PATCH 086/146] TODO updated --- TODO | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/TODO b/TODO index 92b6ea93..8f0a5db2 100644 --- a/TODO +++ b/TODO @@ -54,6 +54,11 @@ KNOWN BUGS What happens if between 1 and 2 for some reason (system under huge load or alike) too many time passes? We should prevent expires while the AOF is loading. +* #519: Slave may have expired keys that were never read in the master (so a DEL + is not sent in the replication channel) but are already expired since + a lot of time. Maybe after a given delay that is undoubltly greater than + the replication link latency we should expire this key on the slave on + access? DISKSTORE TODO ============== From 57d83d5673947bcd2406fb04d4c37cc8428648c2 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 13 Apr 2011 15:58:05 +0200 Subject: [PATCH 087/146] redis-trib create, first version is know working --- src/redis-trib.rb | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/redis-trib.rb b/src/redis-trib.rb index cd35587f..7f83f78e 100755 --- a/src/redis-trib.rb +++ b/src/redis-trib.rb @@ -75,7 +75,7 @@ class ClusterNode @dirty = false end - def info + def info_string slots = @slots.map{|k,v| k}.reduce{|a,b| a = [(a..a)] if !a.is_a?(Array) if b == (a[-1].last)+1 @@ -89,6 +89,15 @@ class ClusterNode }.join(",") "#{self.to_s.ljust(25)} slots:#{slots}" end + + def info + { + :host => @host, + :port => @port, + :slots => @slots, + :dirty => @dirty + } + end def is_dirty? @dirty @@ -127,8 +136,13 @@ class RedisTrib yes_or_die "Can I set the above configuration?" flush_nodes_config puts "** Nodes configuration updated" - puts "Sending CLUSTER MEET messages to join the cluster" + puts "** Sending CLUSTER MEET messages to join the cluster" join_cluster + check_cluster + end + + def check_cluster + puts "Check if the cluster looks sane" end def alloc_slots @@ -151,11 +165,21 @@ class RedisTrib def show_nodes @nodes.each{|n| - puts n.info + puts n.info_string } end def join_cluster + # We use a brute force approach to make sure the node will meet + # each other, that is, sending CLUSTER MEET messages to all the nodes + # about the very same node. + # Thanks to gossip this information should propagate across all the + # cluster in a matter of seconds. + first = false + @nodes.each{|n| + if !first then first = n.info; next; end # Skip the first node + n.r.cluster("meet",first[:host],first[:port]) + } end def yes_or_die(msg) @@ -169,7 +193,8 @@ class RedisTrib end COMMANDS={ - "create-cluster" => ["create_cluster", -2, "node1_addr node2_addr ..."] + "create" => ["create_cluster", -2, "host1:port host2:port ... hostN:port"], + "check" => ["check_cluster", 1, "host:port"] } # Sanity check From 583fc5dd6047b89f68690d38837eface3b015789 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 13 Apr 2011 18:40:51 +0200 Subject: [PATCH 088/146] hopefully more readable info_string method in redis-trib --- src/redis-trib.rb | 61 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/src/redis-trib.rb b/src/redis-trib.rb index 7f83f78e..3ebcf6b6 100755 --- a/src/redis-trib.rb +++ b/src/redis-trib.rb @@ -17,6 +17,7 @@ class ClusterNode puts "Invalid node name #{node}" exit 1 end + @r = nil @host = s[0] @port = s[1] @slots = {} @@ -27,7 +28,7 @@ class ClusterNode "#{@host}:#{@port}" end - def connect + def connect(o={}) xputs "Connecting to node #{self}: " begin @r = Redis.new(:host => @ost, :port => @port) @@ -35,6 +36,8 @@ class ClusterNode rescue puts "ERROR" puts "Sorry, can't connect to node #{self}" + exit 1 if o[:abort] + @r = nil end puts "OK" end @@ -76,17 +79,37 @@ class ClusterNode end def info_string - slots = @slots.map{|k,v| k}.reduce{|a,b| - a = [(a..a)] if !a.is_a?(Array) - if b == (a[-1].last)+1 - a[-1] = (a[-1].first)..b - a + # We want to display the hash slots assigned to this node + # as ranges, like in: "1-5,8,9,20-35,30" + # + # Note: this could be easily written without side effects, + # we use 'slots' just to split the computation into steps. + + # First step: we want an increasing array of integers + # for instance: [1,2,3,4,5,8,9,20,21,22,23,24,25,30] + slots = @slots.keys.sort + + # As we want to aggregate adiacent slots we convert all the + # slot integers into ranges (with just one element) + # So we have something like [1..1,2..2, ... and so forth. + slots = slots.map{|x| x..x} + + # Finally we group ranges with adiacent elements. + slots = slots.reduce([]) {|a,b| + if !a.empty? && b.first == (a[-1].last)+1 + a[0..-2] + [(a[-1].first)..(b.last)] else - a << (b..b) + a + [b] end - }.map{|x| - (x.first == x.last) ? x.first.to_s : "#{x.first}-#{x.last}" + } + + # Now our task is easy, we just convert ranges with just one + # element into a number, and a real range into a start-end format. + # Finally we join the array using the comma as separator. + slots = slots.map{|x| + x.count == 1 ? x.first.to_s : "#{x.first}-#{x.last}" }.join(",") + "#{self.to_s.ljust(25)} slots:#{slots}" end @@ -121,14 +144,18 @@ class RedisTrib end end + def add_node(node) + @nodes << node + end + def create_cluster puts "Creating cluster" ARGV[1..-1].each{|n| node = ClusterNode.new(n) - node.connect + node.connect(:abort => true) node.assert_cluster node.assert_empty - @nodes << node + add_node(node) } puts "Performing hash slots allocation on #{@nodes.length} nodes..." alloc_slots @@ -142,7 +169,15 @@ class RedisTrib end def check_cluster - puts "Check if the cluster looks sane" + puts "Performing Cluster Check (node #{ARGV[1]})" + node = ClusterNode.new(ARGV[1]) + node.connect(:abort => true) + node.assert_cluster + node.add_slots(10..15) + node.add_slots(30..30) + node.add_slots(5..5) + add_node(node) + show_nodes end def alloc_slots @@ -194,7 +229,7 @@ end COMMANDS={ "create" => ["create_cluster", -2, "host1:port host2:port ... hostN:port"], - "check" => ["check_cluster", 1, "host:port"] + "check" => ["check_cluster", 2, "host:port"] } # Sanity check From 3883a381b6ea8354a7f830c3ac508ecba7ae9de2 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 13 Apr 2011 18:46:29 +0200 Subject: [PATCH 089/146] use map! instead of reassigning --- src/redis-trib.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/redis-trib.rb b/src/redis-trib.rb index 3ebcf6b6..5ae0370a 100755 --- a/src/redis-trib.rb +++ b/src/redis-trib.rb @@ -92,7 +92,7 @@ class ClusterNode # As we want to aggregate adiacent slots we convert all the # slot integers into ranges (with just one element) # So we have something like [1..1,2..2, ... and so forth. - slots = slots.map{|x| x..x} + slots.map!{|x| x..x} # Finally we group ranges with adiacent elements. slots = slots.reduce([]) {|a,b| From 9a440ad418ac8b753f08ba98c00c2b5e52f1ca78 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 13 Apr 2011 18:50:50 +0200 Subject: [PATCH 090/146] fixed a typo in redis-trib --- src/redis-trib.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/redis-trib.rb b/src/redis-trib.rb index 5ae0370a..413f7c07 100755 --- a/src/redis-trib.rb +++ b/src/redis-trib.rb @@ -14,7 +14,7 @@ class ClusterNode def initialize(addr) s = addr.split(":") if s.length != 2 - puts "Invalid node name #{node}" + puts "Invalid node name #{addr}" exit 1 end @r = nil From 180ba187d3357b02d7efb1474466c98719fdb5cf Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 13 Apr 2011 20:17:11 +0200 Subject: [PATCH 091/146] fixed typo in comment --- src/redis-trib.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/redis-trib.rb b/src/redis-trib.rb index 413f7c07..af599cbe 100755 --- a/src/redis-trib.rb +++ b/src/redis-trib.rb @@ -80,7 +80,7 @@ class ClusterNode def info_string # We want to display the hash slots assigned to this node - # as ranges, like in: "1-5,8,9,20-35,30" + # as ranges, like in: "1-5,8-9,20-25,30" # # Note: this could be easily written without side effects, # we use 'slots' just to split the computation into steps. From 8bad0ddd9daff59137d1fc2c932bb39e77cabf4c Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 13 Apr 2011 21:26:12 +0200 Subject: [PATCH 092/146] fixed bug due to typo in code --- src/redis-trib.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/redis-trib.rb b/src/redis-trib.rb index af599cbe..ad87d6f8 100755 --- a/src/redis-trib.rb +++ b/src/redis-trib.rb @@ -31,7 +31,7 @@ class ClusterNode def connect(o={}) xputs "Connecting to node #{self}: " begin - @r = Redis.new(:host => @ost, :port => @port) + @r = Redis.new(:host => @host, :port => @port) @r.ping rescue puts "ERROR" From d0cfb2be643e5102cb24c3d22a1cf9cfc9df20d1 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 14 Apr 2011 09:41:22 +0200 Subject: [PATCH 093/146] redis-trib: functions implementing commands moved at bottom --- src/redis-trib.rb | 63 +++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/src/redis-trib.rb b/src/redis-trib.rb index ad87d6f8..31fc5230 100755 --- a/src/redis-trib.rb +++ b/src/redis-trib.rb @@ -148,35 +148,8 @@ class RedisTrib @nodes << node end - def create_cluster - puts "Creating cluster" - ARGV[1..-1].each{|n| - node = ClusterNode.new(n) - node.connect(:abort => true) - node.assert_cluster - node.assert_empty - add_node(node) - } - puts "Performing hash slots allocation on #{@nodes.length} nodes..." - alloc_slots - show_nodes - yes_or_die "Can I set the above configuration?" - flush_nodes_config - puts "** Nodes configuration updated" - puts "** Sending CLUSTER MEET messages to join the cluster" - join_cluster - check_cluster - end - def check_cluster - puts "Performing Cluster Check (node #{ARGV[1]})" - node = ClusterNode.new(ARGV[1]) - node.connect(:abort => true) - node.assert_cluster - node.add_slots(10..15) - node.add_slots(30..30) - node.add_slots(5..5) - add_node(node) + puts "Performing Cluster Check (using node #{@nodes[0]})" show_nodes end @@ -225,11 +198,41 @@ class RedisTrib exit 1 end end + + # redis-trib subcommands implementations + + def check_cluster_cmd + node = ClusterNode.new(ARGV[1]) + node.connect(:abort => true) + node.assert_cluster + add_node(node) + check_cluster + end + + def create_cluster_cmd + puts "Creating cluster" + ARGV[1..-1].each{|n| + node = ClusterNode.new(n) + node.connect(:abort => true) + node.assert_cluster + node.assert_empty + add_node(node) + } + puts "Performing hash slots allocation on #{@nodes.length} nodes..." + alloc_slots + show_nodes + yes_or_die "Can I set the above configuration?" + flush_nodes_config + puts "** Nodes configuration updated" + puts "** Sending CLUSTER MEET messages to join the cluster" + join_cluster + check_cluster + end end COMMANDS={ - "create" => ["create_cluster", -2, "host1:port host2:port ... hostN:port"], - "check" => ["check_cluster", 2, "host:port"] + "create" => ["create_cluster_cmd", -2, "host1:port host2:port ... hostN:port"], + "check" => ["check_cluster_cmd", 2, "host:port"] } # Sanity check From fb2feae599e2a190cfc6906ca9a03abc54528b61 Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 15 Apr 2011 16:35:27 +0200 Subject: [PATCH 094/146] variadic LPUSH/RPUSH --- src/redis.c | 4 ++-- src/t_list.c | 49 +++++++++++++++++++++++++++---------------------- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/redis.c b/src/redis.c index b0c3179e..38e4ce52 100644 --- a/src/redis.c +++ b/src/redis.c @@ -85,8 +85,8 @@ struct redisCommand redisCommandTable[] = { {"incr",incrCommand,2,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0}, {"decr",decrCommand,2,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0}, {"mget",mgetCommand,-2,0,NULL,1,-1,1,0,0}, - {"rpush",rpushCommand,3,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0}, - {"lpush",lpushCommand,3,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0}, + {"rpush",rpushCommand,-3,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0}, + {"lpush",lpushCommand,-3,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0}, {"rpushx",rpushxCommand,3,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0}, {"lpushx",lpushxCommand,3,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0}, {"linsert",linsertCommand,5,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0}, diff --git a/src/t_list.c b/src/t_list.c index 9e659092..adb0c409 100644 --- a/src/t_list.c +++ b/src/t_list.c @@ -259,30 +259,35 @@ void listTypeConvert(robj *subject, int enc) { *----------------------------------------------------------------------------*/ void pushGenericCommand(redisClient *c, int where) { + int j, addlen = 0, pushed = 0; robj *lobj = lookupKeyWrite(c->db,c->argv[1]); - c->argv[2] = tryObjectEncoding(c->argv[2]); - if (lobj == NULL) { - if (handleClientsWaitingListPush(c,c->argv[1],c->argv[2])) { - addReply(c,shared.cone); - return; - } - lobj = createZiplistObject(); - dbAdd(c->db,c->argv[1],lobj); - } else { - if (lobj->type != REDIS_LIST) { - addReply(c,shared.wrongtypeerr); - return; - } - if (handleClientsWaitingListPush(c,c->argv[1],c->argv[2])) { - signalModifiedKey(c->db,c->argv[1]); - addReply(c,shared.cone); - return; - } + int may_have_waiting_clients = (lobj == NULL); + + if (lobj && lobj->type != REDIS_LIST) { + addReply(c,shared.wrongtypeerr); + return; } - listTypePush(lobj,c->argv[2],where); - addReplyLongLong(c,listTypeLength(lobj)); - signalModifiedKey(c->db,c->argv[1]); - server.dirty++; + + for (j = 2; j < c->argc; j++) { + c->argv[j] = tryObjectEncoding(c->argv[j]); + if (may_have_waiting_clients) { + if (handleClientsWaitingListPush(c,c->argv[1],c->argv[j])) { + addlen++; + continue; + } else { + may_have_waiting_clients = 0; + } + } + if (!lobj) { + lobj = createZiplistObject(); + dbAdd(c->db,c->argv[1],lobj); + } + listTypePush(lobj,c->argv[j],where); + pushed++; + } + addReplyLongLong(c,addlen + (lobj ? listTypeLength(lobj) : 0)); + if (pushed) signalModifiedKey(c->db,c->argv[1]); + server.dirty += pushed; } void lpushCommand(redisClient *c) { From 72dff2c084ce67464bded6bc9f5674d5e01b2cf7 Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 15 Apr 2011 16:35:54 +0200 Subject: [PATCH 095/146] test fixed after ascii art banner modified the output of a running server --- tests/support/server.tcl | 2 +- tests/test_helper.tcl | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/support/server.tcl b/tests/support/server.tcl index 4f48d22d..c9275461 100644 --- a/tests/support/server.tcl +++ b/tests/support/server.tcl @@ -214,7 +214,7 @@ proc start_server {options {code undefined}} { # find out the pid while {![info exists pid]} { - regexp {^\[(\d+)\]} [exec head -n1 $stdout] _ pid + regexp {\[(\d+)\]} [exec cat $stdout] _ pid after 100 } diff --git a/tests/test_helper.tcl b/tests/test_helper.tcl index 156ce3c1..c594f6f6 100644 --- a/tests/test_helper.tcl +++ b/tests/test_helper.tcl @@ -127,6 +127,7 @@ proc execute_everything {} { # execute_tests "integration/redis-cli" execute_tests "unit/pubsub" + return; # No diskstore tests for now... # run tests with diskstore enabled puts "\nRunning diskstore tests... this is slow, press Ctrl+C if not interested.." set ::diskstore 1 From 9d0a8656c166f14b6a2f41d134518ffbc4bf8f25 Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 15 Apr 2011 16:52:07 +0200 Subject: [PATCH 096/146] tests for variadic list push --- tests/unit/type/list.tcl | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/unit/type/list.tcl b/tests/unit/type/list.tcl index 6b128b72..b6055b26 100644 --- a/tests/unit/type/list.tcl +++ b/tests/unit/type/list.tcl @@ -55,6 +55,13 @@ start_server { assert_equal $largevalue(linkedlist) [r lindex mylist2 2] } + test {Variadic RPUSH/LPUSH} { + r del mylist + assert_equal 4 [r lpush mylist a b c d] + assert_equal 8 [r rpush mylist 0 1 2 3] + assert_equal {d c b a 0 1 2 3} [r lrange mylist 0 -1] + } + test {DEL a list - ziplist} { assert_equal 1 [r del myziplist2] assert_equal 0 [r exists myziplist2] @@ -142,6 +149,15 @@ start_server { } } + test "BLPOP with variadic LPUSH" { + set rd [redis_deferring_client] + r del blist target + $rd blpop blist 0 + assert_equal 2 [r lpush blist foo bar] + assert_equal {blist foo} [$rd read] + assert_equal bar [lindex [r lrange blist 0 -1] 0] + } + test "BRPOPLPUSH with zero timeout should block indefinitely" { set rd [redis_deferring_client] r del blist target From 2e667806ed0cb9db4ba55743efb4984447e19b4d Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 15 Apr 2011 17:44:08 +0200 Subject: [PATCH 097/146] version bumped to 2.9, that is, 3.0 unstable --- src/version.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/version.h b/src/version.h index 3913b007..0663b556 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define REDIS_VERSION "2.3.0" +#define REDIS_VERSION "2.9.0" From 009db6764504746d64fef7e6ccf661f7882bd72e Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 15 Apr 2011 18:08:24 +0200 Subject: [PATCH 098/146] addReplyLongLong optimized to return shared objects when the value to reply is 0 or 1 --- src/networking.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/networking.c b/src/networking.c index 32c06306..c8a39ba5 100644 --- a/src/networking.c +++ b/src/networking.c @@ -321,7 +321,12 @@ void _addReplyLongLong(redisClient *c, long long ll, char prefix) { } void addReplyLongLong(redisClient *c, long long ll) { - _addReplyLongLong(c,ll,':'); + if (ll == 0) + addReply(c,shared.czero); + else if (ll == 1) + addReply(c,shared.cone); + else + _addReplyLongLong(c,ll,':'); } void addReplyMultiBulkLen(redisClient *c, long length) { From 22f294d24a1d56a655bdca69184e048e3ef49934 Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 15 Apr 2011 18:08:32 +0200 Subject: [PATCH 099/146] variadic SADD --- src/redis.c | 2 +- src/t_set.c | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/redis.c b/src/redis.c index 38e4ce52..5f07903c 100644 --- a/src/redis.c +++ b/src/redis.c @@ -102,7 +102,7 @@ struct redisCommand redisCommandTable[] = { {"ltrim",ltrimCommand,4,0,NULL,1,1,1,0,0}, {"lrem",lremCommand,4,0,NULL,1,1,1,0,0}, {"rpoplpush",rpoplpushCommand,3,REDIS_CMD_DENYOOM,NULL,1,2,1,0,0}, - {"sadd",saddCommand,3,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0}, + {"sadd",saddCommand,-3,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0}, {"srem",sremCommand,3,0,NULL,1,1,1,0,0}, {"smove",smoveCommand,4,0,NULL,1,2,1,0,0}, {"sismember",sismemberCommand,3,0,NULL,1,1,1,0,0}, diff --git a/src/t_set.c b/src/t_set.c index 7d0811ed..138a2f8d 100644 --- a/src/t_set.c +++ b/src/t_set.c @@ -218,9 +218,9 @@ void setTypeConvert(robj *setobj, int enc) { void saddCommand(redisClient *c) { robj *set; + int j, added = 0; set = lookupKeyWrite(c->db,c->argv[1]); - c->argv[2] = tryObjectEncoding(c->argv[2]); if (set == NULL) { set = setTypeCreate(c->argv[2]); dbAdd(c->db,c->argv[1],set); @@ -230,13 +230,14 @@ void saddCommand(redisClient *c) { return; } } - if (setTypeAdd(set,c->argv[2])) { - signalModifiedKey(c->db,c->argv[1]); - server.dirty++; - addReply(c,shared.cone); - } else { - addReply(c,shared.czero); + + for (j = 2; j < c->argc; j++) { + c->argv[j] = tryObjectEncoding(c->argv[j]); + if (setTypeAdd(set,c->argv[j])) added++; } + if (added) signalModifiedKey(c->db,c->argv[1]); + server.dirty += added; + addReplyLongLong(c,added); } void sremCommand(redisClient *c) { From 271f08784264e25c3dbcd751f8c9ee1b1ad8306e Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 15 Apr 2011 18:28:25 +0200 Subject: [PATCH 100/146] Variadic SADD tests --- tests/unit/type/set.tcl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/unit/type/set.tcl b/tests/unit/type/set.tcl index 5608a648..ef7d668c 100644 --- a/tests/unit/type/set.tcl +++ b/tests/unit/type/set.tcl @@ -59,6 +59,13 @@ start_server { assert_encoding hashtable myset } + test {Variadic SADD} { + r del myset + assert_equal 3 [r sadd myset a b c] + assert_equal 2 [r sadd myset A a b c B] + assert_equal [lsort {A a b c B}] [lsort [r smembers myset]] + } + test "Set encoding after DEBUG RELOAD" { r del myintset myhashset mylargeintset for {set i 0} {$i < 100} {incr i} { r sadd myintset $i } From 64a13a36e685c318319a70b775f91f2c34bcc34f Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 19 Apr 2011 17:07:55 +0200 Subject: [PATCH 101/146] variadic HDEL with tests --- src/redis.c | 2 +- src/t_hash.c | 19 ++++++++++++------- tests/unit/type/hash.tcl | 9 +++++++++ 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/redis.c b/src/redis.c index 5f07903c..bf9ced2c 100644 --- a/src/redis.c +++ b/src/redis.c @@ -138,7 +138,7 @@ struct redisCommand redisCommandTable[] = { {"hmset",hmsetCommand,-4,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0}, {"hmget",hmgetCommand,-3,0,NULL,1,1,1,0,0}, {"hincrby",hincrbyCommand,4,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0}, - {"hdel",hdelCommand,3,0,NULL,1,1,1,0,0}, + {"hdel",hdelCommand,-3,0,NULL,1,1,1,0,0}, {"hlen",hlenCommand,2,0,NULL,1,1,1,0,0}, {"hkeys",hkeysCommand,2,0,NULL,1,1,1,0,0}, {"hvals",hvalsCommand,2,0,NULL,1,1,1,0,0}, diff --git a/src/t_hash.c b/src/t_hash.c index 5e7525bd..4b9b37d6 100644 --- a/src/t_hash.c +++ b/src/t_hash.c @@ -396,17 +396,22 @@ void hmgetCommand(redisClient *c) { void hdelCommand(redisClient *c) { robj *o; + int j, deleted = 0; + if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL || checkType(c,o,REDIS_HASH)) return; - if (hashTypeDelete(o,c->argv[2])) { - if (hashTypeLength(o) == 0) dbDelete(c->db,c->argv[1]); - addReply(c,shared.cone); - signalModifiedKey(c->db,c->argv[1]); - server.dirty++; - } else { - addReply(c,shared.czero); + for (j = 2; j < c->argc; j++) { + if (hashTypeDelete(o,c->argv[j])) { + if (hashTypeLength(o) == 0) dbDelete(c->db,c->argv[1]); + deleted++; + } } + if (deleted) { + signalModifiedKey(c->db,c->argv[1]); + server.dirty += deleted; + } + addReplyLongLong(c,deleted); } void hlenCommand(redisClient *c) { diff --git a/tests/unit/type/hash.tcl b/tests/unit/type/hash.tcl index 8559dc3c..9b043d3f 100644 --- a/tests/unit/type/hash.tcl +++ b/tests/unit/type/hash.tcl @@ -226,6 +226,15 @@ start_server {tags {"hash"}} { set _ $rv } {0 0 1 0 {} 1 0 {}} + test {HDEL - more than a single value} { + set rv {} + r del myhash + r hmset myhash a 1 b 2 c 3 + assert_equal 0 [r hdel myhash x y] + assert_equal 2 [r hdel myhash a c f] + r hgetall myhash + } {b 2} + test {HEXISTS} { set rv {} set k [lindex [array names smallhash *] 0] From b3a96d454e9a4a7f8d88bb0e6f301427b5cfaa3e Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 19 Apr 2011 17:37:03 +0200 Subject: [PATCH 102/146] Variadic SREM --- src/redis.c | 2 +- src/t_set.c | 19 +++++++++++-------- tests/unit/type/set.tcl | 8 ++++++++ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/redis.c b/src/redis.c index bf9ced2c..1ac95702 100644 --- a/src/redis.c +++ b/src/redis.c @@ -103,7 +103,7 @@ struct redisCommand redisCommandTable[] = { {"lrem",lremCommand,4,0,NULL,1,1,1,0,0}, {"rpoplpush",rpoplpushCommand,3,REDIS_CMD_DENYOOM,NULL,1,2,1,0,0}, {"sadd",saddCommand,-3,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0}, - {"srem",sremCommand,3,0,NULL,1,1,1,0,0}, + {"srem",sremCommand,-3,0,NULL,1,1,1,0,0}, {"smove",smoveCommand,4,0,NULL,1,2,1,0,0}, {"sismember",sismemberCommand,3,0,NULL,1,1,1,0,0}, {"scard",scardCommand,2,0,NULL,1,1,1,0,0}, diff --git a/src/t_set.c b/src/t_set.c index 138a2f8d..b221e2e9 100644 --- a/src/t_set.c +++ b/src/t_set.c @@ -242,19 +242,22 @@ void saddCommand(redisClient *c) { void sremCommand(redisClient *c) { robj *set; + int j, deleted = 0; if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL || checkType(c,set,REDIS_SET)) return; - c->argv[2] = tryObjectEncoding(c->argv[2]); - if (setTypeRemove(set,c->argv[2])) { - if (setTypeSize(set) == 0) dbDelete(c->db,c->argv[1]); - signalModifiedKey(c->db,c->argv[1]); - server.dirty++; - addReply(c,shared.cone); - } else { - addReply(c,shared.czero); + for (j = 2; j < c->argc; j++) { + if (setTypeRemove(set,c->argv[j])) { + if (setTypeSize(set) == 0) dbDelete(c->db,c->argv[1]); + deleted++; + } } + if (deleted) { + signalModifiedKey(c->db,c->argv[1]); + server.dirty += deleted; + } + addReplyLongLong(c,deleted); } void smoveCommand(redisClient *c) { diff --git a/tests/unit/type/set.tcl b/tests/unit/type/set.tcl index ef7d668c..1a37ed61 100644 --- a/tests/unit/type/set.tcl +++ b/tests/unit/type/set.tcl @@ -97,6 +97,14 @@ start_server { assert_equal {3 5} [lsort [r smembers myset]] } + test {SREM with multiple arguments} { + r del myset + r sadd myset a b c d + assert_equal 0 [r srem myset k k k] + assert_equal 2 [r srem myset b d x y] + lsort [r smembers myset] + } {a c} + foreach {type} {hashtable intset} { for {set i 1} {$i <= 5} {incr i} { r del [format "set%d" $i] From c7ba7b8bbb97e8b9e388ec0e9a2ea161c23a3fd0 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 20 Apr 2011 12:51:03 +0200 Subject: [PATCH 103/146] removed check for zmalloc return NULL in createClient(). The check was misplaced, and zmalloc never returns NULL. --- src/networking.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/networking.c b/src/networking.c index c8a39ba5..995b910c 100644 --- a/src/networking.c +++ b/src/networking.c @@ -16,7 +16,6 @@ redisClient *createClient(int fd) { anetNonBlock(NULL,fd); anetTcpNoDelay(NULL,fd); - if (!c) return NULL; if (aeCreateFileEvent(server.el,fd,AE_READABLE, readQueryFromClient, c) == AE_ERR) { From 17b24ff30d5c84c489a9793e35b7497519a878b9 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 21 Apr 2011 10:49:52 +0200 Subject: [PATCH 104/146] save peak memory usage as statistic and show it in INFO. Also a new INFO field was added showing the fragmentation ratio using the peak memory info. --- src/redis.c | 18 ++++++++++++++++-- src/redis.h | 1 + 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/redis.c b/src/redis.c index 1ac95702..b19ee891 100644 --- a/src/redis.c +++ b/src/redis.c @@ -585,6 +585,10 @@ int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) { */ updateLRUClock(); + /* Record the max memory used since the server was started. */ + if (zmalloc_used_memory() > server.stat_peak_memory) + server.stat_peak_memory = zmalloc_used_memory(); + /* We received a SIGTERM, shutting down here in a safe way, as it is * not ok doing so inside the signal handler. */ if (server.shutdown_asap) { @@ -957,6 +961,7 @@ void initServer() { server.stat_starttime = time(NULL); server.stat_keyspace_misses = 0; server.stat_keyspace_hits = 0; + server.stat_peak_memory = 0; server.unixtime = time(NULL); aeCreateTimeEvent(server.el, 1, serverCron, NULL, NULL); if (server.ipfd > 0 && aeCreateFileEvent(server.el,server.ipfd,AE_READABLE, @@ -1240,7 +1245,6 @@ sds genRedisInfoString(char *section) { sds info = sdsempty(); time_t uptime = time(NULL)-server.stat_starttime; int j, numcommands; - char hmem[64]; struct rusage self_ru, c_ru; unsigned long lol, bib; int allsections = 0, defsections = 0; @@ -1254,7 +1258,6 @@ sds genRedisInfoString(char *section) { getrusage(RUSAGE_SELF, &self_ru); getrusage(RUSAGE_CHILDREN, &c_ru); getClientsMaxBuffers(&lol,&bib); - bytesToHuman(hmem,zmalloc_used_memory()); /* Server */ if (allsections || defsections || !strcasecmp(section,"server")) { @@ -1299,18 +1302,29 @@ sds genRedisInfoString(char *section) { /* Memory */ if (allsections || defsections || !strcasecmp(section,"memory")) { + char hmem[64]; + char peak_hmem[64]; + + bytesToHuman(hmem,zmalloc_used_memory()); + bytesToHuman(peak_hmem,server.stat_peak_memory); if (sections++) info = sdscat(info,"\r\n"); info = sdscatprintf(info, "# Memory\r\n" "used_memory:%zu\r\n" "used_memory_human:%s\r\n" "used_memory_rss:%zu\r\n" + "used_memory_peak:%zu\r\n" + "used_memory_peak_human:%s\r\n" "mem_fragmentation_ratio:%.2f\r\n" + "peak_mem_fragmentation_ratio:%.2f\r\n" "use_tcmalloc:%d\r\n", zmalloc_used_memory(), hmem, zmalloc_get_rss(), + server.stat_peak_memory, + peak_hmem, zmalloc_get_fragmentation_ratio(), + (float)zmalloc_get_rss()/server.stat_peak_memory, #ifdef USE_TCMALLOC 1 #else diff --git a/src/redis.h b/src/redis.h index 5506e365..19d2ddd1 100644 --- a/src/redis.h +++ b/src/redis.h @@ -518,6 +518,7 @@ struct redisServer { long long stat_evictedkeys; /* number of evicted keys (maxmemory) */ long long stat_keyspace_hits; /* number of successful lookups of keys */ long long stat_keyspace_misses; /* number of failed lookups of keys */ + size_t stat_peak_memory; /* max used memory record */ /* Configuration */ int verbosity; int maxidletime; From 3cd12b568776fcee3284ca692ad25e4ddce93c95 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 21 Apr 2011 15:38:02 +0200 Subject: [PATCH 105/146] CLIENT LIST implemented --- src/anet.c | 10 ++++++++++ src/anet.h | 1 + src/networking.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/redis-cli.c | 5 ++++- src/redis.c | 3 ++- src/redis.h | 1 + 6 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/anet.c b/src/anet.c index 4e16f2e4..692cef19 100644 --- a/src/anet.c +++ b/src/anet.c @@ -345,3 +345,13 @@ int anetUnixAccept(char *err, int s) { return fd; } + +int anetPeerToString(int fd, char *ip, int *port) { + struct sockaddr_in sa; + socklen_t salen = sizeof(sa); + + if (getpeername(fd,(struct sockaddr*)&sa,&salen) == -1) return -1; + if (ip) strcpy(ip,inet_ntoa(sa.sin_addr)); + if (port) *port = ntohs(sa.sin_port); + return 0; +} diff --git a/src/anet.h b/src/anet.h index 118b4dda..2b2dea45 100644 --- a/src/anet.h +++ b/src/anet.h @@ -53,5 +53,6 @@ int anetWrite(int fd, char *buf, int count); int anetNonBlock(char *err, int fd); int anetTcpNoDelay(char *err, int fd); int anetTcpKeepAlive(char *err, int fd); +int anetPeerToString(int fd, char *ip, int *port); #endif diff --git a/src/networking.c b/src/networking.c index 995b910c..8949c948 100644 --- a/src/networking.c +++ b/src/networking.c @@ -869,3 +869,49 @@ void getClientsMaxBuffers(unsigned long *longest_output_list, *biggest_input_buffer = bib; } +void clientCommand(redisClient *c) { + if (!strcasecmp(c->argv[1]->ptr,"list") && c->argc == 2) { + listNode *ln; + listIter li; + sds o = sdsempty(); + time_t now = time(NULL); + + listRewind(server.clients,&li); + while ((ln = listNext(&li)) != NULL) { + redisClient *client; + char ip[32], flags[16], *p; + int port; + + client = listNodeValue(ln); + if (anetPeerToString(client->fd,ip,&port) == -1) continue; + p = flags; + if (client->flags & REDIS_SLAVE) { + if (client->flags & REDIS_MONITOR) + *p++ = 'O'; + else + *p++ = 'S'; + } + if (client->flags & REDIS_MASTER) *p++ = 'M'; + if (p == flags) *p++ = 'N'; + if (client->flags & REDIS_MULTI) *p++ = 'x'; + if (client->flags & REDIS_BLOCKED) *p++ = 'b'; + if (client->flags & REDIS_IO_WAIT) *p++ = 'i'; + if (client->flags & REDIS_DIRTY_CAS) *p++ = 'd'; + if (client->flags & REDIS_CLOSE_AFTER_REPLY) *p++ = 'c'; + if (client->flags & REDIS_UNBLOCKED) *p++ = 'u'; + *p++ = '\0'; + o = sdscatprintf(o, + "addr=%s:%d fd=%d idle=%ld flags=%s db=%d sub=%d psub=%d\n", + ip,port,client->fd, + (long)(now - client->lastinteraction), + flags, + client->db->id, + (int) dictSize(client->pubsub_channels), + (int) listLength(client->pubsub_patterns)); + } + addReplyBulkCBuffer(c,o,sdslen(o)); + sdsfree(o); + } else { + addReplyError(c, "Syntax error, try CLIENT (LIST | KILL ip:port)"); + } +} diff --git a/src/redis-cli.c b/src/redis-cli.c index 1b23c0b1..b53a4c82 100644 --- a/src/redis-cli.c +++ b/src/redis-cli.c @@ -471,7 +471,10 @@ static int cliSendCommand(int argc, char **argv, int repeat) { if (!strcasecmp(command,"info") || (argc == 2 && !strcasecmp(command,"cluster") && (!strcasecmp(argv[1],"nodes") || - !strcasecmp(argv[1],"info")))) + !strcasecmp(argv[1],"info"))) || + (argc == 2 && !strcasecmp(command,"client") && + !strcasecmp(argv[1],"list"))) + { output_raw = 1; } diff --git a/src/redis.c b/src/redis.c index b19ee891..b164fdd1 100644 --- a/src/redis.c +++ b/src/redis.c @@ -192,7 +192,8 @@ struct redisCommand redisCommandTable[] = { {"restore",restoreCommand,4,0,NULL,0,0,0,0,0}, {"migrate",migrateCommand,6,0,NULL,0,0,0,0,0}, {"dump",dumpCommand,2,0,NULL,0,0,0,0,0}, - {"object",objectCommand,-2,0,NULL,0,0,0,0,0} + {"object",objectCommand,-2,0,NULL,0,0,0,0,0}, + {"client",clientCommand,-2,0,NULL,0,0,0,0,0} }; /*============================ Utility functions ============================ */ diff --git a/src/redis.h b/src/redis.h index 19d2ddd1..952fcbf6 100644 --- a/src/redis.h +++ b/src/redis.h @@ -1200,6 +1200,7 @@ void restoreCommand(redisClient *c); void migrateCommand(redisClient *c); void dumpCommand(redisClient *c); void objectCommand(redisClient *c); +void clientCommand(redisClient *c); #if defined(__GNUC__) void *calloc(size_t count, size_t size) __attribute__ ((deprecated)); From b93fdb7bbb5b224efee06b36a12d454db975fed8 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 21 Apr 2011 15:47:47 +0200 Subject: [PATCH 106/146] CLIENT KILL implemented --- src/networking.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/networking.c b/src/networking.c index 8949c948..32d071bd 100644 --- a/src/networking.c +++ b/src/networking.c @@ -870,15 +870,16 @@ void getClientsMaxBuffers(unsigned long *longest_output_list, } void clientCommand(redisClient *c) { + listNode *ln; + listIter li; + redisClient *client; + if (!strcasecmp(c->argv[1]->ptr,"list") && c->argc == 2) { - listNode *ln; - listIter li; sds o = sdsempty(); time_t now = time(NULL); listRewind(server.clients,&li); while ((ln = listNext(&li)) != NULL) { - redisClient *client; char ip[32], flags[16], *p; int port; @@ -911,6 +912,26 @@ void clientCommand(redisClient *c) { } addReplyBulkCBuffer(c,o,sdslen(o)); sdsfree(o); + } else if (!strcasecmp(c->argv[1]->ptr,"kill") && c->argc == 3) { + listRewind(server.clients,&li); + while ((ln = listNext(&li)) != NULL) { + char ip[32], addr[64]; + int port; + + client = listNodeValue(ln); + if (anetPeerToString(client->fd,ip,&port) == -1) continue; + snprintf(addr,sizeof(addr),"%s:%d",ip,port); + if (strcmp(addr,c->argv[2]->ptr) == 0) { + addReply(c,shared.ok); + if (c == client) { + client->flags |= REDIS_CLOSE_AFTER_REPLY; + } else { + freeClient(client); + } + return; + } + } + addReplyError(c,"No such client"); } else { addReplyError(c, "Syntax error, try CLIENT (LIST | KILL ip:port)"); } From 7e79dd3f4cf7a4236319f9ee1a408155eda4b881 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 21 Apr 2011 16:56:31 +0200 Subject: [PATCH 107/146] peak fragmentation ratio removed as it is a confusing field for users and trivial to compute at hand now that there is peak memory information in INFO output --- src/redis.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/redis.c b/src/redis.c index b164fdd1..96786df9 100644 --- a/src/redis.c +++ b/src/redis.c @@ -1317,7 +1317,6 @@ sds genRedisInfoString(char *section) { "used_memory_peak:%zu\r\n" "used_memory_peak_human:%s\r\n" "mem_fragmentation_ratio:%.2f\r\n" - "peak_mem_fragmentation_ratio:%.2f\r\n" "use_tcmalloc:%d\r\n", zmalloc_used_memory(), hmem, @@ -1325,7 +1324,6 @@ sds genRedisInfoString(char *section) { server.stat_peak_memory, peak_hmem, zmalloc_get_fragmentation_ratio(), - (float)zmalloc_get_rss()/server.stat_peak_memory, #ifdef USE_TCMALLOC 1 #else From d8b6ae3cd6ac9cfab51696b9b35d3c70fa2d55ba Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Fri, 22 Apr 2011 09:37:28 +0200 Subject: [PATCH 108/146] Reformat AOF tests --- tests/integration/aof.tcl | 54 +++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/tests/integration/aof.tcl b/tests/integration/aof.tcl index 4cbe6eaa..c7ba93c1 100644 --- a/tests/integration/aof.tcl +++ b/tests/integration/aof.tcl @@ -31,13 +31,14 @@ tags {"aof"} { } start_server_aof [list dir $server_path] { - test {Unfinished MULTI: Server should not have been started} { - is_alive $srv - } {0} + test "Unfinished MULTI: Server should not have been started" { + assert_equal 0 [is_alive $srv] + } - test {Unfinished MULTI: Server should have logged an error} { - exec cat [dict get $srv stdout] | tail -n1 - } {*Unexpected end of file reading the append only file*} + test "Unfinished MULTI: Server should have logged an error" { + set result [exec cat [dict get $srv stdout] | tail -n1] + assert_match "*Unexpected end of file reading the append only file*" $result + } } ## Test that the server exits when the AOF contains a short read @@ -47,36 +48,39 @@ tags {"aof"} { } start_server_aof [list dir $server_path] { - test {Short read: Server should not have been started} { - is_alive $srv - } {0} + test "Short read: Server should not have been started" { + assert_equal 0 [is_alive $srv] + } - test {Short read: Server should have logged an error} { - exec cat [dict get $srv stdout] | tail -n1 - } {*Bad file format reading the append only file*} + test "Short read: Server should have logged an error" { + set result [exec cat [dict get $srv stdout] | tail -n1] + assert_match "*Bad file format reading the append only file*" $result + } } ## Test that redis-check-aof indeed sees this AOF is not valid - test {Short read: Utility should confirm the AOF is not valid} { + test "Short read: Utility should confirm the AOF is not valid" { catch { exec src/redis-check-aof $aof_path - } str - set _ $str - } {*not valid*} + } result + assert_match "*not valid*" $result + } - test {Short read: Utility should be able to fix the AOF} { - exec echo y | src/redis-check-aof --fix $aof_path - } {*Successfully truncated AOF*} + test "Short read: Utility should be able to fix the AOF" { + set result [exec echo y | src/redis-check-aof --fix $aof_path] + assert_match "*Successfully truncated AOF*" $result + } ## Test that the server can be started using the truncated AOF start_server_aof [list dir $server_path] { - test {Fixed AOF: Server should have been started} { - is_alive $srv - } {1} + test "Fixed AOF: Server should have been started" { + assert_equal 1 [is_alive $srv] + } - test {Fixed AOF: Keyspace should contain values that were parsable} { + test "Fixed AOF: Keyspace should contain values that were parsable" { set client [redis [dict get $srv host] [dict get $srv port]] - list [$client get foo] [$client get bar] - } {hello {}} + assert_equal "hello" [$client get foo] + assert_equal "" [$client get bar] + } } } From 45b0f6fb14f2a0d5bca0b4e32cb44a31e29aa921 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Fri, 22 Apr 2011 09:44:06 +0200 Subject: [PATCH 109/146] Use correct argc/argv for cleanup when loading AOF --- src/aof.c | 8 +++++--- tests/integration/aof.tcl | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/aof.c b/src/aof.c index 5d75c374..e5327cb2 100644 --- a/src/aof.c +++ b/src/aof.c @@ -284,9 +284,11 @@ int loadAppendOnlyFile(char *filename) { /* The fake client should not have a reply */ redisAssert(fakeClient->bufpos == 0 && listLength(fakeClient->reply) == 0); - /* Clean up, ready for the next command */ - for (j = 0; j < argc; j++) decrRefCount(argv[j]); - zfree(argv); + /* Clean up. Command code may have changed argv/argc so we use the + * argv/argc of the client instead of the local variables. */ + for (j = 0; j < fakeClient->argc; j++) + decrRefCount(fakeClient->argv[j]); + zfree(fakeClient->argv); } /* This point can only be reached when EOF is reached without errors. diff --git a/tests/integration/aof.tcl b/tests/integration/aof.tcl index c7ba93c1..927969b6 100644 --- a/tests/integration/aof.tcl +++ b/tests/integration/aof.tcl @@ -83,4 +83,22 @@ tags {"aof"} { assert_equal "" [$client get bar] } } + + ## Test that SPOP (that modifies the client its argc/argv) is correctly free'd + create_aof { + append_to_aof [formatCommand sadd set foo] + append_to_aof [formatCommand sadd set bar] + append_to_aof [formatCommand spop set] + } + + start_server_aof [list dir $server_path] { + test "AOF+SPOP: Server should have been started" { + assert_equal 1 [is_alive $srv] + } + + test "AOF+SPOP: Set should have 1 member" { + set client [redis [dict get $srv host] [dict get $srv port]] + assert_equal 1 [$client scard set] + } + } } From c772d9c6e7a4b65075c6efd15a53e84bb3c7ba3f Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 28 Apr 2011 19:00:33 +0200 Subject: [PATCH 110/146] take a hashslot -> keys index, will be used for cluster rehasing --- src/cluster.c | 1 + src/db.c | 40 ++++++++++++++++++++++++++++++++++++- src/redis.h | 55 ++++++++++++++++++++++++++++++--------------------- src/t_zset.c | 6 ------ 4 files changed, 73 insertions(+), 29 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index d7e76ed4..b3548755 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -202,6 +202,7 @@ void clusterInit(void) { } if (aeCreateFileEvent(server.el, server.cfd, AE_READABLE, clusterAcceptHandler, NULL) == AE_ERR) oom("creating file event"); + server.cluster.slots_to_keys = zslCreate(); } /* ----------------------------------------------------------------------------- diff --git a/src/db.c b/src/db.c index 85a22062..7d323924 100644 --- a/src/db.c +++ b/src/db.c @@ -2,6 +2,9 @@ #include +void SlotToKeyAdd(robj *key); +void SlotToKeyDel(robj *key); + /*----------------------------------------------------------------------------- * C-level DB API *----------------------------------------------------------------------------*/ @@ -131,6 +134,7 @@ int dbAdd(redisDb *db, robj *key, robj *val) { sds copy = sdsdup(key->ptr); dictAdd(db->dict, copy, val); if (server.ds_enabled) cacheSetKeyMayExist(db,key); + if (server.cluster_enabled) SlotToKeyAdd(key); return REDIS_OK; } } @@ -146,6 +150,7 @@ int dbReplace(redisDb *db, robj *key, robj *val) { if ((oldval = dictFetchValue(db->dict,key->ptr)) == NULL) { sds copy = sdsdup(key->ptr); dictAdd(db->dict, copy, val); + if (server.cluster_enabled) SlotToKeyAdd(key); retval = 1; } else { dictReplace(db->dict, key->ptr, val); @@ -198,7 +203,12 @@ int dbDelete(redisDb *db, robj *key) { /* Deleting an entry from the expires dict will not free the sds of * the key, because it is shared with the main dictionary. */ if (dictSize(db->expires) > 0) dictDelete(db->expires,key->ptr); - return dictDelete(db->dict,key->ptr) == DICT_OK; + if (dictDelete(db->dict,key->ptr) == DICT_OK) { + if (server.cluster_enabled) SlotToKeyDel(key); + return 1; + } else { + return 0; + } } /* Empty the whole database. @@ -698,3 +708,31 @@ int *zunionInterGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *num *numkeys = num; return keys; } + +/* Slot to Key API. This is used by Redis Cluster in order to obtain in + * a fast way a key that belongs to a specified hash slot. This is useful + * while rehashing the cluster. */ +void SlotToKeyAdd(robj *key) { + unsigned int hashslot = keyHashSlot(key->ptr,sdslen(key->ptr)); + + zslInsert(server.cluster.slots_to_keys,hashslot,key); + incrRefCount(key); +} + +void SlotToKeyDel(robj *key) { + unsigned int hashslot = keyHashSlot(key->ptr,sdslen(key->ptr)); + + zslDelete(server.cluster.slots_to_keys,hashslot,key); +} + +robj *GetKeyInSlot(unsigned int hashslot) { + zskiplistNode *n; + zrangespec range; + + range.min = range.max = hashslot; + range.minex = range.maxex = 0; + + n = zslFirstInRange(server.cluster.slots_to_keys, range); + if (!n) return NULL; + return n->obj; +} diff --git a/src/redis.h b/src/redis.h index 952fcbf6..0baa7d81 100644 --- a/src/redis.h +++ b/src/redis.h @@ -367,6 +367,28 @@ struct sharedObjectsStruct { *integers[REDIS_SHARED_INTEGERS]; }; +/* ZSETs use a specialized version of Skiplists */ +typedef struct zskiplistNode { + robj *obj; + double score; + struct zskiplistNode *backward; + struct zskiplistLevel { + struct zskiplistNode *forward; + unsigned int span; + } level[]; +} zskiplistNode; + +typedef struct zskiplist { + struct zskiplistNode *header, *tail; + unsigned long length; + int level; +} zskiplist; + +typedef struct zset { + dict *dict; + zskiplist *zsl; +} zset; + /*----------------------------------------------------------------------------- * Redis cluster data structures *----------------------------------------------------------------------------*/ @@ -425,6 +447,7 @@ typedef struct { clusterNode *migrating_slots_to[REDIS_CLUSTER_SLOTS]; clusterNode *importing_slots_from[REDIS_CLUSTER_SLOTS]; clusterNode *slots[REDIS_CLUSTER_SLOTS]; + zskiplist *slots_to_keys; } clusterState; /* Redis cluster messages header */ @@ -627,6 +650,7 @@ struct redisServer { /* Misc */ unsigned lruclock:22; /* clock incrementing every minute, for LRU */ unsigned lruclock_padding:10; + /* Cluster */ int cluster_enabled; clusterState cluster; }; @@ -671,28 +695,6 @@ typedef struct _redisSortOperation { robj *pattern; } redisSortOperation; -/* ZSETs use a specialized version of Skiplists */ -typedef struct zskiplistNode { - robj *obj; - double score; - struct zskiplistNode *backward; - struct zskiplistLevel { - struct zskiplistNode *forward; - unsigned int span; - } level[]; -} zskiplistNode; - -typedef struct zskiplist { - struct zskiplistNode *header, *tail; - unsigned long length; - int level; -} zskiplist; - -typedef struct zset { - dict *dict; - zskiplist *zsl; -} zset; - /* DIsk store threaded I/O request message */ #define REDIS_IOJOB_LOAD 0 #define REDIS_IOJOB_SAVE 1 @@ -921,10 +923,19 @@ int startAppendOnly(void); void backgroundRewriteDoneHandler(int exitcode, int bysignal); /* Sorted sets data type */ + +/* Struct to hold a inclusive/exclusive range spec. */ +typedef struct { + double min, max; + int minex, maxex; /* are min or max exclusive? */ +} zrangespec; + zskiplist *zslCreate(void); void zslFree(zskiplist *zsl); zskiplistNode *zslInsert(zskiplist *zsl, double score, robj *obj); unsigned char *zzlInsert(unsigned char *zl, robj *ele, double score); +int zslDelete(zskiplist *zsl, double score, robj *obj); +zskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec range); double zzlGetScore(unsigned char *sptr); void zzlNext(unsigned char *zl, unsigned char **eptr, unsigned char **sptr); void zzlPrev(unsigned char *zl, unsigned char **eptr, unsigned char **sptr); diff --git a/src/t_zset.c b/src/t_zset.c index 7ce60349..e9da9fdb 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -174,12 +174,6 @@ int zslDelete(zskiplist *zsl, double score, robj *obj) { return 0; /* not found */ } -/* Struct to hold a inclusive/exclusive range spec. */ -typedef struct { - double min, max; - int minex, maxex; /* are min or max exclusive? */ -} zrangespec; - static int zslValueGteMin(double value, zrangespec *spec) { return spec->minex ? (value > spec->min) : (value >= spec->min); } From d37299e3b737addf5a2f6cd1ea47afabdf98730c Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 29 Apr 2011 14:18:16 +0200 Subject: [PATCH 111/146] Fixed a bug with replication where SLAVEOF NO ONE caused a slave to close the connection with its slaves --- src/networking.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/networking.c b/src/networking.c index 32d071bd..29eed888 100644 --- a/src/networking.c +++ b/src/networking.c @@ -526,10 +526,16 @@ void freeClient(redisClient *c) { * close the connection with all our slaves if we have any, so * when we'll resync with the master the other slaves will sync again * with us as well. Note that also when the slave is not connected - * to the master it will keep refusing connections by other slaves. */ - while (listLength(server.slaves)) { - ln = listFirst(server.slaves); - freeClient((redisClient*)ln->value); + * to the master it will keep refusing connections by other slaves. + * + * We do this only if server.masterhost != NULL. If it is NULL this + * means the user called SLAVEOF NO ONE and we are freeing our + * link with the master, so no need to close link with slaves. */ + if (server.masterhost != NULL) { + while (listLength(server.slaves)) { + ln = listFirst(server.slaves); + freeClient((redisClient*)ln->value); + } } } /* Release memory */ From 1eb713a4c1e601bfc86963f7eab3fe36a0a65b17 Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 29 Apr 2011 14:31:18 +0200 Subject: [PATCH 112/146] CLUSTER KEYSLOT command --- src/cluster.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/cluster.c b/src/cluster.c index b3548755..6c43bbf8 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1205,6 +1205,10 @@ void clusterCommand(redisClient *c) { (unsigned long)sdslen(info))); addReplySds(c,info); addReply(c,shared.crlf); + } else if (!strcasecmp(c->argv[1]->ptr,"keyslot") && c->argc == 3) { + sds key = c->argv[2]->ptr; + + addReplyLongLong(c,keyHashSlot(key,sdslen(key))); } else { addReplyError(c,"Wrong CLUSTER subcommand or number of arguments"); } From 484354ff95e6e1b93552ef9576422709a1c739c2 Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 29 Apr 2011 16:17:58 +0200 Subject: [PATCH 113/146] CLUSTER GETKEYSINSLOT implemented --- src/cluster.c | 21 +++++++++++++++++++++ src/db.c | 10 +++++++--- src/redis.h | 1 + 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index 6c43bbf8..5b747264 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1209,6 +1209,27 @@ void clusterCommand(redisClient *c) { sds key = c->argv[2]->ptr; addReplyLongLong(c,keyHashSlot(key,sdslen(key))); + } else if (!strcasecmp(c->argv[1]->ptr,"getkeysinslot") && c->argc == 4) { + long long maxkeys, slot; + unsigned int numkeys; + int j; + robj **keys; + + if (getLongLongFromObjectOrReply(c,c->argv[2],&slot,NULL) != REDIS_OK) + return; + if (getLongLongFromObjectOrReply(c,c->argv[3],&maxkeys,NULL) != REDIS_OK) + return; + if (slot < 0 || slot >= REDIS_CLUSTER_SLOTS || maxkeys < 0 || + maxkeys > 1024*1024) { + addReplyError(c,"Invalid slot or number of keys"); + return; + } + + keys = zmalloc(sizeof(robj*)*maxkeys); + numkeys = GetKeysInSlot(slot, keys, maxkeys); + addReplyMultiBulkLen(c,numkeys); + for (j = 0; j < numkeys; j++) addReplyBulk(c,keys[j]); + zfree(keys); } else { addReplyError(c,"Wrong CLUSTER subcommand or number of arguments"); } diff --git a/src/db.c b/src/db.c index 7d323924..670e2bce 100644 --- a/src/db.c +++ b/src/db.c @@ -725,14 +725,18 @@ void SlotToKeyDel(robj *key) { zslDelete(server.cluster.slots_to_keys,hashslot,key); } -robj *GetKeyInSlot(unsigned int hashslot) { +unsigned int GetKeysInSlot(unsigned int hashslot, robj **keys, unsigned int count) { zskiplistNode *n; zrangespec range; + int j = 0; range.min = range.max = hashslot; range.minex = range.maxex = 0; n = zslFirstInRange(server.cluster.slots_to_keys, range); - if (!n) return NULL; - return n->obj; + while(n && n->score == hashslot && count--) { + keys[j++] = n->obj; + n = n->level[0].forward; + } + return j; } diff --git a/src/redis.h b/src/redis.h index 0baa7d81..b6955805 100644 --- a/src/redis.h +++ b/src/redis.h @@ -1065,6 +1065,7 @@ long long emptyDb(); int selectDb(redisClient *c, int id); void signalModifiedKey(redisDb *db, robj *key); void signalFlushedDb(int dbid); +unsigned int GetKeysInSlot(unsigned int hashslot, robj **keys, unsigned int count); /* API to get key arguments from commands */ #define REDIS_GETKEYS_ALL 0 From 2f52dac9effe41f79e502c2c8fb78181c874a704 Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 29 Apr 2011 17:34:03 +0200 Subject: [PATCH 114/146] CLUSTER subcommands to set slots in migrating or importing state. Still a work in progress... --- src/cluster.c | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index 5b747264..b2af3761 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1168,6 +1168,44 @@ void clusterCommand(redisClient *c) { clusterUpdateState(); clusterSaveConfigOrDie(); addReply(c,shared.ok); + } else if (!strcasecmp(c->argv[1]->ptr,"setslot") && c->argc >= 4) { + /* SETSLOT 10 MIGRATING */ + /* SETSLOT 10 IMPORTING */ + /* SETSLOT 10 STABLE */ + long long aux; + unsigned int slot; + clusterNode *n; + + if (getLongLongFromObjectOrReply(c,c->argv[2],&aux,NULL) != REDIS_OK) + return; + if (aux < 0 || aux >= REDIS_CLUSTER_SLOTS) { + addReplyError(c,"Slot out of range"); + return; + } + slot = (unsigned int) aux; + if (server.cluster.slots[slot] != server.cluster.myself) { + addReplyErrorFormat(c,"I'm not the owner of hash slot %u",slot); + return; + } + if (!strcasecmp(c->argv[3]->ptr,"migrating") && c->argc == 5) { + if ((n = clusterLookupNode(c->argv[4]->ptr)) == NULL) { + addReplyErrorFormat(c,"I don't know about node %s", + (char*)c->argv[4]->ptr); + return; + } + server.cluster.migrating_slots_to[slot] = n; + } else if (!strcasecmp(c->argv[3]->ptr,"importing") && c->argc == 5) { + if ((n = clusterLookupNode(c->argv[4]->ptr)) == NULL) { + addReplyErrorFormat(c,"I don't know about node %s", + (char*)c->argv[3]->ptr); + return; + } + server.cluster.importing_slots_from[slot] = n; + } else if (!strcasecmp(c->argv[3]->ptr,"stable") && c->argc == 4) { + server.cluster.importing_slots_from[slot] = NULL; + } else { + addReplyError(c,"Invalid CLUSTER SETSLOT action or number of arguments"); + } } else if (!strcasecmp(c->argv[1]->ptr,"info") && c->argc == 2) { char *statestr[] = {"ok","fail","needhelp"}; int slots_assigned = 0, slots_ok = 0, slots_pfail = 0, slots_fail = 0; @@ -1211,8 +1249,7 @@ void clusterCommand(redisClient *c) { addReplyLongLong(c,keyHashSlot(key,sdslen(key))); } else if (!strcasecmp(c->argv[1]->ptr,"getkeysinslot") && c->argc == 4) { long long maxkeys, slot; - unsigned int numkeys; - int j; + unsigned int numkeys, j; robj **keys; if (getLongLongFromObjectOrReply(c,c->argv[2],&slot,NULL) != REDIS_OK) From 5cbe90dbfd5d9f46f5fa42e4e96a40a46d29b234 Mon Sep 17 00:00:00 2001 From: antirez Date: Sun, 1 May 2011 15:36:47 +0200 Subject: [PATCH 115/146] Fixed a problem with string2ll() --- src/util.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/util.c b/src/util.c index 599dc690..2404c77c 100644 --- a/src/util.c +++ b/src/util.c @@ -222,10 +222,13 @@ int string2ll(char *s, size_t slen, long long *value) { return 0; } - /* First digit should be 1-9. */ + /* First digit should be 1-9, otherwise the string should just be 0. */ if (p[0] >= '1' && p[0] <= '9') { v = p[0]-'0'; p++; plen++; + } else if (p[0] == '0' && slen == 1) { + *value = 0; + return 1; } else { return 0; } From 66f2517feb78574cf0decb5669beb03245a1d72d Mon Sep 17 00:00:00 2001 From: antirez Date: Mon, 2 May 2011 19:04:33 +0200 Subject: [PATCH 116/146] render migrating and importing slots in cluster nodes info --- src/cluster.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/cluster.c b/src/cluster.c index b2af3761..9957d487 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1086,6 +1086,21 @@ sds clusterGenNodesDescription(void) { start = -1; } } + + /* Just for MYSELF node we also dump info about slots that + * we are migrating to other instances or importing from other + * instances. */ + if (node->flags & REDIS_NODE_MYSELF) { + for (j = 0; j < REDIS_CLUSTER_SLOTS; j++) { + if (server.cluster.migrating_slots_to[j]) { + ci = sdscatprintf(ci," [%d->%.40s]",j, + server.cluster.migrating_slots_to[j]->name); + } else if (server.cluster.importing_slots_from[j]) { + ci = sdscatprintf(ci," [%d<-%.40s]",j, + server.cluster.importing_slots_from[j]->name); + } + } + } ci = sdscatlen(ci,"\n",1); } dictReleaseIterator(di); @@ -1206,6 +1221,7 @@ void clusterCommand(redisClient *c) { } else { addReplyError(c,"Invalid CLUSTER SETSLOT action or number of arguments"); } + addReply(c,shared.ok); } else if (!strcasecmp(c->argv[1]->ptr,"info") && c->argc == 2) { char *statestr[] = {"ok","fail","needhelp"}; int slots_assigned = 0, slots_ok = 0, slots_pfail = 0, slots_fail = 0; From 0ba2932298e3e432f32441ebc5d95a86158d706c Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 4 May 2011 09:31:37 +0200 Subject: [PATCH 117/146] CLUSTER SETSLOT implemented --- src/cluster.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index 9957d487..c4a56f4d 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -115,7 +115,30 @@ int clusterLoadConfig(char *filename) { for (j = 7; j < argc; j++) { int start, stop; - if ((p = strchr(argv[j],'-')) != NULL) { + if (argv[j][0] == '[') { + /* Here we handle migrating / importing slots */ + int slot; + char direction; + clusterNode *cn; + + p = strchr(argv[j],'-'); + redisAssert(p != NULL); + *p = '\0'; + direction = p[1]; /* Either '>' or '<' */ + slot = atoi(argv[j]+1); + p += 3; + cn = clusterLookupNode(p); + if (!cn) { + cn = createClusterNode(p,0); + clusterAddNode(cn); + } + if (direction == '>') { + server.cluster.migrating_slots_to[slot] = cn; + } else { + server.cluster.importing_slots_from[slot] = cn; + } + continue; + } else if ((p = strchr(argv[j],'-')) != NULL) { *p = '\0'; start = atoi(argv[j]); stop = atoi(p+1); @@ -1093,10 +1116,10 @@ sds clusterGenNodesDescription(void) { if (node->flags & REDIS_NODE_MYSELF) { for (j = 0; j < REDIS_CLUSTER_SLOTS; j++) { if (server.cluster.migrating_slots_to[j]) { - ci = sdscatprintf(ci," [%d->%.40s]",j, + ci = sdscatprintf(ci," [%d->-%.40s]",j, server.cluster.migrating_slots_to[j]->name); } else if (server.cluster.importing_slots_from[j]) { - ci = sdscatprintf(ci," [%d<-%.40s]",j, + ci = sdscatprintf(ci," [%d-<-%.40s]",j, server.cluster.importing_slots_from[j]->name); } } @@ -1221,6 +1244,7 @@ void clusterCommand(redisClient *c) { } else { addReplyError(c,"Invalid CLUSTER SETSLOT action or number of arguments"); } + clusterSaveConfigOrDie(); addReply(c,shared.ok); } else if (!strcasecmp(c->argv[1]->ptr,"info") && c->argc == 2) { char *statestr[] = {"ok","fail","needhelp"}; From 0d8f1ac359e2c4d87a1417a9b8bc6bc7a093d1ac Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 3 May 2011 23:32:37 +0200 Subject: [PATCH 118/146] initial pretty printing in makefile --- src/Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Makefile b/src/Makefile index a566d4ea..fe485bd6 100644 --- a/src/Makefile +++ b/src/Makefile @@ -138,7 +138,8 @@ redis-check-aof: $(CHECKAOFOBJ) $(CC) -o $(CHECKAOFPRGNAME) $(CCOPT) $(DEBUG) $(CHECKAOFOBJ) .c.o: - $(CC) -c $(CFLAGS) $(DEBUG) $(COMPILE_TIME) $< + @$(CC) -c $(CFLAGS) $(DEBUG) $(COMPILE_TIME) $< + @echo "\033[34mCC\033[0m \033[33m$<\033[0m" clean: rm -rf $(PRGNAME) $(BENCHPRGNAME) $(CLIPRGNAME) $(CHECKDUMPPRGNAME) $(CHECKAOFPRGNAME) *.o *.gcda *.gcno *.gcov From 35845afba07982407c7ce35dc5df5a70e21cb4f0 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 4 May 2011 10:17:05 +0200 Subject: [PATCH 119/146] Better pretty printed makefile output --- src/Makefile | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/Makefile b/src/Makefile index fe485bd6..00bdf481 100644 --- a/src/Makefile +++ b/src/Makefile @@ -25,6 +25,14 @@ PREFIX= /usr/local INSTALL_BIN= $(PREFIX)/bin INSTALL= cp -p + +CCCOLOR="\033[34m" +LINKCOLOR="\033[34;1m" +SRCCOLOR="\033[33m" +BINCOLOR="\033[37;1m" +MAKECOLOR="\033[32;1m" +ENDCOLOR="\033[0m" + OBJ = adlist.o ae.o anet.o dict.o redis.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o dscache.o pubsub.o multi.o debug.o sort.o intset.o syncio.o diskstore.o cluster.o crc16.o endian.o BENCHOBJ = ae.o anet.o redis-benchmark.o sds.o adlist.o zmalloc.o CLIOBJ = anet.o sds.o adlist.o redis-cli.o zmalloc.o release.o @@ -112,34 +120,43 @@ zipmap.o: zipmap.c zmalloc.h zmalloc.o: zmalloc.c config.h dependencies: - cd ../deps/hiredis && $(MAKE) static ARCH="$(ARCH)" - cd ../deps/linenoise && $(MAKE) ARCH="$(ARCH)" + @echo $(MAKECOLOR)MAKE$(ENDCOLOR) $(BINCOLOR)hiredis$(ENDCOLOR) + @cd ../deps/hiredis && $(MAKE) static ARCH="$(ARCH)" + @echo $(MAKECOLOR)MAKE$(ENDCOLOR) $(BINCOLOR)linenoise$(ENDCOLOR) + @cd ../deps/linenoise && $(MAKE) ARCH="$(ARCH)" redis-server: $(OBJ) - $(CC) -o $(PRGNAME) $(CCOPT) $(DEBUG) $(OBJ) + @$(CC) -o $(PRGNAME) $(CCOPT) $(DEBUG) $(OBJ) + @echo $(LINKCOLOR)LINK$(ENDCOLOR) $(BINCOLOR)$(@)$(ENDCOLOR) redis-benchmark: dependencies $(BENCHOBJ) - cd ../deps/hiredis && $(MAKE) static - $(CC) -o $(BENCHPRGNAME) $(CCOPT) $(DEBUG) $(BENCHOBJ) ../deps/hiredis/libhiredis.a + @cd ../deps/hiredis && $(MAKE) static + @$(CC) -o $(BENCHPRGNAME) $(CCOPT) $(DEBUG) $(BENCHOBJ) ../deps/hiredis/libhiredis.a + @echo $(LINKCOLOR)LINK$(ENDCOLOR) $(BINCOLOR)$(@)$(ENDCOLOR) redis-benchmark.o: - $(CC) -c $(CFLAGS) -I../deps/hiredis $(DEBUG) $(COMPILE_TIME) $< + @$(CC) -c $(CFLAGS) -I../deps/hiredis $(DEBUG) $(COMPILE_TIME) $< + @echo $(CCCOLOR)CC$(ENDCOLOR) $(SRCCOLOR)$(<)$(ENDCOLOR) redis-cli: dependencies $(CLIOBJ) - $(CC) -o $(CLIPRGNAME) $(CCOPT) $(DEBUG) $(CLIOBJ) ../deps/hiredis/libhiredis.a ../deps/linenoise/linenoise.o + @$(CC) -o $(CLIPRGNAME) $(CCOPT) $(DEBUG) $(CLIOBJ) ../deps/hiredis/libhiredis.a ../deps/linenoise/linenoise.o + @echo $(LINKCOLOR)LINK$(ENDCOLOR) $(BINCOLOR)$(@)$(ENDCOLOR) redis-cli.o: - $(CC) -c $(CFLAGS) -I../deps/hiredis -I../deps/linenoise $(DEBUG) $(COMPILE_TIME) $< + @$(CC) -c $(CFLAGS) -I../deps/hiredis -I../deps/linenoise $(DEBUG) $(COMPILE_TIME) $< + @echo $(CCCOLOR)CC$(ENDCOLOR) $(SRCCOLOR)$(<)$(ENDCOLOR) redis-check-dump: $(CHECKDUMPOBJ) - $(CC) -o $(CHECKDUMPPRGNAME) $(CCOPT) $(DEBUG) $(CHECKDUMPOBJ) + @$(CC) -o $(CHECKDUMPPRGNAME) $(CCOPT) $(DEBUG) $(CHECKDUMPOBJ) + @echo $(LINKCOLOR)LINK$(ENDCOLOR) $(BINCOLOR)$(@)$(ENDCOLOR) redis-check-aof: $(CHECKAOFOBJ) - $(CC) -o $(CHECKAOFPRGNAME) $(CCOPT) $(DEBUG) $(CHECKAOFOBJ) + @$(CC) -o $(CHECKAOFPRGNAME) $(CCOPT) $(DEBUG) $(CHECKAOFOBJ) + @echo $(LINKCOLOR)LINK$(ENDCOLOR) $(BINCOLOR)$(@)$(ENDCOLOR) .c.o: @$(CC) -c $(CFLAGS) $(DEBUG) $(COMPILE_TIME) $< - @echo "\033[34mCC\033[0m \033[33m$<\033[0m" + @echo $(CCCOLOR)CC$(ENDCOLOR) $(SRCCOLOR)$(<)$(ENDCOLOR) clean: rm -rf $(PRGNAME) $(BENCHPRGNAME) $(CLIPRGNAME) $(CHECKDUMPPRGNAME) $(CHECKAOFPRGNAME) *.o *.gcda *.gcno *.gcov From a5dce40726757a6679b534a5f8456120ff974483 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 4 May 2011 10:30:22 +0200 Subject: [PATCH 120/146] if /dev/urandom is not available use rand() to get a random node name --- src/cluster.c | 8 +++----- src/t_zset.c | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index c4a56f4d..95889201 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -24,12 +24,10 @@ void clusterGetRandomName(char *p) { char *charset = "0123456789abcdef"; int j; - if (!fp) { - redisLog(REDIS_WARNING, - "Unrecovarable error: can't open /dev/urandom:%s" ,strerror(errno)); - exit(1); + if (fp == NULL || fread(p,REDIS_CLUSTER_NAMELEN,1,fp) == 0) { + for (j = 0; j < REDIS_CLUSTER_NAMELEN; j++) + p[j] = rand(); } - fread(p,REDIS_CLUSTER_NAMELEN,1,fp); for (j = 0; j < REDIS_CLUSTER_NAMELEN; j++) p[j] = charset[p[j] & 0x0F]; fclose(fp); diff --git a/src/t_zset.c b/src/t_zset.c index e9da9fdb..3e76ebd1 100644 --- a/src/t_zset.c +++ b/src/t_zset.c @@ -1233,7 +1233,7 @@ int zuiNext(zsetopsrc *op, zsetopval *val) { if (op->type == REDIS_SET) { iterset *it = &op->iter.set; if (op->encoding == REDIS_ENCODING_INTSET) { - if (!intsetGet(it->is.is,it->is.ii,&val->ell)) + if (!intsetGet(it->is.is,it->is.ii,(int64_t*)&val->ell)) return 0; val->score = 1.0; From eda827f8b71b81a1170f5524c52bea7db249eb58 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 5 May 2011 11:13:21 +0200 Subject: [PATCH 121/146] cluster import/export of hash slots implemented in the query redirection engine --- src/cluster.c | 71 ++++++++++++++++++++++++++++++++++++++------------- src/redis.c | 8 +++--- src/redis.h | 2 +- 3 files changed, 59 insertions(+), 22 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index 95889201..b77a61b0 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1605,12 +1605,19 @@ file_rd_err: /* Return the pointer to the cluster node that is able to serve the query * as all the keys belong to hash slots for which the node is in charge. * - * If keys in query spawn multiple nodes NULL is returned. */ -clusterNode *getNodeByQuery(redisClient *c, struct redisCommand *cmd, robj **argv, int argc, int *hashslot) { + * If the returned node should be used only for this request, the *ask + * integer is set to '1', otherwise to '0'. This is used in order to + * let the caller know if we should reply with -MOVED or with -ASK. + * + * If the request contains more than a single key NULL is returned, + * however a request with more then a key argument where the key is always + * the same is valid, like in: RPOPLPUSH mylist mylist.*/ +clusterNode *getNodeByQuery(redisClient *c, struct redisCommand *cmd, robj **argv, int argc, int *hashslot, int *ask) { clusterNode *n = NULL; + robj *firstkey = NULL; multiState *ms, _ms; multiCmd mc; - int i; + int i, slot = 0; /* We handle all the cases as if they were EXEC commands, so we have * a common code path for everything */ @@ -1620,7 +1627,9 @@ clusterNode *getNodeByQuery(redisClient *c, struct redisCommand *cmd, robj **arg if (!(c->flags & REDIS_MULTI)) return server.cluster.myself; ms = &c->mstate; } else { - /* Create a fake Multi State structure, with just one command */ + /* In order to have a single codepath create a fake Multi State + * structure if the client is not in MULTI/EXEC state, this way + * we have a single codepath below. */ ms = &_ms; _ms.commands = &mc; _ms.count = 1; @@ -1629,6 +1638,8 @@ clusterNode *getNodeByQuery(redisClient *c, struct redisCommand *cmd, robj **arg mc.cmd = cmd; } + /* Check that all the keys are the same key, and get the slot and + * node for this key. */ for (i = 0; i < ms->count; i++) { struct redisCommand *mcmd; robj **margv; @@ -1641,24 +1652,48 @@ clusterNode *getNodeByQuery(redisClient *c, struct redisCommand *cmd, robj **arg keyindex = getKeysFromCommand(mcmd,margv,margc,&numkeys, REDIS_GETKEYS_PRELOAD); for (j = 0; j < numkeys; j++) { - int slot = keyHashSlot((char*)margv[keyindex[j]]->ptr, - sdslen(margv[keyindex[j]]->ptr)); - struct clusterNode *slotnode; + if (firstkey == NULL) { + /* This is the first key we see. Check what is the slot + * and node. */ + firstkey = margv[keyindex[j]]; - slotnode = server.cluster.slots[slot]; - if (hashslot) *hashslot = slot; - /* Node not assigned? (Should never happen actually - * if we reached this function). - * Different node than the previous one? - * Return NULL, the cluster can't serve multi-node requests */ - if (slotnode == NULL || (n && slotnode != n)) { - getKeysFreeResult(keyindex); - return NULL; + slot = keyHashSlot((char*)firstkey->ptr, sdslen(firstkey->ptr)); + n = server.cluster.slots[slot]; + redisAssert(n != NULL); } else { - n = slotnode; + /* If it is not the first key, make sure it is exactly + * the same key as the first we saw. */ + if (!equalStringObjects(firstkey,margv[keyindex[j]])) { + decrRefCount(firstkey); + getKeysFreeResult(keyindex); + return NULL; + } } } getKeysFreeResult(keyindex); } - return (n == NULL) ? server.cluster.myself : n; + if (ask) *ask = 0; /* This is the default. Set to 1 if needed later. */ + /* No key at all in command? then we can serve the request + * without redirections. */ + if (n == NULL) return server.cluster.myself; + if (hashslot) *hashslot = slot; + /* This request is about a slot we are migrating into another instance? + * Then we need to check if we have the key. If we have it we can reply. + * If instead is a new key, we pass the request to the node that is + * receiving the slot. */ + if (n == server.cluster.myself && + server.cluster.migrating_slots_to[slot] != NULL) + { + if (lookupKeyRead(&server.db[0],firstkey) == NULL) { + if (ask) *ask = 1; + return server.cluster.migrating_slots_to[slot]; + } + } + /* Handle the case in which we are receiving this hash slot from + * another instance, so we'll accept the query even if in the table + * it is assigned to a different node. */ + if (server.cluster.importing_slots_from[slot] != NULL) + return server.cluster.myself; + /* It's not a -ASK case. Base case: just return the right node. */ + return n; } diff --git a/src/redis.c b/src/redis.c index 96786df9..5ddbb177 100644 --- a/src/redis.c +++ b/src/redis.c @@ -1096,13 +1096,15 @@ int processCommand(redisClient *c) { addReplyError(c,"The cluster is down. Check with CLUSTER INFO for more information"); return REDIS_OK; } else { - clusterNode *n = getNodeByQuery(c,cmd,c->argv,c->argc,&hashslot); + int ask; + clusterNode *n = getNodeByQuery(c,cmd,c->argv,c->argc,&hashslot,&ask); if (n == NULL) { - addReplyError(c,"Invalid cross-node request"); + addReplyError(c,"Multi keys request invalid in cluster"); return REDIS_OK; } else if (n != server.cluster.myself) { addReplySds(c,sdscatprintf(sdsempty(), - "-MOVED %d %s:%d\r\n",hashslot,n->ip,n->port)); + "-%s %d %s:%d\r\n", ask ? "ASK" : "MOVED", + hashslot,n->ip,n->port)); return REDIS_OK; } } diff --git a/src/redis.h b/src/redis.h index b6955805..8098a8de 100644 --- a/src/redis.h +++ b/src/redis.h @@ -1083,7 +1083,7 @@ unsigned int keyHashSlot(char *key, int keylen); clusterNode *createClusterNode(char *nodename, int flags); int clusterAddNode(clusterNode *node); void clusterCron(void); -clusterNode *getNodeByQuery(redisClient *c, struct redisCommand *cmd, robj **argv, int argc, int *hashslot); +clusterNode *getNodeByQuery(redisClient *c, struct redisCommand *cmd, robj **argv, int argc, int *hashslot, int *ask); /* Git SHA1 */ char *redisGitSHA1(void); From b7d3bf51575525c0475a13941d8eb44f4800b9cd Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Thu, 5 May 2011 14:55:51 +0200 Subject: [PATCH 122/146] Fix ziplist regression and update stresser --- src/ziplist.c | 65 +++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/src/ziplist.c b/src/ziplist.c index 1c492f25..37c2c18f 100644 --- a/src/ziplist.c +++ b/src/ziplist.c @@ -408,12 +408,17 @@ static unsigned char *__ziplistCascadeUpdate(unsigned char *zl, unsigned char *p offset = p-zl; extra = rawlensize-next.prevrawlensize; zl = ziplistResize(zl,curlen+extra); - ZIPLIST_TAIL_OFFSET(zl) += extra; p = zl+offset; - /* Move the tail to the back. */ + /* Current pointer and offset for next element. */ np = p+rawlen; noffset = np-zl; + + /* Update tail offset when next element is not the tail element. */ + if ((zl+ZIPLIST_TAIL_OFFSET(zl)) != np) + ZIPLIST_TAIL_OFFSET(zl) += extra; + + /* Move the tail to the back. */ memmove(np+rawlensize, np+next.prevrawlensize, curlen-noffset-next.prevrawlensize-1); @@ -889,7 +894,7 @@ void pop(unsigned char *zl, int where) { } } -void randstring(char *target, unsigned int min, unsigned int max) { +int randstring(char *target, unsigned int min, unsigned int max) { int p, len = min+rand()%(max-min+1); int minval, maxval; switch(rand() % 3) { @@ -911,10 +916,9 @@ void randstring(char *target, unsigned int min, unsigned int max) { while(p < len) target[p++] = minval+rand()%(maxval-minval+1); - return; + return len; } - int main(int argc, char **argv) { unsigned char *zl, *p; unsigned char *entry; @@ -1247,6 +1251,7 @@ int main(int argc, char **argv) { int i,j,len,where; unsigned char *p; char buf[1024]; + int buflen; list *ref; listNode *refnode; @@ -1255,10 +1260,6 @@ int main(int argc, char **argv) { unsigned int slen; long long sval; - /* In the regression for the cascade bug, it was triggered - * with a random seed of 2. */ - srand(2); - for (i = 0; i < 20000; i++) { zl = ziplistNew(); ref = listCreate(); @@ -1268,31 +1269,32 @@ int main(int argc, char **argv) { /* Create lists */ for (j = 0; j < len; j++) { where = (rand() & 1) ? ZIPLIST_HEAD : ZIPLIST_TAIL; - switch(rand() % 4) { - case 0: - sprintf(buf,"%lld",(0LL + rand()) >> 20); - break; - case 1: - sprintf(buf,"%lld",(0LL + rand())); - break; - case 2: - sprintf(buf,"%lld",(0LL + rand()) << 20); - break; - case 3: - randstring(buf,0,256); - break; - default: - assert(NULL); + if (rand() % 2) { + buflen = randstring(buf,1,sizeof(buf)-1); + } else { + switch(rand() % 3) { + case 0: + buflen = sprintf(buf,"%lld",(0LL + rand()) >> 20); + break; + case 1: + buflen = sprintf(buf,"%lld",(0LL + rand())); + break; + case 2: + buflen = sprintf(buf,"%lld",(0LL + rand()) << 20); + break; + default: + assert(NULL); + } } /* Add to ziplist */ - zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), where); + zl = ziplistPush(zl, (unsigned char*)buf, buflen, where); /* Add to reference list */ if (where == ZIPLIST_HEAD) { - listAddNodeHead(ref,sdsnew(buf)); + listAddNodeHead(ref,sdsnewlen(buf, buflen)); } else if (where == ZIPLIST_TAIL) { - listAddNodeTail(ref,sdsnew(buf)); + listAddNodeTail(ref,sdsnewlen(buf, buflen)); } else { assert(NULL); } @@ -1307,12 +1309,13 @@ int main(int argc, char **argv) { assert(ziplistGet(p,&sstr,&slen,&sval)); if (sstr == NULL) { - sprintf(buf,"%lld",sval); + buflen = sprintf(buf,"%lld",sval); } else { - memcpy(buf,sstr,slen); - buf[slen] = '\0'; + buflen = slen; + memcpy(buf,sstr,buflen); + buf[buflen] = '\0'; } - assert(strcmp(buf,listNodeValue(refnode)) == 0); + assert(memcmp(buf,listNodeValue(refnode),buflen) == 0); } zfree(zl); listRelease(ref); From 5af302011c80076447403b454c781cf210d1d715 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Mon, 18 Apr 2011 21:09:12 +0200 Subject: [PATCH 123/146] Use custom string2ll and strchr --- src/networking.c | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/src/networking.c b/src/networking.c index 29eed888..9d41b380 100644 --- a/src/networking.c +++ b/src/networking.c @@ -700,46 +700,48 @@ static void setProtocolError(redisClient *c, int pos) { int processMultibulkBuffer(redisClient *c) { char *newline = NULL; - char *eptr; - int pos = 0, tolerr; - long bulklen; + int pos = 0, ok; + long long ll; if (c->multibulklen == 0) { /* The client should have been reset */ redisAssert(c->argc == 0); /* Multi bulk length cannot be read without a \r\n */ - newline = strstr(c->querybuf,"\r\n"); + newline = strchr(c->querybuf,'\r'); if (newline == NULL) return REDIS_ERR; /* We know for sure there is a whole line since newline != NULL, * so go ahead and find out the multi bulk length. */ redisAssert(c->querybuf[0] == '*'); - c->multibulklen = strtol(c->querybuf+1,&eptr,10); - pos = (newline-c->querybuf)+2; - if (c->multibulklen <= 0) { - c->querybuf = sdsrange(c->querybuf,pos,-1); - return REDIS_OK; - } else if (c->multibulklen > 1024*1024) { + ok = string2ll(c->querybuf+1,newline-(c->querybuf+1),&ll); + if (!ok || ll > 1024*1024) { addReplyError(c,"Protocol error: invalid multibulk length"); setProtocolError(c,pos); return REDIS_ERR; + } else { + pos = (newline-c->querybuf)+2; + if (ll <= 0) { + c->querybuf = sdsrange(c->querybuf,pos,-1); + return REDIS_OK; + } } + c->multibulklen = ll; /* Setup argv array on client structure */ if (c->argv) zfree(c->argv); c->argv = zmalloc(sizeof(robj*)*c->multibulklen); /* Search new newline */ - newline = strstr(c->querybuf+pos,"\r\n"); + newline = strchr(c->querybuf+pos,'\r'); } redisAssert(c->multibulklen > 0); while(c->multibulklen) { /* Read bulk length if unknown */ if (c->bulklen == -1) { - newline = strstr(c->querybuf+pos,"\r\n"); + newline = strchr(c->querybuf+pos,'\r'); if (newline != NULL) { if (c->querybuf[pos] != '$') { addReplyErrorFormat(c, @@ -749,17 +751,14 @@ int processMultibulkBuffer(redisClient *c) { return REDIS_ERR; } - bulklen = strtol(c->querybuf+pos+1,&eptr,10); - tolerr = (eptr[0] != '\r'); - if (tolerr || bulklen == LONG_MIN || bulklen == LONG_MAX || - bulklen < 0 || bulklen > 512*1024*1024) - { + ok = string2ll(c->querybuf+pos+1,newline-(c->querybuf+pos+1),&ll); + if (!ok || ll < 0 || ll > 512*1024*1024) { addReplyError(c,"Protocol error: invalid bulk length"); setProtocolError(c,pos); return REDIS_ERR; } - pos += eptr-(c->querybuf+pos)+2; - c->bulklen = bulklen; + pos += newline-(c->querybuf+pos)+2; + c->bulklen = ll; } else { /* No newline in current buffer, so wait for more data */ break; From 9703b1b3dc59aa76a314373fff748e26bdd0466f Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Tue, 19 Apr 2011 23:07:36 +0200 Subject: [PATCH 124/146] Update hiredis --- deps/hiredis/Makefile | 11 +- deps/hiredis/README.md | 4 +- deps/hiredis/async.c | 334 +++++++++++++++++++++++++++++++++------- deps/hiredis/async.h | 34 +++-- deps/hiredis/dict.c | 338 +++++++++++++++++++++++++++++++++++++++++ deps/hiredis/dict.h | 126 +++++++++++++++ deps/hiredis/example.c | 3 +- deps/hiredis/fmacros.h | 9 +- deps/hiredis/hiredis.c | 123 +++++++++------ deps/hiredis/hiredis.h | 18 ++- deps/hiredis/net.c | 118 +++++++++++--- deps/hiredis/net.h | 7 +- deps/hiredis/sds.c | 145 ++++++++++++++++-- deps/hiredis/sds.h | 13 +- deps/hiredis/test.c | 85 +++++++++-- 15 files changed, 1188 insertions(+), 180 deletions(-) create mode 100644 deps/hiredis/dict.c create mode 100644 deps/hiredis/dict.h diff --git a/deps/hiredis/Makefile b/deps/hiredis/Makefile index ca3404a4..2a84b9b3 100644 --- a/deps/hiredis/Makefile +++ b/deps/hiredis/Makefile @@ -15,8 +15,9 @@ ifeq ($(uname_S),SunOS) DYLIB_MAKE_CMD?=$(CC) -G -o ${DYLIBNAME} ${OBJ} STLIBNAME?=libhiredis.a STLIB_MAKE_CMD?=ar rcs ${STLIBNAME} ${OBJ} -else ifeq ($(uname_S),Darwin) - CFLAGS?=-std=c99 -pedantic $(OPTIMIZATION) -fPIC -Wall -W -Wwrite-strings $(ARCH) $(PROF) +else +ifeq ($(uname_S),Darwin) + CFLAGS?=-std=c99 -pedantic $(OPTIMIZATION) -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings $(ARCH) $(PROF) CCLINK?=-lm -pthread LDFLAGS?=-L. -Wl,-rpath,. OBJARCH?=-arch i386 -arch x86_64 @@ -25,7 +26,7 @@ else ifeq ($(uname_S),Darwin) STLIBNAME?=libhiredis.a STLIB_MAKE_CMD?=libtool -static -o ${STLIBNAME} - ${OBJ} else - CFLAGS?=-std=c99 -pedantic $(OPTIMIZATION) -fPIC -Wall -W -Wwrite-strings $(ARCH) $(PROF) + CFLAGS?=-std=c99 -pedantic $(OPTIMIZATION) -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings $(ARCH) $(PROF) CCLINK?=-lm -pthread LDFLAGS?=-L. -Wl,-rpath,. DYLIBNAME?=libhiredis.so @@ -33,6 +34,8 @@ else STLIBNAME?=libhiredis.a STLIB_MAKE_CMD?=ar rcs ${STLIBNAME} ${OBJ} endif +endif + CCOPT= $(CFLAGS) $(CCLINK) DEBUG?= -g -ggdb @@ -45,7 +48,7 @@ all: ${DYLIBNAME} ${BINS} # Deps (use make dep to generate this) net.o: net.c fmacros.h net.h -async.o: async.c async.h hiredis.h sds.h util.h +async.o: async.c async.h hiredis.h sds.h util.h dict.c dict.h example.o: example.c hiredis.h hiredis.o: hiredis.c hiredis.h net.h sds.h util.h sds.o: sds.c sds.h diff --git a/deps/hiredis/README.md b/deps/hiredis/README.md index e39ff0c1..5a77cd38 100644 --- a/deps/hiredis/README.md +++ b/deps/hiredis/README.md @@ -108,7 +108,7 @@ was received: * **`REDIS_REPLY_ARRAY`**: * A multi bulk reply. The number of elements in the multi bulk reply is stored in `reply->elements`. Every element in the multi bulk reply is a `redisReply` object as well - and can be accessed via `reply->elements[..index..]`. + and can be accessed via `reply->element[..index..]`. Redis may reply with nested arrays but this is fully supported. Replies should be freed using the `freeReplyObject()` function. @@ -171,7 +171,7 @@ the latter means an error occurred while reading a reply. Just as with the other the `err` field in the context can be used to find out what the cause of this error is. The following examples shows a simple pipeline (resulting in only a single call to `write(2)` and -a single call to `write(2)`): +a single call to `read(2)`): redisReply *reply; redisAppendCommand(context,"SET foo bar"); diff --git a/deps/hiredis/async.c b/deps/hiredis/async.c index 5c11243e..76c4cc3a 100644 --- a/deps/hiredis/async.c +++ b/deps/hiredis/async.c @@ -30,14 +30,58 @@ */ #include +#include #include +#include #include "async.h" +#include "dict.c" #include "sds.h" #include "util.h" /* Forward declaration of function in hiredis.c */ void __redisAppendCommand(redisContext *c, char *cmd, size_t len); +/* Functions managing dictionary of callbacks for pub/sub. */ +static unsigned int callbackHash(const void *key) { + return dictGenHashFunction((unsigned char*)key,sdslen((char*)key)); +} + +static void *callbackValDup(void *privdata, const void *src) { + ((void) privdata); + redisCallback *dup = malloc(sizeof(*dup)); + memcpy(dup,src,sizeof(*dup)); + return dup; +} + +static int callbackKeyCompare(void *privdata, const void *key1, const void *key2) { + int l1, l2; + ((void) privdata); + + l1 = sdslen((sds)key1); + l2 = sdslen((sds)key2); + if (l1 != l2) return 0; + return memcmp(key1,key2,l1) == 0; +} + +static void callbackKeyDestructor(void *privdata, void *key) { + ((void) privdata); + sdsfree((sds)key); +} + +static void callbackValDestructor(void *privdata, void *val) { + ((void) privdata); + free(val); +} + +static dictType callbackDict = { + callbackHash, + NULL, + callbackValDup, + callbackKeyCompare, + callbackKeyDestructor, + callbackValDestructor +}; + static redisAsyncContext *redisAsyncInitialize(redisContext *c) { redisAsyncContext *ac = realloc(c,sizeof(redisAsyncContext)); c = &(ac->c); @@ -50,19 +94,23 @@ static redisAsyncContext *redisAsyncInitialize(redisContext *c) { ac->err = 0; ac->errstr = NULL; ac->data = NULL; - ac->_adapter_data = NULL; - ac->evAddRead = NULL; - ac->evDelRead = NULL; - ac->evAddWrite = NULL; - ac->evDelWrite = NULL; - ac->evCleanup = NULL; + ac->ev.data = NULL; + ac->ev.addRead = NULL; + ac->ev.delRead = NULL; + ac->ev.addWrite = NULL; + ac->ev.delWrite = NULL; + ac->ev.cleanup = NULL; ac->onConnect = NULL; ac->onDisconnect = NULL; ac->replies.head = NULL; ac->replies.tail = NULL; + ac->sub.invalid.head = NULL; + ac->sub.invalid.tail = NULL; + ac->sub.channels = dictCreate(&callbackDict,NULL); + ac->sub.patterns = dictCreate(&callbackDict,NULL); return ac; } @@ -96,6 +144,11 @@ int redisAsyncSetReplyObjectFunctions(redisAsyncContext *ac, redisReplyObjectFun int redisAsyncSetConnectCallback(redisAsyncContext *ac, redisConnectCallback *fn) { if (ac->onConnect == NULL) { ac->onConnect = fn; + + /* The common way to detect an established connection is to wait for + * the first write event to be fired. This assumes the related event + * library functions are already set. */ + if (ac->ev.addWrite) ac->ev.addWrite(ac->ev.data); return REDIS_OK; } return REDIS_ERR; @@ -114,11 +167,11 @@ static int __redisPushCallback(redisCallbackList *list, redisCallback *source) { redisCallback *cb; /* Copy callback from stack to heap */ - cb = calloc(1,sizeof(*cb)); + cb = malloc(sizeof(*cb)); if (!cb) redisOOM(); if (source != NULL) { - cb->fn = source->fn; - cb->privdata = source->privdata; + memcpy(cb,source,sizeof(*cb)); + cb->next = NULL; } /* Store callback in list */ @@ -146,51 +199,150 @@ static int __redisShiftCallback(redisCallbackList *list, redisCallback *target) return REDIS_ERR; } -/* Tries to do a clean disconnect from Redis, meaning it stops new commands - * from being issued, but tries to flush the output buffer and execute - * callbacks for all remaining replies. - * - * This functions is generally called from within a callback, so the - * processCallbacks function will pick up the flag when there are no - * more replies. */ -void redisAsyncDisconnect(redisAsyncContext *ac) { +static void __redisRunCallback(redisAsyncContext *ac, redisCallback *cb, redisReply *reply) { redisContext *c = &(ac->c); - c->flags |= REDIS_DISCONNECTING; + if (cb->fn != NULL) { + c->flags |= REDIS_IN_CALLBACK; + cb->fn(ac,reply,cb->privdata); + c->flags &= ~REDIS_IN_CALLBACK; + } +} + +/* Helper function to free the context. */ +static void __redisAsyncFree(redisAsyncContext *ac) { + redisContext *c = &(ac->c); + redisCallback cb; + dictIterator *it; + dictEntry *de; + + /* Execute pending callbacks with NULL reply. */ + while (__redisShiftCallback(&ac->replies,&cb) == REDIS_OK) + __redisRunCallback(ac,&cb,NULL); + + /* Execute callbacks for invalid commands */ + while (__redisShiftCallback(&ac->sub.invalid,&cb) == REDIS_OK) + __redisRunCallback(ac,&cb,NULL); + + /* Run subscription callbacks callbacks with NULL reply */ + it = dictGetIterator(ac->sub.channels); + while ((de = dictNext(it)) != NULL) + __redisRunCallback(ac,dictGetEntryVal(de),NULL); + dictReleaseIterator(it); + dictRelease(ac->sub.channels); + + it = dictGetIterator(ac->sub.patterns); + while ((de = dictNext(it)) != NULL) + __redisRunCallback(ac,dictGetEntryVal(de),NULL); + dictReleaseIterator(it); + dictRelease(ac->sub.patterns); + + /* Signal event lib to clean up */ + if (ac->ev.cleanup) ac->ev.cleanup(ac->ev.data); + + /* Execute disconnect callback. When redisAsyncFree() initiated destroying + * this context, the status will always be REDIS_OK. */ + if (ac->onDisconnect && (c->flags & REDIS_CONNECTED)) { + if (c->flags & REDIS_FREEING) { + ac->onDisconnect(ac,REDIS_OK); + } else { + ac->onDisconnect(ac,(ac->err == 0) ? REDIS_OK : REDIS_ERR); + } + } + + /* Cleanup self */ + redisFree(c); +} + +/* Free the async context. When this function is called from a callback, + * control needs to be returned to redisProcessCallbacks() before actual + * free'ing. To do so, a flag is set on the context which is picked up by + * redisProcessCallbacks(). Otherwise, the context is immediately free'd. */ +void redisAsyncFree(redisAsyncContext *ac) { + redisContext *c = &(ac->c); + c->flags |= REDIS_FREEING; + if (!(c->flags & REDIS_IN_CALLBACK)) + __redisAsyncFree(ac); } /* Helper function to make the disconnect happen and clean up. */ static void __redisAsyncDisconnect(redisAsyncContext *ac) { redisContext *c = &(ac->c); - redisCallback cb; - int status; /* Make sure error is accessible if there is any */ __redisAsyncCopyError(ac); - status = (ac->err == 0) ? REDIS_OK : REDIS_ERR; - if (status == REDIS_OK) { - /* When the connection is cleanly disconnected, there should not - * be pending callbacks. */ + if (ac->err == 0) { + /* For clean disconnects, there should be no pending callbacks. */ assert(__redisShiftCallback(&ac->replies,NULL) == REDIS_ERR); } else { - /* Callbacks should not be able to issue new commands. */ + /* Disconnection is caused by an error, make sure that pending + * callbacks cannot call new commands. */ c->flags |= REDIS_DISCONNECTING; - - /* Execute pending callbacks with NULL reply. */ - while (__redisShiftCallback(&ac->replies,&cb) == REDIS_OK) { - if (cb.fn != NULL) - cb.fn(ac,NULL,cb.privdata); - } } - /* Signal event lib to clean up */ - if (ac->evCleanup) ac->evCleanup(ac->_adapter_data); + /* For non-clean disconnects, __redisAsyncFree() will execute pending + * callbacks with a NULL-reply. */ + __redisAsyncFree(ac); +} - /* Execute callback with proper status */ - if (ac->onDisconnect) ac->onDisconnect(ac,status); +/* Tries to do a clean disconnect from Redis, meaning it stops new commands + * from being issued, but tries to flush the output buffer and execute + * callbacks for all remaining replies. When this function is called from a + * callback, there might be more replies and we can safely defer disconnecting + * to redisProcessCallbacks(). Otherwise, we can only disconnect immediately + * when there are no pending callbacks. */ +void redisAsyncDisconnect(redisAsyncContext *ac) { + redisContext *c = &(ac->c); + c->flags |= REDIS_DISCONNECTING; + if (!(c->flags & REDIS_IN_CALLBACK) && ac->replies.head == NULL) + __redisAsyncDisconnect(ac); +} - /* Cleanup self */ - redisFree(c); +static int __redisGetSubscribeCallback(redisAsyncContext *ac, redisReply *reply, redisCallback *dstcb) { + redisContext *c = &(ac->c); + dict *callbacks; + dictEntry *de; + int pvariant; + char *stype; + sds sname; + + /* Custom reply functions are not supported for pub/sub. This will fail + * very hard when they are used... */ + if (reply->type == REDIS_REPLY_ARRAY) { + assert(reply->elements >= 2); + assert(reply->element[0]->type == REDIS_REPLY_STRING); + stype = reply->element[0]->str; + pvariant = (tolower(stype[0]) == 'p') ? 1 : 0; + + if (pvariant) + callbacks = ac->sub.patterns; + else + callbacks = ac->sub.channels; + + /* Locate the right callback */ + assert(reply->element[1]->type == REDIS_REPLY_STRING); + sname = sdsnewlen(reply->element[1]->str,reply->element[1]->len); + de = dictFind(callbacks,sname); + if (de != NULL) { + memcpy(dstcb,dictGetEntryVal(de),sizeof(*dstcb)); + + /* If this is an unsubscribe message, remove it. */ + if (strcasecmp(stype+pvariant,"unsubscribe") == 0) { + dictDelete(callbacks,sname); + + /* If this was the last unsubscribe message, revert to + * non-subscribe mode. */ + assert(reply->element[2]->type == REDIS_REPLY_INTEGER); + if (reply->element[2]->integer == 0) + c->flags &= ~REDIS_SUBSCRIBED; + } + } + sdsfree(sname); + } else { + /* Shift callback for invalid commands. */ + __redisShiftCallback(&ac->sub.invalid,dstcb); + } + return REDIS_OK; } void redisProcessCallbacks(redisAsyncContext *ac) { @@ -213,11 +365,28 @@ void redisProcessCallbacks(redisAsyncContext *ac) { break; } - /* Shift callback and execute it */ - assert(__redisShiftCallback(&ac->replies,&cb) == REDIS_OK); + /* Even if the context is subscribed, pending regular callbacks will + * get a reply before pub/sub messages arrive. */ + if (__redisShiftCallback(&ac->replies,&cb) != REDIS_OK) { + /* No more regular callbacks, the context *must* be subscribed. */ + assert(c->flags & REDIS_SUBSCRIBED); + __redisGetSubscribeCallback(ac,reply,&cb); + } + if (cb.fn != NULL) { - cb.fn(ac,reply,cb.privdata); + __redisRunCallback(ac,&cb,reply); + c->fn->freeObject(reply); + + /* Proceed with free'ing when redisAsyncFree() was called. */ + if (c->flags & REDIS_FREEING) { + __redisAsyncFree(ac); + return; + } } else { + /* No callback for this reply. This can either be a NULL callback, + * or there were no callbacks to begin with. Either way, don't + * abort with an error, but simply ignore it because the client + * doesn't know what the server will spit out over the wire. */ c->fn->freeObject(reply); } } @@ -237,7 +406,7 @@ void redisAsyncHandleRead(redisAsyncContext *ac) { __redisAsyncDisconnect(ac); } else { /* Always re-schedule reads */ - if (ac->evAddRead) ac->evAddRead(ac->_adapter_data); + if (ac->ev.addRead) ac->ev.addRead(ac->ev.data); redisProcessCallbacks(ac); } } @@ -251,13 +420,13 @@ void redisAsyncHandleWrite(redisAsyncContext *ac) { } else { /* Continue writing when not done, stop writing otherwise */ if (!done) { - if (ac->evAddWrite) ac->evAddWrite(ac->_adapter_data); + if (ac->ev.addWrite) ac->ev.addWrite(ac->ev.data); } else { - if (ac->evDelWrite) ac->evDelWrite(ac->_adapter_data); + if (ac->ev.delWrite) ac->ev.delWrite(ac->ev.data); } /* Always schedule reads after writes */ - if (ac->evAddRead) ac->evAddRead(ac->_adapter_data); + if (ac->ev.addRead) ac->ev.addRead(ac->ev.data); /* Fire onConnect when this is the first write event. */ if (!(c->flags & REDIS_CONNECTED)) { @@ -267,26 +436,81 @@ void redisAsyncHandleWrite(redisAsyncContext *ac) { } } -/* Helper function for the redisAsyncCommand* family of functions. - * - * Write a formatted command to the output buffer and register the provided - * callback function with the context. - */ +/* Sets a pointer to the first argument and its length starting at p. Returns + * the number of bytes to skip to get to the following argument. */ +static char *nextArgument(char *start, char **str, size_t *len) { + char *p = start; + if (p[0] != '$') { + p = strchr(p,'$'); + if (p == NULL) return NULL; + } + + *len = (int)strtol(p+1,NULL,10); + p = strchr(p,'\r'); + assert(p); + *str = p+2; + return p+2+(*len)+2; +} + +/* Helper function for the redisAsyncCommand* family of functions. Writes a + * formatted command to the output buffer and registers the provided callback + * function with the context. */ static int __redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, char *cmd, size_t len) { redisContext *c = &(ac->c); redisCallback cb; + int pvariant, hasnext; + char *cstr, *astr; + size_t clen, alen; + char *p; + sds sname; - /* Don't accept new commands when the connection is lazily closed. */ - if (c->flags & REDIS_DISCONNECTING) return REDIS_ERR; - __redisAppendCommand(c,cmd,len); + /* Don't accept new commands when the connection is about to be closed. */ + if (c->flags & (REDIS_DISCONNECTING | REDIS_FREEING)) return REDIS_ERR; - /* Store callback */ + /* Setup callback */ cb.fn = fn; cb.privdata = privdata; - __redisPushCallback(&ac->replies,&cb); + + /* Find out which command will be appended. */ + p = nextArgument(cmd,&cstr,&clen); + assert(p != NULL); + hasnext = (p[0] == '$'); + pvariant = (tolower(cstr[0]) == 'p') ? 1 : 0; + cstr += pvariant; + clen -= pvariant; + + if (hasnext && strncasecmp(cstr,"subscribe\r\n",11) == 0) { + c->flags |= REDIS_SUBSCRIBED; + + /* Add every channel/pattern to the list of subscription callbacks. */ + while ((p = nextArgument(p,&astr,&alen)) != NULL) { + sname = sdsnewlen(astr,alen); + if (pvariant) + dictReplace(ac->sub.patterns,sname,&cb); + else + dictReplace(ac->sub.channels,sname,&cb); + } + } else if (strncasecmp(cstr,"unsubscribe\r\n",13) == 0) { + /* It is only useful to call (P)UNSUBSCRIBE when the context is + * subscribed to one or more channels or patterns. */ + if (!(c->flags & REDIS_SUBSCRIBED)) return REDIS_ERR; + + /* (P)UNSUBSCRIBE does not have its own response: every channel or + * pattern that is unsubscribed will receive a message. This means we + * should not append a callback function for this command. */ + } else { + if (c->flags & REDIS_SUBSCRIBED) + /* This will likely result in an error reply, but it needs to be + * received and passed to the callback. */ + __redisPushCallback(&ac->sub.invalid,&cb); + else + __redisPushCallback(&ac->replies,&cb); + } + + __redisAppendCommand(c,cmd,len); /* Always schedule a write when the write buffer is non-empty */ - if (ac->evAddWrite) ac->evAddWrite(ac->_adapter_data); + if (ac->ev.addWrite) ac->ev.addWrite(ac->ev.data); return REDIS_OK; } diff --git a/deps/hiredis/async.h b/deps/hiredis/async.h index 2ef0e21e..ba2b6f54 100644 --- a/deps/hiredis/async.h +++ b/deps/hiredis/async.h @@ -1,5 +1,7 @@ /* * Copyright (c) 2009-2010, Salvatore Sanfilippo + * Copyright (c) 2010, Pieter Noordhuis + * * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -36,6 +38,7 @@ extern "C" { #endif struct redisAsyncContext; /* need forward declaration of redisAsyncContext */ +struct dict; /* dictionary header is included in async.c */ /* Reply callback prototype and container */ typedef void (redisCallbackFn)(struct redisAsyncContext*, void*, void*); @@ -66,16 +69,18 @@ typedef struct redisAsyncContext { /* Not used by hiredis */ void *data; - /* Used by the different event lib adapters to store their private data */ - void *_adapter_data; + /* Event library data and hooks */ + struct { + void *data; - /* Called when the library expects to start reading/writing. - * The supplied functions should be idempotent. */ - void (*evAddRead)(void *privdata); - void (*evDelRead)(void *privdata); - void (*evAddWrite)(void *privdata); - void (*evDelWrite)(void *privdata); - void (*evCleanup)(void *privdata); + /* Hooks that are called when the library expects to start + * reading/writing. These functions should be idempotent. */ + void (*addRead)(void *privdata); + void (*delRead)(void *privdata); + void (*addWrite)(void *privdata); + void (*delWrite)(void *privdata); + void (*cleanup)(void *privdata); + } ev; /* Called when either the connection is terminated due to an error or per * user request. The status is set accordingly (REDIS_OK, REDIS_ERR). */ @@ -84,16 +89,25 @@ typedef struct redisAsyncContext { /* Called when the first write event was received. */ redisConnectCallback *onConnect; - /* Reply callbacks */ + /* Regular command callbacks */ redisCallbackList replies; + + /* Subscription callbacks */ + struct { + redisCallbackList invalid; + struct dict *channels; + struct dict *patterns; + } sub; } redisAsyncContext; /* Functions that proxy to hiredis */ redisAsyncContext *redisAsyncConnect(const char *ip, int port); +redisAsyncContext *redisAsyncConnectUnix(const char *path); int redisAsyncSetReplyObjectFunctions(redisAsyncContext *ac, redisReplyObjectFunctions *fn); int redisAsyncSetConnectCallback(redisAsyncContext *ac, redisConnectCallback *fn); int redisAsyncSetDisconnectCallback(redisAsyncContext *ac, redisDisconnectCallback *fn); void redisAsyncDisconnect(redisAsyncContext *ac); +void redisAsyncFree(redisAsyncContext *ac); /* Handle read/write events */ void redisAsyncHandleRead(redisAsyncContext *ac); diff --git a/deps/hiredis/dict.c b/deps/hiredis/dict.c new file mode 100644 index 00000000..79b1041c --- /dev/null +++ b/deps/hiredis/dict.c @@ -0,0 +1,338 @@ +/* Hash table implementation. + * + * This file implements in memory hash tables with insert/del/replace/find/ + * get-random-element operations. Hash tables will auto resize if needed + * tables of power of two in size are used, collisions are handled by + * chaining. See the source code for more information... :) + * + * Copyright (c) 2006-2010, Salvatore Sanfilippo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "fmacros.h" +#include +#include +#include +#include "dict.h" + +/* -------------------------- private prototypes ---------------------------- */ + +static int _dictExpandIfNeeded(dict *ht); +static unsigned long _dictNextPower(unsigned long size); +static int _dictKeyIndex(dict *ht, const void *key); +static int _dictInit(dict *ht, dictType *type, void *privDataPtr); + +/* -------------------------- hash functions -------------------------------- */ + +/* Generic hash function (a popular one from Bernstein). + * I tested a few and this was the best. */ +static unsigned int dictGenHashFunction(const unsigned char *buf, int len) { + unsigned int hash = 5381; + + while (len--) + hash = ((hash << 5) + hash) + (*buf++); /* hash * 33 + c */ + return hash; +} + +/* ----------------------------- API implementation ------------------------- */ + +/* Reset an hashtable already initialized with ht_init(). + * NOTE: This function should only called by ht_destroy(). */ +static void _dictReset(dict *ht) { + ht->table = NULL; + ht->size = 0; + ht->sizemask = 0; + ht->used = 0; +} + +/* Create a new hash table */ +static dict *dictCreate(dictType *type, void *privDataPtr) { + dict *ht = malloc(sizeof(*ht)); + _dictInit(ht,type,privDataPtr); + return ht; +} + +/* Initialize the hash table */ +static int _dictInit(dict *ht, dictType *type, void *privDataPtr) { + _dictReset(ht); + ht->type = type; + ht->privdata = privDataPtr; + return DICT_OK; +} + +/* Expand or create the hashtable */ +static int dictExpand(dict *ht, unsigned long size) { + dict n; /* the new hashtable */ + unsigned long realsize = _dictNextPower(size), i; + + /* the size is invalid if it is smaller than the number of + * elements already inside the hashtable */ + if (ht->used > size) + return DICT_ERR; + + _dictInit(&n, ht->type, ht->privdata); + n.size = realsize; + n.sizemask = realsize-1; + n.table = calloc(realsize,sizeof(dictEntry*)); + + /* Copy all the elements from the old to the new table: + * note that if the old hash table is empty ht->size is zero, + * so dictExpand just creates an hash table. */ + n.used = ht->used; + for (i = 0; i < ht->size && ht->used > 0; i++) { + dictEntry *he, *nextHe; + + if (ht->table[i] == NULL) continue; + + /* For each hash entry on this slot... */ + he = ht->table[i]; + while(he) { + unsigned int h; + + nextHe = he->next; + /* Get the new element index */ + h = dictHashKey(ht, he->key) & n.sizemask; + he->next = n.table[h]; + n.table[h] = he; + ht->used--; + /* Pass to the next element */ + he = nextHe; + } + } + assert(ht->used == 0); + free(ht->table); + + /* Remap the new hashtable in the old */ + *ht = n; + return DICT_OK; +} + +/* Add an element to the target hash table */ +static int dictAdd(dict *ht, void *key, void *val) { + int index; + dictEntry *entry; + + /* Get the index of the new element, or -1 if + * the element already exists. */ + if ((index = _dictKeyIndex(ht, key)) == -1) + return DICT_ERR; + + /* Allocates the memory and stores key */ + entry = malloc(sizeof(*entry)); + entry->next = ht->table[index]; + ht->table[index] = entry; + + /* Set the hash entry fields. */ + dictSetHashKey(ht, entry, key); + dictSetHashVal(ht, entry, val); + ht->used++; + return DICT_OK; +} + +/* Add an element, discarding the old if the key already exists. + * Return 1 if the key was added from scratch, 0 if there was already an + * element with such key and dictReplace() just performed a value update + * operation. */ +static int dictReplace(dict *ht, void *key, void *val) { + dictEntry *entry, auxentry; + + /* Try to add the element. If the key + * does not exists dictAdd will suceed. */ + if (dictAdd(ht, key, val) == DICT_OK) + return 1; + /* It already exists, get the entry */ + entry = dictFind(ht, key); + /* Free the old value and set the new one */ + /* Set the new value and free the old one. Note that it is important + * to do that in this order, as the value may just be exactly the same + * as the previous one. In this context, think to reference counting, + * you want to increment (set), and then decrement (free), and not the + * reverse. */ + auxentry = *entry; + dictSetHashVal(ht, entry, val); + dictFreeEntryVal(ht, &auxentry); + return 0; +} + +/* Search and remove an element */ +static int dictDelete(dict *ht, const void *key) { + unsigned int h; + dictEntry *de, *prevde; + + if (ht->size == 0) + return DICT_ERR; + h = dictHashKey(ht, key) & ht->sizemask; + de = ht->table[h]; + + prevde = NULL; + while(de) { + if (dictCompareHashKeys(ht,key,de->key)) { + /* Unlink the element from the list */ + if (prevde) + prevde->next = de->next; + else + ht->table[h] = de->next; + + dictFreeEntryKey(ht,de); + dictFreeEntryVal(ht,de); + free(de); + ht->used--; + return DICT_OK; + } + prevde = de; + de = de->next; + } + return DICT_ERR; /* not found */ +} + +/* Destroy an entire hash table */ +static int _dictClear(dict *ht) { + unsigned long i; + + /* Free all the elements */ + for (i = 0; i < ht->size && ht->used > 0; i++) { + dictEntry *he, *nextHe; + + if ((he = ht->table[i]) == NULL) continue; + while(he) { + nextHe = he->next; + dictFreeEntryKey(ht, he); + dictFreeEntryVal(ht, he); + free(he); + ht->used--; + he = nextHe; + } + } + /* Free the table and the allocated cache structure */ + free(ht->table); + /* Re-initialize the table */ + _dictReset(ht); + return DICT_OK; /* never fails */ +} + +/* Clear & Release the hash table */ +static void dictRelease(dict *ht) { + _dictClear(ht); + free(ht); +} + +static dictEntry *dictFind(dict *ht, const void *key) { + dictEntry *he; + unsigned int h; + + if (ht->size == 0) return NULL; + h = dictHashKey(ht, key) & ht->sizemask; + he = ht->table[h]; + while(he) { + if (dictCompareHashKeys(ht, key, he->key)) + return he; + he = he->next; + } + return NULL; +} + +static dictIterator *dictGetIterator(dict *ht) { + dictIterator *iter = malloc(sizeof(*iter)); + + iter->ht = ht; + iter->index = -1; + iter->entry = NULL; + iter->nextEntry = NULL; + return iter; +} + +static dictEntry *dictNext(dictIterator *iter) { + while (1) { + if (iter->entry == NULL) { + iter->index++; + if (iter->index >= + (signed)iter->ht->size) break; + iter->entry = iter->ht->table[iter->index]; + } else { + iter->entry = iter->nextEntry; + } + if (iter->entry) { + /* We need to save the 'next' here, the iterator user + * may delete the entry we are returning. */ + iter->nextEntry = iter->entry->next; + return iter->entry; + } + } + return NULL; +} + +static void dictReleaseIterator(dictIterator *iter) { + free(iter); +} + +/* ------------------------- private functions ------------------------------ */ + +/* Expand the hash table if needed */ +static int _dictExpandIfNeeded(dict *ht) { + /* If the hash table is empty expand it to the intial size, + * if the table is "full" dobule its size. */ + if (ht->size == 0) + return dictExpand(ht, DICT_HT_INITIAL_SIZE); + if (ht->used == ht->size) + return dictExpand(ht, ht->size*2); + return DICT_OK; +} + +/* Our hash table capability is a power of two */ +static unsigned long _dictNextPower(unsigned long size) { + unsigned long i = DICT_HT_INITIAL_SIZE; + + if (size >= LONG_MAX) return LONG_MAX; + while(1) { + if (i >= size) + return i; + i *= 2; + } +} + +/* Returns the index of a free slot that can be populated with + * an hash entry for the given 'key'. + * If the key already exists, -1 is returned. */ +static int _dictKeyIndex(dict *ht, const void *key) { + unsigned int h; + dictEntry *he; + + /* Expand the hashtable if needed */ + if (_dictExpandIfNeeded(ht) == DICT_ERR) + return -1; + /* Compute the key hash value */ + h = dictHashKey(ht, key) & ht->sizemask; + /* Search if this slot does not already contain the given key */ + he = ht->table[h]; + while(he) { + if (dictCompareHashKeys(ht, key, he->key)) + return -1; + he = he->next; + } + return h; +} + diff --git a/deps/hiredis/dict.h b/deps/hiredis/dict.h new file mode 100644 index 00000000..95fcd280 --- /dev/null +++ b/deps/hiredis/dict.h @@ -0,0 +1,126 @@ +/* Hash table implementation. + * + * This file implements in memory hash tables with insert/del/replace/find/ + * get-random-element operations. Hash tables will auto resize if needed + * tables of power of two in size are used, collisions are handled by + * chaining. See the source code for more information... :) + * + * Copyright (c) 2006-2010, Salvatore Sanfilippo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __DICT_H +#define __DICT_H + +#define DICT_OK 0 +#define DICT_ERR 1 + +/* Unused arguments generate annoying warnings... */ +#define DICT_NOTUSED(V) ((void) V) + +typedef struct dictEntry { + void *key; + void *val; + struct dictEntry *next; +} dictEntry; + +typedef struct dictType { + unsigned int (*hashFunction)(const void *key); + void *(*keyDup)(void *privdata, const void *key); + void *(*valDup)(void *privdata, const void *obj); + int (*keyCompare)(void *privdata, const void *key1, const void *key2); + void (*keyDestructor)(void *privdata, void *key); + void (*valDestructor)(void *privdata, void *obj); +} dictType; + +typedef struct dict { + dictEntry **table; + dictType *type; + unsigned long size; + unsigned long sizemask; + unsigned long used; + void *privdata; +} dict; + +typedef struct dictIterator { + dict *ht; + int index; + dictEntry *entry, *nextEntry; +} dictIterator; + +/* This is the initial size of every hash table */ +#define DICT_HT_INITIAL_SIZE 4 + +/* ------------------------------- Macros ------------------------------------*/ +#define dictFreeEntryVal(ht, entry) \ + if ((ht)->type->valDestructor) \ + (ht)->type->valDestructor((ht)->privdata, (entry)->val) + +#define dictSetHashVal(ht, entry, _val_) do { \ + if ((ht)->type->valDup) \ + entry->val = (ht)->type->valDup((ht)->privdata, _val_); \ + else \ + entry->val = (_val_); \ +} while(0) + +#define dictFreeEntryKey(ht, entry) \ + if ((ht)->type->keyDestructor) \ + (ht)->type->keyDestructor((ht)->privdata, (entry)->key) + +#define dictSetHashKey(ht, entry, _key_) do { \ + if ((ht)->type->keyDup) \ + entry->key = (ht)->type->keyDup((ht)->privdata, _key_); \ + else \ + entry->key = (_key_); \ +} while(0) + +#define dictCompareHashKeys(ht, key1, key2) \ + (((ht)->type->keyCompare) ? \ + (ht)->type->keyCompare((ht)->privdata, key1, key2) : \ + (key1) == (key2)) + +#define dictHashKey(ht, key) (ht)->type->hashFunction(key) + +#define dictGetEntryKey(he) ((he)->key) +#define dictGetEntryVal(he) ((he)->val) +#define dictSlots(ht) ((ht)->size) +#define dictSize(ht) ((ht)->used) + +/* API */ +static unsigned int dictGenHashFunction(const unsigned char *buf, int len); +static dict *dictCreate(dictType *type, void *privDataPtr); +static int dictExpand(dict *ht, unsigned long size); +static int dictAdd(dict *ht, void *key, void *val); +static int dictReplace(dict *ht, void *key, void *val); +static int dictDelete(dict *ht, const void *key); +static void dictRelease(dict *ht); +static dictEntry * dictFind(dict *ht, const void *key); +static dictIterator *dictGetIterator(dict *ht); +static dictEntry *dictNext(dictIterator *iter); +static void dictReleaseIterator(dictIterator *iter); + +#endif /* __DICT_H */ diff --git a/deps/hiredis/example.c b/deps/hiredis/example.c index 2506f39c..90ff9ed5 100644 --- a/deps/hiredis/example.c +++ b/deps/hiredis/example.c @@ -9,7 +9,8 @@ int main(void) { redisContext *c; redisReply *reply; - c = redisConnect((char*)"127.0.0.1", 6379); + struct timeval timeout = { 1, 500000 }; // 1.5 seconds + c = redisConnectWithTimeout((char*)"127.0.0.2", 6379, timeout); if (c->err) { printf("Connection error: %s\n", c->errstr); exit(1); diff --git a/deps/hiredis/fmacros.h b/deps/hiredis/fmacros.h index 38f46482..65f9692c 100644 --- a/deps/hiredis/fmacros.h +++ b/deps/hiredis/fmacros.h @@ -1,7 +1,9 @@ -#ifndef _REDIS_FMACRO_H -#define _REDIS_FMACRO_H +#ifndef __HIREDIS_FMACRO_H +#define __HIREDIS_FMACRO_H +#ifndef _BSD_SOURCE #define _BSD_SOURCE +#endif #ifdef __linux__ #define _XOPEN_SOURCE 700 @@ -9,7 +11,4 @@ #define _XOPEN_SOURCE #endif -#define _LARGEFILE_SOURCE -#define _FILE_OFFSET_BITS 64 - #endif diff --git a/deps/hiredis/hiredis.c b/deps/hiredis/hiredis.c index d4cad7c2..f2135bac 100644 --- a/deps/hiredis/hiredis.c +++ b/deps/hiredis/hiredis.c @@ -271,14 +271,17 @@ static int processLineItem(redisReader *r) { int len; if ((p = readLine(r,&len)) != NULL) { - if (r->fn) { - if (cur->type == REDIS_REPLY_INTEGER) { + if (cur->type == REDIS_REPLY_INTEGER) { + if (r->fn && r->fn->createInteger) obj = r->fn->createInteger(cur,readLongLong(p)); - } else { - obj = r->fn->createString(cur,p,len); - } + else + obj = (void*)REDIS_REPLY_INTEGER; } else { - obj = (void*)(size_t)(cur->type); + /* Type will be error or status. */ + if (r->fn && r->fn->createString) + obj = r->fn->createString(cur,p,len); + else + obj = (void*)(size_t)(cur->type); } /* Set reply if this is the root object. */ @@ -306,15 +309,19 @@ static int processBulkItem(redisReader *r) { if (len < 0) { /* The nil object can always be created. */ - obj = r->fn ? r->fn->createNil(cur) : - (void*)REDIS_REPLY_NIL; + if (r->fn && r->fn->createNil) + obj = r->fn->createNil(cur); + else + obj = (void*)REDIS_REPLY_NIL; success = 1; } else { /* Only continue when the buffer contains the entire bulk item. */ bytelen += len+2; /* include \r\n */ if (r->pos+bytelen <= r->len) { - obj = r->fn ? r->fn->createString(cur,s+2,len) : - (void*)REDIS_REPLY_STRING; + if (r->fn && r->fn->createString) + obj = r->fn->createString(cur,s+2,len); + else + obj = (void*)REDIS_REPLY_STRING; success = 1; } } @@ -351,12 +358,16 @@ static int processMultiBulkItem(redisReader *r) { root = (r->ridx == 0); if (elements == -1) { - obj = r->fn ? r->fn->createNil(cur) : - (void*)REDIS_REPLY_NIL; + if (r->fn && r->fn->createNil) + obj = r->fn->createNil(cur); + else + obj = (void*)REDIS_REPLY_NIL; moveToNextTask(r); } else { - obj = r->fn ? r->fn->createArray(cur,elements) : - (void*)REDIS_REPLY_ARRAY; + if (r->fn && r->fn->createArray) + obj = r->fn->createArray(cur,elements); + else + obj = (void*)REDIS_REPLY_ARRAY; /* Modify task stack when there are more than 0 elements. */ if (elements > 0) { @@ -434,7 +445,7 @@ static int processItem(redisReader *r) { } } -void *redisReplyReaderCreate() { +void *redisReplyReaderCreate(void) { redisReader *r = calloc(sizeof(redisReader),1); r->error = NULL; r->fn = &defaultFunctions; @@ -493,7 +504,7 @@ static void redisSetReplyReaderError(redisReader *r, sds err) { if (r->buf != NULL) { sdsfree(r->buf); r->buf = sdsempty(); - r->pos = 0; + r->pos = r->len = 0; } r->ridx = -1; r->error = err; @@ -504,11 +515,18 @@ char *redisReplyReaderGetError(void *reader) { return r->error; } -void redisReplyReaderFeed(void *reader, char *buf, size_t len) { +void redisReplyReaderFeed(void *reader, const char *buf, size_t len) { redisReader *r = reader; /* Copy the provided buffer. */ if (buf != NULL && len >= 1) { + /* Destroy internal buffer when it is empty and is quite large. */ + if (r->len == 0 && sdsavail(r->buf) > 16*1024) { + sdsfree(r->buf); + r->buf = sdsempty(); + r->pos = 0; + } + r->buf = sdscatlen(r->buf,buf,len); r->len = sdslen(r->buf); } @@ -538,15 +556,10 @@ int redisReplyReaderGetReply(void *reader, void **reply) { if (processItem(r) < 0) break; - /* Discard the consumed part of the buffer. */ - if (r->pos > 0) { - if (r->pos == r->len) { - /* sdsrange has a quirck on this edge case. */ - sdsfree(r->buf); - r->buf = sdsempty(); - } else { - r->buf = sdsrange(r->buf,r->pos,r->len); - } + /* Discard part of the buffer when we've consumed at least 1k, to avoid + * doing unnecessary calls to memmove() in sds.c. */ + if (r->pos >= 1024) { + r->buf = sdsrange(r->buf,r->pos,-1); r->pos = 0; r->len = sdslen(r->buf); } @@ -556,13 +569,6 @@ int redisReplyReaderGetReply(void *reader, void **reply) { void *aux = r->reply; r->reply = NULL; - /* Destroy the buffer when it is empty and is quite large. */ - if (r->len == 0 && sdsavail(r->buf) > 16*1024) { - sdsfree(r->buf); - r->buf = sdsempty(); - r->pos = 0; - } - /* Check if there actually *is* a reply. */ if (r->error != NULL) { return REDIS_ERR; @@ -601,7 +607,7 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) { char *cmd = NULL; /* final command */ int pos; /* position in final command */ sds current; /* current argument */ - int interpolated = 0; /* did we do interpolation on an argument? */ + int touched = 0; /* was the current argument touched? */ char **argv = NULL; int argc = 0, j; int totlen = 0; @@ -615,13 +621,14 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) { while(*c != '\0') { if (*c != '%' || c[1] == '\0') { if (*c == ' ') { - if (sdslen(current) != 0) { + if (touched) { addArgument(current, &argv, &argc, &totlen); current = sdsempty(); - interpolated = 0; + touched = 0; } } else { current = sdscatlen(current,c,1); + touched = 1; } } else { switch(c[1]) { @@ -630,14 +637,12 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) { size = strlen(arg); if (size > 0) current = sdscatlen(current,arg,size); - interpolated = 1; break; case 'b': arg = va_arg(ap,char*); size = va_arg(ap,size_t); if (size > 0) current = sdscatlen(current,arg,size); - interpolated = 1; break; case '%': current = sdscat(current,"%"); @@ -683,7 +688,6 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) { _format[_l] = '\0'; va_copy(_cpy,ap); current = sdscatvprintf(current,_format,_cpy); - interpolated = 1; va_end(_cpy); /* Update current position (note: outer blocks @@ -696,13 +700,14 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) { va_arg(ap,void); } } + touched = 1; c++; } c++; } /* Add the last argument if needed */ - if (interpolated || sdslen(current) != 0) { + if (touched) { addArgument(current, &argv, &argc, &totlen); } else { sdsfree(current); @@ -798,7 +803,7 @@ void __redisSetError(redisContext *c, int type, const sds errstr) { } } -static redisContext *redisContextInit() { +static redisContext *redisContextInit(void) { redisContext *c = calloc(sizeof(redisContext),1); c->err = 0; c->errstr = NULL; @@ -809,8 +814,7 @@ static redisContext *redisContextInit() { } void redisFree(redisContext *c) { - /* Disconnect before free'ing if not yet disconnected. */ - if (c->flags & REDIS_CONNECTED) + if (c->fd > 0) close(c->fd); if (c->errstr != NULL) sdsfree(c->errstr); @@ -827,31 +831,52 @@ void redisFree(redisContext *c) { redisContext *redisConnect(const char *ip, int port) { redisContext *c = redisContextInit(); c->flags |= REDIS_BLOCK; - redisContextConnectTcp(c,ip,port); + redisContextConnectTcp(c,ip,port,NULL); + return c; +} + +redisContext *redisConnectWithTimeout(const char *ip, int port, struct timeval tv) { + redisContext *c = redisContextInit(); + c->flags |= REDIS_BLOCK; + redisContextConnectTcp(c,ip,port,&tv); return c; } redisContext *redisConnectNonBlock(const char *ip, int port) { redisContext *c = redisContextInit(); c->flags &= ~REDIS_BLOCK; - redisContextConnectTcp(c,ip,port); + redisContextConnectTcp(c,ip,port,NULL); return c; } redisContext *redisConnectUnix(const char *path) { redisContext *c = redisContextInit(); c->flags |= REDIS_BLOCK; - redisContextConnectUnix(c,path); + redisContextConnectUnix(c,path,NULL); + return c; +} + +redisContext *redisConnectUnixWithTimeout(const char *path, struct timeval tv) { + redisContext *c = redisContextInit(); + c->flags |= REDIS_BLOCK; + redisContextConnectUnix(c,path,&tv); return c; } redisContext *redisConnectUnixNonBlock(const char *path) { redisContext *c = redisContextInit(); c->flags &= ~REDIS_BLOCK; - redisContextConnectUnix(c,path); + redisContextConnectUnix(c,path,NULL); return c; } +/* Set read/write timeout on a blocking socket. */ +int redisSetTimeout(redisContext *c, struct timeval tv) { + if (c->flags & REDIS_BLOCK) + return redisContextSetTimeout(c,tv); + return REDIS_ERR; +} + /* Set the replyObjectFunctions to use. Returns REDIS_ERR when the reader * was already initialized and the function set could not be re-set. * Return REDIS_OK when they could be set. */ @@ -879,7 +904,7 @@ int redisBufferRead(redisContext *c) { char buf[2048]; int nread = read(c->fd,buf,sizeof(buf)); if (nread == -1) { - if (errno == EAGAIN) { + if (errno == EAGAIN && !(c->flags & REDIS_BLOCK)) { /* Try again later */ } else { __redisSetError(c,REDIS_ERR_IO,NULL); @@ -910,7 +935,7 @@ int redisBufferWrite(redisContext *c, int *done) { if (sdslen(c->obuf) > 0) { nwritten = write(c->fd,c->obuf,sdslen(c->obuf)); if (nwritten == -1) { - if (errno == EAGAIN) { + if (errno == EAGAIN && !(c->flags & REDIS_BLOCK)) { /* Try again later */ } else { __redisSetError(c,REDIS_ERR_IO,NULL); diff --git a/deps/hiredis/hiredis.h b/deps/hiredis/hiredis.h index 1412a344..f4452091 100644 --- a/deps/hiredis/hiredis.h +++ b/deps/hiredis/hiredis.h @@ -33,6 +33,7 @@ #define __HIREDIS_H #include /* for size_t */ #include /* for va_list */ +#include /* for struct timeval */ #define HIREDIS_MAJOR 0 #define HIREDIS_MINOR 9 @@ -64,6 +65,16 @@ * should be terminated once all replies have been read. */ #define REDIS_DISCONNECTING 0x4 +/* Flag specific to the async API which means that the context should be clean + * up as soon as possible. */ +#define REDIS_FREEING 0x8 + +/* Flag that is set when an async callback is executed. */ +#define REDIS_IN_CALLBACK 0x10 + +/* Flag that is set when the async context has one or more subscriptions. */ +#define REDIS_SUBSCRIBED 0x20 + #define REDIS_REPLY_STRING 1 #define REDIS_REPLY_ARRAY 2 #define REDIS_REPLY_INTEGER 3 @@ -118,13 +129,13 @@ typedef struct redisContext { } redisContext; void freeReplyObject(void *reply); -void *redisReplyReaderCreate(); +void *redisReplyReaderCreate(void); int redisReplyReaderSetReplyObjectFunctions(void *reader, redisReplyObjectFunctions *fn); int redisReplyReaderSetPrivdata(void *reader, void *privdata); void *redisReplyReaderGetObject(void *reader); char *redisReplyReaderGetError(void *reader); void redisReplyReaderFree(void *ptr); -void redisReplyReaderFeed(void *reader, char *buf, size_t len); +void redisReplyReaderFeed(void *reader, const char *buf, size_t len); int redisReplyReaderGetReply(void *reader, void **reply); /* Functions to format a command according to the protocol. */ @@ -133,9 +144,12 @@ int redisFormatCommand(char **target, const char *format, ...); int redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen); redisContext *redisConnect(const char *ip, int port); +redisContext *redisConnectWithTimeout(const char *ip, int port, struct timeval tv); redisContext *redisConnectNonBlock(const char *ip, int port); redisContext *redisConnectUnix(const char *path); +redisContext *redisConnectUnixWithTimeout(const char *path, struct timeval tv); redisContext *redisConnectUnixNonBlock(const char *path); +int redisSetTimeout(redisContext *c, struct timeval tv); int redisSetReplyObjectFunctions(redisContext *c, redisReplyObjectFunctions *fn); void redisFree(redisContext *c); int redisBufferRead(redisContext *c); diff --git a/deps/hiredis/net.c b/deps/hiredis/net.c index 88171461..438a129b 100644 --- a/deps/hiredis/net.c +++ b/deps/hiredis/net.c @@ -4,6 +4,7 @@ * Copyright (c) 2010, Pieter Noordhuis * * All rights reserved. + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * @@ -32,6 +33,7 @@ #include "fmacros.h" #include #include +#include #include #include #include @@ -66,7 +68,7 @@ static int redisCreateSocket(redisContext *c, int type) { return s; } -static int redisSetNonBlock(redisContext *c, int fd) { +static int redisSetBlocking(redisContext *c, int fd, int blocking) { int flags; /* Set the socket nonblocking. @@ -78,9 +80,15 @@ static int redisSetNonBlock(redisContext *c, int fd) { close(fd); return REDIS_ERR; } - if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { + + if (blocking) + flags &= ~O_NONBLOCK; + else + flags |= O_NONBLOCK; + + if (fcntl(fd, F_SETFL, flags) == -1) { __redisSetError(c,REDIS_ERR_IO, - sdscatprintf(sdsempty(), "fcntl(F_SETFL,O_NONBLOCK): %s", strerror(errno))); + sdscatprintf(sdsempty(), "fcntl(F_SETFL): %s", strerror(errno))); close(fd); return REDIS_ERR; } @@ -92,19 +100,89 @@ static int redisSetTcpNoDelay(redisContext *c, int fd) { if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) == -1) { __redisSetError(c,REDIS_ERR_IO, sdscatprintf(sdsempty(), "setsockopt(TCP_NODELAY): %s", strerror(errno))); + close(fd); return REDIS_ERR; } return REDIS_OK; } -int redisContextConnectTcp(redisContext *c, const char *addr, int port) { +static int redisContextWaitReady(redisContext *c, int fd, const struct timeval *timeout) { + struct timeval to; + struct timeval *toptr = NULL; + fd_set wfd; + int err; + socklen_t errlen; + + /* Only use timeout when not NULL. */ + if (timeout != NULL) { + to = *timeout; + toptr = &to; + } + + if (errno == EINPROGRESS) { + FD_ZERO(&wfd); + FD_SET(fd, &wfd); + + if (select(FD_SETSIZE, NULL, &wfd, NULL, toptr) == -1) { + __redisSetError(c,REDIS_ERR_IO, + sdscatprintf(sdsempty(), "select(2): %s", strerror(errno))); + close(fd); + return REDIS_ERR; + } + + if (!FD_ISSET(fd, &wfd)) { + errno = ETIMEDOUT; + __redisSetError(c,REDIS_ERR_IO,NULL); + close(fd); + return REDIS_ERR; + } + + err = 0; + errlen = sizeof(err); + if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) { + __redisSetError(c,REDIS_ERR_IO, + sdscatprintf(sdsempty(), "getsockopt(SO_ERROR): %s", strerror(errno))); + close(fd); + return REDIS_ERR; + } + + if (err) { + errno = err; + __redisSetError(c,REDIS_ERR_IO,NULL); + close(fd); + return REDIS_ERR; + } + + return REDIS_OK; + } + + __redisSetError(c,REDIS_ERR_IO,NULL); + close(fd); + return REDIS_ERR; +} + +int redisContextSetTimeout(redisContext *c, struct timeval tv) { + if (setsockopt(c->fd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv)) == -1) { + __redisSetError(c,REDIS_ERR_IO, + sdscatprintf(sdsempty(), "setsockopt(SO_RCVTIMEO): %s", strerror(errno))); + return REDIS_ERR; + } + if (setsockopt(c->fd,SOL_SOCKET,SO_SNDTIMEO,&tv,sizeof(tv)) == -1) { + __redisSetError(c,REDIS_ERR_IO, + sdscatprintf(sdsempty(), "setsockopt(SO_SNDTIMEO): %s", strerror(errno))); + return REDIS_ERR; + } + return REDIS_OK; +} + +int redisContextConnectTcp(redisContext *c, const char *addr, int port, struct timeval *timeout) { int s; int blocking = (c->flags & REDIS_BLOCK); struct sockaddr_in sa; - if ((s = redisCreateSocket(c,AF_INET)) == REDIS_ERR) + if ((s = redisCreateSocket(c,AF_INET)) < 0) return REDIS_ERR; - if (!blocking && redisSetNonBlock(c,s) == REDIS_ERR) + if (redisSetBlocking(c,s,0) != REDIS_OK) return REDIS_ERR; sa.sin_family = AF_INET; @@ -126,30 +204,31 @@ int redisContextConnectTcp(redisContext *c, const char *addr, int port) { if (errno == EINPROGRESS && !blocking) { /* This is ok. */ } else { - __redisSetError(c,REDIS_ERR_IO,NULL); - close(s); - return REDIS_ERR; + if (redisContextWaitReady(c,s,timeout) != REDIS_OK) + return REDIS_ERR; } } - if (redisSetTcpNoDelay(c,s) != REDIS_OK) { - close(s); + /* Reset socket to be blocking after connect(2). */ + if (blocking && redisSetBlocking(c,s,1) != REDIS_OK) + return REDIS_ERR; + + if (redisSetTcpNoDelay(c,s) != REDIS_OK) return REDIS_ERR; - } c->fd = s; c->flags |= REDIS_CONNECTED; return REDIS_OK; } -int redisContextConnectUnix(redisContext *c, const char *path) { +int redisContextConnectUnix(redisContext *c, const char *path, struct timeval *timeout) { int s; int blocking = (c->flags & REDIS_BLOCK); struct sockaddr_un sa; - if ((s = redisCreateSocket(c,AF_LOCAL)) == REDIS_ERR) + if ((s = redisCreateSocket(c,AF_LOCAL)) < 0) return REDIS_ERR; - if (!blocking && redisSetNonBlock(c,s) != REDIS_OK) + if (redisSetBlocking(c,s,0) != REDIS_OK) return REDIS_ERR; sa.sun_family = AF_LOCAL; @@ -158,12 +237,15 @@ int redisContextConnectUnix(redisContext *c, const char *path) { if (errno == EINPROGRESS && !blocking) { /* This is ok. */ } else { - __redisSetError(c,REDIS_ERR_IO,NULL); - close(s); - return REDIS_ERR; + if (redisContextWaitReady(c,s,timeout) != REDIS_OK) + return REDIS_ERR; } } + /* Reset socket to be blocking after connect(2). */ + if (blocking && redisSetBlocking(c,s,1) != REDIS_OK) + return REDIS_ERR; + c->fd = s; c->flags |= REDIS_CONNECTED; return REDIS_OK; diff --git a/deps/hiredis/net.h b/deps/hiredis/net.h index b052d97f..e149ceba 100644 --- a/deps/hiredis/net.h +++ b/deps/hiredis/net.h @@ -1,6 +1,8 @@ /* Extracted from anet.c to work properly with Hiredis error reporting. * * Copyright (c) 2006-2010, Salvatore Sanfilippo + * Copyright (c) 2010, Pieter Noordhuis + * * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -37,7 +39,8 @@ #define AF_LOCAL AF_UNIX #endif -int redisContextConnectTcp(redisContext *c, const char *addr, int port); -int redisContextConnectUnix(redisContext *c, const char *path); +int redisContextSetTimeout(redisContext *c, struct timeval tv); +int redisContextConnectTcp(redisContext *c, const char *addr, int port, struct timeval *timeout); +int redisContextConnectUnix(redisContext *c, const char *path, struct timeval *timeout); #endif diff --git a/deps/hiredis/sds.c b/deps/hiredis/sds.c index e290705a..2bd5a81c 100644 --- a/deps/hiredis/sds.c +++ b/deps/hiredis/sds.c @@ -30,11 +30,11 @@ #define SDS_ABORT_ON_OOM -#include "sds.h" #include #include #include #include +#include "sds.h" static void sdsOomAbort(void) { fprintf(stderr,"SDS: Out Of Memory (SDS_ABORT_ON_OOM defined)\n"); @@ -69,11 +69,6 @@ sds sdsnew(const char *init) { return sdsnewlen(init, initlen); } -size_t sdslen(const sds s) { - struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); - return sh->len; -} - sds sdsdup(const sds s) { return sdsnewlen(s, sdslen(s)); } @@ -83,11 +78,6 @@ void sdsfree(sds s) { free(s-sizeof(struct sdshdr)); } -size_t sdsavail(sds s) { - struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); - return sh->free; -} - void sdsupdatelen(sds s) { struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); int reallen = strlen(s); @@ -115,6 +105,25 @@ static sds sdsMakeRoomFor(sds s, size_t addlen) { return newsh->buf; } +/* Grow the sds to have the specified length. Bytes that were not part of + * the original length of the sds will be set to zero. */ +sds sdsgrowzero(sds s, size_t len) { + struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr))); + size_t totlen, curlen = sh->len; + + if (len <= curlen) return s; + s = sdsMakeRoomFor(s,len-curlen); + if (s == NULL) return NULL; + + /* Make sure added region doesn't contain garbage */ + sh = (void*)(s-(sizeof(struct sdshdr))); + memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */ + totlen = sh->len+sh->free; + sh->len = len; + sh->free = totlen-sh->len; + return s; +} + sds sdscatlen(sds s, const void *t, size_t len) { struct sdshdr *sh; size_t curlen = sdslen(s); @@ -222,13 +231,16 @@ sds sdsrange(sds s, int start, int end) { } newlen = (start > end) ? 0 : (end-start)+1; if (newlen != 0) { - if (start >= (signed)len) start = len-1; - if (end >= (signed)len) end = len-1; - newlen = (start > end) ? 0 : (end-start)+1; + if (start >= (signed)len) { + newlen = 0; + } else if (end >= (signed)len) { + end = len-1; + newlen = (start > end) ? 0 : (end-start)+1; + } } else { start = 0; } - if (start != 0) memmove(sh->buf, sh->buf+start, newlen); + if (start && newlen) memmove(sh->buf, sh->buf+start, newlen); sh->buf[newlen] = 0; sh->free = sh->free+(sh->len-newlen); sh->len = newlen; @@ -477,3 +489,106 @@ err: if (current) sdsfree(current); return NULL; } + +#ifdef SDS_TEST_MAIN +#include + +int __failed_tests = 0; +int __test_num = 0; +#define test_cond(descr,_c) do { \ + __test_num++; printf("%d - %s: ", __test_num, descr); \ + if(_c) printf("PASSED\n"); else {printf("FAILED\n"); __failed_tests++;} \ +} while(0); +#define test_report() do { \ + printf("%d tests, %d passed, %d failed\n", __test_num, \ + __test_num-__failed_tests, __failed_tests); \ + if (__failed_tests) { \ + printf("=== WARNING === We have failed tests here...\n"); \ + } \ +} while(0); + +int main(void) { + { + sds x = sdsnew("foo"), y; + + test_cond("Create a string and obtain the length", + sdslen(x) == 3 && memcmp(x,"foo\0",4) == 0) + + sdsfree(x); + x = sdsnewlen("foo",2); + test_cond("Create a string with specified length", + sdslen(x) == 2 && memcmp(x,"fo\0",3) == 0) + + x = sdscat(x,"bar"); + test_cond("Strings concatenation", + sdslen(x) == 5 && memcmp(x,"fobar\0",6) == 0); + + x = sdscpy(x,"a"); + test_cond("sdscpy() against an originally longer string", + sdslen(x) == 1 && memcmp(x,"a\0",2) == 0) + + x = sdscpy(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk"); + test_cond("sdscpy() against an originally shorter string", + sdslen(x) == 33 && + memcmp(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0) + + sdsfree(x); + x = sdscatprintf(sdsempty(),"%d",123); + test_cond("sdscatprintf() seems working in the base case", + sdslen(x) == 3 && memcmp(x,"123\0",4) ==0) + + sdsfree(x); + x = sdstrim(sdsnew("xxciaoyyy"),"xy"); + test_cond("sdstrim() correctly trims characters", + sdslen(x) == 4 && memcmp(x,"ciao\0",5) == 0) + + y = sdsrange(sdsdup(x),1,1); + test_cond("sdsrange(...,1,1)", + sdslen(y) == 1 && memcmp(y,"i\0",2) == 0) + + sdsfree(y); + y = sdsrange(sdsdup(x),1,-1); + test_cond("sdsrange(...,1,-1)", + sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0) + + sdsfree(y); + y = sdsrange(sdsdup(x),-2,-1); + test_cond("sdsrange(...,-2,-1)", + sdslen(y) == 2 && memcmp(y,"ao\0",3) == 0) + + sdsfree(y); + y = sdsrange(sdsdup(x),2,1); + test_cond("sdsrange(...,2,1)", + sdslen(y) == 0 && memcmp(y,"\0",1) == 0) + + sdsfree(y); + y = sdsrange(sdsdup(x),1,100); + test_cond("sdsrange(...,1,100)", + sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0) + + sdsfree(y); + y = sdsrange(sdsdup(x),100,100); + test_cond("sdsrange(...,100,100)", + sdslen(y) == 0 && memcmp(y,"\0",1) == 0) + + sdsfree(y); + sdsfree(x); + x = sdsnew("foo"); + y = sdsnew("foa"); + test_cond("sdscmp(foo,foa)", sdscmp(x,y) > 0) + + sdsfree(y); + sdsfree(x); + x = sdsnew("bar"); + y = sdsnew("bar"); + test_cond("sdscmp(bar,bar)", sdscmp(x,y) == 0) + + sdsfree(y); + sdsfree(x); + x = sdsnew("aar"); + y = sdsnew("bar"); + test_cond("sdscmp(bar,bar)", sdscmp(x,y) < 0) + } + test_report() +} +#endif diff --git a/deps/hiredis/sds.h b/deps/hiredis/sds.h index 2c3fb52b..94f5871f 100644 --- a/deps/hiredis/sds.h +++ b/deps/hiredis/sds.h @@ -42,13 +42,24 @@ struct sdshdr { char buf[]; }; +static inline size_t sdslen(const sds s) { + struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr))); + return sh->len; +} + +static inline size_t sdsavail(const sds s) { + struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr))); + return sh->free; +} + sds sdsnewlen(const void *init, size_t initlen); sds sdsnew(const char *init); -sds sdsempty(); +sds sdsempty(void); size_t sdslen(const sds s); sds sdsdup(const sds s); void sdsfree(sds s); size_t sdsavail(sds s); +sds sdsgrowzero(sds s, size_t len); sds sdscatlen(sds s, const void *t, size_t len); sds sdscat(sds s, const char *t); sds sdscpylen(sds s, char *t, size_t len); diff --git a/deps/hiredis/test.c b/deps/hiredis/test.c index ed355a73..5724a3ea 100644 --- a/deps/hiredis/test.c +++ b/deps/hiredis/test.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "hiredis.h" @@ -31,7 +32,7 @@ static void __connect(redisContext **target) { } } -static void test_format_commands() { +static void test_format_commands(void) { char *cmd; int len; @@ -53,6 +54,12 @@ static void test_format_commands() { len == 4+4+(3+2)+4+(3+2)+4+(0+2)); free(cmd); + test("Format command with an empty string in between proper interpolations: "); + len = redisFormatCommand(&cmd,"SET %s %s","","foo"); + test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$0\r\n\r\n$3\r\nfoo\r\n",len) == 0 && + len == 4+4+(3+2)+4+(0+2)+4+(3+2)); + free(cmd); + test("Format command with %%b string interpolation: "); len = redisFormatCommand(&cmd,"SET %b %b","foo",3,"b\0r",3); test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nb\0r\r\n",len) == 0 && @@ -115,7 +122,7 @@ static void test_format_commands() { free(cmd); } -static void test_blocking_connection() { +static void test_blocking_connection(void) { redisContext *c; redisReply *reply; int major, minor; @@ -246,13 +253,21 @@ static void test_blocking_connection() { * conditions, the error will be set to EOF. */ assert(c->err == REDIS_ERR_EOF && strcmp(c->errstr,"Server closed the connection") == 0); - - /* Clean up context and reconnect again */ redisFree(c); + + __connect(&c); + test("Returns I/O error on socket timeout: "); + struct timeval tv = { 0, 1000 }; + assert(redisSetTimeout(c,tv) == REDIS_OK); + test_cond(redisGetReply(c,(void**)&reply) == REDIS_ERR && + c->err == REDIS_ERR_IO && errno == EAGAIN); + redisFree(c); + + /* Context should be connected */ __connect(&c); } -static void test_reply_reader() { +static void test_reply_reader(void) { void *reader; void *reply; char *err; @@ -309,10 +324,19 @@ static void test_reply_reader() { ret = redisReplyReaderGetReply(reader,&reply); test_cond(ret == REDIS_OK && reply == (void*)REDIS_REPLY_STATUS); redisReplyReaderFree(reader); + + test("Properly reset state after protocol error: "); + reader = redisReplyReaderCreate(); + redisReplyReaderSetReplyObjectFunctions(reader,NULL); + redisReplyReaderFeed(reader,(char*)"x",1); + ret = redisReplyReaderGetReply(reader,&reply); + assert(ret == REDIS_ERR); + ret = redisReplyReaderGetReply(reader,&reply); + test_cond(ret == REDIS_OK && reply == NULL) } -static void test_throughput() { - int i; +static void test_throughput(void) { + int i, num; long long t1, t2; redisContext *c = blocking_context; redisReply **replies; @@ -321,31 +345,60 @@ static void test_throughput() { for (i = 0; i < 500; i++) freeReplyObject(redisCommand(c,"LPUSH mylist foo")); - replies = malloc(sizeof(redisReply*)*1000); + num = 1000; + replies = malloc(sizeof(redisReply*)*num); t1 = usec(); - for (i = 0; i < 1000; i++) { + for (i = 0; i < num; i++) { replies[i] = redisCommand(c,"PING"); assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS); } t2 = usec(); - for (i = 0; i < 1000; i++) freeReplyObject(replies[i]); + for (i = 0; i < num; i++) freeReplyObject(replies[i]); free(replies); - printf("\t(1000x PING: %.2fs)\n", (t2-t1)/1000000.0); + printf("\t(%dx PING: %.3fs)\n", num, (t2-t1)/1000000.0); - replies = malloc(sizeof(redisReply*)*1000); + replies = malloc(sizeof(redisReply*)*num); t1 = usec(); - for (i = 0; i < 1000; i++) { + for (i = 0; i < num; i++) { replies[i] = redisCommand(c,"LRANGE mylist 0 499"); assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_ARRAY); assert(replies[i] != NULL && replies[i]->elements == 500); } t2 = usec(); - for (i = 0; i < 1000; i++) freeReplyObject(replies[i]); + for (i = 0; i < num; i++) freeReplyObject(replies[i]); free(replies); - printf("\t(1000x LRANGE with 500 elements: %.2fs)\n", (t2-t1)/1000000.0); + printf("\t(%dx LRANGE with 500 elements: %.3fs)\n", num, (t2-t1)/1000000.0); + + num = 10000; + replies = malloc(sizeof(redisReply*)*num); + for (i = 0; i < num; i++) + redisAppendCommand(c,"PING"); + t1 = usec(); + for (i = 0; i < num; i++) { + assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK); + assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS); + } + t2 = usec(); + for (i = 0; i < num; i++) freeReplyObject(replies[i]); + free(replies); + printf("\t(%dx PING (pipelined): %.3fs)\n", num, (t2-t1)/1000000.0); + + replies = malloc(sizeof(redisReply*)*num); + for (i = 0; i < num; i++) + redisAppendCommand(c,"LRANGE mylist 0 499"); + t1 = usec(); + for (i = 0; i < num; i++) { + assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK); + assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_ARRAY); + assert(replies[i] != NULL && replies[i]->elements == 500); + } + t2 = usec(); + for (i = 0; i < num; i++) freeReplyObject(replies[i]); + free(replies); + printf("\t(%dx LRANGE with 500 elements (pipelined): %.3fs)\n", num, (t2-t1)/1000000.0); } -static void cleanup() { +static void cleanup(void) { redisContext *c = blocking_context; redisReply *reply; From ded614f803f56ad979da76485e74732ecdcc57fd Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Tue, 19 Apr 2011 23:10:45 +0200 Subject: [PATCH 125/146] Inline sdslen and sdsavail (thanks to @bitbckt) --- src/sds.c | 12 +----------- src/sds.h | 10 ++++++++++ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/sds.c b/src/sds.c index 37c63b05..343ad1fa 100644 --- a/src/sds.c +++ b/src/sds.c @@ -36,11 +36,11 @@ #define SDS_ABORT_ON_OOM -#include "sds.h" #include #include #include #include +#include "sds.h" #include "zmalloc.h" static void sdsOomAbort(void) { @@ -78,11 +78,6 @@ sds sdsnew(const char *init) { return sdsnewlen(init, initlen); } -size_t sdslen(const sds s) { - struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); - return sh->len; -} - sds sdsdup(const sds s) { return sdsnewlen(s, sdslen(s)); } @@ -92,11 +87,6 @@ void sdsfree(sds s) { zfree(s-sizeof(struct sdshdr)); } -size_t sdsavail(sds s) { - struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); - return sh->free; -} - void sdsupdatelen(sds s) { struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); int reallen = strlen(s); diff --git a/src/sds.h b/src/sds.h index 6a317868..ea43f868 100644 --- a/src/sds.h +++ b/src/sds.h @@ -42,6 +42,16 @@ struct sdshdr { char buf[]; }; +static inline size_t sdslen(const sds s) { + struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr))); + return sh->len; +} + +static inline size_t sdsavail(const sds s) { + struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr))); + return sh->free; +} + sds sdsnewlen(const void *init, size_t initlen); sds sdsnew(const char *init); sds sdsempty(); From 5282562117565c54ecf3153901a80a244a0577ba Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Tue, 19 Apr 2011 23:54:43 +0200 Subject: [PATCH 126/146] More allocators --- src/Makefile | 18 ++++++++++++++++-- src/config.h | 7 +++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Makefile b/src/Makefile index 00bdf481..d542c342 100644 --- a/src/Makefile +++ b/src/Makefile @@ -16,9 +16,23 @@ else endif ifeq ($(USE_TCMALLOC),yes) - CCLINK+= -ltcmalloc - CFLAGS+= -DUSE_TCMALLOC + ALLOC_LINK=-ltcmalloc + ALLOC_FLAGS=-DUSE_TCMALLOC endif + +ifeq ($(USE_TCMALLOC_MINIMAL),yes) + ALLOC_LINK=-ltcmalloc_minimal + ALLOC_FLAGS=-DUSE_TCMALLOC +endif + +ifeq ($(USE_JEMALLOC),yes) + ALLOC_LINK=-ljemalloc + ALLOC_FLAGS=-DUSE_JEMALLOC +endif + +CCLINK+= $(ALLOC_LINK) +CFLAGS+= $(ALLOC_FLAGS) + CCOPT= $(CFLAGS) $(CCLINK) $(ARCH) $(PROF) PREFIX= /usr/local diff --git a/src/config.h b/src/config.h index d98067c1..7e1133ec 100644 --- a/src/config.h +++ b/src/config.h @@ -15,6 +15,13 @@ #define HAVE_MALLOC_SIZE 1 #define redis_malloc_size(p) tc_malloc_size(p) #endif +#elif defined(USE_JEMALLOC) +#define JEMALLOC_MANGLE +#include +#if JEMALLOC_VERSION_MAJOR >= 2 && JEMALLOC_VERSION_MINOR >= 1 +#define HAVE_MALLOC_SIZE 1 +#define redis_malloc_size(p) JEMALLOC_P(malloc_usable_size)(p) +#endif #elif defined(__APPLE__) #include #define HAVE_MALLOC_SIZE 1 From af0e51f2e15bc9c63d11229e64ce7884e1a1402a Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 27 Apr 2011 13:16:23 +0200 Subject: [PATCH 127/146] Move code --- src/networking.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/networking.c b/src/networking.c index 9d41b380..48febb09 100644 --- a/src/networking.c +++ b/src/networking.c @@ -720,13 +720,14 @@ int processMultibulkBuffer(redisClient *c) { addReplyError(c,"Protocol error: invalid multibulk length"); setProtocolError(c,pos); return REDIS_ERR; - } else { - pos = (newline-c->querybuf)+2; - if (ll <= 0) { - c->querybuf = sdsrange(c->querybuf,pos,-1); - return REDIS_OK; - } } + + pos = (newline-c->querybuf)+2; + if (ll <= 0) { + c->querybuf = sdsrange(c->querybuf,pos,-1); + return REDIS_OK; + } + c->multibulklen = ll; /* Setup argv array on client structure */ From 5d08193126df54405dae3073c62b7c19ae03d1a4 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 27 Apr 2011 13:24:52 +0200 Subject: [PATCH 128/146] Tests for string2ll; move isObject* to object.c --- src/Makefile | 3 +- src/object.c | 12 +++- src/redis.h | 14 +---- src/util.c | 159 +++++++++++++++++++++++++++++++++++++++------------ src/util.h | 12 ++++ 5 files changed, 150 insertions(+), 50 deletions(-) create mode 100644 src/util.h diff --git a/src/Makefile b/src/Makefile index d542c342..e08c6987 100644 --- a/src/Makefile +++ b/src/Makefile @@ -125,8 +125,7 @@ t_string.o: t_string.c redis.h fmacros.h config.h ae.h sds.h dict.h \ adlist.h zmalloc.h anet.h zipmap.h ziplist.h intset.h version.h t_zset.o: t_zset.c redis.h fmacros.h config.h ae.h sds.h dict.h adlist.h \ zmalloc.h anet.h zipmap.h ziplist.h intset.h version.h -util.o: util.c redis.h fmacros.h config.h ae.h sds.h dict.h adlist.h \ - zmalloc.h anet.h zipmap.h ziplist.h intset.h version.h +util.o: util.c util.h cluster.o: redis.h fmacros.h config.h ae.h sds.h dict.h adlist.h \ zmalloc.h anet.h zipmap.h ziplist.h intset.h version.h ziplist.o: ziplist.c zmalloc.h ziplist.h diff --git a/src/object.c b/src/object.c index c7c90c54..22f53837 100644 --- a/src/object.c +++ b/src/object.c @@ -202,6 +202,16 @@ int checkType(redisClient *c, robj *o, int type) { return 0; } +int isObjectRepresentableAsLongLong(robj *o, long long *llval) { + redisAssert(o->type == REDIS_STRING); + if (o->encoding == REDIS_ENCODING_INT) { + if (llval) *llval = (long) o->ptr; + return REDIS_OK; + } else { + return string2ll(o->ptr,sdslen(o->ptr),llval) ? REDIS_OK : REDIS_ERR; + } +} + /* Try to encode a string object in order to save space */ robj *tryObjectEncoding(robj *o) { long value; @@ -219,7 +229,7 @@ robj *tryObjectEncoding(robj *o) { redisAssert(o->type == REDIS_STRING); /* Check if we can represent this string as a long integer */ - if (isStringRepresentableAsLong(s,&value) == REDIS_ERR) return o; + if (!string2l(s,sdslen(s),&value)) return o; /* Ok, this object can be encoded... * diff --git a/src/redis.h b/src/redis.h index 8098a8de..5934b6a6 100644 --- a/src/redis.h +++ b/src/redis.h @@ -30,6 +30,7 @@ #include "ziplist.h" /* Compact list data structure */ #include "intset.h" /* Compact integer set structure */ #include "version.h" +#include "util.h" /* Error codes */ #define REDIS_OK 0 @@ -852,6 +853,7 @@ void freeHashObject(robj *o); robj *createObject(int type, void *ptr); robj *createStringObject(char *ptr, size_t len); robj *dupStringObject(robj *o); +int isObjectRepresentableAsLongLong(robj *o, long long *llongval); robj *tryObjectEncoding(robj *o); robj *getDecodedObject(robj *o); size_t stringObjectLen(robj *o); @@ -1028,18 +1030,6 @@ int pubsubUnsubscribeAllPatterns(redisClient *c, int notify); void freePubsubPattern(void *p); int listMatchPubsubPattern(void *a, void *b); -/* Utility functions */ -int stringmatchlen(const char *pattern, int patternLen, - const char *string, int stringLen, int nocase); -int stringmatch(const char *pattern, const char *string, int nocase); -long long memtoll(const char *p, int *err); -int ll2string(char *s, size_t len, long long value); -int string2ll(char *s, size_t len, long long *value); -int d2string(char *s, size_t len, double value); -int isStringRepresentableAsLong(sds s, long *longval); -int isStringRepresentableAsLongLong(sds s, long long *longval); -int isObjectRepresentableAsLongLong(robj *o, long long *llongval); - /* Configuration */ void loadServerConfig(char *filename); void appendServerSaveParams(time_t seconds, int changes); diff --git a/src/util.c b/src/util.c index 2404c77c..5712241b 100644 --- a/src/util.c +++ b/src/util.c @@ -1,7 +1,10 @@ -#include "redis.h" +#include +#include +#include #include #include #include +#include "util.h" /* Glob-style pattern matching. */ int stringmatchlen(const char *pattern, int patternLen, @@ -213,6 +216,12 @@ int string2ll(char *s, size_t slen, long long *value) { if (plen == slen) return 0; + /* Special case: first and only digit is 0. */ + if (slen == 1 && p[0] == '0') { + if (value != NULL) *value = 0; + return 1; + } + if (p[0] == '-') { negative = 1; p++; plen++; @@ -250,7 +259,7 @@ int string2ll(char *s, size_t slen, long long *value) { return 0; if (negative) { - if (v > (-(unsigned long long)LLONG_MIN)) /* Overflow. */ + if (v > ((unsigned long long)(-(LLONG_MIN+1))+1)) /* Overflow. */ return 0; if (value != NULL) *value = -v; } else { @@ -261,6 +270,22 @@ int string2ll(char *s, size_t slen, long long *value) { return 1; } +/* Convert a string into a long. Returns 1 if the string could be parsed into a + * (non-overflowing) long, 0 otherwise. The value will be set to the parsed + * value when appropriate. */ +int string2l(char *s, size_t slen, long *lval) { + long long llval; + + if (!string2ll(s,slen,&llval)) + return 0; + + if (llval < LONG_MIN || llval > LONG_MAX) + return 0; + + *lval = (long)llval; + return 1; +} + /* Convert a double to a string representation. Returns the number of bytes * required. The representation should always be parsable by stdtod(3). */ int d2string(char *buf, size_t len, double value) { @@ -300,44 +325,108 @@ int d2string(char *buf, size_t len, double value) { return len; } -/* Check if the sds string 's' can be represented by a long long - * (that is, is a number that fits into long without any other space or - * character before or after the digits, so that converting this number - * back to a string will result in the same bytes as the original string). - * - * If so, the function returns REDIS_OK and *llongval is set to the value - * of the number. Otherwise REDIS_ERR is returned */ -int isStringRepresentableAsLongLong(sds s, long long *llongval) { - char buf[32], *endptr; - long long value; - int slen; +#ifdef UTIL_TEST_MAIN +#include - value = strtoll(s, &endptr, 10); - if (endptr[0] != '\0') return REDIS_ERR; - slen = ll2string(buf,32,value); +void test_string2ll(void) { + char buf[32]; + long long v; - /* If the number converted back into a string is not identical - * then it's not possible to encode the string as integer */ - if (sdslen(s) != (unsigned)slen || memcmp(buf,s,slen)) return REDIS_ERR; - if (llongval) *llongval = value; - return REDIS_OK; + /* May not start with +. */ + strcpy(buf,"+1"); + assert(string2ll(buf,strlen(buf),&v) == 0); + + /* May not start with 0. */ + strcpy(buf,"01"); + assert(string2ll(buf,strlen(buf),&v) == 0); + + strcpy(buf,"-1"); + assert(string2ll(buf,strlen(buf),&v) == 1); + assert(v == -1); + + strcpy(buf,"0"); + assert(string2ll(buf,strlen(buf),&v) == 1); + assert(v == 0); + + strcpy(buf,"1"); + assert(string2ll(buf,strlen(buf),&v) == 1); + assert(v == 1); + + strcpy(buf,"99"); + assert(string2ll(buf,strlen(buf),&v) == 1); + assert(v == 99); + + strcpy(buf,"-99"); + assert(string2ll(buf,strlen(buf),&v) == 1); + assert(v == -99); + + strcpy(buf,"-9223372036854775808"); + assert(string2ll(buf,strlen(buf),&v) == 1); + assert(v == LLONG_MIN); + + strcpy(buf,"-9223372036854775809"); /* overflow */ + assert(string2ll(buf,strlen(buf),&v) == 0); + + strcpy(buf,"9223372036854775807"); + assert(string2ll(buf,strlen(buf),&v) == 1); + assert(v == LLONG_MAX); + + strcpy(buf,"9223372036854775808"); /* overflow */ + assert(string2ll(buf,strlen(buf),&v) == 0); } -int isStringRepresentableAsLong(sds s, long *longval) { - long long ll; +void test_string2l(void) { + char buf[32]; + long v; - if (isStringRepresentableAsLongLong(s,&ll) == REDIS_ERR) return REDIS_ERR; - if (ll < LONG_MIN || ll > LONG_MAX) return REDIS_ERR; - *longval = (long)ll; - return REDIS_OK; + /* May not start with +. */ + strcpy(buf,"+1"); + assert(string2l(buf,strlen(buf),&v) == 0); + + /* May not start with 0. */ + strcpy(buf,"01"); + assert(string2l(buf,strlen(buf),&v) == 0); + + strcpy(buf,"-1"); + assert(string2l(buf,strlen(buf),&v) == 1); + assert(v == -1); + + strcpy(buf,"0"); + assert(string2l(buf,strlen(buf),&v) == 1); + assert(v == 0); + + strcpy(buf,"1"); + assert(string2l(buf,strlen(buf),&v) == 1); + assert(v == 1); + + strcpy(buf,"99"); + assert(string2l(buf,strlen(buf),&v) == 1); + assert(v == 99); + + strcpy(buf,"-99"); + assert(string2l(buf,strlen(buf),&v) == 1); + assert(v == -99); + +#if LONG_MAX != LLONG_MAX + strcpy(buf,"-2147483648"); + assert(string2l(buf,strlen(buf),&v) == 1); + assert(v == LONG_MIN); + + strcpy(buf,"-2147483649"); /* overflow */ + assert(string2l(buf,strlen(buf),&v) == 0); + + strcpy(buf,"2147483647"); + assert(string2l(buf,strlen(buf),&v) == 1); + assert(v == LONG_MAX); + + strcpy(buf,"2147483648"); /* overflow */ + assert(string2l(buf,strlen(buf),&v) == 0); +#endif } -int isObjectRepresentableAsLongLong(robj *o, long long *llongval) { - redisAssert(o->type == REDIS_STRING); - if (o->encoding == REDIS_ENCODING_INT) { - if (llongval) *llongval = (long) o->ptr; - return REDIS_OK; - } else { - return isStringRepresentableAsLongLong(o->ptr,llongval); - } +int main(int argc, char **argv) { + test_string2ll(); + test_string2l(); + return 0; } +#endif diff --git a/src/util.h b/src/util.h new file mode 100644 index 00000000..b897a89e --- /dev/null +++ b/src/util.h @@ -0,0 +1,12 @@ +#ifndef __REDIS_UTIL_H +#define __REDIS_UTIL_H + +int stringmatchlen(const char *p, int plen, const char *s, int slen, int nocase); +int stringmatch(const char *p, const char *s, int nocase); +long long memtoll(const char *p, int *err); +int ll2string(char *s, size_t len, long long value); +int string2ll(char *s, size_t slen, long long *value); +int string2l(char *s, size_t slen, long *value); +int d2string(char *buf, size_t len, double value); + +#endif From edf23aff0e316908b5d4ed1f2b21800865c6f7bf Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 27 Apr 2011 13:30:32 +0200 Subject: [PATCH 129/146] Use string2ll in ziplist code (faster) --- src/ziplist.c | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/src/ziplist.c b/src/ziplist.c index 37c2c18f..f5f9e9a6 100644 --- a/src/ziplist.c +++ b/src/ziplist.c @@ -67,11 +67,10 @@ #include #include #include "zmalloc.h" +#include "util.h" #include "ziplist.h" #include "endian.h" -int ll2string(char *s, size_t len, long long value); - #define ZIP_END 255 #define ZIP_BIGLEN 254 @@ -252,22 +251,9 @@ static int zipPrevLenByteDiff(unsigned char *p, unsigned int len) { * Stores the integer value in 'v' and its encoding in 'encoding'. */ static int zipTryEncoding(unsigned char *entry, unsigned int entrylen, long long *v, unsigned char *encoding) { long long value; - char *eptr; - char buf[32]; if (entrylen >= 32 || entrylen == 0) return 0; - if (entry[0] == '-' || (entry[0] >= '0' && entry[0] <= '9')) { - int slen; - - /* Perform a back-and-forth conversion to make sure that - * the string turned into an integer is not losing any info. */ - memcpy(buf,entry,entrylen); - buf[entrylen] = '\0'; - value = strtoll(buf,&eptr,10); - if (eptr[0] != '\0') return 0; - slen = ll2string(buf,32,value); - if (entrylen != (unsigned)slen || memcmp(buf,entry,slen)) return 0; - + if (string2ll((char*)entry,entrylen,&value)) { /* Great, the string can be encoded. Check what's the smallest * of our encoding types that can hold this value. */ if (value >= INT16_MIN && value <= INT16_MAX) { From 32f99c5128fc46d5f4d40acbb65d204620432d0a Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 27 Apr 2011 13:35:49 +0200 Subject: [PATCH 130/146] Show memory allocator in INFO output --- src/config.h | 8 +++++++- src/redis.c | 8 ++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/config.h b/src/config.h index 7e1133ec..208c7b93 100644 --- a/src/config.h +++ b/src/config.h @@ -10,12 +10,14 @@ * this expects a different allocation scheme. Therefore, *exclusively* use * either tcmalloc or OSX's malloc_size()! */ #if defined(USE_TCMALLOC) +#define REDIS_MALLOC "tcmalloc" #include #if TC_VERSION_MAJOR >= 1 && TC_VERSION_MINOR >= 6 #define HAVE_MALLOC_SIZE 1 #define redis_malloc_size(p) tc_malloc_size(p) #endif #elif defined(USE_JEMALLOC) +#define REDIS_MALLOC "jemalloc" #define JEMALLOC_MANGLE #include #if JEMALLOC_VERSION_MAJOR >= 2 && JEMALLOC_VERSION_MINOR >= 1 @@ -28,7 +30,11 @@ #define redis_malloc_size(p) malloc_size(p) #endif -/* Tefine redis_fstat to fstat or fstat64() */ +#ifndef REDIS_MALLOC +#define REDIS_MALLOC "libc" +#endif + +/* Define redis_fstat to fstat or fstat64() */ #if defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_6) #define redis_fstat fstat64 #define redis_stat stat64 diff --git a/src/redis.c b/src/redis.c index 5ddbb177..63b41ba8 100644 --- a/src/redis.c +++ b/src/redis.c @@ -1319,18 +1319,14 @@ sds genRedisInfoString(char *section) { "used_memory_peak:%zu\r\n" "used_memory_peak_human:%s\r\n" "mem_fragmentation_ratio:%.2f\r\n" - "use_tcmalloc:%d\r\n", + "mem_allocator:%s\r\n", zmalloc_used_memory(), hmem, zmalloc_get_rss(), server.stat_peak_memory, peak_hmem, zmalloc_get_fragmentation_ratio(), - #ifdef USE_TCMALLOC - 1 - #else - 0 - #endif + REDIS_MALLOC ); } From bf9fd5ffa248ac8a9c577eb2fc7fb6cd8a445113 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 27 Apr 2011 14:29:27 +0200 Subject: [PATCH 131/146] Check for \n after finding \r --- src/networking.c | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/networking.c b/src/networking.c index 48febb09..50a2cef2 100644 --- a/src/networking.c +++ b/src/networking.c @@ -712,6 +712,10 @@ int processMultibulkBuffer(redisClient *c) { if (newline == NULL) return REDIS_ERR; + /* Buffer should also contain \n */ + if (newline-(c->querybuf) > ((signed)sdslen(c->querybuf)-2)) + return REDIS_ERR; + /* We know for sure there is a whole line since newline != NULL, * so go ahead and find out the multi bulk length. */ redisAssert(c->querybuf[0] == '*'); @@ -733,9 +737,6 @@ int processMultibulkBuffer(redisClient *c) { /* Setup argv array on client structure */ if (c->argv) zfree(c->argv); c->argv = zmalloc(sizeof(robj*)*c->multibulklen); - - /* Search new newline */ - newline = strchr(c->querybuf+pos,'\r'); } redisAssert(c->multibulklen > 0); @@ -743,27 +744,30 @@ int processMultibulkBuffer(redisClient *c) { /* Read bulk length if unknown */ if (c->bulklen == -1) { newline = strchr(c->querybuf+pos,'\r'); - if (newline != NULL) { - if (c->querybuf[pos] != '$') { - addReplyErrorFormat(c, - "Protocol error: expected '$', got '%c'", - c->querybuf[pos]); - setProtocolError(c,pos); - return REDIS_ERR; - } - - ok = string2ll(c->querybuf+pos+1,newline-(c->querybuf+pos+1),&ll); - if (!ok || ll < 0 || ll > 512*1024*1024) { - addReplyError(c,"Protocol error: invalid bulk length"); - setProtocolError(c,pos); - return REDIS_ERR; - } - pos += newline-(c->querybuf+pos)+2; - c->bulklen = ll; - } else { - /* No newline in current buffer, so wait for more data */ + if (newline == NULL) break; + + /* Buffer should also contain \n */ + if (newline-(c->querybuf) > ((signed)sdslen(c->querybuf)-2)) + break; + + if (c->querybuf[pos] != '$') { + addReplyErrorFormat(c, + "Protocol error: expected '$', got '%c'", + c->querybuf[pos]); + setProtocolError(c,pos); + return REDIS_ERR; } + + ok = string2ll(c->querybuf+pos+1,newline-(c->querybuf+pos+1),&ll); + if (!ok || ll < 0 || ll > 512*1024*1024) { + addReplyError(c,"Protocol error: invalid bulk length"); + setProtocolError(c,pos); + return REDIS_ERR; + } + + pos += newline-(c->querybuf+pos)+2; + c->bulklen = ll; } /* Read bulk argument */ From c040cbd6252b493456ca910d7b1d1cf4122959d4 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 27 Apr 2011 14:32:56 +0200 Subject: [PATCH 132/146] Check seplen and len before malloc'ing "tokens" --- src/sds.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/sds.c b/src/sds.c index 343ad1fa..63507000 100644 --- a/src/sds.c +++ b/src/sds.c @@ -298,15 +298,17 @@ int sdscmp(sds s1, sds s2) { */ sds *sdssplitlen(char *s, int len, char *sep, int seplen, int *count) { int elements = 0, slots = 5, start = 0, j; + sds *tokens; - sds *tokens = zmalloc(sizeof(sds)*slots); + if (seplen < 1 || len < 0) return NULL; + + tokens = zmalloc(sizeof(sds)*slots); #ifdef SDS_ABORT_ON_OOM if (tokens == NULL) sdsOomAbort(); +#else + if (tokens == NULL) return NULL; #endif - if (seplen < 1 || len < 0 || tokens == NULL) { - *count = 0; - return NULL; - } + if (len == 0) { *count = 0; return tokens; From 3edbcab95a2d5cec861ac28cbdb638ea25d22bb9 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 27 Apr 2011 14:38:58 +0200 Subject: [PATCH 133/146] Extra tests for string2ll --- src/util.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/util.c b/src/util.c index 5712241b..e83dbedd 100644 --- a/src/util.c +++ b/src/util.c @@ -336,6 +336,14 @@ void test_string2ll(void) { strcpy(buf,"+1"); assert(string2ll(buf,strlen(buf),&v) == 0); + /* Leading space. */ + strcpy(buf," 1"); + assert(string2ll(buf,strlen(buf),&v) == 0); + + /* Trailing space. */ + strcpy(buf,"1 "); + assert(string2ll(buf,strlen(buf),&v) == 0); + /* May not start with 0. */ strcpy(buf,"01"); assert(string2ll(buf,strlen(buf),&v) == 0); From 0276e5545c78490613871939d48989090a54c2c9 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 5 May 2011 17:50:14 +0200 Subject: [PATCH 134/146] Fixed problem in cluster redirection due to a stupid typo --- src/cluster.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cluster.c b/src/cluster.c index b77a61b0..1d39d3e8 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1650,7 +1650,7 @@ clusterNode *getNodeByQuery(redisClient *c, struct redisCommand *cmd, robj **arg margv = ms->commands[i].argv; keyindex = getKeysFromCommand(mcmd,margv,margc,&numkeys, - REDIS_GETKEYS_PRELOAD); + REDIS_GETKEYS_ALL); for (j = 0; j < numkeys; j++) { if (firstkey == NULL) { /* This is the first key we see. Check what is the slot From 4763ecc9add311467c9c3852a81664a0b3005919 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 5 May 2011 17:52:19 +0200 Subject: [PATCH 135/146] missing return caused protocol desync in CLUSTER SETSLOT --- src/cluster.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cluster.c b/src/cluster.c index 1d39d3e8..0b55b107 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1241,6 +1241,7 @@ void clusterCommand(redisClient *c) { server.cluster.importing_slots_from[slot] = NULL; } else { addReplyError(c,"Invalid CLUSTER SETSLOT action or number of arguments"); + return; } clusterSaveConfigOrDie(); addReply(c,shared.ok); From 46834808fe31e7cbf08d6c9f7638a54c5c8bff8a Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 5 May 2011 17:56:12 +0200 Subject: [PATCH 136/146] CLUSTER SETSLOT STABLE fixed --- src/cluster.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cluster.c b/src/cluster.c index 0b55b107..e8ca51cf 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1239,6 +1239,7 @@ void clusterCommand(redisClient *c) { server.cluster.importing_slots_from[slot] = n; } else if (!strcasecmp(c->argv[3]->ptr,"stable") && c->argc == 4) { server.cluster.importing_slots_from[slot] = NULL; + server.cluster.migrating_slots_to[slot] = NULL; } else { addReplyError(c,"Invalid CLUSTER SETSLOT action or number of arguments"); return; From a7b058dae612ad3ab31a2b2d6f07e5ff7fe06618 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 5 May 2011 18:10:02 +0200 Subject: [PATCH 137/146] Fixed semantics of CLUSTER SETSLOT, SELECT now only denied in cluster mode if selected DB is not 0 so that MIGRATE still works well. --- src/cluster.c | 13 +++++++++---- src/db.c | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index e8ca51cf..d8eca519 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1219,11 +1219,11 @@ void clusterCommand(redisClient *c) { return; } slot = (unsigned int) aux; - if (server.cluster.slots[slot] != server.cluster.myself) { - addReplyErrorFormat(c,"I'm not the owner of hash slot %u",slot); - return; - } if (!strcasecmp(c->argv[3]->ptr,"migrating") && c->argc == 5) { + if (server.cluster.slots[slot] != server.cluster.myself) { + addReplyErrorFormat(c,"I'm not the owner of hash slot %u",slot); + return; + } if ((n = clusterLookupNode(c->argv[4]->ptr)) == NULL) { addReplyErrorFormat(c,"I don't know about node %s", (char*)c->argv[4]->ptr); @@ -1231,6 +1231,11 @@ void clusterCommand(redisClient *c) { } server.cluster.migrating_slots_to[slot] = n; } else if (!strcasecmp(c->argv[3]->ptr,"importing") && c->argc == 5) { + if (server.cluster.slots[slot] == server.cluster.myself) { + addReplyErrorFormat(c, + "I'm already the owner of hash slot %u",slot); + return; + } if ((n = clusterLookupNode(c->argv[4]->ptr)) == NULL) { addReplyErrorFormat(c,"I don't know about node %s", (char*)c->argv[3]->ptr); diff --git a/src/db.c b/src/db.c index 670e2bce..354f9045 100644 --- a/src/db.c +++ b/src/db.c @@ -317,7 +317,7 @@ void existsCommand(redisClient *c) { void selectCommand(redisClient *c) { int id = atoi(c->argv[1]->ptr); - if (server.cluster_enabled) { + if (server.cluster_enabled && id != 0) { addReplyError(c,"SELECT is not allowed in cluster mode"); return; } From f384df8302ccc6601f5833ae42e6b4664abd0ddc Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 6 May 2011 13:38:27 +0200 Subject: [PATCH 138/146] CLUSTER DELSLOTS --- src/cluster.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index d8eca519..5de05bce 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1014,11 +1014,24 @@ int clusterNodeGetSlotBit(clusterNode *n, int slot) { * If the slot is already assigned to another instance this is considered * an error and REDIS_ERR is returned. */ int clusterAddSlot(clusterNode *n, int slot) { - redisAssert(clusterNodeSetSlotBit(n,slot) == 0); + if (clusterNodeSetSlotBit(n,slot) != 0) + return REDIS_ERR; server.cluster.slots[slot] = n; return REDIS_OK; } +/* Delete the specified slot marking it as unassigned. + * Returns REDIS_OK if the slot was assigned, otherwise if the slot was + * already unassigned REDIS_ERR is returned. */ +int clusterDelSlot(int slot) { + clusterNode *n = server.cluster.slots[slot]; + + if (!n) return REDIS_ERR; + redisAssert(clusterNodeClearSlotBit(n,slot) == 1); + server.cluster.slots[slot] = NULL; + return REDIS_OK; +} + /* ----------------------------------------------------------------------------- * Cluster state evaluation function * -------------------------------------------------------------------------- */ @@ -1165,10 +1178,12 @@ void clusterCommand(redisClient *c) { o = createObject(REDIS_STRING,ci); addReplyBulk(c,o); decrRefCount(o); - } else if (!strcasecmp(c->argv[1]->ptr,"addslots") && c->argc >= 3) { + } else if ((!strcasecmp(c->argv[1]->ptr,"addslots") || + !strcasecmp(c->argv[1]->ptr,"delslots")) && c->argc >= 3) { int j; long long slot; unsigned char *slots = zmalloc(REDIS_CLUSTER_SLOTS); + int del = !strcasecmp(c->argv[1]->ptr,"delslots"); memset(slots,0,REDIS_CLUSTER_SLOTS); /* Check that all the arguments are parsable and that all the @@ -1181,7 +1196,11 @@ void clusterCommand(redisClient *c) { zfree(slots); return; } - if (server.cluster.slots[slot]) { + if (del && server.cluster.slots[slot] == NULL) { + addReplyErrorFormat(c,"Slot %lld is already unassigned", slot); + zfree(slots); + return; + } else if (!del && server.cluster.slots[slot]) { addReplyErrorFormat(c,"Slot %lld is already busy", slot); zfree(slots); return; @@ -1195,7 +1214,8 @@ void clusterCommand(redisClient *c) { } for (j = 0; j < REDIS_CLUSTER_SLOTS; j++) { if (slots[j]) { - int retval = clusterAddSlot(server.cluster.myself,j); + int retval = del ? clusterDelSlot(j) : + clusterAddSlot(server.cluster.myself,j); redisAssert(retval == REDIS_OK); } From f9cbdcb1a67e8903c976e588bbf24f0fe6bf2fac Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 6 May 2011 15:44:09 +0200 Subject: [PATCH 139/146] CLUSTER SETSLOT command and some refactoring of the cluster command --- src/cluster.c | 61 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 17 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index 5de05bce..2e9b256c 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1141,6 +1141,18 @@ sds clusterGenNodesDescription(void) { return ci; } +int getSlotOrReply(redisClient *c, robj *o) { + long long slot; + + if (getLongLongFromObject(o,&slot) != REDIS_OK || + slot < 0 || slot > REDIS_CLUSTER_SLOTS) + { + addReplyError(c,"Invalid or out of range slot"); + return -1; + } + return (int) slot; +} + void clusterCommand(redisClient *c) { if (server.cluster_enabled == 0) { addReplyError(c,"This instance has cluster support disabled"); @@ -1180,8 +1192,7 @@ void clusterCommand(redisClient *c) { decrRefCount(o); } else if ((!strcasecmp(c->argv[1]->ptr,"addslots") || !strcasecmp(c->argv[1]->ptr,"delslots")) && c->argc >= 3) { - int j; - long long slot; + int j, slot; unsigned char *slots = zmalloc(REDIS_CLUSTER_SLOTS); int del = !strcasecmp(c->argv[1]->ptr,"delslots"); @@ -1189,19 +1200,16 @@ void clusterCommand(redisClient *c) { /* Check that all the arguments are parsable and that all the * slots are not already busy. */ for (j = 2; j < c->argc; j++) { - if (getLongLongFromObject(c->argv[j],&slot) != REDIS_OK || - slot < 0 || slot > REDIS_CLUSTER_SLOTS) - { - addReplyError(c,"Invalid or out of range slot index"); + if ((slot = getSlotOrReply(c,c->argv[j])) == -1) { zfree(slots); return; } if (del && server.cluster.slots[slot] == NULL) { - addReplyErrorFormat(c,"Slot %lld is already unassigned", slot); + addReplyErrorFormat(c,"Slot %d is already unassigned", slot); zfree(slots); return; } else if (!del && server.cluster.slots[slot]) { - addReplyErrorFormat(c,"Slot %lld is already busy", slot); + addReplyErrorFormat(c,"Slot %d is already busy", slot); zfree(slots); return; } @@ -1228,17 +1236,11 @@ void clusterCommand(redisClient *c) { /* SETSLOT 10 MIGRATING */ /* SETSLOT 10 IMPORTING */ /* SETSLOT 10 STABLE */ - long long aux; - unsigned int slot; + int slot; clusterNode *n; - if (getLongLongFromObjectOrReply(c,c->argv[2],&aux,NULL) != REDIS_OK) - return; - if (aux < 0 || aux >= REDIS_CLUSTER_SLOTS) { - addReplyError(c,"Slot out of range"); - return; - } - slot = (unsigned int) aux; + if ((slot = getSlotOrReply(c,c->argv[2])) == -1) return; + if (!strcasecmp(c->argv[3]->ptr,"migrating") && c->argc == 5) { if (server.cluster.slots[slot] != server.cluster.myself) { addReplyErrorFormat(c,"I'm not the owner of hash slot %u",slot); @@ -1263,8 +1265,33 @@ void clusterCommand(redisClient *c) { } server.cluster.importing_slots_from[slot] = n; } else if (!strcasecmp(c->argv[3]->ptr,"stable") && c->argc == 4) { + /* CLUSTER SETSLOT STABLE */ server.cluster.importing_slots_from[slot] = NULL; server.cluster.migrating_slots_to[slot] = NULL; + } else if (!strcasecmp(c->argv[3]->ptr,"node") && c->argc == 4) { + /* CLUSTER SETSLOT NODE */ + clusterNode *n = clusterLookupNode(c->argv[4]->ptr); + + if (!n) addReplyErrorFormat(c,"Unknown node %s", + (char*)c->argv[4]->ptr); + /* If this hash slot was served by 'myself' before to switch + * make sure there are no longer local keys for this hash slot. */ + if (server.cluster.slots[slot] == server.cluster.myself && + n != server.cluster.myself) + { + int numkeys; + robj **keys; + + keys = zmalloc(sizeof(robj*)*1); + numkeys = GetKeysInSlot(slot, keys, 1); + zfree(keys); + if (numkeys == 0) { + addReplyErrorFormat(c, "Can't assign hashslot %d to a different node while I still hold keys for this hash slot.", slot); + return; + } + } + clusterDelSlot(slot); + clusterAddSlot(n,slot); } else { addReplyError(c,"Invalid CLUSTER SETSLOT action or number of arguments"); return; From 0caa75074875ca5c13274accdd032c90db93b3c1 Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 6 May 2011 16:08:10 +0200 Subject: [PATCH 140/146] clear importing/exporing state when appropriate on SETSLOT or ADDSLOTS cluster commands --- src/cluster.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index 2e9b256c..3e65af78 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1222,9 +1222,15 @@ void clusterCommand(redisClient *c) { } for (j = 0; j < REDIS_CLUSTER_SLOTS; j++) { if (slots[j]) { - int retval = del ? clusterDelSlot(j) : - clusterAddSlot(server.cluster.myself,j); - + int retval; + + /* If this slot was set as importing we can clear this + * state as now we are the real owner of the slot. */ + if (server.cluster.importing_slots_from[j]) + server.cluster.importing_slots_from[j] = NULL; + + retval = del ? clusterDelSlot(j) : + clusterAddSlot(server.cluster.myself,j); redisAssert(retval == REDIS_OK); } } @@ -1290,6 +1296,13 @@ void clusterCommand(redisClient *c) { return; } } + /* If this node was the slot owner and the slot was marked as + * migrating, assigning the slot to another node will clear + * the migratig status. */ + if (server.cluster.slots[slot] == server.cluster.myself && + server.cluster.migrating_slots_to[slot]) + server.cluster.migrating_slots_to[slot] = NULL; + clusterDelSlot(slot); clusterAddSlot(n,slot); } else { From 8905378c569ba16e8636bc47404c97b952240096 Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 6 May 2011 16:23:14 +0200 Subject: [PATCH 141/146] Cluster TODO updated --- TODO | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/TODO b/TODO index 8f0a5db2..dd6ba28b 100644 --- a/TODO +++ b/TODO @@ -17,6 +17,17 @@ API CHANGES the variable number of arguments represent values, and there is no conflict with the return value of the command. +CLUSTER +======= + +* Implement rehashing and cluster check in redis-trib. +* Reimplement MIGRATE / RESTORE to use just in memory buffers (no disk at + all). This will require touching a lot of the RDB stuff around, but we may + hand with faster persistence for RDB. +* Implement the slave nodes semantics and election. +* Allow redis-trib to create a cluster-wide snapshot (using SYNC). +* Allow redis-trib to restore a cluster-wide snapshot (implement UPLOAD?). + APPEND ONLY FILE ================ From df541beae372f40e9ce82a0780171e7d7a8e67de Mon Sep 17 00:00:00 2001 From: antirez Date: Sat, 7 May 2011 11:47:34 +0200 Subject: [PATCH 142/146] when Redis fails accepting a new connection reports the error at WARNING and not VERBOSE error level. Thanks to offby1 for proposing this in the Redis mailing list. #backport-candidate --- src/networking.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/networking.c b/src/networking.c index 50a2cef2..4a6a8afd 100644 --- a/src/networking.c +++ b/src/networking.c @@ -419,7 +419,7 @@ void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) { cfd = anetTcpAccept(server.neterr, fd, cip, &cport); if (cfd == AE_ERR) { - redisLog(REDIS_VERBOSE,"Accepting client connection: %s", server.neterr); + redisLog(REDIS_WARNING,"Accepting client connection: %s", server.neterr); return; } redisLog(REDIS_VERBOSE,"Accepted %s:%d", cip, cport); @@ -434,7 +434,7 @@ void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask) { cfd = anetUnixAccept(server.neterr, fd); if (cfd == AE_ERR) { - redisLog(REDIS_VERBOSE,"Accepting client connection: %s", server.neterr); + redisLog(REDIS_WARNING,"Accepting client connection: %s", server.neterr); return; } redisLog(REDIS_VERBOSE,"Accepted connection to %s", server.unixsocket); From 6901fe7729d31c07e8053d9fd29bde964050679c Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 10 May 2011 10:07:04 +0200 Subject: [PATCH 143/146] Useless statement removed --- src/aof.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aof.c b/src/aof.c index e5327cb2..ef72a2b1 100644 --- a/src/aof.c +++ b/src/aof.c @@ -348,7 +348,7 @@ int rewriteAppendOnlyFile(char *filename) { /* Iterate this DB writing every entry */ while((de = dictNext(di)) != NULL) { - sds keystr = dictGetEntryKey(de); + sds keystr; robj key, *o; time_t expiretime; From f13cb0d9de088898cd879b3912ea3e3300fc9fdf Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 10 May 2011 10:08:01 +0200 Subject: [PATCH 144/146] DEBUG DIGEST additional lookup needed for VM removed from unstable branch that does not have VM at all --- src/debug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/debug.c b/src/debug.c index 080e2b2e..170bffa5 100644 --- a/src/debug.c +++ b/src/debug.c @@ -100,7 +100,7 @@ void computeDatasetDigest(unsigned char *final) { mixDigest(digest,key,sdslen(key)); /* Make sure the key is loaded if VM is active */ - o = lookupKeyRead(db,keyobj); + o = dictGetEntryVal(de); aux = htonl(o->type); mixDigest(digest,&aux,sizeof(aux)); From 4b53e7365c647235bbb3909596a7defe50709b67 Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 10 May 2011 10:15:50 +0200 Subject: [PATCH 145/146] Introduced a safe iterator interface that can be used to iterate while accessing the dictionary at the same time. Now the default interface is consireded unsafe and should be used only with dictNext() --- src/dict.c | 20 +++++++++++++++----- src/dict.h | 8 ++++++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/dict.c b/src/dict.c index 6b7010ba..24001fdd 100644 --- a/src/dict.c +++ b/src/dict.c @@ -245,9 +245,9 @@ int dictRehashMilliseconds(dict *d, int ms) { } /* This function performs just a step of rehashing, and only if there are - * not iterators bound to our hash table. When we have iterators in the middle - * of a rehashing we can't mess with the two hash tables otherwise some element - * can be missed or duplicated. + * no safe iterators bound to our hash table. When we have iterators in the + * middle of a rehashing we can't mess with the two hash tables otherwise + * some element can be missed or duplicated. * * This function is called by common lookup or update operations in the * dictionary so that the hash table automatically migrates from H1 to H2 @@ -424,17 +424,26 @@ dictIterator *dictGetIterator(dict *d) iter->d = d; iter->table = 0; iter->index = -1; + iter->safe = 0; iter->entry = NULL; iter->nextEntry = NULL; return iter; } +dictIterator *dictGetSafeIterator(dict *d) { + dictIterator *i = dictGetIterator(d); + + i->safe = 1; + return i; +} + dictEntry *dictNext(dictIterator *iter) { while (1) { if (iter->entry == NULL) { dictht *ht = &iter->d->ht[iter->table]; - if (iter->index == -1 && iter->table == 0) iter->d->iterators++; + if (iter->safe && iter->index == -1 && iter->table == 0) + iter->d->iterators++; iter->index++; if (iter->index >= (signed) ht->size) { if (dictIsRehashing(iter->d) && iter->table == 0) { @@ -461,7 +470,8 @@ dictEntry *dictNext(dictIterator *iter) void dictReleaseIterator(dictIterator *iter) { - if (!(iter->index == -1 && iter->table == 0)) iter->d->iterators--; + if (iter->safe && !(iter->index == -1 && iter->table == 0)) + iter->d->iterators--; zfree(iter); } diff --git a/src/dict.h b/src/dict.h index 25cce4e5..74bcd2aa 100644 --- a/src/dict.h +++ b/src/dict.h @@ -74,10 +74,13 @@ typedef struct dict { int iterators; /* number of iterators currently running */ } dict; +/* If safe is set to 1 this is a safe iteartor, that means, you can call + * dictAdd, dictFind, and other functions against the dictionary even while + * iterating. Otherwise it is a non safe iterator, and only dictNext() + * should be called while iterating. */ typedef struct dictIterator { dict *d; - int table; - int index; + int table, index, safe; dictEntry *entry, *nextEntry; } dictIterator; @@ -132,6 +135,7 @@ dictEntry * dictFind(dict *d, const void *key); void *dictFetchValue(dict *d, const void *key); int dictResize(dict *d); dictIterator *dictGetIterator(dict *d); +dictIterator *dictGetSafeIterator(dict *d); dictEntry *dictNext(dictIterator *iter); void dictReleaseIterator(dictIterator *iter); dictEntry *dictGetRandomKey(dict *d); From 6b52ad87c05ca2162a2d21f1f5b5329bf52a7678 Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 10 May 2011 11:31:37 +0200 Subject: [PATCH 146/146] RDB version signature bumped to 2, now that direct saving of specially encoded types is implemented 2.2 instances are no longer able to read rdb files produced by 2.4 or unstable. --- src/rdb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rdb.c b/src/rdb.c index 2557f5b8..eeafc053 100644 --- a/src/rdb.c +++ b/src/rdb.c @@ -425,7 +425,7 @@ int rdbSave(char *filename) { strerror(errno)); return REDIS_ERR; } - if (fwrite("REDIS0001",9,1,fp) == 0) goto werr; + if (fwrite("REDIS0002",9,1,fp) == 0) goto werr; for (j = 0; j < server.dbnum; j++) { redisDb *db = server.db+j; dict *d = db->dict; @@ -931,7 +931,7 @@ int rdbLoad(char *filename) { return REDIS_ERR; } rdbver = atoi(buf+5); - if (rdbver != 1) { + if (rdbver < 1 || rdbver > 2) { fclose(fp); redisLog(REDIS_WARNING,"Can't handle RDB format version %d",rdbver); return REDIS_ERR;