Ejemplo n.º 1
0
        /// <summary> Read state data function for this storage provider. </summary>
        /// <see cref="IGrainStorage.ReadStateAsync{T}"/>
        public async Task ReadStateAsync <T>(string grainType, GrainReference grainReference, IGrainState <T> grainState)
        {
            if (tableDataManager == null)
            {
                throw new ArgumentException("GrainState-Table property not initialized");
            }

            string pk = GetKeyString(grainReference);

            if (logger.IsEnabled(LogLevel.Trace))
            {
                logger.LogTrace((int)AzureProviderErrorCode.AzureTableProvider_ReadingData,
                                "Reading: GrainType={GrainType} Pk={PartitionKey} Grainid={GrainId} from Table={TableName}",
                                grainType,
                                pk,
                                grainReference,
                                this.options.TableName);
            }
            string partitionKey = pk;
            string rowKey       = AzureTableUtils.SanitizeTableProperty(grainType);
            var    entity       = await tableDataManager.Read(partitionKey, rowKey).ConfigureAwait(false);

            if (entity is not null)
            {
                var loadedState = ConvertFromStorageFormat <T>(entity);
                grainState.RecordExists = loadedState != null;
                grainState.State        = loadedState ?? Activator.CreateInstance <T>();
                grainState.ETag         = entity.ETag.ToString();
            }
            // Else leave grainState in previous default condition
        }
Ejemplo n.º 2
0
        /// <summary> Write state data function for this storage provider. </summary>
        /// <see cref="IGrainStorage.WriteStateAsync"/>
        public async Task WriteStateAsync <T>(string grainType, GrainReference grainReference, IGrainState <T> grainState)
        {
            if (tableDataManager == null)
            {
                throw new ArgumentException("GrainState-Table property not initialized");
            }

            string pk = GetKeyString(grainReference);

            if (logger.IsEnabled(LogLevel.Trace))
            {
                logger.Trace((int)AzureProviderErrorCode.AzureTableProvider_WritingData, "Writing: GrainType={0} Pk={1} Grainid={2} ETag={3} to Table={4}", grainType, pk, grainReference, grainState.ETag, this.options.TableName);
            }

            var rowKey = AzureTableUtils.SanitizeTableProperty(grainType);
            var entity = new TableEntity(pk, rowKey)
            {
                ETag = new ETag(grainState.ETag)
            };

            ConvertToStorageFormat(grainState.State, entity);
            try
            {
                await DoOptimisticUpdate(() => tableDataManager.Write(entity), grainType, grainReference.GrainId, this.options.TableName, grainState.ETag).ConfigureAwait(false);

                grainState.ETag         = entity.ETag.ToString();
                grainState.RecordExists = true;
            }
            catch (Exception exc)
            {
                logger.Error((int)AzureProviderErrorCode.AzureTableProvider_WriteError,
                             $"Error Writing: GrainType={grainType} GrainId={grainReference.GrainId} ETag={grainState.ETag} to Table={this.options.TableName} Exception={exc.Message}", exc);
                throw;
            }
        }
Ejemplo n.º 3
0
        /// <summary> Read state data function for this storage provider. </summary>
        /// <see cref="IGrainStorage.ReadStateAsync"/>
        public async Task ReadStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            if (tableDataManager == null)
            {
                throw new ArgumentException("GrainState-Table property not initialized");
            }

            string pk = GetKeyString(grainReference);

            if (logger.IsEnabled(LogLevel.Trace))
            {
                logger.Trace((int)AzureProviderErrorCode.AzureTableProvider_ReadingData, "Reading: GrainType={0} Pk={1} Grainid={2} from Table={3}", grainType, pk, grainReference, this.options.TableName);
            }
            string           partitionKey = pk;
            string           rowKey       = AzureTableUtils.SanitizeTableProperty(grainType);
            GrainStateRecord record       = await tableDataManager.Read(partitionKey, rowKey).ConfigureAwait(false);

            if (record != null)
            {
                var entity = record.Entity;
                if (entity != null)
                {
                    var loadedState = ConvertFromStorageFormat(entity, grainState.Type);
                    grainState.State = loadedState ?? Activator.CreateInstance(grainState.Type);
                    grainState.ETag  = record.ETag;
                }
            }

            // Else leave grainState in previous default condition
        }
