satellite/repair/repairer: fix segmentAge metric

Change-Id: I146b3163aa1bfab5ee060298e6bf9822ca6820a0
This commit is contained in:
Michał Niewrzał 2021-03-17 11:31:07 +01:00 committed by Michal Niewrzal
parent 4b14839e14
commit 141444f6d6
3 changed files with 62 additions and 22 deletions

View File

@ -171,7 +171,7 @@ func (db *DB) GetSegmentByLocation(ctx context.Context, opts GetSegmentByLocatio
err = db.db.QueryRow(ctx, `
SELECT
stream_id,
created_at,
created_at, repaired_at,
root_piece_id, encrypted_key_nonce, encrypted_key,
encrypted_size, plain_offset, plain_size,
encrypted_etag,
@ -190,7 +190,7 @@ func (db *DB) GetSegmentByLocation(ctx context.Context, opts GetSegmentByLocatio
`, opts.ProjectID, []byte(opts.BucketName), []byte(opts.ObjectKey), opts.Position.Encode()).
Scan(
&segment.StreamID,
&segment.CreatedAt,
&segment.CreatedAt, &segment.RepairedAt,
&segment.RootPieceID, &segment.EncryptedKeyNonce, &segment.EncryptedKey,
&segment.EncryptedSize, &segment.PlainOffset, &segment.PlainSize,
&segment.EncryptedETag,
@ -238,7 +238,7 @@ func (db *DB) GetSegmentByPosition(ctx context.Context, opts GetSegmentByPositio
var aliasPieces AliasPieces
err = db.db.QueryRow(ctx, `
SELECT
created_at,
created_at, repaired_at,
root_piece_id, encrypted_key_nonce, encrypted_key,
encrypted_size, plain_offset, plain_size,
encrypted_etag,
@ -250,7 +250,7 @@ func (db *DB) GetSegmentByPosition(ctx context.Context, opts GetSegmentByPositio
position = $2
`, opts.StreamID, opts.Position.Encode()).
Scan(
&segment.CreatedAt,
&segment.CreatedAt, &segment.RepairedAt,
&segment.RootPieceID, &segment.EncryptedKeyNonce, &segment.EncryptedKey,
&segment.EncryptedSize, &segment.PlainOffset, &segment.PlainSize,
&segment.EncryptedETag,
@ -292,7 +292,7 @@ func (db *DB) GetLatestObjectLastSegment(ctx context.Context, opts GetLatestObje
err = db.db.QueryRow(ctx, `
SELECT
stream_id, position,
created_at,
created_at, repaired_at,
root_piece_id, encrypted_key_nonce, encrypted_key,
encrypted_size, plain_offset, plain_size,
encrypted_etag,
@ -313,7 +313,7 @@ func (db *DB) GetLatestObjectLastSegment(ctx context.Context, opts GetLatestObje
`, opts.ProjectID, []byte(opts.BucketName), []byte(opts.ObjectKey)).
Scan(
&segment.StreamID, &segment.Position,
&segment.CreatedAt,
&segment.CreatedAt, &segment.RepairedAt,
&segment.RootPieceID, &segment.EncryptedKeyNonce, &segment.EncryptedKey,
&segment.EncryptedSize, &segment.PlainOffset, &segment.PlainSize,
&segment.EncryptedETag,
@ -357,7 +357,7 @@ func (db *DB) GetSegmentByOffset(ctx context.Context, opts GetSegmentByOffset) (
err = db.db.QueryRow(ctx, `
SELECT
stream_id, position,
created_at,
created_at, repaired_at,
root_piece_id, encrypted_key_nonce, encrypted_key,
encrypted_size, plain_offset, plain_size,
encrypted_etag,
@ -380,7 +380,7 @@ func (db *DB) GetSegmentByOffset(ctx context.Context, opts GetSegmentByOffset) (
`, opts.ProjectID, []byte(opts.BucketName), []byte(opts.ObjectKey), opts.PlainOffset).
Scan(
&segment.StreamID, &segment.Position,
&segment.CreatedAt,
&segment.CreatedAt, &segment.RepairedAt,
&segment.RootPieceID, &segment.EncryptedKeyNonce, &segment.EncryptedKey,
&segment.EncryptedSize, &segment.PlainOffset, &segment.PlainSize,
&segment.EncryptedETag,

View File

@ -7,6 +7,8 @@ import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/require"
"storj.io/common/storj"
@ -366,8 +368,8 @@ func TestUpdateSegmentPieces(t *testing.T) {
StreamID: obj.StreamID,
Position: metabase.SegmentPosition{Index: 0},
OldPieces: segment.Pieces,
NewRedundancy: segment.Redundancy,
NewPieces: expectedPieces,
NewRedundancy: defaultTestRedundancy,
NewRepairedAt: repairedAt,
},
}.Check(ctx, t, db)
@ -375,6 +377,35 @@ func TestUpdateSegmentPieces(t *testing.T) {
expectedSegment := segment
expectedSegment.Pieces = expectedPieces
expectedSegment.RepairedAt = &repairedAt
segment, err = db.GetSegmentByLocation(ctx, metabase.GetSegmentByLocation{
SegmentLocation: metabase.SegmentLocation{
ProjectID: object.ProjectID,
BucketName: object.BucketName,
ObjectKey: object.ObjectKey,
Position: metabase.SegmentPosition{Index: 0},
},
})
require.NoError(t, err)
diff := cmp.Diff(expectedSegment, segment, cmpopts.EquateApproxTime(5*time.Second))
require.Zero(t, diff)
segment, err = db.GetSegmentByPosition(ctx, metabase.GetSegmentByPosition{
StreamID: object.StreamID,
Position: metabase.SegmentPosition{Index: 0},
})
require.NoError(t, err)
diff = cmp.Diff(expectedSegment, segment, cmpopts.EquateApproxTime(5*time.Second))
require.Zero(t, diff)
segment, err = db.GetSegmentByOffset(ctx, metabase.GetSegmentByOffset{
ObjectLocation: object.Location(),
PlainOffset: 0,
})
require.NoError(t, err)
diff = cmp.Diff(expectedSegment, segment, cmpopts.EquateApproxTime(5*time.Second))
require.Zero(t, diff)
Verify{
Objects: []metabase.RawObject{
{

View File

@ -373,18 +373,6 @@ func (repairer *SegmentRepairer) Repair(ctx context.Context, path storj.Path) (s
// add pieces that failed piece hashes verification to the removal list
toRemove = append(toRemove, failedPieces...)
var segmentAge time.Duration
var repairCount int64
// TODO what to do with segmentAge and RepairCount
// if pointer.CreationDate.Before(pointer.LastRepaired) {
// segmentAge = time.Since(pointer.LastRepaired)
// } else {
// segmentAge = time.Since(pointer.CreationDate)
// }
// pointer.LastRepaired = time.Now().UTC()
// pointer.RepairCount++
newPieces, err := updatePieces(segment.Pieces, repairedPieces, toRemove)
if err != nil {
return false, repairPutError.Wrap(err)
@ -397,12 +385,33 @@ func (repairer *SegmentRepairer) Repair(ctx context.Context, path storj.Path) (s
OldPieces: segment.Pieces,
NewRedundancy: segment.Redundancy,
NewPieces: newPieces,
NewRepairedAt: time.Now(),
})
if err != nil {
return false, metainfoPutError.Wrap(err)
}
// TODO all values bellow are zero
createdAt := time.Time{}
if segment.CreatedAt != nil {
createdAt = *segment.CreatedAt
}
repairedAt := time.Time{}
if segment.RepairedAt != nil {
repairedAt = *segment.RepairedAt
}
var segmentAge time.Duration
if createdAt.Before(repairedAt) {
segmentAge = time.Since(repairedAt)
} else {
segmentAge = time.Since(createdAt)
}
// TODO what to do with RepairCount
var repairCount int64
// pointer.RepairCount++
mon.IntVal("segment_time_until_repair").Observe(int64(segmentAge.Seconds())) //mon:locked
stats.segmentTimeUntilRepair.Observe((int64(segmentAge.Seconds())))
mon.IntVal("segment_repair_count").Observe(repairCount) //mon:locked