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" "fmt"
"runtime/debug" "runtime/debug"
"strings" "strings"
"time"
"github.com/hdt3213/godis/config" "github.com/hdt3213/godis/config"
"github.com/hdt3213/godis/database" "github.com/hdt3213/godis/database"
@@ -34,10 +35,31 @@ func (cluster *Cluster) Exec(c redis.Connection, cmdLine [][]byte) (result redis
result = &protocol.UnknownErrReply{} result = &protocol.UnknownErrReply{}
} }
}() }()
// Record the start time of command execution
GodisExecCommandStartUnixTime := time.Now()
cmdName := strings.ToLower(string(cmdLine[0])) cmdName := strings.ToLower(string(cmdLine[0]))
if cmdName == "auth" { if cmdName == "auth" {
return database.Auth(c, cmdLine[1:]) 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) { if !isAuthenticated(c) {
return protocol.MakeErrReply("NOAUTH Authentication required") return protocol.MakeErrReply("NOAUTH Authentication required")
} }
@@ -45,7 +67,10 @@ func (cluster *Cluster) Exec(c redis.Connection, cmdLine [][]byte) (result redis
if !ok { if !ok {
return protocol.MakeErrReply("ERR unknown command '" + cmdName + "', or not supported in cluster mode") 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 { func isAuthenticated(c redis.Connection) bool {

View File

@@ -1,8 +1,8 @@
package core package core
import ( import (
"github.com/hdt3213/godis/cluster/raft" "github.com/hdt3213/godis/cluster/raft"
"github.com/hdt3213/godis/config"
dbimpl "github.com/hdt3213/godis/database" dbimpl "github.com/hdt3213/godis/database"
"github.com/hdt3213/godis/interface/database" "github.com/hdt3213/godis/interface/database"
"github.com/hdt3213/godis/interface/redis" "github.com/hdt3213/godis/interface/redis"
@@ -29,6 +29,9 @@ type Cluster struct {
getSlotImpl func(key string) uint32 getSlotImpl func(key string) uint32
pickNodeImpl func(slotID uint32) string pickNodeImpl func(slotID uint32) string
id_ string // for tests only id_ string // for tests only
// slow log record
slogLogger *dbimpl.SlowLogger
} }
type Config struct { type Config struct {
@@ -47,7 +50,6 @@ func (c *Cluster) SelfID() string {
return c.raftNode.Cfg.ID() return c.raftNode.Cfg.ID()
} }
func NewCluster(cfg *Config) (*Cluster, error) { func NewCluster(cfg *Config) (*Cluster, error) {
var connections ConnectionFactory var connections ConnectionFactory
if cfg.connectionStub != nil { 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() go cluster.clusterCron()
return cluster, nil return cluster, nil
} }