2013-08-29 10:23:57 -04:00
|
|
|
proc test_memory_efficiency {range} {
|
|
|
|
r flushall
|
2015-02-10 08:47:45 -05:00
|
|
|
set rd [redis_deferring_client]
|
2013-08-29 10:23:57 -04:00
|
|
|
set base_mem [s used_memory]
|
|
|
|
set written 0
|
|
|
|
for {set j 0} {$j < 10000} {incr j} {
|
|
|
|
set key key:$j
|
|
|
|
set val [string repeat A [expr {int(rand()*$range)}]]
|
2015-02-10 08:47:45 -05:00
|
|
|
$rd set $key $val
|
2013-08-29 10:23:57 -04:00
|
|
|
incr written [string length $key]
|
|
|
|
incr written [string length $val]
|
|
|
|
incr written 2 ;# A separator is the minimum to store key-value data.
|
|
|
|
}
|
2015-02-10 08:47:45 -05:00
|
|
|
for {set j 0} {$j < 10000} {incr j} {
|
|
|
|
$rd read ; # Discard replies
|
|
|
|
}
|
|
|
|
|
2013-08-29 10:23:57 -04:00
|
|
|
set current_mem [s used_memory]
|
|
|
|
set used [expr {$current_mem-$base_mem}]
|
|
|
|
set efficiency [expr {double($written)/$used}]
|
|
|
|
return $efficiency
|
|
|
|
}
|
|
|
|
|
2021-06-09 08:13:24 -04:00
|
|
|
start_server {tags {"memefficiency external:skip"}} {
|
2013-08-29 10:23:57 -04:00
|
|
|
foreach {size_range expected_min_efficiency} {
|
|
|
|
32 0.15
|
|
|
|
64 0.25
|
|
|
|
128 0.35
|
|
|
|
1024 0.75
|
2013-11-25 04:21:18 -05:00
|
|
|
16384 0.82
|
2013-08-29 10:23:57 -04:00
|
|
|
} {
|
|
|
|
test "Memory efficiency with values in range $size_range" {
|
|
|
|
set efficiency [test_memory_efficiency $size_range]
|
|
|
|
assert {$efficiency >= $expected_min_efficiency}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-30 15:53:13 -05:00
|
|
|
|
2020-04-16 04:05:03 -04:00
|
|
|
run_solo {defrag} {
|
Replace cluster metadata with slot specific dictionaries (#11695)
This is an implementation of https://github.com/redis/redis/issues/10589 that eliminates 16 bytes per entry in cluster mode, that are currently used to create a linked list between entries in the same slot. Main idea is splitting main dictionary into 16k smaller dictionaries (one per slot), so we can perform all slot specific operations, such as iteration, without any additional info in the `dictEntry`. For Redis cluster, the expectation is that there will be a larger number of keys, so the fixed overhead of 16k dictionaries will be The expire dictionary is also split up so that each slot is logically decoupled, so that in subsequent revisions we will be able to atomically flush a slot of data.
## Important changes
* Incremental rehashing - one big change here is that it's not one, but rather up to 16k dictionaries that can be rehashing at the same time, in order to keep track of them, we introduce a separate queue for dictionaries that are rehashing. Also instead of rehashing a single dictionary, cron job will now try to rehash as many as it can in 1ms.
* getRandomKey - now needs to not only select a random key, from the random bucket, but also needs to select a random dictionary. Fairness is a major concern here, as it's possible that keys can be unevenly distributed across the slots. In order to address this search we introduced binary index tree). With that data structure we are able to efficiently find a random slot using binary search in O(log^2(slot count)) time.
* Iteration efficiency - when iterating dictionary with a lot of empty slots, we want to skip them efficiently. We can do this using same binary index that is used for random key selection, this index allows us to find a slot for a specific key index. For example if there are 10 keys in the slot 0, then we can quickly find a slot that contains 11th key using binary search on top of the binary index tree.
* scan API - in order to perform a scan across the entire DB, the cursor now needs to not only save position within the dictionary but also the slot id. In this change we append slot id into LSB of the cursor so it can be passed around between client and the server. This has interesting side effect, now you'll be able to start scanning specific slot by simply providing slot id as a cursor value. The plan is to not document this as defined behavior, however. It's also worth nothing the SCAN API is now technically incompatible with previous versions, although practically we don't believe it's an issue.
* Checksum calculation optimizations - During command execution, we know that all of the keys are from the same slot (outside of a few notable exceptions such as cross slot scripts and modules). We don't want to compute the checksum multiple multiple times, hence we are relying on cached slot id in the client during the command executions. All operations that access random keys, either should pass in the known slot or recompute the slot.
* Slot info in RDB - in order to resize individual dictionaries correctly, while loading RDB, it's not enough to know total number of keys (of course we could approximate number of keys per slot, but it won't be precise). To address this issue, we've added additional metadata into RDB that contains number of keys in each slot, which can be used as a hint during loading.
* DB size - besides `DBSIZE` API, we need to know size of the DB in many places want, in order to avoid scanning all dictionaries and summing up their sizes in a loop, we've introduced a new field into `redisDb` that keeps track of `key_count`. This way we can keep DBSIZE operation O(1). This is also kept for O(1) expires computation as well.
## Performance
This change improves SET performance in cluster mode by ~5%, most of the gains come from us not having to maintain linked lists for keys in slot, non-cluster mode has same performance. For workloads that rely on evictions, the performance is similar because of the extra overhead for finding keys to evict.
RDB loading performance is slightly reduced, as the slot of each key needs to be computed during the load.
## Interface changes
* Removed `overhead.hashtable.slot-to-keys` to `MEMORY STATS`
* Scan API will now require 64 bits to store the cursor, even on 32 bit systems, as the slot information will be stored.
* New RDB version to support the new op code for SLOT information.
---------
Co-authored-by: Vitaly Arbuzov <arvit@amazon.com>
Co-authored-by: Harkrishn Patro <harkrisp@amazon.com>
Co-authored-by: Roshan Khatri <rvkhatri@amazon.com>
Co-authored-by: Madelyn Olson <madelyneolson@gmail.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
2023-10-15 02:58:26 -04:00
|
|
|
proc test_active_defrag {type} {
|
2021-01-08 03:03:21 -05:00
|
|
|
if {[string match {*jemalloc*} [s mem_allocator]] && [r debug mallctl arenas.page] <= 8192} {
|
Replace cluster metadata with slot specific dictionaries (#11695)
This is an implementation of https://github.com/redis/redis/issues/10589 that eliminates 16 bytes per entry in cluster mode, that are currently used to create a linked list between entries in the same slot. Main idea is splitting main dictionary into 16k smaller dictionaries (one per slot), so we can perform all slot specific operations, such as iteration, without any additional info in the `dictEntry`. For Redis cluster, the expectation is that there will be a larger number of keys, so the fixed overhead of 16k dictionaries will be The expire dictionary is also split up so that each slot is logically decoupled, so that in subsequent revisions we will be able to atomically flush a slot of data.
## Important changes
* Incremental rehashing - one big change here is that it's not one, but rather up to 16k dictionaries that can be rehashing at the same time, in order to keep track of them, we introduce a separate queue for dictionaries that are rehashing. Also instead of rehashing a single dictionary, cron job will now try to rehash as many as it can in 1ms.
* getRandomKey - now needs to not only select a random key, from the random bucket, but also needs to select a random dictionary. Fairness is a major concern here, as it's possible that keys can be unevenly distributed across the slots. In order to address this search we introduced binary index tree). With that data structure we are able to efficiently find a random slot using binary search in O(log^2(slot count)) time.
* Iteration efficiency - when iterating dictionary with a lot of empty slots, we want to skip them efficiently. We can do this using same binary index that is used for random key selection, this index allows us to find a slot for a specific key index. For example if there are 10 keys in the slot 0, then we can quickly find a slot that contains 11th key using binary search on top of the binary index tree.
* scan API - in order to perform a scan across the entire DB, the cursor now needs to not only save position within the dictionary but also the slot id. In this change we append slot id into LSB of the cursor so it can be passed around between client and the server. This has interesting side effect, now you'll be able to start scanning specific slot by simply providing slot id as a cursor value. The plan is to not document this as defined behavior, however. It's also worth nothing the SCAN API is now technically incompatible with previous versions, although practically we don't believe it's an issue.
* Checksum calculation optimizations - During command execution, we know that all of the keys are from the same slot (outside of a few notable exceptions such as cross slot scripts and modules). We don't want to compute the checksum multiple multiple times, hence we are relying on cached slot id in the client during the command executions. All operations that access random keys, either should pass in the known slot or recompute the slot.
* Slot info in RDB - in order to resize individual dictionaries correctly, while loading RDB, it's not enough to know total number of keys (of course we could approximate number of keys per slot, but it won't be precise). To address this issue, we've added additional metadata into RDB that contains number of keys in each slot, which can be used as a hint during loading.
* DB size - besides `DBSIZE` API, we need to know size of the DB in many places want, in order to avoid scanning all dictionaries and summing up their sizes in a loop, we've introduced a new field into `redisDb` that keeps track of `key_count`. This way we can keep DBSIZE operation O(1). This is also kept for O(1) expires computation as well.
## Performance
This change improves SET performance in cluster mode by ~5%, most of the gains come from us not having to maintain linked lists for keys in slot, non-cluster mode has same performance. For workloads that rely on evictions, the performance is similar because of the extra overhead for finding keys to evict.
RDB loading performance is slightly reduced, as the slot of each key needs to be computed during the load.
## Interface changes
* Removed `overhead.hashtable.slot-to-keys` to `MEMORY STATS`
* Scan API will now require 64 bits to store the cursor, even on 32 bit systems, as the slot information will be stored.
* New RDB version to support the new op code for SLOT information.
---------
Co-authored-by: Vitaly Arbuzov <arvit@amazon.com>
Co-authored-by: Harkrishn Patro <harkrisp@amazon.com>
Co-authored-by: Roshan Khatri <rvkhatri@amazon.com>
Co-authored-by: Madelyn Olson <madelyneolson@gmail.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
2023-10-15 02:58:26 -04:00
|
|
|
test "Active defrag main dictionary: $type" {
|
2020-02-23 05:46:14 -05:00
|
|
|
r config set hz 100
|
2018-02-18 10:36:21 -05:00
|
|
|
r config set activedefrag no
|
|
|
|
r config set active-defrag-threshold-lower 5
|
2018-07-18 03:16:33 -04:00
|
|
|
r config set active-defrag-cycle-min 65
|
2018-02-18 10:36:21 -05:00
|
|
|
r config set active-defrag-cycle-max 75
|
|
|
|
r config set active-defrag-ignore-bytes 2mb
|
|
|
|
r config set maxmemory 100mb
|
|
|
|
r config set maxmemory-policy allkeys-lru
|
2020-09-03 01:47:29 -04:00
|
|
|
|
|
|
|
populate 700000 asdf1 150
|
Replace cluster metadata with slot specific dictionaries (#11695)
This is an implementation of https://github.com/redis/redis/issues/10589 that eliminates 16 bytes per entry in cluster mode, that are currently used to create a linked list between entries in the same slot. Main idea is splitting main dictionary into 16k smaller dictionaries (one per slot), so we can perform all slot specific operations, such as iteration, without any additional info in the `dictEntry`. For Redis cluster, the expectation is that there will be a larger number of keys, so the fixed overhead of 16k dictionaries will be The expire dictionary is also split up so that each slot is logically decoupled, so that in subsequent revisions we will be able to atomically flush a slot of data.
## Important changes
* Incremental rehashing - one big change here is that it's not one, but rather up to 16k dictionaries that can be rehashing at the same time, in order to keep track of them, we introduce a separate queue for dictionaries that are rehashing. Also instead of rehashing a single dictionary, cron job will now try to rehash as many as it can in 1ms.
* getRandomKey - now needs to not only select a random key, from the random bucket, but also needs to select a random dictionary. Fairness is a major concern here, as it's possible that keys can be unevenly distributed across the slots. In order to address this search we introduced binary index tree). With that data structure we are able to efficiently find a random slot using binary search in O(log^2(slot count)) time.
* Iteration efficiency - when iterating dictionary with a lot of empty slots, we want to skip them efficiently. We can do this using same binary index that is used for random key selection, this index allows us to find a slot for a specific key index. For example if there are 10 keys in the slot 0, then we can quickly find a slot that contains 11th key using binary search on top of the binary index tree.
* scan API - in order to perform a scan across the entire DB, the cursor now needs to not only save position within the dictionary but also the slot id. In this change we append slot id into LSB of the cursor so it can be passed around between client and the server. This has interesting side effect, now you'll be able to start scanning specific slot by simply providing slot id as a cursor value. The plan is to not document this as defined behavior, however. It's also worth nothing the SCAN API is now technically incompatible with previous versions, although practically we don't believe it's an issue.
* Checksum calculation optimizations - During command execution, we know that all of the keys are from the same slot (outside of a few notable exceptions such as cross slot scripts and modules). We don't want to compute the checksum multiple multiple times, hence we are relying on cached slot id in the client during the command executions. All operations that access random keys, either should pass in the known slot or recompute the slot.
* Slot info in RDB - in order to resize individual dictionaries correctly, while loading RDB, it's not enough to know total number of keys (of course we could approximate number of keys per slot, but it won't be precise). To address this issue, we've added additional metadata into RDB that contains number of keys in each slot, which can be used as a hint during loading.
* DB size - besides `DBSIZE` API, we need to know size of the DB in many places want, in order to avoid scanning all dictionaries and summing up their sizes in a loop, we've introduced a new field into `redisDb` that keeps track of `key_count`. This way we can keep DBSIZE operation O(1). This is also kept for O(1) expires computation as well.
## Performance
This change improves SET performance in cluster mode by ~5%, most of the gains come from us not having to maintain linked lists for keys in slot, non-cluster mode has same performance. For workloads that rely on evictions, the performance is similar because of the extra overhead for finding keys to evict.
RDB loading performance is slightly reduced, as the slot of each key needs to be computed during the load.
## Interface changes
* Removed `overhead.hashtable.slot-to-keys` to `MEMORY STATS`
* Scan API will now require 64 bits to store the cursor, even on 32 bit systems, as the slot information will be stored.
* New RDB version to support the new op code for SLOT information.
---------
Co-authored-by: Vitaly Arbuzov <arvit@amazon.com>
Co-authored-by: Harkrishn Patro <harkrisp@amazon.com>
Co-authored-by: Roshan Khatri <rvkhatri@amazon.com>
Co-authored-by: Madelyn Olson <madelyneolson@gmail.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
2023-10-15 02:58:26 -04:00
|
|
|
populate 100 asdf1 150 0 false 1000
|
2020-09-03 01:47:29 -04:00
|
|
|
populate 170000 asdf2 300
|
Replace cluster metadata with slot specific dictionaries (#11695)
This is an implementation of https://github.com/redis/redis/issues/10589 that eliminates 16 bytes per entry in cluster mode, that are currently used to create a linked list between entries in the same slot. Main idea is splitting main dictionary into 16k smaller dictionaries (one per slot), so we can perform all slot specific operations, such as iteration, without any additional info in the `dictEntry`. For Redis cluster, the expectation is that there will be a larger number of keys, so the fixed overhead of 16k dictionaries will be The expire dictionary is also split up so that each slot is logically decoupled, so that in subsequent revisions we will be able to atomically flush a slot of data.
## Important changes
* Incremental rehashing - one big change here is that it's not one, but rather up to 16k dictionaries that can be rehashing at the same time, in order to keep track of them, we introduce a separate queue for dictionaries that are rehashing. Also instead of rehashing a single dictionary, cron job will now try to rehash as many as it can in 1ms.
* getRandomKey - now needs to not only select a random key, from the random bucket, but also needs to select a random dictionary. Fairness is a major concern here, as it's possible that keys can be unevenly distributed across the slots. In order to address this search we introduced binary index tree). With that data structure we are able to efficiently find a random slot using binary search in O(log^2(slot count)) time.
* Iteration efficiency - when iterating dictionary with a lot of empty slots, we want to skip them efficiently. We can do this using same binary index that is used for random key selection, this index allows us to find a slot for a specific key index. For example if there are 10 keys in the slot 0, then we can quickly find a slot that contains 11th key using binary search on top of the binary index tree.
* scan API - in order to perform a scan across the entire DB, the cursor now needs to not only save position within the dictionary but also the slot id. In this change we append slot id into LSB of the cursor so it can be passed around between client and the server. This has interesting side effect, now you'll be able to start scanning specific slot by simply providing slot id as a cursor value. The plan is to not document this as defined behavior, however. It's also worth nothing the SCAN API is now technically incompatible with previous versions, although practically we don't believe it's an issue.
* Checksum calculation optimizations - During command execution, we know that all of the keys are from the same slot (outside of a few notable exceptions such as cross slot scripts and modules). We don't want to compute the checksum multiple multiple times, hence we are relying on cached slot id in the client during the command executions. All operations that access random keys, either should pass in the known slot or recompute the slot.
* Slot info in RDB - in order to resize individual dictionaries correctly, while loading RDB, it's not enough to know total number of keys (of course we could approximate number of keys per slot, but it won't be precise). To address this issue, we've added additional metadata into RDB that contains number of keys in each slot, which can be used as a hint during loading.
* DB size - besides `DBSIZE` API, we need to know size of the DB in many places want, in order to avoid scanning all dictionaries and summing up their sizes in a loop, we've introduced a new field into `redisDb` that keeps track of `key_count`. This way we can keep DBSIZE operation O(1). This is also kept for O(1) expires computation as well.
## Performance
This change improves SET performance in cluster mode by ~5%, most of the gains come from us not having to maintain linked lists for keys in slot, non-cluster mode has same performance. For workloads that rely on evictions, the performance is similar because of the extra overhead for finding keys to evict.
RDB loading performance is slightly reduced, as the slot of each key needs to be computed during the load.
## Interface changes
* Removed `overhead.hashtable.slot-to-keys` to `MEMORY STATS`
* Scan API will now require 64 bits to store the cursor, even on 32 bit systems, as the slot information will be stored.
* New RDB version to support the new op code for SLOT information.
---------
Co-authored-by: Vitaly Arbuzov <arvit@amazon.com>
Co-authored-by: Harkrishn Patro <harkrisp@amazon.com>
Co-authored-by: Roshan Khatri <rvkhatri@amazon.com>
Co-authored-by: Madelyn Olson <madelyneolson@gmail.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
2023-10-15 02:58:26 -04:00
|
|
|
populate 100 asdf2 300 0 false 1000
|
|
|
|
|
|
|
|
assert {[scan [regexp -inline {expires\=([\d]*)} [r info keyspace]] expires=%d] > 0}
|
2018-05-17 02:52:00 -04:00
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
2018-02-18 10:36:21 -05:00
|
|
|
set frag [s allocator_frag_ratio]
|
|
|
|
if {$::verbose} {
|
|
|
|
puts "frag $frag"
|
|
|
|
}
|
|
|
|
assert {$frag >= 1.4}
|
2020-02-23 05:46:14 -05:00
|
|
|
|
|
|
|
r config set latency-monitor-threshold 5
|
|
|
|
r latency reset
|
2020-02-26 01:12:07 -05:00
|
|
|
r config set maxmemory 110mb ;# prevent further eviction (not to fail the digest test)
|
2021-12-19 10:41:51 -05:00
|
|
|
set digest [debug_digest]
|
2018-05-24 12:04:17 -04:00
|
|
|
catch {r config set activedefrag yes} e
|
2020-12-14 04:13:46 -05:00
|
|
|
if {[r config get activedefrag] eq "activedefrag yes"} {
|
2018-05-24 12:04:17 -04:00
|
|
|
# Wait for the active defrag to start working (decision once a
|
|
|
|
# second).
|
|
|
|
wait_for_condition 50 100 {
|
2023-10-22 04:56:45 -04:00
|
|
|
[s total_active_defrag_time] ne 0
|
2018-05-24 12:04:17 -04:00
|
|
|
} else {
|
2023-10-22 04:56:45 -04:00
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
puts [r info memory]
|
|
|
|
puts [r info stats]
|
|
|
|
puts [r memory malloc-stats]
|
2018-05-24 12:04:17 -04:00
|
|
|
fail "defrag not started."
|
|
|
|
}
|
2018-02-18 10:36:21 -05:00
|
|
|
|
2024-02-06 06:39:07 -05:00
|
|
|
# This test usually runs for a while, during this interval, we test the range.
|
|
|
|
assert_range [s active_defrag_running] 65 75
|
|
|
|
r config set active-defrag-cycle-min 1
|
|
|
|
r config set active-defrag-cycle-max 1
|
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
assert_range [s active_defrag_running] 1 1
|
|
|
|
r config set active-defrag-cycle-min 65
|
|
|
|
r config set active-defrag-cycle-max 75
|
|
|
|
|
2018-05-24 12:04:17 -04:00
|
|
|
# Wait for the active defrag to stop working.
|
2021-08-30 05:39:09 -04:00
|
|
|
wait_for_condition 2000 100 {
|
2018-05-24 12:04:17 -04:00
|
|
|
[s active_defrag_running] eq 0
|
|
|
|
} else {
|
2018-07-18 03:16:33 -04:00
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
2018-05-24 12:04:17 -04:00
|
|
|
puts [r info memory]
|
|
|
|
puts [r memory malloc-stats]
|
|
|
|
fail "defrag didn't stop."
|
|
|
|
}
|
2018-02-18 10:36:21 -05:00
|
|
|
|
2022-03-09 06:55:17 -05:00
|
|
|
# Test the fragmentation is lower.
|
2018-05-24 12:04:17 -04:00
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
set frag [s allocator_frag_ratio]
|
2020-02-23 05:46:14 -05:00
|
|
|
set max_latency 0
|
|
|
|
foreach event [r latency latest] {
|
|
|
|
lassign $event eventname time latency max
|
|
|
|
if {$eventname == "active-defrag-cycle"} {
|
|
|
|
set max_latency $max
|
|
|
|
}
|
|
|
|
}
|
2018-05-24 12:04:17 -04:00
|
|
|
if {$::verbose} {
|
|
|
|
puts "frag $frag"
|
2020-05-20 07:08:40 -04:00
|
|
|
set misses [s active_defrag_misses]
|
|
|
|
set hits [s active_defrag_hits]
|
|
|
|
puts "hits: $hits"
|
|
|
|
puts "misses: $misses"
|
2020-02-23 05:46:14 -05:00
|
|
|
puts "max latency $max_latency"
|
|
|
|
puts [r latency latest]
|
|
|
|
puts [r latency history active-defrag-cycle]
|
2018-05-24 12:04:17 -04:00
|
|
|
}
|
|
|
|
assert {$frag < 1.1}
|
2020-02-23 05:46:14 -05:00
|
|
|
# due to high fragmentation, 100hz, and active-defrag-cycle-max set to 75,
|
|
|
|
# we expect max latency to be not much higher than 7.5ms but due to rare slowness threshold is set higher
|
2020-10-22 04:10:53 -04:00
|
|
|
if {!$::no_latency} {
|
|
|
|
assert {$max_latency <= 30}
|
|
|
|
}
|
2018-02-18 10:36:21 -05:00
|
|
|
}
|
2020-02-23 05:46:14 -05:00
|
|
|
# verify the data isn't corrupted or changed
|
2021-12-19 10:41:51 -05:00
|
|
|
set newdigest [debug_digest]
|
2020-02-23 05:46:14 -05:00
|
|
|
assert {$digest eq $newdigest}
|
|
|
|
r save ;# saving an rdb iterates over all the data / pointers
|
2020-09-03 01:47:29 -04:00
|
|
|
|
|
|
|
# if defrag is supported, test AOF loading too
|
Replace cluster metadata with slot specific dictionaries (#11695)
This is an implementation of https://github.com/redis/redis/issues/10589 that eliminates 16 bytes per entry in cluster mode, that are currently used to create a linked list between entries in the same slot. Main idea is splitting main dictionary into 16k smaller dictionaries (one per slot), so we can perform all slot specific operations, such as iteration, without any additional info in the `dictEntry`. For Redis cluster, the expectation is that there will be a larger number of keys, so the fixed overhead of 16k dictionaries will be The expire dictionary is also split up so that each slot is logically decoupled, so that in subsequent revisions we will be able to atomically flush a slot of data.
## Important changes
* Incremental rehashing - one big change here is that it's not one, but rather up to 16k dictionaries that can be rehashing at the same time, in order to keep track of them, we introduce a separate queue for dictionaries that are rehashing. Also instead of rehashing a single dictionary, cron job will now try to rehash as many as it can in 1ms.
* getRandomKey - now needs to not only select a random key, from the random bucket, but also needs to select a random dictionary. Fairness is a major concern here, as it's possible that keys can be unevenly distributed across the slots. In order to address this search we introduced binary index tree). With that data structure we are able to efficiently find a random slot using binary search in O(log^2(slot count)) time.
* Iteration efficiency - when iterating dictionary with a lot of empty slots, we want to skip them efficiently. We can do this using same binary index that is used for random key selection, this index allows us to find a slot for a specific key index. For example if there are 10 keys in the slot 0, then we can quickly find a slot that contains 11th key using binary search on top of the binary index tree.
* scan API - in order to perform a scan across the entire DB, the cursor now needs to not only save position within the dictionary but also the slot id. In this change we append slot id into LSB of the cursor so it can be passed around between client and the server. This has interesting side effect, now you'll be able to start scanning specific slot by simply providing slot id as a cursor value. The plan is to not document this as defined behavior, however. It's also worth nothing the SCAN API is now technically incompatible with previous versions, although practically we don't believe it's an issue.
* Checksum calculation optimizations - During command execution, we know that all of the keys are from the same slot (outside of a few notable exceptions such as cross slot scripts and modules). We don't want to compute the checksum multiple multiple times, hence we are relying on cached slot id in the client during the command executions. All operations that access random keys, either should pass in the known slot or recompute the slot.
* Slot info in RDB - in order to resize individual dictionaries correctly, while loading RDB, it's not enough to know total number of keys (of course we could approximate number of keys per slot, but it won't be precise). To address this issue, we've added additional metadata into RDB that contains number of keys in each slot, which can be used as a hint during loading.
* DB size - besides `DBSIZE` API, we need to know size of the DB in many places want, in order to avoid scanning all dictionaries and summing up their sizes in a loop, we've introduced a new field into `redisDb` that keeps track of `key_count`. This way we can keep DBSIZE operation O(1). This is also kept for O(1) expires computation as well.
## Performance
This change improves SET performance in cluster mode by ~5%, most of the gains come from us not having to maintain linked lists for keys in slot, non-cluster mode has same performance. For workloads that rely on evictions, the performance is similar because of the extra overhead for finding keys to evict.
RDB loading performance is slightly reduced, as the slot of each key needs to be computed during the load.
## Interface changes
* Removed `overhead.hashtable.slot-to-keys` to `MEMORY STATS`
* Scan API will now require 64 bits to store the cursor, even on 32 bit systems, as the slot information will be stored.
* New RDB version to support the new op code for SLOT information.
---------
Co-authored-by: Vitaly Arbuzov <arvit@amazon.com>
Co-authored-by: Harkrishn Patro <harkrisp@amazon.com>
Co-authored-by: Roshan Khatri <rvkhatri@amazon.com>
Co-authored-by: Madelyn Olson <madelyneolson@gmail.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
2023-10-15 02:58:26 -04:00
|
|
|
if {[r config get activedefrag] eq "activedefrag yes" && $type eq "standalone"} {
|
2023-03-04 05:54:36 -05:00
|
|
|
test "Active defrag - AOF loading" {
|
2020-09-03 01:47:29 -04:00
|
|
|
# reset stats and load the AOF file
|
|
|
|
r config resetstat
|
2022-12-09 06:33:38 -05:00
|
|
|
r config set key-load-delay -25 ;# sleep on average 1/25 usec
|
2020-09-03 01:47:29 -04:00
|
|
|
r debug loadaof
|
|
|
|
r config set activedefrag no
|
|
|
|
# measure hits and misses right after aof loading
|
|
|
|
set misses [s active_defrag_misses]
|
|
|
|
set hits [s active_defrag_hits]
|
|
|
|
|
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
set frag [s allocator_frag_ratio]
|
|
|
|
set max_latency 0
|
|
|
|
foreach event [r latency latest] {
|
|
|
|
lassign $event eventname time latency max
|
2021-11-02 09:52:56 -04:00
|
|
|
if {$eventname == "while-blocked-cron"} {
|
2020-09-03 01:47:29 -04:00
|
|
|
set max_latency $max
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if {$::verbose} {
|
|
|
|
puts "AOF loading:"
|
|
|
|
puts "frag $frag"
|
|
|
|
puts "hits: $hits"
|
|
|
|
puts "misses: $misses"
|
|
|
|
puts "max latency $max_latency"
|
|
|
|
puts [r latency latest]
|
2021-11-02 09:52:56 -04:00
|
|
|
puts [r latency history "while-blocked-cron"]
|
2020-09-03 01:47:29 -04:00
|
|
|
}
|
|
|
|
# make sure we had defrag hits during AOF loading
|
|
|
|
assert {$hits > 100000}
|
|
|
|
# make sure the defragger did enough work to keep the fragmentation low during loading.
|
|
|
|
# we cannot check that it went all the way down, since we don't wait for full defrag cycle to complete.
|
|
|
|
assert {$frag < 1.4}
|
2023-03-04 05:54:36 -05:00
|
|
|
# since the AOF contains simple (fast) SET commands (and the cron during loading runs every 1024 commands),
|
2020-09-03 01:47:29 -04:00
|
|
|
# it'll still not block the loading for long periods of time.
|
2020-10-22 04:10:53 -04:00
|
|
|
if {!$::no_latency} {
|
2023-03-04 05:54:36 -05:00
|
|
|
assert {$max_latency <= 40}
|
2020-10-22 04:10:53 -04:00
|
|
|
}
|
2020-09-03 01:47:29 -04:00
|
|
|
}
|
2023-03-04 05:54:36 -05:00
|
|
|
} ;# Active defrag - AOF loading
|
2020-09-03 01:47:29 -04:00
|
|
|
}
|
|
|
|
r config set appendonly no
|
|
|
|
r config set key-load-delay 0
|
Replace cluster metadata with slot specific dictionaries (#11695)
This is an implementation of https://github.com/redis/redis/issues/10589 that eliminates 16 bytes per entry in cluster mode, that are currently used to create a linked list between entries in the same slot. Main idea is splitting main dictionary into 16k smaller dictionaries (one per slot), so we can perform all slot specific operations, such as iteration, without any additional info in the `dictEntry`. For Redis cluster, the expectation is that there will be a larger number of keys, so the fixed overhead of 16k dictionaries will be The expire dictionary is also split up so that each slot is logically decoupled, so that in subsequent revisions we will be able to atomically flush a slot of data.
## Important changes
* Incremental rehashing - one big change here is that it's not one, but rather up to 16k dictionaries that can be rehashing at the same time, in order to keep track of them, we introduce a separate queue for dictionaries that are rehashing. Also instead of rehashing a single dictionary, cron job will now try to rehash as many as it can in 1ms.
* getRandomKey - now needs to not only select a random key, from the random bucket, but also needs to select a random dictionary. Fairness is a major concern here, as it's possible that keys can be unevenly distributed across the slots. In order to address this search we introduced binary index tree). With that data structure we are able to efficiently find a random slot using binary search in O(log^2(slot count)) time.
* Iteration efficiency - when iterating dictionary with a lot of empty slots, we want to skip them efficiently. We can do this using same binary index that is used for random key selection, this index allows us to find a slot for a specific key index. For example if there are 10 keys in the slot 0, then we can quickly find a slot that contains 11th key using binary search on top of the binary index tree.
* scan API - in order to perform a scan across the entire DB, the cursor now needs to not only save position within the dictionary but also the slot id. In this change we append slot id into LSB of the cursor so it can be passed around between client and the server. This has interesting side effect, now you'll be able to start scanning specific slot by simply providing slot id as a cursor value. The plan is to not document this as defined behavior, however. It's also worth nothing the SCAN API is now technically incompatible with previous versions, although practically we don't believe it's an issue.
* Checksum calculation optimizations - During command execution, we know that all of the keys are from the same slot (outside of a few notable exceptions such as cross slot scripts and modules). We don't want to compute the checksum multiple multiple times, hence we are relying on cached slot id in the client during the command executions. All operations that access random keys, either should pass in the known slot or recompute the slot.
* Slot info in RDB - in order to resize individual dictionaries correctly, while loading RDB, it's not enough to know total number of keys (of course we could approximate number of keys per slot, but it won't be precise). To address this issue, we've added additional metadata into RDB that contains number of keys in each slot, which can be used as a hint during loading.
* DB size - besides `DBSIZE` API, we need to know size of the DB in many places want, in order to avoid scanning all dictionaries and summing up their sizes in a loop, we've introduced a new field into `redisDb` that keeps track of `key_count`. This way we can keep DBSIZE operation O(1). This is also kept for O(1) expires computation as well.
## Performance
This change improves SET performance in cluster mode by ~5%, most of the gains come from us not having to maintain linked lists for keys in slot, non-cluster mode has same performance. For workloads that rely on evictions, the performance is similar because of the extra overhead for finding keys to evict.
RDB loading performance is slightly reduced, as the slot of each key needs to be computed during the load.
## Interface changes
* Removed `overhead.hashtable.slot-to-keys` to `MEMORY STATS`
* Scan API will now require 64 bits to store the cursor, even on 32 bit systems, as the slot information will be stored.
* New RDB version to support the new op code for SLOT information.
---------
Co-authored-by: Vitaly Arbuzov <arvit@amazon.com>
Co-authored-by: Harkrishn Patro <harkrisp@amazon.com>
Co-authored-by: Roshan Khatri <rvkhatri@amazon.com>
Co-authored-by: Madelyn Olson <madelyneolson@gmail.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
2023-10-15 02:58:26 -04:00
|
|
|
|
|
|
|
test "Active defrag eval scripts: $type" {
|
2022-02-11 14:58:05 -05:00
|
|
|
r flushdb
|
|
|
|
r script flush sync
|
|
|
|
r config resetstat
|
|
|
|
r config set hz 100
|
|
|
|
r config set activedefrag no
|
|
|
|
r config set active-defrag-threshold-lower 5
|
|
|
|
r config set active-defrag-cycle-min 65
|
|
|
|
r config set active-defrag-cycle-max 75
|
2022-02-21 02:37:25 -05:00
|
|
|
r config set active-defrag-ignore-bytes 1500kb
|
2022-02-11 14:58:05 -05:00
|
|
|
r config set maxmemory 0
|
Replace cluster metadata with slot specific dictionaries (#11695)
This is an implementation of https://github.com/redis/redis/issues/10589 that eliminates 16 bytes per entry in cluster mode, that are currently used to create a linked list between entries in the same slot. Main idea is splitting main dictionary into 16k smaller dictionaries (one per slot), so we can perform all slot specific operations, such as iteration, without any additional info in the `dictEntry`. For Redis cluster, the expectation is that there will be a larger number of keys, so the fixed overhead of 16k dictionaries will be The expire dictionary is also split up so that each slot is logically decoupled, so that in subsequent revisions we will be able to atomically flush a slot of data.
## Important changes
* Incremental rehashing - one big change here is that it's not one, but rather up to 16k dictionaries that can be rehashing at the same time, in order to keep track of them, we introduce a separate queue for dictionaries that are rehashing. Also instead of rehashing a single dictionary, cron job will now try to rehash as many as it can in 1ms.
* getRandomKey - now needs to not only select a random key, from the random bucket, but also needs to select a random dictionary. Fairness is a major concern here, as it's possible that keys can be unevenly distributed across the slots. In order to address this search we introduced binary index tree). With that data structure we are able to efficiently find a random slot using binary search in O(log^2(slot count)) time.
* Iteration efficiency - when iterating dictionary with a lot of empty slots, we want to skip them efficiently. We can do this using same binary index that is used for random key selection, this index allows us to find a slot for a specific key index. For example if there are 10 keys in the slot 0, then we can quickly find a slot that contains 11th key using binary search on top of the binary index tree.
* scan API - in order to perform a scan across the entire DB, the cursor now needs to not only save position within the dictionary but also the slot id. In this change we append slot id into LSB of the cursor so it can be passed around between client and the server. This has interesting side effect, now you'll be able to start scanning specific slot by simply providing slot id as a cursor value. The plan is to not document this as defined behavior, however. It's also worth nothing the SCAN API is now technically incompatible with previous versions, although practically we don't believe it's an issue.
* Checksum calculation optimizations - During command execution, we know that all of the keys are from the same slot (outside of a few notable exceptions such as cross slot scripts and modules). We don't want to compute the checksum multiple multiple times, hence we are relying on cached slot id in the client during the command executions. All operations that access random keys, either should pass in the known slot or recompute the slot.
* Slot info in RDB - in order to resize individual dictionaries correctly, while loading RDB, it's not enough to know total number of keys (of course we could approximate number of keys per slot, but it won't be precise). To address this issue, we've added additional metadata into RDB that contains number of keys in each slot, which can be used as a hint during loading.
* DB size - besides `DBSIZE` API, we need to know size of the DB in many places want, in order to avoid scanning all dictionaries and summing up their sizes in a loop, we've introduced a new field into `redisDb` that keeps track of `key_count`. This way we can keep DBSIZE operation O(1). This is also kept for O(1) expires computation as well.
## Performance
This change improves SET performance in cluster mode by ~5%, most of the gains come from us not having to maintain linked lists for keys in slot, non-cluster mode has same performance. For workloads that rely on evictions, the performance is similar because of the extra overhead for finding keys to evict.
RDB loading performance is slightly reduced, as the slot of each key needs to be computed during the load.
## Interface changes
* Removed `overhead.hashtable.slot-to-keys` to `MEMORY STATS`
* Scan API will now require 64 bits to store the cursor, even on 32 bit systems, as the slot information will be stored.
* New RDB version to support the new op code for SLOT information.
---------
Co-authored-by: Vitaly Arbuzov <arvit@amazon.com>
Co-authored-by: Harkrishn Patro <harkrisp@amazon.com>
Co-authored-by: Roshan Khatri <rvkhatri@amazon.com>
Co-authored-by: Madelyn Olson <madelyneolson@gmail.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
2023-10-15 02:58:26 -04:00
|
|
|
|
2022-02-11 14:58:05 -05:00
|
|
|
set n 50000
|
|
|
|
|
|
|
|
# Populate memory with interleaving script-key pattern of same size
|
2022-02-21 02:37:25 -05:00
|
|
|
set dummy_script "--[string repeat x 400]\nreturn "
|
2022-02-11 14:58:05 -05:00
|
|
|
set rd [redis_deferring_client]
|
|
|
|
for {set j 0} {$j < $n} {incr j} {
|
|
|
|
set val "$dummy_script[format "%06d" $j]"
|
|
|
|
$rd script load $val
|
|
|
|
$rd set k$j $val
|
|
|
|
}
|
|
|
|
for {set j 0} {$j < $n} {incr j} {
|
|
|
|
$rd read ; # Discard script load replies
|
|
|
|
$rd read ; # Discard set replies
|
|
|
|
}
|
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
if {$::verbose} {
|
|
|
|
puts "used [s allocator_allocated]"
|
|
|
|
puts "rss [s allocator_active]"
|
|
|
|
puts "frag [s allocator_frag_ratio]"
|
|
|
|
puts "frag_bytes [s allocator_frag_bytes]"
|
Replace cluster metadata with slot specific dictionaries (#11695)
This is an implementation of https://github.com/redis/redis/issues/10589 that eliminates 16 bytes per entry in cluster mode, that are currently used to create a linked list between entries in the same slot. Main idea is splitting main dictionary into 16k smaller dictionaries (one per slot), so we can perform all slot specific operations, such as iteration, without any additional info in the `dictEntry`. For Redis cluster, the expectation is that there will be a larger number of keys, so the fixed overhead of 16k dictionaries will be The expire dictionary is also split up so that each slot is logically decoupled, so that in subsequent revisions we will be able to atomically flush a slot of data.
## Important changes
* Incremental rehashing - one big change here is that it's not one, but rather up to 16k dictionaries that can be rehashing at the same time, in order to keep track of them, we introduce a separate queue for dictionaries that are rehashing. Also instead of rehashing a single dictionary, cron job will now try to rehash as many as it can in 1ms.
* getRandomKey - now needs to not only select a random key, from the random bucket, but also needs to select a random dictionary. Fairness is a major concern here, as it's possible that keys can be unevenly distributed across the slots. In order to address this search we introduced binary index tree). With that data structure we are able to efficiently find a random slot using binary search in O(log^2(slot count)) time.
* Iteration efficiency - when iterating dictionary with a lot of empty slots, we want to skip them efficiently. We can do this using same binary index that is used for random key selection, this index allows us to find a slot for a specific key index. For example if there are 10 keys in the slot 0, then we can quickly find a slot that contains 11th key using binary search on top of the binary index tree.
* scan API - in order to perform a scan across the entire DB, the cursor now needs to not only save position within the dictionary but also the slot id. In this change we append slot id into LSB of the cursor so it can be passed around between client and the server. This has interesting side effect, now you'll be able to start scanning specific slot by simply providing slot id as a cursor value. The plan is to not document this as defined behavior, however. It's also worth nothing the SCAN API is now technically incompatible with previous versions, although practically we don't believe it's an issue.
* Checksum calculation optimizations - During command execution, we know that all of the keys are from the same slot (outside of a few notable exceptions such as cross slot scripts and modules). We don't want to compute the checksum multiple multiple times, hence we are relying on cached slot id in the client during the command executions. All operations that access random keys, either should pass in the known slot or recompute the slot.
* Slot info in RDB - in order to resize individual dictionaries correctly, while loading RDB, it's not enough to know total number of keys (of course we could approximate number of keys per slot, but it won't be precise). To address this issue, we've added additional metadata into RDB that contains number of keys in each slot, which can be used as a hint during loading.
* DB size - besides `DBSIZE` API, we need to know size of the DB in many places want, in order to avoid scanning all dictionaries and summing up their sizes in a loop, we've introduced a new field into `redisDb` that keeps track of `key_count`. This way we can keep DBSIZE operation O(1). This is also kept for O(1) expires computation as well.
## Performance
This change improves SET performance in cluster mode by ~5%, most of the gains come from us not having to maintain linked lists for keys in slot, non-cluster mode has same performance. For workloads that rely on evictions, the performance is similar because of the extra overhead for finding keys to evict.
RDB loading performance is slightly reduced, as the slot of each key needs to be computed during the load.
## Interface changes
* Removed `overhead.hashtable.slot-to-keys` to `MEMORY STATS`
* Scan API will now require 64 bits to store the cursor, even on 32 bit systems, as the slot information will be stored.
* New RDB version to support the new op code for SLOT information.
---------
Co-authored-by: Vitaly Arbuzov <arvit@amazon.com>
Co-authored-by: Harkrishn Patro <harkrisp@amazon.com>
Co-authored-by: Roshan Khatri <rvkhatri@amazon.com>
Co-authored-by: Madelyn Olson <madelyneolson@gmail.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
2023-10-15 02:58:26 -04:00
|
|
|
}
|
2022-02-11 14:58:05 -05:00
|
|
|
assert_lessthan [s allocator_frag_ratio] 1.05
|
Replace cluster metadata with slot specific dictionaries (#11695)
This is an implementation of https://github.com/redis/redis/issues/10589 that eliminates 16 bytes per entry in cluster mode, that are currently used to create a linked list between entries in the same slot. Main idea is splitting main dictionary into 16k smaller dictionaries (one per slot), so we can perform all slot specific operations, such as iteration, without any additional info in the `dictEntry`. For Redis cluster, the expectation is that there will be a larger number of keys, so the fixed overhead of 16k dictionaries will be The expire dictionary is also split up so that each slot is logically decoupled, so that in subsequent revisions we will be able to atomically flush a slot of data.
## Important changes
* Incremental rehashing - one big change here is that it's not one, but rather up to 16k dictionaries that can be rehashing at the same time, in order to keep track of them, we introduce a separate queue for dictionaries that are rehashing. Also instead of rehashing a single dictionary, cron job will now try to rehash as many as it can in 1ms.
* getRandomKey - now needs to not only select a random key, from the random bucket, but also needs to select a random dictionary. Fairness is a major concern here, as it's possible that keys can be unevenly distributed across the slots. In order to address this search we introduced binary index tree). With that data structure we are able to efficiently find a random slot using binary search in O(log^2(slot count)) time.
* Iteration efficiency - when iterating dictionary with a lot of empty slots, we want to skip them efficiently. We can do this using same binary index that is used for random key selection, this index allows us to find a slot for a specific key index. For example if there are 10 keys in the slot 0, then we can quickly find a slot that contains 11th key using binary search on top of the binary index tree.
* scan API - in order to perform a scan across the entire DB, the cursor now needs to not only save position within the dictionary but also the slot id. In this change we append slot id into LSB of the cursor so it can be passed around between client and the server. This has interesting side effect, now you'll be able to start scanning specific slot by simply providing slot id as a cursor value. The plan is to not document this as defined behavior, however. It's also worth nothing the SCAN API is now technically incompatible with previous versions, although practically we don't believe it's an issue.
* Checksum calculation optimizations - During command execution, we know that all of the keys are from the same slot (outside of a few notable exceptions such as cross slot scripts and modules). We don't want to compute the checksum multiple multiple times, hence we are relying on cached slot id in the client during the command executions. All operations that access random keys, either should pass in the known slot or recompute the slot.
* Slot info in RDB - in order to resize individual dictionaries correctly, while loading RDB, it's not enough to know total number of keys (of course we could approximate number of keys per slot, but it won't be precise). To address this issue, we've added additional metadata into RDB that contains number of keys in each slot, which can be used as a hint during loading.
* DB size - besides `DBSIZE` API, we need to know size of the DB in many places want, in order to avoid scanning all dictionaries and summing up their sizes in a loop, we've introduced a new field into `redisDb` that keeps track of `key_count`. This way we can keep DBSIZE operation O(1). This is also kept for O(1) expires computation as well.
## Performance
This change improves SET performance in cluster mode by ~5%, most of the gains come from us not having to maintain linked lists for keys in slot, non-cluster mode has same performance. For workloads that rely on evictions, the performance is similar because of the extra overhead for finding keys to evict.
RDB loading performance is slightly reduced, as the slot of each key needs to be computed during the load.
## Interface changes
* Removed `overhead.hashtable.slot-to-keys` to `MEMORY STATS`
* Scan API will now require 64 bits to store the cursor, even on 32 bit systems, as the slot information will be stored.
* New RDB version to support the new op code for SLOT information.
---------
Co-authored-by: Vitaly Arbuzov <arvit@amazon.com>
Co-authored-by: Harkrishn Patro <harkrisp@amazon.com>
Co-authored-by: Roshan Khatri <rvkhatri@amazon.com>
Co-authored-by: Madelyn Olson <madelyneolson@gmail.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
2023-10-15 02:58:26 -04:00
|
|
|
|
2022-02-11 14:58:05 -05:00
|
|
|
# Delete all the keys to create fragmentation
|
|
|
|
for {set j 0} {$j < $n} {incr j} { $rd del k$j }
|
|
|
|
for {set j 0} {$j < $n} {incr j} { $rd read } ; # Discard del replies
|
|
|
|
$rd close
|
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
if {$::verbose} {
|
|
|
|
puts "used [s allocator_allocated]"
|
|
|
|
puts "rss [s allocator_active]"
|
|
|
|
puts "frag [s allocator_frag_ratio]"
|
|
|
|
puts "frag_bytes [s allocator_frag_bytes]"
|
Replace cluster metadata with slot specific dictionaries (#11695)
This is an implementation of https://github.com/redis/redis/issues/10589 that eliminates 16 bytes per entry in cluster mode, that are currently used to create a linked list between entries in the same slot. Main idea is splitting main dictionary into 16k smaller dictionaries (one per slot), so we can perform all slot specific operations, such as iteration, without any additional info in the `dictEntry`. For Redis cluster, the expectation is that there will be a larger number of keys, so the fixed overhead of 16k dictionaries will be The expire dictionary is also split up so that each slot is logically decoupled, so that in subsequent revisions we will be able to atomically flush a slot of data.
## Important changes
* Incremental rehashing - one big change here is that it's not one, but rather up to 16k dictionaries that can be rehashing at the same time, in order to keep track of them, we introduce a separate queue for dictionaries that are rehashing. Also instead of rehashing a single dictionary, cron job will now try to rehash as many as it can in 1ms.
* getRandomKey - now needs to not only select a random key, from the random bucket, but also needs to select a random dictionary. Fairness is a major concern here, as it's possible that keys can be unevenly distributed across the slots. In order to address this search we introduced binary index tree). With that data structure we are able to efficiently find a random slot using binary search in O(log^2(slot count)) time.
* Iteration efficiency - when iterating dictionary with a lot of empty slots, we want to skip them efficiently. We can do this using same binary index that is used for random key selection, this index allows us to find a slot for a specific key index. For example if there are 10 keys in the slot 0, then we can quickly find a slot that contains 11th key using binary search on top of the binary index tree.
* scan API - in order to perform a scan across the entire DB, the cursor now needs to not only save position within the dictionary but also the slot id. In this change we append slot id into LSB of the cursor so it can be passed around between client and the server. This has interesting side effect, now you'll be able to start scanning specific slot by simply providing slot id as a cursor value. The plan is to not document this as defined behavior, however. It's also worth nothing the SCAN API is now technically incompatible with previous versions, although practically we don't believe it's an issue.
* Checksum calculation optimizations - During command execution, we know that all of the keys are from the same slot (outside of a few notable exceptions such as cross slot scripts and modules). We don't want to compute the checksum multiple multiple times, hence we are relying on cached slot id in the client during the command executions. All operations that access random keys, either should pass in the known slot or recompute the slot.
* Slot info in RDB - in order to resize individual dictionaries correctly, while loading RDB, it's not enough to know total number of keys (of course we could approximate number of keys per slot, but it won't be precise). To address this issue, we've added additional metadata into RDB that contains number of keys in each slot, which can be used as a hint during loading.
* DB size - besides `DBSIZE` API, we need to know size of the DB in many places want, in order to avoid scanning all dictionaries and summing up their sizes in a loop, we've introduced a new field into `redisDb` that keeps track of `key_count`. This way we can keep DBSIZE operation O(1). This is also kept for O(1) expires computation as well.
## Performance
This change improves SET performance in cluster mode by ~5%, most of the gains come from us not having to maintain linked lists for keys in slot, non-cluster mode has same performance. For workloads that rely on evictions, the performance is similar because of the extra overhead for finding keys to evict.
RDB loading performance is slightly reduced, as the slot of each key needs to be computed during the load.
## Interface changes
* Removed `overhead.hashtable.slot-to-keys` to `MEMORY STATS`
* Scan API will now require 64 bits to store the cursor, even on 32 bit systems, as the slot information will be stored.
* New RDB version to support the new op code for SLOT information.
---------
Co-authored-by: Vitaly Arbuzov <arvit@amazon.com>
Co-authored-by: Harkrishn Patro <harkrisp@amazon.com>
Co-authored-by: Roshan Khatri <rvkhatri@amazon.com>
Co-authored-by: Madelyn Olson <madelyneolson@gmail.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
2023-10-15 02:58:26 -04:00
|
|
|
}
|
2022-02-11 14:58:05 -05:00
|
|
|
assert_morethan [s allocator_frag_ratio] 1.4
|
|
|
|
|
|
|
|
catch {r config set activedefrag yes} e
|
|
|
|
if {[r config get activedefrag] eq "activedefrag yes"} {
|
|
|
|
|
|
|
|
# wait for the active defrag to start working (decision once a second)
|
|
|
|
wait_for_condition 50 100 {
|
2023-10-22 04:56:45 -04:00
|
|
|
[s total_active_defrag_time] ne 0
|
2022-02-11 14:58:05 -05:00
|
|
|
} else {
|
2023-10-22 04:56:45 -04:00
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
puts [r info memory]
|
|
|
|
puts [r info stats]
|
|
|
|
puts [r memory malloc-stats]
|
2022-02-11 14:58:05 -05:00
|
|
|
fail "defrag not started."
|
|
|
|
}
|
|
|
|
|
|
|
|
# wait for the active defrag to stop working
|
|
|
|
wait_for_condition 500 100 {
|
|
|
|
[s active_defrag_running] eq 0
|
|
|
|
} else {
|
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
puts [r info memory]
|
|
|
|
puts [r memory malloc-stats]
|
|
|
|
fail "defrag didn't stop."
|
|
|
|
}
|
|
|
|
|
2022-03-09 06:55:17 -05:00
|
|
|
# test the fragmentation is lower
|
2022-02-11 14:58:05 -05:00
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
if {$::verbose} {
|
|
|
|
puts "used [s allocator_allocated]"
|
|
|
|
puts "rss [s allocator_active]"
|
|
|
|
puts "frag [s allocator_frag_ratio]"
|
|
|
|
puts "frag_bytes [s allocator_frag_bytes]"
|
Replace cluster metadata with slot specific dictionaries (#11695)
This is an implementation of https://github.com/redis/redis/issues/10589 that eliminates 16 bytes per entry in cluster mode, that are currently used to create a linked list between entries in the same slot. Main idea is splitting main dictionary into 16k smaller dictionaries (one per slot), so we can perform all slot specific operations, such as iteration, without any additional info in the `dictEntry`. For Redis cluster, the expectation is that there will be a larger number of keys, so the fixed overhead of 16k dictionaries will be The expire dictionary is also split up so that each slot is logically decoupled, so that in subsequent revisions we will be able to atomically flush a slot of data.
## Important changes
* Incremental rehashing - one big change here is that it's not one, but rather up to 16k dictionaries that can be rehashing at the same time, in order to keep track of them, we introduce a separate queue for dictionaries that are rehashing. Also instead of rehashing a single dictionary, cron job will now try to rehash as many as it can in 1ms.
* getRandomKey - now needs to not only select a random key, from the random bucket, but also needs to select a random dictionary. Fairness is a major concern here, as it's possible that keys can be unevenly distributed across the slots. In order to address this search we introduced binary index tree). With that data structure we are able to efficiently find a random slot using binary search in O(log^2(slot count)) time.
* Iteration efficiency - when iterating dictionary with a lot of empty slots, we want to skip them efficiently. We can do this using same binary index that is used for random key selection, this index allows us to find a slot for a specific key index. For example if there are 10 keys in the slot 0, then we can quickly find a slot that contains 11th key using binary search on top of the binary index tree.
* scan API - in order to perform a scan across the entire DB, the cursor now needs to not only save position within the dictionary but also the slot id. In this change we append slot id into LSB of the cursor so it can be passed around between client and the server. This has interesting side effect, now you'll be able to start scanning specific slot by simply providing slot id as a cursor value. The plan is to not document this as defined behavior, however. It's also worth nothing the SCAN API is now technically incompatible with previous versions, although practically we don't believe it's an issue.
* Checksum calculation optimizations - During command execution, we know that all of the keys are from the same slot (outside of a few notable exceptions such as cross slot scripts and modules). We don't want to compute the checksum multiple multiple times, hence we are relying on cached slot id in the client during the command executions. All operations that access random keys, either should pass in the known slot or recompute the slot.
* Slot info in RDB - in order to resize individual dictionaries correctly, while loading RDB, it's not enough to know total number of keys (of course we could approximate number of keys per slot, but it won't be precise). To address this issue, we've added additional metadata into RDB that contains number of keys in each slot, which can be used as a hint during loading.
* DB size - besides `DBSIZE` API, we need to know size of the DB in many places want, in order to avoid scanning all dictionaries and summing up their sizes in a loop, we've introduced a new field into `redisDb` that keeps track of `key_count`. This way we can keep DBSIZE operation O(1). This is also kept for O(1) expires computation as well.
## Performance
This change improves SET performance in cluster mode by ~5%, most of the gains come from us not having to maintain linked lists for keys in slot, non-cluster mode has same performance. For workloads that rely on evictions, the performance is similar because of the extra overhead for finding keys to evict.
RDB loading performance is slightly reduced, as the slot of each key needs to be computed during the load.
## Interface changes
* Removed `overhead.hashtable.slot-to-keys` to `MEMORY STATS`
* Scan API will now require 64 bits to store the cursor, even on 32 bit systems, as the slot information will be stored.
* New RDB version to support the new op code for SLOT information.
---------
Co-authored-by: Vitaly Arbuzov <arvit@amazon.com>
Co-authored-by: Harkrishn Patro <harkrisp@amazon.com>
Co-authored-by: Roshan Khatri <rvkhatri@amazon.com>
Co-authored-by: Madelyn Olson <madelyneolson@gmail.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
2023-10-15 02:58:26 -04:00
|
|
|
}
|
2022-02-11 14:58:05 -05:00
|
|
|
assert_lessthan_equal [s allocator_frag_ratio] 1.05
|
Replace cluster metadata with slot specific dictionaries (#11695)
This is an implementation of https://github.com/redis/redis/issues/10589 that eliminates 16 bytes per entry in cluster mode, that are currently used to create a linked list between entries in the same slot. Main idea is splitting main dictionary into 16k smaller dictionaries (one per slot), so we can perform all slot specific operations, such as iteration, without any additional info in the `dictEntry`. For Redis cluster, the expectation is that there will be a larger number of keys, so the fixed overhead of 16k dictionaries will be The expire dictionary is also split up so that each slot is logically decoupled, so that in subsequent revisions we will be able to atomically flush a slot of data.
## Important changes
* Incremental rehashing - one big change here is that it's not one, but rather up to 16k dictionaries that can be rehashing at the same time, in order to keep track of them, we introduce a separate queue for dictionaries that are rehashing. Also instead of rehashing a single dictionary, cron job will now try to rehash as many as it can in 1ms.
* getRandomKey - now needs to not only select a random key, from the random bucket, but also needs to select a random dictionary. Fairness is a major concern here, as it's possible that keys can be unevenly distributed across the slots. In order to address this search we introduced binary index tree). With that data structure we are able to efficiently find a random slot using binary search in O(log^2(slot count)) time.
* Iteration efficiency - when iterating dictionary with a lot of empty slots, we want to skip them efficiently. We can do this using same binary index that is used for random key selection, this index allows us to find a slot for a specific key index. For example if there are 10 keys in the slot 0, then we can quickly find a slot that contains 11th key using binary search on top of the binary index tree.
* scan API - in order to perform a scan across the entire DB, the cursor now needs to not only save position within the dictionary but also the slot id. In this change we append slot id into LSB of the cursor so it can be passed around between client and the server. This has interesting side effect, now you'll be able to start scanning specific slot by simply providing slot id as a cursor value. The plan is to not document this as defined behavior, however. It's also worth nothing the SCAN API is now technically incompatible with previous versions, although practically we don't believe it's an issue.
* Checksum calculation optimizations - During command execution, we know that all of the keys are from the same slot (outside of a few notable exceptions such as cross slot scripts and modules). We don't want to compute the checksum multiple multiple times, hence we are relying on cached slot id in the client during the command executions. All operations that access random keys, either should pass in the known slot or recompute the slot.
* Slot info in RDB - in order to resize individual dictionaries correctly, while loading RDB, it's not enough to know total number of keys (of course we could approximate number of keys per slot, but it won't be precise). To address this issue, we've added additional metadata into RDB that contains number of keys in each slot, which can be used as a hint during loading.
* DB size - besides `DBSIZE` API, we need to know size of the DB in many places want, in order to avoid scanning all dictionaries and summing up their sizes in a loop, we've introduced a new field into `redisDb` that keeps track of `key_count`. This way we can keep DBSIZE operation O(1). This is also kept for O(1) expires computation as well.
## Performance
This change improves SET performance in cluster mode by ~5%, most of the gains come from us not having to maintain linked lists for keys in slot, non-cluster mode has same performance. For workloads that rely on evictions, the performance is similar because of the extra overhead for finding keys to evict.
RDB loading performance is slightly reduced, as the slot of each key needs to be computed during the load.
## Interface changes
* Removed `overhead.hashtable.slot-to-keys` to `MEMORY STATS`
* Scan API will now require 64 bits to store the cursor, even on 32 bit systems, as the slot information will be stored.
* New RDB version to support the new op code for SLOT information.
---------
Co-authored-by: Vitaly Arbuzov <arvit@amazon.com>
Co-authored-by: Harkrishn Patro <harkrisp@amazon.com>
Co-authored-by: Roshan Khatri <rvkhatri@amazon.com>
Co-authored-by: Madelyn Olson <madelyneolson@gmail.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
2023-10-15 02:58:26 -04:00
|
|
|
}
|
2022-02-11 14:58:05 -05:00
|
|
|
# Flush all script to make sure we don't crash after defragging them
|
|
|
|
r script flush sync
|
|
|
|
} {OK}
|
2017-04-22 09:59:53 -04:00
|
|
|
|
Replace cluster metadata with slot specific dictionaries (#11695)
This is an implementation of https://github.com/redis/redis/issues/10589 that eliminates 16 bytes per entry in cluster mode, that are currently used to create a linked list between entries in the same slot. Main idea is splitting main dictionary into 16k smaller dictionaries (one per slot), so we can perform all slot specific operations, such as iteration, without any additional info in the `dictEntry`. For Redis cluster, the expectation is that there will be a larger number of keys, so the fixed overhead of 16k dictionaries will be The expire dictionary is also split up so that each slot is logically decoupled, so that in subsequent revisions we will be able to atomically flush a slot of data.
## Important changes
* Incremental rehashing - one big change here is that it's not one, but rather up to 16k dictionaries that can be rehashing at the same time, in order to keep track of them, we introduce a separate queue for dictionaries that are rehashing. Also instead of rehashing a single dictionary, cron job will now try to rehash as many as it can in 1ms.
* getRandomKey - now needs to not only select a random key, from the random bucket, but also needs to select a random dictionary. Fairness is a major concern here, as it's possible that keys can be unevenly distributed across the slots. In order to address this search we introduced binary index tree). With that data structure we are able to efficiently find a random slot using binary search in O(log^2(slot count)) time.
* Iteration efficiency - when iterating dictionary with a lot of empty slots, we want to skip them efficiently. We can do this using same binary index that is used for random key selection, this index allows us to find a slot for a specific key index. For example if there are 10 keys in the slot 0, then we can quickly find a slot that contains 11th key using binary search on top of the binary index tree.
* scan API - in order to perform a scan across the entire DB, the cursor now needs to not only save position within the dictionary but also the slot id. In this change we append slot id into LSB of the cursor so it can be passed around between client and the server. This has interesting side effect, now you'll be able to start scanning specific slot by simply providing slot id as a cursor value. The plan is to not document this as defined behavior, however. It's also worth nothing the SCAN API is now technically incompatible with previous versions, although practically we don't believe it's an issue.
* Checksum calculation optimizations - During command execution, we know that all of the keys are from the same slot (outside of a few notable exceptions such as cross slot scripts and modules). We don't want to compute the checksum multiple multiple times, hence we are relying on cached slot id in the client during the command executions. All operations that access random keys, either should pass in the known slot or recompute the slot.
* Slot info in RDB - in order to resize individual dictionaries correctly, while loading RDB, it's not enough to know total number of keys (of course we could approximate number of keys per slot, but it won't be precise). To address this issue, we've added additional metadata into RDB that contains number of keys in each slot, which can be used as a hint during loading.
* DB size - besides `DBSIZE` API, we need to know size of the DB in many places want, in order to avoid scanning all dictionaries and summing up their sizes in a loop, we've introduced a new field into `redisDb` that keeps track of `key_count`. This way we can keep DBSIZE operation O(1). This is also kept for O(1) expires computation as well.
## Performance
This change improves SET performance in cluster mode by ~5%, most of the gains come from us not having to maintain linked lists for keys in slot, non-cluster mode has same performance. For workloads that rely on evictions, the performance is similar because of the extra overhead for finding keys to evict.
RDB loading performance is slightly reduced, as the slot of each key needs to be computed during the load.
## Interface changes
* Removed `overhead.hashtable.slot-to-keys` to `MEMORY STATS`
* Scan API will now require 64 bits to store the cursor, even on 32 bit systems, as the slot information will be stored.
* New RDB version to support the new op code for SLOT information.
---------
Co-authored-by: Vitaly Arbuzov <arvit@amazon.com>
Co-authored-by: Harkrishn Patro <harkrisp@amazon.com>
Co-authored-by: Roshan Khatri <rvkhatri@amazon.com>
Co-authored-by: Madelyn Olson <madelyneolson@gmail.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
2023-10-15 02:58:26 -04:00
|
|
|
test "Active defrag big keys: $type" {
|
2018-02-18 10:36:21 -05:00
|
|
|
r flushdb
|
|
|
|
r config resetstat
|
2020-02-23 05:46:14 -05:00
|
|
|
r config set hz 100
|
2018-02-18 10:36:21 -05:00
|
|
|
r config set activedefrag no
|
|
|
|
r config set active-defrag-max-scan-fields 1000
|
|
|
|
r config set active-defrag-threshold-lower 5
|
|
|
|
r config set active-defrag-cycle-min 65
|
|
|
|
r config set active-defrag-cycle-max 75
|
|
|
|
r config set active-defrag-ignore-bytes 2mb
|
|
|
|
r config set maxmemory 0
|
|
|
|
r config set list-max-ziplist-size 5 ;# list of 10k items will have 2000 quicklist nodes
|
2018-06-26 07:14:35 -04:00
|
|
|
r config set stream-node-max-entries 5
|
2018-02-18 10:36:21 -05:00
|
|
|
r hmset hash h1 v1 h2 v2 h3 v3
|
|
|
|
r lpush list a b c d
|
|
|
|
r zadd zset 0 a 1 b 2 c 3 d
|
|
|
|
r sadd set a b c d
|
2018-06-26 07:14:35 -04:00
|
|
|
r xadd stream * item 1 value a
|
|
|
|
r xadd stream * item 2 value b
|
2018-06-27 08:32:18 -04:00
|
|
|
r xgroup create stream mygroup 0
|
2018-06-26 07:14:35 -04:00
|
|
|
r xreadgroup GROUP mygroup Alice COUNT 1 STREAMS stream >
|
2018-02-18 10:36:21 -05:00
|
|
|
|
|
|
|
# create big keys with 10k items
|
|
|
|
set rd [redis_deferring_client]
|
|
|
|
for {set j 0} {$j < 10000} {incr j} {
|
|
|
|
$rd hset bighash $j [concat "asdfasdfasdf" $j]
|
|
|
|
$rd lpush biglist [concat "asdfasdfasdf" $j]
|
|
|
|
$rd zadd bigzset $j [concat "asdfasdfasdf" $j]
|
|
|
|
$rd sadd bigset [concat "asdfasdfasdf" $j]
|
2018-06-26 07:14:35 -04:00
|
|
|
$rd xadd bigstream * item 1 value a
|
2018-02-18 10:36:21 -05:00
|
|
|
}
|
2018-06-26 07:14:35 -04:00
|
|
|
for {set j 0} {$j < 50000} {incr j} {
|
2018-02-18 10:36:21 -05:00
|
|
|
$rd read ; # Discard replies
|
|
|
|
}
|
|
|
|
|
Replace cluster metadata with slot specific dictionaries (#11695)
This is an implementation of https://github.com/redis/redis/issues/10589 that eliminates 16 bytes per entry in cluster mode, that are currently used to create a linked list between entries in the same slot. Main idea is splitting main dictionary into 16k smaller dictionaries (one per slot), so we can perform all slot specific operations, such as iteration, without any additional info in the `dictEntry`. For Redis cluster, the expectation is that there will be a larger number of keys, so the fixed overhead of 16k dictionaries will be The expire dictionary is also split up so that each slot is logically decoupled, so that in subsequent revisions we will be able to atomically flush a slot of data.
## Important changes
* Incremental rehashing - one big change here is that it's not one, but rather up to 16k dictionaries that can be rehashing at the same time, in order to keep track of them, we introduce a separate queue for dictionaries that are rehashing. Also instead of rehashing a single dictionary, cron job will now try to rehash as many as it can in 1ms.
* getRandomKey - now needs to not only select a random key, from the random bucket, but also needs to select a random dictionary. Fairness is a major concern here, as it's possible that keys can be unevenly distributed across the slots. In order to address this search we introduced binary index tree). With that data structure we are able to efficiently find a random slot using binary search in O(log^2(slot count)) time.
* Iteration efficiency - when iterating dictionary with a lot of empty slots, we want to skip them efficiently. We can do this using same binary index that is used for random key selection, this index allows us to find a slot for a specific key index. For example if there are 10 keys in the slot 0, then we can quickly find a slot that contains 11th key using binary search on top of the binary index tree.
* scan API - in order to perform a scan across the entire DB, the cursor now needs to not only save position within the dictionary but also the slot id. In this change we append slot id into LSB of the cursor so it can be passed around between client and the server. This has interesting side effect, now you'll be able to start scanning specific slot by simply providing slot id as a cursor value. The plan is to not document this as defined behavior, however. It's also worth nothing the SCAN API is now technically incompatible with previous versions, although practically we don't believe it's an issue.
* Checksum calculation optimizations - During command execution, we know that all of the keys are from the same slot (outside of a few notable exceptions such as cross slot scripts and modules). We don't want to compute the checksum multiple multiple times, hence we are relying on cached slot id in the client during the command executions. All operations that access random keys, either should pass in the known slot or recompute the slot.
* Slot info in RDB - in order to resize individual dictionaries correctly, while loading RDB, it's not enough to know total number of keys (of course we could approximate number of keys per slot, but it won't be precise). To address this issue, we've added additional metadata into RDB that contains number of keys in each slot, which can be used as a hint during loading.
* DB size - besides `DBSIZE` API, we need to know size of the DB in many places want, in order to avoid scanning all dictionaries and summing up their sizes in a loop, we've introduced a new field into `redisDb` that keeps track of `key_count`. This way we can keep DBSIZE operation O(1). This is also kept for O(1) expires computation as well.
## Performance
This change improves SET performance in cluster mode by ~5%, most of the gains come from us not having to maintain linked lists for keys in slot, non-cluster mode has same performance. For workloads that rely on evictions, the performance is similar because of the extra overhead for finding keys to evict.
RDB loading performance is slightly reduced, as the slot of each key needs to be computed during the load.
## Interface changes
* Removed `overhead.hashtable.slot-to-keys` to `MEMORY STATS`
* Scan API will now require 64 bits to store the cursor, even on 32 bit systems, as the slot information will be stored.
* New RDB version to support the new op code for SLOT information.
---------
Co-authored-by: Vitaly Arbuzov <arvit@amazon.com>
Co-authored-by: Harkrishn Patro <harkrisp@amazon.com>
Co-authored-by: Roshan Khatri <rvkhatri@amazon.com>
Co-authored-by: Madelyn Olson <madelyneolson@gmail.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
2023-10-15 02:58:26 -04:00
|
|
|
# create some small items (effective in cluster-enabled)
|
|
|
|
r set "{bighash}smallitem" val
|
|
|
|
r set "{biglist}smallitem" val
|
|
|
|
r set "{bigzset}smallitem" val
|
|
|
|
r set "{bigset}smallitem" val
|
|
|
|
r set "{bigstream}smallitem" val
|
|
|
|
|
|
|
|
|
2018-02-18 10:36:21 -05:00
|
|
|
set expected_frag 1.7
|
|
|
|
if {$::accurate} {
|
|
|
|
# scale the hash to 1m fields in order to have a measurable the latency
|
|
|
|
for {set j 10000} {$j < 1000000} {incr j} {
|
|
|
|
$rd hset bighash $j [concat "asdfasdfasdf" $j]
|
2017-01-30 15:53:13 -05:00
|
|
|
}
|
2018-02-18 10:36:21 -05:00
|
|
|
for {set j 10000} {$j < 1000000} {incr j} {
|
|
|
|
$rd read ; # Discard replies
|
|
|
|
}
|
|
|
|
# creating that big hash, increased used_memory, so the relative frag goes down
|
|
|
|
set expected_frag 1.3
|
|
|
|
}
|
2017-01-30 15:53:13 -05:00
|
|
|
|
2018-02-18 10:36:21 -05:00
|
|
|
# add a mass of string keys
|
|
|
|
for {set j 0} {$j < 500000} {incr j} {
|
|
|
|
$rd setrange $j 150 a
|
|
|
|
}
|
|
|
|
for {set j 0} {$j < 500000} {incr j} {
|
|
|
|
$rd read ; # Discard replies
|
|
|
|
}
|
Replace cluster metadata with slot specific dictionaries (#11695)
This is an implementation of https://github.com/redis/redis/issues/10589 that eliminates 16 bytes per entry in cluster mode, that are currently used to create a linked list between entries in the same slot. Main idea is splitting main dictionary into 16k smaller dictionaries (one per slot), so we can perform all slot specific operations, such as iteration, without any additional info in the `dictEntry`. For Redis cluster, the expectation is that there will be a larger number of keys, so the fixed overhead of 16k dictionaries will be The expire dictionary is also split up so that each slot is logically decoupled, so that in subsequent revisions we will be able to atomically flush a slot of data.
## Important changes
* Incremental rehashing - one big change here is that it's not one, but rather up to 16k dictionaries that can be rehashing at the same time, in order to keep track of them, we introduce a separate queue for dictionaries that are rehashing. Also instead of rehashing a single dictionary, cron job will now try to rehash as many as it can in 1ms.
* getRandomKey - now needs to not only select a random key, from the random bucket, but also needs to select a random dictionary. Fairness is a major concern here, as it's possible that keys can be unevenly distributed across the slots. In order to address this search we introduced binary index tree). With that data structure we are able to efficiently find a random slot using binary search in O(log^2(slot count)) time.
* Iteration efficiency - when iterating dictionary with a lot of empty slots, we want to skip them efficiently. We can do this using same binary index that is used for random key selection, this index allows us to find a slot for a specific key index. For example if there are 10 keys in the slot 0, then we can quickly find a slot that contains 11th key using binary search on top of the binary index tree.
* scan API - in order to perform a scan across the entire DB, the cursor now needs to not only save position within the dictionary but also the slot id. In this change we append slot id into LSB of the cursor so it can be passed around between client and the server. This has interesting side effect, now you'll be able to start scanning specific slot by simply providing slot id as a cursor value. The plan is to not document this as defined behavior, however. It's also worth nothing the SCAN API is now technically incompatible with previous versions, although practically we don't believe it's an issue.
* Checksum calculation optimizations - During command execution, we know that all of the keys are from the same slot (outside of a few notable exceptions such as cross slot scripts and modules). We don't want to compute the checksum multiple multiple times, hence we are relying on cached slot id in the client during the command executions. All operations that access random keys, either should pass in the known slot or recompute the slot.
* Slot info in RDB - in order to resize individual dictionaries correctly, while loading RDB, it's not enough to know total number of keys (of course we could approximate number of keys per slot, but it won't be precise). To address this issue, we've added additional metadata into RDB that contains number of keys in each slot, which can be used as a hint during loading.
* DB size - besides `DBSIZE` API, we need to know size of the DB in many places want, in order to avoid scanning all dictionaries and summing up their sizes in a loop, we've introduced a new field into `redisDb` that keeps track of `key_count`. This way we can keep DBSIZE operation O(1). This is also kept for O(1) expires computation as well.
## Performance
This change improves SET performance in cluster mode by ~5%, most of the gains come from us not having to maintain linked lists for keys in slot, non-cluster mode has same performance. For workloads that rely on evictions, the performance is similar because of the extra overhead for finding keys to evict.
RDB loading performance is slightly reduced, as the slot of each key needs to be computed during the load.
## Interface changes
* Removed `overhead.hashtable.slot-to-keys` to `MEMORY STATS`
* Scan API will now require 64 bits to store the cursor, even on 32 bit systems, as the slot information will be stored.
* New RDB version to support the new op code for SLOT information.
---------
Co-authored-by: Vitaly Arbuzov <arvit@amazon.com>
Co-authored-by: Harkrishn Patro <harkrisp@amazon.com>
Co-authored-by: Roshan Khatri <rvkhatri@amazon.com>
Co-authored-by: Madelyn Olson <madelyneolson@gmail.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
2023-10-15 02:58:26 -04:00
|
|
|
assert_equal [r dbsize] 500015
|
2017-01-30 15:53:13 -05:00
|
|
|
|
2018-02-18 10:36:21 -05:00
|
|
|
# create some fragmentation
|
|
|
|
for {set j 0} {$j < 500000} {incr j 2} {
|
|
|
|
$rd del $j
|
2017-04-22 09:59:53 -04:00
|
|
|
}
|
2018-02-18 10:36:21 -05:00
|
|
|
for {set j 0} {$j < 500000} {incr j 2} {
|
|
|
|
$rd read ; # Discard replies
|
|
|
|
}
|
Replace cluster metadata with slot specific dictionaries (#11695)
This is an implementation of https://github.com/redis/redis/issues/10589 that eliminates 16 bytes per entry in cluster mode, that are currently used to create a linked list between entries in the same slot. Main idea is splitting main dictionary into 16k smaller dictionaries (one per slot), so we can perform all slot specific operations, such as iteration, without any additional info in the `dictEntry`. For Redis cluster, the expectation is that there will be a larger number of keys, so the fixed overhead of 16k dictionaries will be The expire dictionary is also split up so that each slot is logically decoupled, so that in subsequent revisions we will be able to atomically flush a slot of data.
## Important changes
* Incremental rehashing - one big change here is that it's not one, but rather up to 16k dictionaries that can be rehashing at the same time, in order to keep track of them, we introduce a separate queue for dictionaries that are rehashing. Also instead of rehashing a single dictionary, cron job will now try to rehash as many as it can in 1ms.
* getRandomKey - now needs to not only select a random key, from the random bucket, but also needs to select a random dictionary. Fairness is a major concern here, as it's possible that keys can be unevenly distributed across the slots. In order to address this search we introduced binary index tree). With that data structure we are able to efficiently find a random slot using binary search in O(log^2(slot count)) time.
* Iteration efficiency - when iterating dictionary with a lot of empty slots, we want to skip them efficiently. We can do this using same binary index that is used for random key selection, this index allows us to find a slot for a specific key index. For example if there are 10 keys in the slot 0, then we can quickly find a slot that contains 11th key using binary search on top of the binary index tree.
* scan API - in order to perform a scan across the entire DB, the cursor now needs to not only save position within the dictionary but also the slot id. In this change we append slot id into LSB of the cursor so it can be passed around between client and the server. This has interesting side effect, now you'll be able to start scanning specific slot by simply providing slot id as a cursor value. The plan is to not document this as defined behavior, however. It's also worth nothing the SCAN API is now technically incompatible with previous versions, although practically we don't believe it's an issue.
* Checksum calculation optimizations - During command execution, we know that all of the keys are from the same slot (outside of a few notable exceptions such as cross slot scripts and modules). We don't want to compute the checksum multiple multiple times, hence we are relying on cached slot id in the client during the command executions. All operations that access random keys, either should pass in the known slot or recompute the slot.
* Slot info in RDB - in order to resize individual dictionaries correctly, while loading RDB, it's not enough to know total number of keys (of course we could approximate number of keys per slot, but it won't be precise). To address this issue, we've added additional metadata into RDB that contains number of keys in each slot, which can be used as a hint during loading.
* DB size - besides `DBSIZE` API, we need to know size of the DB in many places want, in order to avoid scanning all dictionaries and summing up their sizes in a loop, we've introduced a new field into `redisDb` that keeps track of `key_count`. This way we can keep DBSIZE operation O(1). This is also kept for O(1) expires computation as well.
## Performance
This change improves SET performance in cluster mode by ~5%, most of the gains come from us not having to maintain linked lists for keys in slot, non-cluster mode has same performance. For workloads that rely on evictions, the performance is similar because of the extra overhead for finding keys to evict.
RDB loading performance is slightly reduced, as the slot of each key needs to be computed during the load.
## Interface changes
* Removed `overhead.hashtable.slot-to-keys` to `MEMORY STATS`
* Scan API will now require 64 bits to store the cursor, even on 32 bit systems, as the slot information will be stored.
* New RDB version to support the new op code for SLOT information.
---------
Co-authored-by: Vitaly Arbuzov <arvit@amazon.com>
Co-authored-by: Harkrishn Patro <harkrisp@amazon.com>
Co-authored-by: Roshan Khatri <rvkhatri@amazon.com>
Co-authored-by: Madelyn Olson <madelyneolson@gmail.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
2023-10-15 02:58:26 -04:00
|
|
|
assert_equal [r dbsize] 250015
|
2018-02-18 10:36:21 -05:00
|
|
|
|
|
|
|
# start defrag
|
2018-05-17 02:52:00 -04:00
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
2018-02-18 10:36:21 -05:00
|
|
|
set frag [s allocator_frag_ratio]
|
|
|
|
if {$::verbose} {
|
|
|
|
puts "frag $frag"
|
|
|
|
}
|
|
|
|
assert {$frag >= $expected_frag}
|
|
|
|
r config set latency-monitor-threshold 5
|
|
|
|
r latency reset
|
|
|
|
|
2021-12-19 10:41:51 -05:00
|
|
|
set digest [debug_digest]
|
2018-05-24 12:04:17 -04:00
|
|
|
catch {r config set activedefrag yes} e
|
2020-12-14 04:13:46 -05:00
|
|
|
if {[r config get activedefrag] eq "activedefrag yes"} {
|
2018-05-24 12:04:17 -04:00
|
|
|
# wait for the active defrag to start working (decision once a second)
|
|
|
|
wait_for_condition 50 100 {
|
2023-10-22 04:56:45 -04:00
|
|
|
[s total_active_defrag_time] ne 0
|
2018-05-24 12:04:17 -04:00
|
|
|
} else {
|
2023-10-22 04:56:45 -04:00
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
puts [r info memory]
|
|
|
|
puts [r info stats]
|
|
|
|
puts [r memory malloc-stats]
|
2018-05-24 12:04:17 -04:00
|
|
|
fail "defrag not started."
|
|
|
|
}
|
2018-02-18 10:36:21 -05:00
|
|
|
|
2018-05-24 12:04:17 -04:00
|
|
|
# wait for the active defrag to stop working
|
|
|
|
wait_for_condition 500 100 {
|
|
|
|
[s active_defrag_running] eq 0
|
|
|
|
} else {
|
2018-07-18 03:16:33 -04:00
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
2018-05-24 12:04:17 -04:00
|
|
|
puts [r info memory]
|
|
|
|
puts [r memory malloc-stats]
|
|
|
|
fail "defrag didn't stop."
|
|
|
|
}
|
2018-02-18 10:36:21 -05:00
|
|
|
|
2022-03-09 06:55:17 -05:00
|
|
|
# test the fragmentation is lower
|
2018-05-24 12:04:17 -04:00
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
set frag [s allocator_frag_ratio]
|
|
|
|
set max_latency 0
|
|
|
|
foreach event [r latency latest] {
|
|
|
|
lassign $event eventname time latency max
|
|
|
|
if {$eventname == "active-defrag-cycle"} {
|
|
|
|
set max_latency $max
|
|
|
|
}
|
2018-02-18 10:36:21 -05:00
|
|
|
}
|
2018-05-24 12:04:17 -04:00
|
|
|
if {$::verbose} {
|
|
|
|
puts "frag $frag"
|
2020-05-20 07:08:40 -04:00
|
|
|
set misses [s active_defrag_misses]
|
|
|
|
set hits [s active_defrag_hits]
|
|
|
|
puts "hits: $hits"
|
|
|
|
puts "misses: $misses"
|
2018-05-24 12:04:17 -04:00
|
|
|
puts "max latency $max_latency"
|
|
|
|
puts [r latency latest]
|
|
|
|
puts [r latency history active-defrag-cycle]
|
|
|
|
}
|
|
|
|
assert {$frag < 1.1}
|
2020-02-23 05:46:14 -05:00
|
|
|
# due to high fragmentation, 100hz, and active-defrag-cycle-max set to 75,
|
|
|
|
# we expect max latency to be not much higher than 7.5ms but due to rare slowness threshold is set higher
|
2020-10-22 04:10:53 -04:00
|
|
|
if {!$::no_latency} {
|
|
|
|
assert {$max_latency <= 30}
|
|
|
|
}
|
2018-02-18 10:36:21 -05:00
|
|
|
}
|
2018-06-26 07:14:35 -04:00
|
|
|
# verify the data isn't corrupted or changed
|
2021-12-19 10:41:51 -05:00
|
|
|
set newdigest [debug_digest]
|
2018-06-26 07:14:35 -04:00
|
|
|
assert {$digest eq $newdigest}
|
|
|
|
r save ;# saving an rdb iterates over all the data / pointers
|
|
|
|
} {OK}
|
2020-02-18 09:19:52 -05:00
|
|
|
|
2024-03-04 09:56:50 -05:00
|
|
|
test "Active defrag pubsub: $type" {
|
|
|
|
r flushdb
|
|
|
|
r config resetstat
|
|
|
|
r config set hz 100
|
|
|
|
r config set activedefrag no
|
|
|
|
r config set active-defrag-threshold-lower 5
|
|
|
|
r config set active-defrag-cycle-min 65
|
|
|
|
r config set active-defrag-cycle-max 75
|
|
|
|
r config set active-defrag-ignore-bytes 1500kb
|
|
|
|
r config set maxmemory 0
|
|
|
|
|
|
|
|
# Populate memory with interleaving pubsub-key pattern of same size
|
|
|
|
set n 50000
|
|
|
|
set dummy_channel "[string repeat x 400]"
|
|
|
|
set rd [redis_deferring_client]
|
|
|
|
set rd_pubsub [redis_deferring_client]
|
|
|
|
for {set j 0} {$j < $n} {incr j} {
|
|
|
|
set channel_name "$dummy_channel[format "%06d" $j]"
|
|
|
|
$rd_pubsub subscribe $channel_name
|
|
|
|
$rd_pubsub read ; # Discard subscribe replies
|
|
|
|
$rd_pubsub ssubscribe $channel_name
|
|
|
|
$rd_pubsub read ; # Discard ssubscribe replies
|
|
|
|
$rd set k$j $channel_name
|
|
|
|
$rd read ; # Discard set replies
|
|
|
|
}
|
|
|
|
|
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
if {$::verbose} {
|
|
|
|
puts "used [s allocator_allocated]"
|
|
|
|
puts "rss [s allocator_active]"
|
|
|
|
puts "frag [s allocator_frag_ratio]"
|
|
|
|
puts "frag_bytes [s allocator_frag_bytes]"
|
|
|
|
}
|
|
|
|
assert_lessthan [s allocator_frag_ratio] 1.05
|
|
|
|
|
|
|
|
# Delete all the keys to create fragmentation
|
|
|
|
for {set j 0} {$j < $n} {incr j} { $rd del k$j }
|
|
|
|
for {set j 0} {$j < $n} {incr j} { $rd read } ; # Discard del replies
|
|
|
|
$rd close
|
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
if {$::verbose} {
|
|
|
|
puts "used [s allocator_allocated]"
|
|
|
|
puts "rss [s allocator_active]"
|
|
|
|
puts "frag [s allocator_frag_ratio]"
|
|
|
|
puts "frag_bytes [s allocator_frag_bytes]"
|
|
|
|
}
|
|
|
|
assert_morethan [s allocator_frag_ratio] 1.35
|
|
|
|
|
|
|
|
catch {r config set activedefrag yes} e
|
|
|
|
if {[r config get activedefrag] eq "activedefrag yes"} {
|
|
|
|
|
|
|
|
# wait for the active defrag to start working (decision once a second)
|
|
|
|
wait_for_condition 50 100 {
|
|
|
|
[s total_active_defrag_time] ne 0
|
|
|
|
} else {
|
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
puts [r info memory]
|
|
|
|
puts [r info stats]
|
|
|
|
puts [r memory malloc-stats]
|
|
|
|
fail "defrag not started."
|
|
|
|
}
|
|
|
|
|
|
|
|
# wait for the active defrag to stop working
|
|
|
|
wait_for_condition 500 100 {
|
|
|
|
[s active_defrag_running] eq 0
|
|
|
|
} else {
|
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
puts [r info memory]
|
|
|
|
puts [r memory malloc-stats]
|
|
|
|
fail "defrag didn't stop."
|
|
|
|
}
|
|
|
|
|
|
|
|
# test the fragmentation is lower
|
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
if {$::verbose} {
|
|
|
|
puts "used [s allocator_allocated]"
|
|
|
|
puts "rss [s allocator_active]"
|
|
|
|
puts "frag [s allocator_frag_ratio]"
|
|
|
|
puts "frag_bytes [s allocator_frag_bytes]"
|
|
|
|
}
|
|
|
|
assert_lessthan_equal [s allocator_frag_ratio] 1.05
|
|
|
|
}
|
|
|
|
|
|
|
|
# Publishes some message to all the pubsub clients to make sure that
|
|
|
|
# we didn't break the data structure.
|
|
|
|
for {set j 0} {$j < $n} {incr j} {
|
|
|
|
set channel "$dummy_channel[format "%06d" $j]"
|
|
|
|
r publish $channel "hello"
|
|
|
|
assert_equal "message $channel hello" [$rd_pubsub read]
|
|
|
|
$rd_pubsub unsubscribe $channel
|
|
|
|
$rd_pubsub read
|
|
|
|
r spublish $channel "hello"
|
|
|
|
assert_equal "smessage $channel hello" [$rd_pubsub read]
|
|
|
|
$rd_pubsub sunsubscribe $channel
|
|
|
|
$rd_pubsub read
|
|
|
|
}
|
|
|
|
$rd_pubsub close
|
|
|
|
}
|
|
|
|
|
2023-11-02 07:55:48 -04:00
|
|
|
if {$type eq "standalone"} { ;# skip in cluster mode
|
Replace cluster metadata with slot specific dictionaries (#11695)
This is an implementation of https://github.com/redis/redis/issues/10589 that eliminates 16 bytes per entry in cluster mode, that are currently used to create a linked list between entries in the same slot. Main idea is splitting main dictionary into 16k smaller dictionaries (one per slot), so we can perform all slot specific operations, such as iteration, without any additional info in the `dictEntry`. For Redis cluster, the expectation is that there will be a larger number of keys, so the fixed overhead of 16k dictionaries will be The expire dictionary is also split up so that each slot is logically decoupled, so that in subsequent revisions we will be able to atomically flush a slot of data.
## Important changes
* Incremental rehashing - one big change here is that it's not one, but rather up to 16k dictionaries that can be rehashing at the same time, in order to keep track of them, we introduce a separate queue for dictionaries that are rehashing. Also instead of rehashing a single dictionary, cron job will now try to rehash as many as it can in 1ms.
* getRandomKey - now needs to not only select a random key, from the random bucket, but also needs to select a random dictionary. Fairness is a major concern here, as it's possible that keys can be unevenly distributed across the slots. In order to address this search we introduced binary index tree). With that data structure we are able to efficiently find a random slot using binary search in O(log^2(slot count)) time.
* Iteration efficiency - when iterating dictionary with a lot of empty slots, we want to skip them efficiently. We can do this using same binary index that is used for random key selection, this index allows us to find a slot for a specific key index. For example if there are 10 keys in the slot 0, then we can quickly find a slot that contains 11th key using binary search on top of the binary index tree.
* scan API - in order to perform a scan across the entire DB, the cursor now needs to not only save position within the dictionary but also the slot id. In this change we append slot id into LSB of the cursor so it can be passed around between client and the server. This has interesting side effect, now you'll be able to start scanning specific slot by simply providing slot id as a cursor value. The plan is to not document this as defined behavior, however. It's also worth nothing the SCAN API is now technically incompatible with previous versions, although practically we don't believe it's an issue.
* Checksum calculation optimizations - During command execution, we know that all of the keys are from the same slot (outside of a few notable exceptions such as cross slot scripts and modules). We don't want to compute the checksum multiple multiple times, hence we are relying on cached slot id in the client during the command executions. All operations that access random keys, either should pass in the known slot or recompute the slot.
* Slot info in RDB - in order to resize individual dictionaries correctly, while loading RDB, it's not enough to know total number of keys (of course we could approximate number of keys per slot, but it won't be precise). To address this issue, we've added additional metadata into RDB that contains number of keys in each slot, which can be used as a hint during loading.
* DB size - besides `DBSIZE` API, we need to know size of the DB in many places want, in order to avoid scanning all dictionaries and summing up their sizes in a loop, we've introduced a new field into `redisDb` that keeps track of `key_count`. This way we can keep DBSIZE operation O(1). This is also kept for O(1) expires computation as well.
## Performance
This change improves SET performance in cluster mode by ~5%, most of the gains come from us not having to maintain linked lists for keys in slot, non-cluster mode has same performance. For workloads that rely on evictions, the performance is similar because of the extra overhead for finding keys to evict.
RDB loading performance is slightly reduced, as the slot of each key needs to be computed during the load.
## Interface changes
* Removed `overhead.hashtable.slot-to-keys` to `MEMORY STATS`
* Scan API will now require 64 bits to store the cursor, even on 32 bit systems, as the slot information will be stored.
* New RDB version to support the new op code for SLOT information.
---------
Co-authored-by: Vitaly Arbuzov <arvit@amazon.com>
Co-authored-by: Harkrishn Patro <harkrisp@amazon.com>
Co-authored-by: Roshan Khatri <rvkhatri@amazon.com>
Co-authored-by: Madelyn Olson <madelyneolson@gmail.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
2023-10-15 02:58:26 -04:00
|
|
|
test "Active defrag big list: $type" {
|
2020-02-18 09:19:52 -05:00
|
|
|
r flushdb
|
|
|
|
r config resetstat
|
|
|
|
r config set hz 100
|
|
|
|
r config set activedefrag no
|
|
|
|
r config set active-defrag-max-scan-fields 1000
|
|
|
|
r config set active-defrag-threshold-lower 5
|
|
|
|
r config set active-defrag-cycle-min 65
|
|
|
|
r config set active-defrag-cycle-max 75
|
|
|
|
r config set active-defrag-ignore-bytes 2mb
|
|
|
|
r config set maxmemory 0
|
|
|
|
r config set list-max-ziplist-size 5 ;# list of 500k items will have 100k quicklist nodes
|
|
|
|
|
|
|
|
# create big keys with 10k items
|
|
|
|
set rd [redis_deferring_client]
|
|
|
|
|
|
|
|
set expected_frag 1.7
|
|
|
|
# add a mass of list nodes to two lists (allocations are interlaced)
|
|
|
|
set val [string repeat A 100] ;# 5 items of 100 bytes puts us in the 640 bytes bin, which has 32 regs, so high potential for fragmentation
|
2020-05-20 07:08:40 -04:00
|
|
|
set elements 500000
|
|
|
|
for {set j 0} {$j < $elements} {incr j} {
|
2020-02-18 09:19:52 -05:00
|
|
|
$rd lpush biglist1 $val
|
|
|
|
$rd lpush biglist2 $val
|
|
|
|
}
|
2020-05-20 07:08:40 -04:00
|
|
|
for {set j 0} {$j < $elements} {incr j} {
|
2020-02-18 09:19:52 -05:00
|
|
|
$rd read ; # Discard replies
|
|
|
|
$rd read ; # Discard replies
|
|
|
|
}
|
|
|
|
|
|
|
|
# create some fragmentation
|
|
|
|
r del biglist2
|
|
|
|
|
|
|
|
# start defrag
|
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
set frag [s allocator_frag_ratio]
|
|
|
|
if {$::verbose} {
|
|
|
|
puts "frag $frag"
|
|
|
|
}
|
|
|
|
|
|
|
|
assert {$frag >= $expected_frag}
|
|
|
|
r config set latency-monitor-threshold 5
|
|
|
|
r latency reset
|
|
|
|
|
2021-12-19 10:41:51 -05:00
|
|
|
set digest [debug_digest]
|
2020-02-18 09:19:52 -05:00
|
|
|
catch {r config set activedefrag yes} e
|
2020-12-14 04:13:46 -05:00
|
|
|
if {[r config get activedefrag] eq "activedefrag yes"} {
|
2020-02-18 09:19:52 -05:00
|
|
|
# wait for the active defrag to start working (decision once a second)
|
|
|
|
wait_for_condition 50 100 {
|
2023-10-22 04:56:45 -04:00
|
|
|
[s total_active_defrag_time] ne 0
|
2020-02-18 09:19:52 -05:00
|
|
|
} else {
|
2023-10-22 04:56:45 -04:00
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
puts [r info memory]
|
|
|
|
puts [r info stats]
|
|
|
|
puts [r memory malloc-stats]
|
2020-02-18 09:19:52 -05:00
|
|
|
fail "defrag not started."
|
|
|
|
}
|
|
|
|
|
|
|
|
# wait for the active defrag to stop working
|
|
|
|
wait_for_condition 500 100 {
|
|
|
|
[s active_defrag_running] eq 0
|
|
|
|
} else {
|
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
puts [r info memory]
|
|
|
|
puts [r info stats]
|
|
|
|
puts [r memory malloc-stats]
|
|
|
|
fail "defrag didn't stop."
|
|
|
|
}
|
|
|
|
|
2022-03-09 06:55:17 -05:00
|
|
|
# test the fragmentation is lower
|
2020-02-18 09:19:52 -05:00
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
2020-05-20 07:08:40 -04:00
|
|
|
set misses [s active_defrag_misses]
|
|
|
|
set hits [s active_defrag_hits]
|
2020-02-18 09:19:52 -05:00
|
|
|
set frag [s allocator_frag_ratio]
|
|
|
|
set max_latency 0
|
|
|
|
foreach event [r latency latest] {
|
|
|
|
lassign $event eventname time latency max
|
|
|
|
if {$eventname == "active-defrag-cycle"} {
|
|
|
|
set max_latency $max
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if {$::verbose} {
|
|
|
|
puts "frag $frag"
|
2020-05-20 07:08:40 -04:00
|
|
|
puts "misses: $misses"
|
|
|
|
puts "hits: $hits"
|
2020-02-18 09:19:52 -05:00
|
|
|
puts "max latency $max_latency"
|
|
|
|
puts [r latency latest]
|
|
|
|
puts [r latency history active-defrag-cycle]
|
|
|
|
}
|
|
|
|
assert {$frag < 1.1}
|
|
|
|
# due to high fragmentation, 100hz, and active-defrag-cycle-max set to 75,
|
2020-02-23 05:46:14 -05:00
|
|
|
# we expect max latency to be not much higher than 7.5ms but due to rare slowness threshold is set higher
|
2020-10-22 04:10:53 -04:00
|
|
|
if {!$::no_latency} {
|
|
|
|
assert {$max_latency <= 30}
|
|
|
|
}
|
2020-05-20 07:08:40 -04:00
|
|
|
|
|
|
|
# in extreme cases of stagnation, we see over 20m misses before the tests aborts with "defrag didn't stop",
|
|
|
|
# in normal cases we only see 100k misses out of 500k elements
|
|
|
|
assert {$misses < $elements}
|
2020-02-18 09:19:52 -05:00
|
|
|
}
|
|
|
|
# verify the data isn't corrupted or changed
|
2021-12-19 10:41:51 -05:00
|
|
|
set newdigest [debug_digest]
|
2020-02-18 09:19:52 -05:00
|
|
|
assert {$digest eq $newdigest}
|
|
|
|
r save ;# saving an rdb iterates over all the data / pointers
|
|
|
|
r del biglist1 ;# coverage for quicklistBookmarksClear
|
|
|
|
} {1}
|
2020-05-20 07:08:40 -04:00
|
|
|
|
Replace cluster metadata with slot specific dictionaries (#11695)
This is an implementation of https://github.com/redis/redis/issues/10589 that eliminates 16 bytes per entry in cluster mode, that are currently used to create a linked list between entries in the same slot. Main idea is splitting main dictionary into 16k smaller dictionaries (one per slot), so we can perform all slot specific operations, such as iteration, without any additional info in the `dictEntry`. For Redis cluster, the expectation is that there will be a larger number of keys, so the fixed overhead of 16k dictionaries will be The expire dictionary is also split up so that each slot is logically decoupled, so that in subsequent revisions we will be able to atomically flush a slot of data.
## Important changes
* Incremental rehashing - one big change here is that it's not one, but rather up to 16k dictionaries that can be rehashing at the same time, in order to keep track of them, we introduce a separate queue for dictionaries that are rehashing. Also instead of rehashing a single dictionary, cron job will now try to rehash as many as it can in 1ms.
* getRandomKey - now needs to not only select a random key, from the random bucket, but also needs to select a random dictionary. Fairness is a major concern here, as it's possible that keys can be unevenly distributed across the slots. In order to address this search we introduced binary index tree). With that data structure we are able to efficiently find a random slot using binary search in O(log^2(slot count)) time.
* Iteration efficiency - when iterating dictionary with a lot of empty slots, we want to skip them efficiently. We can do this using same binary index that is used for random key selection, this index allows us to find a slot for a specific key index. For example if there are 10 keys in the slot 0, then we can quickly find a slot that contains 11th key using binary search on top of the binary index tree.
* scan API - in order to perform a scan across the entire DB, the cursor now needs to not only save position within the dictionary but also the slot id. In this change we append slot id into LSB of the cursor so it can be passed around between client and the server. This has interesting side effect, now you'll be able to start scanning specific slot by simply providing slot id as a cursor value. The plan is to not document this as defined behavior, however. It's also worth nothing the SCAN API is now technically incompatible with previous versions, although practically we don't believe it's an issue.
* Checksum calculation optimizations - During command execution, we know that all of the keys are from the same slot (outside of a few notable exceptions such as cross slot scripts and modules). We don't want to compute the checksum multiple multiple times, hence we are relying on cached slot id in the client during the command executions. All operations that access random keys, either should pass in the known slot or recompute the slot.
* Slot info in RDB - in order to resize individual dictionaries correctly, while loading RDB, it's not enough to know total number of keys (of course we could approximate number of keys per slot, but it won't be precise). To address this issue, we've added additional metadata into RDB that contains number of keys in each slot, which can be used as a hint during loading.
* DB size - besides `DBSIZE` API, we need to know size of the DB in many places want, in order to avoid scanning all dictionaries and summing up their sizes in a loop, we've introduced a new field into `redisDb` that keeps track of `key_count`. This way we can keep DBSIZE operation O(1). This is also kept for O(1) expires computation as well.
## Performance
This change improves SET performance in cluster mode by ~5%, most of the gains come from us not having to maintain linked lists for keys in slot, non-cluster mode has same performance. For workloads that rely on evictions, the performance is similar because of the extra overhead for finding keys to evict.
RDB loading performance is slightly reduced, as the slot of each key needs to be computed during the load.
## Interface changes
* Removed `overhead.hashtable.slot-to-keys` to `MEMORY STATS`
* Scan API will now require 64 bits to store the cursor, even on 32 bit systems, as the slot information will be stored.
* New RDB version to support the new op code for SLOT information.
---------
Co-authored-by: Vitaly Arbuzov <arvit@amazon.com>
Co-authored-by: Harkrishn Patro <harkrisp@amazon.com>
Co-authored-by: Roshan Khatri <rvkhatri@amazon.com>
Co-authored-by: Madelyn Olson <madelyneolson@gmail.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
2023-10-15 02:58:26 -04:00
|
|
|
test "Active defrag edge case: $type" {
|
2020-05-20 07:08:40 -04:00
|
|
|
# there was an edge case in defrag where all the slabs of a certain bin are exact the same
|
|
|
|
# % utilization, with the exception of the current slab from which new allocations are made
|
|
|
|
# if the current slab is lower in utilization the defragger would have ended up in stagnation,
|
2021-06-10 08:39:33 -04:00
|
|
|
# kept running and not move any allocation.
|
2020-05-20 07:08:40 -04:00
|
|
|
# this test is more consistent on a fresh server with no history
|
2020-11-04 10:47:57 -05:00
|
|
|
start_server {tags {"defrag"} overrides {save ""}} {
|
2020-05-20 07:08:40 -04:00
|
|
|
r flushdb
|
|
|
|
r config resetstat
|
|
|
|
r config set hz 100
|
|
|
|
r config set activedefrag no
|
|
|
|
r config set active-defrag-max-scan-fields 1000
|
|
|
|
r config set active-defrag-threshold-lower 5
|
|
|
|
r config set active-defrag-cycle-min 65
|
|
|
|
r config set active-defrag-cycle-max 75
|
|
|
|
r config set active-defrag-ignore-bytes 1mb
|
|
|
|
r config set maxmemory 0
|
|
|
|
set expected_frag 1.3
|
|
|
|
|
|
|
|
r debug mallctl-str thread.tcache.flush VOID
|
2023-09-08 09:10:17 -04:00
|
|
|
# fill the first slab containing 32 regs of 640 bytes.
|
2020-05-20 07:08:40 -04:00
|
|
|
for {set j 0} {$j < 32} {incr j} {
|
|
|
|
r setrange "_$j" 600 x
|
|
|
|
r debug mallctl-str thread.tcache.flush VOID
|
|
|
|
}
|
|
|
|
|
|
|
|
# add a mass of keys with 600 bytes values, fill the bin of 640 bytes which has 32 regs per slab.
|
|
|
|
set rd [redis_deferring_client]
|
|
|
|
set keys 640000
|
|
|
|
for {set j 0} {$j < $keys} {incr j} {
|
|
|
|
$rd setrange $j 600 x
|
|
|
|
}
|
|
|
|
for {set j 0} {$j < $keys} {incr j} {
|
|
|
|
$rd read ; # Discard replies
|
|
|
|
}
|
|
|
|
|
|
|
|
# create some fragmentation of 50%
|
|
|
|
set sent 0
|
|
|
|
for {set j 0} {$j < $keys} {incr j 1} {
|
|
|
|
$rd del $j
|
|
|
|
incr sent
|
|
|
|
incr j 1
|
|
|
|
}
|
|
|
|
for {set j 0} {$j < $sent} {incr j} {
|
|
|
|
$rd read ; # Discard replies
|
|
|
|
}
|
|
|
|
|
|
|
|
# create higher fragmentation in the first slab
|
|
|
|
for {set j 10} {$j < 32} {incr j} {
|
|
|
|
r del "_$j"
|
|
|
|
}
|
|
|
|
|
|
|
|
# start defrag
|
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
set frag [s allocator_frag_ratio]
|
|
|
|
if {$::verbose} {
|
|
|
|
puts "frag $frag"
|
|
|
|
}
|
|
|
|
|
|
|
|
assert {$frag >= $expected_frag}
|
|
|
|
|
2021-12-19 10:41:51 -05:00
|
|
|
set digest [debug_digest]
|
2020-05-20 07:08:40 -04:00
|
|
|
catch {r config set activedefrag yes} e
|
2020-12-14 04:13:46 -05:00
|
|
|
if {[r config get activedefrag] eq "activedefrag yes"} {
|
2020-05-20 07:08:40 -04:00
|
|
|
# wait for the active defrag to start working (decision once a second)
|
|
|
|
wait_for_condition 50 100 {
|
2023-10-22 04:56:45 -04:00
|
|
|
[s total_active_defrag_time] ne 0
|
2020-05-20 07:08:40 -04:00
|
|
|
} else {
|
2023-10-22 04:56:45 -04:00
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
puts [r info memory]
|
|
|
|
puts [r info stats]
|
|
|
|
puts [r memory malloc-stats]
|
2020-05-20 07:08:40 -04:00
|
|
|
fail "defrag not started."
|
|
|
|
}
|
|
|
|
|
|
|
|
# wait for the active defrag to stop working
|
|
|
|
wait_for_condition 500 100 {
|
|
|
|
[s active_defrag_running] eq 0
|
|
|
|
} else {
|
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
puts [r info memory]
|
|
|
|
puts [r info stats]
|
|
|
|
puts [r memory malloc-stats]
|
|
|
|
fail "defrag didn't stop."
|
|
|
|
}
|
|
|
|
|
2022-03-09 06:55:17 -05:00
|
|
|
# test the fragmentation is lower
|
2020-05-20 07:08:40 -04:00
|
|
|
after 120 ;# serverCron only updates the info once in 100ms
|
|
|
|
set misses [s active_defrag_misses]
|
|
|
|
set hits [s active_defrag_hits]
|
|
|
|
set frag [s allocator_frag_ratio]
|
|
|
|
if {$::verbose} {
|
|
|
|
puts "frag $frag"
|
|
|
|
puts "hits: $hits"
|
|
|
|
puts "misses: $misses"
|
|
|
|
}
|
|
|
|
assert {$frag < 1.1}
|
|
|
|
assert {$misses < 10000000} ;# when defrag doesn't stop, we have some 30m misses, when it does, we have 2m misses
|
|
|
|
}
|
|
|
|
|
|
|
|
# verify the data isn't corrupted or changed
|
2021-12-19 10:41:51 -05:00
|
|
|
set newdigest [debug_digest]
|
2020-05-20 07:08:40 -04:00
|
|
|
assert {$digest eq $newdigest}
|
|
|
|
r save ;# saving an rdb iterates over all the data / pointers
|
|
|
|
}
|
2023-11-02 07:55:48 -04:00
|
|
|
} ;# standalone
|
2023-10-19 14:12:58 -04:00
|
|
|
}
|
2017-01-30 15:53:13 -05:00
|
|
|
}
|
Replace cluster metadata with slot specific dictionaries (#11695)
This is an implementation of https://github.com/redis/redis/issues/10589 that eliminates 16 bytes per entry in cluster mode, that are currently used to create a linked list between entries in the same slot. Main idea is splitting main dictionary into 16k smaller dictionaries (one per slot), so we can perform all slot specific operations, such as iteration, without any additional info in the `dictEntry`. For Redis cluster, the expectation is that there will be a larger number of keys, so the fixed overhead of 16k dictionaries will be The expire dictionary is also split up so that each slot is logically decoupled, so that in subsequent revisions we will be able to atomically flush a slot of data.
## Important changes
* Incremental rehashing - one big change here is that it's not one, but rather up to 16k dictionaries that can be rehashing at the same time, in order to keep track of them, we introduce a separate queue for dictionaries that are rehashing. Also instead of rehashing a single dictionary, cron job will now try to rehash as many as it can in 1ms.
* getRandomKey - now needs to not only select a random key, from the random bucket, but also needs to select a random dictionary. Fairness is a major concern here, as it's possible that keys can be unevenly distributed across the slots. In order to address this search we introduced binary index tree). With that data structure we are able to efficiently find a random slot using binary search in O(log^2(slot count)) time.
* Iteration efficiency - when iterating dictionary with a lot of empty slots, we want to skip them efficiently. We can do this using same binary index that is used for random key selection, this index allows us to find a slot for a specific key index. For example if there are 10 keys in the slot 0, then we can quickly find a slot that contains 11th key using binary search on top of the binary index tree.
* scan API - in order to perform a scan across the entire DB, the cursor now needs to not only save position within the dictionary but also the slot id. In this change we append slot id into LSB of the cursor so it can be passed around between client and the server. This has interesting side effect, now you'll be able to start scanning specific slot by simply providing slot id as a cursor value. The plan is to not document this as defined behavior, however. It's also worth nothing the SCAN API is now technically incompatible with previous versions, although practically we don't believe it's an issue.
* Checksum calculation optimizations - During command execution, we know that all of the keys are from the same slot (outside of a few notable exceptions such as cross slot scripts and modules). We don't want to compute the checksum multiple multiple times, hence we are relying on cached slot id in the client during the command executions. All operations that access random keys, either should pass in the known slot or recompute the slot.
* Slot info in RDB - in order to resize individual dictionaries correctly, while loading RDB, it's not enough to know total number of keys (of course we could approximate number of keys per slot, but it won't be precise). To address this issue, we've added additional metadata into RDB that contains number of keys in each slot, which can be used as a hint during loading.
* DB size - besides `DBSIZE` API, we need to know size of the DB in many places want, in order to avoid scanning all dictionaries and summing up their sizes in a loop, we've introduced a new field into `redisDb` that keeps track of `key_count`. This way we can keep DBSIZE operation O(1). This is also kept for O(1) expires computation as well.
## Performance
This change improves SET performance in cluster mode by ~5%, most of the gains come from us not having to maintain linked lists for keys in slot, non-cluster mode has same performance. For workloads that rely on evictions, the performance is similar because of the extra overhead for finding keys to evict.
RDB loading performance is slightly reduced, as the slot of each key needs to be computed during the load.
## Interface changes
* Removed `overhead.hashtable.slot-to-keys` to `MEMORY STATS`
* Scan API will now require 64 bits to store the cursor, even on 32 bit systems, as the slot information will be stored.
* New RDB version to support the new op code for SLOT information.
---------
Co-authored-by: Vitaly Arbuzov <arvit@amazon.com>
Co-authored-by: Harkrishn Patro <harkrisp@amazon.com>
Co-authored-by: Roshan Khatri <rvkhatri@amazon.com>
Co-authored-by: Madelyn Olson <madelyneolson@gmail.com>
Co-authored-by: Oran Agra <oran@redislabs.com>
2023-10-15 02:58:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
start_cluster 1 0 {tags {"defrag external:skip cluster"} overrides {appendonly yes auto-aof-rewrite-percentage 0 save ""}} {
|
|
|
|
test_active_defrag "cluster"
|
|
|
|
}
|
|
|
|
|
|
|
|
start_server {tags {"defrag external:skip standalone"} overrides {appendonly yes auto-aof-rewrite-percentage 0 save ""}} {
|
|
|
|
test_active_defrag "standalone"
|
|
|
|
}
|
2020-04-16 04:05:03 -04:00
|
|
|
} ;# run_solo
|