Add info command (#141)

* Generate a new runid for each start of godis

* Add info command

* Add info command

* Generate a new runid for each start of godis

* Add unittests for the info command
This commit is contained in:
bjr
2023-03-19 16:17:40 +08:00
committed by GitHub
parent 3e6d2090c5
commit d7f6420f69
5 changed files with 128 additions and 2 deletions

View File

@@ -1,9 +1,14 @@
package database
import (
"fmt"
"github.com/hdt3213/godis/config"
"github.com/hdt3213/godis/interface/redis"
"github.com/hdt3213/godis/redis/protocol"
"os"
"runtime"
"strings"
"time"
)
// Ping the server
@@ -17,6 +22,24 @@ 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 {
return protocol.MakeBulkReply(GenGodisInfoString())
} else if len(args) == 2 {
section := strings.ToLower(string(args[1]))
switch section {
case "server":
reply := GenGodisInfoString()
return protocol.MakeBulkReply(reply)
}
} else {
return protocol.MakeErrReply("ERR wrong number of arguments for 'info' command")
}
return &protocol.NullBulkReply{}
}
// Auth validate client's password
func Auth(c redis.Connection, args [][]byte) redis.Reply {
if len(args) != 1 {
@@ -39,3 +62,57 @@ func isAuthenticated(c redis.Connection) bool {
}
return c.GetPassword() == config.Properties.RequirePass
}
func GenGodisInfoString() []byte {
startUpTimeFromNow := getGodisRuninngTime()
s := fmt.Sprintf("# Server\r\n"+
"godis_version:%s\r\n"+
//"godis_git_sha1:%s\r\n"+
//"godis_git_dirty:%d\r\n"+
//"godis_build_id:%s\r\n"+
"godis_mode:%s\r\n"+
"os:%s %s\r\n"+
"arch_bits:%d\r\n"+
//"multiplexing_api:%s\r\n"+
"go_version:%s\r\n"+
"process_id:%d\r\n"+
"run_id:%s\r\n"+
"tcp_port:%d\r\n"+
"uptime_in_seconds:%d\r\n"+
"uptime_in_days:%d\r\n"+
//"hz:%d\r\n"+
//"lru_clock:%d\r\n"+
"config_file:%s\r\n",
godisVersion,
//TODO,
//TODO,
//TODO,
getGodisRunningMode(),
runtime.GOOS, runtime.GOARCH,
32<<(^uint(0)>>63),
//TODO,
runtime.Version(),
os.Getpid(),
config.Properties.RunID,
config.Properties.Port,
startUpTimeFromNow,
startUpTimeFromNow/time.Duration(3600*24),
//TODO,
//TODO,
config.Properties.CfPath)
return []byte(s)
}
// getGodisRunningMode return godis running mode
func getGodisRunningMode() string {
if config.Properties.ClusterEnabled == "yes" {
return config.ClusterMode
} else {
return config.StandaloneMode
}
}
// getGodisRuninngTime return the running time of godis
func getGodisRuninngTime() time.Duration {
return time.Since(config.EachTimeServerInfo.StartUpTime) / time.Second
}