From d91ee440ba795db86b6f73ff4c0d1752037f4303 Mon Sep 17 00:00:00 2001 From: Egon Elbre Date: Mon, 11 Sep 2023 16:58:30 +0300 Subject: [PATCH] satellite/metabase: use constant for pending version Currently it wasn't quite clear what was a stub version and an actual version. Use a PendingVersion constant to make this distinction clear. Also use PendingVersion = NextVersion = 0, that way it's clearer that the version hasn't been yet determined. DefaultVersion = 1 might imply that the object will get that version once commited, however that will entirely depend on whether use-pending-objects is used or versioning is enabled or not. Change-Id: I21398141f97035c48c778f23b542266b834c44f1 --- satellite/metabase/commit.go | 6 ++- satellite/metabase/commit_test.go | 48 +++++++++---------- satellite/metabase/common.go | 3 ++ satellite/metabase/delete_bucket_test.go | 4 +- satellite/metabase/delete_objects_test.go | 10 ++-- satellite/metabase/delete_test.go | 6 +-- satellite/metabase/metabasetest/create.go | 3 +- .../metabase/pending_objects_iterator_test.go | 14 +++--- satellite/metainfo/endpoint_object.go | 2 +- 9 files changed, 50 insertions(+), 46 deletions(-) diff --git a/satellite/metabase/commit.go b/satellite/metabase/commit.go index df70d38f5..3ae915193 100644 --- a/satellite/metabase/commit.go +++ b/satellite/metabase/commit.go @@ -99,7 +99,7 @@ func (db *DB) BeginObjectNextVersion(ctx context.Context, opts BeginObjectNextVe if opts.UsePendingObjectsTable { object.Status = Pending - object.Version = DefaultVersion + object.Version = PendingVersion if err := db.db.QueryRowContext(ctx, ` INSERT INTO pending_objects ( @@ -683,6 +683,7 @@ func (db *DB) CommitObject(ctx context.Context, opts CommitObject) (object Objec totalEncryptedSize += int64(seg.EncryptedSize) } + const versionArgIndex = 3 args := []interface{}{ opts.ProjectID, []byte(opts.BucketName), opts.ObjectKey, opts.Version, opts.StreamID, len(segments), @@ -746,7 +747,8 @@ func (db *DB) CommitObject(ctx context.Context, opts CommitObject) (object Objec } } - opts.Version = 1 + opts.Version = DefaultVersion + args[versionArgIndex] = opts.Version args = append(args, opts.EncryptedMetadataNonce, diff --git a/satellite/metabase/commit_test.go b/satellite/metabase/commit_test.go index 2477cae92..5d72e8609 100644 --- a/satellite/metabase/commit_test.go +++ b/satellite/metabase/commit_test.go @@ -365,7 +365,7 @@ func TestBeginObjectNextVersion_PendingObjects(t *testing.T) { UsePendingObjectsTable: true, }, - Version: metabase.DefaultVersion, + Version: metabase.PendingVersion, }.Check(ctx, t, db) metabasetest.BeginObjectNextVersion{ @@ -376,7 +376,7 @@ func TestBeginObjectNextVersion_PendingObjects(t *testing.T) { UsePendingObjectsTable: true, }, - Version: metabase.DefaultVersion, + Version: metabase.PendingVersion, ErrClass: &metabase.ErrObjectAlreadyExists, }.Check(ctx, t, db) @@ -415,7 +415,7 @@ func TestBeginObjectNextVersion_PendingObjects(t *testing.T) { UsePendingObjectsTable: true, }, - Version: metabase.DefaultVersion, + Version: metabase.PendingVersion, }.Check(ctx, t, db) now2 := time.Now() @@ -430,7 +430,7 @@ func TestBeginObjectNextVersion_PendingObjects(t *testing.T) { UsePendingObjectsTable: true, }, - Version: metabase.DefaultVersion, + Version: metabase.PendingVersion, }.Check(ctx, t, db) metabasetest.Verify{ @@ -487,7 +487,7 @@ func TestBeginObjectNextVersion_PendingObjects(t *testing.T) { UsePendingObjectsTable: true, }, - Version: metabase.DefaultVersion, + Version: metabase.PendingVersion, }.Check(ctx, t, db) metabasetest.Verify{ @@ -1214,7 +1214,7 @@ func TestBeginSegment(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) metabasetest.BeginSegment{ @@ -1256,7 +1256,7 @@ func TestBeginSegment(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) for i := 0; i < 5; i++ { @@ -1968,7 +1968,7 @@ func TestCommitSegment(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) rootPieceID := testrand.PieceID() @@ -2065,7 +2065,7 @@ func TestCommitSegment(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) rootPieceID1 := testrand.PieceID() @@ -2171,7 +2171,7 @@ func TestCommitSegment(t *testing.T) { ExpiresAt: &expectedExpiresAt, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) metabasetest.CommitSegment{ @@ -2242,7 +2242,7 @@ func TestCommitSegment(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) metabasetest.CommitSegment{ @@ -2799,7 +2799,7 @@ func TestCommitInlineSegment(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) encryptedKey := testrand.Bytes(32) @@ -2878,7 +2878,7 @@ func TestCommitInlineSegment(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) encryptedKey := testrand.Bytes(32) @@ -2961,7 +2961,7 @@ func TestCommitInlineSegment(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) metabasetest.CommitInlineSegment{ @@ -3023,7 +3023,7 @@ func TestCommitInlineSegment(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) metabasetest.CommitInlineSegment{ @@ -3087,7 +3087,7 @@ func TestCommitInlineSegment(t *testing.T) { ExpiresAt: &expectedExpiresAt, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) metabasetest.CommitInlineSegment{ @@ -3776,7 +3776,7 @@ func TestCommitObject(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) now := time.Now() @@ -3849,7 +3849,7 @@ func TestCommitObject(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) obj.Version++ now := time.Now() @@ -3968,7 +3968,7 @@ func TestCommitObject(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) obj.Version++ now := time.Now() @@ -4082,7 +4082,7 @@ func TestCommitObject(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) obj.Version++ now := time.Now() @@ -4133,7 +4133,7 @@ func TestCommitObject(t *testing.T) { EncryptedMetadataNonce: expectedMetadataNonce, UsePendingObjectsTable: true, }, - Version: metabase.DefaultVersion, + Version: metabase.PendingVersion, }.Check(ctx, t, db) obj.Version++ @@ -4182,7 +4182,7 @@ func TestCommitObject(t *testing.T) { EncryptedMetadataNonce: testrand.Nonce().Bytes(), UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) obj.Version++ @@ -4232,7 +4232,7 @@ func TestCommitObject(t *testing.T) { EncryptedMetadataNonce: testrand.Nonce().Bytes(), UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) obj.Version++ @@ -4281,7 +4281,7 @@ func TestCommitObject(t *testing.T) { EncryptedMetadataNonce: testrand.Nonce().Bytes(), UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) obj.Version++ diff --git a/satellite/metabase/common.go b/satellite/metabase/common.go index babf3d583..0f8d6b1ba 100644 --- a/satellite/metabase/common.go +++ b/satellite/metabase/common.go @@ -342,6 +342,9 @@ const NextVersion = Version(0) // DefaultVersion represents default version 1. const DefaultVersion = Version(1) +// PendingVersion represents version that is used for pending objects (with UsePendingObjects). +const PendingVersion = Version(0) + // MaxVersion represents maximum version. // Version in DB is represented as INT4. const MaxVersion = Version(math.MaxInt32) diff --git a/satellite/metabase/delete_bucket_test.go b/satellite/metabase/delete_bucket_test.go index bbb20afee..191df43ef 100644 --- a/satellite/metabase/delete_bucket_test.go +++ b/satellite/metabase/delete_bucket_test.go @@ -252,7 +252,7 @@ func TestDeleteBucketObjects(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) metabasetest.DeleteBucketObjects{ @@ -272,7 +272,7 @@ func TestDeleteBucketObjects(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) metabasetest.DeleteBucketObjects{ diff --git a/satellite/metabase/delete_objects_test.go b/satellite/metabase/delete_objects_test.go index 0c5980a18..cdd7d57f2 100644 --- a/satellite/metabase/delete_objects_test.go +++ b/satellite/metabase/delete_objects_test.go @@ -526,7 +526,7 @@ func TestDeleteInactivePendingObjects(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) // zombie object with deadline time in the past @@ -537,7 +537,7 @@ func TestDeleteInactivePendingObjects(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) // pending object with expiration time in the future @@ -548,7 +548,7 @@ func TestDeleteInactivePendingObjects(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) metabasetest.DeleteInactivePendingObjects{ @@ -588,7 +588,7 @@ func TestDeleteInactivePendingObjects(t *testing.T) { ZombieDeletionDeadline: &now, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) metabasetest.BeginSegment{ Opts: metabase.BeginSegment{ @@ -680,7 +680,7 @@ func TestDeleteInactivePendingObjects(t *testing.T) { // use default 24h zombie deletion deadline UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) for i := byte(0); i < 3; i++ { diff --git a/satellite/metabase/delete_test.go b/satellite/metabase/delete_test.go index 1903b2528..e2b63e5f1 100644 --- a/satellite/metabase/delete_test.go +++ b/satellite/metabase/delete_test.go @@ -412,7 +412,7 @@ func TestDeletePendingObjectNew(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) object := metabase.RawObject{ @@ -444,7 +444,7 @@ func TestDeletePendingObjectNew(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) metabasetest.CommitSegment{ @@ -498,7 +498,7 @@ func TestDeletePendingObjectNew(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) metabasetest.CommitInlineSegment{ diff --git a/satellite/metabase/metabasetest/create.go b/satellite/metabase/metabasetest/create.go index 9274da1cf..b6142f2d6 100644 --- a/satellite/metabase/metabasetest/create.go +++ b/satellite/metabase/metabasetest/create.go @@ -94,9 +94,8 @@ func CreatePendingObjectNew(ctx *testcontext.Context, t *testing.T, db *metabase Encryption: DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) - obj.Version = 1 for i := byte(0); i < numberOfSegments; i++ { BeginSegment{ diff --git a/satellite/metabase/pending_objects_iterator_test.go b/satellite/metabase/pending_objects_iterator_test.go index 4c740c469..3a64c0339 100644 --- a/satellite/metabase/pending_objects_iterator_test.go +++ b/satellite/metabase/pending_objects_iterator_test.go @@ -94,7 +94,7 @@ func TestIteratePendingObjects(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) encryptedMetadata := testrand.Bytes(1024) @@ -107,7 +107,7 @@ func TestIteratePendingObjects(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) committed.Version++ @@ -644,7 +644,7 @@ func TestIteratePendingObjects(t *testing.T) { EncryptedMetadataNonce: []byte{5}, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) // include custom metadata @@ -700,7 +700,7 @@ func TestIteratePendingObjects(t *testing.T) { EncryptedMetadataNonce: []byte{5}, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) var collector metabasetest.PendingObjectsCollector @@ -816,7 +816,7 @@ func TestIteratePendingObjects(t *testing.T) { UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) } for _, batchSize := range []int{1, 2, 3} { @@ -897,7 +897,7 @@ func TestIteratePendingObjects(t *testing.T) { Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) objs = append(objs, metabase.PendingObjectEntry{ @@ -937,7 +937,7 @@ func createPendingObjects(ctx *testcontext.Context, t *testing.T, db *metabase.D Encryption: metabasetest.DefaultEncryption, UsePendingObjectsTable: true, }, - Version: 1, + Version: metabase.PendingVersion, }.Check(ctx, t, db) obj.Version++ diff --git a/satellite/metainfo/endpoint_object.go b/satellite/metainfo/endpoint_object.go index 78b347212..38971f4de 100644 --- a/satellite/metainfo/endpoint_object.go +++ b/satellite/metainfo/endpoint_object.go @@ -1717,7 +1717,7 @@ func (endpoint *Endpoint) pendingObjectEntryToProtoListItem(ctx context.Context, satStreamID, err := endpoint.packStreamID(ctx, &internalpb.StreamID{ Bucket: bucket, EncryptedObjectKey: append([]byte(prefixToPrependInSatStreamID), []byte(entry.ObjectKey)...), - Version: 1, + Version: int64(metabase.PendingVersion), CreationDate: entry.CreatedAt, ExpirationDate: expiresAt, StreamId: entry.StreamID[:],