2020-10-28 15:28:06 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package metabase
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
pgxerrcode "github.com/jackc/pgerrcode"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/common/storj"
|
2021-04-23 10:52:40 +01:00
|
|
|
"storj.io/private/dbutil/pgutil/pgerrcode"
|
|
|
|
"storj.io/private/dbutil/txutil"
|
|
|
|
"storj.io/private/tagsql"
|
2020-10-28 15:28:06 +00:00
|
|
|
)
|
|
|
|
|
2021-01-04 13:43:24 +00:00
|
|
|
// we need to disable PlainSize validation for old uplinks.
|
|
|
|
const validatePlainSize = false
|
|
|
|
|
2021-05-06 12:42:20 +01:00
|
|
|
const defaultZombieDeletionPeriod = 24 * time.Hour
|
|
|
|
|
2020-10-28 15:28:06 +00:00
|
|
|
var (
|
|
|
|
// ErrInvalidRequest is used to indicate invalid requests.
|
|
|
|
ErrInvalidRequest = errs.Class("metabase: invalid request")
|
|
|
|
// ErrConflict is used to indicate conflict with the request.
|
|
|
|
ErrConflict = errs.Class("metabase: conflict")
|
|
|
|
)
|
|
|
|
|
|
|
|
// BeginObjectNextVersion contains arguments necessary for starting an object upload.
|
|
|
|
type BeginObjectNextVersion struct {
|
|
|
|
ObjectStream
|
|
|
|
|
|
|
|
ExpiresAt *time.Time
|
|
|
|
ZombieDeletionDeadline *time.Time
|
|
|
|
|
2020-10-29 13:49:53 +00:00
|
|
|
Encryption storj.EncryptionParameters
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BeginObjectNextVersion adds a pending object to the database, with automatically assigned version.
|
|
|
|
func (db *DB) BeginObjectNextVersion(ctx context.Context, opts BeginObjectNextVersion) (committed Version, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err := opts.ObjectStream.Verify(); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2021-01-14 10:24:29 +00:00
|
|
|
if opts.Version != NextVersion {
|
2020-10-28 15:28:06 +00:00
|
|
|
return -1, ErrInvalidRequest.New("Version should be metabase.NextVersion")
|
|
|
|
}
|
|
|
|
|
2021-05-06 12:42:20 +01:00
|
|
|
if opts.ZombieDeletionDeadline == nil {
|
|
|
|
deadline := time.Now().Add(defaultZombieDeletionPeriod)
|
|
|
|
opts.ZombieDeletionDeadline = &deadline
|
|
|
|
}
|
|
|
|
|
2020-10-28 15:28:06 +00:00
|
|
|
row := db.db.QueryRow(ctx, `
|
|
|
|
INSERT INTO objects (
|
|
|
|
project_id, bucket_name, object_key, version, stream_id,
|
2020-10-29 13:49:53 +00:00
|
|
|
expires_at, encryption,
|
|
|
|
zombie_deletion_deadline
|
2020-10-28 15:28:06 +00:00
|
|
|
) VALUES (
|
|
|
|
$1, $2, $3,
|
|
|
|
coalesce((
|
|
|
|
SELECT version + 1
|
|
|
|
FROM objects
|
|
|
|
WHERE project_id = $1 AND bucket_name = $2 AND object_key = $3
|
|
|
|
ORDER BY version DESC
|
|
|
|
LIMIT 1
|
2020-11-12 11:50:21 +00:00
|
|
|
), 1),
|
2020-10-29 13:49:53 +00:00
|
|
|
$4, $5, $6,
|
|
|
|
$7)
|
2020-10-28 15:28:06 +00:00
|
|
|
RETURNING version
|
2021-03-02 11:22:49 +00:00
|
|
|
`, opts.ProjectID, []byte(opts.BucketName), []byte(opts.ObjectKey), opts.StreamID,
|
2020-10-29 13:49:53 +00:00
|
|
|
opts.ExpiresAt, encryptionParameters{&opts.Encryption},
|
|
|
|
opts.ZombieDeletionDeadline)
|
2020-10-28 15:28:06 +00:00
|
|
|
|
|
|
|
var v int64
|
|
|
|
if err := row.Scan(&v); err != nil {
|
|
|
|
return -1, Error.New("unable to insert object: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Version(v), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BeginObjectExactVersion contains arguments necessary for starting an object upload.
|
|
|
|
type BeginObjectExactVersion struct {
|
|
|
|
ObjectStream
|
|
|
|
|
|
|
|
ExpiresAt *time.Time
|
|
|
|
ZombieDeletionDeadline *time.Time
|
|
|
|
|
2020-10-29 13:49:53 +00:00
|
|
|
Encryption storj.EncryptionParameters
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BeginObjectExactVersion adds a pending object to the database, with specific version.
|
2021-01-12 11:29:13 +00:00
|
|
|
func (db *DB) BeginObjectExactVersion(ctx context.Context, opts BeginObjectExactVersion) (committed Object, err error) {
|
2020-10-28 15:28:06 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err := opts.ObjectStream.Verify(); err != nil {
|
2021-01-12 11:29:13 +00:00
|
|
|
return Object{}, err
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 10:24:29 +00:00
|
|
|
if opts.Version == NextVersion {
|
2021-01-12 11:29:13 +00:00
|
|
|
return Object{}, ErrInvalidRequest.New("Version should not be metabase.NextVersion")
|
|
|
|
}
|
|
|
|
|
2021-05-06 12:42:20 +01:00
|
|
|
if opts.ZombieDeletionDeadline == nil {
|
|
|
|
deadline := time.Now().Add(defaultZombieDeletionPeriod)
|
|
|
|
opts.ZombieDeletionDeadline = &deadline
|
|
|
|
}
|
|
|
|
|
2021-01-12 11:29:13 +00:00
|
|
|
object := Object{
|
|
|
|
ObjectStream: ObjectStream{
|
|
|
|
ProjectID: opts.ProjectID,
|
|
|
|
BucketName: opts.BucketName,
|
|
|
|
ObjectKey: opts.ObjectKey,
|
|
|
|
Version: opts.Version,
|
|
|
|
StreamID: opts.StreamID,
|
|
|
|
},
|
|
|
|
ExpiresAt: opts.ExpiresAt,
|
|
|
|
Encryption: opts.Encryption,
|
|
|
|
ZombieDeletionDeadline: opts.ZombieDeletionDeadline,
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 11:29:13 +00:00
|
|
|
err = db.db.QueryRow(ctx, `
|
2020-10-28 15:28:06 +00:00
|
|
|
INSERT INTO objects (
|
|
|
|
project_id, bucket_name, object_key, version, stream_id,
|
2020-10-29 13:49:53 +00:00
|
|
|
expires_at, encryption,
|
|
|
|
zombie_deletion_deadline
|
2021-05-06 12:42:20 +01:00
|
|
|
) VALUES (
|
2020-10-28 15:28:06 +00:00
|
|
|
$1, $2, $3, $4, $5,
|
2020-10-29 13:49:53 +00:00
|
|
|
$6, $7,
|
|
|
|
$8
|
2020-10-28 15:28:06 +00:00
|
|
|
)
|
2021-01-12 11:29:13 +00:00
|
|
|
RETURNING status, created_at
|
2021-03-02 11:22:49 +00:00
|
|
|
`, opts.ProjectID, []byte(opts.BucketName), []byte(opts.ObjectKey), opts.Version, opts.StreamID,
|
2020-10-29 13:49:53 +00:00
|
|
|
opts.ExpiresAt, encryptionParameters{&opts.Encryption},
|
2021-01-12 11:29:13 +00:00
|
|
|
opts.ZombieDeletionDeadline).
|
|
|
|
Scan(
|
|
|
|
&object.Status, &object.CreatedAt,
|
|
|
|
)
|
2020-10-28 15:28:06 +00:00
|
|
|
if err != nil {
|
|
|
|
if code := pgerrcode.FromError(err); code == pgxerrcode.UniqueViolation {
|
2021-01-12 11:29:13 +00:00
|
|
|
return Object{}, ErrConflict.New("object already exists")
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
2021-01-12 11:29:13 +00:00
|
|
|
return Object{}, Error.New("unable to insert object: %w", err)
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 11:29:13 +00:00
|
|
|
return object, nil
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BeginSegment contains options to verify, whether a new segment upload can be started.
|
|
|
|
type BeginSegment struct {
|
|
|
|
ObjectStream
|
|
|
|
|
|
|
|
Position SegmentPosition
|
|
|
|
RootPieceID storj.PieceID
|
|
|
|
Pieces Pieces
|
|
|
|
}
|
|
|
|
|
|
|
|
// BeginSegment verifies, whether a new segment upload can be started.
|
|
|
|
func (db *DB) BeginSegment(ctx context.Context, opts BeginSegment) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err := opts.ObjectStream.Verify(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-21 10:48:48 +00:00
|
|
|
if err := opts.Pieces.Verify(); err != nil {
|
|
|
|
return err
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 10:48:48 +00:00
|
|
|
if opts.RootPieceID.IsZero() {
|
|
|
|
return ErrInvalidRequest.New("RootPieceID missing")
|
|
|
|
}
|
2020-10-28 15:28:06 +00:00
|
|
|
|
|
|
|
// NOTE: this isn't strictly necessary, since we can also fail this in CommitSegment.
|
|
|
|
// however, we should prevent creating segements for non-partial objects.
|
|
|
|
|
|
|
|
// NOTE: these queries could be combined into one.
|
|
|
|
|
2020-11-12 11:50:21 +00:00
|
|
|
return txutil.WithTx(ctx, db.db, nil, func(ctx context.Context, tx tagsql.Tx) (err error) {
|
|
|
|
// Verify that object exists and is partial.
|
|
|
|
var value int
|
|
|
|
err = tx.QueryRow(ctx, `
|
|
|
|
SELECT 1
|
|
|
|
FROM objects WHERE
|
|
|
|
project_id = $1 AND
|
|
|
|
bucket_name = $2 AND
|
|
|
|
object_key = $3 AND
|
|
|
|
version = $4 AND
|
|
|
|
stream_id = $5 AND
|
2020-11-16 13:58:22 +00:00
|
|
|
status = `+pendingStatus,
|
2021-03-02 11:22:49 +00:00
|
|
|
opts.ProjectID, []byte(opts.BucketName), []byte(opts.ObjectKey), opts.Version, opts.StreamID).Scan(&value)
|
2020-11-12 11:50:21 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return Error.New("pending object missing")
|
|
|
|
}
|
|
|
|
return Error.New("unable to query object status: %w", err)
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
|
|
|
|
2020-11-12 11:50:21 +00:00
|
|
|
// Verify that the segment does not exist.
|
|
|
|
err = tx.QueryRow(ctx, `
|
|
|
|
SELECT 1
|
|
|
|
FROM segments WHERE
|
|
|
|
stream_id = $1 AND
|
|
|
|
position = $2
|
|
|
|
`, opts.StreamID, opts.Position).Scan(&value)
|
|
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return Error.New("unable to query segments: %w", err)
|
|
|
|
}
|
2021-05-14 16:05:42 +01:00
|
|
|
err = nil //nolint: wastedassign, ineffassign // ignore any other err result (explicitly)
|
2020-10-28 15:28:06 +00:00
|
|
|
|
2020-11-12 11:50:21 +00:00
|
|
|
return nil
|
|
|
|
})
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CommitSegment contains all necessary information about the segment.
|
|
|
|
type CommitSegment struct {
|
|
|
|
ObjectStream
|
|
|
|
|
|
|
|
Position SegmentPosition
|
|
|
|
RootPieceID storj.PieceID
|
|
|
|
|
|
|
|
EncryptedKeyNonce []byte
|
|
|
|
EncryptedKey []byte
|
|
|
|
|
|
|
|
PlainOffset int64 // offset in the original data stream
|
|
|
|
PlainSize int32 // size before encryption
|
|
|
|
EncryptedSize int32 // segment size after encryption
|
|
|
|
|
2021-03-25 07:53:10 +00:00
|
|
|
EncryptedETag []byte
|
|
|
|
|
2020-10-28 15:28:06 +00:00
|
|
|
Redundancy storj.RedundancyScheme
|
|
|
|
|
|
|
|
Pieces Pieces
|
|
|
|
}
|
|
|
|
|
|
|
|
// CommitSegment commits segment to the database.
|
|
|
|
func (db *DB) CommitSegment(ctx context.Context, opts CommitSegment) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err := opts.ObjectStream.Verify(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-21 10:48:48 +00:00
|
|
|
if err := opts.Pieces.Verify(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-28 15:28:06 +00:00
|
|
|
switch {
|
|
|
|
case opts.RootPieceID.IsZero():
|
|
|
|
return ErrInvalidRequest.New("RootPieceID missing")
|
|
|
|
case len(opts.EncryptedKey) == 0:
|
|
|
|
return ErrInvalidRequest.New("EncryptedKey missing")
|
|
|
|
case len(opts.EncryptedKeyNonce) == 0:
|
|
|
|
return ErrInvalidRequest.New("EncryptedKeyNonce missing")
|
|
|
|
case opts.EncryptedSize <= 0:
|
|
|
|
return ErrInvalidRequest.New("EncryptedSize negative or zero")
|
2021-01-04 13:43:24 +00:00
|
|
|
case opts.PlainSize <= 0 && validatePlainSize:
|
2020-10-28 15:28:06 +00:00
|
|
|
return ErrInvalidRequest.New("PlainSize negative or zero")
|
|
|
|
case opts.PlainOffset < 0:
|
|
|
|
return ErrInvalidRequest.New("PlainOffset negative")
|
|
|
|
case opts.Redundancy.IsZero():
|
|
|
|
return ErrInvalidRequest.New("Redundancy zero")
|
|
|
|
}
|
|
|
|
|
2021-03-11 14:52:53 +00:00
|
|
|
if len(opts.Pieces) < int(opts.Redundancy.OptimalShares) {
|
|
|
|
return ErrInvalidRequest.New("number of pieces is less than redundancy optimal shares value")
|
|
|
|
}
|
2020-10-28 15:28:06 +00:00
|
|
|
|
2021-02-08 09:33:45 +00:00
|
|
|
aliasPieces, err := db.aliasCache.ConvertPiecesToAliases(ctx, opts.Pieces)
|
|
|
|
if err != nil {
|
|
|
|
return Error.New("unable to convert pieces to aliases: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-01-21 12:17:51 +00:00
|
|
|
// Verify that object exists and is partial.
|
|
|
|
_, err = db.db.ExecContext(ctx, `
|
|
|
|
INSERT INTO segments (
|
|
|
|
stream_id, position,
|
|
|
|
root_piece_id, encrypted_key_nonce, encrypted_key,
|
2021-03-25 07:53:10 +00:00
|
|
|
encrypted_size, plain_offset, plain_size, encrypted_etag,
|
2021-01-21 12:17:51 +00:00
|
|
|
redundancy,
|
2021-02-08 09:33:45 +00:00
|
|
|
remote_alias_pieces
|
2021-01-21 12:17:51 +00:00
|
|
|
) VALUES (
|
|
|
|
(SELECT stream_id
|
|
|
|
FROM objects WHERE
|
2021-03-25 07:53:10 +00:00
|
|
|
project_id = $11 AND
|
|
|
|
bucket_name = $12 AND
|
|
|
|
object_key = $13 AND
|
|
|
|
version = $14 AND
|
|
|
|
stream_id = $15 AND
|
2021-01-21 12:17:51 +00:00
|
|
|
status = `+pendingStatus+
|
|
|
|
` ), $1,
|
|
|
|
$2, $3, $4,
|
2021-03-25 07:53:10 +00:00
|
|
|
$5, $6, $7, $8,
|
|
|
|
$9,
|
|
|
|
$10
|
2021-01-21 12:17:51 +00:00
|
|
|
)`, opts.Position,
|
|
|
|
opts.RootPieceID, opts.EncryptedKeyNonce, opts.EncryptedKey,
|
2021-03-25 07:53:10 +00:00
|
|
|
opts.EncryptedSize, opts.PlainOffset, opts.PlainSize, opts.EncryptedETag,
|
2021-01-21 12:17:51 +00:00
|
|
|
redundancyScheme{&opts.Redundancy},
|
2021-02-08 09:33:45 +00:00
|
|
|
aliasPieces,
|
2021-03-02 11:22:49 +00:00
|
|
|
opts.ProjectID, []byte(opts.BucketName), []byte(opts.ObjectKey), opts.Version, opts.StreamID,
|
2021-01-21 12:17:51 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if code := pgerrcode.FromError(err); code == pgxerrcode.NotNullViolation {
|
|
|
|
return Error.New("pending object missing")
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
2021-01-21 12:17:51 +00:00
|
|
|
if code := pgerrcode.FromError(err); code == pgxerrcode.UniqueViolation {
|
|
|
|
return ErrConflict.New("segment already exists")
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
2021-01-21 12:17:51 +00:00
|
|
|
return Error.New("unable to insert segment: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CommitInlineSegment contains all necessary information about the segment.
|
|
|
|
type CommitInlineSegment struct {
|
|
|
|
ObjectStream
|
|
|
|
|
2020-11-11 10:54:10 +00:00
|
|
|
Position SegmentPosition
|
2020-10-28 15:28:06 +00:00
|
|
|
|
|
|
|
EncryptedKeyNonce []byte
|
|
|
|
EncryptedKey []byte
|
|
|
|
|
2021-03-25 07:53:10 +00:00
|
|
|
PlainOffset int64 // offset in the original data stream
|
|
|
|
PlainSize int32 // size before encryption
|
|
|
|
EncryptedETag []byte
|
2020-10-28 15:28:06 +00:00
|
|
|
|
|
|
|
InlineData []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// CommitInlineSegment commits inline segment to the database.
|
|
|
|
func (db *DB) CommitInlineSegment(ctx context.Context, opts CommitInlineSegment) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err := opts.ObjectStream.Verify(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: do we have a lower limit for inline data?
|
2021-02-19 09:28:41 +00:00
|
|
|
// TODO should we move check for max inline segment from metainfo here
|
2020-10-28 15:28:06 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case len(opts.EncryptedKey) == 0:
|
|
|
|
return ErrInvalidRequest.New("EncryptedKey missing")
|
|
|
|
case len(opts.EncryptedKeyNonce) == 0:
|
|
|
|
return ErrInvalidRequest.New("EncryptedKeyNonce missing")
|
2021-01-04 13:43:24 +00:00
|
|
|
case opts.PlainSize <= 0 && validatePlainSize:
|
2020-10-28 15:28:06 +00:00
|
|
|
return ErrInvalidRequest.New("PlainSize negative or zero")
|
|
|
|
case opts.PlainOffset < 0:
|
|
|
|
return ErrInvalidRequest.New("PlainOffset negative")
|
|
|
|
}
|
|
|
|
|
2021-01-21 12:17:51 +00:00
|
|
|
// Verify that object exists and is partial.
|
|
|
|
_, err = db.db.ExecContext(ctx, `
|
|
|
|
INSERT INTO segments (
|
|
|
|
stream_id, position,
|
|
|
|
root_piece_id, encrypted_key_nonce, encrypted_key,
|
2021-03-25 07:53:10 +00:00
|
|
|
encrypted_size, plain_offset, plain_size, encrypted_etag,
|
2021-01-21 12:17:51 +00:00
|
|
|
inline_data
|
|
|
|
) VALUES (
|
|
|
|
(SELECT stream_id
|
|
|
|
FROM objects WHERE
|
2021-03-25 07:53:10 +00:00
|
|
|
project_id = $10 AND
|
|
|
|
bucket_name = $11 AND
|
|
|
|
object_key = $12 AND
|
|
|
|
version = $13 AND
|
|
|
|
stream_id = $14 AND
|
2021-01-21 12:17:51 +00:00
|
|
|
status = `+pendingStatus+
|
|
|
|
` ), $1,
|
|
|
|
$2, $3, $4,
|
2021-03-25 07:53:10 +00:00
|
|
|
$5, $6, $7, $8,
|
|
|
|
$9
|
2021-01-21 12:17:51 +00:00
|
|
|
)`, opts.Position,
|
|
|
|
storj.PieceID{}, opts.EncryptedKeyNonce, opts.EncryptedKey,
|
2021-03-25 07:53:10 +00:00
|
|
|
len(opts.InlineData), opts.PlainOffset, opts.PlainSize, opts.EncryptedETag,
|
2021-01-21 12:17:51 +00:00
|
|
|
opts.InlineData,
|
2021-03-02 11:22:49 +00:00
|
|
|
opts.ProjectID, []byte(opts.BucketName), []byte(opts.ObjectKey), opts.Version, opts.StreamID,
|
2021-01-21 12:17:51 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if code := pgerrcode.FromError(err); code == pgxerrcode.NotNullViolation {
|
|
|
|
return Error.New("pending object missing")
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
2021-01-21 12:17:51 +00:00
|
|
|
if code := pgerrcode.FromError(err); code == pgxerrcode.UniqueViolation {
|
|
|
|
return ErrConflict.New("segment already exists")
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
2021-01-21 12:17:51 +00:00
|
|
|
return Error.New("unable to insert segment: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CommitObject contains arguments necessary for committing an object.
|
|
|
|
type CommitObject struct {
|
|
|
|
ObjectStream
|
|
|
|
|
2021-01-14 10:24:29 +00:00
|
|
|
Encryption storj.EncryptionParameters
|
|
|
|
|
2020-11-16 16:46:47 +00:00
|
|
|
EncryptedMetadata []byte
|
|
|
|
EncryptedMetadataNonce []byte
|
|
|
|
EncryptedMetadataEncryptedKey []byte
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CommitObject adds a pending object to the database.
|
2020-11-02 17:25:58 +00:00
|
|
|
func (db *DB) CommitObject(ctx context.Context, opts CommitObject) (object Object, err error) {
|
2020-10-28 15:28:06 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err := opts.ObjectStream.Verify(); err != nil {
|
2020-11-02 17:25:58 +00:00
|
|
|
return Object{}, err
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 10:24:29 +00:00
|
|
|
if opts.Encryption.CipherSuite != storj.EncUnspecified && opts.Encryption.BlockSize <= 0 {
|
|
|
|
return Object{}, ErrInvalidRequest.New("Encryption.BlockSize is negative or zero")
|
|
|
|
}
|
|
|
|
|
2020-11-12 11:50:21 +00:00
|
|
|
err = txutil.WithTx(ctx, db.db, nil, func(ctx context.Context, tx tagsql.Tx) error {
|
2021-03-30 10:44:43 +01:00
|
|
|
segments, err := fetchSegmentsForCommit(ctx, tx, opts.StreamID)
|
2020-11-12 11:50:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return Error.New("failed to fetch segments: %w", err)
|
2020-11-03 14:45:39 +00:00
|
|
|
}
|
|
|
|
|
2021-03-30 10:44:43 +01:00
|
|
|
finalSegments := convertToFinalSegments(segments)
|
|
|
|
err = updateSegmentOffsets(ctx, tx, opts.StreamID, finalSegments)
|
|
|
|
if err != nil {
|
|
|
|
return Error.New("failed to update segments: %w", err)
|
|
|
|
}
|
2020-11-12 11:50:21 +00:00
|
|
|
|
|
|
|
// TODO: would we even need this when we make main index plain_offset?
|
|
|
|
fixedSegmentSize := int32(0)
|
2021-03-30 10:44:43 +01:00
|
|
|
if len(finalSegments) > 0 {
|
|
|
|
fixedSegmentSize = finalSegments[0].PlainSize
|
|
|
|
for i, seg := range finalSegments {
|
2021-01-25 11:30:56 +00:00
|
|
|
if seg.Position.Part != 0 || seg.Position.Index != uint32(i) {
|
2021-01-07 08:46:49 +00:00
|
|
|
fixedSegmentSize = -1
|
|
|
|
break
|
|
|
|
}
|
2021-03-30 10:44:43 +01:00
|
|
|
if i < len(finalSegments)-1 && seg.PlainSize != fixedSegmentSize {
|
2020-11-12 11:50:21 +00:00
|
|
|
fixedSegmentSize = -1
|
|
|
|
break
|
|
|
|
}
|
2020-11-03 14:45:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 12:29:16 +00:00
|
|
|
var totalPlainSize, totalEncryptedSize int64
|
2021-03-30 10:44:43 +01:00
|
|
|
for _, seg := range finalSegments {
|
2020-11-24 12:29:16 +00:00
|
|
|
totalPlainSize += int64(seg.PlainSize)
|
2020-11-12 11:50:21 +00:00
|
|
|
totalEncryptedSize += int64(seg.EncryptedSize)
|
|
|
|
}
|
2020-10-28 15:28:06 +00:00
|
|
|
|
2020-11-12 11:50:21 +00:00
|
|
|
err = tx.QueryRow(ctx, `
|
|
|
|
UPDATE objects SET
|
2020-11-16 13:58:22 +00:00
|
|
|
status =`+committedStatus+`,
|
2020-11-12 11:50:21 +00:00
|
|
|
segment_count = $6,
|
|
|
|
|
2020-11-16 16:46:47 +00:00
|
|
|
encrypted_metadata_nonce = $7,
|
|
|
|
encrypted_metadata = $8,
|
|
|
|
encrypted_metadata_encrypted_key = $9,
|
2020-11-12 11:50:21 +00:00
|
|
|
|
2020-11-24 12:29:16 +00:00
|
|
|
total_plain_size = $10,
|
|
|
|
total_encrypted_size = $11,
|
|
|
|
fixed_segment_size = $12,
|
2021-01-14 10:24:29 +00:00
|
|
|
zombie_deletion_deadline = NULL,
|
|
|
|
|
|
|
|
-- TODO should we allow to override existing encryption parameters or return error if don't match with opts?
|
|
|
|
encryption = CASE
|
|
|
|
WHEN objects.encryption = 0 AND $13 <> 0 THEN $13
|
|
|
|
WHEN objects.encryption = 0 AND $13 = 0 THEN NULL
|
|
|
|
ELSE objects.encryption
|
|
|
|
END
|
2020-11-12 11:50:21 +00:00
|
|
|
WHERE
|
|
|
|
project_id = $1 AND
|
|
|
|
bucket_name = $2 AND
|
|
|
|
object_key = $3 AND
|
|
|
|
version = $4 AND
|
|
|
|
stream_id = $5 AND
|
2020-11-16 13:58:22 +00:00
|
|
|
status = `+pendingStatus+`
|
2020-11-12 11:50:21 +00:00
|
|
|
RETURNING
|
|
|
|
created_at, expires_at,
|
|
|
|
encryption;
|
2021-03-02 11:22:49 +00:00
|
|
|
`, opts.ProjectID, []byte(opts.BucketName), []byte(opts.ObjectKey), opts.Version, opts.StreamID,
|
2020-11-12 11:50:21 +00:00
|
|
|
len(segments),
|
2020-11-16 16:46:47 +00:00
|
|
|
opts.EncryptedMetadataNonce, opts.EncryptedMetadata, opts.EncryptedMetadataEncryptedKey,
|
2020-11-24 12:29:16 +00:00
|
|
|
totalPlainSize,
|
2020-11-12 11:50:21 +00:00
|
|
|
totalEncryptedSize,
|
|
|
|
fixedSegmentSize,
|
2021-01-14 10:24:29 +00:00
|
|
|
encryptionParameters{&opts.Encryption},
|
2020-11-12 11:50:21 +00:00
|
|
|
).
|
|
|
|
Scan(
|
|
|
|
&object.CreatedAt, &object.ExpiresAt,
|
|
|
|
encryptionParameters{&object.Encryption},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return storj.ErrObjectNotFound.Wrap(Error.New("object with specified version and pending status is missing"))
|
2021-01-14 10:24:29 +00:00
|
|
|
} else if code := pgerrcode.FromError(err); code == pgxerrcode.NotNullViolation {
|
|
|
|
// TODO maybe we should check message if 'encryption' label is there
|
|
|
|
return ErrInvalidRequest.New("Encryption is missing")
|
2020-11-12 11:50:21 +00:00
|
|
|
}
|
|
|
|
return Error.New("failed to update object: %w", err)
|
2020-11-02 17:25:58 +00:00
|
|
|
}
|
2020-10-28 15:28:06 +00:00
|
|
|
|
2020-11-12 11:50:21 +00:00
|
|
|
object.StreamID = opts.StreamID
|
|
|
|
object.ProjectID = opts.ProjectID
|
|
|
|
object.BucketName = opts.BucketName
|
|
|
|
object.ObjectKey = opts.ObjectKey
|
|
|
|
object.Version = opts.Version
|
|
|
|
object.Status = Committed
|
|
|
|
object.SegmentCount = int32(len(segments))
|
|
|
|
object.EncryptedMetadataNonce = opts.EncryptedMetadataNonce
|
|
|
|
object.EncryptedMetadata = opts.EncryptedMetadata
|
2020-11-16 16:46:47 +00:00
|
|
|
object.EncryptedMetadataEncryptedKey = opts.EncryptedMetadataEncryptedKey
|
2020-11-24 12:29:16 +00:00
|
|
|
object.TotalPlainSize = totalPlainSize
|
2020-11-12 11:50:21 +00:00
|
|
|
object.TotalEncryptedSize = totalEncryptedSize
|
|
|
|
object.FixedSegmentSize = fixedSegmentSize
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return Object{}, err
|
|
|
|
}
|
|
|
|
return object, nil
|
2020-10-28 15:28:06 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 16:32:24 +00:00
|
|
|
// UpdateObjectMetadata contains arguments necessary for updating an object metadata.
|
|
|
|
type UpdateObjectMetadata struct {
|
|
|
|
ObjectStream
|
|
|
|
|
2020-11-16 16:46:47 +00:00
|
|
|
EncryptedMetadata []byte
|
|
|
|
EncryptedMetadataNonce []byte
|
|
|
|
EncryptedMetadataEncryptedKey []byte
|
2020-11-02 16:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateObjectMetadata updates an object metadata.
|
|
|
|
func (db *DB) UpdateObjectMetadata(ctx context.Context, opts UpdateObjectMetadata) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err := opts.ObjectStream.Verify(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.ObjectStream.Version <= 0 {
|
|
|
|
return ErrInvalidRequest.New("Version invalid: %v", opts.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO So the issue is that during a multipart upload of an object,
|
|
|
|
// uplink can update object metadata. If we add the arguments EncryptedMetadata
|
|
|
|
// to CommitObject, they will need to account for them being optional.
|
|
|
|
// Leading to scenarios where uplink calls update metadata, but wants to clear them
|
|
|
|
// during commit object.
|
|
|
|
result, err := db.db.ExecContext(ctx, `
|
|
|
|
UPDATE objects SET
|
2020-11-16 16:46:47 +00:00
|
|
|
encrypted_metadata_nonce = $6,
|
|
|
|
encrypted_metadata = $7,
|
|
|
|
encrypted_metadata_encrypted_key = $8
|
2020-11-02 16:32:24 +00:00
|
|
|
WHERE
|
|
|
|
project_id = $1 AND
|
|
|
|
bucket_name = $2 AND
|
|
|
|
object_key = $3 AND
|
|
|
|
version = $4 AND
|
|
|
|
stream_id = $5 AND
|
2020-11-16 13:58:22 +00:00
|
|
|
status = `+committedStatus,
|
2021-03-02 11:22:49 +00:00
|
|
|
opts.ProjectID, []byte(opts.BucketName), []byte(opts.ObjectKey), opts.Version, opts.StreamID,
|
2020-11-16 16:46:47 +00:00
|
|
|
opts.EncryptedMetadataNonce, opts.EncryptedMetadata, opts.EncryptedMetadataEncryptedKey)
|
2020-11-02 16:32:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return Error.New("unable to update object metadata: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return Error.New("failed to get rows affected: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if affected == 0 {
|
|
|
|
return storj.ErrObjectNotFound.Wrap(
|
|
|
|
Error.New("object with specified version and committed status is missing"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|