Rename StatsBuffer to TimedStore in utils
This commit is contained in:
parent
a32015579e
commit
08186b6297
@ -22,13 +22,14 @@ import (
|
|||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
info "github.com/google/cadvisor/info/v1"
|
info "github.com/google/cadvisor/info/v1"
|
||||||
"github.com/google/cadvisor/storage"
|
"github.com/google/cadvisor/storage"
|
||||||
|
"github.com/google/cadvisor/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO(vmarmol): See about refactoring this class, we have an unecessary redirection of containerStorage and InMemoryStorage.
|
// TODO(vmarmol): See about refactoring this class, we have an unecessary redirection of containerStorage and InMemoryStorage.
|
||||||
// containerStorage is used to store per-container information
|
// containerStorage is used to store per-container information
|
||||||
type containerStorage struct {
|
type containerStorage struct {
|
||||||
ref info.ContainerReference
|
ref info.ContainerReference
|
||||||
recentStats *StatsBuffer
|
recentStats *utils.TimedStore
|
||||||
maxAge time.Duration
|
maxAge time.Duration
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
}
|
}
|
||||||
@ -51,7 +52,7 @@ func (self *containerStorage) RecentStats(start, end time.Time, maxStats int) ([
|
|||||||
func newContainerStore(ref info.ContainerReference, maxAge time.Duration) *containerStorage {
|
func newContainerStore(ref info.ContainerReference, maxAge time.Duration) *containerStorage {
|
||||||
return &containerStorage{
|
return &containerStorage{
|
||||||
ref: ref,
|
ref: ref,
|
||||||
recentStats: NewStatsBuffer(maxAge),
|
recentStats: utils.NewTimedStore(maxAge),
|
||||||
maxAge: maxAge,
|
maxAge: maxAge,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package memory
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sort"
|
"sort"
|
||||||
@ -22,21 +22,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// A time-based buffer for ContainerStats. Holds information for a specific time period.
|
// A time-based buffer for ContainerStats. Holds information for a specific time period.
|
||||||
type StatsBuffer struct {
|
type TimedStore struct {
|
||||||
buffer []*info.ContainerStats
|
buffer []*info.ContainerStats
|
||||||
age time.Duration
|
age time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a new thread-compatible StatsBuffer.
|
// Returns a new thread-compatible TimedStore.
|
||||||
func NewStatsBuffer(age time.Duration) *StatsBuffer {
|
func NewTimedStore(age time.Duration) *TimedStore {
|
||||||
return &StatsBuffer{
|
return &TimedStore{
|
||||||
buffer: make([]*info.ContainerStats, 0),
|
buffer: make([]*info.ContainerStats, 0),
|
||||||
age: age,
|
age: age,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adds an element to the start of the buffer (removing one from the end if necessary).
|
// Adds an element to the start of the buffer (removing one from the end if necessary).
|
||||||
func (self *StatsBuffer) Add(item *info.ContainerStats) {
|
func (self *TimedStore) Add(item *info.ContainerStats) {
|
||||||
// Remove any elements before the eviction time.
|
// Remove any elements before the eviction time.
|
||||||
evictTime := item.Timestamp.Add(-self.age)
|
evictTime := item.Timestamp.Add(-self.age)
|
||||||
index := sort.Search(len(self.buffer), func(index int) bool {
|
index := sort.Search(len(self.buffer), func(index int) bool {
|
||||||
@ -53,7 +53,7 @@ func (self *StatsBuffer) Add(item *info.ContainerStats) {
|
|||||||
// Returns up to maxResult elements in the specified time period (inclusive).
|
// Returns up to maxResult elements in the specified time period (inclusive).
|
||||||
// Results are from first to last. maxResults of -1 means no limit. When first
|
// Results are from first to last. maxResults of -1 means no limit. When first
|
||||||
// and last are specified, maxResults is ignored.
|
// and last are specified, maxResults is ignored.
|
||||||
func (self *StatsBuffer) InTimeRange(start, end time.Time, maxResults int) []*info.ContainerStats {
|
func (self *TimedStore) InTimeRange(start, end time.Time, maxResults int) []*info.ContainerStats {
|
||||||
// No stats, return empty.
|
// No stats, return empty.
|
||||||
if len(self.buffer) == 0 {
|
if len(self.buffer) == 0 {
|
||||||
return []*info.ContainerStats{}
|
return []*info.ContainerStats{}
|
||||||
@ -117,10 +117,10 @@ func (self *StatsBuffer) InTimeRange(start, end time.Time, maxResults int) []*in
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Gets the element at the specified index. Note that elements are output in LIFO order.
|
// Gets the element at the specified index. Note that elements are output in LIFO order.
|
||||||
func (self *StatsBuffer) Get(index int) *info.ContainerStats {
|
func (self *TimedStore) Get(index int) *info.ContainerStats {
|
||||||
return self.buffer[len(self.buffer)-index-1]
|
return self.buffer[len(self.buffer)-index-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StatsBuffer) Size() int {
|
func (self *TimedStore) Size() int {
|
||||||
return len(self.buffer)
|
return len(self.buffer)
|
||||||
}
|
}
|
@ -12,7 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package memory
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -38,13 +38,13 @@ func createStats(id int32) *info.ContainerStats {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func expectSize(t *testing.T, sb *StatsBuffer, expectedSize int) {
|
func expectSize(t *testing.T, sb *TimedStore, expectedSize int) {
|
||||||
if sb.Size() != expectedSize {
|
if sb.Size() != expectedSize {
|
||||||
t.Errorf("Expected size %v, got %v", expectedSize, sb.Size())
|
t.Errorf("Expected size %v, got %v", expectedSize, sb.Size())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func expectAllElements(t *testing.T, sb *StatsBuffer, expected []int32) {
|
func expectAllElements(t *testing.T, sb *TimedStore, expected []int32) {
|
||||||
size := sb.Size()
|
size := sb.Size()
|
||||||
els := make([]*info.ContainerStats, size)
|
els := make([]*info.ContainerStats, size)
|
||||||
for i := 0; i < size; i++ {
|
for i := 0; i < size; i++ {
|
||||||
@ -81,7 +81,7 @@ func expectElement(t *testing.T, stat *info.ContainerStats, expected int32) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAdd(t *testing.T) {
|
func TestAdd(t *testing.T) {
|
||||||
sb := NewStatsBuffer(5 * time.Second)
|
sb := NewTimedStore(5 * time.Second)
|
||||||
|
|
||||||
// Add 1.
|
// Add 1.
|
||||||
sb.Add(createStats(0))
|
sb.Add(createStats(0))
|
||||||
@ -110,7 +110,7 @@ func TestAdd(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGet(t *testing.T) {
|
func TestGet(t *testing.T) {
|
||||||
sb := NewStatsBuffer(5 * time.Second)
|
sb := NewTimedStore(5 * time.Second)
|
||||||
sb.Add(createStats(1))
|
sb.Add(createStats(1))
|
||||||
sb.Add(createStats(2))
|
sb.Add(createStats(2))
|
||||||
sb.Add(createStats(3))
|
sb.Add(createStats(3))
|
||||||
@ -122,7 +122,7 @@ func TestGet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestInTimeRange(t *testing.T) {
|
func TestInTimeRange(t *testing.T) {
|
||||||
sb := NewStatsBuffer(5 * time.Second)
|
sb := NewTimedStore(5 * time.Second)
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
var empty time.Time
|
var empty time.Time
|
||||||
@ -199,7 +199,7 @@ func TestInTimeRange(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestInTimeRangeWithLimit(t *testing.T) {
|
func TestInTimeRangeWithLimit(t *testing.T) {
|
||||||
sb := NewStatsBuffer(5 * time.Second)
|
sb := NewTimedStore(5 * time.Second)
|
||||||
sb.Add(createStats(1))
|
sb.Add(createStats(1))
|
||||||
sb.Add(createStats(2))
|
sb.Add(createStats(2))
|
||||||
sb.Add(createStats(3))
|
sb.Add(createStats(3))
|
Loading…
Reference in New Issue
Block a user