Skip to content
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

Fix module LatencyAddSample still work when latency-monitor-threshold is 0 #1541

Merged
merged 2 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -7680,7 +7680,7 @@ void VM__Assert(const char *estr, const char *file, int line) {
* command. The call is skipped if the latency is smaller than the configured
* latency-monitor-threshold. */
void VM_LatencyAddSample(const char *event, mstime_t latency) {
if (latency >= server.latency_monitor_threshold) latencyAddSample(event, latency);
latencyAddSampleIfNeeded(event, latency);
}

/* --------------------------------------------------------------------------
Expand Down
21 changes: 21 additions & 0 deletions tests/modules/basics.c
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,24 @@ int TestNotifications(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc)
return ValkeyModule_ReplyWithSimpleString(ctx, "ERR");
}

/* test.latency latency_ms */
int TestLatency(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
if (argc != 2) {
ValkeyModule_WrongArity(ctx);
return VALKEYMODULE_OK;
}

long long latency_ms;
if (ValkeyModule_StringToLongLong(argv[1], &latency_ms) != VALKEYMODULE_OK) {
ValkeyModule_ReplyWithError(ctx, "Invalid integer value");
return VALKEYMODULE_OK;
}

ValkeyModule_LatencyAddSample("test", latency_ms);
ValkeyModule_ReplyWithSimpleString(ctx, "OK");
return VALKEYMODULE_OK;
}

/* TEST.CTXFLAGS -- Test GetContextFlags. */
int TestCtxFlags(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
VALKEYMODULE_NOT_USED(argc);
Expand Down Expand Up @@ -1048,5 +1066,8 @@ int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int arg
TestNotifications,"write deny-oom",1,1,1) == VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;

if (ValkeyModule_CreateCommand(ctx, "test.latency", TestLatency, "readonly", 0, 0, 0) == VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;

return VALKEYMODULE_OK;
}
17 changes: 17 additions & 0 deletions tests/unit/moduleapi/basics.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ start_server {tags {"modules"}} {
verify_log_message 0 "*Module name is busy*" 0
}

test "test latency" {
r config set latency-monitor-threshold 0
r latency reset
r test.latency 0
r test.latency 1
assert_equal {} [r latency latest]
assert_equal {} [r latency history test]

r config set latency-monitor-threshold 1
r test.latency 0
assert_equal 0 [llength [r latency history test]]
r test.latency 1
assert_match {*test * 1 1*} [r latency latest]
r test.latency 2
assert_match {*test * 2 2*} [r latency latest]
}

test "Unload the module - basics" {
assert_equal {OK} [r module unload test]
}
Expand Down
Loading