-
Notifications
You must be signed in to change notification settings - Fork 126
/
root.go
150 lines (137 loc) · 4.47 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
package cmd
import (
"github.com/spf13/cobra"
"golang.org/x/exp/maps"
"github.com/armadaproject/armada/internal/common/armadacontext"
"github.com/armadaproject/armada/internal/common/util"
"github.com/armadaproject/armada/internal/scheduler/simulator"
"github.com/armadaproject/armada/pkg/armadaevents"
)
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.")
cmd.Flags().Bool("showSchedulerLogs", false, "Show scheduler logs.")
cmd.Flags().Int("logInterval", 0, "Log summary statistics every this many events. Disabled if 0.")
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
}
// 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
}
schedulingConfigsByFilePath, err := simulator.SchedulingConfigsByFilePathFromPattern(configPattern)
if err != nil {
return err
}
ctx := armadacontext.Background()
ctx.Info("Armada simulator")
ctx.Infof("ClusterSpecs: %v", util.Map(clusterSpecs, func(clusperSpec *simulator.ClusterSpec) string { return clusperSpec.Name }))
ctx.Infof("WorkloadSpecs: %v", util.Map(workloadSpecs, func(workloadSpec *simulator.WorkloadSpec) string { return workloadSpec.Name }))
ctx.Infof("SchedulingConfigs: %v", maps.Keys(schedulingConfigsByFilePath))
// Setup a simulator for each combination of (clusterSpec, workloadSpec, schedulingConfig).
simulators := make([]*simulator.Simulator, 0)
metricsCollectors := make([]*simulator.MetricsCollector, 0)
eventSequenceChannels := make([]<-chan *armadaevents.EventSequence, 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.Output())
mc.LogSummaryInterval = logInterval
metricsCollectors = append(metricsCollectors, mc)
eventSequenceChannels = append(eventSequenceChannels, s.Output())
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 eventSequenceChannels {
c := c
g.Go(func() error {
for {
select {
case <-ctx.Done():
return ctx.Err()
case eventSequence, ok := <-c:
if !ok {
return nil
}
ctx.Debug(*eventSequence.Events[0].Created, simulator.EventSequenceSummary(eventSequence))
}
}
})
}
// 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
}