1 /* 2 * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * * Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * * Neither the name of Redis nor the names of its contributors may be used 14 * to endorse or promote products derived from this software without 15 * specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include "redis.h" 31 #include "cluster.h" 32 33 #include <signal.h> 34 #include <ctype.h> 35 36 void slotToKeyAdd(robj *key); 37 void slotToKeyDel(robj *key); 38 void slotToKeyFlush(void); 39 40 /*----------------------------------------------------------------------------- 41 * C-level DB API 42 *----------------------------------------------------------------------------*/ 43 44 robj *lookupKey(redisDb *db, robj *key) { 45 dictEntry *de = dictFind(db->dict,key->ptr); 46 if (de) { 47 robj *val = dictGetVal(de); 48 49 /* Update the access time for the ageing algorithm. 50 * Don't do it if we have a saving child, as this will trigger 51 * a copy on write madness. */ 52 if (server.rdb_child_pid == -1 && server.aof_child_pid == -1) 53 val->lru = LRU_CLOCK(); 54 return val; 55 } else { 56 return NULL; 57 } 58 } 59 60 robj *lookupKeyRead(redisDb *db, robj *key) { 61 robj *val; 62 63 if (expireIfNeeded(db,key) == 1) { 64 /* Key expired. If we are in the context of a master, expireIfNeeded() 65 * returns 0 only when the key does not exist at all, so it's save 66 * to return NULL ASAP. */ 67 if (server.masterhost == NULL) return NULL; 68 69 /* However if we are in the context of a slave, expireIfNeeded() will 70 * not really try to expire the key, it only returns information 71 * about the "logical" status of the key: key expiring is up to the 72 * master in order to have a consistent view of master's data set. 73 * 74 * However, if the command caller is not the master, and as additional 75 * safety measure, the command invoked is a read-only command, we can 76 * safely return NULL here, and provide a more consistent behavior 77 * to clients accessign expired values in a read-only fashion, that 78 * will say the key as non exisitng. 79 * 80 * Notably this covers GETs when slaves are used to scale reads. */ 81 if (server.current_client && 82 server.current_client != server.master && 83 server.current_client->cmd && 84 server.current_client->cmd->flags & REDIS_CMD_READONLY) 85 { 86 return NULL; 87 } 88 } 89 val = lookupKey(db,key); 90 if (val == NULL) 91 server.stat_keyspace_misses++; 92 else 93 server.stat_keyspace_hits++; 94 return val; 95 } 96 97 robj *lookupKeyWrite(redisDb *db, robj *key) { 98 expireIfNeeded(db,key); 99 return lookupKey(db,key); 100 } 101 102 robj *lookupKeyReadOrReply(redisClient *c, robj *key, robj *reply) { 103 robj *o = lookupKeyRead(c->db, key); 104 if (!o) addReply(c,reply); 105 return o; 106 } 107 108 robj *lookupKeyWriteOrReply(redisClient *c, robj *key, robj *reply) { 109 robj *o = lookupKeyWrite(c->db, key); 110 if (!o) addReply(c,reply); 111 return o; 112 } 113 114 /* Add the key to the DB. It's up to the caller to increment the reference 115 * counter of the value if needed. 116 * 117 * The program is aborted if the key already exists. */ 118 void dbAdd(redisDb *db, robj *key, robj *val) { 119 sds copy = sdsdup(key->ptr); 120 int retval = dictAdd(db->dict, copy, val); 121 122 redisAssertWithInfo(NULL,key,retval == REDIS_OK); 123 if (val->type == REDIS_LIST) signalListAsReady(db, key); 124 if (server.cluster_enabled) slotToKeyAdd(key); 125 } 126 127 /* Overwrite an existing key with a new value. Incrementing the reference 128 * count of the new value is up to the caller. 129 * This function does not modify the expire time of the existing key. 130 * 131 * The program is aborted if the key was not already present. */ 132 void dbOverwrite(redisDb *db, robj *key, robj *val) { 133 dictEntry *de = dictFind(db->dict,key->ptr); 134 135 redisAssertWithInfo(NULL,key,de != NULL); 136 dictReplace(db->dict, key->ptr, val); 137 } 138 139 /* High level Set operation. This function can be used in order to set 140 * a key, whatever it was existing or not, to a new object. 141 * 142 * 1) The ref count of the value object is incremented. 143 * 2) clients WATCHing for the destination key notified. 144 * 3) The expire time of the key is reset (the key is made persistent). */ 145 void setKey(redisDb *db, robj *key, robj *val) { 146 if (lookupKeyWrite(db,key) == NULL) { 147 dbAdd(db,key,val); 148 } else { 149 dbOverwrite(db,key,val); 150 } 151 incrRefCount(val); 152 removeExpire(db,key); 153 signalModifiedKey(db,key); 154 } 155 156 int dbExists(redisDb *db, robj *key) { 157 return dictFind(db->dict,key->ptr) != NULL; 158 } 159 160 /* Return a random key, in form of a Redis object. 161 * If there are no keys, NULL is returned. 162 * 163 * The function makes sure to return keys not already expired. */ 164 robj *dbRandomKey(redisDb *db) { 165 dictEntry *de; 166 167 while(1) { 168 sds key; 169 robj *keyobj; 170 171 de = dictGetRandomKey(db->dict); 172 if (de == NULL) return NULL; 173 174 key = dictGetKey(de); 175 keyobj = createStringObject(key,sdslen(key)); 176 if (dictFind(db->expires,key)) { 177 if (expireIfNeeded(db,keyobj)) { 178 decrRefCount(keyobj); 179 continue; /* search for another key. This expired. */ 180 } 181 } 182 return keyobj; 183 } 184 } 185 186 /* Delete a key, value, and associated expiration entry if any, from the DB */ 187 int dbDelete(redisDb *db, robj *key) { 188 /* Deleting an entry from the expires dict will not free the sds of 189 * the key, because it is shared with the main dictionary. */ 190 if (dictSize(db->expires) > 0) dictDelete(db->expires,key->ptr); 191 if (dictDelete(db->dict,key->ptr) == DICT_OK) { 192 if (server.cluster_enabled) slotToKeyDel(key); 193 return 1; 194 } else { 195 return 0; 196 } 197 } 198 199 /* Prepare the string object stored at 'key' to be modified destructively 200 * to implement commands like SETBIT or APPEND. 201 * 202 * An object is usually ready to be modified unless one of the two conditions 203 * are true: 204 * 205 * 1) The object 'o' is shared (refcount > 1), we don't want to affect 206 * other users. 207 * 2) The object encoding is not "RAW". 208 * 209 * If the object is found in one of the above conditions (or both) by the 210 * function, an unshared / not-encoded copy of the string object is stored 211 * at 'key' in the specified 'db'. Otherwise the object 'o' itself is 212 * returned. 213 * 214 * USAGE: 215 * 216 * The object 'o' is what the caller already obtained by looking up 'key' 217 * in 'db', the usage pattern looks like this: 218 * 219 * o = lookupKeyWrite(db,key); 220 * if (checkType(c,o,REDIS_STRING)) return; 221 * o = dbUnshareStringValue(db,key,o); 222 * 223 * At this point the caller is ready to modify the object, for example 224 * using an sdscat() call to append some data, or anything else. 225 */ 226 robj *dbUnshareStringValue(redisDb *db, robj *key, robj *o) { 227 redisAssert(o->type == REDIS_STRING); 228 if (o->refcount != 1 || o->encoding != REDIS_ENCODING_RAW) { 229 robj *decoded = getDecodedObject(o); 230 o = createRawStringObject(decoded->ptr, sdslen(decoded->ptr)); 231 decrRefCount(decoded); 232 dbOverwrite(db,key,o); 233 } 234 return o; 235 } 236 237 long long emptyDb(void(callback)(void*)) { 238 int j; 239 long long removed = 0; 240 241 for (j = 0; j < server.dbnum; j++) { 242 removed += dictSize(server.db[j].dict); 243 dictEmpty(server.db[j].dict,callback); 244 dictEmpty(server.db[j].expires,callback); 245 } 246 if (server.cluster_enabled) slotToKeyFlush(); 247 return removed; 248 } 249 250 int selectDb(redisClient *c, int id) { 251 if (id < 0 || id >= server.dbnum) 252 return REDIS_ERR; 253 c->db = &server.db[id]; 254 return REDIS_OK; 255 } 256 257 /*----------------------------------------------------------------------------- 258 * Hooks for key space changes. 259 * 260 * Every time a key in the database is modified the function 261 * signalModifiedKey() is called. 262 * 263 * Every time a DB is flushed the function signalFlushDb() is called. 264 *----------------------------------------------------------------------------*/ 265 266 void signalModifiedKey(redisDb *db, robj *key) { 267 touchWatchedKey(db,key); 268 } 269 270 void signalFlushedDb(int dbid) { 271 touchWatchedKeysOnFlush(dbid); 272 } 273 274 /*----------------------------------------------------------------------------- 275 * Type agnostic commands operating on the key space 276 *----------------------------------------------------------------------------*/ 277 278 void flushdbCommand(redisClient *c) { 279 server.dirty += dictSize(c->db->dict); 280 signalFlushedDb(c->db->id); 281 dictEmpty(c->db->dict,NULL); 282 dictEmpty(c->db->expires,NULL); 283 if (server.cluster_enabled) slotToKeyFlush(); 284 addReply(c,shared.ok); 285 } 286 287 void flushallCommand(redisClient *c) { 288 signalFlushedDb(-1); 289 server.dirty += emptyDb(NULL); 290 addReply(c,shared.ok); 291 if (server.rdb_child_pid != -1) { 292 kill(server.rdb_child_pid,SIGUSR1); 293 rdbRemoveTempFile(server.rdb_child_pid); 294 } 295 if (server.saveparamslen > 0) { 296 /* Normally rdbSave() will reset dirty, but we don't want this here 297 * as otherwise FLUSHALL will not be replicated nor put into the AOF. */ 298 int saved_dirty = server.dirty; 299 rdbSave(server.rdb_filename); 300 server.dirty = saved_dirty; 301 } 302 server.dirty++; 303 } 304 305 void delCommand(redisClient *c) { 306 int deleted = 0, j; 307 308 for (j = 1; j < c->argc; j++) { 309 expireIfNeeded(c->db,c->argv[j]); 310 if (dbDelete(c->db,c->argv[j])) { 311 signalModifiedKey(c->db,c->argv[j]); 312 notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC, 313 "del",c->argv[j],c->db->id); 314 server.dirty++; 315 deleted++; 316 } 317 } 318 addReplyLongLong(c,deleted); 319 } 320 321 void existsCommand(redisClient *c) { 322 expireIfNeeded(c->db,c->argv[1]); 323 if (dbExists(c->db,c->argv[1])) { 324 addReply(c, shared.cone); 325 } else { 326 addReply(c, shared.czero); 327 } 328 } 329 330 void selectCommand(redisClient *c) { 331 long id; 332 333 if (getLongFromObjectOrReply(c, c->argv[1], &id, 334 "invalid DB index") != REDIS_OK) 335 return; 336 337 if (server.cluster_enabled && id != 0) { 338 addReplyError(c,"SELECT is not allowed in cluster mode"); 339 return; 340 } 341 if (selectDb(c,id) == REDIS_ERR) { 342 addReplyError(c,"invalid DB index"); 343 } else { 344 addReply(c,shared.ok); 345 } 346 } 347 348 void randomkeyCommand(redisClient *c) { 349 robj *key; 350 351 if ((key = dbRandomKey(c->db)) == NULL) { 352 addReply(c,shared.nullbulk); 353 return; 354 } 355 356 addReplyBulk(c,key); 357 decrRefCount(key); 358 } 359 360 void keysCommand(redisClient *c) { 361 dictIterator *di; 362 dictEntry *de; 363 sds pattern = c->argv[1]->ptr; 364 int plen = sdslen(pattern), allkeys; 365 unsigned long numkeys = 0; 366 void *replylen = addDeferredMultiBulkLength(c); 367 368 di = dictGetSafeIterator(c->db->dict); 369 allkeys = (pattern[0] == '*' && pattern[1] == '\0'); 370 while((de = dictNext(di)) != NULL) { 371 sds key = dictGetKey(de); 372 robj *keyobj; 373 374 if (allkeys || stringmatchlen(pattern,plen,key,sdslen(key),0)) { 375 keyobj = createStringObject(key,sdslen(key)); 376 if (expireIfNeeded(c->db,keyobj) == 0) { 377 addReplyBulk(c,keyobj); 378 numkeys++; 379 } 380 decrRefCount(keyobj); 381 } 382 } 383 dictReleaseIterator(di); 384 setDeferredMultiBulkLength(c,replylen,numkeys); 385 } 386 387 /* This callback is used by scanGenericCommand in order to collect elements 388 * returned by the dictionary iterator into a list. */ 389 void scanCallback(void *privdata, const dictEntry *de) { 390 void **pd = (void**) privdata; 391 list *keys = pd[0]; 392 robj *o = pd[1]; 393 robj *key, *val = NULL; 394 395 if (o == NULL) { 396 sds sdskey = dictGetKey(de); 397 key = createStringObject(sdskey, sdslen(sdskey)); 398 } else if (o->type == REDIS_SET) { 399 key = dictGetKey(de); 400 incrRefCount(key); 401 } else if (o->type == REDIS_HASH) { 402 key = dictGetKey(de); 403 incrRefCount(key); 404 val = dictGetVal(de); 405 incrRefCount(val); 406 } else if (o->type == REDIS_ZSET) { 407 key = dictGetKey(de); 408 incrRefCount(key); 409 val = createStringObjectFromLongDouble(*(double*)dictGetVal(de),0); 410 } else { 411 redisPanic("Type not handled in SCAN callback."); 412 } 413 414 listAddNodeTail(keys, key); 415 if (val) listAddNodeTail(keys, val); 416 } 417 418 /* Try to parse a SCAN cursor stored at object 'o': 419 * if the cursor is valid, store it as unsigned integer into *cursor and 420 * returns REDIS_OK. Otherwise return REDIS_ERR and send an error to the 421 * client. */ 422 int parseScanCursorOrReply(redisClient *c, robj *o, unsigned long *cursor) { 423 char *eptr; 424 425 /* Use strtoul() because we need an *unsigned* long, so 426 * getLongLongFromObject() does not cover the whole cursor space. */ 427 errno = 0; 428 *cursor = strtoul(o->ptr, &eptr, 10); 429 if (isspace(((char*)o->ptr)[0]) || eptr[0] != '\0' || errno == ERANGE) 430 { 431 addReplyError(c, "invalid cursor"); 432 return REDIS_ERR; 433 } 434 return REDIS_OK; 435 } 436 437 /* This command implements SCAN, HSCAN and SSCAN commands. 438 * If object 'o' is passed, then it must be a Hash or Set object, otherwise 439 * if 'o' is NULL the command will operate on the dictionary associated with 440 * the current database. 441 * 442 * When 'o' is not NULL the function assumes that the first argument in 443 * the client arguments vector is a key so it skips it before iterating 444 * in order to parse options. 445 * 446 * In the case of a Hash object the function returns both the field and value 447 * of every element on the Hash. */ 448 void scanGenericCommand(redisClient *c, robj *o, unsigned long cursor) { 449 int i, j; 450 list *keys = listCreate(); 451 listNode *node, *nextnode; 452 long count = 10; 453 sds pat = NULL; 454 int patlen = 0, use_pattern = 0; 455 dict *ht; 456 457 /* Object must be NULL (to iterate keys names), or the type of the object 458 * must be Set, Sorted Set, or Hash. */ 459 redisAssert(o == NULL || o->type == REDIS_SET || o->type == REDIS_HASH || 460 o->type == REDIS_ZSET); 461 462 /* Set i to the first option argument. The previous one is the cursor. */ 463 i = (o == NULL) ? 2 : 3; /* Skip the key argument if needed. */ 464 465 /* Step 1: Parse options. */ 466 while (i < c->argc) { 467 j = c->argc - i; 468 if (!strcasecmp(c->argv[i]->ptr, "count") && j >= 2) { 469 if (getLongFromObjectOrReply(c, c->argv[i+1], &count, NULL) 470 != REDIS_OK) 471 { 472 goto cleanup; 473 } 474 475 if (count < 1) { 476 addReply(c,shared.syntaxerr); 477 goto cleanup; 478 } 479 480 i += 2; 481 } else if (!strcasecmp(c->argv[i]->ptr, "match") && j >= 2) { 482 pat = c->argv[i+1]->ptr; 483 patlen = sdslen(pat); 484 485 /* The pattern always matches if it is exactly "*", so it is 486 * equivalent to disabling it. */ 487 use_pattern = !(pat[0] == '*' && patlen == 1); 488 489 i += 2; 490 } else { 491 addReply(c,shared.syntaxerr); 492 goto cleanup; 493 } 494 } 495 496 /* Step 2: Iterate the collection. 497 * 498 * Note that if the object is encoded with a ziplist, intset, or any other 499 * representation that is not a hash table, we are sure that it is also 500 * composed of a small number of elements. So to avoid taking state we 501 * just return everything inside the object in a single call, setting the 502 * cursor to zero to signal the end of the iteration. */ 503 504 /* Handle the case of a hash table. */ 505 ht = NULL; 506 if (o == NULL) { 507 ht = c->db->dict; 508 } else if (o->type == REDIS_SET && o->encoding == REDIS_ENCODING_HT) { 509 ht = o->ptr; 510 } else if (o->type == REDIS_HASH && o->encoding == REDIS_ENCODING_HT) { 511 ht = o->ptr; 512 count *= 2; /* We return key / value for this type. */ 513 } else if (o->type == REDIS_ZSET && o->encoding == REDIS_ENCODING_SKIPLIST) { 514 zset *zs = o->ptr; 515 ht = zs->dict; 516 count *= 2; /* We return key / value for this type. */ 517 } 518 519 if (ht) { 520 void *privdata[2]; 521 /* We set the max number of iterations to ten times the specified 522 * COUNT, so if the hash table is in a pathological state (very 523 * sparsely populated) we avoid to block too much time at the cost 524 * of returning no or very few elements. */ 525 long maxiterations = count*10; 526 527 /* We pass two pointers to the callback: the list to which it will 528 * add new elements, and the object containing the dictionary so that 529 * it is possible to fetch more data in a type-dependent way. */ 530 privdata[0] = keys; 531 privdata[1] = o; 532 do { 533 cursor = dictScan(ht, cursor, scanCallback, privdata); 534 } while (cursor && 535 maxiterations-- && 536 listLength(keys) < (unsigned long)count); 537 } else if (o->type == REDIS_SET) { 538 int pos = 0; 539 int64_t ll; 540 541 while(intsetGet(o->ptr,pos++,&ll)) 542 listAddNodeTail(keys,createStringObjectFromLongLong(ll)); 543 cursor = 0; 544 } else if (o->type == REDIS_HASH || o->type == REDIS_ZSET) { 545 unsigned char *p = ziplistIndex(o->ptr,0); 546 unsigned char *vstr; 547 unsigned int vlen; 548 long long vll; 549 550 while(p) { 551 ziplistGet(p,&vstr,&vlen,&vll); 552 listAddNodeTail(keys, 553 (vstr != NULL) ? createStringObject((char*)vstr,vlen) : 554 createStringObjectFromLongLong(vll)); 555 p = ziplistNext(o->ptr,p); 556 } 557 cursor = 0; 558 } else { 559 redisPanic("Not handled encoding in SCAN."); 560 } 561 562 /* Step 3: Filter elements. */ 563 node = listFirst(keys); 564 while (node) { 565 robj *kobj = listNodeValue(node); 566 nextnode = listNextNode(node); 567 int filter = 0; 568 569 /* Filter element if it does not match the pattern. */ 570 if (!filter && use_pattern) { 571 if (sdsEncodedObject(kobj)) { 572 if (!stringmatchlen(pat, patlen, kobj->ptr, sdslen(kobj->ptr), 0)) 573 filter = 1; 574 } else { 575 char buf[REDIS_LONGSTR_SIZE]; 576 int len; 577 578 redisAssert(kobj->encoding == REDIS_ENCODING_INT); 579 len = ll2string(buf,sizeof(buf),(long)kobj->ptr); 580 if (!stringmatchlen(pat, patlen, buf, len, 0)) filter = 1; 581 } 582 } 583 584 /* Filter element if it is an expired key. */ 585 if (!filter && o == NULL && expireIfNeeded(c->db, kobj)) filter = 1; 586 587 /* Remove the element and its associted value if needed. */ 588 if (filter) { 589 decrRefCount(kobj); 590 listDelNode(keys, node); 591 } 592 593 /* If this is a hash or a sorted set, we have a flat list of 594 * key-value elements, so if this element was filtered, remove the 595 * value, or skip it if it was not filtered: we only match keys. */ 596 if (o && (o->type == REDIS_ZSET || o->type == REDIS_HASH)) { 597 node = nextnode; 598 nextnode = listNextNode(node); 599 if (filter) { 600 kobj = listNodeValue(node); 601 decrRefCount(kobj); 602 listDelNode(keys, node); 603 } 604 } 605 node = nextnode; 606 } 607 608 /* Step 4: Reply to the client. */ 609 addReplyMultiBulkLen(c, 2); 610 addReplyBulkLongLong(c,cursor); 611 612 addReplyMultiBulkLen(c, listLength(keys)); 613 while ((node = listFirst(keys)) != NULL) { 614 robj *kobj = listNodeValue(node); 615 addReplyBulk(c, kobj); 616 decrRefCount(kobj); 617 listDelNode(keys, node); 618 } 619 620 cleanup: 621 listSetFreeMethod(keys,decrRefCountVoid); 622 listRelease(keys); 623 } 624 625 /* The SCAN command completely relies on scanGenericCommand. */ 626 void scanCommand(redisClient *c) { 627 unsigned long cursor; 628 if (parseScanCursorOrReply(c,c->argv[1],&cursor) == REDIS_ERR) return; 629 scanGenericCommand(c,NULL,cursor); 630 } 631 632 void dbsizeCommand(redisClient *c) { 633 addReplyLongLong(c,dictSize(c->db->dict)); 634 } 635 636 void lastsaveCommand(redisClient *c) { 637 addReplyLongLong(c,server.lastsave); 638 } 639 640 void typeCommand(redisClient *c) { 641 robj *o; 642 char *type; 643 644 o = lookupKeyRead(c->db,c->argv[1]); 645 if (o == NULL) { 646 type = "none"; 647 } else { 648 switch(o->type) { 649 case REDIS_STRING: type = "string"; break; 650 case REDIS_LIST: type = "list"; break; 651 case REDIS_SET: type = "set"; break; 652 case REDIS_ZSET: type = "zset"; break; 653 case REDIS_HASH: type = "hash"; break; 654 default: type = "unknown"; break; 655 } 656 } 657 addReplyStatus(c,type); 658 } 659 660 void shutdownCommand(redisClient *c) { 661 int flags = 0; 662 663 if (c->argc > 2) { 664 addReply(c,shared.syntaxerr); 665 return; 666 } else if (c->argc == 2) { 667 if (!strcasecmp(c->argv[1]->ptr,"nosave")) { 668 flags |= REDIS_SHUTDOWN_NOSAVE; 669 } else if (!strcasecmp(c->argv[1]->ptr,"save")) { 670 flags |= REDIS_SHUTDOWN_SAVE; 671 } else { 672 addReply(c,shared.syntaxerr); 673 return; 674 } 675 } 676 /* When SHUTDOWN is called while the server is loading a dataset in 677 * memory we need to make sure no attempt is performed to save 678 * the dataset on shutdown (otherwise it could overwrite the current DB 679 * with half-read data). 680 * 681 * Also when in Sentinel mode clear the SAVE flag and force NOSAVE. */ 682 if (server.loading || server.sentinel_mode) 683 flags = (flags & ~REDIS_SHUTDOWN_SAVE) | REDIS_SHUTDOWN_NOSAVE; 684 if (prepareForShutdown(flags) == REDIS_OK) exit(0); 685 addReplyError(c,"Errors trying to SHUTDOWN. Check logs."); 686 } 687 688 void renameGenericCommand(redisClient *c, int nx) { 689 robj *o; 690 long long expire; 691 int samekey = 0; 692 693 /* When source and dest key is the same, no operation is performed, 694 * if the key exists, however we still return an error on unexisting key. */ 695 if (sdscmp(c->argv[1]->ptr,c->argv[2]->ptr) == 0) samekey = 1; 696 697 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr)) == NULL) 698 return; 699 700 if (samekey) { 701 addReply(c,nx ? shared.czero : shared.ok); 702 return; 703 } 704 705 incrRefCount(o); 706 expire = getExpire(c->db,c->argv[1]); 707 if (lookupKeyWrite(c->db,c->argv[2]) != NULL) { 708 if (nx) { 709 decrRefCount(o); 710 addReply(c,shared.czero); 711 return; 712 } 713 /* Overwrite: delete the old key before creating the new one 714 * with the same name. */ 715 dbDelete(c->db,c->argv[2]); 716 } 717 dbAdd(c->db,c->argv[2],o); 718 if (expire != -1) setExpire(c->db,c->argv[2],expire); 719 dbDelete(c->db,c->argv[1]); 720 signalModifiedKey(c->db,c->argv[1]); 721 signalModifiedKey(c->db,c->argv[2]); 722 notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"rename_from", 723 c->argv[1],c->db->id); 724 notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"rename_to", 725 c->argv[2],c->db->id); 726 server.dirty++; 727 addReply(c,nx ? shared.cone : shared.ok); 728 } 729 730 void renameCommand(redisClient *c) { 731 renameGenericCommand(c,0); 732 } 733 734 void renamenxCommand(redisClient *c) { 735 renameGenericCommand(c,1); 736 } 737 738 void moveCommand(redisClient *c) { 739 robj *o; 740 redisDb *src, *dst; 741 int srcid; 742 long long dbid; 743 744 if (server.cluster_enabled) { 745 addReplyError(c,"MOVE is not allowed in cluster mode"); 746 return; 747 } 748 749 /* Obtain source and target DB pointers */ 750 src = c->db; 751 srcid = c->db->id; 752 753 if (getLongLongFromObject(c->argv[2],&dbid) == REDIS_ERR || 754 dbid < INT_MIN || dbid > INT_MAX || 755 selectDb(c,dbid) == REDIS_ERR) 756 { 757 addReply(c,shared.outofrangeerr); 758 return; 759 } 760 dst = c->db; 761 selectDb(c,srcid); /* Back to the source DB */ 762 763 /* If the user is moving using as target the same 764 * DB as the source DB it is probably an error. */ 765 if (src == dst) { 766 addReply(c,shared.sameobjecterr); 767 return; 768 } 769 770 /* Check if the element exists and get a reference */ 771 o = lookupKeyWrite(c->db,c->argv[1]); 772 if (!o) { 773 addReply(c,shared.czero); 774 return; 775 } 776 777 /* Return zero if the key already exists in the target DB */ 778 if (lookupKeyWrite(dst,c->argv[1]) != NULL) { 779 addReply(c,shared.czero); 780 return; 781 } 782 dbAdd(dst,c->argv[1],o); 783 incrRefCount(o); 784 785 /* OK! key moved, free the entry in the source DB */ 786 dbDelete(src,c->argv[1]); 787 server.dirty++; 788 addReply(c,shared.cone); 789 } 790 791 /*----------------------------------------------------------------------------- 792 * Expires API 793 *----------------------------------------------------------------------------*/ 794 795 int removeExpire(redisDb *db, robj *key) { 796 /* An expire may only be removed if there is a corresponding entry in the 797 * main dict. Otherwise, the key will never be freed. */ 798 redisAssertWithInfo(NULL,key,dictFind(db->dict,key->ptr) != NULL); 799 return dictDelete(db->expires,key->ptr) == DICT_OK; 800 } 801 802 void setExpire(redisDb *db, robj *key, long long when) { 803 dictEntry *kde, *de; 804 805 /* Reuse the sds from the main dict in the expire dict */ 806 kde = dictFind(db->dict,key->ptr); 807 redisAssertWithInfo(NULL,key,kde != NULL); 808 de = dictReplaceRaw(db->expires,dictGetKey(kde)); 809 dictSetSignedIntegerVal(de,when); 810 } 811 812 /* Return the expire time of the specified key, or -1 if no expire 813 * is associated with this key (i.e. the key is non volatile) */ 814 long long getExpire(redisDb *db, robj *key) { 815 dictEntry *de; 816 817 /* No expire? return ASAP */ 818 if (dictSize(db->expires) == 0 || 819 (de = dictFind(db->expires,key->ptr)) == NULL) return -1; 820 821 /* The entry was found in the expire dict, this means it should also 822 * be present in the main dict (safety check). */ 823 redisAssertWithInfo(NULL,key,dictFind(db->dict,key->ptr) != NULL); 824 return dictGetSignedIntegerVal(de); 825 } 826 827 /* Propagate expires into slaves and the AOF file. 828 * When a key expires in the master, a DEL operation for this key is sent 829 * to all the slaves and the AOF file if enabled. 830 * 831 * This way the key expiry is centralized in one place, and since both 832 * AOF and the master->slave link guarantee operation ordering, everything 833 * will be consistent even if we allow write operations against expiring 834 * keys. */ 835 void propagateExpire(redisDb *db, robj *key) { 836 robj *argv[2]; 837 838 argv[0] = shared.del; 839 argv[1] = key; 840 incrRefCount(argv[0]); 841 incrRefCount(argv[1]); 842 843 if (server.aof_state != REDIS_AOF_OFF) 844 feedAppendOnlyFile(server.delCommand,db->id,argv,2); 845 replicationFeedSlaves(server.slaves,db->id,argv,2); 846 847 decrRefCount(argv[0]); 848 decrRefCount(argv[1]); 849 } 850 851 int expireIfNeeded(redisDb *db, robj *key) { 852 mstime_t when = getExpire(db,key); 853 mstime_t now; 854 855 if (when < 0) return 0; /* No expire for this key */ 856 857 /* Don't expire anything while loading. It will be done later. */ 858 if (server.loading) return 0; 859 860 /* If we are in the context of a Lua script, we claim that time is 861 * blocked to when the Lua script started. This way a key can expire 862 * only the first time it is accessed and not in the middle of the 863 * script execution, making propagation to slaves / AOF consistent. 864 * See issue #1525 on Github for more information. */ 865 now = server.lua_caller ? server.lua_time_start : mstime(); 866 867 /* If we are running in the context of a slave, return ASAP: 868 * the slave key expiration is controlled by the master that will 869 * send us synthesized DEL operations for expired keys. 870 * 871 * Still we try to return the right information to the caller, 872 * that is, 0 if we think the key should be still valid, 1 if 873 * we think the key is expired at this time. */ 874 if (server.masterhost != NULL) return now > when; 875 876 /* Return when this key has not expired */ 877 if (now <= when) return 0; 878 879 /* Delete the key */ 880 server.stat_expiredkeys++; 881 propagateExpire(db,key); 882 notifyKeyspaceEvent(REDIS_NOTIFY_EXPIRED, 883 "expired",key,db->id); 884 return dbDelete(db,key); 885 } 886 887 /*----------------------------------------------------------------------------- 888 * Expires Commands 889 *----------------------------------------------------------------------------*/ 890 891 /* This is the generic command implementation for EXPIRE, PEXPIRE, EXPIREAT 892 * and PEXPIREAT. Because the commad second argument may be relative or absolute 893 * the "basetime" argument is used to signal what the base time is (either 0 894 * for *AT variants of the command, or the current time for relative expires). 895 * 896 * unit is either UNIT_SECONDS or UNIT_MILLISECONDS, and is only used for 897 * the argv[2] parameter. The basetime is always specified in milliseconds. */ 898 void expireGenericCommand(redisClient *c, long long basetime, int unit) { 899 robj *key = c->argv[1], *param = c->argv[2]; 900 long long when; /* unix time in milliseconds when the key will expire. */ 901 902 if (getLongLongFromObjectOrReply(c, param, &when, NULL) != REDIS_OK) 903 return; 904 905 if (unit == UNIT_SECONDS) when *= 1000; 906 when += basetime; 907 908 /* No key, return zero. */ 909 if (lookupKeyWrite(c->db,key) == NULL) { 910 addReply(c,shared.czero); 911 return; 912 } 913 914 /* EXPIRE with negative TTL, or EXPIREAT with a timestamp into the past 915 * should never be executed as a DEL when load the AOF or in the context 916 * of a slave instance. 917 * 918 * Instead we take the other branch of the IF statement setting an expire 919 * (possibly in the past) and wait for an explicit DEL from the master. */ 920 if (when <= mstime() && !server.loading && !server.masterhost) { 921 robj *aux; 922 923 redisAssertWithInfo(c,key,dbDelete(c->db,key)); 924 server.dirty++; 925 926 /* Replicate/AOF this as an explicit DEL. */ 927 aux = createStringObject("DEL",3); 928 rewriteClientCommandVector(c,2,aux,key); 929 decrRefCount(aux); 930 signalModifiedKey(c->db,key); 931 notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"del",key,c->db->id); 932 addReply(c, shared.cone); 933 return; 934 } else { 935 setExpire(c->db,key,when); 936 addReply(c,shared.cone); 937 signalModifiedKey(c->db,key); 938 notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"expire",key,c->db->id); 939 server.dirty++; 940 return; 941 } 942 } 943 944 void expireCommand(redisClient *c) { 945 expireGenericCommand(c,mstime(),UNIT_SECONDS); 946 } 947 948 void expireatCommand(redisClient *c) { 949 expireGenericCommand(c,0,UNIT_SECONDS); 950 } 951 952 void pexpireCommand(redisClient *c) { 953 expireGenericCommand(c,mstime(),UNIT_MILLISECONDS); 954 } 955 956 void pexpireatCommand(redisClient *c) { 957 expireGenericCommand(c,0,UNIT_MILLISECONDS); 958 } 959 960 void ttlGenericCommand(redisClient *c, int output_ms) { 961 long long expire, ttl = -1; 962 963 /* If the key does not exist at all, return -2 */ 964 if (lookupKeyRead(c->db,c->argv[1]) == NULL) { 965 addReplyLongLong(c,-2); 966 return; 967 } 968 /* The key exists. Return -1 if it has no expire, or the actual 969 * TTL value otherwise. */ 970 expire = getExpire(c->db,c->argv[1]); 971 if (expire != -1) { 972 ttl = expire-mstime(); 973 if (ttl < 0) ttl = 0; 974 } 975 if (ttl == -1) { 976 addReplyLongLong(c,-1); 977 } else { 978 addReplyLongLong(c,output_ms ? ttl : ((ttl+500)/1000)); 979 } 980 } 981 982 void ttlCommand(redisClient *c) { 983 ttlGenericCommand(c, 0); 984 } 985 986 void pttlCommand(redisClient *c) { 987 ttlGenericCommand(c, 1); 988 } 989 990 void persistCommand(redisClient *c) { 991 dictEntry *de; 992 993 de = dictFind(c->db->dict,c->argv[1]->ptr); 994 if (de == NULL) { 995 addReply(c,shared.czero); 996 } else { 997 if (removeExpire(c->db,c->argv[1])) { 998 addReply(c,shared.cone); 999 server.dirty++; 1000 } else { 1001 addReply(c,shared.czero); 1002 } 1003 } 1004 } 1005 1006 /* ----------------------------------------------------------------------------- 1007 * API to get key arguments from commands 1008 * ---------------------------------------------------------------------------*/ 1009 1010 /* The base case is to use the keys position as given in the command table 1011 * (firstkey, lastkey, step). */ 1012 int *getKeysUsingCommandTable(struct redisCommand *cmd,robj **argv, int argc, int *numkeys) { 1013 int j, i = 0, last, *keys; 1014 REDIS_NOTUSED(argv); 1015 1016 if (cmd->firstkey == 0) { 1017 *numkeys = 0; 1018 return NULL; 1019 } 1020 last = cmd->lastkey; 1021 if (last < 0) last = argc+last; 1022 keys = zmalloc(sizeof(int)*((last - cmd->firstkey)+1)); 1023 for (j = cmd->firstkey; j <= last; j += cmd->keystep) { 1024 redisAssert(j < argc); 1025 keys[i++] = j; 1026 } 1027 *numkeys = i; 1028 return keys; 1029 } 1030 1031 /* Return all the arguments that are keys in the command passed via argc / argv. 1032 * 1033 * The command returns the positions of all the key arguments inside the array, 1034 * so the actual return value is an heap allocated array of integers. The 1035 * length of the array is returned by reference into *numkeys. 1036 * 1037 * 'cmd' must be point to the corresponding entry into the redisCommand 1038 * table, according to the command name in argv[0]. 1039 * 1040 * This function uses the command table if a command-specific helper function 1041 * is not required, otherwise it calls the command-specific function. */ 1042 int *getKeysFromCommand(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) { 1043 if (cmd->getkeys_proc) { 1044 return cmd->getkeys_proc(cmd,argv,argc,numkeys); 1045 } else { 1046 return getKeysUsingCommandTable(cmd,argv,argc,numkeys); 1047 } 1048 } 1049 1050 /* Free the result of getKeysFromCommand. */ 1051 void getKeysFreeResult(int *result) { 1052 zfree(result); 1053 } 1054 1055 /* Helper function to extract keys from following commands: 1056 * ZUNIONSTORE <destkey> <num-keys> <key> <key> ... <key> <options> 1057 * ZINTERSTORE <destkey> <num-keys> <key> <key> ... <key> <options> */ 1058 int *zunionInterGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) { 1059 int i, num, *keys; 1060 REDIS_NOTUSED(cmd); 1061 1062 num = atoi(argv[2]->ptr); 1063 /* Sanity check. Don't return any key if the command is going to 1064 * reply with syntax error. */ 1065 if (num > (argc-3)) { 1066 *numkeys = 0; 1067 return NULL; 1068 } 1069 1070 /* Keys in z{union,inter}store come from two places: 1071 * argv[1] = storage key, 1072 * argv[3...n] = keys to intersect */ 1073 keys = zmalloc(sizeof(int)*(num+1)); 1074 1075 /* Add all key positions for argv[3...n] to keys[] */ 1076 for (i = 0; i < num; i++) keys[i] = 3+i; 1077 1078 /* Finally add the argv[1] key position (the storage key target). */ 1079 keys[num] = 1; 1080 *numkeys = num+1; /* Total keys = {union,inter} keys + storage key */ 1081 return keys; 1082 } 1083 1084 /* Helper function to extract keys from the following commands: 1085 * EVAL <script> <num-keys> <key> <key> ... <key> [more stuff] 1086 * EVALSHA <script> <num-keys> <key> <key> ... <key> [more stuff] */ 1087 int *evalGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) { 1088 int i, num, *keys; 1089 REDIS_NOTUSED(cmd); 1090 1091 num = atoi(argv[2]->ptr); 1092 /* Sanity check. Don't return any key if the command is going to 1093 * reply with syntax error. */ 1094 if (num > (argc-3)) { 1095 *numkeys = 0; 1096 return NULL; 1097 } 1098 1099 keys = zmalloc(sizeof(int)*num); 1100 *numkeys = num; 1101 1102 /* Add all key positions for argv[3...n] to keys[] */ 1103 for (i = 0; i < num; i++) keys[i] = 3+i; 1104 1105 return keys; 1106 } 1107 1108 /* Helper function to extract keys from the SORT command. 1109 * 1110 * SORT <sort-key> ... STORE <store-key> ... 1111 * 1112 * The first argument of SORT is always a key, however a list of options 1113 * follow in SQL-alike style. Here we parse just the minimum in order to 1114 * correctly identify keys in the "STORE" option. */ 1115 int *sortGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) { 1116 int i, j, num, *keys, found_store = 0; 1117 REDIS_NOTUSED(cmd); 1118 1119 num = 0; 1120 keys = zmalloc(sizeof(int)*2); /* Alloc 2 places for the worst case. */ 1121 1122 keys[num++] = 1; /* <sort-key> is always present. */ 1123 1124 /* Search for STORE option. By default we consider options to don't 1125 * have arguments, so if we find an unknown option name we scan the 1126 * next. However there are options with 1 or 2 arguments, so we 1127 * provide a list here in order to skip the right number of args. */ 1128 struct { 1129 char *name; 1130 int skip; 1131 } skiplist[] = { 1132 {"limit", 2}, 1133 {"get", 1}, 1134 {"by", 1}, 1135 {NULL, 0} /* End of elements. */ 1136 }; 1137 1138 for (i = 2; i < argc; i++) { 1139 for (j = 0; skiplist[j].name != NULL; j++) { 1140 if (!strcasecmp(argv[i]->ptr,skiplist[j].name)) { 1141 i += skiplist[j].skip; 1142 break; 1143 } else if (!strcasecmp(argv[i]->ptr,"store") && i+1 < argc) { 1144 /* Note: we don't increment "num" here and continue the loop 1145 * to be sure to process the *last* "STORE" option if multiple 1146 * ones are provided. This is same behavior as SORT. */ 1147 found_store = 1; 1148 keys[num] = i+1; /* <store-key> */ 1149 break; 1150 } 1151 } 1152 } 1153 *numkeys = num + found_store; 1154 return keys; 1155 } 1156 1157 /* Slot to Key API. This is used by Redis Cluster in order to obtain in 1158 * a fast way a key that belongs to a specified hash slot. This is useful 1159 * while rehashing the cluster. */ 1160 void slotToKeyAdd(robj *key) { 1161 unsigned int hashslot = keyHashSlot(key->ptr,sdslen(key->ptr)); 1162 1163 zslInsert(server.cluster->slots_to_keys,hashslot,key); 1164 incrRefCount(key); 1165 } 1166 1167 void slotToKeyDel(robj *key) { 1168 unsigned int hashslot = keyHashSlot(key->ptr,sdslen(key->ptr)); 1169 1170 zslDelete(server.cluster->slots_to_keys,hashslot,key); 1171 } 1172 1173 void slotToKeyFlush(void) { 1174 zslFree(server.cluster->slots_to_keys); 1175 server.cluster->slots_to_keys = zslCreate(); 1176 } 1177 1178 unsigned int getKeysInSlot(unsigned int hashslot, robj **keys, unsigned int count) { 1179 zskiplistNode *n; 1180 zrangespec range; 1181 int j = 0; 1182 1183 range.min = range.max = hashslot; 1184 range.minex = range.maxex = 0; 1185 1186 n = zslFirstInRange(server.cluster->slots_to_keys, &range); 1187 while(n && n->score == hashslot && count--) { 1188 keys[j++] = n->obj; 1189 n = n->level[0].forward; 1190 } 1191 return j; 1192 } 1193 1194 /* Remove all the keys in the specified hash slot. 1195 * The number of removed items is returned. */ 1196 unsigned int delKeysInSlot(unsigned int hashslot) { 1197 zskiplistNode *n; 1198 zrangespec range; 1199 int j = 0; 1200 1201 range.min = range.max = hashslot; 1202 range.minex = range.maxex = 0; 1203 1204 n = zslFirstInRange(server.cluster->slots_to_keys, &range); 1205 while(n && n->score == hashslot) { 1206 robj *key = n->obj; 1207 n = n->level[0].forward; /* Go to the next item before freeing it. */ 1208 incrRefCount(key); /* Protect the object while freeing it. */ 1209 dbDelete(&server.db[0],key); 1210 decrRefCount(key); 1211 j++; 1212 } 1213 return j; 1214 } 1215 1216 unsigned int countKeysInSlot(unsigned int hashslot) { 1217 zskiplist *zsl = server.cluster->slots_to_keys; 1218 zskiplistNode *zn; 1219 zrangespec range; 1220 int rank, count = 0; 1221 1222 range.min = range.max = hashslot; 1223 range.minex = range.maxex = 0; 1224 1225 /* Find first element in range */ 1226 zn = zslFirstInRange(zsl, &range); 1227 1228 /* Use rank of first element, if any, to determine preliminary count */ 1229 if (zn != NULL) { 1230 rank = zslGetRank(zsl, zn->score, zn->obj); 1231 count = (zsl->length - (rank - 1)); 1232 1233 /* Find last element in range */ 1234 zn = zslLastInRange(zsl, &range); 1235 1236 /* Use rank of last element, if any, to determine the actual count */ 1237 if (zn != NULL) { 1238 rank = zslGetRank(zsl, zn->score, zn->obj); 1239 count -= (zsl->length - rank); 1240 } 1241 } 1242 return count; 1243 } 1244