Exemplo n.º 1
0
 public RedisNodeInfo(RedisEndPoint endPoint, RedisRole role, string name = null)
     : this()
 {
     Role     = role;
     Name     = name ?? String.Empty;
     EndPoint = endPoint ?? RedisEndPoint.Empty;
 }
Exemplo n.º 2
0
        private NodeRoleAndSiblings GetSlavesOfMaster(RedisServerInfo serverInfo)
        {
            if (serverInfo != null)
            {
                var replicationSection = serverInfo.Replication;
                if (replicationSection != null)
                {
                    var slaves = replicationSection.Slaves;
                    if (slaves != null)
                    {
                        var slaveEndPoints = new List <RedisEndPoint>();

                        foreach (var slave in slaves)
                        {
                            try
                            {
                                if (slave.Port.HasValue && !slave.IPAddress.IsEmpty())
                                {
                                    var endPoint = new RedisEndPoint(slave.IPAddress, slave.Port.Value);
                                    slaveEndPoints.Add(endPoint);
                                }
                            }
                            catch (Exception)
                            { }
                        }

                        return(new NodeRoleAndSiblings(RedisRole.Master, slaveEndPoints.ToArray()));
                    }
                }
            }
            return(null);
        }
Exemplo n.º 3
0
        public bool SetMasterIsDown(RedisEndPoint endPoint)
        {
            if (!Disposed && !endPoint.IsEmpty())
            {
                lock (m_SyncRoot)
                {
                    var masters = m_Masters;
                    if (masters.IsAlive())
                    {
                        var masterNodes = masters.Nodes;
                        if (masterNodes != null)
                        {
                            var masterNode = masterNodes.FirstOrDefault(n => n.IsAlive() && n.EndPoint == endPoint);
                            if (masterNode.IsAlive())
                            {
                                if (!masterNode.IsClosed)
                                {
                                    masterNode.IsClosed = true;
                                }

                                ChangeGroup(masterNode);
                                return(true);
                            }
                        }
                    }
                }
            }
            return(false);
        }
Exemplo n.º 4
0
        private NodeRoleAndSiblings GetMastersOfSentinel(string masterName, RedisServerInfo serverInfo)
        {
            if (serverInfo != null)
            {
                var sentinelSection = serverInfo.Sentinel;
                if (sentinelSection != null)
                {
                    var masters = sentinelSection.Masters;
                    if (masters != null)
                    {
                        var mastersLength = masters.Length;
                        if (mastersLength > 0)
                        {
                            if (masterName.IsEmpty())
                            {
                                if (mastersLength == 1)
                                {
                                    var master = masters[0];
                                    try
                                    {
                                        if (master.Port.HasValue && !master.IPAddress.IsEmpty())
                                        {
                                            var endPoint = new RedisEndPoint(master.IPAddress, master.Port.Value);
                                            return(new NodeRoleAndSiblings(RedisRole.Sentinel, new[] { endPoint }));
                                        }
                                    }
                                    catch (Exception)
                                    { }
                                }
                                return(null);
                            }

                            var masterEndPoints = new List <RedisEndPoint>();

                            foreach (var master in masters)
                            {
                                try
                                {
                                    if (master.Name == masterName &&
                                        master.Port.HasValue && !master.IPAddress.IsEmpty())
                                    {
                                        var endPoint = new RedisEndPoint(master.IPAddress, master.Port.Value);
                                        masterEndPoints.Add(endPoint);
                                    }
                                }
                                catch (Exception)
                                { }
                            }

                            return(new NodeRoleAndSiblings(RedisRole.Sentinel, masterEndPoints.ToArray()));
                        }
                    }
                }
            }
            return(null);
        }
Exemplo n.º 5
0
        protected RedisManagedNode(RedisManagerSettings settings, RedisRole role, object seed,
                                   Action <object, RedisCardioPulseStatus> onPulseStateChange, bool ownsSeed = true)
        {
            m_Seed     = seed;
            m_Role     = role;
            m_OwnsSeed = ownsSeed;
            m_EndPoint = RedisEndPoint.Empty;

            m_Settings           = settings;
            m_OnPulseStateChange = onPulseStateChange;
        }
Exemplo n.º 6
0
 public RedisManagedNode FindNodeOf(RedisEndPoint endPoint)
 {
     if (!(Disposed || endPoint.IsEmpty()))
     {
         var nodes = m_Nodes;
         if (nodes != null)
         {
             return(nodes.FirstOrDefault(n => !ReferenceEquals(n, null) && (n.EndPoint == endPoint)));
         }
     }
     return(null);
 }
