2019-10-16 19:08:33 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-12-18 12:27:45 +00:00
|
|
|
"storj.io/common/uuid"
|
2021-07-01 15:48:07 +01:00
|
|
|
"storj.io/storj/satellite/metabase/segmentloop"
|
2019-10-16 19:08:33 +01:00
|
|
|
)
|
|
|
|
|
2021-07-01 15:48:07 +01:00
|
|
|
// Counter implements the segment loop observer interface for data science metrics collection.
|
2019-10-16 19:08:33 +01:00
|
|
|
//
|
|
|
|
// architecture: Observer
|
|
|
|
type Counter struct {
|
2021-07-01 15:48:07 +01:00
|
|
|
// number of objects that has at least one remote segment
|
|
|
|
RemoteObjects int64
|
|
|
|
// number of objects that has all inline segments
|
|
|
|
InlineObjects int64
|
2021-02-18 15:26:27 +00:00
|
|
|
|
2021-07-01 15:48:07 +01:00
|
|
|
lastStreamID uuid.UUID
|
|
|
|
onlyInline bool
|
2019-10-16 19:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCounter instantiates a new counter to be subscribed to the metainfo loop.
|
|
|
|
func NewCounter() *Counter {
|
2021-07-01 15:48:07 +01:00
|
|
|
return &Counter{
|
|
|
|
onlyInline: true,
|
|
|
|
}
|
2019-10-16 19:08:33 +01:00
|
|
|
}
|
|
|
|
|
2021-04-01 11:56:39 +01:00
|
|
|
// LoopStarted is called at each start of a loop.
|
2021-07-01 15:48:07 +01:00
|
|
|
func (counter *Counter) LoopStarted(context.Context, segmentloop.LoopInfo) (err error) {
|
2021-04-01 11:56:39 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-01 15:48:07 +01:00
|
|
|
// RemoteSegment increments the count for objects with remote segments.
|
|
|
|
func (counter *Counter) RemoteSegment(ctx context.Context, segment *segmentloop.Segment) (err error) {
|
2020-12-18 12:27:45 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2021-07-01 15:48:07 +01:00
|
|
|
counter.onlyInline = false
|
2019-10-16 19:08:33 +01:00
|
|
|
|
2021-07-01 15:48:07 +01:00
|
|
|
if counter.lastStreamID.Compare(segment.StreamID) != 0 {
|
|
|
|
counter.RemoteObjects++
|
2020-12-18 12:27:45 +00:00
|
|
|
|
2021-07-01 15:48:07 +01:00
|
|
|
counter.lastStreamID = segment.StreamID
|
|
|
|
counter.onlyInline = true
|
2020-12-18 12:27:45 +00:00
|
|
|
}
|
2019-10-16 19:08:33 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-18 12:27:45 +00:00
|
|
|
// InlineSegment increments the count for inline objects.
|
2021-07-01 15:48:07 +01:00
|
|
|
func (counter *Counter) InlineSegment(ctx context.Context, segment *segmentloop.Segment) (err error) {
|
|
|
|
if counter.lastStreamID.Compare(segment.StreamID) != 0 {
|
|
|
|
if counter.onlyInline {
|
|
|
|
counter.InlineObjects++
|
|
|
|
} else {
|
|
|
|
counter.RemoteObjects++
|
|
|
|
}
|
2021-02-18 15:26:27 +00:00
|
|
|
|
2021-07-01 15:48:07 +01:00
|
|
|
counter.lastStreamID = segment.StreamID
|
|
|
|
counter.onlyInline = true
|
|
|
|
}
|
|
|
|
return nil
|
2021-02-18 15:26:27 +00:00
|
|
|
}
|