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

util: record sqls and heap profile when memory usage is higher than 80% system memory. (#18858) #20473

Merged
merged 8 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
70 changes: 39 additions & 31 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,22 +371,24 @@ type Status struct {

// Performance is the performance section of the config.
type Performance struct {
MaxProcs uint `toml:"max-procs" json:"max-procs"`
MaxMemory uint64 `toml:"max-memory" json:"max-memory"`
StatsLease string `toml:"stats-lease" json:"stats-lease"`
StmtCountLimit uint `toml:"stmt-count-limit" json:"stmt-count-limit"`
FeedbackProbability float64 `toml:"feedback-probability" json:"feedback-probability"`
QueryFeedbackLimit uint `toml:"query-feedback-limit" json:"query-feedback-limit"`
PseudoEstimateRatio float64 `toml:"pseudo-estimate-ratio" json:"pseudo-estimate-ratio"`
ForcePriority string `toml:"force-priority" json:"force-priority"`
BindInfoLease string `toml:"bind-info-lease" json:"bind-info-lease"`
TxnTotalSizeLimit uint64 `toml:"txn-total-size-limit" json:"txn-total-size-limit"`
TCPKeepAlive bool `toml:"tcp-keep-alive" json:"tcp-keep-alive"`
CrossJoin bool `toml:"cross-join" json:"cross-join"`
RunAutoAnalyze bool `toml:"run-auto-analyze" json:"run-auto-analyze"`
DistinctAggPushDown bool `toml:"distinct-agg-push-down" json:"agg-push-down-join"`
CommitterConcurrency int `toml:"committer-concurrency" json:"committer-concurrency"`
MaxTxnTTL uint64 `toml:"max-txn-ttl" json:"max-txn-ttl"`
MaxProcs uint `toml:"max-procs" json:"max-procs"`
MaxMemory uint64 `toml:"max-memory" json:"max-memory"`
ServerMemoryQuota uint64 `toml:"server-memory-quota" json:"server-memory-quota"`
MemoryUsageAlarmRatio float64 `toml:"memory-usage-alarm-ratio" json:"memory-usage-alarm-ratio"`
StatsLease string `toml:"stats-lease" json:"stats-lease"`
StmtCountLimit uint `toml:"stmt-count-limit" json:"stmt-count-limit"`
FeedbackProbability float64 `toml:"feedback-probability" json:"feedback-probability"`
QueryFeedbackLimit uint `toml:"query-feedback-limit" json:"query-feedback-limit"`
PseudoEstimateRatio float64 `toml:"pseudo-estimate-ratio" json:"pseudo-estimate-ratio"`
ForcePriority string `toml:"force-priority" json:"force-priority"`
BindInfoLease string `toml:"bind-info-lease" json:"bind-info-lease"`
TxnTotalSizeLimit uint64 `toml:"txn-total-size-limit" json:"txn-total-size-limit"`
TCPKeepAlive bool `toml:"tcp-keep-alive" json:"tcp-keep-alive"`
CrossJoin bool `toml:"cross-join" json:"cross-join"`
RunAutoAnalyze bool `toml:"run-auto-analyze" json:"run-auto-analyze"`
DistinctAggPushDown bool `toml:"distinct-agg-push-down" json:"agg-push-down-join"`
CommitterConcurrency int `toml:"committer-concurrency" json:"committer-concurrency"`
MaxTxnTTL uint64 `toml:"max-txn-ttl" json:"max-txn-ttl"`
}

// PlanCache is the PlanCache section of the config.
Expand Down Expand Up @@ -608,21 +610,23 @@ var defaultConf = Config{
RecordQPSbyDB: false,
},
Performance: Performance{
MaxMemory: 0,
TCPKeepAlive: true,
CrossJoin: true,
StatsLease: "3s",
RunAutoAnalyze: true,
StmtCountLimit: 5000,
FeedbackProbability: 0.0,
QueryFeedbackLimit: 512,
PseudoEstimateRatio: 0.8,
ForcePriority: "NO_PRIORITY",
BindInfoLease: "3s",
TxnTotalSizeLimit: DefTxnTotalSizeLimit,
DistinctAggPushDown: false,
CommitterConcurrency: 16,
MaxTxnTTL: 10 * 60 * 1000, // 10min
MaxMemory: 0,
ServerMemoryQuota: 0,
MemoryUsageAlarmRatio: 0.8,
TCPKeepAlive: true,
CrossJoin: true,
StatsLease: "3s",
RunAutoAnalyze: true,
StmtCountLimit: 5000,
FeedbackProbability: 0.0,
QueryFeedbackLimit: 512,
PseudoEstimateRatio: 0.8,
ForcePriority: "NO_PRIORITY",
BindInfoLease: "3s",
TxnTotalSizeLimit: DefTxnTotalSizeLimit,
DistinctAggPushDown: false,
CommitterConcurrency: 16,
MaxTxnTTL: 10 * 60 * 1000, // 10min
},
ProxyProtocol: ProxyProtocol{
Networks: "",
Expand Down Expand Up @@ -862,6 +866,10 @@ func (c *Config) Valid() error {
return fmt.Errorf("txn-total-size-limit should be less than %d", 10<<30)
}

if c.Performance.MemoryUsageAlarmRatio > 1 || c.Performance.MemoryUsageAlarmRatio < 0 {
return fmt.Errorf("memory-usage-alarm-ratio in [Performance] must be greater than or equal to 0 and less than or equal to 1")
}

if c.StmtSummary.MaxStmtCount <= 0 {
return fmt.Errorf("max-stmt-count in [stmt-summary] should be greater than 0")
}
Expand Down
11 changes: 11 additions & 0 deletions config/config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,17 @@ max-procs = 0
# Max memory size to use, 0 use the total usable memory in the machine.
max-memory = 0

# Memory size quota for tidb server, 0 means unlimited
server-memory-quota = 0

# The alarm threshold when memory usage of the tidb-server exceeds. The valid value range is greater than or equal to 0
# and less than or equal to 1. The default value is 0.8.
# If this configuration is set to 0 or 1, it'll disable the alarm.
# Otherwise, related information will be recorded in the directory `tmp-storage-path/record`.
# Note: If the configuration `server-memory-quota` is set and larger than 0, the alarm threshold will be
# `memory-usage-alarm-ratio * server-memory-quota`; otherwise, it'll be `memory-usage-alarm-ratio * system memory size`.
memory-usage-alarm-ratio = 0.8

# StmtCountLimit limits the max count of statement inside a transaction.
stmt-count-limit = 5000

Expand Down
20 changes: 17 additions & 3 deletions util/disk/tempDir.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ var (
sf singleflight.Group
)

const (
lockFile = "_dir.lock"
recordDir = "record"
)

// CheckAndInitTempDir check whether the temp directory is existed.
// If not, initializes the temp directory.
func CheckAndInitTempDir() (err error) {
Expand Down Expand Up @@ -64,7 +69,6 @@ func InitializeTempDir() error {
return err
}
}
lockFile := "_dir.lock"
tempDirLock, err = fslock.Lock(filepath.Join(tempDir, lockFile))
if err != nil {
switch err {
Expand All @@ -77,17 +81,27 @@ func InitializeTempDir() error {
return err
}

// Create dir for MemoryUsageAlarmRecord.
_, err = os.Stat(filepath.Join(tempDir, "record"))
if err != nil && !os.IsExist(err) {
err = os.MkdirAll(filepath.Join(tempDir, "record"), 0755)
if err != nil {
return err
}
}

subDirs, err := ioutil.ReadDir(tempDir)
if err != nil {
return err
}

// If it exists others files except lock file, creates another goroutine to clean them.
if len(subDirs) > 1 {
if len(subDirs) > 2 {
go func() {
for _, subDir := range subDirs {
// Do not remove the lock file.
if subDir.Name() == lockFile {
switch subDir.Name() {
case lockFile, recordDir:
continue
}
err := os.RemoveAll(filepath.Join(tempDir, subDir.Name()))
Expand Down
4 changes: 4 additions & 0 deletions util/expensivequery/expensivequery.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (eqh *Handle) Run() {
ticker := time.NewTicker(tickInterval)
defer ticker.Stop()
sm := eqh.sm.Load().(util.SessionManager)
record := initMemoryUsageAlarmRecord()
for {
select {
case <-ticker.C:
Expand All @@ -73,6 +74,9 @@ func (eqh *Handle) Run() {
}
}
threshold = atomic.LoadUint64(&variable.ExpensiveQueryTimeThreshold)
if record.err == nil {
record.alarm4ExcessiveMemUsage(sm)
}
case <-eqh.exitCh:
return
}
Expand Down
Loading