-
Notifications
You must be signed in to change notification settings - Fork 132
/
root.go
195 lines (178 loc) · 5.79 KB
/
root.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
package cmd
import (
"os"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"golang.org/x/exp/maps"
"github.com/armadaproject/armada/internal/common/armadacontext"
"github.com/armadaproject/armada/internal/common/slices"
"github.com/armadaproject/armada/internal/scheduler/configuration"
"github.com/armadaproject/armada/internal/scheduler/simulator"
"github.com/armadaproject/armada/internal/scheduler/testfixtures"
)
func RootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "Simulate",
Short: "Simulate running jobs on Armada.",
RunE: runSimulations,
}
// cmd.Flags().BoolP("verbose", "v", false, "Log detailed output to console.")
cmd.Flags().String("clusters", "", "Glob pattern specifying cluster configurations to simulate.")
cmd.Flags().String("workloads", "", "Glob pattern specifying workloads to simulate.")
cmd.Flags().String("configs", "", "Glob pattern specifying scheduler configurations to simulate. Uses a default config if not provided.")
cmd.Flags().Bool("showSchedulerLogs", false, "Show scheduler logs.")
cmd.Flags().Int("logInterval", 0, "Log summary statistics every this many events. Disabled if 0.")
cmd.Flags().String("eventsOutputFilePath", "", "Path of file to write events to.")
return cmd
}
func runSimulations(cmd *cobra.Command, args []string) error {
// Get command-line arguments.
clusterPattern, err := cmd.Flags().GetString("clusters")
if err != nil {
return err
}
workloadPattern, err := cmd.Flags().GetString("workloads")
if err != nil {
return err
}
configPattern, err := cmd.Flags().GetString("configs")
if err != nil {
return err
}
showSchedulerLogs, err := cmd.Flags().GetBool("showSchedulerLogs")
if err != nil {
return err
}
logInterval, err := cmd.Flags().GetInt("logInterval")
if err != nil {
return err
}
filePath, err := cmd.Flags().GetString("eventsOutputFilePath")
if err != nil {
return err
}
// Load test specs. and config.
clusterSpecs, err := simulator.ClusterSpecsFromPattern(clusterPattern)
if err != nil {
return err
}
workloadSpecs, err := simulator.WorkloadsFromPattern(workloadPattern)
if err != nil {
return err
}
var schedulingConfigsByFilePath map[string]configuration.SchedulingConfig
if configPattern == "" {
// Use default test config if no pattern is provided.
schedulingConfigsByFilePath = map[string]configuration.SchedulingConfig{
"default": testfixtures.TestSchedulingConfig(),
}
} else {
schedulingConfigsByFilePath, err = simulator.SchedulingConfigsByFilePathFromPattern(configPattern)
if err != nil {
return err
}
}
if len(clusterSpecs)*len(workloadSpecs)*len(schedulingConfigsByFilePath) > 1 && filePath != "" {
return errors.Errorf("cannot save multiple simulations to file")
}
ctx := armadacontext.Background()
ctx.Info("Armada simulator")
ctx.Infof("ClusterSpecs: %v", slices.Map(clusterSpecs, func(clusperSpec *simulator.ClusterSpec) string { return clusperSpec.Name }))
ctx.Infof("WorkloadSpecs: %v", slices.Map(workloadSpecs, func(workloadSpec *simulator.WorkloadSpec) string { return workloadSpec.Name }))
ctx.Infof("SchedulingConfigs: %v", maps.Keys(schedulingConfigsByFilePath))
var fileWriter *simulator.Writer
file, err := os.Create(filePath)
if err != nil {
return err
}
defer func() {
if err = file.Close(); err != nil {
ctx.Errorf("failed to close file: %s", err)
return
}
}()
// Setup a simulator for each combination of (clusterSpec, workloadSpec, schedulingConfig).
simulators := make([]*simulator.Simulator, 0)
metricsCollectors := make([]*simulator.MetricsCollector, 0)
stateTransitionChannels := make([]<-chan simulator.StateTransition, 0)
schedulingConfigPaths := make([]string, 0)
for _, clusterSpec := range clusterSpecs {
for _, workloadSpec := range workloadSpecs {
for schedulingConfigPath, schedulingConfig := range schedulingConfigsByFilePath {
if s, err := simulator.NewSimulator(clusterSpec, workloadSpec, schedulingConfig); err != nil {
return err
} else {
if !showSchedulerLogs {
s.SuppressSchedulerLogs = true
} else {
ctx.Info("Showing scheduler logs")
}
simulators = append(simulators, s)
mc := simulator.NewMetricsCollector(s.StateTransitions())
mc.LogSummaryInterval = logInterval
metricsCollectors = append(metricsCollectors, mc)
if filePath != "" {
fw, err := simulator.NewWriter(file, s.StateTransitions())
if err != nil {
return errors.WithStack(err)
}
fileWriter = fw
}
stateTransitionChannels = append(stateTransitionChannels, s.StateTransitions())
schedulingConfigPaths = append(schedulingConfigPaths, schedulingConfigPath)
}
}
}
}
// Run simulators.
g, ctx := armadacontext.ErrGroup(ctx)
for _, s := range simulators {
s := s
g.Go(func() error {
return s.Run(ctx)
})
}
// Log events to stdout.
for _, c := range stateTransitionChannels {
c := c
g.Go(func() error {
for {
select {
case <-ctx.Done():
return ctx.Err()
case stateTransition, ok := <-c:
if !ok {
return nil
}
ctx.Debug(*stateTransition.EventSequence.Events[0].Created, simulator.EventSequenceSummary(stateTransition.EventSequence))
}
}
})
}
// Run file writer
g.Go(func() error {
return fileWriter.Run(ctx)
})
// Run metric collectors.
for _, mc := range metricsCollectors {
mc := mc
g.Go(func() error {
return mc.Run(ctx)
})
}
// Wait for simulations to complete.
if err := g.Wait(); err != nil {
return err
}
// Log overall statistics.
for i, mc := range metricsCollectors {
s := simulators[i]
schedulingConfigPath := schedulingConfigPaths[i]
ctx.Infof("Simulation result")
ctx.Infof("ClusterSpec: %s", s.ClusterSpec.Name)
ctx.Infof("WorkloadSpec: %s", s.WorkloadSpec.Name)
ctx.Infof("SchedulingConfig: %s", schedulingConfigPath)
ctx.Info(mc.String())
}
return nil
}