-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStreamingJobOperatorService.cs
133 lines (122 loc) · 5.74 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
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.Extensions;
using Arcane.Operator.Models.Api;
using Arcane.Operator.Models.Commands;
using Arcane.Operator.Models.StreamDefinitions.Base;
using Arcane.Operator.Services.Base;
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;
namespace Arcane.Operator.Services.Operator;
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 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,
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;
}
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()
.ToMaterialized(Sink.ForEachAsync<KubernetesCommand>(parallelism, this.HandleCommand), Keep.Right);
}
private Task<List<Option<KubernetesCommand>>> OnJobEvent(ResourceEvent<V1Job> valueTuple)
{
return valueTuple switch
{
(WatchEventType.Deleted, var job) => this.OnJobDelete(job),
(WatchEventType.Modified, var job) => Task.FromResult(new List<Option<KubernetesCommand>> { this.OnJobModified(job) }),
_ => Task.FromResult(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.GetStreamKind(), streamId);
}
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),
_ => throw new ArgumentOutOfRangeException(nameof(response), response, null)
};
}