-
Notifications
You must be signed in to change notification settings - Fork 568
/
util.go
197 lines (182 loc) · 5.42 KB
/
util.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
package pps
import (
"fmt"
"sort"
"strings"
"github.com/pachyderm/pachyderm/v2/src/pfs"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/pachyderm/pachyderm/v2/src/internal/errutil"
)
var (
// format strings for state name parsing errors
errInvalidJobStateName string
errInvalidPipelineStateName string
)
func init() {
// construct error messages from all current job and pipeline state names
var states []string
for i := int32(0); JobState_name[i] != ""; i++ {
states = append(states, strings.ToLower(strings.TrimPrefix(JobState_name[i], "JOB_")))
}
errInvalidJobStateName = fmt.Sprintf("state %%s must be one of %s, or %s, etc", strings.Join(states, ", "), JobState_name[0])
states = states[:0]
for i := int32(0); PipelineState_name[i] != ""; i++ {
states = append(states, strings.ToLower(strings.TrimPrefix(PipelineState_name[i], "PIPELINE_")))
}
errInvalidPipelineStateName = fmt.Sprintf("state %%s must be one of %s, or %s, etc", strings.Join(states, ", "), PipelineState_name[0])
}
// VisitInput visits each input recursively in ascending order (root last). It cannot return an
// error unless f returns an error.
func VisitInput(input *Input, f func(*Input) error) error {
err := visitInput(input, f)
if err != nil && errors.Is(err, errutil.ErrBreak) {
return nil
}
return err
}
func visitInput(input *Input, f func(*Input) error) error {
var source []*Input
switch {
case input == nil:
return nil // Spouts may have nil input
case input.Cross != nil:
source = input.Cross
case input.Join != nil:
source = input.Join
case input.Group != nil:
source = input.Group
case input.Union != nil:
source = input.Union
}
for _, input := range source {
if err := visitInput(input, f); err != nil {
return err
}
}
return f(input)
}
// InputName computes the name of an Input.
func InputName(input *Input) string {
switch {
case input == nil:
return ""
case input.Pfs != nil:
return input.Pfs.Name
case input.Cross != nil:
if len(input.Cross) > 0 {
return InputName(input.Cross[0])
}
case input.Join != nil:
if len(input.Join) > 0 {
return InputName(input.Join[0])
}
case input.Group != nil:
if len(input.Group) > 0 {
return InputName(input.Group[0])
}
case input.Union != nil:
if len(input.Union) > 0 {
return InputName(input.Union[0])
}
}
return ""
}
// SortInput sorts an Input.
func SortInput(input *Input) {
VisitInput(input, func(input *Input) error { //nolint:errcheck
SortInputs := func(inputs []*Input) {
sort.SliceStable(inputs, func(i, j int) bool { return InputName(inputs[i]) < InputName(inputs[j]) })
}
switch {
case input.Cross != nil:
SortInputs(input.Cross)
case input.Join != nil:
SortInputs(input.Join)
case input.Group != nil:
SortInputs(input.Group)
case input.Union != nil:
SortInputs(input.Union)
}
return nil
})
}
// InputBranches returns the branches in an Input.
//
// Deprecated: Use ProjectInputBranches instead.
func InputBranches(input *Input) []*pfs.Branch {
return ProjectInputBranches(pfs.DefaultProjectName, input)
}
func ProjectInputBranches(projectName string, input *Input) []*pfs.Branch {
var result []*pfs.Branch
VisitInput(input, func(input *Input) error { //nolint:errcheck
if input.Pfs != nil {
b := &pfs.Branch{
Repo: &pfs.Repo{
Project: &pfs.Project{Name: input.Pfs.Project},
Name: input.Pfs.Repo,
Type: input.Pfs.RepoType,
},
Name: input.Pfs.Branch,
}
if input.Pfs.Project == "" {
b.Repo.Project.Name = projectName
}
result = append(result, b)
}
if input.Cron != nil {
b := &pfs.Branch{
Repo: &pfs.Repo{
Project: &pfs.Project{Name: input.Cron.Project},
Name: input.Cron.Repo,
Type: pfs.UserRepoType,
},
Name: "master",
}
if input.Cron.Project == "" {
b.Repo.Project.Name = projectName
}
result = append(result, b)
}
return nil
})
return result
}
// JobStateFromName attempts to interpret a string as a
// JobState, accepting either the enum names or the pretty printed state
// names
func JobStateFromName(name string) (JobState, error) {
canonical := "JOB_" + strings.TrimPrefix(strings.ToUpper(name), "JOB_")
if value, ok := JobState_value[canonical]; ok {
return JobState(value), nil
}
return 0, errors.Errorf(errInvalidJobStateName, name)
}
// PipelineStateFromName attempts to interpret a string as a PipelineState,
// accepting either the enum names or the pretty printed state names
func PipelineStateFromName(name string) (PipelineState, error) {
canonical := "PIPELINE_" + strings.TrimPrefix(strings.ToUpper(name), "PIPELINE_")
if value, ok := PipelineState_value[canonical]; ok {
return PipelineState(value), nil
}
return 0, errors.Errorf(errInvalidPipelineStateName, name)
}
// IsTerminal returns 'true' if 'state' indicates that the job is done (i.e.
// the state will not change later: SUCCESS, FAILURE, KILLED) and 'false'
// otherwise.
func IsTerminal(state JobState) bool {
switch state {
case JobState_JOB_SUCCESS, JobState_JOB_FAILURE, JobState_JOB_KILLED, JobState_JOB_UNRUNNABLE:
return true
case JobState_JOB_CREATED, JobState_JOB_STARTING, JobState_JOB_RUNNING, JobState_JOB_EGRESSING, JobState_JOB_FINISHING:
return false
default:
panic(fmt.Sprintf("unrecognized job state: %s", state))
}
}
// RepoPipeline returns a Pipeline with the same project and name as the repo.
func RepoPipeline(r *pfs.Repo) *Pipeline {
return &Pipeline{
Project: r.Project,
Name: r.Name,
}
}