示例#1
0
        public static ModificationResults ApplyModifications(PreparedModifications prepared_modifications, bool continue_on_error = true)
        {
            ModificationResults modification_results = new ModificationResults();

            if (prepared_modifications.modified_throughputs.Any())
            {
                var table_name_groups = prepared_modifications.modified_throughputs
                                        .Where(modified_throughput => modified_throughput.new_provisioned_throughput != null)
                                        .GroupBy(modified_throughput => modified_throughput.table_name)
                                        .ToList();

                foreach (var table_name_group in table_name_groups)
                {
                    try
                    {
                        UpdateTableRequest update_table_request = new UpdateTableRequest {
                            TableName = table_name_group.Key
                        };
                        update_table_request.ProvisionedThroughput       = null;
                        update_table_request.GlobalSecondaryIndexUpdates = new List <GlobalSecondaryIndexUpdate>();

                        ModifiedThroughput table_modified_throughput = table_name_group.SingleOrDefault(modified_throughput => string.IsNullOrEmpty(modified_throughput.index_name));
                        if (table_modified_throughput != null)
                        {
                            update_table_request.ProvisionedThroughput = table_modified_throughput.new_provisioned_throughput;
                        }

                        List <ModifiedThroughput> index_modified_throughputs = table_name_group.Where(modified_throughput => !string.IsNullOrEmpty(modified_throughput.index_name)).ToList();
                        foreach (var index_modified_throughput in index_modified_throughputs)
                        {
                            GlobalSecondaryIndexUpdate global_secondary_index_update = new GlobalSecondaryIndexUpdate
                            {
                                Update = new UpdateGlobalSecondaryIndexAction
                                {
                                    IndexName             = index_modified_throughput.index_name,
                                    ProvisionedThroughput = index_modified_throughput.new_provisioned_throughput
                                }
                            };
                            update_table_request.GlobalSecondaryIndexUpdates.Add(global_secondary_index_update);
                        }

                        AmazonDynamoDBClient amazon_dynamo_db_client = AWS.GetAmazonDynamoDBClient();
                        amazon_dynamo_db_client.UpdateTable(update_table_request);
                        modification_results.SuccessfulUpdates.Add(table_name_group.Key);
                    }
                    catch (Exception exception)
                    {
                        modification_results.Errors.Add(new Tuple <string, Exception>(table_name_group.Key, exception));
                        if (!continue_on_error)
                        {
                            break;
                        }
                    }
                }
            }

            return(modification_results);
        }
示例#2
0
        protected long GetConsumedCapacityUnits(string table_name, string index_name, string metric_name, int look_back_minutes)
        {
            List <Dimension> dimensions = new List <Dimension> {
                new Dimension {
                    Name = "TableName", Value = table_name
                }
            };

            if (!string.IsNullOrWhiteSpace(index_name))
            {
                dimensions.Add(new Dimension {
                    Name = "GlobalSecondaryIndexName", Value = index_name
                });
            }

            DateTime end_time   = DateTime.UtcNow.AddMinutes(-5);
            DateTime start_time = end_time.AddMinutes(-look_back_minutes);

            AmazonCloudWatchClient amazon_cloud_watch_client = AWS.GetAmazonCloudWatchClient();

            GetMetricStatisticsRequest get_metric_statistics_request = new GetMetricStatisticsRequest
            {
                Namespace  = "AWS/DynamoDB",
                Dimensions = dimensions,
                MetricName = metric_name,
                StartTime  = start_time,
                EndTime    = end_time,
                Period     = 300,
                Statistics = new List <string> {
                    "Sum"
                }
            };

            GetMetricStatisticsResponse get_metric_statistics_response = amazon_cloud_watch_client.GetMetricStatistics(get_metric_statistics_request);

            long consumed_capacity_units = 0;

            if (get_metric_statistics_response != null && get_metric_statistics_response.Datapoints.Any())
            {
                double total_seconds = (end_time - start_time).TotalSeconds;
                double total_sum     = get_metric_statistics_response.Datapoints.Sum(datapoint => datapoint.Sum);
                consumed_capacity_units = (long)Math.Ceiling(total_sum / total_seconds);
            }

            return(consumed_capacity_units);
        }
示例#3
0
        public static PreparedModifications BuildModificatons(string rules_config_file_path = null, bool include_non_changes = false)
        {
            try
            {
                PreparedModifications prepared_modifications = new PreparedModifications();
                Rules rules = LoadRules(rules_config_file_path);
                List <ThroughputModification> throughput_modifications = rules.GenerateThroughputModifications();

                if (throughput_modifications.Any())
                {
                    AmazonDynamoDBClient amazon_dynamo_db_client = AWS.GetAmazonDynamoDBClient();
                    ListTablesResponse   list_table_response     = amazon_dynamo_db_client.ListTables();
                    List <string>        table_names             = list_table_response.TableNames;
                    table_names.ForEach(table_name =>
                    {
                        // find all throughput modifications that have applicable rules (both table/index level) for this table name
                        List <ThroughputModification> qualified_throughput_modifications = throughput_modifications.Where(throughput_modification => throughput_modification.Qualify(table_name)).ToList();
                        if (qualified_throughput_modifications.Any())
                        {
                            DescribeTableResponse describe_table_response = amazon_dynamo_db_client.DescribeTable(table_name);
                            TableDescription table = describe_table_response.Table;

                            // find all throughput modifications that have applicable rules at the table level only
                            List <ThroughputModification> qualified_table_throughput_modifications = qualified_throughput_modifications
                                                                                                     .Where(throughput_modification => throughput_modification.QualifyTable(table_name))
                                                                                                     .OrderByDescending(throughput_modification => throughput_modification.GetTableMatchScore(table_name))
                                                                                                     .ThenByDescending(throughput_modification => throughput_modification.index_id)
                                                                                                     .ToList();

                            if (qualified_table_throughput_modifications.Any())
                            {
                                ModifiedThroughput modified_throughput = BuildModifiedThroughput(table_name, null, table.ProvisionedThroughput, qualified_table_throughput_modifications);
                                if (include_non_changes || modified_throughput.new_provisioned_throughput != null)
                                {
                                    prepared_modifications.modified_throughputs.Add(modified_throughput);
                                }
                            }

                            List <GlobalSecondaryIndexDescription> global_secondary_indexes = table.GlobalSecondaryIndexes;
                            global_secondary_indexes.ForEach(global_secondary_index =>
                            {
                                // find all throughput modifications that have applicable rules at the index level only
                                string index_name = global_secondary_index.IndexName;
                                List <ThroughputModification> qualified_index_throughput_modifications = qualified_throughput_modifications
                                                                                                         .Where(throughput_modification => throughput_modification.QualifyIndex(table_name, index_name))
                                                                                                         .OrderByDescending(throughput_modfication => throughput_modfication.GetIndexMatchScore(table_name, index_name))
                                                                                                         .ThenByDescending(throughput_modification => throughput_modification.index_id)
                                                                                                         .ToList();

                                if (qualified_index_throughput_modifications.Any())
                                {
                                    ModifiedThroughput modified_throughput = BuildModifiedThroughput(table_name, index_name, global_secondary_index.ProvisionedThroughput, qualified_index_throughput_modifications);
                                    if (include_non_changes || modified_throughput.new_provisioned_throughput != null)
                                    {
                                        prepared_modifications.modified_throughputs.Add(modified_throughput);
                                    }
                                }
                            });
                        }
                    });
                }

                return(prepared_modifications);
            }
            catch (Exception exception)
            {
                // provide custom exception handling here
                throw;
            }
        }