Example #1
0
        public tb_consumer_model RegisterConsumer(DbConn PubConn, int clientid, string clientname, List <int> partitionindexs)
        {
            long            tempid = CommonHelper.GenerateIntID();
            tb_consumer_dal dal    = new tb_consumer_dal();

            dal.DeleteNotOnLineByClientID(PubConn, clientid, SystemParamConfig.Consumer_ConsumerHeartbeat_MAX_TIME_OUT);
            List <int> usedpartitionindexs     = dal.GetRegisterPartitionIndexs(PubConn, clientid);
            var        conflictpartitionindexs = (from o in usedpartitionindexs from n in partitionindexs where o == n select o).ToList();

            if (conflictpartitionindexs != null && conflictpartitionindexs.Count > 0)
            {
                throw new BusinessMQException(string.Format("当前分区序号已经被注册使用中,冲突分区序号为:{0},可能是上次消费者异常终止导致消费者依然在注册中,请尝试在{1}秒系统超时后重试。",
                                                            string.Join(",", conflictpartitionindexs.ToArray()), SystemParamConfig.Consumer_ConsumerHeartbeat_MAX_TIME_OUT));
            }
            dal.Add2(PubConn, new tb_consumer_model()
            {
                clientname = clientname, consumerclientid = clientid, partitionindexs = string.Join(",", partitionindexs.ToArray()), tempid = tempid
            });
            return(dal.Get(PubConn, tempid, clientid));
        }
        public ActionResult Delete(int id, int mqpathid)
        {
            try
            {
                using (DbConn conn = DbConfig.CreateConn(DataConfig.MqManage))
                {
                    try
                    {
                        conn.Open();
                        conn.BeginTransaction();

                        var item = new tb_consumer_partition_dal().Get(conn, id);
                        new tb_consumer_dal().DeleteNotOnLineByClientID(conn, item.consumerclientid, 20);
                        var consumer = new tb_consumer_dal().GetByTempId(conn, item.lastconsumertempid);
                        if (consumer != null && consumer.tempid == 0)
                        {
                            throw new Exception("当前消费者未处于正常离线状态,若是非正常断线状态,可以在20s超时后尝试");
                        }
                        new tb_consumer_partition_dal().Delete(conn, id);
                        conn.Commit();
                    }
                    catch (Exception exp)
                    {
                        conn.Rollback();
                        throw exp;
                    }
                }
                this.SendCommandToRedistReStart(mqpathid, XXF.BaseService.MessageQuque.BusinessMQ.SystemRuntime.EnumCommandReceiver.Consumer);
                return(Json(new { code = 1, msg = "删除成功" }));
            }

            catch (Exception e)
            {
                return(Json(new { code = -1, msg = e.Message }));
            }
        }
        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);
            }
        }
Example #4
0
        public void ConsumerHeartbeat(DbConn PubConn, long tempid, int clientid)
        {
            tb_consumer_dal dal = new tb_consumer_dal();

            dal.ClientHeatbeat(PubConn, clientid, tempid);
        }
