Refresh cache changes and switch to secondary account for read path (#560)

* Switch to secondary after retry, refresh detectors published in last 5 mins

* additional logs

* Force refresh its own cache after publishing

* Set ClientRequestId and MaxExecutiontime

* commenting unit for now
This commit is contained in:
rekhaswaminathan 2020-08-19 17:27:47 -07:00 коммит произвёл GitHub
Родитель 3207e5401d
Коммит 483a14a42f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 410 добавлений и 315 удалений

Просмотреть файл

@ -13,3 +13,19 @@ Write-Host "Creating new table" $tableName
$tableCreation = New-AzStorageTable -Name $tableName -Context $ctx
Write-Host $tableCreation
$detectorConfigTable = "detectorconfig"
Write-Host "Create new table" $detectorConfigTable
$tableCreation = New-AzStorageTable -Name $detectorConfigTable -Context $ctx
Write-Host $tableCreation
$containerName = "detectors"
Write-Host "Creating new container" $containerName
$containerCreation = New-AzStorageContainer -Name $containerName -Context $ctx
Write-Host $containerCreation

Просмотреть файл

@ -97,12 +97,13 @@ namespace Diagnostics.RuntimeHost.Services.SourceWatcher.Watchers
{
await gitHubClient.CreateOrUpdateFiles(pkg.GetCommitContents(), pkg.GetCommitMessage());
// Insert Kusto Cluster Mapping to Configuration table.
if(pkg.GetCommitContents().Any(content => content.FilePath.Contains("kustoClusterMappings", StringComparison.CurrentCultureIgnoreCase)))
if (pkg.GetCommitContents().Any(content => content.FilePath.Contains("kustoClusterMappings", StringComparison.CurrentCultureIgnoreCase)))
{
var kustoMappingPackage = pkg.GetCommitContents().Where(c => c.FilePath.Contains("kustoClusterMappings", StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
var githubCommit = await gitHubClient.GetCommitByPath(kustoMappingPackage.FilePath);
// store kusto cluster data
var diagconfiguration = new DetectorRuntimeConfiguration{
var diagconfiguration = new DetectorRuntimeConfiguration
{
PartitionKey = "KustoClusterMapping",
RowKey = pkg.Id.ToLower(),
GithubSha = githubCommit != null ? githubCommit.Commit.Tree.Sha : string.Empty,
@ -131,6 +132,10 @@ namespace Diagnostics.RuntimeHost.Services.SourceWatcher.Watchers
diagEntity = DiagEntityHelper.PrepareEntityForLoad(assemblyBytes, pkg.CodeString, diagEntity);
}
await storageService.LoadDataToTable(diagEntity);
// Force refresh its own cache
var assemblyData = Convert.FromBase64String(pkg.DllBytes);
await UpdateInvokerCache(assemblyData, diagEntity.PartitionKey, diagEntity.RowKey);
} catch (Exception ex)
{
DiagnosticsETWProvider.Instance.LogAzureStorageException(nameof(StorageWatcher), ex.Message, ex.GetType().ToString(), ex.ToString());
@ -179,8 +184,10 @@ namespace Diagnostics.RuntimeHost.Services.SourceWatcher.Watchers
var filteredDetectors = LoadOnlyPublicDetectors ? detectorsList.Where(row => !row.IsInternal).ToList() : detectorsList;
if(!startup)
{
entitiesToLoad.AddRange(filteredDetectors.Where(s => s.Timestamp >= blobCacheLastModifiedTime).ToList());
entitiesToLoad.AddRange(gists.Where(s => s.Timestamp >= blobCacheLastModifiedTime).ToList());
// Refresh cache with detectors published in last 5 minutes.
var timeRange = DateTime.UtcNow.AddMinutes(-5);
entitiesToLoad.AddRange(filteredDetectors.Where(s => s.Timestamp >= timeRange).ToList());
entitiesToLoad.AddRange(gists.Where(s => s.Timestamp >= timeRange).ToList());
} else
{
entitiesToLoad.AddRange(filteredDetectors.ToList());
@ -202,37 +209,7 @@ namespace Diagnostics.RuntimeHost.Services.SourceWatcher.Watchers
DiagnosticsETWProvider.Instance.LogAzureStorageWarning(nameof(StorageWatcher), $" blob {entity.RowKey.ToLower()}.dll is either null or 0 bytes in length");
continue;
}
// initializing Entry Point of Invoker using assembly
Assembly temp = Assembly.Load(assemblyData);
EntityType entityType = EntityType.Signal;
if (entity.PartitionKey.Equals("Gist"))
{
entityType = EntityType.Gist;
}
else if (entity.PartitionKey.Equals("Detector"))
{
entityType = EntityType.Detector;
}
var script = string.Empty;
if(entity.PartitionKey.Equals("Gist"))
{
script = await gitHubClient.GetFileContent($"{entity.RowKey.ToLower()}/{entity.RowKey.ToLower()}.csx");
}
EntityMetadata metaData = new EntityMetadata(script, entityType);
var newInvoker = new EntityInvoker(metaData);
newInvoker.InitializeEntryPoint(temp);
if (_invokerDictionary.TryGetValue(entityType, out ICache<string, EntityInvoker> cache) && newInvoker.EntryPointDefinitionAttribute != null)
{
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageWatcher), $"Updating cache with new invoker with id : {newInvoker.EntryPointDefinitionAttribute.Id} {entity.PartitionKey}");
cache.AddOrUpdate(newInvoker.EntryPointDefinitionAttribute.Id, newInvoker);
}
else
{
DiagnosticsETWProvider.Instance.LogAzureStorageWarning(nameof(StorageWatcher), $"No invoker cache exist for {entityType}");
}
await UpdateInvokerCache(assemblyData, entity.PartitionKey, entity.RowKey);
}
}
catch (Exception ex)
@ -291,5 +268,39 @@ namespace Diagnostics.RuntimeHost.Services.SourceWatcher.Watchers
}
}
}
private async Task UpdateInvokerCache(byte[] assemblyData, string partitionkey, string rowkey)
{
// initializing Entry Point of Invoker using assembly
Assembly temp = Assembly.Load(assemblyData);
EntityType entityType = EntityType.Signal;
if (partitionkey.Equals("Gist"))
{
entityType = EntityType.Gist;
}
else if (partitionkey.Equals("Detector"))
{
entityType = EntityType.Detector;
}
var script = string.Empty;
if (partitionkey.Equals("Gist"))
{
script = await gitHubClient.GetFileContent($"{rowkey.ToLower()}/{rowkey.ToLower()}.csx");
}
EntityMetadata metaData = new EntityMetadata(script, entityType);
var newInvoker = new EntityInvoker(metaData);
newInvoker.InitializeEntryPoint(temp);
if (_invokerDictionary.TryGetValue(entityType, out ICache<string, EntityInvoker> cache) && newInvoker.EntryPointDefinitionAttribute != null)
{
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageWatcher), $"Updating cache with new invoker with id : {newInvoker.EntryPointDefinitionAttribute.Id} {partitionkey}");
cache.AddOrUpdate(newInvoker.EntryPointDefinitionAttribute.Id, newInvoker);
}
else
{
DiagnosticsETWProvider.Instance.LogAzureStorageWarning(nameof(StorageWatcher), $"No invoker cache exist for {entityType}");
}
}
}
}

