-
Notifications
You must be signed in to change notification settings - Fork 658
Add INFO COMMANDSTATS tracking with per-command success rates #1702
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f26aea3
Add INFO COMMANDSTATS tracking with per-command success rates
Mathos1432 79bf709
Fix double-counting bug in PopulateCommandStatsInfo and remove stale …
Mathos1432 154c3c4
Address PR review: simplify error detection, use canonical command na…
Mathos1432 ed6b617
Include LatencyMonitor in monitor creation condition
Mathos1432 8196323
Update REspInfo test
Mathos1432 d8dab69
Merge branch 'main' into users/matrembl/commandstats
Mathos1432 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| namespace Garnet.server | ||
| { | ||
| /// <summary> | ||
| /// Per-command statistics entry tracking calls, failures, and rejections. | ||
| /// Follows the Redis COMMANDSTATS convention. | ||
| /// </summary> | ||
| public struct CommandStatsEntry | ||
| { | ||
| /// <summary> | ||
| /// Total number of times this command was called. | ||
| /// </summary> | ||
| public ulong Calls; | ||
|
|
||
| /// <summary> | ||
| /// Total number of times this command failed (returned an error response). | ||
| /// </summary> | ||
| public ulong FailedCalls; | ||
|
|
||
| /// <summary> | ||
| /// Total number of times this command was rejected before execution (e.g., ACL denied, OOM). | ||
| /// </summary> | ||
| public ulong RejectedCalls; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tracks per-command usage statistics for built-in commands. | ||
| /// Array-indexed by RespCommand enum value for O(1) access. | ||
| /// Each session owns its own instance (single-writer, no locking needed). | ||
| /// </summary> | ||
| public class CommandStats | ||
| { | ||
| /// <summary> | ||
| /// Number of entries in the stats array, sized to hold all valid RespCommand values. | ||
| /// </summary> | ||
| internal static readonly int EntryCount = (int)RespCommandExtensions.LastValidCommand + 1; | ||
|
|
||
| /// <summary> | ||
| /// Per-command statistics entries indexed by (int)RespCommand. | ||
| /// </summary> | ||
| internal CommandStatsEntry[] entries; | ||
|
|
||
| /// <summary> | ||
| /// Creates a new CommandStats instance with zeroed entries. | ||
| /// </summary> | ||
| public CommandStats() | ||
| { | ||
| entries = new CommandStatsEntry[EntryCount]; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Increment the calls counter for the given command. | ||
| /// </summary> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public void IncrementCalls(RespCommand cmd) | ||
| { | ||
| ushort idx = (ushort)cmd; | ||
| if (idx < entries.Length) | ||
| entries[idx].Calls++; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Increment the failed calls counter for the given command. | ||
| /// </summary> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public void IncrementFailed(RespCommand cmd) | ||
| { | ||
| ushort idx = (ushort)cmd; | ||
| if (idx < entries.Length) | ||
| entries[idx].FailedCalls++; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Increment the rejected calls counter for the given command. | ||
| /// </summary> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public void IncrementRejected(RespCommand cmd) | ||
| { | ||
| ushort idx = (ushort)cmd; | ||
| if (idx < entries.Length) | ||
| entries[idx].RejectedCalls++; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Get the stats entry for the given command. | ||
| /// </summary> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public CommandStatsEntry GetEntry(RespCommand cmd) | ||
| { | ||
| ushort idx = (ushort)cmd; | ||
| if (idx < entries.Length) | ||
| return entries[idx]; | ||
| return default; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Add another CommandStats instance into this one (for aggregation). | ||
| /// </summary> | ||
| internal void Add(CommandStats other) | ||
| { | ||
| if (other?.entries == null) | ||
| return; | ||
|
|
||
| int len = Math.Min(entries.Length, other.entries.Length); | ||
| for (int i = 0; i < len; i++) | ||
| { | ||
| entries[i].Calls += other.entries[i].Calls; | ||
| entries[i].FailedCalls += other.entries[i].FailedCalls; | ||
| entries[i].RejectedCalls += other.entries[i].RejectedCalls; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Reset all entries to zero. | ||
| /// </summary> | ||
| internal void Reset() | ||
| { | ||
| Array.Clear(entries, 0, entries.Length); | ||
| } | ||
|
|
||
| } | ||
| } |
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.
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.