| 1 | package team |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | const admissionSchema = ` |
| 12 | CREATE TABLE admission_tickets ( |
| 13 | action_id INTEGER PRIMARY KEY REFERENCES actions(id), |
| 14 | job_id INTEGER NOT NULL REFERENCES jobs(id), |
| 15 | request_id TEXT NOT NULL, |
| 16 | attempt_id INTEGER NOT NULL REFERENCES attempts(id), |
| 17 | fence INTEGER NOT NULL, |
| 18 | runner TEXT NOT NULL, |
| 19 | dispatch TEXT NOT NULL, |
| 20 | expires_at TEXT NOT NULL, |
| 21 | incarnation INTEGER NOT NULL, |
| 22 | UNIQUE(job_id, request_id) |
| 23 | ); |
| 24 | UPDATE service SET schema_version=7 WHERE id=1; |
| 25 | ` |
| 26 | |
| 27 | type AdmissionTicket struct { |
| 28 | Scope Scope `json:"scope"` |
| 29 | Handle Handle `json:"handle"` |
| 30 | Dispatch Dispatch `json:"dispatch"` |
| 31 | ExpiresAt time.Time `json:"expires_at"` |
| 32 | Incarnation int64 `json:"incarnation"` |
| 33 | } |
| 34 | |
| 35 | type transactionBinding struct { |
| 36 | store *Store |
| 37 | tx *sql.Tx |
| 38 | } |
| 39 | type transactionKey struct{} |
| 40 | |
| 41 | func (s *Store) bound(ctx context.Context) *sql.Tx { |
| 42 | b, _ := ctx.Value(transactionKey{}).(transactionBinding) |
| 43 | if b.store == s { |
| 44 | return b.tx |
| 45 | } |
| 46 | return nil |
| 47 | } |
| 48 | |
| 49 | // Atomic binds nested core operations to one guarded commit. Callbacks must not |
| 50 | // retain the context or start goroutines with it. |
| 51 | func (s *Store) Atomic(ctx context.Context, fn func(context.Context, *sql.Tx) error) error { |
| 52 | if tx := s.bound(ctx); tx != nil { |
| 53 | return fn(ctx, tx) |
| 54 | } |
| 55 | err := s.WithTransaction(ctx, func(tx *sql.Tx) error { |
| 56 | return fn(context.WithValue(ctx, transactionKey{}, transactionBinding{s, tx}), tx) |
| 57 | }) |
| 58 | var denial *authorityDenial |
| 59 | if errors.As(err, &denial) { |
| 60 | return s.commitAuthorityDenial(ctx, denial) |
| 61 | } |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | func (s *Store) AdmitOnce(ctx context.Context, scope Scope, h Handle, id ActionID, requestID string) (AdmissionTicket, error) { |
| 66 | if requestID == "" || len(requestID) > 256 { |
| 67 | return AdmissionTicket{}, ErrInvalid |
| 68 | } |
| 69 | var ticket AdmissionTicket |
| 70 | err := s.Atomic(ctx, func(ctx context.Context, tx *sql.Tx) error { |
| 71 | if err := admissionOpen(ctx, tx); err != nil { |
| 72 | return err |
| 73 | } |
| 74 | hd, err := current(ctx, tx, scope, h, s.clock.Now()) |
| 75 | if err != nil { |
| 76 | return err |
| 77 | } |
| 78 | a, err := loadAction(ctx, tx, scope.Job, id) |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | if a.Attempt != h.Attempt || a.Fence != h.Fence { |
| 83 | return ErrFence |
| 84 | } |
| 85 | var request, runner, raw, expiry string |
| 86 | var attempt AttemptID |
| 87 | var fence, inc int64 |
| 88 | err = tx.QueryRowContext(ctx, `SELECT request_id,attempt_id,fence,runner,dispatch,expires_at,incarnation FROM admission_tickets WHERE action_id=?`, id).Scan(&request, &attempt, &fence, &runner, &raw, &expiry, &inc) |
| 89 | if errors.Is(err, sql.ErrNoRows) { |
| 90 | var other int |
| 91 | if err := tx.QueryRowContext(ctx, `SELECT count(*) FROM admission_tickets WHERE job_id=? AND request_id=?`, scope.Job, requestID).Scan(&other); err != nil { |
| 92 | return err |
| 93 | } |
| 94 | if other != 0 { |
| 95 | return ErrConflict |
| 96 | } |
| 97 | dispatch, err := s.Admit(ctx, scope, id) |
| 98 | if err != nil { |
| 99 | return err |
| 100 | } |
| 101 | a, err = loadAction(ctx, tx, scope.Job, id) |
| 102 | if err != nil { |
| 103 | return err |
| 104 | } |
| 105 | deadline := a.AdmissionEnds |
| 106 | if max := s.clock.Now().Add(5 * time.Second); deadline.After(max) { |
| 107 | deadline = max |
| 108 | } |
| 109 | ticket = AdmissionTicket{Dispatch: dispatch, Scope: scope, Handle: h, ExpiresAt: deadline, Incarnation: s.incarnation} |
| 110 | data, err := json.Marshal(dispatch) |
| 111 | if err != nil { |
| 112 | return err |
| 113 | } |
| 114 | _, err = tx.ExecContext(ctx, `INSERT INTO admission_tickets(action_id,job_id,request_id,attempt_id,fence,runner,dispatch,expires_at,incarnation) VALUES(?,?,?,?,?,?,?,?,?)`, id, scope.Job, requestID, h.Attempt, h.Fence, h.Runner, string(data), ts(deadline), s.incarnation) |
| 115 | return err |
| 116 | } |
| 117 | if err != nil { |
| 118 | return err |
| 119 | } |
| 120 | if request != requestID || attempt != h.Attempt || fence != h.Fence || runner != h.Runner { |
| 121 | return ErrConflict |
| 122 | } |
| 123 | if inc != s.incarnation { |
| 124 | return ErrStaleStore |
| 125 | } |
| 126 | if !s.clock.Now().Before(parseTS(expiry)) { |
| 127 | return ErrAdmissionExpired |
| 128 | } |
| 129 | if a.State != ActionDispatching || hd.job.State != JobRunning { |
| 130 | return ErrState |
| 131 | } |
| 132 | adm, err := s.auth.Introspect(ctx, AdmissionRequest{Grant: hd.job.Grant.Ref, Account: hd.job.Account, Project: hd.job.Project, Target: hd.job.Target, Operation: a.Operation}) |
| 133 | if err != nil { |
| 134 | return s.observedRevocation(ctx, scope, err) |
| 135 | } |
| 136 | if err := s.fenceObserved(ctx, hd.job.Grant, adm); err != nil { |
| 137 | return err |
| 138 | } |
| 139 | if err := checkAdmission(hd.job, adm, s.clock.Now()); err != nil { |
| 140 | return err |
| 141 | } |
| 142 | if err := checkBudget(ctx, tx, hd.job, Usage{}, s.clock.Now()); err != nil { |
| 143 | return err |
| 144 | } |
| 145 | r, err := loadReservation(ctx, tx, a.Reservation) |
| 146 | if err != nil { |
| 147 | return err |
| 148 | } |
| 149 | if r.State != ReservationOpen { |
| 150 | return ErrState |
| 151 | } |
| 152 | if err = json.Unmarshal([]byte(raw), &ticket.Dispatch); err != nil { |
| 153 | return err |
| 154 | } |
| 155 | if ticket.Dispatch.Hash != a.PayloadHash || ticket.Dispatch.Operation != a.Operation || string(ticket.Dispatch.Payload) != string(a.Payload) { |
| 156 | return ErrConflict |
| 157 | } |
| 158 | if a.Operation == OpForgePublish { |
| 159 | if err := s.checkPublication(ctx, tx, hd.job, a.Payload); err != nil { |
| 160 | return err |
| 161 | } |
| 162 | if hd.job.Policy.Version == 2 { |
| 163 | observed, err := s.observedProvenance(ctx, tx, scope, h) |
| 164 | if err != nil { |
| 165 | return err |
| 166 | } |
| 167 | var p publication |
| 168 | if strictJSON(a.Payload, &p) != nil || !isolationContains(p.Isolation, observed.Isolation) { |
| 169 | return ErrEvidence |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | ticket.Scope = scope |
| 174 | ticket.Handle = h |
| 175 | ticket.ExpiresAt = parseTS(expiry) |
| 176 | ticket.Incarnation = inc |
| 177 | return nil |
| 178 | }) |
| 179 | if err != nil { |
| 180 | return AdmissionTicket{}, err |
| 181 | } |
| 182 | return ticket, nil |
| 183 | } |
| 184 | |
| 185 | // ValidateHandle checks current authority without renewing the lease. |
| 186 | func (s *Store) ValidateHandle(ctx context.Context, scope Scope, h Handle) error { |
| 187 | return s.read(ctx, func(tx *sql.Tx) error { _, err := current(ctx, tx, scope, h, s.clock.Now()); return err }) |
| 188 | } |
| 189 | |
| 190 | // ContainUntrusted releases process capacity but retains the time reservation. |
| 191 | func (s *Store) ContainUntrusted(ctx context.Context, scope Scope, h Handle) error { |
| 192 | return s.Atomic(ctx, func(ctx context.Context, tx *sql.Tx) error { |
| 193 | a, err := loadAttempt(ctx, tx, scope.Job, h.Attempt) |
| 194 | if err != nil { |
| 195 | return err |
| 196 | } |
| 197 | if a.Fence != h.Fence || a.Runner != h.Runner { |
| 198 | return ErrFence |
| 199 | } |
| 200 | if a.Containment == ContainmentContained { |
| 201 | return nil |
| 202 | } |
| 203 | if a.State == AttemptActive { |
| 204 | if _, err := tx.ExecContext(ctx, `UPDATE attempts SET state='stopping',stop_requested=? WHERE id=?`, ts(s.clock.Now()), a.ID); err != nil { |
| 205 | return err |
| 206 | } |
| 207 | if _, err := tx.ExecContext(ctx, `UPDATE steps SET fence=fence+1 WHERE id=? AND holder=? AND fence=?`, a.Step, a.ID, a.Fence); err != nil { |
| 208 | return err |
| 209 | } |
| 210 | } |
| 211 | if _, err := tx.ExecContext(ctx, `UPDATE time_reservations SET elapsed_unknown=1 WHERE attempt_id=?`, h.Attempt); err != nil { |
| 212 | return err |
| 213 | } |
| 214 | return s.Contain(ctx, scope, h.Attempt, "runner-validator") |
| 215 | }) |
| 216 | } |
| 217 | |
| 218 | func (s *Store) AdmissionAvailable(ctx context.Context) error { |
| 219 | return s.read(ctx, func(tx *sql.Tx) error { return admissionOpen(ctx, tx) }) |
| 220 | } |
| 221 | |