Skip to content

Commit

Permalink
server: close the temporary session in HTTP API to avoid memory leak (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Jul 8, 2021
1 parent 53afe14 commit 2016134
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 25 deletions.
39 changes: 14 additions & 25 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 @@ -332,11 +331,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 @@ -810,14 +809,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 @@ -880,16 +876,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 @@ -1055,18 +1053,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 @@ -1678,7 +1665,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 @@ -1708,7 +1695,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 @@ -1809,6 +1796,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

0 comments on commit 2016134

Please sign in to comment.