INT32 rtnCoordDelete::deleteNormalCL( CoordCataInfoPtr cataInfo, bson::BSONObj &boDelete, MsgOpDelete *pDelMsg, netMultiRouteAgent *pRouteAgent, pmdEDUCB *cb, CoordGroupList &sendGroupLst ) { INT32 rc = SDB_OK; CoordGroupList groupLst; rc = getNodeGroups( cataInfo, boDelete, sendGroupLst, groupLst ); PD_RC_CHECK( rc, PDERROR, "delete failed, couldn't get the match sharding(rc=%d)", rc ); rc = buildTransSession( groupLst, pRouteAgent, cb ); PD_RC_CHECK( rc, PDERROR, "failed to build transaction session(rc=%d)", rc ); pDelMsg->version = cataInfo->getVersion(); pDelMsg->header.routeID.value = 0; pDelMsg->header.TID = cb->getTID(); rc = deleteToDataNodeGroup( (CHAR *)pDelMsg, groupLst, sendGroupLst, pRouteAgent, cb ); PD_RC_CHECK( rc, PDERROR, "failed to delete on data node(rc=%d)", rc ); done: return rc; error: goto done; }
// PD_TRACE_DECLARE_FUNCTION ( SDB_RTNCOINS_SHARDANOBJ, "rtnCoordInsert::shardAnObj" ) INT32 rtnCoordInsert::shardAnObj( CHAR *pInsertor, const CoordCataInfoPtr &cataInfo, GroupObjsMap &groupObjsMap ) { INT32 rc = SDB_OK; PD_TRACE_ENTRY ( SDB_RTNCOINS_SHARDANOBJ ) ; try { BSONObj insertObj( pInsertor ); UINT32 groupID = 0; rc = cataInfo->getGroupByRecord( insertObj, groupID ); PD_RC_CHECK( rc, PDERROR, "Failed to get the group(rc=%d)", rc ); groupObjsMap[ groupID ].push_back( pInsertor ) ; } catch ( std::exception &e ) { PD_CHECK( FALSE, SDB_INVALIDARG, error, PDERROR, "Failed to shard the data, received unexpected error:%s", e.what() ); } done: PD_TRACE_EXITRC ( SDB_RTNCOINS_SHARDANOBJ, rc ) ; return rc; error: goto done; }
// PD_TRACE_DECLARE_FUNCTION ( SDB_RTNCOQUERY_GETNODEGROUPS, "rtnCoordQuery::getNodeGroups" ) INT32 rtnCoordQuery::getNodeGroups( const CoordCataInfoPtr &cataInfo, BSONObj &queryObj, const CoordGroupList &sendGroupLst, CoordGroupList &groupLst ) { INT32 rc = SDB_OK; PD_TRACE_ENTRY ( SDB_RTNCOQUERY_GETNODEGROUPS ) ; cataInfo->getGroupByMatcher( queryObj, groupLst ) ; PD_CHECK( groupLst.size() > 0, SDB_CAT_NO_MATCH_CATALOG, error, PDERROR, "failed to get match groups" ); { CoordGroupList::const_iterator iterList = sendGroupLst.begin(); while( iterList != sendGroupLst.end() ) { groupLst.erase( iterList->first ); ++iterList; } } done: PD_TRACE_EXITRC ( SDB_RTNCOQUERY_GETNODEGROUPS, rc ) ; return rc; error: goto done; }
INT32 rtnCoordDelete::buildOpMsg( const CoordCataInfoPtr &cataInfo, const CoordSubCLlist &subCLList, CHAR *pSrcMsg, CHAR *&pDstMsg, INT32 &bufferSize ) { INT32 rc = SDB_OK; INT32 flag; CHAR *pCollectionName = NULL; CHAR *pDeletor = NULL; CHAR *pHint = NULL; BSONObj boDeletor; BSONObj boHint; rc = msgExtractDelete( pSrcMsg, &flag, &pCollectionName, &pDeletor, &pHint ); PD_RC_CHECK( rc, PDERROR, "failed to parse delete request(rc=%d)", rc ); try { boDeletor = BSONObj( pDeletor ); boHint = BSONObj( pHint ); BSONArrayBuilder babSubCL; CoordSubCLlist::const_iterator iterCL = subCLList.begin(); while( iterCL != subCLList.end() ) { babSubCL.append( *iterCL ); ++iterCL; } BSONObjBuilder bobNewDeletor; bobNewDeletor.appendElements( boDeletor ); bobNewDeletor.appendArray( CAT_SUBCL_NAME, babSubCL.arr() ); BSONObj boNewDeletor = bobNewDeletor.obj(); rc = msgBuildDeleteMsg( &pDstMsg, &bufferSize, pCollectionName, flag, 0, &boNewDeletor, &boHint ); PD_RC_CHECK( rc, PDERROR, "failed to build delete request(rc=%d)", rc ); { MsgOpDelete *pReqMsg = (MsgOpDelete *)pDstMsg; MsgOpDelete *pSrcReq = (MsgOpDelete *)pSrcMsg; pReqMsg->version = cataInfo->getVersion(); pReqMsg->w = pSrcReq->w; } } catch ( std::exception &e ) { PD_RC_CHECK( SDB_INVALIDARG, PDERROR, "occur unexpected error:%s", e.what() ); } done: return rc; error: goto done; }
INT32 rtnCoordInsert::shardAnObj( CHAR *pInsertor, const CoordCataInfoPtr &cataInfo, pmdEDUCB * cb, GroupSubCLMap &groupSubCLMap ) { INT32 rc = SDB_OK; std::string subCLName ; UINT32 groupID = CAT_INVALID_GROUPID; try { BSONObj insertObj( pInsertor ) ; CoordCataInfoPtr subClCataInfo; rc = cataInfo->getSubCLNameByRecord( insertObj, subCLName ) ; PD_CHECK( SDB_OK == rc, SDB_CLS_COORD_NODE_CAT_VER_OLD, error, PDWARNING, "couldn't find the match sub-collection(rc=%d)", rc ) ; rc = rtnCoordGetCataInfo( cb, subCLName.c_str(), FALSE, subClCataInfo ); PD_CHECK( SDB_OK == rc, SDB_CLS_COORD_NODE_CAT_VER_OLD, error, PDWARNING, "failed to get catalog of sub-collection(%s)", subCLName.c_str() ); rc = subClCataInfo->getGroupByRecord( insertObj, groupID ); PD_CHECK( SDB_OK == rc, SDB_CLS_COORD_NODE_CAT_VER_OLD, error, PDWARNING, "couldn't find the match catalog of " "sub-collection(%s)", subCLName.c_str() ); (groupSubCLMap[ groupID ])[ subCLName ].push_back( pInsertor ); } catch ( std::exception &e ) { PD_CHECK( FALSE, SDB_INVALIDARG, error, PDERROR, "Failed to shard the data, occur unexpected error:%s", e.what() ); } done: return rc ; error: goto done ; }
INT32 _coordShardKicker::_checkShardingKey( const string &collectionName, const BSONObj &updator, BOOLEAN &hasInclude, _pmdEDUCB *cb ) { INT32 rc = SDB_OK ; CoordCataInfoPtr cataPtr ; rc = _pResource->getOrUpdateCataInfo( collectionName.c_str(), cataPtr, cb ) ; if ( SDB_CLS_COORD_NODE_CAT_VER_OLD == rc ) { rc = SDB_OK ; goto done ; } else if ( rc ) { PD_LOG( PDERROR, "Update collection[%s]'s catalog info failed, " "rc: %d", collectionName.c_str(), rc ) ; goto error ; } if ( !cataPtr->isSharded() ) { goto done ; } rc = _checkShardingKey( cataPtr, updator, hasInclude ) ; if ( rc ) { goto error ; } done: return rc ; error: goto done ; }
INT32 rtnCoordDelete::getNodeGroups( const CoordCataInfoPtr &cataInfo, bson::BSONObj &deleteObj, CoordGroupList &sendGroupLst, CoordGroupList &groupLst ) { INT32 rc = SDB_OK; PD_TRACE_ENTRY ( SDB_RTNCODEL_GETNODEGROUPS ) ; cataInfo->getGroupByMatcher( deleteObj, groupLst ); if ( groupLst.size() <= 0 ) { rc = SDB_CAT_NO_MATCH_CATALOG; } else { CoordGroupList::iterator iter = sendGroupLst.begin(); while( iter != sendGroupLst.end() ) { groupLst.erase( iter->first ); ++iter; } } PD_TRACE_EXITRC ( SDB_RTNCODEL_GETNODEGROUPS, rc ) ; return rc; }
INT32 _coordShardKicker::_kickShardingKey( const CoordCataInfoPtr &cataInfo, const BSONObj &updator, BSONObj &newUpdator, BOOLEAN &hasShardingKey ) { INT32 rc = SDB_OK ; UINT32 skSiteID = cataInfo->getShardingKeySiteID() ; if ( skSiteID > 0 ) { map< UINT32, BOOLEAN >::iterator it = _skSiteIDs.find( skSiteID ); if ( it != _skSiteIDs.end() ) { newUpdator = updator ; hasShardingKey = it->second ; goto done ; } } try { BSONObjBuilder bobNewUpdator( updator.objsize() ) ; BSONObj boShardingKey ; BSONObj subObj ; BOOLEAN isReplace = _isUpdateReplace( updator ) ; cataInfo->getShardingKey( boShardingKey ) ; BSONObjIterator iter( updator ) ; while ( iter.more() ) { BSONElement beTmp = iter.next() ; if ( beTmp.type() != Object ) { rc = SDB_INVALIDARG; PD_LOG( PDERROR, "updator's element must be an Object type:" "updator=%s", updator.toString().c_str() ) ; goto error ; } subObj = beTmp.embeddedObject() ; if ( isReplace && 0 == ossStrcmp( beTmp.fieldName(), CMD_ADMIN_PREFIX FIELD_OP_VALUE_KEEP ) ) { _addKeys( subObj ) ; continue ; } BSONObjBuilder subBuilder( bobNewUpdator.subobjStart( beTmp.fieldName() ) ) ; BSONObjIterator iterField( subObj ) ; while( iterField.more() ) { BSONElement beField = iterField.next() ; BSONObjIterator iterKey( boShardingKey ) ; BOOLEAN isKey = FALSE ; while( iterKey.more() ) { BSONElement beKey = iterKey.next(); const CHAR *pKey = beKey.fieldName(); const CHAR *pField = beField.fieldName(); while( *pKey == *pField && *pKey != '\0' ) { ++pKey ; ++pField ; } if ( *pKey == *pField || ( '\0' == *pKey && '.' == *pField ) || ( '.' == *pKey && '\0' == *pField ) ) { isKey = TRUE ; break ; } } if ( isKey ) { hasShardingKey = TRUE; } else { subBuilder.append( beField ) ; } } // while( iterField.more() ) subBuilder.done() ; } // while ( iter.more() ) if ( isReplace ) { UINT32 count = _addKeys( boShardingKey ) ; if ( count > 0 ) { hasShardingKey = TRUE ; } if ( !_setKeys.empty() ) { BSONObjBuilder keepBuilder( bobNewUpdator.subobjStart( CMD_ADMIN_PREFIX FIELD_OP_VALUE_KEEP ) ) ; SET_SHARDINGKEY::iterator itKey = _setKeys.begin() ; while( itKey != _setKeys.end() ) { keepBuilder.append( itKey->_pStr, (INT32)1 ) ; ++itKey ; } keepBuilder.done() ; } } // if ( isReplace ) newUpdator = bobNewUpdator.obj() ; } catch ( std::exception &e ) { rc = SDB_INVALIDARG; PD_LOG ( PDERROR,"Failed to kick sharding key from the record," "occured unexpected error: %s", e.what() ) ; goto error; } if ( skSiteID > 0 ) { _skSiteIDs.insert( pair< UINT32, BOOLEAN >( skSiteID, hasShardingKey ) ) ; } done: return rc; error: goto done; }
INT32 _coordShardKicker::_checkShardingKey( const CoordCataInfoPtr &cataInfo, const BSONObj &updator, BOOLEAN &hasInclude ) { INT32 rc = SDB_OK ; UINT32 skSiteID = cataInfo->getShardingKeySiteID() ; if ( skSiteID > 0 ) { if ( _skSiteIDs.count( skSiteID ) > 0 ) { goto done ; } _skSiteIDs.insert( pair< UINT32, BOOLEAN >( skSiteID, TRUE ) ) ; } try { BSONObjBuilder bobNewUpdator( updator.objsize() ) ; BSONObj boShardingKey ; BSONObj subObj ; BOOLEAN isReplace = _isUpdateReplace( updator ) ; cataInfo->getShardingKey( boShardingKey ) ; BSONObjIterator iter( updator ) ; while ( iter.more() ) { BSONElement beTmp = iter.next() ; if ( beTmp.type() != Object ) { rc = SDB_INVALIDARG; PD_LOG( PDERROR, "updator's element must be an Object type:" "updator=%s", updator.toString().c_str() ) ; goto error ; } subObj = beTmp.embeddedObject() ; if ( isReplace && 0 == ossStrcmp( beTmp.fieldName(), CMD_ADMIN_PREFIX FIELD_OP_VALUE_KEEP ) ) { _addKeys( subObj ) ; continue ; } BSONObjIterator iterField( subObj ) ; while( iterField.more() ) { BSONElement beField = iterField.next() ; BSONObjIterator iterKey( boShardingKey ) ; BOOLEAN isKey = FALSE ; while( iterKey.more() ) { BSONElement beKey = iterKey.next() ; const CHAR *pKey = beKey.fieldName() ; const CHAR *pField = beField.fieldName() ; while( *pKey == *pField && *pKey != '\0' ) { ++pKey ; ++pField ; } if ( *pKey == *pField || ( '\0' == *pKey && '.' == *pField ) || ( '.' == *pKey && '\0' == *pField ) ) { isKey = TRUE ; break ; } } if ( isKey ) { hasInclude = TRUE ; goto done ; } } // while( iterField.more() ) } // while ( iter.more() ) if ( isReplace && _addKeys( boShardingKey ) > 0 ) { hasInclude = TRUE ; goto done ; } } catch ( std::exception &e ) { rc = SDB_INVALIDARG; PD_LOG ( PDERROR,"Failed to check the record is include sharding-key," "occured unexpected error: %s", e.what() ) ; goto error; } done: return rc; error: goto done; }
INT32 _coordShardKicker::_kickShardingKey( const string &collectionName, const BSONObj &updator, BSONObj &newUpdator, BOOLEAN &isChanged, pmdEDUCB *cb, BOOLEAN keepShardingKey ) { INT32 rc = SDB_OK ; CoordCataInfoPtr cataPtr ; BOOLEAN hasShardingKey = FALSE ; rc = _pResource->getOrUpdateCataInfo( collectionName.c_str(), cataPtr, cb ) ; if ( SDB_CLS_COORD_NODE_CAT_VER_OLD == rc ) { rc = SDB_OK ; goto done ; } else if ( rc ) { PD_LOG( PDERROR, "Update collection[%s]'s catalog info failed, " "rc: %d", collectionName.c_str(), rc ) ; goto error ; } if ( !cataPtr->isSharded() ) { goto done ; } rc = _kickShardingKey( cataPtr, updator, newUpdator, hasShardingKey ) ; if ( rc ) { goto error ; } if ( keepShardingKey ) { if ( 1 != cataPtr->getGroupNum() && hasShardingKey ) { rc = SDB_UPDATE_SHARD_KEY ; PD_LOG( PDERROR, "When the partition cl falls on two or " "more groups, the update rule don't allow sharding" " key. rc: %d", rc ) ; goto error ; } } else { if ( hasShardingKey ) { isChanged = TRUE ; } } done: return rc ; error: goto done ; }
//PD_TRACE_DECLARE_FUNCTION ( SDB_RTNCODEL_EXECUTE, "rtnCoordDelete::execute" ) INT32 rtnCoordDelete::execute( CHAR *pReceiveBuffer, SINT32 packSize, pmdEDUCB *cb, MsgOpReply &replyHeader, rtnContextBuf *buf ) { INT32 rc = SDB_OK; pmdKRCB *pKrcb = pmdGetKRCB(); CoordCB *pCoordcb = pKrcb->getCoordCB(); netMultiRouteAgent *pRouteAgent = pCoordcb->getRouteAgent(); rtnCoordOperator *pRollbackOperator = NULL; BOOLEAN isNeedRefresh = FALSE; BOOLEAN hasRefresh = FALSE; CoordGroupList sendGroupLst; BSONObj boDeletor; MsgHeader *pHeader = (MsgHeader *)pReceiveBuffer; replyHeader.header.messageLength = sizeof( MsgOpReply ); replyHeader.header.opCode = MSG_BS_DELETE_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; CHAR *pDeletor = NULL; CHAR *pHint = NULL; rc = msgExtractDelete( pReceiveBuffer, &flag, &pCollectionName, &pDeletor, &pHint ); PD_RC_CHECK( rc, PDERROR, "failed to parse delete request(rc=%d)", rc ); try { boDeletor = BSONObj( pDeletor ); } catch ( std::exception &e ) { PD_RC_CHECK( SDB_INVALIDARG, PDERROR, "delete failed, received unexpected error:%s", e.what() ); } do { hasRefresh = isNeedRefresh; CoordCataInfoPtr cataInfo; rc = rtnCoordGetCataInfo(cb, pCollectionName, isNeedRefresh, cataInfo ); PD_RC_CHECK( rc, PDERROR, "delete failed, " "failed to get the catalogue info(collection name:%s)", pCollectionName ); if ( cataInfo->isMainCL() ) { std::set< INT32 > emptyRCList; CoordSubCLlist subCLList; rc = cataInfo->getMatchSubCLs( boDeletor, subCLList ); PD_RC_CHECK( rc, PDERROR, "failed to get match sub-collection(rc=%d)", rc ); rc = modifyOpOnMainCL( cataInfo, subCLList, (MsgHeader *)pReceiveBuffer, pRouteAgent, cb, isNeedRefresh, emptyRCList, sendGroupLst ); } else { rc = deleteNormalCL( cataInfo, boDeletor, (MsgOpDelete *)pReceiveBuffer, pRouteAgent, cb, sendGroupLst ); } if ( !hasRefresh && ( (!cb->isTransaction() && rtnCoordWriteRetryRC( rc )) || SDB_CLS_COORD_NODE_CAT_VER_OLD == rc || SDB_CLS_NO_CATALOG_INFO == rc || SDB_CAT_NO_MATCH_CATALOG == rc )) { isNeedRefresh = TRUE; } else { isNeedRefresh = FALSE; } }while( isNeedRefresh ); if ( cb->isTransaction() ) { rc = rc ? rc : cb->getTransRC(); } PD_RC_CHECK( rc, PDERROR, "delete failed(rc=%d)", rc ) ; done: return rc; error: if ( cb->isTransaction() ) { pRollbackOperator = pCoordcb->getProcesserFactory()->getOperator( MSG_BS_TRANS_ROLLBACK_REQ ); if ( pRollbackOperator ) { pRollbackOperator->execute( pReceiveBuffer, packSize, cb, replyHeader, NULL ); } } replyHeader.flags = rc; goto done; }
//PD_TRACE_DECLARE_FUNCTION ( SDB_RTNCODEL_EXECUTE, "rtnCoordDelete::execute" ) INT32 rtnCoordDelete::execute( MsgHeader *pMsg, pmdEDUCB *cb, INT64 &contextID, rtnContextBuf *buf ) { INT32 rc = SDB_OK ; INT32 rcTmp = SDB_OK ; PD_TRACE_ENTRY ( SDB_RTNCODEL_EXECUTE ) ; pmdKRCB *pKrcb = pmdGetKRCB() ; CoordCB *pCoordcb = pKrcb->getCoordCB() ; netMultiRouteAgent *pRouteAgent = pCoordcb->getRouteAgent() ; // process define rtnSendOptions sendOpt( TRUE ) ; rtnSendMsgIn inMsg( pMsg ) ; rtnProcessResult result ; ROUTE_RC_MAP nokRC ; result._pNokRC = &nokRC ; CoordCataInfoPtr cataInfo ; MsgRouteID errNodeID ; UINT64 deleteNum = 0 ; inMsg._pvtData = ( CHAR* )&deleteNum ; inMsg._pvtType = PRIVATE_DATA_NUMBERLONG ; BSONObj boDeletor ; // fill default-reply(delete success) MsgOpDelete *pDelMsg = (MsgOpDelete *)pMsg ; INT32 oldFlag = pDelMsg->flags ; pDelMsg->flags |= FLG_DELETE_RETURNNUM ; contextID = -1 ; INT32 flag = 0; CHAR *pCollectionName = NULL ; CHAR *pDeletor = NULL ; CHAR *pHint = NULL ; rc = msgExtractDelete( (CHAR*)pMsg, &flag, &pCollectionName, &pDeletor, &pHint ) ; if( rc ) { PD_LOG( PDERROR,"Failed to parse delete request, rc: %d", rc ) ; pCollectionName = NULL ; goto error ; } try { boDeletor = BSONObj( pDeletor ) ; } catch ( std::exception &e ) { PD_RC_CHECK( SDB_INVALIDARG, PDERROR, "Delete failed, received unexpected error:%s", e.what() ) ; } // add last op info MON_SAVE_OP_DETAIL( cb->getMonAppCB(), pMsg->opCode, "Collection:%s, Deletor:%s, Hint:%s, " "Flag:0x%08x(%u)", pCollectionName, boDeletor.toString().c_str(), BSONObj(pHint).toString().c_str(), oldFlag, oldFlag ) ; rc = rtnCoordGetCataInfo( cb, pCollectionName, FALSE, cataInfo ) ; PD_RC_CHECK( rc, PDERROR, "Delete failed, failed to get the " "catalogue info(collection name: %s), rc: %d", pCollectionName, rc ) ; retry: do { pDelMsg->version = cataInfo->getVersion() ; pDelMsg->w = 0 ; if ( cataInfo->isMainCL() ) { rcTmp = doOpOnMainCL( cataInfo, boDeletor, inMsg, sendOpt, pRouteAgent, cb, result ) ; } else { rcTmp = doOpOnCL( cataInfo, boDeletor, inMsg, sendOpt, pRouteAgent, cb, result ) ; } }while( FALSE ) ; if ( SDB_OK == rcTmp && nokRC.empty() ) { goto done ; } else if ( checkRetryForCLOpr( rcTmp, &nokRC, inMsg.msg(), sendOpt._retryTimes, cataInfo, cb, rc, &errNodeID, TRUE ) ) { nokRC.clear() ; ++sendOpt._retryTimes ; goto retry ; } else { PD_LOG( PDERROR, "Delete failed on node[%s], rc: %d", routeID2String( errNodeID ).c_str(), rc ) ; goto error ; } done: if ( oldFlag & FLG_DELETE_RETURNNUM ) { contextID = deleteNum ; } if ( pCollectionName ) { /// AUDIT PD_AUDIT_OP( AUDIT_DML, MSG_BS_DELETE_REQ, AUDIT_OBJ_CL, pCollectionName, rc, "DeletedNum:%u, Deletor:%s, Hint:%s, Flag:0x%08x(%u)", deleteNum, boDeletor.toString().c_str(), BSONObj(pHint).toString().c_str(), oldFlag, oldFlag ) ; } PD_TRACE_EXITRC ( SDB_RTNCODEL_EXECUTE, rc ) ; return rc ; error: goto done ; }
// PD_TRACE_DECLARE_FUNCTION ( SDB_RTNCOINS_EXECUTE, "rtnCoordInsert::execute" ) INT32 rtnCoordInsert::execute( CHAR *pReceiveBuffer, SINT32 packSize, pmdEDUCB *cb, MsgOpReply &replyHeader, rtnContextBuf *buf ) { INT32 rc = SDB_OK; PD_TRACE_ENTRY ( SDB_RTNCOINS_EXECUTE ) ; pmdKRCB *pKrcb = pmdGetKRCB(); CoordCB *pCoordcb = pKrcb->getCoordCB(); netMultiRouteAgent *pRouteAgent = pCoordcb->getRouteAgent(); rtnCoordOperator *pRollbackOperator = NULL; MsgHeader *pHeader = (MsgHeader *)pReceiveBuffer; replyHeader.header.messageLength = sizeof( MsgOpReply ); replyHeader.header.opCode = MSG_BS_INSERT_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; MsgOpInsert *pSrcMsg = (MsgOpInsert *)pReceiveBuffer; BOOLEAN isNeedRefreshCata = FALSE; GroupObjsMap groupObjsMap; BOOLEAN hasSendSomeData = FALSE; GroupSubCLMap groupSubCLMap; INT32 flag = 0; CHAR *pCollectionName = NULL; CHAR *pInsertor = NULL; INT32 count = 0; rc = msgExtractInsert( pReceiveBuffer, &flag, &pCollectionName, &pInsertor, count ) ; PD_RC_CHECK( rc, PDERROR, "Failed to parse insert request" ); pSrcMsg->header.TID = cb->getTID(); if ( cb->isTransaction() ) { pSrcMsg->header.opCode = MSG_BS_TRANS_INSERT_REQ; } else { pSrcMsg->header.opCode = MSG_BS_INSERT_REQ; } while ( TRUE ) { CoordCataInfoPtr cataInfo; rc = rtnCoordGetCataInfo( cb, pCollectionName, isNeedRefreshCata, cataInfo ); PD_RC_CHECK( rc, PDERROR, "Failed to get the catalog info(collection name:%s)", pCollectionName ); pSrcMsg->header.routeID.value = 0; pSrcMsg->version = cataInfo->getVersion(); if ( !cataInfo->isSharded() ) { CoordGroupList groupLst; cataInfo->getGroupLst( groupLst ); PD_CHECK( groupLst.size() > 0, SDB_SYS, error, PDERROR, "invalid catalog-info, no group-info" ); rc = buildTransSession( groupLst, pRouteAgent, cb ); PD_RC_CHECK( rc, PDERROR, "Failed to build transaction session(rc=%d)", rc ); CoordGroupList::iterator iterLst = groupLst.begin(); rc = insertToAGroup( pReceiveBuffer, iterLst->first, pRouteAgent, cb ); }//end of if ( !cataInfo->isSharded() ) else if( !cataInfo->isMainCL() ) { rc = insertToNormalCL( cataInfo, pReceiveBuffer, pInsertor, count, pRouteAgent, cb, groupObjsMap, hasSendSomeData ) ; }//end of else if( !cataInfo->isMainCL() ) else { rc = insertToMainCL( cataInfo, pReceiveBuffer, pInsertor, count, pRouteAgent, cb, groupSubCLMap ) ; } if ( SDB_OK != rc ) { if ( !isNeedRefreshCata && ( (!cb->isTransaction() && rtnCoordWriteRetryRC( rc )) || SDB_CLS_COORD_NODE_CAT_VER_OLD == rc || SDB_CLS_NO_CATALOG_INFO == rc || SDB_CAT_NO_MATCH_CATALOG == rc )) { isNeedRefreshCata = TRUE; continue; } if ( SDB_CLS_COORD_NODE_CAT_VER_OLD == rc ) { rc = SDB_CAT_NO_MATCH_CATALOG; } PD_RC_CHECK ( rc, PDERROR, "Failed to insert the record to " "data-node, rc = %d", rc ) ; } break; } if ( cb->isTransaction() ) { rc = rc ? rc : cb->getTransRC(); } if ( rc ) { goto error; } done: PD_TRACE_EXITRC ( SDB_RTNCOINS_EXECUTE, rc ) ; return rc; error: if ( cb->isTransaction() ) { pRollbackOperator = pCoordcb->getProcesserFactory()->getOperator( MSG_BS_TRANS_ROLLBACK_REQ ); if ( pRollbackOperator ) { pRollbackOperator->execute( pReceiveBuffer, packSize, cb, replyHeader, NULL ); } } replyHeader.flags = rc; goto done; }
INT32 rtnCoordQuery::executeQuery( CHAR *pSrc , BSONObj &boQuery , BSONObj &boOrderBy , const CHAR * pCollectionName , netMultiRouteAgent *pRouteAgent , pmdEDUCB *cb , rtnContextCoord *&pContext ) { SDB_ASSERT( pSrc, "pSrc can't be null!" ) ; SDB_ASSERT( pCollectionName, "pCollectionName can't be null!" ) ; INT32 rc = SDB_OK ; SINT64 contextID = -1 ; BOOLEAN isNeedRefresh = FALSE ; BOOLEAN hasRetry = FALSE ; CoordCataInfoPtr cataInfo ; CoordGroupList sendGroupList ; MsgOpQuery *pQuery = (MsgOpQuery *)pSrc ; SDB_RTNCB *pRtncb = pmdGetKRCB()->getRTNCB() ; rc = pRtncb->contextNew( RTN_CONTEXT_COORD, (rtnContext **)&pContext, contextID, cb ) ; PD_RC_CHECK( rc, PDERROR, "Failed to allocate context(rc=%d)", rc ) ; if ( FLG_QUERY_EXPLAIN & pQuery->flags ) { rc = pContext->open( BSONObj(), -1, 0 ) ; } else { rc = pContext->open( boOrderBy, pQuery->numToReturn, pQuery->numToSkip ) ; } PD_RC_CHECK( rc, PDERROR, "Open context failed(rc=%d)", rc ) ; pQuery->header.routeID.value = 0; pQuery->header.TID = cb->getTID(); if ( pQuery->numToReturn > 0 && pQuery->numToSkip > 0 ) { pQuery->numToReturn += pQuery->numToSkip ; } pQuery->numToSkip = 0 ; retry: rc = rtnCoordGetCataInfo( cb, pCollectionName, isNeedRefresh, cataInfo ) ; PD_RC_CHECK( rc, PDERROR, "Failed to get the catalog info(collection:%s)", pCollectionName ) ; pQuery->version = cataInfo->getVersion() ; if ( cataInfo->isMainCL() ) { CoordSubCLlist subCLList ; CoordGroupSubCLMap groupSubCLMap ; rc = cataInfo->getMatchSubCLs( boQuery, subCLList ) ; PD_CHECK( SDB_OK == rc, rc, retry_check, PDWARNING, "Failed to get match sub collection(rc=%d)", rc ) ; rc = rtnCoordGetSubCLsByGroups( subCLList, sendGroupList, cb, groupSubCLMap ) ; PD_CHECK( SDB_OK == rc, rc, retry_check, PDWARNING, "Failed to get sub-collection info(rc=%d)", rc ); rc = queryOnMainCL( groupSubCLMap, pQuery, cb, pRouteAgent, sendGroupList, pContext ) ; PD_CHECK( SDB_OK == rc, rc, retry_check, PDWARNING, "Query on main collection failed(rc=%d)", rc ); } else { CoordGroupList groupList ; rc = getNodeGroups( cataInfo, boQuery, sendGroupList, groupList ) ; PD_CHECK( SDB_OK == rc, rc, retry_check, PDWARNING, "Failed to get match sharding(rc=%d)", rc ) ; rc = queryToDataNodeGroup( pSrc, groupList, sendGroupList, pRouteAgent, cb, pContext ) ; PD_CHECK( SDB_OK == rc, rc, retry_check, PDWARNING, "Query on data node failed(rc=%d)", rc ) ; } retry_check: if ( rc != SDB_OK ) { if ( rc != SDB_APP_INTERRUPT && !hasRetry ) { hasRetry = TRUE ; isNeedRefresh = TRUE ; goto retry ; } goto error ; } done: return rc ; error: if ( SDB_CAT_NO_MATCH_CATALOG == rc ) { rc = SDB_OK ; goto done ; } if ( contextID >= 0 ) { pRtncb->contextDelete( contextID, cb ) ; contextID = -1 ; pContext = NULL ; } goto done ; }