|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2026 Cozystack contributors. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package placer_test |
| 20 | + |
| 21 | +import ( |
| 22 | + "testing" |
| 23 | + |
| 24 | + apiv1 "github.com/cozystack/blockstor/pkg/api/v1" |
| 25 | + "github.com/cozystack/blockstor/pkg/placer" |
| 26 | + "github.com/cozystack/blockstor/pkg/store" |
| 27 | +) |
| 28 | + |
| 29 | +// Bug 038 (release gate): the controller-side Place callers |
| 30 | +// (ResourceGroup apply, RG rebalance, node replacement) feed the |
| 31 | +// placer the RAW RG SelectFilter — no snapshot-node constraint, no |
| 32 | +// provider pin. On stand `big` that landed a FILE_THIN-sourced clone |
| 33 | +// replica on a ZFS pool; the satellite then piped the source's |
| 34 | +// FILE_THIN snapshot stream into `zfs recv`, which looped forever on |
| 35 | +// `cannot receive: invalid stream (bad magic number)` and the clone |
| 36 | +// never reached UpToDate. |
| 37 | +// |
| 38 | +// These tests pin the placer-internal restore-source constraint: |
| 39 | +// Place() on an RD carrying the BlockstorRestoreFromSnapshot marker |
| 40 | +// must (a) only consider the snapshot's nodes when the caller didn't |
| 41 | +// pin any, and (b) only consider pools whose ProviderKind matches the |
| 42 | +// source replica's — even when a different-backend pool is roomier |
| 43 | +// and would win the capacity-weighted ranking. |
| 44 | + |
| 45 | +// seedRestoreMarkedRD builds the Bug 038 repro shape: a deployed |
| 46 | +// FILE_THIN source RD (replicas on n1/n2 in pool "stand"), its |
| 47 | +// internal clone snapshot on those nodes, a restore-marked target RD, |
| 48 | +// and a roomier ZFS_THIN decoy pool on every node. |
| 49 | +func seedRestoreMarkedRD(t *testing.T, st store.Store) { |
| 50 | + t.Helper() |
| 51 | + |
| 52 | + ctx := t.Context() |
| 53 | + |
| 54 | + for _, n := range []string{"n1", "n2", "n3"} { |
| 55 | + if err := st.Nodes().Create(ctx, &apiv1.Node{Name: n, Type: apiv1.NodeTypeSatellite}); err != nil { |
| 56 | + t.Fatalf("seed node %s: %v", n, err) |
| 57 | + } |
| 58 | + |
| 59 | + if err := st.StoragePools().Create(ctx, &apiv1.StoragePool{ |
| 60 | + NodeName: n, |
| 61 | + StoragePoolName: "stand", |
| 62 | + ProviderKind: apiv1.StoragePoolKindFileThin, |
| 63 | + FreeCapacity: 1000, |
| 64 | + TotalCapacity: 10000, |
| 65 | + }); err != nil { |
| 66 | + t.Fatalf("seed FILE_THIN pool on %s: %v", n, err) |
| 67 | + } |
| 68 | + |
| 69 | + // The decoy: same nodes, far more free space AND a far |
| 70 | + // better Free/Total ratio (0.9 vs 0.1) — the capacity- |
| 71 | + // weighted ranking picks it whenever the provider pin is |
| 72 | + // absent (see TestPlaceUnmarkedRDKeepsFreePlacement). |
| 73 | + if err := st.StoragePools().Create(ctx, &apiv1.StoragePool{ |
| 74 | + NodeName: n, |
| 75 | + StoragePoolName: "zfs-thin", |
| 76 | + ProviderKind: apiv1.StoragePoolKindZFSThin, |
| 77 | + FreeCapacity: 900000, |
| 78 | + TotalCapacity: 1000000, |
| 79 | + }); err != nil { |
| 80 | + t.Fatalf("seed ZFS decoy pool on %s: %v", n, err) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if err := st.ResourceDefinitions().Create(ctx, &apiv1.ResourceDefinition{Name: "src"}); err != nil { |
| 85 | + t.Fatalf("seed source RD: %v", err) |
| 86 | + } |
| 87 | + |
| 88 | + for _, n := range []string{"n1", "n2"} { |
| 89 | + if err := st.Resources().Create(ctx, &apiv1.Resource{ |
| 90 | + Name: "src", NodeName: n, |
| 91 | + Props: map[string]string{"StorPoolName": "stand"}, |
| 92 | + }); err != nil { |
| 93 | + t.Fatalf("seed source replica on %s: %v", n, err) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if err := st.Snapshots().Create(ctx, &apiv1.Snapshot{ |
| 98 | + Name: "clone-target", |
| 99 | + ResourceName: "src", |
| 100 | + Nodes: []string{"n1", "n2"}, |
| 101 | + VolumeDefinitions: []apiv1.SnapshotVolumeDef{ |
| 102 | + {VolumeNumber: 0, SizeKib: 64}, |
| 103 | + }, |
| 104 | + }); err != nil { |
| 105 | + t.Fatalf("seed snapshot: %v", err) |
| 106 | + } |
| 107 | + |
| 108 | + if err := st.ResourceDefinitions().Create(ctx, &apiv1.ResourceDefinition{ |
| 109 | + Name: "target", |
| 110 | + Props: map[string]string{"BlockstorRestoreFromSnapshot": "src:clone-target"}, |
| 111 | + }); err != nil { |
| 112 | + t.Fatalf("seed restore-marked target RD: %v", err) |
| 113 | + } |
| 114 | + |
| 115 | + if err := st.VolumeDefinitions().Create(ctx, "target", &apiv1.VolumeDefinition{ |
| 116 | + VolumeNumber: 0, SizeKib: 64, |
| 117 | + }); err != nil { |
| 118 | + t.Fatalf("seed target VD: %v", err) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// TestPlaceConstrainsRestoreMarkedRDToSourceBackend drives Place with |
| 123 | +// the exact filter shape the RG reconcilers use (bare PlaceCount, no |
| 124 | +// pins) and asserts the replica lands on the source's FILE_THIN pool |
| 125 | +// on a snapshot node — not on the roomier ZFS decoy. |
| 126 | +func TestPlaceConstrainsRestoreMarkedRDToSourceBackend(t *testing.T) { |
| 127 | + t.Parallel() |
| 128 | + |
| 129 | + st := store.NewInMemory() |
| 130 | + seedRestoreMarkedRD(t, st) |
| 131 | + |
| 132 | + placed, want, err := placer.New(st).Place(t.Context(), "target", &apiv1.AutoSelectFilter{ |
| 133 | + PlaceCount: 1, |
| 134 | + }) |
| 135 | + if err != nil { |
| 136 | + t.Fatalf("Place: %v", err) |
| 137 | + } |
| 138 | + |
| 139 | + if placed != 1 || want != 1 { |
| 140 | + t.Fatalf("placed/want: got %d/%d, want 1/1", placed, want) |
| 141 | + } |
| 142 | + |
| 143 | + resList, err := st.Resources().ListByDefinition(t.Context(), "target") |
| 144 | + if err != nil { |
| 145 | + t.Fatalf("list target replicas: %v", err) |
| 146 | + } |
| 147 | + |
| 148 | + if len(resList) != 1 { |
| 149 | + t.Fatalf("target replicas: got %d, want 1", len(resList)) |
| 150 | + } |
| 151 | + |
| 152 | + res := resList[0] |
| 153 | + if res.NodeName != "n1" && res.NodeName != "n2" { |
| 154 | + t.Errorf("replica node: got %q, want a snapshot node (n1/n2)", res.NodeName) |
| 155 | + } |
| 156 | + |
| 157 | + if pool := res.Props["StorPoolName"]; pool != "stand" { |
| 158 | + t.Errorf("replica pool: got %q, want the source's FILE_THIN pool %q "+ |
| 159 | + "(a ZFS placement feeds the FILE_THIN snapshot stream into "+ |
| 160 | + "`zfs recv` → bad magic loop)", pool, "stand") |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +// TestPlaceRestoreConstraintDoesNotLeakIntoCallerFilter pins that the |
| 165 | +// constraint operates on a copy: RG reconcilers reuse one filter |
| 166 | +// value across sibling RDs, so a restore-marked RD must not pollute |
| 167 | +// the next sibling's candidate set. |
| 168 | +func TestPlaceRestoreConstraintDoesNotLeakIntoCallerFilter(t *testing.T) { |
| 169 | + t.Parallel() |
| 170 | + |
| 171 | + st := store.NewInMemory() |
| 172 | + seedRestoreMarkedRD(t, st) |
| 173 | + |
| 174 | + filter := apiv1.AutoSelectFilter{PlaceCount: 1} |
| 175 | + |
| 176 | + _, _, err := placer.New(st).Place(t.Context(), "target", &filter) |
| 177 | + if err != nil { |
| 178 | + t.Fatalf("Place: %v", err) |
| 179 | + } |
| 180 | + |
| 181 | + if len(filter.NodeNameList) != 0 || len(filter.ProviderList) != 0 { |
| 182 | + t.Errorf("caller filter mutated: NodeNameList=%v ProviderList=%v, want both empty", |
| 183 | + filter.NodeNameList, filter.ProviderList) |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +// TestPlaceUnmarkedRDKeepsFreePlacement is the negative control: an |
| 188 | +// RD without the restore marker keeps the unconstrained behaviour |
| 189 | +// (the roomier ZFS decoy wins the ranking). |
| 190 | +func TestPlaceUnmarkedRDKeepsFreePlacement(t *testing.T) { |
| 191 | + t.Parallel() |
| 192 | + |
| 193 | + st := store.NewInMemory() |
| 194 | + seedRestoreMarkedRD(t, st) |
| 195 | + |
| 196 | + if err := st.ResourceDefinitions().Create(t.Context(), &apiv1.ResourceDefinition{Name: "plain"}); err != nil { |
| 197 | + t.Fatalf("seed plain RD: %v", err) |
| 198 | + } |
| 199 | + |
| 200 | + placed, _, err := placer.New(st).Place(t.Context(), "plain", &apiv1.AutoSelectFilter{ |
| 201 | + PlaceCount: 1, |
| 202 | + }) |
| 203 | + if err != nil { |
| 204 | + t.Fatalf("Place: %v", err) |
| 205 | + } |
| 206 | + |
| 207 | + if placed != 1 { |
| 208 | + t.Fatalf("placed: got %d, want 1", placed) |
| 209 | + } |
| 210 | + |
| 211 | + resList, err := st.Resources().ListByDefinition(t.Context(), "plain") |
| 212 | + if err != nil { |
| 213 | + t.Fatalf("list plain replicas: %v", err) |
| 214 | + } |
| 215 | + |
| 216 | + if len(resList) != 1 { |
| 217 | + t.Fatalf("plain replicas: got %d, want 1", len(resList)) |
| 218 | + } |
| 219 | + |
| 220 | + if pool := resList[0].Props["StorPoolName"]; pool != "zfs-thin" { |
| 221 | + t.Errorf("unmarked RD pool: got %q, want the roomiest pool %q "+ |
| 222 | + "(free placement must stay unconstrained)", pool, "zfs-thin") |
| 223 | + } |
| 224 | +} |
0 commit comments