[lxc-devel] [lxd/master] Implements boot.stop.priority as described in #2082

Fierykev on Github lxc-bot at linuxcontainers.org
Tue Dec 12 11:05:29 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 754 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20171212/9474b71e/attachment.bin>
-------------- next part --------------
From 2543781446085d7b02f0892d886fbe139f3e2535 Mon Sep 17 00:00:00 2001
From: Kevin Pick <cyberkpick at utexas.edu>
Date: Tue, 12 Dec 2017 01:50:21 -0800
Subject: [PATCH] Shutdown order is now enforced

Signed-off-by: Kevin Pick <cyberkpick at utexas.edu>
---
 config/bash/lxd-client |  3 ++-
 lxd/containers.go      | 58 ++++++++++++++++++++++++++++++++++++++++++++------
 shared/container.go    |  1 +
 3 files changed, 55 insertions(+), 7 deletions(-)

diff --git a/config/bash/lxd-client b/config/bash/lxd-client
index e620c8e98..d7f9fb26f 100644
--- a/config/bash/lxd-client
+++ b/config/bash/lxd-client
@@ -72,7 +72,8 @@ _have lxc && {
       images.compression_algorithm images.remote_cache_expiry"
 
     container_keys="boot.autostart boot.autostart.delay \
-      boot.autostart.priority boot.host_shutdown_timeout environment. \
+      boot.autostart.priority boot.stop.priority \
+      boot.host_shutdown_timeout environment. \
       limits.cpu limits.cpu.allowance limits.cpu.priority \
       limits.disk.priority limits.memory limits.memory.enforce \
       limits.memory.swap limits.memory.swap.priority limits.network.priority \
diff --git a/lxd/containers.go b/lxd/containers.go
index 0ab650cd1..92cb6d96d 100644
--- a/lxd/containers.go
+++ b/lxd/containers.go
@@ -149,6 +149,29 @@ func containersRestart(s *state.State) error {
 	return nil
 }
 
+type containerStopList []container
+
+func (slice containerStopList) Len() int {
+	return len(slice)
+}
+
+func (slice containerStopList) Less(i, j int) bool {
+	iOrder := slice[i].ExpandedConfig()["boot.stop.priority"]
+	jOrder := slice[j].ExpandedConfig()["boot.stop.priority"]
+
+	if iOrder != jOrder {
+		iOrderInt, _ := strconv.Atoi(iOrder)
+		jOrderInt, _ := strconv.Atoi(jOrder)
+		return iOrderInt > jOrderInt // check this line (prob <)
+	}
+
+	return slice[i].Name() < slice[j].Name()
+}
+
+func (slice containerStopList) Swap(i, j int) {
+	slice[i], slice[j] = slice[j], slice[i]
+}
+
 func containersShutdown(s *state.State) error {
 	var wg sync.WaitGroup
 
@@ -158,22 +181,45 @@ func containersShutdown(s *state.State) error {
 		return err
 	}
 
+	containers := []container{}
+
+	for _, name := range results {
+		c, err := containerLoadByName(s, name)
+		if err != nil {
+			return err
+		}
+
+		containers = append(containers, c)
+	}
+
+	sort.Sort(containerStopList(containers))
+
 	// Reset all container states
 	err = s.DB.ContainersResetState()
 	if err != nil {
 		return err
 	}
 
-	for _, r := range results {
-		// Load the container
-		c, err := containerLoadByName(s, r)
-		if err != nil {
-			return err
-		}
+	var lastPriority int = 0
+
+	if len(containers) != 0 {
+		lastPriority, _ = strconv.Atoi(containers[0].ExpandedConfig()["boot.stop.priority"])
+	}
+
+	for _, c := range containers {
+		priority, _ := strconv.Atoi(c.ExpandedConfig()["boot.stop.priority"])
 
 		// Record the current state
 		lastState := c.State()
 
+		// Enforce shutdown priority
+		if priority != lastPriority {
+			lastPriority = priority
+
+			// Wait for containers with higher priority to finish
+			wg.Wait()
+		}
+
 		// Stop the container
 		if c.IsRunning() {
 			// Determinate how long to wait for the container to shutdown cleanly
diff --git a/shared/container.go b/shared/container.go
index 2aa2a4829..79a3ce4a4 100644
--- a/shared/container.go
+++ b/shared/container.go
@@ -94,6 +94,7 @@ var KnownContainerConfigKeys = map[string]func(value string) error{
 	"boot.autostart":             IsBool,
 	"boot.autostart.delay":       IsInt64,
 	"boot.autostart.priority":    IsInt64,
+	"boot.stop.priority":         IsInt64,
 	"boot.host_shutdown_timeout": IsInt64,
 
 	"limits.cpu": IsAny,


More information about the lxc-devel mailing list