From d031bf759eaabaa680c8f865b6def7831973ac24 Mon Sep 17 00:00:00 2001 From: taolx0 Date: Mon, 1 Apr 2024 11:23:13 +0800 Subject: [PATCH] chore:rule execution time --- sqle/driver/mysql/mysql.go | 14 +++++++++++++- sqle/log/logger.go | 7 +++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/sqle/driver/mysql/mysql.go b/sqle/driver/mysql/mysql.go index 65c315052f..af84eed042 100644 --- a/sqle/driver/mysql/mysql.go +++ b/sqle/driver/mysql/mysql.go @@ -6,6 +6,7 @@ import ( _driver "database/sql/driver" "fmt" "strings" + "time" "github.com/actiontech/sqle/sqle/driver" "github.com/actiontech/sqle/sqle/driver/mysql/executor" @@ -349,7 +350,7 @@ func (i *MysqlDriverImpl) audit(ctx context.Context, sql string) (*driverV2.Audi Res: i.result, Node: nodes[0], } - + startTime := time.Now() if err := handler.Func(input); err != nil { // todo #1630 临时跳过解析建表语句失败导致的规则 if session.IsParseShowCreateTableContentErr(err) { @@ -358,9 +359,13 @@ func (i *MysqlDriverImpl) audit(ctx context.Context, sql string) (*driverV2.Audi } return nil, err } + if isExceedMaximum(startTime) { + i.Logger().Warnf("[audit_rule]rule: %v,total time: %v", rule.Desc, time.Since(startTime)) + } } if i.cnf.optimizeIndexEnabled && index.CanOptimize(i.log, i.Ctx, nodes[0]) { + startTime := time.Now() optimizer := index.NewOptimizer( i.log, i.Ctx, index.WithCalculateCardinalityMaxRow(i.cnf.calculateCardinalityMaxRow), @@ -378,6 +383,9 @@ func (i *MysqlDriverImpl) audit(ctx context.Context, sql string) (*driverV2.Audi buf.WriteString(fmt.Sprintf("建议从表 %s 的以下列中 [%s] 选取合适的列添加索引", advice.TableName, strings.Join(advice.IndexedColumns, ","))) } i.result.Add(driverV2.RuleLevelNotice, rulepkg.ConfigOptimizeIndexEnabled, buf.String()) + if isExceedMaximum(startTime) { + i.Logger().Warnf("[audit_rule]rule: %v,total time: %v", rulepkg.ConfigOptimizeIndexEnabled, time.Since(startTime)) + } } // dry run gh-ost @@ -411,6 +419,10 @@ func (i *MysqlDriverImpl) audit(ctx context.Context, sql string) (*driverV2.Audi return i.result, nil } +func isExceedMaximum(startTime time.Time) bool { + return time.Since(startTime).Seconds() > log.MaxRuleExecutionSeconds +} + func (i *MysqlDriverImpl) GenRollbackSQL(ctx context.Context, sql string) (string, string, error) { if i.IsOfflineAudit() { return "", "", nil diff --git a/sqle/log/logger.go b/sqle/log/logger.go index 7e0962041a..b87b1d53ad 100644 --- a/sqle/log/logger.go +++ b/sqle/log/logger.go @@ -2,16 +2,19 @@ package log import ( "fmt" - "github.com/sirupsen/logrus" - rotate "gopkg.in/natefinch/lumberjack.v2" "io" "math/rand" "os" "strings" + + "github.com/sirupsen/logrus" + rotate "gopkg.in/natefinch/lumberjack.v2" ) var std *logrus.Logger +const MaxRuleExecutionSeconds = 5 + func Logger() *logrus.Logger { return std }