forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
master.go
990 lines (931 loc) · 29.4 KB
/
master.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
package worker
import (
"bytes"
"context"
"fmt"
"log"
"os"
"path"
"strings"
"sync"
"time"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/types"
"github.com/montanaflynn/stats"
"golang.org/x/sync/errgroup"
"k8s.io/kubernetes/pkg/api"
"github.com/pachyderm/pachyderm/src/client"
"github.com/pachyderm/pachyderm/src/client/limit"
"github.com/pachyderm/pachyderm/src/client/pfs"
"github.com/pachyderm/pachyderm/src/client/pps"
"github.com/pachyderm/pachyderm/src/server/pkg/backoff"
col "github.com/pachyderm/pachyderm/src/server/pkg/collection"
"github.com/pachyderm/pachyderm/src/server/pkg/dlock"
"github.com/pachyderm/pachyderm/src/server/pkg/hashtree"
"github.com/pachyderm/pachyderm/src/server/pkg/obj"
"github.com/pachyderm/pachyderm/src/server/pkg/pool"
"github.com/pachyderm/pachyderm/src/server/pkg/ppsdb"
pfs_sync "github.com/pachyderm/pachyderm/src/server/pkg/sync"
"github.com/pachyderm/pachyderm/src/server/pkg/util"
ppsserver "github.com/pachyderm/pachyderm/src/server/pps"
"google.golang.org/grpc"
)
const (
// maximumRetriesPerDatum is the maximum number of times each datum
// can failed to be processed before we declare that the job has failed.
maximumRetriesPerDatum = 3
masterLockPath = "_master_worker_lock"
// The number of datums that will be enqueued on each worker.
queueSize = 10
// The number of datums the master caches
numCachedDatums = 1000000
)
func (a *APIServer) getMasterLogger() *taggedLogger {
result := &taggedLogger{
template: a.logMsgTemplate, // Copy struct
stderrLog: log.Logger{},
marshaler: &jsonpb.Marshaler{},
}
result.stderrLog.SetOutput(os.Stderr)
result.stderrLog.SetFlags(log.LstdFlags | log.Llongfile) // Log file/line
result.template.Master = true
return result
}
func (logger *taggedLogger) jobLogger(jobID string) *taggedLogger {
result := logger.clone()
result.template.JobID = jobID
return result
}
func (a *APIServer) master() {
masterLock := dlock.NewDLock(a.etcdClient, path.Join(a.etcdPrefix, masterLockPath, a.pipelineInfo.Pipeline.Name, a.pipelineInfo.Salt))
logger := a.getMasterLogger()
b := backoff.NewInfiniteBackOff()
// Setting a high backoff so that when this master fails, the other
// workers are more likely to become the master.
// Also, we've observed race conditions where StopPipeline would cause
// a master to restart before it's deleted. PPS would then get confused
// by the restart and create the workers again, because the restart would
// bring the pipeline state from PAUSED to RUNNING. By setting a high
// retry interval, the master would be deleted before it gets a chance
// to restart.
b.InitialInterval = 5 * time.Second
backoff.RetryNotify(func() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx, err := masterLock.Lock(ctx)
if err != nil {
return err
}
defer masterLock.Unlock(ctx)
logger.Logf("Launching worker master process")
// Set pipeline state to running
_, err = col.NewSTM(ctx, a.etcdClient, func(stm col.STM) error {
pipelineName := a.pipelineInfo.Pipeline.Name
pipelines := a.pipelines.ReadWrite(stm)
pipelineInfo := new(pps.PipelineInfo)
if err := pipelines.Get(pipelineName, pipelineInfo); err != nil {
return err
}
pipelineInfo.State = pps.PipelineState_PIPELINE_RUNNING
pipelines.Put(pipelineName, pipelineInfo)
return nil
})
return a.jobSpawner(ctx, logger)
}, b, func(err error, d time.Duration) error {
logger.Logf("master: error running the master process: %v; retrying in %v", err, d)
return nil
})
}
// jobSpawner spawns jobs
func (a *APIServer) jobSpawner(ctx context.Context, logger *taggedLogger) error {
// Establish connection pool
pool, err := pool.NewPool(a.kubeClient, a.namespace, ppsserver.PipelineRcName(a.pipelineInfo.Pipeline.Name, a.pipelineInfo.Version), client.PachDialOptions()...)
if err != nil {
return fmt.Errorf("master: error constructing worker pool: %v; retrying in %v", err)
}
defer func() {
if err := pool.Close(); err != nil {
logger.Logf("error closing pool: %v", err)
}
}()
bsf, err := a.newBranchSetFactory(ctx)
if err != nil {
return fmt.Errorf("error constructing branch set factory: %v", err)
}
defer bsf.Close()
nextInput:
for {
// scaleDownCh is closed after we have not received a job for
// a certain amount of time specified by ScaleDownThreshold.
scaleDownCh := make(chan struct{})
if a.pipelineInfo.ScaleDownThreshold != nil {
scaleDownThreshold, err := types.DurationFromProto(a.pipelineInfo.ScaleDownThreshold)
if err != nil {
logger.Logf("error converting scaleDownThreshold: %v", err)
} else {
time.AfterFunc(scaleDownThreshold, func() {
close(scaleDownCh)
})
}
}
var bs *branchSet
select {
case <-ctx.Done():
return context.Canceled
case bs = <-bsf.Chan():
if bs.Err != nil {
return fmt.Errorf("error from branch set factory: %v", bs.Err)
}
case <-scaleDownCh:
if err := a.scaleDownWorkers(); err != nil {
logger.Logf("error scaling down workers: %v", err)
}
continue nextInput
}
// Once we received a job, scale up the workers
if a.pipelineInfo.ScaleDownThreshold != nil {
if err := a.scaleUpWorkers(); err != nil {
logger.Logf("error scaling up workers: %v", err)
}
}
// (create JobInput for new processing job)
jobInput := proto.Clone(a.pipelineInfo.Input).(*pps.Input)
var visitErr error
pps.VisitInput(jobInput, func(input *pps.Input) {
if input.Atom != nil {
for _, branch := range bs.Branches {
if input.Atom.Repo == branch.Head.Repo.Name && input.Atom.Branch == branch.Name {
input.Atom.Commit = branch.Head.ID
}
}
if input.Atom.Commit == "" {
visitErr = fmt.Errorf("didn't find input commit for %s/%s", input.Atom.Repo, input.Atom.Branch)
}
input.Atom.FromCommit = ""
}
if input.Cron != nil {
for _, branch := range bs.Branches {
if input.Cron.Repo == branch.Head.Repo.Name {
input.Cron.Commit = branch.Head.ID
}
}
}
})
if visitErr != nil {
return visitErr
}
jobsRO := a.jobs.ReadOnly(ctx)
// Check if this input set has already been processed
jobIter, err := jobsRO.GetByIndex(ppsdb.JobsInputIndex, jobInput)
if err != nil {
return err
}
// This is doing the same thing as the line above but for 1.4.5 style jobs
oldJobIter, err := jobsRO.GetByIndex(ppsdb.JobsInputsIndex, untranslateJobInputs(jobInput))
if err != nil {
return err
}
// Check if any of the jobs in jobIter have been run already. If so, skip
// this input.
for {
var jobID string
var jobInfo pps.JobInfo
ok, err := jobIter.Next(&jobID, &jobInfo)
if err != nil {
return err
}
if !ok {
ok, err := oldJobIter.Next(&jobID, &jobInfo)
if err != nil {
return err
}
if !ok {
break
}
}
if jobInfo.Pipeline.Name == a.pipelineInfo.Pipeline.Name &&
(jobInfo.Salt == a.pipelineInfo.Salt || (jobInfo.Salt == "" && jobInfo.PipelineVersion == a.pipelineInfo.Version)) {
switch jobInfo.State {
case pps.JobState_JOB_STARTING, pps.JobState_JOB_RUNNING:
if err := a.runJob(ctx, &jobInfo, pool, logger); err != nil {
return err
}
}
continue nextInput
}
}
// now we need to find the parentJob for this job. The parent job
// is defined as the job with the same input commits except for the
// newest input commit which triggered this job. In place of it we
// use that commit's parent.
newBranch := bs.Branches[bs.NewBranch]
newCommitInfo, err := a.pachClient.PfsAPIClient.InspectCommit(ctx, &pfs.InspectCommitRequest{
Commit: newBranch.Head,
})
if err != nil {
return err
}
var parentJob *pps.Job
if newCommitInfo.ParentCommit != nil {
// recreate our parent's inputs so we can look it up in etcd
parentJobInput := proto.Clone(jobInput).(*pps.Input)
pps.VisitInput(parentJobInput, func(input *pps.Input) {
if input.Atom != nil && input.Atom.Repo == newBranch.Head.Repo.Name && input.Atom.Branch == newBranch.Name {
input.Atom.Commit = newCommitInfo.ParentCommit.ID
}
})
if visitErr != nil {
return visitErr
}
jobIter, err := jobsRO.GetByIndex(ppsdb.JobsInputIndex, parentJobInput)
if err != nil {
return err
}
for {
var jobID string
var jobInfo pps.JobInfo
ok, err := jobIter.Next(&jobID, &jobInfo)
if err != nil {
return err
}
if !ok {
break
}
if jobInfo.Pipeline.Name == a.pipelineInfo.Pipeline.Name &&
(jobInfo.Salt == a.pipelineInfo.Salt || (jobInfo.Salt == "" && jobInfo.PipelineVersion == a.pipelineInfo.Version)) {
parentJob = jobInfo.Job
}
}
}
job, err := a.pachClient.PpsAPIClient.CreateJob(ctx, &pps.CreateJobRequest{
Pipeline: a.pipelineInfo.Pipeline,
Input: jobInput,
ParentJob: parentJob,
NewBranch: newBranch,
Salt: a.pipelineInfo.Salt,
PipelineVersion: a.pipelineInfo.Version,
EnableStats: a.pipelineInfo.EnableStats,
Batch: a.pipelineInfo.Batch,
})
if err != nil {
return err
}
jobInfo, err := a.pachClient.PpsAPIClient.InspectJob(ctx, &pps.InspectJobRequest{
Job: job,
})
if err != nil {
return err
}
if err := a.runJob(ctx, jobInfo, pool, logger.jobLogger(job.ID)); err != nil {
return err
}
}
}
func plusDuration(x *types.Duration, y *types.Duration) (*types.Duration, error) {
var xd time.Duration
var yd time.Duration
var err error
if x != nil {
xd, err = types.DurationFromProto(x)
if err != nil {
return nil, err
}
}
if y != nil {
yd, err = types.DurationFromProto(y)
if err != nil {
return nil, err
}
}
return types.DurationProto(xd + yd), nil
}
// jobManager feeds datums to jobs
func (a *APIServer) runJob(ctx context.Context, jobInfo *pps.JobInfo, pool *pool.Pool, logger *taggedLogger) error {
pfsClient := a.pachClient.PfsAPIClient
ppsClient := a.pachClient.PpsAPIClient
jobID := jobInfo.Job.ID
var jobStopped bool
var jobStoppedMutex sync.Mutex
backoff.RetryNotify(func() (retErr error) {
// We use a new context for this particular instance of the retry
// loop, to ensure that all resources are released properly when
// this job retries.
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if jobInfo.ParentJob != nil {
// Wait for the parent job to finish, to ensure that output
// commits are ordered correctly, and that this job doesn't
// contend for workers with its parent.
if _, err := ppsClient.InspectJob(ctx, &pps.InspectJobRequest{
Job: jobInfo.ParentJob,
BlockState: true,
}); err != nil {
return err
}
}
// Cancel the context and move on to the next job if this job
// has been manually stopped.
go func() {
currentJobInfo, err := ppsClient.InspectJob(ctx, &pps.InspectJobRequest{
Job: jobInfo.Job,
BlockState: true,
})
if err != nil {
logger.Logf("error monitoring job state: %v", err)
return
}
switch currentJobInfo.State {
case pps.JobState_JOB_KILLED, pps.JobState_JOB_SUCCESS, pps.JobState_JOB_FAILURE:
jobStoppedMutex.Lock()
defer jobStoppedMutex.Unlock()
jobStopped = true
cancel()
}
}()
var pipelineInfo *pps.PipelineInfo
if jobInfo.Pipeline != nil {
var err error
pipelineInfo, err = ppsClient.InspectPipeline(ctx, &pps.InspectPipelineRequest{
Pipeline: jobInfo.Pipeline,
})
if err != nil {
return err
}
}
// Set the state of this job to 'RUNNING'
_, err := col.NewSTM(ctx, a.etcdClient, func(stm col.STM) error {
jobs := a.jobs.ReadWrite(stm)
jobInfo := new(pps.JobInfo)
if err := jobs.Get(jobID, jobInfo); err != nil {
return err
}
return a.updateJobState(stm, jobInfo, pps.JobState_JOB_RUNNING)
})
if err != nil {
return err
}
failed := false
limiter := limit.New(a.numWorkers * queueSize)
// process all datums
df, err := NewDatumFactory(ctx, pfsClient, jobInfo.Input)
if err != nil {
return err
}
var newBranchParentCommit *pfs.Commit
// If this is an incremental job we need to find the parent
// commit of the new branch.
if jobInfo.Incremental && jobInfo.NewBranch != nil {
newBranchCommitInfo, err := pfsClient.InspectCommit(ctx, &pfs.InspectCommitRequest{
Commit: jobInfo.NewBranch.Head,
})
if err != nil {
return err
}
newBranchParentCommit = newBranchCommitInfo.ParentCommit
}
tree := hashtree.NewHashTree()
var statsTree hashtree.OpenHashTree
if jobInfo.EnableStats {
statsTree = hashtree.NewHashTree()
}
var processStats []*pps.ProcessStats
var treeMu sync.Mutex
processedData := int64(0)
skippedData := int64(0)
setData := int64(0) // sum of skipped and processed data we've told etcd about
stats := &pps.ProcessStats{}
totalData := int64(df.Len())
var progressMu sync.Mutex
updateProgress := func(processed, skipped int64, newStats *pps.ProcessStats) {
progressMu.Lock()
defer progressMu.Unlock()
processedData += processed
skippedData += skipped
totalProcessedData := processedData + skippedData
if newStats != nil {
var err error
if stats.DownloadTime, err = plusDuration(stats.DownloadTime, newStats.DownloadTime); err != nil {
logger.Logf("error adding durations: %+v", err)
}
if stats.ProcessTime, err = plusDuration(stats.ProcessTime, newStats.ProcessTime); err != nil {
logger.Logf("error adding durations: %+v", err)
}
if stats.UploadTime, err = plusDuration(stats.UploadTime, newStats.UploadTime); err != nil {
logger.Logf("error adding durations: %+v", err)
}
stats.DownloadBytes += newStats.DownloadBytes
stats.UploadBytes += newStats.UploadBytes
}
// so as not to overwhelm etcd we update at most 100 times per job
if (float64(totalProcessedData-setData)/float64(totalData)) > .01 ||
totalProcessedData == 0 || totalProcessedData == totalData {
// we setProcessedData even though the update below may fail,
// if we didn't we'd retry updating the progress on the next
// datum, this would lead to more accurate progress but
// progress isn't that important and we don't want to overwelm
// etcd.
setData = totalProcessedData
if _, err := col.NewSTM(ctx, a.etcdClient, func(stm col.STM) error {
jobs := a.jobs.ReadWrite(stm)
jobInfo := new(pps.JobInfo)
if err := jobs.Get(jobID, jobInfo); err != nil {
return err
}
jobInfo.DataProcessed = processedData
jobInfo.DataSkipped = skippedData
jobInfo.DataTotal = totalData
jobInfo.Stats = stats
jobs.Put(jobInfo.Job.ID, jobInfo)
return nil
}); err != nil {
logger.Logf("error updating job progress: %+v", err)
}
}
}
// set the initial values
updateProgress(0, 0, nil)
for i := 0; i < df.Len(); i++ {
limiter.Acquire()
files := df.Datum(i)
datumHash := HashDatum(pipelineInfo.Pipeline.Name, pipelineInfo.Salt, files)
tag := &pfs.Tag{datumHash}
statsTag := &pfs.Tag{datumHash + statsTagSuffix}
var parentOutputTag *pfs.Tag
if newBranchParentCommit != nil {
var parentFiles []*Input
for _, file := range files {
parentFile := proto.Clone(file).(*Input)
if file.FileInfo.File.Commit.Repo.Name == jobInfo.NewBranch.Head.Repo.Name && file.Branch == jobInfo.NewBranch.Name {
parentFileInfo, err := pfsClient.InspectFile(ctx, &pfs.InspectFileRequest{
File: client.NewFile(parentFile.FileInfo.File.Commit.Repo.Name, newBranchParentCommit.ID, parentFile.FileInfo.File.Path),
})
if err != nil {
if !isNotFoundErr(err) {
return err
}
// we didn't find a match for this file,
// so we know there's no matching datum
break
}
file.ParentCommit = parentFileInfo.File.Commit
parentFile.FileInfo = parentFileInfo
}
parentFiles = append(parentFiles, parentFile)
}
if len(parentFiles) == len(files) {
_parentOutputTag := HashDatum(pipelineInfo.Pipeline.Name, pipelineInfo.Salt, parentFiles)
parentOutputTag = &pfs.Tag{Name: _parentOutputTag}
}
}
go func() {
userCodeFailures := 0
defer limiter.Release()
b := backoff.NewInfiniteBackOff()
b.Multiplier = 1
var stats *pps.ProcessStats
// If usedCache is set to true, we know that we thought a
// datum has been processed, but it's not found in the
// object store. Therefore if a retry happens, we know to
// skip the cache and recompute the datums.
var usedCache bool
var skipped bool
if err := backoff.RetryNotify(func() error {
var failed bool
processed := a.getCachedDatum(datumHash)
if usedCache || !processed {
if err := pool.Do(ctx, func(conn *grpc.ClientConn) error {
workerClient := NewWorkerClient(conn)
resp, err := workerClient.Process(ctx, &ProcessRequest{
JobID: jobInfo.Job.ID,
Data: files,
ParentOutput: parentOutputTag,
EnableStats: jobInfo.EnableStats,
})
if err != nil {
return err
}
skipped = resp.Skipped
failed = resp.Failed
stats = resp.Stats
return nil
}); err != nil {
return fmt.Errorf("Process() call failed: %v", err)
}
} else {
usedCache = true
skipped = true
}
if failed {
userCodeFailures++
// If this is our last failure we merge in the stats
// tree for the failed run.
if userCodeFailures > maximumRetriesPerDatum && jobInfo.EnableStats {
statsSubtree, err := a.getTreeFromTag(ctx, statsTag)
if err != nil {
logger.Logf("failed to retrieve stats hashtree after processing for datum %v: %v", files, err)
} else {
treeMu.Lock()
defer treeMu.Unlock()
if err := statsTree.Merge(statsSubtree); err != nil {
logger.Logf("failed to merge into stats tree: %v", err)
}
}
}
return fmt.Errorf("user code failed for datum %v", files)
}
a.setCachedDatum(datumHash)
var eg errgroup.Group
var subTree hashtree.HashTree
var statsSubtree hashtree.HashTree
eg.Go(func() error {
subTree, err = a.getTreeFromTag(ctx, tag)
if err != nil {
return fmt.Errorf("failed to retrieve hashtree after processing for datum %v: %v", files, err)
}
return nil
})
if jobInfo.EnableStats {
eg.Go(func() error {
statsSubtree, err = a.getTreeFromTag(ctx, statsTag)
if err != nil {
logger.Logf("failed to retrieve stats hashtree after processing for datum %v: %v", files, err)
return nil
}
if skipped {
// write file to skipped stats tree
nodes, err := statsSubtree.Glob("*")
if err != nil {
logger.Logf("failed to retrieve datum ID from hashtree for datum %v: %v", files, err)
}
if len(nodes) != 1 {
logger.Logf("should have a single stats object for datum %v", files)
return nil
}
datumID := nodes[0].Name
treeMu.Lock()
err = statsTree.PutFile(fmt.Sprintf("%v/skipped", datumID), nil, 0)
treeMu.Unlock()
if err != nil {
logger.Logf("unable to put skipped file to tree: %", err)
}
}
return nil
})
}
if err := eg.Wait(); err != nil {
return err
}
treeMu.Lock()
defer treeMu.Unlock()
if statsSubtree != nil {
if err := statsTree.Merge(statsSubtree); err != nil {
logger.Logf("failed to merge into stats tree: %v", err)
}
}
if stats != nil {
processStats = append(processStats, stats)
}
return tree.Merge(subTree)
}, b, func(err error, d time.Duration) error {
select {
case <-ctx.Done():
return err
default:
}
if userCodeFailures > maximumRetriesPerDatum {
logger.Logf("job %s failed to process datum %+v %d times failing", jobID, files, userCodeFailures)
failed = true
return err
}
logger.Logf("job %s failed to process datum %+v with: %+v, retrying in: %+v", jobID, files, err, d)
return nil
}); err == nil {
if skipped {
go updateProgress(0, 1, stats)
} else {
go updateProgress(1, 0, stats)
}
}
}()
}
limiter.Wait()
var statsCommit *pfs.Commit
if jobInfo.EnableStats {
if err := func() error {
aggregateProcessStats, err := a.aggregateProcessStats(processStats)
if err != nil {
return err
}
marshalled, err := (&jsonpb.Marshaler{}).MarshalToString(aggregateProcessStats)
if err != nil {
return err
}
aggregateObject, _, err := a.pachClient.WithCtx(ctx).PutObject(strings.NewReader(marshalled))
if err != nil {
return err
}
return statsTree.PutFile("/stats", []*pfs.Object{aggregateObject}, int64(len(marshalled)))
}(); err != nil {
logger.Logf("error aggregating stats")
}
statsObject, err := a.putTree(ctx, statsTree)
if err != nil {
return err
}
statsCommit, err = pfsClient.BuildCommit(ctx, &pfs.BuildCommitRequest{
Parent: &pfs.Commit{
Repo: jobInfo.OutputRepo,
},
Branch: "stats",
Tree: statsObject,
})
if err != nil {
return err
}
}
// check if the job failed
if failed {
_, err = col.NewSTM(ctx, a.etcdClient, func(stm col.STM) error {
jobs := a.jobs.ReadWrite(stm)
jobInfo := new(pps.JobInfo)
if err := jobs.Get(jobID, jobInfo); err != nil {
return err
}
jobInfo.Finished = now()
jobInfo.StatsCommit = statsCommit
return a.updateJobState(stm, jobInfo, pps.JobState_JOB_FAILURE)
})
return err
}
object, err := a.putTree(ctx, tree)
if err != nil {
return err
}
var provenance []*pfs.Commit
for _, commit := range pps.InputCommits(jobInfo.Input) {
provenance = append(provenance, commit)
}
outputCommit, err := pfsClient.BuildCommit(ctx, &pfs.BuildCommitRequest{
Parent: &pfs.Commit{
Repo: jobInfo.OutputRepo,
},
Branch: jobInfo.OutputBranch,
Provenance: provenance,
Tree: object,
})
if err != nil {
return err
}
if jobInfo.Egress != nil {
logger.Logf("Starting egress upload for job (%v)\n", jobInfo)
start := time.Now()
url, err := obj.ParseURL(jobInfo.Egress.URL)
if err != nil {
return err
}
objClient, err := obj.NewClientFromURLAndSecret(ctx, url)
if err != nil {
return err
}
client := client.APIClient{
PfsAPIClient: pfsClient,
}
client.SetMaxConcurrentStreams(100)
if err := pfs_sync.PushObj(client, outputCommit, objClient, url.Object); err != nil {
return err
}
logger.Logf("Completed egress upload for job (%v), duration (%v)\n", jobInfo, time.Since(start))
}
// Record the job's output commit and 'Finished' timestamp, and mark the job
// as a SUCCESS
_, err = col.NewSTM(ctx, a.etcdClient, func(stm col.STM) error {
jobs := a.jobs.ReadWrite(stm)
jobInfo := new(pps.JobInfo)
if err := jobs.Get(jobID, jobInfo); err != nil {
return err
}
jobInfo.OutputCommit = outputCommit
jobInfo.Finished = now()
// By definition, we will have processed all datums at this point
jobInfo.DataProcessed = processedData
jobInfo.DataSkipped = skippedData
jobInfo.Stats = stats
// likely already set but just in case it failed
jobInfo.DataTotal = totalData
jobInfo.StatsCommit = statsCommit
return a.updateJobState(stm, jobInfo, pps.JobState_JOB_SUCCESS)
})
return err
}, backoff.NewInfiniteBackOff(), func(err error, d time.Duration) error {
select {
case <-ctx.Done():
// Exit the retry loop if context got cancelled
return err
default:
}
jobStoppedMutex.Lock()
defer jobStoppedMutex.Unlock()
if jobStopped {
// If the job has been stopped, exit the retry loop
return err
}
logger.Logf("error running jobManager for job %s: %v; retrying in %v", jobInfo.Job.ID, err, d)
// Increment the job's restart count
_, err = col.NewSTM(ctx, a.etcdClient, func(stm col.STM) error {
jobs := a.jobs.ReadWrite(stm)
jobInfo := new(pps.JobInfo)
if err := jobs.Get(jobID, jobInfo); err != nil {
return err
}
jobInfo.Restart++
jobs.Put(jobInfo.Job.ID, jobInfo)
return nil
})
if err != nil {
logger.Logf("error incrementing job %s's restart count", jobInfo.Job.ID)
}
return nil
})
return nil
}
func (a *APIServer) aggregateProcessStats(stats []*pps.ProcessStats) (*pps.AggregateProcessStats, error) {
var downloadTime []float64
var processTime []float64
var uploadTime []float64
var downloadBytes []float64
var uploadBytes []float64
for _, s := range stats {
dt, err := types.DurationFromProto(s.DownloadTime)
if err != nil {
return nil, err
}
downloadTime = append(downloadTime, float64(dt))
pt, err := types.DurationFromProto(s.ProcessTime)
if err != nil {
return nil, err
}
processTime = append(processTime, float64(pt))
ut, err := types.DurationFromProto(s.UploadTime)
if err != nil {
return nil, err
}
uploadTime = append(uploadTime, float64(ut))
downloadBytes = append(downloadBytes, float64(s.DownloadBytes))
uploadBytes = append(uploadBytes, float64(s.UploadBytes))
}
dtAgg, err := a.aggregate(downloadTime)
if err != nil {
return nil, err
}
ptAgg, err := a.aggregate(processTime)
if err != nil {
return nil, err
}
utAgg, err := a.aggregate(uploadTime)
if err != nil {
return nil, err
}
dbAgg, err := a.aggregate(downloadBytes)
if err != nil {
return nil, err
}
ubAgg, err := a.aggregate(uploadBytes)
if err != nil {
return nil, err
}
return &pps.AggregateProcessStats{
DownloadTime: dtAgg,
ProcessTime: ptAgg,
UploadTime: utAgg,
DownloadBytes: dbAgg,
UploadBytes: ubAgg,
}, nil
}
func (a *APIServer) aggregate(datums []float64) (*pps.Aggregate, error) {
logger := a.getMasterLogger()
mean, err := stats.Mean(datums)
if err != nil {
logger.Logf("error aggregating mean: %v\n", err)
}
stddev, err := stats.StandardDeviation(datums)
if err != nil {
logger.Logf("error aggregating std dev: %v\n", err)
}
fifth, err := stats.Percentile(datums, 5)
if err != nil {
logger.Logf("error aggregating 5th percentile: %v\n", err)
}
ninetyFifth, err := stats.Percentile(datums, 95)
if err != nil {
logger.Logf("error aggregating 95th percentile: %v\n", err)
}
return &pps.Aggregate{
Count: int64(len(datums)),
Mean: mean,
Stddev: stddev,
FifthPercentile: fifth,
NinetyFifthPercentile: ninetyFifth,
}, nil
}
func (a *APIServer) getTreeFromTag(ctx context.Context, tag *pfs.Tag) (hashtree.HashTree, error) {
var buffer bytes.Buffer
if err := a.pachClient.WithCtx(ctx).GetTag(tag.Name, &buffer); err != nil {
return nil, err
}
return hashtree.Deserialize(buffer.Bytes())
}
func (a *APIServer) putTree(ctx context.Context, tree hashtree.OpenHashTree) (*pfs.Object, error) {
finishedTree, err := tree.Finish()
if err != nil {
return nil, err
}
data, err := hashtree.Serialize(finishedTree)
if err != nil {
return nil, err
}
object, _, err := a.pachClient.WithCtx(ctx).PutObject(bytes.NewReader(data))
return object, err
}
func (a *APIServer) scaleDownWorkers() error {
rc := a.kubeClient.ReplicationControllers(a.namespace)
workerRc, err := rc.Get(ppsserver.PipelineRcName(a.pipelineInfo.Pipeline.Name, a.pipelineInfo.Version))
if err != nil {
return err
}
workerRc.Spec.Replicas = 1
// When we scale down the workers, we also remove the resource
// requirements so that the remaining master pod does not take up
// the resource it doesn't need, since by definition when a pipeline
// is in scale-down mode, it doesn't process any work.
if a.pipelineInfo.ResourceSpec != nil {
workerRc.Spec.Template.Spec.Containers[0].Resources = api.ResourceRequirements{}
}
_, err = rc.Update(workerRc)
return err
}
func (a *APIServer) scaleUpWorkers() error {
rc := a.kubeClient.ReplicationControllers(a.namespace)
workerRc, err := rc.Get(ppsserver.PipelineRcName(a.pipelineInfo.Pipeline.Name, a.pipelineInfo.Version))
if err != nil {
return err
}
parallelism, err := ppsserver.GetExpectedNumWorkers(a.kubeClient, a.pipelineInfo.ParallelismSpec)
if err != nil {
return err
}
if workerRc.Spec.Replicas != int32(parallelism) {
workerRc.Spec.Replicas = int32(parallelism)
}
// Reset the resource requirements for the RC since the pipeline
// is in scale-down mode and probably has removed its resource
// requirements.
if a.pipelineInfo.ResourceSpec != nil {
resourceList, err := util.GetResourceListFromPipeline(a.pipelineInfo)
if err != nil {
return fmt.Errorf("error parsing resource spec; this is likely a bug: %v", err)
}
workerRc.Spec.Template.Spec.Containers[0].Resources = api.ResourceRequirements{
Requests: *resourceList,
}
}
_, err = rc.Update(workerRc)
return err
}
// getCachedDatum returns whether the given datum (identified by its hash)
// has been processed.
func (a *APIServer) getCachedDatum(hash string) bool {
_, ok := a.datumCache.Get(hash)
return ok
}
// setCachedDatum records that the given datum has been processed.
func (a *APIServer) setCachedDatum(hash string) {
a.datumCache.Add(hash, struct{}{})
}
func untranslateJobInputs(input *pps.Input) []*pps.JobInput {
var result []*pps.JobInput
if input.Cross != nil {
for _, input := range input.Cross {
if input.Atom == nil {
return nil
}
result = append(result, &pps.JobInput{
Name: input.Atom.Name,
Commit: client.NewCommit(input.Atom.Repo, input.Atom.Commit),
Glob: input.Atom.Glob,
Lazy: input.Atom.Lazy,
})
}
}
return result
}
func isNotFoundErr(err error) bool {
return err != nil && strings.Contains(err.Error(), "not found")
}
func now() *types.Timestamp {
t, err := types.TimestampProto(time.Now())
if err != nil {
panic(err)
}
return t
}