package moves import ( "strconv" "github.com/carlosframework/platform/internal/provision" ) // Transition is what one record write did to a move, as the move log names // it (log.go): the move, the event, the identity that keys its log object, // the fields it carries (the regions, the evidence's digests, timings, a // stuck's role), and the instance incarnation the move's record carried, // which binds a queued entry to that instance (the console's outbox). The // box's writers and the console's move pass name their writes with it, so a // transition has one identity whoever writes it. type Transition struct { Op, Event, Identity string Incarnation string Fields map[string]string } // phaseOf is the phase a move's state is at: the cut's, the landing's, or // (rolling back) the target's and the source's. func phaseOf(m *provision.MoveState) (phase, target, source string) { switch m.State { case provision.MoveCutting: return m.CutPhase, "", "" case provision.MoveLanding: return m.LandingPhase, "", "" case provision.MoveRollingBack: if m.Rollback != nil { return "", m.Rollback.TargetPhase, m.Rollback.SourcePhase } } return "", "", "" } // transitionFields are a move's regions, the digests its evidence has // recorded so far (the source's stop: final TXID, database, files, timers; // the landing's installed database; the pinned inputs: release, env report) // and its admitted cut budget. func transitionFields(m *provision.MoveState) map[string]string { f := map[string]string{"from": m.From, "to": m.To} set := func(k, v string) { if v != "" { f[k] = v } } if s := m.Source; s != nil { if s.FinalTXID != 0 { f["final_txid"] = strconv.FormatUint(s.FinalTXID, 10) } set("db_sha256", s.DBSHA256) set("timers_sha256", s.TimersSHA256) if s.Files != nil { set("files_plain", s.Files.Plain) } } if l := m.Landing; l != nil { set("installed_db_sha256", l.InstalledDBSHA256) } if in := m.Inputs; in != nil { set("release", in.Release) set("env_report", in.EnvReport) } if m.CutBudgetS > 0 { // The admitted cut's budget, which the member line for it states. f["cut_budget_s"] = strconv.Itoa(m.CutBudgetS) } return f } // seconds is b - a in whole seconds, "" unless both are known. func seconds(a, b interface{ Unix() int64 }) string { return strconv.FormatInt(b.Unix()-a.Unix(), 10) } // DeclaredLostIdentity is the identity of a declared loss's entry: the // rollback's target phase going to lost (TransitionOf), whoever logs it. const DeclaredLostIdentity = "rolling_back-target-" + provision.RbTargetLost // DeclaredLost is the entry of a declared loss of m's target (ยง7.10), event // declared_lost, named as TransitionOf names the rollback's target going to // lost. The console that performs a declaration whose signed consequence is // that loss names it from the record it read before (a landing, which the // declaration rolls back, or a rollback whose target is releasing or // returning), and a box whose pass sees the target lost names it from that // record. The identity, DeclaredLostIdentity, is the same for both, so // create-only storage keeps one (final review I4). A target loss is declared // only on that path, so ok is false for a move in any other state (a // committed move's destroyed-local-only declaration changes no move state: // final re-review 1, N1) and for a move with no id. func DeclaredLost(m *provision.MoveState) (Transition, bool) { if m == nil || m.ID == "" || (m.State != provision.MoveLanding && m.State != provision.MoveRollingBack) { return Transition{}, false } var rb provision.MoveRollback if m.Rollback != nil { rb = *m.Rollback } before, after := *m, *m before.State, after.State = provision.MoveRollingBack, provision.MoveRollingBack brb, arb := rb, rb brb.TargetPhase, arb.TargetPhase = "", provision.RbTargetLost before.Rollback, after.Rollback = &brb, &arb return TransitionOf(provision.Record{Move: &before}, provision.Record{Move: &after}) } // TransitionOf names the transition from before to after, one record write: // before as the write found it, after as written. ok is false when the // write changed nothing the log records (a pin, recovery inputs, a replay). // // Identities: - for a phase step (a rollback names the half // that moved, rolling_back-target-

