From 7900b48bc73bac45dce50ca43872e1af1412f5d5 Mon Sep 17 00:00:00 2001 From: Binbin Date: Mon, 14 Jun 2021 21:46:45 +0800 Subject: [PATCH] slowlog get command supports passing in -1 to get all logs. (#9018) This was already the case before this commit, but it wasn't clear / intended in the code, now it does. --- src/slowlog.c | 19 ++++++++++++++----- tests/unit/slowlog.tcl | 24 +++++++++++++++++++++++- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/slowlog.c b/src/slowlog.c index b8c13f1cb..320f334a8 100644 --- a/src/slowlog.c +++ b/src/slowlog.c @@ -143,8 +143,8 @@ void slowlogCommand(client *c) { if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"help")) { const char *help[] = { "GET []", -" Return top entries from the slowlog (default: 10). Entries are", -" made of:", +" Return top entries from the slowlog (default: 10, -1 mean all).", +" Entries are made of:", " id, timestamp, time in microseconds, arguments array, client IP and port,", " client name", "LEN", @@ -168,9 +168,18 @@ NULL listNode *ln; slowlogEntry *se; - if (c->argc == 3 && - getLongFromObjectOrReply(c,c->argv[2],&count,NULL) != C_OK) - return; + if (c->argc == 3) { + /* Consume count arg. */ + if (getRangeLongFromObjectOrReply(c, c->argv[2], -1, + LONG_MAX, &count, "count should be greater than or equal to -1") != C_OK) + return; + + if (count == -1) { + /* We treat -1 as a special value, which means to get all slow logs. + * Simply set count to the length of server.slowlog.*/ + count = listLength(server.slowlog); + } + } listRewind(server.slowlog,&li); totentries = addReplyDeferredLen(c); diff --git a/tests/unit/slowlog.tcl b/tests/unit/slowlog.tcl index 52022f90d..b9ac9ddf2 100644 --- a/tests/unit/slowlog.tcl +++ b/tests/unit/slowlog.tcl @@ -175,4 +175,26 @@ start_server {tags {"slowlog"} overrides {slowlog-log-slower-than 1000000}} { r debug sleep 0.2 assert_equal [r slowlog len] 0 } {} {needs:debug} -} + + test {SLOWLOG - count must be >= -1} { + assert_error "ERR count should be greater than or equal to -1" {r slowlog get -2} + assert_error "ERR count should be greater than or equal to -1" {r slowlog get -222} + } + + test {SLOWLOG - get all slow logs} { + r config set slowlog-log-slower-than 0 + r config set slowlog-max-len 3 + r slowlog reset + + r set key test + r sadd set a b c + r incr num + r lpush list a + + assert_equal [r slowlog len] 3 + assert_equal 0 [llength [r slowlog get 0]] + assert_equal 1 [llength [r slowlog get 1]] + assert_equal 3 [llength [r slowlog get -1]] + assert_equal 3 [llength [r slowlog get 3]] + } +} \ No newline at end of file