add slowlog command to cluster

This commit is contained in:
bijingrui
2025-08-08 23:40:07 +08:00
committed by finley
parent de5e9130bd
commit e86948ab1e
2 changed files with 35 additions and 5 deletions

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"runtime/debug"
"strings"
"time"
"github.com/hdt3213/godis/config"
"github.com/hdt3213/godis/database"
@@ -34,10 +35,31 @@ func (cluster *Cluster) Exec(c redis.Connection, cmdLine [][]byte) (result redis
result = &protocol.UnknownErrReply{}
}
}()
// Record the start time of command execution
GodisExecCommandStartUnixTime := time.Now()
cmdName := strings.ToLower(string(cmdLine[0]))
if cmdName == "auth" {
return database.Auth(c, cmdLine[1:])
}
if cmdName == "ping" {
return database.Ping(c, cmdLine[1:])
}
if cmdName == "dbsize" {
dbsize, _ := cluster.db.GetDBSize(0)
return protocol.MakeIntReply(int64(dbsize))
}
if cmdName == "info" {
if server, ok := cluster.db.(*database.Server); ok {
return database.Info(server, cmdLine[1:])
}
}
if cmdName == "slowlog" {
return cluster.slogLogger.HandleSlowlogCommand(cmdLine)
}
if !isAuthenticated(c) {
return protocol.MakeErrReply("NOAUTH Authentication required")
}
@@ -45,7 +67,10 @@ func (cluster *Cluster) Exec(c redis.Connection, cmdLine [][]byte) (result redis
if !ok {
return protocol.MakeErrReply("ERR unknown command '" + cmdName + "', or not supported in cluster mode")
}
return cmdFunc(cluster, c, cmdLine)
exec := cmdFunc(cluster, c, cmdLine)
cluster.slogLogger.Record(GodisExecCommandStartUnixTime, cmdLine, c.Name())
return exec
}
func isAuthenticated(c redis.Connection) bool {

View File

@@ -1,8 +1,8 @@
package core
import (
"github.com/hdt3213/godis/cluster/raft"
"github.com/hdt3213/godis/config"
dbimpl "github.com/hdt3213/godis/database"
"github.com/hdt3213/godis/interface/database"
"github.com/hdt3213/godis/interface/redis"
@@ -29,6 +29,9 @@ type Cluster struct {
getSlotImpl func(key string) uint32
pickNodeImpl func(slotID uint32) string
id_ string // for tests only
// slow log record
slogLogger *dbimpl.SlowLogger
}
type Config struct {
@@ -37,7 +40,7 @@ type Config struct {
JoinAddress string
Master string
connectionStub ConnectionFactory // for test
noCron bool // for test
noCron bool // for test
}
func (c *Cluster) SelfID() string {
@@ -47,7 +50,6 @@ func (c *Cluster) SelfID() string {
return c.raftNode.Cfg.ID()
}
func NewCluster(cfg *Config) (*Cluster, error) {
var connections ConnectionFactory
if cfg.connectionStub != nil {
@@ -120,6 +122,9 @@ func NewCluster(cfg *Config) (*Cluster, error) {
}
}
// record slow log
cluster.slogLogger = dbimpl.NewSlowLogger(config.Properties.SlowLogMaxLen, config.Properties.SlowLogSlowerThan)
go cluster.clusterCron()
return cluster, nil
}