+func (db *DB) Info() (*DbInfo, error) {
+ var info DbInfo
+ resultStr, err := db.client.Info("all").Result()
+ if err != nil {
+ return &info, err
+ }
+
+ result := strings.Split(strings.ReplaceAll(resultStr, "\r\n", "\n"), "\n")
+ err = readRedisInfoReplyFields(result, &info)
+ return &info, err
+}
+
+func readRedisInfoReplyFields(input []string, info *DbInfo) error {
+ for _, line := range input {
+ if idx := strings.Index(line, "role:"); idx != -1 {
+ roleStr := line[idx+len("role:"):]
+ if roleStr == "master" {
+ info.Fields.PrimaryRole = true
+ }
+ } else if idx := strings.Index(line, "connected_slaves:"); idx != -1 {
+ cntStr := line[idx+len("connected_slaves:"):]
+ cnt, err := strconv.ParseUint(cntStr, 10, 32)
+ if err != nil {
+ return fmt.Errorf("Info reply error: %s", err.Error())
+ }
+ info.Fields.ConnectedReplicaCnt = uint32(cnt)
+ }
+ }
+ return nil
+}
+
+func (db *DB) State() (*DbState, error) {
+ dbState := new(DbState)
+ 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 {
+ info, err := db.Info()
+ if err != nil {
+ dbState.PrimaryDbState.Err = err
+ return dbState, err
+ }
+ return db.fillDbStateFromDbInfo(info)
+ }
+}
+
+func (db *DB) fillDbStateFromDbInfo(info *DbInfo) (*DbState, error) {
+ var dbState DbState
+ if info.Fields.PrimaryRole == true {
+ dbState = DbState{
+ PrimaryDbState: PrimaryDbState{
+ Fields: PrimaryDbStateFields{
+ Role: "master",
+ Flags: "master",
+ },
+ },
+ }
+ }
+
+ cnt, err := strconv.Atoi(db.cfg.nodeCnt)
+ if err != nil {
+ dbState.Err = fmt.Errorf("DBAAS_NODE_COUNT configuration value '%s' conversion to integer failed", db.cfg.nodeCnt)
+ } else {
+ dbState.ConfigNodeCnt = cnt
+ }
+
+ return &dbState, dbState.Err
+}
+