-
Notifications
You must be signed in to change notification settings - Fork 132
/
runner.go
220 lines (199 loc) · 6.32 KB
/
runner.go
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
package simulator
import (
"fmt"
"path/filepath"
"strings"
"github.com/mattn/go-zglob"
"github.com/pkg/errors"
"github.com/renstrom/shortuuid"
"github.com/spf13/viper"
"github.com/armadaproject/armada/internal/armada/configuration"
"github.com/armadaproject/armada/internal/common/armadacontext"
commonconfig "github.com/armadaproject/armada/internal/common/config"
)
func Simulate(ctx *armadacontext.Context, clusterSpecsPattern, workloadSpecsPattern, schedulingConfigsPattern string) error {
clusterSpecs, err := ClusterSpecsFromPattern(clusterSpecsPattern)
if err != nil {
return err
}
workloadSpecs, err := WorkloadsFromPattern(workloadSpecsPattern)
if err != nil {
return err
}
schedulingConfigs, err := SchedulingConfigsFromPattern(schedulingConfigsPattern)
if err != nil {
return err
}
g, ctx := armadacontext.ErrGroup(ctx)
for _, clusterSpec := range clusterSpecs {
for _, workloadSpec := range workloadSpecs {
for _, schedulingConfig := range schedulingConfigs {
s, err := NewSimulator(clusterSpec, workloadSpec, schedulingConfig)
if err != nil {
return err
}
g.Go(func() error {
return s.Run(ctx)
})
}
}
}
return g.Wait()
}
func SchedulingConfigsByFilePathFromPattern(pattern string) (map[string]configuration.SchedulingConfig, error) {
filePaths, err := zglob.Glob(pattern)
if err != nil {
return nil, errors.WithStack(err)
}
filePathConfigMap := make(map[string]configuration.SchedulingConfig)
for _, path := range filePaths {
config, err := SchedulingConfigsFromFilePaths(filePaths)
if err != nil {
return nil, err
}
filePathConfigMap[path] = config[0]
}
return filePathConfigMap, nil
}
func SchedulingConfigsFromPattern(pattern string) ([]configuration.SchedulingConfig, error) {
filePaths, err := zglob.Glob(pattern)
if err != nil {
return nil, errors.WithStack(err)
}
return SchedulingConfigsFromFilePaths(filePaths)
}
func SchedulingConfigsFromFilePaths(filePaths []string) ([]configuration.SchedulingConfig, error) {
rv := make([]configuration.SchedulingConfig, len(filePaths))
for i, filePath := range filePaths {
config, err := SchedulingConfigFromFilePath(filePath)
if err != nil {
return nil, err
}
rv[i] = config
}
return rv, nil
}
func SchedulingConfigFromFilePath(filePath string) (configuration.SchedulingConfig, error) {
config := configuration.SchedulingConfig{}
v := viper.NewWithOptions(viper.KeyDelimiter("::"))
v.SetConfigFile(filePath)
if err := v.ReadInConfig(); err != nil {
err = errors.WithMessagef(err, "failed to read in SchedulingConfig %s", filePath)
return config, errors.WithStack(err)
}
if err := v.Unmarshal(&config, commonconfig.CustomHooks...); err != nil {
err = errors.WithMessagef(err, "failed to unmarshal SchedulingConfig %s", filePath)
return config, errors.WithStack(err)
}
return config, nil
}
func ClusterSpecsFromPattern(pattern string) ([]*ClusterSpec, error) {
filePaths, err := zglob.Glob(pattern)
if err != nil {
return nil, errors.WithStack(err)
}
return ClusterSpecsFromFilePaths(filePaths)
}
func WorkloadsFromPattern(pattern string) ([]*WorkloadSpec, error) {
filePaths, err := zglob.Glob(pattern)
if err != nil {
return nil, errors.WithStack(err)
}
return WorkloadSpecsFromFilePaths(filePaths)
}
func ClusterSpecsFromFilePaths(filePaths []string) ([]*ClusterSpec, error) {
rv := make([]*ClusterSpec, len(filePaths))
for i, filePath := range filePaths {
clusterSpec, err := ClusterSpecFromFilePath(filePath)
if err != nil {
return nil, err
}
rv[i] = clusterSpec
}
return rv, nil
}
func WorkloadSpecsFromFilePaths(filePaths []string) ([]*WorkloadSpec, error) {
rv := make([]*WorkloadSpec, len(filePaths))
for i, filePath := range filePaths {
workloadSpec, err := WorkloadSpecFromFilePath(filePath)
if err != nil {
return nil, err
}
rv[i] = workloadSpec
}
return rv, nil
}
func ClusterSpecFromFilePath(filePath string) (*ClusterSpec, error) {
rv := &ClusterSpec{}
v := viper.NewWithOptions(viper.KeyDelimiter("::"))
v.SetConfigFile(filePath)
if err := v.ReadInConfig(); err != nil {
err = errors.WithMessagef(err, "failed to read in ClusterSpec %s", filePath)
return nil, errors.WithStack(err)
}
if err := v.Unmarshal(rv, commonconfig.CustomHooks...); err != nil {
err = errors.WithMessagef(err, "failed to unmarshal ClusterSpec %s", filePath)
return nil, errors.WithStack(err)
}
// If no test name is provided, set it to be the filename.
if rv.Name == "" {
fileName := filepath.Base(filePath)
fileName = strings.TrimSuffix(fileName, filepath.Ext(fileName))
rv.Name = fileName
}
initialiseClusterSpec(rv)
return rv, nil
}
func WorkloadSpecFromFilePath(filePath string) (*WorkloadSpec, error) {
rv := &WorkloadSpec{}
v := viper.NewWithOptions(viper.KeyDelimiter("::"))
v.SetConfigFile(filePath)
if err := v.ReadInConfig(); err != nil {
err = errors.WithMessagef(err, "failed to read in WorkloadSpec %s", filePath)
return nil, errors.WithStack(err)
}
if err := v.Unmarshal(rv, commonconfig.CustomHooks...); err != nil {
err = errors.WithMessagef(err, "failed to unmarshal WorkloadSpec %s", filePath)
return nil, errors.WithStack(err)
}
// If no test name is provided, set it to be the filename.
if rv.Name == "" {
fileName := filepath.Base(filePath)
fileName = strings.TrimSuffix(fileName, filepath.Ext(fileName))
rv.Name = fileName
}
// Generate random ids for any job templates without an explicitly set id.
for _, queue := range rv.Queues {
for j, jobTemplate := range queue.JobTemplates {
if jobTemplate.Id == "" {
jobTemplate.Id = shortuuid.New()
}
queue.JobTemplates[j] = jobTemplate
}
}
initialiseWorkloadSpec(rv)
return rv, nil
}
func initialiseClusterSpec(clusterSpec *ClusterSpec) {
// Assign names to executors with none specified.
for _, pool := range clusterSpec.Pools {
for i, executorGroup := range pool.ClusterGroups {
for j, executor := range executorGroup.Clusters {
if executor.Name == "" {
executor.Name = fmt.Sprintf("%s-%d-%d", pool.Name, i, j)
}
}
}
}
}
func initialiseWorkloadSpec(workloadSpec *WorkloadSpec) {
// Assign names to jobTemplates with none specified.
for _, queue := range workloadSpec.Queues {
for i, jobTemplate := range queue.JobTemplates {
if jobTemplate.Id == "" {
jobTemplate.Id = fmt.Sprintf("%s-%d", queue.Name, i)
}
jobTemplate.Queue = queue.Name
}
}
}