11/*
22 * === This file is part of ALICE O² ===
33 *
4- * Copyright 2018 CERN and copyright holders of ALICE O².
4+ * Copyright 2018-2023 CERN and copyright holders of ALICE O².
55 * Author: Teo Mrnjavac <teo.mrnjavac@cern.ch>
66 *
77 * This program is free software: you can redistribute it and/or modify
@@ -26,21 +26,33 @@ package taskclass
2626
2727import (
2828 "fmt"
29- "strconv"
30- "sync"
3129
3230 "github.com/AliceO2Group/Control/common"
3331 "github.com/AliceO2Group/Control/common/controlmode"
3432 "github.com/AliceO2Group/Control/common/gera"
3533 "github.com/AliceO2Group/Control/common/logger"
3634 "github.com/AliceO2Group/Control/core/task/channel"
3735 "github.com/AliceO2Group/Control/core/task/constraint"
38- "github.com/AliceO2Group/Control/core/task/taskclass/port"
3936 "github.com/sirupsen/logrus"
4037)
4138
4239var log = logger .New (logrus .StandardLogger (), "taskclass" )
4340
41+ type Id struct {
42+ RepoIdentifier string
43+ Hash string
44+ Name string
45+ }
46+
47+ func (tcID Id ) String () string {
48+ return fmt .Sprintf ("%s/tasks/%s@%s" , tcID .RepoIdentifier , tcID .Name , tcID .Hash )
49+ }
50+
51+ func (tcID * Id ) UnmarshalYAML (unmarshal func (interface {}) error ) (err error ) {
52+ err = unmarshal (& tcID .Name )
53+ return
54+ }
55+
4456// ↓ We need the roles tree to know *where* to run it and how to *configure* it, but
4557//
4658// the following information is enough to run the task even with no environment or
@@ -150,101 +162,6 @@ func (c *Class) MarshalYAML() (interface{}, error) {
150162 return aux , nil
151163}
152164
153- type Id struct {
154- RepoIdentifier string
155- Hash string
156- Name string
157- }
158-
159- func (tcID Id ) String () string {
160- return fmt .Sprintf ("%s/tasks/%s@%s" , tcID .RepoIdentifier , tcID .Name , tcID .Hash )
161- }
162-
163- func (tcID * Id ) UnmarshalYAML (unmarshal func (interface {}) error ) (err error ) {
164- err = unmarshal (& tcID .Name )
165- return
166- }
167-
168- type ResourceWants struct {
169- Cpu * float64 `yaml:"cpu"`
170- Memory * float64 `yaml:"memory"`
171- Ports port.Ranges `yaml:"ports,omitempty"`
172- }
173-
174- func (rw * ResourceWants ) UnmarshalYAML (unmarshal func (interface {}) error ) (err error ) {
175- type _resourceWants struct {
176- Cpu * string `yaml:"cpu"`
177- Memory * string `yaml:"memory"`
178- Ports * string `yaml:"ports"`
179- }
180- aux := _resourceWants {}
181- err = unmarshal (& aux )
182- if err != nil {
183- return
184- }
185-
186- if aux .Cpu != nil {
187- var cpuCount float64
188- cpuCount , err = strconv .ParseFloat (* aux .Cpu , 64 )
189- if err != nil {
190- return
191- }
192- rw .Cpu = & cpuCount
193- }
194- if aux .Memory != nil {
195- var memCount float64
196- memCount , err = strconv .ParseFloat (* aux .Memory , 64 )
197- if err != nil {
198- return
199- }
200- rw .Memory = & memCount
201- }
202- if aux .Ports != nil {
203- var ranges port.Ranges
204- ranges , err = port .RangesFromExpression (* aux .Ports )
205- if err != nil {
206- return
207- }
208- rw .Ports = ranges
209- }
210- return
211- }
212-
213- type ResourceLimits struct {
214- Cpu * float64 `yaml:"cpu"`
215- Memory * float64 `yaml:"memory"`
216- }
217-
218- func (rw * ResourceLimits ) UnmarshalYAML (unmarshal func (interface {}) error ) (err error ) {
219- type _resourceLimits struct {
220- Cpu * string `yaml:"cpu"`
221- Memory * string `yaml:"memory"`
222- }
223- aux := _resourceLimits {}
224- err = unmarshal (& aux )
225- if err != nil {
226- return
227- }
228-
229- if aux .Cpu != nil {
230- var cpuCount float64
231- cpuCount , err = strconv .ParseFloat (* aux .Cpu , 64 )
232- if err != nil {
233- return
234- }
235- rw .Cpu = & cpuCount
236- }
237- if aux .Memory != nil {
238- var memCount float64
239- memCount , err = strconv .ParseFloat (* aux .Memory , 64 )
240- if err != nil {
241- return
242- }
243- rw .Memory = & memCount
244- }
245- return
246- }
247-
248165func (c * Class ) Equals (other * Class ) (response bool ) {
249166 if c == nil || other == nil {
250167 return false
@@ -255,73 +172,3 @@ func (c *Class) Equals(other *Class) (response bool) {
255172 c .Wants .Ports .Equals (other .Wants .Ports )
256173 return
257174}
258-
259- type Classes struct {
260- mu sync.RWMutex
261- classMap map [string ]* Class
262- }
263-
264- func (c * Classes ) Do (f func (classMap * map [string ]* Class ) error ) error {
265- c .mu .Lock ()
266- defer c .mu .Unlock ()
267- return f (& c .classMap )
268- }
269-
270- func (c * Classes ) Foreach (do func (string , * Class ) bool ) {
271- c .mu .Lock ()
272- defer c .mu .Unlock ()
273-
274- for taskClassIdentifier , classPtr := range c .classMap {
275- ok := do (taskClassIdentifier , classPtr )
276- if ! ok {
277- return
278- }
279- }
280- }
281-
282- func (c * Classes ) getMap () map [string ]* Class {
283- c .mu .RLock ()
284- defer c .mu .RUnlock ()
285-
286- return c .classMap
287- }
288-
289- func (c * Classes ) DeleteKey (key string ) {
290- c .mu .Lock ()
291- defer c .mu .Unlock ()
292-
293- delete (c .classMap , key )
294- }
295-
296- func (c * Classes ) DeleteKeys (keys []string ) {
297- c .mu .Lock ()
298- defer c .mu .Unlock ()
299-
300- for _ , k := range keys {
301- delete (c .classMap , k )
302- }
303- }
304-
305- func (c * Classes ) UpdateClass (key string , class * Class ) {
306- c .mu .Lock ()
307- defer c .mu .Unlock ()
308- if _ , ok := c .classMap [key ]; ok { //contains
309- * c .classMap [key ] = * class // update
310- } else {
311- c .classMap [key ] = class // else add class as new entry
312- }
313- }
314-
315- func (c * Classes ) GetClass (key string ) (class * Class , ok bool ) {
316- c .mu .RLock ()
317- defer c .mu .RUnlock ()
318-
319- class , ok = c .classMap [key ]
320- return
321- }
322-
323- func NewClasses () * Classes {
324- return & Classes {
325- classMap : make (map [string ]* Class ),
326- }
327- }
0 commit comments