-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add cpu-usec metric support under CLUSTER SLOT-STATS command #712
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5ec9293
Add cpu-usec metric support under CLUSTER SLOT-STATS command (#20).
kyle-yh-kim c7f1ae3
Minor revision.
kyle-yh-kim ba492ba
Minor revision.
kyle-yh-kim b1bdae9
Major revision.
kyle-yh-kim a28ee99
Merge branch 'unstable' into 20-cpu
kyle-yh-kim 4fbf6d4
Minor revision.
kyle-yh-kim 0e9d49f
Update src/cluster_slot_stats.c
madolson 60cfd14
Minor revision.
kyle-yh-kim de6c75e
Update src/server.h
madolson 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
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
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 |
|---|---|---|
|
|
@@ -4,15 +4,11 @@ | |
| * SPDX-License-Identifier: BSD 3-Clause | ||
| */ | ||
|
|
||
| #include "server.h" | ||
| #include "cluster.h" | ||
| #include "cluster_slot_stats.h" | ||
|
|
||
| #define UNASSIGNED_SLOT 0 | ||
|
|
||
| typedef enum { | ||
| KEY_COUNT, | ||
| INVALID, | ||
| } slotStatTypes; | ||
| typedef enum { KEY_COUNT, CPU_USEC, SLOT_STAT_COUNT, INVALID } slotStatTypes; | ||
|
|
||
| /* ----------------------------------------------------------------------------- | ||
| * CLUSTER SLOT-STATS command | ||
|
|
@@ -47,6 +43,8 @@ static uint64_t getSlotStat(int slot, int stat_type) { | |
| uint64_t slot_stat = 0; | ||
| if (stat_type == KEY_COUNT) { | ||
| slot_stat = countKeysInSlot(slot); | ||
| } else if (stat_type == CPU_USEC) { | ||
| slot_stat = server.cluster->slot_stats[slot].cpu_usec; | ||
| } | ||
| return slot_stat; | ||
| } | ||
|
|
@@ -88,9 +86,17 @@ static void addReplySlotStat(client *c, int slot) { | |
| addReplyArrayLen(c, 2); /* Array of size 2, where 0th index represents (int) slot, | ||
| * and 1st index represents (map) usage statistics. */ | ||
| addReplyLongLong(c, slot); | ||
| addReplyMapLen(c, 1); /* Nested map representing slot usage statistics. */ | ||
| addReplyMapLen(c, (server.cluster_slot_stats_enabled) ? SLOT_STAT_COUNT | ||
| : 1); /* Nested map representing slot usage statistics. */ | ||
| addReplyBulkCString(c, "key-count"); | ||
| addReplyLongLong(c, countKeysInSlot(slot)); | ||
|
|
||
| /* Any additional metrics aside from key-count come with a performance trade-off, | ||
| * and are aggregated and returned based on its server config. */ | ||
| if (server.cluster_slot_stats_enabled) { | ||
| addReplyBulkCString(c, "cpu-usec"); | ||
| addReplyLongLong(c, server.cluster->slot_stats[slot].cpu_usec); | ||
| } | ||
| } | ||
|
|
||
| /* Adds reply for the SLOTSRANGE variant. | ||
|
|
@@ -121,6 +127,46 @@ static void addReplyOrderBy(client *c, int order_by, long limit, int desc) { | |
| addReplySortedSlotStats(c, slot_stats, limit); | ||
| } | ||
|
|
||
| /* Resets applicable slot statistics. */ | ||
| void clusterSlotStatReset(int slot) { | ||
| /* key-count is exempt, as it is queried separately through `countKeysInSlot()`. */ | ||
| memset(&server.cluster->slot_stats[slot], 0, sizeof(slotStat)); | ||
| } | ||
|
|
||
| void clusterSlotStatResetAll(void) { | ||
| memset(server.cluster->slot_stats, 0, sizeof(server.cluster->slot_stats)); | ||
| } | ||
|
|
||
| /* For cpu-usec accumulation, nested commands within EXEC, EVAL, FCALL are skipped. | ||
| * This is due to their unique callstack, where the c->duration for | ||
| * EXEC, EVAL and FCALL already includes all of its nested commands. | ||
| * Meaning, the accumulation of cpu-usec for these nested commands | ||
| * would equate to repeating the same calculation twice. | ||
|
madolson marked this conversation as resolved.
|
||
| */ | ||
| static int canAddCpuDuration(client *c) { | ||
| return server.cluster_slot_stats_enabled && // Config should be enabled. | ||
| server.cluster_enabled && // Cluster mode should be enabled. | ||
| c->slot != -1 && // Command should be slot specific. | ||
| (!server.execution_nesting || // Either; | ||
| (server.execution_nesting && // 1) Command should not be nested, or | ||
| c->realcmd->flags & CMD_BLOCKING)); // 2) If command is nested, it must be due to unblocking. | ||
|
madolson marked this conversation as resolved.
Outdated
madolson marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| void clusterSlotStatsAddCpuDuration(client *c, ustime_t duration) { | ||
| if (!canAddCpuDuration(c)) return; | ||
|
|
||
| serverAssert(c->slot >= 0 && c->slot < CLUSTER_SLOTS); | ||
| server.cluster->slot_stats[c->slot].cpu_usec += duration; | ||
| } | ||
|
|
||
| /* For cross-slot scripting, its caller client's slot must be invalidated, | ||
| * such that its slot-stats aggregation is bypassed. */ | ||
| void clusterSlotStatsInvalidateSlotIfApplicable(scriptRunCtx *ctx) { | ||
| if (!(ctx->flags & SCRIPT_ALLOW_CROSS_SLOT)) return; | ||
|
|
||
| ctx->original_client->slot = -1; | ||
| } | ||
|
|
||
| void clusterSlotStatsCommand(client *c) { | ||
| if (server.cluster_enabled == 0) { | ||
| addReplyError(c, "This instance has cluster support disabled"); | ||
|
|
@@ -149,8 +195,10 @@ void clusterSlotStatsCommand(client *c) { | |
| int desc = 1, order_by = INVALID; | ||
| if (!strcasecmp(c->argv[3]->ptr, "key-count")) { | ||
| order_by = KEY_COUNT; | ||
| } else if (!strcasecmp(c->argv[3]->ptr, "cpu-usec")) { | ||
| order_by = CPU_USEC; | ||
|
madolson marked this conversation as resolved.
Outdated
|
||
| } else { | ||
| addReplyError(c, "Unrecognized sort metric for ORDER BY. The supported metrics are: key-count."); | ||
| addReplyError(c, "Unrecognized sort metric for ORDERBY."); | ||
|
Member
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. I don't love this error, it would be nice to be more useful. We can follow up on this though. |
||
| return; | ||
| } | ||
| int i = 4; /* Next argument index, following ORDERBY */ | ||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #include "server.h" | ||
| #include "cluster.h" | ||
| #include "script.h" | ||
| #include "cluster_legacy.h" | ||
|
|
||
| void clusterSlotStatReset(int slot); | ||
| void clusterSlotStatResetAll(void); | ||
| void clusterSlotStatsAddCpuDuration(client *c, ustime_t duration); | ||
| void clusterSlotStatsInvalidateSlotIfApplicable(scriptRunCtx *ctx); |
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 |
|---|---|---|
|
|
@@ -35,6 +35,9 @@ | |
| "properties": { | ||
| "key-count": { | ||
| "type": "integer" | ||
| }, | ||
| "cpu-usec": { | ||
| "type": "integer" | ||
| } | ||
| } | ||
| } | ||
|
|
||
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
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
Oops, something went wrong.
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.