forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plan.go
751 lines (647 loc) · 17.6 KB
/
plan.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
package mirror
import (
"fmt"
"io"
"sort"
"sync"
"text/tabwriter"
"github.com/docker/distribution"
units "github.com/docker/go-units"
godigest "github.com/opencontainers/go-digest"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
"k8s.io/apimachinery/pkg/util/sets"
)
type retrieverError struct {
src, dst imageapi.DockerImageReference
err error
}
func (e retrieverError) Error() string {
return e.err.Error()
}
type repositoryWork struct {
registry *registryPlan
repository *repositoryPlan
stats struct {
mountOpportunities int
}
}
func (w *repositoryWork) calculateStats(existing sets.String) sets.String {
blobs := sets.NewString()
for i := range w.repository.blobs {
blobs.Insert(w.repository.blobs[i].blobs.UnsortedList()...)
}
w.stats.mountOpportunities = blobs.Intersection(existing).Len()
return blobs
}
type phase struct {
independent []repositoryWork
lock sync.Mutex
failed bool
}
func (p *phase) Failed() {
p.lock.Lock()
defer p.lock.Unlock()
p.failed = true
}
func (p *phase) IsFailed() bool {
p.lock.Lock()
defer p.lock.Unlock()
return p.failed
}
func (p *phase) calculateStats(existingBlobs map[string]sets.String) {
blobs := make(map[string]sets.String)
for i, work := range p.independent {
blobs[work.registry.name] = p.independent[i].calculateStats(existingBlobs[work.registry.name]).Union(blobs[work.registry.name])
}
for name, registryBlobs := range blobs {
existingBlobs[name] = existingBlobs[name].Union(registryBlobs)
}
}
type workPlan struct {
phases []phase
lock sync.Mutex
stats struct {
bytes int64
}
}
func (w *workPlan) calculateStats() {
blobs := make(map[string]sets.String)
for i := range w.phases {
w.phases[i].calculateStats(blobs)
}
}
func (w *workPlan) BytesCopied(bytes int64) {
w.lock.Lock()
defer w.lock.Unlock()
w.stats.bytes += bytes
}
func (w *workPlan) Print(out io.Writer) {
tabw := tabwriter.NewWriter(out, 0, 0, 1, ' ', 0)
for i := range w.phases {
phase := &w.phases[i]
fmt.Fprintf(out, "phase %d:\n", i)
for _, unit := range phase.independent {
fmt.Fprintf(tabw, " %s\t%s\tblobs=%d\tmounts=%d\tmanifests=%d\tshared=%d\n", unit.registry.name, unit.repository.name, unit.repository.stats.sharedCount+unit.repository.stats.uniqueCount, unit.stats.mountOpportunities, unit.repository.manifests.stats.count, unit.repository.stats.sharedCount)
}
tabw.Flush()
}
}
type plan struct {
lock sync.Mutex
registries map[string]*registryPlan
errs []error
blobs map[godigest.Digest]distribution.Descriptor
manifests map[godigest.Digest]distribution.Manifest
work *workPlan
stats struct {
}
}
func newPlan() *plan {
return &plan{
registries: make(map[string]*registryPlan),
manifests: make(map[godigest.Digest]distribution.Manifest),
blobs: make(map[godigest.Digest]distribution.Descriptor),
}
}
func (p *plan) AddError(errs ...error) {
p.lock.Lock()
defer p.lock.Unlock()
p.errs = append(p.errs, errs...)
}
func (p *plan) RegistryPlan(name string) *registryPlan {
p.lock.Lock()
defer p.lock.Unlock()
plan, ok := p.registries[name]
if ok {
return plan
}
plan = ®istryPlan{
parent: p,
name: name,
blobsByRepo: make(map[godigest.Digest]string),
}
p.registries[name] = plan
return plan
}
func (p *plan) CacheManifest(digest godigest.Digest, manifest distribution.Manifest) {
p.lock.Lock()
defer p.lock.Unlock()
if _, ok := p.manifests[digest]; ok {
return
}
p.manifests[digest] = manifest
}
func (p *plan) GetManifest(digest godigest.Digest) (distribution.Manifest, bool) {
p.lock.Lock()
defer p.lock.Unlock()
existing, ok := p.manifests[digest]
return existing, ok
}
func (p *plan) CacheBlob(blob distribution.Descriptor) {
p.lock.Lock()
defer p.lock.Unlock()
if existing, ok := p.blobs[blob.Digest]; ok && existing.Size > 0 {
return
}
p.blobs[blob.Digest] = blob
}
func (p *plan) GetBlob(digest godigest.Digest) distribution.Descriptor {
p.lock.Lock()
defer p.lock.Unlock()
return p.blobs[digest]
}
func (p *plan) RegistryNames() sets.String {
p.lock.Lock()
defer p.lock.Unlock()
names := sets.NewString()
for name := range p.registries {
names.Insert(name)
}
return names
}
func (p *plan) Errors() []error {
var errs []error
for _, r := range p.registries {
for _, repo := range r.repositories {
errs = append(errs, repo.errs...)
}
}
errs = append(errs, p.errs...)
return errs
}
func (p *plan) BlobDescriptors(blobs sets.String) []distribution.Descriptor {
descriptors := make([]distribution.Descriptor, 0, len(blobs))
for s := range blobs {
if desc, ok := p.blobs[godigest.Digest(s)]; ok {
descriptors = append(descriptors, desc)
} else {
descriptors = append(descriptors, distribution.Descriptor{
Digest: godigest.Digest(s),
})
}
}
return descriptors
}
func (p *plan) Print(w io.Writer) {
for _, name := range p.RegistryNames().List() {
r := p.registries[name]
fmt.Fprintf(w, "%s/\n", name)
for _, repoName := range r.RepositoryNames().List() {
repo := r.repositories[repoName]
fmt.Fprintf(w, " %s\n", repoName)
for _, err := range repo.errs {
fmt.Fprintf(w, " error: %s\n", err)
}
for _, blob := range repo.blobs {
fmt.Fprintf(w, " blobs:\n")
blobs := p.BlobDescriptors(blob.blobs)
sort.Slice(blobs, func(i, j int) bool {
if blobs[i].Size == blobs[j].Size {
return blobs[i].Digest.String() < blobs[j].Digest.String()
}
return blobs[i].Size < blobs[j].Size
})
for _, b := range blobs {
if size := b.Size; size > 0 {
fmt.Fprintf(w, " %s %s %s\n", blob.fromRef, b.Digest, units.BytesSize(float64(size)))
} else {
fmt.Fprintf(w, " %s %s\n", blob.fromRef, b.Digest)
}
}
}
fmt.Fprintf(w, " manifests:\n")
for _, s := range repo.manifests.digestCopies {
fmt.Fprintf(w, " %s\n", s)
}
for _, digest := range repo.manifests.inputDigests().List() {
tags := repo.manifests.digestsToTags[godigest.Digest(digest)]
for _, s := range tags.List() {
fmt.Fprintf(w, " %s -> %s\n", digest, s)
}
}
}
totalSize := r.stats.uniqueSize + r.stats.sharedSize
if totalSize > 0 {
fmt.Fprintf(w, " stats: shared=%d unique=%d size=%s ratio=%.2f\n", r.stats.sharedCount, r.stats.uniqueCount, units.BytesSize(float64(totalSize)), float32(r.stats.uniqueSize)/float32(totalSize))
} else {
fmt.Fprintf(w, " stats: shared=%d unique=%d size=%s\n", r.stats.sharedCount, r.stats.uniqueCount, units.BytesSize(float64(totalSize)))
}
}
}
func (p *plan) trim() {
for name, registry := range p.registries {
if registry.trim() {
delete(p.registries, name)
}
}
}
func (p *plan) calculateStats() {
for _, registry := range p.registries {
registry.calculateStats()
}
}
type registryPlan struct {
parent *plan
name string
lock sync.Mutex
repositories map[string]*repositoryPlan
blobsByRepo map[godigest.Digest]string
stats struct {
uniqueSize int64
sharedSize int64
uniqueCount int32
sharedCount int32
}
}
func (p *registryPlan) AssociateBlob(digest godigest.Digest, repo string) {
p.lock.Lock()
defer p.lock.Unlock()
p.blobsByRepo[digest] = repo
}
func (p *registryPlan) MountFrom(digest godigest.Digest) (string, bool) {
p.lock.Lock()
defer p.lock.Unlock()
repo, ok := p.blobsByRepo[digest]
return repo, ok
}
func (p *registryPlan) RepositoryNames() sets.String {
p.lock.Lock()
defer p.lock.Unlock()
names := sets.NewString()
for name := range p.repositories {
names.Insert(name)
}
return names
}
func (p *registryPlan) RepositoryPlan(name string) *repositoryPlan {
p.lock.Lock()
defer p.lock.Unlock()
if p.repositories == nil {
p.repositories = make(map[string]*repositoryPlan)
}
plan, ok := p.repositories[name]
if ok {
return plan
}
plan = &repositoryPlan{
parent: p,
name: name,
existingBlobs: sets.NewString(),
absentBlobs: sets.NewString(),
}
p.repositories[name] = plan
return plan
}
func (p *registryPlan) trim() bool {
for name, plan := range p.repositories {
if plan.trim() {
delete(p.repositories, name)
}
}
return len(p.repositories) == 0
}
func (p *registryPlan) calculateStats() {
counts := make(map[string]int)
for _, plan := range p.repositories {
plan.blobCounts(counts)
}
for _, plan := range p.repositories {
plan.calculateStats(counts)
}
for digest, count := range counts {
if count > 1 {
p.stats.sharedSize += p.parent.GetBlob(godigest.Digest(digest)).Size
p.stats.sharedCount++
} else {
p.stats.uniqueSize += p.parent.GetBlob(godigest.Digest(digest)).Size
p.stats.uniqueCount++
}
}
}
type repositoryPlan struct {
parent *registryPlan
name string
lock sync.Mutex
existingBlobs sets.String
absentBlobs sets.String
blobs []*repositoryBlobCopy
manifests *repositoryManifestPlan
errs []error
stats struct {
size int64
sharedSize int64
uniqueSize int64
sharedCount int32
uniqueCount int32
}
}
func (p *repositoryPlan) AddError(errs ...error) {
p.lock.Lock()
defer p.lock.Unlock()
p.errs = append(p.errs, errs...)
}
func (p *repositoryPlan) Blobs(from imageapi.DockerImageReference, t DestinationType, location string) *repositoryBlobCopy {
p.lock.Lock()
defer p.lock.Unlock()
for _, blob := range p.blobs {
if blob.fromRef == from {
return blob
}
}
p.blobs = append(p.blobs, &repositoryBlobCopy{
parent: p,
fromRef: from,
toRef: imageapi.DockerImageReference{Registry: p.parent.name, Name: p.name},
destinationType: t,
location: location,
blobs: sets.NewString(),
})
return p.blobs[len(p.blobs)-1]
}
func (p *repositoryPlan) ExpectBlob(digest godigest.Digest) {
p.lock.Lock()
defer p.lock.Unlock()
p.absentBlobs.Delete(digest.String())
p.existingBlobs.Insert(digest.String())
}
func (p *repositoryPlan) Manifests(destinationType DestinationType) *repositoryManifestPlan {
p.lock.Lock()
defer p.lock.Unlock()
if p.manifests == nil {
p.manifests = &repositoryManifestPlan{
parent: p,
toRef: imageapi.DockerImageReference{Registry: p.parent.name, Name: p.name},
destinationType: destinationType,
digestsToTags: make(map[godigest.Digest]sets.String),
digestCopies: sets.NewString(),
}
}
return p.manifests
}
func (p *repositoryPlan) blobCounts(registryCounts map[string]int) {
for i := range p.blobs {
for digest := range p.blobs[i].blobs {
registryCounts[digest]++
}
}
}
func (p *repositoryPlan) trim() bool {
var blobs []*repositoryBlobCopy
for _, blob := range p.blobs {
if blob.trim() {
continue
}
blobs = append(blobs, blob)
}
p.blobs = blobs
if p.manifests != nil {
if p.manifests.trim() {
p.manifests = nil
}
}
return len(p.blobs) == 0 && p.manifests == nil
}
func (p *repositoryPlan) calculateStats(registryCounts map[string]int) {
p.manifests.calculateStats()
blobs := sets.NewString()
for i := range p.blobs {
for digest := range p.blobs[i].blobs {
blobs.Insert(digest)
}
p.blobs[i].calculateStats()
p.stats.size += p.blobs[i].stats.size
}
for digest := range blobs {
count := registryCounts[digest]
if count > 1 {
p.stats.sharedSize += p.parent.parent.GetBlob(godigest.Digest(digest)).Size
p.stats.sharedCount++
} else {
p.stats.uniqueSize += p.parent.parent.GetBlob(godigest.Digest(digest)).Size
p.stats.uniqueCount++
}
}
}
type repositoryBlobCopy struct {
parent *repositoryPlan
fromRef imageapi.DockerImageReference
toRef imageapi.DockerImageReference
destinationType DestinationType
location string
lock sync.Mutex
from distribution.BlobService
to distribution.BlobService
blobs sets.String
stats struct {
size int64
averageSize int64
}
}
func (p *repositoryBlobCopy) AlreadyExists(blob distribution.Descriptor) {
p.parent.parent.parent.CacheBlob(blob)
p.parent.parent.AssociateBlob(blob.Digest, p.parent.name)
p.parent.ExpectBlob(blob.Digest)
p.lock.Lock()
defer p.lock.Unlock()
p.blobs.Delete(blob.Digest.String())
}
func (p *repositoryBlobCopy) Copy(blob distribution.Descriptor, from, to distribution.BlobService) {
p.parent.parent.parent.CacheBlob(blob)
p.lock.Lock()
defer p.lock.Unlock()
if p.from == nil {
p.from = from
}
if p.to == nil {
p.to = to
}
p.blobs.Insert(blob.Digest.String())
}
func (p *repositoryBlobCopy) trim() bool {
return len(p.blobs) == 0
}
func (p *repositoryBlobCopy) calculateStats() {
for digest := range p.blobs {
p.stats.size += p.parent.parent.parent.GetBlob(godigest.Digest(digest)).Size
}
if len(p.blobs) > 0 {
p.stats.averageSize = p.stats.size / int64(len(p.blobs))
}
}
type repositoryManifestPlan struct {
parent *repositoryPlan
toRef imageapi.DockerImageReference
destinationType DestinationType
lock sync.Mutex
to distribution.ManifestService
toBlobs distribution.BlobService
digestsToTags map[godigest.Digest]sets.String
digestCopies sets.String
stats struct {
count int
}
}
func (p *repositoryManifestPlan) Copy(srcDigest godigest.Digest, srcManifest distribution.Manifest, tags []string, to distribution.ManifestService, toBlobs distribution.BlobService) {
p.parent.parent.parent.CacheManifest(srcDigest, srcManifest)
p.lock.Lock()
defer p.lock.Unlock()
if p.to == nil {
p.to = to
}
if p.toBlobs == nil {
p.toBlobs = toBlobs
}
if len(tags) == 0 {
p.digestCopies.Insert(srcDigest.String())
return
}
allTags := p.digestsToTags[srcDigest]
if allTags == nil {
allTags = sets.NewString()
p.digestsToTags[srcDigest] = allTags
}
allTags.Insert(tags...)
}
func (p *repositoryManifestPlan) inputDigests() sets.String {
p.lock.Lock()
defer p.lock.Unlock()
names := sets.NewString()
for digest := range p.digestsToTags {
names.Insert(digest.String())
}
return names
}
func (p *repositoryManifestPlan) trim() bool {
for digest, tags := range p.digestsToTags {
if len(tags) == 0 {
delete(p.digestsToTags, digest)
}
}
return len(p.digestCopies) == 0 && len(p.digestsToTags) == 0
}
func (p *repositoryManifestPlan) calculateStats() {
p.stats.count += len(p.digestCopies)
for _, tags := range p.digestsToTags {
p.stats.count += len(tags)
}
}
// Greedy turns a plan into parallizable work by taking one repo at a time. It guarantees
// that no two phases in the plan attempt to upload the same blob at the same time. In the
// worst case each phase has one unit of work.
func Greedy(plan *plan) *workPlan {
remaining := make(map[string]map[string]repositoryWork)
for name, registry := range plan.registries {
work := make(map[string]repositoryWork)
remaining[name] = work
for repoName, repository := range registry.repositories {
work[repoName] = repositoryWork{
registry: registry,
repository: repository,
}
}
}
alreadyUploaded := make(map[string]sets.String)
var phases []phase
for len(remaining) > 0 {
var independent []repositoryWork
for name, registry := range remaining {
// we can always take any repository that has no shared layers
if found := takeIndependent(registry); len(found) > 0 {
independent = append(independent, found...)
}
exists := alreadyUploaded[name]
if exists == nil {
exists = sets.NewString()
alreadyUploaded[name] = exists
}
// take the most shared repositories and any that don't overlap with it
independent = append(independent, takeMostSharedWithoutOverlap(registry, exists)...)
if len(registry) == 0 {
delete(remaining, name)
}
}
for _, work := range independent {
repositoryPlanAddAllExcept(work.repository, alreadyUploaded[work.registry.name], nil)
}
phases = append(phases, phase{independent: independent})
}
work := &workPlan{
phases: phases,
}
work.calculateStats()
return work
}
func takeIndependent(all map[string]repositoryWork) []repositoryWork {
var work []repositoryWork
for k, v := range all {
if v.repository.stats.sharedCount == 0 {
delete(all, k)
work = append(work, v)
}
}
return work
}
type keysWithCount struct {
name string
count int
}
// takeMostSharedWithoutOverlap is a greedy algorithm that finds the repositories with the
// most shared layers that do not overlap. It will always return at least one unit of work.
func takeMostSharedWithoutOverlap(all map[string]repositoryWork, alreadyUploaded sets.String) []repositoryWork {
keys := make([]keysWithCount, 0, len(all))
for k, v := range all {
keys = append(keys, keysWithCount{name: k, count: int(v.repository.stats.sharedCount)})
}
sort.Slice(keys, func(i, j int) bool { return keys[i].count > keys[j].count })
// from the set of possible work, ordered from most shared to least shared, take:
// 1. the first available unit of work
// 2. any other unit of work that does not have overlapping shared blobs
uploadingBlobs := sets.NewString()
var work []repositoryWork
for _, key := range keys {
name := key.name
next, ok := all[name]
if !ok {
continue
}
if repositoryPlanHasAnyBlobs(next.repository, uploadingBlobs) {
continue
}
repositoryPlanAddAllExcept(next.repository, uploadingBlobs, alreadyUploaded)
delete(all, name)
work = append(work, next)
}
return work
}
func repositoryPlanAddAllExcept(plan *repositoryPlan, blobs sets.String, ignore sets.String) {
for i := range plan.blobs {
for key := range plan.blobs[i].blobs {
if !ignore.Has(key) {
blobs.Insert(key)
}
}
}
}
func repositoryPlanHasAnyBlobs(plan *repositoryPlan, blobs sets.String) bool {
for i := range plan.blobs {
if stringsIntersects(blobs, plan.blobs[i].blobs) {
return true
}
}
return false
}
func stringsIntersects(a, b sets.String) bool {
for key := range a {
if _, ok := b[key]; ok {
return true
}
}
return false
}
func takeOne(all map[string]repositoryWork) []repositoryWork {
for k, v := range all {
delete(all, k)
return []repositoryWork{v}
}
return nil
}