-
Notifications
You must be signed in to change notification settings - Fork 42
/
temporary.go
584 lines (494 loc) · 16 KB
/
temporary.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
package flow
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
"strconv"
"time"
cloudevents "github.com/cloudevents/sdk-go/v2"
derrors "github.com/direktiv/direktiv/pkg/flow/errors"
"github.com/direktiv/direktiv/pkg/flow/grpc"
log "github.com/direktiv/direktiv/pkg/flow/internallogger"
"github.com/direktiv/direktiv/pkg/flow/states"
"github.com/direktiv/direktiv/pkg/model"
"github.com/direktiv/direktiv/pkg/refactor/core"
"github.com/direktiv/direktiv/pkg/refactor/datastore"
enginerefactor "github.com/direktiv/direktiv/pkg/refactor/engine"
"github.com/direktiv/direktiv/pkg/refactor/filestore"
"github.com/direktiv/direktiv/pkg/refactor/service"
"github.com/direktiv/direktiv/pkg/util"
"github.com/google/uuid"
)
// TEMPORARY EVERYTHING
func (im *instanceMemory) BroadcastCloudevent(ctx context.Context, event *cloudevents.Event, dd int64) error {
return im.engine.events.BroadcastCloudevent(ctx, im.Namespace(), event, dd)
}
func (im *instanceMemory) GetVariables(ctx context.Context, vars []states.VariableSelector) ([]states.Variable, error) {
x := make([]states.Variable, 0)
tx, err := im.engine.flow.beginSqlTx(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback()
for _, selector := range vars {
if selector.Scope == "" || selector.Scope == util.VarScopeInstance || selector.Scope == util.VarScopeWorkflow || selector.Scope == util.VarScopeNamespace {
if selector.Scope == "" {
selector.Scope = util.VarScopeNamespace
}
var item *datastore.RuntimeVariable
switch selector.Scope {
case util.VarScopeInstance:
item, err = tx.DataStore().RuntimeVariables().GetForInstance(ctx, im.instance.Instance.ID, selector.Key)
case util.VarScopeWorkflow:
item, err = tx.DataStore().RuntimeVariables().GetForWorkflow(ctx, im.instance.Instance.Namespace, im.instance.Instance.WorkflowPath, selector.Key)
case util.VarScopeNamespace:
item, err = tx.DataStore().RuntimeVariables().GetForNamespace(ctx, im.instance.Instance.Namespace, selector.Key)
default:
return nil, derrors.NewInternalError(errors.New("invalid scope"))
}
if errors.Is(err, datastore.ErrNotFound) {
x = append(x, states.Variable{
Scope: selector.Scope,
Key: selector.Key,
Data: []byte{},
})
} else if err != nil {
return nil, derrors.NewInternalError(err)
} else {
data, err := tx.DataStore().RuntimeVariables().LoadData(ctx, item.ID)
if err != nil {
return nil, derrors.NewInternalError(err)
}
x = append(x, states.Variable{
Scope: selector.Scope,
Key: selector.Key,
Data: data,
})
}
continue
}
if selector.Scope == util.VarScopeFileSystem {
file, err := tx.FileStore().ForNamespace(im.instance.Instance.Namespace).GetFile(ctx, selector.Key)
if errors.Is(err, filestore.ErrNotFound) {
x = append(x, states.Variable{
Scope: selector.Scope,
Key: selector.Key,
Data: make([]byte, 0),
})
} else if err != nil {
return nil, err
} else {
// TODO: alan, maybe need to enhance the GetData function to also return us some information like mime type, checksum, and size
if file.Typ == filestore.FileTypeDirectory {
return nil, model.ErrVarNotFile
}
data, err := tx.FileStore().ForFile(file).GetData(ctx)
if err != nil {
return nil, err
}
x = append(x, states.Variable{
Scope: selector.Scope,
Key: selector.Key,
Data: data,
})
}
continue
}
}
return x, nil
}
func (im *instanceMemory) ListenForEvents(ctx context.Context, events []*model.ConsumeEventDefinition, all bool) error {
err := im.engine.events.deleteInstanceEventListeners(ctx, im)
if err != nil {
return err
}
err = im.engine.events.listenForEvents(ctx, im, events, all)
if err != nil {
return err
}
return nil
}
func (im *instanceMemory) Log(ctx context.Context, level log.Level, a string, x ...interface{}) {
ctx = im.WithTags(ctx)
slog.With(enginerefactor.GetSlogAttributesWithStatus(ctx, core.LogRunningStatus)...)
switch level {
case log.Info:
slog.Info(fmt.Sprintf(a, x...))
case log.Debug:
slog.Debug(fmt.Sprintf(a, x...))
case log.Error:
slog.Error(fmt.Sprintf(a, x...))
case log.Panic:
slog.Error(fmt.Sprintf("Panic: "+a, x...))
}
}
func (im *instanceMemory) AddAttribute(tag, value string) {
if im.tags == nil {
im.tags = make(map[string]string)
}
im.tags[tag] = value
}
func (im *instanceMemory) Iterator() (int, bool) {
if im.tags == nil {
return 0, false
}
val, ok := im.tags["loop-index"]
iterator, err := strconv.Atoi(val)
if err != nil {
return 0, false
}
return iterator, ok
}
func (im *instanceMemory) Raise(ctx context.Context, err *derrors.CatchableError) error {
return im.engine.InstanceRaise(ctx, im, err)
}
func (im *instanceMemory) RetrieveSecret(ctx context.Context, secret string) (string, error) {
tx, err := im.engine.flow.beginSqlTx(ctx)
if err != nil {
return "", err
}
defer tx.Rollback()
secretData, err := tx.DataStore().Secrets().Get(ctx, im.instance.Instance.Namespace, secret)
if err != nil {
return "", err
}
return string(secretData.Data), nil
}
func (im *instanceMemory) SetVariables(ctx context.Context, vars []states.VariableSetter) error {
tx, err := im.engine.flow.beginSqlTx(ctx)
if err != nil {
return err
}
defer tx.Rollback()
for idx := range vars {
v := vars[idx]
var item *datastore.RuntimeVariable
switch v.Scope {
case util.VarScopeInstance:
item, err = tx.DataStore().RuntimeVariables().GetForInstance(ctx, im.instance.Instance.ID, v.Key)
case util.VarScopeWorkflow:
item, err = tx.DataStore().RuntimeVariables().GetForWorkflow(ctx, im.instance.Instance.Namespace, im.instance.Instance.WorkflowPath, v.Key)
case util.VarScopeNamespace:
item, err = tx.DataStore().RuntimeVariables().GetForNamespace(ctx, im.instance.Instance.Namespace, v.Key)
default:
return derrors.NewInternalError(errors.New("invalid scope"))
}
if err != nil && !errors.Is(err, datastore.ErrNotFound) {
return err
}
d := string(v.Data)
if len(d) == 0 {
err = tx.DataStore().RuntimeVariables().Delete(ctx, item.ID)
if err != nil && !errors.Is(err, datastore.ErrNotFound) {
return err
}
continue
}
if !(v.MIMEType == "text/plain; charset=utf-8" || v.MIMEType == "text/plain" || v.MIMEType == "application/octet-stream") && (d == "{}" || d == "[]" || d == "0" || d == `""` || d == "null") {
if item != nil {
err = tx.DataStore().RuntimeVariables().Delete(ctx, item.ID)
if err != nil && !errors.Is(err, datastore.ErrNotFound) {
return err
}
}
} else {
newVar := &datastore.RuntimeVariable{
Name: v.Key,
MimeType: v.MIMEType,
Data: v.Data,
Namespace: im.instance.Instance.Namespace,
}
switch v.Scope {
case util.VarScopeInstance:
newVar.InstanceID = im.instance.Instance.ID
case util.VarScopeWorkflow:
newVar.WorkflowPath = im.instance.Instance.WorkflowPath
}
_, err = tx.DataStore().RuntimeVariables().Set(ctx, newVar)
if err != nil {
return err
}
}
}
err = tx.Commit(ctx)
if err != nil {
return err
}
return nil
}
func (im *instanceMemory) Sleep(ctx context.Context, d time.Duration, x interface{}) error {
return im.ScheduleRetry(ctx, d, im.logic.GetID(), x)
}
func (im *instanceMemory) GetInstanceData() interface{} {
return im.data
}
func (im *instanceMemory) GetModel() (*model.Workflow, error) {
return im.Model()
}
func (im *instanceMemory) GetInstanceID() uuid.UUID {
return im.instance.Instance.ID
}
func (im *instanceMemory) PrimeDelayedEvent(event cloudevents.Event) {
im.eventQueue = append(im.eventQueue, event.ID())
}
func (im *instanceMemory) SetMemory(ctx context.Context, x interface{}) error {
return im.engine.SetMemory(ctx, im, x)
}
func (im *instanceMemory) Deadline(ctx context.Context) time.Time {
return time.Now().UTC().Add(states.DefaultShortDeadline)
}
func (im *instanceMemory) LivingChildren(ctx context.Context) []*states.ChildInfo {
return nil
}
func (im *instanceMemory) ScheduleRetry(ctx context.Context, d time.Duration, stateID string, x interface{}) error {
data, err := json.Marshal(x)
if err != nil {
return err
}
t := time.Now().UTC().Add(d)
err = im.engine.scheduleRetry(im.ID().String(), t, data)
if err != nil {
return err
}
return nil
}
func (im *instanceMemory) CreateChild(ctx context.Context, args states.CreateChildArgs) (states.Child, error) {
var ci states.ChildInfo
if args.Definition.GetType() == model.SubflowFunctionType {
pi := &enginerefactor.ParentInfo{
ID: im.ID(),
State: im.logic.GetID(),
Step: im.Step(),
Branch: args.Iterator,
}
sfim, err := im.engine.subflowInvoke(ctx, pi, im.instance, args.Definition.(*model.SubflowFunctionDefinition).Workflow, args.Input)
if err != nil {
return nil, err
}
ci.ID = sfim.ID().String()
ci.Type = "subflow"
// ci.Attempts: this is ignored here. Must be handled elsewhere.
return &subflowHandle{
im: sfim,
info: ci,
engine: im.engine,
}, nil
}
switch args.Definition.GetType() {
case model.NamespacedKnativeFunctionType:
case model.ReusableContainerFunctionType:
default:
return nil, derrors.NewInternalError(fmt.Errorf("unsupported function type: %v", args.Definition.GetType()))
}
uid := uuid.New()
ar, err := im.engine.newIsolateRequest(im, im.logic.GetID(), args.Timeout, args.Definition, args.Input, uid, args.Async, args.Files, args.Iterator)
if err != nil {
return nil, err
}
ci.ID = ar.ActionID
ci.ServiceName = ar.Container.Service
ci.Type = "isolate"
return &knativeHandle{
im: im,
info: ci,
engine: im.engine,
ar: ar,
}, nil
}
type subflowHandle struct {
im *instanceMemory
info states.ChildInfo
engine *engine
}
func (child *subflowHandle) Run(ctx context.Context) {
go child.engine.start(child.im)
}
func (child *subflowHandle) Info() states.ChildInfo {
return child.info
}
func (engine *engine) newIsolateRequest(im *instanceMemory, stateId string, timeout int,
fn model.FunctionDefinition, inputData []byte,
uid uuid.UUID, async bool, files []model.FunctionFileDefinition, iterator int,
) (*functionRequest, error) {
ar := new(functionRequest)
ar.ActionID = uid.String()
ar.Workflow.Timeout = timeout
ar.Workflow.NamespaceName = im.instance.TelemetryInfo.NamespaceName
ar.Workflow.Path = im.instance.Instance.WorkflowPath
ar.Iterator = iterator
if !async {
ar.Workflow.InstanceID = im.ID().String()
ar.Workflow.NamespaceID = im.instance.Instance.NamespaceID.String()
ar.Workflow.State = stateId
ar.Workflow.Step = im.Step()
}
fnt := fn.GetType()
ar.Container.Type = fnt
ar.Container.Data = inputData
switch fnt {
case model.ReusableContainerFunctionType:
con := fn.(*model.ReusableFunctionDefinition)
scale := int32(0)
ar.Container.Image = con.Image
ar.Container.Cmd = con.Cmd
ar.Container.Size = con.Size
ar.Container.Scale = int(scale)
ar.Container.Files = files
ar.Container.ID = con.ID
ar.Container.Service = service.GetServiceURL(ar.Workflow.NamespaceName, core.ServiceTypeWorkflow, ar.Workflow.Path, con.ID)
case model.NamespacedKnativeFunctionType:
con := fn.(*model.NamespacedFunctionDefinition)
ar.Container.Files = files
ar.Container.ID = con.ID
ar.Container.Service = service.GetServiceURL(ar.Workflow.NamespaceName, core.ServiceTypeNamespace, con.Path, "")
default:
return nil, fmt.Errorf("unexpected function type: %v", fn)
}
// check for duplicate file names
m := make(map[string]*model.FunctionFileDefinition)
for i := range ar.Container.Files {
f := &ar.Container.Files[i]
k := f.As
if k == "" {
k = f.Key
}
if _, exists := m[k]; exists {
return nil, fmt.Errorf("multiple files with same name: %s", k)
}
m[k] = f
}
return ar, nil
}
type knativeHandle struct {
im *instanceMemory
info states.ChildInfo
engine *engine
ar *functionRequest
}
func (child *knativeHandle) Run(ctx context.Context) {
go func(ctx context.Context, im *instanceMemory, ar *functionRequest) {
err := child.engine.doActionRequest(ctx, ar)
if err != nil {
return
}
}(ctx, child.im, child.ar)
}
func (child *knativeHandle) Info() states.ChildInfo {
return child.info
}
func (engine *engine) doActionRequest(ctx context.Context, ar *functionRequest) error {
if ar.Workflow.Timeout == 0 {
ar.Workflow.Timeout = 5 * 60 // 5 mins default, knative's default
}
// Log warning if timeout exceeds max allowed timeout
if actionTimeout := time.Duration(ar.Workflow.Timeout) * time.Second; actionTimeout > engine.server.config.GetFunctionsTimeout() {
_, err := engine.internal.ActionLog(context.Background(), &grpc.ActionLogRequest{
InstanceId: ar.Workflow.InstanceID, Msg: []string{fmt.Sprintf("Warning: Action timeout '%v' is longer than max allowed duariton '%v'", actionTimeout, engine.server.config.GetFunctionsTimeout())},
})
if err != nil {
slog.Error("failed to write action log", "error", err)
}
}
switch ar.Container.Type {
case model.DefaultFunctionType:
fallthrough
case model.NamespacedKnativeFunctionType:
fallthrough
case model.ReusableContainerFunctionType:
go engine.doKnativeHTTPRequest(ctx, ar)
}
return nil
}
func (engine *engine) doKnativeHTTPRequest(ctx context.Context,
ar *functionRequest,
) {
var err error
tr := engine.createTransport()
addr := ar.Container.Service
slog.Debug("function request for image", "name", ar.Container.Image, "addr", addr, "image_id", ar.Container.ID)
deadline := time.Now().UTC().Add(time.Duration(ar.Workflow.Timeout) * time.Second)
rctx, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
slog.Debug("deadline for request", "deadline", time.Until(deadline))
req, err := http.NewRequestWithContext(rctx, http.MethodPost, addr,
bytes.NewReader(ar.Container.Data))
if err != nil {
engine.reportError(ar, err)
return
}
// add headers
req.Header.Add(DirektivDeadlineHeader, deadline.Format(time.RFC3339))
req.Header.Add(DirektivNamespaceHeader, ar.Workflow.NamespaceName)
req.Header.Add(DirektivActionIDHeader, ar.ActionID)
req.Header.Add(DirektivInstanceIDHeader, ar.Workflow.InstanceID)
req.Header.Add(DirektivStepHeader, fmt.Sprintf("%d",
int64(ar.Workflow.Step)))
req.Header.Add(DirektivIteratorHeader, fmt.Sprintf("%d",
int64(ar.Iterator)))
for i := range ar.Container.Files {
f := &ar.Container.Files[i]
data, err := json.Marshal(f)
if err != nil {
panic(err)
}
str := base64.StdEncoding.EncodeToString(data)
req.Header.Add(DirektivFileHeader, str)
}
client := &http.Client{
Transport: tr,
}
var resp *http.Response
// potentially dns error for a brand new service
// we just loop and see if we can recreate the service
// one minute wait max
cleanup := util.TraceHTTPRequest(ctx, req)
defer cleanup()
for i := 0; i < 300; i++ { // 5 minutes retries.
slog.Debug("functions request", "i", i, "addr", addr)
resp, err = client.Do(req)
if err != nil {
if ctxErr := rctx.Err(); ctxErr != nil {
slog.Debug("context error in knative call", "error", rctx.Err())
return
}
slog.Debug("function request", "image", ar.Container.Image, "image_id", ar.Container.ID, "error", err)
time.Sleep(time.Second)
} else {
defer resp.Body.Close()
slog.Debug("successfully created function", "image", ar.Container.Image, "image_id", ar.Container.ID)
break
}
}
if err != nil {
err := fmt.Errorf("failed creating function with image %s name %s with error: %w", ar.Container.Image, ar.Container.ID, err)
engine.reportError(ar, err)
return
}
if resp.StatusCode != http.StatusOK {
engine.reportError(ar, fmt.Errorf("action error status: %d",
resp.StatusCode))
}
slog.Debug("function request done")
}
func (engine *engine) reportError(ar *functionRequest, err error) {
ec := ""
em := err.Error()
step := int32(ar.Workflow.Step)
r := &grpc.ReportActionResultsRequest{
InstanceId: ar.Workflow.InstanceID,
Step: step,
ActionId: ar.ActionID,
ErrorCode: ec,
ErrorMessage: em,
Iterator: int32(ar.Iterator),
}
_, err = engine.internal.ReportActionResults(context.Background(), r)
if err != nil {
slog.Error("failed to respond to flow", "error", err)
}
}