You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/cron_task/clear_msg.go

202 lines
6.3 KiB

2 years ago
package cronTask
2 years ago
import (
"Open_IM/pkg/common/config"
2 years ago
"Open_IM/pkg/common/constant"
2 years ago
"Open_IM/pkg/common/db"
"Open_IM/pkg/common/log"
server_api_params "Open_IM/pkg/proto/sdk_ws"
"Open_IM/pkg/utils"
2 years ago
goRedis "github.com/go-redis/redis/v8"
2 years ago
"github.com/golang/protobuf/proto"
2 years ago
"math"
2 years ago
"strconv"
"strings"
2 years ago
)
const oldestList = 0
const newestList = -1
2 years ago
func ResetUserGroupMinSeq(operationID, groupID string, userIDList []string) error {
var delMsgIDList [][2]interface{}
minSeq, err := deleteMongoMsg(operationID, groupID, oldestList, &delMsgIDList)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), groupID, "deleteMongoMsg failed")
}
2 years ago
if minSeq == 0 {
return nil
}
2 years ago
log.NewDebug(operationID, utils.GetSelfFuncName(), "delMsgIDList:", delMsgIDList, "minSeq", minSeq)
2 years ago
for _, userID := range userIDList {
userMinSeq, err := db.DB.GetGroupUserMinSeq(groupID, userID)
2 years ago
if err != nil && err != goRedis.Nil {
2 years ago
log.NewError(operationID, utils.GetSelfFuncName(), "GetGroupUserMinSeq failed", groupID, userID, err.Error())
continue
}
if userMinSeq > uint64(minSeq) {
err = db.DB.SetGroupUserMinSeq(groupID, userID, userMinSeq)
} else {
err = db.DB.SetGroupUserMinSeq(groupID, userID, uint64(minSeq))
}
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), groupID, userID, userMinSeq, minSeq)
}
}
2 years ago
return nil
}
func DeleteMongoMsgAndResetRedisSeq(operationID, userID string) error {
2 years ago
var delMsgIDList [][2]interface{}
2 years ago
minSeq, err := deleteMongoMsg(operationID, userID, oldestList, &delMsgIDList)
2 years ago
if err != nil {
return utils.Wrap(err, "")
}
2 years ago
if minSeq == 0 {
return nil
}
2 years ago
log.NewDebug(operationID, utils.GetSelfFuncName(), "delMsgIDMap: ", delMsgIDList, "minSeq", minSeq)
2 years ago
err = db.DB.SetUserMinSeq(userID, minSeq)
2 years ago
return utils.Wrap(err, "")
2 years ago
}
2 years ago
// del list
func delMongoMsgsPhysical(delMsgIDList *[][2]interface{}) error {
2 years ago
if len(*delMsgIDList) > 0 {
var IDList []string
for _, v := range *delMsgIDList {
IDList = append(IDList, v[0].(string))
}
err := db.DB.DelMongoMsgs(IDList)
if err != nil {
return utils.Wrap(err, "DelMongoMsgs failed")
}
}
return nil
}
2 years ago
// index 0....19(del) 20...69
// seq 70
// set minSeq 21
2 years ago
// recursion
2 years ago
func deleteMongoMsg(operationID string, ID string, index int64, delMsgIDList *[][2]interface{}) (uint32, error) {
2 years ago
// delMsgIDList [[uid:0, minSeq], [uid:1, minSeq]]
// find from oldest list
2 years ago
msgs, err := db.DB.GetUserMsgListByIndex(ID, index)
2 years ago
if err != nil || msgs.UID == "" {
if err != nil {
2 years ago
if err == db.ErrMsgListNotExist {
2 years ago
log.NewDebug(operationID, utils.GetSelfFuncName(), "ID:", ID, "index:", index, err.Error())
2 years ago
} else {
log.NewError(operationID, utils.GetSelfFuncName(), "GetUserMsgListByIndex failed", err.Error(), index, ID)
}
2 years ago
}
2 years ago
// 获取报错或者获取不到了物理删除并且返回seq
err = delMongoMsgsPhysical(delMsgIDList)
if err != nil {
return 0, err
}
return getDelMaxSeqByIDList(*delMsgIDList) + 1, nil
2 years ago
}
2 years ago
log.NewDebug(operationID, "ID:", ID, "index:", index, "uid:", msgs.UID, "len:", len(msgs.Msg))
2 years ago
if len(msgs.Msg) > db.GetSingleGocMsgNum() {
log.NewWarn(operationID, utils.GetSelfFuncName(), "msgs too large", len(msgs.Msg), msgs.UID)
}
2 years ago
log.NewDebug(operationID, utils.GetSelfFuncName(), "get msgs: ", msgs.UID)
for i, msg := range msgs.Msg {
2 years ago
// 找到列表中不需要删除的消息了, 表示为递归到最后一个块
if utils.GetCurrentTimestampByMill() < msg.SendTime+(int64(config.Config.Mongo.DBRetainChatRecords)*24*60*60*1000) {
// 删除块失败 递归结束 返回0
if err := delMongoMsgsPhysical(delMsgIDList); err != nil {
2 years ago
return 0, err
2 years ago
}
2 years ago
// unMarshall失败 块删除成功 返回块的最大seq+1 设置为最小seq
msgPb := &server_api_params.MsgData{}
if err = proto.Unmarshal(msg.Msg, msgPb); err != nil {
return getDelMaxSeqByIDList(*delMsgIDList) + 1, utils.Wrap(err, "")
}
// 如果不是块中第一个,就把前面比他早插入的全部设置空。
2 years ago
if i > 0 {
2 years ago
err = db.DB.ReplaceMsgToBlankByIndex(msgs.UID, i-1)
2 years ago
if err != nil {
2 years ago
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), msgs.UID, i)
return getDelMaxSeqByIDList(*delMsgIDList) + 1, utils.Wrap(err, "")
2 years ago
}
}
2 years ago
// 递归结束
2 years ago
return msgPb.Seq, nil
2 years ago
}
}
2 years ago
// 该列表中消息全部为老消息并且列表满了, 加入删除列表继续递归
if msgListIsFull(msgs) {
2 years ago
msgPb := &server_api_params.MsgData{}
err = proto.Unmarshal(msgs.Msg[len(msgs.Msg)-1].Msg, msgPb)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), len(msgs.Msg)-1, msgs.UID)
return 0, utils.Wrap(err, "proto.Unmarshal failed")
}
*delMsgIDList = append(*delMsgIDList, [2]interface{}{msgs.UID, msgPb.Seq})
2 years ago
}
2 years ago
// 继续递归 index+1
seq, err := deleteMongoMsg(operationID, ID, index+1, delMsgIDList)
2 years ago
if err != nil {
2 years ago
return seq, utils.Wrap(err, "deleteMongoMsg failed")
2 years ago
}
return seq, nil
}
2 years ago
func msgListIsFull(chat *db.UserChat) bool {
index, _ := strconv.Atoi(strings.Split(chat.UID, ":")[1])
if index == 0 {
if len(chat.Msg) >= 4999 {
return true
}
}
if len(chat.Msg) >= 5000 {
return true
}
return false
}
2 years ago
func getDelMaxSeqByIDList(delMsgIDList [][2]interface{}) uint32 {
if len(delMsgIDList) == 0 {
2 years ago
return 0
}
2 years ago
return delMsgIDList[len(delMsgIDList)-1][1].(uint32)
2 years ago
}
2 years ago
func checkMaxSeqWithMongo(operationID, ID string, diffusionType int) error {
2 years ago
var seqRedis uint64
2 years ago
var err error
if diffusionType == constant.WriteDiffusion {
2 years ago
seqRedis, err = db.DB.GetUserMaxSeq(ID)
2 years ago
} else {
2 years ago
seqRedis, err = db.DB.GetGroupMaxSeq(ID)
2 years ago
}
if err != nil {
2 years ago
if err == goRedis.Nil {
return nil
}
2 years ago
return utils.Wrap(err, "GetUserMaxSeq failed")
}
msg, err := db.DB.GetNewestMsg(ID)
if err != nil {
return utils.Wrap(err, "GetNewestMsg failed")
}
2 years ago
if msg == nil {
return nil
}
2 years ago
var seqMongo uint32
2 years ago
msgPb := &server_api_params.MsgData{}
err = proto.Unmarshal(msg.Msg, msgPb)
if err != nil {
return utils.Wrap(err, "")
2 years ago
}
2 years ago
seqMongo = msgPb.Seq
2 years ago
if math.Abs(float64(seqMongo-uint32(seqRedis))) > 10 {
2 years ago
log.NewWarn(operationID, utils.GetSelfFuncName(), seqMongo, seqRedis, "redis maxSeq is different with msg.Seq > 10", ID, diffusionType)
2 years ago
} else {
2 years ago
log.NewInfo(operationID, utils.GetSelfFuncName(), diffusionType, ID, "seq and msg OK", seqMongo, seqRedis)
2 years ago
}
return nil
}