-
Notifications
You must be signed in to change notification settings - Fork 567
/
pipeline.go
147 lines (139 loc) · 4.25 KB
/
pipeline.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
package ppsload
import (
"bytes"
"fmt"
"strings"
"github.com/gogo/protobuf/proto"
"github.com/pachyderm/pachyderm/v2/src/client"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/pachyderm/pachyderm/v2/src/internal/uuid"
"github.com/pachyderm/pachyderm/v2/src/pfs"
"github.com/pachyderm/pachyderm/v2/src/pps"
)
func Pipeline(pachClient *client.APIClient, req *pps.RunLoadTestRequest) (*pps.RunLoadTestResponse, error) {
var branch *pfs.Branch
var stateID string
if req.StateId == "" {
var err error
branch, err = createPipelines(pachClient, req.DagSpec, req.Parallelism, req.PodPatch)
if err != nil {
return nil, err
}
} else {
state, err := deserializeState(pachClient, req.StateId)
if err != nil {
return nil, err
}
branch = state.Branch
stateID = state.PfsStateId
}
resp, err := pachClient.PfsAPIClient.RunLoadTest(pachClient.Ctx(), &pfs.RunLoadTestRequest{
Spec: req.LoadSpec,
Branch: branch,
Seed: req.Seed,
StateId: stateID,
})
if err != nil {
return nil, errors.EnsureStack(err)
}
stateID, err = serializeState(pachClient, &State{
Branch: branch,
PfsStateId: resp.StateId,
})
if err != nil {
return nil, err
}
return &pps.RunLoadTestResponse{
Error: resp.Error,
StateId: stateID,
}, nil
}
func createPipelines(pachClient *client.APIClient, spec string, parallelism int64, podPatch string) (*pfs.Branch, error) {
namespace := "-" + uuid.NewWithoutDashes()[:5]
var retBranch *pfs.Branch
for _, pipelineStr := range strings.Split(spec, "\n") {
if strings.TrimSpace(pipelineStr) == "" {
continue
}
split := strings.Split(pipelineStr, ":")
repoName := strings.TrimSpace(split[0]) + namespace
// Create source repos.
if strings.TrimSpace(split[1]) == "" {
if err := pachClient.CreateProjectRepo(pfs.DefaultProjectName, repoName); err != nil {
return nil, err
}
// First source repo will be the target of the PFS load test.
if retBranch == nil {
retBranch = client.NewProjectBranch(pfs.DefaultProjectName, repoName, "master")
}
continue
}
// Create pipelines.
inputs := strings.Split(split[1], ",")
for i := range inputs {
inputs[i] = strings.TrimSpace(inputs[i]) + namespace
}
// TODO(CORE-1063): plumb through project somehow
repo := &pfs.Repo{Project: &pfs.Project{Name: pfs.DefaultProjectName}, Name: repoName}
if err := createPipeline(pachClient, repo, inputs, parallelism, podPatch); err != nil {
return nil, err
}
}
return retBranch, nil
}
func createPipeline(pachClient *client.APIClient, repo *pfs.Repo, inputRepos []string, parallelism int64, podPatch string) error {
var inputs []*pps.Input
for i, inputRepo := range inputRepos {
inputs = append(inputs, &pps.Input{
Pfs: &pps.PFSInput{
Name: fmt.Sprint("input-", i),
Project: pfs.DefaultProjectName,
Repo: inputRepo,
Branch: "master",
Glob: "/(*)",
JoinOn: "$1",
OuterJoin: true,
},
})
}
_, err := pachClient.PpsAPIClient.CreatePipeline(
pachClient.Ctx(),
&pps.CreatePipelineRequest{
Pipeline: &pps.Pipeline{Project: repo.Project, Name: repo.Name},
Transform: &pps.Transform{
Cmd: []string{"bash"},
Stdin: []string{"cp -r /pfs/input-*/* /pfs/out/"},
},
ParallelismSpec: &pps.ParallelismSpec{Constant: uint64(parallelism)},
Input: &pps.Input{Join: inputs},
PodPatch: podPatch,
},
)
return errors.EnsureStack(err)
}
const stateFileName = "state"
func serializeState(pachClient *client.APIClient, state *State) (string, error) {
resp, err := pachClient.WithCreateFileSetClient(func(mf client.ModifyFile) error {
data, err := proto.Marshal(state)
if err != nil {
return errors.EnsureStack(err)
}
return errors.EnsureStack(mf.PutFile(stateFileName, bytes.NewReader(data)))
})
if err != nil {
return "", err
}
return resp.FileSetId, nil
}
func deserializeState(pachClient *client.APIClient, stateID string) (*State, error) {
commit := client.NewProjectRepo(pfs.DefaultProjectName, client.FileSetsRepoName).NewCommit("", stateID)
buf := &bytes.Buffer{}
if err := pachClient.GetFile(commit, stateFileName, buf); err != nil {
return nil, err
}
state := &State{}
if err := proto.Unmarshal(buf.Bytes(), state); err != nil {
return nil, errors.EnsureStack(err)
}
return state, nil
}