-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathtenant_status.go
142 lines (126 loc) · 4.8 KB
/
tenant_status.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// 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.
// TODO(azhng): The implementation for tenantStatusServer here will need to be updated
// once we have pod-to-pod communication implemented. After all dependencies that are
// unavailable to tenants have been removed, we can likely remove tenant status server
// entirely and use the normal status server.
package server
import (
"context"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/contention"
"github.com/cockroachdb/cockroach/pkg/sql/flowinfra"
"github.com/cockroachdb/cockroach/pkg/util/log"
)
// tenantStatusServer is an implementation of a SQLStatusServer that is
// available to tenants. The full statusServer implementation is unavailable to
// tenants due to its use of gossip and other unavailable subsystems.
// The tenantStatusServer implementation is local only. This is enough for
// Phase 2 requirements that there can only be at most one live SQL pod per
// tenant.
type tenantStatusServer struct {
baseStatusServer
}
func newTenantStatusServer(
ambient log.AmbientContext,
privilegeChecker *adminPrivilegeChecker,
sessionRegistry *sql.SessionRegistry,
contentionRegistry *contention.Registry,
flowScheduler *flowinfra.FlowScheduler,
st *cluster.Settings,
sqlServer *SQLServer,
) *tenantStatusServer {
ambient.AddLogTag("tenant-status", nil)
return &tenantStatusServer{
baseStatusServer: baseStatusServer{
AmbientContext: ambient,
privilegeChecker: privilegeChecker,
sessionRegistry: sessionRegistry,
contentionRegistry: contentionRegistry,
flowScheduler: flowScheduler,
st: st,
sqlServer: sqlServer,
},
}
}
func (t *tenantStatusServer) ListSessions(
ctx context.Context, request *serverpb.ListSessionsRequest,
) (*serverpb.ListSessionsResponse, error) {
return t.ListLocalSessions(ctx, request)
}
func (t *tenantStatusServer) ListLocalSessions(
ctx context.Context, request *serverpb.ListSessionsRequest,
) (*serverpb.ListSessionsResponse, error) {
sessions, err := t.getLocalSessions(ctx, request)
if err != nil {
return nil, err
}
return &serverpb.ListSessionsResponse{Sessions: sessions}, nil
}
func (t *tenantStatusServer) CancelQuery(
ctx context.Context, request *serverpb.CancelQueryRequest,
) (*serverpb.CancelQueryResponse, error) {
reqUsername := security.MakeSQLUsernameFromPreNormalizedString(request.Username)
if err := t.checkCancelPrivilege(ctx, reqUsername, findSessionByQueryID(request.QueryID)); err != nil {
return nil, err
}
var (
output = &serverpb.CancelQueryResponse{}
err error
)
output.Canceled, err = t.sessionRegistry.CancelQuery(request.QueryID)
if err != nil {
output.Error = err.Error()
}
return output, nil
}
func (t *tenantStatusServer) CancelSession(
ctx context.Context, request *serverpb.CancelSessionRequest,
) (*serverpb.CancelSessionResponse, error) {
reqUsername := security.MakeSQLUsernameFromPreNormalizedString(request.Username)
if err := t.checkCancelPrivilege(ctx, reqUsername, findSessionBySessionID(request.SessionID)); err != nil {
return nil, err
}
return t.sessionRegistry.CancelSession(request.SessionID)
}
func (t *tenantStatusServer) ListContentionEvents(
ctx context.Context, request *serverpb.ListContentionEventsRequest,
) (*serverpb.ListContentionEventsResponse, error) {
return t.ListLocalContentionEvents(ctx, request)
}
func (t *tenantStatusServer) ResetSQLStats(
ctx context.Context, _ *serverpb.ResetSQLStatsRequest,
) (*serverpb.ResetSQLStatsResponse, error) {
t.sqlServer.pgServer.SQLServer.ResetSQLStats(ctx)
return &serverpb.ResetSQLStatsResponse{}, nil
}
func (t *tenantStatusServer) Statements(
ctx context.Context, _ *serverpb.StatementsRequest,
) (*serverpb.StatementsResponse, error) {
if _, err := t.privilegeChecker.requireViewActivityPermission(ctx); err != nil {
return nil, err
}
// Use a dummy value here until pod-to-pod communication is implemented since tenant status server
// does not have concept of node.
resp, err := statementsLocal(ctx, &base.NodeIDContainer{}, t.sqlServer)
if err != nil {
return nil, err
}
return resp, nil
}
func (t *tenantStatusServer) ListDistSQLFlows(
ctx context.Context, request *serverpb.ListDistSQLFlowsRequest,
) (*serverpb.ListDistSQLFlowsResponse, error) {
return t.ListLocalDistSQLFlows(ctx, request)
}