| 1 | package hostops |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | |
| 6 | "github.com/carlosframework/platform/internal/hostclaim" |
| 7 | ) |
| 8 | |
| 9 | // CompletePurgeFences releases every held purge fence, records that in the |
| 10 | // journal while the purging operation still owns admission, closes the |
| 11 | // incarnation with the typed CompletePurge (the only writer of last_purge), |
| 12 | // and finally removes the journal. Each step replays. |
| 13 | func (c Controller) CompletePurgeFences(ctx context.Context, h *OperationHandle) error { |
| 14 | if c.Control == nil || h == nil || h.Kind() != OpPurging { |
| 15 | return ErrContract |
| 16 | } |
| 17 | account, app, id, appID := h.Account(), h.App(), h.ID(), h.AppID() |
| 18 | j, _, err := ReadPurgeJournal(ctx, c.Store, account, app, id, appID) |
| 19 | if err != nil { |
| 20 | return err |
| 21 | } |
| 22 | if j.Phase != "done" { |
| 23 | return ErrPending |
| 24 | } |
| 25 | if !j.Cleanup.Fences { |
| 26 | for host, ph := range j.Hosts { |
| 27 | if ph.FenceState != "held" { |
| 28 | continue |
| 29 | } |
| 30 | obs, err := hostclaim.Read(ctx, c.Store, host) |
| 31 | if err != nil { |
| 32 | return err |
| 33 | } |
| 34 | if obs.Absent { |
| 35 | return ErrPending |
| 36 | } |
| 37 | if obs.Claim.Holder == nil && obs.Claim.Op.Kind == hostclaim.KindPurging && obs.Claim.Op.ID == id { |
| 38 | if _, err = hostclaim.Update(ctx, c.Store, obs, ReleasePurgeFence(id)); err != nil { |
| 39 | return err |
| 40 | } |
| 41 | } else if obs.Claim.Gen <= ph.FenceGen { |
| 42 | return ErrPending |
| 43 | } |
| 44 | // A changed successor cannot have been admitted before this fence |
| 45 | // was released. Do not touch it; keep the old generation as proof. |
| 46 | fresh, tag, err := ReadPurgeJournal(ctx, c.Store, account, app, id, appID) |
| 47 | if err != nil { |
| 48 | return err |
| 49 | } |
| 50 | saved := fresh.Hosts[host] |
| 51 | saved.FenceState = "released" |
| 52 | fresh.Hosts[host] = saved |
| 53 | if _, err = c.Control.SavePurgeJournal(ctx, fresh, tag); err != nil { |
| 54 | return err |
| 55 | } |
| 56 | } |
| 57 | fresh, tag, err := ReadPurgeJournal(ctx, c.Store, account, app, id, appID) |
| 58 | if err != nil { |
| 59 | return err |
| 60 | } |
| 61 | fresh.Cleanup.Fences = true |
| 62 | if _, err = c.Control.SavePurgeJournal(ctx, fresh, tag); err != nil { |
| 63 | return err |
| 64 | } |
| 65 | } |
| 66 | if err = h.CompletePurge(ctx); err != nil { |
| 67 | return err |
| 68 | } |
| 69 | return c.Control.RemovePurgeJournal(ctx, account, app, id) |
| 70 | } |
| 71 | |