Compare commits

...
6 Commits
3 changed files with 71 additions and 171 deletions
+24 -165
View File
@@ -53,6 +53,7 @@ static struct config {
int hostport;
long repeat;
int dbnum;
int argn_from_stdin;
int interactive;
int shutdown;
int monitor_mode;
@@ -61,135 +62,9 @@ static struct config {
char *auth;
} config;
struct redisCommand {
char *name;
int arity;
int flags;
};
#define CMDFLAG_NONE 0
#define CMDFLAG_RAWOUTPUT 1
static struct redisCommand cmdTable[] = {
{"auth",2,CMDFLAG_NONE},
{"get",2,CMDFLAG_NONE},
{"set",3,CMDFLAG_NONE},
{"setnx",3,CMDFLAG_NONE},
{"setex",4,CMDFLAG_NONE},
{"append",3,CMDFLAG_NONE},
{"substr",4,CMDFLAG_NONE},
{"del",-2,CMDFLAG_NONE},
{"exists",2,CMDFLAG_NONE},
{"incr",2,CMDFLAG_NONE},
{"decr",2,CMDFLAG_NONE},
{"rpush",3,CMDFLAG_NONE},
{"lpush",3,CMDFLAG_NONE},
{"rpop",2,CMDFLAG_NONE},
{"lpop",2,CMDFLAG_NONE},
{"brpop",-3,CMDFLAG_NONE},
{"blpop",-3,CMDFLAG_NONE},
{"llen",2,CMDFLAG_NONE},
{"lindex",3,CMDFLAG_NONE},
{"lset",4,CMDFLAG_NONE},
{"lrange",4,CMDFLAG_NONE},
{"ltrim",4,CMDFLAG_NONE},
{"lrem",4,CMDFLAG_NONE},
{"rpoplpush",3,CMDFLAG_NONE},
{"sadd",3,CMDFLAG_NONE},
{"srem",3,CMDFLAG_NONE},
{"smove",4,CMDFLAG_NONE},
{"sismember",3,CMDFLAG_NONE},
{"scard",2,CMDFLAG_NONE},
{"spop",2,CMDFLAG_NONE},
{"srandmember",2,CMDFLAG_NONE},
{"sinter",-2,CMDFLAG_NONE},
{"sinterstore",-3,CMDFLAG_NONE},
{"sunion",-2,CMDFLAG_NONE},
{"sunionstore",-3,CMDFLAG_NONE},
{"sdiff",-2,CMDFLAG_NONE},
{"sdiffstore",-3,CMDFLAG_NONE},
{"smembers",2,CMDFLAG_NONE},
{"zadd",4,CMDFLAG_NONE},
{"zincrby",4,CMDFLAG_NONE},
{"zrem",3,CMDFLAG_NONE},
{"zremrangebyscore",4,CMDFLAG_NONE},
{"zunion",-4,CMDFLAG_NONE},
{"zinter",-4,CMDFLAG_NONE},
{"zrange",-4,CMDFLAG_NONE},
{"zrank",3,CMDFLAG_NONE},
{"zrevrank",3,CMDFLAG_NONE},
{"zrangebyscore",-4,CMDFLAG_NONE},
{"zcount",4,CMDFLAG_NONE},
{"zrevrange",-4,CMDFLAG_NONE},
{"zcard",2,CMDFLAG_NONE},
{"zscore",3,CMDFLAG_NONE},
{"incrby",3,CMDFLAG_NONE},
{"decrby",3,CMDFLAG_NONE},
{"getset",3,CMDFLAG_NONE},
{"randomkey",1,CMDFLAG_NONE},
{"select",2,CMDFLAG_NONE},
{"move",3,CMDFLAG_NONE},
{"rename",3,CMDFLAG_NONE},
{"renamenx",3,CMDFLAG_NONE},
{"keys",2,CMDFLAG_NONE},
{"dbsize",1,CMDFLAG_NONE},
{"ping",1,CMDFLAG_NONE},
{"echo",2,CMDFLAG_NONE},
{"save",1,CMDFLAG_NONE},
{"bgsave",1,CMDFLAG_NONE},
{"rewriteaof",1,CMDFLAG_NONE},
{"bgrewriteaof",1,CMDFLAG_NONE},
{"shutdown",1,CMDFLAG_NONE},
{"lastsave",1,CMDFLAG_NONE},
{"type",2,CMDFLAG_NONE},
{"flushdb",1,CMDFLAG_NONE},
{"flushall",1,CMDFLAG_NONE},
{"sort",-2,CMDFLAG_NONE},
{"info",1,CMDFLAG_RAWOUTPUT},
{"mget",-2,CMDFLAG_NONE},
{"expire",3,CMDFLAG_NONE},
{"expireat",3,CMDFLAG_NONE},
{"ttl",2,CMDFLAG_NONE},
{"slaveof",3,CMDFLAG_NONE},
{"debug",-2,CMDFLAG_NONE},
{"mset",-3,CMDFLAG_NONE},
{"msetnx",-3,CMDFLAG_NONE},
{"monitor",1,CMDFLAG_NONE},
{"multi",1,CMDFLAG_NONE},
{"exec",1,CMDFLAG_NONE},
{"discard",1,CMDFLAG_NONE},
{"hset",4,CMDFLAG_NONE},
{"hget",3,CMDFLAG_NONE},
{"hmset",-4,CMDFLAG_NONE},
{"hmget",-3,CMDFLAG_NONE},
{"hincrby",4,CMDFLAG_NONE},
{"hdel",3,CMDFLAG_NONE},
{"hlen",2,CMDFLAG_NONE},
{"hkeys",2,CMDFLAG_NONE},
{"hvals",2,CMDFLAG_NONE},
{"hgetall",2,CMDFLAG_NONE},
{"hexists",3,CMDFLAG_NONE},
{"config",-2,CMDFLAG_NONE},
{"subscribe",-2,CMDFLAG_NONE},
{"unsubscribe",-1,CMDFLAG_NONE},
{"psubscribe",-2,CMDFLAG_NONE},
{"punsubscribe",-1,CMDFLAG_NONE},
{"publish",3,CMDFLAG_NONE},
{NULL,0,CMDFLAG_NONE}
};
static int cliReadReply(int fd);
static void usage();
static struct redisCommand *lookupCommand(char *name) {
int j = 0;
while(cmdTable[j].name != NULL) {
if (!strcasecmp(name,cmdTable[j].name)) return &cmdTable[j];
j++;
}
return NULL;
}
static int cliConnect(void) {
char err[ANET_ERR_LEN];
static int fd = ANET_ERR;
@@ -359,26 +234,15 @@ static int selectDb(int fd) {
}
static int cliSendCommand(int argc, char **argv, int repeat) {
struct redisCommand *rc = lookupCommand(argv[0]);
char *command = argv[0];
int fd, j, retval = 0;
sds cmd;
if (!rc) {
fprintf(stderr,"Unknown command '%s'\n",argv[0]);
return 1;
}
config.raw_output = (rc->flags & CMDFLAG_RAWOUTPUT);
if ((rc->arity > 0 && argc != rc->arity) ||
(rc->arity < 0 && argc < -rc->arity)) {
fprintf(stderr,"Wrong number of arguments for '%s'\n",rc->name);
return 1;
}
if (!strcasecmp(rc->name,"shutdown")) config.shutdown = 1;
if (!strcasecmp(rc->name,"monitor")) config.monitor_mode = 1;
if (!strcasecmp(rc->name,"subscribe") ||
!strcasecmp(rc->name,"psubscribe")) config.pubsub_mode = 1;
config.raw_output = !strcasecmp(command,"info");
if (!strcasecmp(command,"shutdown")) config.shutdown = 1;
if (!strcasecmp(command,"monitor")) config.monitor_mode = 1;
if (!strcasecmp(command,"subscribe") ||
!strcasecmp(command,"psubscribe")) config.pubsub_mode = 1;
if ((fd = cliConnect()) == -1) return 1;
/* Select db number */
@@ -388,18 +252,17 @@ static int cliSendCommand(int argc, char **argv, int repeat) {
return 1;
}
while(repeat--) {
/* Build the command to send */
cmd = sdscatprintf(sdsempty(),"*%d\r\n",argc);
for (j = 0; j < argc; j++) {
cmd = sdscatprintf(cmd,"$%lu\r\n",
(unsigned long)sdslen(argv[j]));
cmd = sdscatlen(cmd,argv[j],sdslen(argv[j]));
cmd = sdscatlen(cmd,"\r\n",2);
}
anetWrite(fd,cmd,sdslen(cmd));
sdsfree(cmd);
/* Build the command to send */
cmd = sdscatprintf(sdsempty(),"*%d\r\n",argc);
for (j = 0; j < argc; j++) {
cmd = sdscatprintf(cmd,"$%lu\r\n",
(unsigned long)sdslen(argv[j]));
cmd = sdscatlen(cmd,argv[j],sdslen(argv[j]));
cmd = sdscatlen(cmd,"\r\n",2);
}
while(repeat--) {
anetWrite(fd,cmd,sdslen(cmd));
while (config.monitor_mode) {
cliReadSingleLineReply(fd,0);
}
@@ -413,7 +276,6 @@ static int cliSendCommand(int argc, char **argv, int repeat) {
}
retval = cliReadReply(fd);
if (retval) {
return retval;
}
@@ -451,6 +313,8 @@ static int parseOptions(int argc, char **argv) {
i++;
} else if (!strcmp(argv[i],"-i")) {
config.interactive = 1;
} else if (!strcmp(argv[i],"-c")) {
config.argn_from_stdin = 1;
} else {
break;
}
@@ -477,7 +341,7 @@ static sds readArgFromStdin(void) {
static void usage() {
fprintf(stderr, "usage: redis-cli [-h host] [-p port] [-a authpw] [-r repeat_times] [-n db_num] [-i] cmd arg1 arg2 arg3 ... argN\n");
fprintf(stderr, "usage: echo \"argN\" | redis-cli [-h host] [-a authpw] [-p port] [-r repeat_times] [-n db_num] cmd arg1 arg2 ... arg(N-1)\n");
fprintf(stderr, "usage: echo \"argN\" | redis-cli -c [-h host] [-p port] [-a authpw] [-r repeat_times] [-n db_num] cmd arg1 arg2 ... arg(N-1)\n");
fprintf(stderr, "\nIf a pipe from standard input is detected this data is used as last argument.\n\n");
fprintf(stderr, "example: cat /etc/passwd | redis-cli set my_passwd\n");
fprintf(stderr, "example: redis-cli get my_passwd\n");
@@ -489,7 +353,7 @@ static void usage() {
/* Turn the plain C strings into Sds strings */
static char **convertToSds(int count, char** args) {
int j;
char **sds = zmalloc(sizeof(char*)*count+1);
char **sds = zmalloc(sizeof(char*)*count);
for(j = 0; j < count; j++)
sds[j] = sdsnew(args[j]);
@@ -592,12 +456,12 @@ static void repl() {
int main(int argc, char **argv) {
int firstarg;
char **argvcopy;
struct redisCommand *rc;
config.hostip = "127.0.0.1";
config.hostport = 6379;
config.repeat = 1;
config.dbnum = 0;
config.argn_from_stdin = 0;
config.shutdown = 0;
config.interactive = 0;
config.monitor_mode = 0;
@@ -619,16 +483,11 @@ int main(int argc, char **argv) {
if (argc == 0 || config.interactive == 1) repl();
argvcopy = convertToSds(argc, argv);
/* Read the last argument from stdandard input if needed */
if ((rc = lookupCommand(argv[0])) != NULL) {
if (rc->arity > 0 && argc == rc->arity-1) {
argvcopy = convertToSds(argc+1, argv);
if (config.argn_from_stdin) {
sds lastarg = readArgFromStdin();
argvcopy[argc] = lastarg;
argc++;
}
}
return cliSendCommand(argc, argvcopy, config.repeat);
}
+28 -6
View File
@@ -4057,7 +4057,6 @@ static int rdbLoad(char *filename) {
redisDb *db = server.db+0;
char buf[1024];
time_t expiretime, now = time(NULL);
long long loadedkeys = 0;
fp = fopen(filename,"r");
if (!fp) return REDIS_ERR;
@@ -4076,6 +4075,7 @@ static int rdbLoad(char *filename) {
}
while(1) {
robj *key, *val;
int force_swapout;
expiretime = -1;
/* Read type. */
@@ -4114,7 +4114,6 @@ static int rdbLoad(char *filename) {
redisLog(REDIS_WARNING,"Loading DB, duplicated key (%s) found! Unrecoverable error, exiting now.", key->ptr);
exit(1);
}
loadedkeys++;
/* Set the expire time if needed */
if (expiretime != -1) setExpire(db,key,expiretime);
@@ -4140,9 +4139,14 @@ static int rdbLoad(char *filename) {
continue;
}
/* Flush data on disk once 32 MB of additional RAM are used... */
force_swapout = 0;
if ((zmalloc_used_memory() - server.vm_max_memory) > 1024*1024*32)
force_swapout = 1;
/* If we have still some hope of having some value fitting memory
* then we try random sampling. */
if (!swap_all_values && server.vm_enabled && (loadedkeys % 5000) == 0) {
if (!swap_all_values && server.vm_enabled && force_swapout) {
while (zmalloc_used_memory() > server.vm_max_memory) {
if (vmSwapOneObjectBlocking() == REDIS_ERR) break;
}
@@ -5710,6 +5714,11 @@ static void zaddGenericCommand(redisClient *c, robj *key, robj *ele, double scor
zset *zs;
double *score;
if (isnan(scoreval)) {
addReplySds(c,sdsnew("-ERR provide score is Not A Number (nan)\r\n"));
return;
}
zsetobj = lookupKeyWrite(c->db,key);
if (zsetobj == NULL) {
zsetobj = createZsetObject();
@@ -5738,6 +5747,15 @@ static void zaddGenericCommand(redisClient *c, robj *key, robj *ele, double scor
} else {
*score = scoreval;
}
if (isnan(*score)) {
addReplySds(c,
sdsnew("-ERR resulting score is Not A Number (nan)\r\n"));
zfree(score);
/* 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;
}
} else {
*score = scoreval;
}
@@ -8316,7 +8334,6 @@ int loadAppendOnlyFile(char *filename) {
struct redisClient *fakeClient;
FILE *fp = fopen(filename,"r");
struct redis_stat sb;
unsigned long long loadedkeys = 0;
int appendonly = server.appendonly;
if (redis_fstat(fileno(fp),&sb) != -1 && sb.st_size == 0)
@@ -8339,6 +8356,7 @@ int loadAppendOnlyFile(char *filename) {
char buf[128];
sds argsds;
struct redisCommand *cmd;
int force_swapout;
if (fgets(buf,sizeof(buf),fp) == NULL) {
if (feof(fp))
@@ -8379,8 +8397,11 @@ int loadAppendOnlyFile(char *filename) {
for (j = 0; j < argc; j++) decrRefCount(argv[j]);
zfree(argv);
/* Handle swapping while loading big datasets when VM is on */
loadedkeys++;
if (server.vm_enabled && (loadedkeys % 5000) == 0) {
force_swapout = 0;
if ((zmalloc_used_memory() - server.vm_max_memory) > 1024*1024*32)
force_swapout = 1;
if (server.vm_enabled && force_swapout) {
while (zmalloc_used_memory() > server.vm_max_memory) {
if (vmSwapOneObjectBlocking() == REDIS_ERR) break;
}
@@ -9225,6 +9246,7 @@ static int vmSwapOneObject(int usethreads) {
if (maxtries) i--; /* don't count this try */
continue;
}
val->vm.atime = key->vm.atime; /* atime is updated on key object */
swappability = computeObjectSwappability(val);
if (!best || swappability > best_swappability) {
best = de;
+19
View File
@@ -381,4 +381,23 @@ start_server default.conf {} {
}
set _ $err
} {}
test {ZSET element can't be set to nan with ZADD} {
set e {}
catch {r zadd myzset nan abc} e
set _ $e
} {*Not A Number*}
test {ZSET element can't be set to nan with ZINCRBY} {
set e {}
catch {r zincrby myzset nan abc} e
set _ $e
} {*Not A Number*}
test {ZINCRBY calls leading to Nan are refused} {
set e {}
r zincrby myzset +inf abc
catch {r zincrby myzset -inf abc} e
set _ $e
} {*Not A Number*}
}