2015-07-29 18:12:01 +00:00
|
|
|
package scheduler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-10-15 00:22:52 +00:00
|
|
|
"sync"
|
2015-07-29 18:12:01 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/distribution/context"
|
2015-12-15 22:35:23 +00:00
|
|
|
"github.com/docker/distribution/reference"
|
2015-07-29 18:12:01 +00:00
|
|
|
"github.com/docker/distribution/registry/storage/driver"
|
|
|
|
)
|
|
|
|
|
2015-10-15 00:22:52 +00:00
|
|
|
// onTTLExpiryFunc is called when a repository's TTL expires
|
2016-01-27 00:42:10 +00:00
|
|
|
type expiryFunc func(reference.Reference) error
|
2015-07-29 18:12:01 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
entryTypeBlob = iota
|
|
|
|
entryTypeManifest
|
2015-10-23 22:25:42 +00:00
|
|
|
indexSaveFrequency = 5 * time.Second
|
2015-07-29 18:12:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// schedulerEntry represents an entry in the scheduler
|
|
|
|
// fields are exported for serialization
|
|
|
|
type schedulerEntry struct {
|
|
|
|
Key string `json:"Key"`
|
|
|
|
Expiry time.Time `json:"ExpiryData"`
|
|
|
|
EntryType int `json:"EntryType"`
|
2015-10-15 00:22:52 +00:00
|
|
|
|
|
|
|
timer *time.Timer
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new instance of the scheduler
|
|
|
|
func New(ctx context.Context, driver driver.StorageDriver, path string) *TTLExpirationScheduler {
|
|
|
|
return &TTLExpirationScheduler{
|
2015-10-15 00:22:52 +00:00
|
|
|
entries: make(map[string]*schedulerEntry),
|
2015-07-29 18:12:01 +00:00
|
|
|
driver: driver,
|
|
|
|
pathToStateFile: path,
|
|
|
|
ctx: ctx,
|
|
|
|
stopped: true,
|
2015-10-23 22:25:42 +00:00
|
|
|
doneChan: make(chan struct{}),
|
|
|
|
saveTimer: time.NewTicker(indexSaveFrequency),
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TTLExpirationScheduler is a scheduler used to perform actions
|
|
|
|
// when TTLs expire
|
|
|
|
type TTLExpirationScheduler struct {
|
2015-10-15 00:22:52 +00:00
|
|
|
sync.Mutex
|
|
|
|
|
|
|
|
entries map[string]*schedulerEntry
|
2015-07-29 18:12:01 +00:00
|
|
|
|
|
|
|
driver driver.StorageDriver
|
|
|
|
ctx context.Context
|
|
|
|
pathToStateFile string
|
|
|
|
|
|
|
|
stopped bool
|
|
|
|
|
|
|
|
onBlobExpire expiryFunc
|
|
|
|
onManifestExpire expiryFunc
|
2015-10-23 22:25:42 +00:00
|
|
|
|
|
|
|
indexDirty bool
|
|
|
|
saveTimer *time.Ticker
|
|
|
|
doneChan chan struct{}
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnBlobExpire is called when a scheduled blob's TTL expires
|
|
|
|
func (ttles *TTLExpirationScheduler) OnBlobExpire(f expiryFunc) {
|
2015-10-15 00:22:52 +00:00
|
|
|
ttles.Lock()
|
|
|
|
defer ttles.Unlock()
|
|
|
|
|
2015-07-29 18:12:01 +00:00
|
|
|
ttles.onBlobExpire = f
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnManifestExpire is called when a scheduled manifest's TTL expires
|
|
|
|
func (ttles *TTLExpirationScheduler) OnManifestExpire(f expiryFunc) {
|
2015-10-15 00:22:52 +00:00
|
|
|
ttles.Lock()
|
|
|
|
defer ttles.Unlock()
|
|
|
|
|
2015-07-29 18:12:01 +00:00
|
|
|
ttles.onManifestExpire = f
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddBlob schedules a blob cleanup after ttl expires
|
2016-01-27 00:42:10 +00:00
|
|
|
func (ttles *TTLExpirationScheduler) AddBlob(blobRef reference.Canonical, ttl time.Duration) error {
|
2015-10-15 00:22:52 +00:00
|
|
|
ttles.Lock()
|
|
|
|
defer ttles.Unlock()
|
|
|
|
|
2015-07-29 18:12:01 +00:00
|
|
|
if ttles.stopped {
|
|
|
|
return fmt.Errorf("scheduler not started")
|
|
|
|
}
|
2016-01-27 00:42:10 +00:00
|
|
|
|
|
|
|
ttles.add(blobRef, ttl, entryTypeBlob)
|
2015-07-29 18:12:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddManifest schedules a manifest cleanup after ttl expires
|
2016-01-27 00:42:10 +00:00
|
|
|
func (ttles *TTLExpirationScheduler) AddManifest(manifestRef reference.Canonical, ttl time.Duration) error {
|
2015-10-15 00:22:52 +00:00
|
|
|
ttles.Lock()
|
|
|
|
defer ttles.Unlock()
|
|
|
|
|
2015-07-29 18:12:01 +00:00
|
|
|
if ttles.stopped {
|
|
|
|
return fmt.Errorf("scheduler not started")
|
|
|
|
}
|
|
|
|
|
2016-01-27 00:42:10 +00:00
|
|
|
ttles.add(manifestRef, ttl, entryTypeManifest)
|
2015-07-29 18:12:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the scheduler
|
|
|
|
func (ttles *TTLExpirationScheduler) Start() error {
|
2015-10-15 00:22:52 +00:00
|
|
|
ttles.Lock()
|
|
|
|
defer ttles.Unlock()
|
2015-07-29 18:12:01 +00:00
|
|
|
|
|
|
|
err := ttles.readState()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ttles.stopped {
|
|
|
|
return fmt.Errorf("Scheduler already started")
|
|
|
|
}
|
|
|
|
|
|
|
|
context.GetLogger(ttles.ctx).Infof("Starting cached object TTL expiration scheduler...")
|
|
|
|
ttles.stopped = false
|
2015-10-15 00:22:52 +00:00
|
|
|
|
|
|
|
// Start timer for each deserialized entry
|
|
|
|
for _, entry := range ttles.entries {
|
|
|
|
entry.timer = ttles.startTimer(entry, entry.Expiry.Sub(time.Now()))
|
|
|
|
}
|
2015-07-29 18:12:01 +00:00
|
|
|
|
2015-10-23 22:25:42 +00:00
|
|
|
// Start a ticker to periodically save the entries index
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ttles.saveTimer.C:
|
|
|
|
if !ttles.indexDirty {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ttles.Lock()
|
|
|
|
err := ttles.writeState()
|
|
|
|
if err != nil {
|
|
|
|
context.GetLogger(ttles.ctx).Errorf("Error writing scheduler state: %s", err)
|
|
|
|
} else {
|
|
|
|
ttles.indexDirty = false
|
|
|
|
}
|
|
|
|
ttles.Unlock()
|
|
|
|
|
|
|
|
case <-ttles.doneChan:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-07-29 18:12:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-27 00:42:10 +00:00
|
|
|
func (ttles *TTLExpirationScheduler) add(r reference.Reference, ttl time.Duration, eType int) {
|
2015-10-15 00:22:52 +00:00
|
|
|
entry := &schedulerEntry{
|
2016-01-27 00:42:10 +00:00
|
|
|
Key: r.String(),
|
2015-10-15 00:22:52 +00:00
|
|
|
Expiry: time.Now().Add(ttl),
|
|
|
|
EntryType: eType,
|
|
|
|
}
|
|
|
|
context.GetLogger(ttles.ctx).Infof("Adding new scheduler entry for %s with ttl=%s", entry.Key, entry.Expiry.Sub(time.Now()))
|
2016-01-27 00:42:10 +00:00
|
|
|
if oldEntry, present := ttles.entries[entry.Key]; present && oldEntry.timer != nil {
|
2015-10-15 00:22:52 +00:00
|
|
|
oldEntry.timer.Stop()
|
|
|
|
}
|
2016-01-27 00:42:10 +00:00
|
|
|
ttles.entries[entry.Key] = entry
|
2015-10-15 00:22:52 +00:00
|
|
|
entry.timer = ttles.startTimer(entry, ttl)
|
2015-10-23 22:25:42 +00:00
|
|
|
ttles.indexDirty = true
|
2015-10-15 00:22:52 +00:00
|
|
|
}
|
2015-07-29 18:12:01 +00:00
|
|
|
|
2015-10-15 00:22:52 +00:00
|
|
|
func (ttles *TTLExpirationScheduler) startTimer(entry *schedulerEntry, ttl time.Duration) *time.Timer {
|
|
|
|
return time.AfterFunc(ttl, func() {
|
|
|
|
ttles.Lock()
|
|
|
|
defer ttles.Unlock()
|
2015-07-29 18:12:01 +00:00
|
|
|
|
2015-10-15 00:22:52 +00:00
|
|
|
var f expiryFunc
|
2015-07-29 18:12:01 +00:00
|
|
|
|
2015-10-15 00:22:52 +00:00
|
|
|
switch entry.EntryType {
|
|
|
|
case entryTypeBlob:
|
|
|
|
f = ttles.onBlobExpire
|
|
|
|
case entryTypeManifest:
|
|
|
|
f = ttles.onManifestExpire
|
|
|
|
default:
|
2016-01-27 00:42:10 +00:00
|
|
|
f = func(reference.Reference) error {
|
|
|
|
return fmt.Errorf("scheduler entry type")
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
2015-10-15 00:22:52 +00:00
|
|
|
}
|
2015-07-29 18:12:01 +00:00
|
|
|
|
2016-01-27 00:42:10 +00:00
|
|
|
ref, err := reference.Parse(entry.Key)
|
|
|
|
if err == nil {
|
|
|
|
if err := f(ref); err != nil {
|
|
|
|
context.GetLogger(ttles.ctx).Errorf("Scheduler error returned from OnExpire(%s): %s", entry.Key, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
context.GetLogger(ttles.ctx).Errorf("Error unpacking reference: %s", err)
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 00:22:52 +00:00
|
|
|
delete(ttles.entries, entry.Key)
|
2015-10-23 22:25:42 +00:00
|
|
|
ttles.indexDirty = true
|
2015-10-15 00:22:52 +00:00
|
|
|
})
|
|
|
|
}
|
2015-07-29 18:12:01 +00:00
|
|
|
|
2015-10-15 00:22:52 +00:00
|
|
|
// Stop stops the scheduler.
|
|
|
|
func (ttles *TTLExpirationScheduler) Stop() {
|
|
|
|
ttles.Lock()
|
|
|
|
defer ttles.Unlock()
|
2015-07-29 18:12:01 +00:00
|
|
|
|
2015-10-15 00:22:52 +00:00
|
|
|
if err := ttles.writeState(); err != nil {
|
|
|
|
context.GetLogger(ttles.ctx).Errorf("Error writing scheduler state: %s", err)
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 00:22:52 +00:00
|
|
|
for _, entry := range ttles.entries {
|
|
|
|
entry.timer.Stop()
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
2015-10-23 22:25:42 +00:00
|
|
|
|
|
|
|
close(ttles.doneChan)
|
|
|
|
ttles.saveTimer.Stop()
|
2015-10-15 00:22:52 +00:00
|
|
|
ttles.stopped = true
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ttles *TTLExpirationScheduler) writeState() error {
|
|
|
|
jsonBytes, err := json.Marshal(ttles.entries)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ttles.driver.PutContent(ttles.ctx, ttles.pathToStateFile, jsonBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-23 22:25:42 +00:00
|
|
|
|
2015-07-29 18:12:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ttles *TTLExpirationScheduler) readState() error {
|
|
|
|
if _, err := ttles.driver.Stat(ttles.ctx, ttles.pathToStateFile); err != nil {
|
|
|
|
switch err := err.(type) {
|
|
|
|
case driver.PathNotFoundError:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err := ttles.driver.GetContent(ttles.ctx, ttles.pathToStateFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(bytes, &ttles.entries)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|