-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
job_exec_context.go
122 lines (113 loc) · 4.69 KB
/
job_exec_context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package sql
import (
"context"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/security/username"
"github.com/cockroachdb/cockroach/pkg/spanconfig"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/lease"
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"
"github.com/cockroachdb/cockroach/pkg/sql/sqlutil"
"github.com/cockroachdb/cockroach/pkg/upgrade"
)
// plannerJobExecContext is a wrapper to implement JobExecContext with a planner
// without allowing casting directly to a planner. Eventually it would be nice
// if we could implement the API entirely without a planner however the only
// implementation of extendedEvalContext is very tied to a planner.
type plannerJobExecContext struct {
p *planner
}
// MakeJobExecContext makes a JobExecContext.
func MakeJobExecContext(
opName string, user username.SQLUsername, memMetrics *MemoryMetrics, execCfg *ExecutorConfig,
) (JobExecContext, func()) {
plannerInterface, close := NewInternalPlanner(
opName,
nil, /*txn*/
user,
memMetrics,
execCfg,
sessiondatapb.SessionData{},
)
p := plannerInterface.(*planner)
return &plannerJobExecContext{p: p}, close
}
func (e *plannerJobExecContext) SemaCtx() *tree.SemaContext { return e.p.SemaCtx() }
func (e *plannerJobExecContext) ExtendedEvalContext() *extendedEvalContext {
return e.p.ExtendedEvalContext()
}
func (e *plannerJobExecContext) SessionData() *sessiondata.SessionData {
return e.p.SessionData()
}
func (e *plannerJobExecContext) SessionDataMutatorIterator() *sessionDataMutatorIterator {
return e.p.SessionDataMutatorIterator()
}
func (e *plannerJobExecContext) ExecCfg() *ExecutorConfig { return e.p.ExecCfg() }
func (e *plannerJobExecContext) DistSQLPlanner() *DistSQLPlanner { return e.p.DistSQLPlanner() }
func (e *plannerJobExecContext) LeaseMgr() *lease.Manager { return e.p.LeaseMgr() }
func (e *plannerJobExecContext) User() username.SQLUsername { return e.p.User() }
func (e *plannerJobExecContext) MigrationJobDeps() upgrade.JobDeps {
return e.p.MigrationJobDeps()
}
func (e *plannerJobExecContext) SpanConfigReconciler() spanconfig.Reconciler {
return e.p.SpanConfigReconciler()
}
func (e *plannerJobExecContext) Txn() *kv.Txn { return e.p.Txn() }
func (e *plannerJobExecContext) WithInternalExecutor(
ctx context.Context,
run func(ctx context.Context, txn *kv.Txn, ie sqlutil.InternalExecutor) error,
) error {
return e.p.WithInternalExecutor(ctx, run)
}
// ConstrainPrimaryIndexSpanByExpr implements SpanConstrainer
func (e *plannerJobExecContext) ConstrainPrimaryIndexSpanByExpr(
ctx context.Context,
req SpanConstraintRequirement,
tn *tree.TableName,
desc catalog.TableDescriptor,
evalCtx *eval.Context,
semaCtx *tree.SemaContext,
filter tree.Expr,
) ([]roachpb.Span, tree.Expr, error) {
return e.p.ConstrainPrimaryIndexSpanByExpr(ctx, req, tn, desc, evalCtx, semaCtx, filter)
}
// JobExecContext provides the execution environment for a job. It is what is
// passed to the Resume/OnFailOrCancel/OnPauseRequested methods of a jobs's
// Resumer to give that resumer access to things like ExecutorCfg, LeaseMgr,
// etc -- the kinds of things that would usually be on planner or similar during
// a non-job SQL statement's execution. Unlike a planner however, or planner-ish
// interfaces like PlanHookState, JobExecContext does not include a txn or the
// methods that defined in terms of "the" txn, such as privilege/name accessors.
// (though note that ExtendedEvalContext may transitively include methods that
// close over/expect a txn so use it with caution).
type JobExecContext interface {
SpanConstrainer
SemaCtx() *tree.SemaContext
ExtendedEvalContext() *extendedEvalContext
SessionData() *sessiondata.SessionData
SessionDataMutatorIterator() *sessionDataMutatorIterator
ExecCfg() *ExecutorConfig
DistSQLPlanner() *DistSQLPlanner
LeaseMgr() *lease.Manager
User() username.SQLUsername
MigrationJobDeps() upgrade.JobDeps
SpanConfigReconciler() spanconfig.Reconciler
Txn() *kv.Txn
WithInternalExecutor(
ctx context.Context,
run func(ctx context.Context, txn *kv.Txn, ie sqlutil.InternalExecutor) error,
) error
}