mirror of
				https://github.com/moby/moby.git
				synced 2022-11-09 12:21:53 -05:00 
			
		
		
		
	Parallel migration and optimizations
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
		
							parent
							
								
									0641429ad8
								
							
						
					
					
						commit
						a8f88ef403
					
				
					 9 changed files with 343 additions and 154 deletions
				
			
		| 
						 | 
				
			
			@ -6,6 +6,10 @@ import (
 | 
			
		|||
	"io/ioutil"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"runtime"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -19,7 +23,7 @@ import (
 | 
			
		|||
)
 | 
			
		||||
 | 
			
		||||
type graphIDRegistrar interface {
 | 
			
		||||
	RegisterByGraphID(string, layer.ChainID, string) (layer.Layer, error)
 | 
			
		||||
	RegisterByGraphID(string, layer.ChainID, layer.DiffID, string, int64) (layer.Layer, error)
 | 
			
		||||
	Release(layer.Layer) ([]layer.Metadata, error)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -27,11 +31,18 @@ type graphIDMounter interface {
 | 
			
		|||
	CreateRWLayerByGraphID(string, string, layer.ChainID) error
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type checksumCalculator interface {
 | 
			
		||||
	ChecksumForGraphID(id, parent, oldTarDataPath, newTarDataPath string) (diffID layer.DiffID, size int64, err error)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	graphDirName                 = "graph"
 | 
			
		||||
	tarDataFileName              = "tar-data.json.gz"
 | 
			
		||||
	migrationFileName            = ".migration-v1-images.json"
 | 
			
		||||
	migrationTagsFileName        = ".migration-v1-tags"
 | 
			
		||||
	migrationDiffIDFileName      = ".migration-diffid"
 | 
			
		||||
	migrationSizeFileName        = ".migration-size"
 | 
			
		||||
	migrationTarDataFileName     = ".migration-tardata"
 | 
			
		||||
	containersDirName            = "containers"
 | 
			
		||||
	configFileNameLegacy         = "config.json"
 | 
			
		||||
	configFileName               = "config.v2.json"
 | 
			
		||||
| 
						 | 
				
			
			@ -45,7 +56,19 @@ var (
 | 
			
		|||
// Migrate takes an old graph directory and transforms the metadata into the
 | 
			
		||||
// new format.
 | 
			
		||||
func Migrate(root, driverName string, ls layer.Store, is image.Store, rs reference.Store, ms metadata.Store) error {
 | 
			
		||||
	mappings := make(map[string]image.ID)
 | 
			
		||||
	graphDir := filepath.Join(root, graphDirName)
 | 
			
		||||
	if _, err := os.Lstat(graphDir); os.IsNotExist(err) {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	mappings, err := restoreMappings(root)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if cc, ok := ls.(checksumCalculator); ok {
 | 
			
		||||
		CalculateLayerChecksums(root, cc, mappings)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if registrar, ok := ls.(graphIDRegistrar); !ok {
 | 
			
		||||
		return errUnsupported
 | 
			
		||||
| 
						 | 
				
			
			@ -53,6 +76,11 @@ func Migrate(root, driverName string, ls layer.Store, is image.Store, rs referen
 | 
			
		|||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = saveMappings(root, mappings)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if mounter, ok := ls.(graphIDMounter); !ok {
 | 
			
		||||
		return errUnsupported
 | 
			
		||||
	} else if err := migrateContainers(root, mounter, is, mappings); err != nil {
 | 
			
		||||
| 
						 | 
				
			
			@ -66,28 +94,115 @@ func Migrate(root, driverName string, ls layer.Store, is image.Store, rs referen
 | 
			
		|||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func migrateImages(root string, ls graphIDRegistrar, is image.Store, ms metadata.Store, mappings map[string]image.ID) error {
 | 
			
		||||
// CalculateLayerChecksums walks an old graph directory and calculates checksums
 | 
			
		||||
// for each layer. These checksums are later used for migration.
 | 
			
		||||
func CalculateLayerChecksums(root string, ls checksumCalculator, mappings map[string]image.ID) {
 | 
			
		||||
	graphDir := filepath.Join(root, graphDirName)
 | 
			
		||||
	if _, err := os.Lstat(graphDir); err != nil {
 | 
			
		||||
		if os.IsNotExist(err) {
 | 
			
		||||
			return nil
 | 
			
		||||
	// spawn some extra workers also for maximum performance because the process is bounded by both cpu and io
 | 
			
		||||
	workers := runtime.NumCPU() * 3
 | 
			
		||||
	workQueue := make(chan string, workers)
 | 
			
		||||
 | 
			
		||||
	wg := sync.WaitGroup{}
 | 
			
		||||
 | 
			
		||||
	for i := 0; i < workers; i++ {
 | 
			
		||||
		wg.Add(1)
 | 
			
		||||
		go func() {
 | 
			
		||||
			for id := range workQueue {
 | 
			
		||||
				start := time.Now()
 | 
			
		||||
				if err := calculateLayerChecksum(graphDir, id, ls); err != nil {
 | 
			
		||||
					logrus.Errorf("could not calculate checksum for %q, %q", id, err)
 | 
			
		||||
				}
 | 
			
		||||
				elapsed := time.Since(start)
 | 
			
		||||
				logrus.Debugf("layer %s took %.2f seconds", id, elapsed.Seconds())
 | 
			
		||||
			}
 | 
			
		||||
			wg.Done()
 | 
			
		||||
		}()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dir, err := ioutil.ReadDir(graphDir)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		logrus.Errorf("could not read directory %q", graphDir)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	for _, v := range dir {
 | 
			
		||||
		v1ID := v.Name()
 | 
			
		||||
		if err := imagev1.ValidateID(v1ID); err != nil {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		if _, ok := mappings[v1ID]; ok { // support old migrations without helper files
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		workQueue <- v1ID
 | 
			
		||||
	}
 | 
			
		||||
	close(workQueue)
 | 
			
		||||
	wg.Wait()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func calculateLayerChecksum(graphDir, id string, ls checksumCalculator) error {
 | 
			
		||||
	diffIDFile := filepath.Join(graphDir, id, migrationDiffIDFileName)
 | 
			
		||||
	if _, err := os.Lstat(diffIDFile); err == nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	} else if !os.IsNotExist(err) {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	parent, err := getParent(filepath.Join(graphDir, id))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	diffID, size, err := ls.ChecksumForGraphID(id, parent, filepath.Join(graphDir, id, tarDataFileName), filepath.Join(graphDir, id, migrationTarDataFileName))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := ioutil.WriteFile(filepath.Join(graphDir, id, migrationSizeFileName), []byte(strconv.Itoa(int(size))), 0600); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := ioutil.WriteFile(filepath.Join(graphDir, id, migrationDiffIDFileName), []byte(diffID), 0600); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	logrus.Infof("calculated checksum for layer %s: %s", id, diffID)
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func restoreMappings(root string) (map[string]image.ID, error) {
 | 
			
		||||
	mappings := make(map[string]image.ID)
 | 
			
		||||
 | 
			
		||||
	mfile := filepath.Join(root, migrationFileName)
 | 
			
		||||
	f, err := os.Open(mfile)
 | 
			
		||||
	if err != nil && !os.IsNotExist(err) {
 | 
			
		||||
		return err
 | 
			
		||||
		return nil, err
 | 
			
		||||
	} else if err == nil {
 | 
			
		||||
		err := json.NewDecoder(f).Decode(&mappings)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			f.Close()
 | 
			
		||||
			return err
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		f.Close()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return mappings, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func saveMappings(root string, mappings map[string]image.ID) error {
 | 
			
		||||
	mfile := filepath.Join(root, migrationFileName)
 | 
			
		||||
	f, err := os.OpenFile(mfile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	defer f.Close()
 | 
			
		||||
	if err := json.NewEncoder(f).Encode(mappings); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func migrateImages(root string, ls graphIDRegistrar, is image.Store, ms metadata.Store, mappings map[string]image.ID) error {
 | 
			
		||||
	graphDir := filepath.Join(root, graphDirName)
 | 
			
		||||
 | 
			
		||||
	dir, err := ioutil.ReadDir(graphDir)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
| 
						 | 
				
			
			@ -105,15 +220,6 @@ func migrateImages(root string, ls graphIDRegistrar, is image.Store, ms metadata
 | 
			
		|||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	f, err = os.OpenFile(mfile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	defer f.Close()
 | 
			
		||||
	if err := json.NewEncoder(f).Encode(mappings); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -251,6 +357,30 @@ func migrateRefs(root, driverName string, rs refAdder, mappings map[string]image
 | 
			
		|||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getParent(confDir string) (string, error) {
 | 
			
		||||
	jsonFile := filepath.Join(confDir, "json")
 | 
			
		||||
	imageJSON, err := ioutil.ReadFile(jsonFile)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
	}
 | 
			
		||||
	var parent struct {
 | 
			
		||||
		Parent   string
 | 
			
		||||
		ParentID digest.Digest `json:"parent_id"`
 | 
			
		||||
	}
 | 
			
		||||
	if err := json.Unmarshal(imageJSON, &parent); err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
	}
 | 
			
		||||
	if parent.Parent == "" && parent.ParentID != "" { // v1.9
 | 
			
		||||
		parent.Parent = parent.ParentID.Hex()
 | 
			
		||||
	}
 | 
			
		||||
	// compatibilityID for parent
 | 
			
		||||
	parentCompatibilityID, err := ioutil.ReadFile(filepath.Join(confDir, "parent"))
 | 
			
		||||
	if err == nil && len(parentCompatibilityID) > 0 {
 | 
			
		||||
		parent.Parent = string(parentCompatibilityID)
 | 
			
		||||
	}
 | 
			
		||||
	return parent.Parent, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func migrateImage(id, root string, ls graphIDRegistrar, is image.Store, ms metadata.Store, mappings map[string]image.ID) (err error) {
 | 
			
		||||
	defer func() {
 | 
			
		||||
		if err != nil {
 | 
			
		||||
| 
						 | 
				
			
			@ -258,36 +388,20 @@ func migrateImage(id, root string, ls graphIDRegistrar, is image.Store, ms metad
 | 
			
		|||
		}
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	jsonFile := filepath.Join(root, graphDirName, id, "json")
 | 
			
		||||
	imageJSON, err := ioutil.ReadFile(jsonFile)
 | 
			
		||||
	parent, err := getParent(filepath.Join(root, graphDirName, id))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	var parent struct {
 | 
			
		||||
		Parent   string
 | 
			
		||||
		ParentID digest.Digest `json:"parent_id"`
 | 
			
		||||
	}
 | 
			
		||||
	if err := json.Unmarshal(imageJSON, &parent); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	if parent.Parent == "" && parent.ParentID != "" { // v1.9
 | 
			
		||||
		parent.Parent = parent.ParentID.Hex()
 | 
			
		||||
	}
 | 
			
		||||
	// compatibilityID for parent
 | 
			
		||||
	parentCompatibilityID, err := ioutil.ReadFile(filepath.Join(root, graphDirName, id, "parent"))
 | 
			
		||||
	if err == nil && len(parentCompatibilityID) > 0 {
 | 
			
		||||
		parent.Parent = string(parentCompatibilityID)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var parentID image.ID
 | 
			
		||||
	if parent.Parent != "" {
 | 
			
		||||
	if parent != "" {
 | 
			
		||||
		var exists bool
 | 
			
		||||
		if parentID, exists = mappings[parent.Parent]; !exists {
 | 
			
		||||
			if err := migrateImage(parent.Parent, root, ls, is, ms, mappings); err != nil {
 | 
			
		||||
		if parentID, exists = mappings[parent]; !exists {
 | 
			
		||||
			if err := migrateImage(parent, root, ls, is, ms, mappings); err != nil {
 | 
			
		||||
				// todo: fail or allow broken chains?
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
			parentID = mappings[parent.Parent]
 | 
			
		||||
			parentID = mappings[parent]
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -304,12 +418,32 @@ func migrateImage(id, root string, ls graphIDRegistrar, is image.Store, ms metad
 | 
			
		|||
		history = parentImg.History
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	layer, err := ls.RegisterByGraphID(id, rootFS.ChainID(), filepath.Join(filepath.Join(root, graphDirName, id, tarDataFileName)))
 | 
			
		||||
	diffID, err := ioutil.ReadFile(filepath.Join(root, graphDirName, id, migrationDiffIDFileName))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	sizeStr, err := ioutil.ReadFile(filepath.Join(root, graphDirName, id, migrationSizeFileName))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	size, err := strconv.ParseInt(string(sizeStr), 10, 64)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	layer, err := ls.RegisterByGraphID(id, rootFS.ChainID(), layer.DiffID(diffID), filepath.Join(root, graphDirName, id, migrationTarDataFileName), size)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	logrus.Infof("migrated layer %s to %s", id, layer.DiffID())
 | 
			
		||||
 | 
			
		||||
	jsonFile := filepath.Join(root, graphDirName, id, "json")
 | 
			
		||||
	imageJSON, err := ioutil.ReadFile(jsonFile)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	h, err := imagev1.HistoryFromConfig(imageJSON, false)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -234,12 +234,30 @@ func TestMigrateUnsupported(t *testing.T) {
 | 
			
		|||
	}
 | 
			
		||||
	defer os.RemoveAll(tmpdir)
 | 
			
		||||
 | 
			
		||||
	err = os.MkdirAll(filepath.Join(tmpdir, "graph"), 0700)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = Migrate(tmpdir, "generic", nil, nil, nil, nil)
 | 
			
		||||
	if err != errUnsupported {
 | 
			
		||||
		t.Fatalf("expected unsupported error, got %q", err)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestMigrateEmptyDir(t *testing.T) {
 | 
			
		||||
	tmpdir, err := ioutil.TempDir("", "migrate-empty")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	defer os.RemoveAll(tmpdir)
 | 
			
		||||
 | 
			
		||||
	err = Migrate(tmpdir, "generic", nil, nil, nil, nil)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func addImage(dest, jsonConfig, parent, checksum string) (string, error) {
 | 
			
		||||
	var config struct{ ID string }
 | 
			
		||||
	if err := json.Unmarshal([]byte(jsonConfig), &config); err != nil {
 | 
			
		||||
| 
						 | 
				
			
			@ -257,6 +275,17 @@ func addImage(dest, jsonConfig, parent, checksum string) (string, error) {
 | 
			
		|||
	if err := ioutil.WriteFile(filepath.Join(contDir, "json"), []byte(jsonConfig), 0600); err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
	}
 | 
			
		||||
	if checksum != "" {
 | 
			
		||||
		if err := ioutil.WriteFile(filepath.Join(contDir, "checksum"), []byte(checksum), 0600); err != nil {
 | 
			
		||||
			return "", err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if err := ioutil.WriteFile(filepath.Join(contDir, ".migration-diffid"), []byte(layer.EmptyLayer.DiffID()), 0600); err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
	}
 | 
			
		||||
	if err := ioutil.WriteFile(filepath.Join(contDir, ".migration-size"), []byte("0"), 0600); err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
	}
 | 
			
		||||
	if parent != "" {
 | 
			
		||||
		if err := ioutil.WriteFile(filepath.Join(contDir, "parent"), []byte(parent), 0600); err != nil {
 | 
			
		||||
			return "", err
 | 
			
		||||
| 
						 | 
				
			
			@ -305,7 +334,7 @@ type mockRegistrar struct {
 | 
			
		|||
	count  int
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (r *mockRegistrar) RegisterByGraphID(graphID string, parent layer.ChainID, tarDataFile string) (layer.Layer, error) {
 | 
			
		||||
func (r *mockRegistrar) RegisterByGraphID(graphID string, parent layer.ChainID, diffID layer.DiffID, tarDataFile string, size int64) (layer.Layer, error) {
 | 
			
		||||
	r.count++
 | 
			
		||||
	l := &mockLayer{}
 | 
			
		||||
	if parent != "" {
 | 
			
		||||
| 
						 | 
				
			
			@ -316,7 +345,7 @@ func (r *mockRegistrar) RegisterByGraphID(graphID string, parent layer.ChainID,
 | 
			
		|||
		l.parent = p
 | 
			
		||||
		l.diffIDs = append(l.diffIDs, p.diffIDs...)
 | 
			
		||||
	}
 | 
			
		||||
	l.diffIDs = append(l.diffIDs, layer.EmptyLayer.DiffID())
 | 
			
		||||
	l.diffIDs = append(l.diffIDs, diffID)
 | 
			
		||||
	if r.layers == nil {
 | 
			
		||||
		r.layers = make(map[layer.ChainID]*mockLayer)
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue