[lxc-devel] [lxd/master] Db logic cleanup part 4

freeekanayaka on Github lxc-bot at linuxcontainers.org
Wed May 27 09:55:12 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200527/d91c498a/attachment.bin>
-------------- next part --------------
From e7e50897e285921da7559d85e72d83b1e4719bab Mon Sep 17 00:00:00 2001
From: Free Ekanayaka <free.ekanayaka at canonical.com>
Date: Tue, 26 May 2020 15:44:45 +0100
Subject: [PATCH 1/2] lxd: Reduce number of transactions in
 containerPostClusteringMigrate

Signed-off-by: Free Ekanayaka <free.ekanayaka at canonical.com>
---
 lxd/db/instances.go  | 24 +++++++++++++++++-------
 lxd/instance_post.go | 39 ++++++++++++++++++++++-----------------
 2 files changed, 39 insertions(+), 24 deletions(-)

diff --git a/lxd/db/instances.go b/lxd/db/instances.go
index 9da263677c..15c0ebd977 100644
--- a/lxd/db/instances.go
+++ b/lxd/db/instances.go
@@ -680,9 +680,9 @@ func CreateInstanceConfig(tx *sql.Tx, id int, config map[string]string) error {
 func (c *Cluster) GetInstanceConfig(id int, key string) (string, error) {
 	q := "SELECT value FROM instances_config WHERE instance_id=? AND key=?"
 	value := ""
-	arg1 := []interface{}{id, key}
-	arg2 := []interface{}{&value}
-	err := dbQueryRowScan(c, q, arg1, arg2)
+	err := c.Transaction(func(tx *ClusterTx) error {
+		return tx.tx.QueryRow(q, id, key).Scan(&value)
+	})
 	if err == sql.ErrNoRows {
 		return "", ErrNoSuchObject
 	}
@@ -693,7 +693,16 @@ func (c *Cluster) GetInstanceConfig(id int, key string) (string, error) {
 // DeleteInstanceConfigKey removes the given key from the config of the instance
 // with the given ID.
 func (c *Cluster) DeleteInstanceConfigKey(id int, key string) error {
-	err := exec(c, "DELETE FROM instances_config WHERE key=? AND instance_id=?", key, id)
+	return c.Transaction(func(tx *ClusterTx) error {
+		return tx.DeleteInstanceConfigKey(int64(id), key)
+	})
+}
+
+// DeleteInstanceConfigKey removes the given key from the config of the instance
+// with the given ID.
+func (c *ClusterTx) DeleteInstanceConfigKey(id int64, key string) error {
+	q := "DELETE FROM instances_config WHERE key=? AND instance_id=?"
+	_, err := c.tx.Exec(q, key, id)
 	return err
 }
 
@@ -704,9 +713,10 @@ func (c *Cluster) UpdateInstanceStatefulFlag(id int, stateful bool) error {
 	if stateful {
 		statefulInt = 1
 	}
-
-	err := exec(c, "UPDATE instances SET stateful=? WHERE id=?", statefulInt, id)
-	return err
+	return c.Transaction(func(tx *ClusterTx) error {
+		_, err := tx.tx.Exec("UPDATE instances SET stateful=? WHERE id=?", statefulInt, id)
+		return err
+	})
 }
 
 // AddProfilesToInstance associates the instance with the given ID with the
diff --git a/lxd/instance_post.go b/lxd/instance_post.go
index 448ece18fb..88872df4a2 100644
--- a/lxd/instance_post.go
+++ b/lxd/instance_post.go
@@ -373,26 +373,31 @@ func containerPostClusteringMigrate(d *Daemon, c instance.Instance, oldName, new
 		}
 
 		// Restore the original value of "volatile.apply_template"
-		id, err := d.cluster.GetInstanceID(c.Project(), destName)
-		if err != nil {
-			return errors.Wrap(err, "Failed to get ID of moved instance")
-		}
-
-		err = d.cluster.DeleteInstanceConfigKey(id, "volatile.apply_template")
-		if err != nil {
-			return errors.Wrap(err, "Failed to remove volatile.apply_template config key")
-		}
-
-		if origVolatileApplyTemplate != "" {
-			config := map[string]string{
-				"volatile.apply_template": origVolatileApplyTemplate,
+		project := c.Project()
+		err = d.cluster.Transaction(func(tx *db.ClusterTx) error {
+			id, err := tx.GetInstanceID(project, destName)
+			if err != nil {
+				return errors.Wrap(err, "Failed to get ID of moved instance")
 			}
-			err := d.cluster.Transaction(func(tx *db.ClusterTx) error {
-				return tx.CreateInstanceConfig(id, config)
-			})
+			err = tx.DeleteInstanceConfigKey(id, "volatile.apply_template")
 			if err != nil {
-				return errors.Wrap(err, "Failed to set volatile.apply_template config key")
+				return errors.Wrap(err, "Failed to remove volatile.apply_template config key")
+			}
+
+			if origVolatileApplyTemplate != "" {
+				config := map[string]string{
+					"volatile.apply_template": origVolatileApplyTemplate,
+				}
+				err = tx.CreateInstanceConfig(int(id), config)
+				if err != nil {
+					return errors.Wrap(err, "Failed to set volatile.apply_template config key")
+				}
 			}
+
+			return nil
+		})
+		if err != nil {
+			return err
 		}
 
 		return nil

From 1968db8fe5528d5c06ced1ba3d3d29d1ec289768 Mon Sep 17 00:00:00 2001
From: Free Ekanayaka <free.ekanayaka at canonical.com>
Date: Tue, 26 May 2020 15:52:38 +0100
Subject: [PATCH 2/2] lxd/db: Use query.SelectStrings helper in
 LegacyContainersList

Signed-off-by: Free Ekanayaka <free.ekanayaka at canonical.com>
---
 lxd/db/instances.go | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/lxd/db/instances.go b/lxd/db/instances.go
index 15c0ebd977..ca48f00f06 100644
--- a/lxd/db/instances.go
+++ b/lxd/db/instances.go
@@ -766,17 +766,16 @@ INSERT INTO instances_profiles (instance_id, profile_id, apply_order)
 // use it for new code.
 func (c *Cluster) LegacyContainersList() ([]string, error) {
 	q := fmt.Sprintf("SELECT name FROM instances WHERE type=? ORDER BY name")
-	inargs := []interface{}{instancetype.Container}
-	var container string
-	outfmt := []interface{}{container}
-	result, err := queryScan(c, q, inargs, outfmt)
-	if err != nil {
-		return nil, err
-	}
 
 	var ret []string
-	for _, container := range result {
-		ret = append(ret, container[0].(string))
+
+	err := c.Transaction(func(tx *ClusterTx) error {
+		var err error
+		ret, err = query.SelectStrings(tx.tx, q, instancetype.Container)
+		return err
+	})
+	if err != nil {
+		return nil, err
 	}
 
 	return ret, nil


More information about the lxc-devel mailing list