-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLooper.cs
247 lines (207 loc) · 9 KB
/
Looper.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
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
/* Cognite Extractor for OPC-UA
Copyright (C) 2021 Cognite AS
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
using Cognite.Extractor.Common;
using Cognite.OpcUa.Config;
using Microsoft.Extensions.Logging;
using Prometheus;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Cognite.OpcUa
{
public sealed class Looper
{
private readonly UAExtractor extractor;
private readonly FullConfig config;
public PeriodicScheduler Scheduler { get; }
private readonly IEnumerable<IPusher> pushers;
private readonly ILogger<Looper> log;
private TaskCompletionSource<bool>? pushWaiterSource;
private bool restart;
private List<IPusher> failingPushers = new List<IPusher>();
private List<IPusher> passingPushers = new List<IPusher>();
private static readonly Counter numPushes = Metrics.CreateCounter("opcua_num_pushes",
"Increments by one after each push to destination systems");
public Looper(
ILogger<Looper> log,
PeriodicScheduler scheduler,
UAExtractor extractor,
FullConfig config,
IEnumerable<IPusher> pushers)
{
this.log = log;
Scheduler = scheduler;
this.extractor = extractor;
this.config = config;
this.pushers = pushers;
}
private static TimeSpan ToTimespan(string? t, bool allowZero, string unit)
{
if (t == null) return Timeout.InfiniteTimeSpan;
var conv = CogniteTime.ParseTimeSpanString(t, unit);
if (conv == TimeSpan.Zero && !allowZero) return Timeout.InfiniteTimeSpan;
return conv ?? Timeout.InfiniteTimeSpan;
}
public void Run()
{
failingPushers = pushers.Where(pusher => pusher.DataFailing || pusher.EventsFailing || !pusher.Initialized).ToList();
passingPushers = pushers.Except(failingPushers).ToList();
Scheduler.SchedulePeriodicTask(nameof(Pushers), config.Extraction.DataPushDelayValue.Value, Pushers, true);
Scheduler.SchedulePeriodicTask(nameof(ExtraTasks), Timeout.InfiniteTimeSpan, ExtraTasks, false);
Scheduler.SchedulePeriodicTask(nameof(Rebrowse), config.Extraction.AutoRebrowsePeriodValue, Rebrowse, false);
if (extractor.StateStorage != null && !config.DryRun)
{
var interval = config.StateStorage.IntervalValue.Value;
Scheduler.SchedulePeriodicTask(nameof(StoreState), interval, StoreState, interval != Timeout.InfiniteTimeSpan);
}
Scheduler.SchedulePeriodicTask(nameof(HistoryRestart), config.History.RestartPeriodValue, HistoryRestart, false);
}
/// <summary>
/// Trigger a rebrowse.
/// </summary>
public void QueueRebrowse()
{
Scheduler.TryTriggerTask(nameof(Rebrowse));
}
/// <summary>
/// Schedule a restart of the extractor.
/// </summary>
public void Restart()
{
restart = true;
Scheduler.TryTriggerTask(nameof(ExtraTasks));
}
/// <summary>
/// Wait for the next push of data to CDF
/// </summary>
/// <param name="timeout">Timeout in 1/10th of a second</param>
public async Task WaitForNextPush(bool trigger = false, int timeout = 100)
{
pushWaiterSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
if (trigger)
{
Scheduler.TryTriggerTask(nameof(Pushers));
}
var t = new Stopwatch();
t.Start();
var waitTask = pushWaiterSource.Task;
var task = await Task.WhenAny(waitTask, Task.Delay(timeout * 100));
pushWaiterSource = null;
if (task != waitTask) throw new TimeoutException("Waiting for push timed out");
t.Stop();
log.LogDebug("Waited {MS} milliseconds for push", t.ElapsedMilliseconds);
}
private async Task CheckFailingPushers(CancellationToken token)
{
if (failingPushers.Count == 0) return;
var result = await Task.WhenAll(failingPushers.Select(pusher => pusher.TestConnection(config, token)));
var recovered = result.Select((res, idx) => (result: res, pusher: failingPushers.ElementAt(idx)))
.Where(x => x.result == true).ToList();
if (recovered.Count != 0)
{
log.LogInformation("Pushers {Names} recovered", string.Join(", ", recovered.Select(val => val.pusher.GetType())));
}
if (recovered.Any(pair => !pair.pusher.Initialized))
{
var tasks = new List<Task>();
var toInit = recovered.Select(pair => pair.pusher).Where(pusher => !pusher.Initialized);
foreach (var pusher in toInit)
{
pusher.NoInit = false;
var pending = pusher.PendingNodes;
pusher.PendingNodes = null;
tasks.Add(extractor.PushNodes(pending, pusher, true));
}
await Task.WhenAll(tasks);
}
foreach (var pair in recovered)
{
if (pair.pusher.Initialized)
{
pair.pusher.DataFailing = true;
pair.pusher.EventsFailing = true;
failingPushers.Remove(pair.pusher);
passingPushers.Add(pair.pusher);
}
}
}
private async Task Pushers(CancellationToken token)
{
if (token.IsCancellationRequested) return;
await CheckFailingPushers(token);
await Task.WhenAll(
Task.Run(async () => await extractor.Streamer.PushDataPoints(passingPushers, failingPushers, token), token),
Task.Run(async () => await extractor.Streamer.PushEvents(passingPushers, failingPushers, token), token)
);
var failedPushers = passingPushers.Where(pusher =>
pusher.DataFailing && extractor.Streamer.AllowData
|| pusher.EventsFailing && extractor.Streamer.AllowEvents
|| !pusher.Initialized).ToList();
foreach (var pusher in failedPushers)
{
pusher.DataFailing = extractor.Streamer.AllowData;
pusher.EventsFailing = extractor.Streamer.AllowEvents;
failingPushers.Add(pusher);
passingPushers.Remove(pusher);
}
numPushes.Inc();
pushWaiterSource?.TrySetResult(true);
}
public async Task StoreState(CancellationToken token)
{
if (token.IsCancellationRequested) return;
if (extractor.StateStorage == null) return;
await Task.WhenAll(
extractor.StateStorage.StoreExtractionState(extractor.State.NodeStates
.Where(state => state.FrontfillEnabled), config.StateStorage.VariableStore, token),
extractor.StateStorage.StoreExtractionState(extractor.State.EmitterStates
.Where(state => state.FrontfillEnabled), config.StateStorage.EventStore, token)
);
}
private async Task Rebrowse(CancellationToken token)
{
if (token.IsCancellationRequested) return;
await extractor.Rebrowse();
}
private async Task ExtraTasks(CancellationToken token)
{
if (token.IsCancellationRequested) return;
var newTasks = new List<Task>();
bool restarted = false;
if (restart)
{
restarted = true;
newTasks.Add(extractor.FinishExtractorRestart());
}
else
{
newTasks.Add(extractor.PushExtraNodes());
}
await Task.WhenAll(newTasks);
if (restarted)
{
restart = false;
}
}
private void HistoryRestart(CancellationToken token)
{
if (token.IsCancellationRequested) return;
log.LogInformation("Requesting restart of history...");
extractor.TriggerHistoryRestart();
}
}
}