-
Notifications
You must be signed in to change notification settings - Fork 11
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 a retry-backoff logic to snapshot blob storage initialization #126
Merged
Aaronontheweb
merged 1 commit into
petabridge:dev
from
Arkatufus:Add_retry_backoff_logic_to_snapshot_blob_storage
Dec 15, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,17 @@ namespace Akka.Persistence.Azure.Snapshot | |
/// </summary> | ||
public class AzureBlobSnapshotStore : SnapshotStore | ||
{ | ||
private static readonly Dictionary<int, TimeSpan> RetryInterval = | ||
new Dictionary<int, TimeSpan>() | ||
{ | ||
{ 6, TimeSpan.FromMilliseconds(100) }, | ||
{ 5, TimeSpan.FromMilliseconds(500) }, | ||
{ 4, TimeSpan.FromMilliseconds(1000) }, | ||
{ 3, TimeSpan.FromMilliseconds(2000) }, | ||
{ 2, TimeSpan.FromMilliseconds(4000) }, | ||
{ 1, TimeSpan.FromMilliseconds(8000) }, | ||
}; | ||
|
||
private const string TimeStampMetaDataKey = "Timestamp"; | ||
private const string SeqNoMetaDataKey = "SeqNo"; | ||
|
||
|
@@ -43,26 +54,42 @@ public AzureBlobSnapshotStore(Config config = null) | |
_storageAccount = _settings.Development ? | ||
CloudStorageAccount.DevelopmentStorageAccount : | ||
CloudStorageAccount.Parse(_settings.ConnectionString); | ||
|
||
_container = new Lazy<CloudBlobContainer>(() => InitCloudStorage().Result); | ||
} | ||
|
||
public CloudBlobContainer Container => _container.Value; | ||
|
||
private async Task<CloudBlobContainer> InitCloudStorage() | ||
{ | ||
var blobClient = _storageAccount.CreateCloudBlobClient(); | ||
var containerRef = blobClient.GetContainerReference(_settings.ContainerName); | ||
var op = new OperationContext(); | ||
var retry = 5; | ||
var exceptions = new List<Exception>(); | ||
|
||
using (var cts = new CancellationTokenSource(_settings.ConnectTimeout)) | ||
while (true) | ||
{ | ||
if (await containerRef.CreateIfNotExistsAsync(BlobContainerPublicAccessType.Container, | ||
new BlobRequestOptions(), op, cts.Token)) | ||
_log.Info("Created Azure Blob Container", _settings.ContainerName); | ||
else | ||
_log.Info("Successfully connected to existing container", _settings.ContainerName); | ||
try | ||
{ | ||
var blobClient = _storageAccount.CreateCloudBlobClient(); | ||
var containerRef = blobClient.GetContainerReference(_settings.ContainerName); | ||
var op = new OperationContext(); | ||
|
||
if (await containerRef.CreateIfNotExistsAsync(BlobContainerPublicAccessType.Container, new BlobRequestOptions(), op)) | ||
_log.Info("Created Azure Blob Container", _settings.ContainerName); | ||
else | ||
_log.Info("Successfully connected to existing container", _settings.ContainerName); | ||
|
||
return containerRef; | ||
return containerRef; | ||
} | ||
catch (Exception ex) | ||
{ | ||
retry--; | ||
if (retry < 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be |
||
throw new AggregateException(exceptions); | ||
|
||
exceptions.Add(ex); | ||
_log.Error(ex, $"[{retry}] more tries to initialize blob storage remaining..."); | ||
await Task.Delay(RetryInterval[retry]); | ||
} | ||
} | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - exponential backoff table...