Skip to content
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

[release/1.7] Cumulative stats can't decrease #10670

Merged
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
5 changes: 5 additions & 0 deletions pkg/cri/sbserver/container_stats_list.go

Choose a reason for hiding this comment

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

pkg/cri/sbserver/container_stats_list_test.go

Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ func (c *criService) getUsageNanoCores(containerID string, isSandbox bool, curre
return 0, nil
}

// can't go backwards, this value might come in as 0 if the container was just removed
if currentUsageCoreNanoSeconds < oldStats.UsageCoreNanoSeconds {
return 0, nil
}

newUsageNanoCores := uint64(float64(currentUsageCoreNanoSeconds-oldStats.UsageCoreNanoSeconds) /
float64(nanoSeconds) * float64(time.Second/time.Nanosecond))

Expand Down
27 changes: 19 additions & 8 deletions pkg/cri/sbserver/container_stats_list_test.go

Choose a reason for hiding this comment

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

pkg/cri/sbserver/container_stats_list_test.go

Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,56 @@ import (
func TestContainerMetricsCPUNanoCoreUsage(t *testing.T) {
c := newTestCRIService()
timestamp := time.Now()
secondAfterTimeStamp := timestamp.Add(time.Second)
ID := "ID"
tenSecondAftertimeStamp := timestamp.Add(time.Second * 10)

for desc, test := range map[string]struct {
id string
desc string
firstCPUValue uint64
secondCPUValue uint64
expectedNanoCoreUsageFirst uint64
expectedNanoCoreUsageSecond uint64
}{
"metrics": {
id: "id1",
desc: "metrics",
firstCPUValue: 50,
secondCPUValue: 500,
expectedNanoCoreUsageFirst: 0,
expectedNanoCoreUsageSecond: 450,
expectedNanoCoreUsageSecond: 45,
},
"no metrics in second CPU sample": {
id: "id2",
desc: "metrics",
firstCPUValue: 234235,
secondCPUValue: 0,
expectedNanoCoreUsageFirst: 0,
expectedNanoCoreUsageSecond: 0,
},
} {
t.Run(desc, func(t *testing.T) {
container, err := containerstore.NewContainer(
containerstore.Metadata{ID: ID},
containerstore.Metadata{ID: test.id},
)
assert.NoError(t, err)
assert.Nil(t, container.Stats)
err = c.containerStore.Add(container)
assert.NoError(t, err)

cpuUsage, err := c.getUsageNanoCores(ID, false, test.firstCPUValue, timestamp)
cpuUsage, err := c.getUsageNanoCores(test.id, false, test.firstCPUValue, timestamp)
assert.NoError(t, err)

container, err = c.containerStore.Get(ID)
container, err = c.containerStore.Get(test.id)
assert.NoError(t, err)
assert.NotNil(t, container.Stats)

assert.Equal(t, test.expectedNanoCoreUsageFirst, cpuUsage)

cpuUsage, err = c.getUsageNanoCores(ID, false, test.secondCPUValue, secondAfterTimeStamp)
cpuUsage, err = c.getUsageNanoCores(test.id, false, test.secondCPUValue, tenSecondAftertimeStamp)
assert.NoError(t, err)
assert.Equal(t, test.expectedNanoCoreUsageSecond, cpuUsage)

container, err = c.containerStore.Get(ID)
container, err = c.containerStore.Get(test.id)
assert.NoError(t, err)
assert.NotNil(t, container.Stats)
})
Expand Down
5 changes: 5 additions & 0 deletions pkg/cri/server/container_stats_list.go

Choose a reason for hiding this comment

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

pkg/cri/server/container_stats_list.go

Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ func (c *criService) getUsageNanoCores(containerID string, isSandbox bool, curre
return 0, nil
}

// can't go backwards, this value might come in as 0 if the container was just removed
if currentUsageCoreNanoSeconds < oldStats.UsageCoreNanoSeconds {
return 0, nil
}

newUsageNanoCores := uint64(float64(currentUsageCoreNanoSeconds-oldStats.UsageCoreNanoSeconds) /
float64(nanoSeconds) * float64(time.Second/time.Nanosecond))

Expand Down
28 changes: 19 additions & 9 deletions pkg/cri/server/container_stats_list_test.go

Choose a reason for hiding this comment

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

pkg/cri/server/container_stats_list_test.go

Original file line number Diff line number Diff line change
Expand Up @@ -27,48 +27,58 @@ import (
func TestContainerMetricsCPUNanoCoreUsage(t *testing.T) {
c := newTestCRIService()
timestamp := time.Now()
secondAfterTimeStamp := timestamp.Add(time.Second)
ID := "ID"
tenSecondAftertimeStamp := timestamp.Add(time.Second * 10)

for desc, test := range map[string]struct {
id string
desc string
firstCPUValue uint64
secondCPUValue uint64
expectedNanoCoreUsageFirst uint64
expectedNanoCoreUsageSecond uint64
}{
"metrics": {
id: "id1",
desc: "metrics",
firstCPUValue: 50,
secondCPUValue: 500,
expectedNanoCoreUsageFirst: 0,
expectedNanoCoreUsageSecond: 450,
expectedNanoCoreUsageSecond: 45,
},
"no metrics in second CPU sample": {
id: "id2",
desc: "metrics",
firstCPUValue: 234235,
secondCPUValue: 0,
expectedNanoCoreUsageFirst: 0,
expectedNanoCoreUsageSecond: 0,
},
} {
t.Run(desc, func(t *testing.T) {
container, err := containerstore.NewContainer(
containerstore.Metadata{ID: ID},
containerstore.Metadata{ID: test.id},
)
assert.NoError(t, err)
assert.Nil(t, container.Stats)
err = c.containerStore.Add(container)
assert.NoError(t, err)

cpuUsage, err := c.getUsageNanoCores(ID, false, test.firstCPUValue, timestamp)
cpuUsage, err := c.getUsageNanoCores(test.id, false, test.firstCPUValue, timestamp)
assert.NoError(t, err)

container, err = c.containerStore.Get(ID)
container, err = c.containerStore.Get(test.id)
assert.NoError(t, err)
assert.NotNil(t, container.Stats)

assert.Equal(t, test.expectedNanoCoreUsageFirst, cpuUsage)

cpuUsage, err = c.getUsageNanoCores(ID, false, test.secondCPUValue, secondAfterTimeStamp)
cpuUsage, err = c.getUsageNanoCores(test.id, false, test.secondCPUValue, tenSecondAftertimeStamp)
assert.NoError(t, err)
assert.Equal(t, test.expectedNanoCoreUsageSecond, cpuUsage)

container, err = c.containerStore.Get(ID)
container, err = c.containerStore.Get(test.id)
assert.NoError(t, err)
assert.NotNil(t, container.Stats)
})
}

}
Loading