2 Copyright (c) 2021 AT&T Intellectual Property.
3 Copyright (c) 2018-2021 Nokia.
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
19 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
20 * platform project (RICP).
26 "github.com/go-redis/redis/v7"
29 type Sentinel struct {
34 type IredisSentinelClient interface {
35 Master(name string) *redis.StringStringMapCmd
36 Slaves(name string) *redis.SliceCmd
37 Sentinels(name string) *redis.SliceCmd
40 type RedisSentinelCreateCb func(cfg *Config, addr string) *Sentinel
42 func newRedisSentinel(cfg *Config, addr string) *Sentinel {
43 redisAddress := addr + ":" + cfg.sentinelPort
45 IredisSentinelClient: redis.NewSentinelClient(&redis.Options{
47 Password: "", // no password set
48 DB: 0, // use default DB
56 func (s *Sentinel) GetDbState() (*DbState, error) {
58 mState, mErr := s.getMasterDbState()
59 rState, rErr := s.getReplicasState()
60 sState, sErr := s.getSentinelsState()
61 state.MasterDbState = *mState
62 state.ReplicasDbState = rState
63 state.SentinelsDbState = sState
73 func (s *Sentinel) getMasterDbState() (*MasterDbState, error) {
74 state := new(MasterDbState)
75 redisVal, redisErr := s.Master(s.Cfg.masterName).Result()
77 state.Fields.Ip = redisVal["ip"]
78 state.Fields.Port = redisVal["port"]
79 state.Fields.Flags = redisVal["flags"]
80 state.Fields.Role = redisVal["role-reported"]
83 return state, redisErr
86 func (s *Sentinel) getReplicasState() (*ReplicasDbState, error) {
87 states := new(ReplicasDbState)
88 states.States = make([]*ReplicaDbState, 0)
90 redisVal, redisErr := s.Slaves(s.Cfg.masterName).Result()
92 for _, redisSlave := range redisVal {
93 replicaState := readReplicaState(redisSlave.([]interface{}))
94 states.States = append(states.States, replicaState)
98 return states, redisErr
101 func readReplicaState(redisSlaves []interface{}) *ReplicaDbState {
102 state := new(ReplicaDbState)
103 for i := 0; i < len(redisSlaves); i += 2 {
104 if redisSlaves[i].(string) == "ip" {
105 state.Fields.Ip = redisSlaves[i+1].(string)
106 } else if redisSlaves[i].(string) == "port" {
107 state.Fields.Port = redisSlaves[i+1].(string)
108 } else if redisSlaves[i].(string) == "flags" {
109 state.Fields.Flags = redisSlaves[i+1].(string)
110 } else if redisSlaves[i].(string) == "role-reported" {
111 state.Fields.Role = redisSlaves[i+1].(string)
112 } else if redisSlaves[i].(string) == "master-link-status" {
113 state.Fields.MasterLinkStatus = redisSlaves[i+1].(string)
119 func (s *Sentinel) getSentinelsState() (*SentinelsDbState, error) {
120 states := new(SentinelsDbState)
121 states.States = make([]*SentinelDbState, 0)
123 redisVal, redisErr := s.Sentinels(s.Cfg.masterName).Result()
125 for _, redisSentinel := range redisVal {
126 sentinelState := readSentinelState(redisSentinel.([]interface{}))
127 states.States = append(states.States, sentinelState)
130 states.Err = redisErr
131 return states, redisErr
134 func readSentinelState(redisSentinels []interface{}) *SentinelDbState {
135 state := new(SentinelDbState)
136 for i := 0; i < len(redisSentinels); i += 2 {
137 if redisSentinels[i].(string) == "ip" {
138 state.Fields.Ip = redisSentinels[i+1].(string)
139 } else if redisSentinels[i].(string) == "port" {
140 state.Fields.Port = redisSentinels[i+1].(string)
141 } else if redisSentinels[i].(string) == "flags" {
142 state.Fields.Flags = redisSentinels[i+1].(string)