From 1a24eed18ffe7254ce0847335c12fdf8600eac02 Mon Sep 17 00:00:00 2001 From: Vishnu Kannan Date: Wed, 20 Aug 2014 19:58:27 +0000 Subject: [PATCH] Fix storage driver buffer duration default value. The cache will now hold atlest the minimum number of stats required by the UI and more if the buffer duration is longer than the default. Docker-DCO-1.1-Signed-off-by: Vishnu Kannan (github: vishh) --- manager/container.go | 5 ++++- pages/static/containers_js.go | 1 + storagedriver.go | 14 ++++++++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/manager/container.go b/manager/container.go index 228ff8eb..19dab0c9 100644 --- a/manager/container.go +++ b/manager/container.go @@ -27,6 +27,9 @@ import ( "github.com/google/cadvisor/storage" ) +// Housekeeping duration +const HousekeepingTick = 1 * time.Second + // Internal mirror of the external data structure. type containerStat struct { Timestamp time.Time @@ -105,7 +108,7 @@ func NewContainerData(containerName string, driver storage.StorageDriver) (*cont func (c *containerData) housekeeping() { // Housekeep every second. - ticker := time.NewTicker(1 * time.Second) + ticker := time.NewTicker(HousekeepingTick) defer ticker.Stop() for { select { diff --git a/pages/static/containers_js.go b/pages/static/containers_js.go index 0858d5e1..e01aadb0 100644 --- a/pages/static/containers_js.go +++ b/pages/static/containers_js.go @@ -92,6 +92,7 @@ function getMachineInfo(callback) { function getStats(containerName, callback) { // Request 60s of container history and no samples. var request = JSON.stringify({ + // Update main.statsRequestedByUI while updating "num_stats" here. "num_stats": 60, "num_samples": 0 }); diff --git a/storagedriver.go b/storagedriver.go index d9dd9411..72fb362a 100644 --- a/storagedriver.go +++ b/storagedriver.go @@ -20,6 +20,8 @@ import ( "os" "time" + "github.com/golang/glog" + "github.com/google/cadvisor/manager" "github.com/google/cadvisor/storage" "github.com/google/cadvisor/storage/cache" "github.com/google/cadvisor/storage/influxdb" @@ -32,7 +34,9 @@ var argDbPassword = flag.String("storage_driver_password", "root", "database pas var argDbHost = flag.String("storage_driver_host", "localhost:8086", "database host:port") var argDbName = flag.String("storage_driver_db", "cadvisor", "database name") var argDbIsSecure = flag.Bool("storage_driver_secure", false, "use secure connection with database") -var argDbBufferDuration = flag.Duration("storage_driver_buffer_duration", 60, "Writes in the storage driver will be bufferd for this duration (in seconds), and committed to the non memory backends as a single transaction") +var argDbBufferDuration = flag.Duration("storage_driver_buffer_duration", 60*time.Second, "Writes in the storage driver will be bufferd for this duration, and committed to the non memory backends as a single transaction") + +const statsRequestedByUI = 60 func NewStorageDriver(driverName string) (storage.StorageDriver, error) { var storageDriver storage.StorageDriver @@ -63,7 +67,13 @@ func NewStorageDriver(driverName string) (storage.StorageDriver, error) { // TODO(monnand): One hour? Or user-defined? 1*time.Hour, ) - storageDriver = cache.MemoryCache(int(*argDbBufferDuration), int(*argDbBufferDuration), storageDriver) + samplesToCache := int(*argDbBufferDuration / manager.HousekeepingTick) + if samplesToCache < statsRequestedByUI { + // The UI requests the most recent 60 stats by default. + samplesToCache = statsRequestedByUI + } + glog.V(2).Infof("Caching %d recent stats in memory\n", samplesToCache) + storageDriver = cache.MemoryCache(samplesToCache, samplesToCache, storageDriver) default: err = fmt.Errorf("Unknown database driver: %v", *argDbDriver) }