Skip to content

Commit

Permalink
feat: impl role listener
Browse files Browse the repository at this point in the history
  • Loading branch information
earayu committed Dec 22, 2023
1 parent 90859a5 commit 9bd78dc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
11 changes: 9 additions & 2 deletions go/vt/vttablet/tabletmanager/tm_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,15 @@ func (tm *TabletManager) Start(tablet *topodatapb.Tablet, healthCheckInterval ti
servenv.OnTerm(tm.VDiffEngine.Close)
}

tm.roleListener = role.NewListener(func(ctx context.Context, tabletType topodatapb.TabletType) {
tm.ChangeType(ctx, tabletType, false)
tm.roleListener = role.NewListener(func(ctx context.Context, tabletType topodatapb.TabletType) (bool, error) {
if tm.Tablet().Type == tabletType {
return false, nil
}
err := tm.ChangeType(ctx, tabletType, false)
if err != nil {
return false, err
}
return true, nil
})
servenv.OnRun(tm.roleListener.Open)
servenv.OnTerm(tm.roleListener.Close)
Expand Down
12 changes: 9 additions & 3 deletions go/vt/vttablet/tabletserver/role/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ type Listener struct {
stateMutex sync.Mutex
reconcileMutex sync.Mutex

changeTypeFunc func(ctx context.Context, tabletType topodatapb.TabletType)
changeTypeFunc func(ctx context.Context, tabletType topodatapb.TabletType) (bool, error)
}

func NewListener(changeTypeFunc func(ctx context.Context, tabletType topodatapb.TabletType)) *Listener {
func NewListener(changeTypeFunc func(ctx context.Context, tabletType topodatapb.TabletType) (bool, error)) *Listener {
l := &Listener{
isOpen: 0,
changeTypeFunc: changeTypeFunc,
Expand Down Expand Up @@ -185,7 +185,13 @@ func (collector *Listener) reconcileLeadership(ctx context.Context) {
case topodatapb.TabletType_PRIMARY, topodatapb.TabletType_REPLICA, topodatapb.TabletType_RDONLY:
changeTypeCtx, cancel := context.WithTimeout(context.Background(), topo.RemoteOperationTimeout)
defer cancel()
collector.changeTypeFunc(changeTypeCtx, tabletType)
changed, err := collector.changeTypeFunc(changeTypeCtx, tabletType)
if err != nil {
log.Error("change vttablet role to %s error", tabletType.String())
}
if changed {
log.Infof("change vttablet role to %s successfully", tabletType.String())
}
default:
log.Error("role value is not a string, role:%v", role)
}
Expand Down

0 comments on commit 9bd78dc

Please sign in to comment.