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

server: support resigning ddl owner, use http method ddl/owner/resign #7649

Merged
merged 16 commits into from
Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
8 changes: 8 additions & 0 deletions docs/tidb_http_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,11 @@ timezone.*
curl http://{TiDBIP}:10080/tables/{colID}/{colFlag}/{colLen}?rowBin={val}
```
*Hint: For the column which field type is timezone dependent, e.g. `timestamp`, convert its value to UTC timezone.*

1. Resign the ddl owner, let tidb start a new ddl owner election.

```shell
curl http://{TiDBIP}:10080/ddl/owner/resign
Copy link
Member

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.

```

**Note**: If you request a tidb that is not ddl owner, the response will be `This node is not a ddl owner, can't be resigned.`
67 changes: 53 additions & 14 deletions owner/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"math"
"os"
"strconv"
"sync"
"sync/atomic"
"time"

Expand Down Expand Up @@ -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()
}
Expand All @@ -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),
}
}

Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

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

Can we call RetireOwner here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions owner/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,11 @@ func (m *mockManager) CampaignOwner(_ context.Context) error {
m.SetOwner(true)
return nil
}

// ResignOwner lets the owner start a new election.
func (m *mockManager) ResignOwner(ctx context.Context) error {
if m.IsOwner() {
m.SetOwner(false)
}
return nil
}
36 changes: 36 additions & 0 deletions server/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

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

We can use session.GetDomain().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What is the difference? Use session.GetDomain() will look likes:

dom, err := session.GetDomain(s.GetStore())
	if err != nil {
		return errors.Trace(err)
	}

Copy link
Contributor

Choose a reason for hiding this comment

The 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 {
}
Expand Down Expand Up @@ -334,6 +353,11 @@ type ddlHistoryJobHandler struct {
*tikvHandlerTool
}

// ddlResignOwnerHandler is the handler for resigning ddl owner.
type ddlResignOwnerHandler struct {
*tikvHandlerTool
Copy link
Member

Choose a reason for hiding this comment

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

This is not operated on TiKV.
How about we implement the method on ddlResignOwnerHandler directly?

}

type serverInfoHandler struct {
*tikvHandlerTool
}
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions server/http_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (s *Server) startHTTPServer() {
router.Handle("/schema/{db}/{table}", schemaHandler{tikvHandlerTool})
router.Handle("/tables/{colID}/{colTp}/{colFlag}/{colLen}", valueHandler{})
router.Handle("/ddl/history", ddlHistoryJobHandler{tikvHandlerTool})
router.Handle("/ddl/owner/resign", ddlResignOwnerHandler{tikvHandlerTool})

// HTTP path for get server info.
router.Handle("/info", serverInfoHandler{tikvHandlerTool})
Expand Down