Documentation
¶
Index ¶
- type AdjustmentRecord
- type BillingPeriod
- type CompleteRunParams
- type ConsecutiveFailedRunsRow
- type CorrectionDelta
- type CreateAdjustmentRecordsParams
- type CreateCorrectionDeltasParams
- type CreateRatedRecordsParams
- type CreateUsageRecordsParams
- type DBTX
- type FailRunParams
- type FinalizeBillingPeriodParams
- type InsertPricingModelParams
- type InsertProjectStatementParams
- type InsertRunParams
- type InsertRunRow
- type LatestFinalizedRunRow
- type ListBillingPeriodsRow
- type ListCorrectionDeltasRow
- type ListPricingModelsRow
- type ListRatedRecordsRow
- type PricingModel
- type ProjectStatement
- type Queries
- func (q *Queries) CompleteRun(ctx context.Context, arg CompleteRunParams) (int64, error)
- func (q *Queries) ConsecutiveFailedRuns(ctx context.Context, periodFrom pgtype.Timestamptz) (ConsecutiveFailedRunsRow, error)
- func (q *Queries) CreateAdjustmentRecords(ctx context.Context, arg []CreateAdjustmentRecordsParams) (int64, error)
- func (q *Queries) CreateCorrectionDeltas(ctx context.Context, arg []CreateCorrectionDeltasParams) (int64, error)
- func (q *Queries) CreateRatedRecords(ctx context.Context, arg []CreateRatedRecordsParams) (int64, error)
- func (q *Queries) CreateUsageRecords(ctx context.Context, arg []CreateUsageRecordsParams) (int64, error)
- func (q *Queries) EarliestBillingPeriod(ctx context.Context) (pgtype.Timestamptz, error)
- func (q *Queries) EarliestPricingValidFrom(ctx context.Context) (pgtype.Timestamptz, error)
- func (q *Queries) FailRun(ctx context.Context, arg FailRunParams) (int64, error)
- func (q *Queries) FinalizeBillingPeriod(ctx context.Context, arg FinalizeBillingPeriodParams) error
- func (q *Queries) FinalizeRun(ctx context.Context, id pgtype.UUID) error
- func (q *Queries) GetBillingPeriod(ctx context.Context, periodFrom pgtype.Timestamptz) (BillingPeriod, error)
- func (q *Queries) GetPricingModel(ctx context.Context, version string) (PricingModel, error)
- func (q *Queries) GetRun(ctx context.Context, id pgtype.UUID) (Run, error)
- func (q *Queries) GetRunForUpdate(ctx context.Context, id pgtype.UUID) (Run, error)
- func (q *Queries) HasCompleteRegularRun(ctx context.Context, periodFrom pgtype.Timestamptz) (bool, error)
- func (q *Queries) InsertPricingModel(ctx context.Context, arg InsertPricingModelParams) (int64, error)
- func (q *Queries) InsertProjectStatement(ctx context.Context, arg InsertProjectStatementParams) error
- func (q *Queries) InsertRun(ctx context.Context, arg InsertRunParams) (InsertRunRow, error)
- func (q *Queries) LatestCompletedRegularRun(ctx context.Context, periodFrom pgtype.Timestamptz) (pgtype.UUID, error)
- func (q *Queries) LatestFinalizedRun(ctx context.Context, periodFrom pgtype.Timestamptz) (LatestFinalizedRunRow, error)
- func (q *Queries) ListAdjustmentRecords(ctx context.Context, runID pgtype.UUID) ([]AdjustmentRecord, error)
- func (q *Queries) ListBillingPeriods(ctx context.Context) ([]ListBillingPeriodsRow, error)
- func (q *Queries) ListCorrectionDeltas(ctx context.Context, runID pgtype.UUID) ([]ListCorrectionDeltasRow, error)
- func (q *Queries) ListPricingModels(ctx context.Context) ([]ListPricingModelsRow, error)
- func (q *Queries) ListProjectStatements(ctx context.Context, runID pgtype.UUID) ([]ProjectStatement, error)
- func (q *Queries) ListRatedRecords(ctx context.Context, runID pgtype.UUID) ([]ListRatedRecordsRow, error)
- func (q *Queries) LockRun(ctx context.Context, id pgtype.UUID) (pgtype.UUID, error)
- func (q *Queries) PricingModelForPeriod(ctx context.Context, validFrom pgtype.Timestamptz) (PricingModel, error)
- func (q *Queries) ReclaimStaleRuns(ctx context.Context, periodFrom pgtype.Timestamptz) ([]pgtype.UUID, error)
- func (q *Queries) SetBillingPeriodStatus(ctx context.Context, arg SetBillingPeriodStatusParams) error
- func (q *Queries) SumRatedByRun(ctx context.Context, runID pgtype.UUID) ([]SumRatedByRunRow, error)
- func (q *Queries) SupersedeCompletedRuns(ctx context.Context, arg SupersedeCompletedRunsParams) ([]pgtype.UUID, error)
- func (q *Queries) TryLockPeriod(ctx context.Context, periodFrom string) (bool, error)
- func (q *Queries) UnlockPeriod(ctx context.Context, periodFrom string) (bool, error)
- func (q *Queries) UpsertBillingPeriod(ctx context.Context, arg UpsertBillingPeriodParams) error
- func (q *Queries) WithTx(tx pgx.Tx) *Queries
- type RatedRecord
- type Run
- type SetBillingPeriodStatusParams
- type SumRatedByRunRow
- type SupersedeCompletedRunsParams
- type UpsertBillingPeriodParams
- type UsageRecord
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AdjustmentRecord ¶
type BillingPeriod ¶
type BillingPeriod struct {
PeriodFrom pgtype.Timestamptz
PeriodTo pgtype.Timestamptz
Status string
FinalizedRunID pgtype.UUID
FinalizedAt pgtype.Timestamptz
}
type CompleteRunParams ¶
type ConsecutiveFailedRunsRow ¶
type ConsecutiveFailedRunsRow struct {
Failures int32
LastFailure pgtype.Timestamptz
}
type CorrectionDelta ¶
type DBTX ¶
type DBTX interface {
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
QueryRow(context.Context, string, ...interface{}) pgx.Row
CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)
}
type FailRunParams ¶
type FinalizeBillingPeriodParams ¶
type FinalizeBillingPeriodParams struct {
PeriodFrom pgtype.Timestamptz
FinalizedRunID pgtype.UUID
}
type InsertPricingModelParams ¶
type InsertPricingModelParams struct {
Version string
ValidFrom pgtype.Timestamptz
Currency string
Document []byte
}
type InsertRunParams ¶
type InsertRunParams struct {
PeriodFrom pgtype.Timestamptz
PeriodTo pgtype.Timestamptz
Kind string
CorrectsRunID pgtype.UUID
PricingVersion pgtype.Text
Clouds []string
}
type InsertRunRow ¶
type InsertRunRow struct {
ID pgtype.UUID
StartedAt pgtype.Timestamptz
}
type LatestFinalizedRunRow ¶
type ListBillingPeriodsRow ¶
type ListBillingPeriodsRow struct {
PeriodFrom pgtype.Timestamptz
Status string
FinalizedRunID pgtype.UUID
FinalizedAt pgtype.Timestamptz
}
type ListCorrectionDeltasRow ¶
type ListPricingModelsRow ¶
type ListPricingModelsRow struct {
Version string
ValidFrom pgtype.Timestamptz
Currency string
ImportedAt pgtype.Timestamptz
}
type ListRatedRecordsRow ¶
type PricingModel ¶
type PricingModel struct {
Version string
ValidFrom pgtype.Timestamptz
Currency string
Document []byte
ImportedAt pgtype.Timestamptz
}
type ProjectStatement ¶
type Queries ¶
type Queries struct {
// contains filtered or unexported fields
}
func (*Queries) CompleteRun ¶
The three ends of a run. stats carries what the run counted, and the failed one keeps it: a run that broke halfway is read for how far it got.
The two ends of a run in flight name the status they move from and report how many rows they moved. A run this process opened can have been failed underneath it by the reclaim below, and putting it back would return a run the period has already replaced to that period's numbers: trg_runs_immutable fires on OLD.status = 'finalized' alone, so 'failed' -> 'completed' passes the database untouched. The caller reads the count and refuses rather than writing over a status another process settled.
func (*Queries) ConsecutiveFailedRuns ¶
func (q *Queries) ConsecutiveFailedRuns(ctx context.Context, periodFrom pgtype.Timestamptz) (ConsecutiveFailedRunsRow, error)
How often the metering of a period failed in a row: the failed regular runs it carries since the last one that got to the end, and when the last of them stopped. Without this the tick meters a month again on every hourly pass for as long as it keeps failing, and a month that never stops failing -- an event ordering its history carries and no later event repairs -- costs a full metering pass and another stats blob every hour for the life of the deployment. The scheduler spaces its retries out over this count.
func (*Queries) CreateAdjustmentRecords ¶
func (q *Queries) CreateAdjustmentRecords(ctx context.Context, arg []CreateAdjustmentRecordsParams) (int64, error)
The adjustment rows of a run, over the same protocol and naming id for the same reason: COPY evaluates no defaults.
func (*Queries) CreateCorrectionDeltas ¶
func (*Queries) CreateRatedRecords ¶
func (*Queries) CreateUsageRecords ¶
func (q *Queries) CreateUsageRecords(ctx context.Context, arg []CreateUsageRecordsParams) (int64, error)
The record writes of a run, over the COPY protocol: a month of metering is tens of thousands of rows, and one round trip per row is what that costs. All three name id rather than leaving it to the column default, because COPY evaluates no defaults. The caller generating the ids is also what lets a rated record name the usage record it was rated from before either row is written.
func (*Queries) EarliestBillingPeriod ¶
The first period the engine knows, where the scheduler starts its walk. The aggregate answers over an empty table too, with one NULL row.
func (*Queries) EarliestPricingValidFrom ¶ added in v0.5.0
The first instant any pricing model is valid from, which bounds the scheduler's walk from below: a month that begins before it is priced by no model. The aggregate answers over an empty table too, with one NULL row.
func (*Queries) FinalizeBillingPeriod ¶
func (q *Queries) FinalizeBillingPeriod(ctx context.Context, arg FinalizeBillingPeriodParams) error
Closes a period. The three columns move in one statement because the check constraint on billing_periods ties them together: a period is finalized exactly when it names the run that closed it and the time that happened.
func (*Queries) FinalizeRun ¶
func (*Queries) GetBillingPeriod ¶
func (q *Queries) GetBillingPeriod(ctx context.Context, periodFrom pgtype.Timestamptz) (BillingPeriod, error)
func (*Queries) GetPricingModel ¶
func (*Queries) GetRun ¶
The same eleven columns without a FOR clause. An export only reads a run, so it takes no row lock: the FOR NO KEY UPDATE above would queue the export behind a run being finalized, and hold that finalization up in turn.
func (*Queries) GetRunForUpdate ¶
Reads a run under that same lock, which is how finalization sees a status that no concurrent writer can move underneath it.
func (*Queries) HasCompleteRegularRun ¶
func (q *Queries) HasCompleteRegularRun(ctx context.Context, periodFrom pgtype.Timestamptz) (bool, error)
Whether the period already has a regular run that got to the end. A finalized run counts as one: it is a completed run that closed its period.
func (*Queries) InsertPricingModel ¶
func (*Queries) InsertProjectStatement ¶
func (q *Queries) InsertProjectStatement(ctx context.Context, arg InsertProjectStatementParams) error
func (*Queries) InsertRun ¶
func (q *Queries) InsertRun(ctx context.Context, arg InsertRunParams) (InsertRunRow, error)
Opens a run. status stays at the column default 'running' and completed_at stays null until the run ends. The caller writes its records under the returned id and reports the returned start time. A regular run binds NULL for corrects_run_id, which is an invalid pgtype.UUID; a correction binds the run it corrects.
func (*Queries) LatestCompletedRegularRun ¶
func (q *Queries) LatestCompletedRegularRun(ctx context.Context, periodFrom pgtype.Timestamptz) (pgtype.UUID, error)
The run a period's current numbers come from. Superseding leaves at most one completed regular run per period, and the ordering settles which one a reader takes if it ever sees more.
func (*Queries) LatestFinalizedRun ¶
func (q *Queries) LatestFinalizedRun(ctx context.Context, periodFrom pgtype.Timestamptz) (LatestFinalizedRunRow, error)
The latest finalized truth of a period, which a correction diffs against and then names in corrects_run_id: the regular run that closed the period for the first correction, the last finalized correction after that (roadmap WP 3.9, item 5). detect-late reads the snapshot time out of its stats.
func (*Queries) ListAdjustmentRecords ¶
func (q *Queries) ListAdjustmentRecords(ctx context.Context, runID pgtype.UUID) ([]AdjustmentRecord, error)
The adjustments a run applied, in a total order over the rows one run writes: a project's adjustments come from distinct relations, and one relation contributes at most one line per type and scope. A correction diffs these rows against its own, which is what the order is for.
func (*Queries) ListBillingPeriods ¶
func (q *Queries) ListBillingPeriods(ctx context.Context) ([]ListBillingPeriodsRow, error)
func (*Queries) ListCorrectionDeltas ¶
func (q *Queries) ListCorrectionDeltas(ctx context.Context, runID pgtype.UUID) ([]ListCorrectionDeltasRow, error)
The delta rows of a correction run, in the order corrections.Diff sorts by (internal/engine/corrections/corrections.go), so an export prints them as the correction computed them. idx_delta_run serves the filter.
func (*Queries) ListPricingModels ¶
func (q *Queries) ListPricingModels(ctx context.Context) ([]ListPricingModelsRow, error)
func (*Queries) ListProjectStatements ¶
func (*Queries) ListRatedRecords ¶
func (q *Queries) ListRatedRecords(ctx context.Context, runID pgtype.UUID) ([]ListRatedRecordsRow, error)
One row per rated record of a run, joined to the usage record it rates. The ordering is a total one: the drafts of one resource never overlap (draftsOf in internal/engine/metering/metering.go builds them from the intervals of a folded history), so from_ts is unique per resource within a run, and rating emits one record per usage record and dimension. That total order is what makes two exports of one run byte-identical. idx_rated_run serves the filter.
func (*Queries) LockRun ¶
The first statement of a transaction that writes records. The trigger on the record tables locks the run FOR SHARE for every row it checks, and a transaction holding that lock which then updates the same run row has to escalate to FOR NO KEY UPDATE, which deadlocks two writers of one run. Taking the stronger lock up front is the ordering the comment on forbid_finalized_mutation in migration 0001 asks for.
func (*Queries) PricingModelForPeriod ¶
func (q *Queries) PricingModelForPeriod(ctx context.Context, validFrom pgtype.Timestamptz) (PricingModel, error)
func (*Queries) ReclaimStaleRuns ¶
func (q *Queries) ReclaimStaleRuns(ctx context.Context, periodFrom pgtype.Timestamptz) ([]pgtype.UUID, error)
Reclaims the runs of a period that are still in flight with no process behind them. A killed process leaves its run at 'running' forever, and the period is blocked for as long as that row reads as a run in progress. The reason is merged into stats rather than written over it, so whatever the run had already counted survives.
Age is what stands for the missing process, and the period lock cannot: that lock is session scoped on one pooled connection which stays protocol-idle for the whole run, so anything that closes only that connection -- an idle timeout, a reaper, a failover -- releases it while the process keeps metering. Two hours is longer than a run that is still alive: the CronJob's Job is killed after fifty minutes (activeDeadlineSeconds), and a month an operator meters by hand is minutes of work. Waiting costs nothing, because no query counts a 'running' row.
func (*Queries) SetBillingPeriodStatus ¶
func (q *Queries) SetBillingPeriodStatus(ctx context.Context, arg SetBillingPeriodStatusParams) error
func (*Queries) SumRatedByRun ¶
The amounts of one run summed per the key a correction diffs by (decision D6: cloud, platform, resource type, resource id, project id, dimension). The currency is grouped too, so a run rated in one currency yields one row per key. idx_rated_run serves the filter.
func (*Queries) SupersedeCompletedRuns ¶
func (q *Queries) SupersedeCompletedRuns(ctx context.Context, arg SupersedeCompletedRunsParams) ([]pgtype.UUID, error)
Retires the completed runs of the given kind that a new run of that kind replaces, so that a period carries at most one completed run per kind and a sum over that kind counts one set of records. It runs inside the new run's write transaction while that run is still 'running', so the filter on 'completed' never matches the run doing the superseding.
func (*Queries) TryLockPeriod ¶
One process at a time meters a period. The lock is an advisory one rather than a row lock, because it is taken before the billing_periods row of the period exists. Its key is hashed from the period start as text, which the caller renders in UTC and RFC 3339, so that the same instant always reaches the same key: a timestamptz argument would be rendered by the server and the key would follow that session's DateStyle and TimeZone.
func (*Queries) UnlockPeriod ¶
Releases what TryLockPeriod took. An advisory lock is released by key, so the key expression has to stay identical to the one above.
func (*Queries) UpsertBillingPeriod ¶
func (q *Queries) UpsertBillingPeriod(ctx context.Context, arg UpsertBillingPeriodParams) error
Records that a period exists. The scheduler walks the months it is responsible for and calls this for each of them, so a period already known keeps the status and the finalization columns it has.
type RatedRecord ¶
type Run ¶
type Run struct {
ID pgtype.UUID
PeriodFrom pgtype.Timestamptz
PeriodTo pgtype.Timestamptz
Kind string
CorrectsRunID pgtype.UUID
PricingVersion pgtype.Text
Status string
Clouds []string
Stats []byte
StartedAt pgtype.Timestamptz
CompletedAt pgtype.Timestamptz
}
type SetBillingPeriodStatusParams ¶
type SetBillingPeriodStatusParams struct {
PeriodFrom pgtype.Timestamptz
Status string
}
type SumRatedByRunRow ¶
type SupersedeCompletedRunsParams ¶
type SupersedeCompletedRunsParams struct {
PeriodFrom pgtype.Timestamptz
Kind string
}
type UpsertBillingPeriodParams ¶
type UpsertBillingPeriodParams struct {
PeriodFrom pgtype.Timestamptz
PeriodTo pgtype.Timestamptz
}