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