X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=internal%2Fsdlgoredis%2Fsdlgosentinel.go;h=a06722bdb44a4f2a9c8bb2073d735d01eddc71e6;hb=refs%2Fchanges%2F00%2F11900%2F1;hp=f4395700d1e6e5799ba0ad17afcb7b6965e2cd40;hpb=ee834c59a83cdd10889b3439a281fe43b9f98839;p=ric-plt%2Fsdlgo.git diff --git a/internal/sdlgoredis/sdlgosentinel.go b/internal/sdlgoredis/sdlgosentinel.go index f439570..a06722b 100644 --- a/internal/sdlgoredis/sdlgosentinel.go +++ b/internal/sdlgoredis/sdlgosentinel.go @@ -1,6 +1,6 @@ /* Copyright (c) 2021 AT&T Intellectual Property. - Copyright (c) 2018-2021 Nokia. + Copyright (c) 2018-2022 Nokia. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -23,25 +23,31 @@ package sdlgoredis import ( - "github.com/go-redis/redis/v7" + "context" + "fmt" + "github.com/go-redis/redis/v8" + "strconv" ) type Sentinel struct { + ctx context.Context + MasterName string + NodeCnt string IredisSentinelClient - Cfg *Config } type IredisSentinelClient interface { - Master(name string) *redis.StringStringMapCmd - Slaves(name string) *redis.SliceCmd - Sentinels(name string) *redis.SliceCmd + Master(ctx context.Context, name string) *redis.StringStringMapCmd + Slaves(ctx context.Context, name string) *redis.SliceCmd + Sentinels(ctx context.Context, name string) *redis.SliceCmd } -type RedisSentinelCreateCb func(cfg *Config, addr string) *Sentinel +type RedisSentinelCreateCb func(addr, sentinelPort, masterName, nodeCnt string) *Sentinel -func newRedisSentinel(cfg *Config, addr string) *Sentinel { - redisAddress := addr + ":" + cfg.sentinelPort +func newRedisSentinel(addr, sentinelPort, masterName, nodeCnt string) *Sentinel { + redisAddress := addr + ":" + sentinelPort return &Sentinel{ + ctx: context.Background(), IredisSentinelClient: redis.NewSentinelClient(&redis.Options{ Addr: redisAddress, Password: "", // no password set @@ -49,7 +55,8 @@ func newRedisSentinel(cfg *Config, addr string) *Sentinel { PoolSize: 20, MaxRetries: 2, }), - Cfg: cfg, + MasterName: masterName, + NodeCnt: nodeCnt, } } @@ -61,6 +68,14 @@ func (s *Sentinel) GetDbState() (*DbState, error) { state.PrimaryDbState = *pState state.ReplicasDbState = rState state.SentinelsDbState = sState + + cnt, err := strconv.Atoi(s.NodeCnt) + if err != nil { + state.Err = fmt.Errorf("Sentinel DBAAS_NODE_COUNT configuration value '%s' conversion to integer failed", s.NodeCnt) + return state, state.Err + } + state.ConfigNodeCnt = cnt + if pErr != nil { return state, pErr } @@ -72,7 +87,7 @@ func (s *Sentinel) GetDbState() (*DbState, error) { func (s *Sentinel) getPrimaryDbState() (*PrimaryDbState, error) { state := new(PrimaryDbState) - redisVal, redisErr := s.Master(s.Cfg.masterName).Result() + redisVal, redisErr := s.Master(s.ctx, s.MasterName).Result() if redisErr == nil { state.Fields.Ip = redisVal["ip"] state.Fields.Port = redisVal["port"] @@ -87,7 +102,7 @@ func (s *Sentinel) getReplicasState() (*ReplicasDbState, error) { states := new(ReplicasDbState) states.States = make([]*ReplicaDbState, 0) - redisVal, redisErr := s.Slaves(s.Cfg.masterName).Result() + redisVal, redisErr := s.Slaves(s.ctx, s.MasterName).Result() if redisErr == nil { for _, redisReplica := range redisVal { replicaState := readReplicaState(redisReplica.([]interface{})) @@ -120,11 +135,15 @@ func (s *Sentinel) getSentinelsState() (*SentinelsDbState, error) { states := new(SentinelsDbState) states.States = make([]*SentinelDbState, 0) - redisVal, redisErr := s.Sentinels(s.Cfg.masterName).Result() + redisVal, redisErr := s.Sentinels(s.ctx, s.MasterName).Result() if redisErr == nil { for _, redisSentinel := range redisVal { sentinelState := readSentinelState(redisSentinel.([]interface{})) - states.States = append(states.States, sentinelState) + // Ignore a sentinel entry with zero port, because missing of fix + // for the Redis Bug #9240. + if sentinelState.Fields.Port != "0" { + states.States = append(states.States, sentinelState) + } } } states.Err = redisErr