From 555271c5eff1596d4310bec6fcce4faa0e5fb151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20Peliz=C3=A4us?= Date: Fri, 11 Aug 2023 17:10:44 +0200 Subject: [PATCH 1/2] api/instance: Fix instance movement node selection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When moving an instance to the same cluster target group it already belongs to, another node needs to be picked in case it currently hosts the instance and got selected due to least amount of instances. Signed-off-by: Julian Pelizäus --- lxd/instance_post.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lxd/instance_post.go b/lxd/instance_post.go index 708defb85d9f..15dea5585144 100644 --- a/lxd/instance_post.go +++ b/lxd/instance_post.go @@ -260,8 +260,18 @@ func instancePost(d *Daemon, r *http.Request) response.Response { // If no member was selected yet, pick the member with the least number of instances. if targetMemberInfo == nil { + var filteredCandidateMembers []db.NodeInfo + + // The instance might already be placed on the node with least number of instances. + // Therefore remove it from the list of possible candidates if existent. + for _, candidateMember := range candidateMembers { + if candidateMember.Name != inst.Location() { + filteredCandidateMembers = append(filteredCandidateMembers, candidateMember) + } + } + err := s.DB.Cluster.Transaction(r.Context(), func(ctx context.Context, tx *db.ClusterTx) error { - targetMemberInfo, err = tx.GetNodeWithLeastInstances(ctx, candidateMembers) + targetMemberInfo, err = tx.GetNodeWithLeastInstances(ctx, filteredCandidateMembers) return err }) if err != nil { From 58e3e9be9489f7592382c87ea326121bb7c9fdf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20Peliz=C3=A4us?= Date: Fri, 11 Aug 2023 17:32:59 +0200 Subject: [PATCH 2/2] test/migration: Check instance node when moving within cluster group MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julian Pelizäus --- test/suites/clustering_move.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/suites/clustering_move.sh b/test/suites/clustering_move.sh index 80a70b53d30f..3ac88099ff8e 100644 --- a/test/suites/clustering_move.sh +++ b/test/suites/clustering_move.sh @@ -52,8 +52,12 @@ test_clustering_move() { LXD_DIR="${LXD_ONE_DIR}" lxc info c1 | grep -q "Location: node1" # c1 can be moved within the same cluster group if it has multiple members + current_location="$(LXD_DIR="${LXD_ONE_DIR}" lxc query /1.0/instances/c1 | jq -r '.location')" LXD_DIR="${LXD_ONE_DIR}" lxc move c1 --target=@default + LXD_DIR="${LXD_ONE_DIR}" lxc query /1.0/instances/c1 | jq -re ".location != \"$current_location\"" + current_location="$(LXD_DIR="${LXD_ONE_DIR}" lxc query /1.0/instances/c1 | jq -r '.location')" LXD_DIR="${LXD_ONE_DIR}" lxc move c1 --target=@default + LXD_DIR="${LXD_ONE_DIR}" lxc query /1.0/instances/c1 | jq -re ".location != \"$current_location\"" # c1 cannot be moved within the same cluster group if it has a single member LXD_DIR="${LXD_ONE_DIR}" lxc move c1 --target=@foobar3