You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kvrocks.apache.org by hu...@apache.org on 2023/04/19 06:48:24 UTC

[incubator-kvrocks] branch unstable updated: Add TIME command as Redis (#1394)

This is an automated email from the ASF dual-hosted git repository.

hulk pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/incubator-kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new d2aa7b58 Add TIME command as Redis (#1394)
d2aa7b58 is described below

commit d2aa7b5807c368d9b8066d466164a66fbff58085
Author: Binbin <bi...@qq.com>
AuthorDate: Wed Apr 19 14:48:18 2023 +0800

    Add TIME command as Redis (#1394)
    
    This PR adds the TIME command. The TIME command returns the current
    server time as a two items list: a Unix timestamp and the amount of
    microseconds already elapsed in the current second.
    
    Example:
    ```
    127.0.0.1:6666> time
    1) "1681828105"
    2) "810037"
    ```
---
 src/commands/cmd_server.cc                            | 17 +++++++++++++++++
 tests/gocase/unit/introspection/introspection_test.go | 18 ++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/src/commands/cmd_server.cc b/src/commands/cmd_server.cc
index 8d4b3653..2b37d6fd 100644
--- a/src/commands/cmd_server.cc
+++ b/src/commands/cmd_server.cc
@@ -25,6 +25,7 @@
 #include "server/redis_connection.h"
 #include "server/server.h"
 #include "stats/disk_stats.h"
+#include "time_util.h"
 
 namespace redis {
 
@@ -641,6 +642,21 @@ class CommandEcho : public Commander {
   }
 };
 
+class CommandTime : public Commander {
+ public:
+  Status Execute(Server *svr, Connection *conn, std::string *output) override {
+    uint64_t now = util::GetTimeStampUS();
+    uint64_t s = now / 1000 / 1000;         // unix time in seconds.
+    uint64_t us = now - (s * 1000 * 1000);  // microseconds.
+
+    *output = redis::MultiLen(2);
+    *output += redis::BulkString(std::to_string(s));
+    *output += redis::BulkString(std::to_string(us));
+
+    return Status::OK();
+  }
+};
+
 /* HELLO [<protocol-version> [AUTH <password>] [SETNAME <name>] ] */
 class CommandHello final : public Commander {
  public:
@@ -941,6 +957,7 @@ REDIS_REGISTER_COMMANDS(MakeCmdAttr<CommandAuth>("auth", 2, "read-only ok-loadin
                         MakeCmdAttr<CommandDebug>("debug", -2, "read-only exclusive", 0, 0, 0),
                         MakeCmdAttr<CommandCommand>("command", -1, "read-only", 0, 0, 0),
                         MakeCmdAttr<CommandEcho>("echo", 2, "read-only", 0, 0, 0),
+                        MakeCmdAttr<CommandTime>("time", 1, "read-only ok-loading", 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),
diff --git a/tests/gocase/unit/introspection/introspection_test.go b/tests/gocase/unit/introspection/introspection_test.go
index 4f88527c..c8414399 100644
--- a/tests/gocase/unit/introspection/introspection_test.go
+++ b/tests/gocase/unit/introspection/introspection_test.go
@@ -40,6 +40,24 @@ func TestIntrospection(t *testing.T) {
 	rdb := srv.NewClient()
 	defer func() { require.NoError(t, rdb.Close()) }()
 
+	t.Run("TIME", func(t *testing.T) {
+		nowUnix := int(time.Now().Unix())
+
+		r := rdb.Do(ctx, "TIME")
+		vs, err := r.Slice()
+		require.NoError(t, err)
+
+		s, err := strconv.Atoi(vs[0].(string))
+		require.NoError(t, err)
+		require.Greater(t, s, nowUnix-2)
+		require.Less(t, s, nowUnix+2)
+
+		us, err := strconv.Atoi(vs[1].(string))
+		require.NoError(t, err)
+		require.Greater(t, us, 0)
+		require.Less(t, us, 1000000)
+	})
+
 	t.Run("CLIENT LIST", func(t *testing.T) {
 		v := rdb.ClientList(ctx).Val()
 		require.Regexp(t, "id=.* addr=.*:.* fd=.* name=.* age=.* idle=.* flags=N namespace=.* qbuf=.* .*obuf=.* cmd=client.*", v)