Пример #1
0
        /// <summary>
        /// Sets the host list to be used during failover operations.
        /// </summary>
        /// <param name="hostList">The host list.</param>
        /// <param name="failoverMethod">The failover method.</param>
        internal static void SetHostList(List <XServer> hostList, FailoverMethod failoverMethod)
        {
            switch (failoverMethod)
            {
            case FailoverMethod.Sequential:
                FailoverGroup = new SequentialFailoverGroup(hostList);
                break;

            case FailoverMethod.Priority:
                FailoverGroup = new SequentialFailoverGroup(hostList.OrderByDescending(o => o.Priority).ToList());
                break;
            }
        }
Пример #2
0
        /// <summary>
        /// Sets the host list to be used during failover operations.
        /// </summary>
        /// <param name="hostList">The host list.</param>
        /// <param name="failoverMethod">The failover method.</param>
        internal static void SetHostList(List <FailoverServer> hostList, FailoverMethod failoverMethod)
        {
            if (FailoverGroup != null)
            {
                return;
            }

            switch (failoverMethod)
            {
            case FailoverMethod.Sequential:
                FailoverGroup = new SequentialFailoverGroup(hostList);
                break;

            case FailoverMethod.Priority:
                FailoverGroup = new SequentialFailoverGroup(hostList.OrderByDescending(o => o.Priority).ToList());
                break;

            case FailoverMethod.Random:
                FailoverGroup = new RandomFailoverGroup(hostList);
                break;
            }
        }
Пример #3
0
        /// <summary>
        /// Initializes the <see cref="FailoverManager"/> if more than one host is found.
        /// </summary>
        /// <param name="hierPart">A string containing an unparsed list of hosts.</param>
        /// <param name="connectionDataIsUri"><c>true</c> if the connection data is a URI; otherwise <c>false</c>.</param>
        /// <returns>The number of hosts found, -1 if an error was raised during parsing.</returns>
        private int ParseHostList(string hierPart, bool connectionDataIsUri)
        {
            if (string.IsNullOrWhiteSpace(hierPart))
            {
                return(-1);
            }

            int            hostCount      = -1;
            FailoverMethod failoverMethod = FailoverMethod.Sequential;

            string[]       hostArray = null;
            List <XServer> hostList  = new List <XServer>();

            hierPart = hierPart.Replace(" ", "");

            if (!hierPart.StartsWith("(") && !hierPart.EndsWith(")"))
            {
                hostArray = hierPart.Split(',');
                foreach (var host in hostArray)
                {
                    if (IsUnixSocket(host))
                    {
                        hostList.Add(new XServer(NormalizeUnixSocket(host), -1, -1));
                    }
                    else
                    {
                        hostList.Add(this.ConvertToXServer(host, connectionDataIsUri));
                    }
                }

                if (hostArray.Length == 1)
                {
                    return(1);
                }

                hostCount = hostArray.Length;
            }
            else
            {
                string[] groups          = hierPart.Split(new string[] { "),(" }, StringSplitOptions.RemoveEmptyEntries);
                bool?    allHavePriority = null;
                int      defaultPriority = 100;
                foreach (var group in groups)
                {
                    // Remove leading parenthesis.
                    var normalizedGroup = group;
                    if (normalizedGroup.StartsWith("("))
                    {
                        normalizedGroup = group.Substring(1);
                    }

                    if (normalizedGroup.EndsWith(")"))
                    {
                        normalizedGroup = normalizedGroup.Substring(0, normalizedGroup.Length - 1);
                    }

                    string[] items         = normalizedGroup.Split(',');
                    string[] keyValuePairs = items[0].Split(CONNECTION_DATA_VALUE_SEPARATOR);
                    if (keyValuePairs[0].ToLowerInvariant() != "address")
                    {
                        throw new KeyNotFoundException(string.Format(ResourcesX.KeywordNotFound, "address"));
                    }

                    string host = keyValuePairs[1];
                    if (string.IsNullOrWhiteSpace(host))
                    {
                        throw new ArgumentNullException(SERVER_CONNECTION_OPTION_KEYWORD);
                    }

                    if (items.Length == 2)
                    {
                        if (allHavePriority != null && allHavePriority == false)
                        {
                            throw new ArgumentException(ResourcesX.PriorityForAllOrNoHosts);
                        }

                        allHavePriority = allHavePriority ?? true;
                        keyValuePairs   = items[1].Split('=');
                        if (keyValuePairs[0].ToLowerInvariant() != "priority")
                        {
                            throw new KeyNotFoundException(string.Format(ResourcesX.KeywordNotFound, "priority"));
                        }

                        if (string.IsNullOrWhiteSpace(keyValuePairs[1]))
                        {
                            throw new ArgumentNullException("priority");
                        }

                        int priority = -1;
                        Int32.TryParse(keyValuePairs[1], out priority);
                        if (priority < 0 || priority > 100)
                        {
                            throw new ArgumentException(ResourcesX.PriorityOutOfLimits);
                        }

                        hostList.Add(ConvertToXServer(IsUnixSocket(host) ? NormalizeUnixSocket(host) : host, connectionDataIsUri, priority));
                    }
                    else
                    {
                        if (allHavePriority != null && allHavePriority == true)
                        {
                            throw new ArgumentException(ResourcesX.PriorityForAllOrNoHosts);
                        }

                        allHavePriority = allHavePriority ?? false;

                        hostList.Add(ConvertToXServer(host, connectionDataIsUri, defaultPriority > 0 ? defaultPriority-- : 0));
                    }
                }

                hostCount      = groups.Length;
                failoverMethod = FailoverMethod.Priority;
            }

            FailoverManager.SetHostList(hostList, failoverMethod);
            return(hostCount);
        }
