Beispiel #1
0
        public bool ContainsNode(RedisManagedNode node)
        {
            if (node != null)
            {
                lock (m_SyncRoot)
                {
                    var nodes = m_Nodes;

                    var length = (nodes != null) ? nodes.Length : 0;
                    if (length == 0)
                    {
                        return(false);
                    }

                    return(nodes.Contains(node));
                }
            }
            return(false);
        }
Beispiel #2
0
        public virtual bool RemoveNode(RedisManagedNode node)
        {
            if (node != null)
            {
                lock (m_SyncRoot)
                {
                    var nodes = m_Nodes;
                    if (nodes != null)
                    {
                        var length = nodes.Length;
                        if (length > 0 && nodes.Contains(node))
                        {
                            if (length == 1)
                            {
                                Interlocked.Exchange(ref m_NodeIndex, -1);
                                Interlocked.Exchange(ref m_Nodes, new RedisManagedNode[0]);
                                return(true);
                            }

                            var newNodes = new RedisManagedNode[length - 1];

                            var index = 0;
                            for (var i = 0; i < length; i++)
                            {
                                var groupNode = nodes[i];
                                if (groupNode == node)
                                {
                                    continue;
                                }

                                newNodes[index++] = groupNode;
                            }

                            Interlocked.Exchange(ref m_Nodes, newNodes);
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Beispiel #3
0
        public virtual bool AppendNode(RedisManagedNode node)
        {
            if (node != null)
            {
                lock (m_SyncRoot)
                {
                    var nodes = m_Nodes;

                    var length = (nodes != null) ? nodes.Length : 0;
                    if (length == 0)
                    {
                        node.Role = Role;
                        Interlocked.Exchange(ref m_Nodes, new[] { node });
                        Interlocked.Exchange(ref m_NodeIndex, 0);
                        return(true);
                    }

                    if (!nodes.Contains(node))
                    {
                        var isDown   = node.IsClosed;
                        var newNodes = new RedisManagedNode[length + 1];

                        var index = isDown ? 0 : 1;
                        newNodes[!isDown ? 0 : length] = node;

                        for (var i = 0; i < length; i++)
                        {
                            newNodes[index++] = nodes[i];
                        }

                        node.Role = Role;
                        Interlocked.Exchange(ref m_Nodes, newNodes);
                        return(true);
                    }
                }
            }
            return(false);
        }
Beispiel #4
0
        public bool ChangeGroup(RedisManagedNode node)
        {
            if (!Disposed && node.IsAlive())
            {
                lock (m_SyncRoot)
                {
                    var slaves  = m_Slaves;
                    var masters = m_Masters;

                    if (slaves.IsAlive() && masters.IsAlive())
                    {
                        if (masters.ContainsNode(node))
                        {
                            node.Role = RedisRole.Slave;
                            if (!masters.RemoveNode(node))
                            {
                                node.IsClosed = true;
                            }
                            else
                            {
                                if (slaves.AppendNode(node))
                                {
                                    return(true);
                                }

                                node.Dispose();
                            }
                        }
                        else if (slaves.ContainsNode(node))
                        {
                            node.Role = RedisRole.Slave;
                            if (!slaves.RemoveNode(node))
                            {
                                node.IsClosed = true;
                            }
                            else
                            {
                                var oldMasters = masters.Nodes;
                                if (!masters.AppendNode(node))
                                {
                                    node.Dispose();
                                }
                                else
                                {
                                    try
                                    {
                                        node.Role = RedisRole.Master;

                                        if (!oldMasters.IsEmpty())
                                        {
                                            var length = oldMasters.Length;
                                            var nodes  = new RedisManagedNode[length];

                                            Array.Copy(oldMasters, nodes, length);

                                            foreach (var oldNode in nodes)
                                            {
                                                if (!(ReferenceEquals(oldNode, null) ||
                                                      ReferenceEquals(oldNode, node)))
                                                {
                                                    oldNode.IsClosed = true;
                                                    oldNode.Role     = RedisRole.Slave;

                                                    if (masters.RemoveNode(oldNode) && oldNode.IsAlive())
                                                    {
                                                        slaves.AppendNode(node);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    catch (Exception)
                                    { }

                                    return(true);
                                }
                            }
                        }
                    }
                }
            }
            return(false);
        }