Exemplo n.º 1
0
 protected internal RedisManagedServer(RedisAsyncClient client, RedisConnectionSettings settings, RedisRole role,
                                       Action <object, RedisCardioPulseStatus> onPulseStateChange)
     : base(client, settings)
 {
     Role = role;
     m_OnPulseStateChange = onPulseStateChange;
 }
        public RedisManagedSentinelListener(RedisConnectionSettings settings,
                                            Action <object, RedisCardioPulseStatus> onPulseStateChange)
            : base(settings)
        {
            m_OnPulseStateChange = onPulseStateChange;

            m_Client = new RedisAsyncClient(settings);
            Init(settings, m_Client);
        }
Exemplo n.º 3
0
        protected NodeRoleAndSiblings GetNodeInfo(string masterName, RedisAsyncClient client)
        {
            if (client != null)
            {
                try
                {
                    var serverInfo = client.GetServerInfo();
                    if (serverInfo != null)
                    {
                        var role = DiscoverRoleOfNode(serverInfo);

                        switch (role)
                        {
                        case RedisRole.Slave:
                            return(GetMasterOfSlave(serverInfo) ?? new NodeRoleAndSiblings(role, null));

                        case RedisRole.Master:
                            return(GetSlavesOfMaster(serverInfo) ?? new NodeRoleAndSiblings(role, null));

                        case RedisRole.Sentinel:
                        {
                            var masters = GetMastersOfSentinel(masterName, serverInfo);
                            if (!Disposed)
                            {
                                var otherSentinels = GetSiblingSentinelsOfSentinel(masterName, client);
                                if (!Disposed)
                                {
                                    if (otherSentinels == null || otherSentinels.Siblings == null)
                                    {
                                        return(masters ?? new NodeRoleAndSiblings(role, null));
                                    }

                                    if (masters == null || masters.Siblings == null)
                                    {
                                        return(otherSentinels ?? new NodeRoleAndSiblings(role, null));
                                    }

                                    var siblings = new HashSet <RedisEndPoint>(masters.Siblings);
                                    siblings.UnionWith(otherSentinels.Siblings);

                                    return(new NodeRoleAndSiblings(role, siblings.ToArray()));
                                }
                            }
                            break;
                        }

                        default:
                            break;
                        }
                    }
                }
                catch (Exception)
                { }
            }
            return(null);
        }
Exemplo n.º 4
0
        protected internal RedisSentinelClient(RedisAsyncClient client)
        {
            if (client == null)
            {
                throw new RedisFatalException(new ArgumentNullException("client"), RedisErrorCode.MissingParameter);
            }

            m_Client   = client.Disposed ? new RedisAsyncClient(client.Settings) : client;
            m_Executer = new RedisAsyncCommandExecuter(m_Client, RedisConstants.UninitializedDbIndex);
        }
        private void Init(RedisConnectionSettings settings, RedisAsyncClient client)
        {
            m_Executer = new RedisAsyncCommandExecuter(client, RedisConstants.UninitializedDbIndex);
            if (IsHeartBeatEnabled(settings))
            {
                m_HeartBeatProbe = new RedisHeartBeatProbe(settings, this, null);
                m_HeartBeatProbe.SetOnPulseStateChange(OnPulseStateChange);

                m_HeartBeatProbe.AttachToCardio();
            }
        }
Exemplo n.º 6
0
        public RedisSentinelClient(RedisSentinelSettings settings)
            : base()
        {
            if (settings == null)
            {
                throw new RedisFatalException(new ArgumentNullException("settings"), RedisErrorCode.MissingParameter);
            }

            m_Client   = new RedisAsyncClient(settings);
            m_Executer = new RedisAsyncCommandExecuter(m_Client, RedisConstants.UninitializedDbIndex);
        }
