From 8c4a1ef81d1011ac44780f80d3c395e30180f624 Mon Sep 17 00:00:00 2001 From: Nan Monnand Deng Date: Fri, 11 Jul 2014 17:21:28 -0400 Subject: [PATCH] Requires storage driver to return stats in time increasing order. Change influxdb driver to return stats in correct order. --- storage/influxdb/influxdb.go | 15 +++++++++++---- storage/storage.go | 4 +++- storage/test/storagetests.go | 24 ++++++++---------------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/storage/influxdb/influxdb.go b/storage/influxdb/influxdb.go index 1fcb170f..99cb92c7 100644 --- a/storage/influxdb/influxdb.go +++ b/storage/influxdb/influxdb.go @@ -331,8 +331,13 @@ func (self *influxdbStorage) RecentStats(containerName string, numStats int) ([] return nil, err } statsList := make([]*info.ContainerStats, 0, len(series)) - for _, s := range series { - for _, values := range s.Points { + // By default, influxDB returns data in time descending order. + // RecentStats() requires stats in time increasing order, + // so we need to go through from the last one to the first one. + for i := len(series) - 1; i >= 0; i-- { + s := series[i] + for j := len(s.Points) - 1; j >= 0; j-- { + values := s.Points[j] stats, err := self.valuesToContainerStats(s.Columns, values) if err != nil { return nil, err @@ -358,8 +363,10 @@ func (self *influxdbStorage) Samples(containerName string, numSamples int) ([]*i return nil, err } sampleList := make([]*info.ContainerStatsSample, 0, len(series)) - for _, s := range series { - for _, values := range s.Points { + for i := len(series) - 1; i >= 0; i-- { + s := series[i] + for j := len(s.Points) - 1; j >= 0; j-- { + values := s.Points[j] sample, err := self.valuesToContainerSample(s.Columns, values) if err != nil { return nil, err diff --git a/storage/storage.go b/storage/storage.go index 8b264a93..84ca0b3b 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -21,7 +21,9 @@ type StorageDriver interface { // Read most recent stats. numStats indicates max number of stats // returned. The returned stats must be consecutive observed stats. If - // numStats < 0, then return all stats stored in the storage. + // numStats < 0, then return all stats stored in the storage. The + // returned stats should be sorted in time increasing order, i.e. Most + // recent stats should be the last. RecentStats(containerName string, numStats int) ([]*info.ContainerStats, error) // Read the specified percentiles of CPU and memory usage of the container. diff --git a/storage/test/storagetests.go b/storage/test/storagetests.go index 99912032..8bad2c4d 100644 --- a/storage/test/storagetests.go +++ b/storage/test/storagetests.go @@ -392,14 +392,10 @@ func StorageDriverTestRetrievePartialRecentStats(driver storage.StorageDriver, t actualRecentStats := trace[len(trace)-len(recentStats):] - for _, r := range recentStats { - found := false - for _, s := range actualRecentStats { - if statsEq(s, r) { - found = true - } - } - if !found { + // The returned stats should be sorted in time increasing order + for i, s := range actualRecentStats { + r := recentStats[i] + if !statsEq(s, r) { t.Errorf("unexpected stats %+v with memory usage %v", r, r.Memory.Usage) } } @@ -438,14 +434,10 @@ func StorageDriverTestRetrieveAllRecentStats(driver storage.StorageDriver, t *te actualRecentStats := trace[len(trace)-len(recentStats):] - for _, r := range recentStats { - found := false - for _, s := range actualRecentStats { - if statsEq(s, r) { - found = true - } - } - if !found { + // The returned stats should be sorted in time increasing order + for i, s := range actualRecentStats { + r := recentStats[i] + if !statsEq(s, r) { t.Errorf("unexpected stats %+v with memory usage %v", r, r.Memory.Usage) } }