Skip to content

Commit

Permalink
Introduce Manager.Ctx()
Browse files Browse the repository at this point in the history
  • Loading branch information
CbcWestwolf committed Jul 28, 2022
1 parent d2ab14c commit 27bd71f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ func (d *ddl) DisableDDL() error {
// FIXME: if possible, when this node is the only node with DDL, ths setting of DisableDDL should fail.

// lets the owner start a new election.
err = d.ownerManager.ResignOwner(d.ctx)
err = d.ownerManager.ResignOwner(d.ownerManager.Ctx())

if err != nil {
logutil.BgLogger().Error("[ddl] error when ResignOwner", zap.Error(err))
Expand Down
11 changes: 9 additions & 2 deletions owner/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ type Manager interface {
CampaignOwner() error
// ResignOwner lets the owner start a new election.
ResignOwner(ctx context.Context) error
// Ctx returns the context of the manager.
Ctx() context.Context
// Cancel cancels this etcd ownerManager.
Cancel()
// RequireOwner requires the ownerManager is owner.
Expand Down Expand Up @@ -113,6 +115,11 @@ func (m *ownerManager) IsOwner() bool {
return atomic.LoadPointer(&m.elec) != unsafe.Pointer(nil)
}

// Ctx implements Manager.Ctx interface.
func (m *ownerManager) Ctx() context.Context {
return m.ctx
}

// Cancel implements Manager.Cancel interface.
func (m *ownerManager) Cancel() {
m.cancel()
Expand Down Expand Up @@ -149,12 +156,12 @@ func setManagerSessionTTL() error {
func (m *ownerManager) CampaignOwner() error {
logPrefix := fmt.Sprintf("[%s] %s", m.prompt, m.key)
logutil.BgLogger().Info("start campaign owner", zap.String("ownerInfo", logPrefix))
session, err := util2.NewSession(m.ctx, logPrefix, m.etcdCli, util2.NewSessionDefaultRetryCnt, ManagerSessionTTL)
etcdSession, err := util2.NewSession(m.ctx, logPrefix, m.etcdCli, util2.NewSessionDefaultRetryCnt, ManagerSessionTTL)
if err != nil {
return errors.Trace(err)
}
m.wg.Add(1)
go m.campaignLoop(session)
go m.campaignLoop(etcdSession)
return nil
}

Expand Down
6 changes: 6 additions & 0 deletions owner/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var _ Manager = &mockManager{}
type mockManager struct {
owner int32
id string // id is the ID of manager.
ctx context.Context
cancel context.CancelFunc
beOwnerHook func()
}
Expand All @@ -42,6 +43,11 @@ func NewMockManager(ctx context.Context, id string) Manager {
}
}

// Ctx implements Manager.Ctx interface
func (m *mockManager) Ctx() context.Context {
return m.ctx
}

// ID implements Manager.ID interface.
func (m *mockManager) ID() string {
return m.id
Expand Down

0 comments on commit 27bd71f

Please sign in to comment.