Skip to content

Commit 58895c6

Browse files
committed
Merge remote-tracking branch 'origin/optimize-ci-cd-workflow' into optimize-ci-cd-workflow
2 parents df5fb33 + ca578cf commit 58895c6

File tree

2 files changed

+11
-35
lines changed

2 files changed

+11
-35
lines changed

internal/sql/repository/pipelineConfig/CdWorfkflowRepository.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package pipelineConfig
1919
import (
2020
"context"
2121
"errors"
22-
"fmt"
2322
apiBean "github.com/devtron-labs/devtron/api/bean"
2423
"github.com/devtron-labs/devtron/internal/sql/repository"
2524
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig/bean/workflow"
@@ -29,7 +28,6 @@ import (
2928
"github.com/go-pg/pg"
3029
"go.opentelemetry.io/otel"
3130
"go.uber.org/zap"
32-
"strings"
3331
"time"
3432
)
3533

@@ -71,7 +69,7 @@ type CdWorkflowRepository interface {
7169
IsLatestWf(pipelineId int, wfId int) (bool, error)
7270
FindLatestCdWorkflowByPipelineId(pipelineIds []int) (*CdWorkflow, error)
7371
FindLatestCdWorkflowByPipelineIdV2(pipelineIds []int) ([]*CdWorkflow, error)
74-
FetchAllCdStagesLatestEntity(pipelineWorkflowPairs map[int][]apiBean.WorkflowType) ([]*CdWorkflowStatus, error)
72+
FetchAllCdStagesLatestEntity(pipelineIds []int) ([]*CdWorkflowStatus, error)
7573
FetchAllCdStagesLatestEntityStatus(wfrIds []int) ([]*CdWorkflowRunner, error)
7674
ExistsByStatus(status string) (bool, error)
7775
FetchEnvAllCdStagesLatestEntityStatus(wfrIds []int, envID int) ([]*CdWorkflowRunner, error)
@@ -580,15 +578,15 @@ func (impl *CdWorkflowRepositoryImpl) IsLatestWf(pipelineId int, wfId int) (bool
580578
return !exists, err
581579
}
582580

583-
func (impl *CdWorkflowRepositoryImpl) FetchAllCdStagesLatestEntity(pipelineWorkflowPairs map[int][]apiBean.WorkflowType) ([]*CdWorkflowStatus, error) {
581+
func (impl *CdWorkflowRepositoryImpl) FetchAllCdStagesLatestEntity(pipelineIds []int) ([]*CdWorkflowStatus, error) {
584582
var cdWorkflowStatus []*CdWorkflowStatus
585-
if len(pipelineWorkflowPairs) == 0 {
583+
if len(pipelineIds) == 0 {
586584
return cdWorkflowStatus, nil
587585
}
588586
query := "select p.ci_pipeline_id, wf.pipeline_id, wfr.workflow_type, max(wfr.id) as wfr_id from cd_workflow_runner wfr" +
589587
" inner join cd_workflow wf on wf.id=wfr.cd_workflow_id" +
590588
" inner join pipeline p on p.id = wf.pipeline_id" +
591-
" where (wf.pipeline_id, wfr.workflow_type) in (" + buildPipelineTypeValuesList(pipelineWorkflowPairs) + ")" +
589+
" where wf.pipeline_id in (" + sqlIntSeq(pipelineIds) + ")" +
592590
" group by p.ci_pipeline_id, wf.pipeline_id, wfr.workflow_type order by wfr_id desc;"
593591
_, err := impl.dbConnection.Query(&cdWorkflowStatus, query)
594592
if err != nil {
@@ -598,15 +596,6 @@ func (impl *CdWorkflowRepositoryImpl) FetchAllCdStagesLatestEntity(pipelineWorkf
598596
return cdWorkflowStatus, nil
599597
}
600598

601-
func buildPipelineTypeValuesList(pairs map[int][]apiBean.WorkflowType) string {
602-
var values []string
603-
for pipelineId, workflowTypes := range pairs {
604-
for _, workflowType := range workflowTypes {
605-
values = append(values, fmt.Sprintf("(%d,'%s')", pipelineId, workflowType))
606-
}
607-
}
608-
return strings.Join(values, ",")
609-
}
610599
func (impl *CdWorkflowRepositoryImpl) FetchAllCdStagesLatestEntityStatus(wfrIds []int) ([]*CdWorkflowRunner, error) {
611600
var wfrList []*CdWorkflowRunner
612601
err := impl.dbConnection.Model(&wfrList).

pkg/workflow/cd/read/CdWorkflowRunnerReadService.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,8 @@ func (impl *CdWorkflowRunnerReadServiceImpl) GetWfrStatusForLatestRunners(pipeli
104104
}
105105

106106
// calculating all the pipelines not present in the index table cdWorkflowLatest
107-
pipelinesAbsentInCache := make(map[int][]bean2.WorkflowType)
107+
absentPipelineIds := make([]int, 0)
108108
for _, item := range pipelines {
109-
110109
var isPreCDConfigured, isPostCDConfigured bool
111110
if configuredStages, ok := pipelineStageMap[item.Id]; ok {
112111
for _, stage := range configuredStages {
@@ -119,13 +118,7 @@ func (impl *CdWorkflowRunnerReadServiceImpl) GetWfrStatusForLatestRunners(pipeli
119118
}
120119

121120
if _, ok := cdWorfklowLatestMap[item.Id]; !ok {
122-
pipelinesAbsentInCache[item.Id] = append(pipelinesAbsentInCache[item.Id], bean2.CD_WORKFLOW_TYPE_DEPLOY)
123-
if isPreCDConfigured {
124-
pipelinesAbsentInCache[item.Id] = append(pipelinesAbsentInCache[item.Id], bean2.CD_WORKFLOW_TYPE_PRE)
125-
}
126-
if isPostCDConfigured {
127-
pipelinesAbsentInCache[item.Id] = append(pipelinesAbsentInCache[item.Id], bean2.CD_WORKFLOW_TYPE_POST)
128-
}
121+
absentPipelineIds = append(absentPipelineIds, item.Id)
129122
} else {
130123
isPreCDStageAbsent, isPostCdStageAbsent, isDeployStageAbsent := true, true, true
131124
for _, stage := range cdWorfklowLatestMap[item.Id] {
@@ -138,21 +131,15 @@ func (impl *CdWorkflowRunnerReadServiceImpl) GetWfrStatusForLatestRunners(pipeli
138131
isDeployStageAbsent = false
139132
}
140133
}
141-
if isDeployStageAbsent {
142-
pipelinesAbsentInCache[item.Id] = append(pipelinesAbsentInCache[item.Id], bean2.CD_WORKFLOW_TYPE_DEPLOY)
143-
}
144-
if isPreCDConfigured && isPreCDStageAbsent {
145-
pipelinesAbsentInCache[item.Id] = append(pipelinesAbsentInCache[item.Id], bean2.CD_WORKFLOW_TYPE_PRE)
146-
}
147-
if isPostCDConfigured && isPostCdStageAbsent {
148-
pipelinesAbsentInCache[item.Id] = append(pipelinesAbsentInCache[item.Id], bean2.CD_WORKFLOW_TYPE_POST)
134+
if isDeployStageAbsent || (isPreCDConfigured && isPreCDStageAbsent) || (isPostCDConfigured && isPostCdStageAbsent) {
135+
absentPipelineIds = append(absentPipelineIds, item.Id)
149136
}
150137
}
151138
}
152-
if len(pipelinesAbsentInCache) > 0 {
153-
remainingRunners, err := impl.cdWorkflowRepository.FetchAllCdStagesLatestEntity(pipelinesAbsentInCache)
139+
if len(absentPipelineIds) > 0 {
140+
remainingRunners, err := impl.cdWorkflowRepository.FetchAllCdStagesLatestEntity(absentPipelineIds)
154141
if err != nil {
155-
impl.logger.Errorw("error in fetching all cd stages latest entity", "pipelinesAbsentInCache", pipelinesAbsentInCache, "err", err)
142+
impl.logger.Errorw("error in fetching all cd stages latest entity", "pipelineIds", absentPipelineIds, "err", err)
156143
return nil, err
157144
}
158145
result = append(result, remainingRunners...)

0 commit comments

Comments
 (0)