-
Notifications
You must be signed in to change notification settings - Fork 955
Added cache for COMMAND #2839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ebarskiy
wants to merge
6
commits into
valkey-io:unstable
Choose a base branch
from
ebarskiy:cachecommand
base: unstable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+271
−26
Open
Added cache for COMMAND #2839
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6b6f4f3
Added cache for COMMAND
ebarskiy 4bf34f3
Added cache for COMMAND
ebarskiy 06a7a71
removed pointer
ebarskiy ec497e8
switch to array
ebarskiy 6aae939
change aggregate function to allow buffer
ebarskiy 8b77e7a
vibe test
ebarskiy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2342,6 +2342,8 @@ void initServerConfig(void) { | |
| * valkey.conf using the rename-command directive. */ | ||
| server.commands = hashtableCreate(&commandSetType); | ||
| server.orig_commands = hashtableCreate(&originalCommandSetType); | ||
| server.command_response_cache_resp2 = NULL; | ||
| server.command_response_cache_resp3 = NULL; | ||
| populateCommandTable(); | ||
|
|
||
| /* Debugging */ | ||
|
|
@@ -3282,6 +3284,10 @@ int populateCommandStructure(struct serverCommand *c) { | |
| * has been issued for the first time */ | ||
| c->latency_histogram = NULL; | ||
|
|
||
| /* Initialize command info cache */ | ||
| c->info_cache_resp2 = NULL; | ||
| c->info_cache_resp3 = NULL; | ||
|
|
||
| /* Handle the legacy range spec and the "movablekeys" flag (must be done after populating all key specs). */ | ||
| populateCommandLegacyRangeSpec(c); | ||
|
|
||
|
|
@@ -5222,30 +5228,78 @@ void addReplyCommandSubCommands(client *c, | |
| hashtableResetIterator(&iter); | ||
| } | ||
|
|
||
| /* Collect all output from a caching client (both buffer and reply list) */ | ||
| static sds collectCachedResponse(client *c) { | ||
| sds response = sdsempty(); | ||
|
|
||
| /* First, collect from the fixed buffer if any */ | ||
| if (c->bufpos > 0) { | ||
| response = sdscatlen(response, c->buf, c->bufpos); | ||
| } | ||
|
|
||
| /* Then, collect from the reply list */ | ||
| listIter li; | ||
| listNode *ln; | ||
| clientReplyBlock *val_block; | ||
| listRewind(c->reply, &li); | ||
| while ((ln = listNext(&li)) != NULL) { | ||
| val_block = (clientReplyBlock *)listNodeValue(ln); | ||
| response = sdscatlen(response, val_block->buf, val_block->used); | ||
| } | ||
|
|
||
| return response; | ||
| } | ||
|
|
||
| /* Forward declaration */ | ||
| void addReplyCommandInfo(client *c, struct serverCommand *cmd); | ||
ebarskiy marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a block of forward declarations near the top of this file where we could move this declaration, under |
||
|
|
||
| /* Generate and cache the command info response for a given protocol version */ | ||
| static void cacheCommandInfo(struct serverCommand *cmd, int resp) { | ||
| client *caching_client = createCachedResponseClient(resp); | ||
|
|
||
| int firstkey = 0, lastkey = 0, keystep = 0; | ||
| if (cmd->legacy_range_key_spec.begin_search_type != KSPEC_BS_INVALID) { | ||
| firstkey = cmd->legacy_range_key_spec.bs.index.pos; | ||
| lastkey = cmd->legacy_range_key_spec.fk.range.lastkey; | ||
| if (lastkey >= 0) lastkey += firstkey; | ||
| keystep = cmd->legacy_range_key_spec.fk.range.keystep; | ||
| } | ||
|
|
||
| addReplyArrayLen(caching_client, 10); | ||
| addReplyBulkCBuffer(caching_client, cmd->fullname, sdslen(cmd->fullname)); | ||
| addReplyLongLong(caching_client, cmd->arity); | ||
| addReplyFlagsForCommand(caching_client, cmd); | ||
| addReplyLongLong(caching_client, firstkey); | ||
| addReplyLongLong(caching_client, lastkey); | ||
| addReplyLongLong(caching_client, keystep); | ||
| addReplyCommandCategories(caching_client, cmd); | ||
| addReplyCommandTips(caching_client, cmd); | ||
| addReplyCommandKeySpecs(caching_client, cmd); | ||
| addReplyCommandSubCommands(caching_client, cmd, addReplyCommandInfo, 0); | ||
|
|
||
| if (resp == 2) { | ||
| cmd->info_cache_resp2 = collectCachedResponse(caching_client); | ||
| } else { | ||
| cmd->info_cache_resp3 = collectCachedResponse(caching_client); | ||
| } | ||
|
|
||
| deleteCachedResponseClient(caching_client); | ||
| } | ||
|
|
||
| /* Output the representation of a server command. Used by the COMMAND command and COMMAND INFO. */ | ||
| void addReplyCommandInfo(client *c, struct serverCommand *cmd) { | ||
| if (!cmd) { | ||
| addReplyNull(c); | ||
| } else { | ||
| int firstkey = 0, lastkey = 0, keystep = 0; | ||
| if (cmd->legacy_range_key_spec.begin_search_type != KSPEC_BS_INVALID) { | ||
| firstkey = cmd->legacy_range_key_spec.bs.index.pos; | ||
| lastkey = cmd->legacy_range_key_spec.fk.range.lastkey; | ||
| if (lastkey >= 0) lastkey += firstkey; | ||
| keystep = cmd->legacy_range_key_spec.fk.range.keystep; | ||
| /* Use cached response if available for the client's protocol version */ | ||
| sds cache = (c->resp == 2) ? cmd->info_cache_resp2 : cmd->info_cache_resp3; | ||
|
|
||
| if (cache == NULL) { | ||
| cacheCommandInfo(cmd, c->resp); | ||
| cache = (c->resp == 2) ? cmd->info_cache_resp2 : cmd->info_cache_resp3; | ||
| } | ||
|
|
||
| addReplyArrayLen(c, 10); | ||
| addReplyBulkCBuffer(c, cmd->fullname, sdslen(cmd->fullname)); | ||
| addReplyLongLong(c, cmd->arity); | ||
| addReplyFlagsForCommand(c, cmd); | ||
| addReplyLongLong(c, firstkey); | ||
| addReplyLongLong(c, lastkey); | ||
| addReplyLongLong(c, keystep); | ||
| addReplyCommandCategories(c, cmd); | ||
| addReplyCommandTips(c, cmd); | ||
| addReplyCommandKeySpecs(c, cmd); | ||
| addReplyCommandSubCommands(c, cmd, addReplyCommandInfo, 0); | ||
|
|
||
| addReplyProto(c, cache, sdslen(cache)); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -5370,17 +5424,52 @@ void getKeysSubcommand(client *c) { | |
| getKeysSubcommandImpl(c, 0); | ||
| } | ||
|
|
||
| /* COMMAND (no args) */ | ||
| void commandCommand(client *c) { | ||
| /* Invalidate the cached COMMAND response when command table changes */ | ||
| void invalidateCommandCache(void) { | ||
| if (server.command_response_cache_resp2) { | ||
| sdsfree(server.command_response_cache_resp2); | ||
| server.command_response_cache_resp2 = NULL; | ||
| } | ||
| if (server.command_response_cache_resp3) { | ||
| sdsfree(server.command_response_cache_resp3); | ||
| server.command_response_cache_resp3 = NULL; | ||
| } | ||
| } | ||
|
|
||
| /* Generate and cache the full COMMAND response */ | ||
| static void cacheCommandResponse(int resp) { | ||
| client *caching_client = createCachedResponseClient(resp); | ||
|
|
||
| hashtableIterator iter; | ||
| void *next; | ||
| addReplyArrayLen(c, hashtableSize(server.commands)); | ||
| addReplyArrayLen(caching_client, hashtableSize(server.commands)); | ||
| hashtableInitIterator(&iter, server.commands, 0); | ||
| while (hashtableNext(&iter, &next)) { | ||
| struct serverCommand *cmd = next; | ||
| addReplyCommandInfo(c, cmd); | ||
| addReplyCommandInfo(caching_client, cmd); | ||
| } | ||
| hashtableResetIterator(&iter); | ||
|
|
||
| if (resp == 2) { | ||
| server.command_response_cache_resp2 = collectCachedResponse(caching_client); | ||
| } else { | ||
| server.command_response_cache_resp3 = collectCachedResponse(caching_client); | ||
| } | ||
|
|
||
| deleteCachedResponseClient(caching_client); | ||
| } | ||
|
|
||
| /* COMMAND (no args) */ | ||
| void commandCommand(client *c) { | ||
| /* Use cached response if available for the client's protocol version */ | ||
| sds cache = (c->resp == 2) ? server.command_response_cache_resp2 : server.command_response_cache_resp3; | ||
|
|
||
| if (cache == NULL) { | ||
| cacheCommandResponse(c->resp); | ||
| cache = (c->resp == 2) ? server.command_response_cache_resp2 : server.command_response_cache_resp3; | ||
| } | ||
|
|
||
| addReplyProto(c, cache, sdslen(cache)); | ||
| } | ||
|
|
||
| /* COMMAND COUNT */ | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.