satellite/metainfo: reduce database hits for segment creation

Change-Id: I48a748b48daefa95f1dfbf6a0d75e65a568ee36a
This commit is contained in:
JT Olio 2023-12-01 13:44:17 -05:00 committed by Storj Robot
parent 5d492a9e01
commit 591971b4dc
3 changed files with 27 additions and 14 deletions

View File

@ -224,6 +224,8 @@ type BeginSegment struct {
RootPieceID storj.PieceID RootPieceID storj.PieceID
Pieces Pieces Pieces Pieces
ObjectExistsChecked bool
} }
// BeginSegment verifies, whether a new segment upload can be started. // BeginSegment verifies, whether a new segment upload can be started.
@ -242,24 +244,27 @@ func (db *DB) BeginSegment(ctx context.Context, opts BeginSegment) (err error) {
return ErrInvalidRequest.New("RootPieceID missing") return ErrInvalidRequest.New("RootPieceID missing")
} }
// NOTE: this isn't strictly necessary, since we can also fail this in CommitSegment. if !opts.ObjectExistsChecked {
// however, we should prevent creating segements for non-partial objects. // NOTE: Find a way to safely remove this. This isn't strictly necessary,
// since we can also fail this in CommitSegment.
// We should prevent creating segements for non-partial objects.
// Verify that object exists and is partial. // Verify that object exists and is partial.
var exists bool var exists bool
err = db.db.QueryRowContext(ctx, ` err = db.db.QueryRowContext(ctx, `
SELECT EXISTS ( SELECT EXISTS (
SELECT 1 SELECT 1
FROM objects FROM objects
WHERE (project_id, bucket_name, object_key, version, stream_id) = ($1, $2, $3, $4, $5) AND WHERE (project_id, bucket_name, object_key, version, stream_id) = ($1, $2, $3, $4, $5) AND
status = `+statusPending+` status = `+statusPending+`
)`, )`,
opts.ProjectID, []byte(opts.BucketName), opts.ObjectKey, opts.Version, opts.StreamID).Scan(&exists) opts.ProjectID, []byte(opts.BucketName), opts.ObjectKey, opts.Version, opts.StreamID).Scan(&exists)
if err != nil { if err != nil {
return Error.New("unable to query object status: %w", err) return Error.New("unable to query object status: %w", err)
} }
if !exists { if !exists {
return ErrPendingObjectMissing.New("") return ErrPendingObjectMissing.New("")
}
} }
mon.Meter("segment_begin").Mark(1) mon.Meter("segment_begin").Mark(1)

View File

@ -226,11 +226,13 @@ func (endpoint *Endpoint) Batch(ctx context.Context, req *pb.BatchRequest) (resp
case *pb.BatchRequestItem_SegmentBegin: case *pb.BatchRequestItem_SegmentBegin:
singleRequest.SegmentBegin.Header = req.Header singleRequest.SegmentBegin.Header = req.Header
justCreatedObject := false
if singleRequest.SegmentBegin.StreamId.IsZero() && !lastStreamID.IsZero() { if singleRequest.SegmentBegin.StreamId.IsZero() && !lastStreamID.IsZero() {
singleRequest.SegmentBegin.StreamId = lastStreamID singleRequest.SegmentBegin.StreamId = lastStreamID
justCreatedObject = true
} }
response, err := endpoint.BeginSegment(ctx, singleRequest.SegmentBegin) response, err := endpoint.beginSegment(ctx, singleRequest.SegmentBegin, justCreatedObject)
if err != nil { if err != nil {
return resp, err return resp, err
} }

View File

@ -31,6 +31,11 @@ func calculateSpaceUsed(segmentSize int64, numberOfPieces int, rs storj.Redundan
// BeginSegment begins segment uploading. // BeginSegment begins segment uploading.
func (endpoint *Endpoint) BeginSegment(ctx context.Context, req *pb.SegmentBeginRequest) (resp *pb.SegmentBeginResponse, err error) { func (endpoint *Endpoint) BeginSegment(ctx context.Context, req *pb.SegmentBeginRequest) (resp *pb.SegmentBeginResponse, err error) {
defer mon.Task()(&ctx)(&err) defer mon.Task()(&ctx)(&err)
return endpoint.beginSegment(ctx, req, false)
}
func (endpoint *Endpoint) beginSegment(ctx context.Context, req *pb.SegmentBeginRequest, objectJustCreated bool) (resp *pb.SegmentBeginResponse, err error) {
defer mon.Task()(&ctx)(&err)
endpoint.versionCollector.collect(req.Header.UserAgent, mon.Func().ShortName()) endpoint.versionCollector.collect(req.Header.UserAgent, mon.Func().ShortName())
@ -121,8 +126,9 @@ func (endpoint *Endpoint) BeginSegment(ctx context.Context, req *pb.SegmentBegin
Part: uint32(req.Position.PartNumber), Part: uint32(req.Position.PartNumber),
Index: uint32(req.Position.Index), Index: uint32(req.Position.Index),
}, },
RootPieceID: rootPieceID, RootPieceID: rootPieceID,
Pieces: pieces, Pieces: pieces,
ObjectExistsChecked: objectJustCreated,
}) })
if err != nil { if err != nil {
return nil, endpoint.convertMetabaseErr(err) return nil, endpoint.convertMetabaseErr(err)