Skip to content

Commit

Permalink
sql: create jobs for truncated and dropped tables
Browse files Browse the repository at this point in the history
Creates a job for statements involving dropping or truncated tables, including
DROP DATABASE. The job is completed when the GC TTL expires and both table
data and ID is deleted for each of the tables involved.

Detailed running statuses are added to provide visibility to the
progress of the dropping or truncating of tables. This is surfaced by
adding an additional status field to the payload proto of jobs, and
concatenated to the running status when populating the interal jobs
table.

For dropping or truncating jobs, the detailed running status is
determined by the status of the table at the earliest stage of the
schema change.

Fixes cockroachdb#19004

Release note: None
  • Loading branch information
Erik Trinh committed Sep 18, 2018
1 parent 2ddf654 commit 2e9450f
Show file tree
Hide file tree
Showing 17 changed files with 1,099 additions and 349 deletions.
40 changes: 40 additions & 0 deletions pkg/jobs/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ type Record struct {
// Status represents the status of a job in the system.jobs table.
type Status string

// RunningStatus represents the more detailed status of a running job in
// the system.jobs table.
type RunningStatus string

const (
// StatusPending is for jobs that have been created but on which work has
// not yet started.
Expand All @@ -78,6 +82,18 @@ const (
// StatusCanceled is for jobs that were explicitly canceled by the user and
// cannot be resumed.
StatusCanceled Status = "canceled"
// RunningStatusGeneral is for jobs that are currently in progress and do
// not have other detailed status information.
RunningStatusGeneral RunningStatus = ""
// RunningStatusDrainingNames is for jobs that are currently in progress and
// are draining names.
RunningStatusDrainingNames RunningStatus = "draining names"
// RunningStatusWaitingForGC is for jobs that are currently in progress and
// are waiting for the GC interval to expire
RunningStatusWaitingGC RunningStatus = "waiting for GC TTL"
// RunningStatusWaitingForGC is for jobs that are currently in progress and
// undergoing RocksDB compaction
RunningStatusCompaction RunningStatus = "RocksDB compaction"
)

// Terminal returns whether this status represents a "terminal" state: a state
Expand Down Expand Up @@ -138,6 +154,30 @@ func (j *Job) Started(ctx context.Context) error {
})
}

// RunningStatus updates the detailed status of a job currently in progress.
// It sets the job's RunningStatus field to the value returned by runningStatusFn
// and persists runningStatusFn's modifications to the job's details, if any.
func (j *Job) RunningStatus(ctx context.Context, runningStatusFn RunningStatusFn) error {
return j.updateRow(ctx, updateProgressAndDetails,
func(_ *client.Txn, status *Status, payload *jobspb.Payload, progress *jobspb.Progress) (bool, error) {
if *status != StatusRunning {
return false, &InvalidStatusError{*j.id, *status, "update progress on", payload.Error}
}
runningStatus, err := runningStatusFn(ctx, progress.Details)
if err != nil {
return false, err
}
payload.RunningStatus = string(runningStatus)
return true, nil
},
)
}

// RunningStatusFn is a callback that computes a job's running status
// given its details. It is safe to modify details in the callback; those
// modifications will be automatically persisted to the database record.
type RunningStatusFn func(ctx context.Context, details jobspb.Details) (RunningStatus, error)

// FractionProgressedFn is a callback that computes a job's completion fraction
// given its details. It is safe to modify details in the callback; those
// modifications will be automatically persisted to the database record.
Expand Down
Loading

0 comments on commit 2e9450f

Please sign in to comment.