From 360d59662fde02101d9f2c00dbccd4c3e1be658d Mon Sep 17 00:00:00 2001
From: Brian Goff <cpuguy83@gmail.com>
Date: Wed, 20 Jan 2016 12:06:03 -0500
Subject: [PATCH] Fix loading of containerized plugins

During daemon startup, all containers are registered before any are
started.
During container registration it was calling out to initialize volumes.
If the volume uses a plugin that is running in a container, this will
cause the restart of that container to fail since the plugin is not yet
running.
This also slowed down daemon startup since volume initialization was
happening sequentially, which can be slow (and is flat out slow since
initialization would fail but take 8 seconds for each volume to do it).

This fix holds off on volume initialization until after containers are
restarted and does the initialization in parallel.

The containers that are restarted will have thier volumes initialized
because they are being started. If any of these containers are using a
plugin they will just keep retrying to reach the plugin (up to the
timeout, which is 8seconds) until the container with the plugin is up
and running.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
---
 daemon/daemon.go  | 21 +++++++++++++++++----
 daemon/volumes.go |  1 -
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/daemon/daemon.go b/daemon/daemon.go
index c710dc838e..9bacc029ea 100644
--- a/daemon/daemon.go
+++ b/daemon/daemon.go
@@ -282,10 +282,6 @@ func (daemon *Daemon) Register(container *container.Container) error {
 		}
 	}
 
-	if err := daemon.prepareMountPoints(container); err != nil {
-		return err
-	}
-
 	return nil
 }
 
@@ -408,6 +404,23 @@ func (daemon *Daemon) restore() error {
 	}
 	group.Wait()
 
+	// any containers that were started above would already have had this done,
+	// however we need to now prepare the mountpoints for the rest of the containers as well.
+	// This shouldn't cause any issue running on the containers that already had this run.
+	// This must be run after any containers with a restart policy so that containerized plugins
+	// can have a chance to be running before we try to initialize them.
+	for _, c := range containers {
+		group.Add(1)
+		go func(c *container.Container) {
+			defer group.Done()
+			if err := daemon.prepareMountPoints(c); err != nil {
+				logrus.Error(err)
+			}
+		}(c)
+	}
+
+	group.Wait()
+
 	if !debug {
 		if logrus.GetLevel() == logrus.InfoLevel {
 			fmt.Println()
diff --git a/daemon/volumes.go b/daemon/volumes.go
index c0097679f6..d807fe84bb 100644
--- a/daemon/volumes.go
+++ b/daemon/volumes.go
@@ -159,7 +159,6 @@ func (daemon *Daemon) registerMountPoints(container *container.Container, hostCo
 func (daemon *Daemon) lazyInitializeVolume(containerID string, m *volume.MountPoint) error {
 	if len(m.Driver) > 0 && m.Volume == nil {
 		v, err := daemon.volumes.GetWithRef(m.Name, m.Driver, containerID)
-
 		if err != nil {
 			return err
 		}