forked from OrleansContrib/OrleansV2.Fork.Indexing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseIndexingFixture.cs
105 lines (96 loc) · 5.39 KB
/
BaseIndexingFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using Microsoft.Extensions.Logging;
using Orleans.Hosting;
using Orleans.Runtime;
using Orleans.TestingHost;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using TestExtensions;
using UnitTests.GrainInterfaces;
using UnitTests.Grains;
namespace Orleans.Indexing.Tests
{
public abstract class BaseIndexingFixture : BaseTestClusterFixture
{
protected TestClusterBuilder ConfigureTestClusterForIndexing(TestClusterBuilder builder)
{
// Currently nothing
//builder.Options.InitialSilosCount = 1; // For debugging if needed
return builder;
}
internal static ISiloHostBuilder Configure(ISiloHostBuilder hostBuilder, string databaseName = null)
{
if (!TestDefaultConfiguration.GetValue("CosmosDBEndpoint", out string cosmosDBEndpoint)
|| !TestDefaultConfiguration.GetValue("CosmosDBKey", out string cosmosDBKey))
{
throw new IndexConfigurationException("CosmosDB connection values are not specified");
}
hostBuilder.AddMemoryGrainStorage(IndexingTestConstants.GrainStore)
.AddMemoryGrainStorage("PubSubStore") // PubSubStore service is needed for the streams underlying OrleansQueryResults
.ConfigureLogging(loggingBuilder =>
{
loggingBuilder.SetMinimumLevel(LogLevel.Information);
loggingBuilder.AddDebug();
})
.ConfigureApplicationParts(parts =>
{
parts.AddApplicationPart(typeof(BaseIndexingFixture).Assembly);
parts.AddApplicationPart(typeof(ISimpleGrain).Assembly);
parts.AddApplicationPart(typeof(SimpleGrain).Assembly);
});
return databaseName != null
? hostBuilder.AddCosmosDBGrainStorage(IndexingTestConstants.CosmosDBGrainStorage, opt =>
{
opt.AccountEndpoint = cosmosDBEndpoint;
opt.AccountKey = cosmosDBKey;
opt.ConnectionMode = Microsoft.Azure.Documents.Client.ConnectionMode.Gateway;
opt.DropDatabaseOnInit = true;
opt.AutoUpdateStoredProcedures = true;
opt.CanCreateResources = true;
opt.DB = databaseName;
opt.InitStage = ServiceLifecycleStage.RuntimeStorageServices;
opt.StateFieldsToIndex.AddRange(GetDSMIStateFieldsToIndex());
})
: hostBuilder;
}
internal static IClientBuilder Configure(IClientBuilder clientBuilder)
{
return clientBuilder.ConfigureLogging(loggingBuilder =>
{
loggingBuilder.SetMinimumLevel(LogLevel.Information);
loggingBuilder.AddDebug();
})
.ConfigureApplicationParts(parts =>
{
parts.AddApplicationPart(typeof(BaseIndexingFixture).Assembly);
parts.AddApplicationPart(typeof(ISimpleGrain).Assembly);
parts.AddApplicationPart(typeof(SimpleGrain).Assembly);
});
}
// Code below adapted from ApplicationPartsIndexableGrainLoader to identify the necessary fields for the DSMI storage
// provider to index.
private static IEnumerable<string> GetDSMIStateFieldsToIndex()
{
var grainClassTypes = typeof(BaseIndexingFixture).Assembly.GetConcreteGrainClasses(logger: null).ToArray();
// Orleans.CosmosDB appends the field names to "State."; thus we do not prepend the interface names.
var interfacesToIndexedPropertyNames = new Dictionary<Type, string[]>();
foreach (var grainClassType in grainClassTypes)
{
GetDSMIFieldsForASingleGrainType(grainClassType, interfacesToIndexedPropertyNames);
}
return new HashSet<string>(interfacesToIndexedPropertyNames.Where(kvp => kvp.Value.Length > 0).SelectMany(kvp => kvp.Value));
}
internal static void GetDSMIFieldsForASingleGrainType(Type grainClassType, Dictionary<Type, string[]> interfacesToIndexedPropertyNames)
{
foreach (var (grainInterfaceType, propertiesClassType) in ApplicationPartsIndexableGrainLoader.EnumerateIndexedInterfacesForAGrainClassType(grainClassType)
.Where(tup => !interfacesToIndexedPropertyNames.ContainsKey(tup.interfaceType)))
{
interfacesToIndexedPropertyNames[grainInterfaceType] = propertiesClassType.GetProperties()
.Where(propInfo => propInfo.GetCustomAttributes<StorageManagedIndexAttribute>(inherit: false).Any())
.Select(propInfo => IndexingConstants.UserStatePrefix + propInfo.Name)
.ToArray();
}
}
}
}