From 8af72911e6d18c8abfa90e0b15cd175fbb18555f Mon Sep 17 00:00:00 2001 From: ekexium Date: Thu, 4 Mar 2021 17:51:40 +0800 Subject: [PATCH 1/3] server: add HTTP API to enable/disable async commit and 1PC Signed-off-by: ekexium --- docs/tidb_http_api.md | 15 +++++++++++++++ server/http_handler.go | 33 +++++++++++++++++++++++++++++++++ server/http_handler_test.go | 20 ++++++++++++++++++++ server/http_status.go | 4 ++-- 4 files changed, 70 insertions(+), 2 deletions(-) diff --git a/docs/tidb_http_api.md b/docs/tidb_http_api.md index 2caddcec5981d..d948146b58b83 100644 --- a/docs/tidb_http_api.md +++ b/docs/tidb_http_api.md @@ -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 + ``` + diff --git a/server/http_handler.go b/server/http_handler.go index 5a3b59e6de6ae..1beedcb6d56b1 100644 --- a/server/http_handler.go +++ b/server/http_handler.go @@ -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. @@ -720,6 +721,38 @@ 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 + } + switch asyncCommit { + case "0": + s.GetSessionVars().GlobalVarsAccessor.SetGlobalSysVar(variable.TiDBEnableAsyncCommit, variable.BoolOff) + case "1": + s.GetSessionVars().GlobalVarsAccessor.SetGlobalSysVar(variable.TiDBEnableAsyncCommit, variable.BoolOn) + default: + writeError(w, errors.New("illegal argument")) + 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 + } + switch onePC { + case "0": + s.GetSessionVars().GlobalVarsAccessor.SetGlobalSysVar(variable.TiDBEnable1PC, variable.BoolOff) + case "1": + s.GetSessionVars().GlobalVarsAccessor.SetGlobalSysVar(variable.TiDBEnable1PC, variable.BoolOn) + default: + writeError(w, errors.New("illegal argument")) + return + } + } if ddlSlowThreshold := req.Form.Get("ddl_slow_threshold"); ddlSlowThreshold != "" { threshold, err1 := strconv.Atoi(ddlSlowThreshold) if err1 != nil { diff --git a/server/http_handler_test.go b/server/http_handler_test.go index d2e75d319ec36..89b41d0aa3db3 100644 --- a/server/http_handler_test.go +++ b/server/http_handler_test.go @@ -1104,9 +1104,14 @@ 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) @@ -1114,9 +1119,18 @@ func (ts *HTTPHandlerTestSuite) TestPostSettings(c *C) { 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) @@ -1124,6 +1138,12 @@ func (ts *HTTPHandlerTestSuite) TestPostSettings(c *C) { 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 diff --git a/server/http_status.go b/server/http_status.go index f956c4cfb3e93..4671da3b4bdd9 100644 --- a/server/http_status.go +++ b/server/http_status.go @@ -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}) From 5277183e9ab30f28f6bb61caf82774973eade7cb Mon Sep 17 00:00:00 2001 From: ekexium Date: Thu, 4 Mar 2021 18:48:19 +0800 Subject: [PATCH 2/3] check errors in test Signed-off-by: ekexium --- go.mod | 2 +- go.sum | 4 ++-- server/http_handler.go | 16 ++++++++++++---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index a04c6589d348e..0a8ddf3543c9e 100644 --- a/go.mod +++ b/go.mod @@ -80,7 +80,7 @@ require ( gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect gopkg.in/natefinch/lumberjack.v2 v2.0.0 gopkg.in/yaml.v2 v2.3.0 // indirect - honnef.co/go/tools v0.1.1 // indirect + honnef.co/go/tools v0.1.2 // indirect sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0 sourcegraph.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67 ) diff --git a/go.sum b/go.sum index b4c4d54c3b3de..3418b16919230 100644 --- a/go.sum +++ b/go.sum @@ -848,8 +848,8 @@ honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= -honnef.co/go/tools v0.1.1 h1:EVDuO03OCZwpV2t/tLLxPmPiomagMoBOgfPt0FM+4IY= -honnef.co/go/tools v0.1.1/go.mod h1:NgwopIslSNH47DimFoV78dnkksY2EFtX0ajyb3K/las= +honnef.co/go/tools v0.1.2 h1:SMdYLJl312RXuxXziCCHhRsp/tvct9cGKey0yv95tZM= +honnef.co/go/tools v0.1.2/go.mod h1:NgwopIslSNH47DimFoV78dnkksY2EFtX0ajyb3K/las= k8s.io/klog v1.0.0/go.mod h1:4Bi6QPql/J/LkTDqv7R/cd3hPo4k2DG6Ptcz060Ez5I= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs= diff --git a/server/http_handler.go b/server/http_handler.go index 1beedcb6d56b1..e1528cfcd8b63 100644 --- a/server/http_handler.go +++ b/server/http_handler.go @@ -729,13 +729,17 @@ func (h settingsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { } switch asyncCommit { case "0": - s.GetSessionVars().GlobalVarsAccessor.SetGlobalSysVar(variable.TiDBEnableAsyncCommit, variable.BoolOff) + err = s.GetSessionVars().GlobalVarsAccessor.SetGlobalSysVar(variable.TiDBEnableAsyncCommit, variable.BoolOff) case "1": - s.GetSessionVars().GlobalVarsAccessor.SetGlobalSysVar(variable.TiDBEnableAsyncCommit, variable.BoolOn) + 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)) @@ -745,13 +749,17 @@ func (h settingsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { } switch onePC { case "0": - s.GetSessionVars().GlobalVarsAccessor.SetGlobalSysVar(variable.TiDBEnable1PC, variable.BoolOff) + err = s.GetSessionVars().GlobalVarsAccessor.SetGlobalSysVar(variable.TiDBEnable1PC, variable.BoolOff) case "1": - s.GetSessionVars().GlobalVarsAccessor.SetGlobalSysVar(variable.TiDBEnable1PC, variable.BoolOn) + 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) From 127290d69cb0b424fe32d800244d5fefb8534574 Mon Sep 17 00:00:00 2001 From: ekexium Date: Fri, 5 Mar 2021 12:26:02 +0800 Subject: [PATCH 3/3] close sessions Signed-off-by: ekexium --- go.sum | 4 ---- server/http_handler.go | 6 ++++++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/go.sum b/go.sum index 3418b16919230..d9de852cbfdd0 100644 --- a/go.sum +++ b/go.sum @@ -39,10 +39,8 @@ github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmx github.com/VividCortex/mysqlerr v0.0.0-20200629151747-c28746d985dd/go.mod h1:f3HiCrHjHBdcm6E83vGaXh1KomZMA2P6aeo3hKx/wg0= github.com/Xeoncross/go-aesctr-with-hmac v0.0.0-20200623134604-12b17a7ff502/go.mod h1:pmnBM9bxWSiHvC/gSWunUIyDvGn33EkP2CUjxFKtTTM= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= -github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= -github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4 h1:Hs82Z41s6SdL1CELW+XaDYmOH4hkBN4/N9og/AsOv7E= github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/antihax/optional v0.0.0-20180407024304-ca021399b1a6/go.mod h1:V8iCPQYkqmusNa815XgQio277wI47sdRh1dUOLdyC6Q= github.com/appleboy/gin-jwt/v2 v2.6.3/go.mod h1:MfPYA4ogzvOcVkRwAxT7quHOtQmVKDpTwxyUrC2DNw0= @@ -467,7 +465,6 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sasha-s/go-deadlock v0.2.0/go.mod h1:StQn567HiB1fF2yJ44N9au7wOhrPS3iZqiDbRupzT10= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= -github.com/sergi/go-diff v1.0.1-0.20180205163309-da645544ed44 h1:tB9NOR21++IjLyVx3/PCPhWMwqGNCMQEH96A6dMZ/gc= github.com/sergi/go-diff v1.0.1-0.20180205163309-da645544ed44/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/shirou/gopsutil v2.19.10+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/shirou/gopsutil v3.20.12+incompatible h1:6VEGkOXP/eP4o2Ilk8cSsX0PhOEfX6leqAnD+urrp9M= @@ -810,7 +807,6 @@ google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8 google.golang.org/grpc v1.27.1 h1:zvIju4sqAGvwKspUQOhwnpcqSbzi7/H6QomNNjTL4sk= google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= gopkg.in/alecthomas/gometalinter.v2 v2.0.12/go.mod h1:NDRytsqEZyolNuAgTzJkZMkSQM7FIKyzVzGhjB/qfYo= -gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20180810215634-df19058c872c/go.mod h1:3HH7i1SgMqlzxCcBmUHW657sD4Kvv9sC3HpL3YukzwA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/server/http_handler.go b/server/http_handler.go index e1528cfcd8b63..d2705d721e3c2 100644 --- a/server/http_handler.go +++ b/server/http_handler.go @@ -727,6 +727,9 @@ func (h settingsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { writeError(w, err) return } + if s != nil { + defer s.Close() + } switch asyncCommit { case "0": err = s.GetSessionVars().GlobalVarsAccessor.SetGlobalSysVar(variable.TiDBEnableAsyncCommit, variable.BoolOff) @@ -747,6 +750,9 @@ func (h settingsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { writeError(w, err) return } + if s != nil { + defer s.Close() + } switch onePC { case "0": err = s.GetSessionVars().GlobalVarsAccessor.SetGlobalSysVar(variable.TiDBEnable1PC, variable.BoolOff)