void sdbReportError( const CHAR *filename, UINT32 lineno, const CHAR *msg, BOOLEAN isException ) { BOOLEAN add = FALSE ; if ( SDB_OK == sdbGetErrno() || !__hasSetErrNo__ ) { const CHAR *p = NULL ; if ( isException && msg && NULL != ( p = ossStrstr( msg, ":" ) ) && 0 != ossAtoi( p + 1 ) ) { sdbSetErrno( ossAtoi( p + 1 ) ) ; } else { sdbSetErrno( SDB_SPT_EVAL_FAIL ) ; } } if ( ( sdbIsErrMsgEmpty() || !__hasSetErrMsg__ ) && msg ) { if ( filename ) { stringstream ss ; ss << filename << ":" << lineno << " " << msg ; sdbSetErrMsg( ss.str().c_str() ) ; } else { sdbSetErrMsg( msg ) ; } add = TRUE ; } if ( sdbNeedPrintError() ) { ossPrintf( "%s:%d %s\n", filename ? filename : "(nofile)" , lineno, msg ) ; if ( !add && !sdbIsErrMsgEmpty() && NULL == ossStrstr( sdbGetErrMsg(), msg ) ) { ossPrintf( "%s\n", sdbGetErrMsg() ) ; } } __hasSetErrMsg__ = FALSE ; __hasSetErrNo__ = FALSE ; }
INT32 dpsArchiveFileMgr::getFileId( const string& fileName, UINT32& fileId ) { INT32 rc = SDB_OK ; vector<string> substrs ; SDB_ASSERT( isArchiveFileName( fileName ), "not archive file name" ) ; try { substrs = utilStrSplit( fileName, DPS_ARCHIVE_FILE_SEP ) ; SDB_ASSERT( substrs.size() > 1, "invalid fileName" ) ; string fileIdStr = substrs.at( 1 ) ; if ( !utilStrIsDigit( fileIdStr ) ) { rc = SDB_INVALIDARG ; PD_LOG( PDERROR, "invalid fileName[%s]", fileName.c_str() ) ; goto error ; } fileId = ossAtoi( fileIdStr.c_str() ) ; } catch ( exception& e ) { rc = SDB_SYS ; PD_LOG( PDERROR, "unexpected exception: %s", e.what() ) ; } done: return rc ; error: goto done ; }
INT32 restAdaptor::recvRequestBody( pmdRestSession *pSession, HTTP_PARSE_COMMON &common, CHAR **ppPath, INT32 &pathSize ) { INT32 rc = SDB_OK ; PD_TRACE_ENTRY( SDB__RESTADP_RECVREQBO ) ; SDB_ASSERT ( pSession, "pSession is NULL" ) ; SDB_ASSERT ( ppPath, "ppPath is NULL" ) ; httpConnection *pHttpCon = pSession->getRestConn() ; CHAR *pBuffer = NULL ; const CHAR *pContentLength = NULL ; CHAR *pUrl = NULL ; INT32 bodySize = 0 ; INT32 sumBodySize = 0 ; INT32 curRecvSize = 0 ; INT32 receivedSize = 0 ; INT32 tempSize = 0 ; INT32 urlSize = 0 ; rc = getHttpHeader( pSession, REST_STRING_CONLEN, &pContentLength ) ; if ( rc ) { PD_LOG ( PDERROR, "Failed to get http header, rc=%d", rc ) ; goto error ; } if ( pContentLength ) { bodySize = ossAtoi( pContentLength ) ; if ( bodySize > 0 ) { if ( bodySize > _maxHttpBodySize ) { rc = SDB_REST_RECV_SIZE ; PD_LOG ( PDERROR, "http body size %d greater than %d", bodySize, _maxHttpBodySize ) ; goto error ; } rc = pSession->allocBuff( bodySize + 1, &(pHttpCon->_pBodyBuf), sumBodySize ) ; if ( rc ) { PD_LOG ( PDERROR, "Unable to allocate %d bytes memory, rc=%d", bodySize, rc ) ; goto error ; } pBuffer = pHttpCon->_pBodyBuf ; pBuffer[bodySize] = 0 ; if ( pHttpCon->_pPartBody ) { ossMemcpy( pHttpCon->_pBodyBuf, pHttpCon->_pPartBody, pHttpCon->_partSize ) ; receivedSize = pHttpCon->_partSize ; } rc = pSession->recvData( pBuffer + receivedSize, bodySize - receivedSize, _timeout, TRUE, &curRecvSize, 0 ) ; if ( rc ) { PD_LOG ( PDERROR, "Failed to recv, rc=%d", rc ) ; goto error ; } receivedSize += curRecvSize ; urlSize = urlDecodeSize( pBuffer, receivedSize ) ; rc = pSession->allocBuff( urlSize + 1, &pUrl, tempSize ) ; if ( rc ) { PD_LOG ( PDERROR, "Unable to allocate %d bytes memory, rc=%d", urlSize + 1, rc ) ; goto error ; } urlDecode( pBuffer, receivedSize, &pUrl, urlSize ) ; pUrl[ urlSize ] = 0 ; _parse_http_query( pHttpCon, pUrl, urlSize ) ; } } rc = _convertMsg( pSession, common, ppPath, pathSize ) ; if ( rc ) { PD_LOG ( PDERROR, "Failed to build msg, rc=%d", rc ) ; goto error ; } pHttpCon->_common = common ; done: PD_TRACE_EXITRC( SDB__RESTADP_RECVREQBO, rc ) ; return rc ; error: goto done ; }
INT32 omRemoveBusinessCommand::_generateTaskConfig( list<BSONObj> &configList, BSONObj &taskConfig ) { INT32 rc = SDB_OK ; BSONObj filter ; BSONObjBuilder taskConfigBuilder ; BSONArrayBuilder configBuilder ; list<BSONObj>::iterator iter ; omDatabaseTool dbTool( _cb ) ; filter = BSON( OM_HOST_FIELD_NAME << "" << OM_HOST_FIELD_IP << "" << OM_HOST_FIELD_CLUSTERNAME << "" << OM_HOST_FIELD_USER << "" << OM_HOST_FIELD_PASSWD << "" << OM_HOST_FIELD_SSHPORT << "" ) ; taskConfigBuilder.append( OM_BSON_CLUSTER_NAME, _clusterName ) ; taskConfigBuilder.append( OM_BSON_BUSINESS_TYPE, _businessType ) ; taskConfigBuilder.append( OM_BSON_BUSINESS_NAME, _businessName ) ; taskConfigBuilder.append( OM_BSON_DEPLOY_MOD, _deployMod ) ; if ( OM_BUSINESS_SEQUOIADB == _businessType ) { string authUser ; string authPasswd ; rc = dbTool.getAuth( _businessName, authUser, authPasswd ) ; if ( rc ) { _errorMsg.setError( TRUE, "failed to get business auth: " "name=%s, rc=%d", _businessName.c_str(), rc ) ; PD_LOG( PDERROR, _errorMsg.getError() ) ; goto error ; } taskConfigBuilder.append( OM_TASKINFO_FIELD_AUTH_USER, authUser ) ; taskConfigBuilder.append( OM_TASKINFO_FIELD_AUTH_PASSWD, authPasswd ) ; } else if ( OM_BUSINESS_ZOOKEEPER == _businessType || OM_BUSINESS_SEQUOIASQL_OLAP == _businessType ) { string sdbUser ; string sdbPasswd ; string sdbUserGroup ; BSONObj clusterInfo ; rc = dbTool.getClusterInfo( _clusterName, clusterInfo ) ; if ( rc ) { _errorMsg.setError( TRUE, "failed to get cluster info: " "name=%s, rc=%d", _clusterName.c_str(), rc ) ; PD_LOG( PDERROR, _errorMsg.getError() ) ; goto error ; } sdbUser = clusterInfo.getStringField( OM_CLUSTER_FIELD_SDBUSER ) ; sdbPasswd = clusterInfo.getStringField( OM_CLUSTER_FIELD_SDBPASSWD ) ; sdbUserGroup = clusterInfo.getStringField( OM_CLUSTER_FIELD_SDBUSERGROUP ) ; taskConfigBuilder.append( OM_TASKINFO_FIELD_SDBUSER, sdbUser ) ; taskConfigBuilder.append( OM_TASKINFO_FIELD_SDBPASSWD, sdbPasswd ) ; taskConfigBuilder.append( OM_TASKINFO_FIELD_SDBUSERGROUP, sdbUserGroup ) ; } else if( OM_BUSINESS_SEQUOIASQL_OLTP == _businessType ) { } for ( iter = configList.begin(); iter != configList.end(); ++iter ) { string hostName ; string installPath ; BSONObj hostInfo ; BSONObj tmpHostInfo ; BSONObj configInfo ; BSONObj packages ; hostName = iter->getStringField( OM_CONFIGURE_FIELD_HOSTNAME ) ; configInfo = iter->getObjectField( OM_CONFIGURE_FIELD_CONFIG ) ; rc = dbTool.getHostInfoByAddress( hostName, tmpHostInfo ) ; if ( rc ) { _errorMsg.setError( TRUE, "failed to get host info: name=%s, rc=%d", hostName.c_str(), rc ) ; PD_LOG( PDERROR, _errorMsg.getError() ) ; goto error ; } hostInfo = tmpHostInfo.filterFieldsUndotted( filter, TRUE ) ; packages = tmpHostInfo.getObjectField( OM_HOST_FIELD_PACKAGES ) ; { BSONObjIterator pkgIter( packages ) ; while ( pkgIter.more() ) { BSONElement ele = pkgIter.next() ; BSONObj pkgInfo = ele.embeddedObject() ; string pkgName = pkgInfo.getStringField( OM_HOST_FIELD_PACKAGENAME ) ; if ( pkgName == _businessType ) { installPath = pkgInfo.getStringField( OM_HOST_FIELD_INSTALLPATH ) ; break ; } } } { BSONObjIterator configIter( configInfo ) ; while ( configIter.more() ) { BSONObjBuilder configInfoBuilder ; BSONElement ele = configIter.next() ; BSONObj nodeInfo = ele.embeddedObject() ; if ( OM_BUSINESS_SEQUOIADB == _businessType && 0 == ossStrlen( nodeInfo.getStringField( OM_CONF_DETAIL_CATANAME ) ) ) { CHAR catName[ OM_INT32_LENGTH + 1 ] = { 0 } ; string svcName = nodeInfo.getStringField( OM_CONF_DETAIL_SVCNAME ) ; INT32 iSvcName = ossAtoi( svcName.c_str() ) ; INT32 iCatName = iSvcName + MSG_ROUTE_CAT_SERVICE ; ossItoa( iCatName, catName, OM_INT32_LENGTH ) ; configInfoBuilder.append( OM_CONF_DETAIL_CATANAME, catName ) ; } configInfoBuilder.appendElements( nodeInfo ) ; configInfoBuilder.appendElements( hostInfo ) ; configInfoBuilder.append( OM_BSON_INSTALL_PATH, installPath ) ; configBuilder.append( configInfoBuilder.obj() ) ; } } } taskConfigBuilder.append( OM_TASKINFO_FIELD_CONFIG, configBuilder.arr() ) ; taskConfig = taskConfigBuilder.obj() ; done: return rc ; error: goto done ; }
INT32 pmdThreadMainEntry( INT32 argc, CHAR** argv ) { INT32 rc = SDB_OK ; pmdKRCB *krcb = pmdGetKRCB() ; CHAR currentPath[ OSS_MAX_PATHSIZE + 1 ] = { 0 } ; CHAR dialogPath[ OSS_MAX_PATHSIZE + 1 ] = { 0 } ; CHAR dialogFile[ OSS_MAX_PATHSIZE + 1 ] = { 0 } ; INT32 delSig[] = { 17, 0 } ; // del SIGCHLD CHAR verText[ OSS_MAX_PATHSIZE + 1 ] = { 0 } ; po::variables_map vm ; rc = initArgs( argc, argv, vm ) ; if ( rc ) { if ( SDB_PMD_HELP_ONLY == rc || SDB_PMD_VERSION_ONLY == rc ) { rc = SDB_OK ; } goto done ; } rc = ossGetEWD( currentPath, OSS_MAX_PATHSIZE ) ; if ( rc ) { std::cout << "Get current path failed: " << rc << std::endl ; goto error ; } ossChDir( currentPath ) ; rc = utilBuildFullPath( currentPath, SDBCM_LOG_PATH, OSS_MAX_PATHSIZE, dialogPath ) ; if ( rc ) { std::cout << "Build dialog path failed: " << rc << std::endl ; goto error ; } rc = ossMkdir( dialogPath ) ; if ( rc && SDB_FE != rc ) { std::cout << "Create dialog dir: " << dialogPath << " failed: " << rc << std::endl ; goto error ; } rc = utilBuildFullPath( dialogPath, SDBCM_DIALOG_FILE_NAME, OSS_MAX_PATHSIZE, dialogFile ) ; if ( rc ) { std::cout << "Build dialog path failed: " << rc << std::endl ; goto error ; } sdbEnablePD( dialogFile ) ; ossSprintVersion( "Version", verText, OSS_MAX_PATHSIZE, FALSE ) ; PD_LOG( PDEVENT, "Start cm[%s]...", verText) ; rc = sdbGetOMAgentOptions()->init( currentPath ) ; if ( rc ) { PD_LOG( PDERROR, "Failed to init config, rc: %d", rc ) ; goto error ; } if ( vm.count( PMD_OPTION_CURUSER ) ) { sdbGetOMAgentOptions()->setCurUser() ; } if ( vm.count( PMD_OPTION_STANDALONE ) ) { sdbGetOMAgentOptions()->setStandAlone() ; if ( vm.count( PMD_OPTION_ALIVE_TIME ) ) { UINT32 timeout = vm[ PMD_OPTION_ALIVE_TIME ].as<INT32>() ; sdbGetOMAgentOptions()->setAliveTimeout( timeout ) ; } } if ( vm.count( PMD_OPTION_PORT ) ) { string svcname = vm[ PMD_OPTION_PORT ].as<string>() ; sdbGetOMAgentOptions()->setCMServiceName( svcname.c_str() ) ; pmdSetLocalPort( (UINT16)ossAtoi( svcname.c_str() ) ) ; } setPDLevel( sdbGetOMAgentOptions()->getDiagLevel() ) ; { string configs ; sdbGetOMAgentOptions()->toString( configs ) ; PD_LOG( PDEVENT, "All configs:\n%s", configs.c_str() ) ; } pmdSetDBRole( SDB_ROLE_OMA ) ; rc = pmdEnableSignalEvent( dialogPath, (PMD_ON_QUIT_FUNC)pmdOnQuit, delSig ) ; PD_RC_CHECK ( rc, PDERROR, "Failed to enable trap, rc: %d", rc ) ; #if defined( _LINUX ) signal( SIGCHLD, SIG_IGN ) ; #endif // _LINUX PMD_REGISTER_CB( sdbGetOMAgentMgr() ) ; rc = krcb->init() ; PD_RC_CHECK( rc, PDERROR, "Failed to init krcb, rc: %d", rc ) ; { EDUID agentEDU = PMD_INVALID_EDUID ; pmdEDUMgr *eduMgr = krcb->getEDUMgr() ; rc = eduMgr->startEDU ( EDU_TYPE_PIPESLISTENER, (void*)sdbGetOMAgentOptions()->getCMServiceName(), &agentEDU ) ; PD_RC_CHECK( rc, PDERROR, "Start PIPELISTENER failed, rc: %d", rc ) ; rc = eduMgr->waitUntil( agentEDU, PMD_EDU_RUNNING ) ; PD_RC_CHECK( rc, PDERROR, "Wait pipe listener to running " "failed, rc: %d", rc ) ; } #if defined (_LINUX) { CHAR pmdProcessName [ OSS_RENAME_PROCESS_BUFFER_LEN + 1 ] = {0} ; ossSnprintf ( pmdProcessName, OSS_RENAME_PROCESS_BUFFER_LEN, "%s(%s)", utilDBTypeStr( pmdGetDBType() ), sdbGetOMAgentOptions()->getCMServiceName() ) ; ossEnableNameChanges ( argc, argv ) ; ossRenameProcess ( pmdProcessName ) ; } #endif // _LINUX while ( PMD_IS_DB_UP() ) { ossSleepsecs ( 1 ) ; } rc = krcb->getShutdownCode() ; done: PMD_SHUTDOWN_DB( rc ) ; pmdSetQuit() ; krcb->destroy () ; PD_LOG ( PDEVENT, "Stop programme, exit code: %d", krcb->getShutdownCode() ) ; return rc == SDB_OK ? 0 : 1 ; error: goto done ; }
INT32 utilParseVersion( CHAR * pVersionStr, INT32 &version, INT32 &subVersion, INT32 &release, string &buildInfo ) { INT32 rc = SDB_OK ; const CHAR *pDelim = " \r\n" ; CHAR *pToken = NULL ; CHAR *pLast = NULL ; if ( !pVersionStr ) { rc = SDB_INVALIDARG ; goto error ; } pToken = ossStrtok( pVersionStr, pDelim, &pLast ) ; if ( !pToken ) { rc = SDB_INVALIDARG ; goto error ; } else { CHAR *pTokenTmp = NULL ; CHAR *pLastTmp = NULL ; CHAR *pVerPtr = ossStrstr( pToken, ":" ) ; if ( !pVerPtr ) { rc = SDB_INVALIDARG ; goto error ; } pTokenTmp = ossStrtok( pVerPtr, " .", &pLastTmp ) ; if ( !pTokenTmp ) { rc = SDB_INVALIDARG ; goto error ; } version = ossAtoi( pTokenTmp ) ; pTokenTmp = ossStrtok( NULL, " .", &pLastTmp ) ; if ( !pTokenTmp ) { rc = SDB_INVALIDARG ; goto error ; } subVersion = ossAtoi( pTokenTmp ) ; } pToken = ossStrtok( NULL, pDelim, &pLast ) ; if ( !pToken ) { rc = SDB_INVALIDARG ; goto error ; } else { string releaseStr ; CHAR *pReleasePtr = ossStrstr( pToken, ":" ) ; if ( !pReleasePtr ) { rc = SDB_INVALIDARG ; goto error ; } releaseStr = pReleasePtr ; utilStrLtrim( releaseStr ) ; release = ossAtoi( releaseStr.c_str() ) ; } pToken = ossStrtok( NULL, pDelim, &pLast ) ; if ( !pToken ) { rc = SDB_INVALIDARG ; goto error ; } buildInfo = pToken ; done: return rc ; error: goto done ; }
INT32 _pmdController::init () { INT32 rc = SDB_OK ; pmdOptionsCB *pOptCB = pmdGetOptionCB() ; UINT16 port = 0 ; UINT16 protocolPort = 0 ; port = pOptCB->getServicePort() ; _pTcpListener = SDB_OSS_NEW ossSocket( port ) ; if ( !_pTcpListener ) { PD_LOG( PDERROR, "Failed to alloc socket" ) ; rc = SDB_OOM ; goto error ; } rc = _pTcpListener->initSocket() ; PD_RC_CHECK( rc, PDERROR, "Failed to init tcp listener socket[%d], " "rc: %d", port, rc ) ; rc = _pTcpListener->bind_listen() ; PD_RC_CHECK( rc, PDERROR, "Failed to bind tcp listener socket[%d], " "rc: %d", port, rc ) ; PD_LOG( PDEVENT, "Listerning on port[%d]", port ) ; rc = ossGetPort( pOptCB->getRestService(), port ) ; PD_RC_CHECK( rc, PDERROR, "Failed to get port by service name: %s, " "rc: %d", pOptCB->getRestService(), rc ) ; _pHttpListener = SDB_OSS_NEW ossSocket( port ) ; if ( !_pHttpListener ) { PD_LOG( PDERROR, "Failed to alloc socket" ) ; rc = SDB_OOM ; goto error ; } rc = _pHttpListener->initSocket() ; PD_RC_CHECK( rc, PDERROR, "Failed to init http listener socket[%d], " "rc: %d", port, rc ) ; rc = _pHttpListener->bind_listen() ; PD_RC_CHECK( rc, PDERROR, "Failed to bind http listerner socket[%d], " "rc: %d", port, rc ) ; PD_LOG( PDEVENT, "Http Listerning on port[%d]", port ) ; if ( FALSE ) { protocolPort = ossAtoi( _protocol->getServiceName() ) ; _pMongoListener = SDB_OSS_NEW ossSocket( protocolPort ) ; if ( !_pMongoListener ) { PD_LOG( PDERROR, "Failed to alloc socket" ) ; rc = SDB_OOM ; goto error ; } rc = _pMongoListener->initSocket() ; PD_RC_CHECK( rc, PDERROR, "Failed to init FAP listener socket[%d], " "rc: %d", protocolPort, rc ) ; rc = _pMongoListener->bind_listen() ; PD_RC_CHECK( rc, PDERROR, "Failed to bind FAP listener socket[%d], " "rc: %d", protocolPort, rc ) ; PD_LOG( PDEVENT, "Listerning on port[%d]", protocolPort ) ; } done: return rc ; error: goto done ; }
INT32 _pmdController::initForeignModule( const CHAR *moduleName ) { INT32 rc = SDB_OK ; UINT16 protocolPort = 0 ; CHAR rootPath[ OSS_MAX_PATHSIZE + 1 ] = { 0 } ; CHAR path[ OSS_MAX_PATHSIZE + 1 ] = { 0 } ; if ( NULL == moduleName || '\0' == moduleName[ 0 ] ) { goto done ; } _fapMongo = SDB_OSS_NEW pmdModuleLoader() ; if ( NULL == _fapMongo ) { PD_LOG( PDERROR, "Failed to alloc foreign access protocol module" ) ; rc = SDB_OOM ; goto error ; } rc = ossGetEWD( rootPath, OSS_MAX_PATHSIZE ) ; if ( rc ) { ossPrintf ( "Failed to get excutable file's working " "directory"OSS_NEWLINE ) ; goto error ; } rc = utilBuildFullPath( rootPath, FAP_MODULE_PATH, OSS_MAX_PATHSIZE, path ); if ( rc ) { ossPrintf( "Failed to build module path: %d"OSS_NEWLINE, rc ) ; goto error ; } rc = _fapMongo->load( moduleName, path ) ; PD_RC_CHECK( rc, PDERROR, "Failed to load module: %s, path: %s" " rc: %d", moduleName, FAP_MODULE_PATH, rc ) ; rc = _fapMongo->create( _protocol ) ; PD_RC_CHECK( rc, PDERROR, "Failed to create protocol service" ) ; SDB_ASSERT( _protocol, "Foreign access protocol can not be NULL" ) ; rc = _protocol->init( pmdGetKRCB() ) ; PD_RC_CHECK( rc, PDERROR, "Failed to init protocol" ) ; // memory will be freed in fini protocolPort = ossAtoi( _protocol->getServiceName() ) ; _pMongoListener = SDB_OSS_NEW ossSocket( protocolPort ) ; if ( !_pMongoListener ) { PD_LOG( PDERROR, "Failed to alloc socket" ) ; rc = SDB_OOM ; goto error ; } rc = _pMongoListener->initSocket() ; PD_RC_CHECK( rc, PDERROR, "Failed to init FAP listener socket[%d], " "rc: %d", protocolPort, rc ) ; rc = _pMongoListener->bind_listen() ; PD_RC_CHECK( rc, PDERROR, "Failed to bind FAP listener socket[%d], " "rc: %d", protocolPort, rc ) ; PD_LOG( PDEVENT, "Listerning on port[%d]", protocolPort ) ; done: return rc ; error: goto done ; }