Skip to content
quarry Artifact [5992d903e8]
Login

Artifact [5992d903e8]

Artifact 5992d903e820b7f395d9d4a0c917310691bcb530235874e9c9c630231626a64d:


// pack.go — the maintenance lane's pack task: a store's repack, asked
// for by an operator and never scheduled.
package main

import (
	"context"
	"fmt"
	"log"

	"craftdesign.group/quarry/internal/api"
	"craftdesign.group/quarry/internal/lease"
	"craftdesign.group/quarry/internal/ops"
	"craftdesign.group/quarry/internal/zitcli"
)

// packTask is the task name on the maintenance lane and the ops page.
const packTask = "pack"

// requestPack queues a pack of one registered store. It reports false
// when a pack of it is already queued or running, and an error for a
// store that is not served here.
func (rg *registrar) requestPack(name string) (bool, error) {
	repo, ok := rg.deps.Repos.Get(name)
	if !ok || repo == nil || repo.Zit == nil {
		return false, fmt.Errorf("no repository %q: %w", name, ops.ErrNotFound)
	}
	return rg.maint.RequestOnce(name, packTask, func(ctx context.Context) error {
		return runPack(ctx, repo)
	}), nil
}

// runPack repacks the store under its write lease and nothing more: it
// takes neither the ingest pipeline's lock nor a lane slot of the
// pushes', because zit itself refuses a pack the store was written
// during (zitcli.ErrPackAborted) and leaves the store intact.
//
// A pack that rewrote the store may have dropped artifacts the saved
// frames cursor consumed, which the next round reads as a lost cursor
// and rebuilds from. That round is started here, off the maintenance
// slot, so the rebuild is prompt and does not hold the lane.
func runPack(ctx context.Context, repo *api.Repo) error {
	var res zitcli.PackResult
	err := repo.Guard(func(tok lease.Token) error {
		var perr error
		res, perr = repo.Zit.Pack(tok, ctx)
		return perr
	})
	if err != nil {
		return err
	}
	reportPack(ctx, zitcli.MaintPack{Before: res.Before, After: res.After, Reclaimed: res.Reclaimed, Nothing: res.Nothing})
	if !res.Nothing && repo.Sync != nil {
		go func() {
			if _, err := repo.Sync(); err != nil {
				log.Printf("pack: the round after it: %v", err)
			}
		}()
	}
	return nil
}