Update gc policy configuration to use time duration

Signed-off-by: Derek McGowan <derek@mcgstyle.net>
This commit is contained in:
Derek McGowan 2017-11-20 12:50:45 -08:00
parent 3f1a61f76a
commit 374f04d0e9
No known key found for this signature in database
GPG Key ID: F58C5D0A4405ACDB
2 changed files with 38 additions and 26 deletions

View File

@ -12,8 +12,8 @@ import (
"github.com/containerd/containerd/plugin" "github.com/containerd/containerd/plugin"
) )
// Config configures the garbage collection policies. // config configures the garbage collection policies.
type Config struct { type config struct {
// PauseThreshold represents the maximum amount of time garbage // PauseThreshold represents the maximum amount of time garbage
// collection should be scheduled based on the average pause time. // collection should be scheduled based on the average pause time.
// For example, a value of 0.02 means that scheduled garbage collection // For example, a value of 0.02 means that scheduled garbage collection
@ -46,21 +46,33 @@ type Config struct {
// Default 100 // Default 100
MutationThreshold int `toml:"mutation_threshold"` MutationThreshold int `toml:"mutation_threshold"`
// ScheduleDelayMs is the number of milliseconds in the future to // ScheduleDelay is the duration in the future to schedule a garbage
// schedule a garbage collection triggered manually or by exceeding // collection triggered manually or by exceeding the configured
// the configured threshold for deletion or mutation. A zero value // threshold for deletion or mutation. A zero value will immediately
// will immediately schedule. // schedule. Use suffix "ms" for millisecond and "s" for second.
// //
// Default is 0 // Default is "0ms"
ScheduleDelayMs int `toml:"schedule_delay_ms"` ScheduleDelay duration `toml:"schedule_delay"`
// StartupDelayMs is the number of milliseconds to do an initial // StartupDelay is the delay duration to do an initial garbage
// garbage collection after startup. The initial garbage collection // collection after startup. The initial garbage collection is used to
// is used to set the base for pause threshold and should be scheduled // set the base for pause threshold and should be scheduled in the
// in the future to avoid slowing down other startup processes. // future to avoid slowing down other startup processes. Use suffix
// "ms" for millisecond and "s" for second.
// //
// Default is 100 // Default is "100ms"
StartupDelayMs int `toml:"startup_delay_ms"` StartupDelay duration `toml:"startup_delay"`
}
type duration time.Duration
func (d *duration) UnmarshalText(text []byte) error {
ed, err := time.ParseDuration(string(text))
if err != nil {
return err
}
*d = duration(ed)
return nil
} }
func init() { func init() {
@ -70,12 +82,12 @@ func init() {
Requires: []plugin.Type{ Requires: []plugin.Type{
plugin.MetadataPlugin, plugin.MetadataPlugin,
}, },
Config: &Config{ Config: &config{
PauseThreshold: 0.02, PauseThreshold: 0.02,
DeletionThreshold: 0, DeletionThreshold: 0,
MutationThreshold: 100, MutationThreshold: 100,
ScheduleDelayMs: 0, ScheduleDelay: duration(0),
StartupDelayMs: 100, StartupDelay: duration(100 * time.Millisecond),
}, },
InitFn: func(ic *plugin.InitContext) (interface{}, error) { InitFn: func(ic *plugin.InitContext) (interface{}, error) {
md, err := ic.Get(plugin.MetadataPlugin) md, err := ic.Get(plugin.MetadataPlugin)
@ -83,7 +95,7 @@ func init() {
return nil, err return nil, err
} }
m := newScheduler(md.(*metadata.DB), ic.Config.(*Config)) m := newScheduler(md.(*metadata.DB), ic.Config.(*config))
ic.Meta.Exports = map[string]string{ ic.Meta.Exports = map[string]string{
"PauseThreshold": fmt.Sprint(m.pauseThreshold), "PauseThreshold": fmt.Sprint(m.pauseThreshold),
@ -125,7 +137,7 @@ type gcScheduler struct {
startupDelay time.Duration startupDelay time.Duration
} }
func newScheduler(c collector, cfg *Config) *gcScheduler { func newScheduler(c collector, cfg *config) *gcScheduler {
eventC := make(chan mutationEvent) eventC := make(chan mutationEvent)
s := &gcScheduler{ s := &gcScheduler{
@ -134,8 +146,8 @@ func newScheduler(c collector, cfg *Config) *gcScheduler {
pauseThreshold: cfg.PauseThreshold, pauseThreshold: cfg.PauseThreshold,
deletionThreshold: cfg.DeletionThreshold, deletionThreshold: cfg.DeletionThreshold,
mutationThreshold: cfg.MutationThreshold, mutationThreshold: cfg.MutationThreshold,
scheduleDelay: time.Duration(cfg.ScheduleDelayMs) * time.Millisecond, scheduleDelay: time.Duration(cfg.ScheduleDelay),
startupDelay: time.Duration(cfg.StartupDelayMs) * time.Millisecond, startupDelay: time.Duration(cfg.StartupDelay),
} }
if s.pauseThreshold < 0.0 { if s.pauseThreshold < 0.0 {

View File

@ -10,7 +10,7 @@ import (
) )
func TestPauseThreshold(t *testing.T) { func TestPauseThreshold(t *testing.T) {
cfg := &Config{ cfg := &config{
// With 100μs, gc should run about every 5ms // With 100μs, gc should run about every 5ms
PauseThreshold: 0.02, PauseThreshold: 0.02,
} }
@ -45,7 +45,7 @@ func TestPauseThreshold(t *testing.T) {
} }
func TestDeletionThreshold(t *testing.T) { func TestDeletionThreshold(t *testing.T) {
cfg := &Config{ cfg := &config{
// Prevent GC from scheduling again before check // Prevent GC from scheduling again before check
PauseThreshold: 0.001, PauseThreshold: 0.001,
DeletionThreshold: 5, DeletionThreshold: 5,
@ -91,7 +91,7 @@ func TestDeletionThreshold(t *testing.T) {
func TestTrigger(t *testing.T) { func TestTrigger(t *testing.T) {
var ( var (
cfg = &Config{} cfg = &config{}
tc = &testCollector{ tc = &testCollector{
d: time.Millisecond * 10, d: time.Millisecond * 10,
} }
@ -132,10 +132,10 @@ func TestTrigger(t *testing.T) {
func TestStartupDelay(t *testing.T) { func TestStartupDelay(t *testing.T) {
var ( var (
cfg = &Config{ cfg = &config{
// Prevent GC from scheduling again before check // Prevent GC from scheduling again before check
PauseThreshold: 0.001, PauseThreshold: 0.001,
StartupDelayMs: 1, StartupDelay: duration(time.Millisecond),
} }
tc = &testCollector{ tc = &testCollector{
d: time.Second, d: time.Second,