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

server: close the temporary session in HTTP API to avoid memory leak (#24339) #24649

Merged
merged 2 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 20 additions & 33 deletions server/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,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"
Expand Down Expand Up @@ -370,11 +369,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) {
Expand Down Expand Up @@ -722,14 +721,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.BoolOff)
Expand All @@ -745,14 +743,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.BoolOff)
Expand Down Expand Up @@ -888,14 +885,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()
Expand Down Expand Up @@ -958,16 +952,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 {
Expand Down Expand Up @@ -1133,18 +1129,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)
}
Expand Down Expand Up @@ -1737,7 +1722,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)
Expand Down Expand Up @@ -1767,7 +1752,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)
Expand Down Expand Up @@ -1868,6 +1853,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())
Expand Down
1 change: 1 addition & 0 deletions server/sql_info_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions server/statistics_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down