public void SiloHostBuilder_ServiceProviderTest()
        {
            var builder = new SiloHostBuilder()
                          .ConfigureDefaults()
                          .UseLocalhostClustering()
                          .ConfigureServices(RemoveConfigValidatorsAndSetAddress)
                          .ConfigureServices(services => services.AddSingleton <IMembershipTable, NoOpMembershipTable>());

            Assert.Throws <ArgumentNullException>(() => builder.ConfigureServices(null));

            var registeredFirst = new int[1];

            var one = new MyService {
                Id = 1
            };

            builder.ConfigureServices(
                services =>
            {
                Interlocked.CompareExchange(ref registeredFirst[0], 1, 0);
                services.AddSingleton(one);
            });

            var two = new MyService {
                Id = 2
            };

            builder.ConfigureServices(
                services =>
            {
                Interlocked.CompareExchange(ref registeredFirst[0], 2, 0);
                services.AddSingleton(two);
            });

            using (var silo = builder.Build())
            {
                var services = silo.Services.GetServices <MyService>()?.ToList();
                Assert.NotNull(services);

                // Both services should be registered.
                Assert.Equal(2, services.Count);
                Assert.NotNull(services.FirstOrDefault(svc => svc.Id == 1));
                Assert.NotNull(services.FirstOrDefault(svc => svc.Id == 2));

                // Service 1 should have been registered first - the pipeline order should be preserved.
                Assert.Equal(1, registeredFirst[0]);

                // The last registered service should be provided by default.
                Assert.Equal(2, silo.Services.GetRequiredService <MyService>().Id);
            }
        }
        /// <inheritdoc />
        public async Task <string> OpenAsync(CancellationToken cancellationToken)
        {
            try
            {
                var builder = new SiloHostBuilder();
                builder.ConfigureServices(
                    services =>
                {
                    services.AddOptions <FabricSiloInfo>().Configure <ILocalSiloDetails>((info, details) =>
                    {
                        info.Name = details.Name;
                        info.Silo = details.SiloAddress.ToParsableString();
                        if (details.GatewayAddress != null)
                        {
                            info.Gateway = details.GatewayAddress.ToParsableString();
                        }
                    });
                });
                this.configure(builder);

                this.Host = builder.Build();
                await this.Host.StartAsync(cancellationToken);
            }
            catch
            {
                this.Abort();
                throw;
            }

            var endpoint = this.Host.Services.GetRequiredService <IOptions <FabricSiloInfo> >().Value;

            return(JsonConvert.SerializeObject(endpoint));
        }
Exemple #3
0
        /// <inheritdoc />
        public async Task <string> OpenAsync(CancellationToken cancellationToken)
        {
            try
            {
                var builder = new SiloHostBuilder();
                builder.ConfigureServices(
                    services =>
                {
                    services.AddSingleton <IConfigureOptions <FabricSiloInfo>, SiloInfoConfiguration>();
                });
                this.configure(builder);

                this.Host = builder.Build();
                await this.Host.StartAsync(cancellationToken);
            }
            catch
            {
                this.Abort();
                throw;
            }

            var endpoint = this.Host.Services.GetRequiredService <IOptions <FabricSiloInfo> >().Value;

            return(JsonConvert.SerializeObject(endpoint));
        }
