mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
G9D: add placement planner candidates
This commit is contained in:
1 parent
1e4106e4b1
commit
ebae9e2369
2 files changed
+340
No files matched your search
@@ -0,0 +1,132 @@
|
||||
package lifecycle
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
const (
|
||||
PlacementSourceBlankPool = "blank_pool"
|
||||
PlacementSourceExistingReplica = "existing_replica"
|
||||
)
|
||||
|
||||
// PlacementCandidate is planner output. It is a candidate for future
|
||||
// controller action, not an assignment and not readiness.
|
||||
type PlacementCandidate struct {
|
||||
VolumeID string
|
||||
ServerID string
|
||||
PoolID string
|
||||
ReplicaID string
|
||||
Source string
|
||||
}
|
||||
|
||||
// PlacementConflict records inventory that blocks automatic placement.
|
||||
type PlacementConflict struct {
|
||||
VolumeID string
|
||||
ServerID string
|
||||
Reason string
|
||||
}
|
||||
|
||||
// PlacementPlan summarizes candidate slots for one desired volume.
|
||||
type PlacementPlan struct {
|
||||
VolumeID string
|
||||
DesiredRF int
|
||||
Candidates []PlacementCandidate
|
||||
Conflicts []PlacementConflict
|
||||
}
|
||||
|
||||
// PlanPlacement converts desired volume state plus node inventory into
|
||||
// placement candidates. It deliberately stops before authority: callers must
|
||||
// pass the plan through a controller/publisher seam before any assignment can
|
||||
// exist.
|
||||
func PlanPlacement(volume VolumeRecord, nodes []NodeRegistration) PlacementPlan {
|
||||
spec := volume.Spec
|
||||
plan := PlacementPlan{
|
||||
VolumeID: spec.VolumeID,
|
||||
DesiredRF: spec.ReplicationFactor,
|
||||
}
|
||||
seenServers := make(map[string]bool)
|
||||
nodes = append([]NodeRegistration(nil), nodes...)
|
||||
sort.Slice(nodes, func(i, j int) bool {
|
||||
return nodes[i].ServerID < nodes[j].ServerID
|
||||
})
|
||||
for _, node := range nodes {
|
||||
if seenServers[node.ServerID] {
|
||||
plan.Conflicts = append(plan.Conflicts, PlacementConflict{
|
||||
VolumeID: spec.VolumeID,
|
||||
ServerID: node.ServerID,
|
||||
Reason: "duplicate server inventory",
|
||||
})
|
||||
continue
|
||||
}
|
||||
seenServers[node.ServerID] = true
|
||||
if existing, ok := matchingReplica(spec.VolumeID, node.Replicas); ok {
|
||||
if existing.SizeBytes != spec.SizeBytes {
|
||||
plan.Conflicts = append(plan.Conflicts, PlacementConflict{
|
||||
VolumeID: spec.VolumeID,
|
||||
ServerID: node.ServerID,
|
||||
Reason: fmt.Sprintf("existing replica %s size %d != desired %d",
|
||||
existing.ReplicaID, existing.SizeBytes, spec.SizeBytes),
|
||||
})
|
||||
continue
|
||||
}
|
||||
plan.Candidates = append(plan.Candidates, PlacementCandidate{
|
||||
VolumeID: spec.VolumeID,
|
||||
ServerID: node.ServerID,
|
||||
ReplicaID: existing.ReplicaID,
|
||||
Source: PlacementSourceExistingReplica,
|
||||
})
|
||||
continue
|
||||
}
|
||||
if pool, ok := firstPoolWithCapacity(spec.SizeBytes, node.Pools); ok {
|
||||
plan.Candidates = append(plan.Candidates, PlacementCandidate{
|
||||
VolumeID: spec.VolumeID,
|
||||
ServerID: node.ServerID,
|
||||
PoolID: pool.PoolID,
|
||||
Source: PlacementSourceBlankPool,
|
||||
})
|
||||
continue
|
||||
}
|
||||
plan.Conflicts = append(plan.Conflicts, PlacementConflict{
|
||||
VolumeID: spec.VolumeID,
|
||||
ServerID: node.ServerID,
|
||||
Reason: "no matching replica and no pool with enough free capacity",
|
||||
})
|
||||
}
|
||||
if len(plan.Candidates) > spec.ReplicationFactor {
|
||||
plan.Candidates = plan.Candidates[:spec.ReplicationFactor]
|
||||
}
|
||||
return plan
|
||||
}
|
||||
|
||||
// EnoughCandidates reports whether the plan has enough candidate slots to
|
||||
// satisfy the desired replication factor. This is a planner predicate only;
|
||||
// it must not be treated as authority or replica_ready.
|
||||
func (p PlacementPlan) EnoughCandidates() bool {
|
||||
return p.DesiredRF > 0 && len(p.Candidates) >= p.DesiredRF
|
||||
}
|
||||
|
||||
func matchingReplica(volumeID string, replicas []ReplicaInventory) (ReplicaInventory, bool) {
|
||||
for _, replica := range replicas {
|
||||
if replica.VolumeID == volumeID {
|
||||
return replica, true
|
||||
}
|
||||
}
|
||||
return ReplicaInventory{}, false
|
||||
}
|
||||
|
||||
func firstPoolWithCapacity(sizeBytes uint64, pools []StoragePool) (StoragePool, bool) {
|
||||
pools = append([]StoragePool(nil), pools...)
|
||||
sort.Slice(pools, func(i, j int) bool {
|
||||
if pools[i].FreeBytes == pools[j].FreeBytes {
|
||||
return pools[i].PoolID < pools[j].PoolID
|
||||
}
|
||||
return pools[i].FreeBytes > pools[j].FreeBytes
|
||||
})
|
||||
for _, pool := range pools {
|
||||
if pool.FreeBytes >= sizeBytes {
|
||||
return pool, true
|
||||
}
|
||||
}
|
||||
return StoragePool{}, false
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
package lifecycle
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPlanPlacement_BlankCapacityProducesCandidatesOnly(t *testing.T) {
|
||||
volume := VolumeRecord{Spec: VolumeSpec{
|
||||
VolumeID: "vol-a",
|
||||
SizeBytes: 1 << 20,
|
||||
ReplicationFactor: 2,
|
||||
}}
|
||||
nodes := []NodeRegistration{
|
||||
{
|
||||
ServerID: "node-b",
|
||||
Addr: "127.0.0.1:9102",
|
||||
Pools: []StoragePool{{
|
||||
PoolID: "pool-b",
|
||||
TotalBytes: 1 << 30,
|
||||
FreeBytes: 1 << 29,
|
||||
BlockSize: 4096,
|
||||
}},
|
||||
},
|
||||
{
|
||||
ServerID: "node-a",
|
||||
Addr: "127.0.0.1:9101",
|
||||
Pools: []StoragePool{{
|
||||
PoolID: "pool-a",
|
||||
TotalBytes: 1 << 30,
|
||||
FreeBytes: 1 << 30,
|
||||
BlockSize: 4096,
|
||||
}},
|
||||
},
|
||||
}
|
||||
plan := PlanPlacement(volume, nodes)
|
||||
if !plan.EnoughCandidates() {
|
||||
t.Fatalf("plan should have enough candidates: %+v", plan)
|
||||
}
|
||||
if len(plan.Candidates) != 2 {
|
||||
t.Fatalf("candidate count=%d want 2", len(plan.Candidates))
|
||||
}
|
||||
if plan.Candidates[0].ServerID != "node-a" || plan.Candidates[1].ServerID != "node-b" {
|
||||
t.Fatalf("candidates should be deterministic by server id: %+v", plan.Candidates)
|
||||
}
|
||||
for _, c := range plan.Candidates {
|
||||
if c.Source != PlacementSourceBlankPool {
|
||||
t.Fatalf("source=%q want %q", c.Source, PlacementSourceBlankPool)
|
||||
}
|
||||
if c.ReplicaID != "" {
|
||||
t.Fatalf("blank-pool candidate must not assign replica id: %+v", c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanPlacement_ExistingReplicaIsCandidateButNotReady(t *testing.T) {
|
||||
volume := VolumeRecord{Spec: VolumeSpec{
|
||||
VolumeID: "vol-a",
|
||||
SizeBytes: 1 << 20,
|
||||
ReplicationFactor: 1,
|
||||
}}
|
||||
plan := PlanPlacement(volume, []NodeRegistration{{
|
||||
ServerID: "node-a",
|
||||
Addr: "127.0.0.1:9101",
|
||||
Replicas: []ReplicaInventory{{
|
||||
VolumeID: "vol-a",
|
||||
ReplicaID: "r2",
|
||||
StoreUUID: "store-1",
|
||||
SizeBytes: 1 << 20,
|
||||
DurableLSN: 99,
|
||||
State: "existing",
|
||||
}},
|
||||
}})
|
||||
if !plan.EnoughCandidates() {
|
||||
t.Fatalf("plan should have existing candidate: %+v", plan)
|
||||
}
|
||||
got := plan.Candidates[0]
|
||||
if got.Source != PlacementSourceExistingReplica || got.ReplicaID != "r2" {
|
||||
t.Fatalf("existing candidate mismatch: %+v", got)
|
||||
}
|
||||
typ := mustParseStruct(t, "planner.go", "PlacementCandidate")
|
||||
if _, ok := typ.Fields["Ready"]; ok {
|
||||
t.Fatal("placement candidate must not carry Ready")
|
||||
}
|
||||
if _, ok := typ.Fields["Primary"]; ok {
|
||||
t.Fatal("placement candidate must not carry Primary")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanPlacement_ExistingReplicaSizeMismatchConflicts(t *testing.T) {
|
||||
volume := VolumeRecord{Spec: VolumeSpec{
|
||||
VolumeID: "vol-a",
|
||||
SizeBytes: 1 << 20,
|
||||
ReplicationFactor: 1,
|
||||
}}
|
||||
plan := PlanPlacement(volume, []NodeRegistration{{
|
||||
ServerID: "node-a",
|
||||
Addr: "127.0.0.1:9101",
|
||||
Pools: []StoragePool{{
|
||||
PoolID: "pool-a",
|
||||
TotalBytes: 1 << 30,
|
||||
FreeBytes: 1 << 30,
|
||||
BlockSize: 4096,
|
||||
}},
|
||||
Replicas: []ReplicaInventory{{
|
||||
VolumeID: "vol-a",
|
||||
ReplicaID: "r2",
|
||||
StoreUUID: "store-1",
|
||||
SizeBytes: 2 << 20,
|
||||
State: "existing",
|
||||
}},
|
||||
}})
|
||||
if plan.EnoughCandidates() {
|
||||
t.Fatalf("conflicting existing replica must not be candidate: %+v", plan)
|
||||
}
|
||||
if len(plan.Conflicts) != 1 {
|
||||
t.Fatalf("conflict count=%d want 1: %+v", len(plan.Conflicts), plan)
|
||||
}
|
||||
if plan.Candidates != nil {
|
||||
t.Fatalf("candidates=%+v want none", plan.Candidates)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanPlacement_InsufficientCapacityDoesNotOverclaim(t *testing.T) {
|
||||
volume := VolumeRecord{Spec: VolumeSpec{
|
||||
VolumeID: "vol-a",
|
||||
SizeBytes: 1 << 20,
|
||||
ReplicationFactor: 2,
|
||||
}}
|
||||
plan := PlanPlacement(volume, []NodeRegistration{
|
||||
{
|
||||
ServerID: "node-a",
|
||||
Addr: "127.0.0.1:9101",
|
||||
Pools: []StoragePool{{
|
||||
PoolID: "pool-a",
|
||||
TotalBytes: 1 << 30,
|
||||
FreeBytes: 1 << 29,
|
||||
BlockSize: 4096,
|
||||
}},
|
||||
},
|
||||
{
|
||||
ServerID: "node-b",
|
||||
Addr: "127.0.0.1:9102",
|
||||
Pools: []StoragePool{{
|
||||
PoolID: "pool-b",
|
||||
TotalBytes: 1 << 30,
|
||||
FreeBytes: 512,
|
||||
BlockSize: 4096,
|
||||
}},
|
||||
},
|
||||
})
|
||||
if plan.EnoughCandidates() {
|
||||
t.Fatalf("plan must not claim enough candidates: %+v", plan)
|
||||
}
|
||||
if len(plan.Candidates) != 1 || len(plan.Conflicts) != 1 {
|
||||
t.Fatalf("candidate/conflict counts got %d/%d want 1/1: %+v",
|
||||
len(plan.Candidates), len(plan.Conflicts), plan)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlannerPackageDoesNotMentionAuthorityTypes(t *testing.T) {
|
||||
for _, name := range []string{"PlacementCandidate", "PlacementPlan"} {
|
||||
typ := mustParseStruct(t, "planner.go", name)
|
||||
for _, forbidden := range []string{"Epoch", "EndpointVersion", "Assignment", "Ready", "Healthy"} {
|
||||
if _, ok := typ.Fields[forbidden]; ok {
|
||||
t.Fatalf("%s must not carry %s", name, forbidden)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type parsedStruct struct {
|
||||
Fields map[string]struct{}
|
||||
}
|
||||
|
||||
func mustParseStruct(t *testing.T, fileName, structName string) parsedStruct {
|
||||
t.Helper()
|
||||
fset := token.NewFileSet()
|
||||
file, err := parser.ParseFile(fset, filepath.Clean(fileName), nil, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("parse %s: %v", fileName, err)
|
||||
}
|
||||
var out parsedStruct
|
||||
ast.Inspect(file, func(n ast.Node) bool {
|
||||
typeSpec, ok := n.(*ast.TypeSpec)
|
||||
if !ok || typeSpec.Name.Name != structName {
|
||||
return true
|
||||
}
|
||||
st, ok := typeSpec.Type.(*ast.StructType)
|
||||
if !ok {
|
||||
t.Fatalf("%s is not a struct", structName)
|
||||
}
|
||||
out.Fields = make(map[string]struct{})
|
||||
for _, field := range st.Fields.List {
|
||||
for _, name := range field.Names {
|
||||
out.Fields[name.Name] = struct{}{}
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
if out.Fields == nil {
|
||||
t.Fatalf("struct %s not found in %s", structName, fileName)
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in new issue
Block a user