Просмотреть файл

@ -15,6 +15,7 @@ using System.IO;
using System.Linq;
using Diagnostics.RuntimeHost.Services.SourceWatcher;
using Diagnostics.RuntimeHost.Models;
using Microsoft.WindowsAzure.Storage.RetryPolicies;
namespace Diagnostics.RuntimeHost.Services.StorageService
{
@ -81,37 +82,59 @@ namespace Diagnostics.RuntimeHost.Services.StorageService
public async Task<List<DiagEntity>> GetEntitiesByPartitionkey(string partitionKey = null)
{
try
int retryThreshold = 2;
int attempt = 0;
do
{
CloudTable table = tableClient.GetTableReference(tableName);
await table.CreateIfNotExistsAsync();
var timeTakenStopWatch = new Stopwatch();
partitionKey = partitionKey == null ? "Detector" : partitionKey;
var filterPartitionKey = TableQuery.GenerateFilterCondition(PartitionKey, QueryComparisons.Equal, partitionKey);
var tableQuery = new TableQuery<DiagEntity>();
tableQuery.Where(filterPartitionKey);
TableContinuationToken tableContinuationToken = null;
var detectorsResult = new List<DiagEntity>();
timeTakenStopWatch.Start();
do
var clientRequestId = Guid.NewGuid().ToString();
try
{
// Execute the operation.
var detectorList = await table.ExecuteQuerySegmentedAsync(tableQuery, tableContinuationToken);
tableContinuationToken = detectorList.ContinuationToken;
if (detectorList.Results != null)
CloudTable table = tableClient.GetTableReference(tableName);
var timeTakenStopWatch = new Stopwatch();
if(string.IsNullOrWhiteSpace(partitionKey))
{
detectorsResult.AddRange(detectorList.Results);
partitionKey = "Detector";
}
} while (tableContinuationToken != null);
timeTakenStopWatch.Stop();
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageService), $"GetEntities by Partition key {partitionKey} took {timeTakenStopWatch.ElapsedMilliseconds}, Total rows = {detectorsResult.Count}");
return detectorsResult.Where(result => !result.IsDisabled).ToList();
}
catch (Exception ex)
{
DiagnosticsETWProvider.Instance.LogAzureStorageException(nameof(StorageService), ex.Message, ex.GetType().ToString(), ex.ToString());
return null;
}
var filterPartitionKey = TableQuery.GenerateFilterCondition(PartitionKey, QueryComparisons.Equal, partitionKey);
var tableQuery = new TableQuery<DiagEntity>();
tableQuery.Where(filterPartitionKey);
TableContinuationToken tableContinuationToken = null;
var detectorsResult = new List<DiagEntity>();
timeTakenStopWatch.Start();
TableRequestOptions tableRequestOptions = new TableRequestOptions();
tableRequestOptions.LocationMode = LocationMode.PrimaryThenSecondary;
tableRequestOptions.MaximumExecutionTime = TimeSpan.FromSeconds(30);
OperationContext oc = new OperationContext();
oc.ClientRequestID = clientRequestId;
if (attempt == retryThreshold)
{
tableRequestOptions.LocationMode = LocationMode.SecondaryOnly;
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageService), $"Retrying table against secondary account after {attempt} attempts");
}
do
{
// Execute the operation.
var detectorList = await table.ExecuteQuerySegmentedAsync(tableQuery, tableContinuationToken, tableRequestOptions, null);
tableContinuationToken = detectorList.ContinuationToken;
if (detectorList.Results != null)
{
detectorsResult.AddRange(detectorList.Results);
}
} while (tableContinuationToken != null);
timeTakenStopWatch.Stop();
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageService), $"GetEntities by Partition key {partitionKey} took {timeTakenStopWatch.ElapsedMilliseconds}, Total rows = {detectorsResult.Count}, ClientRequestId = {clientRequestId} ");
return detectorsResult.Where(result => !result.IsDisabled).ToList();
}
catch (Exception ex)
{
DiagnosticsETWProvider.Instance.LogAzureStorageException(nameof(StorageService), $"ClientRequestId : {clientRequestId} {ex.Message}", ex.GetType().ToString(), ex.ToString());
return null;
}
finally
{
attempt++;
}
} while (attempt <= retryThreshold);
}
public bool GetStorageFlag()
@ -121,11 +144,11 @@ namespace Diagnostics.RuntimeHost.Services.StorageService
public async Task<DiagEntity> LoadDataToTable(DiagEntity detectorEntity)
{
var clientRequestId = Guid.NewGuid().ToString();
try
{
// Create a table client for interacting with the table service
CloudTable table = tableClient.GetTableReference(tableName);
await table.CreateIfNotExistsAsync();
if (detectorEntity == null || detectorEntity.PartitionKey == null || detectorEntity.RowKey == null)
{
throw new ArgumentNullException(nameof(detectorEntity));
@ -138,111 +161,152 @@ namespace Diagnostics.RuntimeHost.Services.StorageService
// Create the InsertOrReplace table operation
TableOperation insertOrReplaceOperation = TableOperation.InsertOrReplace(detectorEntity);
TableRequestOptions tableRequestOptions = new TableRequestOptions();
tableRequestOptions.LocationMode = LocationMode.PrimaryOnly;
tableRequestOptions.MaximumExecutionTime = TimeSpan.FromSeconds(60);
OperationContext oc = new OperationContext();
oc.ClientRequestID = clientRequestId;
// Execute the operation.
TableResult result = await table.ExecuteAsync(insertOrReplaceOperation);
TableResult result = await table.ExecuteAsync(insertOrReplaceOperation, tableRequestOptions, oc);
timeTakenStopWatch.Stop();
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageService), $"InsertOrReplace result : {result.HttpStatusCode}, time taken {timeTakenStopWatch.ElapsedMilliseconds}");
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageService), $"InsertOrReplace result : {result.HttpStatusCode}, time taken {timeTakenStopWatch.ElapsedMilliseconds}, ClientRequestId {clientRequestId}");
DiagEntity insertedEntity = result.Result as DiagEntity;
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageService), $"Inserted entity {insertedEntity.RowKey.ToString()} timestamp: {insertedEntity.Timestamp}");
return detectorEntity;
}
catch (Exception ex)
{
DiagnosticsETWProvider.Instance.LogAzureStorageException(nameof(StorageService), ex.Message, ex.GetType().ToString(), ex.ToString());
DiagnosticsETWProvider.Instance.LogAzureStorageException(nameof(StorageService), $"ClientRequestId {clientRequestId} {ex.Message}", ex.GetType().ToString(), ex.ToString());
return null;
}
}
public async Task<string> LoadBlobToContainer(string blobname, string contents)
{
var clientRequestId = Guid.NewGuid().ToString();
try
{
var timeTakenStopWatch = new Stopwatch();
await containerClient.CreateIfNotExistsAsync();
var timeTakenStopWatch = new Stopwatch();
timeTakenStopWatch.Start();
var cloudBlob = containerClient.GetBlockBlobReference(blobname);
BlobRequestOptions blobRequestOptions = new BlobRequestOptions();
blobRequestOptions.LocationMode = LocationMode.PrimaryOnly;
blobRequestOptions.MaximumExecutionTime = TimeSpan.FromSeconds(60);
OperationContext oc = new OperationContext();
oc.ClientRequestID = clientRequestId;
using (var uploadStream = new MemoryStream(Convert.FromBase64String(contents)))
{
await cloudBlob.UploadFromStreamAsync(uploadStream);
await cloudBlob.UploadFromStreamAsync(uploadStream, null, blobRequestOptions, oc);
}
await cloudBlob.FetchAttributesAsync();
timeTakenStopWatch.Stop();
var uploadResult = cloudBlob.Properties;
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageService), $"Loaded {blobname}, etag {uploadResult.ETag}, time taken {timeTakenStopWatch.ElapsedMilliseconds}");
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageService), $"Loaded {blobname}, etag {uploadResult.ETag}, time taken {timeTakenStopWatch.ElapsedMilliseconds} ClientRequestId {clientRequestId}");
return uploadResult.ETag;
} catch (Exception ex)
{
DiagnosticsETWProvider.Instance.LogAzureStorageException(nameof(StorageService), ex.Message, ex.GetType().ToString(), ex.ToString());
DiagnosticsETWProvider.Instance.LogAzureStorageException(nameof(StorageService), $" ClientRequestId {clientRequestId} {ex.Message}", ex.GetType().ToString(), ex.ToString());
return null;
}
}
public async Task<byte[]> GetBlobByName(string name)
{
try
int retryThreshold = 2;
int attempt = 0;
do
{
var timeTakenStopWatch = new Stopwatch();
await containerClient.CreateIfNotExistsAsync();
timeTakenStopWatch.Start();
var cloudBlob = containerClient.GetBlockBlobReference(name);
using (MemoryStream ms = new MemoryStream())
var clientRequestId = Guid.NewGuid().ToString();
try
{
await cloudBlob.DownloadToStreamAsync(ms);
timeTakenStopWatch.Stop();
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageService), $"Downloaded {name} to memory stream, time taken {timeTakenStopWatch.ElapsedMilliseconds}");
return ms.ToArray();
}
} catch (Exception ex)
{
DiagnosticsETWProvider.Instance.LogAzureStorageException(nameof(StorageService), ex.Message, ex.GetType().ToString(), ex.ToString());
return null;
}
BlobRequestOptions options = new BlobRequestOptions();
options.LocationMode = LocationMode.PrimaryThenSecondary;
options.MaximumExecutionTime = TimeSpan.FromSeconds(30);
if (attempt == retryThreshold)
{
options.LocationMode = LocationMode.SecondaryOnly;
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageService), $"Retrying blob against secondary account after {attempt} attempts");
}
var timeTakenStopWatch = new Stopwatch();
timeTakenStopWatch.Start();
var cloudBlob = containerClient.GetBlockBlobReference(name);
OperationContext oc = new OperationContext();
oc.ClientRequestID = clientRequestId;
using (MemoryStream ms = new MemoryStream())
{
await cloudBlob.DownloadToStreamAsync(ms, null, options, oc);
timeTakenStopWatch.Stop();
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageService), $"Downloaded {name} to memory stream, time taken {timeTakenStopWatch.ElapsedMilliseconds} ClientRequestid {clientRequestId}");
return ms.ToArray();
}
}
catch (Exception ex)
{
DiagnosticsETWProvider.Instance.LogAzureStorageException(nameof(StorageService), $"ClientRequestId {clientRequestId} {ex.Message}", ex.GetType().ToString(), ex.ToString());
return null;
}
finally
{
attempt++;
}
} while (attempt <= retryThreshold);
}
public async Task<int> ListBlobsInContainer()
{
var clientRequestId = Guid.NewGuid().ToString();
try
{
var timeTakenStopWatch = new Stopwatch();
timeTakenStopWatch.Start();
var blobsResult = await containerClient.ListBlobsSegmentedAsync(null);
BlobRequestOptions options = new BlobRequestOptions();
options.LocationMode = LocationMode.PrimaryThenSecondary;
options.MaximumExecutionTime = TimeSpan.FromSeconds(60);
OperationContext oc = new OperationContext();
oc.ClientRequestID = clientRequestId;
var blobsResult = await containerClient.ListBlobsSegmentedAsync(null, true, BlobListingDetails.All, 1000, null, options, oc );
timeTakenStopWatch.Stop();
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageService), $"Number of blobs stored in container {container} is {blobsResult.Results.Count()}, time taken {timeTakenStopWatch.ElapsedMilliseconds} milliseconds");
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageService), $"Number of blobs stored in container {container} is {blobsResult.Results.Count()}, time taken {timeTakenStopWatch.ElapsedMilliseconds} milliseconds, ClientRequestId {clientRequestId}");
return blobsResult.Results != null ? blobsResult.Results.Count() : 0 ;
} catch (Exception ex)
{
DiagnosticsETWProvider.Instance.LogAzureStorageException(nameof(StorageService), ex.Message, ex.GetType().ToString(), ex.ToString());
DiagnosticsETWProvider.Instance.LogAzureStorageException(nameof(StorageService), $"ClientRequestId {clientRequestId} {ex.Message}", ex.GetType().ToString(), ex.ToString());
throw ex;
}
}
public async Task<DetectorRuntimeConfiguration> LoadConfiguration(DetectorRuntimeConfiguration configuration)
{
var clientRequestId = Guid.NewGuid().ToString();
try
{
// Create a table client for interacting with the table service
CloudTable table = tableClient.GetTableReference(detectorRuntimeConfigTable);
await table.CreateIfNotExistsAsync();
if(configuration == null || configuration.PartitionKey == null || configuration.RowKey == null )
{
throw new ArgumentNullException(nameof(configuration));
}
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageService), $"Insert or Replace {configuration.RowKey} into {detectorRuntimeConfigTable}");
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageService), $"Insert or Replace {configuration.RowKey} into {detectorRuntimeConfigTable} ClientRequestId {clientRequestId}");
var timeTakenStopWatch = new Stopwatch();
timeTakenStopWatch.Start();
// Create the InsertOrReplace table operation
TableOperation insertOrReplaceOperation = TableOperation.InsertOrReplace(configuration);
TableRequestOptions tableRequestOptions = new TableRequestOptions();
tableRequestOptions.LocationMode = LocationMode.PrimaryOnly;
tableRequestOptions.MaximumExecutionTime = TimeSpan.FromSeconds(60);
OperationContext oc = new OperationContext();
oc.ClientRequestID = clientRequestId;
// Execute the operation.
TableResult result = await table.ExecuteAsync(insertOrReplaceOperation);
TableResult result = await table.ExecuteAsync(insertOrReplaceOperation, tableRequestOptions, oc);
timeTakenStopWatch.Stop();
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageService), $"InsertOrReplace result : {result.HttpStatusCode}, time taken {timeTakenStopWatch.ElapsedMilliseconds}");
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageService), $"InsertOrReplace result : {result.HttpStatusCode}, time taken {timeTakenStopWatch.ElapsedMilliseconds} ClientRequestId {clientRequestId}");
DetectorRuntimeConfiguration insertedRow = result.Result as DetectorRuntimeConfiguration;
return insertedRow;
} catch (Exception ex)
{
DiagnosticsETWProvider.Instance.LogAzureStorageException(nameof(StorageService), ex.Message, ex.GetType().ToString(), ex.ToString());
DiagnosticsETWProvider.Instance.LogAzureStorageException(nameof(StorageService), $"ClientRequestId {clientRequestId} {ex.Message}", ex.GetType().ToString(), ex.ToString());
return null;
}
@ -250,23 +314,27 @@ namespace Diagnostics.RuntimeHost.Services.StorageService
public async Task<List<DetectorRuntimeConfiguration>> GetKustoConfiguration()
{
var clientRequestId = Guid.NewGuid().ToString();
try
{
CloudTable cloudTable = tableClient.GetTableReference(detectorRuntimeConfigTable);
await cloudTable.CreateIfNotExistsAsync();
var timeTakenStopWatch = new Stopwatch();
var partitionkey = "KustoClusterMapping";
var filterPartitionKey = TableQuery.GenerateFilterCondition(PartitionKey, QueryComparisons.Equal, partitionkey);
var tableQuery = new TableQuery<DetectorRuntimeConfiguration>();
tableQuery.Where(filterPartitionKey);
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageService), $"GetConfiguration by partition key {partitionkey}");
TableContinuationToken tableContinuationToken = null;
var diagConfigurationsResult = new List<DetectorRuntimeConfiguration>();
timeTakenStopWatch.Start();
do
{
// Execute the operation.
var diagConfigurations = await cloudTable.ExecuteQuerySegmentedAsync(tableQuery, tableContinuationToken);
TableRequestOptions tableRequestOptions = new TableRequestOptions();
tableRequestOptions.LocationMode = LocationMode.PrimaryThenSecondary;
tableRequestOptions.MaximumExecutionTime = TimeSpan.FromSeconds(30);
OperationContext oc = new OperationContext();
oc.ClientRequestID = clientRequestId;
var diagConfigurations = await cloudTable.ExecuteQuerySegmentedAsync(tableQuery, tableContinuationToken, tableRequestOptions, oc);
tableContinuationToken = diagConfigurations.ContinuationToken;
if (diagConfigurations.Results != null)
{
@ -274,11 +342,11 @@ namespace Diagnostics.RuntimeHost.Services.StorageService
}
} while (tableContinuationToken != null);
timeTakenStopWatch.Stop();
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageService), $"GetConfiguration by Partition key {partitionkey} took {timeTakenStopWatch.ElapsedMilliseconds}, Total rows = {diagConfigurationsResult.Count}");
DiagnosticsETWProvider.Instance.LogAzureStorageMessage(nameof(StorageService), $"GetConfiguration by Partition key {partitionkey} took {timeTakenStopWatch.ElapsedMilliseconds}, Total rows = {diagConfigurationsResult.Count} ClientRequestId {clientRequestId}");
return diagConfigurationsResult.Where(row => !row.IsDisabled).ToList();
} catch (Exception ex)
{
DiagnosticsETWProvider.Instance.LogAzureStorageException(nameof(StorageService), ex.Message, ex.GetType().ToString(), ex.ToString());
DiagnosticsETWProvider.Instance.LogAzureStorageException(nameof(StorageService), $"ClientRequestId {clientRequestId} {ex.Message}", ex.GetType().ToString(), ex.ToString());
return null;
}
}

