Each mirror reads its peers every thirty seconds and copies the versions a peer holds whole that it lacks, keeping a copy only if its checksum is the peer's. A removal is kept with its time (.removed.json) and listed for peers: a version removed while a peer was down is removed there when it is back, not copied back from it; one added again after its removal stands on both. Neither mirror leads. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
package mirror
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
)
|
|
|
|
// Removals kept so mirrors in step agree on them: a version removed here
|
|
// while a peer was down is removed there when it is back, rather than copied
|
|
// back here from it. Kept in the mirror's own bookkeeping, a dot file the
|
|
// file server never serves as a model.
|
|
|
|
const tombstoneFile = ".removed.json"
|
|
|
|
// Tombstones are the versions removed, model/version to when, in ms.
|
|
func (m *Manager) Tombstones() map[string]int64 {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
return m.readTombstones()
|
|
}
|
|
|
|
func (m *Manager) readTombstones() map[string]int64 {
|
|
out := map[string]int64{}
|
|
b, err := os.ReadFile(filepath.Join(m.root, tombstoneFile))
|
|
if err == nil {
|
|
_ = json.Unmarshal(b, &out)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (m *Manager) writeTombstones(t map[string]int64) {
|
|
b, _ := json.Marshal(t)
|
|
tmp := filepath.Join(m.root, tombstoneFile+".tmp")
|
|
if os.WriteFile(tmp, b, 0o644) == nil {
|
|
_ = os.Rename(tmp, filepath.Join(m.root, tombstoneFile))
|
|
}
|
|
}
|
|
|
|
func (m *Manager) recordTombstone(model, version string, at time.Time) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
t := m.readTombstones()
|
|
if at.UnixMilli() > t[model+"/"+version] {
|
|
t[model+"/"+version] = at.UnixMilli()
|
|
m.writeTombstones(t)
|
|
}
|
|
}
|
|
|
|
func (m *Manager) clearTombstone(model, version string) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
t := m.readTombstones()
|
|
if _, ok := t[model+"/"+version]; ok {
|
|
delete(t, model+"/"+version)
|
|
m.writeTombstones(t)
|
|
}
|
|
}
|