Exemple #4
0
        public static async Task Main(string[] args)
        {
            SetupApplicationShutdown();

            Console.Write("SiloPort: ");
            var siloPort = int.Parse(Console.ReadLine());

            Console.Write("GatewayPort: ");
            var gatewayPort = int.Parse(Console.ReadLine());

            var appSettings = LoadConfiguration();

            var siloBuilder = new SiloHostBuilder()
                              .UseDashboard(options => { })
                              .Configure <GrainCollectionOptions>(options =>
            {
                options.CollectionAge = TimeSpan.FromDays(appSettings.OrleansSiloSettings.AgeLimitInDays);
            })
                              .Configure <ClusterOptions>(options =>
            {
                options.ClusterId = appSettings.OrleansSiloSettings.ClusterId;
                options.ServiceId = appSettings.OrleansSiloSettings.ServiceId;
            })
                              .AddMemoryGrainStorageAsDefault()
                              .UseAdoNetClustering(options =>
            {
                options.Invariant        = appSettings.OrleansSiloSettings.ClusterInfo.Invariant;
                options.ConnectionString = appSettings.OrleansSiloSettings.ClusterInfo.ConnectionString;
            })
                              .ConfigureEndpoints(siloPort: siloPort, gatewayPort: gatewayPort)
                              .ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(Sensor).Assembly).WithReferences())
                              .Configure <ProcessExitHandlingOptions>(options => options.FastKillOnProcessExit = false)
                              .ConfigureLogging(logging => logging.AddConsole());

            siloBuilder.ConfigureServices(svc => svc.AddSingleton <IPublisher <SensorState>, Publisher <SensorState> >());
            siloBuilder.ConfigureServices(svc => svc.AddSingleton(appSettings));
            using (host = siloBuilder.Build())
            {
                await host.StartAsync();

                Console.ReadLine();
            }
            _siloStopped.WaitOne();
        }