Example #5
0
        public void RemoveConsumer(DbConn PubConn, long tempid, int clientid)
        {
            tb_consumer_dal dal = new tb_consumer_dal();

            dal.DeleteClient(PubConn, clientid, tempid);
        }
        /// <summary>
        /// 任务调度平台根据发布的任务时间配置,定时回调运行方法
        /// 开发人员的任务插件必须要重载并该方法
        /// </summary>
        public override void Run()
        {
            runcount++;

            ConfigHelper.LoadConfig(this.AppConfig["BusinessMQManageConnectString"]);

            List <tb_partition_model>           userdpartitions          = new List <tb_partition_model>();
            Dictionary <int, tb_datanode_model> datanodemodels           = new Dictionary <int, tb_datanode_model>();
            List <RegisterdConsumersModel>      registeredconsumermodels = new List <RegisterdConsumersModel>();
            List <tb_consumer_model>            consumers = new List <tb_consumer_model>();
            DateTime servertime = DateTime.Now;

            SqlHelper.ExcuteSql(this.AppConfig["BusinessMQManageConnectString"], (c) =>
            {
                tb_partition_dal partitiondal = new tb_partition_dal();
                userdpartitions             = partitiondal.List(c, true);
                tb_datanode_dal datanodedal = new tb_datanode_dal();
                var ms = datanodedal.List(c);
                foreach (var m in ms)
                {
                    datanodemodels.Add(m.datanodepartition, m);
                }
                servertime = c.GetServerDate();

                //获取当前注册的消费者
                tb_consumer_dal consumerdal = new tb_consumer_dal();
                registeredconsumermodels    = consumerdal.GetRegisterdConsumers(c);
                consumers = consumerdal.GetAllList(c);
            });
            //消费者端心跳停止,异常退出检测(超过1分钟)
            List <Exception> exps = new List <Exception>();

            foreach (var c in consumers)
            {
                if ((servertime - c.lastheartbeat) > TimeSpan.FromMinutes(1))
                {
                    exps.Add(new Exception(string.Format("当前消费者tempid:{0},consumerclientid:{1}", c.tempid, c.consumerclientid)));
                }
            }
            Error(this.AppConfig["BusinessMQManageConnectString"], "消费者端心跳停止,异常退出检测(超过1分钟),若是异常停止,请手工清除消费者中断", exps);
            //消费者端长时间无消费,但是还有多余消息(超过10分钟未消费,但还有多余消息未消费)
            exps.Clear();
            foreach (var c in registeredconsumermodels)
            {
                if ((servertime - c.consumerpartitionmodel.lastupdatetime) > TimeSpan.FromMinutes(10))
                {
                    var partitionidinfo = XXF.BaseService.MessageQuque.BusinessMQ.SystemRuntime.PartitionRuleHelper.GetPartitionIDInfo(c.consumerpartitionmodel.partitionid);
                    var tablename       = XXF.BaseService.MessageQuque.BusinessMQ.SystemRuntime.PartitionRuleHelper.GetTableName(partitionidinfo.TablePartition, servertime.Date);
                    SqlHelper.ExcuteSql(this.GetDataNodeConnectString(datanodemodels[partitionidinfo.DataNodePartition]), (conn) =>
                    {
                        tb_messagequeue_dal messagedal = new tb_messagequeue_dal(); messagedal.TableName = tablename;
                        long maxmqid = messagedal.GetMaxID(conn);
                        if (maxmqid > c.consumerpartitionmodel.lastmqid && (servertime - messagedal.GetIDCreateTime(conn, maxmqid).Value > TimeSpan.FromMinutes(10)))//还有消息未消费,且该消息的创建时间是10分钟前
                        {
                            exps.Add(new Exception(string.Format("当前消费者tempid:{0},consumerclientid:{1},client:{2},clientname:{3}", c.consumermodel.tempid,
                                                                 c.consumermodel.consumerclientid, c.consumerclientmodel.client, c.consumermodel.clientname)));
                        }
                    });
                }
            }
            Error(this.AppConfig["BusinessMQManageConnectString"], "消费者端长时间无消费,但是还有多余消息(超过已创建超过10分钟的消息未消费),可能是消费者处理逻辑异常", exps);
            //某个分区没有指定消费者(分区没有消费信息,或者分区消费最后消费时间超过1个小时无消费)
            exps.Clear();
            if (runcount % (12 * 6) == 0)
            {
                foreach (var u in userdpartitions)
                {
                    bool isfind = false;
                    foreach (var c in registeredconsumermodels)
                    {
                        if (c.consumerpartitionmodel.partitionid == u.partitionid)
                        {
                            isfind = true; break;
                        }
                    }
                    if (isfind == false)
                    {
                        SqlHelper.ExcuteSql(this.AppConfig["BusinessMQManageConnectString"], (c) =>
                        {
                            tb_consumer_partition_dal dal = new tb_consumer_partition_dal();
                            var model = dal.GetByPartitionId(c, u.partitionid);
                            if (model == null)
                            {
                                exps.Add(new Exception(string.Format("分区{0}未指定消费者", u.partitionid)));
                            }
                            //else if((servertime - model.lastupdatetime) > TimeSpan.FromHours(24))
                            //     exps.Add(new Exception(string.Format("分区{0}消费者已注销,且有1天未消费", u.partitionid)));
                        });
                    }
                }
                Error(this.AppConfig["BusinessMQManageConnectString"], "某个分区没有指定消费者(分区没有消费信息)", exps);
            }
        }