Пример #4
0
        /// <summary>
        /// Creates a <see cref="FailoverGroup"/> if more than one host is found.
        /// </summary>
        /// <param name="hierPart">A string containing an unparsed list of hosts.</param>
        /// <param name="isXProtocol"><c>true</c> if the connection is X Protocol; otherwise <c>false</c>.</param>
        /// <param name="connectionDataIsUri"><c>true</c> if the connection data is a URI; otherwise <c>false</c>.</param>
        /// <returns>The number of hosts found, -1 if an error was raised during parsing.</returns>
        internal static int ParseHostList(string hierPart, bool isXProtocol, bool connectionDataIsUri = true)
        {
            if (string.IsNullOrWhiteSpace(hierPart))
            {
                return(-1);
            }

            int            hostCount      = -1;
            FailoverMethod failoverMethod = FailoverMethod.Random;

            string[] hostArray             = null;
            List <FailoverServer> hostList = new List <FailoverServer>();

            hierPart = hierPart.Replace(" ", "");

            if (!hierPart.StartsWith("(") && !hierPart.EndsWith(")"))
            {
                hostArray = hierPart.Split(',');
                if (hostArray.Length == 1)
                {
                    return(1);
                }

                foreach (var host in hostArray)
                {
                    hostList.Add(ConvertToFailoverServer(host, connectionDataIsUri: connectionDataIsUri));
                }

                hostCount = hostArray.Length;
            }
            else
            {
                string[] groups          = hierPart.Split(new string[] { "),(" }, StringSplitOptions.RemoveEmptyEntries);
                bool?    allHavePriority = null;
                int      defaultPriority = -1;
                foreach (var group in groups)
                {
                    // Remove leading parenthesis.
                    var normalizedGroup = group;
                    if (normalizedGroup.StartsWith("("))
                    {
                        normalizedGroup = group.Substring(1);
                    }

                    if (normalizedGroup.EndsWith(")"))
                    {
                        normalizedGroup = normalizedGroup.Substring(0, normalizedGroup.Length - 1);
                    }

                    string[] items         = normalizedGroup.Split(',');
                    string[] keyValuePairs = items[0].Split('=');
                    if (keyValuePairs[0].ToLowerInvariant() != "address")
                    {
                        throw new KeyNotFoundException(string.Format(ResourcesX.KeywordNotFound, "address"));
                    }

                    string host = keyValuePairs[1];
                    if (string.IsNullOrWhiteSpace(host))
                    {
                        throw new ArgumentNullException("server");
                    }

                    if (items.Length == 2)
                    {
                        if (allHavePriority != null && allHavePriority == false)
                        {
                            throw new ArgumentException(ResourcesX.PriorityForAllOrNoHosts);
                        }

                        allHavePriority = allHavePriority ?? true;
                        keyValuePairs   = items[1].Split('=');
                        if (keyValuePairs[0].ToLowerInvariant() != "priority")
                        {
                            throw new KeyNotFoundException(string.Format(ResourcesX.KeywordNotFound, "priority"));
                        }

                        if (string.IsNullOrWhiteSpace(keyValuePairs[1]))
                        {
                            throw new ArgumentNullException("priority");
                        }

                        int priority = -1;
                        Int32.TryParse(keyValuePairs[1], out priority);
                        if (priority < 0 || priority > 100)
                        {
                            throw new ArgumentException(ResourcesX.PriorityOutOfLimits);
                        }

                        if (isXProtocol)
                        {
                            hostList.Add(ConvertToFailoverServer(BaseSession.IsUnixSocket(host) ? BaseSession.NormalizeUnixSocket(host) : host, priority, connectionDataIsUri: connectionDataIsUri));
                        }
                        else
                        {
                            hostList.Add(ConvertToFailoverServer(host, priority));
                        }
                    }
                    else
                    {
                        if (allHavePriority != null && allHavePriority == true)
                        {
                            throw new ArgumentException(ResourcesX.PriorityForAllOrNoHosts);
                        }

                        allHavePriority = allHavePriority ?? false;

                        hostList.Add(ConvertToFailoverServer(host, defaultPriority, connectionDataIsUri: connectionDataIsUri));
                    }
                }

                hostCount = groups.Length;
                if (hostList.GroupBy(h => h.Priority).ToList().Count > 1)
                {
                    failoverMethod = FailoverMethod.Priority;
                }
                else
                {
                    failoverMethod = FailoverMethod.Random;
                }
            }

            SetHostList(hostList, failoverMethod);
            return(hostCount);
        }