-
Notifications
You must be signed in to change notification settings - Fork 136
/
load-test.go
322 lines (276 loc) · 8.62 KB
/
load-test.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package client
import (
"context"
"strconv"
"sync"
"time"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/armadaproject/armada/internal/common"
"github.com/armadaproject/armada/internal/common/logging"
"github.com/armadaproject/armada/pkg/api"
"github.com/armadaproject/armada/pkg/client/domain"
)
type LoadTester interface {
RunSubmissionTest(ctx context.Context, spec domain.LoadTestSpecification, watchEvents bool) *domain.WatchContext
}
type ArmadaLoadTester struct {
apiConnectionDetails *ApiConnectionDetails
}
func NewArmadaLoadTester(connectionDetails *ApiConnectionDetails) *ArmadaLoadTester {
return &ArmadaLoadTester{
apiConnectionDetails: connectionDetails,
}
}
func (apiLoadTester ArmadaLoadTester) RunSubmissionTest(ctx context.Context, spec domain.LoadTestSpecification, watchEvents bool) domain.LoadTestSummary {
wg := &sync.WaitGroup{}
wg.Add(1)
eventChannel := make(chan api.Event, 10000)
var jobCurrentState *domain.WatchContext
if watchEvents {
complete, cancel, currentState := watchJobInfoChannel(eventChannel)
jobCurrentState = currentState
defer complete.Wait()
defer func() { cancel <- true }()
}
allSubmittedJobs := NewThreadSafeStringSlice()
for _, submission := range spec.Submissions {
for i := 0; i < submission.Count; i++ {
wg.Add(1)
go func(i int, submission *domain.SubmissionDescription) {
defer wg.Done()
jobIdChannel, jobSetId, submissionComplete := apiLoadTester.runSubmission(ctx, submission, i)
if watchEvents {
submittedIds := apiLoadTester.monitorJobsUntilCompletion(ctx, createQueueName(submission, i), jobSetId, jobIdChannel, eventChannel)
allSubmittedJobs.Append(submittedIds)
}
if ctx.Err() != nil {
apiLoadTester.cancelRemainingJobs(createQueueName(submission, i), jobSetId)
}
submissionComplete.Wait()
}(i, submission)
}
}
wg.Done()
wg.Wait()
return domain.LoadTestSummary{
SubmittedJobs: allSubmittedJobs.GetAll(),
CurrentState: jobCurrentState,
}
}
func watchJobInfoChannel(eventChannel chan api.Event) (*sync.WaitGroup, chan bool, *domain.WatchContext) {
stop := make(chan bool)
tickChannel := time.NewTicker(5 * time.Second)
complete := &sync.WaitGroup{}
complete.Add(1)
aggregatedCurrentState := domain.NewWatchContext()
go func() {
for {
select {
case event := <-eventChannel:
aggregatedCurrentState.ProcessEvent(event)
case <-tickChannel.C:
log.Info(aggregatedCurrentState.GetCurrentStateSummary())
case <-stop:
close(eventChannel)
for event := range eventChannel {
aggregatedCurrentState.ProcessEvent(event)
}
log.Info(aggregatedCurrentState.GetCurrentStateSummary())
complete.Done()
return
}
}
}()
return complete, stop, aggregatedCurrentState
}
func (apiLoadTester ArmadaLoadTester) runSubmission(
ctx context.Context,
submission *domain.SubmissionDescription,
i int,
) (jobIds chan string, jobSetId string, submissionComplete *sync.WaitGroup) {
queue := createQueueName(submission, i)
startTime := time.Now()
priorityFactor := submission.QueuePriorityFactor
if priorityFactor <= 0 {
priorityFactor = 1
}
jobSetId = submission.JobSetPrefix + "-" + strconv.Itoa(i)
jobs := submission.Jobs
jobCount := 0
for _, job := range jobs {
jobCount += job.Count
}
jobIds = make(chan string, jobCount)
submissionComplete = &sync.WaitGroup{}
submissionComplete.Add(1)
go func() {
err := WithSubmitClient(apiLoadTester.apiConnectionDetails, func(client api.SubmitClient) error {
defer submissionComplete.Done()
e := CreateQueue(client, &api.Queue{Name: queue, PriorityFactor: priorityFactor})
if status.Code(e) == codes.AlreadyExists {
log.Infof("queue %s already exists so no need to create it.\n", queue)
} else if e != nil {
log.Errorf("failed to create queue: %s because: %s\n", queue, e)
return nil
} else {
log.Infof("queue %s created.\n", queue)
}
for len(jobs) > 0 {
select {
case <-ctx.Done():
break
default:
}
readyJobs, remainingJobs := filterReadyJobs(startTime, jobs)
jobs = remainingJobs
readyRequests := createJobSubmitRequestItems(readyJobs)
requests := CreateChunkedSubmitRequests(queue, jobSetId, readyRequests)
for _, request := range requests {
response, e := SubmitJobs(client, request)
if e != nil {
log.Errorf("failed to submit jobs for job set: %s because %s\n", jobSetId, e)
continue
}
failedJobs := 0
for _, jobSubmitResponse := range response.JobResponseItems {
if jobSubmitResponse.Error != "" {
failedJobs++
} else {
jobIds <- jobSubmitResponse.JobId
}
}
log.Infof("submitted %d jobs to queue %s job set %s", len(request.JobRequestItems), queue, jobSetId)
if failedJobs > 0 {
log.Errorf("%d jobs failed to be created when submitting to queue %s job set %s", failedJobs, queue, jobSetId)
}
}
if len(jobs) > 0 {
time.Sleep(time.Second)
}
}
close(jobIds)
return nil
})
if err != nil {
log.Errorf("detected when submitting jobs: %s", err)
}
}()
return jobIds, jobSetId, submissionComplete
}
func filterReadyJobs(
startTime time.Time,
jobs []*domain.JobSubmissionDescription,
) (ready []*domain.JobSubmissionDescription, notReady []*domain.JobSubmissionDescription) {
now := time.Now()
ready = []*domain.JobSubmissionDescription{}
notReady = []*domain.JobSubmissionDescription{}
for _, j := range jobs {
if startTime.Add(j.DelaySubmit).Before(now) {
ready = append(ready, j)
} else {
notReady = append(notReady, j)
}
}
return ready, notReady
}
func createQueueName(submission *domain.SubmissionDescription, i int) string {
queue := ""
if submission.Queue != "" {
queue = submission.Queue
} else if submission.QueuePrefix != "" {
queue = submission.QueuePrefix + "-" + strconv.Itoa(i)
}
if queue == "" {
log.Error("queue name is blank, please set queue or queuePrefix ")
panic("Queue name is blank")
}
return queue
}
func (apiLoadTester ArmadaLoadTester) monitorJobsUntilCompletion(
ctx context.Context,
queue, jobSetId string,
jobIds chan string,
eventChannel chan api.Event,
) []string {
log := logrus.StandardLogger().WithField("Armada", "LoadTester")
var submittedIds []string = nil
go func() {
ids := []string{}
for id := range jobIds {
ids = append(ids, id)
}
submittedIds = ids
}()
err := WithEventClient(apiLoadTester.apiConnectionDetails, func(client api.EventClient) error {
WatchJobSet(client, queue, jobSetId, true, false, false, false, ctx, func(state *domain.WatchContext, e api.Event) bool {
eventChannel <- e
if submittedIds == nil {
return false
}
numberOfJobsInCompletedState := state.GetNumberOfFinishedJobs()
if numberOfJobsInCompletedState < len(submittedIds) {
return false
}
return state.AreJobsFinished(submittedIds)
})
return nil
})
if err != nil {
logging.WithStacktrace(log, err).Error("unable to monitor jobs")
}
return submittedIds
}
func createJobSubmitRequestItems(jobDescs []*domain.JobSubmissionDescription) []*api.JobSubmitRequestItem {
requestItems := []*api.JobSubmitRequestItem{}
for _, jobDesc := range jobDescs {
for i := 0; i < jobDesc.Count; i++ {
requestItems = append(requestItems, &api.JobSubmitRequestItem{
Priority: jobDesc.Priority,
Namespace: jobDesc.Namespace,
Annotations: jobDesc.Annotations,
Labels: jobDesc.Labels,
RequiredNodeLabels: jobDesc.RequiredNodeLabels,
PodSpec: jobDesc.Spec,
})
}
}
return requestItems
}
func (apiLoadTester ArmadaLoadTester) cancelRemainingJobs(queue string, jobSetId string) {
log := logrus.StandardLogger().WithField("Armada", "LoadTester")
err := WithSubmitClient(apiLoadTester.apiConnectionDetails, func(client api.SubmitClient) error {
timeout, _ := common.ContextWithDefaultTimeout()
cancelRequest := &api.JobCancelRequest{
JobSetId: jobSetId,
Queue: queue,
}
_, err := client.CancelJobs(timeout, cancelRequest)
return err
})
if err != nil {
logging.WithStacktrace(log, err).Error("unable to cancel jobs")
}
}
type threadSafeStringSlice struct {
slice []string
mutex *sync.Mutex
}
func NewThreadSafeStringSlice() *threadSafeStringSlice {
return &threadSafeStringSlice{
slice: make([]string, 0, 10),
mutex: &sync.Mutex{},
}
}
func (s *threadSafeStringSlice) Append(additionalElements []string) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.slice = append(s.slice, additionalElements...)
}
func (s *threadSafeStringSlice) GetAll() []string {
s.mutex.Lock()
defer s.mutex.Unlock()
return append([]string{}, s.slice...)
}