Skip to content

Commit e41a7ce

Browse files
committed
objstore: list object versions on common client and detect swift delete markers
Signed-off-by: Artem Torubarov <artem.torubarov@clyso.com>
1 parent 41d0332 commit e41a7ce

8 files changed

Lines changed: 92 additions & 44 deletions

File tree

‎pkg/objstore/s3_common.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import (
2828
)
2929

3030
func WrapS3common(client s3client.Client) Common {
31-
return &s3CommonClient{
31+
return commonClient{&s3CommonClient{
3232
client: client,
33-
}
33+
}}
3434
}
3535

3636
type s3CommonClient struct {

‎pkg/objstore/service.go‎

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,6 @@ func WithLimit(limit uint32) func(o *commonListOptions) {
104104
}
105105
}
106106

107-
// ObjectVersions lists versions of the object with exactly the given name,
108-
// skipping objects whose names only start with it.
109-
func ObjectVersions(ctx context.Context, client Common, bucket string, name string) iter.Seq2[CommonObjectInfo, error] {
110-
return func(yield func(CommonObjectInfo, error) bool) {
111-
for object, err := range client.ListObjects(ctx, bucket, WithPrefix(name), WithVersions()) {
112-
if err == nil && object.Key != name {
113-
continue
114-
}
115-
if !yield(object, err) {
116-
return
117-
}
118-
}
119-
}
120-
}
121-
122107
type commonObjectOptions struct {
123108
versionID string
124109
}
@@ -159,6 +144,12 @@ type commonGetter interface {
159144

160145
// Common - common object storage client interface
161146
type Common interface {
147+
CommonBase
148+
// ObjectVersions skips objects whose names only start with the given name.
149+
ObjectVersions(ctx context.Context, bucket string, name string) iter.Seq2[CommonObjectInfo, error]
150+
}
151+
152+
type CommonBase interface {
162153
BucketExists(ctx context.Context, bucket string) (bool, error)
163154
IsBucketVersioned(ctx context.Context, bucket string) (bool, error)
164155
ListBuckets(ctx context.Context) ([]string, error)
@@ -176,6 +167,23 @@ type Common interface {
176167
ListObjects(ctx context.Context, bucket string, opts ...func(o *commonListOptions)) iter.Seq2[CommonObjectInfo, error]
177168
}
178169

170+
type commonClient struct {
171+
CommonBase
172+
}
173+
174+
func (c commonClient) ObjectVersions(ctx context.Context, bucket string, name string) iter.Seq2[CommonObjectInfo, error] {
175+
return func(yield func(CommonObjectInfo, error) bool) {
176+
for object, err := range c.ListObjects(ctx, bucket, WithPrefix(name), WithVersions()) {
177+
if err == nil && object.Key != name {
178+
continue
179+
}
180+
if !yield(object, err) {
181+
return
182+
}
183+
}
184+
}
185+
}
186+
179187
// TODO:
180188
// TODO-DC: todos for dynamic credentials support:
181189
// - use DynamicCredeintilas Service everywhere instead of using Storage Config directly

‎pkg/objstore/service_test.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ import (
99
"github.com/stretchr/testify/require"
1010
)
1111

12-
// listStub lists objects as a storage does for the listing prefix.
1312
type listStub struct {
14-
Common
13+
CommonBase
1514
listed []CommonObjectInfo
1615
err error
1716
prefix string
@@ -49,7 +48,7 @@ func TestObjectVersions(t *testing.T) {
4948

5049
var versions []string
5150
var listErr error
52-
for object, err := range ObjectVersions(t.Context(), stub, "bucket", "a") {
51+
for object, err := range (commonClient{stub}).ObjectVersions(t.Context(), "bucket", "a") {
5352
if err != nil {
5453
listErr = err
5554
continue

‎pkg/objstore/swift_common.go‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ import (
2828
"github.com/gophercloud/gophercloud/v2/pagination"
2929
)
3030

31+
// https://docs.openstack.org/swift/latest/middleware.html#module-swift.common.middleware.versioned_writes.object_versioning
32+
const swiftDeleteMarkerContentType = "application/x-deleted;swift_versions_deleted=1"
33+
3134
func WrapSwiftCommon(client *gophercloud.ServiceClient) Common {
32-
return &swiftCommonClient{
35+
return commonClient{&swiftCommonClient{
3336
client: client,
34-
}
37+
}}
3538
}
3639

3740
type swiftCommonClient struct {
@@ -143,11 +146,12 @@ func (s *swiftCommonClient) ListObjects(ctx context.Context, bucket string, opts
143146

144147
for _, object := range objectList {
145148
objectInfo := CommonObjectInfo{
146-
LastModified: object.LastModified,
147-
Key: object.Name,
148-
VersionID: object.VersionID,
149-
Etag: object.Hash,
150-
Size: uint64(object.Bytes),
149+
LastModified: object.LastModified,
150+
Key: object.Name,
151+
VersionID: object.VersionID,
152+
Etag: object.Hash,
153+
Size: uint64(object.Bytes),
154+
IsDeleteMarker: object.ContentType == swiftDeleteMarkerContentType,
151155
}
152156
if !yield(objectInfo, nil) {
153157
return false, nil

‎service/worker/copy/copy.go‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (r *S3CopySvc) GetVersionInfo(ctx context.Context, user string, to File) ([
123123
if err != nil {
124124
return nil, fmt.Errorf("unable to get %s storage client: %w", to.Storage, err)
125125
}
126-
objects := objstore.ObjectVersions(ctx, storageClient, to.Bucket, to.Name)
126+
objects := storageClient.ObjectVersions(ctx, to.Bucket, to.Name)
127127

128128
result := []entity.ObjectVersionInfo{}
129129
for object, err := range objects {
@@ -227,7 +227,7 @@ func (r *S3CopySvc) ClearDestination(ctx context.Context, user string, to File,
227227
return nil
228228
}
229229

230-
objectIter := objstore.ObjectVersions(ctx, toClient, to.Bucket, to.Name)
230+
objectIter := toClient.ObjectVersions(ctx, to.Bucket, to.Name)
231231

232232
listErrs := []error{}
233233
namesAndVersions := []objstore.NameAndVersion{}
@@ -363,8 +363,7 @@ func (r *S3CopySvc) CopyObject(ctx context.Context, user string, from File, to F
363363
return fmt.Errorf("unable to get from object: %w", err)
364364
}
365365
defer fromObject.Close()
366-
// The source object can be deleted after the initial stat, before its content is requested.
367-
// Requests to the source returning not found mean the object is gone: return dom.ErrNotFound as for the initial stat.
366+
// the source object can be deleted after the initial stat
368367
stat, err := fromObject.Stat()
369368
if err != nil {
370369
if isNotFound(err) {
@@ -401,9 +400,8 @@ func (r *S3CopySvc) CopyObject(ctx context.Context, user string, from File, to F
401400
return nil
402401
}
403402

404-
// sourceReader reads the source object for PutObject and keeps the error of reading it,
405-
// so a failed source read is told apart from a failed destination write.
406-
// It does not expose minio.Object.ReadAt, so PutObject keeps reading the source sequentially, as for *minio.Object.
403+
// sourceReader keeps the source read error to tell it apart from a destination write error.
404+
// It hides minio.Object.ReadAt so PutObject reads sequentially.
407405
type sourceReader struct {
408406
obj *minio.Object
409407
err error
@@ -428,15 +426,14 @@ func (r *sourceReader) setErr(err error) {
428426
}
429427
}
430428

431-
// isDeleteMarkerVersion reports whether the file version is listed as a delete marker in the object versions list.
432429
func (r *S3CopySvc) isDeleteMarkerVersion(ctx context.Context, user string, file File) (bool, error) {
433430
client, err := r.clientRegistry.AsCommon(ctx, file.Storage, user)
434431
if err != nil {
435432
return false, fmt.Errorf("unable to get %s storage client: %w", file.Storage, err)
436433
}
437434
// the listing is read to the end: stopping it early leaks the minio-go listing goroutine
438435
isDeleteMarker := false
439-
for object, err := range objstore.ObjectVersions(ctx, client, file.Bucket, file.Name) {
436+
for object, err := range client.ObjectVersions(ctx, file.Bucket, file.Name) {
440437
if errors.Is(err, objstore.ErrBucketDoesntExist) {
441438
return false, nil
442439
}
@@ -450,7 +447,6 @@ func (r *S3CopySvc) isDeleteMarkerVersion(ctx context.Context, user string, file
450447
return isDeleteMarker, nil
451448
}
452449

453-
// isNotFound reports whether err is an S3 not found error response (NoSuchKey, NoSuchVersion, NoSuchBucket, etc.).
454450
func isNotFound(err error) bool {
455451
var errorResp minio.ErrorResponse
456452
return errors.As(err, &errorResp) && (errorResp.Code == minio.NoSuchKey || errorResp.StatusCode == http.StatusNotFound)

‎service/worker/handler/diff_handlers.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ func (r *DiffSvc) ProduceObjectTasks(ctx context.Context, diffID entity.DiffID,
351351
}
352352

353353
func (r *DiffSvc) AccountObjectVersions(ctx context.Context, diffID entity.DiffID,
354-
user string, location entity.DiffLocation, prefix string, ignoreEtags bool, ignoreSizes bool) error {
354+
user string, location entity.DiffLocation, object string, ignoreEtags bool, ignoreSizes bool) error {
355355
locationCount := len(diffID.Locations)
356356

357357
client, err := r.clients.AsCommon(ctx, location.Storage, user)
@@ -360,7 +360,7 @@ func (r *DiffSvc) AccountObjectVersions(ctx context.Context, diffID entity.DiffI
360360
}
361361

362362
versionIdx := uint64(0)
363-
objects := objstore.ObjectVersions(ctx, client, location.Bucket, prefix)
363+
objects := client.ObjectVersions(ctx, location.Bucket, object)
364364
for object, err := range objects {
365365
if err != nil {
366366
return fmt.Errorf("unable to list object versions: %w", err)
@@ -773,7 +773,7 @@ func (r *DiffSvc) RemoveDiffObjects(ctx context.Context, id entity.DiffFixID, lo
773773
listErrs := []error{}
774774
namesAndVersions := []objstore.NameAndVersion{}
775775
for _, objectName := range objectNames {
776-
versionIter := objstore.ObjectVersions(ctx, client, location.Bucket, objectName)
776+
versionIter := client.ObjectVersions(ctx, location.Bucket, objectName)
777777

778778
for versionInfo, err := range versionIter {
779779
if err != nil {

‎test/diff/suite_test.go‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,6 +2153,49 @@ var _ = Describe("Diff scenarious", Ordered, func() {
21532153
Expect(getCheckResponse.Check.IgnoreSizes).To(BeFalse())
21542154
Expect(getCheckResponse.Check.Versioned).To(BeTrue())
21552155
})
2156+
2157+
It("Should fail, object name is the start of another object name", func() {
2158+
objectPath := "name-prefix/a"
2159+
for _, name := range []string{objectPath, objectPath + "-b"} {
2160+
for _, bucket := range []string{bucket1Name, bucket2Name} {
2161+
for i := range 2 {
2162+
// bucket2 misses the latest version of the object
2163+
if bucket == bucket2Name && name == objectPath && i == 1 {
2164+
continue
2165+
}
2166+
content := []byte(fmt.Sprintf("%s %d", name, i))
2167+
err := storeClient.PutObject(ctx, bucket, name, bytes.NewReader(content), uint64(len(content)))
2168+
Expect(err).NotTo(HaveOccurred())
2169+
}
2170+
}
2171+
}
2172+
2173+
_, err := diffClient.Start(ctx, &pb.StartDiffCheckRequest{
2174+
Locations: locations,
2175+
User: CSyncUserKey,
2176+
})
2177+
Expect(err).NotTo(HaveOccurred())
2178+
2179+
var getCheckResponse *pb.GetDiffCheckReportResponse
2180+
Eventually(func(g Gomega) {
2181+
getCheckResponse, err = diffClient.GetReport(ctx, &pb.DiffCheckRequest{Locations: locations})
2182+
g.Expect(err).NotTo(HaveOccurred())
2183+
g.Expect(getCheckResponse).NotTo(BeNil())
2184+
g.Expect(getCheckResponse.Check).NotTo(BeNil())
2185+
g.Expect(getCheckResponse.Check.Ready).To(BeTrue())
2186+
}, 1*time.Minute, time.Millisecond*100).Should(Succeed())
2187+
Expect(getCheckResponse.Check.Consistent).To(BeFalse())
2188+
2189+
checkEntries, err := diffClient.GetReportEntries(ctx, &pb.GetDiffCheckReportEntriesRequest{
2190+
Locations: locations,
2191+
PageSize: 1000,
2192+
})
2193+
Expect(err).NotTo(HaveOccurred())
2194+
Expect(checkEntries.Entries).NotTo(BeEmpty())
2195+
for _, entry := range checkEntries.Entries {
2196+
Expect(entry.Object).To(Equal(objectPath))
2197+
}
2198+
})
21562199
})
21572200
}
21582201
})

‎test/versioned/history_test.go‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525

2626
const historyPrefix = "history/"
2727

28-
// histories of objects to migrate in addition to the generated ones: p - put a version, d - delete creating a delete marker.
29-
// Names of objects starting with the name of another object must not mix up their versions.
28+
// p - put a version, d - delete creating a delete marker.
29+
// Object names are prefixes of each other on purpose.
3030
var histories = map[string]string{
3131
historyPrefix + "a": "ppp",
3232
historyPrefix + "a-b": "ppp",
@@ -48,7 +48,6 @@ func fillHistories(ctx context.Context, client *minio.Client, bucket string) {
4848
}
4949
}
5050

51-
// expectSameHistories expects the destination objects to have the same versions as the source ones.
5251
// Delete marker version ids are not compared, as migration does not preserve them.
5352
func expectSameHistories(ctx context.Context, src *minio.Client, srcBucket string, dest *minio.Client, destBucket string) {
5453
srcVersions := listHistories(ctx, src, srcBucket)
@@ -59,7 +58,6 @@ func expectSameHistories(ctx context.Context, src *minio.Client, srcBucket strin
5958
Expect(listHistories(ctx, dest, destBucket)).To(Equal(srcVersions))
6059
}
6160

62-
// listHistories returns versions of objects from histories by object name, newest first.
6361
func listHistories(ctx context.Context, client *minio.Client, bucket string) map[string][]string {
6462
versions := map[string][]string{}
6563
for object := range client.ListObjects(ctx, bucket, minio.ListObjectsOptions{Prefix: historyPrefix, WithVersions: true, Recursive: true}) {

0 commit comments

Comments
 (0)