[lxc-devel] [lxd/master] lxd: Rework task handling

stgraber on Github lxc-bot at linuxcontainers.org
Wed Nov 7 04:32:02 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 468 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20181107/087a039b/attachment.bin>
-------------- next part --------------
From f1710016680c74e7312ab74e11bae95dc78411ba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 6 Nov 2018 23:31:13 -0500
Subject: [PATCH] lxd: Rework task handling
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Clustering tasks cause a lot of un-necessary wakeups on non-clustered
systems so only start them when needed.

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/api_cluster.go |  3 +++
 lxd/daemon.go      | 50 ++++++++++++++++++++++++++++++++++------------
 2 files changed, 40 insertions(+), 13 deletions(-)

diff --git a/lxd/api_cluster.go b/lxd/api_cluster.go
index 68b5be91c9..42237cfb68 100644
--- a/lxd/api_cluster.go
+++ b/lxd/api_cluster.go
@@ -173,6 +173,9 @@ func clusterPut(d *Daemon, r *http.Request) Response {
 		return clusterPutDisable(d)
 	}
 
+	// Start clustering tasks
+	d.startClusterTasks()
+
 	// Depending on the provided parameters we either bootstrap a brand new
 	// cluster with this node as first node, or perform a request to join a
 	// given cluster.
diff --git a/lxd/daemon.go b/lxd/daemon.go
index 0974ed420b..3b93ba8dfc 100644
--- a/lxd/daemon.go
+++ b/lxd/daemon.go
@@ -55,7 +55,8 @@ type Daemon struct {
 	shutdownChan chan struct{}
 
 	// Tasks registry for long-running background tasks.
-	tasks task.Group
+	tasks        task.Group
+	clusterTasks bool
 
 	// Indexes of tasks that need to be reset when their execution interval
 	// changes.
@@ -665,9 +666,6 @@ func (d *Daemon) init() error {
 		return err
 	}
 
-	/* Log expiry */
-	d.tasks.Add(expireLogsTask(d.State()))
-
 	// Cleanup leftover images
 	pruneLeftoverImages(d)
 
@@ -763,16 +761,35 @@ func (d *Daemon) init() error {
 	return nil
 }
 
-func (d *Daemon) Ready() error {
-	/* Heartbeats */
+func (d *Daemon) startClusterTasks() {
+	// Check if already running
+	if d.clusterTasks {
+		return
+	}
+
+	// Heartbeats
 	d.tasks.Add(cluster.Heartbeat(d.gateway, d.cluster))
 
-	/* Events */
+	// Events
 	d.tasks.Add(cluster.Events(d.endpoints, d.cluster, eventForward))
 
-	/* Cluster update trigger */
+	// Cluster update trigger
 	d.tasks.Add(cluster.KeepUpdated(d.State()))
 
+	d.clusterTasks = true
+}
+
+func (d *Daemon) Ready() error {
+	// Check if clustered
+	clustered, err := cluster.Enabled(d.db)
+	if err != nil {
+		return err
+	}
+
+	if clustered {
+		d.startClusterTasks()
+	}
+
 	// FIXME: There's no hard reason for which we should not run these
 	//        tasks in mock mode. However it requires that we tweak them so
 	//        they exit gracefully without blocking (something we should do
@@ -780,28 +797,35 @@ func (d *Daemon) Ready() error {
 	//        for proper cancellation is something that has been started
 	//        but has not been fully completed.
 	if !d.os.MockMode {
+		// Log expiry (daily)
+		d.tasks.Add(expireLogsTask(d.State()))
+
+		// Remove expired images (daily)
 		d.taskPruneImages = d.tasks.Add(pruneExpiredImagesTask(d))
 
-		/* Auto-update images */
+		// Auto-update images (every 6 hours, configurable)
 		d.taskAutoUpdate = d.tasks.Add(autoUpdateImagesTask(d))
 
-		/* Auto-update instance types */
+		// Auto-update instance types (daily)
 		d.tasks.Add(instanceRefreshTypesTask(d))
 
-		// Remove expired container backups
+		// Remove expired container backups (hourly)
 		d.tasks.Add(pruneExpiredContainerBackupsTask(d))
 	}
 
+	// Start all background tasks
 	d.tasks.Start()
 
+	// Get daemon state struct
 	s := d.State()
 
-	/* Restore containers */
+	// Restore containers
 	containersRestart(s)
 
-	/* Re-balance in case things changed while LXD was down */
+	// Re-balance in case things changed while LXD was down
 	deviceTaskBalance(s)
 
+	// Unblock incoming requests
 	close(d.readyChan)
 
 	return nil


More information about the lxc-devel mailing list