|
| 1 | +package workspace |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/open-source-cloud/devstack/internal/docker" |
| 10 | + "github.com/open-source-cloud/devstack/internal/generate" |
| 11 | + "github.com/open-source-cloud/devstack/internal/lock" |
| 12 | + "github.com/open-source-cloud/devstack/internal/state" |
| 13 | +) |
| 14 | + |
| 15 | +// This file implements `shared gc` (reclaim zero-ref shared services) and |
| 16 | +// `doctor --rebuild-state` (reconstruct the ledger from on-disk config + live |
| 17 | +// container labels). The ledger is a cache of reality, never the trusted source |
| 18 | +// (spec 09 §crash-recovery, spec 13). |
| 19 | + |
| 20 | +// RebuildSummary reports what RebuildState reconstructed. |
| 21 | +type RebuildSummary struct { |
| 22 | + Shared []string `json:"shared"` // shared_service rows re-derived (aliases) |
| 23 | + Refs int `json:"refs"` // service_ref rows re-derived |
| 24 | +} |
| 25 | + |
| 26 | +// RebuildState reconstructs the shared_service + service_ref ledger rows from the |
| 27 | +// on-disk workspace config intersected with live tool-labelled containers — the |
| 28 | +// recovery path when state.db is lost or corrupt. Acquires the lock. |
| 29 | +func (m *Manager) RebuildState(ctx context.Context) (RebuildSummary, error) { |
| 30 | + cs, err := m.Docker.ListManaged(ctx, map[string]string{generate.LabelManaged: "true"}) |
| 31 | + if err != nil { |
| 32 | + return RebuildSummary{}, fmt.Errorf("rebuild-state: list containers: %w", err) |
| 33 | + } |
| 34 | + liveProjects := map[string]bool{} |
| 35 | + liveShared := map[string]bool{} |
| 36 | + for _, c := range cs { |
| 37 | + if !c.Running() { |
| 38 | + continue |
| 39 | + } |
| 40 | + if p := c.Labels[generate.LabelProject]; p != "" { |
| 41 | + liveProjects[p] = true |
| 42 | + } |
| 43 | + if s := c.Labels[generate.LabelShared]; s != "" { |
| 44 | + liveShared[s] = true |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + instances, err := m.SharedInstances() |
| 49 | + if err != nil { |
| 50 | + return RebuildSummary{}, err |
| 51 | + } |
| 52 | + |
| 53 | + var sum RebuildSummary |
| 54 | + err = lock.WithLock(ctx, m.LockPath, func() error { |
| 55 | + for _, name := range sortedKeys(instances) { |
| 56 | + if !liveShared[name] { |
| 57 | + continue |
| 58 | + } |
| 59 | + inst := instances[name] |
| 60 | + if err := m.DB.UpsertSharedService(state.SharedService{ |
| 61 | + Name: inst.Alias, Engine: inst.Engine, MajorVersion: inst.Major, Status: "running", |
| 62 | + }); err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + sum.Shared = append(sum.Shared, inst.Alias) |
| 66 | + } |
| 67 | + for _, project := range sortedKeys(liveProjects) { |
| 68 | + for _, inst := range m.instancesUsedBy(project, instances) { |
| 69 | + if !liveShared[inst.Name] { |
| 70 | + continue |
| 71 | + } |
| 72 | + for _, c := range m.consumersOf(inst.Name) { |
| 73 | + if c.project != project { |
| 74 | + continue |
| 75 | + } |
| 76 | + if err := m.DB.AddRef(c.project, c.service, inst.Alias); err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + sum.Refs++ |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + return nil |
| 84 | + }) |
| 85 | + if err == nil { |
| 86 | + m.DB.LogEvent("rebuild-state", m.Model.Workspace.Name, |
| 87 | + fmt.Sprintf("re-derived %d shared + %d refs from live labels", len(sum.Shared), sum.Refs)) |
| 88 | + } |
| 89 | + return sum, err |
| 90 | +} |
| 91 | + |
| 92 | +// GCResult reports the zero-ref shared services and which were stopped. |
| 93 | +type GCResult struct { |
| 94 | + Candidates []string `json:"candidates"` // zero-ref shared aliases |
| 95 | + Stopped []string `json:"stopped"` // actually stopped (when stop=true) |
| 96 | +} |
| 97 | + |
| 98 | +// GC reconciles the ledger, then finds shared services at zero refs. With |
| 99 | +// stop=false it only reports candidates (the safe default — warm DBs are cheap). |
| 100 | +// With stop=true it `compose stop`s each candidate on the shared stack and marks |
| 101 | +// it stopped. The external network and volumes are never touched (spec 03/09). |
| 102 | +func (m *Manager) GC(ctx context.Context, stop bool) (GCResult, error) { |
| 103 | + if _, err := m.Reconcile(ctx); err != nil { |
| 104 | + // Reconcile is best-effort; a down daemon shouldn't block a dry-run report. |
| 105 | + if stop { |
| 106 | + return GCResult{}, err |
| 107 | + } |
| 108 | + } |
| 109 | + shared, err := m.DB.ListSharedServices() |
| 110 | + if err != nil { |
| 111 | + return GCResult{}, err |
| 112 | + } |
| 113 | + instances, err := m.SharedInstances() |
| 114 | + if err != nil { |
| 115 | + return GCResult{}, err |
| 116 | + } |
| 117 | + aliasToName := map[string]string{} |
| 118 | + for name, inst := range instances { |
| 119 | + aliasToName[inst.Alias] = name |
| 120 | + } |
| 121 | + |
| 122 | + var res GCResult |
| 123 | + var zero []state.SharedService |
| 124 | + for _, s := range shared { |
| 125 | + n, err := m.DB.RefCount(s.Name) |
| 126 | + if err != nil { |
| 127 | + return GCResult{}, err |
| 128 | + } |
| 129 | + if n == 0 { |
| 130 | + res.Candidates = append(res.Candidates, s.Name) |
| 131 | + zero = append(zero, s) |
| 132 | + } |
| 133 | + } |
| 134 | + if !stop || len(zero) == 0 { |
| 135 | + return res, nil |
| 136 | + } |
| 137 | + |
| 138 | + cp := m.sharedCompose() |
| 139 | + for _, s := range zero { |
| 140 | + name := aliasToName[s.Name] |
| 141 | + if name == "" { |
| 142 | + continue // ledger alias has no current config service; skip (orphan) |
| 143 | + } |
| 144 | + if err := cp.Stop(ctx, name); err != nil { |
| 145 | + return res, fmt.Errorf("stop shared %s: %w", s.Name, err) |
| 146 | + } |
| 147 | + if err := lock.WithLock(ctx, m.LockPath, func() error { |
| 148 | + return m.DB.SetSharedStatus(s.Engine, s.MajorVersion, "stopped") |
| 149 | + }); err != nil { |
| 150 | + return res, err |
| 151 | + } |
| 152 | + m.DB.LogEvent("shared-gc", s.Name, "stopped (0 refs)") |
| 153 | + res.Stopped = append(res.Stopped, s.Name) |
| 154 | + } |
| 155 | + return res, nil |
| 156 | +} |
| 157 | + |
| 158 | +// sharedCompose builds the compose driver for the shared stack. The compose file |
| 159 | +// is used when present; otherwise the label-driven `-p devstack-shared` form |
| 160 | +// drives stop/down (so gc works even if generated artifacts were cleaned). |
| 161 | +func (m *Manager) sharedCompose() docker.Compose { |
| 162 | + outDir := filepath.Join(m.Model.Root, generate.GenDir, "shared") |
| 163 | + file := filepath.Join(outDir, generate.ComposeFile) |
| 164 | + if _, err := os.Stat(file); err != nil { |
| 165 | + file = "" |
| 166 | + } |
| 167 | + runner := m.Runner |
| 168 | + if runner == nil { |
| 169 | + runner = docker.ExecRunner{} |
| 170 | + } |
| 171 | + return docker.Compose{Project: generate.SharedStackName, File: file, Dir: outDir, Runner: runner} |
| 172 | +} |
0 commit comments