Exemplo n.º 1
0
        internal HystrixCommandBase(string instanceKey, string commandKey, string groupKey, string domain, Action <ICommandConfigSet> config)
        {
            _instanceKey = string.IsNullOrWhiteSpace(instanceKey) ? null : instanceKey.Trim();
            _commandKey  = string.IsNullOrWhiteSpace(commandKey) ? null : commandKey.Trim();
            _groupKey    = string.IsNullOrWhiteSpace(groupKey) ? DefaultGroupKey : groupKey.Trim();
            _domain      = string.IsNullOrWhiteSpace(domain) ? CommandDomains.Default : domain.Trim();
            _config      = config;

            Type type = this.GetType();

            if (string.IsNullOrWhiteSpace(Key))
            {
                if (string.IsNullOrWhiteSpace(CommandKey))
                {
                    if (string.IsNullOrWhiteSpace(_commandKey))
                    {
                        if (type.IsGenericType)
                        {
                            throw new ArgumentNullException("CommandKey cannot be null.");
                        }

                        _commandKey = TypePredefinedKeyMappings.GetOrAdd(type, t => CommonUtils.GenerateTypeKey(t));
                    }
                }
            }

            _key = CommonUtils.GenerateKey(InstanceKey, CommandKey);

            CommandComponents commandComponents = CommandComponentsCollection.GetOrAdd(Key, key =>
                                                                                       CreateCommandComponents(key, InstanceKey, CommandKey, GroupKey, Domain, IsolationMode, Config, type));

            if (commandComponents.IsolationMode != IsolationMode)
            {
                string message = "The key " + Key + " has been used for " + commandComponents.IsolationMode + ". Now it cannot be used for " + IsolationMode + ".";
                CommonUtils.Log.Log(LogLevelEnum.Fatal, message, new Dictionary <string, string>().AddLogTagData("FXD303009"));
                throw new ArgumentException(message);
            }

            CircuitBreaker = commandComponents.CircuitBreaker;
            Log            = commandComponents.Log;
            ConfigSet      = commandComponents.ConfigSet;
            Metrics        = commandComponents.Metrics;

            _groupKey    = commandComponents.CommandInfo.GroupKey;
            _commandKey  = commandComponents.CommandInfo.CommandKey;
            _instanceKey = commandComponents.CommandInfo.InstanceKey;
            _domain      = commandComponents.CommandInfo.Domain;
        }
Exemplo n.º 2
0
        internal HystrixCommandBase(string instanceKey, string commandKey, string groupKey, string domain, Action <ICommandConfigSet> config)
        {
            this.ExecutionLock = new object();
            this._instanceKey  = string.IsNullOrWhiteSpace(instanceKey) ? null : instanceKey.Trim();
            this._commandKey   = string.IsNullOrWhiteSpace(commandKey) ? null : commandKey.Trim();
            this._groupKey     = string.IsNullOrWhiteSpace(groupKey) ? "DefaultGroup" : groupKey.Trim();
            this._domain       = string.IsNullOrWhiteSpace(domain) ? "Ant" : domain.Trim();
            this._config       = config;
            Type type = base.GetType();

            if ((string.IsNullOrWhiteSpace(this.Key) && string.IsNullOrWhiteSpace(this.CommandKey)) && string.IsNullOrWhiteSpace(this._commandKey))
            {
                if (type.IsGenericType)
                {
                    throw new ArgumentNullException("CommandKey cannot be null.");
                }
                this._commandKey = TypePredefinedKeyMappings.GetOrAdd(type, t => CommonUtils.GenerateTypeKey(t));
            }
            this._key = CommonUtils.GenerateKey(this.InstanceKey, this.CommandKey);
            CommandComponents orAdd = CommandComponentsCollection.GetOrAdd(this.Key, key => CreateCommandComponents(key, this.InstanceKey, this.CommandKey, this.GroupKey, this.Domain, this.IsolationMode, new Action <ICommandConfigSet>(this.Config), type));

            if (orAdd.IsolationMode != this.IsolationMode)
            {
                string message = string.Concat(new object[] { "The key ", this.Key, " has been used for ", orAdd.IsolationMode, ". Now it cannot be used for ", this.IsolationMode, "." });
                CommonUtils.Log.Log(LogLevelEnum.Fatal, message, new Dictionary <string, string>().AddLogTagData("FXD303009"));
                throw new ArgumentException(message);
            }
            this.CircuitBreaker = orAdd.CircuitBreaker;
            this.Log            = orAdd.Log;
            this.ConfigSet      = orAdd.ConfigSet;
            this.Metrics        = orAdd.Metrics;
            this._groupKey      = orAdd.CommandInfo.GroupKey;
            this._commandKey    = orAdd.CommandInfo.CommandKey;
            this._instanceKey   = orAdd.CommandInfo.InstanceKey;
            this._domain        = orAdd.CommandInfo.Domain;
        }
