2020-12-11 12:09:59 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package metabase
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2021-03-17 10:20:21 +00:00
|
|
|
"time"
|
2020-12-11 12:09:59 +00:00
|
|
|
|
2020-12-21 10:48:48 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2021-03-12 11:23:44 +00:00
|
|
|
"storj.io/common/storj"
|
2020-12-11 12:09:59 +00:00
|
|
|
"storj.io/common/uuid"
|
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UpdateSegmentPieces contains arguments necessary for updating segment pieces.
|
|
|
|
type UpdateSegmentPieces struct {
|
|
|
|
StreamID uuid.UUID
|
|
|
|
Position SegmentPosition
|
|
|
|
|
|
|
|
OldPieces Pieces
|
2021-03-12 11:23:44 +00:00
|
|
|
|
|
|
|
NewRedundancy storj.RedundancyScheme
|
|
|
|
NewPieces Pieces
|
2021-03-17 10:20:21 +00:00
|
|
|
|
|
|
|
NewRepairedAt time.Time // sets new time of last segment repair (optional).
|
2020-12-11 12:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateSegmentPieces updates pieces for specified segment. If provided old pieces
|
|
|
|
// won't match current database state update will fail.
|
|
|
|
func (db *DB) UpdateSegmentPieces(ctx context.Context, opts UpdateSegmentPieces) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2020-12-21 10:48:48 +00:00
|
|
|
if opts.StreamID.IsZero() {
|
2020-12-11 12:09:59 +00:00
|
|
|
return ErrInvalidRequest.New("StreamID missing")
|
2020-12-21 10:48:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := opts.OldPieces.Verify(); err != nil {
|
|
|
|
if ErrInvalidRequest.Has(err) {
|
|
|
|
return ErrInvalidRequest.New("OldPieces: %v", errs.Unwrap(err))
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-12 11:23:44 +00:00
|
|
|
if opts.NewRedundancy.IsZero() {
|
|
|
|
return ErrInvalidRequest.New("NewRedundancy zero")
|
|
|
|
}
|
|
|
|
|
|
|
|
// its possible that in this method we will have less pieces
|
|
|
|
// than optimal shares (e.g. after repair)
|
|
|
|
if len(opts.NewPieces) < int(opts.NewRedundancy.RepairShares) {
|
|
|
|
return ErrInvalidRequest.New("number of new pieces is less than new redundancy repair shares value")
|
|
|
|
}
|
|
|
|
|
2020-12-21 10:48:48 +00:00
|
|
|
if err := opts.NewPieces.Verify(); err != nil {
|
|
|
|
if ErrInvalidRequest.Has(err) {
|
|
|
|
return ErrInvalidRequest.New("NewPieces: %v", errs.Unwrap(err))
|
|
|
|
}
|
|
|
|
return err
|
2020-12-11 12:09:59 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 10:20:21 +00:00
|
|
|
updateRepairAt := !opts.NewRepairedAt.IsZero()
|
|
|
|
|
2022-09-15 14:50:13 +01:00
|
|
|
oldPieces, err := db.aliasCache.EnsurePiecesToAliases(ctx, opts.OldPieces)
|
2021-02-08 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return Error.New("unable to convert pieces to aliases: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-09-15 14:50:13 +01:00
|
|
|
newPieces, err := db.aliasCache.EnsurePiecesToAliases(ctx, opts.NewPieces)
|
2021-02-08 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return Error.New("unable to convert pieces to aliases: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var resultPieces AliasPieces
|
2021-07-28 14:44:22 +01:00
|
|
|
err = db.db.QueryRowContext(ctx, `
|
2020-12-11 12:09:59 +00:00
|
|
|
UPDATE segments SET
|
2021-02-08 09:33:45 +00:00
|
|
|
remote_alias_pieces = CASE
|
|
|
|
WHEN remote_alias_pieces = $3 THEN $4
|
|
|
|
ELSE remote_alias_pieces
|
2021-03-12 11:23:44 +00:00
|
|
|
END,
|
|
|
|
redundancy = CASE
|
|
|
|
WHEN remote_alias_pieces = $3 THEN $5
|
|
|
|
ELSE redundancy
|
2021-03-17 10:20:21 +00:00
|
|
|
END,
|
|
|
|
repaired_at = CASE
|
|
|
|
WHEN remote_alias_pieces = $3 AND $7 = true THEN $6
|
|
|
|
ELSE repaired_at
|
2020-12-11 12:09:59 +00:00
|
|
|
END
|
|
|
|
WHERE
|
|
|
|
stream_id = $1 AND
|
|
|
|
position = $2
|
2021-02-08 09:33:45 +00:00
|
|
|
RETURNING remote_alias_pieces
|
2021-03-17 10:20:21 +00:00
|
|
|
`, opts.StreamID, opts.Position, oldPieces, newPieces, redundancyScheme{&opts.NewRedundancy}, opts.NewRepairedAt, updateRepairAt).
|
2021-03-12 11:23:44 +00:00
|
|
|
Scan(&resultPieces)
|
2020-12-11 12:09:59 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2020-12-15 12:49:58 +00:00
|
|
|
return ErrSegmentNotFound.New("segment missing")
|
2020-12-11 12:09:59 +00:00
|
|
|
}
|
|
|
|
return Error.New("unable to update segment pieces: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-02-08 09:33:45 +00:00
|
|
|
if !EqualAliasPieces(newPieces, resultPieces) {
|
|
|
|
return storage.ErrValueChanged.New("segment remote_alias_pieces field was changed")
|
2020-12-11 12:09:59 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 14:21:09 +01:00
|
|
|
mon.Meter("segment_update").Mark(1)
|
|
|
|
|
2020-12-11 12:09:59 +00:00
|
|
|
return nil
|
|
|
|
}
|