Exemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="OptionsFactory{TOptions}"/> class.
 /// </summary>
 /// <param name="setups">setups.</param>
 /// <param name="defaultValueProviders">Провайдер значения по-умолчанию для типа.</param>
 public OptionsFactory(
     IEnumerable <IConfigureOptions <TOptions> > setups,
     IKeyedServiceCollection <string, IDefaultValueProvider <TOptions> > defaultValueProviders = null)
 {
     _setups = setups;
     _defaultValueProviders = defaultValueProviders;
 }
Exemplo n.º 2
0
        /// <summary>
        /// Acquire a service by key.
        /// </summary>
        public static TService GetServiceByKey <TKey, TService>(this IServiceProvider services, TKey key)
            where TService : class
        {
            IKeyedServiceCollection <TKey, TService> collection = (IKeyedServiceCollection <TKey, TService>)services.GetService(typeof(IKeyedServiceCollection <TKey, TService>));

            return(collection?.GetService(services, key));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Acquire a service by key.
        /// </summary>
        public static TService GetServiceByKey <TKey, TService>(this IServiceProvider services, TKey key)
            where TService : class
        {
            IKeyedServiceCollection <TKey, TService> collection = services.GetService <IKeyedServiceCollection <TKey, TService> >();

            return(collection?.GetService(key));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ClusterClient"/> class.
        /// </summary>
        /// <param name="runtimeClient">The runtime client.</param>
        /// <param name="configuration">The client configuration.</param>
        /// <param name="loggerFactory">Logger factory used to create loggers</param>
        public ClusterClient(OutsideRuntimeClient runtimeClient, ILoggerFactory loggerFactory, IOptions <ClientMessagingOptions> clientMessagingOptions)
        {
            this.runtimeClient          = runtimeClient;
            this.clusterClientLifecycle = new ClusterClientLifecycle(loggerFactory);

            //set PropagateActivityId flag from node cofnig
            RequestContext.PropagateActivityId = clientMessagingOptions.Value.PropagateActivityId;

            // register all lifecycle participants
            IEnumerable <ILifecycleParticipant <IClusterClientLifecycle> > lifecycleParticipants = this.ServiceProvider.GetServices <ILifecycleParticipant <IClusterClientLifecycle> >();

            foreach (ILifecycleParticipant <IClusterClientLifecycle> participant in lifecycleParticipants)
            {
                participant?.Participate(clusterClientLifecycle);
            }

            // register all named lifecycle participants
            IKeyedServiceCollection <string, ILifecycleParticipant <IClusterClientLifecycle> > namedLifecycleParticipantCollections = this.ServiceProvider.GetService <IKeyedServiceCollection <string, ILifecycleParticipant <IClusterClientLifecycle> > >();

            foreach (ILifecycleParticipant <IClusterClientLifecycle> participant in namedLifecycleParticipantCollections
                     ?.GetServices(this.ServiceProvider)
                     ?.Select(s => s?.GetService(this.ServiceProvider)))
            {
                participant?.Participate(clusterClientLifecycle);
            }
        }
Exemplo n.º 5
0
        public Silo(ILocalSiloDetails siloDetails, IServiceProvider services)
        {
            string name = siloDetails.Name;

            // Temporarily still require this. Hopefuly gone when 2.0 is released.
            this.siloDetails       = siloDetails;
            this.SystemStatus      = SystemStatus.Creating;
            AsynchAgent.IsStarting = true; // todo. use ISiloLifecycle instead?

            var startTime = DateTime.UtcNow;

            IOptions <ClusterMembershipOptions> clusterMembershipOptions = services.GetRequiredService <IOptions <ClusterMembershipOptions> >();

            initTimeout = clusterMembershipOptions.Value.MaxJoinAttemptTime;
            if (Debugger.IsAttached)
            {
                initTimeout = StandardExtensions.Max(TimeSpan.FromMinutes(10), clusterMembershipOptions.Value.MaxJoinAttemptTime);
                stopTimeout = initTimeout;
            }

            var localEndpoint = this.siloDetails.SiloAddress.Endpoint;

            services.GetService <SerializationManager>().RegisterSerializers(services.GetService <IApplicationPartManager>());

            this.Services = services;
            this.Services.InitializeSiloUnobservedExceptionsHandler();
            //set PropagateActivityId flag from node config
            IOptions <SiloMessagingOptions> messagingOptions = services.GetRequiredService <IOptions <SiloMessagingOptions> >();

            RequestContext.PropagateActivityId = messagingOptions.Value.PropagateActivityId;
            this.loggerFactory = this.Services.GetRequiredService <ILoggerFactory>();
            logger             = this.loggerFactory.CreateLogger <Silo>();

            logger.Info(ErrorCode.SiloGcSetting, "Silo starting with GC settings: ServerGC={0} GCLatencyMode={1}", GCSettings.IsServerGC, Enum.GetName(typeof(GCLatencyMode), GCSettings.LatencyMode));
            if (!GCSettings.IsServerGC)
            {
                logger.Warn(ErrorCode.SiloGcWarning, "Note: Silo not running with ServerGC turned on - recommend checking app config : <configuration>-<runtime>-<gcServer enabled=\"true\">");
                logger.Warn(ErrorCode.SiloGcWarning, "Note: ServerGC only kicks in on multi-core systems (settings enabling ServerGC have no effect on single-core machines).");
            }

            logger.Info(ErrorCode.SiloInitializing, "-------------- Initializing silo on host {0} MachineName {1} at {2}, gen {3} --------------",
                        this.siloDetails.DnsHostName, Environment.MachineName, localEndpoint, this.siloDetails.SiloAddress.Generation);
            logger.Info(ErrorCode.SiloInitConfig, "Starting silo {0}", name);

            var siloMessagingOptions = this.Services.GetRequiredService <IOptions <SiloMessagingOptions> >();

            BufferPool.InitGlobalBufferPool(siloMessagingOptions.Value);

            try
            {
                grainFactory = Services.GetRequiredService <GrainFactory>();
            }
            catch (InvalidOperationException exc)
            {
                logger.Error(ErrorCode.SiloStartError, "Exception during Silo.Start, GrainFactory was not registered in Dependency Injection container", exc);
                throw;
            }

            // Performance metrics
            siloStatistics = Services.GetRequiredService <SiloStatisticsManager>();

            // The scheduler
            scheduler = Services.GetRequiredService <OrleansTaskScheduler>();
            healthCheckParticipants.Add(scheduler);

            runtimeClient = Services.GetRequiredService <InsideRuntimeClient>();

            // Initialize the message center
            messageCenter = Services.GetRequiredService <MessageCenter>();
            var dispatcher = this.Services.GetRequiredService <Dispatcher>();

            messageCenter.RerouteHandler       = dispatcher.RerouteMessage;
            messageCenter.SniffIncomingMessage = runtimeClient.SniffIncomingMessage;

            // Now the router/directory service
            // This has to come after the message center //; note that it then gets injected back into the message center.;
            localGrainDirectory = Services.GetRequiredService <LocalGrainDirectory>();

            // Now the activation directory.
            activationDirectory = Services.GetRequiredService <ActivationDirectory>();

            // Now the consistent ring provider
            RingProvider = Services.GetRequiredService <IConsistentRingProvider>();

            catalog = Services.GetRequiredService <Catalog>();

            executorService = Services.GetRequiredService <ExecutorService>();

            // Now the incoming message agents
            var messageFactory = this.Services.GetRequiredService <MessageFactory>();

            incomingSystemAgent = new IncomingMessageAgent(Message.Categories.System, messageCenter, activationDirectory, scheduler, catalog.Dispatcher, messageFactory, executorService, this.loggerFactory);
            incomingPingAgent   = new IncomingMessageAgent(Message.Categories.Ping, messageCenter, activationDirectory, scheduler, catalog.Dispatcher, messageFactory, executorService, this.loggerFactory);
            incomingAgent       = new IncomingMessageAgent(Message.Categories.Application, messageCenter, activationDirectory, scheduler, catalog.Dispatcher, messageFactory, executorService, this.loggerFactory);

            membershipOracle    = Services.GetRequiredService <IMembershipOracle>();
            this.clusterOptions = Services.GetRequiredService <IOptions <ClusterOptions> >().Value;
            var multiClusterOptions = Services.GetRequiredService <IOptions <MultiClusterOptions> >().Value;

            if (!multiClusterOptions.HasMultiClusterNetwork)
            {
                logger.Info("Skip multicluster oracle creation (no multicluster network configured)");
            }
            else
            {
                multiClusterOracle = Services.GetRequiredService <IMultiClusterOracle>();
            }

            this.SystemStatus      = SystemStatus.Created;
            AsynchAgent.IsStarting = false;

            StringValueStatistic.FindOrCreate(StatisticNames.SILO_START_TIME,
                                              () => LogFormatter.PrintDate(startTime)); // this will help troubleshoot production deployment when looking at MDS logs.

            this.siloLifecycle = this.Services.GetRequiredService <ISiloLifecycleSubject>();
            // register all lifecycle participants
            IEnumerable <ILifecycleParticipant <ISiloLifecycle> > lifecycleParticipants = this.Services.GetServices <ILifecycleParticipant <ISiloLifecycle> >();

            foreach (ILifecycleParticipant <ISiloLifecycle> participant in lifecycleParticipants)
            {
                participant?.Participate(this.siloLifecycle);
            }
            // register all named lifecycle participants
            IKeyedServiceCollection <string, ILifecycleParticipant <ISiloLifecycle> > namedLifecycleParticipantCollection = this.Services.GetService <IKeyedServiceCollection <string, ILifecycleParticipant <ISiloLifecycle> > >();

            foreach (ILifecycleParticipant <ISiloLifecycle> participant in namedLifecycleParticipantCollection
                     ?.GetServices(this.Services)
                     ?.Select(s => s.GetService(this.Services)))
            {
                participant?.Participate(this.siloLifecycle);
            }

            // add self to lifecycle
            this.Participate(this.siloLifecycle);

            logger.Info(ErrorCode.SiloInitializingFinished, "-------------- Started silo {0}, ConsistentHashCode {1:X} --------------", SiloAddress.ToLongString(), SiloAddress.GetConsistentHashCode());
        }
Exemplo n.º 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ClusterClient"/> class.
        /// </summary>
        /// <param name="runtimeClient">The runtime client.</param>
        /// <param name="loggerFactory">Logger factory used to create loggers</param>
        /// <param name="clientMessagingOptions">Messaging parameters</param>
        public ClusterClient(IRuntimeClient runtimeClient, ILoggerFactory loggerFactory, IOptions <ClientMessagingOptions> clientMessagingOptions)
        {
            this.runtimeClient          = runtimeClient;
            this.clusterClientLifecycle = new ClusterClientLifecycle(loggerFactory.CreateLogger <LifecycleSubject>());

            //set PropagateActivityId flag from node cofnig
            RequestContext.PropagateActivityId |= clientMessagingOptions.Value.PropagateActivityId;

            // register all lifecycle participants
            IEnumerable <ILifecycleParticipant <IClusterClientLifecycle> > lifecycleParticipants = this.ServiceProvider.GetServices <ILifecycleParticipant <IClusterClientLifecycle> >();

            foreach (ILifecycleParticipant <IClusterClientLifecycle> participant in lifecycleParticipants)
            {
                participant?.Participate(clusterClientLifecycle);
            }

            // register all named lifecycle participants
            IKeyedServiceCollection <string, ILifecycleParticipant <IClusterClientLifecycle> > namedLifecycleParticipantCollections = this.ServiceProvider.GetService <IKeyedServiceCollection <string, ILifecycleParticipant <IClusterClientLifecycle> > >();

            foreach (ILifecycleParticipant <IClusterClientLifecycle> participant in namedLifecycleParticipantCollections
                     ?.GetServices(this.ServiceProvider)
                     ?.Select(s => s?.GetService(this.ServiceProvider)))
            {
                participant?.Participate(clusterClientLifecycle);
            }

            // It is fine for this field to be null in the case that the client is not the host.
            this.applicationLifetime = runtimeClient.ServiceProvider.GetService <IHostApplicationLifetime>() as ClientApplicationLifetime;
        }
Exemplo n.º 7
0
        public Silo(ILocalSiloDetails siloDetails, IServiceProvider services)
        {
            string name = siloDetails.Name;

            // Temporarily still require this. Hopefuly gone when 2.0 is released.
            this.siloDetails  = siloDetails;
            this.SystemStatus = SystemStatus.Creating;

            var startTime = DateTime.UtcNow;

            IOptions <ClusterMembershipOptions> clusterMembershipOptions = services.GetRequiredService <IOptions <ClusterMembershipOptions> >();

            initTimeout = clusterMembershipOptions.Value.MaxJoinAttemptTime;
            if (Debugger.IsAttached)
            {
                initTimeout = StandardExtensions.Max(TimeSpan.FromMinutes(10), clusterMembershipOptions.Value.MaxJoinAttemptTime);
                stopTimeout = initTimeout;
            }

            var localEndpoint = this.siloDetails.SiloAddress.Endpoint;

            this.Services = services;

            //set PropagateActivityId flag from node config
            IOptions <SiloMessagingOptions> messagingOptions = services.GetRequiredService <IOptions <SiloMessagingOptions> >();

            RequestContext.PropagateActivityId       = messagingOptions.Value.PropagateActivityId;
            this.waitForMessageToBeQueuedForOutbound = messagingOptions.Value.WaitForMessageToBeQueuedForOutboundTime;

            this.loggerFactory = this.Services.GetRequiredService <ILoggerFactory>();
            logger             = this.loggerFactory.CreateLogger <Silo>();

            logger.Info(ErrorCode.SiloGcSetting, "Silo starting with GC settings: ServerGC={0} GCLatencyMode={1}", GCSettings.IsServerGC, Enum.GetName(typeof(GCLatencyMode), GCSettings.LatencyMode));
            if (!GCSettings.IsServerGC)
            {
                logger.Warn(ErrorCode.SiloGcWarning, "Note: Silo not running with ServerGC turned on - recommend checking app config : <configuration>-<runtime>-<gcServer enabled=\"true\">");
                logger.Warn(ErrorCode.SiloGcWarning, "Note: ServerGC only kicks in on multi-core systems (settings enabling ServerGC have no effect on single-core machines).");
            }

            if (logger.IsEnabled(LogLevel.Debug))
            {
                var highestLogLevel = logger.IsEnabled(LogLevel.Trace) ? nameof(LogLevel.Trace) : nameof(LogLevel.Debug);
                logger.LogWarning(
                    new EventId((int)ErrorCode.SiloGcWarning),
                    $"A verbose logging level ({highestLogLevel}) is configured. This will impact performance. The recommended log level is {nameof(LogLevel.Information)}.");
            }

            logger.Info(ErrorCode.SiloInitializing, "-------------- Initializing silo on host {0} MachineName {1} at {2}, gen {3} --------------",
                        this.siloDetails.DnsHostName, Environment.MachineName, localEndpoint, this.siloDetails.SiloAddress.Generation);
            logger.Info(ErrorCode.SiloInitConfig, "Starting silo {0}", name);

            try
            {
                grainFactory = Services.GetRequiredService <GrainFactory>();
            }
            catch (InvalidOperationException exc)
            {
                logger.Error(ErrorCode.SiloStartError, "Exception during Silo.Start, GrainFactory was not registered in Dependency Injection container", exc);
                throw;
            }

            // Performance metrics
            siloStatistics = Services.GetRequiredService <SiloStatisticsManager>();

            runtimeClient = Services.GetRequiredService <InsideRuntimeClient>();

            // Initialize the message center
            messageCenter = Services.GetRequiredService <MessageCenter>();
            messageCenter.SniffIncomingMessage = runtimeClient.SniffIncomingMessage;

            // Now the router/directory service
            // This has to come after the message center //; note that it then gets injected back into the message center.;
            localGrainDirectory = Services.GetRequiredService <LocalGrainDirectory>();

            // Now the consistent ring provider
            RingProvider = Services.GetRequiredService <IConsistentRingProvider>();

            catalog = Services.GetRequiredService <Catalog>();

            siloStatusOracle       = Services.GetRequiredService <ISiloStatusOracle>();
            this.membershipService = Services.GetRequiredService <IMembershipService>();

            this.SystemStatus = SystemStatus.Created;

            StringValueStatistic.FindOrCreate(StatisticNames.SILO_START_TIME,
                                              () => LogFormatter.PrintDate(startTime)); // this will help troubleshoot production deployment when looking at MDS logs.

            this.siloLifecycle = this.Services.GetRequiredService <ISiloLifecycleSubject>();
            // register all lifecycle participants
            IEnumerable <ILifecycleParticipant <ISiloLifecycle> > lifecycleParticipants = this.Services.GetServices <ILifecycleParticipant <ISiloLifecycle> >();

            foreach (ILifecycleParticipant <ISiloLifecycle> participant in lifecycleParticipants)
            {
                participant?.Participate(this.siloLifecycle);
            }
            // register all named lifecycle participants
            IKeyedServiceCollection <string, ILifecycleParticipant <ISiloLifecycle> > namedLifecycleParticipantCollection = this.Services.GetService <IKeyedServiceCollection <string, ILifecycleParticipant <ISiloLifecycle> > >();

            foreach (ILifecycleParticipant <ISiloLifecycle> participant in namedLifecycleParticipantCollection
                     ?.GetServices(this.Services)
                     ?.Select(s => s.GetService(this.Services)))
            {
                participant?.Participate(this.siloLifecycle);
            }

            // add self to lifecycle
            this.Participate(this.siloLifecycle);

            logger.Info(ErrorCode.SiloInitializingFinished, "-------------- Started silo {0}, ConsistentHashCode {1:X} --------------", SiloAddress.ToLongString(), SiloAddress.GetConsistentHashCode());
        }
Exemplo n.º 8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ClusterClient"/> class.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        /// <param name="runtimeClient">The runtime client.</param>
        /// <param name="loggerFactory">Logger factory used to create loggers</param>
        /// <param name="clientMessagingOptions">Messaging parameters</param>
        public ClusterClient(IServiceProvider serviceProvider, OutsideRuntimeClient runtimeClient, ILoggerFactory loggerFactory, IOptions <ClientMessagingOptions> clientMessagingOptions)
        {
            ValidateSystemConfiguration(serviceProvider);

            _runtimeClient          = runtimeClient;
            _logger                 = loggerFactory.CreateLogger <ClusterClient>();
            _clusterClientLifecycle = new ClusterClientLifecycle(_logger);

            //set PropagateActivityId flag from node config
            RequestContext.PropagateActivityId |= clientMessagingOptions.Value.PropagateActivityId;

            // register all lifecycle participants
            IEnumerable <ILifecycleParticipant <IClusterClientLifecycle> > lifecycleParticipants = ServiceProvider.GetServices <ILifecycleParticipant <IClusterClientLifecycle> >();

            foreach (var participant in lifecycleParticipants)
            {
                participant?.Participate(_clusterClientLifecycle);
            }

            // register all named lifecycle participants
            IKeyedServiceCollection <string, ILifecycleParticipant <IClusterClientLifecycle> > namedLifecycleParticipantCollections = ServiceProvider.GetService <IKeyedServiceCollection <string, ILifecycleParticipant <IClusterClientLifecycle> > >();

            foreach (var participant in namedLifecycleParticipantCollections
                     ?.GetServices(ServiceProvider)
                     ?.Select(s => s?.GetService(ServiceProvider)))
            {
                participant?.Participate(_clusterClientLifecycle);
            }