Skip to content

Commit

Permalink
Add MEMORY USAGE command as an alias to existing DISK USAGE command (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
torwig authored Apr 9, 2023
1 parent 8d5ccbc commit f089758
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/commands/cmd_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ class CommandDisk : public Commander {
}
};

class CommandMemory : public CommandDisk {};

class CommandRole : public Commander {
public:
Status Execute(Server *svr, Connection *conn, std::string *output) override {
Expand Down Expand Up @@ -645,14 +647,13 @@ class CommandHello final : public Commander {
Status Execute(Server *svr, Connection *conn, std::string *output) override {
size_t next_arg = 1;
if (args_.size() >= 2) {
int64_t protocol = 0;
auto parse_result = ParseInt<int64_t>(args_[next_arg], 10);
++next_arg;
if (!parse_result) {
return {Status::NotOK, "Protocol version is not an integer or out of range"};
}

protocol = *parse_result;
int64_t protocol = *parse_result;

// In redis, it will check protocol < 2 or protocol > 3,
// kvrocks only supports REPL2 by now, but for supporting some
Expand Down Expand Up @@ -941,6 +942,7 @@ REDIS_REGISTER_COMMANDS(MakeCmdAttr<CommandAuth>("auth", 2, "read-only ok-loadin
MakeCmdAttr<CommandCommand>("command", -1, "read-only", 0, 0, 0),
MakeCmdAttr<CommandEcho>("echo", 2, "read-only", 0, 0, 0),
MakeCmdAttr<CommandDisk>("disk", 3, "read-only", 0, 0, 0),
MakeCmdAttr<CommandMemory>("memory", 3, "read-only", 0, 0, 0),
MakeCmdAttr<CommandHello>("hello", -1, "read-only ok-loading", 0, 0, 0),

MakeCmdAttr<CommandCompact>("compact", 1, "read-only no-script", 0, 0, 0),
Expand Down
12 changes: 12 additions & 0 deletions tests/gocase/unit/disk/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strconv"
"strings"
"testing"
"time"

"github.com/apache/incubator-kvrocks/tests/gocase/util"
"github.com/go-redis/redis/v9"
Expand Down Expand Up @@ -142,4 +143,15 @@ func TestDisk(t *testing.T) {
require.ErrorContains(t, rdb.Do(ctx, "Disk", "usage", "nonexistentkey").Err(), "Not found")
})

t.Run("Memory usage existing key - check that Kvrocks support it", func(t *testing.T) {
key := "arbitrary-key"
require.NoError(t, rdb.Del(ctx, key).Err())

require.NoError(t, rdb.Set(ctx, key, "some-arbitrary-value-with-non-zero-length",
time.Duration(0)).Err())

size, err := rdb.MemoryUsage(ctx, key).Result()
require.NoError(t, err)
require.Greater(t, size, int64(0))
})
}

0 comments on commit f089758

Please sign in to comment.