Files
llmgw/internal/mirror/tombstone.go
T

79 lines
2.0 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)
}
}
// RecordRemoval keeps a version's removal for a mirror's peers when the
// removal was made outside the running mirror, by llmgw mirror remove: once
// none of its files is left, so a peer removes its copy rather than this
// mirror copying it back.
func RecordRemoval(root, model, version string) error {
entries, err := List(root)
if err != nil {
return err
}
for _, e := range entries {
if e.Model == model && e.Version == version {
return nil
}
}
m := &Manager{root: root}
m.recordTombstone(model, version, time.Now())
return nil
}