redict/tests/unit/querybuf.tcl
sundb e5d8a5eb85
Fix the wrong reisze of querybuf (#9003)
The initialize memory of `querybuf` is `PROTO_IOBUF_LEN(1024*16) * 2` (due to sdsMakeRoomFor being greedy), under `jemalloc`, the allocated memory will be 40k.
This will most likely result in the `querybuf` being resized when call `clientsCronResizeQueryBuffer` unless the client requests it fast enough.

Note that this bug existed even before #7875, since the condition for resizing includes the sds headers (32k+6).

## Changes
1. Use non-greedy sdsMakeRoomFor when allocating the initial query buffer (of 16k).
1. Also use non-greedy allocation when working with BIG_ARG (we won't use that extra space anyway)
2. in case we did use a greedy allocation, read as much as we can into the buffer we got (including internal frag), to reduce system calls.
3. introduce a dedicated constant for the shrinking (same value as before)
3. Add test for querybuf.
4. improve a maxmemory test by ignoring the effect of replica query buffers (can accumulate many ACKs on slow env)
5. improve a maxmemory by disabling slowlog (it will cause slight memory growth on slow env).
2021-06-15 14:46:19 +03:00

49 lines
1.8 KiB
Tcl

proc client_idle_sec {name} {
set clients [split [r client list] "\r\n"]
set c [lsearch -inline $clients *name=$name*]
assert {[regexp {idle=([0-9]+)} $c - idle]}
return $idle
}
# Calculate query buffer memory of slave
proc client_query_buffer {name} {
set clients [split [r client list] "\r\n"]
set c [lsearch -inline $clients *name=$name*]
if {[string length $c] > 0} {
assert {[regexp {qbuf=([0-9]+)} $c - qbuf]}
assert {[regexp {qbuf-free=([0-9]+)} $c - qbuf_free]}
return [expr $qbuf + $qbuf_free]
}
return 0
}
start_server {tags {"querybuf slow"}} {
# The test will run at least 2s to check if client query
# buffer will be resized when client idle 2s.
test "query buffer resized correctly" {
# Memory will increase by more than 32k due to client query buffer.
set rd [redis_deferring_client]
$rd client setname test_client
set orig_test_client_qbuf [client_query_buffer test_client]
assert {$orig_test_client_qbuf > 16384 && $orig_test_client_qbuf < 32768}
# Check that the initial query buffer is not resized if it is idle for more than 2s
wait_for_condition 1000 10 {
[client_idle_sec test_client] > 3 && [client_query_buffer test_client] == $orig_test_client_qbuf
} else {
fail "query buffer was resized"
}
# Fill query buffer to more than 32k
$rd set bigstring v ;# create bigstring in advance to avoid adding extra memory
$rd set bigstring [string repeat A 32768] nx
# Wait for query buffer to be resized to 0.
wait_for_condition 1000 10 {
[client_query_buffer test_client] == 0
} else {
fail "querybuf expected to be resized"
}
}
}