[lxc-devel] [lxd/master] lxd: Add clustering_fix_raft_address_zero patch to fix node with "0" as address

freeekanayaka on Github lxc-bot at linuxcontainers.org
Thu Jul 9 16:43:50 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 363 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200709/afd915db/attachment.bin>
-------------- next part --------------
From c495f3ecf068142ea97f2177444e0c2a49dbb000 Mon Sep 17 00:00:00 2001
From: Free Ekanayaka <free.ekanayaka at canonical.com>
Date: Thu, 9 Jul 2020 18:13:10 +0200
Subject: [PATCH] lxd: Add clustering_fix_raft_address_zero patch to fix node
 with "0" as address

Signed-off-by: Free Ekanayaka <free.ekanayaka at canonical.com>
---
 lxd/cluster/gateway.go |  2 +-
 lxd/db/node/schema.go  |  2 +-
 lxd/db/node/update.go  | 51 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/lxd/cluster/gateway.go b/lxd/cluster/gateway.go
index face315dde..b57db673c5 100644
--- a/lxd/cluster/gateway.go
+++ b/lxd/cluster/gateway.go
@@ -878,7 +878,7 @@ func (g *Gateway) currentRaftNodes() ([]db.RaftNode, error) {
 // Translate a raft address to a node address. They are always the same except
 // for the bootstrap node, which has address "1".
 func (g *Gateway) nodeAddress(raftAddress string) (string, error) {
-	if raftAddress != "1" {
+	if raftAddress != "1" && raftAddress != "0" {
 		return raftAddress, nil
 	}
 	var address string
diff --git a/lxd/db/node/schema.go b/lxd/db/node/schema.go
index aaf0e09676..907e3ba5e9 100644
--- a/lxd/db/node/schema.go
+++ b/lxd/db/node/schema.go
@@ -25,5 +25,5 @@ CREATE TABLE raft_nodes (
     UNIQUE (address)
 );
 
-INSERT INTO schema (version, updated_at) VALUES (39, strftime("%s"))
+INSERT INTO schema (version, updated_at) VALUES (40, strftime("%s"))
 `
diff --git a/lxd/db/node/update.go b/lxd/db/node/update.go
index b5787d0dd9..4a1c35a9d4 100644
--- a/lxd/db/node/update.go
+++ b/lxd/db/node/update.go
@@ -96,6 +96,7 @@ var updates = map[int]schema.Update{
 	37: updateFromV36,
 	38: updateFromV37,
 	39: updateFromV38,
+	40: updateFromV39,
 }
 
 // UpdateFromPreClustering is the last schema version where clustering support
@@ -104,6 +105,56 @@ const UpdateFromPreClustering = 36
 
 // Schema updates begin here
 
+// Fix the address of the bootstrap node being set to "0" in the raft_nodes
+// table.
+func updateFromV39(tx *sql.Tx) error {
+	nodes := []struct {
+		ID      uint64
+		Address string
+	}{}
+	dest := func(i int) []interface{} {
+		nodes = append(nodes, struct {
+			ID      uint64
+			Address string
+		}{})
+		return []interface{}{&nodes[i].ID, &nodes[i].Address}
+	}
+	stmt, err := tx.Prepare("SELECT id, address FROM raft_nodes")
+	if err != nil {
+		return err
+	}
+
+	defer stmt.Close()
+
+	err = query.SelectObjects(stmt, dest)
+	if err != nil {
+		return errors.Wrap(err, "Failed to fetch raft nodes")
+	}
+
+	if len(nodes) != 1 {
+		return nil
+	}
+
+	info := nodes[0]
+	if info.ID != 1 || info.Address != "0" {
+		return nil
+	}
+
+	config, err := query.SelectConfig(tx, "config", "")
+	if err != nil {
+		return err
+	}
+	address := config["cluster.https_address"]
+	if address != "" {
+		_, err := tx.Exec("UPDATE raft_nodes SET address=? WHERE id=1", address)
+		if err != nil {
+			return err
+		}
+	}
+
+	return nil
+}
+
 // Add role column to raft_nodes table. All existing entries will have role "0"
 // which means voter.
 func updateFromV38(tx *sql.Tx) error {


More information about the lxc-devel mailing list