or rolling_back-source-

), the // flip as cutting-flipped, committed-opened and committed-source_parked, // reverting-opened, probe_attempt- for the landing's n-th probe, // stuck- keyed by the record's recovery retry epoch, the new state // itself (requested for preparing, cutting-requested, ready, aborting, // reverting, relocating, rolling_back), and the terminal outcome (done, // rolled_back, aborted). A done or rolled_back outcome carries the move's // timings (Timings) over the journal as the close found it. func TransitionOf(before, after provision.Record) (Transition, bool) { bm, am := before.Move, after.Move if am == nil { // A close clears the move and leaves its outcome (D11). if bm == nil || after.LastMove == nil || after.LastMove.ID != bm.ID || bm.ID == "" { return Transition{}, false } f := transitionFields(bm) if out := after.LastMove.Outcome; out == "done" || out == "rolled_back" { // The timings (Plan 6 Task 4), from the journal the close // found: the same for every writer of the entry. for k, v := range Timings(bm) { f[k] = timingText(v) } } f["offline_s"] = timingText(after.LastMove.OfflineS) return Transition{Op: bm.ID, Event: after.LastMove.Outcome, Identity: after.LastMove.Outcome, Incarnation: bm.Incarnation, Fields: f}, true } if am.ID == "" || (bm != nil && bm.ID != am.ID) { return Transition{}, false } e := Transition{Op: am.ID, Incarnation: am.Incarnation, Fields: transitionFields(am)} if bm == nil { bm = &provision.MoveState{} } switch { case am.State == provision.MoveStuck: if bm.State == provision.MoveStuck { return Transition{}, false } e.Event, e.Identity = "stuck", "stuck-"+strconv.FormatUint(am.RecoveryRetryEpoch, 10) // A rollback's target stops for an operator at its return // (returning) and at its release (releasing, whose files cannot be // archived: final review M1); every other stuck is the source's. e.Fields["role"] = "source" if rb := am.Rollback; rb != nil && (rb.TargetPhase == provision.RbTargetReturning || rb.TargetPhase == provision.RbTargetReleasing && am.StuckReason == provision.StuckReleaseTargetFiles) { e.Fields["role"] = "target" } if am.StuckReason != "" { e.Fields["reason"] = am.StuckReason } return e, true case am.State == provision.MoveLanding && am.Landing != nil && am.Landing.Attempts > 0 && (bm.Landing == nil || am.Landing.Attempts > bm.Landing.Attempts): e.Event, e.Identity = "probe_attempt", "probe_attempt-"+strconv.Itoa(am.Landing.Attempts) return e, true case bm.State == provision.MoveCutting && am.State == provision.MoveLanding: e.Event, e.Identity = "flipped", "cutting-flipped" return e, true case am.State == provision.MoveCommitted && bm.State == provision.MoveCommitted: switch { case am.SourceParkedAt != nil && bm.SourceParkedAt == nil: e.Event, e.Identity = "source_parked", "committed-source_parked" case am.OpenedAt != nil && bm.OpenedAt == nil: e.Event, e.Identity = "opened", "committed-opened" if am.CutAt != nil { e.Fields["offline_s"] = seconds(*am.CutAt, *am.OpenedAt) } default: return Transition{}, false } return e, true case am.State == provision.MoveReverting && bm.State == provision.MoveReverting: if am.ReopenedAt == nil || bm.ReopenedAt != nil { return Transition{}, false } e.Event, e.Identity = "rev_opened", "reverting-opened" if am.CutAt != nil { e.Fields["offline_s"] = seconds(*am.CutAt, *am.ReopenedAt) } return e, true } phase, target, source := phaseOf(am) bphase, btarget, bsource := phaseOf(bm) if am.State == bm.State { switch { case am.State == provision.MoveRollingBack && target != btarget: e.Event, e.Identity = target, "rolling_back-target-"+target if target == provision.RbTargetLost { e.Event = "declared_lost" } case am.State == provision.MoveRollingBack && source != bsource: e.Event, e.Identity = source, "rolling_back-source-"+source rb := am.Rollback switch source { case provision.RbSourceRecovered: if rb.RecoveredAt != nil { if rb.RequestedAt != nil { e.Fields["recovery_s"] = seconds(*rb.RequestedAt, *rb.RecoveredAt) } if am.CutAt != nil { e.Fields["offline_s"] = seconds(*am.CutAt, *rb.RecoveredAt) } } case provision.RbSourceOpened: if rb.OpenedAt != nil && am.CutAt != nil { e.Fields["offline_s"] = seconds(*am.CutAt, *rb.OpenedAt) } } case phase != bphase && phase != "": e.Event, e.Identity = phase, am.State+"-"+phase if am.State == provision.MoveCutting && phase == provision.CutRequested { e.Event = "cut_requested" } default: return Transition{}, false } return e, true } // A new state. switch am.State { case provision.MovePreparing: e.Event, e.Identity = "requested", "requested" case provision.MoveCutting: e.Event, e.Identity = phase, "cutting-"+phase if phase == provision.CutRequested || phase == "" { e.Event, e.Identity = "cut_requested", "cutting-requested" } case provision.MoveLanding: e.Event, e.Identity = phase, "landing-"+phase case provision.MoveRollingBack: e.Event, e.Identity = "rolling_back", "rolling_back" if am.Rollback != nil && am.Rollback.Reason != "" { e.Fields["reason"] = am.Rollback.Reason } default: // ready, committed, aborting, reverting, relocating: the state is the // event. e.Event, e.Identity = am.State, am.State } return e, true }