Exemple #5
0
        /// <summary>
        /// Creates an returns a new silo.
        /// </summary>
        /// <param name="hostName">The silo name if it is not already specified in the configuration.</param>
        /// <param name="configurationSources">The configuration.</param>
        /// <returns>A new silo.</returns>
        public static ISiloHost CreateSiloHost(string hostName, IEnumerable <IConfigurationSource> configurationSources)
        {
            var configBuilder = new ConfigurationBuilder();

            foreach (var source in configurationSources)
            {
                configBuilder.Add(source);
            }
            var configuration = configBuilder.Build();

            string siloName = configuration[nameof(TestSiloSpecificOptions.SiloName)] ?? hostName;

            var hostBuilder = new SiloHostBuilder()
                              .Configure <ClusterOptions>(configuration)
                              .Configure <SiloOptions>(options => options.SiloName = siloName)
                              .Configure <ClusterMembershipOptions>(options =>
            {
                options.ExpectedClusterSize = int.Parse(configuration["InitialSilosCount"]);
            })
                              .ConfigureHostConfiguration(cb =>
            {
                // TODO: Instead of passing the sources individually, just chain the pre-built configuration once we upgrade to Microsoft.Extensions.Configuration 2.1
                foreach (var source in configBuilder.Sources)
                {
                    cb.Add(source);
                }
            });

            hostBuilder.Properties["Configuration"] = configuration;
            ConfigureAppServices(configuration, hostBuilder);

            hostBuilder.ConfigureServices((context, services) =>
            {
                services.AddSingleton <TestHooksHostEnvironmentStatistics>();
                services.AddFromExisting <IHostEnvironmentStatistics, TestHooksHostEnvironmentStatistics>();
                services.AddSingleton <TestHooksSystemTarget>();
                ConfigureListeningPorts(context, services);

                TryConfigureTestClusterMembership(context, services);
                TryConfigureFileLogging(configuration, services, siloName);

                if (Debugger.IsAttached)
                {
                    // Test is running inside debugger - Make timeout ~= infinite
                    services.Configure <SiloMessagingOptions>(op => op.ResponseTimeout = TimeSpan.FromMilliseconds(1000000));
                }
            });

            hostBuilder.GetApplicationPartManager().ConfigureDefaults();

            var host = hostBuilder.Build();

            InitializeTestHooksSystemTarget(host);
            return(host);
        }
        public static ISiloHost CreateSiloHost(string hostName, IEnumerable <IConfigurationSource> configurationSources)
        {
            var configBuilder = new ConfigurationBuilder();

            foreach (var source in configurationSources)
            {
                configBuilder.Add(source);
            }
            var configuration = configBuilder.Build();

            string siloName = configuration[nameof(TestSiloSpecificOptions.SiloName)] ?? hostName;

            ISiloHostBuilder hostBuilder = new SiloHostBuilder()
                                           .Configure(ob => ob.Bind(configuration))
                                           .ConfigureSiloName(siloName)
                                           .ConfigureHostConfiguration(cb =>
            {
                // TODO: Instead of passing the sources individually, just chain the pre-built configuration once we upgrade to Microsoft.Extensions.Configuration 2.1
                foreach (var source in configBuilder.Sources)
                {
                    cb.Add(source);
                }
            });

            hostBuilder.Properties["Configuration"] = configuration;
            ConfigureAppServices(configuration, hostBuilder);

            hostBuilder.ConfigureServices((context, services) =>
            {
                services.AddSingleton <TestHooksHostEnvironmentStatistics>();
                services.AddFromExisting <IHostEnvironmentStatistics, TestHooksHostEnvironmentStatistics>();
                services.AddSingleton <TestHooksSystemTarget>();
                ConfigureListeningPorts(context, services);

                TryConfigureTestClusterMembership(context, services);
                TryConfigureFileLogging(configuration, services, siloName);

                // TODO: make SiloHostBuilder work when not using the legacy configuration, similar to what we did with ClientBuilder.
                // All The important information has been migrated to strongly typed options (everything should be migrated, but the minimum required set is already there).
                var clusterConfiguration = GetOrCreateClusterConfiguration(services);
                if (Debugger.IsAttached)
                {
                    // Test is running inside debugger - Make timeout ~= infinite
                    clusterConfiguration.Globals.ResponseTimeout = TimeSpan.FromMilliseconds(1000000);
                }
            });

            hostBuilder.GetApplicationPartManager().ConfigureDefaults();

            var host = hostBuilder.Build();

            InitializeTestHooksSystemTarget(host);
            return(host);
        }
        private ISiloHost CreateSiloHost()
        {
            var builder = new SiloHostBuilder();

            if (_dashboardOptions.Value.Enable)
            {
                builder.UseDashboard(options =>
                {
                    options.Port = _dashboardOptions.Value.Port;
                });
            }

            builder.Configure <ClusterOptions>(options =>
            {
                options.ClusterId = _siloOptions.Value.ClusterId;
                options.ServiceId = _siloOptions.Value.ServiceId;
            });

            if (string.IsNullOrEmpty(_siloOptions.Value.AdvertisedIp) || "*".Equals(_siloOptions.Value.AdvertisedIp))
            {
                builder.ConfigureEndpoints(_siloOptions.Value.SiloPort, _siloOptions.Value.GatewayPort,
                                           listenOnAnyHostAddress: _siloOptions.Value.ListenOnAnyHostAddress);
            }
            else
            {
                var ip = IPAddress.Parse(_siloOptions.Value.AdvertisedIp);

                builder.ConfigureEndpoints(ip, _siloOptions.Value.SiloPort, _siloOptions.Value.GatewayPort,
                                           listenOnAnyHostAddress: _siloOptions.Value.ListenOnAnyHostAddress);
            }

            if (_providerOptions.Value.DefaultProvider == "MongoDB")
            {
                var mongoDbOption = _providerOptions.Value.MongoDB;
                builder.UseMongoDBClustering(options =>
                {
                    var clusterOption = mongoDbOption.Cluster;

                    options.ConnectionString = clusterOption.DbConn;
                    options.DatabaseName     = clusterOption.DbName;

                    // see:https://github.com/OrleansContrib/Orleans.Providers.MongoDB/issues/54
                    options.CollectionPrefix = clusterOption.CollectionPrefix;
                })
                .UseMongoDBReminders(options =>
                {
                    var reminderOption = mongoDbOption.Reminder;

                    options.ConnectionString = reminderOption.DbConn;
                    options.DatabaseName     = reminderOption.DbName;

                    if (!string.IsNullOrEmpty(reminderOption.CollectionPrefix))
                    {
                        options.CollectionPrefix = reminderOption.CollectionPrefix;
                    }
                })
                .AddMongoDBGrainStorageAsDefault(options =>
                {
                    var storageOption = mongoDbOption.Storage;

                    options.ConnectionString = storageOption.DbConn;
                    options.DatabaseName     = storageOption.DbName;

                    if (!string.IsNullOrEmpty(storageOption.CollectionPrefix))
                    {
                        options.CollectionPrefix = storageOption.CollectionPrefix;
                    }
                });
            }

            builder.ConfigureServices(services =>
            {
                services
                .AddLogging(loggingBuilder => loggingBuilder.AddSerilog())
                .AddTransient <GameHost>()
                .AddTransient <LeaderBoard>()
                .AddTransient <Player>();
            })
            .ConfigureApplicationParts(parts => { parts.AddFromApplicationBaseDirectory().WithReferences(); })
            .ConfigureLogging(logging => { logging.AddSerilog(dispose: true); });

            return(builder.Build());
        }
