-
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
base: unstable
Are you sure you want to change the base?
Added cache for COMMAND #2839
Changes from all commits
6b6f4f3
4bf34f3
06a7a71
ec497e8
6aae939
8b77e7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2342,6 +2342,9 @@ void initServerConfig(void) { | |||||||||||||
| * valkey.conf using the rename-command directive. */ | ||||||||||||||
| server.commands = hashtableCreate(&commandSetType); | ||||||||||||||
| server.orig_commands = hashtableCreate(&originalCommandSetType); | ||||||||||||||
| for (int i = 0; i < RESP_CACHE_INDEX_MAX; i++) { | ||||||||||||||
| server.command_response_cache[i] = NULL; | ||||||||||||||
| } | ||||||||||||||
| populateCommandTable(); | ||||||||||||||
|
|
||||||||||||||
| /* Debugging */ | ||||||||||||||
|
|
@@ -3282,6 +3285,11 @@ int populateCommandStructure(struct serverCommand *c) { | |||||||||||||
| * has been issued for the first time */ | ||||||||||||||
| c->latency_histogram = NULL; | ||||||||||||||
|
|
||||||||||||||
| /* Initialize command info cache */ | ||||||||||||||
| for (int i = 0; i < RESP_CACHE_INDEX_MAX; i++) { | ||||||||||||||
| c->info_cache[i] = NULL; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /* Handle the legacy range spec and the "movablekeys" flag (must be done after populating all key specs). */ | ||||||||||||||
| populateCommandLegacyRangeSpec(c); | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -5222,30 +5230,53 @@ void addReplyCommandSubCommands(client *c, | |||||||||||||
| hashtableResetIterator(&iter); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /* Forward declaration */ | ||||||||||||||
| void addReplyCommandInfo(client *c, struct serverCommand *cmd); | ||||||||||||||
|
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); | ||||||||||||||
|
|
||||||||||||||
| cmd->info_cache[RESP_CACHE_INDEX(resp)] = aggregateClientOutputBuffer(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 */ | ||||||||||||||
| int cache_idx = RESP_CACHE_INDEX(c->resp); | ||||||||||||||
| sds cache = cmd->info_cache[cache_idx]; | ||||||||||||||
|
|
||||||||||||||
| if (cache == NULL) { | ||||||||||||||
| cacheCommandInfo(cmd, c->resp); | ||||||||||||||
| cache = cmd->info_cache[cache_idx]; | ||||||||||||||
|
Comment on lines
+5274
to
+5276
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. Similar here. We can let the other function just return the response instead of caching it to avoid some dependency on side-effects between these two functions:
Suggested change
|
||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| 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 +5401,47 @@ 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) { | ||||||||||||||
| for (int i = 0; i < RESP_CACHE_INDEX_MAX; i++) { | ||||||||||||||
| if (server.command_response_cache[i]) { | ||||||||||||||
| sdsfree(server.command_response_cache[i]); | ||||||||||||||
| server.command_response_cache[i] = 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); | ||||||||||||||
|
|
||||||||||||||
| server.command_response_cache[RESP_CACHE_INDEX(resp)] = aggregateClientOutputBuffer(caching_client); | ||||||||||||||
|
|
||||||||||||||
| deleteCachedResponseClient(caching_client); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /* COMMAND (no args) */ | ||||||||||||||
| void commandCommand(client *c) { | ||||||||||||||
| /* Use cached response if available for the client's protocol version */ | ||||||||||||||
| int cache_idx = RESP_CACHE_INDEX(c->resp); | ||||||||||||||
| sds cache = server.command_response_cache[cache_idx]; | ||||||||||||||
|
|
||||||||||||||
| if (cache == NULL) { | ||||||||||||||
| cacheCommandResponse(c->resp); | ||||||||||||||
| cache = server.command_response_cache[cache_idx]; | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+5437
to
+5442
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. Here we rely on cacheCommandResponse populating a specific global and then we fetch it afterwards. It looks like an implicit dependency that we can avoid. Consider returning the response from cachecCommandResponse, like we do in generateClusterSlotResponse, and where the corresponding call looks like this: sds cached_reply = server.cached_cluster_slot_info[conn_type];
if (!cached_reply) {
cached_reply = generateClusterSlotResponse(c->resp);
server.cached_cluster_slot_info[conn_type] = cached_reply;
} |
||||||||||||||
|
|
||||||||||||||
| addReplyProto(c, cache, sdslen(cache)); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /* COMMAND COUNT */ | ||||||||||||||
|
|
||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.