Repository navigation
Implement MEMORY USAGE command - #212
Open
surajrawat11 wants to merge 9 commits into
Open
surajrawat11 wants to merge 9 commits into
surajrawat11 wants to merge 9 commits into
Conversation
MEMORY USAGE needs the per-key estimate that maxmemory accounting already computes. Make eviction::entry_bytes pub(crate) so the keyspace module can call it instead of adding a second estimator. The calculation is unchanged. Tested with: cargo test.
MEMORY USAGE key [SAMPLES count] replies with the logical byte estimate for one key by calling eviction::entry_bytes, or a nil bulk when the key is missing or expired. SAMPLES is validated and then ignored, because the estimate always covers the whole value: a missing or negative count and any other option are a syntax error, and a non-integer count is rejected. queue_error also checks MEMORY, so a missing or unknown subcommand and a missing key abort MULTI when the command is queued, as for OBJECT. Tested with: cargo test.
Eviction::touch treats the first argument of an unlisted command as its key, so MEMORY USAGE would have refreshed a key named "USAGE". List MEMORY next to OBJECT so reading an estimate never changes which key is evicted next. Tested with: cargo test.
Advertise memory|usage with arity -3 and its key at argument 2, and the memory container with arity -2. COMMAND INFO, the MULTI queue-time arity check and Cluster key routing now recognise the command. The Keyspace row of the compatibility inventory gains MEMORY USAGE in the same commit because a unit test keeps that table equal to the command table. Tested with: cargo test.
COMMAND INFO MEMORY must list memory|usage as a subcommand with arity -3 and key positions (2, 2, 1), next to the existing OBJECT assertion. Tested with: cargo test.
For strings, lists, hashes, sets and sorted sets the estimate must grow with the value, and the per-key numbers must add up to used_memory in INFO memory, which sums the same estimator over the dataset. No byte count is hardcoded. Missing and expired keys reply nil. SAMPLES and lowercase spellings report the same number, and reading the estimate leaves the value, the TTL and another client's WATCH untouched. Tested with: cargo test.
Pin the exact error replies for a missing subcommand, a missing key, an unknown subcommand, malformed SAMPLES and unknown options, for both an existing and a missing key. Inside MULTI, a missing or unknown subcommand and a missing key are rejected when queued and abort EXEC. A valid command queues and runs, and a malformed SAMPLES queues and is reported by EXEC as that command's reply. Tested with: cargo test.
Under allkeys-lru the oldest key must still be the one evicted after MEMORY USAGE measures it, and also when the oldest key is merely named "USAGE" like the subcommand. Tested with: cargo test.
Describe the reply in the compatibility notes: it is the key's share of the logical dataset estimate, not allocator memory or process RSS, and SAMPLES is accepted but ignored. List the MEMORY subcommands that are not implemented and add the changelog entry. Tested with: cargo test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds the MEMORY USAGE command to ReRu.
It returns an estimate of how much memory a key is using by reusing the existing size estimator instead of adding a separate calculation.
The optional SAMPLES argument is supported too. For now, it's accepted but doesn't affect the calculation, as mentioned in the issue.
A few other things covered in this PR:
MEMORY USAGEdoesn't affect the LRU/LFU eviction state.I've also added tests for different data types, invalid arguments, missing keys, transactions, and read-only behaviour.
Updated the compatibility documentation and changelog as well.
Testing
Ran the following checks:
All checks are passing.
Reference: https://redis.io/docs/latest/commands/memory-usage/