redis应用
消息序列号和未读消息计数
消息序列号(msg_id)
应用场景:DB_PROXY:sendMessage发送消息的时候使用(保证聊天消息的有序性)
void sendMessage(ClmPdu.uint32_tconn_uuid)
·单聊:CMessageModel:sendMessage
·群聊:CGroupMessageModel::sendMessage
- 单聊RelateId的的设计思路?
分析:
–用户关系表,标识两个用户之间的唯一关系id,用于消息分表。relationId % 消息表数目。
CREATE TABLE `IMRelationShip` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`smallId` int(11) unsigned NOT NULL COMMENT '用户A的id',
`bigId` int(11) unsigned NOT NULL COMMENT '用户B的id',
`status` tinyint(1) unsigned DEFAULT '0' COMMENT '用户:0-正常, 1-用户A删除,群组:0-正常, 1-被删除',
`created` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
`updated` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_smallId_bigId_status_updated` (`smallId`,`bigId`,`status`,`updated`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
答:RelateId 来自于mysql的自增id,取决于两个人之间的id映射。
- 群聊msg_id和单聊的区别
- 消息ID为什么这么设计?
分析如下。
消息计数之单聊
存储在unread连接池所在的redis数据库
key设计:“msg_id_ + nRelateld”
uint32_t CMessageModel::getMsgId(uint32_t nRelateId)
{
uint32_t nMsgId = 0;
CacheManager* pCacheManager = CacheManager::getInstance();
CacheConn* pCacheConn = pCacheManager->GetCacheConn("unread");
if(pCacheConn)
{
string strKey = "msg_id_" + int2string(nRelateId);
nMsgId = pCacheConn->incrBy(strKey, 1);
pCacheManager->RelCacheConn(pCacheConn);
}
return nMsgId;
}
消息计数之群聊
存储在unread连接池所在的redis数据库
key设计:“group_msg_id_ + group_id”
/**
* 获取一个群组的msgId,自增,通过redis控制
*
* @param nGroupId 群Id
*
* @return 返回msgId
*/
uint32_t CGroupMessageModel::getMsgId(uint32_t nGroupId)
{
uint32_t nMsgId = 0;
CacheManager* pCacheManager = CacheManager::getInstance();
CacheConn* pCacheConn = pCacheManager->GetCacheConn("unread");
if(pCacheConn)
{
string strKey = "group_msg_id_" + int2string(nGroupId);
nMsgId = pCacheConn->incrBy(strKey, 1);
pCacheManager->RelCacheConn(pCacheConn);
}
else
{
log("no cache connection for unread");
}
return nMsgId;
}
未读消息计数
- 未读消息为什么不可以用MySQL?
未读消息计数之单聊
key设计:“unread_ + int2string(nUserld)”
使用一个hash存储同一个user_id对应不同聊天的未读消息数量
暂时无法在飞书文档外展示此内容
发送消息时,写入数据库后;增加对方未读消息计数调用 incMsgCount 函数接口。
void CMessageModel::incMsgCount(uint32_t nFromId, uint32_t nToId)
{
CacheManager* pCacheManager = CacheManager::getInstance();
// increase message count
CacheConn* pCacheConn = pCacheManager->GetCacheConn("unread");
if (pCacheConn) {
pCacheConn->hincrBy("unread_" + int2string(nToId), int2string(nFromId), 1);
pCacheManager->RelCacheConn(pCacheConn);
} else {
log("no cache connection to increase unread count: %d->%d", nFromId, nToId);
}
}
未读消息计数之群聊
思考:一千人的大群,1个人发信息,其他人都不在线,给每个群成员做未读计数吗?
群聊的未读消息机制如果和单聊做同样样的设计?即收到群聊后对每个群成员聊天数量+1?答:效率低下。
方案如下:
- 一个群groud_id对应多个user_id
- 同一个groud_id,不同的user_id对应的未读消息数量是不一样的
- 每次发消息时群消息数量+1,发消息的个人计数也+1
- 未读消息数量:群消息数量 - 个人已读消息数量
对于群组未读消息,每个人都是不一样的,所以设计如下:
key为"nGroupld_ + nUserld"结合。
#define GROUP_TOTAL_MSG_COUNTER_REDIS_KEY_SUFFIX "_im_group_msg"
#define GROUP_USER_MSG_COUNTER_REDIS_KEY_SUFFIX "_im_user_group"
#define GROUP_COUNTER_SUBKEY_COUNTER_FIELD "count"
作为消息发送者,首先增加群消息总共的消息数量,同时在发送群聊的时候也要把自己的消息计数设置成和群消息数量一样(即没有未读计数)。
/**
* 增加群消息计数
*
* @param nUserId 用户Id
* @param nGroupId 群组Id
*
* @return 成功返回true,失败返回false
*/
bool CGroupMessageModel::incMessageCount(uint32_t nUserId, uint32_t nGroupId)
{
bool bRet = false;
CacheManager* pCacheManager = CacheManager::getInstance();
CacheConn* pCacheConn = pCacheManager->GetCacheConn("unread");
if (pCacheConn)
{
string strGroupKey = int2string(nGroupId) + GROUP_TOTAL_MSG_COUNTER_REDIS_KEY_SUFFIX;
pCacheConn->hincrBy(strGroupKey, GROUP_COUNTER_SUBKEY_COUNTER_FIELD, 1);
map<string, string> mapGroupCount;
bool bRet = pCacheConn->hgetAll(strGroupKey, mapGroupCount);
if(bRet)
{
string strUserKey = int2string(nUserId) + "_" + int2string(nGroupId) + GROUP_USER_MSG_COUNTER_REDIS_KEY_SUFFIX;
string strReply = pCacheConn->hmset(strUserKey, mapGroupCount);
if(!strReply.empty())
{
bRet = true;
}
else
{
log("hmset %s failed !", strUserKey.c_str());
}
}
else
{
log("hgetAll %s failed!", strGroupKey.c_str());
}
pCacheManager->RelCacheConn(pCacheConn);
}
else
{
log("no cache connection for unread");
}
return bRet;
}
清除未读消息计数
单聊和群聊:
客户端读取消息后要发送CID_MSG_READ_ACK给服务器,服务器根据该命令清除未读消息计数。
- 单聊:直接删除"unread_nUserId" 的key
- 群聊:更新 “user_id+group_id+_im_user_group”(自己已经读取的消息数量) 对应的value和"group_id+_im_group_msg"(群总共的消息数量)一致
m_handler_map.insert(make_pair(
uint32_t(CID_MSG_READ_ACK), DB_PROXY::clearUnreadMsgCounter));
void clearUnreadMsgCounter(CImPdu* pPdu, uint32_t conn_uuid)
{
IM::Message::IMMsgDataReadAck msg;
if(msg.ParseFromArray(pPdu->GetBodyData(), pPdu->GetBodyLength()))
{
uint32_t nUserId = msg.user_id();
uint32_t nFromId = msg.session_id();
IM::BaseDefine::SessionType nSessionType = msg.session_type();
CUserModel::getInstance()->clearUserCounter(nUserId, nFromId, nSessionType);
log("userId=%u, peerId=%u, type=%u", nFromId, nUserId, nSessionType);
}
else
{
log("parse pb failed");
}
}
void CUserModel::clearUserCounter(uint32_t nUserId, uint32_t nPeerId, IM::BaseDefine::SessionType nSessionType)
{
if(IM::BaseDefine::SessionType_IsValid(nSessionType))
{
CacheManager* pCacheManager = CacheManager::getInstance();
CacheConn* pCacheConn = pCacheManager->GetCacheConn("unread");
if (pCacheConn)
{
// Clear P2P msg Counter
if(nSessionType == IM::BaseDefine::SESSION_TYPE_SINGLE)
{
int nRet = pCacheConn->hdel("unread_" + int2string(nUserId), int2string(nPeerId));
if(!nRet)
{
log("hdel failed %d->%d", nPeerId, nUserId);
}
}
// Clear Group msg Counter
else if(nSessionType == IM::BaseDefine::SESSION_TYPE_GROUP)
{
string strGroupKey = int2string(nPeerId) + GROUP_TOTAL_MSG_COUNTER_REDIS_KEY_SUFFIX;
map<string, string> mapGroupCount;
bool bRet = pCacheConn->hgetAll(strGroupKey, mapGroupCount);
if(bRet)
{
string strUserKey = int2string(nUserId) + "_" + int2string(nPeerId) + GROUP_USER_MSG_COUNTER_REDIS_KEY_SUFFIX;
string strReply = pCacheConn->hmset(strUserKey, mapGroupCount);
if(strReply.empty()) {
log("hmset %s failed !", strUserKey.c_str());
}
}
else
{
log("hgetall %s failed!", strGroupKey.c_str());
}
}
pCacheManager->RelCacheConn(pCacheConn);
}
else
{
log("no cache connection for unread");
}
}
else{
log("invalid sessionType. userId=%u, fromId=%u, sessionType=%u", nUserId, nPeerId, nSessionType);
}
}
信令和协议设计
enum MessageCmdID {
// ...... 省略无关逻辑
CID_MSG_UNREAD_CNT_REQUEST = 775,
CID_MSG_UNREAD_CNT_RESPONSE = 776,
// ...... 省略无关逻辑
};
message IMUnreadMsgCntReq{
//cmd id: 0x0307
required uint32 user_id = 1;
optional bytes attach_data = 20;
}
message IMUnreadMsgCntRsp{
//cmd id: 0x0308
required uint32 user_id = 1;
required uint32 total_cnt = 2; // 多个人的未读消息
repeated IM.BaseDefine.UnreadInfo unreadinfo_list = 3;
optional bytes attach_data = 20;
}
message UnreadInfo{
required uint32 session_id = 1; // 会话ID
required SessionType session_type = 2; // 会话类型
required uint32 unread_cnt = 3; // 未读消息数量
required uint32 latest_msg_id = 4; // 最新的消息ID
required bytes latest_msg_data = 5; // 最新的消息
required MsgType latest_msg_type = 6; // 消息类型
required uint32 latest_msg_from_user_id = 7; //发送的用户id
}
流程图:
代码分析
msg_server收到CID_MSG_UNREAD_CNT_REQUEST后调用 CMsgConn::_HandleClientUnreadMsgCntRequest 函数
void CMsgConn::HandlePdu(CImPdu* pPdu)
{
// ...... 省略无关逻辑
switch (pPdu->GetCommandId()) {
// ...... 省略无关逻辑
case CID_MSG_UNREAD_CNT_REQUEST:
_HandleClientUnreadMsgCntRequest(pPdu );
break;
// ...... 省略无关逻辑
}
}
void CMsgConn::_HandleClientUnreadMsgCntRequest(CImPdu* pPdu)
{
log("HandleClientUnreadMsgCntReq, from_id=%u ", GetUserId());
IM::Message::IMUnreadMsgCntReq msg;
CHECK_PB_PARSE_MSG(msg.ParseFromArray(pPdu->GetBodyData(), pPdu->GetBodyLength()));
CDBServConn* pDBConn = get_db_serv_conn_for_login();
if (pDBConn) {
CDbAttachData attach(ATTACH_TYPE_HANDLE, m_handle, 0);
msg.set_user_id(GetUserId());
msg.set_attach_data(attach.GetBuffer(), attach.GetLength());
pPdu->SetPBMsg(&msg);
pDBConn->SendPdu(pPdu);
}
}
db_proxy_server收到CID_MSG_UNREAD_CNT_REQUEST后调用 DB_PROXY::getUnreadMsgCounter函数
值得注意的是,返回的未读消息里面包含每个会话的未读消息个数,消息类型,最后一条消息。
m_handler_map.insert(make_pair(uint32_t(CID_MSG_UNREAD_CNT_REQUEST), DB_PROXY::getUnreadMsgCounter));
void getUnreadMsgCounter(CImPdu* pPdu, uint32_t conn_uuid)
{
IM::Message::IMUnreadMsgCntReq msg;
IM::Message::IMUnreadMsgCntRsp msgResp;
if(msg.ParseFromArray(pPdu->GetBodyData(), pPdu->GetBodyLength()))
{
CImPdu* pPduResp = new CImPdu;
uint32_t nUserId = msg.user_id();
list<IM::BaseDefine::UnreadInfo> lsUnreadCount;
uint32_t nTotalCnt = 0;
// 从redis获取未读消息数量 和 从mysql获取最后一条未读消息
CMessageModel::getInstance()->getUnreadMsgCount(nUserId, nTotalCnt, lsUnreadCount);
CGroupMessageModel::getInstance()->getUnreadMsgCount(nUserId, nTotalCnt, lsUnreadCount);
msgResp.set_user_id(nUserId);
msgResp.set_total_cnt(nTotalCnt);
for(auto it= lsUnreadCount.begin(); it!=lsUnreadCount.end(); ++it)
{
IM::BaseDefine::UnreadInfo* pInfo = msgResp.add_unreadinfo_list();
pInfo->set_session_id(it->session_id());
pInfo->set_session_type(it->session_type());
pInfo->set_unread_cnt(it->unread_cnt());
pInfo->set_latest_msg_id(it->latest_msg_id());
pInfo->set_latest_msg_data(it->latest_msg_data());
pInfo->set_latest_msg_type(it->latest_msg_type());
pInfo->set_latest_msg_from_user_id(it->latest_msg_from_user_id());
}
log("userId=%d, unreadCnt=%u, totalCount=%u", nUserId, msgResp.unreadinfo_list_size(), nTotalCnt);
msgResp.set_attach_data(msg.attach_data());
pPduResp->SetPBMsg(&msgResp);
pPduResp->SetSeqNum(pPdu->GetSeqNum());
pPduResp->SetServiceId(IM::BaseDefine::SID_MSG);
pPduResp->SetCommandId(IM::BaseDefine::CID_MSG_UNREAD_CNT_RESPONSE);
CProxyConn::AddResponsePdu(conn_uuid, pPduResp);
}
else
{
log("parse pb failed");
}
}
void CMessageModel::getUnreadMsgCount(uint32_t nUserId, uint32_t &nTotalCnt, list<IM::BaseDefine::UnreadInfo>& lsUnreadCount)
{
// redis
CacheManager* pCacheManager = CacheManager::getInstance();
CacheConn* pCacheConn = pCacheManager->GetCacheConn("unread");
if (pCacheConn)
{
map<string, string> mapUnread;
string strKey = "unread_" + int2string(nUserId);
bool bRet = pCacheConn->hgetAll(strKey, mapUnread);
pCacheManager->RelCacheConn(pCacheConn);
if(bRet)
{
IM::BaseDefine::UnreadInfo cUnreadInfo;
for (auto it = mapUnread.begin(); it != mapUnread.end(); it++) {
cUnreadInfo.set_session_id(atoi(it->first.c_str()));
cUnreadInfo.set_unread_cnt(atoi(it->second.c_str()));
cUnreadInfo.set_session_type(IM::BaseDefine::SESSION_TYPE_SINGLE);
uint32_t nMsgId = 0;
string strMsgData;
IM::BaseDefine::MsgType nMsgType;
// 从mysql获取最后一条未读消息 mysql
getLastMsg(cUnreadInfo.session_id(), nUserId, nMsgId, strMsgData, nMsgType);
if(IM::BaseDefine::MsgType_IsValid(nMsgType))
{
cUnreadInfo.set_latest_msg_id(nMsgId);
cUnreadInfo.set_latest_msg_data(strMsgData);
cUnreadInfo.set_latest_msg_type(nMsgType);
cUnreadInfo.set_latest_msg_from_user_id(cUnreadInfo.session_id());
lsUnreadCount.push_back(cUnreadInfo);
nTotalCnt += cUnreadInfo.unread_cnt();
}
else
{
log("invalid msgType. userId=%u, peerId=%u, msgType=%u", nUserId, cUnreadInfo.session_id(), nMsgType);
}
}
}
else
{
log("hgetall %s failed!", strKey.c_str());
}
}
else
{
log("no cache connection for unread");
}
}
void CMessageModel::getLastMsg(uint32_t nFromId, uint32_t nToId, uint32_t& nMsgId, string& strMsgData, IM::BaseDefine::MsgType& nMsgType, uint32_t nStatus)
{
uint32_t nRelateId = CRelationModel::getInstance()->getRelationId(nFromId, nToId, false);
if (nRelateId != INVALID_VALUE)
{
CDBManager* pDBManager = CDBManager::getInstance();
// 读从库
CDBConn* pDBConn = pDBManager->GetDBConn("teamtalk_slave");
if (pDBConn)
{
string strTableName = "IMMessage_" + int2string(nRelateId % 8);
string strSql = "select msgId,type,content from " + strTableName + " force index (idx_relateId_status_created) where relateId= " + int2string(nRelateId) + " and status = 0 order by created desc, id desc limit 1";
CResultSet* pResultSet = pDBConn->ExecuteQuery(strSql.c_str());
if (pResultSet)
{
while (pResultSet->Next())
{
nMsgId = pResultSet->GetInt("msgId");
nMsgType = IM::BaseDefine::MsgType(pResultSet->GetInt("type"));
if (nMsgType == IM::BaseDefine::MSG_TYPE_SINGLE_AUDIO)
{
// "[语音]"加密后的字符串
strMsgData = strAudioEnc;
}
else
{
strMsgData = pResultSet->GetString("content");
}
}
delete pResultSet;
}
else
{
log("no result set: %s", strSql.c_str());
}
pDBManager->RelDBConn(pDBConn);
}
else
{
log("no db connection_slave");
}
}
else
{
log("no relation between %lu and %lu", nFromId, nToId);
}
}
db_proxy_server回复信令CID_MSG_UNREAD_CNT_RESPONSE给msg_server,调用CDBServConn::_HandleUnreadMsgCountResponse
void CDBServConn::_HandleUnreadMsgCountResponse(CImPdu* pPdu)
{
IM::Message::IMUnreadMsgCntRsp msg;
CHECK_PB_PARSE_MSG(msg.ParseFromArray(pPdu->GetBodyData(), pPdu->GetBodyLength()));
uint32_t user_id = msg.user_id();
uint32_t total_cnt = msg.total_cnt();
uint32_t user_unread_cnt = msg.unreadinfo_list_size();
CDbAttachData attach_data((uchar_t*)msg.attach_data().c_str(), msg.attach_data().length());
uint32_t handle = attach_data.GetHandle();
log("HandleUnreadMsgCntResp, userId=%u, total_cnt=%u, user_unread_cnt=%u.", user_id,
total_cnt, user_unread_cnt);
CMsgConn* pMsgConn = CImUserManager::GetInstance()->GetMsgConnByHandle(user_id, handle);
if (pMsgConn && pMsgConn->IsOpen()) {
msg.clear_attach_data();
pPdu->SetPBMsg(&msg);
pMsgConn->SendPdu(pPdu);
}
}