From f66df1d957936a9c0f2385e29c32d66bbfebc965 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Sun, 8 May 2022 00:24:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BF=BD=E7=95=A5=E9=83=A8=E5=88=86MySQL=20121?= =?UTF-8?q?3=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/db/models/metric_stat_dao.go | 22 +++++++++++++++++++--- internal/db/models/metric_sum_stat_dao.go | 22 +++++++++++++++++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/internal/db/models/metric_stat_dao.go b/internal/db/models/metric_stat_dao.go index 4f86fecb..dee8b50a 100644 --- a/internal/db/models/metric_stat_dao.go +++ b/internal/db/models/metric_stat_dao.go @@ -90,9 +90,7 @@ func (this *MetricStatDAO) CreateStat(tx *dbs.Tx, hash string, clusterId int64, "value": value, }) if err != nil { - // 忽略 Error 1213: Deadlock found 错误 - mysqlErr, ok := err.(*mysql.MySQLError) - if ok && mysqlErr.Number == 1213 { + if this.canIgnore(err) { return nil } return err @@ -135,6 +133,9 @@ func (this *MetricStatDAO) DeleteNodeItemStats(tx *dbs.Tx, nodeId int64, serverI Attr("itemId", itemId). Attr("time", time). Delete() + if this.canIgnore(err) { + return nil + } return err } @@ -751,3 +752,18 @@ func (this *MetricStatDAO) mergeStats(stats []*MetricStat) (result []*MetricStat } return result } + +// 检查错误是否可以忽略 +func (this *MetricStatDAO) canIgnore(err error) bool { + if err == nil { + return true + } + + // 忽略 Error 1213: Deadlock found 错误 + mysqlErr, ok := err.(*mysql.MySQLError) + if ok && mysqlErr.Number == 1213 { + return true + } + + return false +} diff --git a/internal/db/models/metric_sum_stat_dao.go b/internal/db/models/metric_sum_stat_dao.go index befe7801..e47c0080 100644 --- a/internal/db/models/metric_sum_stat_dao.go +++ b/internal/db/models/metric_sum_stat_dao.go @@ -4,6 +4,7 @@ import ( "github.com/TeaOSLab/EdgeAPI/internal/goman" "github.com/TeaOSLab/EdgeAPI/internal/remotelogs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" + "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql" "github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/dbs" @@ -55,7 +56,7 @@ func init() { // UpdateSum 更新统计数据 func (this *MetricSumStatDAO) UpdateSum(tx *dbs.Tx, clusterId int64, nodeId int64, serverId int64, time string, itemId int64, version int32, count int64, total float32) error { - return this.Query(tx). + err := this.Query(tx). Table(this.partialTable(serverId)). InsertOrUpdateQuickly(maps.Map{ "clusterId": clusterId, @@ -71,6 +72,10 @@ func (this *MetricSumStatDAO) UpdateSum(tx *dbs.Tx, clusterId int64, nodeId int6 "count": count, "total": total, }) + if this.canIgnore(err) { + return nil + } + return err } // FindNodeServerSum 查找某个服务在某个节点上的统计数据 @@ -277,3 +282,18 @@ func (this *MetricSumStatDAO) runBatch(f func(table string, locker *sync.Mutex) wg.Wait() return resultErr } + +// 检查错误是否可以忽略 +func (this *MetricSumStatDAO) canIgnore(err error) bool { + if err == nil { + return true + } + + // 忽略 Error 1213: Deadlock found 错误 + mysqlErr, ok := err.(*mysql.MySQLError) + if ok && mysqlErr.Number == 1213 { + return true + } + + return false +}