+func (db *DB) Info() (*DbInfo, error) {
+ var info DbInfo
+ resultStr, err := db.client.Info("all").Result()
+ result := strings.Split(strings.ReplaceAll(resultStr, "\r\n", "\n"), "\n")
+ readRedisInfoReplyFields(result, &info)
+ return &info, err
+}
+
+func readRedisInfoReplyFields(input []string, info *DbInfo) {
+ for _, line := range input {
+ if idx := strings.Index(line, "role:"); idx != -1 {
+ roleStr := line[idx+len("role:"):]
+ if roleStr == "master" {
+ info.Fields.MasterRole = true
+ }
+ } else if idx := strings.Index(line, "connected_slaves:"); idx != -1 {
+ cntStr := line[idx+len("connected_slaves:"):]
+ if cnt, err := strconv.ParseUint(cntStr, 10, 32); err == nil {
+ info.Fields.ConnectedReplicaCnt = uint32(cnt)
+ }
+ }
+ }
+}
+
+func (db *DB) State() (*DbState, error) {
+ if db.cfg.sentinelPort != "" {
+ //Establish connection to Redis sentinel. The reason why connection is done
+ //here instead of time of the SDL instance creation is that for the time being
+ //sentinel connection is needed only here to get state information and
+ //state information is needed only by 'sdlcli' hence it is not time critical
+ //and also we want to avoid opening unnecessary TCP connections towards Redis
+ //sentinel for every SDL instance. Now it is done only when 'sdlcli' is used.
+ sentinelClient := db.sentinel(&db.cfg, db.addr)
+ return sentinelClient.GetDbState()
+ } else {
+ var dbState DbState
+ info, err := db.Info()
+ if err != nil {
+ return &dbState, err
+ }
+ dbState = fillDbStateFromDbInfo(info)
+ return &dbState, err
+ }
+}
+
+func fillDbStateFromDbInfo(info *DbInfo) DbState {
+ var dbState DbState
+ if info.Fields.MasterRole == true {
+ dbState = DbState{
+ MasterDbState: MasterDbState{
+ Fields: MasterDbStateFields{
+ Role: "master",
+ Flags: "master",
+ },
+ },
+ }
+ }
+ return dbState
+}
+