Files
redis/tests/unit/client-eviction.tcl
Yuan Wang 64a40b20d9 Async IO Threads (#13695)
## Introduction
Redis introduced IO Thread in 6.0, allowing IO threads to handle client
request reading, command parsing and reply writing, thereby improving
performance. The current IO thread implementation has a few drawbacks.
- The main thread is blocked during IO thread read/write operations and
must wait for all IO threads to complete their current tasks before it
can continue execution. In other words, the entire process is
synchronous. This prevents the efficient utilization of multi-core CPUs
for parallel processing.

- When the number of clients and requests increases moderately, it
causes all IO threads to reach full CPU utilization due to the busy wait
mechanism used by the IO threads. This makes it challenging for us to
determine which part of Redis has reached its bottleneck.

- When IO threads are enabled with TLS and io-threads-do-reads, a
disconnection of a connection with pending data may result in it being
assigned to multiple IO threads simultaneously. This can cause race
conditions and trigger assertion failures. Related issue:
redis#12540

Therefore, we designed an asynchronous IO threads solution. The IO
threads adopt an event-driven model, with the main thread dedicated to
command processing, meanwhile, the IO threads handle client read and
write operations in parallel.

## Implementation
### Overall
As before, we did not change the fact that all client commands must be
executed on the main thread, because Redis was originally designed to be
single-threaded, and processing commands in a multi-threaded manner
would inevitably introduce numerous race and synchronization issues. But
now each IO thread has independent event loop, therefore, IO threads can
use a multiplexing approach to handle client read and write operations,
eliminating the CPU overhead caused by busy-waiting.

the execution process can be briefly described as follows:
the main thread assigns clients to IO threads after accepting
connections, IO threads will notify the main thread when clients
finish reading and parsing queries, then the main thread processes
queries from IO threads and generates replies, IO threads handle
writing reply to clients after receiving clients list from main thread,
and then continue to handle client read and write events.

### Each IO thread has independent event loop
We now assign each IO thread its own event loop. This approach
eliminates the need for the main thread to perform the costly
`epoll_wait` operation for handling connections (except for specific
ones). Instead, the main thread processes requests from the IO threads
and hands them back once completed, fully offloading read and write
events to the IO threads.

Additionally, all TLS operations, including handling pending data, have
been moved entirely to the IO threads. This resolves the issue where
io-threads-do-reads could not be used with TLS.

### Event-notified client queue
To facilitate communication between the IO threads and the main thread,
we designed an event-notified client queue. Each IO thread and the main
thread have two such queues to store clients waiting to be processed.
These queues are also integrated with the event loop to enable handling.
We use pthread_mutex to ensure the safety of queue operations, as well
as data visibility and ordering, and race conditions are minimized, as
each IO thread and the main thread operate on independent queues,
avoiding thread suspension due to lock contention. And we implemented an
event notifier based on `eventfd` or `pipe` to support event-driven
handling.

### Thread safety
Since the main thread and IO threads can execute in parallel, we must
handle data race issues carefully.

**client->flags**
The primary tasks of IO threads are reading and writing, i.e.
`readQueryFromClient` and `writeToClient`. However, IO threads and the
main thread may concurrently modify or access `client->flags`, leading
to potential race conditions. To address this, we introduced an io-flags
variable to record operations performed by IO threads, thereby avoiding
race conditions on `client->flags`.

**Pause IO thread**
In the main thread, we may want to operate data of IO threads, maybe
uninstall event handler, access or operate query/output buffer or resize
event loop, we need a clean and safe context to do that. We pause IO
thread in `IOThreadBeforeSleep`, do some jobs and then resume it. To
avoid thread suspended, we use busy waiting to confirm the target
status. Besides we use atomic variable to make sure memory visibility
and ordering. We introduce these functions to pause/resume IO Threads as
below.
```
pauseIOThread, resumeIOThread
pauseAllIOThreads, resumeAllIOThreads
pauseIOThreadsRange, resumeIOThreadsRange
```
Testing has shown that `pauseIOThread` is highly efficient, allowing the
main thread to execute nearly 200,000 operations per second during
stress tests. Similarly, `pauseAllIOThreads` with 8 IO threads can
handle up to nearly 56,000 operations per second. But operations
performed between pausing and resuming IO threads must be quick;
otherwise, they could cause the IO threads to reach full CPU
utilization.

**freeClient and freeClientAsync**
The main thread may need to terminate a client currently running on an
IO thread, for example, due to ACL rule changes, reaching the output
buffer limit, or evicting a client. In such cases, we need to pause the
IO thread to safely operate on the client.

**maxclients and maxmemory-clients updating**
When adjusting `maxclients`, we need to resize the event loop for all IO
threads. Similarly, when modifying `maxmemory-clients`, we need to
traverse all clients to calculate their memory usage. To ensure safe
operations, we pause all IO threads during these adjustments.

**Client info reading**
The main thread may need to read a client’s fields to generate a
descriptive string, such as for the `CLIENT LIST` command or logging
purposes. In such cases, we need to pause the IO thread handling that
client. If information for all clients needs to be displayed, all IO
threads must be paused.

**Tracking redirect**
Redis supports the tracking feature and can even send invalidation
messages to a connection with a specified ID. But the target client may
be running on IO thread, directly manipulating the client’s output
buffer is not thread-safe, and the IO thread may not be aware that the
client requires a response. In such cases, we pause the IO thread
handling the client, modify the output buffer, and install a write event
handler to ensure proper handling.

**clientsCron**
In the `clientsCron` function, the main thread needs to traverse all
clients to perform operations such as timeout checks, verifying whether
they have reached the soft output buffer limit, resizing the
output/query buffer, or updating memory usage. To safely operate on a
client, the IO thread handling that client must be paused.
If we were to pause the IO thread for each client individually, the
efficiency would be very low. Conversely, pausing all IO threads
simultaneously would be costly, especially when there are many IO
threads, as clientsCron is invoked relatively frequently.
To address this, we adopted a batched approach for pausing IO threads.
At most, 8 IO threads are paused at a time. The operations mentioned
above are only performed on clients running in the paused IO threads,
significantly reducing overhead while maintaining safety.

### Observability
In the current design, the main thread always assigns clients to the IO
thread with the least clients. To clearly observe the number of clients
handled by each IO thread, we added the new section in INFO output. The
`INFO THREADS` section can show the client count for each IO thread.
```
# Threads
io_thread_0:clients=0
io_thread_1:clients=2
io_thread_2:clients=2
```

Additionally, in the `CLIENT LIST` output, we also added a field to
indicate the thread to which each client is assigned.

`id=244 addr=127.0.0.1:41870 laddr=127.0.0.1:6379 ... resp=2 lib-name=
lib-ver= io-thread=1`

## Trade-off
### Special Clients
For certain special types of clients, keeping them running on IO threads
would result in severe race issues that are difficult to resolve.
Therefore, we chose not to offload these clients to the IO threads.

For replica, monitor, subscribe, and tracking clients, main thread may
directly write them a reply when conditions are met. Race issues are
difficult to resolve, so we have them processed in the main thread. This
includes the Lua debug clients as well, since we may operate connection
directly.

For blocking client, after the IO thread reads and parses a command and
hands it over to the main thread, if the client is identified as a
blocking type, it will be remained in the main thread. Once the blocking
operation completes and the reply is generated, the client is
transferred back to the IO thread to send the reply and wait for event
triggers.

### Clients Eviction
To support client eviction, it is necessary to update each client’s
memory usage promptly during operations such as read, write, or command
execution. However, when a client operates on an IO thread, it is not
feasible to update the memory usage immediately due to the risk of data
races. As a result, memory usage can only be updated either in the main
thread while processing commands or in the `ClientsCron` periodically.
The downside of this approach is that updates might experience a delay
of up to one second, which could impact the precision of memory
management for eviction.

To avoid incorrectly evicting clients. We adopted a best-effort
compensation solution, when we decide to eviction a client, we update
its memory usage again before evicting, if the memory used by the client
does not decrease or memory usage bucket is not changed, then we will
evict it, otherwise, not evict it.

However, we have not completely solved this problem. Due to the delay in
memory usage updates, it may lead us to make incorrect decisions about
the need to evict clients.

### Defragment
In the majority of cases we do NOT use the data from argv directly in
the db.
1. key names
We store a copy that we allocate in the main thread, see `sdsdup()` in
`dbAdd()`.
2. hash key and value
We store key as hfield and store value as sds, see `hfieldNew()` and
`sdsdup()` in `hashTypeSet()`.
3. other datatypes
   They don't even use SDS, so there is no reference issues.

But in some cases client the data from argv may be retain by the main
thread.
As a result, during fragmentation cleanup, we need to move allocations
from the IO thread’s arena to the main thread’s arena. We always
allocate new memory in the main thread’s arena, but the memory released
by IO threads may not yet have been reclaimed. This ultimately causes
the fragmentation rate to be higher compared to creating and allocating
entirely within a single thread.
The following cases below will lead to memory allocated by the IO thread
being kept by the main thread.
1. string related command: `append`, `getset`, `mset` and `set`.
If `tryObjectEncoding()` does not change argv, we will keep it directly
in the main thread, see the code in `tryObjectEncoding()`(specifically
`trimStringObjectIfNeeded()`)
2. block related command.
    the key names will be kept in `c->db->blocking_keys`.
3. watch command
    the key names will be kept in `c->db->watched_keys`.
4. [s]subscribe command
    channel name will be kept in `serverPubSubChannels`.
5. script load command
    script will be kept in `server.lua_scripts`.
7. some module API: `RM_RetainString`, `RM_HoldString`

Those issues will be handled in other PRs.

## Testing
### Functional Testing
The commit with enabling IO Threads has passed all TCL tests, but we did
some changes:
**Client query buffer**: In the original code, when using a reusable
query buffer, ownership of the query buffer would be released after the
command was processed. However, with IO threads enabled, the client
transitions from an IO thread to the main thread for processing. This
causes the ownership release to occur earlier than the command
execution. As a result, when IO threads are enabled, the client's
information will never indicate that a shared query buffer is in use.
Therefore, we skip the corresponding query buffer tests in this case.
**Defragment**: Add a new defragmentation test to verify the effect of
io threads on defragmentation.
**Command delay**: For deferred clients in TCL tests, due to clients
being assigned to different threads for execution, delays may occur. To
address this, we introduced conditional waiting: the process proceeds to
the next step only when the `client list` contains the corresponding
commands.

### Sanitizer Testing
The commit passed all TCL tests and reported no errors when compiled
with the `fsanitizer=thread` and `fsanitizer=address` options enabled.
But we made the following modifications: we suppressed the sanitizer
warnings for clients with watched keys when updating `client->flags`, we
think IO threads read `client->flags`, but never modify it or read the
`CLIENT_DIRTY_CAS` bit, main thread just only modifies this bit, so
there is no actual data race.

## Others
### IO thread number
In the new multi-threaded design, the main thread is primarily focused
on command processing to improve performance. Typically, the main thread
does not handle regular client I/O operations but is responsible for
clients such as replication and tracking clients. To avoid breaking
changes, we still consider the main thread as the first IO thread.

When the io-threads configuration is set to a low value (e.g., 2),
performance does not show a significant improvement compared to a
single-threaded setup for simple commands (such as SET or GET), as the
main thread does not consume much CPU for these simple operations. This
results in underutilized multi-core capacity. However, for more complex
commands, having a low number of IO threads may still be beneficial.
Therefore, it’s important to adjust the `io-threads` based on your own
performance tests.

Additionally, you can clearly monitor the CPU utilization of the main
thread and IO threads using `top -H -p $redis_pid`. This allows you to
easily identify where the bottleneck is. If the IO thread is the
bottleneck, increasing the `io-threads` will improve performance. If the
main thread is the bottleneck, the overall performance can only be
scaled by increasing the number of shards or replicas.

---------

Co-authored-by: debing.sun <debing.sun@redis.com>
Co-authored-by: oranagra <oran@redislabs.com>
2024-12-23 14:16:40 +08:00

612 lines
22 KiB
Tcl

tags {"external:skip logreqres:skip"} {
# Get info about a redis client connection:
# name - name of client we want to query
# f - field name from "CLIENT LIST" we want to get
proc client_field {name f} {
set clients [split [string trim [r client list]] "\r\n"]
set c [lsearch -inline $clients *name=$name*]
if {![regexp $f=(\[a-zA-Z0-9-\]+) $c - res]} {
error "no client named $name found with field $f"
}
return $res
}
proc client_exists {name} {
if {[catch { client_field $name tot-mem } e]} {
return false
}
return true
}
proc gen_client {} {
set rr [redis_client]
set name "tst_[randstring 4 4 simplealpha]"
$rr client setname $name
assert {[client_exists $name]}
return [list $rr $name]
}
# Sum a value across all redis client connections:
# f - the field name from "CLIENT LIST" we want to sum
proc clients_sum {f} {
set sum 0
set clients [split [string trim [r client list]] "\r\n"]
foreach c $clients {
if {![regexp $f=(\[a-zA-Z0-9-\]+) $c - res]} {
error "field $f not found in $c"
}
incr sum $res
}
return $sum
}
proc mb {v} {
return [expr $v * 1024 * 1024]
}
proc kb {v} {
return [expr $v * 1024]
}
start_server {} {
set maxmemory_clients 3000000
r config set maxmemory-clients $maxmemory_clients
test "client evicted due to large argv" {
r flushdb
lassign [gen_client] rr cname
# Attempt a large multi-bulk command under eviction limit
$rr mset k v k2 [string repeat v 1000000]
assert_equal [$rr get k] v
# Attempt another command, now causing client eviction
catch { $rr mset k v k2 [string repeat v $maxmemory_clients] } e
assert {![client_exists $cname]}
$rr close
}
test "client evicted due to large query buf" {
r flushdb
lassign [gen_client] rr cname
# Attempt to fill the query buff without completing the argument above the limit, causing client eviction
catch {
$rr write [join [list "*1\r\n\$$maxmemory_clients\r\n" [string repeat v $maxmemory_clients]] ""]
$rr flush
$rr read
} e
assert {![client_exists $cname]}
$rr close
}
test "client evicted due to percentage of maxmemory" {
set maxmemory [mb 6]
r config set maxmemory $maxmemory
# Set client eviction threshold to 7% of maxmemory
set maxmemory_clients_p 7
r config set maxmemory-clients $maxmemory_clients_p%
r flushdb
set maxmemory_clients_actual [expr $maxmemory * $maxmemory_clients_p / 100]
lassign [gen_client] rr cname
# Attempt to fill the query buff with only half the percentage threshold verify we're not disconnected
set n [expr $maxmemory_clients_actual / 2]
$rr write [join [list "*1\r\n\$$n\r\n" [string repeat v $n]] ""]
$rr flush
wait_for_condition 100 10 {
[client_field $cname tot-mem] >= $n
} else {
fail "Failed to fill qbuf for test"
}
set tot_mem [client_field $cname tot-mem]
assert {$tot_mem >= $n && $tot_mem < $maxmemory_clients_actual}
# Attempt to fill the query buff with the percentage threshold of maxmemory and verify we're evicted
$rr close
lassign [gen_client] rr cname
catch {
$rr write [join [list "*1\r\n\$$maxmemory_clients_actual\r\n" [string repeat v $maxmemory_clients_actual]] ""]
$rr flush
} e
wait_for_condition 100 10 {
![client_exists $cname]
} else {
fail "Failed to evict client"
}
$rr close
# Restore settings
r config set maxmemory 0
r config set maxmemory-clients $maxmemory_clients
}
test "client evicted due to large multi buf" {
r flushdb
lassign [gen_client] rr cname
# Attempt a multi-exec where sum of commands is less than maxmemory_clients
$rr multi
$rr set k [string repeat v [expr $maxmemory_clients / 4]]
$rr set k [string repeat v [expr $maxmemory_clients / 4]]
assert_equal [$rr exec] {OK OK}
# Attempt a multi-exec where sum of commands is more than maxmemory_clients, causing client eviction
$rr multi
catch {
for {set j 0} {$j < 5} {incr j} {
$rr set k [string repeat v [expr $maxmemory_clients / 4]]
}
} e
assert {![client_exists $cname]}
$rr close
}
test "client evicted due to watched key list" {
r flushdb
set rr [redis_client]
# Since watched key list is a small overhead this test uses a minimal maxmemory-clients config
set temp_maxmemory_clients 200000
r config set maxmemory-clients $temp_maxmemory_clients
# Append watched keys until list maxes out maxmemory clients and causes client eviction
catch {
for {set j 0} {$j < $temp_maxmemory_clients} {incr j} {
$rr watch $j
}
} e
assert_match {I/O error reading reply} $e
$rr close
# Restore config for next tests
r config set maxmemory-clients $maxmemory_clients
}
test "client evicted due to pubsub subscriptions" {
r flushdb
# Since pubsub subscriptions cause a small overhead this test uses a minimal maxmemory-clients config
set temp_maxmemory_clients 200000
r config set maxmemory-clients $temp_maxmemory_clients
# Test eviction due to pubsub patterns
set rr [redis_client]
# Add patterns until list maxes out maxmemory clients and causes client eviction
catch {
for {set j 0} {$j < $temp_maxmemory_clients} {incr j} {
$rr psubscribe $j
}
} e
assert_match {I/O error reading reply} $e
$rr close
# Test eviction due to pubsub channels
set rr [redis_client]
# Subscribe to global channels until list maxes out maxmemory clients and causes client eviction
catch {
for {set j 0} {$j < $temp_maxmemory_clients} {incr j} {
$rr subscribe $j
}
} e
assert_match {I/O error reading reply} $e
$rr close
# Test eviction due to sharded pubsub channels
set rr [redis_client]
# Subscribe to sharded pubsub channels until list maxes out maxmemory clients and causes client eviction
catch {
for {set j 0} {$j < $temp_maxmemory_clients} {incr j} {
$rr ssubscribe $j
}
} e
assert_match {I/O error reading reply} $e
$rr close
# Restore config for next tests
r config set maxmemory-clients $maxmemory_clients
}
test "client evicted due to tracking redirection" {
r flushdb
set rr [redis_client]
set redirected_c [redis_client]
$redirected_c client setname redirected_client
set redir_id [$redirected_c client id]
$redirected_c SUBSCRIBE __redis__:invalidate
$rr client tracking on redirect $redir_id bcast
# Use a big key name to fill the redirected tracking client's buffer quickly
set key_length [expr 1024*200]
set long_key [string repeat k $key_length]
# Use a script so we won't need to pass the long key name when dirtying it in the loop
set script_sha [$rr script load "redis.call('incr', '$long_key')"]
# Pause serverCron so it won't update memory usage since we're testing the update logic when
# writing tracking redirection output
r debug pause-cron 1
# Read and write to same (long) key until redirected_client's buffers cause it to be evicted
catch {
while true {
set mem [client_field redirected_client tot-mem]
assert {$mem < $maxmemory_clients}
$rr evalsha $script_sha 0
}
} e
assert_match {no client named redirected_client found*} $e
r debug pause-cron 0
$rr close
$redirected_c close
} {0} {needs:debug}
test "client evicted due to client tracking prefixes" {
r flushdb
set rr [redis_client]
# Since tracking prefixes list is a small overhead this test uses a minimal maxmemory-clients config
set temp_maxmemory_clients 200000
r config set maxmemory-clients $temp_maxmemory_clients
# Append tracking prefixes until list maxes out maxmemory clients and causes client eviction
# Combine more prefixes in each command to speed up the test. Because we did not actually count
# the memory usage of all prefixes, see getClientMemoryUsage, so we can not use larger prefixes
# to speed up the test here.
catch {
for {set j 0} {$j < $temp_maxmemory_clients} {incr j} {
$rr client tracking on prefix [format a%09s $j] prefix [format b%09s $j] prefix [format c%09s $j] bcast
}
} e
assert_match {I/O error reading reply} $e
$rr close
# Restore config for next tests
r config set maxmemory-clients $maxmemory_clients
}
test "client evicted due to output buf" {
r flushdb
r setrange k 200000 v
set rr [redis_deferring_client]
$rr client setname test_client
$rr flush
assert {[$rr read] == "OK"}
# Attempt a large response under eviction limit
$rr get k
$rr flush
assert {[string length [$rr read]] == 200001}
set mem [client_field test_client tot-mem]
assert {$mem < $maxmemory_clients}
# Fill output buff in loop without reading it and make sure
# we're eventually disconnected, but before reaching maxmemory_clients
while true {
if { [catch {
set mem [client_field test_client tot-mem]
assert {$mem < $maxmemory_clients}
$rr get k
$rr flush
} e]} {
assert {![client_exists test_client]}
break
}
}
$rr close
}
foreach {no_evict} {on off} {
test "client no-evict $no_evict" {
r flushdb
r client setname control
r client no-evict on ;# Avoid evicting the main connection
lassign [gen_client] rr cname
$rr client no-evict $no_evict
# Overflow maxmemory-clients
set qbsize [expr {$maxmemory_clients + 1}]
if {[catch {
$rr write [join [list "*1\r\n\$$qbsize\r\n" [string repeat v $qbsize]] ""]
$rr flush
wait_for_condition 200 10 {
[client_field $cname qbuf] == $qbsize
} else {
fail "Failed to fill qbuf for test"
}
} e] && $no_evict == off} {
assert {![client_exists $cname]}
} elseif {$no_evict == on} {
assert {[client_field $cname tot-mem] > $maxmemory_clients}
}
$rr close
}
}
}
start_server {} {
set server_pid [s process_id]
set maxmemory_clients [mb 10]
set obuf_limit [mb 3]
r config set maxmemory-clients $maxmemory_clients
r config set client-output-buffer-limit "normal $obuf_limit 0 0"
test "avoid client eviction when client is freed by output buffer limit" {
r flushdb
set obuf_size [expr {$obuf_limit + [mb 1]}]
r setrange k $obuf_size v
set rr1 [redis_client]
$rr1 client setname "qbuf-client"
set rr2 [redis_deferring_client]
$rr2 client setname "obuf-client1"
assert_equal [$rr2 read] OK
set rr3 [redis_deferring_client]
$rr3 client setname "obuf-client2"
assert_equal [$rr3 read] OK
# Occupy client's query buff with less than output buffer limit left to exceed maxmemory-clients
set qbsize [expr {$maxmemory_clients - $obuf_size}]
$rr1 write [join [list "*1\r\n\$$qbsize\r\n" [string repeat v $qbsize]] ""]
$rr1 flush
# Wait for qbuff to be as expected
wait_for_condition 200 10 {
[client_field qbuf-client qbuf] == $qbsize
} else {
fail "Failed to fill qbuf for test"
}
# Make the other two obuf-clients pass obuf limit and also pass maxmemory-clients
# We use two obuf-clients to make sure that even if client eviction is attempted
# between two command processing (with no sleep) we don't perform any client eviction
# because the obuf limit is enforced with precedence.
pause_process $server_pid
$rr2 get k
$rr2 flush
$rr3 get k
$rr3 flush
resume_process $server_pid
r ping ;# make sure a full event loop cycle is processed before issuing CLIENT LIST
# wait for get commands to be processed
wait_for_condition 100 10 {
[expr {[regexp {calls=(\d+)} [cmdrstat get r] -> calls] ? $calls : 0}] >= 2
} else {
fail "get did not arrive"
}
# Validate obuf-clients were disconnected (because of obuf limit)
catch {client_field obuf-client1 name} e
assert_match {no client named obuf-client1 found*} $e
catch {client_field obuf-client2 name} e
assert_match {no client named obuf-client2 found*} $e
# Validate qbuf-client is still connected and wasn't evicted
if {[lindex [r config get io-threads] 1] == 1} {
assert_equal [client_field qbuf-client name] {qbuf-client}
}
$rr1 close
$rr2 close
$rr3 close
}
}
start_server {} {
test "decrease maxmemory-clients causes client eviction" {
set maxmemory_clients [mb 4]
set client_count 10
set qbsize [expr ($maxmemory_clients - [mb 1]) / $client_count]
r config set maxmemory-clients $maxmemory_clients
# Make multiple clients consume together roughly 1mb less than maxmemory_clients
set rrs {}
for {set j 0} {$j < $client_count} {incr j} {
set rr [redis_client]
lappend rrs $rr
$rr client setname client$j
$rr write [join [list "*2\r\n\$$qbsize\r\n" [string repeat v $qbsize]] ""]
$rr flush
wait_for_condition 200 10 {
[client_field client$j qbuf] >= $qbsize
} else {
fail "Failed to fill qbuf for test"
}
}
# Make sure all clients are still connected
set connected_clients [llength [lsearch -all [split [string trim [r client list]] "\r\n"] *name=client*]]
assert {$connected_clients == $client_count}
# Decrease maxmemory_clients and expect client eviction
r config set maxmemory-clients [expr $maxmemory_clients / 2]
wait_for_condition 200 10 {
[llength [regexp -all -inline {name=client} [r client list]]] < $client_count
} else {
fail "Failed to evict clients"
}
foreach rr $rrs {$rr close}
}
}
start_server {} {
test "evict clients only until below limit" {
set client_count 10
set client_mem [mb 1]
r debug replybuffer resizing 0
r config set maxmemory-clients 0
r client setname control
r client no-evict on
# Make multiple clients consume together roughly 1mb less than maxmemory_clients
set total_client_mem 0
set max_client_mem 0
set rrs {}
for {set j 0} {$j < $client_count} {incr j} {
set rr [redis_client]
lappend rrs $rr
$rr client setname client$j
$rr write [join [list "*2\r\n\$$client_mem\r\n" [string repeat v $client_mem]] ""]
$rr flush
wait_for_condition 200 10 {
[client_field client$j tot-mem] >= $client_mem
} else {
fail "Failed to fill qbuf for test"
}
# In theory all these clients should use the same amount of memory (~1mb). But in practice
# some allocators (libc) can return different allocation sizes for the same malloc argument causing
# some clients to use slightly more memory than others. We find the largest client and make sure
# all clients are roughly the same size (+-1%). Then we can safely set the client eviction limit and
# expect consistent results in the test.
set cmem [client_field client$j tot-mem]
if {$max_client_mem > 0} {
set size_ratio [expr $max_client_mem.0/$cmem.0]
assert_range $size_ratio 0.99 1.01
}
if {$cmem > $max_client_mem} {
set max_client_mem $cmem
}
}
# Make sure all clients are still connected
set connected_clients [llength [lsearch -all [split [string trim [r client list]] "\r\n"] *name=client*]]
assert {$connected_clients == $client_count}
# Set maxmemory-clients to accommodate half our clients (taking into account the control client)
set maxmemory_clients [expr ($max_client_mem * $client_count) / 2 + [client_field control tot-mem]]
r config set maxmemory-clients $maxmemory_clients
# Make sure total used memory is below maxmemory_clients
set total_client_mem [clients_sum tot-mem]
assert {$total_client_mem <= $maxmemory_clients}
# Make sure we have only half of our clients now
wait_for_condition 200 100 {
[llength [regexp -all -inline {name=client} [r client list]]] == $client_count / 2
} else {
fail "Failed to evict clients"
}
# Restore the reply buffer resize to default
r debug replybuffer resizing 1
foreach rr $rrs {$rr close}
} {} {needs:debug}
}
start_server {} {
test "evict clients in right order (large to small)" {
# Note that each size step needs to be at least x2 larger than previous step
# because of how the client-eviction size bucketing works
set sizes [list [kb 128] [mb 1] [mb 3]]
set clients_per_size 3
r client setname control
r client no-evict on
r config set maxmemory-clients 0
r debug replybuffer resizing 0
# Run over all sizes and create some clients using up that size
set total_client_mem 0
set rrs {}
for {set i 0} {$i < [llength $sizes]} {incr i} {
set size [lindex $sizes $i]
for {set j 0} {$j < $clients_per_size} {incr j} {
set rr [redis_client]
lappend rrs $rr
$rr client setname client-$i
$rr write [join [list "*2\r\n\$$size\r\n" [string repeat v $size]] ""]
$rr flush
}
set client_mem [client_field client-$i tot-mem]
# Update our size list based on actual used up size (this is usually
# slightly more than expected because of allocator bins
assert {$client_mem >= $size}
set sizes [lreplace $sizes $i $i $client_mem]
# Account total client memory usage
incr total_mem [expr $clients_per_size * $client_mem]
}
# Make sure all clients are connected
set clients [split [string trim [r client list]] "\r\n"]
for {set i 0} {$i < [llength $sizes]} {incr i} {
assert_equal [llength [lsearch -all $clients "*name=client-$i *"]] $clients_per_size
}
# For each size reduce maxmemory-clients so relevant clients should be evicted
# do this from largest to smallest
foreach size [lreverse $sizes] {
set control_mem [client_field control tot-mem]
set total_mem [expr $total_mem - $clients_per_size * $size]
# allow some tolerance when using io threads
r config set maxmemory-clients [expr $total_mem + $control_mem + 1000]
set clients [split [string trim [r client list]] "\r\n"]
# Verify only relevant clients were evicted
for {set i 0} {$i < [llength $sizes]} {incr i} {
set verify_size [lindex $sizes $i]
set count [llength [lsearch -all $clients "*name=client-$i *"]]
if {$verify_size < $size} {
assert_equal $count $clients_per_size
} else {
assert_equal $count 0
}
}
}
# Restore the reply buffer resize to default
r debug replybuffer resizing 1
foreach rr $rrs {$rr close}
} {} {needs:debug}
}
start_server {} {
foreach type {"client no-evict" "maxmemory-clients disabled"} {
r flushall
r client no-evict on
r config set maxmemory-clients 0
test "client total memory grows during $type" {
r setrange k [mb 1] v
set rr [redis_client]
$rr client setname test_client
if {$type eq "client no-evict"} {
$rr client no-evict on
r config set maxmemory-clients 1
}
$rr deferred 1
# Fill output buffer in loop without reading it and make sure
# the tot-mem of client has increased (OS buffers didn't swallow it)
# and eviction not occurring.
while {true} {
$rr get k
$rr flush
after 10
if {[client_field test_client tot-mem] > [mb 10]} {
break
}
}
# Trigger the client eviction, by flipping the no-evict flag to off
if {$type eq "client no-evict"} {
$rr client no-evict off
} else {
r config set maxmemory-clients 1
}
# wait for the client to be disconnected
wait_for_condition 5000 50 {
![client_exists test_client]
} else {
puts [r client list]
fail "client was not disconnected"
}
$rr close
}
}
}
} ;# tags