Skip to content

Unregister views to avoid slow oom issue during meter cleanup #2005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion metrics/resource_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,16 @@ func cleanup() {
expiryCutoff := allMeters.clock.Now().Add(-1 * maxMeterExporterAge)
allMeters.lock.Lock()
defer allMeters.lock.Unlock()
resourceViews.lock.Lock()
defer resourceViews.lock.Unlock()
for key, meter := range allMeters.meters {
if key != "" && meter.t.Before(expiryCutoff) {
flushGivenExporter(meter.e)
// Make a copy of views to avoid data races
viewsCopy := copyViews(resourceViews.views)
meter.m.Unregister(viewsCopy...)
delete(allMeters.meters, key)
meter.m.Stop()
}
}
}
Expand Down Expand Up @@ -139,7 +145,7 @@ func RegisterResourceView(views ...*view.View) error {
return nil
}

// UnregisterResourceView is similar to view.Unregiste(), except that it will
// UnregisterResourceView is similar to view.Unregister(), except that it will
// unregister the view across all Resources tracked byt he system, rather than
// simply the default view.
func UnregisterResourceView(views ...*view.View) {
Expand Down
73 changes: 72 additions & 1 deletion metrics/resource_view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/apimachinery/pkg/util/wait"
)

var (
Expand Down Expand Up @@ -74,6 +75,8 @@ type testExporter struct {
id string
}

func (testExporter) ExportView(viewData *view.Data) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of interest: what is this needed for? I quickly checked the branch out and the tests still seem to pass with this line deleted

Copy link
Contributor Author

@skonto skonto Feb 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not all tests pass when using -tagnostackdriver. When a view is being unregistered it exports the data before it is unregistered and if there is no method it will fail with a nill pointer exception.


func TestSetFactory(t *testing.T) {
var oldFactory ResourceExporterFactory
func() {
Expand Down Expand Up @@ -170,7 +173,13 @@ func TestAllMetersExpiration(t *testing.T) {

// Expire the second entry
fakeClock.Step(9 * time.Minute) // t+12m
time.Sleep(time.Second) // Wait a second on the wallclock, so that the cleanup thread has time to finish a loop
_ = wait.PollImmediate(100*time.Millisecond, 5*time.Second, func() (bool, error) {
// Non-expiring defaultMeter should be available along with the non-expired entry
allMeters.lock.Lock()
defer allMeters.lock.Unlock()
return len(allMeters.meters) == 2, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well that's just one — what's the second one?
Also I'd suggest 100ms/5s params for poll

Copy link
Contributor Author

@skonto skonto Feb 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second one? There was another test there using the delay to let the cleanup thread run. I changed that too to use a poll.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant len==2, the comment suggests thaere should be 1?

Copy link
Contributor Author

@skonto skonto Feb 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is the default one and one that comes from the test. At that point the test has added 2 meters and expired one of them. However there is also the default one so in total there 2 of them at that point. I will update the comment so that this is clear.

})

allMeters.lock.Lock()
if len(allMeters.meters) != 2 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonder why we need a lock for the other accesses to len(allMeters.meters) (e.g. L179 and L157) but we dont here 🤔 (race detector isn't complaining, though, so maybe it's fine 🤷)

Copy link
Contributor Author

@skonto skonto Feb 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the nature of the test there should not be a race, we progress the clock manually and when polling ends we should read the right value. No other goroutine is expected to write anything in allMeters at that point. For the sake of readability and having less confusion I can change it and do a lock/unlock there.

t.Errorf("len(allMeters)=%d, want: 2", len(allMeters.meters))
Expand All @@ -189,6 +198,68 @@ func TestAllMetersExpiration(t *testing.T) {
// (123=9m, 456=evicted, 789=0m)
}

func TestIfAllMeterResourcesAreRemoved(t *testing.T) {
allMeters.clock = clock.Clock(clock.NewFakeClock(time.Now()))
var fakeClock *clock.FakeClock = allMeters.clock.(*clock.FakeClock)
ClearMetersForTest() // t+0m
// Register many resources at once
for i := 1; i <= 100; i++ {
res := resource.Resource{Labels: map[string]string{"foo": "bar"}}
res.Labels["id"] = fmt.Sprint(i)
if _, err := optionForResource(&res); err != nil {
t.Error("Should succeed getting option, instead got error ", err)
}
m := stats.Int64("testView_sum", "", stats.UnitDimensionless)
v := view.View{Name: fmt.Sprintf("testview-%d", i), Measure: m, Aggregation: view.Sum()}

err := RegisterResourceView(&v)
if err != nil {
t.Fatal("RegisterResourceView =", err)
}
}

func() {
allMeters.lock.Lock()
resourceViews.lock.Lock()
defer allMeters.lock.Unlock()
defer resourceViews.lock.Unlock()
// Make a copy to test against as allMeters should be cleaned up
copyAllMeters := make(map[string]*meterExporter, len(allMeters.meters))
for k, v := range allMeters.meters {
copyAllMeters[k] = v
}

for _, meter := range copyAllMeters {
for _, mView := range resourceViews.views {
want, got := mView, meter.m.Find(mView.Name)
if got == nil {
t.Errorf("View %s is not available", want.Name)
} else if want.Name != got.Name {
t.Errorf("Want %v, got %v", want.Name, got.Name)
}
}
}
}()

// Expire all meters and views
// We need to unlock before we move the clock ahead in time
fakeClock.Step(12 * time.Minute) // t+12m
_ = wait.PollImmediate(100*time.Millisecond, 5*time.Second, func() (bool, error) {
// Non-expiring defaultMeter should be available
allMeters.lock.Lock()
defer allMeters.lock.Unlock()
return len(allMeters.meters) == 1, nil
})

allMeters.lock.Lock()
defer allMeters.lock.Unlock()
resourceViews.lock.Lock()
defer resourceViews.lock.Unlock()
if len(allMeters.meters) != 1 {
t.Errorf("len(allMeters)=%d, want: 1", len(allMeters.meters))
}
}

func TestResourceAsString(t *testing.T) {
r1 := &resource.Resource{Type: "foobar", Labels: map[string]string{"k1": "v1", "k3": "v3", "k2": "v2"}}
r2 := &resource.Resource{Type: "foobar", Labels: map[string]string{"k2": "v2", "k3": "v3", "k1": "v1"}}
Expand Down