示例#1
0
        public IMetricsCollector Build(ActorSystem system)
        {
            var log      = Logging.GetLogger(system, GetType());
            var settings = ClusterMetricsSettings.Create(system.Settings.Config);

            var collectorCustom  = settings.CollectorProvider;
            var defaultCollector = typeof(DefaultCollector).AssemblyQualifiedName;

            var useCustom   = !settings.CollectorFallback;
            var useInternal = settings.CollectorFallback && string.IsNullOrEmpty(settings.CollectorProvider);

            Try <IMetricsCollector> Create(string provider)
            {
                log.Debug("Trying {0}", provider);
                return(DynamicAccess.CreateInstanceFor <IMetricsCollector>(provider, system));
            }

            Try <IMetricsCollector> collector;

            if (useCustom)
            {
                collector = Create(collectorCustom);
            }
            else if (useInternal)
            {
                collector = Create(defaultCollector);
            }
            else // Use complete fall back chain.
            {
                collector = Create(collectorCustom).OrElse(Create(defaultCollector));
            }

            return(collector.Recover(ex => throw new ConfigurationException($"Could not create metrics collector: {ex}")).Get());
        }
示例#2
0
        private ServiceDiscovery CreateServiceDiscovery(string method)
        {
            var config = _system.Settings.Config;

            string ClassNameFromConfig(string path) => config.HasPath(path)
                ? config.GetString(path)
                : throw new ArgumentException($"{path} must contain field `class` that is a FQN of an `Akka.Discovery.ServiceDiscovery` implementation");

            Try <ServiceDiscovery> Create(string typeName)
            {
                var dynamic = DynamicAccess.CreateInstanceFor <ServiceDiscovery>(typeName, _system);

                return(dynamic.RecoverWith(ex => ex is TypeLoadException || ex is MissingMethodException
                    ? DynamicAccess.CreateInstanceFor <ServiceDiscovery>(typeName)
                    : dynamic));
            }

            var configName  = $"akka.discovery.{method}.class";
            var instanceTry = Create(ClassNameFromConfig(configName));

            return(instanceTry.IsSuccess switch
            {
                true => instanceTry.Get(),
                false when instanceTry.Failure.Value is TypeLoadException || instanceTry.Failure.Value is MissingMethodException =>
                throw new ArgumentException(nameof(method), $"Illegal {configName} value or incompatible class! \n" +
                                            "The implementation class MUST extend Akka.Discovery.ServiceDiscovery and take an \n" +
                                            "ExtendedActorSystem as constructor argument."),
                _ => throw instanceTry.Failure.Value
            });
示例#3
0
        /// <summary>
        /// Builds <see cref="IMetricsSelector"/> defined in configuration
        /// </summary>
        /// <returns></returns>
        public static IMetricsSelector BuildFromConfig(Config config)
        {
            if (config.IsNullOrEmpty())
            {
                throw ConfigurationException.NullOrEmptyConfig <IMetricsSelector>();
            }

            var selectorTypeName = config.GetString("metrics-selector");

            switch (selectorTypeName)
            {
            case "mix": return(MixMetricsSelector.Instance);

            case "memory": return(MemoryMetricsSelector.Instance);

            case "cpu": return(CpuMetricsSelector.Instance);

            default:
                return(DynamicAccess.CreateInstanceFor <IMetricsSelector>(selectorTypeName, config)
                       .Recover(ex => throw new ArgumentException($"Cannot instantiate metrics-selector [{selectorTypeName}]," +
                                                                  $"make sure it extends [Akka.Cluster.Metrics.MetricsSelector] and " +
                                                                  $"has constructor with [Akka.Configuration.Config] parameter", ex))
                       .Get());
            }
        }
示例#4
0
        private ServiceDiscovery CreateServiceDiscovery(string method)
        {
            var config = _system.Settings.Config.GetConfig($"akka.discovery.{method}");

            if (config is null)
            {
                throw new ArgumentException($"Could not load discovery config from path [akka.discovery.{method}]");
            }
            if (!config.HasPath("class"))
            {
                throw new ArgumentException($"akka.discovery.{method} must contain field `class` that is a FQN of an `Akka.Discovery.ServiceDiscovery` implementation");
            }

            Try <ServiceDiscovery> Create(string typeName)
            {
                var dynamic = DynamicAccess.CreateInstanceFor <ServiceDiscovery>(typeName, _system);

                return(dynamic.RecoverWith(ex => ex is TypeLoadException || ex is MissingMethodException
                    ? DynamicAccess.CreateInstanceFor <ServiceDiscovery>(typeName)
                    : dynamic));
            }

            var className = config.GetString("class");

            _log.Info($"Starting Discovery service using [{method}] method, class: [{className}]");
            var instanceTry = Create(className);

            return(instanceTry.IsSuccess switch
            {
                true => instanceTry.Get(),
                false when instanceTry.Failure.Value is TypeLoadException || instanceTry.Failure.Value is MissingMethodException =>
                throw new ArgumentException(nameof(method), $"Illegal akka.discovery.{method}.class value or incompatible class! \n" +
                                            "The implementation class MUST extend Akka.Discovery.ServiceDiscovery and take an \n" +
                                            "ExtendedActorSystem as constructor argument."),
                _ => throw instanceTry.Failure.Value
            });