From 018f19f8c988ee8be15b212ef46104ec7a789eac Mon Sep 17 00:00:00 2001 From: John Howard Date: Wed, 30 Nov 2016 10:59:54 -0800 Subject: [PATCH] Windows: Factor out sqlite Signed-off-by: John Howard (cherry picked from commit 3f6127b173949cb36557601a56bc15ae2c45a698) Signed-off-by: Victor Vieux --- daemon/daemon.go | 19 ++--- daemon/links.go | 41 ----------- daemon/links_linux.go | 72 +++++++++++++++++++ daemon/{links_test.go => links_linux_test.go} | 0 daemon/links_notlinux.go | 10 +++ ...{conn_sqlite3.go => conn_sqlite3_linux.go} | 6 +- pkg/graphdb/conn_sqlite3_unix.go | 7 -- pkg/graphdb/conn_sqlite3_windows.go | 7 -- pkg/graphdb/conn_unsupported.go | 8 --- pkg/graphdb/{graphdb.go => graphdb_linux.go} | 0 ...{graphdb_test.go => graphdb_linux_test.go} | 0 pkg/graphdb/{sort.go => sort_linux.go} | 0 .../{sort_test.go => sort_linux_test.go} | 0 pkg/graphdb/unsupported.go | 3 + pkg/graphdb/{utils.go => utils_linux.go} | 0 15 files changed, 95 insertions(+), 78 deletions(-) create mode 100644 daemon/links_linux.go rename daemon/{links_test.go => links_linux_test.go} (100%) create mode 100644 daemon/links_notlinux.go rename pkg/graphdb/{conn_sqlite3.go => conn_sqlite3_linux.go} (75%) delete mode 100644 pkg/graphdb/conn_sqlite3_unix.go delete mode 100644 pkg/graphdb/conn_sqlite3_windows.go delete mode 100644 pkg/graphdb/conn_unsupported.go rename pkg/graphdb/{graphdb.go => graphdb_linux.go} (100%) rename pkg/graphdb/{graphdb_test.go => graphdb_linux_test.go} (100%) rename pkg/graphdb/{sort.go => sort_linux.go} (100%) rename pkg/graphdb/{sort_test.go => sort_linux_test.go} (100%) create mode 100644 pkg/graphdb/unsupported.go rename pkg/graphdb/{utils.go => utils_linux.go} (100%) diff --git a/daemon/daemon.go b/daemon/daemon.go index c3c2dc5f72..7c2ce50559 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -40,7 +40,6 @@ import ( "github.com/docker/docker/libcontainerd" "github.com/docker/docker/migrate/v1" "github.com/docker/docker/pkg/fileutils" - "github.com/docker/docker/pkg/graphdb" "github.com/docker/docker/pkg/idtools" "github.com/docker/docker/pkg/plugingetter" "github.com/docker/docker/pkg/progress" @@ -158,7 +157,6 @@ func (daemon *Daemon) restore() error { } } - var migrateLegacyLinks bool removeContainers := make(map[string]*container.Container) restartContainers := make(map[*container.Container]chan struct{}) activeSandboxes := make(map[string]interface{}) @@ -190,6 +188,8 @@ func (daemon *Daemon) restore() error { } } } + + var migrateLegacyLinks bool // Not relevant on Windows var wg sync.WaitGroup var mapLock sync.Mutex for _, c := range containers { @@ -265,24 +265,15 @@ func (daemon *Daemon) restore() error { return fmt.Errorf("Error initializing network controller: %v", err) } - // migrate any legacy links from sqlite - linkdbFile := filepath.Join(daemon.root, "linkgraph.db") - var legacyLinkDB *graphdb.Database + // Perform migration of legacy sqlite links (no-op on Windows) if migrateLegacyLinks { - legacyLinkDB, err = graphdb.NewSqliteConn(linkdbFile) - if err != nil { - return fmt.Errorf("error connecting to legacy link graph DB %s, container links may be lost: %v", linkdbFile, err) + if err := daemon.sqliteMigration(containers); err != nil { + return err } - defer legacyLinkDB.Close() } // Now that all the containers are registered, register the links for _, c := range containers { - if migrateLegacyLinks { - if err := daemon.migrateLegacySqliteLinks(legacyLinkDB, c); err != nil { - return err - } - } if err := daemon.registerLinks(c, c.HostConfig); err != nil { logrus.Errorf("failed to register link for container %s: %v", c.ID, err) } diff --git a/daemon/links.go b/daemon/links.go index aaf1917d7c..7f691d4f16 100644 --- a/daemon/links.go +++ b/daemon/links.go @@ -1,12 +1,9 @@ package daemon import ( - "strings" "sync" - "github.com/Sirupsen/logrus" "github.com/docker/docker/container" - "github.com/docker/docker/pkg/graphdb" ) // linkIndex stores link relationships between containers, including their specified alias @@ -88,41 +85,3 @@ func (l *linkIndex) delete(container *container.Container) { delete(l.childIdx, container) l.mu.Unlock() } - -// migrateLegacySqliteLinks migrates sqlite links to use links from HostConfig -// when sqlite links were used, hostConfig.Links was set to nil -func (daemon *Daemon) migrateLegacySqliteLinks(db *graphdb.Database, container *container.Container) error { - // if links is populated (or an empty slice), then this isn't using sqlite links and can be skipped - if container.HostConfig == nil || container.HostConfig.Links != nil { - return nil - } - - logrus.Debugf("migrating legacy sqlite link info for container: %s", container.ID) - - fullName := container.Name - if fullName[0] != '/' { - fullName = "/" + fullName - } - - // don't use a nil slice, this ensures that the check above will skip once the migration has completed - links := []string{} - children, err := db.Children(fullName, 0) - if err != nil { - if !strings.Contains(err.Error(), "Cannot find child for") { - return err - } - // else continue... it's ok if we didn't find any children, it'll just be nil and we can continue the migration - } - - for _, child := range children { - c, err := daemon.GetContainer(child.Entity.ID()) - if err != nil { - return err - } - - links = append(links, c.Name+":"+child.Edge.Name) - } - - container.HostConfig.Links = links - return container.WriteHostConfig() -} diff --git a/daemon/links_linux.go b/daemon/links_linux.go new file mode 100644 index 0000000000..2ea40d9e51 --- /dev/null +++ b/daemon/links_linux.go @@ -0,0 +1,72 @@ +package daemon + +import ( + "fmt" + "path/filepath" + "strings" + + "github.com/Sirupsen/logrus" + "github.com/docker/docker/container" + "github.com/docker/docker/pkg/graphdb" +) + +// migrateLegacySqliteLinks migrates sqlite links to use links from HostConfig +// when sqlite links were used, hostConfig.Links was set to nil +func (daemon *Daemon) migrateLegacySqliteLinks(db *graphdb.Database, container *container.Container) error { + // if links is populated (or an empty slice), then this isn't using sqlite links and can be skipped + if container.HostConfig == nil || container.HostConfig.Links != nil { + return nil + } + + logrus.Debugf("migrating legacy sqlite link info for container: %s", container.ID) + + fullName := container.Name + if fullName[0] != '/' { + fullName = "/" + fullName + } + + // don't use a nil slice, this ensures that the check above will skip once the migration has completed + links := []string{} + children, err := db.Children(fullName, 0) + if err != nil { + if !strings.Contains(err.Error(), "Cannot find child for") { + return err + } + // else continue... it's ok if we didn't find any children, it'll just be nil and we can continue the migration + } + + for _, child := range children { + c, err := daemon.GetContainer(child.Entity.ID()) + if err != nil { + return err + } + + links = append(links, c.Name+":"+child.Edge.Name) + } + + container.HostConfig.Links = links + return container.WriteHostConfig() +} + +// sqliteMigration performs the link graph DB migration. +func (daemon *Daemon) sqliteMigration(containers map[string]*container.Container) error { + // migrate any legacy links from sqlite + linkdbFile := filepath.Join(daemon.root, "linkgraph.db") + var ( + legacyLinkDB *graphdb.Database + err error + ) + + legacyLinkDB, err = graphdb.NewSqliteConn(linkdbFile) + if err != nil { + return fmt.Errorf("error connecting to legacy link graph DB %s, container links may be lost: %v", linkdbFile, err) + } + defer legacyLinkDB.Close() + + for _, c := range containers { + if err := daemon.migrateLegacySqliteLinks(legacyLinkDB, c); err != nil { + return err + } + } + return nil +} diff --git a/daemon/links_test.go b/daemon/links_linux_test.go similarity index 100% rename from daemon/links_test.go rename to daemon/links_linux_test.go diff --git a/daemon/links_notlinux.go b/daemon/links_notlinux.go new file mode 100644 index 0000000000..12c226cfac --- /dev/null +++ b/daemon/links_notlinux.go @@ -0,0 +1,10 @@ +// +build !linux + +package daemon + +import "github.com/docker/docker/container" + +// sqliteMigration performs the link graph DB migration. No-op on platforms other than Linux +func (daemon *Daemon) sqliteMigration(_ map[string]*container.Container) error { + return nil +} diff --git a/pkg/graphdb/conn_sqlite3.go b/pkg/graphdb/conn_sqlite3_linux.go similarity index 75% rename from pkg/graphdb/conn_sqlite3.go rename to pkg/graphdb/conn_sqlite3_linux.go index dbcf44c256..8e61ff3b4f 100644 --- a/pkg/graphdb/conn_sqlite3.go +++ b/pkg/graphdb/conn_sqlite3_linux.go @@ -2,7 +2,11 @@ package graphdb -import "database/sql" +import ( + "database/sql" + + _ "github.com/mattn/go-sqlite3" // registers sqlite +) // NewSqliteConn opens a connection to a sqlite // database. diff --git a/pkg/graphdb/conn_sqlite3_unix.go b/pkg/graphdb/conn_sqlite3_unix.go deleted file mode 100644 index f932fff286..0000000000 --- a/pkg/graphdb/conn_sqlite3_unix.go +++ /dev/null @@ -1,7 +0,0 @@ -// +build cgo,!windows - -package graphdb - -import ( - _ "github.com/mattn/go-sqlite3" // registers sqlite -) diff --git a/pkg/graphdb/conn_sqlite3_windows.go b/pkg/graphdb/conn_sqlite3_windows.go deleted file mode 100644 index 52590303d4..0000000000 --- a/pkg/graphdb/conn_sqlite3_windows.go +++ /dev/null @@ -1,7 +0,0 @@ -// +build cgo,windows - -package graphdb - -import ( - _ "github.com/mattn/go-sqlite3" // registers sqlite -) diff --git a/pkg/graphdb/conn_unsupported.go b/pkg/graphdb/conn_unsupported.go deleted file mode 100644 index cf977050da..0000000000 --- a/pkg/graphdb/conn_unsupported.go +++ /dev/null @@ -1,8 +0,0 @@ -// +build !cgo - -package graphdb - -// NewSqliteConn return a new sqlite connection. -func NewSqliteConn(root string) (*Database, error) { - panic("Not implemented") -} diff --git a/pkg/graphdb/graphdb.go b/pkg/graphdb/graphdb_linux.go similarity index 100% rename from pkg/graphdb/graphdb.go rename to pkg/graphdb/graphdb_linux.go diff --git a/pkg/graphdb/graphdb_test.go b/pkg/graphdb/graphdb_linux_test.go similarity index 100% rename from pkg/graphdb/graphdb_test.go rename to pkg/graphdb/graphdb_linux_test.go diff --git a/pkg/graphdb/sort.go b/pkg/graphdb/sort_linux.go similarity index 100% rename from pkg/graphdb/sort.go rename to pkg/graphdb/sort_linux.go diff --git a/pkg/graphdb/sort_test.go b/pkg/graphdb/sort_linux_test.go similarity index 100% rename from pkg/graphdb/sort_test.go rename to pkg/graphdb/sort_linux_test.go diff --git a/pkg/graphdb/unsupported.go b/pkg/graphdb/unsupported.go new file mode 100644 index 0000000000..2b8ba71724 --- /dev/null +++ b/pkg/graphdb/unsupported.go @@ -0,0 +1,3 @@ +// +build !cgo !linux + +package graphdb diff --git a/pkg/graphdb/utils.go b/pkg/graphdb/utils_linux.go similarity index 100% rename from pkg/graphdb/utils.go rename to pkg/graphdb/utils_linux.go