Skip to content

Commit

Permalink
Merge pull request #2432 from actiontech/cherry-pick-issue2326
Browse files Browse the repository at this point in the history
chore:rule execution time
  • Loading branch information
hasa1K authored May 24, 2024
2 parents 0fc7558 + d031bf7 commit cb5194e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
14 changes: 13 additions & 1 deletion sqle/driver/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand All @@ -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),
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions sqle/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit cb5194e

Please sign in to comment.