Inheritance: RedisInstanceBase
Exemple #1
0
        private SlaveInstance MasterToSlave(MasterInstance master)
        {
            var group    = master.Group;
            var newSlave = new SlaveInstance(group)
            {
                EndPoint = master.EndPoint,
            };

            newSlave.CopyFrom(master);

            redisInstancesDict[newSlave.EndPoint.ToString()] = newSlave;
            group.AddSlave(newSlave);

            Console.WriteLine("modify {0} from master to slave in momery", master.EndPoint);

            return(newSlave);
        }
Exemple #2
0
        private MasterInstance SlaveToMaster(SlaveInstance slave)
        {
            var group     = slave.Group;
            var newMaster = new MasterInstance(group)
            {
                EndPoint = slave.EndPoint,
            };

            newMaster.CopyFrom(slave);

            redisInstancesDict[newMaster.EndPoint.ToString()] = newMaster;
            group.SetCurrentMaster(newMaster);
            group.RemoveSlave(slave);

            Console.WriteLine("modify {0} from slave to master in momery", slave.EndPoint);

            return(newMaster);
        }
        // every 10s
        private async Task RefreshAllMasters()
        {
            var masters = groups.Values.Select(g => g.Master);
            var toConnectSlaves = new List<RedisInstanceBase>();

            foreach (var master in masters)
            {
                try
                {
                    if (!master.Server.IsConnected)
                    {
                        continue;
                    }

                    if (master.Server.IsSlave)
                    {
                        Console.WriteLine("master {0} state hasn't been sync, still slave", master.EndPoint);
                        //todo
                        continue;
                    }

                    var val = await master.Server.InfoAsync();

                    foreach (var group in val)
                    {
                        if (group.Key.Equals("Replication"))
                        {
                            var slavesNum = int.Parse(group.First(kv => kv.Key.Equals("connected_slaves")).Value);
                            for (int i = 0; i < slavesNum; i++)
                            {
                                var slaveId = string.Format("slave{0}", i);
                                var slaveInfo = group.First(kv => kv.Key.Equals(slaveId)).Value;
                                var infos = slaveInfo.Split(',');

                                var ip = infos[0].Split('=')[1];
                                if (ip.Equals("127.0.0.1"))
                                {
                                    ip = master.EndPoint.ToString().Split(':')[0];
                                }

                                var ep = new IPEndPoint(IPAddress.Parse(ip),
                                    int.Parse(infos[1].Split('=')[1]));
                                var epStr = ep.ToString();

                                RedisInstanceBase ins = null;
                                if (!redisInstancesDict.TryGetValue(epStr, out ins))
                                {
                                    ins = redisInstancesDict[epStr] = new SlaveInstance(master.Group);
                                    toConnectSlaves.Add(ins);
                                }

                                var slaveIns = ins as SlaveInstance;

                                if (slaveIns == null)
                                {
                                    Console.WriteLine("slave {0} state hasn't been sync, still master", ins.EndPoint);
                                    continue;
                                }

                                slaveIns.OnMasterInfoRefresh(ep, slaveInfo);
                                master.Group.AddSlave(slaveIns);
                            }
                        }
                        else if (group.Key.Equals("Server"))
                        {
                            var runId = group.First(kv => kv.Key.Equals("run_id")).Value;

                            master.RunId = runId;
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }

            if (toConnectSlaves.Count > 0)
            {
                ReconfigureConnection(toConnectSlaves);
            }
        }
        private SlaveInstance MasterToSlave(MasterInstance master)
        {
            var group = master.Group;
            var newSlave = new SlaveInstance(group)
            {
                EndPoint = master.EndPoint,
            };
            newSlave.CopyFrom(master);

            redisInstancesDict[newSlave.EndPoint.ToString()] = newSlave;
            group.AddSlave(newSlave);

            Console.WriteLine("modify {0} from master to slave in momery", master.EndPoint);

            return newSlave;
        }
        private MasterInstance SlaveToMaster(SlaveInstance slave)
        {
            var group = slave.Group;
            var newMaster = new MasterInstance(group)
            {
                EndPoint = slave.EndPoint,
            };

            newMaster.CopyFrom(slave);

            redisInstancesDict[newMaster.EndPoint.ToString()] = newMaster;
            group.SetCurrentMaster(newMaster);
            group.RemoveSlave(slave);

            Console.WriteLine("modify {0} from slave to master in momery", slave.EndPoint);

            return newMaster;
        }
 public void RemoveSlave(SlaveInstance slave)
 {
     Slaves.Remove(slave.EndPoint.ToString());
 }
 public void AddSlave(SlaveInstance slave)
 {
     Slaves[slave.EndPoint.ToString()] = slave;
 }
Exemple #8
0
        // every 10s
        private async Task RefreshAllMasters()
        {
            var masters         = groups.Values.Select(g => g.Master);
            var toConnectSlaves = new List <RedisInstanceBase>();

            foreach (var master in masters)
            {
                try
                {
                    if (!master.Server.IsConnected)
                    {
                        continue;
                    }

                    if (master.Server.IsSlave)
                    {
                        Console.WriteLine("master {0} state hasn't been sync, still slave", master.EndPoint);
                        //todo
                        continue;
                    }

                    var val = await master.Server.InfoAsync();

                    foreach (var group in val)
                    {
                        if (group.Key.Equals("Replication"))
                        {
                            var slavesNum = int.Parse(group.First(kv => kv.Key.Equals("connected_slaves")).Value);
                            for (int i = 0; i < slavesNum; i++)
                            {
                                var slaveId   = string.Format("slave{0}", i);
                                var slaveInfo = group.First(kv => kv.Key.Equals(slaveId)).Value;
                                var infos     = slaveInfo.Split(',');

                                var ip = infos[0].Split('=')[1];
                                if (ip.Equals("127.0.0.1"))
                                {
                                    ip = master.EndPoint.ToString().Split(':')[0];
                                }

                                var ep = new IPEndPoint(IPAddress.Parse(ip),
                                                        int.Parse(infos[1].Split('=')[1]));
                                var epStr = ep.ToString();

                                RedisInstanceBase ins = null;
                                if (!redisInstancesDict.TryGetValue(epStr, out ins))
                                {
                                    ins = redisInstancesDict[epStr] = new SlaveInstance(master.Group);
                                    toConnectSlaves.Add(ins);
                                }

                                var slaveIns = ins as SlaveInstance;

                                if (slaveIns == null)
                                {
                                    Console.WriteLine("slave {0} state hasn't been sync, still master", ins.EndPoint);
                                    continue;
                                }

                                slaveIns.OnMasterInfoRefresh(ep, slaveInfo);
                                master.Group.AddSlave(slaveIns);
                            }
                        }
                        else if (group.Key.Equals("Server"))
                        {
                            var runId = group.First(kv => kv.Key.Equals("run_id")).Value;

                            master.RunId = runId;
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }

            if (toConnectSlaves.Count > 0)
            {
                ReconfigureConnection(toConnectSlaves);
            }
        }