Skip to content

Commit ce3cda9

Browse files
authored
extending retries for scale metric table creation (#5611)
1 parent aa3d057 commit ce3cda9

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/WebJobs.Script.WebHost/Scale/TableStorageScaleMetricsRepository.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,27 @@ public class TableStorageScaleMetricsRepository : IScaleMetricsRepository
2525
private const string MonitorIdPropertyName = "MonitorId";
2626
private const string SampleTimestampPropertyName = "SampleTimestamp";
2727
private const int MetricsPurgeDelaySeconds = 30;
28+
private const int DefaultTableCreationRetries = 3;
2829

2930
private readonly IConfiguration _configuration;
3031
private readonly IHostIdProvider _hostIdProvider;
3132
private readonly ScaleOptions _scaleOptions;
3233
private readonly ILogger _logger;
34+
private readonly int _tableCreationRetries;
3335
private CloudTableClient _tableClient;
3436

3537
public TableStorageScaleMetricsRepository(IConfiguration configuration, IHostIdProvider hostIdProvider, IOptions<ScaleOptions> scaleOptions, ILoggerFactory loggerFactory)
38+
: this(configuration, hostIdProvider, scaleOptions, loggerFactory, DefaultTableCreationRetries)
39+
{
40+
}
41+
42+
internal TableStorageScaleMetricsRepository(IConfiguration configuration, IHostIdProvider hostIdProvider, IOptions<ScaleOptions> scaleOptions, ILoggerFactory loggerFactory, int tableCreationRetries)
3643
{
3744
_configuration = configuration;
3845
_hostIdProvider = hostIdProvider;
3946
_scaleOptions = scaleOptions.Value;
4047
_logger = loggerFactory.CreateLogger<TableStorageScaleMetricsRepository>();
48+
_tableCreationRetries = tableCreationRetries;
4149
}
4250

4351
internal CloudTableClient TableClient
@@ -199,7 +207,7 @@ internal async Task ExecuteBatchSafeAsync(TableBatchOperation batch, DateTime? n
199207
}
200208
}
201209

202-
internal async Task CreateIfNotExistsAsync(CloudTable table, int retryCount = 3, int retryDelayMS = 1000)
210+
internal async Task CreateIfNotExistsAsync(CloudTable table, int retryDelayMS = 1000)
203211
{
204212
int attempt = 0;
205213
do
@@ -222,7 +230,8 @@ internal async Task CreateIfNotExistsAsync(CloudTable table, int retryCount = 3,
222230
// though these should only happen in tests not production, because we only ever
223231
// delete OLD tables and we'll never be attempting to recreate a table we just
224232
// deleted outside of tests.
225-
if (e.RequestInformation.HttpStatusCode == (int)HttpStatusCode.Conflict)
233+
if (e.RequestInformation.HttpStatusCode == (int)HttpStatusCode.Conflict &&
234+
attempt < _tableCreationRetries)
226235
{
227236
// wait a bit and try again
228237
await Task.Delay(retryDelayMS);
@@ -233,7 +242,7 @@ internal async Task CreateIfNotExistsAsync(CloudTable table, int retryCount = 3,
233242

234243
return;
235244
}
236-
while (attempt++ < retryCount);
245+
while (attempt++ < _tableCreationRetries);
237246
}
238247

239248
internal async Task AccumulateMetricsBatchAsync(TableBatchOperation batch, IScaleMonitor monitor, IEnumerable<ScaleMetrics> metrics, DateTime? now = null)

test/WebJobs.Script.Tests.Integration/Scale/TableStorageScaleMetricsRepositoryTests.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ public TableStorageScaleMetricsRepositoryTests()
4141
_loggerProvider = new TestLoggerProvider();
4242
ILoggerFactory loggerFactory = new LoggerFactory();
4343
loggerFactory.AddProvider(_loggerProvider);
44-
_repository = new TableStorageScaleMetricsRepository(configuration, _hostIdProviderMock.Object, new OptionsWrapper<ScaleOptions>(_scaleOptions), loggerFactory);
44+
45+
// Allow for up to 30 seconds of creation retries for tests due to slow table deletes
46+
_repository = new TableStorageScaleMetricsRepository(configuration, _hostIdProviderMock.Object, new OptionsWrapper<ScaleOptions>(_scaleOptions), loggerFactory, 30);
4547

4648
EmptyMetricsTableAsync().GetAwaiter().GetResult();
4749
}
@@ -163,7 +165,7 @@ public async Task ReadMetricsAsync_FiltersExpiredMetrics()
163165
}
164166

165167
await _repository.ExecuteBatchSafeAsync(batch);
166-
168+
167169
var result = await _repository.ReadMetricsAsync(monitors);
168170

169171
var resultMetrics = result[monitor1].Cast<TestScaleMetrics1>().ToArray();
@@ -327,7 +329,7 @@ await TestHelpers.Await(async () =>
327329
public async Task LogStorageException_LogsDetails()
328330
{
329331
StorageException ex = null;
330-
var table =_repository.TableClient.GetTableReference("dne");
332+
var table = _repository.TableClient.GetTableReference("dne");
331333
var continuationToken = new TableContinuationToken();
332334
try
333335
{

0 commit comments

Comments
 (0)