INT32 _rtnQueryOptions::toQueryMsg ( CHAR **ppMsg,
                                        INT32 &buffSize,
                                        IExecutor *cb ) const
   {
      INT32 rc = SDB_OK ;

      SDB_ASSERT( ppMsg, "ppMsg can't be NULL" ) ;

      rc = msgBuildQueryMsg( ppMsg, &buffSize, _fullName, _flag, 0,
                             _skip, _limit, &_query, &_selector, &_orderBy,
                             &_hint, cb ) ;
      PD_RC_CHECK( rc, PDERROR, "Build query msg failed, rc: %d", rc ) ;

   done:
      return rc ;
   error:
      goto done ;
   }
Beispiel #2
0
   INT32 _qgmPlScan::_executeOnCoord( _pmdEDUCB *eduCB )
   {
      PD_TRACE_ENTRY( SDB__QGMPLSCAN__EXECONCOORD ) ;
      INT32 rc = SDB_OK ;
      INT32 bufSize = 0 ;
      CHAR *qMsg = NULL ;
      MsgOpReply dummyReply ;
      BSONObj *err = NULL ;
      BSONObj selector = _selector.selector() ;

      rc = msgBuildQueryMsg ( &qMsg, &bufSize,
                              _collection.toString().c_str(),0,
                              0, _skip, _return,
                              &_condition, &selector,
                              &_orderby, &_hint ) ;

      if ( SDB_OK != rc )
      {
         goto error ;
      }

      rc = _coordQuery.execute ( qMsg, *(SINT32*)qMsg, eduCB,
                                 dummyReply, NULL ) ;
      SDB_ASSERT( NULL == err, "impossible" ) ;
      PD_RC_CHECK ( rc, PDERROR,
                    "Failed to execute coordQuery, rc = %d", rc ) ;

      _contextID = dummyReply.contextID ;

   done:
      if ( NULL != qMsg )
      {
         SDB_OSS_FREE( qMsg ) ;
         qMsg = NULL ;
      }
      PD_TRACE_EXITRC( SDB__QGMPLSCAN__EXECONCOORD, rc ) ;
      return rc ;
   error:
      goto done ;
   }
Beispiel #3
0
   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 omClusterNotifier::_addUpdateHostReq( pmdRemoteSession *remoteSession )
   {
      INT32 rc    = SDB_OK ;
      BSONArrayBuilder arrayBuilder ;
      BSONArray hosttable ;
      BSONObj request ;
      UINT32 i     = 0 ;
      for ( ; i < _vHostTable.size(); i++ )
      {
         omHostContent &agentInfo = _vHostTable[i] ;
         BSONObj tmp ;
         if ( agentInfo.serviceName 
                              == boost::lexical_cast<string>( SDBCM_DFT_PORT ) )
         {
            tmp = BSON ( OM_BSON_FIELD_HOST_NAME << agentInfo.hostName
                         << OM_BSON_FIELD_HOST_IP << agentInfo.ip ) ;
         }
         else
         {
            tmp = BSON ( OM_BSON_FIELD_HOST_NAME << agentInfo.hostName
                        << OM_BSON_FIELD_HOST_IP << agentInfo.ip
                        << OM_BSON_FIELD_AGENT_PORT << agentInfo.serviceName ) ;
         }

         arrayBuilder.append( tmp ) ;
      }

      hosttable = arrayBuilder.arr() ;

      _MAPAGENT_ITER iter = _mapTargetAgents.begin() ;
      while ( iter != _mapTargetAgents.end() )
      {
         MsgRouteID routeID ;
         pmdSubSession *subSession = NULL ;
         CHAR *pContent            = NULL ;
         INT32 contentSize         = 0 ;
         omHostContent &agentInfo  = iter->second ;
         routeID   = _om->updateAgentInfo( agentInfo.hostName, 
                                           agentInfo.serviceName ) ;
         subSession = remoteSession->addSubSession( routeID.value ) ;
         if ( NULL == subSession )
         {
            rc = SDB_OOM ;
            PD_LOG( PDERROR, "addSubSessin failed" ) ;
            goto error ;
         }

         request = BSON( OM_BSON_FIELD_HOST_NAME << agentInfo.hostName
                         << OM_BSON_FIELD_HOST_IP << agentInfo.ip
                         << OM_BSON_FIELD_HOST_USER << agentInfo.user
                         << OM_BSON_FIELD_HOST_PASSWD << agentInfo.passwd
                         << OM_BSON_FIELD_HOST_INFO << hosttable ) ;
         rc = msgBuildQueryMsg( &pContent, &contentSize, 
                                CMD_ADMIN_PREFIX OM_UPDATE_HOSTNAME_REQ,
                                0, 0, 0, -1, &request, NULL, NULL, NULL ) ;
         if ( SDB_OK != rc )
         {
            PD_LOG( PDERROR, "msgBuildQueryMsg failed:rc=%d", rc ) ;
            goto error ;
         }

         subSession->setReqMsg( (MsgHeader *)pContent ) ;
         iter++ ;
      }
   done:
      return rc ;
   error:
      goto done ;
   }
