-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStreamingJobOperatorService.cs
188 lines (172 loc) · 7.97 KB
/
StreamingJobOperatorService.cs
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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Akka.Streams;
using Akka.Streams.Dsl;
using Akka.Util;
using Arcane.Operator.Configurations;
using Arcane.Operator.Exceptions;
using Arcane.Operator.Extensions;
using Arcane.Operator.Models.Api;
using Arcane.Operator.Models.Base;
using Arcane.Operator.Models.Commands;
using Arcane.Operator.Models.StreamDefinitions.Base;
using Arcane.Operator.Services.Base;
using Arcane.Operator.Services.Base.CommandHandlers;
using Arcane.Operator.Services.Base.Metrics;
using Arcane.Operator.Services.Base.Repositories.CustomResources;
using Arcane.Operator.Services.Base.Repositories.StreamingJob;
using k8s;
using k8s.Models;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Snd.Sdk.ActorProviders;
using Snd.Sdk.Kubernetes;
using Snd.Sdk.Tasks;
using Akka.Streams.Supervision;
namespace Arcane.Operator.Services.Operators;
public class StreamingJobOperatorService : IStreamingJobOperatorService
{
private const int parallelism = 1;
private readonly StreamingJobOperatorServiceConfiguration configuration;
private readonly ILogger<StreamingJobOperatorService> logger;
private readonly IResourceCollection<IStreamDefinition> streamDefinitionCollection;
private readonly IMetricsReporter metricsReporter;
private readonly ICommandHandler<UpdateStatusCommand> updateStatusCommandHandler;
private readonly ICommandHandler<SetAnnotationCommand<IStreamDefinition>> setAnnotationCommandHandler;
private readonly ICommandHandler<StreamingJobCommand> streamingJobCommandHandler;
private readonly ICommandHandler<RemoveAnnotationCommand<IStreamDefinition>> removeAnnotationHandler;
private readonly IStreamingJobCollection streamingJobCollection;
public StreamingJobOperatorService(
ILogger<StreamingJobOperatorService> logger,
IOptions<StreamingJobOperatorServiceConfiguration> options,
IMetricsReporter metricsReporter,
IResourceCollection<IStreamDefinition> streamDefinitionCollection,
ICommandHandler<UpdateStatusCommand> updateStatusCommandHandler,
ICommandHandler<SetAnnotationCommand<IStreamDefinition>> setAnnotationCommandHandler,
ICommandHandler<StreamingJobCommand> streamingJobCommandHandler,
ICommandHandler<RemoveAnnotationCommand<IStreamDefinition>> removeAnnotationHandler,
IStreamingJobCollection streamingJobCollection)
{
this.configuration = options.Value;
this.streamDefinitionCollection = streamDefinitionCollection;
this.logger = logger;
this.metricsReporter = metricsReporter;
this.updateStatusCommandHandler = updateStatusCommandHandler;
this.streamingJobCommandHandler = streamingJobCommandHandler;
this.setAnnotationCommandHandler = setAnnotationCommandHandler;
this.streamingJobCollection = streamingJobCollection;
this.removeAnnotationHandler = removeAnnotationHandler;
}
public IRunnableGraph<Task> GetJobEventsGraph(CancellationToken cancellationToken)
{
return this.streamingJobCollection.GetEvents(this.configuration.Namespace, this.configuration.MaxBufferCapacity)
.Via(cancellationToken.AsFlow<ResourceEvent<V1Job>>(true))
.Select(this.metricsReporter.ReportTrafficMetrics)
.SelectAsync(parallelism, this.OnJobEvent)
.SelectMany(e => e)
.CollectOption()
.WithAttributes(ActorAttributes.CreateSupervisionStrategy(this.HandleError))
.ToMaterialized(Sink.ForEachAsync<KubernetesCommand>(parallelism, this.HandleCommand), Keep.Right);
}
private Task<List<Option<KubernetesCommand>>> OnJobEvent(ResourceEvent<V1Job> valueTuple)
{
return valueTuple switch
{
(WatchEventType.Added, var job) => this.OnJobAdded(job),
(WatchEventType.Modified, var job) => Task.FromResult(new List<Option<KubernetesCommand>> { this.OnJobModified(job) }),
(WatchEventType.Deleted, var job) => this.OnJobDelete(job),
_ => Task.FromResult(new List<Option<KubernetesCommand>>())
};
}
private Task<List<Option<KubernetesCommand>>> OnJobAdded(V1Job job)
{
return this.streamDefinitionCollection
.Get(job.Name(), job.ToOwnerApiRequest())
.Map(maybeSd => maybeSd switch
{
{ HasValue: true, Value: var sd } when job.IsReloading() && sd.ReloadRequested => new
List<Option<KubernetesCommand>>
{
new RemoveReloadRequestedAnnotation(sd),
new Reloading(sd)
},
{ HasValue: true, Value: var sd } when job.IsReloading() && !sd.ReloadRequested => new
List<Option<KubernetesCommand>>
{
new Reloading(sd)
},
_ => new List<Option<KubernetesCommand>>()
});
}
private Option<KubernetesCommand> OnJobModified(V1Job job)
{
var streamId = job.GetStreamId();
if (job.IsStopping())
{
this.logger.LogInformation("Streaming job for stream with id {streamId} is already stopping",
streamId);
return Option<KubernetesCommand>.None;
}
if (job.IsReloadRequested() || job.IsRestartRequested())
{
return new StopJob(job.Name(), job.Namespace());
}
return Option<KubernetesCommand>.None;
}
private Task<List<Option<KubernetesCommand>>> OnJobDelete(V1Job job)
{
return this.streamDefinitionCollection
.Get(job.Name(), job.ToOwnerApiRequest())
.Map(maybeSd => maybeSd switch
{
{ HasValue: true, Value: var sd } when job.IsFailed() => new List<Option<KubernetesCommand>>
{
new SetCrashLoopStatusCommand(sd),
new SetCrashLoopStatusAnnotationCommand(sd)
},
{ HasValue: true, Value: var sd } when sd.Suspended => new List<Option<KubernetesCommand>>
{
new Suspended(sd)
},
{ HasValue: true, Value: var sd } when sd.CrashLoopDetected => new List<Option<KubernetesCommand>>
{
new SetCrashLoopStatusCommand(sd)
},
{ HasValue: true, Value: var sd } when !sd.Suspended => new List<Option<KubernetesCommand>>
{
new StartJob(sd, job.IsReloadRequested() || job.IsSchemaMismatch())
},
{ HasValue: false } => new List<Option<KubernetesCommand>>(),
_ => throw new ArgumentOutOfRangeException(nameof(maybeSd), maybeSd, null)
});
}
private Task HandleCommand(KubernetesCommand response) => response switch
{
UpdateStatusCommand sdc => this.updateStatusCommandHandler.Handle(sdc),
StreamingJobCommand sjc => this.streamingJobCommandHandler.Handle(sjc),
SetAnnotationCommand<IStreamDefinition> sac => this.setAnnotationCommandHandler.Handle(sac),
RemoveAnnotationCommand<IStreamDefinition> command => this.removeAnnotationHandler.Handle(command),
_ => throw new ArgumentOutOfRangeException(nameof(response), response, null)
};
private Directive HandleError(Exception exception)
{
this.logger.LogError(exception, "Failed to handle stream definition event");
return exception switch
{
JobListenerException ex => this.LogAndContinue(ex),
_ => this.LogAndStop(exception),
};
}
private Directive LogAndContinue(Exception exception)
{
this.logger.LogWarning(exception, "Failed to handle stream definition event");
return Directive.Resume;
}
private Directive LogAndStop(Exception exception)
{
this.logger.LogError(exception, "Failed to handle stream definition event");
return Directive.Resume;
}
}