diff options
author | antirez <antirez@gmail.com> | 2012-09-04 10:37:49 +0200 |
---|---|---|
committer | antirez <antirez@gmail.com> | 2012-09-17 10:26:46 +0200 |
commit | 7eb850ef0e437323e2d84157ddc2e6e82af57bbc (patch) | |
tree | e69d67369e1271070abfb356f78a0a7e97c9d13a /tests | |
parent | bfc197c3b604baf0dba739ea174d5054284133f0 (diff) | |
download | redis-7eb850ef0e437323e2d84157ddc2e6e82af57bbc.tar.gz |
A reimplementation of blocking operation internals.
Redis provides support for blocking operations such as BLPOP or BRPOP.
This operations are identical to normal LPOP and RPOP operations as long
as there are elements in the target list, but if the list is empty they
block waiting for new data to arrive to the list.
All the clients blocked waiting for th same list are served in a FIFO
way, so the first that blocked is the first to be served when there is
more data pushed by another client into the list.
The previous implementation of blocking operations was conceived to
serve clients in the context of push operations. For for instance:
1) There is a client "A" blocked on list "foo".
2) The client "B" performs `LPUSH foo somevalue`.
3) The client "A" is served in the context of the "B" LPUSH,
synchronously.
Processing things in a synchronous way was useful as if "A" pushes a
value that is served by "B", from the point of view of the database is a
NOP (no operation) thing, that is, nothing is replicated, nothing is
written in the AOF file, and so forth.
However later we implemented two things:
1) Variadic LPUSH that could add multiple values to a list in the
context of a single call.
2) BRPOPLPUSH that was a version of BRPOP that also provided a "PUSH"
side effect when receiving data.
This forced us to make the synchronous implementation more complex. If
client "B" is waiting for data, and "A" pushes three elemnents in a
single call, we needed to propagate an LPUSH with a missing argument
in the AOF and replication link. We also needed to make sure to
replicate the LPUSH side of BRPOPLPUSH, but only if in turn did not
happened to serve another blocking client into another list ;)
This were complex but with a few of mutually recursive functions
everything worked as expected... until one day we introduced scripting
in Redis.
Scripting + synchronous blocking operations = Issue #614.
Basically you can't "rewrite" a script to have just a partial effect on
the replicas and AOF file if the script happened to serve a few blocked
clients.
The solution to all this problems, implemented by this commit, is to
change the way we serve blocked clients. Instead of serving the blocked
clients synchronously, in the context of the command performing the PUSH
operation, it is now an asynchronous and iterative process:
1) If a key that has clients blocked waiting for data is the subject of
a list push operation, We simply mark keys as "ready" and put it into a
queue.
2) Every command pushing stuff on lists, as a variadic LPUSH, a script,
or whatever it is, is replicated verbatim without any rewriting.
3) Every time a Redis command, a MULTI/EXEC block, or a script,
completed its execution, we run the list of keys ready to serve blocked
clients (as more data arrived), and process this list serving the
blocked clients.
4) As a result of "3" maybe more keys are ready again for other clients
(as a result of BRPOPLPUSH we may have push operations), so we iterate
back to step "3" if it's needed.
The new code has a much simpler semantics, and a simpler to understand
implementation, with the disadvantage of not being able to "optmize out"
a PUSH+BPOP as a No OP.
This commit will be tested with care before the final merge, more tests
will be added likely.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/integration/replication.tcl | 10 | ||||
-rw-r--r-- | tests/unit/scripting.tcl | 17 | ||||
-rw-r--r-- | tests/unit/type/list.tcl | 90 |
3 files changed, 112 insertions, 5 deletions
diff --git a/tests/integration/replication.tcl b/tests/integration/replication.tcl index 18e639d41..da94b0880 100644 --- a/tests/integration/replication.tcl +++ b/tests/integration/replication.tcl @@ -61,9 +61,13 @@ start_server {tags {"repl"}} { test {SET on the master should immediately propagate} { r -1 set mykey bar - if {$::valgrind} {after 2000} - r 0 get mykey - } {bar} + + wait_for_condition 500 100 { + [r 0 get mykey] eq {bar} + } else { + fail "SET on master did not propagated on slave" + } + } test {FLUSHALL should replicate} { r -1 flushall diff --git a/tests/unit/scripting.tcl b/tests/unit/scripting.tcl index 057918716..6dbdb6b63 100644 --- a/tests/unit/scripting.tcl +++ b/tests/unit/scripting.tcl @@ -344,5 +344,22 @@ start_server {tags {"scripting repl"}} { fail "Expected 2 in x, but value is '[r -1 get x]'" } } + + test {Replication of script multiple pushes to list with BLPOP} { + set rd [redis_deferring_client] + $rd brpop a 0 + r eval { + redis.call("lpush","a","1"); + redis.call("lpush","a","2"); + } 0 + set res [$rd read] + $rd close + wait_for_condition 50 100 { + [r -1 lrange a 0 -1] eq [r lrange a 0 -1] + } else { + fail "Expected list 'a' in slave and master to be the same, but they are respectively '[r -1 lrange a 0 -1]' and '[r lrange a 0 -1]'" + } + set res + } {a 1} } } diff --git a/tests/unit/type/list.tcl b/tests/unit/type/list.tcl index 85dde5690..8f598a4ab 100644 --- a/tests/unit/type/list.tcl +++ b/tests/unit/type/list.tcl @@ -161,6 +161,47 @@ start_server { } } + test "BLPOP, LPUSH + DEL should not awake blocked client" { + set rd [redis_deferring_client] + r del list + + $rd blpop list 0 + r multi + r lpush list a + r del list + r exec + r del list + r lpush list b + $rd read + } {list b} + + test "BLPOP, LPUSH + DEL + SET should not awake blocked client" { + set rd [redis_deferring_client] + r del list + + $rd blpop list 0 + r multi + r lpush list a + r del list + r set list foo + r exec + r del list + r lpush list b + $rd read + } {list b} + + test "MULTI/EXEC is isolated from the point of view of BLPOP" { + set rd [redis_deferring_client] + r del list + $rd blpop list 0 + r multi + r lpush list a + r lpush list b + r lpush list c + r exec + $rd read + } {list c} + test "BLPOP with variadic LPUSH" { set rd [redis_deferring_client] r del blist target @@ -169,8 +210,8 @@ start_server { if {$::valgrind} {after 100} assert_equal 2 [r lpush blist foo bar] if {$::valgrind} {after 100} - assert_equal {blist foo} [$rd read] - assert_equal bar [lindex [r lrange blist 0 -1] 0] + assert_equal {blist bar} [$rd read] + assert_equal foo [lindex [r lrange blist 0 -1] 0] } test "BRPOPLPUSH with zero timeout should block indefinitely" { @@ -222,6 +263,16 @@ start_server { assert_equal {foo} [r lrange blist 0 -1] } + test "BRPOPLPUSH maintains order of elements after failure" { + set rd [redis_deferring_client] + r del blist target + r set target nolist + $rd brpoplpush blist target 0 + r rpush blist a b c + assert_error "ERR*wrong kind*" {$rd read} + r lrange blist 0 -1 + } {a b c} + test "BRPOPLPUSH with multiple blocked clients" { set rd1 [redis_deferring_client] set rd2 [redis_deferring_client] @@ -293,6 +344,41 @@ start_server { r exec } {foo bar {} {} {bar foo}} + test "PUSH resulting from BRPOPLPUSH affect WATCH" { + set blocked_client [redis_deferring_client] + set watching_client [redis_deferring_client] + r del srclist dstlist somekey + r set somekey somevalue + $blocked_client brpoplpush srclist dstlist 0 + $watching_client watch dstlist + $watching_client read + $watching_client multi + $watching_client read + $watching_client get somekey + $watching_client read + r lpush srclist element + $watching_client exec + $watching_client read + } {} + + test "BRPOPLPUSH does not affect WATCH while still blocked" { + set blocked_client [redis_deferring_client] + set watching_client [redis_deferring_client] + r del srclist dstlist somekey + r set somekey somevalue + $blocked_client brpoplpush srclist dstlist 0 + $watching_client watch dstlist + $watching_client read + $watching_client multi + $watching_client read + $watching_client get somekey + $watching_client read + $watching_client exec + # Blocked BLPOPLPUSH may create problems, unblock it. + r lpush srclist element + $watching_client read + } {somevalue} + test {BRPOPLPUSH timeout} { set rd [redis_deferring_client] |