Beispiel #5
0
   // PD_TRACE_DECLARE_FUNCTION( SDB__QGMPLCOMMAND_EXECONCOORD, "_qgmPlCommand::_executeOnCoord" )
   INT32 _qgmPlCommand::_executeOnCoord( _pmdEDUCB *eduCB )
   {
      PD_TRACE_ENTRY( SDB__QGMPLCOMMAND_EXECONCOORD ) ;
      INT32 rc = SDB_OK ;

      CoordCB *pCoord = pmdGetKRCB()->getCoordCB() ;
      coordCommandFactory *pFactory = coordGetFactory() ;
      coordOperator *pOpr = NULL ;

      CHAR *msg = NULL ;
      INT32 bufSize = 0 ;
      const CHAR *pCommand = NULL ;
      rtnContextBuf buff ;

      if ( SQL_GRAMMAR::CRTCS == _commandType )
      {
         pCommand = CMD_NAME_CREATE_COLLECTIONSPACE ;
         BSONObj obj = BSON( FIELD_NAME_NAME << _fullName.toString() ) ;
         rc = msgBuildQueryMsg( &msg, &bufSize,
                                CMD_ADMIN_PREFIX CMD_NAME_CREATE_COLLECTIONSPACE,
                                0, 0, 0, -1,
                                &obj, NULL, NULL, NULL,
                                eduCB ) ;
      }
      else if ( SQL_GRAMMAR::DROPCS == _commandType )
      {
         pCommand = CMD_NAME_DROP_COLLECTIONSPACE ;
         BSONObj obj = BSON( FIELD_NAME_NAME << _fullName.toString() ) ;
         rc = msgBuildQueryMsg( &msg, &bufSize,
                                CMD_ADMIN_PREFIX CMD_NAME_DROP_COLLECTIONSPACE,
                                0, 0, 0, -1,
                                &obj, NULL, NULL, NULL,
                                eduCB ) ;
      }
      else if ( SQL_GRAMMAR::CRTCL == _commandType )
      {
         pCommand = CMD_NAME_CREATE_COLLECTION ;
         BSONObj obj ;
         if ( _partition.isEmpty() )
         {
            obj = BSON( FIELD_NAME_NAME << _fullName.toString() ) ;
         }
         else
         {
            obj = BSON( FIELD_NAME_NAME << _fullName.toString() <<
                        FIELD_NAME_SHARDINGKEY << _partition ) ;
         }
         rc = msgBuildQueryMsg( &msg, &bufSize,
                                CMD_ADMIN_PREFIX CMD_NAME_CREATE_COLLECTION,
                                0, 0, 0, -1,
                                &obj, NULL, NULL, NULL,
                                eduCB ) ;
      }
      else if ( SQL_GRAMMAR::DROPCL == _commandType )
      {
         pCommand = CMD_NAME_DROP_COLLECTION ;
         BSONObj obj = BSON( FIELD_NAME_NAME << _fullName.toString() ) ;
         rc = msgBuildQueryMsg( &msg, &bufSize,
                                CMD_ADMIN_PREFIX CMD_NAME_DROP_COLLECTION,
                                0, 0, 0, -1,
                                &obj, NULL, NULL, NULL,
                                eduCB ) ;
      }
      else if ( SQL_GRAMMAR::CRTINDEX == _commandType )
      {
         pCommand = CMD_NAME_CREATE_INDEX ;
         BSONObjBuilder builder ;
         qgmOPFieldVec::const_iterator itr = _indexColumns.begin() ;
         for ( ; itr != _indexColumns.end(); itr++ )
         {
            builder.append( itr->value.attr().toString(),
                            SQL_GRAMMAR::ASC == itr->type?
                            1 : -1 ) ;
         }

         BSONObj index ;
         if ( !_uniqIndex )
         {
            index = BSON( IXM_FIELD_NAME_KEY << builder.obj() <<
                          IXM_FIELD_NAME_NAME << _indexName.toString() ) ;
         }
         else
         {
            BSONObjBuilder indexBuilder ;
            indexBuilder.append( IXM_FIELD_NAME_KEY, builder.obj()) ;
            indexBuilder.append( IXM_FIELD_NAME_NAME, _indexName.toString()) ;
            indexBuilder.appendBool( IXM_FIELD_NAME_UNIQUE, TRUE ) ;
            index = indexBuilder.obj() ;
         }

         BSONObj obj = BSON( FIELD_NAME_COLLECTION << _fullName.toString() <<
                             FIELD_NAME_INDEX << index ) ;
         rc = msgBuildQueryMsg( &msg, &bufSize,
                                CMD_ADMIN_PREFIX CMD_NAME_CREATE_INDEX,
                                0, 0, 0, -1,
                                &obj, NULL, NULL, NULL,
                                eduCB ) ;
      }
      else if ( SQL_GRAMMAR::DROPINDEX == _commandType )
      {
         pCommand = CMD_NAME_DROP_INDEX ;
         BSONObj obj = BSON( FIELD_NAME_COLLECTION << _fullName.toString() <<
                             FIELD_NAME_INDEX <<
                                 BSON( IXM_FIELD_NAME_NAME << _indexName.toString() << 
                                       IXM_FIELD_NAME_KEY << "" )
                            ) ;
         rc = msgBuildQueryMsg( &msg, &bufSize,
                                CMD_ADMIN_PREFIX CMD_NAME_DROP_INDEX,
                                0, 0, 0, -1,
                                &obj, NULL, NULL, NULL,
                                eduCB ) ;
      }
      else if ( SQL_GRAMMAR::LISTCS == _commandType )
      {
         pCommand = CMD_NAME_LIST_COLLECTIONSPACES ;
         BSONObj obj ;
         rc = msgBuildQueryMsg( &msg, &bufSize,
                                CMD_ADMIN_PREFIX CMD_NAME_LIST_COLLECTIONSPACES,
                                0, 0, 0, -1,
                                &obj, NULL, NULL, NULL,
                                eduCB ) ;
      }
      else if ( SQL_GRAMMAR::LISTCL == _commandType )
      {
         pCommand = CMD_NAME_LIST_COLLECTIONS ;
         BSONObj obj ;
         rc = msgBuildQueryMsg( &msg, &bufSize,
                                CMD_ADMIN_PREFIX CMD_NAME_LIST_COLLECTIONS,
                                0, 0, 0, -1,
                                &obj, NULL, NULL, NULL,
                                eduCB ) ;
      }
      else if ( SQL_GRAMMAR::BEGINTRAN == _commandType )
      {
         coordTransBegin opr ;
         MsgOpTransBegin transMsg ;
         transMsg.header.messageLength = sizeof( MsgOpTransBegin ) ;
         transMsg.header.opCode = MSG_BS_TRANS_BEGIN_REQ ;
         transMsg.header.TID = 0 ;
         transMsg.header.routeID.value = 0 ;

         rc = opr.init( pCoord->getResource(), eduCB ) ;
         if ( rc )
         {
            PD_LOG( PDERROR, "Init operator[%s] failed, rc: %d",
                    opr.getName(), rc ) ;
            goto error ;
         }
         rc = opr.execute( ( MsgHeader *)&transMsg, eduCB,
                           _contextID, &buff ) ;
         if ( rc )
         {
            PD_LOG( PDERROR, "Execute operator[%s] failed, rc: %d",
                    opr.getName(), rc ) ;
            goto error ;
         }
      }
      else if ( SQL_GRAMMAR::ROLLBACK == _commandType )
      {
         coordTransRollback opr ;
         MsgOpTransRollback transMsg ;
         transMsg.header.messageLength = sizeof( MsgOpTransRollback ) ;
         transMsg.header.opCode = MSG_BS_TRANS_ROLLBACK_REQ ;
         transMsg.header.TID = 0 ;
         transMsg.header.routeID.value = 0 ;

         rc = opr.init( pCoord->getResource(), eduCB ) ;
         if ( rc )
         {
            PD_LOG( PDERROR, "Init operator[%s] failed, rc: %d",
                    opr.getName(), rc ) ;
            goto error ;
         }
         rc = opr.execute( ( MsgHeader *)&transMsg, eduCB,
                           _contextID, &buff ) ;
         if ( rc )
         {
            PD_LOG( PDERROR, "Execute operator[%s] failed, rc: %d",
                    opr.getName(), rc ) ;
            goto error ;
         }
      }
      else if ( SQL_GRAMMAR::COMMIT == _commandType )
      {
         coordTransCommit opr ;
         MsgOpTransCommit transMsg ;
         transMsg.header.messageLength = sizeof( MsgOpTransCommit ) ;
         transMsg.header.opCode = MSG_BS_TRANS_COMMIT_REQ ;
         transMsg.header.TID = 0 ;
         transMsg.header.routeID.value = 0 ;

         rc = opr.init( pCoord->getResource(), eduCB ) ;
         if ( rc )
         {
            PD_LOG( PDERROR, "Init operator[%s] failed, rc: %d",
                    opr.getName(), rc ) ;
            goto error ;
         }
         rc = opr.execute( ( MsgHeader *)&transMsg, eduCB,
                           _contextID, &buff ) ;
         if ( rc )
         {
            PD_LOG( PDERROR, "Execute operator[%s] failed, rc: %d",
                    opr.getName(), rc ) ;
            goto error ;
         }
      }
      else
      {
         PD_LOG( PDERROR, "Invalid command type:%d", _commandType ) ;
         rc = SDB_SYS ;
         goto error ;
      }

      if ( rc )
      {
         PD_LOG( PDERROR, "Build message failed, rc: %d", rc ) ;
         goto error ;
      }

      if ( pCommand )
      {
         rc = pFactory->create( pCommand, pOpr ) ;
         if ( rc )
         {
            PD_LOG( PDERROR, "Create operator by name[%s] failed, rc: %d",
                    pCommand, rc ) ;
            goto error ;
         }
         rc = pOpr->init( pCoord->getResource(), eduCB ) ;
         if ( rc )
         {
            PD_LOG( PDERROR, "Init operator[%s] failed, rc: %d",
                    pOpr->getName(), rc ) ;
            goto error ;
         }
         SDB_ASSERT( msg, "Msg cant' be NULL" ) ;
         if ( !msg )
         {
            rc = SDB_SYS ;
            goto error ;
         }
         rc = pOpr->execute( (MsgHeader*)msg, eduCB, _contextID, &buff ) ;
         if ( rc )
         {
            PD_LOG( PDERROR, "Execute operator[%s] failed, rc: %d",
                    pOpr->getName(), rc ) ;
            goto error ;
         }
      }

   done:
      if ( NULL != msg )
      {
         msgReleaseBuffer( msg, eduCB ) ;
      }
      if ( pOpr )
      {
         pFactory->release( pOpr ) ;
      }
      PD_TRACE_EXITRC( SDB__QGMPLCOMMAND_EXECONCOORD, rc ) ;
      return rc ;
   error:
      goto done ;
   }