-
Notifications
You must be signed in to change notification settings - Fork 26
/
executor.go
530 lines (458 loc) · 18.6 KB
/
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
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
package backend
import (
context "context"
"errors"
"fmt"
"strconv"
"strings"
"sync"
"time"
"github.com/cenkalti/backoff/v4"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
"github.com/microsoft/durabletask-go/api"
"github.com/microsoft/durabletask-go/internal/helpers"
"github.com/microsoft/durabletask-go/internal/protos"
)
var emptyCompleteTaskResponse = &protos.CompleteTaskResponse{}
var errShuttingDown error = status.Error(codes.Canceled, "shutting down")
type ExecutionResults struct {
Response *protos.OrchestratorResponse
complete chan struct{}
pending chan string
}
type activityExecutionResult struct {
response *protos.ActivityResponse
complete chan struct{}
pending chan string
}
type Executor interface {
ExecuteOrchestrator(ctx context.Context, iid api.InstanceID, oldEvents []*protos.HistoryEvent, newEvents []*protos.HistoryEvent) (*ExecutionResults, error)
ExecuteActivity(context.Context, api.InstanceID, *protos.HistoryEvent) (*protos.HistoryEvent, error)
Shutdown(ctx context.Context) error
}
type grpcExecutor struct {
protos.UnimplementedTaskHubSidecarServiceServer
workItemQueue chan *protos.WorkItem
pendingOrchestrators *sync.Map // map[api.InstanceID]*ExecutionResults
pendingActivities *sync.Map // map[string]*activityExecutionResult
backend Backend
logger Logger
onWorkItemConnection func(context.Context) error
streamShutdownChan <-chan any
}
type grpcExecutorOptions func(g *grpcExecutor)
// IsDurableTaskGrpcRequest returns true if the specified gRPC method name represents an operation
// that is compatible with the gRPC executor.
func IsDurableTaskGrpcRequest(fullMethodName string) bool {
return strings.HasPrefix(fullMethodName, "/TaskHubSidecarService/")
}
// WithOnGetWorkItemsConnectionCallback allows the caller to get a notification when an external process
// connects over gRPC and invokes the GetWorkItems operation. This can be useful for doing things like
// lazily auto-starting the task hub worker only when necessary.
func WithOnGetWorkItemsConnectionCallback(callback func(context.Context) error) grpcExecutorOptions {
return func(g *grpcExecutor) {
g.onWorkItemConnection = callback
}
}
func WithStreamShutdownChannel(c <-chan any) grpcExecutorOptions {
return func(g *grpcExecutor) {
g.streamShutdownChan = c
}
}
// NewGrpcExecutor returns the Executor object and a method to invoke to register the gRPC server in the executor.
func NewGrpcExecutor(be Backend, logger Logger, opts ...grpcExecutorOptions) (executor Executor, registerServerFn func(grpcServer grpc.ServiceRegistrar)) {
grpcExecutor := &grpcExecutor{
workItemQueue: make(chan *protos.WorkItem),
backend: be,
logger: logger,
pendingOrchestrators: &sync.Map{},
pendingActivities: &sync.Map{},
}
for _, opt := range opts {
opt(grpcExecutor)
}
return grpcExecutor, func(grpcServer grpc.ServiceRegistrar) {
protos.RegisterTaskHubSidecarServiceServer(grpcServer, grpcExecutor)
}
}
// ExecuteOrchestrator implements Executor
func (executor *grpcExecutor) ExecuteOrchestrator(ctx context.Context, iid api.InstanceID, oldEvents []*protos.HistoryEvent, newEvents []*protos.HistoryEvent) (*ExecutionResults, error) {
result := &ExecutionResults{complete: make(chan struct{})}
executor.pendingOrchestrators.Store(iid, result)
workItem := &protos.WorkItem{
Request: &protos.WorkItem_OrchestratorRequest{
OrchestratorRequest: &protos.OrchestratorRequest{
InstanceId: string(iid),
ExecutionId: nil,
PastEvents: oldEvents,
NewEvents: newEvents,
},
},
}
// Send the orchestration execution work-item to the connected worker.
// This will block if the worker isn't listening for work items.
select {
case <-ctx.Done():
executor.logger.Warnf("%s: context canceled before dispatching orchestrator work item", iid)
return nil, ctx.Err()
case executor.workItemQueue <- workItem:
}
// Wait for the connected worker to signal that it's done executing the work-item
select {
case <-ctx.Done():
executor.logger.Warnf("%s: context canceled before receiving orchestrator result", iid)
return nil, ctx.Err()
case <-result.complete:
executor.logger.Debugf("%s: orchestrator got result", iid)
if result.Response == nil {
return nil, errors.New("operation aborted")
}
}
return result, nil
}
// ExecuteActivity implements Executor
func (executor *grpcExecutor) ExecuteActivity(ctx context.Context, iid api.InstanceID, e *protos.HistoryEvent) (*protos.HistoryEvent, error) {
key := getActivityExecutionKey(string(iid), e.EventId)
result := &activityExecutionResult{complete: make(chan struct{})}
executor.pendingActivities.Store(key, result)
task := e.GetTaskScheduled()
workItem := &protos.WorkItem{
Request: &protos.WorkItem_ActivityRequest{
ActivityRequest: &protos.ActivityRequest{
Name: task.Name,
Version: task.Version,
Input: task.Input,
OrchestrationInstance: &protos.OrchestrationInstance{InstanceId: string(iid)},
TaskId: e.EventId,
},
},
}
// Send the activity execution work-item to the connected worker.
// This will block if the worker isn't listening for work items.
select {
case <-ctx.Done():
executor.logger.Warnf("%s/%s#%d: context canceled before dispatching activity work item", iid, task.Name, e.EventId)
return nil, ctx.Err()
case executor.workItemQueue <- workItem:
}
// Wait for the connected worker to signal that it's done executing the work-item
select {
case <-ctx.Done():
executor.logger.Warnf("%s/%s#%d: context canceled before receiving activity result", iid, task.Name, e.EventId)
return nil, ctx.Err()
case <-result.complete:
executor.logger.Debugf("%s: activity got result", key)
if result.response == nil {
return nil, errors.New("operation aborted")
}
}
var responseEvent *protos.HistoryEvent
if failureDetails := result.response.GetFailureDetails(); failureDetails != nil {
responseEvent = helpers.NewTaskFailedEvent(result.response.TaskId, result.response.FailureDetails)
} else {
responseEvent = helpers.NewTaskCompletedEvent(result.response.TaskId, result.response.Result)
}
return responseEvent, nil
}
// Shutdown implements Executor
func (g *grpcExecutor) Shutdown(ctx context.Context) error {
// closing the work item queue is a signal for shutdown
close(g.workItemQueue)
// Iterate through all pending items and close them to unblock the goroutines waiting on this
g.pendingActivities.Range(func(_, value any) bool {
p, ok := value.(*activityExecutionResult)
if ok {
close(p.complete)
}
return true
})
g.pendingOrchestrators.Range(func(_, value any) bool {
p, ok := value.(*ExecutionResults)
if ok {
close(p.complete)
}
return true
})
return nil
}
// Hello implements protos.TaskHubSidecarServiceServer
func (grpcExecutor) Hello(ctx context.Context, empty *emptypb.Empty) (*emptypb.Empty, error) {
return empty, nil
}
// GetWorkItems implements protos.TaskHubSidecarServiceServer
func (g *grpcExecutor) GetWorkItems(req *protos.GetWorkItemsRequest, stream protos.TaskHubSidecarService_GetWorkItemsServer) error {
if md, ok := metadata.FromIncomingContext(stream.Context()); ok {
g.logger.Infof("work item stream established by user-agent: %v", md.Get("user-agent"))
}
// There are some cases where the app may need to be notified when a client connects to fetch work items, like
// for auto-starting the worker. The app also has an opportunity to set itself as unavailable by returning an error.
callback := g.onWorkItemConnection
if callback != nil {
if err := callback(stream.Context()); err != nil {
message := "unable to establish work item stream at this time: " + err.Error()
g.logger.Warn(message)
return status.Errorf(codes.Unavailable, message)
}
}
// Collect all pending activities on this stream
// Note: we don't need sync.Map's here because access is only on this thread
pendingActivities := make(map[string]struct{})
pendingActivityCh := make(chan string, 1)
pendingOrchestrators := make(map[string]struct{})
pendingOrchestratorCh := make(chan string, 1)
defer func() {
// If there's any pending activity left, remove them
for key := range pendingActivities {
g.logger.Debugf("cleaning up pending activity: %s", key)
p, ok := g.pendingActivities.LoadAndDelete(key)
if ok {
pending := p.(*activityExecutionResult)
close(pending.complete)
}
}
for key := range pendingOrchestrators {
g.logger.Debugf("cleaning up pending orchestrator: %s", key)
p, ok := g.pendingOrchestrators.LoadAndDelete(api.InstanceID(key))
if ok {
pending := p.(*ExecutionResults)
close(pending.complete)
}
}
}()
// The worker client invokes this method, which streams back work-items as they arrive.
for {
select {
case <-stream.Context().Done():
g.logger.Info("work item stream closed")
return nil
case wi, ok := <-g.workItemQueue:
if !ok {
continue
}
switch x := wi.Request.(type) {
case *protos.WorkItem_OrchestratorRequest:
key := x.OrchestratorRequest.GetInstanceId()
pendingOrchestrators[key] = struct{}{}
p, ok := g.pendingOrchestrators.Load(api.InstanceID(key))
if ok {
p.(*ExecutionResults).pending = pendingOrchestratorCh
}
case *protos.WorkItem_ActivityRequest:
key := getActivityExecutionKey(x.ActivityRequest.GetOrchestrationInstance().GetInstanceId(), x.ActivityRequest.GetTaskId())
pendingActivities[key] = struct{}{}
p, ok := g.pendingActivities.Load(key)
if ok {
p.(*activityExecutionResult).pending = pendingActivityCh
}
}
if err := stream.Send(wi); err != nil {
g.logger.Errorf("encountered an error while sending work item: %v", err)
return err
}
case key := <-pendingActivityCh:
delete(pendingActivities, key)
case key := <-pendingOrchestratorCh:
delete(pendingOrchestrators, key)
case <-g.streamShutdownChan:
return errShuttingDown
}
}
}
// CompleteOrchestratorTask implements protos.TaskHubSidecarServiceServer
func (g *grpcExecutor) CompleteOrchestratorTask(ctx context.Context, res *protos.OrchestratorResponse) (*protos.CompleteTaskResponse, error) {
iid := api.InstanceID(res.InstanceId)
if g.deletePendingOrchestrator(iid, res) {
return emptyCompleteTaskResponse, nil
}
return emptyCompleteTaskResponse, fmt.Errorf("unknown instance ID: %s", res.InstanceId)
}
func (g *grpcExecutor) deletePendingOrchestrator(iid api.InstanceID, res *protos.OrchestratorResponse) bool {
p, ok := g.pendingOrchestrators.LoadAndDelete(iid)
if !ok {
return false
}
// Note that res can be nil in case of certain failures
pending := p.(*ExecutionResults)
pending.Response = res
if pending.pending != nil {
pending.pending <- string(iid)
}
close(pending.complete)
return true
}
// CompleteActivityTask implements protos.TaskHubSidecarServiceServer
func (g *grpcExecutor) CompleteActivityTask(ctx context.Context, res *protos.ActivityResponse) (*protos.CompleteTaskResponse, error) {
key := getActivityExecutionKey(res.InstanceId, res.TaskId)
if g.deletePendingActivityTask(key, res) {
return emptyCompleteTaskResponse, nil
}
return emptyCompleteTaskResponse, fmt.Errorf("unknown instance ID/task ID combo: %s", key)
}
func (g *grpcExecutor) deletePendingActivityTask(key string, res *protos.ActivityResponse) bool {
p, ok := g.pendingActivities.LoadAndDelete(key)
if !ok {
return false
}
// Note that res can be nil in case of certain failures
pending := p.(*activityExecutionResult)
pending.response = res
if pending.pending != nil {
pending.pending <- key
}
close(pending.complete)
return true
}
func getActivityExecutionKey(iid string, taskID int32) string {
return iid + "/" + strconv.FormatInt(int64(taskID), 10)
}
// CreateTaskHub implements protos.TaskHubSidecarServiceServer
func (grpcExecutor) CreateTaskHub(context.Context, *protos.CreateTaskHubRequest) (*protos.CreateTaskHubResponse, error) {
return nil, errors.New("unimplemented")
}
// DeleteTaskHub implements protos.TaskHubSidecarServiceServer
func (grpcExecutor) DeleteTaskHub(context.Context, *protos.DeleteTaskHubRequest) (*protos.DeleteTaskHubResponse, error) {
return nil, errors.New("unimplemented")
}
// GetInstance implements protos.TaskHubSidecarServiceServer
func (g *grpcExecutor) GetInstance(ctx context.Context, req *protos.GetInstanceRequest) (*protos.GetInstanceResponse, error) {
metadata, err := g.backend.GetOrchestrationMetadata(ctx, api.InstanceID(req.InstanceId))
if err != nil {
return nil, err
}
if metadata == nil {
return &protos.GetInstanceResponse{Exists: false}, nil
}
return createGetInstanceResponse(req, metadata), nil
}
// PurgeInstances implements protos.TaskHubSidecarServiceServer
func (g *grpcExecutor) PurgeInstances(ctx context.Context, req *protos.PurgeInstancesRequest) (*protos.PurgeInstancesResponse, error) {
if req.GetPurgeInstanceFilter() != nil {
return nil, errors.New("multi-instance purge is not unimplemented")
}
count, err := purgeOrchestrationState(ctx, g.backend, api.InstanceID(req.GetInstanceId()), req.Recursive)
resp := &protos.PurgeInstancesResponse{DeletedInstanceCount: int32(count)}
if err != nil {
return resp, fmt.Errorf("failed to purge orchestration state: %w", err)
}
return resp, nil
}
// QueryInstances implements protos.TaskHubSidecarServiceServer
func (grpcExecutor) QueryInstances(context.Context, *protos.QueryInstancesRequest) (*protos.QueryInstancesResponse, error) {
return nil, errors.New("unimplemented")
}
// RaiseEvent implements protos.TaskHubSidecarServiceServer
func (g *grpcExecutor) RaiseEvent(ctx context.Context, req *protos.RaiseEventRequest) (*protos.RaiseEventResponse, error) {
e := helpers.NewEventRaisedEvent(req.Name, req.Input)
if err := g.backend.AddNewOrchestrationEvent(ctx, api.InstanceID(req.InstanceId), e); err != nil {
return nil, err
}
return &protos.RaiseEventResponse{}, nil
}
// StartInstance implements protos.TaskHubSidecarServiceServer
func (g *grpcExecutor) StartInstance(ctx context.Context, req *protos.CreateInstanceRequest) (*protos.CreateInstanceResponse, error) {
instanceID := req.InstanceId
ctx, span := helpers.StartNewCreateOrchestrationSpan(ctx, req.Name, req.Version.GetValue(), instanceID)
defer span.End()
e := helpers.NewExecutionStartedEvent(req.Name, instanceID, req.Input, nil, helpers.TraceContextFromSpan(span), req.ScheduledStartTimestamp)
if err := g.backend.CreateOrchestrationInstance(ctx, e, WithOrchestrationIdReusePolicy(req.OrchestrationIdReusePolicy)); err != nil {
return nil, err
}
return &protos.CreateInstanceResponse{InstanceId: instanceID}, nil
}
// TerminateInstance implements protos.TaskHubSidecarServiceServer
func (g *grpcExecutor) TerminateInstance(ctx context.Context, req *protos.TerminateRequest) (*protos.TerminateResponse, error) {
e := helpers.NewExecutionTerminatedEvent(req.Output, req.Recursive)
if err := g.backend.AddNewOrchestrationEvent(ctx, api.InstanceID(req.InstanceId), e); err != nil {
return nil, fmt.Errorf("failed to submit termination request: %w", err)
}
return &protos.TerminateResponse{}, nil
}
// SuspendInstance implements protos.TaskHubSidecarServiceServer
func (g *grpcExecutor) SuspendInstance(ctx context.Context, req *protos.SuspendRequest) (*protos.SuspendResponse, error) {
e := helpers.NewSuspendOrchestrationEvent(req.Reason.GetValue())
if err := g.backend.AddNewOrchestrationEvent(ctx, api.InstanceID(req.InstanceId), e); err != nil {
return nil, err
}
return &protos.SuspendResponse{}, nil
}
// ResumeInstance implements protos.TaskHubSidecarServiceServer
func (g *grpcExecutor) ResumeInstance(ctx context.Context, req *protos.ResumeRequest) (*protos.ResumeResponse, error) {
e := helpers.NewResumeOrchestrationEvent(req.Reason.GetValue())
if err := g.backend.AddNewOrchestrationEvent(ctx, api.InstanceID(req.InstanceId), e); err != nil {
return nil, err
}
return &protos.ResumeResponse{}, nil
}
// WaitForInstanceCompletion implements protos.TaskHubSidecarServiceServer
func (g *grpcExecutor) WaitForInstanceCompletion(ctx context.Context, req *protos.GetInstanceRequest) (*protos.GetInstanceResponse, error) {
return g.waitForInstance(ctx, req, func(m *api.OrchestrationMetadata) bool {
return m.IsComplete()
})
}
// WaitForInstanceStart implements protos.TaskHubSidecarServiceServer
func (g *grpcExecutor) WaitForInstanceStart(ctx context.Context, req *protos.GetInstanceRequest) (*protos.GetInstanceResponse, error) {
return g.waitForInstance(ctx, req, func(m *api.OrchestrationMetadata) bool {
return m.RuntimeStatus != protos.OrchestrationStatus_ORCHESTRATION_STATUS_PENDING
})
}
func (g *grpcExecutor) waitForInstance(ctx context.Context, req *protos.GetInstanceRequest, condition func(*api.OrchestrationMetadata) bool) (*protos.GetInstanceResponse, error) {
iid := api.InstanceID(req.InstanceId)
var b backoff.BackOff = &backoff.ExponentialBackOff{
InitialInterval: 1 * time.Millisecond,
MaxInterval: 3 * time.Second,
Multiplier: 1.5,
RandomizationFactor: 0.5,
Stop: backoff.Stop,
Clock: backoff.SystemClock,
}
b = backoff.WithContext(b, ctx)
b.Reset()
loop:
for {
t := time.NewTimer(b.NextBackOff())
select {
case <-ctx.Done():
if !t.Stop() {
<-t.C
}
break loop
case <-t.C:
metadata, err := g.backend.GetOrchestrationMetadata(ctx, iid)
if err != nil {
return nil, err
}
if metadata == nil {
return &protos.GetInstanceResponse{Exists: false}, nil
}
if condition(metadata) {
return createGetInstanceResponse(req, metadata), nil
}
}
}
return nil, status.Errorf(codes.Canceled, "instance hasn't completed")
}
// mustEmbedUnimplementedTaskHubSidecarServiceServer implements protos.TaskHubSidecarServiceServer
func (grpcExecutor) mustEmbedUnimplementedTaskHubSidecarServiceServer() {
}
func createGetInstanceResponse(req *protos.GetInstanceRequest, metadata *api.OrchestrationMetadata) *protos.GetInstanceResponse {
state := &protos.OrchestrationState{
InstanceId: req.InstanceId,
Name: metadata.Name,
OrchestrationStatus: metadata.RuntimeStatus,
CreatedTimestamp: timestamppb.New(metadata.CreatedAt),
LastUpdatedTimestamp: timestamppb.New(metadata.LastUpdatedAt),
}
if req.GetInputsAndOutputs {
state.Input = wrapperspb.String(metadata.SerializedInput)
state.CustomStatus = wrapperspb.String(metadata.SerializedCustomStatus)
state.Output = wrapperspb.String(metadata.SerializedOutput)
state.FailureDetails = metadata.FailureDetails
}
return &protos.GetInstanceResponse{Exists: true, OrchestrationState: state}
}