-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
owner: fix bug of owner manager hang #58622
Open
tiancaiamao
wants to merge
1
commit into
pingcap:master
Choose a base branch
from
tiancaiamao:issue58620
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+40
−9
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,7 +144,7 @@ type ownerManager struct { | |
} | ||
|
||
// NewOwnerManager creates a new Manager. | ||
func NewOwnerManager(ctx context.Context, etcdCli *clientv3.Client, prompt, id, key string) Manager { | ||
func NewOwnerManager(ctx context.Context, etcdCli *clientv3.Client, prompt, id, key string) *ownerManager { | ||
return &ownerManager{ | ||
etcdCli: etcdCli, | ||
id: id, | ||
|
@@ -176,9 +176,9 @@ func (m *ownerManager) SetListener(listener Listener) { | |
m.listener = listener | ||
} | ||
|
||
func (m *ownerManager) ForceToBeOwner(context.Context) error { | ||
func (m *ownerManager) ForceToBeOwner(ctx context.Context) error { | ||
m.logger.Info("force to be owner") | ||
if err := m.refreshSession(util2.NewSessionDefaultRetryCnt, ManagerSessionTTL); err != nil { | ||
if err := m.refreshSession(ctx, util2.NewSessionDefaultRetryCnt, ManagerSessionTTL); err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
||
|
@@ -287,7 +287,7 @@ func (m *ownerManager) CampaignOwner(withTTL ...int) error { | |
} | ||
if m.etcdSes == nil { | ||
m.logger.Info("start campaign owner") | ||
if err := m.refreshSession(util2.NewSessionDefaultRetryCnt, ttl); err != nil { | ||
if err := m.refreshSession(m.ctx, util2.NewSessionDefaultRetryCnt, ttl); err != nil { | ||
return errors.Trace(err) | ||
} | ||
} else { | ||
|
@@ -364,13 +364,13 @@ func (m *ownerManager) campaignLoop(campaignContext context.Context) { | |
select { | ||
case <-m.etcdSes.Done(): | ||
m.logger.Info("etcd session done, refresh it") | ||
if err2 := m.refreshSession(util2.NewSessionRetryUnlimited, ManagerSessionTTL); err2 != nil { | ||
if err2 := m.refreshSession(campaignContext, util2.NewSessionRetryUnlimited, ManagerSessionTTL); err2 != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the core of the fix. The code is:
If we use |
||
m.logger.Info("break campaign loop, refresh session failed", zap.Error(err2)) | ||
return | ||
} | ||
case <-leaseNotFoundCh: | ||
m.logger.Info("meet lease not found error, refresh session") | ||
if err2 := m.refreshSession(util2.NewSessionRetryUnlimited, ManagerSessionTTL); err2 != nil { | ||
if err2 := m.refreshSession(campaignContext, util2.NewSessionRetryUnlimited, ManagerSessionTTL); err2 != nil { | ||
m.logger.Info("break campaign loop, refresh session failed", zap.Error(err2)) | ||
return | ||
} | ||
|
@@ -434,7 +434,7 @@ func (m *ownerManager) closeSession() { | |
} | ||
} | ||
|
||
func (m *ownerManager) refreshSession(retryCnt, ttl int) error { | ||
func (m *ownerManager) refreshSession(ctx context.Context, retryCnt, ttl int) error { | ||
m.closeSession() | ||
// Note: we must use manager's context to create session. If we use campaign | ||
// context and the context is cancelled, the created session cannot be closed | ||
|
@@ -443,7 +443,7 @@ func (m *ownerManager) refreshSession(retryCnt, ttl int) error { | |
// loop is refreshing the session, it might wait for a long time to return, it | ||
// should be fine as long as network is ok, and acceptable to wait when not. | ||
logPrefix := fmt.Sprintf("[%s] %s ownerManager %s", m.prompt, m.key, m.id) | ||
sess, err2 := util2.NewSession(m.ctx, logPrefix, m.etcdCli, retryCnt, ttl) | ||
sess, err2 := util2.NewSession(ctx, logPrefix, m.etcdCli, retryCnt, ttl) | ||
if err2 != nil { | ||
return errors.Trace(err2) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without the fix, the test hang... because the old code use wrong ctx and cancel in owner manager Close() does make campaign loop break.