From 141444f6d6e14b20f5b24f4fb36704517e061b55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Niewrza=C5=82?= Date: Wed, 17 Mar 2021 11:31:07 +0100 Subject: [PATCH] satellite/repair/repairer: fix segmentAge metric Change-Id: I146b3163aa1bfab5ee060298e6bf9822ca6820a0 --- satellite/metainfo/metabase/get.go | 16 +++++----- satellite/metainfo/metabase/update_test.go | 33 +++++++++++++++++++- satellite/repair/repairer/segments.go | 35 ++++++++++++++-------- 3 files changed, 62 insertions(+), 22 deletions(-) diff --git a/satellite/metainfo/metabase/get.go b/satellite/metainfo/metabase/get.go index cf9eeba2e..8a7978726 100644 --- a/satellite/metainfo/metabase/get.go +++ b/satellite/metainfo/metabase/get.go @@ -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, diff --git a/satellite/metainfo/metabase/update_test.go b/satellite/metainfo/metabase/update_test.go index 9b7962bd3..988b7c553 100644 --- a/satellite/metainfo/metabase/update_test.go +++ b/satellite/metainfo/metabase/update_test.go @@ -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{ { diff --git a/satellite/repair/repairer/segments.go b/satellite/repair/repairer/segments.go index d854430d7..3e83fe8cc 100644 --- a/satellite/repair/repairer/segments.go +++ b/satellite/repair/repairer/segments.go @@ -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