[lxc-devel] [lxd/master] lxd/db: Flush any leftover operation on startup

stgraber on Github lxc-bot at linuxcontainers.org
Mon Sep 30 01:56:42 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/20190929/96ec5949/attachment.bin>
-------------- next part --------------
From b91cfd8036069ec97714a0d7b64a54de3c7e8010 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Sun, 29 Sep 2019 21:55:35 -0400
Subject: [PATCH] lxd/db: Flush any leftover operation on startup
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/db/db.go         | 36 ++++++++++++++++++++++++++----------
 lxd/db/operations.go | 10 ++++++++++
 2 files changed, 36 insertions(+), 10 deletions(-)

diff --git a/lxd/db/db.go b/lxd/db/db.go
index 40fd7ad876..3c769fd623 100644
--- a/lxd/db/db.go
+++ b/lxd/db/db.go
@@ -267,24 +267,40 @@ func OpenCluster(name string, store driver.NodeStore, address, dir string, timeo
 		stmts: stmts,
 	}
 
-	// Figure out the ID of this node.
 	err = cluster.Transaction(func(tx *ClusterTx) error {
+		// Figure out the ID of this node.
 		nodes, err := tx.Nodes()
 		if err != nil {
-			return errors.Wrap(err, "failed to fetch nodes")
+			return errors.Wrap(err, "Failed to fetch nodes")
 		}
+
+		nodeID := int64(-1)
 		if len(nodes) == 1 && nodes[0].Address == "0.0.0.0" {
 			// We're not clustered
-			cluster.NodeID(1)
-			return nil
-		}
-		for _, node := range nodes {
-			if node.Address == address {
-				cluster.nodeID = node.ID
-				return nil
+			nodeID = 1
+		} else {
+			for _, node := range nodes {
+				if node.Address == address {
+					nodeID = node.ID
+					break
+				}
 			}
 		}
-		return fmt.Errorf("no node registered with address %s", address)
+
+		if nodeID < 0 {
+			return fmt.Errorf("No node registered with address %s", address)
+		}
+
+		// Set the local node ID
+		cluster.NodeID(nodeID)
+
+		// Delete any operation tied to this node
+		err = tx.OperationFlush(nodeID)
+		if err != nil {
+			return err
+		}
+
+		return nil
 	})
 	if err != nil {
 		return nil, err
diff --git a/lxd/db/operations.go b/lxd/db/operations.go
index 58e697ee53..e3c3e564c0 100644
--- a/lxd/db/operations.go
+++ b/lxd/db/operations.go
@@ -315,6 +315,16 @@ func (c *ClusterTx) OperationRemove(uuid string) error {
 	return nil
 }
 
+// OperationFlush removes all operations for the given node.
+func (c *ClusterTx) OperationFlush(nodeID int64) error {
+	_, err := c.tx.Exec("DELETE FROM operations WHERE node_id=?", nodeID)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
 // Operations returns all operations in the cluster, filtered by the given clause.
 func (c *ClusterTx) operations(where string, args ...interface{}) ([]Operation, error) {
 	operations := []Operation{}


More information about the lxc-devel mailing list