Make the duration of housekeeping flag configurable.

This commit is contained in:
Victor Marmol 2014-08-30 19:57:53 -07:00
parent cf725b2173
commit 3d10fc747c
3 changed files with 34 additions and 14 deletions

View File

@ -17,6 +17,7 @@
package manager package manager
import ( import (
"flag"
"fmt" "fmt"
"sync" "sync"
"time" "time"
@ -27,8 +28,8 @@ import (
"github.com/google/cadvisor/storage" "github.com/google/cadvisor/storage"
) )
// Housekeeping duration // Housekeeping interval.
const HousekeepingTick = 1 * time.Second var HousekeepingInterval = flag.Duration("housekeeping_interval", 1*time.Second, "Interval between container housekeepings")
// Internal mirror of the external data structure. // Internal mirror of the external data structure.
type containerStat struct { type containerStat struct {
@ -107,8 +108,14 @@ func NewContainerData(containerName string, driver storage.StorageDriver) (*cont
} }
func (c *containerData) housekeeping() { func (c *containerData) housekeeping() {
// Long housekeeping is either 100ms or half of the housekeeping interval.
longHousekeeping := 100 * time.Millisecond
if *HousekeepingInterval/2 < longHousekeeping {
longHousekeeping = *HousekeepingInterval / 2
}
// Housekeep every second. // Housekeep every second.
ticker := time.NewTicker(HousekeepingTick) ticker := time.NewTicker(*HousekeepingInterval)
defer ticker.Stop() defer ticker.Stop()
for { for {
select { select {
@ -119,10 +126,10 @@ func (c *containerData) housekeeping() {
start := time.Now() start := time.Now()
c.housekeepingTick() c.housekeepingTick()
// Log if housekeeping took longer than 120ms. // Log if housekeeping took too long.
duration := time.Since(start) duration := time.Since(start)
if duration >= 120*time.Millisecond { if duration >= longHousekeeping {
glog.V(2).Infof("Housekeeping(%s) took %s", c.info.Name, duration) glog.V(1).Infof("Housekeeping(%s) took %s", c.info.Name, duration)
} }
} }
} }

View File

@ -15,6 +15,7 @@
package manager package manager
import ( import (
"flag"
"fmt" "fmt"
"path" "path"
"strings" "strings"
@ -27,6 +28,8 @@ import (
"github.com/google/cadvisor/storage" "github.com/google/cadvisor/storage"
) )
var globalHousekeepingInterval = flag.Duration("global_housekeeping_interval", 1*time.Second, "Interval between global housekeepings")
type Manager interface { type Manager interface {
// Start the manager, blocks forever. // Start the manager, blocks forever.
Start() error Start() error
@ -70,11 +73,13 @@ func New(driver storage.StorageDriver) (Manager, error) {
} }
type manager struct { type manager struct {
containers map[string]*containerData containers map[string]*containerData
containersLock sync.RWMutex containersLock sync.RWMutex
storageDriver storage.StorageDriver storageDriver storage.StorageDriver
machineInfo info.MachineInfo machineInfo info.MachineInfo
versionInfo info.VersionInfo versionInfo info.VersionInfo
globalHousekeepingInterval time.Duration
containerHousekeepingInterval time.Duration
} }
// Start the container manager. // Start the container manager.
@ -91,8 +96,15 @@ func (m *manager) Start() error {
} }
glog.Infof("Recovery completed") glog.Infof("Recovery completed")
// Long housekeeping is either 100ms or half of the housekeeping interval.
longHousekeeping := 100 * time.Millisecond
if *globalHousekeepingInterval/2 < longHousekeeping {
longHousekeeping = *globalHousekeepingInterval / 2
}
// Look for new containers in the main housekeeping thread. // Look for new containers in the main housekeeping thread.
for t := range time.Tick(time.Second) { ticker := time.Tick(*globalHousekeepingInterval)
for t := range ticker {
start := time.Now() start := time.Now()
// Check for new containers. // Check for new containers.
@ -103,7 +115,7 @@ func (m *manager) Start() error {
// Log if housekeeping took more than 100ms. // Log if housekeeping took more than 100ms.
duration := time.Since(start) duration := time.Since(start)
if duration >= 100*time.Millisecond { if duration >= longHousekeeping {
glog.V(1).Infof("Global Housekeeping(%d) took %s", t.Unix(), duration) glog.V(1).Infof("Global Housekeeping(%d) took %s", t.Unix(), duration)
} }
} }

View File

@ -42,7 +42,8 @@ const statsRequestedByUI = 60
func NewStorageDriver(driverName string) (storage.StorageDriver, error) { func NewStorageDriver(driverName string) (storage.StorageDriver, error) {
var storageDriver storage.StorageDriver var storageDriver storage.StorageDriver
var err error var err error
samplesToCache := int(*argDbBufferDuration / manager.HousekeepingTick) // TODO(vmarmol): We shouldn't need the housekeeping interval here and it shouldn't be public.
samplesToCache := int(*argDbBufferDuration / *manager.HousekeepingInterval)
if samplesToCache < statsRequestedByUI { if samplesToCache < statsRequestedByUI {
// The UI requests the most recent 60 stats by default. // The UI requests the most recent 60 stats by default.
samplesToCache = statsRequestedByUI samplesToCache = statsRequestedByUI