/
workload.go
354 lines (317 loc) · 9.33 KB
/
workload.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package workload
import (
"fmt"
"io"
"math/rand"
"github.com/pachyderm/pachyderm/src/client"
"github.com/pachyderm/pachyderm/src/client/pfs"
"github.com/pachyderm/pachyderm/src/client/pps"
)
// RunWorkload runs a test workload against a Pachyderm cluster.
func RunWorkload(
client *client.APIClient,
rand *rand.Rand,
size int,
) error {
worker := newWorker(rand)
for i := 0; i < size; i++ {
if err := worker.work(client); err != nil {
return err
}
}
for _, job := range worker.startedJobs {
jobInfo, err := client.InspectJob(job.ID, true)
if err != nil {
return err
}
if jobInfo.State != pps.JobState_JOB_SUCCESS {
return fmt.Errorf("job %s failed", job.ID)
}
}
return nil
}
// Constants corresponding to operations that worker.work() may perform. For
// each constant c_i below, (c_i - c_{i-1}) is the probability that operation i
// is performed
const (
repo float64 = .02 // Create a repo
commit = .1 // Start or finish a commit
file = .9 // Put a file
job = .98 // Start a job, or wait for a job to finish
pipeline = 1.0 // Create a pipeline
)
type worker struct {
// Repos created by the worker. Modified by the 'repo' operation.
repos []*pfs.Repo
// Commits that the worker has finished. Guaranteed to be a subset of
// 'started'. Modified by 'repo' and 'commit'.
finished []*pfs.Commit
// Commits that the worker has started. Modified by 'repo' and 'commit'.
started []*pfs.Commit
// Files that the worker has created
files []*pfs.File
// Jobs that the worker has started (but may not have finished). Guaranteed to
// be mutually exclusive with 'jobs'.
startedJobs []*pps.Job
// Worker jobs that have definitely finished. Modified by 'job'.
finishedJobs []*pps.Job
// Pipelines that the worker has created. Modified by 'pipeline'.
pipelines []*pps.Pipeline
// PRNG used to pick each operation
rand *rand.Rand
}
func newWorker(rand *rand.Rand) *worker {
return &worker{
rand: rand,
}
}
const maxStartedCommits = 6
const maxStartedJobs = 6
func (w *worker) work(c *client.APIClient) error {
// Generate a random float, and use it to choose the next operation
opt := w.rand.Float64()
switch {
case opt < repo:
return w.createRepo(c)
case opt < commit:
return w.advanceCommit(c)
case opt < file:
return w.putFile(c)
case opt < job:
return w.advanceJob(c)
case opt < pipeline:
return w.createPipeline(c)
}
return nil
}
// createRepo creates a new repo in the cluster
func (w *worker) createRepo(c *client.APIClient) error {
repoName := w.randString(10)
if err := c.CreateRepo(repoName); err != nil {
return err
}
w.repos = append(w.repos, &pfs.Repo{Name: repoName})
// Start the first commit in the repo (no parent). This is critical to
// advanceCommit(), which will try to finish a commit the first time it's
// called, and therefore must have an open commit to finish.
commit, err := c.StartCommit(repoName, "")
if err != nil {
return err
}
w.started = append(w.started, commit)
return nil
}
// advanceCommit either starts or finishes a commit, depending on the state of
// the cluster.
func (w *worker) advanceCommit(c *client.APIClient) error {
if len(w.started) >= maxStartedCommits || len(w.finished) == 0 {
// Randomly select a commit that has been started and finish it
if len(w.started) == 0 {
return nil
}
i := w.rand.Intn(len(w.started))
commit := w.started[i]
// Before we finish a commit, we add a file. This assures that there
// won't be any empty commits which will later crash jobs
if _, err := c.PutFile(commit.Repo.Name, commit.ID, w.randString(10), w.reader()); err != nil {
return err
}
if err := c.FinishCommit(commit.Repo.Name, commit.ID); err != nil {
return err
}
// remove commit[i] from 'started' and add it to 'finished'
w.started = append(w.started[:i], w.started[i+1:]...)
w.finished = append(w.finished, commit)
} else {
// Start a new commmit (parented off of a commit that we've finished)
commit := w.finished[w.rand.Intn(len(w.finished))]
commit, err := c.StartCommitParent(commit.Repo.Name, "", commit.ID)
if err != nil {
return err
}
w.started = append(w.started, commit)
}
return nil
}
// putFile puts a file with random contents into a random open commit (or exits
// early if there are none)
func (w *worker) putFile(c *client.APIClient) error {
if len(w.started) == 0 {
return nil
}
commit := w.started[w.rand.Intn(len(w.started))]
if _, err := c.PutFile(commit.Repo.Name, commit.ID, w.randString(10), w.reader()); err != nil {
return err
}
return nil
}
// advanceJob either waits for the first job in 'startedJobs' to finish (if its
// job queue is full) or starts a new job
func (w *worker) advanceJob(c *client.APIClient) error {
if len(w.startedJobs) >= maxStartedJobs {
// Wait for the first job in 'w.startedJobs' to finish.
// Pull first job off of 'startedJobs'
job := w.startedJobs[0]
copy(w.startedJobs, w.startedJobs[1:])
w.startedJobs = w.startedJobs[:len(w.startedJobs)-1]
// Wait for first started job to finish. Crash if the job failed
jobInfo, err := c.InspectJob(job.ID, true /* block until job finishes */)
if err != nil {
return err
}
if jobInfo.State != pps.JobState_JOB_SUCCESS {
return fmt.Errorf("job %s failed", job.ID)
}
w.startedJobs = append(w.startedJobs, job)
} else {
// Start a new job
// If there are no finished commits, there will be nothing for the new job
// to process. Just exit.
if len(w.finished) == 0 {
return nil
}
// Pick up to 5 finished commits from distinct repos: those are the job
// inputs. Store the repo names in 'inputs' to generate the 'grep' command
// that the job will run.
inputs := [5]string{}
var jobInputs []*pps.JobInput
repoSet := make(map[string]bool)
for i := range inputs {
commit := w.finished[w.rand.Intn(len(w.finished))]
if _, ok := repoSet[commit.Repo.Name]; ok {
// skip commit if we already have one from this repo (job will have
// one fewer inputs)
continue
}
repoSet[commit.Repo.Name] = true
inputs[i] = commit.Repo.Name
jobInputs = append(jobInputs, &pps.JobInput{Commit: commit})
}
outFilename := w.randString(10)
// Create a job to grep for a random string in the input files, and write
// the results to an output file
job, err := c.CreateJob(
"", // Image (use default)
[]string{"bash"},
w.grepCmd(inputs, outFilename),
&pps.ParallelismSpec{
Strategy: pps.ParallelismSpec_CONSTANT,
Constant: 1,
},
jobInputs,
0, // internal port
0, // external port
)
if err != nil {
return err
}
w.startedJobs = append(w.startedJobs, job)
}
return nil
}
func (w *worker) createPipeline(c *client.APIClient) error {
// If we haven't created any repos yet, then a new pipeline won't have
// anything to process. Just exit.
if len(w.repos) == 0 {
return nil
}
// Pick up to 5 distinct repos: those are the pipeline inputs. Store the repo
// names in 'inputs' to generate the 'grep' command that the pipeline's jobs
// will run.
inputs := [5]string{}
var pipelineInputs []*pps.PipelineInput
repoSet := make(map[string]bool)
for i := range inputs {
repo := w.repos[w.rand.Intn(len(w.repos))]
if _, ok := repoSet[repo.Name]; ok {
// skip commit if we already have one from this repo (job will have
// one fewer inputs)
continue
}
repoSet[repo.Name] = true
inputs[i] = repo.Name
pipelineInputs = append(pipelineInputs, &pps.PipelineInput{Repo: repo})
}
// Create a pipeline to grep for a random string in the input files, and write
// the results to an output file
pipelineName := w.randString(10)
outFilename := w.randString(10)
if err := c.CreatePipeline(
pipelineName,
"",
[]string{"bash"},
w.grepCmd(inputs, outFilename),
&pps.ParallelismSpec{
Strategy: pps.ParallelismSpec_CONSTANT,
Constant: 1,
},
pipelineInputs,
"master",
false,
); err != nil {
return err
}
w.pipelines = append(w.pipelines, client.NewPipeline(pipelineName))
return nil
}
const letters = "abcdefghijklmnopqrstuvwxyz"
const lettersAndSpaces = "abcdefghijklmnopqrstuvwxyz "
// RandString returns a random alphabetical string of size n
func RandString(r *rand.Rand, n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letters[r.Intn(len(letters))]
}
return string(b)
}
func (w *worker) randString(n int) string {
return RandString(w.rand, n)
}
func (w *worker) grepCmd(inputs [5]string, outFilename string) []string {
return []string{
fmt.Sprintf(
"grep %s /pfs/{%s,%s,%s,%s,%s}/* >/pfs/out/%s; true",
w.randString(4),
inputs[0],
inputs[1],
inputs[2],
inputs[3],
inputs[4],
outFilename,
),
}
}
func (w *worker) reader() io.Reader {
return NewReader(w.rand, 1000)
}
type reader struct {
rand *rand.Rand
bytes int64
bytesRead int64
}
// NewReader returns a Reader which generates strings of characters.
func NewReader(rand *rand.Rand, bytes int64) io.Reader {
return &reader{
rand: rand,
bytes: bytes,
}
}
func (r *reader) Read(p []byte) (int, error) {
var bytesReadThisTime int
for i := range p {
if r.bytesRead+int64(bytesReadThisTime) == r.bytes {
break
}
if i%128 == 127 {
p[i] = '\n'
} else {
p[i] = lettersAndSpaces[r.rand.Intn(len(lettersAndSpaces))]
}
bytesReadThisTime++
}
r.bytesRead += int64(bytesReadThisTime)
if r.bytesRead == r.bytes {
return bytesReadThisTime, io.EOF
}
return bytesReadThisTime, nil
}