Ejemplo n.º 1
0
 private void CreateClient()
 {
     if (this.service.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
         this.service.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
     {
         // Local DynamoDB instance (for testing)
         var credentials = new BasicAWSCredentials("dummy", "dummyKey");
         this.ddbClient = new AmazonDynamoDBClient(credentials, new AmazonDynamoDBConfig {
             ServiceURL = this.service
         });
     }
     else if (!string.IsNullOrEmpty(this.accessKey) && !string.IsNullOrEmpty(this.secretKey))
     {
         // AWS DynamoDB instance (auth via explicit credentials)
         var credentials = new BasicAWSCredentials(this.accessKey, this.secretKey);
         this.ddbClient = new AmazonDynamoDBClient(credentials, new AmazonDynamoDBConfig {
             ServiceURL = service, RegionEndpoint = AWSUtils.GetRegionEndpoint(this.service)
         });
     }
     else
     {
         // AWS DynamoDB instance (implicit auth - EC2 IAM Roles etc)
         this.ddbClient = new AmazonDynamoDBClient(new AmazonDynamoDBConfig {
             ServiceURL = service, RegionEndpoint = AWSUtils.GetRegionEndpoint(this.service)
         });
     }
 }
Ejemplo n.º 2
0
 private void CreateClient()
 {
     if (service.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
         service.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
     {
         // Local SQS instance (for testing)
         var credentials = new BasicAWSCredentials("dummy", "dummyKey");
         sqsClient = new AmazonSQSClient(credentials, new AmazonSQSConfig {
             ServiceURL = service
         });
     }
     else if (!string.IsNullOrEmpty(accessKey) && !string.IsNullOrEmpty(secretKey))
     {
         // AWS SQS instance (auth via explicit credentials)
         var credentials = new BasicAWSCredentials(accessKey, secretKey);
         sqsClient = new AmazonSQSClient(credentials, new AmazonSQSConfig {
             RegionEndpoint = AWSUtils.GetRegionEndpoint(service)
         });
     }
     else
     {
         // AWS SQS instance (implicit auth - EC2 IAM Roles etc)
         sqsClient = new AmazonSQSClient(new AmazonSQSConfig {
             RegionEndpoint = AWSUtils.GetRegionEndpoint(service)
         });
     }
 }
        /// <summary> Write state data function for this storage provider. </summary>
        /// <see cref="IGrainStorage.WriteStateAsync"/>
        public async Task WriteStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            if (this.storage == null)
            {
                throw new ArgumentException("GrainState-Table property not initialized");
            }

            string partitionKey = GetKeyString(grainReference);
            string rowKey       = AWSUtils.ValidateDynamoDBRowKey(grainType);

            var record = new GrainStateRecord {
                GrainReference = partitionKey, GrainType = rowKey
            };

            try
            {
                ConvertToStorageFormat(grainState.State, record);
                await WriteStateInternal(grainState, record);
            }
            catch (ConditionalCheckFailedException exc)
            {
                throw new InconsistentStateException("Invalid grain state", exc);
            }
            catch (Exception exc)
            {
                this.logger.Error(ErrorCode.StorageProviderBase,
                                  string.Format("Error Writing: GrainType={0} Grainid={1} ETag={2} to Table={3} Exception={4}",
                                                grainType, grainReference, grainState.ETag, this.options.TableName, exc.Message), exc);
                throw;
            }
        }
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 (this.storage == null)
            {
                throw new ArgumentException("GrainState-Table property not initialized");
            }

            string partitionKey = GetKeyString(grainReference);

            if (this.logger.IsEnabled(LogLevel.Trace))
            {
                this.logger.LogTrace(
                    (int)ErrorCode.StorageProviderBase,
                    "Clearing: GrainType={GrainType} Pk={PartitionKey} GrainId={GrainId} ETag={ETag} DeleteStateOnClear={DeleteStateOnClear} from Table={TableName}",
                    grainType,
                    partitionKey,
                    grainReference?.GrainId,
                    grainState.ETag,
                    this.options.DeleteStateOnClear,
                    this.options.TableName);
            }
            string rowKey = AWSUtils.ValidateDynamoDBRowKey(grainType);
            var    record = new GrainStateRecord {
                GrainReference = partitionKey, ETag = string.IsNullOrWhiteSpace(grainState.ETag) ? 0 : int.Parse(grainState.ETag), GrainType = rowKey
            };

            var operation = "Clearing";

            try
            {
                if (this.options.DeleteStateOnClear)
                {
                    operation = "Deleting";
                    var keys = new Dictionary <string, AttributeValue>();
                    keys.Add(GRAIN_REFERENCE_PROPERTY_NAME, new AttributeValue(record.GrainReference));
                    keys.Add(GRAIN_TYPE_PROPERTY_NAME, new AttributeValue(record.GrainType));

                    await this.storage.DeleteEntryAsync(this.options.TableName, keys).ConfigureAwait(false);

                    grainState.ETag = null;
                }
                else
                {
                    await WriteStateInternal(grainState, record, true);
                }
            }
            catch (Exception exc)
            {
                this.logger.LogError(
                    (int)ErrorCode.StorageProviderBase,
                    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
        private void CreateClient()
        {
            var credentials = new BasicAWSCredentials(accessKey, secretKey);

            sqsClient = new AmazonSQSClient(credentials, new AmazonSQSConfig {
                RegionEndpoint = AWSUtils.GetRegionEndpoint(service)
            });
        }
Ejemplo n.º 6
0
 private void CreateClient()
 {
     if (this.service.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
         this.service.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
     {
         // Local DynamoDB instance (for testing)
         var credentials = new BasicAWSCredentials("dummy", "dummyKey");
         this.ddbClient = new AmazonDynamoDBClient(credentials, new AmazonDynamoDBConfig {
             ServiceURL = this.service
         });
     }
     else if (!string.IsNullOrEmpty(this.accessKey) && !string.IsNullOrEmpty(this.secretKey) && !string.IsNullOrEmpty(this.token))
     {
         // AWS DynamoDB instance (auth via explicit credentials and token)
         var credentials = new SessionAWSCredentials(this.accessKey, this.secretKey, this.token);
         this.ddbClient = new AmazonDynamoDBClient(credentials, new AmazonDynamoDBConfig {
             RegionEndpoint = AWSUtils.GetRegionEndpoint(this.service)
         });
     }
     else if (!string.IsNullOrEmpty(this.accessKey) && !string.IsNullOrEmpty(this.secretKey))
     {
         // AWS DynamoDB instance (auth via explicit credentials)
         var credentials = new BasicAWSCredentials(this.accessKey, this.secretKey);
         this.ddbClient = new AmazonDynamoDBClient(credentials, new AmazonDynamoDBConfig {
             RegionEndpoint = AWSUtils.GetRegionEndpoint(this.service)
         });
     }
     else if (!string.IsNullOrEmpty(this.profileName))
     {
         // AWS DynamoDB instance (auth via explicit credentials and token found in a named profile)
         var chain = new CredentialProfileStoreChain();
         if (chain.TryGetAWSCredentials(this.profileName, out var credentials))
         {
             this.ddbClient = new AmazonDynamoDBClient(
                 credentials,
                 new AmazonDynamoDBConfig
             {
                 RegionEndpoint = AWSUtils.GetRegionEndpoint(this.service)
             });
         }
         else
         {
             throw new InvalidOperationException(
                       $"AWS named profile '{this.profileName}' provided, but credentials could not be retrieved");
         }
     }
     else
     {
         // AWS DynamoDB instance (implicit auth - EC2 IAM Roles etc)
         this.ddbClient = new AmazonDynamoDBClient(new AmazonDynamoDBConfig {
             RegionEndpoint = AWSUtils.GetRegionEndpoint(this.service)
         });
     }
 }
Ejemplo n.º 7
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 (this.storage == null)
            {
                throw new ArgumentException("GrainState-Table property not initialized");
            }

            string partitionKey = GetKeyString(grainReference);

            if (this.logger.IsEnabled(LogLevel.Trace))
            {
                this.logger.LogTrace(
                    (int)ErrorCode.StorageProviderBase,
                    "Reading: GrainType={GrainType} Pk={PartitionKey} GrainId={GrainId} from Table={TableName}",
                    grainType,
                    partitionKey,
                    grainReference?.GrainId,
                    this.options.TableName);
            }

            string rowKey = AWSUtils.ValidateDynamoDBRowKey(grainType);

            var record = await this.storage.ReadSingleEntryAsync(this.options.TableName,
                                                                 new Dictionary <string, AttributeValue>
            {
                { GRAIN_REFERENCE_PROPERTY_NAME, new AttributeValue(partitionKey) },
                { GRAIN_TYPE_PROPERTY_NAME, new AttributeValue(rowKey) }
            },
                                                                 (fields) =>
            {
                return(new GrainStateRecord
                {
                    GrainType = fields[GRAIN_TYPE_PROPERTY_NAME].S,
                    GrainReference = fields[GRAIN_REFERENCE_PROPERTY_NAME].S,
                    ETag = int.Parse(fields[ETAG_PROPERTY_NAME].N),
                    BinaryState = fields.ContainsKey(BINARY_STATE_PROPERTY_NAME) ? fields[BINARY_STATE_PROPERTY_NAME].B?.ToArray() : null,
                    StringState = fields.ContainsKey(STRING_STATE_PROPERTY_NAME) ? fields[STRING_STATE_PROPERTY_NAME].S : null
                });
            }).ConfigureAwait(false);

            if (record != null)
            {
                var loadedState = ConvertFromStorageFormat <T>(record);
                grainState.RecordExists = loadedState != null;
                grainState.State        = loadedState ?? Activator.CreateInstance <T>();
                grainState.ETag         = record.ETag.ToString();
            }

            // Else leave grainState in previous default condition
        }
Ejemplo n.º 8
0
 private void CreateClient()
 {
     if (service.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase) ||
         service.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase))
     {
         ddbClient = new AmazonDynamoDBClient(new AmazonDynamoDBConfig {
             ServiceURL = service
         });
     }
     else
     {
         var credentials = new BasicAWSCredentials(accessKey, secretKey);
         ddbClient = new AmazonDynamoDBClient(credentials, new AmazonDynamoDBConfig {
             ServiceURL = service, RegionEndpoint = AWSUtils.GetRegionEndpoint(service)
         });
     }
 }
Ejemplo n.º 9
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="IStorageProvider.ClearStateAsync"/>
        public async Task ClearStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            if (storage == null)
            {
                throw new ArgumentException("GrainState-Table property not initialized");
            }

            string partitionKey = GetKeyString(grainReference);

            if (Log.IsVerbose3)
            {
                Log.Verbose3(ErrorCode.StorageProviderBase, "Clearing: GrainType={0} Pk={1} Grainid={2} ETag={3} DeleteStateOnClear={4} from Table={5}", grainType, partitionKey, grainReference, grainState.ETag, isDeleteStateOnClear, tableName);
            }
            string rowKey = AWSUtils.ValidateDynamoDBRowKey(grainType);
            var    record = new GrainStateRecord {
                GrainReference = partitionKey, ETag = string.IsNullOrWhiteSpace(grainState.ETag) ? 0 : int.Parse(grainState.ETag), GrainType = rowKey
            };

            var operation = "Clearing";

            try
            {
                if (isDeleteStateOnClear)
                {
                    operation = "Deleting";
                    var keys = new Dictionary <string, AttributeValue>();
                    keys.Add(GRAIN_REFERENCE_PROPERTY_NAME, new AttributeValue(record.GrainReference));
                    keys.Add(GRAIN_TYPE_PROPERTY_NAME, new AttributeValue(record.GrainType));

                    await storage.DeleteEntryAsync(tableName, keys).ConfigureAwait(false);

                    grainState.ETag = string.Empty;
                }
                else
                {
                    await WriteStateInternal(grainState, record, true);
                }
            }
            catch (Exception exc)
            {
                Log.Error(ErrorCode.StorageProviderBase, string.Format("Error {0}: GrainType={1} Grainid={2} ETag={3} from Table={4} Exception={5}",
                                                                       operation, grainType, grainReference, grainState.ETag, tableName, exc.Message), exc);
                throw;
            }
        }
