forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
330 lines (301 loc) · 11.6 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
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
// Package ppsutil contains utilities for various PPS-related tasks, which are
// shared by both the PPS API and the worker binary. These utilities include:
// - Getting the RC name and querying k8s reguarding pipelines
// - Reading and writing pipeline resource requests and limits
// - Reading and writing EtcdPipelineInfos and PipelineInfos[1]
//
// [1] Note that PipelineInfo in particular is complicated because it contains
// fields that are not always set or are stored in multiple places
// ('job_state', for example, is not stored in PFS along with the rest of each
// PipelineInfo, because this field is volatile and we cannot commit to PFS
// every time it changes. 'job_counts' is the same, and 'reason' is in etcd
// because it is only updated alongside 'job_state'). As of 12/7/2017, these
// are the only fields not stored in PFS.
package ppsutil
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"net/http"
"net/url"
"os"
"path"
"strings"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
"github.com/pachyderm/pachyderm/src/client"
"github.com/pachyderm/pachyderm/src/client/pfs"
"github.com/pachyderm/pachyderm/src/client/pps"
ppsclient "github.com/pachyderm/pachyderm/src/client/pps"
col "github.com/pachyderm/pachyderm/src/server/pkg/collection"
"github.com/pachyderm/pachyderm/src/server/pkg/ppsconsts"
etcd "github.com/coreos/etcd/clientv3"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kube "k8s.io/client-go/kubernetes"
)
// PipelineRepo creates a pfs repo for a given pipeline.
func PipelineRepo(pipeline *ppsclient.Pipeline) *pfs.Repo {
return &pfs.Repo{Name: pipeline.Name}
}
// PipelineRcName generates the name of the k8s replication controller that
// manages a pipeline's workers
func PipelineRcName(name string, version uint64) string {
// k8s won't allow RC names that contain upper-case letters
// or underscores
// TODO: deal with name collision
name = strings.Replace(name, "_", "-", -1)
return fmt.Sprintf("pipeline-%s-v%d", strings.ToLower(name), version)
}
// GetRequestsResourceListFromPipeline returns a list of resources that the pipeline,
// minimally requires.
func GetRequestsResourceListFromPipeline(pipelineInfo *pps.PipelineInfo) (*v1.ResourceList, error) {
return getResourceListFromSpec(pipelineInfo.ResourceRequests, pipelineInfo.CacheSize)
}
func getResourceListFromSpec(resources *pps.ResourceSpec, cacheSize string) (*v1.ResourceList, error) {
var result v1.ResourceList = make(map[v1.ResourceName]resource.Quantity)
cpuStr := fmt.Sprintf("%f", resources.Cpu)
cpuQuantity, err := resource.ParseQuantity(cpuStr)
if err != nil {
log.Warnf("error parsing cpu string: %s: %+v", cpuStr, err)
} else {
result[v1.ResourceCPU] = cpuQuantity
}
memQuantity, err := resource.ParseQuantity(resources.Memory)
if err != nil {
log.Warnf("error parsing memory string: %s: %+v", resources.Memory, err)
} else {
result[v1.ResourceMemory] = memQuantity
}
// Here we are sanity checking. A pipeline should request at least
// as much memory as it needs for caching.
cacheQuantity, err := resource.ParseQuantity(cacheSize)
if err != nil {
log.Warnf("error parsing cache string: %s: %+v", cacheSize, err)
} else if cacheQuantity.Cmp(memQuantity) > 0 {
result[v1.ResourceMemory] = cacheQuantity
}
if resources.Gpu != 0 {
gpuStr := fmt.Sprintf("%d", resources.Gpu)
gpuQuantity, err := resource.ParseQuantity(gpuStr)
if err != nil {
log.Warnf("error parsing gpu string: %s: %+v", gpuStr, err)
} else {
result[v1.ResourceNvidiaGPU] = gpuQuantity
}
}
return &result, nil
}
// GetLimitsResourceListFromPipeline returns a list of resources that the pipeline,
// maximally is limited to.
func GetLimitsResourceListFromPipeline(pipelineInfo *pps.PipelineInfo) (*v1.ResourceList, error) {
return getResourceListFromSpec(pipelineInfo.ResourceLimits, pipelineInfo.CacheSize)
}
// getNumNodes attempts to retrieve the number of nodes in the current k8s
// cluster
func getNumNodes(kubeClient *kube.Clientset) (int, error) {
nodeList, err := kubeClient.CoreV1().Nodes().List(metav1.ListOptions{})
if err != nil {
return 0, fmt.Errorf("unable to retrieve node list from k8s to determine parallelism: %v", err)
}
if len(nodeList.Items) == 0 {
return 0, fmt.Errorf("pachyderm.pps.jobserver: no k8s nodes found")
}
return len(nodeList.Items), nil
}
// GetExpectedNumWorkers computes the expected number of workers that
// pachyderm will start given the ParallelismSpec 'spec'.
//
// This is only exported for testing
func GetExpectedNumWorkers(kubeClient *kube.Clientset, spec *ppsclient.ParallelismSpec) (int, error) {
if spec == nil || (spec.Constant == 0 && spec.Coefficient == 0) {
return 1, nil
} else if spec.Constant > 0 && spec.Coefficient == 0 {
return int(spec.Constant), nil
} else if spec.Constant == 0 && spec.Coefficient > 0 {
// Start ('coefficient' * 'nodes') workers. Determine number of workers
numNodes, err := getNumNodes(kubeClient)
if err != nil {
return 0, err
}
result := math.Floor(spec.Coefficient * float64(numNodes))
return int(math.Max(result, 1)), nil
}
return 0, fmt.Errorf("Unable to interpret ParallelismSpec %+v", spec)
}
// GetPipelineInfo retrieves and returns a valid PipelineInfo from PFS. It does
// the PFS read/unmarshalling of bytes as well as filling in missing fields
func GetPipelineInfo(pachClient *client.APIClient, ptr *pps.EtcdPipelineInfo) (*pps.PipelineInfo, error) {
buf := bytes.Buffer{}
if err := pachClient.GetFile(ppsconsts.SpecRepo, ptr.SpecCommit.ID, ppsconsts.SpecFile, 0, 0, &buf); err != nil {
return nil, fmt.Errorf("could not read existing PipelineInfo from PFS: %v", err)
}
result := &pps.PipelineInfo{}
if err := result.Unmarshal(buf.Bytes()); err != nil {
return nil, fmt.Errorf("could not unmarshal PipelineInfo bytes from PFS: %v", err)
}
result.State = ptr.State
result.Reason = ptr.Reason
result.JobCounts = ptr.JobCounts
result.SpecCommit = ptr.SpecCommit
return result, nil
}
// FailPipeline updates the pipeline's state to failed and sets the failure reason
func FailPipeline(ctx context.Context, etcdClient *etcd.Client, pipelinesCollection col.Collection, pipelineName string, reason string) error {
_, err := col.NewSTM(ctx, etcdClient, func(stm col.STM) error {
pipelines := pipelinesCollection.ReadWrite(stm)
pipelinePtr := new(pps.EtcdPipelineInfo)
if err := pipelines.Get(pipelineName, pipelinePtr); err != nil {
return err
}
pipelinePtr.State = pps.PipelineState_PIPELINE_FAILURE
pipelinePtr.Reason = reason
pipelines.Put(pipelineName, pipelinePtr)
return nil
})
return err
}
// JobInput fills in the commits for a JobInfo
func JobInput(pipelineInfo *pps.PipelineInfo, outputCommitInfo *pfs.CommitInfo) *pps.Input {
// branchToCommit maps strings of the form "<repo>/<branch>" to PFS commits
branchToCommit := make(map[string]*pfs.Commit)
key := path.Join
for i, provCommit := range outputCommitInfo.Provenance {
branchToCommit[key(provCommit.Repo.Name, outputCommitInfo.BranchProvenance[i].Name)] = provCommit
}
jobInput := proto.Clone(pipelineInfo.Input).(*pps.Input)
pps.VisitInput(jobInput, func(input *pps.Input) {
if input.Atom != nil {
if commit, ok := branchToCommit[key(input.Atom.Repo, input.Atom.Branch)]; ok {
input.Atom.Commit = commit.ID
}
}
if input.Cron != nil {
if commit, ok := branchToCommit[key(input.Cron.Repo, "master")]; ok {
input.Cron.Commit = commit.ID
}
}
if input.Git != nil {
if commit, ok := branchToCommit[key(input.Git.Name, input.Git.Branch)]; ok {
input.Git.Commit = commit.ID
}
}
})
return jobInput
}
// PipelineReqFromInfo converts a PipelineInfo into a CreatePipelineRequest.
func PipelineReqFromInfo(pipelineInfo *ppsclient.PipelineInfo) *ppsclient.CreatePipelineRequest {
return &ppsclient.CreatePipelineRequest{
Pipeline: pipelineInfo.Pipeline,
Transform: pipelineInfo.Transform,
ParallelismSpec: pipelineInfo.ParallelismSpec,
Egress: pipelineInfo.Egress,
OutputBranch: pipelineInfo.OutputBranch,
ScaleDownThreshold: pipelineInfo.ScaleDownThreshold,
ResourceRequests: pipelineInfo.ResourceRequests,
ResourceLimits: pipelineInfo.ResourceLimits,
Input: pipelineInfo.Input,
Description: pipelineInfo.Description,
Incremental: pipelineInfo.Incremental,
CacheSize: pipelineInfo.CacheSize,
EnableStats: pipelineInfo.EnableStats,
Batch: pipelineInfo.Batch,
MaxQueueSize: pipelineInfo.MaxQueueSize,
Service: pipelineInfo.Service,
ChunkSpec: pipelineInfo.ChunkSpec,
DatumTimeout: pipelineInfo.DatumTimeout,
JobTimeout: pipelineInfo.JobTimeout,
Salt: pipelineInfo.Salt,
}
}
// PipelineManifestReader helps with unmarshalling pipeline configs from JSON. It's used by
// create-pipeline and update-pipeline
type PipelineManifestReader struct {
buf bytes.Buffer
decoder *json.Decoder
}
// NewPipelineManifestReader creates a new manifest reader from a path.
func NewPipelineManifestReader(path string) (result *PipelineManifestReader, retErr error) {
result = &PipelineManifestReader{}
var pipelineReader io.Reader
if path == "-" {
pipelineReader = io.TeeReader(os.Stdin, &result.buf)
fmt.Print("Reading from stdin.\n")
} else if url, err := url.Parse(path); err == nil && url.Scheme != "" {
resp, err := http.Get(url.String())
if err != nil {
return nil, err
}
defer func() {
if err := resp.Body.Close(); err != nil && retErr == nil {
retErr = err
}
}()
rawBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
pipelineReader = io.TeeReader(strings.NewReader(string(rawBytes)), &result.buf)
} else {
rawBytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
pipelineReader = io.TeeReader(strings.NewReader(string(rawBytes)), &result.buf)
}
result.decoder = json.NewDecoder(pipelineReader)
return result, nil
}
// NextCreatePipelineRequest gets the next request from the manifest reader.
func (r *PipelineManifestReader) NextCreatePipelineRequest() (*ppsclient.CreatePipelineRequest, error) {
var result ppsclient.CreatePipelineRequest
if err := jsonpb.UnmarshalNext(r.decoder, &result); err != nil {
if err == io.EOF {
return nil, err
}
return nil, fmt.Errorf("malformed pipeline spec: %s", err)
}
return &result, nil
}
// DescribeSyntaxError describes a syntax error encountered parsing json.
func DescribeSyntaxError(originalErr error, parsedBuffer bytes.Buffer) error {
sErr, ok := originalErr.(*json.SyntaxError)
if !ok {
return originalErr
}
buffer := make([]byte, sErr.Offset)
parsedBuffer.Read(buffer)
lineOffset := strings.LastIndex(string(buffer[:len(buffer)-1]), "\n")
if lineOffset == -1 {
lineOffset = 0
}
lines := strings.Split(string(buffer[:len(buffer)-1]), "\n")
lineNumber := len(lines)
descriptiveErrorString := fmt.Sprintf("Syntax Error on line %v:\n%v\n%v^\n%v\n",
lineNumber,
string(buffer[lineOffset:]),
strings.Repeat(" ", int(sErr.Offset)-2-lineOffset),
originalErr,
)
return errors.New(descriptiveErrorString)
}
// 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 pps.JobState) bool {
switch state {
case pps.JobState_JOB_SUCCESS, pps.JobState_JOB_FAILURE, pps.JobState_JOB_KILLED:
return true
case pps.JobState_JOB_STARTING, pps.JobState_JOB_RUNNING:
return false
default:
panic(fmt.Sprintf("unrecognized job state: %s", state))
}
}