-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dbnode] Track blocks recently queried within some duration in dbnode (…
- Loading branch information
Showing
7 changed files
with
261 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright (c) 2020 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package stats | ||
|
||
import ( | ||
"time" | ||
|
||
"go.uber.org/atomic" | ||
) | ||
|
||
// For tracking query stats in past X duration such as blocks queried. | ||
type queryStats struct { | ||
tracker QueryStatsTracker | ||
|
||
recentDocs *atomic.Int64 | ||
stopCh chan struct{} | ||
} | ||
|
||
type noOpQueryStats struct { | ||
} | ||
|
||
// QueryStats provides an interface for updating query stats. | ||
type QueryStats interface { | ||
Update(newDocs int) error | ||
Start() | ||
Stop() | ||
} | ||
|
||
// QueryStatsTracker provides an interface for tracking current query stats. | ||
type QueryStatsTracker interface { | ||
Lookback() time.Duration | ||
TrackDocs(recentDocs int) error | ||
} | ||
|
||
// NewQueryStats enables query stats to be tracked within a recency lookback duration. | ||
func NewQueryStats(tracker QueryStatsTracker) QueryStats { | ||
return &queryStats{ | ||
tracker: tracker, | ||
recentDocs: atomic.NewInt64(0), | ||
stopCh: make(chan struct{}), | ||
} | ||
} | ||
|
||
// NoOpQueryStats returns inactive query stats. | ||
func NoOpQueryStats() QueryStats { | ||
return &noOpQueryStats{} | ||
} | ||
|
||
// UpdateQueryStats adds new query stats which are being tracked. | ||
func (q *queryStats) Update(newDocs int) error { | ||
if q == nil { | ||
return nil | ||
} | ||
if newDocs <= 0 { | ||
return nil | ||
} | ||
|
||
// Add the new stats to the global state. | ||
recentDocs := q.recentDocs.Add(int64(newDocs)) | ||
|
||
// Invoke the custom tracker based on the new stats values. | ||
return q.tracker.TrackDocs(int(recentDocs)) | ||
} | ||
|
||
// Start initializes background processing for handling query stats. | ||
func (q *queryStats) Start() { | ||
if q == nil { | ||
return | ||
} | ||
go func() { | ||
ticker := time.NewTicker(q.tracker.Lookback()) | ||
defer ticker.Stop() | ||
for { | ||
select { | ||
case <-ticker.C: | ||
// Clear recent docs every X duration. | ||
q.recentDocs.Store(0) | ||
|
||
// Also invoke the track func for having zero value. | ||
q.tracker.TrackDocs(0) | ||
case <-q.stopCh: | ||
return | ||
} | ||
} | ||
}() | ||
} | ||
|
||
func (q *queryStats) Stop() { | ||
if q == nil { | ||
return | ||
} | ||
close(q.stopCh) | ||
} | ||
|
||
func (q *noOpQueryStats) Update(newDocs int) error { | ||
return nil | ||
} | ||
|
||
func (q *noOpQueryStats) Stop() { | ||
} | ||
|
||
func (q *noOpQueryStats) Start() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) 2020 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package stats | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/m3db/m3/src/x/instrument" | ||
|
||
"github.com/uber-go/tally" | ||
) | ||
|
||
const defaultLookback = time.Second * 5 | ||
|
||
// Tracker implementation that emits query stats as metrics. | ||
type queryStatsMetricsTracker struct { | ||
recentDocs tally.Gauge | ||
totalDocs tally.Counter | ||
} | ||
|
||
// DefaultQueryStatsTrackerForMetrics provides a tracker | ||
// implementation that emits query stats as metrics. | ||
func DefaultQueryStatsTrackerForMetrics(opts instrument.Options) QueryStatsTracker { | ||
scope := opts. | ||
MetricsScope(). | ||
SubScope("query-stats") | ||
return &queryStatsMetricsTracker{ | ||
recentDocs: scope.Gauge("recent-docs-per-block"), | ||
totalDocs: scope.Counter("total-docs-per-block"), | ||
} | ||
} | ||
|
||
func (t *queryStatsMetricsTracker) TrackDocs(recentDocs int) error { | ||
t.recentDocs.Update(float64(recentDocs)) | ||
t.totalDocs.Inc(int64(recentDocs)) | ||
return nil | ||
} | ||
|
||
func (t *queryStatsMetricsTracker) Lookback() time.Duration { | ||
return defaultLookback | ||
} |