Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: redis get role overtime #8691

Merged
merged 3 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading