-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathaggregator.go
More file actions
253 lines (221 loc) · 6.15 KB
/
Copy pathaggregator.go
File metadata and controls
253 lines (221 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* === This file is part of ALICE O² ===
*
* Copyright 2018 CERN and copyright holders of ALICE O².
* Author: Teo Mrnjavac <teo.mrnjavac@cern.ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In applying this license CERN does not waive the privileges and
* immunities granted to it by virtue of its status as an
* Intergovernmental Organization or submit itself to any jurisdiction.
*/
package workflow
import (
"errors"
"strconv"
"github.com/AliceO2Group/Control/core/task"
"github.com/AliceO2Group/Control/core/workflow/callable"
)
type aggregator struct {
Roles []Role `yaml:"roles,omitempty"`
}
func (r *aggregator) copy() copyable {
rCopy := aggregator{
Roles: make([]Role, len(r.Roles)),
}
for i, childRole := range r.Roles {
rCopy.Roles[i] = childRole.copy().(Role)
}
return &rCopy
}
// Auxiliary types for unmarshaling
type _unionTypeProbe struct {
For *struct{}
Task *struct{}
Roles []interface{}
Call *struct{}
Include *string
}
type _roleUnion struct {
*iteratorRole
*aggregatorRole
*taskRole
*callRole
*includeRole
}
func (union *_roleUnion) UnmarshalYAML(unmarshal func(interface{}) error) (unionErr error) {
_probe := _unionTypeProbe{}
unionErr = unmarshal(&_probe)
if unionErr != nil {
return
}
switch {
case _probe.For != nil:
unionErr = unmarshal(&union.iteratorRole)
case _probe.Roles != nil && _probe.Task == nil && _probe.Call == nil && _probe.Include == nil:
unionErr = unmarshal(&union.aggregatorRole)
case _probe.Task != nil && _probe.Roles == nil && _probe.Call == nil && _probe.Include == nil:
unionErr = unmarshal(&union.taskRole)
case _probe.Call != nil && _probe.Task == nil && _probe.Roles == nil && _probe.Include == nil:
unionErr = unmarshal(&union.callRole)
case _probe.Include != nil && _probe.Task == nil && _probe.Roles == nil && _probe.Call == nil:
unionErr = unmarshal(&union.includeRole)
default:
unionErr = errors.New("cannot unmarshal invalid role to union")
}
return
}
func (r *aggregator) UnmarshalYAML(unmarshal func(interface{}) error) (err error) {
// We define a fake aggregator, whose Roles member is a slice of _roleUnions
// The _roleUnions, when unmarshaling, will have probed for the actual role type
// through their own fake types, and unmarshaled the true role inside each of them.
type _aggregatorOfUnions struct {
Roles []_roleUnion
}
_a := _aggregatorOfUnions{Roles: make([]_roleUnion, 0)}
err = unmarshal(&_a)
if err != nil {
return
}
roles := make([]Role, len(_a.Roles))
for i, v := range _a.Roles {
switch {
case v.iteratorRole != nil:
roles[i] = v.iteratorRole
case v.aggregatorRole != nil:
roles[i] = v.aggregatorRole
case v.taskRole != nil:
roles[i] = v.taskRole
case v.callRole != nil:
roles[i] = v.callRole
case v.includeRole != nil:
roles[i] = v.includeRole
default:
err = errors.New("invalid child role at index " + strconv.Itoa(i))
return
}
}
r.Roles = roles
return
}
func (r *aggregator) MarshalYAML() (interface{}, error) {
aux := make(map[string]interface{})
aux["roles"] = r.Roles
return aux, nil
}
func (r *aggregator) GenerateTaskDescriptors() (ds task.Descriptors) {
if r == nil {
return nil
}
ds = make(task.Descriptors, 0)
for _, role := range r.GetRoles() {
ds = append(ds, role.GenerateTaskDescriptors()...)
}
return
}
func (r *aggregator) GetTasks() (tasks task.Tasks) {
if r == nil {
return nil
}
tasks = make(task.Tasks, 0)
for _, role := range r.GetRoles() {
tasks = append(tasks, role.GetTasks()...)
}
return
}
func (r *aggregator) GetHooksMapForTrigger(trigger string) (hooksMap callable.HooksMap) {
if r == nil {
return make(callable.HooksMap)
}
hooksMap = make(callable.HooksMap)
for _, role := range r.GetRoles() {
childHooksMap := role.GetHooksMapForTrigger(trigger)
for k, v := range childHooksMap {
_, exists := hooksMap[k]
if exists && len(hooksMap[k]) > 0 {
hooksMap[k] = append(hooksMap[k], v...)
} else {
hooksMap[k] = v
}
}
}
return
}
func (r *aggregator) GetAllHooks() (hooks callable.Hooks) {
if r == nil {
return nil
}
hooks = make(callable.Hooks, 0)
for _, role := range r.GetRoles() {
hooks = append(hooks, role.GetAllHooks()...)
}
return
}
func (r *aggregator) GetTaskClasses() (taskClassesSlice []string) {
if r == nil {
return nil
}
taskClasses := make(map[string]bool, 0)
for _, role := range r.GetRoles() { //Keep task classes in a set to avoid duplication
for _, taskClass := range role.GetTaskClasses() {
taskClasses[taskClass] = true
}
}
taskClassesSlice = make([]string, 0) //But return a regular slice
for key := range taskClasses {
taskClassesSlice = append(taskClassesSlice, key)
}
return
}
func (r *aggregator) GetRoles() []Role {
if r == nil {
return nil
}
roles := make([]Role, 0)
for _, v := range r.Roles {
if iter, ok := v.(*iteratorRole); ok {
roles = append(roles, iter.GetRoles()...)
continue
}
roles = append(roles, v)
}
return roles
}
/*func (r *aggregator) doTransition(transition Transition) (status task.Status, state task.State) {
if r == nil || len(r.Roles) == 0 {
status = task.UNDEFINED
state = task.MIXED
return
}
// parallel for
var wg sync.WaitGroup
wg.Add(len(r.Roles))
statuses := make([]task.Status, len(r.Roles))
states := make([]task.State, len(r.Roles))
for i := 0; i < len(r.Roles); i++ {
go func(i int) {
defer wg.Done()
statuses[i], states[i] = r.Roles[i].doTransition(transition)
}(i)
}
wg.Wait()
status = statuses[0]
state = states[0]
for i := 1; i < len(r.Roles); i++ {
status = status.X(statuses[i])
state = state.X(states[i])
}
return
}*/