INT32 _rtnQueryOptions::fromQueryMsg ( CHAR *pMsg ) { INT32 rc = SDB_OK ; CHAR *pQuery = NULL ; CHAR *pSelector = NULL ; CHAR *pOrderBy = NULL ; CHAR *pHint = NULL ; rc = msgExtractQuery( pMsg, &_flag, (CHAR**)&_fullName, &_skip, &_limit, &pQuery, &pSelector, &pOrderBy, &pHint ) ; PD_RC_CHECK( rc, PDERROR, "Extrace query msg failed, rc: %d", rc ) ; if ( NULL != _fullNameBuf ) { SDB_OSS_FREE( _fullNameBuf ) ; _fullNameBuf = NULL ; } try { _query = BSONObj( pQuery ) ; _selector = BSONObj( pSelector ) ; _orderBy = BSONObj( pOrderBy ) ; _hint = BSONObj( pHint ) ; } catch( std::exception &e ) { PD_LOG( PDERROR, "Extrace query msg occur exception: %s", e.what() ) ; rc = SDB_SYS ; goto error ; } done: return rc ; error: goto done ; }
INT32 _pmdDataProcessor::_onQueryReqMsg( MsgHeader * msg, SDB_DPSCB *dpsCB, _rtnContextBuf &buffObj, INT64 &contextID ) { INT32 rc = SDB_OK ; INT32 flags = 0 ; CHAR *pCollectionName = NULL ; CHAR *pQueryBuff = NULL ; CHAR *pFieldSelector = NULL ; CHAR *pOrderByBuffer = NULL ; CHAR *pHintBuffer = NULL ; INT64 numToSkip = -1 ; INT64 numToReturn = -1 ; _rtnCommand *pCommand = NULL ; rc = msgExtractQuery ( (CHAR *)msg, &flags, &pCollectionName, &numToSkip, &numToReturn, &pQueryBuff, &pFieldSelector, &pOrderByBuffer, &pHintBuffer ) ; PD_RC_CHECK( rc, PDERROR, "Session[%s] extract query msg failed, rc: %d", getSession()->sessionName(), rc ) ; if ( !rtnIsCommand ( pCollectionName ) ) { rtnContextBase *pContext = NULL ; try { BSONObj matcher ( pQueryBuff ) ; BSONObj selector ( pFieldSelector ) ; BSONObj orderBy ( pOrderByBuffer ) ; BSONObj hint ( pHintBuffer ) ; MON_SAVE_OP_DETAIL( eduCB()->getMonAppCB(), msg->opCode, "CL:%s, Match:%s, Selector:%s, OrderBy:%s, " "Hint:%s", pCollectionName, matcher.toString().c_str(), selector.toString().c_str(), orderBy.toString().c_str(), hint.toString().c_str() ) ; PD_LOG ( PDDEBUG, "Session[%s] Query: matcher: %s\nselector: " "%s\norderBy: %s\nhint:%s", getSession()->sessionName(), matcher.toString().c_str(), selector.toString().c_str(), orderBy.toString().c_str(), hint.toString().c_str() ) ; rc = rtnQuery( pCollectionName, selector, matcher, orderBy, hint, flags, eduCB(), numToSkip, numToReturn, _pDMSCB, _pRTNCB, contextID, &pContext, TRUE ) ; if ( rc ) { goto error ; } if ( ( flags & FLG_QUERY_WITH_RETURNDATA ) && NULL != pContext ) { rc = pContext->getMore( -1, buffObj, eduCB() ) ; if ( rc || pContext->eof() ) { _pRTNCB->contextDelete( contextID, eduCB() ) ; contextID = -1 ; } if ( SDB_DMS_EOC == rc ) { rc = SDB_OK ; } else if ( rc ) { PD_LOG( PDERROR, "Session[%s] failed to query with return " "data, rc: %d", getSession()->sessionName(), rc ) ; goto error ; } } } catch ( std::exception &e ) { PD_LOG ( PDERROR, "Session[%s] Failed to create matcher and " "selector for QUERY: %s", getSession()->sessionName(), e.what () ) ; rc = SDB_INVALIDARG ; goto error ; } } else { rc = rtnParserCommand( pCollectionName, &pCommand ) ; if ( SDB_OK != rc ) { PD_LOG ( PDERROR, "Parse command[%s] failed[rc:%d]", pCollectionName, rc ) ; goto error ; } rc = rtnInitCommand( pCommand , flags, numToSkip, numToReturn, pQueryBuff, pFieldSelector, pOrderByBuffer, pHintBuffer ) ; if ( SDB_OK != rc ) { goto error ; } PD_LOG ( PDDEBUG, "Command: %s", pCommand->name () ) ; rc = rtnRunCommand( pCommand, getSession()->getServiceType(), eduCB(), _pDMSCB, _pRTNCB, dpsCB, 1, &contextID ) ; if ( rc ) { goto error ; } } done: if ( pCommand ) { rtnReleaseCommand( &pCommand ) ; } return rc ; error: goto done ; }
INT32 _coordCMDListLobs::execute( MsgHeader *pMsg, pmdEDUCB *cb, INT64 &contextID, rtnContextBuf *buf ) { INT32 rc = SDB_OK ; SDB_RTNCB *pRtncb = pmdGetKRCB()->getRTNCB() ; CHAR *pQuery = NULL ; BSONObj query ; rtnContextCoord *context = NULL ; coordQueryOperator queryOpr( TRUE ) ; coordQueryConf queryConf ; coordSendOptions sendOpt ; contextID = -1 ; rc = msgExtractQuery( (CHAR*)pMsg, NULL, NULL, NULL, NULL, &pQuery, NULL, NULL, NULL ) ; PD_RC_CHECK( rc, PDERROR, "Parse message failed, rc: %d", rc ) ; try { query = BSONObj( pQuery ) ; BSONElement ele = query.getField( FIELD_NAME_COLLECTION ) ; if ( String != ele.type() ) { PD_LOG( PDERROR, "invalid obj of list lob:%s", query.toString( FALSE, TRUE ).c_str() ) ; rc = SDB_INVALIDARG ; goto error ; } queryConf._realCLName = ele.valuestr() ; } catch ( std::exception &e ) { PD_LOG( PDERROR, "unexpected err happened:%s", e.what() ) ; rc = SDB_SYS ; goto error ; } rc = queryOpr.init( _pResource, cb, getTimeout() ) ; if ( rc ) { PD_LOG( PDERROR, "Init query operator failed, rc: %d", rc ) ; goto error ; } queryConf._openEmptyContext = TRUE ; queryConf._allCataGroups = TRUE ; rc = queryOpr.queryOrDoOnCL( pMsg, cb, &context, sendOpt, &queryConf, buf ) ; PD_RC_CHECK( rc, PDERROR, "List lobs[%s] on groups failed, rc: %d", queryConf._realCLName.c_str(), rc ) ; contextID = context->contextID() ; done: return rc ; error: if ( context ) { pRtncb->contextDelete( context->contextID(), cb ) ; } goto done ; }
INT32 _coordCMDListCLInDomain::execute( MsgHeader *pMsg, pmdEDUCB *cb, INT64 &contextID, rtnContextBuf *buf ) { INT32 rc = SDB_OK ; BSONObj conObj ; BSONObj dummy ; CHAR *query = NULL ; BSONElement domain ; rtnQueryOptions queryOptions ; vector<BSONObj> replyFromCata ; contextID = -1 ; rc = msgExtractQuery( (CHAR*)pMsg, NULL, NULL, NULL, NULL, &query, NULL, NULL, NULL ); if ( rc != SDB_OK ) { PD_LOG ( PDERROR, "failed to parse query request(rc=%d)", rc ) ; goto error ; } try { conObj = BSONObj( query ) ; } catch ( std::exception &e ) { PD_LOG( PDERROR, "unexpected err happened: %s", e.what() ) ; rc = SDB_SYS ; goto error ; } domain = conObj.getField( FIELD_NAME_DOMAIN ) ; if ( String != domain.type() ) { PD_LOG( PDERROR, "invalid domain field in object:%s", conObj.toString().c_str() ) ; rc = SDB_INVALIDARG ; goto error ; } queryOptions.setQuery( BSON( CAT_DOMAIN_NAME << domain.valuestr() ) ) ; queryOptions.setCLFullName( CAT_COLLECTION_SPACE_COLLECTION ) ; rc = queryOnCataAndPushToVec( queryOptions, cb, replyFromCata, buf ) ; if ( SDB_OK != rc ) { PD_LOG( PDERROR, "failed to execute query on catalog:%d", rc ) ; goto error ; } rc = _rebuildListResult( replyFromCata, cb, contextID ) ; if ( SDB_OK != rc ) { PD_LOG( PDERROR, "failed to rebuild list result:%d", rc ) ; goto error ; } done: return rc ; error: if ( contextID >= 0 ) { pmdGetKRCB()->getRTNCB()->contextDelete( contextID, cb ) ; contextID = -1 ; } goto done ; }
// PD_TRACE_DECLARE_FUNCTION ( COORD_EVAL_EXE, "_coordCMDEval::execute" ) INT32 _coordCMDEval::execute( MsgHeader *pMsg, pmdEDUCB *cb, INT64 &contextID, rtnContextBuf *buf ) { INT32 rc = SDB_OK ; PD_TRACE_ENTRY( COORD_EVAL_EXE ) ; spdSession *session = NULL ; contextID = -1 ; CHAR *pQuery = NULL ; BSONObj procedures ; spdCoordDownloader downloader( this, cb ) ; BSONObj runInfo ; rc = msgExtractQuery( (CHAR*)pMsg, NULL, NULL, NULL, NULL, &pQuery, NULL, NULL, NULL ); if ( SDB_OK != rc ) { PD_LOG( PDERROR, "failed to extract eval msg:%d", rc) ; goto error ; } try { procedures = BSONObj( pQuery ) ; PD_LOG( PDDEBUG, "eval:%s", procedures.toString().c_str() ) ; } catch ( std::exception &e ) { PD_LOG( PDERROR, "unexpected err happened:%s", e.what() ) ; rc = SDB_SYS ; goto error ; } session = SDB_OSS_NEW _spdSession() ; if ( NULL == session ) { PD_LOG( PDERROR, "failed to allocate mem." ) ; rc = SDB_OOM ; goto error ; } rc = session->eval( procedures, &downloader, cb ) ; if ( SDB_OK != rc ) { const BSONObj &errmsg = session->getErrMsg() ; if ( !errmsg.isEmpty() ) { *buf = rtnContextBuf( errmsg.getOwned() ) ; } PD_LOG( PDERROR, "failed to eval store procedure:%d", rc ) ; goto error ; } if ( FMP_RES_TYPE_VOID != session->resType() ) { rc = _buildContext( session, cb, contextID ) ; if ( SDB_OK != rc ) { PD_LOG( PDERROR, "failed to prepare reply msg:%d", rc ) ; goto error ; } } runInfo = BSON( FIELD_NAME_RTYPE << session->resType() ) ; *buf = rtnContextBuf( runInfo ) ; done: if ( -1 == contextID ) { SAFE_OSS_DELETE( session ) ; } PD_TRACE_EXITRC( COORD_EVAL_EXE, rc ) ; return rc ; error: if ( contextID >= 0 ) { pmdGetKRCB()->getRTNCB()->contextDelete( contextID, cb ) ; contextID = -1 ; } goto done ; }
// PD_TRACE_DECLARE_FUNCTION ( COORD_CMDSTATBASE_EXE, "_coordCMDStatisticsBase::execute" ) INT32 _coordCMDStatisticsBase::execute( MsgHeader *pMsg, pmdEDUCB *cb, INT64 &contextID, rtnContextBuf *buf ) { INT32 rc = SDB_OK; PD_TRACE_ENTRY ( COORD_CMDSTATBASE_EXE ) ; SDB_RTNCB *pRtncb = pmdGetKRCB()->getRTNCB() ; contextID = -1 ; coordQueryOperator queryOpr( isReadOnly() ) ; rtnContextCoord *pContext = NULL ; coordQueryConf queryConf ; coordSendOptions sendOpt ; queryConf._openEmptyContext = openEmptyContext() ; CHAR *pHint = NULL ; rc = msgExtractQuery( (CHAR*)pMsg, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &pHint ); PD_RC_CHECK ( rc, PDERROR, "Execute failed, failed to parse query " "request, rc: %d", rc ) ; try { BSONObj boHint( pHint ) ; BSONElement ele = boHint.getField( FIELD_NAME_COLLECTION ) ; PD_CHECK ( ele.type() == String, SDB_INVALIDARG, error, PDERROR, "Execute failed, failed to get the field(%s)", FIELD_NAME_COLLECTION ) ; queryConf._realCLName = ele.str() ; } catch( std::exception &e ) { PD_RC_CHECK ( rc, PDERROR, "Execute failed, occured unexpected " "error:%s", e.what() ) ; } rc = queryOpr.init( _pResource, cb, getTimeout() ) ; if ( rc ) { PD_LOG( PDERROR, "Init query operator failed, rc: %d", rc ) ; goto error ; } rc = queryOpr.queryOrDoOnCL( pMsg, cb, &pContext, sendOpt, &queryConf, buf ) ; PD_RC_CHECK( rc, PDERROR, "Query failed, rc: %d", rc ) ; rc = generateResult( pContext, cb ) ; PD_RC_CHECK( rc, PDERROR, "Failed to execute statistics, rc: %d", rc ) ; contextID = pContext->contextID() ; pContext->reopen() ; done: PD_TRACE_EXITRC ( COORD_CMDSTATBASE_EXE, rc ) ; return rc; error: if ( pContext ) { pRtncb->contextDelete( pContext->contextID(), cb ) ; } goto done ; }
INT32 rtnCoordQuery::queryOnMainCL( CoordGroupSubCLMap &groupSubCLMap, MsgOpQuery *pSrc, pmdEDUCB *cb, netMultiRouteAgent *pRouteAgent, CoordGroupList &sendGroupList, rtnContextCoord *pContext ) { INT32 rc = SDB_OK; INT32 rcTmp = SDB_OK; CHAR *pBuffer = NULL; INT32 bufferSize = 0; REPLY_QUE replyQue; BOOLEAN takeOver = FALSE ; SDB_ASSERT( pContext, "pContext can't be NULL!" ); try { CoordGroupSubCLMap::iterator iterGroup; REQUESTID_MAP sendNodes; INT32 flag; CHAR *pCollectionName; SINT64 numToSkip; SINT64 numToReturn; CHAR *pQuery; CHAR *pFieldSelector; CHAR *pOrderBy; CHAR *pHint; BSONObj boQuery; BSONObj boFieldSelector; BSONObj boOrderBy; BSONObj boHint; rc = msgExtractQuery( (CHAR *)pSrc, &flag, &pCollectionName, &numToSkip, &numToReturn, &pQuery, &pFieldSelector, &pOrderBy, &pHint ); PD_RC_CHECK( rc, PDERROR, "failed to parse query message(rc=%d)", rc ); boQuery = BSONObj( pQuery ); boFieldSelector = BSONObj( pFieldSelector ); boOrderBy = BSONObj( pOrderBy ); boHint = BSONObj( pHint ); iterGroup = groupSubCLMap.begin(); while( iterGroup != groupSubCLMap.end() ) { BSONArrayBuilder babSubCL; CoordGroupInfoPtr groupInfo; CoordSubCLlist::iterator iterSubCL = iterGroup->second.begin(); while( iterSubCL != iterGroup->second.end() ) { babSubCL.append( *iterSubCL ); ++iterSubCL; } BSONObjBuilder bobNewQuery; bobNewQuery.appendElements( boQuery ); bobNewQuery.appendArray( CAT_SUBCL_NAME, babSubCL.arr() ); BSONObj boNewQuery = bobNewQuery.obj(); rc = msgBuildQueryMsg( &pBuffer, &bufferSize, pCollectionName, flag, 0, numToSkip, numToReturn, &boNewQuery, &boFieldSelector, &boOrderBy, &boHint ); PD_CHECK( SDB_OK == rc, rc, RECV_MSG, PDERROR, "Failed to build query message(rc=%d)", rc ); { MsgOpQuery *pReqMsg = (MsgOpQuery *)pBuffer; pReqMsg->version = pSrc->version; pReqMsg->w = pSrc->w; } rc = rtnCoordGetGroupInfo( cb, iterGroup->first, FALSE, groupInfo ) ; PD_CHECK( SDB_OK == rc, rc, RECV_MSG, PDERROR, "Failed to get group info(groupId=%u, rc=%d)", iterGroup->first, rc ) ; rc = rtnCoordSendRequestToOne( pBuffer, groupInfo, sendNodes, pRouteAgent, MSG_ROUTE_SHARD_SERVCIE, cb ) ; PD_CHECK( SDB_OK == rc, rc, RECV_MSG, PDERROR, "Failed to send request(rc=%d)", rc ) ; ++iterGroup; } RECV_MSG: rcTmp = rtnCoordGetReply( cb, sendNodes, replyQue, MSG_BS_QUERY_RES ) ; if ( SDB_OK != rcTmp ) { PD_LOG( PDWARNING, "failed to get reply(rcTmp=%d)", rcTmp ); if ( SDB_APP_INTERRUPT == rcTmp || SDB_OK == rc ) { rc = rcTmp; } } while( !replyQue.empty() ) { takeOver = FALSE ; MsgOpReply *pReply = NULL; pReply = (MsgOpReply *)(replyQue.front()); rcTmp = pReply->flags ; replyQue.pop(); UINT32 groupID = pReply->header.routeID.columns.groupID; if ( rcTmp != SDB_OK ) { if ( SDB_OK == rc ) { rc = rcTmp; } if ( SDB_DMS_EOC == rcTmp ) { sendGroupList[ groupID ] = groupID; groupSubCLMap.erase( groupID ); rcTmp = SDB_OK; } else { CoordGroupInfoPtr groupInfoTmp; cb->getCoordSession()->removeLastNode( groupID ); if ( SDB_CLS_FULL_SYNC == rcTmp ) { rtnCoordUpdateNodeStatByRC( pReply->header.routeID, rcTmp ); } else { rcTmp = rtnCoordGetGroupInfo( cb, groupID, TRUE, groupInfoTmp ); if ( rcTmp ) { PD_LOG ( PDERROR, "Failed to update group info" "(groupID=%u, rc=%d)", pReply->header.routeID.columns.groupID, rcTmp ); if ( SDB_OK == rc ) { rc = rcTmp; } } } } } else { rcTmp = pContext->addSubContext( pReply, takeOver ); if ( SDB_OK == rcTmp ) { sendGroupList[ groupID ] = groupID; groupSubCLMap.erase( groupID ); } } if ( rcTmp != SDB_OK && SDB_OK == rc ) { rc = rcTmp; } if ( !takeOver ) { SDB_OSS_FREE( pReply ); } } } catch ( std::exception &e ) { PD_RC_CHECK( SDB_INVALIDARG, PDERROR, "Occur unexpected error:%s", e.what() ) ; } done: if ( pBuffer != NULL ) { SDB_OSS_FREE( pBuffer ); pBuffer = NULL; } return rc; error: goto done; }
INT32 rtnCoordQuery::execute( CHAR *pReceiveBuffer, SINT32 packSize, CHAR **ppResultBuffer, pmdEDUCB *cb, MsgOpReply &replyHeader, BSONObj **ppErrorObj ) { INT32 rc = SDB_OK ; pmdKRCB *pKrcb = pmdGetKRCB(); CoordCB *pCoordcb = pKrcb->getCoordCB(); netMultiRouteAgent *pRouteAgent = pCoordcb->getRouteAgent(); rtnContextCoord *pContext = NULL ; BSONObj boQuery; BSONObj boOrderBy; CoordGroupList sendGroupList ; MsgHeader*pHeader = (MsgHeader *)pReceiveBuffer; replyHeader.header.messageLength = sizeof( MsgOpReply ); replyHeader.header.opCode = MSG_BS_QUERY_RES; replyHeader.header.requestID = pHeader->requestID; replyHeader.header.routeID.value = 0; replyHeader.header.TID = pHeader->TID; replyHeader.contextID = -1; replyHeader.flags = SDB_OK; replyHeader.numReturned = 0; replyHeader.startFrom = 0; INT32 flag = 0; CHAR *pCollectionName = NULL; SINT64 numToSkip = 0; SINT64 numToReturn = 0; CHAR *pQuery = NULL; CHAR *pFieldSelector = NULL; CHAR *pOrderBy = NULL; CHAR *pHint = NULL; BSONObj *err = NULL ; MsgOpQuery *pSrc = (MsgOpQuery *)pReceiveBuffer; rc = msgExtractQuery( pReceiveBuffer, &flag, &pCollectionName, &numToSkip, &numToReturn, &pQuery, &pFieldSelector, &pOrderBy, &pHint ); PD_RC_CHECK( rc, PDERROR, "failed to parse query request(rc=%d)", rc ); if ( pCollectionName != NULL && '$' == pCollectionName[0] ) { rtnCoordCommand *pCmdProcesser = NULL; rtnCoordProcesserFactory *pProcesserFactory = pCoordcb->getProcesserFactory(); pCmdProcesser = pProcesserFactory->getCommandProcesser( pCollectionName ); PD_CHECK( pCmdProcesser != NULL, SDB_INVALIDARG, error, PDERROR, "unknown command:%s", pCollectionName ); rc = pCmdProcesser->execute( pReceiveBuffer, packSize, ppResultBuffer, cb, replyHeader, &err ); SDB_ASSERT( NULL == err, "impossible" ); PD_RC_CHECK( rc, PDERROR, "failed to execute the command(command:%s, " "rc=%d)", pCollectionName, rc ); goto done; } try { boQuery = BSONObj( pQuery ); boOrderBy = BSONObj( pOrderBy ); } catch ( std::exception &e ) { PD_RC_CHECK( SDB_INVALIDARG, PDERROR, "occur unexpected error:%s", e.what() ); } rc = executeQuery( (CHAR *)pSrc, boQuery, boOrderBy, pCollectionName, pRouteAgent, cb, pContext ) ; PD_RC_CHECK( rc, PDERROR, "query failed(rc=%d)", rc ); replyHeader.contextID = pContext->contextID() ; pContext->addSubDone( cb ); done: return rc; error: replyHeader.flags = rc; goto done; }