Skip to content

Commit 1b35697

Browse files
committed
fix global prefix handling with marker
1 parent fa71646 commit 1b35697

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

backends/s3/marker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (b *Backend) setMarker(ctx context.Context, name, etag string, isDel bool)
1818
}
1919
nanos := time.Now().UnixNano()
2020
s := fmt.Sprintf("%s:%s:%d:%v", name, etag, nanos, isDel)
21-
_, err := b.doStore(ctx, UpdateMarkerFilename, []byte(s))
21+
_, err := b.doStore(ctx, b.markerName, []byte(s))
2222
if err != nil {
2323
return err
2424
}

backends/s3/s3.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,24 @@ func (o Options) Check() error {
106106
}
107107

108108
type Backend struct {
109-
opt Options
110-
config *minio.Options
111-
client *minio.Client
112-
log logr.Logger
109+
opt Options
110+
config *minio.Options
111+
client *minio.Client
112+
log logr.Logger
113+
markerName string
113114

114115
mu sync.Mutex
115116
lastMarker string
116117
lastList simpleblob.BlobList
117118
lastTime time.Time
118119
}
119120

120-
func (b *Backend) List(ctx context.Context, prefix string) (simpleblob.BlobList, error) {
121-
// Prepend global prefix
122-
prefix = b.prependGlobalPrefix(prefix)
121+
func (b *Backend) List(ctx context.Context, prefix string) (blobList simpleblob.BlobList, err error) {
122+
// Handle global prefix
123+
combinedPrefix := b.prependGlobalPrefix(prefix)
123124

124125
if !b.opt.UseUpdateMarker {
125-
return b.doList(ctx, prefix)
126+
return b.doList(ctx, combinedPrefix)
126127
}
127128

128129
m, err := b.Load(ctx, UpdateMarkerFilename)
@@ -144,7 +145,7 @@ func (b *Backend) List(ctx context.Context, prefix string) (simpleblob.BlobList,
144145
return blobs.WithPrefix(prefix), nil
145146
}
146147

147-
blobs, err = b.doList(ctx, "") // We want to cache all, so no prefix
148+
blobs, err = b.doList(ctx, b.opt.GlobalPrefix) // We want to cache all, so no prefix
148149
if err != nil {
149150
return nil, err
150151
}
@@ -162,7 +163,9 @@ func (b *Backend) doList(ctx context.Context, prefix string) (simpleblob.BlobLis
162163
var blobs simpleblob.BlobList
163164

164165
// Runes to strip from blob names for GlobalPrefix
165-
gpEndRune := len(b.opt.GlobalPrefix)
166+
// This is fine, because we can trust the API to only return with the prefix.
167+
// TODO: trust but verify
168+
gpEndIndex := len(b.opt.GlobalPrefix)
166169

167170
objCh := b.client.ListObjects(ctx, b.opt.Bucket, minio.ListObjectsOptions{
168171
Prefix: prefix,
@@ -177,14 +180,14 @@ func (b *Backend) doList(ctx context.Context, prefix string) (simpleblob.BlobLis
177180

178181
metricCalls.WithLabelValues("list").Inc()
179182
metricLastCallTimestamp.WithLabelValues("list").SetToCurrentTime()
180-
if obj.Key == UpdateMarkerFilename {
183+
if obj.Key == b.markerName {
181184
continue
182185
}
183186

184187
// Strip global prefix from blob
185188
blobName := obj.Key
186-
if gpEndRune > 0 {
187-
blobName = blobName[gpEndRune:]
189+
if gpEndIndex > 0 {
190+
blobName = blobName[gpEndIndex:]
188191
}
189192

190193
blobs = append(blobs, simpleblob.Blob{Name: blobName, Size: obj.Size})
@@ -378,10 +381,18 @@ func New(ctx context.Context, opt Options) (*Backend, error) {
378381
client: client,
379382
log: log,
380383
}
384+
b.setGlobalPrefix(opt.GlobalPrefix)
381385

382386
return b, nil
383387
}
384388

389+
// setGlobalPrefix updates the global prefix in b and the cached marker name,
390+
// so it can be dynamically changed in tests.
391+
func (b *Backend) setGlobalPrefix(prefix string) {
392+
b.opt.GlobalPrefix = prefix
393+
b.markerName = b.prependGlobalPrefix(UpdateMarkerFilename)
394+
}
395+
385396
// convertMinioError takes an error, possibly a minio.ErrorResponse
386397
// and turns it into a well known error when possible.
387398
// If error is not well known, it is returned as is.

backends/s3/s3_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func getBackend(ctx context.Context, t *testing.T) (b *Backend) {
6666
}
6767
}
6868
// This one is not returned by the List command
69-
err = b.client.RemoveObject(ctx, b.opt.Bucket, UpdateMarkerFilename, minio.RemoveObjectOptions{})
69+
err = b.client.RemoveObject(ctx, b.opt.Bucket, b.markerName, minio.RemoveObjectOptions{})
7070
require.NoError(t, err)
7171
}
7272
t.Cleanup(cleanup)
@@ -107,13 +107,19 @@ func TestBackend_globalprefix(t *testing.T) {
107107
defer cancel()
108108

109109
b := getBackend(ctx, t)
110-
b.opt.GlobalPrefix = "v5/"
110+
b.setGlobalPrefix("v5/")
111111

112112
tester.DoBackendTests(t, b)
113113
assert.Empty(t, b.lastMarker)
114+
}
114115

115-
b = getBackend(ctx, t)
116-
b.opt.GlobalPrefix = "v6/"
116+
func TestBackend_globalPrefixAndMarker(t *testing.T) {
117+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
118+
defer cancel()
119+
120+
// Start the backend over
121+
b := getBackend(ctx, t)
122+
b.setGlobalPrefix("v6/")
117123
b.opt.UseUpdateMarker = true
118124

119125
tester.DoBackendTests(t, b)

0 commit comments

Comments
 (0)