コード例 #1
0
ファイル: DataMoveEveryDayTask.cs プロジェクト: xwx2015/MQ
 private long StartMoveMQID(string fromtablename, PartitionIDInfo info, Dictionary<int, tb_datanode_model> datanodemodels, tb_partition_model p, DateTime tabletime)
 {
     long r = -1;
     SqlHelper.ExcuteSql(this.GetDataNodeConnectString(datanodemodels[info.DataNodePartition]), (c) =>
     {
         bool exsit = c.TableIsExist(fromtablename);
         if (exsit == true)
         {
             tb_messagequeue_dal dal = new tb_messagequeue_dal(); dal.TableName = fromtablename;
             long maxid = dal.GetMaxID(c); long minid = dal.GetMinID(c);
             int findcount = 0;
             while (findcount<=30)//二分查找算法,超过30次意味着算法若对,查询过一亿的数据,说明算法有问题
             {
                 findcount++;
                 KeyValuePair<long, DateTime> mininfo = dal.GetIDCreateTime(c, minid); KeyValuePair<long, DateTime> maxinfo = dal.GetIDCreateTime(c, maxid);
                 if (mininfo.Value.Date <= tabletime.Date && maxinfo.Value.Date > tabletime.Date)//最小id小于等于当天,最大id>当天,说明中间有数据要迁移
                 {
                     var midid = (long)((maxinfo.Key + mininfo.Key) / 2); KeyValuePair<long, DateTime> midinfo = dal.GetIDCreateTime(c, midid);
                     if (midinfo.Value.Date <= tabletime.Date)//往大的查找
                     {
                         minid = midid;
                     }
                     else//往小的查找
                     {
                         maxid = minid;
                     }
                     System.Threading.Thread.Sleep(50);//避免给数据库大的压力
                 }
                 else if (mininfo.Value.Date > tabletime.Date)//最小id的时间超过表当天
                 {
                     r = mininfo.Key; return;
                 }
                 else if (maxinfo.Value.Date <= tabletime.Date)//最大id的时间小于或等同当天
                 {
                     r = maxinfo.Key; return;
                 }
             }
             if (findcount >= 30)
                 throw new Exception("二分查找算法错误,查找次数超过30次");
         }
     });
     return r;
 }
コード例 #2
0
        public override void Run()
        {
            ConfigHelper.LoadConfig(this.AppConfig["BusinessMQManageConnectString"]);

            List<tb_datanode_model> nodeList = new List<tb_datanode_model>();

            using (DbConn conn = DbConfig.CreateConn(this.AppConfig["BusinessMQManageConnectString"]))
            {
                conn.Open();
                List<tb_partition_model> createTimeList = new tb_consumer_dal().GetClientCreateTime(conn);
                var serverdate = conn.GetServerDate().Date; var timenow = conn.GetServerDate();
                nodeList = new tb_datanode_dal().List(conn);
                List<Exception> exps = new List<Exception>();
                foreach (var item in nodeList)
                {
                    try
                    {
                        string dataNode = PartitionRuleHelper.GetDataNodeName(item.datanodepartition);
                        string nodeConn = string.Format("server={0};Initial Catalog={1};User ID={2};Password={3};", item.serverip, dataNode, item.username, item.password);
                        string partitionId = PartitionRuleHelper.PartitionNameRule(item.datanodepartition);
                        string t = PartitionRuleHelper.GetDataNodeName(item.datanodepartition);

                        using (DbConn dataNodeConn = DbConfig.CreateConn(nodeConn))
                        {
                            dataNodeConn.Open();
                            var tablesinfo = new tb_messagequeue_dal().GetDataNodeTable(dataNodeConn, serverdate.AddDays(-30));
                            foreach (var table in tablesinfo)
                            {
                                string tablename = XXF.BaseService.MessageQuque.BusinessMQ.SystemRuntime.PartitionRuleHelper.GetTableName(table.TablePartition, table.Day);

                                TableInfo info = PartitionRuleHelper.GetTableInfo(tablename);
                                var beginId = XXF.BaseService.MessageQuque.BusinessMQ.SystemRuntime.PartitionRuleHelper.GetMQID(new MQIDInfo() { AutoID = 0, DataNodePartition = item.datanodepartition, Day = table.Day, TablePartition = table.TablePartition });

                                int mqPartitionId = PartitionRuleHelper.GetPartitionID(new PartitionIDInfo() { DataNodePartition = item.datanodepartition, TablePartition = table.TablePartition });
                                tb_partition_model timeModel = createTimeList.Where(q => q.partitionid == mqPartitionId).FirstOrDefault();
                                DateTime time = DateTime.Parse("2015-01-01");
                                if (timeModel != null)
                                {
                                    time = timeModel.createtime;
                                }
                                var dal = new tb_messagequeue_dal(); dal.TableName = tablename;

                                int count = dal.GetCount(dataNodeConn, time);
                                long maxId = dal.GetMaxID(dataNodeConn, time); maxId = (maxId == 0 ? beginId : maxId);
                                long minId = dal.GetMinID(dataNodeConn, time); minId = (minId == 0 ? beginId : minId);
                                tb_partition_messagequeue_report_model model = new tb_partition_messagequeue_report_model();
                                model.partitionid = XXF.BaseService.MessageQuque.BusinessMQ.SystemRuntime.PartitionRuleHelper.GetPartitionID(new PartitionIDInfo() { DataNodePartition = item.datanodepartition, TablePartition = table.TablePartition });

                                MQIDInfo mqInfo = PartitionRuleHelper.GetMQIDInfo(maxId);
                                model.day = mqInfo.Day;
                                model.lastupdatetime = timenow;
                                model.createtime = timenow;
                                model.mqmaxid = maxId;
                                model.mqminid = minId;
                                model.mqcount = count;
                                new tb_partition_messagequeue_report_dal().AddReport(conn, model);
                            }

                        }
                    }
                    catch (Exception exp)
                    {
                        exps.Add(exp);
                    }

                }
                Error(this.AppConfig["BusinessMQManageConnectString"], "消息统计出错", exps);
            }
        }