Exemplo n.º 1
0
   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 ;
   }
Exemplo n.º 2
0
   //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 ;
   }
Exemplo n.º 3
0
   // 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;
   }
Exemplo n.º 4
0
   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 ;
   }