Exemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 5
0
        protected RedisManagedNode(RedisManagerSettings settings, RedisRole role, object seed,
                                   Action <object, RedisCardioPulseStatus> onPulseStateChange, bool ownsSeed = true)
        {
            m_Role     = role;
            m_OwnsSeed = ownsSeed;
            m_EndPoint = RedisEndPoint.Empty;

            m_Settings           = settings;
            m_OnPulseStateChange = onPulseStateChange;

            ExchangeSeedInternal(seed);
            AttachToCardio();
        }
Exemplo n.º 6
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.º 7
0
        protected string[] GetMyIPAndPort()
        {
            var ep = EndPoint;

            if (ep != null)
            {
                var ipEp = ep as IPEndPoint;
                if (ipEp != null)
                {
                    return new[] { ipEp.Address.ToString() + ":" + ipEp.Port }
                }
                ;

                var rep = ep as RedisEndPoint;
                if (!ReferenceEquals(rep, null))
                {
                    var ipAddresses = rep.ResolveHost();

                    if (!ipAddresses.IsEmpty())
                    {
                        return(ipAddresses.Select(addr => addr + ":" + rep.Port).ToArray());
                    }

                    return(new[] { rep.Host + ":" + rep.Port });
                }

                var dnsEp = ep as DnsEndPoint;
                if (dnsEp != null)
                {
                    rep = new RedisEndPoint(dnsEp.Host, dnsEp.Port);

                    var ipAddresses = rep.ResolveHost();
                    if (!ipAddresses.IsEmpty())
                    {
                        return(ipAddresses.Select(addr => addr + ":" + rep.Port).ToArray());
                    }

                    return(new[] { dnsEp.Host + ":" + dnsEp.Port });
                }
            }
            return(null);
        }
Exemplo n.º 8
0
        private RedisEndPoint GetEndPoint(RedisManagedSentinelListener sentinel)
        {
            var result = (RedisEndPoint)null;

            if (sentinel != null)
            {
                var sEndPoint = sentinel.EndPoint;
                if (sEndPoint != null)
                {
                    var ipEP = sEndPoint as IPEndPoint;
                    if (ipEP != null)
                    {
                        result = new RedisEndPoint(ipEP.Address.ToString(), ipEP.Port);
                    }
                    else
                    {
                        result = sEndPoint as RedisEndPoint;
                        if (ReferenceEquals(result, null))
                        {
                            var dnsEP = sEndPoint as DnsEndPoint;
                            if (dnsEP != null)
                            {
                                result = new RedisEndPoint(dnsEP.Host, ipEP.Port);
                            }
                        }
                    }
                }
            }

            if (ReferenceEquals(result, null) || result.IsEmpty)
            {
                var endPoints = Settings.EndPoints;
                if (!endPoints.IsEmpty())
                {
                    result = endPoints.FirstOrDefault(ep => !ReferenceEquals(ep, null) && !ep.IsEmpty);
                }
            }

            return(result ?? RedisEndPoint.Empty);
        }
Exemplo n.º 9
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.º 10
0
        public Tuple <RedisRole, RedisEndPoint[], RedisAsyncClient> DiscoverNode(RedisEndPoint endPoint)
        {
            if (endPoint.IsEmpty())
            {
                throw new RedisFatalException(new ArgumentNullException("endPoint"), RedisErrorCode.MissingParameter);
            }

            ValidateNotDisposed();

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

            var client = NewClient(settings);

            var nodeInfo = GetNodeInfo(settings.MasterName, client);

            if (!(nodeInfo == null || nodeInfo.Role == RedisRole.Undefined))
            {
                var role             = nodeInfo.Role;
                var siblingEndPoints = nodeInfo.Siblings;

                return(new Tuple <RedisRole, RedisEndPoint[], RedisAsyncClient>(role, siblingEndPoints, client));
            }
            return(null);
        }
Exemplo n.º 11
0
 public RedisNodeInfo(RedisEndPoint endPoint, RedisRole role, string name = null)
 {
     Role = role;
     Name = name ?? String.Empty;
     EndPoint = endPoint ?? RedisEndPoint.Empty;
 }
Exemplo n.º 12
0
        protected GroupedClients CreateGroupSockets()
        {
            if (!Disposed)
            {
                var settings = m_Settings;
                var ipEPList = RedisEndPoint.ToIPEndPoints(settings.EndPoints);

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

                    if (!ipEPSettings.IsEmpty())
                    {
                        var discoveredEndPoints = new HashSet <IPEndPoint>();
                        var emptyTuple          = new Tuple <RedisRole, RedisAsyncClient> [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, RedisAsyncClient[]>(role, group.ToArray()))
                                           .ToList();

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

                            var result = new RedisAsyncClient[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 GroupedClients(result[MastersPos], result[SlavesPos], result[SentinelsPos]));
                        }
                    }
                }
            }
            return(null);
        }