forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workload.go
261 lines (245 loc) · 5.82 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
package workload
import (
"fmt"
"io"
"math/rand"
"golang.org/x/net/context"
"github.com/pachyderm/pachyderm/src/client/pfs"
pfsclient "github.com/pachyderm/pachyderm/src/client/pfs"
ppsclient "github.com/pachyderm/pachyderm/src/client/pps"
)
func RunWorkload(
pfsClient pfsclient.APIClient,
ppsClient ppsclient.APIClient,
rand *rand.Rand,
size int,
) error {
worker := newWorker(rand)
for i := 0; i < size; i++ {
if err := worker.work(pfsClient, ppsClient); err != nil {
return err
}
}
for _, job := range worker.startedJobs {
jobInfo, err := ppsClient.InspectJob(
context.Background(),
&ppsclient.InspectJobRequest{
Job: job,
BlockState: true,
},
)
if err != nil {
return err
}
if jobInfo.State != ppsclient.JobState_JOB_STATE_SUCCESS {
return fmt.Errorf("job %s failed", job.ID)
}
}
return nil
}
type worker struct {
repos []*pfs.Repo
finished []*pfs.Commit
started []*pfs.Commit
files []*pfs.File
startedJobs []*ppsclient.Job
jobs []*ppsclient.Job
pipelines []*ppsclient.Pipeline
rand *rand.Rand
}
func newWorker(rand *rand.Rand) *worker {
return &worker{
rand: rand,
}
}
const (
repo float64 = .02
commit = .1
file = .9
job = .98
pipeline = 1.0
)
const maxStartedCommits = 6
const maxStartedJobs = 6
func (w *worker) work(pfsClient pfsclient.APIClient, ppsClient ppsclient.APIClient) error {
opt := w.rand.Float64()
switch {
case opt < repo:
repoName := w.randString(10)
if err := pfsclient.CreateRepo(pfsClient, repoName); err != nil {
return err
}
w.repos = append(w.repos, &pfs.Repo{Name: repoName})
commit, err := pfsclient.StartCommit(pfsClient, repoName, "", "")
if err != nil {
return err
}
w.started = append(w.started, commit)
case opt < commit:
if len(w.started) >= maxStartedCommits || len(w.finished) == 0 {
if len(w.started) == 0 {
return nil
}
i := w.rand.Intn(len(w.started))
commit := w.started[i]
if err := pfsclient.FinishCommit(pfsClient, commit.Repo.Name, commit.ID); err != nil {
return err
}
w.started = append(w.started[:i], w.started[i+1:]...)
w.finished = append(w.finished, commit)
} else {
if len(w.finished) == 0 {
return nil
}
commit := w.finished[w.rand.Intn(len(w.finished))]
commit, err := pfsclient.StartCommit(pfsClient, commit.Repo.Name, commit.ID, "")
if err != nil {
return err
}
w.started = append(w.started, commit)
}
case opt < file:
if len(w.started) == 0 {
return nil
}
commit := w.started[w.rand.Intn(len(w.started))]
if _, err := pfsclient.PutFile(pfsClient, commit.Repo.Name, commit.ID, w.randString(10), w.reader()); err != nil {
return err
}
case opt < job:
if len(w.startedJobs) >= maxStartedJobs {
job := w.startedJobs[0]
w.startedJobs = w.startedJobs[1:]
jobInfo, err := ppsClient.InspectJob(
context.Background(),
&ppsclient.InspectJobRequest{
Job: job,
BlockState: true,
},
)
if err != nil {
return err
}
if jobInfo.State != ppsclient.JobState_JOB_STATE_SUCCESS {
return fmt.Errorf("job %s failed", job.ID)
}
w.jobs = append(w.jobs, job)
} else {
if len(w.finished) == 0 {
return nil
}
inputs := [5]string{}
var jobInputs []*ppsclient.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 {
continue
}
repoSet[commit.Repo.Name] = true
inputs[i] = commit.Repo.Name
jobInputs = append(jobInputs, &ppsclient.JobInput{Commit: commit})
}
var parentJobID string
if len(w.jobs) > 0 {
parentJobID = w.jobs[w.rand.Intn(len(w.jobs))].ID
}
outFilename := w.randString(10)
job, err := ppsclient.CreateJob(
ppsClient,
"",
[]string{"bash"},
w.grepCmd(inputs, outFilename),
1,
jobInputs,
parentJobID,
)
if err != nil {
return err
}
w.startedJobs = append(w.startedJobs, job)
}
case opt < pipeline:
if len(w.repos) == 0 {
return nil
}
inputs := [5]string{}
var pipelineInputs []*ppsclient.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 {
continue
}
repoSet[repo.Name] = true
inputs[i] = repo.Name
pipelineInputs = append(pipelineInputs, &ppsclient.PipelineInput{Repo: repo})
}
pipelineName := w.randString(10)
outFilename := w.randString(10)
if err := ppsclient.CreatePipeline(
ppsClient,
pipelineName,
"",
[]string{"bash"},
w.grepCmd(inputs, outFilename),
1,
pipelineInputs,
); err != nil {
return err
}
w.pipelines = append(w.pipelines, ppsclient.NewPipeline(pipelineName))
}
return nil
}
const letters = "abcdefghijklmnopqrstuvwxyz"
const lettersAndSpaces = "abcdefghijklmnopqrstuvwxyz "
func (w *worker) randString(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letters[w.rand.Intn(len(letters))]
}
return string(b)
}
type reader struct {
rand *rand.Rand
bytes int
}
func NewReader(rand *rand.Rand, bytes int) io.Reader {
return &reader{
rand: rand,
bytes: bytes,
}
}
func (r *reader) Read(p []byte) (int, error) {
for i := range p {
if i%128 == 127 {
p[i] = '\n'
} else {
p[i] = lettersAndSpaces[r.rand.Intn(len(lettersAndSpaces))]
}
}
p[len(p)-1] = '\n'
r.bytes -= len(p)
if r.bytes <= 0 {
return len(p), io.EOF
}
return len(p), nil
}
func (w *worker) reader() io.Reader {
return NewReader(w.rand, 1000)
}
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,
),
}
}