Ejemplo n.º 10
0
        /// <summary> Read state data function for this storage provider. </summary>
        /// <see cref="IStorageProvider.ReadStateAsync"/>
        public async Task ReadStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            if (storage == null)
            {
                throw new ArgumentException("GrainState-Table property not initialized");
            }

            string partitionKey = GetKeyString(grainReference);

            if (Log.IsVerbose3)
            {
                Log.Verbose3(ErrorCode.StorageProviderBase, "Reading: GrainType={0} Pk={1} Grainid={2} from Table={3}", grainType, partitionKey, grainReference, tableName);
            }
            string rowKey = AWSUtils.ValidateDynamoDBRowKey(grainType);

            var record = await storage.ReadSingleEntryAsync(tableName,
                                                            new Dictionary <string, AttributeValue>
            {
                { GRAIN_REFERENCE_PROPERTY_NAME, new AttributeValue(partitionKey) },
                { GRAIN_TYPE_PROPERTY_NAME, new AttributeValue(rowKey) }
            },
                                                            (fields) =>
            {
                return(new GrainStateRecord
                {
                    GrainType = fields[GRAIN_TYPE_PROPERTY_NAME].S,
                    GrainReference = fields[GRAIN_REFERENCE_PROPERTY_NAME].S,
                    ETag = int.Parse(fields[ETAG_PROPERTY_NAME].N),
                    BinaryState = fields.ContainsKey(BINARY_STATE_PROPERTY_NAME) ? fields[BINARY_STATE_PROPERTY_NAME].B.ToArray() : null,
                    StringState = fields.ContainsKey(STRING_STATE_PROPERTY_NAME) ? fields[STRING_STATE_PROPERTY_NAME].S : string.Empty
                });
            }).ConfigureAwait(false);

            if (record != null)
            {
                var loadedState = ConvertFromStorageFormat(record);
                grainState.State = loadedState ?? Activator.CreateInstance(grainState.State.GetType());
                grainState.ETag  = record.ETag.ToString();
            }

            // Else leave grainState in previous default condition
        }
