This is an automated email from the ASF dual-hosted git repository. klesh pushed a commit to branch kw-6329-reloadbp-mutex in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git
commit 24e868802f73737571303eae6670b7dd7dfb45e2 Author: Klesh Wong <[email protected]> AuthorDate: Wed Oct 25 17:28:13 2023 +0800 fix: ReloadBlueprints is not safe --- backend/helpers/dbhelper/txhelper.go | 5 +++-- backend/server/services/blueprint.go | 10 +++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/backend/helpers/dbhelper/txhelper.go b/backend/helpers/dbhelper/txhelper.go index a7da084d4..c9101f4ec 100644 --- a/backend/helpers/dbhelper/txhelper.go +++ b/backend/helpers/dbhelper/txhelper.go @@ -19,9 +19,10 @@ package dbhelper import ( "fmt" - "github.com/apache/incubator-devlake/core/log" "time" + "github.com/apache/incubator-devlake/core/log" + "github.com/apache/incubator-devlake/core/context" "github.com/apache/incubator-devlake/core/dal" "github.com/apache/incubator-devlake/core/errors" @@ -46,7 +47,7 @@ func (l *TxHelper[E]) Begin() dal.Transaction { // LockTablesTimeout locks tables with timeout func (l *TxHelper[E]) LockTablesTimeout(timeout time.Duration, lockTables dal.LockTables) errors.Error { - l.logger.Warn(fmt.Errorf("timeout"), "local tables") + l.logger.Info("try locking tables with timeout %v", timeout) c := make(chan errors.Error, 1) go func() { c <- l.tx.LockTables(lockTables) diff --git a/backend/server/services/blueprint.go b/backend/server/services/blueprint.go index e295e694b..34eef95d9 100644 --- a/backend/server/services/blueprint.go +++ b/backend/server/services/blueprint.go @@ -21,6 +21,7 @@ import ( "encoding/json" "fmt" "strings" + "sync" "github.com/apache/incubator-devlake/helpers/pluginhelper/services" @@ -235,8 +236,15 @@ func DeleteBlueprint(id uint64) errors.Error { return nil } +var blueprintReloadLock sync.Mutex + // ReloadBlueprints FIXME ... -func ReloadBlueprints(c *cron.Cron) errors.Error { +func ReloadBlueprints(c *cron.Cron) (err errors.Error) { + // preventing concurrent reloads. It would be better to use Table Lock , however, it requires massive refactor + // like the `bpManager` must accept transaction. Use mutex as a temporary fix. + blueprintReloadLock.Lock() + defer blueprintReloadLock.Unlock() + enable := true isManual := false blueprints, _, err := bpManager.GetDbBlueprints(&services.GetBlueprintQuery{
