@@ -22,6 +22,8 @@ package redis
2222import (
2323 "context"
2424 "fmt"
25+ "os/exec"
26+ "strconv"
2527 "strings"
2628 "time"
2729
@@ -44,7 +46,6 @@ type Manager struct {
4446 clientSettings * Settings
4547 sentinelClient * redis.SentinelClient
4648
47- startAt time.Time
4849 role string
4950 roleSubscribeUpdateTime int64
5051 roleProbePeriod int64
@@ -76,7 +77,14 @@ func NewManager(properties engines.Properties) (engines.DBManager, error) {
7677 roleProbePeriod : int64 (viper .GetInt (constant .KBEnvRoleProbePeriod )),
7778 }
7879
79- mgr .startAt = time .Now ()
80+ majorVersion , err := getRedisMajorVersion ()
81+ if err != nil {
82+ return nil , err
83+ }
84+ // The username is supported after 6.0
85+ if majorVersion < 6 {
86+ redisUser = ""
87+ }
8088
8189 defaultSettings := & Settings {
8290 Password : redisPasswd ,
@@ -120,3 +128,31 @@ func tokenizeCmd2Args(cmd string) []interface{} {
120128 }
121129 return redisArgs
122130}
131+
132+ // we get redis version from 'redis-cli --version'
133+ func getRedisMajorVersion () (int , error ) {
134+ redisCliCMD , err := exec .LookPath ("redis-cli" )
135+ if err != nil {
136+ if viper .IsSet ("REDIS_VERSION" ) {
137+ return strconv .Atoi (strings .Split (strings .TrimSpace (viper .GetString ("REDIS_VERSION" )), "." )[0 ])
138+ }
139+ return - 1 , err
140+ }
141+
142+ out , err := exec .Command (redisCliCMD , "--version" ).Output ()
143+ if err != nil {
144+ return - 1 , err
145+ }
146+
147+ // output eg: redis-cli 7.2.4
148+ versionInfo := strings .Split (strings .TrimSpace (string (out )), " " )
149+ if len (versionInfo ) < 2 {
150+ return - 1 , fmt .Errorf ("invalid redis version info: %s" , string (out ))
151+ }
152+
153+ majorVersion , err := strconv .Atoi (strings .Split (strings .TrimSpace (versionInfo [1 ]), "." )[0 ])
154+ if err != nil {
155+ return - 1 , err
156+ }
157+ return majorVersion , nil
158+ }
0 commit comments