Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exponential backoff to reading config file #2467

Merged
merged 7 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/Config/FileSystemRuntimeConfigLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Reflection;
using System.Text.Json;
using Azure.DataApiBuilder.Config.ObjectModel;
using Azure.DataApiBuilder.Config.Utilities;
using Azure.DataApiBuilder.Service.Exceptions;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -199,8 +200,31 @@ public bool TryLoadConfig(
// and ensures the file handle is released immediately after reading.
// Previous usage of File.Open may cause file locking issues when
// actively using hot-reload and modifying the config file in a text editor.
string json = _fileSystem.File.ReadAllText(path);
if (TryParseConfig(json, out RuntimeConfig, connectionString: _connectionString, replaceEnvVar: replaceEnvVar))
// Includes an exponential back-off retry mechanism to accommodate
// circumstances where the file may be in use by another process.
int runCount = 1;
string json = string.Empty;
while (runCount <= FileUtilities.RunLimit)
{
try
{
json = _fileSystem.File.ReadAllText(path);
break;
}
catch (IOException ex)
{
Console.WriteLine($"IO Exception, retrying due to {ex.Message}");
if (runCount == FileUtilities.RunLimit)
{
throw;
}

Thread.Sleep(TimeSpan.FromSeconds(Math.Pow(FileUtilities.ExponentialRetryBase, runCount)));
runCount++;
}
}

if (!string.IsNullOrEmpty(json) && TryParseConfig(json, out RuntimeConfig, connectionString: _connectionString, replaceEnvVar: replaceEnvVar))
{
if (TrySetupConfigFileWatcher())
{
Expand Down
12 changes: 6 additions & 6 deletions src/Config/Utilities/FileUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ internal class FileUtilities
/// <summary>
/// Limit the number of retries when failures occur.
/// </summary>
private static readonly int _runLimit = 3;
public static readonly int RunLimit = 3;

/// <summary>
/// Base of the exponential retry back-off.
/// -> base ^ runCount
/// e.g. 2^1 , 2^2, 2^3, etc.
/// </summary>
private static readonly int _exponentialRetryBase = 2;
public static readonly int ExponentialRetryBase = 2;

/// <summary>
/// Computes the SHA256 hash for the input data (file contents) at the given path.
Expand All @@ -41,8 +41,8 @@ public static byte[] ComputeHash(IFileSystem fileSystem, string filePath)
// Exponential back-off retry mechanism.
int runCount = 1;

// Maximum 2^_runLimit seconds of wait time due to retries.
while (runCount <= _runLimit)
// Maximum 2^RunLimit seconds of wait time due to retries.
while (runCount <= RunLimit)
{
try
{
Expand All @@ -68,12 +68,12 @@ public static byte[] ComputeHash(IFileSystem fileSystem, string filePath)
catch (IOException ex)
{
Console.WriteLine($"IO Exception, retrying due to {ex.Message}");
if (runCount == 3)
if (runCount == RunLimit)
{
throw;
}

Thread.Sleep(TimeSpan.FromSeconds(Math.Pow(_exponentialRetryBase, runCount)));
Thread.Sleep(TimeSpan.FromSeconds(Math.Pow(ExponentialRetryBase, runCount)));
runCount++;
}
}
Expand Down