This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
admin.go
177 lines (147 loc) · 5.3 KB
/
admin.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
package launchplan
import (
"context"
"fmt"
"time"
"github.com/lyft/flytestdlib/cache"
"golang.org/x/time/rate"
"k8s.io/client-go/util/workqueue"
"github.com/lyft/flytestdlib/errors"
"github.com/lyft/flytestdlib/logger"
"github.com/lyft/flytestdlib/promutils"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/core"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/service"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Executor for Launchplans that executes on a remote FlyteAdmin service (if configured)
type adminLaunchPlanExecutor struct {
adminClient service.AdminServiceClient
cache cache.AutoRefresh
}
type executionCacheItem struct {
core.WorkflowExecutionIdentifier
ExecutionClosure *admin.ExecutionClosure
SyncError error
}
func (e executionCacheItem) ID() string {
return e.String()
}
func (a *adminLaunchPlanExecutor) Launch(ctx context.Context, launchCtx LaunchContext,
executionID *core.WorkflowExecutionIdentifier, launchPlanRef *core.Identifier, inputs *core.LiteralMap) error {
req := &admin.ExecutionCreateRequest{
Project: executionID.Project,
Domain: executionID.Domain,
Name: executionID.Name,
Spec: &admin.ExecutionSpec{
LaunchPlan: launchPlanRef,
Metadata: &admin.ExecutionMetadata{
Mode: admin.ExecutionMetadata_SYSTEM,
Nesting: launchCtx.NestingLevel + 1,
Principal: launchCtx.Principal,
ParentNodeExecution: launchCtx.ParentNodeExecution,
},
Inputs: inputs,
},
}
_, err := a.adminClient.CreateExecution(ctx, req)
if err != nil {
statusCode := status.Code(err)
switch statusCode {
case codes.AlreadyExists:
_, err := a.cache.GetOrCreate(executionID.String(), executionCacheItem{WorkflowExecutionIdentifier: *executionID})
if err != nil {
logger.Errorf(ctx, "Failed to add ExecID [%v] to auto refresh cache", executionID)
}
return errors.Wrapf(RemoteErrorAlreadyExists, err, "ExecID %s already exists", executionID.Name)
case codes.DataLoss, codes.DeadlineExceeded, codes.Internal, codes.Unknown, codes.Canceled:
return errors.Wrapf(RemoteErrorSystem, err, "failed to launch workflow [%s], system error", launchPlanRef.Name)
default:
return errors.Wrapf(RemoteErrorUser, err, "failed to launch workflow")
}
}
_, err = a.cache.GetOrCreate(executionID.String(), executionCacheItem{WorkflowExecutionIdentifier: *executionID})
if err != nil {
logger.Info(ctx, "Failed to add ExecID [%v] to auto refresh cache", executionID)
}
return nil
}
func (a *adminLaunchPlanExecutor) GetStatus(ctx context.Context, executionID *core.WorkflowExecutionIdentifier) (*admin.ExecutionClosure, error) {
if executionID == nil {
return nil, fmt.Errorf("nil executionID")
}
obj, err := a.cache.GetOrCreate(executionID.String(), executionCacheItem{WorkflowExecutionIdentifier: *executionID})
if err != nil {
return nil, err
}
item := obj.(executionCacheItem)
return item.ExecutionClosure, item.SyncError
}
func (a *adminLaunchPlanExecutor) Kill(ctx context.Context, executionID *core.WorkflowExecutionIdentifier, reason string) error {
req := &admin.ExecutionTerminateRequest{
Id: executionID,
Cause: reason,
}
_, err := a.adminClient.TerminateExecution(ctx, req)
if err != nil {
if status.Code(err) == codes.NotFound {
return nil
}
return errors.Wrapf(RemoteErrorSystem, err, "system error")
}
return nil
}
func (a *adminLaunchPlanExecutor) Initialize(ctx context.Context) error {
return a.cache.Start(ctx)
}
func (a *adminLaunchPlanExecutor) syncItem(ctx context.Context, batch cache.Batch) (
resp []cache.ItemSyncResponse, err error) {
resp = make([]cache.ItemSyncResponse, 0, len(batch))
for _, obj := range batch {
exec := obj.GetItem().(executionCacheItem)
req := &admin.WorkflowExecutionGetRequest{
Id: &exec.WorkflowExecutionIdentifier,
}
res, err := a.adminClient.GetExecution(ctx, req)
if err != nil {
// TODO: Define which error codes are system errors (and return the error) vs user errors.
if status.Code(err) == codes.NotFound {
err = errors.Wrapf(RemoteErrorNotFound, err, "execID [%s] not found on remote", exec.WorkflowExecutionIdentifier.Name)
} else {
err = errors.Wrapf(RemoteErrorSystem, err, "system error")
}
resp = append(resp, cache.ItemSyncResponse{
ID: obj.GetID(),
Item: executionCacheItem{
WorkflowExecutionIdentifier: exec.WorkflowExecutionIdentifier,
SyncError: err,
},
Action: cache.Update,
})
continue
}
resp = append(resp, cache.ItemSyncResponse{
ID: obj.GetID(),
Item: executionCacheItem{
WorkflowExecutionIdentifier: exec.WorkflowExecutionIdentifier,
ExecutionClosure: res.Closure,
},
Action: cache.Update,
})
}
return resp, nil
}
func NewAdminLaunchPlanExecutor(_ context.Context, client service.AdminServiceClient,
syncPeriod time.Duration, cfg *AdminConfig, scope promutils.Scope) (Executor, error) {
exec := &adminLaunchPlanExecutor{
adminClient: client,
}
rateLimiter := &workqueue.BucketRateLimiter{Limiter: rate.NewLimiter(rate.Limit(cfg.TPS), cfg.Burst)}
c, err := cache.NewAutoRefreshCache("admin-launcher", exec.syncItem, rateLimiter, syncPeriod, cfg.Workers, cfg.MaxCacheSize, scope)
if err != nil {
return nil, err
}
exec.cache = c
return exec, nil
}