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

ddl: add slow operations log to add indices, make trouble shooting easier. #7060

Merged
merged 8 commits into from
Jul 17, 2018
18 changes: 18 additions & 0 deletions ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package ddl

import (
"context"
"sync/atomic"
"time"

"github.com/juju/errors"
Expand Down Expand Up @@ -552,8 +553,13 @@ func (w *addIndexWorker) fetchRowColVals(txn kv.Transaction, taskRange reorgInde
w.idxRecords = w.idxRecords[:0]
startTime := time.Now()
handleOutOfRange := false
oprStartTime := time.Now()
err := iterateSnapshotRows(w.sessCtx.GetStore(), w.table, txn.StartTS(), taskRange.startHandle,
func(handle int64, recordKey kv.Key, rawRow []byte) (bool, error) {
oprEndTime := time.Now()
w.logSlowOperations(oprEndTime.Sub(oprStartTime), "iterateSnapshotRows in fetchRowColVals", 0)
oprStartTime = oprEndTime

if !taskRange.endIncluded {
handleOutOfRange = handle >= taskRange.endHandle
} else {
Expand Down Expand Up @@ -582,11 +588,22 @@ func (w *addIndexWorker) fetchRowColVals(txn kv.Transaction, taskRange reorgInde
return w.idxRecords, handleOutOfRange, errors.Trace(err)
}

func (w *addIndexWorker) logSlowOperations(elapsed time.Duration, slowMsg string, threshold uint32) {
if threshold == 0 {
threshold = atomic.LoadUint32(&variable.DDLSlowOprThreshold)
}

if elapsed >= time.Duration(threshold)*time.Millisecond {
log.Infof("[ddl-reorg][SLOW-OPERATIONS] elapsed time: %v, message: %v", elapsed, slowMsg)
}
}

// backfillIndexInTxn will backfill table index in a transaction, lock corresponding rowKey, if the value of rowKey is changed,
// indicate that index columns values may changed, index is not allowed to be added, so the txn will rollback and retry.
// backfillIndexInTxn will add w.batchCnt indices once, default value of w.batchCnt is 128.
// TODO: make w.batchCnt can be modified by system variable.
func (w *addIndexWorker) backfillIndexInTxn(handleRange reorgIndexTask) (nextHandle int64, addedCount, scanCount int, errInTxn error) {
oprStartTime := time.Now()
errInTxn = kv.RunInNewTxn(w.sessCtx.GetStore(), true, func(txn kv.Transaction) error {
addedCount = 0
scanCount = 0
Expand Down Expand Up @@ -624,6 +641,7 @@ func (w *addIndexWorker) backfillIndexInTxn(handleRange reorgIndexTask) (nextHan
}
return nil
})
w.logSlowOperations(time.Since(oprStartTime), "backfillIndexInTxn", 3000)
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 a constant threshold?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think 3s is large enough, so no needs to be configurable.


return
}
Expand Down
8 changes: 8 additions & 0 deletions docs/tidb_http_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ timezone.*
curl -x POST -d "log_level=info" http://{TiDBIP}:10080/settings
```

1. Change TiDB DDL slow log threshold

The unit is millisecond.

```shell
curl -x POST -d "ddl_slow_threshold=300" http://{TiDBIP}:10080/settings
```

1. Get the column value by an encoded row and some information that can be obtained from a column of the table schema information.

Argument example: rowBin=base64_encoded_row_value
Expand Down
11 changes: 11 additions & 0 deletions server/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,17 @@ func (h settingsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return
}
}
if ddlSlowThreshold := req.Form.Get("ddl_slow_threshold"); ddlSlowThreshold != "" {
threshold, err1 := strconv.Atoi(ddlSlowThreshold)
if err1 != nil {
writeError(w, err1)
return
}
if threshold > 0 {
atomic.StoreUint32(&variable.DDLSlowOprThreshold, uint32(threshold))
}
}

} else {
writeData(w, config.GetGlobalConfig())
}
Expand Down
8 changes: 8 additions & 0 deletions server/http_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,14 @@ func (ts *HTTPHandlerTestSuite) TestPostSettings(c *C) {
c.Assert(atomic.LoadUint32(&variable.ProcessGeneralLog), Equals, uint32(0))
c.Assert(log.GetLevel(), Equals, log.InfoLevel)
c.Assert(config.GetGlobalConfig().Log.Level, Equals, "info")

// test ddl_slow_threshold
form = make(url.Values)
form.Set("ddl_slow_threshold", "200")
resp, err = http.PostForm("http://127.0.0.1:10090/settings", form)
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
c.Assert(atomic.LoadUint32(&variable.DDLSlowOprThreshold), Equals, uint32(200))
}

func (ts *HTTPHandlerTestSuite) TestPprof(c *C) {
Expand Down
2 changes: 2 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,6 @@ var (
ProcessGeneralLog uint32
ddlReorgWorkerCounter int32 = DefTiDBDDLReorgWorkerCount
maxDDLReorgWorkerCount int32 = 128
// DDLSlowOprThreshold is the threshold for ddl slow operations, uint is millisecond.
DDLSlowOprThreshold uint32 = 300
)