2018-03-22 17:11:03 -04:00
|
|
|
package service // import "github.com/docker/docker/volume/service"
|
2016-12-01 17:17:07 -05:00
|
|
|
|
|
|
|
import (
|
2018-03-22 17:11:03 -04:00
|
|
|
"context"
|
2016-12-01 17:17:07 -05:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/docker/docker/volume"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-09-11 18:12:55 -04:00
|
|
|
bolt "go.etcd.io/bbolt"
|
2016-12-01 17:17:07 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// restore is called when a new volume store is created.
|
|
|
|
// It's primary purpose is to ensure that all drivers' refcounts are set based
|
|
|
|
// on known volumes after a restart.
|
|
|
|
// This only attempts to track volumes that are actually stored in the on-disk db.
|
|
|
|
// It does not probe the available drivers to find anything that may have been added
|
|
|
|
// out of band.
|
|
|
|
func (s *VolumeStore) restore() {
|
2016-12-22 11:49:47 -05:00
|
|
|
var ls []volumeMetadata
|
2016-12-01 17:17:07 -05:00
|
|
|
s.db.View(func(tx *bolt.Tx) error {
|
2016-12-22 11:49:47 -05:00
|
|
|
ls = listMeta(tx)
|
2016-12-01 17:17:07 -05:00
|
|
|
return nil
|
|
|
|
})
|
2018-03-22 17:11:03 -04:00
|
|
|
ctx := context.Background()
|
2016-12-01 17:17:07 -05:00
|
|
|
|
2016-12-22 11:49:47 -05:00
|
|
|
chRemove := make(chan *volumeMetadata, len(ls))
|
2016-12-01 17:17:07 -05:00
|
|
|
var wg sync.WaitGroup
|
2016-12-22 11:49:47 -05:00
|
|
|
for _, meta := range ls {
|
2016-12-01 17:17:07 -05:00
|
|
|
wg.Add(1)
|
|
|
|
// this is potentially a very slow operation, so do it in a goroutine
|
2016-12-22 11:49:47 -05:00
|
|
|
go func(meta volumeMetadata) {
|
2016-12-01 17:17:07 -05:00
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
var v volume.Volume
|
|
|
|
var err error
|
|
|
|
if meta.Driver != "" {
|
2018-03-22 17:11:03 -04:00
|
|
|
v, err = lookupVolume(ctx, s.drivers, meta.Driver, meta.Name)
|
2016-12-01 17:17:07 -05:00
|
|
|
if err != nil && err != errNoSuchVolume {
|
2016-12-22 11:49:47 -05:00
|
|
|
logrus.WithError(err).WithField("driver", meta.Driver).WithField("volume", meta.Name).Warn("Error restoring volume")
|
2016-12-01 17:17:07 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if v == nil {
|
|
|
|
// doesn't exist in the driver, remove it from the db
|
2016-12-22 11:49:47 -05:00
|
|
|
chRemove <- &meta
|
2016-12-01 17:17:07 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2018-03-22 17:11:03 -04:00
|
|
|
v, err = s.getVolume(ctx, meta.Name, meta.Driver)
|
2016-12-01 17:17:07 -05:00
|
|
|
if err != nil {
|
|
|
|
if err == errNoSuchVolume {
|
2016-12-22 11:49:47 -05:00
|
|
|
chRemove <- &meta
|
2016-12-01 17:17:07 -05:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
meta.Driver = v.DriverName()
|
|
|
|
if err := s.setMeta(v.Name(), meta); err != nil {
|
|
|
|
logrus.WithError(err).WithField("driver", meta.Driver).WithField("volume", v.Name()).Warn("Error updating volume metadata on restore")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// increment driver refcount
|
2018-03-19 17:18:52 -04:00
|
|
|
s.drivers.CreateDriver(meta.Driver)
|
2016-12-01 17:17:07 -05:00
|
|
|
|
|
|
|
// cache the volume
|
|
|
|
s.globalLock.Lock()
|
|
|
|
s.options[v.Name()] = meta.Options
|
|
|
|
s.labels[v.Name()] = meta.Labels
|
|
|
|
s.names[v.Name()] = v
|
2018-03-22 17:11:03 -04:00
|
|
|
s.refs[v.Name()] = make(map[string]struct{})
|
2016-12-01 17:17:07 -05:00
|
|
|
s.globalLock.Unlock()
|
2016-12-22 11:49:47 -05:00
|
|
|
}(meta)
|
2016-12-01 17:17:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
close(chRemove)
|
|
|
|
s.db.Update(func(tx *bolt.Tx) error {
|
2016-12-22 11:49:47 -05:00
|
|
|
for meta := range chRemove {
|
|
|
|
if err := removeMeta(tx, meta.Name); err != nil {
|
|
|
|
logrus.WithField("volume", meta.Name).Warnf("Error removing stale entry from volume db: %v", err)
|
2016-12-01 17:17:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|