mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 11:07:31 +01:00
265 lines
8.7 KiB
Go
265 lines
8.7 KiB
Go
package lifecycle
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"sync"
|
|
)
|
|
|
|
var ErrInsufficientPlacementCandidates = errors.New("lifecycle: insufficient placement candidates")
|
|
|
|
// PlacementSlotIntent is the controller-facing desired slot generated from a
|
|
// placement plan. It is still not authority: no epoch, no endpoint version,
|
|
// no readiness.
|
|
type PlacementSlotIntent struct {
|
|
ServerID string `json:"server_id"`
|
|
PoolID string `json:"pool_id,omitempty"`
|
|
ReplicaID string `json:"replica_id,omitempty"`
|
|
Source string `json:"source"`
|
|
DataAddr string `json:"data_addr,omitempty"`
|
|
CtrlAddr string `json:"ctrl_addr,omitempty"`
|
|
ISCSIListenPort int `json:"iscsi_listen_port,omitempty"`
|
|
NVMeListenPort int `json:"nvme_listen_port,omitempty"`
|
|
}
|
|
|
|
// PlacementIntent is durable controller input for one volume.
|
|
type PlacementIntent struct {
|
|
VolumeID string `json:"volume_id"`
|
|
DesiredRF int `json:"desired_rf"`
|
|
Slots []PlacementSlotIntent `json:"slots"`
|
|
}
|
|
|
|
// PlacementIntentStore persists one placement intent per volume.
|
|
type PlacementIntentStore struct {
|
|
mu sync.Mutex
|
|
dir string
|
|
records map[string]PlacementIntent
|
|
published sync.Map // committed immutable PlacementIntent values; renewal never takes mu
|
|
}
|
|
|
|
// OpenPlacementIntentStore opens or creates a placement intent store.
|
|
func OpenPlacementIntentStore(dir string) (*PlacementIntentStore, error) {
|
|
if dir == "" {
|
|
return nil, fmt.Errorf("%w: empty dir", ErrInvalidVolumeSpec)
|
|
}
|
|
dir, err := filepath.Abs(filepath.Clean(dir))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("lifecycle: resolve store path: %w", err)
|
|
}
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
return nil, fmt.Errorf("lifecycle: mkdir placement intent %q: %w", dir, err)
|
|
}
|
|
// INV-LIFECYCLE-STORE-DURABLE-001: existence does not prove durability.
|
|
// Every open syncs the full ancestor chain, also after a failed first open.
|
|
for parent := filepath.Dir(dir); ; parent = filepath.Dir(parent) {
|
|
if err := syncBindingParent(parent); err != nil {
|
|
log.Printf("lifecycle: could not sync %s (%v): the store directory entry may not survive a crash right after its creation", parent, err)
|
|
}
|
|
if parent == filepath.Dir(parent) {
|
|
break
|
|
}
|
|
}
|
|
|
|
s := &PlacementIntentStore{
|
|
dir: dir,
|
|
records: make(map[string]PlacementIntent),
|
|
}
|
|
if err := s.load(); err != nil {
|
|
return nil, err
|
|
}
|
|
for volume, rec := range s.records {
|
|
s.published.Store(volume, copyPlacementIntent(rec))
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
// ApplyPlan records placement intent from a sufficient plan. Insufficient
|
|
// plans are rejected rather than persisted as partial authority-shaped state.
|
|
func (s *PlacementIntentStore) ApplyPlan(plan PlacementPlan) (PlacementIntent, error) {
|
|
if !plan.EnoughCandidates() {
|
|
return PlacementIntent{}, ErrInsufficientPlacementCandidates
|
|
}
|
|
intent := PlacementIntent{
|
|
VolumeID: plan.VolumeID,
|
|
DesiredRF: plan.DesiredRF,
|
|
Slots: make([]PlacementSlotIntent, 0, plan.DesiredRF),
|
|
}
|
|
for _, candidate := range plan.Candidates[:plan.DesiredRF] {
|
|
intent.Slots = append(intent.Slots, PlacementSlotIntent{
|
|
ServerID: candidate.ServerID,
|
|
PoolID: candidate.PoolID,
|
|
ReplicaID: candidate.ReplicaID,
|
|
Source: candidate.Source,
|
|
DataAddr: candidate.DataAddr,
|
|
CtrlAddr: candidate.CtrlAddr,
|
|
ISCSIListenPort: candidate.ISCSIListenPort,
|
|
NVMeListenPort: candidate.NVMeListenPort,
|
|
})
|
|
}
|
|
if err := validatePlacementIntent(intent); err != nil {
|
|
return PlacementIntent{}, err
|
|
}
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if err := s.putLocked(intent); err != nil {
|
|
return PlacementIntent{}, err
|
|
}
|
|
s.records[intent.VolumeID] = intent
|
|
s.published.Store(intent.VolumeID, copyPlacementIntent(intent))
|
|
return copyPlacementIntent(intent), nil
|
|
}
|
|
|
|
// DeletePlacement removes one placement intent. Missing intents are a
|
|
// successful no-op so reconciliation cleanup is retry-safe.
|
|
func (s *PlacementIntentStore) DeletePlacement(volumeID string) error {
|
|
if err := validateVolumeID(volumeID); err != nil {
|
|
return err
|
|
}
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if err := os.Remove(s.path(volumeID)); err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
return fmt.Errorf("lifecycle: remove placement intent %s: %w", volumeID, err)
|
|
}
|
|
delete(s.records, volumeID)
|
|
s.published.Delete(volumeID)
|
|
return nil
|
|
}
|
|
|
|
// GetPlacement returns a copy of one placement intent.
|
|
func (s *PlacementIntentStore) GetPlacement(volumeID string) (PlacementIntent, bool) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
rec, ok := s.records[volumeID]
|
|
return copyPlacementIntent(rec), ok
|
|
}
|
|
|
|
// GetPlacementSnapshot reads only committed placement, without waiting for a
|
|
// writer's filesystem I/O (INV-WS-RENEW-NONBLOCKING-001). The returned slice
|
|
// is a copy: a caller cannot change the published holder membership.
|
|
func (s *PlacementIntentStore) GetPlacementSnapshot(volumeID string) (PlacementIntent, bool) {
|
|
value, ok := s.published.Load(volumeID)
|
|
if !ok {
|
|
return PlacementIntent{}, false
|
|
}
|
|
return copyPlacementIntent(value.(PlacementIntent)), true
|
|
}
|
|
|
|
// ListPlacements returns intents ordered by VolumeID.
|
|
func (s *PlacementIntentStore) ListPlacements() []PlacementIntent {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
keys := make([]string, 0, len(s.records))
|
|
for k := range s.records {
|
|
keys = append(keys, k)
|
|
}
|
|
sort.Strings(keys)
|
|
out := make([]PlacementIntent, 0, len(keys))
|
|
for _, k := range keys {
|
|
out = append(out, copyPlacementIntent(s.records[k]))
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (s *PlacementIntentStore) load() error {
|
|
entries, err := os.ReadDir(s.dir)
|
|
if err != nil {
|
|
return fmt.Errorf("lifecycle: readdir placement intent %q: %w", s.dir, err)
|
|
}
|
|
for _, entry := range entries {
|
|
if entry.IsDir() || filepath.Ext(entry.Name()) != ".json" {
|
|
continue
|
|
}
|
|
path := filepath.Join(s.dir, entry.Name())
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return fmt.Errorf("lifecycle: read placement intent %q: %w", path, err)
|
|
}
|
|
var rec PlacementIntent
|
|
if err := json.Unmarshal(raw, &rec); err != nil {
|
|
return fmt.Errorf("lifecycle: parse placement intent %q: %w", path, err)
|
|
}
|
|
if err := validatePlacementIntent(rec); err != nil {
|
|
return fmt.Errorf("lifecycle: invalid placement intent %q: %w", path, err)
|
|
}
|
|
if filepath.Base(s.path(rec.VolumeID)) != entry.Name() {
|
|
return fmt.Errorf("lifecycle: placement filename %q does not match volume_id %q", entry.Name(), rec.VolumeID)
|
|
}
|
|
s.records[rec.VolumeID] = rec
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *PlacementIntentStore) putLocked(rec PlacementIntent) error {
|
|
raw, err := json.MarshalIndent(rec, "", " ")
|
|
if err != nil {
|
|
return fmt.Errorf("lifecycle: marshal placement intent %s: %w", rec.VolumeID, err)
|
|
}
|
|
return writeRecordDurably(s.dir, rec.VolumeID+".json", raw)
|
|
}
|
|
|
|
func (s *PlacementIntentStore) path(volumeID string) string {
|
|
return filepath.Join(s.dir, volumeID+".json")
|
|
}
|
|
|
|
func validatePlacementIntent(intent PlacementIntent) error {
|
|
if err := validateVolumeID(intent.VolumeID); err != nil {
|
|
return err
|
|
}
|
|
if intent.DesiredRF <= 0 {
|
|
return fmt.Errorf("%w: desired_rf must be > 0", ErrInvalidVolumeSpec)
|
|
}
|
|
if len(intent.Slots) != intent.DesiredRF {
|
|
return fmt.Errorf("%w: slot count %d != desired_rf %d", ErrInvalidVolumeSpec, len(intent.Slots), intent.DesiredRF)
|
|
}
|
|
seenServers := make(map[string]bool)
|
|
for _, slot := range intent.Slots {
|
|
if err := validateServerID(slot.ServerID); err != nil {
|
|
return err
|
|
}
|
|
if seenServers[slot.ServerID] {
|
|
return fmt.Errorf("%w: duplicate server %s", ErrInvalidVolumeSpec, slot.ServerID)
|
|
}
|
|
if err := validateOptionalPort("iscsi_listen_port", slot.ISCSIListenPort); err != nil {
|
|
return err
|
|
}
|
|
if err := validateOptionalPort("nvme_listen_port", slot.NVMeListenPort); err != nil {
|
|
return err
|
|
}
|
|
seenServers[slot.ServerID] = true
|
|
switch slot.Source {
|
|
case PlacementSourceBlankPool:
|
|
if slot.PoolID == "" {
|
|
return fmt.Errorf("%w: blank-pool slot on %s missing pool id", ErrInvalidVolumeSpec, slot.ServerID)
|
|
}
|
|
if slot.ReplicaID != "" {
|
|
return fmt.Errorf("%w: blank-pool slot on %s must not preassign replica id", ErrInvalidVolumeSpec, slot.ServerID)
|
|
}
|
|
case PlacementSourceExistingReplica:
|
|
if slot.ReplicaID == "" {
|
|
return fmt.Errorf("%w: existing-replica slot on %s missing replica id", ErrInvalidVolumeSpec, slot.ServerID)
|
|
}
|
|
default:
|
|
return fmt.Errorf("%w: unknown placement source %q", ErrInvalidVolumeSpec, slot.Source)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validateOptionalPort(name string, port int) error {
|
|
if port < 0 || port > 65535 {
|
|
return fmt.Errorf("%w: %s %d outside 0..65535", ErrInvalidVolumeSpec, name, port)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func copyPlacementIntent(in PlacementIntent) PlacementIntent {
|
|
out := in
|
|
out.Slots = append([]PlacementSlotIntent(nil), in.Slots...)
|
|
return out
|
|
}
|