forked from pachyderm/pachyderm
/
driver.go
1403 lines (1317 loc) · 40.4 KB
/
driver.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
991
992
993
994
995
996
997
998
999
1000
package drive
import (
"fmt"
"io"
"path"
"regexp"
"sort"
"sync"
"github.com/pachyderm/pachyderm/src/client"
"github.com/pachyderm/pachyderm/src/client/pfs"
pfsserver "github.com/pachyderm/pachyderm/src/server/pfs"
"github.com/pachyderm/pachyderm/src/server/pkg/dag"
"github.com/pachyderm/pachyderm/src/server/pkg/metrics"
"go.pedge.io/pb/go/google/protobuf"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
type driver struct {
blockAddress string
blockClient pfs.BlockAPIClient
blockClientOnce sync.Once
diffs diffMap
dags map[string]*dag.DAG
branches map[string]map[string]string
lock sync.RWMutex
// used for signaling the completion (i.e. finishing) of a commit
commitConds map[string]*sync.Cond
}
func newDriver(blockAddress string) (Driver, error) {
return &driver{
blockAddress: blockAddress,
blockClient: nil,
blockClientOnce: sync.Once{},
diffs: make(diffMap),
dags: make(map[string]*dag.DAG),
branches: make(map[string]map[string]string),
lock: sync.RWMutex{},
commitConds: make(map[string]*sync.Cond),
}, nil
}
func (d *driver) getBlockClient() (pfs.BlockAPIClient, error) {
if d.blockClient == nil {
var onceErr error
d.blockClientOnce.Do(func() {
clientConn, err := grpc.Dial(d.blockAddress, grpc.WithInsecure())
if err != nil {
onceErr = err
}
d.blockClient = pfs.NewBlockAPIClient(clientConn)
})
if onceErr != nil {
return nil, onceErr
}
}
return d.blockClient, nil
}
func validateRepoName(name string) error {
match, _ := regexp.MatchString("^[a-zA-Z0-9_]+$", name)
if !match {
return fmt.Errorf("Repo name (%v) invalid. Only alphanumeric and underscore characters allowed.", name)
}
return nil
}
func (d *driver) CreateRepo(repo *pfs.Repo, created *google_protobuf.Timestamp,
provenance []*pfs.Repo, shards map[uint64]bool) error {
d.lock.Lock()
defer d.lock.Unlock()
if _, ok := d.diffs[repo.Name]; ok {
return fmt.Errorf("repo %s exists", repo.Name)
}
if err := validateRepoName(repo.Name); err != nil {
return err
}
for _, provRepo := range provenance {
if _, err := d.inspectRepo(provRepo, shards); err != nil {
return nil
}
}
d.createRepoState(repo)
blockClient, err := d.getBlockClient()
if err != nil {
return err
}
var wg sync.WaitGroup
errCh := make(chan error, 1)
for shard := range shards {
wg.Add(1)
diffInfo := &pfs.DiffInfo{
Diff: client.NewDiff(repo.Name, "", shard),
Finished: created,
}
for _, provRepo := range provenance {
diffInfo.Provenance = append(diffInfo.Provenance, client.NewCommit(provRepo.Name, ""))
}
if err := d.diffs.insert(diffInfo); err != nil {
return err
}
go func() {
defer wg.Done()
if _, err := blockClient.CreateDiff(context.Background(), diffInfo); err != nil {
select {
case errCh <- err:
default:
}
return
}
}()
}
wg.Wait()
select {
case err := <-errCh:
return err
default:
}
return nil
}
func (d *driver) InspectRepo(repo *pfs.Repo, shards map[uint64]bool) (*pfs.RepoInfo, error) {
d.lock.RLock()
defer d.lock.RUnlock()
return d.inspectRepo(repo, shards)
}
func (d *driver) ListRepo(provenance []*pfs.Repo, shards map[uint64]bool) ([]*pfs.RepoInfo, error) {
d.lock.RLock()
defer d.lock.RUnlock()
var wg sync.WaitGroup
errCh := make(chan error, 1)
var result []*pfs.RepoInfo
var lock sync.Mutex
for repoName := range d.diffs {
wg.Add(1)
repoName := repoName
go func() {
defer wg.Done()
repoInfo, err := d.inspectRepo(&pfs.Repo{Name: repoName}, shards)
if err != nil {
select {
case errCh <- err:
default:
}
return
}
provSet := repoSet(repoInfo.Provenance)
for _, repo := range provenance {
if !provSet[repo.Name] {
// this repo doesn't match the provenance we want, ignore it
return
}
}
lock.Lock()
defer lock.Unlock()
result = append(result, repoInfo)
}()
}
wg.Wait()
select {
case err := <-errCh:
return nil, err
default:
}
return result, nil
}
func (d *driver) DeleteRepo(repo *pfs.Repo, shards map[uint64]bool) error {
// Make sure that this repo is not the provenance of any other repo
repoInfos, err := d.ListRepo([]*pfs.Repo{repo}, shards)
if err != nil {
return err
}
var diffInfos []*pfs.DiffInfo
err = func() error {
d.lock.Lock()
defer d.lock.Unlock()
if _, ok := d.diffs[repo.Name]; !ok {
return pfsserver.NewErrRepoNotFound(repo.Name)
}
if len(repoInfos) > 0 {
var repoNames []string
for _, repoInfo := range repoInfos {
repoNames = append(repoNames, repoInfo.Repo.Name)
}
return fmt.Errorf("cannot delete repo %v; it's the provenance of the following repos: %v", repo.Name, repoNames)
}
for shard := range shards {
for _, diffInfo := range d.diffs[repo.Name][shard] {
diffInfos = append(diffInfos, diffInfo)
}
}
delete(d.diffs, repo.Name)
return nil
}()
if err != nil {
return err
}
blockClient, err := d.getBlockClient()
if err != nil {
return err
}
errCh := make(chan error, 1)
var wg sync.WaitGroup
for _, diffInfo := range diffInfos {
diffInfo := diffInfo
wg.Add(1)
go func() {
defer wg.Done()
if _, err := blockClient.DeleteDiff(
context.Background(),
&pfs.DeleteDiffRequest{Diff: diffInfo.Diff},
); err != nil {
select {
case errCh <- err:
default:
}
return
}
}()
}
wg.Wait()
select {
case err := <-errCh:
return err
default:
}
return nil
}
func (d *driver) StartCommit(repo *pfs.Repo, commitID string, parentID string, branch string,
started *google_protobuf.Timestamp, provenance []*pfs.Commit, shards map[uint64]bool) error {
d.lock.Lock()
defer d.lock.Unlock()
// make sure that the parent commit exists
if parentID != "" {
_, err := d.inspectCommit(client.NewCommit(repo.Name, parentID), shards)
if err != nil {
return err
}
}
for shard := range shards {
if len(provenance) != 0 {
diffInfo, ok := d.diffs.get(client.NewDiff(repo.Name, "", shard))
if !ok {
return pfsserver.NewErrRepoNotFound(repo.Name)
}
provRepos := repoSetFromCommits(diffInfo.Provenance)
for _, provCommit := range provenance {
if !provRepos[provCommit.Repo.Name] {
return fmt.Errorf("cannot use %s/%s as provenance, %s is not provenance of %s",
provCommit.Repo.Name, provCommit.ID, provCommit.Repo.Name, repo.Name)
}
}
}
diffInfo := &pfs.DiffInfo{
Diff: client.NewDiff(repo.Name, commitID, shard),
Started: started,
Appends: make(map[string]*pfs.Append),
Branch: branch,
Provenance: provenance,
}
if branch != "" {
parentCommit, err := d.branchParent(client.NewCommit(repo.Name, commitID), branch)
if err != nil {
return err
}
if parentCommit != nil && parentID != "" {
return fmt.Errorf("branch %s already exists as %s, can't create with %s as parent",
branch, parentCommit.ID, parentID)
}
diffInfo.ParentCommit = parentCommit
}
if diffInfo.ParentCommit == nil && parentID != "" {
diffInfo.ParentCommit = client.NewCommit(repo.Name, parentID)
}
if err := d.insertDiffInfo(diffInfo); err != nil {
return err
}
}
d.commitConds[commitID] = sync.NewCond(&d.lock)
return nil
}
// FinishCommit blocks until its parent has been finished/cancelled
func (d *driver) FinishCommit(commit *pfs.Commit, finished *google_protobuf.Timestamp, cancel bool, shards map[uint64]bool) error {
canonicalCommit, err := d.canonicalCommit(commit)
if err != nil {
return err
}
// closure so we can defer Unlock
var diffInfos []*pfs.DiffInfo
if err := func() error {
d.lock.Lock()
defer d.lock.Unlock()
for shard := range shards {
diffInfo, ok := d.diffs.get(client.NewDiff(canonicalCommit.Repo.Name, canonicalCommit.ID, shard))
if !ok {
return pfsserver.NewErrCommitNotFound(canonicalCommit.Repo.Name, canonicalCommit.ID)
}
if diffInfo.ParentCommit != nil {
parentDiffInfo, ok := d.diffs.get(client.NewDiff(canonicalCommit.Repo.Name, diffInfo.ParentCommit.ID, shard))
if !ok {
return pfsserver.NewErrParentCommitNotFound(canonicalCommit.Repo.Name, diffInfo.ParentCommit.ID)
}
// Wait for parent to finish
for parentDiffInfo.Finished == nil {
cond, ok := d.commitConds[diffInfo.ParentCommit.ID]
if !ok {
return fmt.Errorf("parent commit %s/%s was not finished but a corresponding conditional variable could not be found; this is likely a bug", canonicalCommit.Repo.Name, diffInfo.ParentCommit.ID)
}
cond.Wait()
}
diffInfo.Cancelled = parentDiffInfo.Cancelled
}
diffInfo.Finished = finished
for _, _append := range diffInfo.Appends {
coalesceHandles(_append)
}
diffInfo.Cancelled = diffInfo.Cancelled || cancel
diffInfos = append(diffInfos, diffInfo)
}
return nil
}(); err != nil {
return err
}
blockClient, err := d.getBlockClient()
if err != nil {
return err
}
var wg sync.WaitGroup
errCh := make(chan error, 1)
for _, diffInfo := range diffInfos {
diffInfo := diffInfo
wg.Add(1)
go func() {
defer wg.Done()
if _, err := blockClient.CreateDiff(context.Background(), diffInfo); err != nil {
select {
case errCh <- err:
default:
}
return
}
}()
}
wg.Wait()
select {
case err := <-errCh:
return err
default:
}
d.lock.Lock()
defer d.lock.Unlock()
cond, ok := d.commitConds[canonicalCommit.ID]
if !ok {
return fmt.Errorf("could not found a conditional variable to signal commit completion; this is likely a bug")
}
cond.Broadcast()
delete(d.commitConds, canonicalCommit.ID)
return nil
}
func (d *driver) InspectCommit(commit *pfs.Commit, shards map[uint64]bool) (*pfs.CommitInfo, error) {
d.lock.RLock()
defer d.lock.RUnlock()
return d.inspectCommit(commit, shards)
}
func (d *driver) ListCommit(repos []*pfs.Repo, commitType pfs.CommitType, fromCommit []*pfs.Commit,
provenance []*pfs.Commit, all bool, shards map[uint64]bool) ([]*pfs.CommitInfo, error) {
repoSet := repoSet(repos)
var canonicalProvenance []*pfs.Commit
for _, provCommit := range provenance {
canonicalCommit, err := d.canonicalCommit(provCommit)
if err != nil {
return nil, err
}
canonicalProvenance = append(canonicalProvenance, canonicalCommit)
}
breakCommitIDs := make(map[string]bool)
for _, commit := range fromCommit {
if !repoSet[commit.Repo.Name] {
return nil, fmt.Errorf("Commit %s/%s is from a repo that isn't being listed.", commit.Repo.Name, commit.ID)
}
breakCommitIDs[commit.ID] = true
}
d.lock.RLock()
defer d.lock.RUnlock()
var result []*pfs.CommitInfo
for _, repo := range repos {
_, ok := d.diffs[repo.Name]
if !ok {
return nil, pfsserver.NewErrRepoNotFound(repo.Name)
}
for _, commitID := range d.dags[repo.Name].Leaves() {
commit := &pfs.Commit{
Repo: repo,
ID: commitID,
}
for commit != nil && !breakCommitIDs[commit.ID] {
// we add this commit to breakCommitIDs so we won't see it twice
breakCommitIDs[commit.ID] = true
commitInfo, err := d.inspectCommit(commit, shards)
if err != nil {
return nil, err
}
commit = commitInfo.ParentCommit
if commitInfo.Cancelled && !all {
continue
}
if !MatchProvenance(canonicalProvenance, commitInfo.Provenance) {
continue
}
if commitType != pfs.CommitType_COMMIT_TYPE_NONE &&
commitType != commitInfo.CommitType {
continue
}
result = append(result, commitInfo)
}
}
}
return result, nil
}
func MatchProvenance(want []*pfs.Commit, have []*pfs.Commit) bool {
repoToCommit := make(map[string]*pfs.Commit)
for _, haveCommit := range have {
repoToCommit[haveCommit.Repo.Name] = haveCommit
}
for _, wantCommit := range want {
haveCommit, ok := repoToCommit[wantCommit.Repo.Name]
if !ok || wantCommit.ID != haveCommit.ID {
return false
}
}
return true
}
func (d *driver) ListBranch(repo *pfs.Repo, shards map[uint64]bool) ([]*pfs.CommitInfo, error) {
var result []*pfs.CommitInfo
_, ok := d.branches[repo.Name]
if !ok {
return nil, pfsserver.NewErrRepoNotFound(repo.Name)
}
for commitID := range d.branches[repo.Name] {
commitInfo, err := d.inspectCommit(client.NewCommit(repo.Name, commitID), shards)
if err != nil {
return nil, err
}
result = append(result, commitInfo)
}
return result, nil
}
func (d *driver) DeleteCommit(commit *pfs.Commit, shards map[uint64]bool) error {
return fmt.Errorf("DeleteCommit is not implemented")
}
func (d *driver) PutFile(file *pfs.File, handle string,
delimiter pfs.Delimiter, shard uint64, reader io.Reader) (retErr error) {
blockClient, err := d.getBlockClient()
if err != nil {
return err
}
_client := client.APIClient{BlockAPIClient: blockClient}
blockRefs, err := _client.PutBlock(delimiter, reader)
if err != nil {
return err
}
defer func() {
if retErr == nil {
metrics.AddFiles(1)
for _, blockRef := range blockRefs.BlockRef {
metrics.AddBytes(int64(blockRef.Range.Upper - blockRef.Range.Lower))
}
}
}()
d.lock.Lock()
defer d.lock.Unlock()
fileType, err := d.getFileType(file, shard)
if err != nil {
return err
}
if fileType == pfs.FileType_FILE_TYPE_DIR {
return fmt.Errorf("%s is a directory", file.Path)
}
canonicalCommit, err := d.canonicalCommit(file.Commit)
if err != nil {
return err
}
diffInfo, ok := d.diffs.get(client.NewDiff(canonicalCommit.Repo.Name, canonicalCommit.ID, shard))
if !ok {
// This is a weird case since the commit existed above, it means someone
// deleted the commit while the above code was running
return pfsserver.NewErrCommitNotFound(canonicalCommit.Repo.Name, canonicalCommit.ID)
}
if diffInfo.Finished != nil {
return fmt.Errorf("commit %s/%s has already been finished", canonicalCommit.Repo.Name, canonicalCommit.ID)
}
d.addDirs(diffInfo, file, shard)
_append, ok := diffInfo.Appends[path.Clean(file.Path)]
if !ok {
_append = newAppend(pfs.FileType_FILE_TYPE_REGULAR)
} else {
_append.FileType = pfs.FileType_FILE_TYPE_REGULAR
}
if diffInfo.ParentCommit != nil {
_append.LastRef = d.lastRef(
client.NewFile(diffInfo.ParentCommit.Repo.Name, diffInfo.ParentCommit.ID, file.Path),
shard,
)
}
diffInfo.Appends[path.Clean(file.Path)] = _append
if handle == "" {
_append.BlockRefs = append(_append.BlockRefs, blockRefs.BlockRef...)
} else {
handleBlockRefs, ok := _append.Handles[handle]
if !ok {
handleBlockRefs = &pfs.BlockRefs{}
_append.Handles[handle] = handleBlockRefs
}
handleBlockRefs.BlockRef = append(handleBlockRefs.BlockRef, blockRefs.BlockRef...)
}
for _, blockRef := range blockRefs.BlockRef {
diffInfo.SizeBytes += blockRef.Range.Upper - blockRef.Range.Lower
}
return nil
}
func (d *driver) MakeDirectory(file *pfs.File, shard uint64) (retErr error) {
defer func() {
if retErr == nil {
metrics.AddFiles(1)
}
}()
d.lock.Lock()
defer d.lock.Unlock()
fileType, err := d.getFileType(file, shard)
if err != nil {
return err
}
if fileType == pfs.FileType_FILE_TYPE_REGULAR {
return fmt.Errorf("%s already exists and is a file", file.Path)
} else if fileType == pfs.FileType_FILE_TYPE_DIR {
return nil
}
canonicalCommit, err := d.canonicalCommit(file.Commit)
if err != nil {
return err
}
diffInfo, ok := d.diffs.get(client.NewDiff(canonicalCommit.Repo.Name, canonicalCommit.ID, shard))
if !ok {
return pfsserver.NewErrCommitNotFound(canonicalCommit.Repo.Name, canonicalCommit.ID)
}
if diffInfo.Finished != nil {
return fmt.Errorf("commit %s/%s has already been finished", canonicalCommit.Repo.Name, canonicalCommit.ID)
}
d.addDirs(diffInfo, file, shard)
_append, ok := diffInfo.Appends[path.Clean(file.Path)]
if !ok {
_append = newAppend(pfs.FileType_FILE_TYPE_DIR)
} else {
_append.FileType = pfs.FileType_FILE_TYPE_DIR
}
if diffInfo.ParentCommit != nil {
_append.LastRef = d.lastRef(
client.NewFile(
diffInfo.ParentCommit.Repo.Name,
diffInfo.ParentCommit.ID,
file.Path,
),
shard,
)
}
diffInfo.Appends[path.Clean(file.Path)] = _append
// The fact that this is a directory is signified by setting Children
// to non-nil
_append.Children = make(map[string]bool)
return nil
}
func (d *driver) GetFile(file *pfs.File, filterShard *pfs.Shard, offset int64,
size int64, from *pfs.Commit, shard uint64, unsafe bool, handle string) (io.ReadCloser, error) {
d.lock.RLock()
defer d.lock.RUnlock()
fileInfo, blockRefs, err := d.inspectFile(file, filterShard, shard, from, false, unsafe, handle)
if err != nil {
return nil, err
}
if fileInfo.FileType == pfs.FileType_FILE_TYPE_DIR {
return nil, fmt.Errorf("file %s/%s/%s is directory", file.Commit.Repo.Name, file.Commit.ID, file.Path)
}
blockClient, err := d.getBlockClient()
if err != nil {
return nil, err
}
return newFileReader(blockClient, blockRefs, offset, size), nil
}
func (d *driver) InspectFile(file *pfs.File, filterShard *pfs.Shard, from *pfs.Commit, shard uint64, unsafe bool, handle string) (*pfs.FileInfo, error) {
d.lock.RLock()
defer d.lock.RUnlock()
fileInfo, _, err := d.inspectFile(file, filterShard, shard, from, false, unsafe, handle)
return fileInfo, err
}
func (d *driver) ListFile(file *pfs.File, filterShard *pfs.Shard, from *pfs.Commit, shard uint64, recurse bool, unsafe bool, handle string) ([]*pfs.FileInfo, error) {
d.lock.RLock()
defer d.lock.RUnlock()
fileInfo, _, err := d.inspectFile(file, filterShard, shard, from, false, unsafe, handle)
if err != nil {
return nil, err
}
if fileInfo.FileType == pfs.FileType_FILE_TYPE_REGULAR {
return []*pfs.FileInfo{fileInfo}, nil
}
var result []*pfs.FileInfo
for _, child := range fileInfo.Children {
fileInfo, _, err := d.inspectFile(child, filterShard, shard, from, recurse, unsafe, handle)
_, ok := err.(*pfsserver.ErrFileNotFound)
if err != nil && !ok {
return nil, err
}
if ok {
// how can a listed child return not found?
// regular files without any blocks in this shard count as not found
continue
}
result = append(result, fileInfo)
}
return result, nil
}
func (d *driver) DeleteFile(file *pfs.File, shard uint64, unsafe bool, handle string) error {
d.lock.RLock()
// We don't want to be able to delete files that are only added in the current
// commit, which is why we set unsafe to false.
fileInfo, _, err := d.inspectFile(file, nil, shard, nil, false, unsafe, handle)
if err != nil {
d.lock.RUnlock()
return err
}
d.lock.RUnlock()
if fileInfo.FileType == pfs.FileType_FILE_TYPE_DIR {
fileInfos, err := d.ListFile(file, nil, nil, shard, false, unsafe, handle)
if err != nil {
return err
}
for _, info := range fileInfos {
// We are deleting the file from the current commit, not whatever
// commit they were last modified in
info.File.Commit = file.Commit
if err := d.DeleteFile(info.File, shard, unsafe, handle); err != nil {
return err
}
}
}
return d.deleteFile(file, shard, unsafe, handle)
}
func (d *driver) deleteFile(file *pfs.File, shard uint64, unsafe bool, handle string) error {
d.lock.Lock()
defer d.lock.Unlock()
canonicalCommit, err := d.canonicalCommit(file.Commit)
if err != nil {
return err
}
diffInfo, ok := d.diffs.get(client.NewDiff(canonicalCommit.Repo.Name, canonicalCommit.ID, shard))
if !ok {
// This is a weird case since the commit existed above, it means someone
// deleted the commit while the above code was running
return pfsserver.NewErrCommitNotFound(canonicalCommit.Repo.Name, canonicalCommit.ID)
}
if diffInfo.Finished != nil {
return fmt.Errorf("commit %s/%s has already been finished", canonicalCommit.Repo.Name, canonicalCommit.ID)
}
cleanPath := path.Clean(file.Path)
if _append, ok := diffInfo.Appends[cleanPath]; !ok {
// we have no append for this file, we create on so that we can set the
// Delete flag in it
diffInfo.Appends[cleanPath] = newAppend(pfs.FileType_FILE_TYPE_NONE)
} else if unsafe {
// we have an append for this file and unsafe is true so we need to modify the append
if handle == "" {
diffInfo.Appends[cleanPath] = newAppend(pfs.FileType_FILE_TYPE_NONE)
} else {
delete(_append.Handles, handle)
}
}
if !unsafe || handle == "" {
diffInfo.Appends[cleanPath].Delete = true
} else {
diffInfo.Appends[cleanPath].HandleDeletes[handle] = true
}
d.deleteFromDir(diffInfo, file, shard)
return nil
}
type byProvenance []*pfs.RepoInfo
func (s byProvenance) Len() int {
return len(s)
}
func (s byProvenance) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s byProvenance) Less(i, j int) bool {
return len(s[i].Provenance) < len(s[j].Provenance)
}
func (d *driver) DeleteAll(shards map[uint64]bool) error {
repoInfos, err := d.ListRepo(nil, shards)
if err != nil {
return err
}
// We want to make sure we delete repos before their provenance.
// Provenance has a nice invariant:
// A in Provenance(B) => len(Provenance(A)) < len(Provenance(B))
// Since Provenance is transitive
// Thus when we sort by length we guarantee that A will have a lower index than B
// Because we want to make sure we delete the higher indexes first we traverse in reverse
sort.Sort(byProvenance(repoInfos))
for i := range repoInfos {
if err := d.DeleteRepo(repoInfos[len(repoInfos)-i-1].Repo, shards); err != nil {
return err
}
}
return nil
}
func (d *driver) AddShard(shard uint64) error {
blockClient, err := d.getBlockClient()
if err != nil {
return err
}
listDiffClient, err := blockClient.ListDiff(context.Background(), &pfs.ListDiffRequest{Shard: shard})
if err != nil {
return err
}
diffInfos := make(diffMap)
dags := make(map[string]*dag.DAG)
for {
diffInfo, err := listDiffClient.Recv()
if err != nil && err != io.EOF {
return err
}
if err == io.EOF {
break
}
if diffInfo.Diff == nil || diffInfo.Diff.Commit == nil || diffInfo.Diff.Commit.Repo == nil {
return fmt.Errorf("broken diff info: %v; this is likely a bug", diffInfo)
}
repoName := diffInfo.Diff.Commit.Repo.Name
if _, ok := diffInfos[repoName]; !ok {
diffInfos[repoName] = make(map[uint64]map[string]*pfs.DiffInfo)
dags[repoName] = dag.NewDAG(nil)
}
updateDAG(diffInfo, dags[repoName])
if err := diffInfos.insert(diffInfo); err != nil {
return err
}
}
for repoName, dag := range dags {
if ghosts := dag.Ghosts(); len(ghosts) != 0 {
return fmt.Errorf("error adding shard %d, repo %s has ghost commits: %+v", shard, repoName, ghosts)
}
}
d.lock.Lock()
defer d.lock.Unlock()
for repoName, dag := range dags {
for _, commitID := range dag.Sorted() {
d.createRepoState(client.NewRepo(repoName))
if diffInfo, ok := diffInfos.get(client.NewDiff(repoName, commitID, shard)); ok {
if err := d.insertDiffInfo(diffInfo); err != nil {
return err
}
if diffInfo.Finished == nil {
return fmt.Errorf("diff %s/%s/%d is not finished; this is likely a bug", repoName, commitID, shard)
}
} else {
return fmt.Errorf("diff %s/%s/%d not found; this is likely a bug", repoName, commitID, shard)
}
}
}
return nil
}
func (d *driver) DeleteShard(shard uint64) error {
d.lock.Lock()
defer d.lock.Unlock()
for _, shardMap := range d.diffs {
delete(shardMap, shard)
}
return nil
}
func (d *driver) Dump() {
d.lock.RLock()
defer d.lock.RUnlock()
fmt.Printf("%p.Dump()\n", d)
for repoName, dag := range d.dags {
fmt.Printf("%s:\n", repoName)
for _, commitID := range dag.Sorted() {
fmt.Printf("\t%s: ", commitID)
for shard, commitToDiffInfo := range d.diffs[repoName] {
if _, ok := commitToDiffInfo[commitID]; ok {
fmt.Printf("%d, ", shard)
}
}
fmt.Printf("\n")
}
}
}
// inspectRepo assumes that the lock is being held
func (d *driver) inspectRepo(repo *pfs.Repo, shards map[uint64]bool) (*pfs.RepoInfo, error) {
result := &pfs.RepoInfo{
Repo: repo,
}
shardToDiffInfo, ok := d.diffs[repo.Name]
if !ok {
return nil, pfsserver.NewErrRepoNotFound(repo.Name)
}
for shard := range shards {
diffInfos, ok := shardToDiffInfo[shard]
if !ok {
continue
}
for _, diffInfo := range diffInfos {
diffInfo := diffInfo
if diffInfo.Diff.Commit.ID == "" && result.Created == nil {
result.Created = diffInfo.Finished
}
result.SizeBytes += diffInfo.SizeBytes
}
}
provenance, err := d.fullRepoProvenance(repo, shards)
if err != nil {
return nil, err
}
result.Provenance = provenance
return result, nil
}
// fullCommitProvenance recursively computes the provenance of the commit,
// starting with the immediate provenance that was declared when the commit was
// created, then our immediate provenance's immediate provenance etc.
func (d *driver) fullCommitProvenance(commit *pfs.Commit, repoSet map[string]bool,
shards map[uint64]bool) ([]*pfs.Commit, error) {
shardToDiffInfo, ok := d.diffs[commit.Repo.Name]
if !ok {
return nil, pfsserver.NewErrRepoNotFound(commit.Repo.Name)
}
var result []*pfs.Commit
for shard := range shards {
diffInfos := shardToDiffInfo[shard]
if !ok {
return nil, fmt.Errorf("missing shard %d (this is likely a bug)", shard)
}
diffInfo, ok := diffInfos[commit.ID]
if !ok {
return nil, fmt.Errorf("missing \"%s\" diff (this is likely a bug)", commit.ID)
}
for _, provCommit := range diffInfo.Provenance {
if !repoSet[provCommit.Repo.Name] {
repoSet[provCommit.Repo.Name] = true
result = append(result, provCommit)
provCommits, err := d.fullCommitProvenance(provCommit, repoSet, shards)
if err != nil {
return nil, err
}
result = append(result, provCommits...)
}
}
break // we only need to consider 1 shard
}
return result, nil
}
// fullRepoProvenance recursively computes the provenance of a repo
func (d *driver) fullRepoProvenance(repo *pfs.Repo, shards map[uint64]bool) ([]*pfs.Repo, error) {
provCommits, err := d.fullCommitProvenance(client.NewCommit(repo.Name, ""), make(map[string]bool), shards)
if err != nil {
return nil, err
}
var result []*pfs.Repo
for _, provCommit := range provCommits {
result = append(result, provCommit.Repo)
}
return result, nil
}
func (d *driver) commitProvenance(commit *pfs.Commit, shards map[uint64]bool) ([]*pfs.Commit, error) {
return d.fullCommitProvenance(commit, make(map[string]bool), shards)
}
func (d *driver) inspectCommit(commit *pfs.Commit, shards map[uint64]bool) (*pfs.CommitInfo, error) {
var commitInfos []*pfs.CommitInfo
canonicalCommit, err := d.canonicalCommit(commit)
if err != nil {
return nil, err
}
for shard := range shards {
var diffInfo *pfs.DiffInfo
var ok bool
commitInfo := &pfs.CommitInfo{Commit: canonicalCommit}
diff := client.NewDiff(canonicalCommit.Repo.Name, canonicalCommit.ID, shard)
if diffInfo, ok = d.diffs.get(diff); !ok {
return nil, pfsserver.NewErrCommitNotFound(canonicalCommit.Repo.Name, canonicalCommit.ID)
}
if diffInfo.Finished == nil {
commitInfo.CommitType = pfs.CommitType_COMMIT_TYPE_WRITE
} else {
commitInfo.CommitType = pfs.CommitType_COMMIT_TYPE_READ
}
commitInfo.Branch = diffInfo.Branch
commitInfo.ParentCommit = diffInfo.ParentCommit
commitInfo.Started = diffInfo.Started
commitInfo.Finished = diffInfo.Finished
commitInfo.SizeBytes = diffInfo.SizeBytes
commitInfo.Cancelled = diffInfo.Cancelled
commitInfos = append(commitInfos, commitInfo)
}
commitInfo := pfsserver.ReduceCommitInfos(commitInfos)
if len(commitInfo) < 1 {
// we should have caught this above
return nil, pfsserver.NewErrCommitNotFound(canonicalCommit.Repo.Name, canonicalCommit.ID)
}
if len(commitInfo) > 1 {
return nil, fmt.Errorf("multiple commitInfos, (this is likely a bug)")
}
result := commitInfo[0]
provenance, err := d.commitProvenance(canonicalCommit, shards)
if err != nil {
return nil, err
}
result.Provenance = provenance
return commitInfo[0], nil
}
func filterBlockRefs(filterShard *pfs.Shard, blockRefs []*pfs.BlockRef) []*pfs.BlockRef {
var result []*pfs.BlockRef
for _, blockRef := range blockRefs {
if pfsserver.BlockInShard(filterShard, blockRef.Block) {
result = append(result, blockRef)
}
}
return result
}
func (d *driver) getFileType(file *pfs.File, shard uint64) (pfs.FileType, error) {
commit, err := d.canonicalCommit(file.Commit)
if err != nil {
return pfs.FileType_FILE_TYPE_NONE, err
}
for commit != nil {
diffInfo, ok := d.diffs.get(client.NewDiff(commit.Repo.Name, commit.ID, shard))
if !ok {
return pfs.FileType_FILE_TYPE_NONE, pfsserver.NewErrCommitNotFound(commit.Repo.Name, commit.ID)
}
if _append, ok := diffInfo.Appends[path.Clean(file.Path)]; ok {
if _append.FileType == pfs.FileType_FILE_TYPE_NONE {
break
}
return _append.FileType, nil
}
commit = diffInfo.ParentCommit
}
return pfs.FileType_FILE_TYPE_NONE, nil
}