/
mutation_executor.go
283 lines (255 loc) · 8.78 KB
/
mutation_executor.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Copyright 2018- The Pixie Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package controllers
import (
"context"
"fmt"
"github.com/gofrs/uuid"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"px.dev/pixie/src/api/proto/uuidpb"
"px.dev/pixie/src/api/proto/vizierpb"
"px.dev/pixie/src/carnot/planner/distributedpb"
"px.dev/pixie/src/carnot/planner/plannerpb"
"px.dev/pixie/src/carnot/planpb"
"px.dev/pixie/src/common/base/statuspb"
"px.dev/pixie/src/shared/services/authcontext"
"px.dev/pixie/src/utils"
"px.dev/pixie/src/vizier/services/metadata/metadatapb"
)
// TracepointMap stores a map from the name to tracepoint info.
type TracepointMap map[string]*TracepointInfo
// MutationExecutor is the interface for running script mutations.
type MutationExecutor interface {
Execute(ctx context.Context, request *vizierpb.ExecuteScriptRequest, options *planpb.PlanOptions) (*statuspb.Status, error)
MutationInfo(ctx context.Context) (*vizierpb.MutationInfo, error)
}
// MutationExecutorImpl is responsible for running script mutations.
type MutationExecutorImpl struct {
planner Planner
mdtp metadatapb.MetadataTracepointServiceClient
mdconf metadatapb.MetadataConfigServiceClient
activeTracepoints TracepointMap
outputTables []string
distributedState *distributedpb.DistributedState
}
// TracepointInfo stores information of a particular tracepoint.
type TracepointInfo struct {
Name string
ID uuid.UUID
Status *statuspb.Status
}
// NewMutationExecutor creates a new mutation executor.
func NewMutationExecutor(
planner Planner,
mdtp metadatapb.MetadataTracepointServiceClient,
mdconf metadatapb.MetadataConfigServiceClient,
distributedState *distributedpb.DistributedState) MutationExecutor {
return &MutationExecutorImpl{
planner: planner,
mdtp: mdtp,
mdconf: mdconf,
distributedState: distributedState,
activeTracepoints: make(TracepointMap),
}
}
// Execute runs the mutation. On unknown errors it will return an error, otherwise we return a status message
// that has more context about the error message.
func (m *MutationExecutorImpl) Execute(ctx context.Context, req *vizierpb.ExecuteScriptRequest, planOpts *planpb.PlanOptions) (*statuspb.Status, error) {
convertedReq, err := VizierQueryRequestToPlannerMutationRequest(req)
if err != nil {
return nil, err
}
convertedReq.LogicalPlannerState = &distributedpb.LogicalPlannerState{
DistributedState: m.distributedState,
PlanOptions: planOpts,
}
mutations, err := m.planner.CompileMutations(convertedReq)
if err != nil {
log.WithError(err).Error("Got an error while compiling mutations")
return nil, err
}
if mutations.Status != nil && mutations.Status.ErrCode != statuspb.OK {
return mutations.Status, nil
}
aCtx, err := authcontext.FromContext(ctx)
if err != nil {
return nil, err
}
ctx = metadata.AppendToOutgoingContext(ctx, "authorization", fmt.Sprintf("bearer %s", aCtx.AuthToken))
if len(mutations.Mutations) == 0 {
// No mutations to apply.
return nil, nil
}
registerTracepointsReq := &metadatapb.RegisterTracepointRequest{
Requests: make([]*metadatapb.RegisterTracepointRequest_TracepointRequest, 0),
}
deleteTracepointsReq := &metadatapb.RemoveTracepointRequest{
Names: make([]string, 0),
}
configmapReqs := make([]*metadatapb.UpdateConfigRequest, 0)
outputTablesMap := make(map[string]bool)
// TODO(zasgar): We should make sure that we don't simultaneously add and delete the tracepoint.
// While this will probably work, we should restrict this because it's likely not the intended behavior.
for _, mut := range mutations.Mutations {
switch mut := mut.Mutation.(type) {
case *plannerpb.CompileMutation_Trace:
{
name := mut.Trace.Name
registerTracepointsReq.Requests = append(registerTracepointsReq.Requests,
&metadatapb.RegisterTracepointRequest_TracepointRequest{
TracepointDeployment: mut.Trace,
Name: mut.Trace.Name,
TTL: mut.Trace.TTL,
})
if _, ok := m.activeTracepoints[name]; ok {
return nil, fmt.Errorf("tracepoint with name '%s', already used", name)
}
for _, tracepoint := range mut.Trace.Programs {
outputTablesMap[tracepoint.TableName] = true
}
m.activeTracepoints[name] = &TracepointInfo{
Name: name,
ID: uuid.Nil,
Status: nil,
}
}
case *plannerpb.CompileMutation_DeleteTracepoint:
{
deleteTracepointsReq.Names = append(deleteTracepointsReq.Names, mut.DeleteTracepoint.Name)
}
case *plannerpb.CompileMutation_ConfigUpdate:
{
configmapReqs = append(configmapReqs, &metadatapb.UpdateConfigRequest{
Key: mut.ConfigUpdate.Key,
Value: mut.ConfigUpdate.Value,
AgentPodName: mut.ConfigUpdate.AgentPodName,
})
}
}
}
if len(registerTracepointsReq.Requests) > 0 {
resp, err := m.mdtp.RegisterTracepoint(ctx, registerTracepointsReq)
if err != nil {
log.WithError(err).
Errorf("Failed to register tracepoints")
return nil, ErrTracepointRegistrationFailed
}
if resp.Status != nil && resp.Status.ErrCode != statuspb.OK {
log.WithField("status", resp.Status.String()).
Errorf("Failed to register tracepoints with bad status")
return resp.Status, ErrTracepointRegistrationFailed
}
// Update the internal stat of the tracepoints.
for _, tp := range resp.Tracepoints {
id := utils.UUIDFromProtoOrNil(tp.ID)
m.activeTracepoints[tp.Name].ID = id
m.activeTracepoints[tp.Name].Status = tp.Status
}
}
if len(deleteTracepointsReq.Names) > 0 {
delResp, err := m.mdtp.RemoveTracepoint(ctx, deleteTracepointsReq)
if err != nil {
log.WithError(err).
Errorf("Failed to delete tracepoints")
return nil, ErrTracepointDeletionFailed
}
if delResp.Status != nil && delResp.Status.ErrCode != statuspb.OK {
log.WithField("status", delResp.Status.String()).
Errorf("Failed to delete tracepoints with bad status")
return delResp.Status, ErrTracepointDeletionFailed
}
// Remove the tracepoints we considered deleted.
for _, tpName := range deleteTracepointsReq.Names {
delete(m.activeTracepoints, tpName)
}
}
if len(configmapReqs) > 0 {
for _, configmapReq := range configmapReqs {
resp, err := m.mdconf.UpdateConfig(ctx, configmapReq)
if err != nil || (resp.Status != nil && resp.Status.ErrCode != statuspb.OK) {
return nil, ErrConfigUpdateFailed
}
}
}
m.outputTables = make([]string, 0)
for k := range outputTablesMap {
m.outputTables = append(m.outputTables, k)
}
return nil, nil
}
// MutationInfo returns the summarized mutation information.
func (m *MutationExecutorImpl) MutationInfo(ctx context.Context) (*vizierpb.MutationInfo, error) {
req := &metadatapb.GetTracepointInfoRequest{
IDs: make([]*uuidpb.UUID, 0),
}
for _, tp := range m.activeTracepoints {
req.IDs = append(req.IDs, utils.ProtoFromUUID(tp.ID))
}
aCtx, err := authcontext.FromContext(ctx)
if err != nil {
return nil, err
}
ctx = metadata.AppendToOutgoingContext(ctx, "authorization", fmt.Sprintf("bearer %s", aCtx.AuthToken))
resp, err := m.mdtp.GetTracepointInfo(ctx, req)
if err != nil {
return nil, err
}
mutationInfo := &vizierpb.MutationInfo{
Status: &vizierpb.Status{Code: 0},
States: make([]*vizierpb.MutationInfo_MutationState, len(resp.Tracepoints)),
}
ready := true
for idx, tp := range resp.Tracepoints {
mutationInfo.States[idx] = &vizierpb.MutationInfo_MutationState{
ID: utils.UUIDFromProtoOrNil(tp.ID).String(),
State: convertLifeCycleStateToVizierLifeCycleState(tp.State),
Name: tp.Name,
}
if tp.State != statuspb.RUNNING_STATE {
ready = false
}
}
if !ready {
mutationInfo.Status = &vizierpb.Status{
Code: int32(codes.Unavailable),
Message: "probe installation in progress",
}
return mutationInfo, nil
}
if !m.isSchemaReady() {
mutationInfo.Status = &vizierpb.Status{
Code: int32(codes.Unavailable),
Message: "Schema is not ready yet",
}
}
return mutationInfo, nil
}
func (m *MutationExecutorImpl) isSchemaReady() bool {
schemaNames := make(map[string]bool)
for _, s := range m.distributedState.SchemaInfo {
schemaNames[s.Name] = true
}
for _, s := range m.outputTables {
if _, ok := schemaNames[s]; !ok {
return false
}
}
return true
}