private void InsertVirtualMachineAvailabilityZoneRegionResults(string region, int availbilityZone)
        {
            var virtualMachineQuery = TableQuery.CombineFilters(TableQuery.GenerateFilterConditionForInt(
                                                                    "AvailabilityZone",
                                                                    QueryComparisons.Equal,
                                                                    availbilityZone),
                                                                TableOperators.And,
                                                                TableQuery.GenerateFilterCondition("RegionName",
                                                                                                   QueryComparisons.Equal,
                                                                                                   region));

            //TableQuery.GenerateFilterConditionForInt("AvailabilityZone", QueryComparisons.GreaterThanOrEqual, 0);
            var virtualMachinesTableQuery     = new TableQuery <VirtualMachineCrawlerResponse>().Where(virtualMachineQuery);
            var crawledVirtualMachinesResults = StorageAccountProvider.GetEntities(
                virtualMachinesTableQuery,
                StorageTableNames.VirtualMachineCrawlerTableName);

            var virtualMachinesResults = crawledVirtualMachinesResults.ToList();

            if (!virtualMachinesResults.Any())
            {
                return;
            }
            var batchTasks = new List <Task>();
            var table      = StorageAccountProvider.CreateOrGetTable(StorageTableNames.ScheduledRulesTableName);

            if (table == null)
            {
                return;
            }

            for (var i = 0; i < virtualMachinesResults.Count; i += TableConstants.TableServiceBatchMaximumOperations)
            {
                var batchItems = virtualMachinesResults.Skip(i)
                                 .Take(TableConstants.TableServiceBatchMaximumOperations).ToList();
                var scheduledRulesbatchOperation = VirtualMachineHelper
                                                   .CreateScheduleEntityForAvailabilityZone(
                    batchItems,
                    azureClient.AzureSettings.Chaos.SchedulerFrequency,
                    azureClient.AzureSettings.Chaos.AzureFaultInjectionActions);

                if (scheduledRulesbatchOperation.Count <= 0)
                {
                    return;
                }
                batchTasks.Add(table.ExecuteBatchAsync(scheduledRulesbatchOperation));
            }

            if (batchTasks.Count > 0)
            {
                Task.WhenAll(batchTasks);
            }
        }
Esempio n. 2
0
        private void InsertVirtualMachineAvailabilityZoneRegionResults(string region, int availbilityZone, TraceWriter log)
        {
            try
            {
                var virtualMachineQuery = TableQuery.CombineFilters(TableQuery.GenerateFilterConditionForInt(
                                                                        "AvailabilityZone",
                                                                        QueryComparisons.Equal,
                                                                        availbilityZone),
                                                                    TableOperators.And,
                                                                    TableQuery.GenerateFilterCondition("RegionName",
                                                                                                       QueryComparisons.Equal,
                                                                                                       region));

                //TableQuery.GenerateFilterConditionForInt("AvailabilityZone", QueryComparisons.GreaterThanOrEqual, 0);
                var virtualMachinesTableQuery     = new TableQuery <VirtualMachineCrawlerResponse>().Where(virtualMachineQuery);
                var crawledVirtualMachinesResults = StorageAccountProvider.GetEntities(
                    virtualMachinesTableQuery,
                    StorageTableNames.VirtualMachineCrawlerTableName);
                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                crawledVirtualMachinesResults = crawledVirtualMachinesResults.Where(x => PowerState.Parse(x.State) == PowerState.Running);
                var virtualMachinesResults = crawledVirtualMachinesResults.ToList();
                var meanTimeQuery          = TableQuery.GenerateFilterConditionForDate("ScheduledExecutionTime",
                                                                                       QueryComparisons.GreaterThanOrEqual,
                                                                                       DateTimeOffset.UtcNow.AddMinutes(-azureClient.AzureSettings.Chaos.MeanTime));

                var recentlyExecutedAvailabilityZoneRegionCombinationQuery = TableQuery.GenerateFilterCondition(
                    "ResourceType",
                    QueryComparisons.Equal,
                    VirtualMachineGroup.VirtualMachineScaleSets.ToString());

                var recentlyExecutedVMScaleSetsQuery = TableQuery.CombineFilters(meanTimeQuery,
                                                                                 TableOperators.And,
                                                                                 recentlyExecutedAvailabilityZoneRegionCombinationQuery);

                var scheduledQuery = new TableQuery <ScheduledRules>().Where(recentlyExecutedVMScaleSetsQuery);
                //Will get the executed query results.
                var executedVMScaleSetsResults = StorageAccountProvider.GetEntities(scheduledQuery,
                                                                                    StorageTableNames.ScheduledRulesTableName);

                List <VirtualMachineCrawlerResponse> executedVMAvailabilityZones = null;

                if (virtualMachinesResults.Count() != 0 && executedVMScaleSetsResults.Count() != 0)
                {
                    foreach (var virtualMachineResult in virtualMachinesResults)
                    {
                        foreach (var executedVMScaleSetsResult in executedVMScaleSetsResults)
                        {
                            if ((executedVMScaleSetsResult.RowKey.ToLowerInvariant().Contains(virtualMachineResult.ResourceName.ToLowerInvariant())) &&
                                (executedVMScaleSetsResult.RowKey.ToLowerInvariant().Contains(virtualMachineResult.ResourceGroupName.ToLowerInvariant())) &&
                                (executedVMScaleSetsResult.RowKey.ToLowerInvariant().Contains(virtualMachineResult.AvailabilityZone.ToString()))
                                //&& (executedVMScaleSetsResult.RowKey.ToLowerInvariant().Contains(executedVMScaleSetsResult..ToLowerInvariant()))
                                )
                            {
                                executedVMAvailabilityZones.Add(virtualMachineResult);
                            }
                        }
                    }
                    if (executedVMAvailabilityZones != null && executedVMAvailabilityZones.Count() != 0)
                    {
                        virtualMachinesResults = virtualMachinesResults.Except(executedVMAvailabilityZones).ToList();
                    }
                }
                if (!virtualMachinesResults.Any())
                {
                    return;
                }
                var batchTasks = new List <Task>();
                var table      = StorageAccountProvider.CreateOrGetTable(StorageTableNames.ScheduledRulesTableName);
                if (table == null)
                {
                    return;
                }

                for (var i = 0; i < virtualMachinesResults.Count; i += TableConstants.TableServiceBatchMaximumOperations)
                {
                    var batchItems = virtualMachinesResults.Skip(i)
                                     .Take(TableConstants.TableServiceBatchMaximumOperations).ToList();
                    var scheduledRulesbatchOperation = VirtualMachineHelper
                                                       .CreateScheduleEntityForAvailabilityZone(
                        batchItems,
                        azureClient.AzureSettings.Chaos.SchedulerFrequency,
                        azureClient.AzureSettings.Chaos.AzureFaultInjectionActions);

                    if (scheduledRulesbatchOperation.Count <= 0)
                    {
                        return;
                    }
                    batchTasks.Add(table.ExecuteBatchAsync(scheduledRulesbatchOperation));
                }

                if (batchTasks.Count > 0)
                {
                    Task.WhenAll(batchTasks);
                }
            }
            catch (Exception ex)
            {
                log.Error("AvailabilityZone RuleEngine: thrown exception", ex);
                log.Error(ex.Source);
                log.Error(ex.Message);
            }
        }