void CwxMqImportConfig::outputConfig() { CWX_INFO(("\n*****************BEGIN CONFIG*******************")); CWX_INFO(("home=%s", m_strWorkDir.c_str())); CWX_INFO( ("listen=%s:%u", m_listen.getHostName().c_str(), m_listen.getPort())); CWX_INFO(("unix=%s", m_strUnixPathFile.c_str())); CWX_INFO(("user=%s", m_strUser.c_str())); CWX_INFO(("passwd=%s", m_strPasswd.c_str())); CWX_INFO(("conn_type=%s", m_bTcp?"tcp":"unix")); CWX_INFO(("conn_num=%u", m_unConnNum)); CWX_INFO(("conn_lasting=%u", m_bLasting?1:0)); CWX_INFO(("data_group=%u", m_uiGroup)); CWX_INFO(("data_size=%u", m_unDataSize)); CWX_INFO(("*****************END CONFIG *******************")); }
int UnistorHandler4Trans::rebuildConn(UnistorApp* app){ if (!m_strMasterHost.length()){ CWX_INFO(("UnistorHandler4Trans: Master is empty, refuse to connect trans connection.")); } if (!m_bRebuildConn) return 0; CWX_UINT32 ttNow = time(NULL); if (m_ttLastRebuildConn + 2 > ttNow){ return 0; } m_ttLastRebuildConn = ttNow; m_bCanTrans = false; ///关闭已有的所有连接 UnistorHandler4Trans* handle = NULL; map<CWX_UINT32, UnistorHandler4Trans*>::iterator iter = m_handlers->begin(); while(iter != m_handlers->end()){ handle = iter->second; m_handlers->erase(iter); handle->close(); iter = m_handlers->begin(); } m_uiAuthConnNum = 0; CWX_INFO(("UnistorHandler4Trans:Rebuild trans connection to %s:%u, user:passwd=%s:%s", m_strMasterHost.c_str(), app->getConfig().getRecv().getPort(), app->getConfig().getRecv().getUser().length()?app->getConfig().getRecv().getUser().c_str():"", app->getConfig().getRecv().getUser().length()?app->getConfig().getRecv().getUser().c_str():"")); ///重建所有连接 CwxINetAddr addr; if (0 != addr.set(app->getConfig().getRecv().getPort(), m_strMasterHost.c_str())){ CWX_ERROR(("Failure to init addr, addr:%s, port:%u, err=%d", m_strMasterHost.c_str(), app->getConfig().getRecv().getPort(), errno)); return -1; } CWX_UINT32 i=0; int* fds = new int[app->getConfig().getCommon().m_uiTranConnNum]; for (i=0; i<app->getConfig().getCommon().m_uiTranConnNum; i++){ fds[i] = -1; } CwxTimeValue timeout(UNISTOR_CONN_TIMEOUT_SECOND); CwxTimeouter timeouter(&timeout); if (0 != UnistorConnector::connect(addr, app->getConfig().getCommon().m_uiTranConnNum, fds, &timeouter, true, UnistorApp::setConnSockAttr, app->getRecvSockAttr())) { CWX_ERROR(("Failure to connect to addr:%s, port:%u, err=%d",m_strMasterHost.c_str(), app->getConfig().getRecv().getPort(), errno)); return -1; } ///将连接注册到channel CwxAppChannel* channel = app->getTransChannel(); for (i=0; i<app->getConfig().getCommon().m_uiTranConnNum; i++){ if (channel->isRegIoHandle(fds[i])){ CWX_ERROR(("Handler[%] is register", fds[i])); break; } UnistorHandler4Trans* pHandler = new UnistorHandler4Trans(app, app->reactor()->getNextConnId(), channel); pHandler->setHandle(fds[i]); if (0 != pHandler->open()){ pHandler->setHandle(-1); delete pHandler; break; } pHandler->m_tss = (UnistorTss*)CwxTss::instance(); ///<对象对应的tss对象 } if (i < app->getConfig().getCommon().m_uiTranConnNum){ for (CWX_UINT32 j=i; j<app->getConfig().getCommon().m_uiTranConnNum; j++){ ::close(fds[j]); } return -1; } m_bRebuildConn = false; return 0; }
void CwxMcConfig::outputConfig() const { CWX_INFO(("\n*****************BEGIN CONFIG*******************")); CWX_INFO(("*****************cmn*******************")); CWX_INFO(("home=%s", m_common.m_strWorkDir.c_str())); CWX_INFO(("monitor=%s:%u", m_common.m_monitor.getHostName().c_str(), m_common.m_monitor.getPort())); CWX_INFO(("*****************store*******************")); CWX_INFO(("path=%s", m_store.m_strPath.c_str())); CWX_INFO(("file_max_mbyte=%u", m_store.m_uiLogMSize)); CWX_INFO(("file_max_second=%u", m_store.m_uiSwitchSecond)); CWX_INFO(("append_return=%s", m_store.m_bAppendReturn?"yes":"no")); CWX_INFO(("record_prefix=%s", m_store.m_strRecordPrefix.length()? m_store.m_strRecordPrefix.c_str():"")); CWX_INFO(("flush_log_num=%u", m_store.m_uiFlushNum)); CWX_INFO(("flush_log_second=%u", m_store.m_uiFlushSecond)); CWX_INFO(("*****************mq*******************")); CWX_INFO(("name=%s", m_mq.m_strName.c_str())); CWX_INFO(("user=%s", m_mq.m_mq.getUser().c_str())); CWX_INFO(("passwd=%s", m_mq.m_mq.getPasswd().c_str())); CWX_INFO(("listen=%s:%u",m_mq.m_mq.getHostName().c_str(), m_mq.m_mq.getPort())); CWX_INFO(("cache_msize=%u", m_mq.m_uiCacheMSize)); CWX_INFO(("cache_second=%u", m_mq.m_uiCacheTimeout)); CWX_INFO(("*****************sync*******************")); CWX_INFO(("source=%s", m_sync.m_strSource.c_str())); CWX_INFO(("sock_buf_kbyte=%d", m_sync.m_uiSockBufKByte)); CWX_INFO(("max_chunk_kbyte=%d", m_sync.m_uiChunkKBye)); CWX_INFO(("sync_conn_num=%d",m_sync.m_uiConnNum)); CWX_INFO(("zip=%u", m_sync.m_bzip?"yes":"no")); CWX_INFO(("*****************END CONFIG *******************")); }
///配置运行环境信息 int CwxEchoApp::initRunEnv(){ ///设置系统的时钟间隔,最小刻度为1ms,此为1s。 this->setClick(1000);//1s ///设置工作目录 this->setWorkDir(this->m_config.m_strWorkDir.c_str()); ///设置循环运行日志的数量 this->setLogFileNum(LOG_FILE_NUM); ///设置每个日志文件的大小 this->setLogFileSize(LOG_FILE_SIZE*1024*1024); ///调用架构的initRunEnv,使以上设置的参数生效 if (CwxAppFramework::initRunEnv() == -1 ) return -1; blockSignal(SIGPIPE); //set version this->setAppVersion(ECHO_APP_VERSION); //set last modify date this->setLastModifyDatetime(ECHO_MODIFY_DATE); //set compile date this->setLastCompileDatetime(CWX_COMPILE_DATE(_BUILD_DATE)); ///将加载的配置文件信息输出到日志文件中,以供查看检查 string strConfOut; m_config.outputConfig(strConfOut); CWX_INFO((strConfOut.c_str())); ///注册echo请求的处理handle,echo请求的svr-id为SVR_TYPE_ECHO m_eventHandler = new CwxEchoEventHandler(this); this->getCommander().regHandle(SVR_TYPE_ECHO, m_eventHandler); ///监听TCP连接,其建立的连接的svr-id都为SVR_TYPE_ECHO,接收的消息的svr-id都为SVR_TYPE_ECHO。 ///全部由m_eventHandler对象来处理 if (0 > this->noticeTcpListen(SVR_TYPE_ECHO, this->m_config.m_listen.getHostName().c_str(), this->m_config.m_listen.getPort(), false, CWX_APP_MSG_MODE, CwxEchoApp::setSockAttr, this)) { CWX_ERROR(("Can't register the echo acceptor port: addr=%s, port=%d", this->m_config.m_listen.getHostName().c_str(), this->m_config.m_listen.getPort())); return -1; } ///监听UNIX DOMAIN连接,其建立的连接的svr-id都为SVR_TYPE_ECHO,接收的消息的svr-id都为SVR_TYPE_ECHO。 ///全部由m_eventHandler对象来处理 if (0 > this->noticeLsockListen(SVR_TYPE_ECHO, this->m_config.m_strUnixPathFile.c_str())) { CWX_ERROR(("Can't register the echo unix acceptor port: path=%s", m_config.m_strUnixPathFile.c_str())); return -1; } ///创建线程池对象,此线程池中线程的group-id为2,线程池的线程数量为m_config.m_unThreadNum。 m_threadPool = new CwxThreadPool(m_config.m_unThreadNum, &getCommander()); ///启动线程,线程池中线程的线程栈大小为1M。 if ( 0 != m_threadPool->start(NULL)){ CWX_ERROR(("Failure to start thread pool")); return -1; } return 0; }
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); } }
int CwxMqInnerDispHandler::recvReport(CwxMqTss* pTss) { int iRet = 0; CWX_UINT64 ullSid = 0; CWX_UINT32 uiChunk = 0; bool bZip = false; CwxMsgBlock* msg = NULL; do { if (!m_recvMsgData) { strcpy(pTss->m_szBuf2K, "No data."); CWX_ERROR(("Report package is empty, from:%s:%u", m_strPeerHost.c_str(), m_unPeerPort)); iRet = CWX_MQ_ERR_ERROR; break; } ///禁止重复report sid。若cunsor存在,表示已经报告过一次 if (m_syncSession) { iRet = CWX_MQ_ERR_ERROR; CwxCommon::snprintf(pTss->m_szBuf2K, 2048, "Can't report sync sid duplicate."); CWX_ERROR(("Report is duplicate, from:%s:%u", m_strPeerHost.c_str(), m_unPeerPort)); break; } ///若是同步sid的报告消息,则获取报告sid iRet = CwxMqPoco::parseInnerReportData(pTss->m_pReader, m_recvMsgData, ullSid, uiChunk, bZip, pTss->m_szBuf2K); if (CWX_MQ_ERR_SUCCESS != iRet) { CWX_ERROR(("Failure to parse report msg, err=%s, from:%s:%u", pTss->m_szBuf2K, m_strPeerHost.c_str(), m_unPeerPort)); break; } CWX_INFO(("Report info :%s:%u, sid=%s, chunk=%u, zip=%s", m_strPeerHost.c_str(), m_unPeerPort, CwxCommon::toString(ullSid, pTss->m_szBuf2K, 10), uiChunk, bZip?"yes":"no")); m_syncSession = new CwxMqInnerDispSession(m_pApp); m_syncSession->m_strHost = m_strPeerHost; m_syncSession->m_uiChunk = uiChunk; m_syncSession->m_bZip = bZip; if (m_syncSession->m_uiChunk) { if (m_syncSession->m_uiChunk > CwxMqConfigCmn::MAX_CHUNK_SIZE_KB) m_syncSession->m_uiChunk = CwxMqConfigCmn::MAX_CHUNK_SIZE_KB; if (m_syncSession->m_uiChunk < CwxMqConfigCmn::MIN_CHUNK_SIZE_KB) m_syncSession->m_uiChunk = CwxMqConfigCmn::MIN_CHUNK_SIZE_KB; m_syncSession->m_uiChunk *= 1024; } m_syncSession->reformSessionId(); ///将session加到session的map while (m_sessions.find(m_syncSession->m_ullSessionId) != m_sessions.end()) { m_syncSession->reformSessionId(); } m_sessions[m_syncSession->m_ullSessionId] = m_syncSession; m_ullSessionId = m_syncSession->m_ullSessionId; m_syncSession->addConn(this); if (0 != m_syncSession->init()) { CWX_ERROR(("Failure to init session min heap.")); break; } ///回复iRet的值 iRet = CWX_MQ_ERR_SUCCESS; ///创建binlog的读取cursor list<string> topics; m_pApp->getTopicMgr()->getAllTopics(topics); for(list<string>::iterator iter = topics.begin(); iter != topics.end(); iter++) { CwxBinLogMgr* binlogMgr = m_pApp->getTopicMgr()->getBinlogMgrByTopic(*iter); if (!binlogMgr) { ///binlogMgr被删除 CWX_ERROR(("Failure to get BinloMgr topic:%s", iter->c_str())); continue; } CwxBinLogCursor* pCursor = binlogMgr->createCurser(ullSid); if (!pCursor) { iRet = CWX_MQ_ERR_ERROR; strcpy(pTss->m_szBuf2K, "Failure to create cursor"); CWX_ERROR(("%s, from:%s:%u", pTss->m_szBuf2K, m_strPeerHost.c_str(), m_unPeerPort)); m_pApp->getTopicMgr()->freeBinlogMgrByTopic(*iter, binlogMgr); break; } CwxBinlogMgrCursor* item = new CwxBinlogMgrCursor(*iter, binlogMgr, pCursor); m_syncSession->m_binlogCursor[*iter] = item; m_syncSession->m_unReadyBinlogCursor.push_back(item); } m_syncSession->m_ullStartSid = ullSid; ///发送session id的消息 if (CWX_MQ_ERR_SUCCESS != CwxMqPoco::packReportDataReply(pTss->m_pWriter, msg, m_header.getTaskId(), m_syncSession->m_ullSessionId, pTss->m_szBuf2K)) { CWX_ERROR(("Failure to pack sync data reply, err=%s, from:%s:%u", pTss->m_szBuf2K, m_strPeerHost.c_str(), m_unPeerPort)); return -1; } msg->send_ctrl().setMsgAttr(CwxMsgSendCtrl::NONE); if (!putMsg(msg)) { CwxMsgBlockAlloc::free(msg); CWX_ERROR(("Failure push msg to send queue. from:%s:%u", m_strPeerHost.c_str(), m_unPeerPort)); return -1; } ///发送发一条binlog int iState = syncSendBinLog(pTss); if (-1 == iState) { iRet = CWX_MQ_ERR_ERROR; CWX_ERROR(("%s, from:%s:%u", pTss->m_szBuf2K, m_strPeerHost.c_str(), m_unPeerPort)); break; } else if (0 == iState) { ///产生continue的消息 channel()->regRedoHander(this); } return 0; }while(0); ///到此一定错误 CWX_ASSERT(CWX_MQ_ERR_SUCCESS != iRet); CwxMsgBlock* pBlock = NULL; if (CWX_MQ_ERR_SUCCESS != CwxMqPoco::packSyncErr(pTss->m_pWriter, pBlock, m_header.getTaskId(), iRet, pTss->m_szBuf2K, pTss->m_szBuf2K)) { CWX_ERROR(("Failure to create binlog reply package, err:%s", pTss->m_szBuf2K)); return -1; } pBlock->send_ctrl().setMsgAttr(CwxMsgSendCtrl::CLOSE_NOTICE); if (!putMsg(pBlock)) { CwxMsgBlockAlloc::free(pBlock); CWX_ERROR(("Failure push msg to send queue. from:%s:%u", m_strPeerHost.c_str(), m_unPeerPort)); return -1; } return 0; }
void CwxMqFetchConfig::outputConfig() { CWX_INFO(("\n*****************BEGIN CONFIG*******************")); CWX_INFO(("home=%s", m_strWorkDir.c_str())); CWX_INFO( ("listen=%s:%u", m_listen.getHostName().c_str(), m_listen.getPort())); CWX_INFO(("unix=%s", m_strUnixPathFile.c_str())); CWX_INFO(("user=%s", m_strUser.c_str())); CWX_INFO(("passwd=%s", m_strPasswd.c_str())); CWX_INFO(("queue=%s", m_strQueue.c_str())); CWX_INFO(("conn_type=%s", m_bTcp?"tcp":"unix")); CWX_INFO(("conn_num=%u", m_unConnNum)); CWX_INFO(("conn_lasting=%u", m_bLasting?1:0)); CWX_INFO(("block=%u", m_bBlock?1:0)); CWX_INFO(("*****************END CONFIG *******************")); }
void UnistorBenchConfig::outputConfig(){ CWX_INFO(("*****************BEGIN CONFIG *******************")); CWX_INFO(("home=%s", m_strWorkDir.c_str())); CWX_INFO(("listen=%s:%u", m_listen.getHostName().c_str(), m_listen.getPort())); CWX_INFO(("conn_conn_num=%u", m_unConnNum)); CWX_INFO(("conn_lasting=%d", m_bLasting?1:0)); CWX_INFO(("data_opr=%s", m_strOpr.c_str())); CWX_INFO(("data_size=%u", m_uiDataSize)); CWX_INFO(("data_key_in_order=%s", m_bKeyOrder?"yes":"no")); CWX_INFO(("data_get_order=%s", m_bGetOrder?"yes":"no")); CWX_INFO(("data_base=%u", m_uiDataBase)); CWX_INFO(("data_mod=%u", m_uiDataMod)); CWX_INFO(("data_master=%s", m_bGetMaster?"yes":"no")); CWX_INFO(("key_group=%d", m_uiKeyGroup)); CWX_INFO(("key_group_index=%d", m_uiKeyIndex)); CWX_INFO(("expire=%d", m_uiExpire)); CWX_INFO(("cache=%d", m_bCache?"yes":"no")); CWX_INFO(("user=%s", m_strUser.c_str())); CWX_INFO(("passwd=%s", m_strPasswd.c_str())); CWX_INFO(("*****************END CONFIG *******************")); }