Skip to content

Commit

Permalink
limit the count of getting ddlhistory jobs
Browse files Browse the repository at this point in the history
Signed-off-by: joccau <[email protected]>
  • Loading branch information
joccau committed Aug 22, 2024
1 parent f751f73 commit 9388b9e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
8 changes: 8 additions & 0 deletions pkg/ddl/ddl_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import (
const (
DefNumHistoryJobs = 10
batchNumHistoryJobs = 128
// GetDDLHistoryCountMax is the max count for getting the ddl history once.
GetDDLHistoryCountMax = 1024
)

// AddHistoryDDLJob record the history job.
Expand Down Expand Up @@ -150,9 +152,15 @@ func GetAllHistoryDDLJobs(m *meta.Meta) ([]*model.Job, error) {
func ScanHistoryDDLJobs(m *meta.Meta, startJobID int64, limit int) ([]*model.Job, error) {
var iter meta.LastJobIterator
var err error

if startJobID == 0 {
// if 'start_job_id' == 0 and 'limit' == 0(default value), get the 1024 ddl history job by defaultly.
if limit == 0 {
limit = GetDDLHistoryCountMax
}
iter, err = m.GetLastHistoryDDLJobsIterator()
} else {
// if 'start_job_id' > 0, it must set value to 'limit'
if limit == 0 {
return nil, errors.New("when 'start_job_id' is specified, it must work with a 'limit'")
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/ddl/ddl_history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,14 @@ func TestDDLHistoryBasic(t *testing.T) {

require.NoError(t, err)
}

func TestScanHistoryDDLJobsWithErrorLimit(t *testing.T) {
var (
m = &meta.Meta{}
startJobID int64 = 10
limit = 0
)

_, err := ddl.ScanHistoryDDLJobs(m, startJobID, limit)
require.ErrorContains(t, err, "when 'start_job_id' is specified, it must work with a 'limit'")
}
18 changes: 9 additions & 9 deletions pkg/server/handler/tikvhandler/tikv_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1125,8 +1125,11 @@ 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) {
var jobID, limitID int
var err error
var (
jobID = 0
limitID = 0
err error
)
if jobValue := req.FormValue(handler.JobID); len(jobValue) > 0 {
jobID, err = strconv.Atoi(jobValue)
if err != nil {
Expand All @@ -1144,8 +1147,9 @@ func (h DDLHistoryJobHandler) ServeHTTP(w http.ResponseWriter, req *http.Request
handler.WriteError(w, err)
return
}
if limitID < 1 {
handler.WriteError(w, errors.New("ddl history limit must be greater than 0"))
if limitID < 1 || limitID > ddl.GetDDLHistoryCountMax {
handler.WriteError(w,
errors.Errorf("ddl history limit must be greater than 0 and less than or equal to %v", ddl.GetDDLHistoryCountMax))
return
}
}
Expand All @@ -1165,11 +1169,7 @@ func (h DDLHistoryJobHandler) getHistoryDDL(jobID, limit int) (jobs []*model.Job
}
txnMeta := meta.NewMeta(txn)

if jobID == 0 && limit == 0 {
jobs, err = ddl.GetAllHistoryDDLJobs(txnMeta)
} else {
jobs, err = ddl.ScanHistoryDDLJobs(txnMeta, int64(jobID), limit)
}
jobs, err = ddl.ScanHistoryDDLJobs(txnMeta, int64(jobID), limit)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down

0 comments on commit 9388b9e

Please sign in to comment.