Exemplo n.º 7
0
        protected internal RedisAsyncServer(RedisAsyncClient client, RedisConnectionSettings settings)
        {
            if (client == null)
            {
                throw new RedisFatalException(new ArgumentNullException("client"), RedisErrorCode.MissingParameter);
            }

            settings = settings ?? client.Settings;

            Init(settings);
            if (client.IsAlive())
            {
                m_Clients[0] = client;
            }
        }
        protected internal RedisManagedSentinelListener(RedisAsyncClient client,
                                                        RedisConnectionSettings settings, Action <object, RedisCardioPulseStatus> onPulseStateChange)
            : base(settings ?? client.Settings)
        {
            if (client == null)
            {
                throw new RedisFatalException(new ArgumentNullException("client"), RedisErrorCode.MissingParameter);
            }

            m_OnPulseStateChange = onPulseStateChange;

            settings = settings ?? client.Settings;

            m_Client = client.Disposed ? new RedisAsyncClient(settings) : client;
            Init(settings, m_Client);
        }
Exemplo n.º 9
0
        private NodeRoleAndSiblings GetSiblingSentinelsOfSentinel(string masterName, RedisAsyncClient client)
        {
            if (masterName.IsEmpty())
            {
                throw new ArgumentNullException("masterName");
            }

            try
            {
                var response = client.Expect(new RedisCommand(-1, RedisCommandList.Sentinel, RedisCommandType.SendAndReceive,
                                                              RedisCommandList.Sentinels, masterName.ToBytes()));

                if (!ReferenceEquals(response, null) && response.Type == RedisResultType.Array)
                {
                    var result = response as RedisArray;
                    if (!ReferenceEquals(result, null))
                    {
                        var sentinelInfos = RedisSentinelNodeInfo.ParseInfoResponse(result);
                        if (sentinelInfos != null)
                        {
                            var length = sentinelInfos.Length;
                            if (length > 0)
                            {
                                var siblingEndPoints = new List <RedisEndPoint>(length);
                                for (var i = 0; i < length; i++)
                                {
                                    var sentinelInfo = sentinelInfos[i];

                                    if (!Disposed && sentinelInfo != null && sentinelInfo.Port.HasValue && !sentinelInfo.IPAddress.IsEmpty())
                                    {
                                        siblingEndPoints.Add(new RedisEndPoint(sentinelInfo.IPAddress, (int)sentinelInfo.Port.Value));
                                    }
                                }

                                if (!Disposed)
                                {
                                    return(new NodeRoleAndSiblings(RedisRole.Sentinel, siblingEndPoints.ToArray()));
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception)
            { }
            return(null);
        }
Exemplo n.º 10
0
        private RedisAsyncClient GetTransactionalClient()
        {
            var client = m_TransactionalClient;

            if (client == null)
            {
                lock (m_TransClientLock)
                {
                    client = m_TransactionalClient;
                    if (client == null)
                    {
                        client = (m_TransactionalClient = new RedisAsyncClient(m_Settings));
                    }
                }
            }
            return(client);
        }
Exemplo n.º 11
0
        private RedisAsyncClient GetClient()
        {
            lock (m_ClientLock)
            {
                var index = Interlocked.Add(ref m_ClientIndex, 1);
                if (index >= m_ClientCount)
                {
                    index = 0;
                    Interlocked.Exchange(ref m_ClientIndex, 0);
                }

                var client = m_Clients[index];
                if (client == null)
                {
                    client = (m_Clients[index] = new RedisAsyncClient(m_Settings));
                }
                return(client);
            }
        }
Exemplo n.º 12
0
 public RedisPipeline(RedisAsyncClient asyncClient, int dbIndex)
     : base(asyncClient, dbIndex)
 {
 }
Exemplo n.º 13
0
 public RedisAsyncCommandExecuter(RedisAsyncClient asyncClient, int dbIndex)
 {
     m_AsyncClient = asyncClient;
     m_DbIndex     = Math.Min(Math.Max(dbIndex, RedisConstants.UninitializedDbIndex), RedisConstants.MaxDbIndex);
 }
Exemplo n.º 14
0
 public RedisTransaction(RedisAsyncClient asyncClient, int dbIndex)
     : base(asyncClient, dbIndex)
 {
 }
Exemplo n.º 15
0
 public RedisDb(RedisAsyncClient asyncClient, int dbIndex)
     : base(asyncClient, dbIndex)
 {
 }
Exemplo n.º 16
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);
        }