mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-01 07:35:58 +08:00
errcode
This commit is contained in:
@@ -0,0 +1,193 @@
|
||||
package cronTask
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db"
|
||||
"Open_IM/pkg/common/db/mongo"
|
||||
"Open_IM/pkg/common/log"
|
||||
server_api_params "Open_IM/pkg/proto/sdk_ws"
|
||||
"Open_IM/pkg/utils"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
goRedis "github.com/go-redis/redis/v8"
|
||||
"github.com/golang/protobuf/proto"
|
||||
)
|
||||
|
||||
const oldestList = 0
|
||||
const newestList = -1
|
||||
|
||||
func ResetUserGroupMinSeq(operationID, groupID string, userIDList []string) error {
|
||||
var delStruct delMsgRecursionStruct
|
||||
minSeq, err := deleteMongoMsg(operationID, groupID, oldestList, &delStruct)
|
||||
if err != nil {
|
||||
log.NewError(operationID, utils.GetSelfFuncName(), groupID, "deleteMongoMsg failed")
|
||||
}
|
||||
if minSeq == 0 {
|
||||
return nil
|
||||
}
|
||||
log.NewDebug(operationID, utils.GetSelfFuncName(), "delMsgIDList:", delStruct, "minSeq", minSeq)
|
||||
for _, userID := range userIDList {
|
||||
userMinSeq, err := db.DB.GetGroupUserMinSeq(groupID, userID)
|
||||
if err != nil && err != goRedis.Nil {
|
||||
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)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteMongoMsgAndResetRedisSeq(operationID, userID string) error {
|
||||
var delStruct delMsgRecursionStruct
|
||||
minSeq, err := deleteMongoMsg(operationID, userID, oldestList, &delStruct)
|
||||
if err != nil {
|
||||
return utils.Wrap(err, "")
|
||||
}
|
||||
if minSeq == 0 {
|
||||
return nil
|
||||
}
|
||||
log.NewDebug(operationID, utils.GetSelfFuncName(), "delMsgIDStruct: ", delStruct, "minSeq", minSeq)
|
||||
err = db.DB.SetUserMinSeq(userID, minSeq)
|
||||
return utils.Wrap(err, "")
|
||||
}
|
||||
|
||||
// del list
|
||||
func delMongoMsgsPhysical(uidList []string) error {
|
||||
if len(uidList) > 0 {
|
||||
err := db.DB.DelMongoMsgs(uidList)
|
||||
if err != nil {
|
||||
return utils.Wrap(err, "DelMongoMsgs failed")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type delMsgRecursionStruct struct {
|
||||
minSeq uint32
|
||||
delUidList []string
|
||||
}
|
||||
|
||||
func (d *delMsgRecursionStruct) getSetMinSeq() uint32 {
|
||||
return d.minSeq
|
||||
}
|
||||
|
||||
// index 0....19(del) 20...69
|
||||
// seq 70
|
||||
// set minSeq 21
|
||||
// recursion 删除list并且返回设置的最小seq
|
||||
func deleteMongoMsg(operationID string, ID string, index int64, delStruct *delMsgRecursionStruct) (uint32, error) {
|
||||
// find from oldest list
|
||||
msgs, err := db.DB.GetUserMsgListByIndex(ID, index)
|
||||
if err != nil || msgs.UID == "" {
|
||||
if err != nil {
|
||||
if err == mongoDB.ErrMsgListNotExist {
|
||||
log.NewInfo(operationID, utils.GetSelfFuncName(), "ID:", ID, "index:", index, err.Error())
|
||||
} else {
|
||||
log.NewError(operationID, utils.GetSelfFuncName(), "GetUserMsgListByIndex failed", err.Error(), index, ID)
|
||||
}
|
||||
}
|
||||
// 获取报错,或者获取不到了,物理删除并且返回seq
|
||||
err = delMongoMsgsPhysical(delStruct.delUidList)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return delStruct.getSetMinSeq() + 1, nil
|
||||
}
|
||||
log.NewDebug(operationID, "ID:", ID, "index:", index, "uid:", msgs.UID, "len:", len(msgs.Msg))
|
||||
if len(msgs.Msg) > mongoDB.GetSingleGocMsgNum() {
|
||||
log.NewWarn(operationID, utils.GetSelfFuncName(), "msgs too large", len(msgs.Msg), msgs.UID)
|
||||
}
|
||||
if msgs.Msg[len(msgs.Msg)-1].SendTime+(int64(config.Config.Mongo.DBRetainChatRecords)*24*60*60*1000) > utils.GetCurrentTimestampByMill() && msgListIsFull(msgs) {
|
||||
delStruct.delUidList = append(delStruct.delUidList, msgs.UID)
|
||||
lastMsgPb := &server_api_params.MsgData{}
|
||||
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
|
||||
} else {
|
||||
var hasMarkDelFlag bool
|
||||
for _, msg := range msgs.Msg {
|
||||
msgPb := &server_api_params.MsgData{}
|
||||
err = proto.Unmarshal(msg.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")
|
||||
}
|
||||
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
|
||||
}
|
||||
if hasMarkDelFlag {
|
||||
if err := db.DB.UpdateOneMsgList(msgs); err != nil {
|
||||
return delStruct.getSetMinSeq(), utils.Wrap(err, "")
|
||||
}
|
||||
}
|
||||
return msgPb.Seq + 1, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
log.NewDebug(operationID, ID, "continue to", delStruct)
|
||||
// 继续递归 index+1
|
||||
seq, err := deleteMongoMsg(operationID, ID, index+1, delStruct)
|
||||
return seq, utils.Wrap(err, "deleteMongoMsg failed")
|
||||
}
|
||||
|
||||
func msgListIsFull(chat *mongoDB.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
|
||||
}
|
||||
|
||||
func checkMaxSeqWithMongo(operationID, ID string, diffusionType int) error {
|
||||
var seqRedis uint64
|
||||
var err error
|
||||
if diffusionType == constant.WriteDiffusion {
|
||||
seqRedis, err = db.DB.GetUserMaxSeq(ID)
|
||||
} else {
|
||||
seqRedis, err = db.DB.GetGroupMaxSeq(ID)
|
||||
}
|
||||
if err != nil {
|
||||
if err == goRedis.Nil {
|
||||
return nil
|
||||
}
|
||||
return utils.Wrap(err, "GetUserMaxSeq failed")
|
||||
}
|
||||
msg, err := db.DB.GetNewestMsg(ID)
|
||||
if err != nil {
|
||||
return utils.Wrap(err, "GetNewestMsg failed")
|
||||
}
|
||||
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)
|
||||
} else {
|
||||
log.NewInfo(operationID, utils.GetSelfFuncName(), "seqMongo, seqRedis", msg.Seq, seqRedis, ID, "seq and msg OK", "status:", msg.Status, msg.SendTime)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package cronTask
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
mongo2 "Open_IM/pkg/common/db/mongo"
|
||||
server_api_params "Open_IM/pkg/proto/sdk_ws"
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/golang/protobuf/proto"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
redisClient *redis.Client
|
||||
mongoClient *mongo.Collection
|
||||
)
|
||||
|
||||
func GenUserChat(startSeq, stopSeq, delSeq, index uint32, userID string) *mongo2.UserChat {
|
||||
chat := &mongo2.UserChat{UID: userID + strconv.Itoa(int(index))}
|
||||
for i := startSeq; i <= stopSeq; i++ {
|
||||
msg := server_api_params.MsgData{
|
||||
SendID: "sendID1",
|
||||
RecvID: "recvID1",
|
||||
GroupID: "",
|
||||
ClientMsgID: "xxx",
|
||||
ServerMsgID: "xxx",
|
||||
SenderPlatformID: 1,
|
||||
SenderNickname: "testNickName",
|
||||
SenderFaceURL: "testFaceURL",
|
||||
SessionType: 1,
|
||||
MsgFrom: 100,
|
||||
ContentType: 101,
|
||||
Content: []byte("testFaceURL"),
|
||||
Seq: uint32(i),
|
||||
SendTime: time.Now().Unix(),
|
||||
CreateTime: time.Now().Unix(),
|
||||
Status: 1,
|
||||
}
|
||||
bytes, _ := proto.Marshal(&msg)
|
||||
sendTime := 0
|
||||
chat.Msg = append(chat.Msg, mongo2.MsgInfo{SendTime: int64(sendTime), Msg: bytes})
|
||||
}
|
||||
return chat
|
||||
}
|
||||
|
||||
func SetUserMaxSeq(userID string, seq int) error {
|
||||
return redisClient.Set(context.Background(), "REDIS_USER_INCR_SEQ"+userID, seq, 0).Err()
|
||||
}
|
||||
|
||||
func CreateChat(userChat *mongo2.UserChat) error {
|
||||
_, err := mongoClient.InsertOne(context.Background(), userChat)
|
||||
return err
|
||||
}
|
||||
|
||||
func TestDeleteMongoMsgAndResetRedisSeq(t *testing.T) {
|
||||
operationID := getCronTaskOperationID()
|
||||
redisClient = redis.NewClient(&redis.Options{
|
||||
Addr: "127.0.0.1:16379",
|
||||
Password: "openIM123", // no password set
|
||||
DB: 13, // use default DB
|
||||
})
|
||||
mongoUri := fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin",
|
||||
"root", "openIM123", "127.0.0.1:37017",
|
||||
"openIM", 100)
|
||||
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(mongoUri))
|
||||
mongoClient = client.Database("openIM").Collection("msg")
|
||||
testUID1 := "test_del_id1"
|
||||
//testUID2 := "test_del_id2"
|
||||
//testUID3 := "test_del_id3"
|
||||
//testUID4 := "test_del_id4"
|
||||
//testUID5 := "test_del_id5"
|
||||
//testUID6 := "test_del_id6"
|
||||
err = SetUserMaxSeq(testUID1, 600)
|
||||
userChat := GenUserChat(1, 500, 200, 0, testUID1)
|
||||
err = CreateChat(userChat)
|
||||
|
||||
if err := DeleteMongoMsgAndResetRedisSeq(operationID, testUID1); err != nil {
|
||||
t.Error("checkMaxSeqWithMongo failed", testUID1)
|
||||
}
|
||||
if err := checkMaxSeqWithMongo(operationID, testUID1, constant.WriteDiffusion); err != nil {
|
||||
t.Error("checkMaxSeqWithMongo failed", testUID1)
|
||||
}
|
||||
if err != nil {
|
||||
t.Error("err is not nil", testUID1, err.Error())
|
||||
}
|
||||
// testWorkingGroupIDList := []string{"test_del_id1", "test_del_id2", "test_del_id3", "test_del_id4", "test_del_id5"}
|
||||
// for _, groupID := range testWorkingGroupIDList {
|
||||
// operationID = groupID + "-" + operationID
|
||||
// log.NewDebug(operationID, utils.GetSelfFuncName(), "groupID:", groupID, "userIDList:", testUserIDList)
|
||||
// if err := ResetUserGroupMinSeq(operationID, groupID, testUserIDList); err != nil {
|
||||
// t.Error("checkMaxSeqWithMongo failed", groupID)
|
||||
// }
|
||||
// if err := checkMaxSeqWithMongo(operationID, groupID, constant.ReadDiffusion); err != nil {
|
||||
// t.Error("checkMaxSeqWithMongo failed", groupID)
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package cronTask
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db/controller"
|
||||
"Open_IM/pkg/common/db/mysql_model/im_mysql_model"
|
||||
rocksCache "Open_IM/pkg/common/db/rocks_cache"
|
||||
"Open_IM/pkg/common/log"
|
||||
"Open_IM/pkg/utils"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
const cronTaskOperationID = "cronTaskOperationID-"
|
||||
|
||||
func StartCronTask(userID, workingGroupID string) {
|
||||
log.NewPrivateLog("cron")
|
||||
log.NewInfo(utils.OperationIDGenerator(), "start cron task", "cron config", config.Config.Mongo.ChatRecordsClearTime)
|
||||
fmt.Println("cron task start, config", config.Config.Mongo.ChatRecordsClearTime)
|
||||
if userID != "" {
|
||||
operationID := getCronTaskOperationID()
|
||||
StartClearMsg(operationID, []string{userID})
|
||||
}
|
||||
if workingGroupID != "" {
|
||||
operationID := getCronTaskOperationID()
|
||||
StartClearWorkingGroupMsg(operationID, []string{workingGroupID})
|
||||
}
|
||||
if userID != "" || workingGroupID != "" {
|
||||
fmt.Println("clear msg finished")
|
||||
return
|
||||
}
|
||||
c := cron.New()
|
||||
_, err := c.AddFunc(config.Config.Mongo.ChatRecordsClearTime, ClearAll)
|
||||
if err != nil {
|
||||
fmt.Println("start cron failed", err.Error(), config.Config.Mongo.ChatRecordsClearTime)
|
||||
panic(err)
|
||||
}
|
||||
c.Start()
|
||||
fmt.Println("start cron task success")
|
||||
for {
|
||||
time.Sleep(10 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func getCronTaskOperationID() string {
|
||||
return cronTaskOperationID + utils.OperationIDGenerator()
|
||||
}
|
||||
|
||||
func ClearAll() {
|
||||
operationID := getCronTaskOperationID()
|
||||
log.NewInfo(operationID, "========================= start del cron task =========================")
|
||||
var err error
|
||||
userIDList, err := im_mysql_model.SelectAllUserID()
|
||||
if err == nil {
|
||||
StartClearMsg(operationID, userIDList)
|
||||
} else {
|
||||
log.NewError(operationID, utils.GetSelfFuncName(), err.Error())
|
||||
}
|
||||
|
||||
// working group msg clear
|
||||
workingGroupIDList, err := im_mysql_model.GetGroupIDListByGroupType(constant.WorkingGroup)
|
||||
if err == nil {
|
||||
StartClearWorkingGroupMsg(operationID, workingGroupIDList)
|
||||
} else {
|
||||
log.NewError(operationID, utils.GetSelfFuncName(), err.Error())
|
||||
}
|
||||
|
||||
log.NewInfo(operationID, "========================= start del cron finished =========================")
|
||||
}
|
||||
|
||||
func StartClearMsg(operationID string, userIDList []string) {
|
||||
log.NewDebug(operationID, utils.GetSelfFuncName(), "userIDList: ", userIDList)
|
||||
for _, userID := range userIDList {
|
||||
if err := DeleteMongoMsgAndResetRedisSeq(operationID, userID); err != nil {
|
||||
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), userID)
|
||||
}
|
||||
if err := checkMaxSeqWithMongo(operationID, userID, constant.WriteDiffusion); err != nil {
|
||||
log.NewError(operationID, utils.GetSelfFuncName(), userID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func StartClearWorkingGroupMsg(operationID string, workingGroupIDList []string) {
|
||||
log.NewDebug(operationID, utils.GetSelfFuncName(), "workingGroupIDList: ", workingGroupIDList)
|
||||
for _, groupID := range workingGroupIDList {
|
||||
userIDList, err := rocksCache.GetGroupMemberIDListFromCache(groupID)
|
||||
if err != nil {
|
||||
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), groupID)
|
||||
continue
|
||||
}
|
||||
log.NewDebug(operationID, utils.GetSelfFuncName(), "groupID:", groupID, "workingGroupIDList:", userIDList)
|
||||
if err := ResetUserGroupMinSeq(operationID, groupID, userIDList); err != nil {
|
||||
log.NewError(operationID, utils.GetSelfFuncName(), err.Error(), groupID, userIDList)
|
||||
}
|
||||
if err := checkMaxSeqWithMongo(operationID, groupID, constant.ReadDiffusion); err != nil {
|
||||
log.NewError(operationID, utils.GetSelfFuncName(), groupID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user