Skip to content

Commit e22f7cb

Browse files
committed
Pass the last paging flag to storage drivers
Storage drivers may be able to take advantage of the hint to start their walk more efficiently. For S3: The API takes a start-after parameter. Registries with many repositories can drastically reduce calls to s3 by telling s3 to only list results lexographically after the last parameter. For the fallback: We can start deeper in the tree and avoid statting the files and directories before the hint in a walk. For a filesystem this improves performance a little, but many of the API based drivers are currently treated like a filesystem, so this drastically improves the performance of GCP and Azure blob. Signed-off-by: James Hewitt <[email protected]>
1 parent 3a44c2e commit e22f7cb

13 files changed

Lines changed: 390 additions & 63 deletions

File tree

‎registry/storage/catalog.go‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ func (reg *registry) Repositories(ctx context.Context, repos []string, last stri
2727
return 0, err
2828
}
2929

30+
startAfter := ""
31+
if last != "" {
32+
startAfter, err = pathFor(manifestsPathSpec{name: last})
33+
if err != nil {
34+
return 0, err
35+
}
36+
}
37+
3038
err = reg.blobStore.driver.Walk(ctx, root, func(fileInfo driver.FileInfo) error {
3139
err := handleRepository(fileInfo, root, last, func(repoPath string) error {
3240
repos[foundRepos] = repoPath
@@ -40,11 +48,11 @@ func (reg *registry) Repositories(ctx context.Context, repos []string, last stri
4048
// if we've filled our slice, no need to walk any further
4149
if foundRepos == len(repos) {
4250
filledBuffer = true
43-
return driver.ErrSkipDir
51+
return driver.ErrFilledBuffer
4452
}
4553

4654
return nil
47-
})
55+
}, driver.WithStartAfterHint(startAfter))
4856

4957
if err != nil {
5058
return foundRepos, err
@@ -136,8 +144,9 @@ func compareReplaceInline(s1, s2 string, old, new byte) int {
136144

137145
// handleRepository calls function fn with a repository path if fileInfo
138146
// has a path of a repository under root and that it is lexographically
139-
// after last. Otherwise, it will return ErrSkipDir. This should be used
140-
// with Walk to do handling with repositories in a storage.
147+
// after last. Otherwise, it will return ErrSkipDir or ErrFilledBuffer.
148+
// These should be used with Walk to do handling with repositories in a
149+
// storage.
141150
func handleRepository(fileInfo driver.FileInfo, root, last string, fn func(repoPath string) error) error {
142151
filePath := fileInfo.Path()
143152

‎registry/storage/catalog_test.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,11 @@ func TestCatalogInParts(t *testing.T) {
151151
lastRepo = p[len(p)-1]
152152
numFilled, err = env.registry.Repositories(env.ctx, p, lastRepo)
153153

154-
if err != io.EOF || numFilled != len(p) {
154+
if numFilled != len(p) {
155+
t.Errorf("Expected more values in catalog")
156+
}
157+
158+
if err != io.EOF {
155159
t.Errorf("Expected end of catalog")
156160
}
157161

‎registry/storage/driver/azure/azure.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,8 @@ func (d *driver) URLFor(ctx context.Context, path string, options map[string]int
385385

386386
// Walk traverses a filesystem defined within driver, starting
387387
// from the given path, calling f on each file and directory
388-
func (d *driver) Walk(ctx context.Context, path string, f storagedriver.WalkFn) error {
389-
return storagedriver.WalkFallback(ctx, d, path, f)
388+
func (d *driver) Walk(ctx context.Context, path string, f storagedriver.WalkFn, options ...func(*storagedriver.WalkOptions)) error {
389+
return storagedriver.WalkFallback(ctx, d, path, f, options...)
390390
}
391391

392392
// directDescendants will find direct descendants (blobs or virtual containers)

‎registry/storage/driver/base/base.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,13 @@ func (base *Base) URLFor(ctx context.Context, path string, options map[string]in
226226
}
227227

228228
// Walk wraps Walk of underlying storage driver.
229-
func (base *Base) Walk(ctx context.Context, path string, f storagedriver.WalkFn) error {
229+
func (base *Base) Walk(ctx context.Context, path string, f storagedriver.WalkFn, options ...func(*storagedriver.WalkOptions)) error {
230230
ctx, done := dcontext.WithTrace(ctx)
231231
defer done("%s.Walk(%q)", base.Name(), path)
232232

233233
if !storagedriver.PathRegexp.MatchString(path) && path != "/" {
234234
return storagedriver.InvalidPathError{Path: path, DriverName: base.StorageDriver.Name()}
235235
}
236236

237-
return base.setDriverName(base.StorageDriver.Walk(ctx, path, f))
237+
return base.setDriverName(base.StorageDriver.Walk(ctx, path, f, options...))
238238
}

‎registry/storage/driver/filesystem/driver.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ func (d *driver) URLFor(ctx context.Context, path string, options map[string]int
290290

291291
// Walk traverses a filesystem defined within driver, starting
292292
// from the given path, calling f on each file and directory
293-
func (d *driver) Walk(ctx context.Context, path string, f storagedriver.WalkFn) error {
294-
return storagedriver.WalkFallback(ctx, d, path, f)
293+
func (d *driver) Walk(ctx context.Context, path string, f storagedriver.WalkFn, options ...func(*storagedriver.WalkOptions)) error {
294+
return storagedriver.WalkFallback(ctx, d, path, f, options...)
295295
}
296296

297297
// fullPath returns the absolute path of a key within the Driver's storage.

‎registry/storage/driver/gcs/gcs.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,8 +849,8 @@ func (d *driver) URLFor(ctx context.Context, path string, options map[string]int
849849

850850
// Walk traverses a filesystem defined within driver, starting
851851
// from the given path, calling f on each file
852-
func (d *driver) Walk(ctx context.Context, path string, f storagedriver.WalkFn) error {
853-
return storagedriver.WalkFallback(ctx, d, path, f)
852+
func (d *driver) Walk(ctx context.Context, path string, f storagedriver.WalkFn, options ...func(*storagedriver.WalkOptions)) error {
853+
return storagedriver.WalkFallback(ctx, d, path, f, options...)
854854
}
855855

856856
func startSession(client *http.Client, bucket string, name string) (uri string, err error) {

‎registry/storage/driver/inmemory/driver.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@ func (d *driver) URLFor(ctx context.Context, path string, options map[string]int
244244

245245
// Walk traverses a filesystem defined within driver, starting
246246
// from the given path, calling f on each file and directory
247-
func (d *driver) Walk(ctx context.Context, path string, f storagedriver.WalkFn) error {
248-
return storagedriver.WalkFallback(ctx, d, path, f)
247+
func (d *driver) Walk(ctx context.Context, path string, f storagedriver.WalkFn, options ...func(*storagedriver.WalkOptions)) error {
248+
return storagedriver.WalkFallback(ctx, d, path, f, options...)
249249
}
250250

251251
type writer struct {

‎registry/storage/driver/s3-aws/s3.go‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,21 +1040,21 @@ func (d *driver) URLFor(ctx context.Context, path string, options map[string]int
10401040

10411041
// Walk traverses a filesystem defined within driver, starting
10421042
// from the given path, calling f on each file
1043-
func (d *driver) Walk(ctx context.Context, from string, f storagedriver.WalkFn) error {
1044-
var objectCount int64
1045-
if err := d.doWalk(ctx, &objectCount, from, f); err != nil {
1046-
return err
1043+
func (d *driver) Walk(ctx context.Context, from string, f storagedriver.WalkFn, options ...func(*storagedriver.WalkOptions)) error {
1044+
walkOptions := &storagedriver.WalkOptions{}
1045+
for _, o := range options {
1046+
o(walkOptions)
10471047
}
10481048

1049-
// S3 doesn't have the concept of empty directories, so it'll return path not found if there are no objects
1050-
if objectCount == 0 {
1051-
return storagedriver.PathNotFoundError{Path: from}
1049+
var objectCount int64
1050+
if err := d.doWalk(ctx, &objectCount, from, walkOptions.StartAfterHint, f); err != nil {
1051+
return err
10521052
}
10531053

10541054
return nil
10551055
}
10561056

1057-
func (d *driver) doWalk(parentCtx context.Context, objectCount *int64, from string, f storagedriver.WalkFn) error {
1057+
func (d *driver) doWalk(parentCtx context.Context, objectCount *int64, from string, startAfter string, f storagedriver.WalkFn) error {
10581058
var (
10591059
retError error
10601060
// the most recent directory walked for de-duping
@@ -1075,13 +1075,14 @@ func (d *driver) doWalk(parentCtx context.Context, objectCount *int64, from stri
10751075
}
10761076

10771077
listObjectsInput := &s3.ListObjectsV2Input{
1078-
Bucket: aws.String(d.Bucket),
1079-
Prefix: aws.String(d.s3Path(path)),
1080-
MaxKeys: aws.Int64(listMax),
1078+
Bucket: aws.String(d.Bucket),
1079+
Prefix: aws.String(d.s3Path(path)),
1080+
MaxKeys: aws.Int64(listMax),
1081+
StartAfter: aws.String(d.s3Path(startAfter)),
10811082
}
10821083

10831084
ctx, done := dcontext.WithTrace(parentCtx)
1084-
defer done("s3aws.ListObjectsV2Pages(%s)", path)
1085+
defer done("s3aws.ListObjectsV2PagesWithContext(%s)", listObjectsInput)
10851086

10861087
// When the "delimiter" argument is omitted, the S3 list API will list all objects in the bucket
10871088
// recursively, omitting directory paths. Objects are listed in sorted, depth-first order so we
@@ -1133,11 +1134,10 @@ func (d *driver) doWalk(parentCtx context.Context, objectCount *int64, from stri
11331134

11341135
if err != nil {
11351136
if err == storagedriver.ErrSkipDir {
1136-
if walkInfo.IsDir() {
1137-
prevSkipDir = walkInfo.Path()
1138-
continue
1139-
}
1140-
// is file, stop gracefully
1137+
prevSkipDir = walkInfo.Path()
1138+
continue
1139+
}
1140+
if err == storagedriver.ErrFilledBuffer {
11411141
return false
11421142
}
11431143
retError = err
@@ -1187,7 +1187,7 @@ func directoryDiff(prev, current string) []string {
11871187
parent := current
11881188
for {
11891189
parent = filepath.Dir(parent)
1190-
if parent == "/" || parent == prev || strings.HasPrefix(prev, parent) {
1190+
if parent == "/" || parent == prev || strings.HasPrefix(prev+"/", parent+"/") {
11911191
break
11921192
}
11931193
paths = append(paths, parent)

‎registry/storage/driver/s3-aws/s3_test.go‎

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ func TestWalk(t *testing.T) {
491491

492492
fileset := []string{
493493
"/file1",
494+
"/folder1-suffix/file1",
494495
"/folder1/file1",
495496
"/folder2/file1",
496497
"/folder3/subfolder1/subfolder1/file1",
@@ -524,18 +525,23 @@ func TestWalk(t *testing.T) {
524525
}
525526
}()
526527

528+
noopFn := func(fileInfo storagedriver.FileInfo) error { return nil }
529+
527530
tcs := []struct {
528531
name string
529532
fn storagedriver.WalkFn
530533
from string
534+
options []func(*storagedriver.WalkOptions)
531535
expected []string
532536
err bool
533537
}{
534538
{
535539
name: "walk all",
536-
fn: func(fileInfo storagedriver.FileInfo) error { return nil },
540+
fn: noopFn,
537541
expected: []string{
538542
"/file1",
543+
"/folder1-suffix",
544+
"/folder1-suffix/file1",
539545
"/folder1",
540546
"/folder1/file1",
541547
"/folder2",
@@ -564,6 +570,8 @@ func TestWalk(t *testing.T) {
564570
},
565571
expected: []string{
566572
"/file1",
573+
"/folder1-suffix",
574+
"/folder1-suffix/file1",
567575
"/folder1",
568576
"/folder1/file1",
569577
"/folder2",
@@ -574,22 +582,101 @@ func TestWalk(t *testing.T) {
574582
"/folder4/file1",
575583
},
576584
},
585+
{
586+
name: "start late without from",
587+
fn: noopFn,
588+
options: []func(*storagedriver.WalkOptions){
589+
storagedriver.WithStartAfterHint("/folder3/subfolder1/subfolder1/file1"),
590+
},
591+
expected: []string{
592+
// start late
593+
"/folder3",
594+
"/folder3/subfolder2",
595+
"/folder3/subfolder2/subfolder1",
596+
"/folder3/subfolder2/subfolder1/file1",
597+
"/folder4",
598+
"/folder4/file1",
599+
},
600+
err: false,
601+
},
602+
{
603+
name: "start late with from",
604+
fn: noopFn,
605+
from: "/folder3",
606+
options: []func(*storagedriver.WalkOptions){
607+
storagedriver.WithStartAfterHint("/folder3/subfolder1/subfolder1/file1"),
608+
},
609+
expected: []string{
610+
// start late
611+
"/folder3/subfolder2",
612+
"/folder3/subfolder2/subfolder1",
613+
"/folder3/subfolder2/subfolder1/file1",
614+
},
615+
err: false,
616+
},
617+
{
618+
name: "start after from",
619+
fn: noopFn,
620+
from: "/folder1",
621+
options: []func(*storagedriver.WalkOptions){
622+
storagedriver.WithStartAfterHint("/folder2"),
623+
},
624+
expected: []string{},
625+
err: false,
626+
},
627+
{
628+
name: "start matches from",
629+
fn: noopFn,
630+
from: "/folder3",
631+
options: []func(*storagedriver.WalkOptions){
632+
storagedriver.WithStartAfterHint("/folder3"),
633+
},
634+
expected: []string{
635+
"/folder3/subfolder1",
636+
"/folder3/subfolder1/subfolder1",
637+
"/folder3/subfolder1/subfolder1/file1",
638+
"/folder3/subfolder2",
639+
"/folder3/subfolder2/subfolder1",
640+
"/folder3/subfolder2/subfolder1/file1",
641+
},
642+
err: false,
643+
},
644+
{
645+
name: "start doesn't exist",
646+
fn: noopFn,
647+
from: "/folder3",
648+
options: []func(*storagedriver.WalkOptions){
649+
storagedriver.WithStartAfterHint("/folder3/notafolder/notafile"),
650+
},
651+
expected: []string{
652+
"/folder3/subfolder1",
653+
"/folder3/subfolder1/subfolder1",
654+
"/folder3/subfolder1/subfolder1/file1",
655+
"/folder3/subfolder2",
656+
"/folder3/subfolder2/subfolder1",
657+
"/folder3/subfolder2/subfolder1/file1",
658+
},
659+
err: false,
660+
},
577661
{
578662
name: "stop early",
579663
fn: func(fileInfo storagedriver.FileInfo) error {
580664
if fileInfo.Path() == "/folder1/file1" {
581-
return storagedriver.ErrSkipDir
665+
return storagedriver.ErrFilledBuffer
582666
}
583667
return nil
584668
},
585669
expected: []string{
586670
"/file1",
671+
"/folder1-suffix",
672+
"/folder1-suffix/file1",
587673
"/folder1",
588674
"/folder1/file1",
589675
// stop early
590676
},
591677
err: false,
592678
},
679+
593680
{
594681
name: "error",
595682
fn: func(fileInfo storagedriver.FileInfo) error {
@@ -602,7 +689,7 @@ func TestWalk(t *testing.T) {
602689
},
603690
{
604691
name: "from folder",
605-
fn: func(fileInfo storagedriver.FileInfo) error { return nil },
692+
fn: noopFn,
606693
expected: []string{
607694
"/folder1/file1",
608695
},
@@ -619,7 +706,7 @@ func TestWalk(t *testing.T) {
619706
err := drvr.Walk(context.Background(), tc.from, func(fileInfo storagedriver.FileInfo) error {
620707
walked = append(walked, fileInfo.Path())
621708
return tc.fn(fileInfo)
622-
})
709+
}, tc.options...)
623710
if tc.err && err == nil {
624711
t.Fatalf("expected err")
625712
}

‎registry/storage/driver/storagedriver.go‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ func (version Version) Minor() uint {
3232
// CurrentVersion is the current storage driver Version.
3333
const CurrentVersion Version = "0.1"
3434

35+
// WalkOptions provides options to the walk function that may adjust its behaviour
36+
type WalkOptions struct {
37+
// If StartAfterHint is set, the walk may start with the first item lexographically
38+
// after the hint, but it is not guaranteed and drivers may start the walk from the path.
39+
StartAfterHint string
40+
}
41+
42+
func WithStartAfterHint(startAfterHint string) func(*WalkOptions) {
43+
return func(s *WalkOptions) {
44+
s.StartAfterHint = startAfterHint
45+
}
46+
}
47+
3548
// StorageDriver defines methods that a Storage Driver must implement for a
3649
// filesystem-like key/value object storage. Storage Drivers are automatically
3750
// registered via an internal registration mechanism, and generally created
@@ -88,8 +101,9 @@ type StorageDriver interface {
88101
// from the given path, calling f on each file.
89102
// If the returned error from the WalkFn is ErrSkipDir and fileInfo refers
90103
// to a directory, the directory will not be entered and Walk
91-
// will continue the traversal. If fileInfo refers to a normal file, processing stops
92-
Walk(ctx context.Context, path string, f WalkFn) error
104+
// will continue the traversal.
105+
// If the returned error from the WalkFn is ErrFilledBuffer, processing stops.
106+
Walk(ctx context.Context, path string, f WalkFn, options ...func(*WalkOptions)) error
93107
}
94108

95109
// FileWriter provides an abstraction for an opened writable file-like object in

0 commit comments

Comments
 (0)