| 1 | package proc |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "io/fs" |
| 7 | "os" |
| 8 | "slices" |
| 9 | "sync" |
| 10 | "syscall" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | // Debts keeps what unproven turns still owe, past their turn records: |
| 15 | // a JSON file (Path), or memory when Path is "". |
| 16 | type Debts struct { |
| 17 | Path string |
| 18 | mu sync.Mutex |
| 19 | mem []Obligation |
| 20 | // between runs between a read and its write (tests: another |
| 21 | // process's write in the gap). |
| 22 | between func() |
| 23 | // saved runs once a save has written (tests: a slow write). |
| 24 | saved func() |
| 25 | // LockHook (a test seam) runs before each lock is taken, with its number from 1 |
| 26 | // (tests: a lock that fails, or is slow). |
| 27 | LockHook func(n int) error |
| 28 | locks int |
| 29 | } |
| 30 | |
| 31 | // Owe keeps o durably. |
| 32 | func (d *Debts) Owe(o Obligation) error { |
| 33 | return d.locked(func() error { |
| 34 | all, err := d.load() |
| 35 | if err != nil { |
| 36 | return err |
| 37 | } |
| 38 | return d.save(append(all, o)) |
| 39 | }) |
| 40 | } |
| 41 | |
| 42 | // locked runs fn holding the debts: this instance's mutex and, for a |
| 43 | // file, an flock on Path+".lock" that every runner process takes for |
| 44 | // every read-modify-write. |
| 45 | func (d *Debts) locked(fn func() error) error { return d.lockedBy(time.Time{}, fn) } |
| 46 | |
| 47 | // errLate: the debts could not be read or written before the deadline. |
| 48 | var errLate = errors.New("mac: the debts were not reached in time") |
| 49 | |
| 50 | // lockedBy is locked, given up at deadline (zero: none). Every error |
| 51 | // (the lock file, the lock, the deadline) is the caller's. |
| 52 | func (d *Debts) lockedBy(deadline time.Time, fn func() error) error { |
| 53 | for !d.mu.TryLock() { |
| 54 | if late(deadline) { |
| 55 | return errLate |
| 56 | } |
| 57 | time.Sleep(10 * time.Millisecond) |
| 58 | } |
| 59 | defer d.mu.Unlock() |
| 60 | if d.locks++; d.LockHook != nil { |
| 61 | if err := d.LockHook(d.locks); err != nil { |
| 62 | return err |
| 63 | } |
| 64 | } |
| 65 | if d.Path == "" { |
| 66 | if late(deadline) { |
| 67 | return errLate |
| 68 | } |
| 69 | return fn() |
| 70 | } |
| 71 | f, err := os.OpenFile(d.Path+".lock", os.O_CREATE|os.O_RDWR, 0600) |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | defer f.Close() |
| 76 | for { |
| 77 | err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB) |
| 78 | if err == nil { |
| 79 | break |
| 80 | } |
| 81 | if !errors.Is(err, syscall.EWOULDBLOCK) { |
| 82 | return err |
| 83 | } |
| 84 | if late(deadline) { |
| 85 | return errLate |
| 86 | } |
| 87 | time.Sleep(10 * time.Millisecond) |
| 88 | } |
| 89 | defer syscall.Flock(int(f.Fd()), syscall.LOCK_UN) |
| 90 | if late(deadline) { |
| 91 | return errLate |
| 92 | } |
| 93 | return fn() |
| 94 | } |
| 95 | |
| 96 | // Incomplete reports whether an obligation can never be met (a turn |
| 97 | // whose record was never completed); an unreadable file says yes. |
| 98 | func (d *Debts) Incomplete() bool { |
| 99 | found := true |
| 100 | _ = d.locked(func() error { |
| 101 | all, err := d.load() |
| 102 | found = err != nil || slices.ContainsFunc(all, func(o Obligation) bool { return o.Incomplete }) |
| 103 | return nil |
| 104 | }) |
| 105 | return found |
| 106 | } |
| 107 | |
| 108 | func (d *Debts) load() ([]Obligation, error) { |
| 109 | if d.Path == "" { |
| 110 | return slices.Clone(d.mem), nil |
| 111 | } |
| 112 | raw, err := os.ReadFile(d.Path) |
| 113 | if errors.Is(err, fs.ErrNotExist) { |
| 114 | return nil, nil |
| 115 | } |
| 116 | if err != nil { |
| 117 | return nil, err |
| 118 | } |
| 119 | var all []Obligation |
| 120 | return all, json.Unmarshal(raw, &all) |
| 121 | } |
| 122 | |
| 123 | func (d *Debts) save(all []Obligation) error { |
| 124 | if d.saved != nil { |
| 125 | defer d.saved() |
| 126 | } |
| 127 | if d.Path == "" { |
| 128 | d.mem = all |
| 129 | return nil |
| 130 | } |
| 131 | if len(all) == 0 { |
| 132 | if err := os.Remove(d.Path); err != nil && !errors.Is(err, fs.ErrNotExist) { |
| 133 | return err |
| 134 | } |
| 135 | return nil |
| 136 | } |
| 137 | raw, err := json.Marshal(all) |
| 138 | if err != nil { |
| 139 | return err |
| 140 | } |
| 141 | return WriteDurably(d.Path, raw) |
| 142 | } |
| 143 | |
| 144 | // WriteDurably writes raw beside p, syncs it and renames it over p. |
| 145 | func WriteDurably(p string, raw []byte) error { |
| 146 | f, err := os.OpenFile(p+".new", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600) |
| 147 | if err != nil { |
| 148 | return err |
| 149 | } |
| 150 | _, err = f.Write(raw) |
| 151 | if serr := f.Sync(); err == nil { |
| 152 | err = serr |
| 153 | } |
| 154 | if cerr := f.Close(); err == nil { |
| 155 | err = cerr |
| 156 | } |
| 157 | if err != nil { |
| 158 | return err |
| 159 | } |
| 160 | return os.Rename(p+".new", p) |
| 161 | } |
| 162 | |
| 163 | // Clone is a copy of s (nil stays nil). |
| 164 | func (s Span) Clone() Span { |
| 165 | if s == nil { |
| 166 | return nil |
| 167 | } |
| 168 | c := Span{} |
| 169 | for k, v := range s { |
| 170 | c[k] = v |
| 171 | } |
| 172 | return c |
| 173 | } |
| 174 | |