Просмотреть файл

@ -1,244 +1,244 @@
using System;
using Xunit;
using Diagnostics.RuntimeHost.Services.CacheService;
using Diagnostics.RuntimeHost.Services.CacheService.Interfaces;
using Microsoft.Extensions.Configuration;
using Diagnostics.RuntimeHost.Services.StorageService;
using Microsoft.AspNetCore.Hosting;
using Diagnostics.ModelsAndUtils.Models.Storage;
using Diagnostics.RuntimeHost.Models;
using Diagnostics.ModelsAndUtils.Models;
using System.Threading;
using RimDev.Automation.StorageEmulator;
using System.Linq;
using Diagnostics.ModelsAndUtils.Attributes;
using Diagnostics.Tests.Helpers;
using Diagnostics.Scripts.Models;
using Diagnostics.Scripts.CompilationService;
using Diagnostics.Scripts.CompilationService.Interfaces;
using Microsoft.CodeAnalysis.Scripting;
using Octokit;
using Diagnostics.RuntimeHost.Utilities;
using System.Reflection;
using Diagnostics.Scripts;
using System.IO;
using Diagnostics.ModelsAndUtils.ScriptUtilities;
//using System;
//using Xunit;
//using Diagnostics.RuntimeHost.Services.CacheService;
//using Diagnostics.RuntimeHost.Services.CacheService.Interfaces;
//using Microsoft.Extensions.Configuration;
//using Diagnostics.RuntimeHost.Services.StorageService;
//using Microsoft.AspNetCore.Hosting;
//using Diagnostics.ModelsAndUtils.Models.Storage;
//using Diagnostics.RuntimeHost.Models;
//using Diagnostics.ModelsAndUtils.Models;
//using System.Threading;
//using RimDev.Automation.StorageEmulator;
//using System.Linq;
//using Diagnostics.ModelsAndUtils.Attributes;
//using Diagnostics.Tests.Helpers;
//using Diagnostics.Scripts.Models;
//using Diagnostics.Scripts.CompilationService;
//using Diagnostics.Scripts.CompilationService.Interfaces;
//using Microsoft.CodeAnalysis.Scripting;
//using Octokit;
//using Diagnostics.RuntimeHost.Utilities;
//using System.Reflection;
//using Diagnostics.Scripts;
//using System.IO;
//using Diagnostics.ModelsAndUtils.ScriptUtilities;
namespace Diagnostics.Tests.AzureStorageTests
{
public class AzureStorageTests: IDisposable
{
//namespace Diagnostics.Tests.AzureStorageTests
//{
// public class AzureStorageTests: IDisposable
// {
IStorageService storageService;
// IStorageService storageService;
IDiagEntityTableCacheService tableCacheService;
// IDiagEntityTableCacheService tableCacheService;
IConfiguration configuration;
// IConfiguration configuration;
IHostingEnvironment environment;
// IHostingEnvironment environment;
AzureStorageEmulatorAutomation emulator;
// AzureStorageEmulatorAutomation emulator;
public AzureStorageTests()
{
StartStorageEmulator();
configuration = InitConfig();
environment = new MockHostingEnvironment();
environment.EnvironmentName = "UnitTest";
if(string.IsNullOrWhiteSpace(configuration["SourceWatcher:TableName"]))
{
configuration["SourceWatcher:TableName"] = "diagentities";
}
if (string.IsNullOrWhiteSpace(configuration["SourceWatcher:BlobContainerName"]))
{
configuration["SourceWatcher:BlobContainerName"] = "detectors";
}
configuration[$"SourceWatcher:WatcherType"] = "AzureStorage";
storageService = new StorageService(configuration, environment);
tableCacheService = new DiagEntityTableCacheService(storageService);
}
// public AzureStorageTests()
// {
// StartStorageEmulator();
// configuration = InitConfig();
// environment = new MockHostingEnvironment();
// environment.EnvironmentName = "UnitTest";
// if(string.IsNullOrWhiteSpace(configuration["SourceWatcher:TableName"]))
// {
// configuration["SourceWatcher:TableName"] = "diagentities";
// }
// if (string.IsNullOrWhiteSpace(configuration["SourceWatcher:BlobContainerName"]))
// {
// configuration["SourceWatcher:BlobContainerName"] = "detectors";
// }
// configuration[$"SourceWatcher:WatcherType"] = "AzureStorage";
// storageService = new StorageService(configuration, environment);
// tableCacheService = new DiagEntityTableCacheService(storageService);
// }
private void StartStorageEmulator()
{
emulator = new AzureStorageEmulatorAutomation();
emulator.Init();
emulator.Start();
}
// private void StartStorageEmulator()
// {
// emulator = new AzureStorageEmulatorAutomation();
// emulator.Init();
// emulator.Start();
// }
public void Dispose()
{
emulator.Stop();
}
// public void Dispose()
// {
// emulator.Stop();
// }
private IConfiguration InitConfig()
{
var builder = new ConfigurationBuilder()
.AddEnvironmentVariables();
return builder.Build();
}
// private IConfiguration InitConfig()
// {
// var builder = new ConfigurationBuilder()
// .AddEnvironmentVariables();
// return builder.Build();
// }
private bool CheckProcessRunning(int maxAttempts)
{
bool isRunning = AzureStorageEmulatorAutomation.IsEmulatorRunning();
int currentAttempt = 0;
while (!isRunning && currentAttempt <= maxAttempts)
{
currentAttempt++;
// Wait for 15s and then try
Thread.Sleep(15 * 1000);
isRunning = AzureStorageEmulatorAutomation.IsEmulatorRunning();
}
return isRunning;
}
// private bool CheckProcessRunning(int maxAttempts)
// {
// bool isRunning = AzureStorageEmulatorAutomation.IsEmulatorRunning();
// int currentAttempt = 0;
// while (!isRunning && currentAttempt <= maxAttempts)
// {
// currentAttempt++;
// // Wait for 15s and then try
// Thread.Sleep(15 * 1000);
// isRunning = AzureStorageEmulatorAutomation.IsEmulatorRunning();
// }
// return isRunning;
// }
[Fact]
/// <summary>
/// Tests load entity and insert entity
/// </summary>
public async void TestTableOperations()
{
// First check if emulator is running before proceeding.
bool isEmulatorRunning = CheckProcessRunning(4);
if(isEmulatorRunning)
{
// Generate fake entity;
var diagEntity = new DiagEntity
{
PartitionKey = "Detector",
RowKey = "xyz",
GithubLastModified = DateTime.UtcNow
};
var insertResult = await storageService.LoadDataToTable(diagEntity);
Assert.NotNull(insertResult);
var retrieveResult = await storageService.GetEntitiesByPartitionkey("Detector");
Assert.NotNull(retrieveResult);
Assert.NotEmpty(retrieveResult);
var gistResult = await storageService.GetEntitiesByPartitionkey("Gist");
Assert.Empty(gistResult);
}
}
// [Fact]
// /// <summary>
// /// Tests load entity and insert entity
// /// </summary>
// public async void TestTableOperations()
// {
// // First check if emulator is running before proceeding.
// bool isEmulatorRunning = CheckProcessRunning(4);
// if(isEmulatorRunning)
// {
// // Generate fake entity;
// var diagEntity = new DiagEntity
// {
// PartitionKey = "Detector",
// RowKey = "xyz",
// GithubLastModified = DateTime.UtcNow
// };
// var insertResult = await storageService.LoadDataToTable(diagEntity);
// Assert.NotNull(insertResult);
// var retrieveResult = await storageService.GetEntitiesByPartitionkey("Detector");
// Assert.NotNull(retrieveResult);
// Assert.NotEmpty(retrieveResult);
// var gistResult = await storageService.GetEntitiesByPartitionkey("Gist");
// Assert.Empty(gistResult);
// }
// }
[Fact]
/// <summary>
/// Test if entites are retrieved according to runtime context.
/// </summary>
public async void TestCacheOperations()
{
// [Fact]
// /// <summary>
// /// Test if entites are retrieved according to runtime context.
// /// </summary>
// public async void TestCacheOperations()
// {
// First check if emulator is running before proceeding.
bool isEmulatorRunning = CheckProcessRunning(4);
if(isEmulatorRunning)
{
var windowsDiagEntity = new DiagEntity
{
PartitionKey = "Detector",
RowKey = "webappDown",
GithubLastModified = DateTime.UtcNow,
PlatForm = "Windows",
ResourceProvider = "Microsoft.Web",
ResourceType = "sites",
StackType = "AspNet,NetCore",
AppType = "WebApp",
DetectorType = "Detector"
};
// // First check if emulator is running before proceeding.
// bool isEmulatorRunning = CheckProcessRunning(4);
// if(isEmulatorRunning)
// {
// var windowsDiagEntity = new DiagEntity
// {
// PartitionKey = "Detector",
// RowKey = "webappDown",
// GithubLastModified = DateTime.UtcNow,
// PlatForm = "Windows",
// ResourceProvider = "Microsoft.Web",
// ResourceType = "sites",
// StackType = "AspNet,NetCore",
// AppType = "WebApp",
// DetectorType = "Detector"
// };
var insertResult = await storageService.LoadDataToTable(windowsDiagEntity);
// var insertResult = await storageService.LoadDataToTable(windowsDiagEntity);
// Test Analysis detectors
// // Test Analysis detectors
var appDownAnalysisEntity = new DiagEntity
{
PartitionKey = "Detector",
RowKey = "appDownAnalysis",
GithubLastModified = DateTime.UtcNow,
PlatForm = "Windows",
ResourceProvider = "Microsoft.Web",
ResourceType = "sites",
StackType = "AspNet,NetCore",
AppType = "WebApp",
DetectorType = "Analysis"
};
// var appDownAnalysisEntity = new DiagEntity
// {
// PartitionKey = "Detector",
// RowKey = "appDownAnalysis",
// GithubLastModified = DateTime.UtcNow,
// PlatForm = "Windows",
// ResourceProvider = "Microsoft.Web",
// ResourceType = "sites",
// StackType = "AspNet,NetCore",
// AppType = "WebApp",
// DetectorType = "Analysis"
// };
insertResult = await storageService.LoadDataToTable(appDownAnalysisEntity);
var webApp = new App("72383ac7-d6f4-4a5e-bf56-b172f2fdafb2", "resourcegp-default", "diag-test");
var operationContext = new OperationContext<App>(
webApp,
DateTime.Now.ToString(),
DateTime.Now.AddHours(1).ToString(),
true,
new Guid().ToString()
);
var context = new RuntimeContext<App>(configuration);
context.OperationContext = operationContext;
// insertResult = await storageService.LoadDataToTable(appDownAnalysisEntity);
// var webApp = new App("72383ac7-d6f4-4a5e-bf56-b172f2fdafb2", "resourcegp-default", "diag-test");
// var operationContext = new OperationContext<App>(
// webApp,
// DateTime.Now.ToString(),
// DateTime.Now.AddHours(1).ToString(),
// true,
// new Guid().ToString()
// );
// var context = new RuntimeContext<App>(configuration);
// context.OperationContext = operationContext;
var detectorsForWebApps = await tableCacheService.GetEntityListByType(context, "Detector");
Assert.NotNull(detectorsForWebApps);
Assert.NotEmpty(detectorsForWebApps);
Assert.NotEmpty(detectorsForWebApps.Where(s => s.DetectorType != null && s.DetectorType.Equals("Analysis", StringComparison.CurrentCultureIgnoreCase)));
// var detectorsForWebApps = await tableCacheService.GetEntityListByType(context, "Detector");
// Assert.NotNull(detectorsForWebApps);
// Assert.NotEmpty(detectorsForWebApps);
// Assert.NotEmpty(detectorsForWebApps.Where(s => s.DetectorType != null && s.DetectorType.Equals("Analysis", StringComparison.CurrentCultureIgnoreCase)));
var logicApp = new LogicApp("72383ac7-d6f4-4a5e-bf56-b172f2fdafb2", "resourcegp-default", "la-test");
var logicAppOperContext = new OperationContext<LogicApp>(logicApp,
DateTime.Now.ToString(),
DateTime.Now.AddHours(1).ToString(),
true,
new Guid().ToString());
// var logicApp = new LogicApp("72383ac7-d6f4-4a5e-bf56-b172f2fdafb2", "resourcegp-default", "la-test");
// var logicAppOperContext = new OperationContext<LogicApp>(logicApp,
// DateTime.Now.ToString(),
// DateTime.Now.AddHours(1).ToString(),
// true,
// new Guid().ToString());
var runtimeContextLogicApp = new RuntimeContext<LogicApp>(configuration);
runtimeContextLogicApp.OperationContext = logicAppOperContext;
// var runtimeContextLogicApp = new RuntimeContext<LogicApp>(configuration);
// runtimeContextLogicApp.OperationContext = logicAppOperContext;
var detectorsForLogicApps = await tableCacheService.GetEntityListByType(runtimeContextLogicApp, "Detector");
Assert.NotNull(detectorsForLogicApps);
Assert.Empty(detectorsForLogicApps);
// var detectorsForLogicApps = await tableCacheService.GetEntityListByType(runtimeContextLogicApp, "Detector");
// Assert.NotNull(detectorsForLogicApps);
// Assert.Empty(detectorsForLogicApps);
}
}
// }
// }
[Fact]
/// <summary>
/// Test blob upload operation
/// </summary>
public async void TestBlobOperations()
{
// First check if emulator is running before proceeding.
bool isEmulatorRunning = CheckProcessRunning(4);
if (isEmulatorRunning)
{
// Test .dll blob upload
// [Fact]
// /// <summary>
// /// Test blob upload operation
// /// </summary>
// public async void TestBlobOperations()
// {
// // First check if emulator is running before proceeding.
// bool isEmulatorRunning = CheckProcessRunning(4);
// if (isEmulatorRunning)
// {
// // Test .dll blob upload
Definition definitonAttribute = new Definition()
{
Id = "blobDetector"
};
// Definition definitonAttribute = new Definition()
// {
// Id = "blobDetector"
// };
EntityMetadata metadata = ScriptTestDataHelper.GetRandomMetadata();
metadata.ScriptText = await File.ReadAllTextAsync("blobDetector.csx");
var scriptOptions = ScriptTestDataHelper.GetScriptOption(ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports());
var serviceInstance = CompilationServiceFactory.CreateService(metadata, scriptOptions);
ICompilation compilation = await serviceInstance.GetCompilationAsync();
// EntityMetadata metadata = ScriptTestDataHelper.GetRandomMetadata();
// metadata.ScriptText = await File.ReadAllTextAsync("blobDetector.csx");
// var scriptOptions = ScriptTestDataHelper.GetScriptOption(ScriptHelper.GetFrameworkReferences(), ScriptHelper.GetFrameworkImports());
// var serviceInstance = CompilationServiceFactory.CreateService(metadata, scriptOptions);
// ICompilation compilation = await serviceInstance.GetCompilationAsync();
var assemblyBytes = await compilation.GetAssemblyBytesAsync();
var blobName = $"{definitonAttribute.Id}/{definitonAttribute.Id}.dll";
var etagdetector = await storageService.LoadBlobToContainer(blobName, assemblyBytes.Item1);
Assert.NotNull(etagdetector);
var assemblyData = await storageService.GetBlobByName(blobName);
Assert.NotNull(assemblyData);
// var assemblyBytes = await compilation.GetAssemblyBytesAsync();
// var blobName = $"{definitonAttribute.Id}/{definitonAttribute.Id}.dll";
// var etagdetector = await storageService.LoadBlobToContainer(blobName, assemblyBytes.Item1);
// Assert.NotNull(etagdetector);
// var assemblyData = await storageService.GetBlobByName(blobName);
// Assert.NotNull(assemblyData);
// Now test initializing Entry Point of Invoker using assembly
Assembly temp = Assembly.Load(assemblyData);
using (EntityInvoker invoker = new EntityInvoker(metadata))
{
Exception ex = Record.Exception(() =>
{
invoker.InitializeEntryPoint(temp);
});
// // Now test initializing Entry Point of Invoker using assembly
// Assembly temp = Assembly.Load(assemblyData);
// using (EntityInvoker invoker = new EntityInvoker(metadata))
// {
// Exception ex = Record.Exception(() =>
// {
// invoker.InitializeEntryPoint(temp);
// });
Assert.Null(ex);
Assert.True(invoker.IsCompilationSuccessful);
Assert.Equal(definitonAttribute.Id, invoker.EntryPointDefinitionAttribute.Id);
}
}
}
}
}
// Assert.Null(ex);
// Assert.True(invoker.IsCompilationSuccessful);
// Assert.Equal(definitonAttribute.Id, invoker.EntryPointDefinitionAttribute.Id);
// }
// }
// }
// }
//}