[lxc-devel] [lxd/master] lxd/containers: Optimize snapshot retrieval

stgraber on Github lxc-bot at linuxcontainers.org
Tue Apr 30 22:00:46 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 370 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190430/25f8bb48/attachment.bin>
-------------- next part --------------
From 7fdc67e3e82bf0a6d8c25f852a3537388a0f2d5f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 30 Apr 2019 17:58:47 -0400
Subject: [PATCH] lxd/containers: Optimize snapshot retrieval
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Closes #5678

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

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index 68b238e9c6..9652702dcc 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -3312,21 +3312,26 @@ func (c *containerLXC) RenderState() (*api.ContainerState, error) {
 }
 
 func (c *containerLXC) Snapshots() ([]container, error) {
+	var snaps []db.Container
+
 	// Get all the snapshots
-	snaps, err := c.state.Cluster.ContainerGetSnapshots(c.Project(), c.name)
+	err := c.state.Cluster.Transaction(func(tx *db.ClusterTx) error {
+		var err error
+		snaps, err = tx.ContainerGetSnapshotsFull(c.Project(), c.name)
+		if err != nil {
+			return err
+		}
+
+		return nil
+	})
 	if err != nil {
 		return nil, err
 	}
 
 	// Build the snapshot list
-	containers := []container{}
-	for _, snapName := range snaps {
-		snap, err := containerLoadByProjectAndName(c.state, c.project, snapName)
-		if err != nil {
-			return nil, err
-		}
-
-		containers = append(containers, snap)
+	containers, err := containerLoadAllInternal(snaps, c.state)
+	if err != nil {
+		return nil, err
 	}
 
 	return containers, nil
diff --git a/lxd/db/containers.go b/lxd/db/containers.go
index 111bb8ff01..f32e71296e 100644
--- a/lxd/db/containers.go
+++ b/lxd/db/containers.go
@@ -85,6 +85,7 @@ type ContainerFilter struct {
 	Project string
 	Name    string
 	Node    string
+	Parent  string
 	Type    int
 }
 
@@ -879,6 +880,32 @@ WHERE projects.name=? AND containers.type=? AND SUBSTR(containers.name,1,?)=?
 	return result, nil
 }
 
+// ContainerGetSnapshotsFull returns all container objects for snapshots of a given container
+func (c *ClusterTx) ContainerGetSnapshotsFull(project string, name string) ([]Container, error) {
+	filter := ContainerFilter{
+		Parent:  name,
+		Project: project,
+		Type:    int(CTypeSnapshot),
+	}
+
+	// FIXME: temporary until the Parent filter works
+	cts, err := c.ContainerList(filter)
+	if err != nil {
+		return nil, err
+	}
+
+	filteredCts := []Container{}
+	for _, ct := range cts {
+		if strings.Split(ct.Name, "/")[0] != name {
+			continue
+		}
+
+		filteredCts = append(filteredCts, ct)
+	}
+
+	return filteredCts, nil
+}
+
 // ContainerNextSnapshot returns the index the next snapshot of the container
 // with the given name and pattern should have.
 func (c *Cluster) ContainerNextSnapshot(project string, name string, pattern string) int {


More information about the lxc-devel mailing list