-
Notifications
You must be signed in to change notification settings - Fork 325
Expand file tree
/
Copy pathAzureStorageClient.cs
More file actions
200 lines (163 loc) · 9.64 KB
/
AzureStorageClient.cs
File metadata and controls
200 lines (163 loc) · 9.64 KB
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
// ----------------------------------------------------------------------------------
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------
#nullable enable
namespace DurableTask.AzureStorage.Storage
{
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using DurableTask.AzureStorage.Monitoring;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Queue;
using Microsoft.WindowsAzure.Storage.Table;
class AzureStorageClient
{
static readonly TimeSpan StorageMaximumExecutionTime = TimeSpan.FromMinutes(2);
readonly CloudBlobClient blobClient;
readonly CloudQueueClient queueClient;
readonly CloudTableClient tableClient;
readonly SemaphoreSlim requestThrottleSemaphore;
public AzureStorageClient(AzureStorageOrchestrationServiceSettings settings) :
this(settings.StorageAccountDetails == null ?
CloudStorageAccount.Parse(settings.StorageConnectionString) : settings.StorageAccountDetails.ToCloudStorageAccount(),
settings)
{ }
public AzureStorageClient(CloudStorageAccount account, AzureStorageOrchestrationServiceSettings settings)
{
this.Settings = settings;
this.BlobAccountName = GetAccountName(account.Credentials, settings, account.BlobStorageUri, "blob");
this.QueueAccountName = GetAccountName(account.Credentials, settings, account.QueueStorageUri, "queue");
this.TableAccountName = GetAccountName(account.Credentials, settings, account.TableStorageUri, "table");
this.Stats = new AzureStorageOrchestrationServiceStats();
this.queueClient = account.CreateCloudQueueClient();
this.queueClient.BufferManager = SimpleBufferManager.Shared;
if (settings.HasTrackingStoreStorageAccount)
{
var trackingStoreAccount = settings.TrackingStoreStorageAccountDetails.ToCloudStorageAccount();
this.tableClient = trackingStoreAccount.CreateCloudTableClient();
this.blobClient = trackingStoreAccount.CreateCloudBlobClient();
}
else
{
this.tableClient = account.CreateCloudTableClient();
this.blobClient = account.CreateCloudBlobClient();
}
this.blobClient.BufferManager = SimpleBufferManager.Shared;
this.blobClient.DefaultRequestOptions.MaximumExecutionTime = StorageMaximumExecutionTime;
this.tableClient.BufferManager = SimpleBufferManager.Shared;
this.requestThrottleSemaphore = new SemaphoreSlim(this.Settings.MaxStorageOperationConcurrency);
}
public AzureStorageOrchestrationServiceSettings Settings { get; }
public AzureStorageOrchestrationServiceStats Stats { get; }
public string BlobAccountName { get; }
public string QueueAccountName { get; }
public string TableAccountName { get; }
public Blob GetBlobReference(string container, string blobName, string? blobDirectory = null)
{
NameValidator.ValidateBlobName(blobName);
return new Blob(this, this.blobClient, container, blobName, blobDirectory);
}
internal Blob GetBlobReference(Uri blobUri)
{
return new Blob(this, this.blobClient, blobUri);
}
public BlobContainer GetBlobContainerReference(string container)
{
NameValidator.ValidateContainerName(container);
return new BlobContainer(this, this.blobClient, container);
}
public Queue GetQueueReference(string queueName)
{
NameValidator.ValidateQueueName(queueName);
return new Queue(this, this.queueClient, queueName);
}
public Table GetTableReference(string tableName)
{
NameValidator.ValidateTableName(tableName);
return new Table(this, this.tableClient, tableName);
}
public Task<T> MakeBlobStorageRequest<T>(Func<OperationContext, CancellationToken, Task<T>> storageRequest, string operationName, string? clientRequestId = null) =>
this.MakeStorageRequest<T>(storageRequest, BlobAccountName, operationName, clientRequestId);
public Task<T> MakeQueueStorageRequest<T>(Func<OperationContext, CancellationToken, Task<T>> storageRequest, string operationName, string? clientRequestId = null) =>
this.MakeStorageRequest<T>(storageRequest, QueueAccountName, operationName, clientRequestId);
public Task<T> MakeTableStorageRequest<T>(Func<OperationContext, CancellationToken, Task<T>> storageRequest, string operationName, string? clientRequestId = null) =>
this.MakeStorageRequest<T>(storageRequest, TableAccountName, operationName, clientRequestId);
public Task MakeBlobStorageRequest(Func<OperationContext, CancellationToken, Task> storageRequest, string operationName, string? clientRequestId = null, bool force = false) =>
this.MakeStorageRequest(storageRequest, BlobAccountName, operationName, clientRequestId, force);
public Task MakeQueueStorageRequest(Func<OperationContext, CancellationToken, Task> storageRequest, string operationName, string? clientRequestId = null) =>
this.MakeStorageRequest(storageRequest, QueueAccountName, operationName, clientRequestId);
public Task MakeTableStorageRequest(Func<OperationContext, CancellationToken, Task> storageRequest, string operationName, string? clientRequestId = null) =>
this.MakeStorageRequest(storageRequest, TableAccountName, operationName, clientRequestId);
private async Task<T> MakeStorageRequest<T>(Func<OperationContext, CancellationToken, Task<T>> storageRequest, string accountName, string operationName, string? clientRequestId = null, bool force = false)
{
Guid guid = Guid.NewGuid();
if (!force)
{
var cTkWait = new CancellationTokenSource();
_ = WaitAndLog(guid, "Wait", cTkWait.Token);
await requestThrottleSemaphore.WaitAsync();
cTkWait.Cancel();
}
try
{
return await TimeoutHandler.ExecuteWithTimeout<T>(operationName, accountName, this.Settings, storageRequest, this.Stats, clientRequestId);
}
catch (StorageException ex)
{
throw new DurableTaskStorageException(ex);
}
finally
{
if (!force)
{
var cTkRelease = new CancellationTokenSource();
_ = WaitAndLog(guid, "Release", cTkRelease.Token);
requestThrottleSemaphore.Release();
cTkRelease.Cancel();
}
}
}
private Task MakeStorageRequest(Func<OperationContext, CancellationToken, Task> storageRequest, string accountName, string operationName, string? clientRequestId = null, bool force = false) =>
this.MakeStorageRequest<object?>((context, cancellationToken) => WrapFunctionWithReturnType(storageRequest, context, cancellationToken), accountName, operationName, clientRequestId, force);
private static async Task<object?> WrapFunctionWithReturnType(Func<OperationContext, CancellationToken, Task> storageRequest, OperationContext context, CancellationToken cancellationToken)
{
await storageRequest(context, cancellationToken);
return null;
}
private static string GetAccountName(StorageCredentials credentials, AzureStorageOrchestrationServiceSettings settings, StorageUri serviceUri, string service) =>
credentials.AccountName ?? settings.StorageAccountDetails?.AccountName ?? serviceUri.GetAccountName(service) ?? "(unknown)";
private async Task WaitAndLog(Guid guid, string waitOrRelease, CancellationToken cTk)
{
if (this.Settings.MaxWaitForSemaphoreLoggingInSeconds >= 0)
{
await Task.Delay(this.Settings.MaxWaitForSemaphoreLoggingInSeconds * 1000, cTk);
if (!cTk.IsCancellationRequested)
{
StackTrace stackTrace = new StackTrace();
this.Settings?.Logger.PartitionManagerInfo(
this.Settings?.StorageAccountDetails?.AccountName,
this.Settings?.TaskHubName,
this.Settings?.WorkerId,
string.Empty,
$"Before {waitOrRelease} Id:{guid.ToString()}, processId: {Thread.CurrentThread.ManagedThreadId}" +
$"| Semaphore currentCount = {requestThrottleSemaphore.CurrentCount}" +
$"| Semaphore initialCount = {this.Settings?.MaxStorageOperationConcurrency}" +
$"| StackTrace : {stackTrace.ToString()}");
}
}
}
}
}