You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by GitBox <gi...@apache.org> on 2022/11/21 11:32:23 UTC

[GitHub] [incubator-devlake] mappjzc commented on a diff in pull request #3764: Issues/3234 add parallelLabels for blueprint/pipeline

mappjzc commented on code in PR #3764:
URL: https://github.com/apache/incubator-devlake/pull/3764#discussion_r1027761590


##########
models/migrationscripts/20221115_add_parallel_labels.go:
##########
@@ -0,0 +1,69 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package migrationscripts
+
+import (
+	"github.com/apache/incubator-devlake/errors"
+	"github.com/apache/incubator-devlake/plugins/core"
+	"time"
+)
+
+type DbPipelineLabel20221115 struct {
+	CreatedAt  time.Time `json:"createdAt"`
+	UpdatedAt  time.Time `json:"updatedAt"`
+	PipelineId uint64    `json:"pipeline_id" gorm:"primaryKey"`
+	Name       string    `json:"name" gorm:"primaryKey"`
+}
+
+func (DbPipelineLabel20221115) TableName() string {
+	return "_devlake_pipeline_labels"
+}
+
+type DbBlueprintLabel20221115 struct {
+	CreatedAt   time.Time `json:"createdAt"`
+	UpdatedAt   time.Time `json:"updatedAt"`
+	BlueprintId uint64    `json:"blueprint_id" gorm:"primaryKey"`
+	Name        string    `json:"name" gorm:"primaryKey"`
+}
+
+func (DbBlueprintLabel20221115) TableName() string {
+	return "_devlake_blueprint_labels"
+}
+
+type addLabels struct{}
+
+func (*addLabels) Up(res core.BasicRes) errors.Error {
+	db := res.GetDal()
+	err := db.AutoMigrate(&DbPipelineLabel20221115{})
+	if err != nil {
+		return err
+	}
+	err = db.AutoMigrate(&DbBlueprintLabel20221115{})

Review Comment:
   why don't use migrationhelper.AutoMigrateTables?



##########
services/pipeline.go:
##########
@@ -165,29 +167,57 @@ func RunPipelineInQueue(pipelineMaxParallel int64) {
 		}
 		globalPipelineLog.Info("get lock and wait next pipeline")
 		dbPipeline := &models.DbPipeline{}
+		var dbLabels []models.DbPipelineLabel
 		for {
 			cronLocker.Lock()
+			// prepare query to find an appropriate pipeline to execute
 			db.Where("status IN ?", []string{models.TASK_CREATED, models.TASK_RERUN}).
+				Joins(`left join _devlake_pipeline_labels ON
+                  _devlake_pipeline_labels.pipeline_id = _devlake_pipelines.id AND
+                  _devlake_pipeline_labels.name LIKE 'parallel/%' AND
+                  _devlake_pipeline_labels.name in ?`, runningParallelLabels).
+				Group(`id`).
+				Having(`count(_devlake_pipeline_labels.name)=0`).
+				Select("id").
 				Order("id ASC").Limit(1).Find(dbPipeline)
 			cronLocker.Unlock()
 			if dbPipeline.ID != 0 {
-				db.Model(&models.DbPipeline{}).Where("id = ?", dbPipeline.ID).Updates(map[string]interface{}{
-					"status":   models.TASK_RUNNING,
-					"message":  "",
-					"began_at": time.Now(),
-				})
 				break
 			}
 			time.Sleep(time.Second)
 		}
-		go func(pipelineId uint64) {
+
+		db.Model(&models.DbPipeline{}).Where("id = ?", dbPipeline.ID).Updates(map[string]interface{}{
+			"status":   models.TASK_RUNNING,
+			"message":  "",
+			"began_at": time.Now(),
+		})
+		dbPipeline, dbLabels, err = GetDbPipeline(dbPipeline.ID)
+		if err != nil {
+			panic(err)
+		}
+
+		// add pipelineParallelLabels to runningParallelLabels
+		var pipelineParallelLabels []string
+		for _, dbLabel := range dbLabels {
+			if strings.HasPrefix(dbLabel.Name, `parallel/`) {
+				pipelineParallelLabels = append(pipelineParallelLabels, dbLabel.Name)
+			}
+		}
+		runningParallelLabels = append(runningParallelLabels, pipelineParallelLabels...)
+
+		go func(pipelineId uint64, parallelLabels []string) {
 			defer sema.Release(1)
-			globalPipelineLog.Info("run pipeline, %d", pipelineId)
+			defer func() {
+				runningParallelLabels = utils.SliceRemove(runningParallelLabels, parallelLabels...)

Review Comment:
   The append and SliceRemove operations on runningParallelLabels are not thread safe.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@devlake.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org