Skip to content

Commit fa8b653

Browse files
committed
[core] Refactor taskclass package
1 parent 846183e commit fa8b653

4 files changed

Lines changed: 251 additions & 169 deletions

File tree

core/task/taskclass/class.go

Lines changed: 16 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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

2727
import (
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

4239
var 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-
248165
func (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-
}

core/task/taskclass/classes.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* === This file is part of ALICE O² ===
3+
*
4+
* Copyright 2018-2023 CERN and copyright holders of ALICE O².
5+
* Author: Teo Mrnjavac <teo.mrnjavac@cern.ch>
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
* In applying this license CERN does not waive the privileges and
21+
* immunities granted to it by virtue of its status as an
22+
* Intergovernmental Organization or submit itself to any jurisdiction.
23+
*/
24+
25+
package taskclass
26+
27+
import "sync"
28+
29+
type Classes struct {
30+
mu sync.RWMutex
31+
classMap map[string]*Class
32+
}
33+
34+
func (c *Classes) Do(f func(classMap *map[string]*Class) error) error {
35+
c.mu.Lock()
36+
defer c.mu.Unlock()
37+
return f(&c.classMap)
38+
}
39+
40+
func (c *Classes) Foreach(do func(string, *Class) bool) {
41+
c.mu.Lock()
42+
defer c.mu.Unlock()
43+
44+
for taskClassIdentifier, classPtr := range c.classMap {
45+
ok := do(taskClassIdentifier, classPtr)
46+
if !ok {
47+
return
48+
}
49+
}
50+
}
51+
52+
func (c *Classes) getMap() map[string]*Class {
53+
c.mu.RLock()
54+
defer c.mu.RUnlock()
55+
56+
return c.classMap
57+
}
58+
59+
func (c *Classes) DeleteKey(key string) {
60+
c.mu.Lock()
61+
defer c.mu.Unlock()
62+
63+
delete(c.classMap, key)
64+
}
65+
66+
func (c *Classes) DeleteKeys(keys []string) {
67+
c.mu.Lock()
68+
defer c.mu.Unlock()
69+
70+
for _, k := range keys {
71+
delete(c.classMap, k)
72+
}
73+
}
74+
75+
func (c *Classes) UpdateClass(key string, class *Class) {
76+
c.mu.Lock()
77+
defer c.mu.Unlock()
78+
if _, ok := c.classMap[key]; ok { //contains
79+
*c.classMap[key] = *class // update
80+
} else {
81+
c.classMap[key] = class // else add class as new entry
82+
}
83+
}
84+
85+
func (c *Classes) GetClass(key string) (class *Class, ok bool) {
86+
c.mu.RLock()
87+
defer c.mu.RUnlock()
88+
89+
class, ok = c.classMap[key]
90+
return
91+
}
92+
93+
func NewClasses() *Classes {
94+
return &Classes{
95+
classMap: make(map[string]*Class),
96+
}
97+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* === This file is part of ALICE O² ===
3+
*
4+
* Copyright 2018-2023 CERN and copyright holders of ALICE O².
5+
* Author: Teo Mrnjavac <teo.mrnjavac@cern.ch>
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
* In applying this license CERN does not waive the privileges and
21+
* immunities granted to it by virtue of its status as an
22+
* Intergovernmental Organization or submit itself to any jurisdiction.
23+
*/
24+
25+
package taskclass
26+
27+
import "strconv"
28+
29+
type ResourceLimits struct {
30+
Cpu *float64 `yaml:"cpu"`
31+
Memory *float64 `yaml:"memory"`
32+
}
33+
34+
func (rw *ResourceLimits) UnmarshalYAML(unmarshal func(interface{}) error) (err error) {
35+
type _resourceLimits struct {
36+
Cpu *string `yaml:"cpu"`
37+
Memory *string `yaml:"memory"`
38+
}
39+
aux := _resourceLimits{}
40+
err = unmarshal(&aux)
41+
if err != nil {
42+
return
43+
}
44+
45+
if aux.Cpu != nil {
46+
var cpuCount float64
47+
cpuCount, err = strconv.ParseFloat(*aux.Cpu, 64)
48+
if err != nil {
49+
return
50+
}
51+
rw.Cpu = &cpuCount
52+
}
53+
if aux.Memory != nil {
54+
var memCount float64
55+
memCount, err = strconv.ParseFloat(*aux.Memory, 64)
56+
if err != nil {
57+
return
58+
}
59+
rw.Memory = &memCount
60+
}
61+
return
62+
}

0 commit comments

Comments
 (0)