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
|
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Integration.Operation;
namespace Tango.Telemetry
{
public class TelemetryPublisherAdvanced : TelemetryPublisher
{
/// <summary>
/// Defines the maximum backoff delay between retries when exponential backoff is applied.
/// For example, setting to 1 hour means the retry interval will not exceed 1 hour regardless of the retry count.
/// </summary>
public TimeSpan MaxExponentialBackoff { get; set; } = TimeSpan.FromHours(1);
public TelemetryPublisherAdvanced(ITelemetryStorageManager storageManager, ITelemetryQueueManager queueManager, TelemetryPublisherConfiguration config) : base(storageManager, queueManager, config)
{
}
protected override async Task<TelemetryPublishResult> PublishTelemetryPackage(TelemetryPublishPackage package)
{
Stopwatch totalWatch = Stopwatch.StartNew();
var result = new TelemetryPublishResult();
if (!IsStarted || _isDisposed) return result;
List<KeyValuePair<string, string>> properties = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("MachineID", Config.MachineID),
new KeyValuePair<string, string>("Model", Config.MachineType.ToShortName()),
new KeyValuePair<string, string>("Environment", Config.Environment)
};
var now = DateTime.UtcNow;
var pendingDestinations = package.PendingTelemetry.PendingDestinations.ToList();
// For Streaming/External: initialize pending destinations list (used if publishing fails)
if (package.SourceType == TelemetrySourceTypes.Streaming || package.SourceType == TelemetrySourceTypes.ExternalStorage)
{
foreach (var dest in Destinations)
{
if (!pendingDestinations.Any(x => x.Name == dest.Name))
{
pendingDestinations.Add(new TelemetryPendingDestination
{
Name = dest.Name,
RetryCount = 0,
LastAttempt = DateTime.MinValue,
NextEligibleAttempt = now
});
}
}
}
foreach (var destination in Destinations.Where(x => x.SupportedSourceTypes.Contains(package.SourceType)))
{
var pendingEntry = pendingDestinations.FirstOrDefault(x => x.Name == destination.Name);
if (pendingEntry == null)
continue;
Stopwatch destinationWatch = Stopwatch.StartNew();
var destinationResult = new TelemetryPublishResult.DestinationResult();
destinationResult.Destination = destination;
result.DestinationsResults.Add(destinationResult);
// Respect backoff timing
if (now < pendingEntry.NextEligibleAttempt)
{
destinationWatch.Stop();
destinationResult.Status = TelemetryPublishResult.DestinationStatus.Postponed;
destinationResult.ElapsedTime = destinationWatch.Elapsed;
continue;
}
try
{
if (OnPublishingPackage(package, destination))
{
if (await destination.IsAvailable())
{
await destination.Publish(package, properties);
OnPackagePublished(package, destination);
destinationWatch.Stop();
destinationResult.Status = TelemetryPublishResult.DestinationStatus.Passed;
destinationResult.ElapsedTime = destinationWatch.Elapsed;
// On success: remove entry from pending list
pendingDestinations.RemoveAll(x => x.Name == destination.Name);
}
else
{
destinationWatch.Stop();
destinationResult.Status = TelemetryPublishResult.DestinationStatus.Unavailable;
destinationResult.ElapsedTime = destinationWatch.Elapsed;
// Only track retry state if retry is supported
if (destination.SupportedSourceTypes.Contains(TelemetrySourceTypes.PendingStorage))
{
if (pendingEntry == null)
{
pendingEntry = new TelemetryPendingDestination { Name = destination.Name };
pendingDestinations.Add(pendingEntry);
}
pendingEntry.RetryCount++;
pendingEntry.LastAttempt = now;
// Apply exponential backoff
int delaySeconds = Math.Min((int)Math.Pow(2, pendingEntry.RetryCount), (int)MaxExponentialBackoff.TotalSeconds);
pendingEntry.NextEligibleAttempt = now.AddSeconds(delaySeconds);
}
else
{
// Remove if not retryable
pendingDestinations.RemoveAll(x => x.Name == destination.Name);
}
}
}
}
catch (Exception ex)
{
destinationWatch.Stop();
destinationResult.Status = TelemetryPublishResult.DestinationStatus.Failed;
destinationResult.Error = ex;
destinationResult.ElapsedTime = destinationWatch.Elapsed;
LogManager.Log(ex, $"Error publishing telemetry package to destination {destination.Name}.");
OnPackagePublishFailed(package, destination, ex);
// Only track retry state if retry is supported
if (destination.SupportedSourceTypes.Contains(TelemetrySourceTypes.PendingStorage))
{
if (pendingEntry == null)
{
pendingEntry = new TelemetryPendingDestination { Name = destination.Name };
pendingDestinations.Add(pendingEntry);
}
pendingEntry.RetryCount++;
pendingEntry.LastAttempt = now;
// Apply exponential backoff
int delaySeconds = Math.Min((int)Math.Pow(2, pendingEntry.RetryCount), (int)MaxExponentialBackoff.TotalSeconds);
pendingEntry.NextEligibleAttempt = now.AddSeconds(delaySeconds);
}
else
{
// Remove if not retryable
pendingDestinations.RemoveAll(x => x.Name == destination.Name);
}
}
}
package.PendingTelemetry.PendingDestinations = new List<TelemetryPendingDestination>(pendingDestinations);
if (package.SourceType == TelemetrySourceTypes.PendingStorage && !pendingDestinations.Any())
{
StorageManager.DeletePendingTelemetry(package.PendingTelemetry);
}
else
{
StorageManager.DeletePendingTelemetry(package.PendingTelemetry);
}
totalWatch.Stop();
result.TotalElapsedTime = totalWatch.Elapsed;
return result;
}
}
}
|