Example #1
0
///master变化的处理函数
void UnistorHandler4Recv::doEvent(UnistorApp* pApp,
                                  UnistorTss* tss,
                                  CwxMsgBlock*& msg,
                                  CWX_UINT32 uiPoolIndex)
{
    if (EVENT_SEND_MSG == msg->event().getEvent()){ ///发送消息
        UnistorHandler4Recv* pHandler =((UnistorRecvThreadUserObj*)tss->getUserObj())->getConn(msg->event().getConnId());
        UnistorWriteMsgArg* pWriteArg=(UnistorWriteMsgArg*)msg->event().getConnUserData();
        if (pWriteArg){///<存在两种,一种是来自write thread的,一种是来自trans thread的。只有write thead需要释放
            msg->event().setConnUserData(NULL);
            CwxMsgBlockAlloc::free(pWriteArg->m_recvMsg);
            tss->pushWriteMsgArg(pWriteArg);
        }
        if (pHandler){
            pHandler->reply(msg, false);
            msg = NULL;
        }
    }else if (CwxEventInfo::CONN_CREATED == msg->event().getEvent()){///连接建立
        CwxAppChannel* channel = pApp->getRecvChannels()[uiPoolIndex];
        if (channel->isRegIoHandle(msg->event().getIoHandle())){
            CWX_ERROR(("Handler[%] is register, it's a big bug, stop.", msg->event().getIoHandle()));
            pApp->stop();
            return;
        }
        UnistorHandler4Recv* pHandler = new UnistorHandler4Recv(pApp,
            pApp->reactor()->getNextConnId(),
            uiPoolIndex,
            channel);
        CwxINetAddr  remoteAddr;
        CwxSockStream stream(msg->event().getIoHandle());
        stream.getRemoteAddr(remoteAddr);
        pHandler->m_unPeerPort = remoteAddr.getPort();
        if (remoteAddr.getHostIp(tss->m_szBuf2K, 2047)){
            pHandler->m_strPeerHost = tss->m_szBuf2K;
        }
        pHandler->setHandle(msg->event().getIoHandle());
        pHandler->m_tss = (UnistorTss*)CwxTss::instance();
        if (0 != pHandler->open()){
            CWX_ERROR(("Failure to register handler[%d] from:%s:%u", pHandler->getHandle(), pHandler->m_strPeerHost.c_str(), pHandler->m_unPeerPort));
            delete pHandler;
            return;
        }
    }else if (EVENT_ZK_CONF_CHANGE == msg->event().getEvent()){
        UnistorZkConf* pConf = NULL;
        memcpy(&pConf, msg->rd_ptr(), sizeof(pConf));
        if (tss->m_pZkConf){
            if (tss->m_pZkConf->m_ullVersion > pConf->m_ullVersion){///<采用旧版本
                delete pConf;
            }else{///采用新版本
                delete tss->m_pZkConf;
                tss->m_pZkConf = pConf;
            }
        }else{///<采用新版本
            tss->m_pZkConf = pConf;
        }
        CWX_INFO(("UnistorHandler4Recv[thread:%u]: ZK config is changed. master_idc:%s, is_master_idc:%s, master_host:%s, is_master=%s, sync_host:%s",
            tss->m_uiThreadIndex,
            tss->getMasterIdc(),
            tss->isMasterIdc()?"yes":"no",
            tss->getMasterHost(),
            tss->isMaster()?"yes":"no",
            tss->getSyncHost()));

    }else if (EVENT_ZK_LOCK_CHANGE == msg->event().getEvent()){
        UnistorZkLock* pLock = NULL;
        memcpy(&pLock, msg->rd_ptr(), sizeof(pLock));
        if (tss->m_pZkLock){
            if (tss->m_pZkLock->m_ullVersion > pLock->m_ullVersion){///<采用旧版本
                delete pLock;
            }else{///采用新版本
                delete tss->m_pZkLock;
                tss->m_pZkLock = pLock;
            }
        }else{///<采用新版本
            tss->m_pZkLock = pLock;
        }
        CWX_INFO(("UnistorHandler4Recv[thread:%u]: ZK config is changed. master_idc:%s, is_master_idc:%s, master_host:%s, is_master=%s, sync_host:%s",
            tss->m_uiThreadIndex,
            tss->getMasterIdc(),
            tss->isMasterIdc()?"yes":"no",
            tss->getMasterHost(),
            tss->isMaster()?"yes":"no",
            tss->getSyncHost()));

    }else{
        CWX_ERROR(("Unkwown event type:%d", msg->event().getEvent()));
    }
}
void CwxMqInnerDispHandler::doEvent(CwxMqApp* app, CwxMqTss* tss, CwxMsgBlock*& msg) {
  if (CwxEventInfo::CONN_CREATED == msg->event().getEvent()) { ///连接建立
    CwxAppChannel* channel = app->getInnerDispChannel();
    if (channel->isRegIoHandle(msg->event().getIoHandle())) {
      CWX_ERROR(("Handler[%d] is register, it's a big bug. exit....", msg->event().getIoHandle()));
      app->stop();
      return ;
    }
    CwxMqInnerDispHandler* pHandler = new CwxMqInnerDispHandler(app, channel,
        app->reactor()->getNextConnId());
    ///获取连接的来源信息
    CwxINetAddr remoteAddr;
    CwxSockStream stream(msg->event().getIoHandle());
    stream.getRemoteAddr(remoteAddr);
    pHandler->m_unPeerPort = remoteAddr.getPort();
    if (remoteAddr.getHostIp(tss->m_szBuf2K, 2047)) {
      pHandler->m_strPeerHost = tss->m_szBuf2K;
    }
    ///设置handle的io后,open handler
    pHandler->setHandle(msg->event().getIoHandle());
    if (0 != pHandler->open()) {
      CWX_ERROR(("Failure to register sync handler[%d], from:%s:%u", pHandler->getHandle(),
          pHandler->m_strPeerHost.c_str(), pHandler->m_unPeerPort));
      delete pHandler;
      return ;
    }
    ///设置对象的tss对象
    pHandler->m_tss = (CwxMqTss*)CwxTss::instance();
    CWX_INFO(("Accept inner-sync connection from %s:%u", pHandler->m_strPeerHost.c_str(), pHandler->m_unPeerPort));
  } else if (EVENT_ZK_TOPIC_CHANGE == msg->event().getEvent()) { ///topic变化通知
    map<string, CWX_UINT8>* changedTopics = NULL;
    memcpy(&changedTopics, msg->rd_ptr(), sizeof(changedTopics));
    if (changedTopics->size()) {
      map<string, CWX_UINT8>::iterator iter = changedTopics->begin();
      for (;iter != changedTopics->end(); iter++) {
        map<CWX_UINT64, CwxMqInnerDispSession*>::iterator ses_iter = m_sessions.begin();
        for (;ses_iter != m_sessions.end();ses_iter++) {
          ses_iter->second->m_topicIsChanged = true;
          ///忽略topic/状态改变
          if ( iter->second == CWX_MQ_TOPIC_DELETE ||
              ses_iter->second->m_binlogCursor.find(iter->first) != ses_iter->second->m_binlogCursor.end()) continue;
          {///新增topic
            CwxBinLogMgr* binlogMgr = app->getTopicMgr()->getBinlogMgrByTopic(iter->first);
            if (!binlogMgr) {
              CWX_ERROR(("Failure to get binlogMgr by topic:%s", iter->first.c_str()));
              break;
            }
            CwxBinLogCursor* pCursor = binlogMgr->createCurser(ses_iter->second->m_ullStartSid);
            if (!pCursor) {
              CWX_ERROR(("Failure to createCursor by topic:%s", iter->first.c_str()));
              app->getTopicMgr()->freeBinlogMgrByTopic(iter->first, binlogMgr);
              break;
            }
            CwxBinlogMgrCursor* item = new CwxBinlogMgrCursor(iter->first, binlogMgr, pCursor);
            ses_iter->second->m_unReadyBinlogCursor.push_back(item);
            ses_iter->second->m_binlogCursor[iter->first] = item;
          }
        }
      }
    }
    delete changedTopics;
  } else {
    CWX_ERROR(("Recv unkown event:%d", msg->event().getEvent()));
    CWX_ASSERT(msg->event().getEvent() == CwxEventInfo::TIMEOUT_CHECK);
    CWX_ASSERT(msg->event().getSvrId() == CwxMqApp::SVR_TYPE_INNER_DISP);
  }
}