bus5 B3 (§681, A-44, A-45, A-59): the bus module serves snapshot-get, snapshot-restore, snapshot-restore-cancel and snapshot-remove through its tools bundle, sends the chunks, hands the work to the keeper through requests; nats_snapshots; the whole test covers send, restore in place and removal
Builds: design 47 G §681, ADR 0675, lead answers A-44, A-45, A-59
This commit is contained in:
@@ -415,7 +415,7 @@ func Into(nc *nats.Conn, dir string) func(ctx context.Context, streams []string)
|
||||
if dir == "" {
|
||||
return "", errors.New("no snapshots directory is set (MESH_NATS_SNAPSHOTS_DIR), so no snapshot is taken and nothing is removed")
|
||||
}
|
||||
name := filepath.Join(dir, "entry-"+strings.Join(streams, "+")+"-"+time.Now().UTC().Format("20060102T150405Z")+".tar")
|
||||
name := filepath.Join(dir, "entry-"+strings.Join(streams, "+")+"-"+time.Now().UTC().Format("20060102T150405.000000Z")+".tar")
|
||||
f, err := os.CreateTemp(dir, ".entry-*.partial")
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
||||
@@ -845,6 +845,62 @@ func TestTheKeeperMakesWhatAVerdictAsksForAndSaysIt(t *testing.T) {
|
||||
t.Errorf("a cut-off restore: said %q, record %+v, removed %v", got, get("a5"), bus.deletedStreams)
|
||||
}
|
||||
}},
|
||||
// Design 47 G §681: snapshot-remove removes the snapshot of one request, or every one past its days, written in the
|
||||
// hand-act log first; a snapshot that cannot be recorded stays; one of before G goes only by its request.
|
||||
{"snapshot-remove removes by request or all expired, after the hand act", func(t *testing.T, k *Keeper, bus *fakeBus, out *said) {
|
||||
store, files := k.Store.(*memStore), k.Files.(*memFiles)
|
||||
var acts []string
|
||||
actErr := error(nil)
|
||||
k.Act = func(what, why, cause string) error { acts = append(acts, cause+": "+what); return actErr }
|
||||
put := func(r Request) {
|
||||
raw, _ := json.Marshal(r)
|
||||
_, _ = store.Put(StateRequests, r.ID, raw)
|
||||
}
|
||||
get := func(id string) Request {
|
||||
raw, _, _, _ := store.Get(StateRequests, id)
|
||||
var got Request
|
||||
_ = json.Unmarshal(raw, &got)
|
||||
return got
|
||||
}
|
||||
old := k.now().AddDate(0, 0, -100)
|
||||
soon := k.now().AddDate(0, 0, -80)
|
||||
put(Request{ID: "s1", Verb: VerbDrop, Path: "/snapshots/a.tar", Size: 10, Digest: "sha256:a", At: k.now(), Done: k.now()})
|
||||
put(Request{ID: "s2", Verb: VerbDrop, Path: "/snapshots/b.tar", Size: 10, Digest: "sha256:b", At: soon, Done: soon})
|
||||
put(Request{ID: "s3", Verb: VerbDrop, Snapshot: "/before/g.tar", At: old, Done: old})
|
||||
ask := func(r Request) Request {
|
||||
r.Caller = "person.jo"
|
||||
put(r)
|
||||
k.ActOnRequests()
|
||||
return get(r.ID)
|
||||
}
|
||||
for _, r := range []Request{{ID: "x1", Verb: VerbSnapshotRemove, Why: "w"}, {ID: "x2", Verb: VerbSnapshotRemove, Of: "s1", AllExpired: true, Why: "w"}} {
|
||||
if got := ask(r); got.Outcome != "error" || !strings.Contains(got.Error, "not both and not neither") {
|
||||
t.Errorf("%s: %+v", r.ID, got)
|
||||
}
|
||||
}
|
||||
actErr = errors.New("the log is down")
|
||||
if got := ask(Request{ID: "x3", Verb: VerbSnapshotRemove, Of: "s1", Why: "w"}); got.Outcome != "error" || len(files.removed) != 0 || get("s1").Path == "" {
|
||||
t.Errorf("an unrecorded hand act: %+v, removed %v", got, files.removed)
|
||||
}
|
||||
actErr = nil
|
||||
if got := ask(Request{ID: "x4", Verb: VerbSnapshotRemove, Of: "s1", Why: "w"}); got.Outcome != "accepted" || fmt.Sprint(files.removed) != "[/snapshots/a.tar]" || get("s1").ID != "" {
|
||||
t.Errorf("by request: %+v, removed %v", got, files.removed)
|
||||
}
|
||||
// The keeper's pass at each request removes what is past its days first, so the verb is driven apart from it.
|
||||
base := k.now()
|
||||
k.Now = func() time.Time { return base.AddDate(0, 0, 20) }
|
||||
all := &Request{ID: "x5", Verb: VerbSnapshotRemove, AllExpired: true, Why: "w", Caller: "person.jo"}
|
||||
if outcome, err := k.removeSnapshots(all); outcome != "accepted" || err != nil || all.Result != "1 removed" || get("s2").ID != "" || get("s3").ID == "" {
|
||||
t.Errorf("all expired: %q %v %+v: s2 goes, s3 (before G) stays", outcome, err, all)
|
||||
}
|
||||
k.Now = func() time.Time { return base }
|
||||
if got := ask(Request{ID: "x6", Verb: VerbSnapshotRemove, Of: "s3", Why: "w"}); got.Outcome != "accepted" || get("s3").ID != "" || files.removed[len(files.removed)-1] != "/before/g.tar" {
|
||||
t.Errorf("a snapshot of before G by its request: %+v, removed %v", got, files.removed)
|
||||
}
|
||||
if len(acts) != 4 || !strings.Contains(acts[len(acts)-1], "asked by person.jo") {
|
||||
t.Errorf("the hand acts: %v", acts)
|
||||
}
|
||||
}},
|
||||
{"a recreate rebuilds a drifted entry after a snapshot", func(t *testing.T, k *Keeper, bus *fakeBus, out *said) {
|
||||
store := k.Store.(*memStore)
|
||||
k.Heard(Accepted{Module: "m", Revision: 1, BusState: []catalogue.BusStateEntry{log}})
|
||||
|
||||
@@ -54,11 +54,13 @@ type Request struct {
|
||||
// Of is the keeper's snapshot a snapshot-restore or snapshot-remove names, by its request; Verify checks only; ActID is the
|
||||
// restore's act (the id of the running one for a busy answer); Ended is when the restore ended, and Result how: done or failed
|
||||
// (design 47 G §681).
|
||||
Of string `json:"of,omitempty"`
|
||||
Verify bool `json:"verify,omitempty"`
|
||||
ActID string `json:"act-id,omitempty"`
|
||||
Ended time.Time `json:"ended,omitempty"`
|
||||
Result string `json:"result,omitempty"`
|
||||
Of string `json:"of,omitempty"`
|
||||
Verify bool `json:"verify,omitempty"`
|
||||
// AllExpired is a snapshot-remove of every snapshot past its days, and Result then how many went.
|
||||
AllExpired bool `json:"all-expired,omitempty"`
|
||||
ActID string `json:"act-id,omitempty"`
|
||||
Ended time.Time `json:"ended,omitempty"`
|
||||
Result string `json:"result,omitempty"`
|
||||
// Waiting is why it waits, while it waits: the operator's warrant, or the keeper's first view.
|
||||
Waiting string `json:"waiting,omitempty"`
|
||||
// Unrecorded is why the hand act of what it did could not be recorded, when it could not: said, never only logged.
|
||||
@@ -157,6 +159,8 @@ func (k *Keeper) act(r *Request) (string, error) {
|
||||
return k.startRestore(r)
|
||||
case VerbSnapshotRestoreCancel:
|
||||
return k.cancelRestore(r)
|
||||
case VerbSnapshotRemove:
|
||||
return k.removeSnapshots(r)
|
||||
}
|
||||
if !k.Viewed() {
|
||||
return "", fmt.Errorf("%w: the keeper holds no first view yet, so it acts on nothing", ErrNoWarrant)
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
const (
|
||||
VerbSnapshotRestore = meshbroker.VerbSnapshotRestore
|
||||
VerbSnapshotRestoreCancel = meshbroker.VerbSnapshotRestoreCancel
|
||||
VerbSnapshotRemove = meshbroker.VerbSnapshotRemove
|
||||
)
|
||||
|
||||
// RestoreBound is the longest a restore runs: past it the keeper ends it as failed.
|
||||
|
||||
@@ -175,12 +175,16 @@ func (k *Keeper) recordSnapshot(ctx context.Context, r *Request, path string, st
|
||||
|
||||
// ExpireSnapshots removes every recorded snapshot past its days, and its record, each removal said in the journal:
|
||||
// at the keeper's start and at each new request, never on a timer. It judges the fill after.
|
||||
func (k *Keeper) ExpireSnapshots() {
|
||||
func (k *Keeper) ExpireSnapshots() { k.expireSnapshots() }
|
||||
|
||||
// expireSnapshots is ExpireSnapshots, answering how many it removed.
|
||||
func (k *Keeper) expireSnapshots() int {
|
||||
recs, err := k.snapshotRecords()
|
||||
if err != nil {
|
||||
k.logf("the snapshots cannot be read: %v", err)
|
||||
return
|
||||
return 0
|
||||
}
|
||||
removed := 0
|
||||
var used int64
|
||||
for key, r := range recs {
|
||||
if !r.Expired(k.now(), k.keepDays()) {
|
||||
@@ -198,8 +202,52 @@ func (k *Keeper) ExpireSnapshots() {
|
||||
continue
|
||||
}
|
||||
k.logf("removed the snapshot %s of the request %s, taken %s, past its %d days", r.Path, key, r.At.Format(time.RFC3339), k.keepDays())
|
||||
removed++
|
||||
}
|
||||
k.snapshotsFill(used)
|
||||
return removed
|
||||
}
|
||||
|
||||
// removeSnapshots is the seat mesh-broker's snapshot-remove: the snapshot of one request, or every one past its days
|
||||
// (all-expired). The hand act is written first, and a snapshot that cannot be recorded stays; a snapshot of before G goes only
|
||||
// by its request (design 47 G §681). The answer is `accepted` with the request's own id as the act.
|
||||
func (k *Keeper) removeSnapshots(r *Request) (string, error) {
|
||||
if (r.Of == "") == !r.AllExpired {
|
||||
return "error", fmt.Errorf("snapshot-remove names a request, or all-expired, not both and not neither")
|
||||
}
|
||||
what := "removed every snapshot past its days"
|
||||
if r.Of != "" {
|
||||
what = "removed the snapshot of the request " + r.Of
|
||||
}
|
||||
if k.Act != nil {
|
||||
if err := k.Act(what+", asked by "+r.Caller, r.Why, "mesh-broker_snapshot_remove"); err != nil {
|
||||
return "error", fmt.Errorf("the hand act cannot be recorded, so nothing is removed: %w", err)
|
||||
}
|
||||
}
|
||||
if r.AllExpired {
|
||||
r.Result = fmt.Sprintf("%d removed", k.expireSnapshots())
|
||||
return "accepted", nil
|
||||
}
|
||||
raw, _, found, err := k.Store.Get(StateRequests, r.Of)
|
||||
var rec Request
|
||||
if err != nil || !found || json.Unmarshal(raw, &rec) != nil || (rec.Path == "" && rec.Snapshot == "") {
|
||||
return "error", fmt.Errorf("no snapshot of the request %s is held", r.Of)
|
||||
}
|
||||
path := rec.Path
|
||||
if path == "" {
|
||||
path = rec.Snapshot
|
||||
}
|
||||
if err := k.files().Remove(path); err != nil {
|
||||
return "error", fmt.Errorf("the snapshot %s cannot be removed: %w", path, err)
|
||||
}
|
||||
if err := k.Store.Delete(StateRequests, r.Of); err != nil {
|
||||
return "error", fmt.Errorf("the record of the removed snapshot %s cannot be removed: %w", r.Of, err)
|
||||
}
|
||||
k.logf("removed the snapshot %s of the request %s by hand, asked by %s", path, r.Of, r.Caller)
|
||||
if used, err := k.SnapshotsUsed(""); err == nil {
|
||||
k.snapshotsFill(used)
|
||||
}
|
||||
return "accepted", nil
|
||||
}
|
||||
|
||||
// SnapshotsAtStart is the keeper's first look at the snapshots at its start: what is past its days removed, and the fill
|
||||
|
||||
Reference in New Issue
Block a user