Skip to content

Commit

Permalink
fix: redis get role overtime (#8691)
Browse files Browse the repository at this point in the history
  • Loading branch information
kizuna-lek authored Dec 25, 2024
1 parent 31ff93a commit bd1a859
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
16 changes: 12 additions & 4 deletions pkg/lorry/engines/redis/get_replica_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ func (mgr *Manager) GetReplicaRole(ctx context.Context, _ *dcs.Cluster) (string,
return mgr.role, nil
}

// We use the role obtained from Sentinel as the sole source of truth.
masterAddr, err := mgr.sentinelClient.GetMasterAddrByName(ctx, mgr.ClusterCompName).Result()
if err != nil {
// when we can't get role from sentinel, we query redis instead
// when we can't get role from sentinel, we query redis instead
getRoleFromRedisClient := func() (string, error) {
var role string
result, err := mgr.client.Info(ctx, "Replication").Result()
if err != nil {
Expand All @@ -61,6 +59,16 @@ func (mgr *Manager) GetReplicaRole(ctx context.Context, _ *dcs.Cluster) (string,
}
}

if mgr.sentinelClient == nil {
return getRoleFromRedisClient()
}

// We use the role obtained from Sentinel as the sole source of truth.
masterAddr, err := mgr.sentinelClient.GetMasterAddrByName(ctx, mgr.ClusterCompName).Result()
if err != nil {
return getRoleFromRedisClient()
}

masterName := strings.Split(masterAddr[0], ".")[0]
// if current member is not master from sentinel, just return secondary to avoid double master
if masterName != mgr.CurrentMemberName {
Expand Down
8 changes: 3 additions & 5 deletions pkg/lorry/engines/redis/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ type Manager struct {
clientSettings *Settings
sentinelClient *redis.SentinelClient

ctx context.Context
cancel context.CancelFunc
startAt time.Time
role string
roleSubscribeUpdateTime int64
Expand Down Expand Up @@ -90,10 +88,10 @@ func NewManager(properties engines.Properties) (engines.DBManager, error) {
}

mgr.sentinelClient = newSentinelClient(mgr.clientSettings, mgr.ClusterCompName)
if mgr.sentinelClient != nil {
go mgr.SubscribeRoleChange(context.Background())
}

mgr.ctx, mgr.cancel = context.WithCancel(context.Background())

go mgr.SubscribeRoleChange(mgr.ctx)
return mgr, nil
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/lorry/engines/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,15 @@ func newClient(s *Settings) redis.UniversalClient {
}

func newSentinelClient(s *Settings, clusterCompName string) *redis.SentinelClient {
if !viper.IsSet("SENTINEL_COMPONENT_NAME") {
// cluster has no sentinel
return nil
}

sentinelHost := fmt.Sprintf("%s-sentinel-headless", clusterCompName)
if viper.IsSet("SENTINEL_HEADLESS_SERVICE_NAME") {
sentinelHost = viper.GetString("SENTINEL_HEADLESS_SERVICE_NAME")
}
sentinelPort := "26379"
if viper.IsSet("REDIS_SENTINEL_HOST_NETWORK_PORT") {
sentinelPort = viper.GetString("REDIS_SENTINEL_HOST_NETWORK_PORT")
Expand Down

0 comments on commit bd1a859

Please sign in to comment.