orleans icon indicating copy to clipboard operation
orleans copied to clipboard

CloudTable reference in Azure Table Storage is a singleton but it is not threadsafe

Open rmja opened this issue 8 years ago • 12 comments

The AzureTableDataManager used by AzureTableGrainStorage which is registered as a singleton has a single instance of CloudTable, but this type is not thread safe. This results in exceptions thrown by the AzureStorage under load from AzureTableDataManager.ReadSingleTableEntryAsync:

Microsoft.WindowsAzure.Storage.StorageException: An item with the same key has already been added. Key: timeout ---> System.ArgumentException: An item with the same key has already been added. Key: timeout
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at Microsoft.WindowsAzure.Storage.Shared.Protocol.HttpRequestMessageFactory.CreateRequestMessage(HttpMethod method, Uri uri, Nullable`1 timeout, UriQueryBuilder builder, HttpContent content, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
   at Microsoft.WindowsAzure.Storage.Table.Protocol.TableOperationHttpRequestMessageFactory.BuildRequestCore(Uri uri, UriQueryBuilder builder, HttpMethod method, Nullable`1 timeout, HttpContent content, OperationContext ctx, ICanonicalizer canonicalizer, StorageCredentials credentials)
   at Microsoft.WindowsAzure.Storage.Table.Protocol.TableOperationHttpRequestMessageFactory.BuildRequestForTableQuery(Uri uri, UriQueryBuilder builder, Nullable`1 timeout, HttpContent content, OperationContext ctx, TablePayloadFormat payloadFormat, ICanonicalizer canonicalizer, StorageCredentials credentials)
   at Microsoft.WindowsAzure.Storage.Table.TableQuery`1.<>c__DisplayClass24_0`2.<QueryImpl>b__0(RESTCommand`1 cmd, Uri uri, UriQueryBuilder queryBuilder, HttpContent cnt, Nullable`1 serverTimeout, OperationContext ctx)
   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteAsyncInternal[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext, CancellationToken token)
   --- End of inner exception stack trace ---
   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteAsyncInternal[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext, CancellationToken token)
   at Microsoft.WindowsAzure.Storage.Table.TableQuery`1.<>c__DisplayClass20_0.<<ExecuteQuerySegmentedAsync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Orleans.Persistence.AzureStorage.AzureTableDataManager`1.ReadSingleTableEntryAsync(String partitionKey, String rowKey)
   at SampleStore.Silo.CustomAzureTableGrainStorage.GrainStateTableDataManager.Read(String partitionKey, String rowKey) in C:\Source\Utiliread\src\SampleStore.Silo\CustomAzureTableGrainStorage.cs:line 408
   at SampleStore.Silo.CustomAzureTableGrainStorage.ReadStateAsync(String grainType, GrainReference grainReference, IGrainState grainState) in C:\Source\Utiliread\src\SampleStore.Silo\CustomAzureTableGrainStorage.cs:line 74
   at Orleans.Core.StateStorageBridge`1.ReadStateAsync()

The Table property on AzureTableDataManager is unfortunately public, so I don't see an easy fix (that I can do). If it was private to the manager, one could simply have a ConcurrentBag<CloudTable> where one can borrow initialized clients from.

rmja avatar Sep 18 '18 12:09 rmja

It turns out that I get this error when I am activating too many cold grains from table storage in a Task.WhenAll(). The ExecuteQuerySegmentedAsync call then throws an exception with status code 306 which is uncaught.

rmja avatar Sep 18 '18 16:09 rmja

I think this is very related to what I am seeing: https://github.com/Azure/azure-storage-net/issues/620

rmja avatar Sep 19 '18 22:09 rmja

We are also seeing this exception, but not even under a lot of load.

vmendi avatar Sep 20 '18 00:09 vmendi

@rmja sounds correct. I saw that one of our internal users also hit the same issue. I took a stab at implementing an easy fix, but I'm I don't know how to build/test that library.

https://github.com/Azure/azure-storage-net/compare/master...ReubenBond:fix-620?expand=1

ReubenBond avatar Sep 20 '18 00:09 ReubenBond

@ReubenBond I sent a PR https://github.com/Azure/azure-storage-net/pull/774. It seems to fix the issue.

rmja avatar Sep 20 '18 02:09 rmja

Thanks, @rmja - your fix looks good, much better than mine 😜

ReubenBond avatar Sep 20 '18 21:09 ReubenBond

@ReubenBond It seems as if the Table storage API is not supported by the Azure Storage API anymore: https://github.com/Azure/azure-storage-net#features This seems akward! - but how do we proceed from here?

rmja avatar Oct 01 '18 12:10 rmja

I created this issue, lets see what they have to say: https://github.com/Azure/azure-storage-net/issues/779

rmja avatar Oct 01 '18 12:10 rmja

Interesting turn of events 😅 - thanks for following up with them. I suppose we need to wait for this new .NET Standard API to become available, as mentioned in the linked issue.

ReubenBond avatar Oct 04 '18 00:10 ReubenBond

@rmja @ReubenBond I have a naive idea. Would something like this do the trick? https://github.com/dotnet/orleans/commit/ef65cb9ec1a2fda68e7d654d5823953fd373f602

ilyalukyanov avatar Oct 26 '18 07:10 ilyalukyanov

The thing is that I am under the impression that the root of the exception in the description is a problem in the Azure Storage SDK, a problem for which I created a patch which probably never will be merged.... I am currently running with a custom build of the SDK with the patch applied, and I havn't seen any issues with it.

@ilyalukyanov I have tried a similar approach as you suggest, but it it didn't resolve the issue for me.

I think we are left in a vacuum and will have to wait until the new CosmosDB SDK with Table support is released before we can proceed with this.

rmja avatar Oct 26 '18 08:10 rmja

close this since it is not an orleans issue.

xiazen avatar Mar 18 '19 21:03 xiazen