Exemple #8
0
        public PingBenchmark(int numSilos, bool startClient, bool grainsOnSecondariesOnly = false)
        {
            for (var i = 0; i < numSilos; ++i)
            {
                var primary     = i == 0 ? null : new IPEndPoint(IPAddress.Loopback, 11111);
                var siloBuilder = new SiloHostBuilder()
                                  .ConfigureDefaults()
                                  .UseLocalhostClustering(
                    siloPort: 11111 + i,
                    gatewayPort: 30000 + i,
                    primarySiloEndpoint: primary);

                if (i == 0 && grainsOnSecondariesOnly)
                {
                    siloBuilder.ConfigureApplicationParts(parts =>
                                                          parts.AddApplicationPart(typeof(IPingGrain).Assembly));
                    siloBuilder.ConfigureServices(services =>
                    {
                        services.Remove(services.First(s => s.ImplementationType?.Name == "ApplicationPartValidator"));
                    });
                }
                else
                {
                    siloBuilder.ConfigureApplicationParts(parts =>
                                                          parts.AddApplicationPart(typeof(IPingGrain).Assembly)
                                                          .AddApplicationPart(typeof(PingGrain).Assembly));
                }

                var silo = siloBuilder.Build();
                silo.StartAsync().GetAwaiter().GetResult();
                this.hosts.Add(silo);
            }

            if (grainsOnSecondariesOnly)
            {
                Thread.Sleep(4000);
            }

            if (startClient)
            {
                var clientBuilder = new ClientBuilder()
                                    .ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(IPingGrain).Assembly))
                                    .Configure <ClusterOptions>(options => options.ClusterId = options.ServiceId = "dev");

                if (numSilos == 1)
                {
                    clientBuilder.UseLocalhostClustering();
                }
                else
                {
                    var gateways = Enumerable.Range(30000, numSilos).Select(i => new IPEndPoint(IPAddress.Loopback, i)).ToArray();
                    clientBuilder.UseStaticClustering(gateways);
                }

                this.client = clientBuilder.Build();
                this.client.Connect().GetAwaiter().GetResult();
                var grainFactory = this.client;

                this.grain = grainFactory.GetGrain <IPingGrain>(Guid.NewGuid().GetHashCode());
                this.grain.Run().GetAwaiter().GetResult();
            }
        }
