satellite/metainfo/metabase: remove sql transaction while committing segments

Change-Id: Ia2cb1df63e1cbb8b9d36b0161a75b9c9cb3a56f9
This commit is contained in:
Michał Niewrzał 2021-01-21 13:17:51 +01:00 committed by Michal Niewrzal
parent 292caa9043
commit ce675b7707

View File

@ -257,28 +257,8 @@ func (db *DB) CommitSegment(ctx context.Context, opts CommitSegment) (err error)
// TODO: verify opts.Pieces is compatible with opts.Redundancy // TODO: verify opts.Pieces is compatible with opts.Redundancy
return txutil.WithTx(ctx, db.db, nil, func(ctx context.Context, tx tagsql.Tx) error {
// Verify that object exists and is partial. // Verify that object exists and is partial.
var value int _, err = db.db.ExecContext(ctx, `
err = tx.QueryRowContext(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
status = `+pendingStatus,
opts.ProjectID, opts.BucketName, []byte(opts.ObjectKey), opts.Version, opts.StreamID).Scan(&value)
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)
}
// Insert into segments.
_, err = tx.ExecContext(ctx, `
INSERT INTO segments ( INSERT INTO segments (
stream_id, position, stream_id, position,
root_piece_id, encrypted_key_nonce, encrypted_key, root_piece_id, encrypted_key_nonce, encrypted_key,
@ -286,27 +266,36 @@ func (db *DB) CommitSegment(ctx context.Context, opts CommitSegment) (err error)
redundancy, redundancy,
remote_pieces remote_pieces
) VALUES ( ) VALUES (
$1, $2, (SELECT stream_id
$3, $4, $5, FROM objects WHERE
$6, $7, $8, project_id = $10 AND
$9, bucket_name = $11 AND
$10 object_key = $12 AND
)`, version = $13 AND
opts.StreamID, opts.Position, stream_id = $14 AND
status = `+pendingStatus+
` ), $1,
$2, $3, $4,
$5, $6, $7,
$8,
$9
)`, opts.Position,
opts.RootPieceID, opts.EncryptedKeyNonce, opts.EncryptedKey, opts.RootPieceID, opts.EncryptedKeyNonce, opts.EncryptedKey,
opts.EncryptedSize, opts.PlainOffset, opts.PlainSize, opts.EncryptedSize, opts.PlainOffset, opts.PlainSize,
redundancyScheme{&opts.Redundancy}, redundancyScheme{&opts.Redundancy},
opts.Pieces, opts.Pieces,
opts.ProjectID, opts.BucketName, []byte(opts.ObjectKey), opts.Version, opts.StreamID,
) )
if err != nil { if err != nil {
if code := pgerrcode.FromError(err); code == pgxerrcode.NotNullViolation {
return Error.New("pending object missing")
}
if code := pgerrcode.FromError(err); code == pgxerrcode.UniqueViolation { if code := pgerrcode.FromError(err); code == pgxerrcode.UniqueViolation {
return ErrConflict.New("segment already exists") return ErrConflict.New("segment already exists")
} }
return Error.New("unable to insert segment: %w", err) return Error.New("unable to insert segment: %w", err)
} }
return nil return nil
})
} }
// CommitInlineSegment contains all necessary information about the segment. // CommitInlineSegment contains all necessary information about the segment.
@ -347,53 +336,42 @@ func (db *DB) CommitInlineSegment(ctx context.Context, opts CommitInlineSegment)
return ErrInvalidRequest.New("PlainOffset negative") return ErrInvalidRequest.New("PlainOffset negative")
} }
return txutil.WithTx(ctx, db.db, nil, func(ctx context.Context, tx tagsql.Tx) error {
// Verify that object exists and is partial. // Verify that object exists and is partial.
var value int _, err = db.db.ExecContext(ctx, `
err = tx.QueryRowContext(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
status = `+pendingStatus,
opts.ProjectID, opts.BucketName, []byte(opts.ObjectKey), opts.Version, opts.StreamID).Scan(&value)
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)
}
// Insert into segments.
_, err = tx.ExecContext(ctx, `
INSERT INTO segments ( INSERT INTO segments (
stream_id, position, stream_id, position,
root_piece_id, encrypted_key_nonce, encrypted_key, root_piece_id, encrypted_key_nonce, encrypted_key,
encrypted_size, plain_offset, plain_size, encrypted_size, plain_offset, plain_size,
inline_data inline_data
) VALUES ( ) VALUES (
$1, $2, (SELECT stream_id
$3, $4, $5, FROM objects WHERE
$6, $7, $8, project_id = $9 AND
$9 bucket_name = $10 AND
)`, object_key = $11 AND
opts.StreamID, opts.Position, version = $12 AND
stream_id = $13 AND
status = `+pendingStatus+
` ), $1,
$2, $3, $4,
$5, $6, $7,
$8
)`, opts.Position,
storj.PieceID{}, opts.EncryptedKeyNonce, opts.EncryptedKey, storj.PieceID{}, opts.EncryptedKeyNonce, opts.EncryptedKey,
len(opts.InlineData), opts.PlainOffset, opts.PlainSize, len(opts.InlineData), opts.PlainOffset, opts.PlainSize,
opts.InlineData, opts.InlineData,
opts.ProjectID, opts.BucketName, []byte(opts.ObjectKey), opts.Version, opts.StreamID,
) )
if err != nil { if err != nil {
if code := pgerrcode.FromError(err); code == pgxerrcode.NotNullViolation {
return Error.New("pending object missing")
}
if code := pgerrcode.FromError(err); code == pgxerrcode.UniqueViolation { if code := pgerrcode.FromError(err); code == pgxerrcode.UniqueViolation {
return ErrConflict.New("segment already exists") return ErrConflict.New("segment already exists")
} }
return Error.New("unable to insert segment: %w", err) return Error.New("unable to insert segment: %w", err)
} }
return nil return nil
})
} }
// CommitObject contains arguments necessary for committing an object. // CommitObject contains arguments necessary for committing an object.