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);
        }
Beispiel #2
0
        public static RedisServerInfo Parse(string info)
        {
            if (!info.IsEmpty())
            {
                var lines = info.Split(new[] { RedisConstants.CRLF }, StringSplitOptions.RemoveEmptyEntries);
                if (lines != null)
                {
                    var length = lines.Length;
                    if (length > 0)
                    {
                        var result = new RedisServerInfo();

                        for (var i = 0; i < length; i++)
                        {
                            var line = (lines[i] ?? String.Empty).TrimStart();
                            if (!line.IsEmpty() && line[0] == '#')
                            {
                                var sectionName = line.TrimStart('#').Trim();

                                var sectionLines = !sectionName.IsEmpty() ?
                                                   new List <string>() : null;

                                i++;
                                for (; i < length; i++)
                                {
                                    line = (lines[i] ?? String.Empty).TrimStart();
                                    if (!line.IsEmpty())
                                    {
                                        if (line[0] == '#')
                                        {
                                            i--;
                                            break;
                                        }

                                        if (sectionLines != null)
                                        {
                                            sectionLines.Add(line);
                                        }
                                    }
                                }

                                if (!sectionName.IsEmpty())
                                {
                                    var section = ParseSection(sectionName, sectionLines);
                                    if (section != null)
                                    {
                                        result[sectionName.ToLowerInvariant()] = section;
                                    }
                                }
                            }
                        }

                        return(result);
                    }
                }
            }
            return(null);
        }
        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);
        }
Beispiel #4
0
        public RedisResult <RedisServerInfo> Info(RedisParam?section = null)
        {
            string lines;

            if (!section.HasValue || section.Value.IsNull)
            {
                lines = ExpectBulkString(RedisCommandList.Info);
            }
            else
            {
                lines = ExpectBulkString(RedisCommandList.Info, section);
            }

            var info = RedisServerInfo.Parse(lines);

            return(new RedisResult <RedisServerInfo>(info));
        }
Beispiel #5
0
 public RedisServerInfo GetServerInfo()
 {
     try
     {
         var socket = GetSocket();
         if (socket.IsConnected())
         {
             using (var cmd = new RedisCommand(-1, RedisCommandList.Info, RedisCommandType.SendAndReceive))
             {
                 string lines = cmd.ExpectBulkString(new RedisSocketContext(socket, Settings), true);
                 return(RedisServerInfo.Parse(lines));
             }
         }
     }
     catch (Exception)
     { }
     return(null);
 }
        private RedisRole DiscoverRoleOfNode(RedisServerInfo serverInfo)
        {
            if (serverInfo != null)
            {
                var serverSection = serverInfo.Server;
                if (serverSection != null)
                {
                    var redisMode = (serverSection.RedisMode ?? String.Empty).Trim().ToLowerInvariant();
                    if (redisMode == "sentinel")
                    {
                        return(RedisRole.Sentinel);
                    }

                    var replicationSection = serverInfo.Replication;
                    if (replicationSection != null)
                    {
                        return(replicationSection.Role.ToRedisRole());
                    }
                }
            }
            return(RedisRole.Undefined);
        }
 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);
 }