|
| 1 | +package cloudhypervisor |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/projecteru2/cocoon/gc" |
| 10 | + "github.com/projecteru2/cocoon/hypervisor" |
| 11 | + "github.com/projecteru2/cocoon/utils" |
| 12 | +) |
| 13 | + |
| 14 | +// compile-time interface check. |
| 15 | +var _ hypervisor.Hypervisor = (*CloudHypervisor)(nil) |
| 16 | + |
| 17 | +// chSnapshot is the typed GC snapshot for the Cloud Hypervisor backend. |
| 18 | +type chSnapshot struct { |
| 19 | + vmIDs map[string]struct{} // VM IDs present in the DB index |
| 20 | + runDirs []string // VM IDs that have a runtime dir on disk |
| 21 | + logDirs []string // VM IDs that have a log dir on disk |
| 22 | +} |
| 23 | + |
| 24 | +// GCModule returns a typed gc.Module[chSnapshot] for the Cloud Hypervisor backend. |
| 25 | +// |
| 26 | +// ReadDB (under lock): reads the VM index and scans the runtime/log base dirs |
| 27 | +// on disk, returning a snapshot of what exists vs what is recorded. |
| 28 | +// |
| 29 | +// Resolve: returns VM IDs whose runtime or log dirs are orphaned (present on |
| 30 | +// disk but no longer in the DB). |
| 31 | +// |
| 32 | +// Collect (under lock): removes the orphaned dirs for each VM ID. |
| 33 | +func (ch *CloudHypervisor) GCModule() gc.Module[chSnapshot] { |
| 34 | + return gc.Module[chSnapshot]{ |
| 35 | + Name: typ, |
| 36 | + Locker: ch.locker, |
| 37 | + ReadDB: func(ctx context.Context) (chSnapshot, error) { |
| 38 | + var snap chSnapshot |
| 39 | + if err := ch.store.Read(func(idx *hypervisor.VMIndex) error { |
| 40 | + snap.vmIDs = make(map[string]struct{}, len(idx.VMs)) |
| 41 | + for id := range idx.VMs { |
| 42 | + snap.vmIDs[id] = struct{}{} |
| 43 | + } |
| 44 | + return nil |
| 45 | + }); err != nil { |
| 46 | + return snap, err |
| 47 | + } |
| 48 | + snap.runDirs = utils.ScanSubdirs(filepath.Join(ch.conf.RunDir, "cloudhypervisor")) |
| 49 | + snap.logDirs = utils.ScanSubdirs(filepath.Join(ch.conf.LogDir, "cloudhypervisor")) |
| 50 | + return snap, nil |
| 51 | + }, |
| 52 | + Resolve: func(snap chSnapshot, _ map[string]any) []string { |
| 53 | + unreferenced := utils.FilterUnreferenced(snap.runDirs, snap.vmIDs) |
| 54 | + seen := make(map[string]struct{}, len(unreferenced)) |
| 55 | + for _, id := range unreferenced { |
| 56 | + seen[id] = struct{}{} |
| 57 | + } |
| 58 | + for _, id := range utils.FilterUnreferenced(snap.logDirs, snap.vmIDs) { |
| 59 | + if _, already := seen[id]; !already { |
| 60 | + unreferenced = append(unreferenced, id) |
| 61 | + } |
| 62 | + } |
| 63 | + return unreferenced |
| 64 | + }, |
| 65 | + Collect: func(ctx context.Context, ids []string) error { |
| 66 | + var errs []error |
| 67 | + for _, vmID := range ids { |
| 68 | + if err := os.RemoveAll(ch.conf.CHVMRunDir(vmID)); err != nil && !os.IsNotExist(err) { |
| 69 | + errs = append(errs, err) |
| 70 | + } |
| 71 | + if err := os.RemoveAll(ch.conf.CHVMLogDir(vmID)); err != nil && !os.IsNotExist(err) { |
| 72 | + errs = append(errs, err) |
| 73 | + } |
| 74 | + } |
| 75 | + return errors.Join(errs...) |
| 76 | + }, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// RegisterGC registers the Cloud Hypervisor GC module with the given Orchestrator. |
| 81 | +func (ch *CloudHypervisor) RegisterGC(orch *gc.Orchestrator) { |
| 82 | + gc.Register(orch, ch.GCModule()) |
| 83 | +} |
0 commit comments