-
Notifications
You must be signed in to change notification settings - Fork 332
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
Changes from all commits
3e89b8d
7c2c04d
944b345
4a399c8
320f55d
8023f0e
8b1987f
7e05f81
e6dd2de
4a2c053
2059b06
1208261
5000a74
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ( | ||
|
@@ -74,6 +75,8 @@ type testExporter struct { | |
id string | ||
} | ||
|
||
func (testExporter) ExportView(viewData *view.Data) {} | ||
|
||
func TestSetFactory(t *testing.T) { | ||
var oldFactory ResourceExporterFactory | ||
func() { | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well that's just one — what's the second one? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant len==2, the comment suggests thaere should be 1? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wonder why we need a lock for the other accesses to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
|
@@ -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"}} | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.