Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge remote-tracking branch 'antirez/2.4' into 2.4
  • Loading branch information
yosh committed Dec 1, 2011
2 parents d0abd58 + bc62bc5 commit 765da04
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 21 deletions.
23 changes: 23 additions & 0 deletions src/replication.c
Expand Up @@ -471,11 +471,24 @@ int connectWithMaster(void) {
return REDIS_ERR;
}

server.repl_transfer_lastio = time(NULL);
server.repl_transfer_s = fd;
server.replstate = REDIS_REPL_CONNECTING;
return REDIS_OK;
}

/* This function can be called when a non blocking connection is currently
* in progress to undo it. */
void undoConnectWithMaster(void) {
int fd = server.repl_transfer_s;

redisAssert(server.replstate == REDIS_REPL_CONNECTING);
aeDeleteFileEvent(server.el,fd,AE_READABLE|AE_WRITABLE);
close(fd);
server.repl_transfer_s = -1;
server.replstate = REDIS_REPL_CONNECT;
}

void slaveofCommand(redisClient *c) {
if (!strcasecmp(c->argv[1]->ptr,"no") &&
!strcasecmp(c->argv[2]->ptr,"one")) {
Expand All @@ -485,6 +498,8 @@ void slaveofCommand(redisClient *c) {
if (server.master) freeClient(server.master);
if (server.replstate == REDIS_REPL_TRANSFER)
replicationAbortSyncTransfer();
else if (server.replstate == REDIS_REPL_CONNECTING)
undoConnectWithMaster();
server.replstate = REDIS_REPL_NONE;
redisLog(REDIS_NOTICE,"MASTER MODE enabled (user request)");
}
Expand All @@ -505,6 +520,14 @@ void slaveofCommand(redisClient *c) {
/* --------------------------- REPLICATION CRON ---------------------------- */

void replicationCron(void) {
/* Non blocking connection timeout? */
if (server.masterhost && server.replstate == REDIS_REPL_CONNECTING &&
(time(NULL)-server.repl_transfer_lastio) > server.repl_timeout)
{
redisLog(REDIS_WARNING,"Timeout connecting to the MASTER...");
undoConnectWithMaster();
}

/* Bulk transfer I/O timeout? */
if (server.masterhost && server.replstate == REDIS_REPL_TRANSFER &&
(time(NULL)-server.repl_transfer_lastio) > server.repl_timeout)
Expand Down
21 changes: 9 additions & 12 deletions src/sort.c
Expand Up @@ -141,11 +141,7 @@ void sortCommand(redisClient *c) {

/* Lookup the key to sort. It must be of the right types */
sortval = lookupKeyRead(c->db,c->argv[1]);
if (sortval == NULL) {
addReply(c,shared.emptymultibulk);
return;
}
if (sortval->type != REDIS_SET && sortval->type != REDIS_LIST &&
if (sortval && sortval->type != REDIS_SET && sortval->type != REDIS_LIST &&
sortval->type != REDIS_ZSET)
{
addReply(c,shared.wrongtypeerr);
Expand All @@ -161,7 +157,10 @@ void sortCommand(redisClient *c) {
/* Now we need to protect sortval incrementing its count, in the future
* SORT may have options able to overwrite/delete keys during the sorting
* and the sorted key itself may get destroied */
incrRefCount(sortval);
if (sortval)
incrRefCount(sortval);
else
sortval = createListObject();

/* The SORT command has an SQL-alike syntax, parse it */
while(j < c->argc) {
Expand Down Expand Up @@ -200,7 +199,8 @@ void sortCommand(redisClient *c) {
}

/* Destructively convert encoded sorted sets for SORT. */
if (sortval->type == REDIS_ZSET) zsetConvert(sortval, REDIS_ENCODING_SKIPLIST);
if (sortval->type == REDIS_ZSET)
zsetConvert(sortval, REDIS_ENCODING_SKIPLIST);

/* Load the sorting vector with all the objects to sort */
switch(sortval->type) {
Expand Down Expand Up @@ -366,12 +366,9 @@ void sortCommand(redisClient *c) {
}
}
}
setKey(c->db,storekey,sobj);
if (outputlen) setKey(c->db,storekey,sobj);
decrRefCount(sobj);
/* Note: we add 1 because the DB is dirty anyway since even if the
* SORT result is empty a new key is set and maybe the old content
* replaced. */
server.dirty += 1+outputlen;
server.dirty += outputlen;
addReplyLongLong(c,outputlen);
}

Expand Down
34 changes: 25 additions & 9 deletions tests/test_helper.tcl
Expand Up @@ -41,6 +41,7 @@ set ::port 21111
set ::traceleaks 0
set ::valgrind 0
set ::verbose 0
set ::quiet 0
set ::denytags {}
set ::allowtags {}
set ::external 0; # If "1" this means, we are running against external instance
Expand Down Expand Up @@ -141,11 +142,11 @@ proc s {args} {
}

proc cleanup {} {
puts -nonewline "Cleanup: may take some time... "
if {!$::quiet} {puts -nonewline "Cleanup: may take some time... "}
flush stdout
catch {exec rm -rf {*}[glob tests/tmp/redis.conf.*]}
catch {exec rm -rf {*}[glob tests/tmp/server.*]}
puts "OK"
if {!$::quiet} {puts "OK"}
}

proc find_available_port start {
Expand All @@ -168,7 +169,9 @@ proc test_server_main {} {
# Open a listening socket, trying different ports in order to find a
# non busy one.
set port [find_available_port 11111]
puts "Starting test server at port $port"
if {!$::quiet} {
puts "Starting test server at port $port"
}
socket -server accept_test_clients $port

# Start the client instances
Expand Down Expand Up @@ -222,16 +225,22 @@ proc read_from_test_client fd {
set payload [read $fd $bytes]
foreach {status data} $payload break
if {$status eq {ready}} {
puts "\[$status\]: $data"
if {!$::quiet} {
puts "\[$status\]: $data"
}
signal_idle_client $fd
} elseif {$status eq {done}} {
set elapsed [expr {[clock seconds]-$::clients_start_time($fd)}]
puts "\[[colorstr yellow $status]\]: $data ($elapsed seconds)"
puts "+++ [expr {[llength $::active_clients]-1}] units still in execution."
set all_tests_count [llength $::all_tests]
set running_tests_count [expr {[llength $::active_clients]-1}]
set completed_tests_count [expr {$::next_test-$running_tests_count}]
puts "\[$completed_tests_count/$all_tests_count [colorstr yellow $status]\]: $data ($elapsed seconds)"
lappend ::clients_time_history $elapsed $data
signal_idle_client $fd
} elseif {$status eq {ok}} {
puts "\[[colorstr green $status]\]: $data"
if {!$::quiet} {
puts "\[[colorstr green $status]\]: $data"
}
} elseif {$status eq {err}} {
set err "\[[colorstr red $status]\]: $data"
puts $err
Expand All @@ -245,7 +254,9 @@ proc read_from_test_client fd {
} elseif {$status eq {testing}} {
# No op
} else {
puts "\[$status\]: $data"
if {!$::quiet} {
puts "\[$status\]: $data"
}
}
}

Expand All @@ -257,7 +268,9 @@ proc signal_idle_client fd {
[lsearch -all -inline -not -exact $::active_clients $fd]
# New unit to process?
if {$::next_test != [llength $::all_tests]} {
puts [colorstr bold-white "Testing [lindex $::all_tests $::next_test]"]
if {!$::quiet} {
puts [colorstr bold-white "Testing [lindex $::all_tests $::next_test]"]
}
set ::clients_start_time($fd) [clock seconds]
send_data_packet $fd run [lindex $::all_tests $::next_test]
lappend ::active_clients $fd
Expand Down Expand Up @@ -321,6 +334,7 @@ proc print_help_screen {} {
puts [join {
"--valgrind Run the test over valgrind."
"--accurate Run slow randomized tests for more iterations."
"--quiet Don't show individual tests."
"--single <unit> Just execute the specified unit (see next option)."
"--list-tests List all the available test units."
"--force-failure Force the execution of a test that always fails."
Expand All @@ -343,6 +357,8 @@ for {set j 0} {$j < [llength $argv]} {incr j} {
incr j
} elseif {$opt eq {--valgrind}} {
set ::valgrind 1
} elseif {$opt eq {--quiet}} {
set ::quiet 1
} elseif {$opt eq {--host}} {
set ::external 1
set ::host $arg
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/sort.tcl
Expand Up @@ -134,6 +134,18 @@ start_server {
assert_equal [lsort -real $floats] [r sort mylist]
}

test "SORT with STORE returns zero if result is empty (github isse 224)" {
r flushdb
r sort foo store bar
} {0}

test "SORT with STORE does not create empty lists (github issue 224)" {
r flushdb
r lpush foo bar
r sort foo limit 10 10 store zap
r exists zap
} {0}

tags {"slow"} {
set num 100
set res [create_random_dataset $num lpush]
Expand Down

0 comments on commit 765da04

Please sign in to comment.