-
Notifications
You must be signed in to change notification settings - Fork 6k
ddl: use a global owner manager instance for DDL, to avoid owner change #57179
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
db9d126
tmp
D3Hunter 0e79fd3
change
D3Hunter e3b0cd5
change
D3Hunter 0715c44
change
D3Hunter b6256b5
change
D3Hunter d41bc3b
change
D3Hunter 1b0498f
Merge remote-tracking branch 'upstream/master' into start-ddl-owner-once
D3Hunter 65efbbb
change
D3Hunter cd2a9dd
change
D3Hunter 411fa62
Merge remote-tracking branch 'upstream/master' into start-ddl-owner-once
D3Hunter dea61c2
Merge remote-tracking branch 'upstream/master' into start-ddl-owner-once
D3Hunter a815d88
change
D3Hunter d3db505
br change
D3Hunter dcd6b27
change
D3Hunter 597eae0
br change
D3Hunter 02a2c4a
for br
D3Hunter 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ddl | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/google/uuid" | ||
"github.com/pingcap/errors" | ||
"github.com/pingcap/failpoint" | ||
"github.com/pingcap/tidb/pkg/config" | ||
"github.com/pingcap/tidb/pkg/kv" | ||
"github.com/pingcap/tidb/pkg/owner" | ||
storepkg "github.com/pingcap/tidb/pkg/store" | ||
"github.com/pingcap/tidb/pkg/util/logutil" | ||
clientv3 "go.etcd.io/etcd/client/v3" | ||
"go.uber.org/zap" | ||
) | ||
|
||
var globalOwnerManager = &ownerManager{} | ||
|
||
// StartOwnerManager starts a global DDL owner manager. | ||
func StartOwnerManager(ctx context.Context, store kv.Storage) error { | ||
return globalOwnerManager.Start(ctx, store) | ||
} | ||
|
||
// CloseOwnerManager closes the global DDL owner manager. | ||
func CloseOwnerManager() { | ||
globalOwnerManager.Close() | ||
} | ||
|
||
// ownerManager is used to manage lifecycle of a global DDL owner manager which | ||
// we only want it to init session once, to avoid DDL owner change after upgrade. | ||
type ownerManager struct { | ||
etcdCli *clientv3.Client | ||
id string | ||
ownerMgr owner.Manager | ||
started bool | ||
} | ||
|
||
// Start starts the TiDBInstance. | ||
func (om *ownerManager) Start(ctx context.Context, store kv.Storage) error { | ||
// BR might start domain multiple times, we need to avoid it. when BR have refactored | ||
// this part, we can remove this. | ||
if om.started { | ||
return nil | ||
} | ||
if config.GetGlobalConfig().Store != "tikv" { | ||
return nil | ||
} | ||
cli, err := storepkg.NewEtcdCli(store) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
failpoint.InjectCall("injectEtcdClient", &cli) | ||
if cli == nil { | ||
return errors.New("etcd client is nil, maybe the server is not started with PD") | ||
} | ||
om.id = uuid.New().String() | ||
om.etcdCli = cli | ||
om.ownerMgr = owner.NewOwnerManager(ctx, om.etcdCli, Prompt, om.id, DDLOwnerKey) | ||
om.started = true | ||
return nil | ||
} | ||
|
||
// Close closes the TiDBInstance. | ||
func (om *ownerManager) Close() { | ||
if om.ownerMgr != nil { | ||
om.ownerMgr.Close() | ||
} | ||
if om.etcdCli != nil { | ||
if err := om.etcdCli.Close(); err != nil { | ||
logutil.BgLogger().Error("close etcd client failed", zap.Error(err)) | ||
} | ||
} | ||
om.started = false | ||
} | ||
|
||
func (om *ownerManager) ID() string { | ||
return om.id | ||
} | ||
|
||
func (om *ownerManager) OwnerManager() owner.Manager { | ||
return om.ownerMgr | ||
} |
Oops, something went wrong.
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.
cc @Leavrth , please help remove this part after BR refactor