Skip to content

Commit

Permalink
HTTP API: support query the DDL history (pingcap#6245)
Browse files Browse the repository at this point in the history
  • Loading branch information
ciscoxll authored Apr 16, 2018
1 parent 3513319 commit d3290b9
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
71 changes: 71 additions & 0 deletions server/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx"
Expand Down Expand Up @@ -66,6 +67,7 @@ const (

// For query string
const qTableID = "table_id"
const qLimit = "limit"

const (
headerContentType = "Content-Type"
Expand Down Expand Up @@ -273,6 +275,31 @@ func (t *tikvHandlerTool) handleMvccGetByHex(params map[string]string) (interfac
return t.getMvccByEncodedKey(encodedKey)
}

func (t *tikvHandlerTool) getAllHistoryDDL() ([]*model.Job, error) {
s, err := session.CreateSession(t.store.(kv.Storage))
if err != nil {
return nil, errors.Trace(err)
}

if s != nil {
defer s.Close()
}

store := domain.GetDomain(s.(sessionctx.Context)).Store()
txn, err := store.Begin()

if err != nil {
return nil, errors.Trace(err)
}
txnMeta := meta.NewMeta(txn)

jobs, err := txnMeta.GetAllHistoryDDLJobs()
if err != nil {
return nil, errors.Trace(err)
}
return jobs, nil
}

// settingsHandler is the handler for list tidb server settings.
type settingsHandler struct {
}
Expand All @@ -294,6 +321,11 @@ type tableHandler struct {
op string
}

// ddlHistoryJobHandler is the handler for list job history.
type ddlHistoryJobHandler struct {
*tikvHandlerTool
}

// valueHandle is the handler for get value.
type valueHandler struct {
}
Expand Down Expand Up @@ -576,6 +608,45 @@ func (h tableHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}
}

// ServeHTTP handles request of ddl jobs history.
func (h ddlHistoryJobHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if limitID := req.FormValue(qLimit); len(limitID) > 0 {
lid, err := strconv.Atoi(limitID)

if err != nil {
writeError(w, err)
return
}

if lid < 1 {
writeError(w, errors.New("ddl history limit must be greater than 1"))
return
}

jobs, err := h.getAllHistoryDDL()
if err != nil {
writeError(w, errors.New("ddl history not found"))
return
}

jobsLen := len(jobs)
if jobsLen > lid {
start := jobsLen - lid
jobs = jobs[start:]
}

writeData(w, jobs)
return
}
jobs, err := h.getAllHistoryDDL()
if err != nil {
writeError(w, errors.New("ddl history not found"))
return
}
writeData(w, jobs)
return
}

func (h tableHandler) handleRegionRequest(schema infoschema.InfoSchema, tbl table.Table, w http.ResponseWriter, req *http.Request) {
tableID := tbl.Meta().ID
// for record
Expand Down
31 changes: 31 additions & 0 deletions server/http_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ import (
. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/kvrpcpb"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/mysql"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/store/mockstore/mocktikv"
Expand Down Expand Up @@ -547,3 +551,30 @@ func (ts *HTTPHandlerTestSuite) TestGetSchema(c *C) {
_, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/schema/tidb/abc"))
c.Assert(err, IsNil)
}

func (ts *HTTPHandlerTestSuite) TestAllHistory(c *C) {
ts.startServer(c)
ts.prepareData(c)
defer ts.stopServer(c)
resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:10090/ddl/history/?limit=3"))
c.Assert(err, IsNil)
resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/ddl/history/?limit=-1"))
c.Assert(err, IsNil)

resp, err = http.Get(fmt.Sprintf("http://127.0.0.1:10090/ddl/history"))
c.Assert(err, IsNil)
decoder := json.NewDecoder(resp.Body)

var jobs []*model.Job
s, _ := session.CreateSession(ts.server.newTikvHandlerTool().store.(kv.Storage))
defer s.Close()
store := domain.GetDomain(s.(sessionctx.Context)).Store()
txn, _ := store.Begin()
txnMeta := meta.NewMeta(txn)
txnMeta.GetAllHistoryDDLJobs()
data, _ := txnMeta.GetAllHistoryDDLJobs()
err = decoder.Decode(&jobs)

c.Assert(err, IsNil)
c.Assert(jobs, DeepEquals, data)
}
1 change: 1 addition & 0 deletions server/http_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (s *Server) startHTTPServer() {
router.Handle("/schema/{db}", schemaHandler{tikvHandlerTool})
router.Handle("/schema/{db}/{table}", schemaHandler{tikvHandlerTool})
router.Handle("/tables/{colID}/{colTp}/{colFlag}/{colLen}", valueHandler{})
router.Handle("/ddl/history", ddlHistoryJobHandler{tikvHandlerTool})
if s.cfg.Store == "tikv" {
// HTTP path for tikv
router.Handle("/tables/{db}/{table}/regions", tableHandler{tikvHandlerTool, opTableRegions})
Expand Down

0 comments on commit d3290b9

Please sign in to comment.