Ejemplo n.º 11
0
        /// <summary> Write state data function for this storage provider. </summary>
        /// <see cref="IGrainStorage.WriteStateAsync{T}"/>
        public async Task WriteStateAsync <T>(string grainType, GrainReference grainReference, IGrainState <T> grainState)
        {
            if (this.storage == null)
            {
                throw new ArgumentException("GrainState-Table property not initialized");
            }

            string partitionKey = GetKeyString(grainReference);
            string rowKey       = AWSUtils.ValidateDynamoDBRowKey(grainType);

            var record = new GrainStateRecord {
                GrainReference = partitionKey, GrainType = rowKey
            };

            try
            {
                ConvertToStorageFormat(grainState.State, record);
                await WriteStateInternal(grainState, record);
            }
            catch (ConditionalCheckFailedException exc)
            {
                throw new InconsistentStateException($"Inconsistent grain state: {exc}");
            }
            catch (Exception exc)
            {
                this.logger.LogError(
                    (int)ErrorCode.StorageProviderBase,
                    exc,
                    "Error Writing: GrainType={GrainType} Grainid={GrainId} ETag={ETag} to Table={TableName}",
                    grainType,
                    grainReference,
                    grainState.ETag,
                    this.options.TableName);
                throw;
            }
        }
        private string GetKeyString(GrainReference grainReference)
        {
            var key = string.Format("{0}_{1}", this.options.ServiceId, grainReference.ToKeyString());

            return(AWSUtils.ValidateDynamoDBPartitionKey(key));
        }