| 1 | package team |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "path/filepath" |
| 7 | ) |
| 8 | |
| 9 | // OpenShared borrows the activation writer after the app has migrated it. |
| 10 | // The path must name that same file so every handle uses the same lock. |
| 11 | func OpenShared(ctx context.Context, writer *sql.DB, path string, o Options) (*Store, error) { |
| 12 | if writer == nil || writer.Stats().MaxOpenConnections != 1 || o.Clock == nil || o.Authority == nil { |
| 13 | return nil, fail(CodeInvalid, "shared open needs one writer, a clock and an authority") |
| 14 | } |
| 15 | path, err := canonicalStorePath(path) |
| 16 | if err != nil { |
| 17 | return nil, err |
| 18 | } |
| 19 | if o.Marker == "" { |
| 20 | o.Marker = path + ".incarnation" |
| 21 | } |
| 22 | o.Marker, err = filepath.Abs(o.Marker) |
| 23 | if err != nil { |
| 24 | return nil, err |
| 25 | } |
| 26 | s := &Store{db: writer, path: path, marker: o.Marker, clock: &storeClock{source: o.Clock}, auth: o.Authority, adapters: o.Adapters, provenance: o.Provenance, executorEvidence: o.ExecutorEvidence, renderedApproval: o.RenderedApproval, sourceReference: o.SourceReference, observeWrite: o.ObserveWrite, observerFailed: o.ObserverFailed, commitHook: o.commitHook} |
| 27 | if s.adapters == nil { |
| 28 | s.adapters = map[Operation]Adapter{} |
| 29 | } |
| 30 | if err := s.checkSharedSchema(ctx); err != nil { |
| 31 | return nil, err |
| 32 | } |
| 33 | if err := s.start(ctx); err != nil { |
| 34 | return nil, err |
| 35 | } |
| 36 | return s, nil |
| 37 | } |
| 38 | |
| 39 | // WithTransaction keeps SQL commit and witness publication under the core guard. |
| 40 | // Use the supplied transaction throughout; do not call Store methods inside fn. |
| 41 | func (s *Store) WithTransaction(ctx context.Context, fn func(*sql.Tx) error) error { |
| 42 | if tx := s.bound(ctx); tx != nil { |
| 43 | return fn(tx) |
| 44 | } |
| 45 | s.recoveryMu.Lock() |
| 46 | defer s.recoveryMu.Unlock() |
| 47 | return s.tx(ctx, fn) |
| 48 | } |
| 49 | |
| 50 | func (s *Store) checkSharedSchema(ctx context.Context) error { |
| 51 | return s.withStoreGuard(ctx, func() error { |
| 52 | rows, err := s.db.QueryContext(ctx, `PRAGMA database_list`) |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | var mainPath string |
| 57 | for rows.Next() { |
| 58 | var seq int |
| 59 | var name, path string |
| 60 | if err = rows.Scan(&seq, &name, &path); err != nil { |
| 61 | break |
| 62 | } |
| 63 | if name == "main" { |
| 64 | mainPath = path |
| 65 | } |
| 66 | } |
| 67 | if err == nil { |
| 68 | err = rows.Err() |
| 69 | } |
| 70 | rows.Close() |
| 71 | if err != nil { |
| 72 | return err |
| 73 | } |
| 74 | if mainPath == "" { |
| 75 | return fail(CodeInvalid, "shared writer needs a file database") |
| 76 | } |
| 77 | mainPath, err = canonicalStorePath(mainPath) |
| 78 | if err != nil { |
| 79 | return err |
| 80 | } |
| 81 | if mainPath != s.path { |
| 82 | return fail(CodeInvalid, "shared writer path differs from lock path") |
| 83 | } |
| 84 | var version int |
| 85 | if err := s.db.QueryRowContext(ctx, `SELECT schema_version FROM service WHERE id=1`).Scan(&version); err != nil { |
| 86 | return fail(CodeStaleStore, "shared schema: %v", err) |
| 87 | } |
| 88 | if version != SchemaVersion { |
| 89 | return fail(CodeStaleStore, "shared schema %d, need %d", version, SchemaVersion) |
| 90 | } |
| 91 | for _, query := range sharedSchemaQueries { |
| 92 | rows, err := s.db.QueryContext(ctx, query) |
| 93 | if err != nil { |
| 94 | return fail(CodeStaleStore, "shared schema: %v", err) |
| 95 | } |
| 96 | err = rows.Close() |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | } |
| 101 | for _, index := range sharedSchemaIndexes { |
| 102 | var count int |
| 103 | if err := s.db.QueryRowContext(ctx, `SELECT count(*) FROM sqlite_master WHERE type='index' AND name=?`, index).Scan(&count); err != nil { |
| 104 | return err |
| 105 | } |
| 106 | if count != 1 { |
| 107 | return fail(CodeStaleStore, "shared schema missing index %s", index) |
| 108 | } |
| 109 | } |
| 110 | return nil |
| 111 | }) |
| 112 | } |
| 113 | |
| 114 | var sharedSchemaQueries = []string{ |
| 115 | `SELECT account,person,revision,limits,content_hash FROM coordinator_limits LIMIT 0`, |
| 116 | `SELECT action_id,job_id,request_id,attempt_id,fence,runner,dispatch,expires_at,incarnation FROM admission_tickets LIMIT 0`, |
| 117 | `SELECT release_required,recovery_revision,reconciled_revision,clock_high_water FROM service LIMIT 0`, |
| 118 | `SELECT grant_stale,request_key,request_hash FROM jobs LIMIT 0`, |
| 119 | `SELECT elapsed_unknown FROM time_reservations LIMIT 0`, |
| 120 | `SELECT ref,version,epoch FROM grant_fences LIMIT 0`, |
| 121 | `SELECT key,account,project_id,reserved,actor,reason,incarnation,created_at FROM recovery_charges LIMIT 0`, |
| 122 | `SELECT revision,kind,actor,reason,snapshot_hash FROM recovery_events LIMIT 0`, |
| 123 | `SELECT |
| 124 | actions."id", |
| 125 | actions."job_id", |
| 126 | actions."step_id", |
| 127 | actions."attempt_id", |
| 128 | actions."fence", |
| 129 | actions."key", |
| 130 | actions."operation", |
| 131 | actions."class", |
| 132 | actions."payload_hash", |
| 133 | actions."payload", |
| 134 | actions."state", |
| 135 | actions."reservation_id", |
| 136 | actions."grant_ref", |
| 137 | actions."grant_version", |
| 138 | actions."grant_epoch", |
| 139 | actions."admitted_at", |
| 140 | actions."admission_ends", |
| 141 | actions."receipt", |
| 142 | actions."last_error", |
| 143 | actions."created_at", |
| 144 | actions."updated_at", |
| 145 | actions."admitted_incarnation" |
| 146 | FROM actions LIMIT 0`, |
| 147 | `SELECT |
| 148 | artifacts."id", |
| 149 | artifacts."job_id", |
| 150 | artifacts."step_id", |
| 151 | artifacts."attempt_id", |
| 152 | artifacts."fence", |
| 153 | artifacts."role", |
| 154 | artifacts."hash", |
| 155 | artifacts."media_type", |
| 156 | artifacts."size", |
| 157 | artifacts."provenance", |
| 158 | artifacts."payload", |
| 159 | artifacts."created_at" |
| 160 | FROM artifacts LIMIT 0`, |
| 161 | `SELECT |
| 162 | attempts."id", |
| 163 | attempts."job_id", |
| 164 | attempts."step_id", |
| 165 | attempts."number", |
| 166 | attempts."runner", |
| 167 | attempts."workspace", |
| 168 | attempts."fence", |
| 169 | attempts."lease_deadline", |
| 170 | attempts."state", |
| 171 | attempts."containment", |
| 172 | attempts."stop_requested", |
| 173 | attempts."stop_acked", |
| 174 | attempts."provider", |
| 175 | attempts."model", |
| 176 | attempts."effort", |
| 177 | attempts."outcome", |
| 178 | attempts."created_at", |
| 179 | attempts."updated_at" |
| 180 | FROM attempts LIMIT 0`, |
| 181 | `SELECT |
| 182 | events."job_id", |
| 183 | events."seq", |
| 184 | events."type", |
| 185 | events."actor", |
| 186 | events."payload", |
| 187 | events."source_at" |
| 188 | FROM events LIMIT 0`, |
| 189 | `SELECT |
| 190 | jobs."id", |
| 191 | jobs."account", |
| 192 | jobs."parent", |
| 193 | jobs."depth", |
| 194 | jobs."requester", |
| 195 | jobs."target", |
| 196 | jobs."evidence", |
| 197 | jobs."policy_version", |
| 198 | jobs."policy", |
| 199 | jobs."grant_ref", |
| 200 | jobs."grant_version", |
| 201 | jobs."grant_epoch", |
| 202 | jobs."grant_observed_at", |
| 203 | jobs."state", |
| 204 | jobs."wait", |
| 205 | jobs."revision", |
| 206 | jobs."outcome", |
| 207 | jobs."created_at", |
| 208 | jobs."updated_at", |
| 209 | jobs."project_id" |
| 210 | FROM jobs LIMIT 0`, |
| 211 | `SELECT |
| 212 | period_budgets."account", |
| 213 | period_budgets."revision", |
| 214 | period_budgets."config", period_budgets."content_hash" |
| 215 | FROM period_budgets LIMIT 0`, |
| 216 | `SELECT |
| 217 | reservations."id", |
| 218 | reservations."account", |
| 219 | reservations."job_id", |
| 220 | reservations."step_id", |
| 221 | reservations."attempt_id", |
| 222 | reservations."action_id", |
| 223 | reservations."r_calls", |
| 224 | reservations."r_input", |
| 225 | reservations."r_output", |
| 226 | reservations."r_cost", |
| 227 | reservations."s_calls", |
| 228 | reservations."s_input", |
| 229 | reservations."s_output", |
| 230 | reservations."s_cost", |
| 231 | reservations."state", |
| 232 | reservations."receipt_id", |
| 233 | reservations."settled_by", |
| 234 | reservations."created_at", |
| 235 | reservations."settled_at", reservations."project_id", reservations."period", reservations."cost_unavailable", reservations."s_cached" |
| 236 | FROM reservations LIMIT 0`, |
| 237 | `SELECT |
| 238 | service."id", |
| 239 | service."schema_version", |
| 240 | service."incarnation", |
| 241 | service."revision", |
| 242 | service."restore_uncertain", |
| 243 | service."admission_paused", |
| 244 | service."quarantine", |
| 245 | service."quarantine_why" |
| 246 | FROM service LIMIT 0`, |
| 247 | `SELECT |
| 248 | steps."id", |
| 249 | steps."job_id", |
| 250 | steps."key", |
| 251 | steps."capability", |
| 252 | steps."version", |
| 253 | steps."depends_on", |
| 254 | steps."state", |
| 255 | steps."fence", |
| 256 | steps."holder", |
| 257 | steps."attempts" |
| 258 | FROM steps LIMIT 0`, |
| 259 | `SELECT |
| 260 | time_reservations."attempt_id", |
| 261 | time_reservations."account", |
| 262 | time_reservations."project_id", |
| 263 | time_reservations."job_id", |
| 264 | time_reservations."period", |
| 265 | time_reservations."reserved_seconds", |
| 266 | time_reservations."settled_seconds", |
| 267 | time_reservations."state", |
| 268 | time_reservations."settled_at" |
| 269 | FROM time_reservations LIMIT 0`, |
| 270 | } |
| 271 | |
| 272 | var sharedSchemaIndexes = []string{ |
| 273 | "reservations_period_account", |
| 274 | "reservations_period_project", |
| 275 | "time_period_account", |
| 276 | "time_period_project", |
| 277 | "recovery_charges_scope", |
| 278 | "actions_state", |
| 279 | "artifacts_job", |
| 280 | "attempts_job", |
| 281 | "attempts_one_live", |
| 282 | "jobs_account", |
| 283 | "jobs_request", |
| 284 | "reservations_account", |
| 285 | "reservations_job", |
| 286 | "time_reservations_account", |
| 287 | } |
| 288 | |