Exemple #9
0
        void BuildSiloHost()
        {
            var instrumentationKey           = _configuration.GetValue <string>("ApplicationInsights:InstrumentationKey");
            var hostname                     = _configuration.GetValue <string>("HOSTNAME");
            var azureStorageConnectionString = _configuration.GetValue <string>("Storage:AzureStorageConnectionString");
            var redisClusteringUrl           = _configuration.GetValue <string>("REDIS_URL");

            var builder = new SiloHostBuilder()
                          .ConfigureLogging(loggingBuilder =>
            {
                if (!string.IsNullOrEmpty(instrumentationKey))
                {
                    loggingBuilder.AddApplicationInsights(instrumentationKey);
                }
                loggingBuilder.AddConsole();
            })
                          // Configure ClusterId and ServiceId
                          .Configure <ClusterOptions>(options =>
            {
                options.ClusterId = "dev";
                options.ServiceId = "TwitchServices";
            })
                          .AddStartupTask <LoadConfigurationStartupTask>()
                          .ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(ChannelGrain).Assembly).WithReferences())
                          // Configure connectivity
                          .ConfigureEndpoints(hostname: hostname, siloPort: 11111, gatewayPort: 30000);

            if (!string.IsNullOrEmpty(azureStorageConnectionString))
            {
                builder.AddAzureTableGrainStorage("profileStore", (AzureTableStorageOptions options) =>
                {
                    options.ConnectionString = azureStorageConnectionString;
                    options.TableName        = "profiles";
                    options.UseJson          = true;
                    options.IndentJson       = false;
                });
                builder.AddAzureTableGrainStorage("channelStore", (AzureTableStorageOptions options) =>
                {
                    options.ConnectionString = azureStorageConnectionString;
                    options.TableName        = "channels";
                    options.UseJson          = true;
                    options.IndentJson       = false;
                });
                builder.AddAzureTableGrainStorage("botSettingsStore", (AzureTableStorageOptions options) =>
                {
                    options.ConnectionString = azureStorageConnectionString;
                    options.TableName        = "botsettings";
                    options.UseJson          = true;
                    options.IndentJson       = false;
                });
                builder.AddCustomCategoriesStorage("customCategoriesStore", (CustomCategoriesStorageOptions options) =>
                {
                    options.ConnectionString = azureStorageConnectionString;
                    options.TableName        = "customcategories";
                });
            }
            else
            {
                builder.AddMemoryGrainStorage("profileStore");
                builder.AddMemoryGrainStorage("channelStore");
                builder.AddMemoryGrainStorage("botSettingsStore");
                builder.AddMemoryGrainStorage("customCategoriesStore");
            }

            if (!string.IsNullOrEmpty(redisClusteringUrl))
            {
                // Use redis clustering when available
                builder.UseRedisClustering(redisClusteringUrl);
            }
            else
            {
                // Use localhost clustering for a single local silo
                builder.UseLocalhostClustering(11111, 30000, null, "TwitchServices", "dev");
            }

            // Temp

            builder.ConfigureServices((context, services) =>
            {
                // Load channels and command configuration from static json file, and inject
                var channelsConfig = new ConfigurationBuilder().AddJsonFile("channels.json").Build();
                IEnumerable <ChannelOptions> channelOptions = new List <ChannelOptions>();
                channelsConfig.GetSection("channels").Bind(channelOptions);
                services.AddTransient <IEnumerable <ChannelOptions> >((_) => channelOptions);

                // Configure services
                services.AddHttpClient();
                services.Configure <TwitchApplicationOptions>(_configuration.GetSection("twitch"));
                services.Configure <TwitchChatClientOptions>(_configuration.GetSection("twitch").GetSection("IrcOptions"));
                services.Configure <AzureGameLocalizationStoreOptions>(_configuration.GetSection("loc:azure"));
                services.AddSingleton <IMessageProcessor, TracingMessageProcessor>();
                services.AddTransient <TwitchChatClient>();
                services.AddTransient <TwitchAPIClient>();
                services.AddTransient <IGDBClient>();
                services.AddSingleton <IMemoryCache, MemoryCache>();
                services.AddSingleton <SteamStoreClient>();
                services.AddSingleton <IAuthenticated>(s =>
                                                       Twitch.Authenticate()
                                                       .FromAppCredentials(
                                                           s.GetService <IOptions <TwitchApplicationOptions> >().Value.ClientId,
                                                           s.GetService <IOptions <TwitchApplicationOptions> >().Value.ClientSecret)
                                                       .Build()
                                                       );
                services.AddTransient <ITwitchCategoryProvider, GrainTwitchCategoryProvider>();
                services.AddSingleton <IGameLocalizationStore, AzureStorageGameLocalizationStore>();
                services.PostConfigure <TwitchChatClientOptions>(options =>
                {
                    var oauth = Twitch.Authenticate()
                                .FromOAuthToken(options.OAuthToken)
                                .Build();
                    var loggerFactory = new LoggerFactory();
                    using (var httpClient = new HttpClient())
                        using (var apiClient = TwitchAPIClient.Create(oauth))
                        {
                            options.TokenInfo = apiClient.ValidateToken().Result;
                        }
                });

                // Configure commands
                services.AddCommand <GameSynopsisCommand>("GameSynopsis");
                services.AddCommand <TracingMessageProcessor>("Logger");
                services.AddCommand <ResponseCommandProcessor>("Response");
            });
            _siloHost = builder.Build();
        }