Skip to content

Commit

Permalink
remove useless function
Browse files Browse the repository at this point in the history
  • Loading branch information
ying-jeanne committed Jan 16, 2025
1 parent e5cda41 commit 41e9f47
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
5 changes: 1 addition & 4 deletions pkg/costattribution/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,7 @@ func (m *Manager) purgeInactiveAttributionsUntil(deadline time.Time) error {
continue
}

invalidKeys := st.inactiveObservations(deadline)
for _, key := range invalidKeys {
st.cleanupTrackerAttribution(key)
}
st.cleanupInactiveObservations(deadline)

// only sample tracker can recovered from overflow, the activeseries tracker after the cooldown would just be deleted and recreated
if st.recoveredFromOverflow(deadline) {
Expand Down
16 changes: 7 additions & 9 deletions pkg/costattribution/sample_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ var bufferPool = sync.Pool{
},
}

func (st *SampleTracker) cleanupTrackerAttribution(key string) {
st.observedMtx.Lock()
defer st.observedMtx.Unlock()
delete(st.observed, key)
}

func (st *SampleTracker) Collect(out chan<- prometheus.Metric) {
// We don't know the performance of out receiver, so we don't want to hold the lock for too long
var prometheusMetrics []prometheus.Metric
Expand Down Expand Up @@ -304,16 +298,20 @@ func (st *SampleTracker) recoveredFromOverflow(deadline time.Time) bool {
return false
}

func (st *SampleTracker) inactiveObservations(deadline time.Time) []string {
func (st *SampleTracker) cleanupInactiveObservations(deadline time.Time) {
// otherwise, we need to check all observations and clean up the ones that are inactive
var invalidKeys []string
st.observedMtx.RLock()
defer st.observedMtx.RUnlock()
for labkey, ob := range st.observed {
if ob != nil && ob.lastUpdate.Load() <= deadline.Unix() {
invalidKeys = append(invalidKeys, labkey)
}
}
st.observedMtx.RUnlock()

return invalidKeys
st.observedMtx.Lock()
for _, key := range invalidKeys {
delete(st.observed, key)
}
st.observedMtx.Unlock()
}
17 changes: 8 additions & 9 deletions pkg/costattribution/sample_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,17 @@ func TestSampleTracker_inactiveObservations(t *testing.T) {
require.Len(t, st.observed, 3)

// Purge observations that haven't been updated in the last 10 seconds.
purged := st.inactiveObservations(time.Unix(0, 0))
require.Len(t, purged, 0)
st.cleanupInactiveObservations(time.Unix(0, 0))
require.Len(t, st.observed, 3)

purged = st.inactiveObservations(time.Unix(10, 0))
assert.ElementsMatch(t, []string{"foo"}, purged)
st.cleanupInactiveObservations(time.Unix(10, 0))
assert.Len(t, st.observed, 2)

purged = st.inactiveObservations(time.Unix(15, 0))
assert.ElementsMatch(t, []string{"foo", "bar"}, purged)
st.cleanupInactiveObservations(time.Unix(15, 0))
assert.Len(t, st.observed, 1)

// Check that the purged observation matches the expected details.
purged = st.inactiveObservations(time.Unix(25, 0))
assert.ElementsMatch(t, []string{"foo", "bar", "baz"}, purged)
st.cleanupInactiveObservations(time.Unix(25, 0))
assert.Len(t, st.observed, 0)
}

func TestSampleTracker_Concurrency(t *testing.T) {
Expand Down

0 comments on commit 41e9f47

Please sign in to comment.