Ejemplo n.º 4
0
        /// <summary> Clear / Delete state data function for this storage provider. </summary>
        /// <remarks>
        /// If the <c>DeleteStateOnClear</c> is set to <c>true</c> then the table row
        /// for this grain will be deleted / removed, otherwise the table row will be
        /// cleared by overwriting with default / null values.
        /// </remarks>
        /// <see cref="IGrainStorage.ClearStateAsync{T}"/>
        public async Task ClearStateAsync <T>(string grainType, GrainReference grainReference, IGrainState <T> grainState)
        {
            if (tableDataManager == null)
            {
                throw new ArgumentException("GrainState-Table property not initialized");
            }

            string pk = GetKeyString(grainReference);

            if (logger.IsEnabled(LogLevel.Trace))
            {
                logger.LogTrace((int)AzureProviderErrorCode.AzureTableProvider_WritingData,
                                "Clearing: GrainType={GrainType} Pk={PartitionKey} Grainid={GrainId} ETag={ETag} DeleteStateOnClear={DeleteStateOnClear} from Table={TableName}",
                                grainType,
                                pk,
                                grainReference,
                                grainState.ETag,
                                this.options.DeleteStateOnClear,
                                this.options.TableName);
            }
            var rowKey = AzureTableUtils.SanitizeTableProperty(grainType);
            var entity = new TableEntity(pk, rowKey)
            {
                ETag = new ETag(grainState.ETag)
            };
            string operation = "Clearing";

            try
            {
                if (this.options.DeleteStateOnClear)
                {
                    operation = "Deleting";
                    await DoOptimisticUpdate(() => tableDataManager.Delete(entity), grainType, grainReference.GrainId, this.options.TableName, grainState.ETag).ConfigureAwait(false);
                }
                else
                {
                    await DoOptimisticUpdate(() => tableDataManager.Write(entity), grainType, grainReference.GrainId, this.options.TableName, grainState.ETag).ConfigureAwait(false);
                }

                grainState.ETag         = entity.ETag.ToString(); // Update in-memory data to the new ETag
                grainState.RecordExists = false;
            }
            catch (Exception exc)
            {
                logger.LogError((int)AzureProviderErrorCode.AzureTableProvider_DeleteError,
                                exc,
                                "Error {Operation}: GrainType={GrainType} Grainid={GrainId} ETag={ETag} from Table={TableName}",
                                operation,
                                grainType,
                                grainReference,
                                grainState.ETag,
                                this.options.TableName);
                throw;
            }
        }
Ejemplo n.º 5
0
        public static string ConstructPartitionKey(string serviceId, uint number)
        {
            // IMPORTANT NOTE: Other code using this return data is very sensitive to format changes,
            //       so take great care when making any changes here!!!

            // this format of partition key makes sure that the comparisons in FindReminderEntries(begin, end) work correctly
            // the idea is that when converting to string, negative numbers start with 0, and positive start with 1. Now,
            // when comparisons will be done on strings, this will ensure that positive numbers are always greater than negative
            // string grainHash = number < 0 ? string.Format("0{0}", number.ToString("X")) : string.Format("1{0:d16}", number);

            return(AzureTableUtils.SanitizeTableProperty($"{serviceId}_{number:X8}"));
        }
