Skip to content

Commit b4967bd

Browse files
authored
refactor: delete ratio service option (#6099)
1 parent bed92ee commit b4967bd

File tree

11 files changed

+51
-273
lines changed

11 files changed

+51
-273
lines changed

docker-compose.yml

-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ services:
9393
/app/cds-engine-linux-amd64 config edit /app/conf/conf.toml --output /app/conf/conf.toml hatchery.swarm.commonConfiguration.provision.workerApiHttp.url=http://$HOSTNAME:8081;
9494
/app/cds-engine-linux-amd64 config edit /app/conf/conf.toml --output /app/conf/conf.toml hatchery.swarm.dockerEngines.sample-docker-engine.host=tcp://dockerhost:2375;
9595
/app/cds-engine-linux-amd64 config edit /app/conf/conf.toml --output /app/conf/conf.toml hatchery.swarm.dockerEngines.sample-docker-engine.maxContainers=4;
96-
/app/cds-engine-linux-amd64 config edit /app/conf/conf.toml --output /app/conf/conf.toml hatchery.swarm.ratioService=50;
9796
/app/cds-engine-linux-amd64 config edit /app/conf/conf.toml --output /app/conf/conf.toml hooks.api.http.url=http://cds-api:8081;
9897
/app/cds-engine-linux-amd64 config edit /app/conf/conf.toml --output /app/conf/conf.toml hooks.cache.redis.host=cds-cache:6379;
9998
/app/cds-engine-linux-amd64 config edit /app/conf/conf.toml --output /app/conf/conf.toml hooks.cache.redis.password=cds;

engine/api/workflow/dao_node_run_job.go

+32-56
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ import (
1919

2020
// QueueFilter contains all criteria used to fetch queue
2121
type QueueFilter struct {
22-
ModelType []string
23-
RatioService *int
24-
Rights int
25-
Since *time.Time
26-
Until *time.Time
27-
Limit *int
28-
Statuses []string
29-
Regions []string
22+
ModelType []string
23+
Rights int
24+
Since *time.Time
25+
Until *time.Time
26+
Limit *int
27+
Statuses []string
28+
Regions []string
3029
}
3130

3231
func NewQueueFilter() QueueFilter {
@@ -79,38 +78,27 @@ func CountNodeJobRunQueueByGroupIDs(ctx context.Context, db gorp.SqlExecutor, st
7978
func LoadNodeJobRunQueue(ctx context.Context, db gorp.SqlExecutor, store cache.Store, filter QueueFilter) ([]sdk.WorkflowNodeJobRun, error) {
8079
ctx, end := telemetry.Span(ctx, "workflow.LoadNodeJobRunQueue")
8180
defer end()
82-
containsService := []bool{true, false}
83-
if filter.RatioService != nil {
84-
if *filter.RatioService == 100 {
85-
containsService = []bool{true, true}
86-
} else if *filter.RatioService == 0 {
87-
containsService = []bool{false, false}
88-
}
89-
}
9081

9182
query := gorpmapping.NewQuery(`select distinct workflow_node_run_job.*
9283
from workflow_node_run_job
9384
where workflow_node_run_job.queued >= $1
9485
and workflow_node_run_job.queued <= $2
9586
and workflow_node_run_job.status = ANY($3)
96-
AND contains_service IN ($4, $5)
97-
AND (model_type is NULL OR model_type = '' OR model_type = ANY($6))
87+
AND (model_type is NULL OR model_type = '' OR model_type = ANY($4))
9888
AND (
99-
workflow_node_run_job.region = ANY($7)
89+
workflow_node_run_job.region = ANY($5)
10090
OR
101-
(workflow_node_run_job.region is NULL AND '' = ANY($7))
91+
(workflow_node_run_job.region is NULL AND '' = ANY($5))
10292
OR
103-
array_length($7, 1) is NULL
93+
array_length($5, 1) is NULL
10494
)
10595
ORDER BY workflow_node_run_job.queued ASC
10696
`).Args(
10797
*filter.Since, // $1
10898
*filter.Until, // $2
10999
pq.StringArray(filter.Statuses), // $3
110-
containsService[0], // $4
111-
containsService[1], // $5
112-
pq.StringArray(filter.ModelType), // $6
113-
pq.StringArray(filter.Regions), // $7
100+
pq.StringArray(filter.ModelType), // $4
101+
pq.StringArray(filter.Regions), // $5
114102
)
115103

116104
return loadNodeJobRunQueue(ctx, db, store, query, filter.Limit)
@@ -120,45 +108,36 @@ func LoadNodeJobRunQueue(ctx context.Context, db gorp.SqlExecutor, store cache.S
120108
func LoadNodeJobRunQueueByGroupIDs(ctx context.Context, db gorp.SqlExecutor, store cache.Store, filter QueueFilter, groupIDs []int64) ([]sdk.WorkflowNodeJobRun, error) {
121109
ctx, end := telemetry.Span(ctx, "workflow.LoadNodeJobRunQueueByGroups")
122110
defer end()
123-
containsService := []bool{true, false}
124-
if filter.RatioService != nil {
125-
if *filter.RatioService == 100 {
126-
containsService = []bool{true, true}
127-
} else if *filter.RatioService == 0 {
128-
containsService = []bool{false, false}
129-
}
130-
}
131111

132112
query := gorpmapping.NewQuery(`
133113
-- Parameters:
134114
-- $1: Queue since
135115
-- $2: Queue until
136116
-- $3: List of status
137-
-- $4, $5: Should (or should not) contains service, or we don't care
138-
-- $6: List of model types
139-
-- $7: Comman separated list of groups ID
140-
-- $8: shared infra group ID
141-
-- $9: minimum level of permission
142-
-- $10: List of regions
117+
-- $4: List of model types
118+
-- $5: Comman separated list of groups ID
119+
-- $6: shared infra group ID
120+
-- $7: minimum level of permission
121+
-- $8: List of regions
143122
WITH workflow_id_with_permissions AS (
144123
SELECT workflow_perm.workflow_id,
145-
CASE WHEN $8 = ANY(string_to_array($7, ',')::int[]) THEN 7
124+
CASE WHEN $6 = ANY(string_to_array($5, ',')::int[]) THEN 7
146125
ELSE max(workflow_perm.role)
147126
END as "role"
148127
FROM workflow_perm
149128
JOIN project_group ON project_group.id = workflow_perm.project_group_id
150129
WHERE
151-
project_group.group_id = ANY(string_to_array($7, ',')::int[])
130+
project_group.group_id = ANY(string_to_array($5, ',')::int[])
152131
OR
153-
$8 = ANY(string_to_array($7, ',')::int[])
132+
$6 = ANY(string_to_array($5, ',')::int[])
154133
GROUP BY workflow_perm.workflow_id
155134
), workflow_node_run_job_exec_groups AS (
156135
SELECT id, jsonb_array_elements_text(exec_groups)::jsonb->'id' AS exec_group_id
157136
FROM workflow_node_run_job
158137
), workflow_node_run_job_matching_exec_groups AS (
159138
SELECT id
160139
FROM workflow_node_run_job_exec_groups
161-
WHERE exec_group_id::text = ANY(string_to_array($7, ','))
140+
WHERE exec_group_id::text = ANY(string_to_array($5, ','))
162141
)
163142
SELECT DISTINCT workflow_node_run_job.*
164143
FROM workflow_node_run_job
@@ -168,7 +147,7 @@ func LoadNodeJobRunQueueByGroupIDs(ctx context.Context, db gorp.SqlExecutor, sto
168147
WHERE workflow.id IN (
169148
SELECT workflow_id
170149
FROM workflow_id_with_permissions
171-
WHERE role >= $9
150+
WHERE role >= $7
172151
)
173152
AND workflow_node_run_job.id IN (
174153
SELECT id
@@ -177,31 +156,28 @@ func LoadNodeJobRunQueueByGroupIDs(ctx context.Context, db gorp.SqlExecutor, sto
177156
AND workflow_node_run_job.queued >= $1
178157
AND workflow_node_run_job.queued <= $2
179158
AND workflow_node_run_job.status = ANY($3)
180-
AND workflow_node_run_job.contains_service IN ($4, $5)
181159
AND (
182160
workflow_node_run_job.model_type is NULL
183161
OR
184-
model_type = '' OR model_type = ANY($6)
162+
model_type = '' OR model_type = ANY($4)
185163
)
186164
AND (
187-
workflow_node_run_job.region = ANY($10)
165+
workflow_node_run_job.region = ANY($8)
188166
OR
189-
(workflow_node_run_job.region is NULL AND '' = ANY($10))
167+
(workflow_node_run_job.region is NULL AND '' = ANY($8))
190168
OR
191-
array_length($10, 1) is NULL
169+
array_length($8, 1) is NULL
192170
)
193171
ORDER BY workflow_node_run_job.queued ASC
194172
`).Args(
195173
*filter.Since, // $1
196174
*filter.Until, // $2
197175
pq.StringArray(filter.Statuses), // $3
198-
containsService[0], // $4
199-
containsService[1], // $5
200-
pq.StringArray(filter.ModelType), // $6
201-
gorpmapping.IDsToQueryString(groupIDs), // $7
202-
group.SharedInfraGroup.ID, // $8
203-
filter.Rights, // $9
204-
pq.StringArray(filter.Regions), // $10
176+
pq.StringArray(filter.ModelType), // $4
177+
gorpmapping.IDsToQueryString(groupIDs), // $5
178+
group.SharedInfraGroup.ID, // $6
179+
filter.Rights, // $7
180+
pq.StringArray(filter.Regions), // $8
205181
)
206182
return loadNodeJobRunQueue(ctx, db, store, query, filter.Limit)
207183
}

engine/api/workflow/run_workflow_test.go

-30
Original file line numberDiff line numberDiff line change
@@ -732,36 +732,6 @@ queueRun:
732732
}
733733
}
734734

735-
// there is one job with a CDS Service prerequisiste
736-
// Getting queue with RatioService=100 -> we want this job only.
737-
// If we get a job without a service, it's a failure
738-
cent := 100
739-
filter = workflow.NewQueueFilter()
740-
filter.Rights = sdk.PermissionReadExecute
741-
filter.RatioService = &cent
742-
jobsSince, err = workflow.LoadNodeJobRunQueueByGroupIDs(ctx, db, cache, filter, sdk.Groups(append(u.Groups, proj.ProjectGroups[0].Group, g0, g1)).ToIDs())
743-
require.NoError(t, err)
744-
for _, job := range jobsSince {
745-
if !job.ContainsService {
746-
assert.Fail(t, " this job should not be in queue !job.ContainsService: job")
747-
}
748-
}
749-
750-
// there is one job with a CDS Service prerequisiste
751-
// Getting queue with RatioService=0 -> we want job only without CDS Service.
752-
// If we get a job with a service, it's a failure
753-
zero := 0
754-
filter = workflow.NewQueueFilter()
755-
filter.Rights = sdk.PermissionReadExecute
756-
filter.RatioService = &zero
757-
jobsSince, err = workflow.LoadNodeJobRunQueueByGroupIDs(ctx, db, cache, filter, sdk.Groups(append(u.Groups, proj.ProjectGroups[0].Group, g0, g1)).ToIDs())
758-
require.NoError(t, err)
759-
for _, job := range jobsSince {
760-
if job.ContainsService {
761-
assert.Fail(t, " this job should not be in queue job.ContainsService")
762-
}
763-
}
764-
765735
// there is one job with a CDS Model prerequisiste
766736
// we get the queue with a modelType openstack : we don't want
767737
// job with worker model type docker in result

engine/api/workflow_queue.go

+5-16
Original file line numberDiff line numberDiff line change
@@ -811,14 +811,13 @@ func (api *API) countWorkflowJobQueueHandler() service.Handler {
811811
}
812812

813813
since, until, _ := getSinceUntilLimitHeader(ctx, w, r)
814-
modelType, ratioService, err := getModelTypeRatioService(ctx, r)
814+
modelType, err := getModelType(ctx, r)
815815
if err != nil {
816816
return err
817817
}
818818

819819
filter := workflow.NewQueueFilter()
820820
filter.ModelType = []string{modelType}
821-
filter.RatioService = ratioService
822821
filter.Since = &since
823822
filter.Until = &until
824823

@@ -849,7 +848,7 @@ func (api *API) getWorkflowJobQueueHandler() service.Handler {
849848
status = []string{sdk.StatusWaiting}
850849
}
851850

852-
modelType, ratioService, err := getModelTypeRatioService(ctx, r)
851+
modelType, err := getModelType(ctx, r)
853852
if err != nil {
854853
return err
855854
}
@@ -880,7 +879,6 @@ func (api *API) getWorkflowJobQueueHandler() service.Handler {
880879
permissions = sdk.PermissionReadExecute
881880
}
882881
filter := workflow.NewQueueFilter()
883-
filter.RatioService = ratioService
884882
filter.Since = &since
885883
filter.Until = &until
886884
filter.Rights = permissions
@@ -905,23 +903,14 @@ func (api *API) getWorkflowJobQueueHandler() service.Handler {
905903
}
906904
}
907905

908-
func getModelTypeRatioService(ctx context.Context, r *http.Request) (string, *int, error) {
906+
func getModelType(ctx context.Context, r *http.Request) (string, error) {
909907
modelType := FormString(r, "modelType")
910908
if modelType != "" {
911909
if !sdk.WorkerModelValidate(modelType) {
912-
return "", nil, sdk.NewErrorFrom(sdk.ErrWrongRequest, "invalid given modelType")
910+
return "", sdk.NewErrorFrom(sdk.ErrWrongRequest, "invalid given modelType")
913911
}
914912
}
915-
ratioService := FormString(r, "ratioService")
916-
var ratio *int
917-
if ratioService != "" {
918-
i, err := strconv.Atoi(ratioService)
919-
if err != nil {
920-
return "", nil, sdk.NewErrorFrom(sdk.ErrInvalidNumber, " %s is not a integer", ratioService)
921-
}
922-
ratio = &i
923-
}
924-
return modelType, ratio, nil
913+
return modelType, nil
925914
}
926915

927916
// getSinceUntilLimitHeader returns since, until, limit

engine/hatchery/swarm/swarm.go

-19
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,6 @@ func (h *HatcherySwarm) CanSpawn(ctx context.Context, model *sdk.Model, jobID in
465465

466466
nbContainersFromHatchery := len(cs)
467467

468-
// List all workers containers
469-
ws := cs.FilterWorkers()
470-
471468
// Checking the number of container on each docker engine
472469
if nbContainersFromHatchery >= dockerClient.MaxContainers {
473470
log.Debug(ctx, "hatchery> swarm> CanSpawn> max containers reached on %s. current:%d max:%d", dockerName, nbContainersFromHatchery, dockerClient.MaxContainers)
@@ -482,22 +479,6 @@ func (h *HatcherySwarm) CanSpawn(ctx context.Context, model *sdk.Model, jobID in
482479
}
483480
}
484481

485-
// hatcherySwarm.ratioService: Percent reserved for spawning worker with service requirement
486-
// if no link -> we need to check ratioService
487-
if len(links) == 0 {
488-
ratioService := h.Config.Provision.RatioService
489-
if ratioService != nil && *ratioService >= 100 {
490-
log.Debug(ctx, "hatchery> swarm> CanSpawn> ratioService 100 by conf on %s - no spawn worker without CDS Service", dockerName)
491-
return false
492-
}
493-
if nbContainersFromHatchery > 0 {
494-
percentFree := 100 - (100 * len(ws) / dockerClient.MaxContainers)
495-
if ratioService != nil && percentFree <= *ratioService {
496-
log.Debug(ctx, "hatchery> swarm> CanSpawn> ratio reached on %s. percentFree:%d ratioService:%d", dockerName, percentFree, *ratioService)
497-
return false
498-
}
499-
}
500-
}
501482
return true
502483
}
503484
return false

0 commit comments

Comments
 (0)