~james/core

core/spindle/nixcache/publishing.go -rw-r--r-- 2.5 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package nixcache

import (
	"context"
	"maps"
	"slices"
	"time"

	"tangled.org/core/spindle/db"
)

// Reservations settles the quota holds of interrupted uploads, *quota.Manager in production.
// a reservation that is already gone has to return nil, or the row never settles
type Reservations interface {
	Commit(ctx context.Context, id string) error
	Release(ctx context.Context, id string) error
}

// rows only release once older than grace, which is why the second pass waits for it
func (p *Pruner) resolveOnce(ctx context.Context) {
	p.resolvePublishing(ctx)
	if len(p.Publishing) == 0 {
		return
	}
	grace, err := p.grace(ctx)
	if err != nil {
		p.Log.Warn("reading live nix cache grants failed", "err", err)
		return
	}
	select {
	case <-ctx.Done():
		return
	case <-time.After(grace):
	}
	p.resolvePublishing(ctx)
	if len(p.Publishing) > 0 {
		p.Log.Warn("attic uploads a restart interrupted stay unresolved", "count", len(p.Publishing))
	}
}

// once attic is unreachable, the caches after it wait for the next call
func (p *Pruner) resolvePublishing(ctx context.Context) {
	byCache := map[string][]db.NixPublishing{}
	for _, row := range p.Publishing {
		byCache[row.Cache] = append(byCache[row.Cache], row)
	}
	grace, err := p.grace(ctx)
	if err != nil {
		p.Log.Warn("reading live nix cache grants failed", "err", err)
		return
	}
	var kept []db.NixPublishing
	down := false
	for _, cache := range slices.Sorted(maps.Keys(byCache)) {
		rows := byCache[cache]
		if down {
			kept = append(kept, rows...)
			continue
		}
		hashes := make([]string, len(rows))
		for j, row := range rows {
			hashes[j] = row.StoreHash
		}
		missing, err := p.Attic.MissingPaths(ctx, cache, hashes)
		if err != nil {
			p.Log.Warn("checking interrupted attic uploads failed", "cache", cache, "err", err)
			kept = append(kept, rows...)
			down = unreachable(err)
			continue
		}
		for _, row := range rows {
			if !p.settle(ctx, row, slices.Contains(missing, row.StoreHash), grace) {
				kept = append(kept, row)
			}
		}
	}
	p.Publishing = kept
}

// false keeps the row in Publishing for the next call
func (p *Pruner) settle(ctx context.Context, row db.NixPublishing, missing bool, grace time.Duration) bool {
	var err error
	switch {
	case !missing:
		err = p.Reservations.Commit(ctx, row.ReservationID)
	case p.clock().Sub(row.CreatedAt) >= grace:
		err = p.Reservations.Release(ctx, row.ReservationID)
	default:
		return false
	}
	if err != nil {
		p.Log.Warn("settling an interrupted attic upload failed", "reservation", row.ReservationID, "err", err)
	}
	return err == nil
}