Skip to content

Commit

Permalink
server: add HTTP API to enable/disable async commit and 1PC (#23115)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekexium authored Mar 9, 2021
1 parent b42a6f4 commit 01be9ce
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
15 changes: 15 additions & 0 deletions docs/tidb_http_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,3 +477,18 @@ timezone.*
* op=nowait: return after binlog status is recoverd, do not wait until the skipped-binlog transactions are committed.
* op=reset: reset `SkippedCommitterCounter` to 0 to avoid the problem that `SkippedCommitterCounter` is not cleared due to some unusual cases.
* op=status: Get the current status of binlog recovery.
1. Enable/disable async commit feature
```shell
curl -X POST -d "tidb_enable_async_commit=1" http://{TiDBIP}:10080/settings
curl -X POST -d "tidb_enable_async_commit=0" http://{TiDBIP}:10080/settings
```
1. Enable/disable one-phase commit feature
```shell
curl -X POST -d "tidb_enable_1pc=1" http://{TiDBIP}:10080/settings
curl -X POST -d "tidb_enable_1pc=0" http://{TiDBIP}:10080/settings
```
47 changes: 47 additions & 0 deletions server/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ func (t *tikvHandlerTool) handleMvccGetByHex(params map[string]string) (*mvccKV,

// settingsHandler is the handler for list tidb server settings.
type settingsHandler struct {
*tikvHandlerTool
}

// binlogRecover is used to recover binlog service.
Expand Down Expand Up @@ -720,6 +721,52 @@ func (h settingsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return
}
}
if asyncCommit := req.Form.Get("tidb_enable_async_commit"); asyncCommit != "" {
s, err := session.CreateSession(h.Store.(kv.Storage))
if err != nil {
writeError(w, err)
return
}
if s != nil {
defer s.Close()
}
switch asyncCommit {
case "0":
err = s.GetSessionVars().GlobalVarsAccessor.SetGlobalSysVar(variable.TiDBEnableAsyncCommit, variable.BoolOff)
case "1":
err = s.GetSessionVars().GlobalVarsAccessor.SetGlobalSysVar(variable.TiDBEnableAsyncCommit, variable.BoolOn)
default:
writeError(w, errors.New("illegal argument"))
return
}
if err != nil {
writeError(w, err)
return
}
}
if onePC := req.Form.Get("tidb_enable_1pc"); onePC != "" {
s, err := session.CreateSession(h.Store.(kv.Storage))
if err != nil {
writeError(w, err)
return
}
if s != nil {
defer s.Close()
}
switch onePC {
case "0":
err = s.GetSessionVars().GlobalVarsAccessor.SetGlobalSysVar(variable.TiDBEnable1PC, variable.BoolOff)
case "1":
err = s.GetSessionVars().GlobalVarsAccessor.SetGlobalSysVar(variable.TiDBEnable1PC, variable.BoolOn)
default:
writeError(w, errors.New("illegal argument"))
return
}
if err != nil {
writeError(w, err)
return
}
}
if ddlSlowThreshold := req.Form.Get("ddl_slow_threshold"); ddlSlowThreshold != "" {
threshold, err1 := strconv.Atoi(ddlSlowThreshold)
if err1 != nil {
Expand Down
20 changes: 20 additions & 0 deletions server/http_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1104,26 +1104,46 @@ func (ts *HTTPHandlerTestSuite) TestPostSettings(c *C) {
ts.startServer(c)
ts.prepareData(c)
defer ts.stopServer(c)
se, err := session.CreateSession(ts.store.(kv.Storage))
c.Assert(err, IsNil)

form := make(url.Values)
form.Set("log_level", "error")
form.Set("tidb_general_log", "1")
form.Set("tidb_enable_async_commit", "1")
form.Set("tidb_enable_1pc", "1")
resp, err := ts.formStatus("/settings", form)
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
c.Assert(log.GetLevel(), Equals, log.ErrorLevel)
c.Assert(zaplog.GetLevel(), Equals, zap.ErrorLevel)
c.Assert(config.GetGlobalConfig().Log.Level, Equals, "error")
c.Assert(variable.ProcessGeneralLog.Load(), IsTrue)
val, err := variable.GetGlobalSystemVar(se.GetSessionVars(), variable.TiDBEnableAsyncCommit)
c.Assert(err, IsNil)
c.Assert(val, Equals, variable.BoolOn)
val, err = variable.GetGlobalSystemVar(se.GetSessionVars(), variable.TiDBEnable1PC)
c.Assert(err, IsNil)
c.Assert(val, Equals, variable.BoolOn)

form = make(url.Values)
form.Set("log_level", "fatal")
form.Set("tidb_general_log", "0")
form.Set("tidb_enable_async_commit", "0")
form.Set("tidb_enable_1pc", "0")
resp, err = ts.formStatus("/settings", form)
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
c.Assert(variable.ProcessGeneralLog.Load(), IsFalse)
c.Assert(log.GetLevel(), Equals, log.FatalLevel)
c.Assert(zaplog.GetLevel(), Equals, zap.FatalLevel)
c.Assert(config.GetGlobalConfig().Log.Level, Equals, "fatal")
val, err = variable.GetGlobalSystemVar(se.GetSessionVars(), variable.TiDBEnableAsyncCommit)
c.Assert(err, IsNil)
c.Assert(val, Equals, variable.BoolOff)
val, err = variable.GetGlobalSystemVar(se.GetSessionVars(), variable.TiDBEnable1PC)
c.Assert(err, IsNil)
c.Assert(val, Equals, variable.BoolOff)
form.Set("log_level", os.Getenv("log_level"))

// test ddl_slow_threshold
Expand Down
4 changes: 2 additions & 2 deletions server/http_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ func (s *Server) startHTTPServer() {
router.Handle("/stats/dump/{db}/{table}", s.newStatsHandler()).Name("StatsDump")
router.Handle("/stats/dump/{db}/{table}/{snapshot}", s.newStatsHistoryHandler()).Name("StatsHistoryDump")

router.Handle("/settings", settingsHandler{}).Name("Settings")
tikvHandlerTool := s.newTikvHandlerTool()
router.Handle("/settings", settingsHandler{tikvHandlerTool}).Name("Settings")
router.Handle("/binlog/recover", binlogRecover{}).Name("BinlogRecover")

tikvHandlerTool := s.newTikvHandlerTool()
router.Handle("/schema", schemaHandler{tikvHandlerTool}).Name("Schema")
router.Handle("/schema/{db}", schemaHandler{tikvHandlerTool})
router.Handle("/schema/{db}/{table}", schemaHandler{tikvHandlerTool})
Expand Down

0 comments on commit 01be9ce

Please sign in to comment.