diff --git a/cache/memory/memory.go b/cache/memory/memory.go index a43e1b442e0..0885fcbd231 100644 --- a/cache/memory/memory.go +++ b/cache/memory/memory.go @@ -129,6 +129,10 @@ func (c *InMemoryCache) RemoveContainer(containerName string) error { return nil } +func (c *InMemoryCache) GetMaxAge() time.Duration { + return c.maxAge +} + func New( maxAge time.Duration, backend []storage.StorageDriver, diff --git a/manager/container.go b/manager/container.go index cf868953c9a..a268f61b5ec 100644 --- a/manager/container.go +++ b/manager/container.go @@ -47,7 +47,9 @@ import ( // Housekeeping interval. var enableLoadReader = flag.Bool("enable_load_reader", false, "Whether to enable cpu load reader") -var HousekeepingInterval = flag.Duration("housekeeping_interval", 1*time.Second, "Interval between container housekeepings") +var HousekeepingInterval = flag.Duration("housekeeping_interval", 1*time.Second, "Interval between container housekeepings."+ + " It should be less than 'max_housekeeping_interval',"+ + " and it must be less than memory's maxAge('storage_duration'), which is set to 2 minutes by default.") // TODO: replace regular expressions with something simpler, such as strings.Split(). // cgroup type chosen to fetch the cgroup path of a process. @@ -496,6 +498,22 @@ func (cd *containerData) nextHousekeepingInterval() time.Duration { // Lower interval back to the baseline. cd.housekeepingInterval = *HousekeepingInterval } + } else if len(stats) < 2 { + // Lower the interval if less than two stats + + // If set housekeepingInterval and maxHousekeepingInterval to large values, both greater than memoryCache.maxAge. + if cd.housekeepingInterval >= cd.memoryCache.GetMaxAge() && + cd.maxHousekeepingInterval >= cd.memoryCache.GetMaxAge() && + *HousekeepingInterval >= cd.memoryCache.GetMaxAge() { + const defaultHousekeepingInterval = 10 * time.Second + cd.housekeepingInterval = cd.memoryCache.GetMaxAge() - defaultHousekeepingInterval + } else if cd.housekeepingInterval > cd.maxHousekeepingInterval { + // Due to the lack of enforced validation, incorrect settings may occur. + cd.housekeepingInterval = cd.maxHousekeepingInterval + } else { + // Lower interval back to the baseline. + cd.housekeepingInterval = *HousekeepingInterval + } } }