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/crontask/clear_msg.go

194 lines
5.9 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"
2 years ago
"Open_IM/pkg/common/db/mongo"
2 years ago
"Open_IM/pkg/common/log"
2 years ago
sdkws "Open_IM/pkg/proto/sdkws"
2 years ago
"Open_IM/pkg/utils"
2 years ago
"math"
2 years ago
"strconv"
"strings"
2 years ago
goRedis "github.com/go-redis/redis/v8"
"github.com/golang/protobuf/proto"
2 years ago
)
const oldestList = 0
const newestList = -1
2 years ago
func ResetUserGroupMinSeq(operationID, groupID string, userIDList []string) error {
2 years ago
var delStruct delMsgRecursionStruct
minSeq, err := deleteMongoMsg(operationID, groupID, oldestList, &delStruct)
2 years ago
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:", delStruct, "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 delStruct delMsgRecursionStruct
minSeq, err := deleteMongoMsg(operationID, userID, oldestList, &delStruct)
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(), "delMsgIDStruct: ", delStruct, "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
2 years ago
func delMongoMsgsPhysical(uidList []string) error {
if len(uidList) > 0 {
err := db.DB.DelMongoMsgs(uidList)
2 years ago
if err != nil {
return utils.Wrap(err, "DelMongoMsgs failed")
}
}
return nil
}
2 years ago
type delMsgRecursionStruct struct {
minSeq uint32
delUidList []string
}
func (d *delMsgRecursionStruct) getSetMinSeq() uint32 {
return d.minSeq
}
2 years ago
// index 0....19(del) 20...69
// seq 70
// set minSeq 21
2 years ago
// recursion 删除list并且返回设置的最小seq
2 years ago
func deleteMongoMsg(operationID string, ID string, index int64, delStruct *delMsgRecursionStruct) (uint32, error) {
2 years ago
// 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 {
if err == mongoDB.ErrMsgListNotExist {
2 years ago
log.NewInfo(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
2 years ago
err = delMongoMsgsPhysical(delStruct.delUidList)
2 years ago
if err != nil {
return 0, err
}
2 years ago
return delStruct.getSetMinSeq() + 1, nil
2 years ago
}
2 years ago
log.NewDebug(operationID, "ID:", ID, "index:", index, "uid:", msgs.UID, "len:", len(msgs.Msg))
if len(msgs.Msg) > mongoDB.GetSingleGocMsgNum() {
2 years ago
log.NewWarn(operationID, utils.GetSelfFuncName(), "msgs too large", len(msgs.Msg), msgs.UID)
}
2 years ago
if msgs.Msg[len(msgs.Msg)-1].SendTime+(int64(config.Config.Mongo.DBRetainChatRecords)*24*60*60*1000) > utils.GetCurrentTimestampByMill() && msgListIsFull(msgs) {
2 years ago
delStruct.delUidList = append(delStruct.delUidList, msgs.UID)
2 years ago
lastMsgPb := &sdkws.MsgData{}
2 years ago
err = proto.Unmarshal(msgs.Msg[len(msgs.Msg)-1].Msg, lastMsgPb)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), len(msgs.Msg)-1, msgs.UID)
return 0, utils.Wrap(err, "proto.Unmarshal failed")
}
delStruct.minSeq = lastMsgPb.Seq
2 years ago
} else {
2 years ago
var hasMarkDelFlag bool
for _, msg := range msgs.Msg {
2 years ago
msgPb := &sdkws.MsgData{}
2 years ago
err = proto.Unmarshal(msg.Msg, msgPb)
2 years ago
if err != nil {
2 years ago
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), len(msgs.Msg)-1, msgs.UID)
return 0, utils.Wrap(err, "proto.Unmarshal failed")
}
if utils.GetCurrentTimestampByMill() > msg.SendTime+(int64(config.Config.Mongo.DBRetainChatRecords)*24*60*60*1000) {
msgPb.Status = constant.MsgDeleted
bytes, _ := proto.Marshal(msgPb)
msg.Msg = bytes
msg.SendTime = 0
hasMarkDelFlag = true
} else {
if err := delMongoMsgsPhysical(delStruct.delUidList); err != nil {
return 0, err
2 years ago
}
2 years ago
if hasMarkDelFlag {
if err := db.DB.UpdateOneMsgList(msgs); err != nil {
return delStruct.getSetMinSeq(), utils.Wrap(err, "")
}
}
return msgPb.Seq + 1, nil
2 years ago
}
}
2 years ago
}
2 years ago
log.NewDebug(operationID, ID, "continue to", delStruct)
2 years ago
// 继续递归 index+1
2 years ago
seq, err := deleteMongoMsg(operationID, ID, index+1, delStruct)
2 years ago
return seq, utils.Wrap(err, "deleteMongoMsg failed")
2 years ago
}
func msgListIsFull(chat *mongoDB.UserChat) bool {
2 years ago
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 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
}
if math.Abs(float64(msg.Seq-uint32(seqRedis))) > 10 {
log.NewWarn(operationID, utils.GetSelfFuncName(), "seqMongo, seqRedis", msg.Seq, seqRedis, ID, "redis maxSeq is different with msg.Seq > 10", "status: ", msg.Status, msg.SendTime)
2 years ago
} else {
log.NewInfo(operationID, utils.GetSelfFuncName(), "seqMongo, seqRedis", msg.Seq, seqRedis, ID, "seq and msg OK", "status:", msg.Status, msg.SendTime)
2 years ago
}
return nil
}