-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
server: support resigning ddl owner, use http method ddl/owner/resign #7649
Changes from 5 commits
8453481
3ac8bfa
0e10bb0
91406bc
861a528
3e1481c
aa218ae
30172e0
c90f2c5
e7c24f0
d12ce8d
e3f88d2
24cdaf7
e376c2b
d94720a
b89de26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import ( | |
"math" | ||
"os" | ||
"strconv" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
|
@@ -51,6 +52,8 @@ type Manager interface { | |
GetOwnerID(ctx context.Context) (string, error) | ||
// CampaignOwner campaigns the owner. | ||
CampaignOwner(ctx context.Context) error | ||
// ResignOwner lets the owner start a new election. | ||
ResignOwner(ctx context.Context) error | ||
// Cancel cancels this etcd ownerManager campaign. | ||
Cancel() | ||
} | ||
|
@@ -70,22 +73,26 @@ type DDLOwnerChecker interface { | |
|
||
// ownerManager represents the structure which is used for electing owner. | ||
type ownerManager struct { | ||
owner int32 | ||
id string // id is the ID of the manager. | ||
key string | ||
prompt string | ||
etcdCli *clientv3.Client | ||
cancel context.CancelFunc | ||
owner int32 | ||
id string // id is the ID of the manager. | ||
key string | ||
prompt string | ||
logPrefix string | ||
etcdCli *clientv3.Client | ||
cancel context.CancelFunc | ||
elec *concurrency.Election | ||
mu sync.Mutex | ||
} | ||
|
||
// NewOwnerManager creates a new Manager. | ||
func NewOwnerManager(etcdCli *clientv3.Client, prompt, id, key string, cancel context.CancelFunc) Manager { | ||
return &ownerManager{ | ||
etcdCli: etcdCli, | ||
id: id, | ||
key: key, | ||
prompt: prompt, | ||
cancel: cancel, | ||
etcdCli: etcdCli, | ||
id: id, | ||
key: key, | ||
prompt: prompt, | ||
cancel: cancel, | ||
logPrefix: fmt.Sprintf("[%s] %s ownerManager %s", prompt, key, id), | ||
} | ||
} | ||
|
||
|
@@ -179,6 +186,37 @@ func (m *ownerManager) CampaignOwner(ctx context.Context) error { | |
return nil | ||
} | ||
|
||
// ResignOwner lets the owner start a new election. | ||
func (m *ownerManager) ResignOwner(ctx context.Context) error { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
if !m.IsOwner() || m.elec == nil { | ||
return errors.Errorf("This node is not a ddl owner, can't be resigned.") | ||
} | ||
|
||
err := m.elec.Resign(ctx) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
||
log.Warnf("%s Resign ddl owner success!", m.logPrefix) | ||
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. Can we call 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. No, this is not the work of ResignOwner. |
||
return nil | ||
} | ||
|
||
func (m *ownerManager) toBeOwner(elec *concurrency.Election) { | ||
m.mu.Lock() | ||
m.elec = elec | ||
m.SetOwner(true) | ||
m.mu.Unlock() | ||
} | ||
|
||
func (m *ownerManager) retireOwner() { | ||
m.mu.Lock() | ||
m.SetOwner(false) | ||
m.elec = nil | ||
m.mu.Unlock() | ||
} | ||
|
||
func (m *ownerManager) campaignLoop(ctx context.Context, etcdSession *concurrency.Session) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
|
@@ -188,7 +226,7 @@ func (m *ownerManager) campaignLoop(ctx context.Context, etcdSession *concurrenc | |
} | ||
}() | ||
|
||
logPrefix := fmt.Sprintf("[%s] %s ownerManager %s", m.prompt, m.key, m.id) | ||
logPrefix := m.logPrefix | ||
var err error | ||
for { | ||
if err != nil { | ||
|
@@ -232,9 +270,10 @@ func (m *ownerManager) campaignLoop(ctx context.Context, etcdSession *concurrenc | |
if err != nil { | ||
continue | ||
} | ||
m.SetOwner(true) | ||
|
||
m.toBeOwner(elec) | ||
m.watchOwner(ctx, etcdSession, ownerKey) | ||
m.SetOwner(false) | ||
m.retireOwner() | ||
|
||
metrics.CampaignOwnerCounter.WithLabelValues(m.prompt, metrics.NoLongerOwner).Inc() | ||
log.Warnf("%s isn't the owner", logPrefix) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -303,6 +303,25 @@ func (t *tikvHandlerTool) getAllHistoryDDL() ([]*model.Job, error) { | |
return jobs, nil | ||
} | ||
|
||
func (t *tikvHandlerTool) resignDDLOwner() error { | ||
s, err := session.CreateSession(t.store.(kv.Storage)) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
||
if s != nil { | ||
defer s.Close() | ||
} | ||
|
||
dom := domain.GetDomain(s.(sessionctx.Context)) | ||
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. We can use 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. What is the difference? Use session.GetDomain() will look likes:
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. The difference is that we do not need to create a session. |
||
ownerMgr := dom.DDL().OwnerManager() | ||
err = ownerMgr.ResignOwner(context.Background()) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
return nil | ||
} | ||
|
||
// settingsHandler is the handler for list tidb server settings. | ||
type settingsHandler struct { | ||
} | ||
|
@@ -334,6 +353,11 @@ type ddlHistoryJobHandler struct { | |
*tikvHandlerTool | ||
} | ||
|
||
// ddlResignOwnerHandler is the handler for resigning ddl owner. | ||
type ddlResignOwnerHandler struct { | ||
*tikvHandlerTool | ||
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 not operated on TiKV. |
||
} | ||
|
||
type serverInfoHandler struct { | ||
*tikvHandlerTool | ||
} | ||
|
@@ -713,6 +737,18 @@ func (h ddlHistoryJobHandler) ServeHTTP(w http.ResponseWriter, req *http.Request | |
return | ||
} | ||
|
||
// ServeHTTP handles request of resigning ddl owner. | ||
func (h ddlResignOwnerHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { | ||
err := h.resignDDLOwner() | ||
if err != nil { | ||
log.Error(err) | ||
writeError(w, err) | ||
return | ||
} | ||
|
||
writeData(w, "success!") | ||
} | ||
|
||
func (h tableHandler) getPDAddr() ([]string, error) { | ||
var pdAddrs []string | ||
etcd, ok := h.store.(domain.EtcdBackend) | ||
|
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.
This should be a
POST
request.