Ejemplo n.º 6
0
        /// <summary> Clear / Delete state data function for this storage provider. </summary>
        /// <remarks>
        /// If the <c>DeleteStateOnClear</c> is set to <c>true</c> then the table row
        /// for this grain will be deleted / removed, otherwise the table row will be
        /// cleared by overwriting with default / null values.
        /// </remarks>
        /// <see cref="IGrainStorage.ClearStateAsync"/>
        public async Task ClearStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            if (tableDataManager == null)
            {
                throw new ArgumentException("GrainState-Table property not initialized");
            }

            string pk = GetKeyString(grainReference);

            if (logger.IsEnabled(LogLevel.Trace))
            {
                logger.Trace((int)AzureProviderErrorCode.AzureTableProvider_WritingData, "Clearing: GrainType={0} Pk={1} Grainid={2} ETag={3} DeleteStateOnClear={4} from Table={5}", grainType, pk, grainReference, grainState.ETag, this.options.DeleteStateOnClear, this.options.TableName);
            }
            var rowKey = AzureTableUtils.SanitizeTableProperty(grainType);
            var entity = new DynamicTableEntity(pk, rowKey);
            var record = new GrainStateRecord {
                Entity = entity, ETag = grainState.ETag
            };
            string operation = "Clearing";

            try
            {
                if (this.options.DeleteStateOnClear)
                {
                    operation = "Deleting";
                    await DoOptimisticUpdate(() => tableDataManager.Delete(record), grainType, grainReference.GrainId, this.options.TableName, grainState.ETag).ConfigureAwait(false);
                }
                else
                {
                    await DoOptimisticUpdate(() => tableDataManager.Write(record), grainType, grainReference.GrainId, this.options.TableName, grainState.ETag).ConfigureAwait(false);
                }

                grainState.ETag         = record.ETag; // Update in-memory data to the new ETag
                grainState.RecordExists = false;
            }
            catch (Exception exc)
            {
                logger.Error((int)AzureProviderErrorCode.AzureTableProvider_DeleteError, string.Format("Error {0}: GrainType={1} Grainid={2} ETag={3} from Table={4} Exception={5}",
                                                                                                       operation, grainType, grainReference, grainState.ETag, this.options.TableName, exc.Message), exc);
                throw;
            }
        }
        public static string MakeRowKey(string partition)
        {
            string key = $"partition_{partition}";

            return(AzureTableUtils.SanitizeTableProperty(key));
        }
        public static string MakePartitionKey(string streamProviderName, string checkpointNamespace)
        {
            string key = $"EventHubCheckpoints_{streamProviderName}_{checkpointNamespace}";

            return(AzureTableUtils.SanitizeTableProperty(key));
        }
Ejemplo n.º 9
0
        public static (string LowerBound, string UpperBound) ConstructPartitionKeyBounds(string serviceId)
        {
            var baseKey = AzureTableUtils.SanitizeTableProperty(serviceId);

            return(baseKey + '_', baseKey + (char)('_' + 1));
        }
Ejemplo n.º 10
0
        public static (string LowerBound, string UpperBound) ConstructRowKeyBounds(GrainReference grainRef)
        {
            var baseKey = AzureTableUtils.SanitizeTableProperty(grainRef.ToKeyString());

            return(baseKey + '-', baseKey + (char)('-' + 1));
        }
Ejemplo n.º 11
0
        }                                                     // Part of PartitionKey


        public static string ConstructRowKey(GrainReference grainRef, string reminderName)
        {
            var key = string.Format("{0}-{1}", grainRef.ToKeyString(), reminderName);

            return(AzureTableUtils.SanitizeTableProperty(key));
        }
        public void AzureStorageUtils_TablePropertyShouldBeSanitized()
        {
            var tableProperty = "/A\\C#?";

            Assert.Equal("_A_C__", AzureTableUtils.SanitizeTableProperty(tableProperty));
        }
        private string GetKeyString(GrainReference grainReference)
        {
            var key = String.Format("{0}_{1}", this.clusterOptions.ServiceId, grainReference.ToKeyString());

            return(AzureTableUtils.SanitizeTableProperty(key));
        }