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

owner: fix bug of owner manager hang #58622

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion pkg/owner/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ go_test(
],
embed = [":owner"],
flaky = True,
shard_count = 11,
shard_count = 12,
deps = [
"//pkg/parser/terror",
"//pkg/testkit/testfailpoint",
Expand Down
24 changes: 24 additions & 0 deletions pkg/owner/fail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/tests/v3/integration"
"google.golang.org/grpc"
)

Expand Down Expand Up @@ -110,3 +111,26 @@ func TestFailNewSession(t *testing.T) {
require.Truef(t, isContextDone, "err %v", err)
}()
}

func TestOwnerManagerClose(t *testing.T) {
integration.BeforeTestExternal(t)
cluster := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
defer cluster.Terminate(t)
client := cluster.Client(0)
ownerMgr := NewOwnerManager(context.Background(), client, "ddl", "1", "/owner/key")
err := ownerMgr.CampaignOwner()
require.NoError(t, err)
time.Sleep(100 * time.Millisecond)

require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/pkg/util/waitCancelCtx", `return(true)`))
defer func() {
// require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/util/waitCancelCtx"))
failpoint.Disable("github.com/pingcap/tidb/pkg/util/waitCancelCtx")
}()
// Close etcd session causes campaign loop to refreshSession()
// When the grpc is not available, the campaign should not hang.
ownerMgr.etcdSes.Close()

// This should not hang.
ownerMgr.Close()
Copy link
Contributor Author

@tiancaiamao tiancaiamao Dec 30, 2024

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.

}
16 changes: 8 additions & 8 deletions pkg/owner/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the core of the fix.
We should pass the campaignContext.

The code is:

campaignContext, cancel := context.WithCancel(m.ctx)
go campaignLoop(campaignContext)
cancel()

If we use m.ctx in the campaign loop, cancel() cannot cancel the ctx!

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
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/util/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ func NewSession(ctx context.Context, logPrefix string, etcdCli *clientv3.Client,
}
})

failpoint.Inject("waitCancelCtx", func(val failpoint.Value) {
if val.(bool) {
<-ctx.Done()
ctx = context.Background()
}
})

startTime := time.Now()
etcdSession, err = concurrency.NewSession(etcdCli,
concurrency.WithTTL(ttl), concurrency.WithContext(ctx))
Expand Down