Skip to content

Commit

Permalink
fix: lorry support redis 5.x (#8746)
Browse files Browse the repository at this point in the history
  • Loading branch information
kizuna-lek authored Jan 6, 2025
1 parent 9f3532d commit b6d38ea
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
40 changes: 38 additions & 2 deletions pkg/lorry/engines/redis/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package redis
import (
"context"
"fmt"
"os/exec"
"strconv"
"strings"
"time"

Expand All @@ -44,7 +46,6 @@ type Manager struct {
clientSettings *Settings
sentinelClient *redis.SentinelClient

startAt time.Time
role string
roleSubscribeUpdateTime int64
roleProbePeriod int64
Expand Down Expand Up @@ -76,7 +77,14 @@ func NewManager(properties engines.Properties) (engines.DBManager, error) {
roleProbePeriod: int64(viper.GetInt(constant.KBEnvRoleProbePeriod)),
}

mgr.startAt = time.Now()
majorVersion, err := getRedisMajorVersion()
if err != nil {
return nil, err
}
// The username is supported after 6.0
if majorVersion < 6 {
redisUser = ""
}

defaultSettings := &Settings{
Password: redisPasswd,
Expand Down Expand Up @@ -120,3 +128,31 @@ func tokenizeCmd2Args(cmd string) []interface{} {
}
return redisArgs
}

// we get redis version from 'redis-cli --version'
func getRedisMajorVersion() (int, error) {
redisCliCMD, err := exec.LookPath("redis-cli")
if err != nil {
if viper.IsSet("REDIS_VERSION") {
return strconv.Atoi(strings.Split(strings.TrimSpace(viper.GetString("REDIS_VERSION")), ".")[0])
}
return -1, err
}

out, err := exec.Command(redisCliCMD, "--version").Output()
if err != nil {
return -1, err
}

// output eg: redis-cli 7.2.4
versionInfo := strings.Split(strings.TrimSpace(string(out)), " ")
if len(versionInfo) < 2 {
return -1, fmt.Errorf("invalid redis version info: %s", string(out))
}

majorVersion, err := strconv.Atoi(strings.Split(strings.TrimSpace(versionInfo[1]), ".")[0])
if err != nil {
return -1, err
}
return majorVersion, nil
}
1 change: 1 addition & 0 deletions pkg/lorry/engines/redis/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var _ = Describe("Redis DBManager", func() {
// Set up relevant viper config variables
viper.Set(constant.KBEnvServiceUser, "testuser")
viper.Set(constant.KBEnvServicePassword, "testpassword")
viper.Set("REDIS_VERSION", "7.2.4")
Context("new db manager", func() {
It("with right configurations", func() {
properties := engines.Properties{
Expand Down

0 comments on commit b6d38ea

Please sign in to comment.