diff --git a/server/http_handler.go b/server/http_handler.go index 67babd1f05e8d..093a57e45c1ab 100644 --- a/server/http_handler.go +++ b/server/http_handler.go @@ -45,7 +45,6 @@ import ( "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/meta" "github.com/pingcap/tidb/session" - "github.com/pingcap/tidb/sessionctx" "github.com/pingcap/tidb/sessionctx/binloginfo" "github.com/pingcap/tidb/sessionctx/stmtctx" "github.com/pingcap/tidb/sessionctx/variable" @@ -355,11 +354,11 @@ func (t *tikvHandlerTool) getPartition(tableVal table.Table, partitionName strin } func (t *tikvHandlerTool) schema() (infoschema.InfoSchema, error) { - session, err := session.CreateSession(t.Store) + dom, err := session.GetDomain(t.Store) if err != nil { - return nil, errors.Trace(err) + return nil, err } - return domain.GetDomain(session.(sessionctx.Context)).InfoSchema(), nil + return dom.InfoSchema(), nil } func (t *tikvHandlerTool) handleMvccGetByHex(params map[string]string) (*mvccKV, error) { @@ -712,14 +711,13 @@ func (h settingsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { } } if asyncCommit := req.Form.Get("tidb_enable_async_commit"); asyncCommit != "" { - s, err := session.CreateSession(h.Store.(kv.Storage)) + s, err := session.CreateSession(h.Store) if err != nil { writeError(w, err) return } - if s != nil { - defer s.Close() - } + defer s.Close() + switch asyncCommit { case "0": err = s.GetSessionVars().GlobalVarsAccessor.SetGlobalSysVar(variable.TiDBEnableAsyncCommit, variable.Off) @@ -735,14 +733,13 @@ func (h settingsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { } } if onePC := req.Form.Get("tidb_enable_1pc"); onePC != "" { - s, err := session.CreateSession(h.Store.(kv.Storage)) + s, err := session.CreateSession(h.Store) if err != nil { writeError(w, err) return } - if s != nil { - defer s.Close() - } + defer s.Close() + switch onePC { case "0": err = s.GetSessionVars().GlobalVarsAccessor.SetGlobalSysVar(variable.TiDBEnable1PC, variable.Off) @@ -878,14 +875,11 @@ func (h flashReplicaHandler) getTiFlashReplicaInfo(tblInfo *model.TableInfo, rep } func (h flashReplicaHandler) getDropOrTruncateTableTiflash(currentSchema infoschema.InfoSchema) ([]*tableFlashReplicaInfo, error) { - s, err := session.CreateSession(h.Store.(kv.Storage)) + s, err := session.CreateSession(h.Store) if err != nil { return nil, errors.Trace(err) } - - if s != nil { - defer s.Close() - } + defer s.Close() store := domain.GetDomain(s).Store() txn, err := store.Begin() @@ -948,16 +942,18 @@ func (h flashReplicaHandler) handleStatusReport(w http.ResponseWriter, req *http writeError(w, err) return } - do, err := session.GetDomain(h.Store.(kv.Storage)) + do, err := session.GetDomain(h.Store) if err != nil { writeError(w, err) return } - s, err := session.CreateSession(h.Store.(kv.Storage)) + s, err := session.CreateSession(h.Store) if err != nil { writeError(w, err) return } + defer s.Close() + available := status.checkTableFlashReplicaAvailable() err = do.DDL().UpdateTableReplicaInfo(s, status.ID, available) if err != nil { @@ -1123,18 +1119,7 @@ func (h ddlHistoryJobHandler) ServeHTTP(w http.ResponseWriter, req *http.Request } func (h ddlHistoryJobHandler) getAllHistoryDDL() ([]*model.Job, error) { - s, err := session.CreateSession(h.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() - + txn, err := h.Store.Begin() if err != nil { return nil, errors.Trace(err) } @@ -1741,7 +1726,7 @@ type serverInfo struct { // ServeHTTP handles request of ddl server info. func (h serverInfoHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { - do, err := session.GetDomain(h.Store.(kv.Storage)) + do, err := session.GetDomain(h.Store) if err != nil { writeError(w, errors.New("create session error")) log.Error(err) @@ -1771,7 +1756,7 @@ type clusterServerInfo struct { // ServeHTTP handles request of all ddl servers info. func (h allServerInfoHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { - do, err := session.GetDomain(h.Store.(kv.Storage)) + do, err := session.GetDomain(h.Store) if err != nil { writeError(w, errors.New("create session error")) log.Error(err) @@ -1872,6 +1857,8 @@ func (h profileHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { writeError(w, err) return } + defer sctx.Close() + var start, end time.Time if req.FormValue("end") != "" { end, err = time.ParseInLocation(time.RFC3339, req.FormValue("end"), sctx.GetSessionVars().Location()) diff --git a/server/sql_info_fetcher.go b/server/sql_info_fetcher.go index a7be33ea00154..6fc80daf506d6 100644 --- a/server/sql_info_fetcher.go +++ b/server/sql_info_fetcher.go @@ -81,6 +81,7 @@ func (sh *sqlInfoFetcher) zipInfoForSQL(w http.ResponseWriter, r *http.Request) return } defer sh.s.Close() + sh.do = domain.GetDomain(sh.s) reqCtx := r.Context() sql := r.FormValue("sql") diff --git a/server/statistics_handler.go b/server/statistics_handler.go index 733a0559f4943..55e9e4f16df18 100644 --- a/server/statistics_handler.go +++ b/server/statistics_handler.go @@ -92,6 +92,8 @@ func (sh StatsHistoryHandler) ServeHTTP(w http.ResponseWriter, req *http.Request writeError(w, err) return } + defer se.Close() + se.GetSessionVars().StmtCtx.TimeZone = time.Local t, err := types.ParseTime(se.GetSessionVars().StmtCtx, params[pSnapshot], mysql.TypeTimestamp, 6) if err != nil {