Skip to content

Commit

Permalink
Merge pull request moby#28996 from Microsoft/jjh/sqlite-remove-windows
Browse files Browse the repository at this point in the history
Windows: Factor out sqlite
  • Loading branch information
justincormack authored Dec 1, 2016
2 parents 0020398 + 3f6127b commit a756c1a
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 78 deletions.
19 changes: 5 additions & 14 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{})
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
41 changes: 0 additions & 41 deletions daemon/links.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
}
72 changes: 72 additions & 0 deletions daemon/links_linux.go
Original file line number Diff line number Diff line change
@@ -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
}
File renamed without changes.
10 changes: 10 additions & 0 deletions daemon/links_notlinux.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 0 additions & 7 deletions pkg/graphdb/conn_sqlite3_unix.go

This file was deleted.

7 changes: 0 additions & 7 deletions pkg/graphdb/conn_sqlite3_windows.go

This file was deleted.

8 changes: 0 additions & 8 deletions pkg/graphdb/conn_unsupported.go

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions pkg/graphdb/unsupported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// +build !cgo !linux

package graphdb
File renamed without changes.

0 comments on commit a756c1a

Please sign in to comment.