Exemplo n.º 3
0
        internal static CommandComponents CreateCommandComponents(string key, string instanceKey, string commandKey, string groupKey, string domain, IsolationModeEnum isolationMode, Action <ICommandConfigSet> config, Type type)
        {
            if (!key.IsValidHystrixName())
            {
                string message = "Hystrix command key has invalid char: " + key + @". Name pattern is: ^[a-zA-Z0-9][a-zA-Z0-9\-_.]*[a-zA-Z0-9]$";
                CommonUtils.Log.Log(LogLevelEnum.Fatal, message, new Dictionary <string, string>().AddLogTagData("FXD303004"));
                throw new ArgumentException(message);
            }
            if (!string.IsNullOrWhiteSpace(instanceKey) && !instanceKey.IsValidHystrixName())
            {
                string str2 = "Hystrix command instanceKey has invalid char: " + instanceKey + @". Name pattern is: ^[a-zA-Z0-9][a-zA-Z0-9\-_.]*[a-zA-Z0-9]$";
                CommonUtils.Log.Log(LogLevelEnum.Fatal, str2, new Dictionary <string, string>().AddLogTagData("FXD303004"));
                throw new ArgumentException(str2);
            }
            if (!commandKey.IsValidHystrixName())
            {
                string str3 = "Hystrix command commandKey has invalid char: " + commandKey + @". Name pattern is: ^[a-zA-Z0-9][a-zA-Z0-9\-_.]*[a-zA-Z0-9]$";
                CommonUtils.Log.Log(LogLevelEnum.Fatal, str3, new Dictionary <string, string>().AddLogTagData("FXD303004"));
                throw new ArgumentException(str3);
            }
            if (!groupKey.IsValidHystrixName())
            {
                string str4 = "Hystrix command group commandKey has invalid char: " + groupKey + @". Name pattern is: ^[a-zA-Z0-9][a-zA-Z0-9\-_.]*[a-zA-Z0-9]$";
                CommonUtils.Log.Log(LogLevelEnum.Fatal, str4, new Dictionary <string, string>().AddLogTagData("FXD303005"));
                throw new ArgumentException(str4);
            }
            if (!domain.IsValidHystrixName())
            {
                string str5 = "Hystrix domain has invalid char: " + domain + @". Name pattern is: ^[a-zA-Z0-9][a-zA-Z0-9\-_.]*[a-zA-Z0-9]$";
                CommonUtils.Log.Log(LogLevelEnum.Fatal, str5, new Dictionary <string, string>().AddLogTagData("FXD303006"));
                throw new ArgumentException(str5);
            }
            if (CommandCount >= MaxCommandCount)
            {
                string str6 = "Hystrix command count has reached the limit: " + MaxCommandCount;
                CommonUtils.Log.Log(LogLevelEnum.Fatal, str6, new Dictionary <string, string>().AddLogTagData("FXD303007"));
                throw new ArgumentException(str6);
            }
            CommandCount.IncrementAndGet();
            ICommandConfigSet configSet = ComponentFactory.CreateCommandConfigSet(isolationMode);

            configSet.SubcribeConfigChangeEvent(delegate(ICommandConfigSet c) {
                OnConfigChanged(key, c);
            });
            try
            {
                if (config != null)
                {
                    config(configSet);
                }
            }
            catch (Exception exception)
            {
                CommonUtils.Log.Log(LogLevelEnum.Fatal, "Failed to config command: " + key, exception, new Dictionary <string, string>().AddLogTagData("FXD303008"));
            }
            ICommandMetrics   metrics    = ComponentFactory.CreateCommandMetrics(configSet, key, isolationMode);
            CommandComponents components = new CommandComponents {
                ConfigSet      = configSet,
                Metrics        = metrics,
                CircuitBreaker = ComponentFactory.CreateCircuitBreaker(configSet, metrics)
            };
            CommandInfo info = new CommandInfo {
                Domain      = domain.ToLower(),
                GroupKey    = groupKey.ToLower(),
                CommandKey  = commandKey.ToLower(),
                InstanceKey = (instanceKey == null) ? null : instanceKey.ToLower(),
                Key         = key.ToLower(),
                Type        = isolationMode.ToString()
            };

            components.CommandInfo   = info;
            components.Log           = ComponentFactory.CreateLog(configSet, type);
            components.IsolationMode = isolationMode;
            return(components);
        }