gc

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 2, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package gc provides periodic garbage collection and maintenance tasks for nstance instances.

The garbage collector runs periodically (controlled by the interval config) and performs:

  • Provider sync: Fetches current instance state from the cloud provider and backfills the local database with any instances not yet tracked.

  • Dangling instance cleanup: Terminates instances that were created but never registered with the server within the registration timeout period.

  • Health monitoring: Detects instances with stale health reports and enqueues them for reconciliation checks.

  • Deleted record cleanup: Purges storage records for instances that have been deleted for longer than the configured retention period.

The gc Runner orchestrates these tasks and only runs when the server is the shard leader.

Index

Constants

View Source
const DefaultDeletedRecordRetention = 30 * time.Minute

DefaultDeletedRecordRetention is the default retention period for deleted instance records

Variables

This section is empty.

Functions

This section is empty.

Types

type InstanceGarbageCollector

type InstanceGarbageCollector struct {
	// contains filtered or unexported fields
}

InstanceGarbageCollector handles garbage collection of dangling provider instances

func NewInstanceGarbageCollector

func NewInstanceGarbageCollector(db *localdb.DB, prov infra.Provider, store storage.Storage, rec Reconciler, logger *slog.Logger) *InstanceGarbageCollector

NewInstanceGarbageCollector creates a new garbage collection service

func (*InstanceGarbageCollector) CheckInstanceHealth

func (s *InstanceGarbageCollector) CheckInstanceHealth(ctx context.Context, interval time.Duration) error

CheckInstanceHealth checks for instances with stale health_at timestamps

func (*InstanceGarbageCollector) CleanupDeletedInstanceRecords

func (s *InstanceGarbageCollector) CleanupDeletedInstanceRecords(ctx context.Context, retentionPeriod time.Duration) error

CleanupDeletedInstanceRecords removes storage records for instances that have been deleted for longer than the retention period.

SAFETY: All code paths that mark instances as deleted are provider-verified:

  1. Manager.DeleteInstance() - calls provider.DeleteInstance() before marking deleted
  2. GC.terminateAndCleanup() - terminates via provider before marking deleted
  3. Manager.seedFromProvider() - marks deleted if provider_id not in provider's instance list (handles EC2 instances terminated while server was down)

We do NOT re-check the provider API here to avoid rate limiting issues.

func (*InstanceGarbageCollector) RunGarbageCollection

func (s *InstanceGarbageCollector) RunGarbageCollection(ctx context.Context, maxAge time.Duration) error

RunGarbageCollection performs a complete garbage collection cycle. SQLite is already populated from S3 + provider after leader election via RebuildCache. GC does not query the provider during its loop - SQLite has everything needed.

func (*InstanceGarbageCollector) SetReconciler

func (s *InstanceGarbageCollector) SetReconciler(rec Reconciler)

SetReconciler sets the reconciler for the garbage collector. This exists because of an init-order dependency: the GC service is created before the reconciler, which itself depends on the instance manager.

type Reconciler

type Reconciler interface {
	Enqueue(event reconciler.ReconcileEvent)
}

Reconciler interface for enqueuing reconciliation events

type Runner

type Runner struct {
	// contains filtered or unexported fields
}

Runner manages periodic garbage collection runs

func NewRunner

func NewRunner(service *InstanceGarbageCollector, interval, cycleTimeout, registrationTimeout, deletedRecordRetention, healthCheckInterval time.Duration, isLeader func() bool, logger *slog.Logger) *Runner

NewRunner creates a new garbage collector runner

func (*Runner) SetIsLeaderFunc

func (r *Runner) SetIsLeaderFunc(isLeader func() bool)

SetIsLeaderFunc updates the function used to check leadership

func (*Runner) Start

func (r *Runner) Start(ctx context.Context)

Start begins the garbage collection loop in a background goroutine

func (*Runner) Stop

func (r *Runner) Stop()

Stop terminates the garbage collection loop and waits for completion

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL