[lxc-devel] [lxd/master] lxd/containers: Combine state updates

stgraber on Github lxc-bot at linuxcontainers.org
Tue Jul 9 15:52:08 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 354 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190709/ab890afb/attachment-0001.bin>
-------------- next part --------------
From e2e81a305c7d1b39f4699a39a91b4c358806ed50 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 9 Jul 2019 11:51:36 -0400
Subject: [PATCH] lxd/containers: Combine state updates
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/container_lxc.go | 24 ++++++++++++++--------
 lxd/db/containers.go | 49 +++++++++++++++++++++++++++++---------------
 2 files changed, 49 insertions(+), 24 deletions(-)

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index dd4faed75c..0aa1e2a187 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -2635,12 +2635,6 @@ func (c *containerLXC) startCommon() (string, error) {
 		os.RemoveAll(c.StatePath())
 	}
 
-	// Update time container was last started
-	err = c.state.Cluster.ContainerLastUsedUpdate(c.id, time.Now().UTC())
-	if err != nil {
-		return "", fmt.Errorf("Error updating last used: %v", err)
-	}
-
 	// Unmount any previously mounted shiftfs
 	unix.Unmount(c.RootfsPath(), unix.MNT_DETACH)
 
@@ -3320,8 +3314,22 @@ func (c *containerLXC) OnStart() error {
 		}
 	}
 
-	// Record current state
-	err = c.state.Cluster.ContainerSetState(c.id, "RUNNING")
+	// Database updates
+	err = c.state.Cluster.Transaction(func(tx *db.ClusterTx) error {
+		// Record current state
+		err = tx.ContainerSetState(c.id, "RUNNING")
+		if err != nil {
+			return errors.Wrap(err, "Error updating container state")
+		}
+
+		// Update time container last started time
+		err = tx.ContainerLastUsedUpdate(c.id, time.Now().UTC())
+		if err != nil {
+			return errors.Wrap(err, "Error updating last used")
+		}
+
+		return nil
+	})
 	if err != nil {
 		return err
 	}
diff --git a/lxd/db/containers.go b/lxd/db/containers.go
index e8709ebb3a..036b4bab51 100644
--- a/lxd/db/containers.go
+++ b/lxd/db/containers.go
@@ -844,22 +844,29 @@ func (c *Cluster) ContainersResetState() error {
 // ContainerSetState sets the the power state of the container with the given ID.
 func (c *Cluster) ContainerSetState(id int, state string) error {
 	err := c.Transaction(func(tx *ClusterTx) error {
-		// Set the new value
-		str := fmt.Sprintf("INSERT OR REPLACE INTO containers_config (container_id, key, value) VALUES (?, 'volatile.last_state.power', ?)")
-		stmt, err := tx.tx.Prepare(str)
-		if err != nil {
-			return err
-		}
-		defer stmt.Close()
-
-		if _, err = stmt.Exec(id, state); err != nil {
-			return err
-		}
-		return nil
+		return tx.ContainerSetState(id, state)
 	})
 	return err
 }
 
+// ContainerSetState sets the the power state of the container with the given ID.
+func (c *ClusterTx) ContainerSetState(id int, state string) error {
+	// Set the new value
+	str := fmt.Sprintf("INSERT OR REPLACE INTO containers_config (container_id, key, value) VALUES (?, 'volatile.last_state.power', ?)")
+	stmt, err := c.tx.Prepare(str)
+	if err != nil {
+		return err
+	}
+	defer stmt.Close()
+
+	_, err = stmt.Exec(id, state)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
 // ContainerUpdate updates the description, architecture and ephemeral flag of
 // the container with the given ID.
 func ContainerUpdate(tx *sql.Tx, id int, description string, architecture int, ephemeral bool,
@@ -898,10 +905,20 @@ func (c *Cluster) ContainerCreationUpdate(id int, date time.Time) error {
 
 // ContainerLastUsedUpdate updates the last_use_date field of the container
 // with the given ID.
-func (c *Cluster) ContainerLastUsedUpdate(id int, date time.Time) error {
-	stmt := `UPDATE containers SET last_use_date=? WHERE id=?`
-	err := exec(c.db, stmt, date, id)
-	return err
+func (c *ClusterTx) ContainerLastUsedUpdate(id int, date time.Time) error {
+	str := `UPDATE containers SET last_use_date=? WHERE id=?`
+	stmt, err := c.tx.Prepare(str)
+	if err != nil {
+		return err
+	}
+	defer stmt.Close()
+
+	_, err = stmt.Exec(id, date)
+	if err != nil {
+		return err
+	}
+
+	return nil
 }
 
 // ContainerGetSnapshots returns the names of all snapshots of the container


More information about the lxc-devel mailing list