satellite/metainfo: fix setting object ZombieDeletionDeadline

So far we were setting ZombieDeletionDeadline alwasy as nil and because of that DB default was never set. This change adds separate query for inserting object if deadline is not set.

Change-Id: I3d6a16570e7c74b5304e13edad8c7adcd021340c
This commit is contained in:
Michał Niewrzał 2021-05-06 13:42:20 +02:00 committed by Michal Niewrzal
parent 7fb86617fc
commit 033006403f
9 changed files with 116 additions and 53 deletions

View File

@ -21,6 +21,8 @@ import (
// we need to disable PlainSize validation for old uplinks.
const validatePlainSize = false
const defaultZombieDeletionPeriod = 24 * time.Hour
var (
// ErrInvalidRequest is used to indicate invalid requests.
ErrInvalidRequest = errs.Class("metabase: invalid request")
@ -50,6 +52,11 @@ func (db *DB) BeginObjectNextVersion(ctx context.Context, opts BeginObjectNextVe
return -1, ErrInvalidRequest.New("Version should be metabase.NextVersion")
}
if opts.ZombieDeletionDeadline == nil {
deadline := time.Now().Add(defaultZombieDeletionPeriod)
opts.ZombieDeletionDeadline = &deadline
}
row := db.db.QueryRow(ctx, `
INSERT INTO objects (
project_id, bucket_name, object_key, version, stream_id,
@ -101,6 +108,11 @@ func (db *DB) BeginObjectExactVersion(ctx context.Context, opts BeginObjectExact
return Object{}, ErrInvalidRequest.New("Version should not be metabase.NextVersion")
}
if opts.ZombieDeletionDeadline == nil {
deadline := time.Now().Add(defaultZombieDeletionPeriod)
opts.ZombieDeletionDeadline = &deadline
}
object := Object{
ObjectStream: ObjectStream{
ProjectID: opts.ProjectID,
@ -119,7 +131,7 @@ func (db *DB) BeginObjectExactVersion(ctx context.Context, opts BeginObjectExact
project_id, bucket_name, object_key, version, stream_id,
expires_at, encryption,
zombie_deletion_deadline
) values (
) VALUES (
$1, $2, $3, $4, $5,
$6, $7,
$8

View File

@ -184,6 +184,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
Version: 1,
}.Check(ctx, t, db)
now := time.Now()
zombieDeadline := now.Add(24 * time.Hour)
pos00 := metabase.SegmentPosition{Part: 0, Index: 0}
metabasetest.CommitObjectWithSegments{
@ -205,6 +206,7 @@ func TestCommitObjectWithSegments(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
},
}.Check(ctx, t, db)

View File

@ -63,6 +63,8 @@ func TestBeginObjectNextVersion(t *testing.T) {
defer metabasetest.DeleteAll{}.Check(ctx, t, db)
now1 := time.Now()
zombieDeadline := now1.Add(24 * time.Hour)
futureTime := now1.Add(10 * 24 * time.Hour)
objectStream.Version = metabase.NextVersion
@ -80,6 +82,7 @@ func TestBeginObjectNextVersion(t *testing.T) {
Opts: metabase.BeginObjectNextVersion{
ObjectStream: objectStream,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &futureTime,
},
Version: 2,
}.Check(ctx, t, db)
@ -98,6 +101,7 @@ func TestBeginObjectNextVersion(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
{
ObjectStream: metabase.ObjectStream{
@ -111,6 +115,7 @@ func TestBeginObjectNextVersion(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &futureTime,
},
},
}.Check(ctx, t, db)
@ -328,6 +333,7 @@ func TestBeginObjectExactVersion(t *testing.T) {
defer metabasetest.DeleteAll{}.Check(ctx, t, db)
now1 := time.Now()
zombieDeadline := now1.Add(24 * time.Hour)
objectStream.Version = 5
@ -353,6 +359,7 @@ func TestBeginObjectExactVersion(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
},
}.Check(ctx, t, db)
@ -362,6 +369,7 @@ func TestBeginObjectExactVersion(t *testing.T) {
defer metabasetest.DeleteAll{}.Check(ctx, t, db)
now1 := time.Now()
zombieDeadline := now1.Add(24 * time.Hour)
objectStream.Version = 5
@ -397,6 +405,7 @@ func TestBeginObjectExactVersion(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
},
}.Check(ctx, t, db)
@ -772,6 +781,7 @@ func TestBeginSegment(t *testing.T) {
t.Run("begin segment successfully", func(t *testing.T) {
defer metabasetest.DeleteAll{}.Check(ctx, t, db)
now := time.Now()
zombieDeadline := now.Add(24 * time.Hour)
metabasetest.BeginObjectExactVersion{
Opts: metabase.BeginObjectExactVersion{
@ -800,6 +810,7 @@ func TestBeginSegment(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
},
}.Check(ctx, t, db)
@ -808,6 +819,7 @@ func TestBeginSegment(t *testing.T) {
t.Run("multiple begin segment successfully", func(t *testing.T) {
defer metabasetest.DeleteAll{}.Check(ctx, t, db)
now := time.Now()
zombieDeadline := now.Add(24 * time.Hour)
metabasetest.BeginObjectExactVersion{
Opts: metabase.BeginObjectExactVersion{
@ -838,6 +850,7 @@ func TestBeginSegment(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
},
}.Check(ctx, t, db)
@ -869,6 +882,7 @@ func TestCommitSegment(t *testing.T) {
defer metabasetest.DeleteAll{}.Check(ctx, t, db)
now := time.Now()
zombieDeadline := now.Add(24 * time.Hour)
metabasetest.BeginObjectExactVersion{
Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj,
@ -1091,6 +1105,7 @@ func TestCommitSegment(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
},
}.Check(ctx, t, db)
@ -1100,6 +1115,7 @@ func TestCommitSegment(t *testing.T) {
defer metabasetest.DeleteAll{}.Check(ctx, t, db)
now1 := time.Now()
zombieDeadline := now1.Add(24 * time.Hour)
metabasetest.BeginObjectExactVersion{
Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj,
@ -1166,6 +1182,7 @@ func TestCommitSegment(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
},
Segments: []metabase.RawSegment{
@ -1283,6 +1300,7 @@ func TestCommitSegment(t *testing.T) {
encryptedETag := testrand.Bytes(32)
now := time.Now()
zombieDeadline := now.Add(24 * time.Hour)
metabasetest.BeginObjectExactVersion{
Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj,
@ -1316,6 +1334,7 @@ func TestCommitSegment(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
},
Segments: []metabase.RawSegment{
@ -1345,7 +1364,7 @@ func TestCommitInlineSegment(t *testing.T) {
metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) {
obj := metabasetest.RandObjectStream()
now := time.Now()
zombieDeadline := now.Add(24 * time.Hour)
for _, test := range metabasetest.InvalidObjectStreams(obj) {
test := test
t.Run(test.Name, func(t *testing.T) {
@ -1439,6 +1458,7 @@ func TestCommitInlineSegment(t *testing.T) {
defer metabasetest.DeleteAll{}.Check(ctx, t, db)
now1 := time.Now()
metabasetest.BeginObjectExactVersion{
Opts: metabase.BeginObjectExactVersion{
ObjectStream: obj,
@ -1488,6 +1508,7 @@ func TestCommitInlineSegment(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
},
Segments: []metabase.RawSegment{
@ -1605,6 +1626,7 @@ func TestCommitInlineSegment(t *testing.T) {
CreatedAt: now,
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
},
Segments: []metabase.RawSegment{
@ -1662,6 +1684,7 @@ func TestCommitInlineSegment(t *testing.T) {
CreatedAt: now,
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
},
Segments: []metabase.RawSegment{

View File

@ -21,6 +21,7 @@ func TestDeleteExpiredObjects(t *testing.T) {
obj3 := metabasetest.RandObjectStream()
now := time.Now()
zombieDeadline := now.Add(24 * time.Hour)
pastTime := now.Add(-1 * time.Hour)
futureTime := now.Add(1 * time.Hour)
@ -81,6 +82,7 @@ func TestDeleteExpiredObjects(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
{
ObjectStream: obj3,
@ -89,6 +91,7 @@ func TestDeleteExpiredObjects(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
},
}.Check(ctx, t, db)
@ -171,6 +174,7 @@ func TestDeleteZombieObjects(t *testing.T) {
obj3 := metabasetest.RandObjectStream()
now := time.Now()
zombieDeadline := now.Add(24 * time.Hour)
pastTime := now.Add(-1 * time.Hour)
futureTime := now.Add(1 * time.Hour)
@ -231,14 +235,15 @@ func TestDeleteZombieObjects(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
{
ObjectStream: obj3,
CreatedAt: now,
ZombieDeletionDeadline: &futureTime,
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &futureTime,
},
},
}.Check(ctx, t, db)

View File

@ -19,6 +19,7 @@ func TestDeletePendingObject(t *testing.T) {
metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) {
obj := metabasetest.RandObjectStream()
now := time.Now()
zombieDeadline := now.Add(24 * time.Hour)
for _, test := range metabasetest.InvalidObjectStreams(obj) {
test := test
@ -80,6 +81,7 @@ func TestDeletePendingObject(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
},
}.Check(ctx, t, db)
@ -145,6 +147,7 @@ func TestDeletePendingObject(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
},
}.Check(ctx, t, db)
@ -263,6 +266,7 @@ func TestDeleteObjectExactVersion(t *testing.T) {
location := obj.Location()
now := time.Now()
zombieDeadline := now.Add(24 * time.Hour)
for _, test := range metabasetest.InvalidObjectLocations(location) {
test := test
@ -349,6 +353,7 @@ func TestDeleteObjectExactVersion(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
},
}.Check(ctx, t, db)
@ -461,6 +466,7 @@ func TestDeleteObjectLatestVersion(t *testing.T) {
location := obj.Location()
now := time.Now()
zombieDeadline := now.Add(24 * time.Hour)
for _, test := range metabasetest.InvalidObjectLocations(location) {
test := test
@ -524,6 +530,7 @@ func TestDeleteObjectLatestVersion(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
},
}.Check(ctx, t, db)
@ -892,6 +899,7 @@ func TestDeleteObjectsAllVersions(t *testing.T) {
location := obj.Location()
now := time.Now()
zombieDeadline := now.Add(24 * time.Hour)
for _, test := range metabasetest.InvalidObjectLocations(location) {
test := test
@ -984,6 +992,7 @@ func TestDeleteObjectsAllVersions(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
},
}.Check(ctx, t, db)

View File

@ -21,6 +21,7 @@ func TestGetObjectExactVersion(t *testing.T) {
location := obj.Location()
now := time.Now()
zombieDeadline := now.Add(24 * time.Hour)
for _, test := range metabasetest.InvalidObjectLocations(location) {
test := test
@ -124,6 +125,7 @@ func TestGetObjectExactVersion(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
},
}.Check(ctx, t, db)
@ -168,6 +170,7 @@ func TestGetObjectLatestVersion(t *testing.T) {
location := obj.Location()
now := time.Now()
zombieDeadline := now.Add(24 * time.Hour)
for _, test := range metabasetest.InvalidObjectLocations(location) {
test := test
@ -227,6 +230,7 @@ func TestGetObjectLatestVersion(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
},
}.Check(ctx, t, db)
@ -762,6 +766,7 @@ func TestBucketEmpty(t *testing.T) {
metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) {
obj := metabasetest.RandObjectStream()
now := time.Now()
zombieDeadline := now.Add(24 * time.Hour)
t.Run("ProjectID missing", func(t *testing.T) {
defer metabasetest.DeleteAll{}.Check(ctx, t, db)
@ -830,6 +835,7 @@ func TestBucketEmpty(t *testing.T) {
CreatedAt: now,
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
},
},
}.Check(ctx, t, db)

View File

@ -1059,6 +1059,7 @@ func TestIteratePendingObjectsWithObjectKey(t *testing.T) {
location := obj.Location()
now := time.Now()
zombieDeadline := now.Add(24 * time.Hour)
for _, test := range metabasetest.InvalidObjectLocations(location) {
test := test
@ -1100,6 +1101,7 @@ func TestIteratePendingObjectsWithObjectKey(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
}
metabasetest.IteratePendingObjectsByKey{
@ -1140,6 +1142,7 @@ func TestIteratePendingObjectsWithObjectKey(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
}
expected[i] = objectEntryFromRaw(objects[i])
}
@ -1180,6 +1183,7 @@ func TestIteratePendingObjectsWithObjectKey(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
}
metabasetest.IteratePendingObjectsByKey{
@ -1214,6 +1218,7 @@ func TestIteratePendingObjectsWithObjectKey(t *testing.T) {
Status: metabase.Pending,
Encryption: metabasetest.DefaultEncryption,
ZombieDeletionDeadline: &zombieDeadline,
}
expected[i] = objectEntryFromRaw(objects[i])
}
@ -1318,6 +1323,5 @@ func objectEntryFromRaw(m metabase.RawObject) metabase.ObjectEntry {
TotalEncryptedSize: m.TotalEncryptedSize,
FixedSegmentSize: m.FixedSegmentSize,
Encryption: m.Encryption,
ZombieDeletionDeadline: m.ZombieDeletionDeadline,
}
}

View File

@ -34,11 +34,6 @@ type ObjectEntry struct {
FixedSegmentSize int32
Encryption storj.EncryptionParameters
// ZombieDeletionDeadline defines when the pending raw object should be deleted from the database.
// This is as a safeguard against objects that failed to upload and the client has not indicated
// whether they want to continue uploading or delete the already uploaded data.
ZombieDeletionDeadline *time.Time
}
// ObjectsIterator iterates over a sequence of ObjectEntry items.

View File

@ -52,7 +52,14 @@ func (step BeginObjectExactVersion) Check(ctx *testcontext.Context, t testing.TB
require.WithinDuration(t, time.Now(), got.CreatedAt, 5*time.Second)
require.Equal(t, step.Opts.ObjectStream, got.ObjectStream)
require.Equal(t, step.Opts.ExpiresAt, got.ExpiresAt)
require.Equal(t, step.Opts.ZombieDeletionDeadline, got.ZombieDeletionDeadline)
gotDeadline := got.ZombieDeletionDeadline
optsDeadline := step.Opts.ZombieDeletionDeadline
if optsDeadline == nil {
require.WithinDuration(t, time.Now().Add(24*time.Hour), *gotDeadline, 5*time.Second)
} else {
require.WithinDuration(t, *optsDeadline, *gotDeadline, 5*time.Second)
}
require.Equal(t, step.Opts.Encryption, got.Encryption)
}
}