Move StatsBuffer to be time-based.
It now keeps stats for a certain amount of time before expiring them. It used to keep a certain number of stats instead.
This commit is contained in:
parent
2ef063d404
commit
d9f8a0920c
@ -29,7 +29,7 @@ import (
|
|||||||
type containerStorage struct {
|
type containerStorage struct {
|
||||||
ref info.ContainerReference
|
ref info.ContainerReference
|
||||||
recentStats *StatsBuffer
|
recentStats *StatsBuffer
|
||||||
maxNumStats int
|
maxAge time.Duration
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,18 +48,18 @@ func (self *containerStorage) RecentStats(start, end time.Time, maxStats int) ([
|
|||||||
return self.recentStats.InTimeRange(start, end, maxStats), nil
|
return self.recentStats.InTimeRange(start, end, maxStats), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newContainerStore(ref info.ContainerReference, maxNumStats int) *containerStorage {
|
func newContainerStore(ref info.ContainerReference, maxAge time.Duration) *containerStorage {
|
||||||
return &containerStorage{
|
return &containerStorage{
|
||||||
ref: ref,
|
ref: ref,
|
||||||
recentStats: NewStatsBuffer(maxNumStats),
|
recentStats: NewStatsBuffer(maxAge),
|
||||||
maxNumStats: maxNumStats,
|
maxAge: maxAge,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type InMemoryStorage struct {
|
type InMemoryStorage struct {
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
containerStorageMap map[string]*containerStorage
|
containerStorageMap map[string]*containerStorage
|
||||||
maxNumStats int
|
maxAge time.Duration
|
||||||
backend storage.StorageDriver
|
backend storage.StorageDriver
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,7 +71,7 @@ func (self *InMemoryStorage) AddStats(ref info.ContainerReference, stats *info.C
|
|||||||
self.lock.Lock()
|
self.lock.Lock()
|
||||||
defer self.lock.Unlock()
|
defer self.lock.Unlock()
|
||||||
if cstore, ok = self.containerStorageMap[ref.Name]; !ok {
|
if cstore, ok = self.containerStorageMap[ref.Name]; !ok {
|
||||||
cstore = newContainerStore(ref, self.maxNumStats)
|
cstore = newContainerStore(ref, self.maxAge)
|
||||||
self.containerStorageMap[ref.Name] = cstore
|
self.containerStorageMap[ref.Name] = cstore
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@ -113,12 +113,12 @@ func (self *InMemoryStorage) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func New(
|
func New(
|
||||||
maxNumStats int,
|
maxAge time.Duration,
|
||||||
backend storage.StorageDriver,
|
backend storage.StorageDriver,
|
||||||
) *InMemoryStorage {
|
) *InMemoryStorage {
|
||||||
ret := &InMemoryStorage{
|
ret := &InMemoryStorage{
|
||||||
containerStorageMap: make(map[string]*containerStorage, 32),
|
containerStorageMap: make(map[string]*containerStorage, 32),
|
||||||
maxNumStats: maxNumStats,
|
maxAge: maxAge,
|
||||||
backend: backend,
|
backend: backend,
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
|
@ -47,7 +47,7 @@ func getRecentStats(t *testing.T, memoryStorage *InMemoryStorage, numStats int)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAddStats(t *testing.T) {
|
func TestAddStats(t *testing.T) {
|
||||||
memoryStorage := New(60, nil)
|
memoryStorage := New(60*time.Second, nil)
|
||||||
|
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
assert.Nil(memoryStorage.AddStats(containerRef, makeStat(0)))
|
assert.Nil(memoryStorage.AddStats(containerRef, makeStat(0)))
|
||||||
@ -70,7 +70,7 @@ func TestRecentStatsNoRecentStats(t *testing.T) {
|
|||||||
|
|
||||||
// Make an instance of InMemoryStorage with n stats.
|
// Make an instance of InMemoryStorage with n stats.
|
||||||
func makeWithStats(n int) *InMemoryStorage {
|
func makeWithStats(n int) *InMemoryStorage {
|
||||||
memoryStorage := New(60, nil)
|
memoryStorage := New(60*time.Second, nil)
|
||||||
|
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
memoryStorage.AddStats(containerRef, makeStat(i))
|
memoryStorage.AddStats(containerRef, makeStat(i))
|
||||||
|
@ -21,30 +21,33 @@ import (
|
|||||||
info "github.com/google/cadvisor/info/v1"
|
info "github.com/google/cadvisor/info/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A circular buffer for ContainerStats.
|
// A time-based buffer for ContainerStats. Holds information for a specific time period.
|
||||||
type StatsBuffer struct {
|
type StatsBuffer struct {
|
||||||
buffer []*info.ContainerStats
|
buffer []*info.ContainerStats
|
||||||
size int
|
age time.Duration
|
||||||
index int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a new thread-compatible StatsBuffer.
|
// Returns a new thread-compatible StatsBuffer.
|
||||||
func NewStatsBuffer(size int) *StatsBuffer {
|
func NewStatsBuffer(age time.Duration) *StatsBuffer {
|
||||||
return &StatsBuffer{
|
return &StatsBuffer{
|
||||||
buffer: make([]*info.ContainerStats, size),
|
buffer: make([]*info.ContainerStats, 0),
|
||||||
size: 0,
|
age: age,
|
||||||
index: size - 1,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 *StatsBuffer) Add(item *info.ContainerStats) {
|
||||||
if self.size < len(self.buffer) {
|
// Remove any elements before the eviction time.
|
||||||
self.size++
|
evictTime := item.Timestamp.Add(-self.age)
|
||||||
|
index := sort.Search(len(self.buffer), func(index int) bool {
|
||||||
|
return self.buffer[index].Timestamp.After(evictTime)
|
||||||
|
})
|
||||||
|
if index < len(self.buffer) {
|
||||||
|
self.buffer = self.buffer[index:]
|
||||||
}
|
}
|
||||||
self.index = (self.index + 1) % len(self.buffer)
|
|
||||||
copied := *item
|
copied := *item
|
||||||
self.buffer[self.index] = &copied
|
self.buffer = append(self.buffer, &copied)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns up to maxResult elements in the specified time period (inclusive).
|
// Returns up to maxResult elements in the specified time period (inclusive).
|
||||||
@ -52,7 +55,7 @@ func (self *StatsBuffer) Add(item *info.ContainerStats) {
|
|||||||
// 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 *StatsBuffer) InTimeRange(start, end time.Time, maxResults int) []*info.ContainerStats {
|
||||||
// No stats, return empty.
|
// No stats, return empty.
|
||||||
if self.size == 0 {
|
if len(self.buffer) == 0 {
|
||||||
return []*info.ContainerStats{}
|
return []*info.ContainerStats{}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,12 +70,12 @@ func (self *StatsBuffer) InTimeRange(start, end time.Time, maxResults int) []*in
|
|||||||
var startIndex int
|
var startIndex int
|
||||||
if start.IsZero() {
|
if start.IsZero() {
|
||||||
// None specified, start at the beginning.
|
// None specified, start at the beginning.
|
||||||
startIndex = self.size - 1
|
startIndex = len(self.buffer) - 1
|
||||||
} else {
|
} else {
|
||||||
// Start is the index before the elements smaller than it. We do this by
|
// Start is the index before the elements smaller than it. We do this by
|
||||||
// finding the first element smaller than start and taking the index
|
// finding the first element smaller than start and taking the index
|
||||||
// before that element
|
// before that element
|
||||||
startIndex = sort.Search(self.size, func(index int) bool {
|
startIndex = sort.Search(len(self.buffer), func(index int) bool {
|
||||||
// buffer[index] < start
|
// buffer[index] < start
|
||||||
return self.Get(index).Timestamp.Before(start)
|
return self.Get(index).Timestamp.Before(start)
|
||||||
}) - 1
|
}) - 1
|
||||||
@ -88,12 +91,12 @@ func (self *StatsBuffer) InTimeRange(start, end time.Time, maxResults int) []*in
|
|||||||
endIndex = 0
|
endIndex = 0
|
||||||
} else {
|
} else {
|
||||||
// End is the first index smaller than or equal to it (so, not larger).
|
// End is the first index smaller than or equal to it (so, not larger).
|
||||||
endIndex = sort.Search(self.size, func(index int) bool {
|
endIndex = sort.Search(len(self.buffer), func(index int) bool {
|
||||||
// buffer[index] <= t -> !(buffer[index] > t)
|
// buffer[index] <= t -> !(buffer[index] > t)
|
||||||
return !self.Get(index).Timestamp.After(end)
|
return !self.Get(index).Timestamp.After(end)
|
||||||
})
|
})
|
||||||
// Check if end is before all the data we have.
|
// Check if end is before all the data we have.
|
||||||
if endIndex == self.size {
|
if endIndex == len(self.buffer) {
|
||||||
return []*info.ContainerStats{}
|
return []*info.ContainerStats{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -113,15 +116,11 @@ func (self *StatsBuffer) InTimeRange(start, end time.Time, maxResults int) []*in
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets the element at the specified index. Note that elements are stored 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 *StatsBuffer) Get(index int) *info.ContainerStats {
|
||||||
calculatedIndex := self.index - index
|
return self.buffer[len(self.buffer)-index-1]
|
||||||
if calculatedIndex < 0 {
|
|
||||||
calculatedIndex += len(self.buffer)
|
|
||||||
}
|
|
||||||
return self.buffer[calculatedIndex]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *StatsBuffer) Size() int {
|
func (self *StatsBuffer) Size() int {
|
||||||
return self.size
|
return len(self.buffer)
|
||||||
}
|
}
|
||||||
|
@ -53,18 +53,22 @@ func expectAllElements(t *testing.T, sb *StatsBuffer, expected []int32) {
|
|||||||
expectElements(t, els, expected)
|
expectElements(t, els, expected)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getActualElements(actual []*info.ContainerStats) string {
|
||||||
|
actualElements := make([]string, len(actual))
|
||||||
|
for i, element := range actual {
|
||||||
|
actualElements[i] = strconv.Itoa(int(element.Cpu.LoadAverage))
|
||||||
|
}
|
||||||
|
return strings.Join(actualElements, ",")
|
||||||
|
}
|
||||||
|
|
||||||
func expectElements(t *testing.T, actual []*info.ContainerStats, expected []int32) {
|
func expectElements(t *testing.T, actual []*info.ContainerStats, expected []int32) {
|
||||||
if len(actual) != len(expected) {
|
if len(actual) != len(expected) {
|
||||||
t.Errorf("Expected elements %v, got %v", expected, actual)
|
t.Errorf("Expected elements %v, got %v", expected, getActualElements(actual))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for i, el := range actual {
|
for i, el := range actual {
|
||||||
if el.Cpu.LoadAverage != expected[i] {
|
if el.Cpu.LoadAverage != expected[i] {
|
||||||
actualElements := make([]string, len(actual))
|
t.Errorf("Expected elements %v, got %v", expected, getActualElements(actual))
|
||||||
for i, element := range actual {
|
|
||||||
actualElements[i] = strconv.Itoa(int(element.Cpu.LoadAverage))
|
|
||||||
}
|
|
||||||
t.Errorf("Expected elements %v, got %v", expected, strings.Join(actualElements, ","))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -77,12 +81,12 @@ func expectElement(t *testing.T, stat *info.ContainerStats, expected int32) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAdd(t *testing.T) {
|
func TestAdd(t *testing.T) {
|
||||||
sb := NewStatsBuffer(5)
|
sb := NewStatsBuffer(5 * time.Second)
|
||||||
|
|
||||||
// Add 1.
|
// Add 1.
|
||||||
sb.Add(createStats(1))
|
sb.Add(createStats(0))
|
||||||
expectSize(t, sb, 1)
|
expectSize(t, sb, 1)
|
||||||
expectAllElements(t, sb, []int32{1})
|
expectAllElements(t, sb, []int32{0})
|
||||||
|
|
||||||
// Fill the buffer.
|
// Fill the buffer.
|
||||||
for i := 1; i <= 5; i++ {
|
for i := 1; i <= 5; i++ {
|
||||||
@ -106,7 +110,7 @@ func TestAdd(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGet(t *testing.T) {
|
func TestGet(t *testing.T) {
|
||||||
sb := NewStatsBuffer(5)
|
sb := NewStatsBuffer(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))
|
||||||
@ -118,7 +122,7 @@ func TestGet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestInTimeRange(t *testing.T) {
|
func TestInTimeRange(t *testing.T) {
|
||||||
sb := NewStatsBuffer(5)
|
sb := NewStatsBuffer(5 * time.Second)
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
var empty time.Time
|
var empty time.Time
|
||||||
@ -195,7 +199,7 @@ func TestInTimeRange(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestInTimeRangeWithLimit(t *testing.T) {
|
func TestInTimeRangeWithLimit(t *testing.T) {
|
||||||
sb := NewStatsBuffer(5)
|
sb := NewStatsBuffer(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))
|
||||||
|
@ -92,6 +92,6 @@ func NewMemoryStorage(backendStorageName string) (*memory.InMemoryStorage, error
|
|||||||
glog.Infof("No backend storage selected")
|
glog.Infof("No backend storage selected")
|
||||||
}
|
}
|
||||||
glog.Infof("Caching %d stats in memory", statsToCache)
|
glog.Infof("Caching %d stats in memory", statsToCache)
|
||||||
storageDriver = memory.New(statsToCache, backendStorage)
|
storageDriver = memory.New(time.Duration(statsToCache)*time.Second, backendStorage)
|
||||||
return storageDriver, nil
|
return storageDriver, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user