Skip to content

Commit

Permalink
Add the keyspace section of the info command
Browse files Browse the repository at this point in the history
  • Loading branch information
NaNShaner authored and HDT3213 committed May 28, 2023
1 parent 7eaaf91 commit 3b9c423
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 20 deletions.
4 changes: 3 additions & 1 deletion cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ func (cluster *Cluster) Exec(c redis.Connection, cmdLine [][]byte) (result redis
}()
cmdName := strings.ToLower(string(cmdLine[0]))
if cmdName == "info" {
return database2.Info(c, cmdLine)
if ser, ok := cluster.db.(*database2.Server); ok {
return database2.Info(ser, cmdLine[1:])
}
}
if cmdName == "auth" {
return database2.Auth(c, cmdLine[1:])
Expand Down
22 changes: 21 additions & 1 deletion database/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (server *Server) Exec(c redis.Connection, cmdLine [][]byte) (result redis.R
}
// info
if cmdName == "info" {
return Info(c, cmdLine)
return Info(server, cmdLine[1:])
}
if cmdName == "slaveof" {
if c != nil && c.InMultiState() {
Expand Down Expand Up @@ -388,3 +388,23 @@ func (server *Server) startReplCron() {
}
}(server)
}

// GetAvgTTL Calculate the average expiration time of keys
func (server *Server) GetAvgTTL(dbIndex, randomKeyCount int) int64 {
var ttlCount int64
db := server.mustSelectDB(dbIndex)
keys := db.data.RandomKeys(randomKeyCount)
for _, k := range keys {
t := time.Now()
rawExpireTime, ok := db.ttlMap.Get(k)
if !ok {
continue
}
expireTime, _ := rawExpireTime.(time.Time)
// if the key has already reached its expiration time during calculation, ignore it
if expireTime.Sub(t).Microseconds() > 0 {
ttlCount += expireTime.Sub(t).Microseconds()
}
}
return ttlCount / int64(len(keys))
}
53 changes: 36 additions & 17 deletions database/systemcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,31 @@ func Ping(c redis.Connection, args [][]byte) redis.Reply {
}

// Info the information of the godis server returned by the INFO command
func Info(c redis.Connection, args [][]byte) redis.Reply {
if len(args) == 1 {
infoCommandList := [...]string{"server", "client", "cluster"}
func Info(db *Server, args [][]byte) redis.Reply {
if len(args) == 0 {
infoCommandList := [...]string{"server", "client", "cluster", "keyspace"}
var allSection []byte
for _, s := range infoCommandList {
allSection = append(allSection, GenGodisInfoString(s)...)
allSection = append(allSection, GenGodisInfoString(s, db)...)
}

return protocol.MakeBulkReply(allSection)
} else if len(args) == 2 {
section := strings.ToLower(string(args[1]))
} else if len(args) == 1 {
section := strings.ToLower(string(args[0]))
switch section {
case "server":
reply := GenGodisInfoString("server")
reply := GenGodisInfoString("server", db)
return protocol.MakeBulkReply(reply)
case "client":
return protocol.MakeBulkReply(GenGodisInfoString("client"))
return protocol.MakeBulkReply(GenGodisInfoString("client", db))
case "cluster":
return protocol.MakeBulkReply(GenGodisInfoString("cluster"))
return protocol.MakeBulkReply(GenGodisInfoString("cluster", db))
case "keyspace":
return protocol.MakeBulkReply(GenGodisInfoString("keyspace", db))
default:
return protocol.MakeNullBulkReply()
return protocol.MakeErrReply("Invalid section for 'info' command")
}
}
return protocol.MakeErrReply("ERR wrong number of arguments for 'info' command")
return protocol.MakeArgNumErrReply("info")
}

// Auth validate client's password
Expand All @@ -73,8 +74,8 @@ func isAuthenticated(c redis.Connection) bool {
return c.GetPassword() == config.Properties.RequirePass
}

func GenGodisInfoString(section string) []byte {
startUpTimeFromNow := getGodisRunningTime()
func GenGodisInfoString(section string, db *Server) []byte {
startUpTimeFromNow := getGodisRuninngTime()
switch section {
case "server":
s := fmt.Sprintf("# Server\r\n"+
Expand Down Expand Up @@ -139,8 +140,20 @@ func GenGodisInfoString(section string) []byte {
)
return []byte(s)
}
case "keyspace":
dbCount := config.Properties.Databases
var serv []byte
for i := 0; i < dbCount; i++ {
keys, expiresKeys := db.GetDBSize(i)
if keys != 0 {
ttlSampleAverage := db.GetAvgTTL(i, 20)
serv = append(serv, getDbSize(i, keys, expiresKeys, ttlSampleAverage)...)
}
}
prefix := []byte("# Keyspace\r\n")
keyspaceInfo := append(prefix, serv...)
return keyspaceInfo
}

return []byte("")
}

Expand All @@ -153,7 +166,13 @@ func getGodisRunningMode() string {
}
}

// getGodisRunningTime return the running time of godis
func getGodisRunningTime() time.Duration {
// getGodisRuninngTime return the running time of godis
func getGodisRuninngTime() time.Duration {
return time.Since(config.EachTimeServerInfo.StartUpTime) / time.Second
}

func getDbSize(dbIndex, keys, expiresKeys int, ttl int64) []byte {
s := fmt.Sprintf("db%d:keys=%d,expires=%d,avg_ttl=%d\r\n",
dbIndex, keys, expiresKeys, ttl)
return []byte(s)
}
4 changes: 3 additions & 1 deletion database/systemcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ func TestInfo(t *testing.T) {
asserts.AssertNotError(t, ret)
ret = testServer.Exec(c, utils.ToCmdLine("iNFO", "SeRvEr"))
asserts.AssertNotError(t, ret)
ret = testServer.Exec(c, utils.ToCmdLine("INFO", "Keyspace"))
asserts.AssertNotError(t, ret)
ret = testServer.Exec(c, utils.ToCmdLine("iNFO", "abc", "bde"))
asserts.AssertErrReply(t, ret, "ERR wrong number of arguments for 'info' command")
ret = testServer.Exec(c, utils.ToCmdLine("INFO", "abc"))
asserts.AssertNullBulk(t, ret)
asserts.AssertErrReply(t, ret, "Invalid section for 'info' command")
}

0 comments on commit 3b9c423

Please sign in to comment.