Exemplo n.º 7
0
        public void PromoteToMaster(RedisEndPoint newEndPoint, RedisEndPoint oldEndPoint)
        {
            if (!Disposed && !newEndPoint.IsEmpty())
            {
                lock (m_SyncRoot)
                {
                    SetMasterIsDown(oldEndPoint);

                    var masters = m_Masters;
                    if (masters.IsAlive())
                    {
                        var slaves = m_Slaves;

                        var node = masters.FindNodeOf(newEndPoint);
                        if (node.IsAlive())
                        {
                            node.Ping();
                            if (node.IsClosed)
                            {
                                node.IsClosed = false;
                            }

                            if (slaves.IsAlive())
                            {
                                slaves.RemoveNode(node);
                            }
                            return;
                        }

                        if (slaves.IsAlive())
                        {
                            var slaveNode = slaves.FindNodeOf(newEndPoint);
                            if (slaveNode.IsAlive())
                            {
                                if (ChangeGroup(slaveNode))
                                {
                                    if (slaveNode.IsClosed)
                                    {
                                        slaveNode.IsClosed = false;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 8
0
 private NodeRoleAndSiblings GetMasterOfSlave(RedisServerInfo serverInfo)
 {
     if (serverInfo != null)
     {
         var replicationSection = serverInfo.Replication;
         if (replicationSection != null)
         {
             try
             {
                 if (replicationSection.MasterPort.HasValue &&
                     !replicationSection.MasterHost.IsEmpty())
                 {
                     var endPoint = new RedisEndPoint(replicationSection.MasterHost,
                                                      (int)replicationSection.MasterPort.Value);
                     return(new NodeRoleAndSiblings(RedisRole.Slave, new[] { endPoint }));
                 }
             }
             catch (Exception)
             { }
         }
     }
     return(null);
 }
Exemplo n.º 9
0
        private RedisSocket CreateSocket(RedisEndPoint endPoint, IPAddress ipAddress)
        {
            var socket = NewSocketInternal(ipAddress);

            try
            {
                ConfigureInternal(socket);
                socket.ConnectAsync(ipAddress, endPoint.Port).Wait();

                SetLastError(0L);
                SetState((long)RedisConnectionState.Connected);

                var oldSocket = Interlocked.Exchange(ref m_Socket, socket);
                if (oldSocket != null && oldSocket != socket)
                {
                    oldSocket.DisposeSocket();
                }
            }
            catch (Exception e)
            {
                SetState((long)RedisConnectionState.Failed);

                var errorCode = e.GetSocketErrorCode();
                if (errorCode != SocketError.Success)
                {
                    SetLastError((long)errorCode);
                }
                else
                {
                    SetLastError(RedisErrorCode.GenericError);
                }

                socket.DisposeSocket();
                throw;
            }
            return(socket);
        }
        public Tuple <RedisRole, RedisEndPoint[], RedisSocket> DiscoverNode(RedisEndPoint endPoint)
        {
            if (endPoint.IsEmpty())
            {
                throw new RedisFatalException(new ArgumentNullException("endPoint"), RedisErrorCode.MissingParameter);
            }

            ValidateNotDisposed();

            var settings = Settings.Clone(endPoint.Host, endPoint.Port);

            using (var connection = NewConnection(settings))
            {
                var nodeInfo = GetNodeInfo(settings.MasterName, connection);
                if (!(nodeInfo == null || nodeInfo.Role == RedisRole.Undefined))
                {
                    var role             = nodeInfo.Role;
                    var siblingEndPoints = nodeInfo.Siblings;

                    return(new Tuple <RedisRole, RedisEndPoint[], RedisSocket>(role, siblingEndPoints, connection.RemoveSocket()));
                }
            }
            return(null);
        }
Exemplo n.º 11
0
        protected GroupedSockets CreateGroupSockets()
        {
            if (!Disposed)
            {
                var settings = m_Settings;
                var ipEPList = RedisEndPoint.ToIPEndPoints(settings.EndPoints);

                if (ipEPList != null && ipEPList.Count > 0)
                {
                    var ipEPSettings = ipEPList
                                       .Select(ep => (RedisPoolSettings)settings.Clone(ep.Address.ToString(), ep.Port))
                                       .ToArray();

                    if (!ipEPSettings.IsEmpty())
                    {
                        var discoveredEndPoints = new HashSet <IPEndPoint>();
                        var emptyTuple          = new Tuple <RedisRole, RedisSocket> [0];

                        var groupsTuples = ipEPSettings
                                           .SelectMany(setting => CreateNodes(discoveredEndPoints, setting) ?? emptyTuple)
                                           .Where(node => node != null)
                                           .GroupBy(
                            tuple => tuple.Item1,
                            tuple => tuple.Item2,
                            (role, group) => new Tuple <RedisRole, RedisSocket[]>(role, group.ToArray()))
                                           .ToList();

                        if (!groupsTuples.IsEmpty())
                        {
                            // 0: Masters, 1: Slaves, 2: Sentinels
                            const int MastersPos = 0, SlavesPos = 1, SentinelsPos = 2;

                            var result = new RedisSocket[3][];
                            foreach (var tuple in groupsTuples)
                            {
                                switch (tuple.Item1)
                                {
                                case RedisRole.Master:
                                    result[MastersPos] = tuple.Item2;
                                    break;

                                case RedisRole.Slave:
                                    result[SlavesPos] = tuple.Item2;
                                    break;

                                case RedisRole.Sentinel:
                                    result[SentinelsPos] = tuple.Item2;
                                    break;

                                default:
                                    break;
                                }
                            }

                            return(new GroupedSockets(result[MastersPos], result[SlavesPos], result[SentinelsPos]));
                        }
                    }
                }
            }
            return(null);
        }