Exemple #1
0
 // PD_TRACE_DECLARE_FUNCTION ( SDB__DMSROUNIT__ALCEXT, "_dmsReorgUnit::_allocateExtent" )
 INT32 _dmsReorgUnit::_allocateExtent ( INT32 requestSize )
 {
    INT32 rc = SDB_OK ;
    PD_TRACE_ENTRY ( SDB__DMSROUNIT__ALCEXT );
    SDB_ASSERT ( !_pCurrentExtent, "current extent must be NULL" ) ;
    if ( requestSize < DMS_MIN_EXTENT_SZ(_pageSize) )
       requestSize = DMS_MIN_EXTENT_SZ(_pageSize) ;
    else if ( requestSize > DMS_MAX_EXTENT_SZ )
       requestSize = DMS_MAX_EXTENT_SZ ;
    else
       requestSize = ossRoundUpToMultipleX ( requestSize, _pageSize ) ;
    _pCurrentExtent = (CHAR*)SDB_OSS_MALLOC ( requestSize ) ;
    if ( !_pCurrentExtent )
    {
       PD_LOG ( PDERROR, "Unable to allocate %d bytes memory", requestSize ) ;
       rc = SDB_OOM ;
       goto error ;
    }
    _currentExtentSize = requestSize ;
    _initExtentHeader ( (dmsExtent*)_pCurrentExtent,
                        _currentExtentSize/_pageSize ) ;
 done :
    PD_TRACE_EXITRC ( SDB__DMSROUNIT__ALCEXT, rc );
    return rc ;
 error :
    goto done ;
 }
Exemple #2
0
INT32 _msgBuffer::write( const bson::BSONObj &obj, BOOLEAN align, INT32 bytes )
{
   INT32 rc   = SDB_OK ;
   INT32 size = 0 ;        // new size to realloc
   INT32 num  = 0 ;        // number of memory block
   UINT32 objsize = obj.objsize() ;
   if( objsize > _capacity - _size )
   {
      num = ( objsize + _size ) / MEMERY_BLOCK_SIZE + 1 ;
      size = num * MEMERY_BLOCK_SIZE ;

      rc = realloc( _data, size ) ;
      if( SDB_OK != rc )
      {
         goto error ;
      }
   }

   ossMemcpy( _data + _size, obj.objdata(), objsize ) ;
   if ( align )
   {
      _size += ossRoundUpToMultipleX( objsize, bytes ) ;
   }
   else
   {
      _size += objsize ;
   }

done:
   return rc ;
error:
   goto done ;
}
Exemple #3
0
 // PD_TRACE_DECLARE_FUNCTION ( SDB__DMSROUNIT_VLDHDBUFF, "_dmsReorgUnit::validateHeadBuffer" )
 INT32 _dmsReorgUnit::validateHeadBuffer ( CHAR *pBuffer )
 {
    INT32 rc = SDB_OK ;
    PD_TRACE_ENTRY ( SDB__DMSROUNIT_VLDHDBUFF );
    SDB_ASSERT ( pBuffer, "pBuffer can't be NULL" ) ;
    class _reorgUnitHead *unitHead = (class _reorgUnitHead *)pBuffer ;
    if ( ossMemcmp ( pBuffer, DMS_REORG_UNIT_EYECATCHER,
                     DMS_REORG_UNIT_EYECATCHER_LEN ) )
    {
       PD_LOG ( PDWARNING, "reorg file header is invalid" ) ;
       rc = SDB_DMS_INVALID_REORG_FILE ;
       goto error ;
    }
    if ( ossRoundUpToMultipleX ( unitHead->_headerSize,
                                 DMS_REORG_UNIT_HEAD_SIZE_UNIT ) !=
         unitHead->_headerSize )
    {
       PD_LOG ( PDWARNING, "reorg file header size is not valid: %d",
                unitHead->_headerSize ) ;
       rc = SDB_DMS_INVALID_REORG_FILE ;
       goto error ;
    }
 done :
    PD_TRACE_EXITRC ( SDB__DMSROUNIT_VLDHDBUFF, rc );
    return rc ;
 error :
    goto done ;
 }
Exemple #4
0
   INT32 _pmdEDUCB::allocBuff( INT32 len, CHAR **ppBuff, INT32 &buffLen )
   {
      INT32 rc = SDB_OK ;

      if ( _totalCatchSize >= len && _allocFromCatch( len, ppBuff, buffLen ) )
      {
         goto done ;
      }

      len = ossRoundUpToMultipleX( len, EDU_MEM_ALIGMENT_SIZE ) ;
      *ppBuff = ( CHAR* )SDB_OSS_MALLOC( len ) ;
      if( !*ppBuff )
      {
         rc = SDB_OOM ;
         PD_LOG( PDERROR, "Edu[%s] malloc memory[size: %d] failed",
                 toString().c_str(), len ) ;
         goto error ;
      }
      buffLen = len ;

      _totalMemSize += buffLen ;
      _allocMap[ *ppBuff ] = buffLen ;

   done:
      return rc ;
   error:
      goto done ;
   }
   // PD_TRACE_DECLARE_FUNCTION ( SDB_RTNCOINS_SHARDDBGROUP, "rtnCoordInsert::shardDataByGroup" )
   INT32 rtnCoordInsert::shardDataByGroup( const CoordCataInfoPtr &cataInfo,
                                           INT32 count,
                                           CHAR *pInsertor,
                                           GroupObjsMap &groupObjsMap )
   {
      INT32 rc = SDB_OK;
      PD_TRACE_ENTRY ( SDB_RTNCOINS_SHARDDBGROUP ) ;
      while ( count > 0 )
      {
         rc = shardAnObj( pInsertor, cataInfo, groupObjsMap );
         PD_RC_CHECK( rc, PDERROR, "Failed to shard the obj(rc=%d)", rc );

         BSONObj boInsertor ;
         try
         {
            boInsertor = BSONObj( pInsertor );
         }
         catch ( std::exception &e )
         {
            PD_CHECK( FALSE, SDB_INVALIDARG, error, PDERROR,
                      "Failed to parse the insert-obj:%s", e.what() ) ;
         }
         --count ;
         pInsertor += ossRoundUpToMultipleX( boInsertor.objsize(), 4 ) ;
      }

   done:
      PD_TRACE_EXITRC ( SDB_RTNCOINS_SHARDDBGROUP, rc ) ;
      return rc;
   error:
      groupObjsMap.clear() ;
      goto done ;
   }
   INT32 rtnCoordInsert::shardDataByGroup( const CoordCataInfoPtr &cataInfo,
                                           INT32 count,
                                           CHAR *pInsertor,
                                           pmdEDUCB *cb,
                                           GroupSubCLMap &groupSubCLMap )
   {
      INT32 rc = SDB_OK;
      std::string subCLName ;

      while ( count > 0 )
      {
         rc = shardAnObj( pInsertor, cataInfo, cb, groupSubCLMap );
         PD_RC_CHECK( rc, PDERROR, "Failed to shard the obj(rc=%d)", rc );

         try
         {
            BSONObj boInsertor( pInsertor );
            pInsertor += ossRoundUpToMultipleX( boInsertor.objsize(), 4 ) ;
            --count ;
         }
         catch ( std::exception &e )
         {
            PD_CHECK( FALSE, SDB_INVALIDARG, error, PDERROR,
                      "Failed to parse the insert-obj, "
                      "occur unexpected error:%s", e.what() ) ;
         }
      }

   done:
      return rc;
   error:
      groupSubCLMap.clear() ;
      goto done ;
   }
   // PD_TRACE_DECLARE_FUNCTION ( SDB__DMSSTORAGELOADEXT__ALLOCEXTENT, "dmsStorageLoadOp::_allocateExtent" )
   INT32 dmsStorageLoadOp::_allocateExtent ( INT32 requestSize )
   {
      INT32 rc = SDB_OK ;
      PD_TRACE_ENTRY ( SDB__DMSSTORAGELOADEXT__ALLOCEXTENT );
      if ( requestSize < DMS_MIN_EXTENT_SZ(_pageSize) )
      {
         requestSize = DMS_MIN_EXTENT_SZ(_pageSize) ;
      }
      else if ( requestSize > DMS_MAX_EXTENT_SZ )
      {
         requestSize = DMS_MAX_EXTENT_SZ ;
      }
      else
      {
         requestSize = ossRoundUpToMultipleX ( requestSize, _pageSize ) ;
      }

      if ( !_pCurrentExtent )
      {
         _pCurrentExtent = (CHAR*)SDB_OSS_MALLOC ( requestSize ) ;
         if ( !_pCurrentExtent )
         {
            PD_LOG ( PDERROR, "Unable to allocate %d bytes memory",
                     requestSize ) ;
            rc = SDB_OOM ;
            goto error ;
         }
         _currentExtentSize = requestSize ;
         _initExtentHeader ( (dmsExtent*)_pCurrentExtent,
                             _currentExtentSize/_pageSize ) ;
      }
      else
      {
         if ( requestSize > _currentExtentSize )
         {
            CHAR *pOldPtr = _pCurrentExtent ;
            _pCurrentExtent = (CHAR*)SDB_OSS_REALLOC ( _pCurrentExtent,
                                                       requestSize ) ;
            if ( !_pCurrentExtent )
            {
               PD_LOG ( PDERROR, "Unable to realloc %d bytes memory",
                        requestSize ) ;
               _pCurrentExtent = pOldPtr ;
               rc = SDB_OOM ;
               goto error ;
            }
            _currentExtentSize = requestSize ;
         }
         _initExtentHeader ( (dmsExtent*)_pCurrentExtent,
                             _currentExtentSize/_pageSize ) ;
      }

   done :
      PD_TRACE_EXITRC ( SDB__DMSSTORAGELOADEXT__ALLOCEXTENT, rc );
      return rc ;
   error :
      goto done ;
   }
   INT32 rtnCoordInsert::buildInsertMsg( CHAR *pHeadRemain,
                                         INT32 remainLen,
                                         const GroupObjsMap &groupObjsMap,
                                         void *pFiller,
                                         GroupInsertMsgMap &groupMsgMap )
   {
      INT32 rc = SDB_OK;
      GroupObjsMap::const_iterator iterGroup = groupObjsMap.begin();
      while( iterGroup != groupObjsMap.end() )
      {
         UINT32 i = 0;
         UINT32 groupID = iterGroup->first;
         groupMsgMap[groupID].dataList.clear();
         groupMsgMap[groupID].dataLen = 0;
         netIOV ioHead;
         ioHead.iovBase = (void *)pHeadRemain;
         ioHead.iovLen = remainLen;
         groupMsgMap[groupID].dataList.push_back( ioHead );
         for( ; i < iterGroup->second.size(); i++ )
         {
            try
            {
               BSONObj boInsertor( (iterGroup->second)[i] );
               netIOV ioObj;
               ioObj.iovBase = (iterGroup->second)[i] ;
               ioObj.iovLen = boInsertor.objsize();
               groupMsgMap[groupID].dataList.push_back( ioObj );

               SINT32 usedLen = ossRoundUpToMultipleX( boInsertor.objsize(),
                                                       4 );
               SINT32 fillLen = usedLen - boInsertor.objsize();
               if ( fillLen > 0 )
               {
                  netIOV ioFiller;
                  ioFiller.iovBase = pFiller;
                  ioFiller.iovLen = fillLen;
                  groupMsgMap[groupID].dataList.push_back( ioFiller );
               }
               groupMsgMap[groupID].dataLen += usedLen;
            }
            catch( std::exception &e )
            {
               PD_CHECK( FALSE, SDB_INVALIDARG, error, PDERROR,
                         "Failed to build insert-message, "
                         "occur unexpected error:%s", e.what() );
            }
         }
         ++iterGroup ;
      }

   done:
      return rc;
   error:
      groupMsgMap.clear() ;
      goto done;
   }
Exemple #9
0
   INT32 _pmdEDUCB::reallocBuff( INT32 len, CHAR **ppBuff, INT32 &buffLen )
   {
      INT32 rc = SDB_OK ;
      CHAR *pOld = *ppBuff ;
      INT32 oldLen = buffLen ;

      ALLOC_MAP_IT itAlloc = _allocMap.find( *ppBuff ) ;
      if ( itAlloc != _allocMap.end() )
      {
         buffLen = itAlloc->second ;
         oldLen = buffLen ;
      }
      else if ( *ppBuff != NULL || buffLen != 0 )
      {
         PD_LOG( PDERROR, "EDU[%s] realloc input buffer error",
                 toString().c_str() ) ;
         rc = SDB_SYS ;
         goto error ;
      }

      if ( buffLen >= len )
      {
         goto done ;
      }
      len = ossRoundUpToMultipleX( len, EDU_MEM_ALIGMENT_SIZE ) ;
      *ppBuff = ( CHAR* )SDB_OSS_REALLOC( *ppBuff, len ) ;
      if ( !*ppBuff )
      {
         PD_LOG( PDERROR, "Failed to realloc memory, size: %d", len ) ;
         goto error ;
      }

      buffLen = len ;

      _totalMemSize += ( len - oldLen ) ;

      _allocMap[ *ppBuff ] = buffLen ;

   done:
      return rc ;
   error:
      if ( pOld )
      {
         releaseBuff( pOld ) ;
         *ppBuff = NULL ;
         buffLen = 0 ;
      }
      goto done ;
   }
   // PD_TRACE_DECLARE_FUNCTION ( SDB__DMSSTORAGELOADEXT__ALLOCEXTENT, "dmsStorageLoadOp::_allocateExtent" )
   INT32 dmsStorageLoadOp::_allocateExtent ( INT32 requestSize )
   {
      INT32 rc = SDB_OK ;
      PD_TRACE_ENTRY ( SDB__DMSSTORAGELOADEXT__ALLOCEXTENT );
      if ( requestSize < DMS_MIN_EXTENT_SZ(_pageSize) )
      {
         requestSize = DMS_MIN_EXTENT_SZ(_pageSize) ;
      }
      else if ( requestSize > DMS_MAX_EXTENT_SZ )
      {
         requestSize = DMS_MAX_EXTENT_SZ ;
      }
      else
      {
         requestSize = ossRoundUpToMultipleX ( requestSize, _pageSize ) ;
      }

      if ( (UINT32)requestSize > _buffSize && _pCurrentExtent )
      {
         SDB_OSS_FREE( _pCurrentExtent ) ;
         _pCurrentExtent = NULL ;
         _buffSize = 0 ;
      }

      if ( (UINT32)requestSize > _buffSize )
      {
         _pCurrentExtent = ( CHAR* )SDB_OSS_MALLOC( requestSize << 1 ) ;
         if ( !_pCurrentExtent )
         {
            PD_LOG( PDERROR, "Alloc memroy[%d] failed",
                    requestSize << 1 ) ;
            rc = SDB_OOM ;
            goto error ;
         }
         _buffSize = ( requestSize << 1 ) ;
      }

      _currentExtentSize = requestSize ;
      _initExtentHeader ( (dmsExtent*)_pCurrentExtent,
                          _currentExtentSize/_pageSize ) ;

   done :
      PD_TRACE_EXITRC ( SDB__DMSSTORAGELOADEXT__ALLOCEXTENT, rc );
      return rc ;
   error :
      goto done ;
   }
Exemple #11
0
   // PD_TRACE_DECLARE_FUNCTION ( SDB_RTNCOINS_SENDTOGROUPS, "sendToGroups" )
   INT32 rtnCoordInsert::sendToGroups( const GroupInsertMsgMap &groupMsgMap,
                                       MsgOpInsert *pSrcMsg,
                                       netMultiRouteAgent *pRouteAgent,
                                       pmdEDUCB *cb,
                                       REQUESTID_MAP &sendNodes )
   {
      INT32 rc = SDB_OK;
      PD_TRACE_ENTRY ( SDB_RTNCOINS_SENDTOGROUPS ) ;
      SDB_ASSERT( pSrcMsg != NULL, "pSrcMsg can't be null!" );

      GroupInsertMsgMap::const_iterator iterMap = groupMsgMap.begin();
      INT32 headLen = ossRoundUpToMultipleX( offsetof(MsgOpInsert, name) +
                                             ossStrlen ( pSrcMsg->name ) + 1,
                                             4 ) ;
      while( iterMap != groupMsgMap.end() )
      {
         BSONObj boInsertor;
         CoordGroupList groupLst;
         groupLst[iterMap->first] = iterMap->first;
         if ( iterMap->second.dataList.size() == 0 )
         {
            ++iterMap ;
            continue ;
         }

         pSrcMsg->header.messageLength = headLen + iterMap->second.dataLen ;
         pSrcMsg->header.routeID.value = 0;
         rc = rtnCoordSendRequestToNodeGroups( (MsgHeader *)pSrcMsg,
                                               groupLst, TRUE, pRouteAgent,
                                               cb, iterMap->second.dataList,
                                               sendNodes ) ;
         PD_RC_CHECK( rc, PDERROR, "Failed to send the insert request to "
                      "group(goupID=%u)", iterMap->first ) ;
         ++iterMap ;
      }

   done:
      PD_TRACE_EXITRC ( SDB_RTNCOINS_SENDTOGROUPS, rc ) ;
      return rc ;
   error:
      goto done ;
   }
Exemple #12
0
void _mongoSession::_handleResponse( const INT32 opType,
                                     engine::rtnContextBuf &buff )
{
   if ( SDB_AUTH_AUTHORITY_FORBIDDEN == _replyHeader.flags )
   {
      bson::BSONObjBuilder bob ;
      bson::BSONObj obj( buff.data() ) ;
      bob.append( "ok", obj.getIntField( "ok" ) ) ;
      bob.append( "$err", obj.getStringField( "err" ) ) ;
      bob.append( "code", obj.getIntField( "code" ) ) ;
      buff = engine::rtnContextBuf( bob.obj() ) ;
   }

   if ( OP_CMD_COUNT_MORE == opType )
   {
      bson::BSONObjBuilder bob ;
      bson::BSONObj obj( buff.data() ) ;
      bob.append( "n", obj.getIntField( "Total" ) ) ;
      buff = engine::rtnContextBuf( bob.obj() ) ;
      _replyHeader.contextID = -1 ;
      _replyHeader.startFrom = 0 ;
   }

   if ( OP_CMD_GET_CLS == opType &&
        SDB_DMS_EOC != _replyHeader.flags &&
        _replyHeader.numReturned > 0 )
   {
      INT32 offset = 0 ;
      INT32 len = buff.size() ;
      const CHAR *pBody = buff.data() ;
      INT32 recordNum = buff.recordNum() ;
      bson::BSONObj obj, tmp ;
      _tmpBuffer.zero() ;
      while ( offset < len )
      {
         tmp.init( pBody + offset ) ;
         obj = BSON( "name" << tmp.getStringField( "Name" ) ) ;
         _tmpBuffer.write( obj.objdata(), obj.objsize(), TRUE ) ;
         offset += ossRoundUpToMultipleX( tmp.objsize(), 4 ) ;
      }
      const CHAR *pBuffer = _tmpBuffer.data() ;
      INT32 size = _tmpBuffer.size() ;
      engine::rtnContextBuf ctx( pBuffer, size, recordNum ) ;
      buff = ctx ;
   }

   if ( OP_CMD_GET_INDEX == opType )
   {
      INT32 rNum = buff.recordNum() ;
      INT32 len = buff.size() ;
      const CHAR *pBody = buff.data() ;
      INT32 offset = 0 ;
      bson::BSONObj obj, tmp, indexObj, value ;
      _tmpBuffer.zero() ;
      while ( offset < len )
      {
         tmp.init( pBody + offset ) ;
         obj = tmp.getObjectField( "IndexDef" ) ;
         _tmpBuffer.write( obj.objdata(), obj.objsize(), TRUE ) ;
         offset += ossRoundUpToMultipleX( tmp.objsize(), 4 ) ;
      }
      const CHAR *pBuffer = _tmpBuffer.data() ;
      INT32 size = _tmpBuffer.size() ;
      engine::rtnContextBuf ctx( pBuffer, size, rNum ) ;
      buff = ctx ;
   }

   if ( SDB_DMS_EOC == _replyHeader.flags )
   {
      buff = engine::rtnContextBuf() ;
      _replyHeader.numReturned = 0 ;
      _replyHeader.startFrom = _cursorStartFrom.startFrom ;
   }
}
Exemple #13
0
INT32 _mongoSession::_reply( MsgOpReply *replyHeader,
                             const CHAR *pBody,
                             const INT32 len )
{
   INT32 rc         = SDB_OK ;
   INT32 offset     = 0 ;
   mongoMsgReply reply ;
   bson::BSONObjBuilder bob ;
   bson::BSONObj bsonBody ;
   bson::BSONObj objToSend ;
   mongoDataPacket &packet = _converter.getParser().dataPacket() ;

   if ( OP_KILLCURSORS == _converter.getOpType() ||
        dbInsert == packet.opCode ||
        dbUpdate == packet.opCode ||
        dbDelete == packet.opCode )
   {
      goto done;
   }
   reply.header.requestId = 0 ;//replyHeader->header.requestID ;
   reply.header.responseTo = packet.requestId ;
   reply.header.opCode = dbReply ;
   reply.header.flags = 0 ;
   reply.header.version = 0 ;
   reply.header.reservedFlags = 0 ;
   if ( SDB_AUTH_AUTHORITY_FORBIDDEN == replyHeader->flags )
   {
      reply.header.reservedFlags |= 2 ;
   }
   if ( -1 != replyHeader->contextID )
   {
      _cursorStartFrom.cursorId = reply.cursorId ;
      reply.startingFrom = replyHeader->startFrom ;
      _cursorStartFrom.startFrom = reply.startingFrom
                                   + replyHeader->numReturned ;
   }
   else
   {
      if ( replyHeader->contextID == _cursorStartFrom.cursorId )
      {
         reply.startingFrom = _cursorStartFrom.startFrom ;
      }
      else
      {
         reply.startingFrom = 0;
      }
      _cursorStartFrom.cursorId = 0 ;
      _cursorStartFrom.startFrom = 0 ;
   }
   if ( SDB_OK != replyHeader->flags )
   {
      reply.cursorId = 0 ;
   }
   else
   {
      reply.cursorId = replyHeader->contextID + 1 ;
   }

   if ( packet.with( OPTION_CMD ) &&
        OP_GETMORE != _converter.getOpType() )
   {
      reply.nReturned = ( replyHeader->numReturned > 0 ?
                          replyHeader->numReturned : 1 ) ;
   }
   else
   {
      reply.nReturned = replyHeader->numReturned ;
   }

   if ( reply.nReturned > 1 )
   {
      while ( offset < len )
      {
         bsonBody.init( pBody + offset ) ;
         _outBuffer.write( bsonBody.objdata(), bsonBody.objsize() ) ;
         offset += ossRoundUpToMultipleX( bsonBody.objsize(), 4 ) ;
      }
   }
   else
   {
      if ( pBody )
      {
         if ( 0 == reply.cursorId &&
             ( SDB_OK == _replyHeader.flags &&
               OP_QUERY != _converter.getOpType() ) )
         {
            bsonBody.init( pBody ) ;
            if ( !bsonBody.hasField( "ok" ) )
            {
               bob.append( "ok", 0 == replyHeader->flags ? TRUE : FALSE ) ;
               bob.append( "code", replyHeader->flags ) ;
               bob.append( "err", _errorInfo.getStringField( OP_ERRDESP_FIELD) ) ;
               bob.appendElements( bsonBody ) ;
               objToSend = bob.obj() ;
               _outBuffer.write( objToSend ) ;
            }
            else
            {
               _outBuffer.write( bsonBody ) ;
            }
         }
         else
         {
            bsonBody.init( pBody ) ;
            _outBuffer.write( bsonBody ) ;
         }
      }
      else
      {
         if ( OP_GETMORE != _converter.getOpType() &&
              OP_CMD_GET_INDEX == _converter.getOpType()  )
         {
            bob.append( "ok", 1.0 ) ;
            objToSend = bob.obj() ;
            _outBuffer.write( objToSend ) ;
         }
      }
   }

   if ( !_outBuffer.empty() )
   {
      pBody = _outBuffer.data() ;
   }
   reply.header.msgLen = sizeof( mongoMsgReply ) + _outBuffer.size() ;

   rc = sendData( (CHAR *)&reply, sizeof( mongoMsgReply ) ) ;
   if ( rc )
   {
      PD_LOG( PDERROR, "Session[%s] failed to send response header, rc: %d",
              sessionName(), rc ) ;
      goto error ;
   }

   if ( pBody )
   {
      rc = sendData( pBody, reply.header.msgLen - sizeof( mongoMsgReply ) ) ;
      if ( rc )
      {
         PD_LOG( PDERROR, "Session[%s] failed to send response body, rc: %d",
                          sessionName(), rc ) ;
         goto error ;
      }
   }

done:
   return rc ;
error:
   goto done ;
}
   INT32 _coordDeleteOperator::_prepareMainCLOp( coordCataSel &cataSel,
                                                 coordSendMsgIn &inMsg,
                                                 coordSendOptions &options,
                                                 pmdEDUCB *cb,
                                                 coordProcessResult &result )
   {
      INT32 rc                = SDB_OK ;
      MsgOpDelete *pDelMsg    = ( MsgOpDelete* )inMsg.msg() ;

      INT32 flag              = 0 ;
      CHAR *pCollectionName   = NULL;
      CHAR *pDeletor          = NULL;
      CHAR *pHint             = NULL;

      CoordGroupSubCLMap &grpSubCl = cataSel.getGroup2SubsMap() ;
      CoordGroupSubCLMap::iterator it ;

      inMsg.data()->clear() ;

      rc = msgExtractDelete( (CHAR*)inMsg.msg(), &flag, &pCollectionName,
                             &pDeletor, &pHint ) ;
      PD_RC_CHECK( rc, PDERROR, "Failed to parse delete request, rc: %d",
                   rc ) ;

      try
      {
         BSONObj boDeletor( pDeletor ) ;
         BSONObj boHint( pHint ) ;
         BSONObj boNew ;

         CHAR *pBuff             = NULL ;
         UINT32 buffLen          = 0 ;
         UINT32 buffPos          = 0 ;

         it = grpSubCl.begin() ;
         while( it != grpSubCl.end() )
         {
            CoordSubCLlist &subCLLst = it->second ;

            netIOVec &iovec = inMsg._datas[ it->first ] ;
            netIOV ioItem ;

            ioItem.iovBase = (const CHAR*)inMsg.msg() + sizeof( MsgHeader ) ;
            ioItem.iovLen = ossRoundUpToMultipleX ( offsetof(MsgOpDelete, name) +
                                                    pDelMsg->nameLength + 1, 4 ) -
                            sizeof( MsgHeader ) ;
            iovec.push_back( ioItem ) ;

            boNew = _buildNewDeletor( boDeletor, subCLLst ) ;
            UINT32 roundLen = ossRoundUpToMultipleX( boNew.objsize(), 4 ) ;
            if ( buffPos + roundLen > buffLen )
            {
               UINT32 alignLen = ossRoundUpToMultipleX( roundLen,
                                                        DMS_PAGE_SIZE4K ) ;
               rc = cb->allocBuff( alignLen, &pBuff, &buffLen ) ;
               PD_RC_CHECK( rc, PDERROR, "Alloc buff[%u] failed, rc: %d",
                            alignLen, rc ) ;
               _vecBlock.push_back( pBuff ) ;
               buffPos = 0 ;
            }
            ossMemcpy( &pBuff[ buffPos ], boNew.objdata(), boNew.objsize() ) ;
            ioItem.iovBase = &pBuff[ buffPos ] ;
            ioItem.iovLen = roundLen ;
            buffPos += roundLen ;
            iovec.push_back( ioItem ) ;

            ioItem.iovBase = boHint.objdata() ;
            ioItem.iovLen = boHint.objsize() ;
            iovec.push_back( ioItem ) ;         

            ++it ;
         }
      }
      catch( std::exception &e )
      {
         PD_LOG( PDERROR, "Parse delete message occur exception: %s",
                 e.what() ) ;
         rc = SDB_SYS ;
         goto error ;
      }

   done:
      return rc ;
   error:
      _clearBlock( cb ) ;
      goto done ;
   }
Exemple #15
0
 // PD_TRACE_DECLARE_FUNCTION ( SDB__DMSROUNIT__INIT, "_dmsReorgUnit::_init" )
 INT32 _dmsReorgUnit::_init ( BOOLEAN createNew )
 {
    INT32 rc = SDB_OK ;
    PD_TRACE_ENTRY ( SDB__DMSROUNIT__INIT );
    class _reorgUnitHead *unitHead = NULL ;
    INT32 bufSize = ossRoundUpToMultipleX (
                          sizeof ( class _reorgUnitHead ),
                          DMS_REORG_UNIT_HEAD_SIZE_UNIT ) ;
    INT32 restSize = bufSize ;
    _headSize = bufSize ;
    CHAR *pBuffer = (CHAR*)SDB_OSS_MALLOC (bufSize) ;
    if ( !pBuffer )
    {
       PD_LOG ( PDERROR, "Failed to allocate %d bytes of memory", bufSize ) ;
       rc = SDB_OOM ;
       goto error ;
    }
    unitHead = (class _reorgUnitHead*)pBuffer ;
    ossMemset ( unitHead, 0, bufSize ) ;
    if ( createNew )
    {
       SINT64 writeSize = 0 ;
       _readOnly = FALSE ;
       ossMemcpy ( unitHead->_eyeCatcher, DMS_REORG_UNIT_EYECATCHER,
                   DMS_REORG_UNIT_EYECATCHER_LEN ) ;
       unitHead->_headerSize = bufSize ;
       ossMemcpy ( unitHead->_fileName, _fileName, OSS_MAX_PATHSIZE ) ;
       unitHead->_pageSize = _pageSize ;
       while ( restSize != 0 )
       {
          rc = ossWrite ( &_file, &pBuffer[bufSize-restSize], restSize,
                          &writeSize ) ;
          if ( rc && SDB_INTERRUPT != rc )
          {
             PD_LOG ( PDERROR, "Failed to write into file: %s, rc = %d",
                      _fileName, rc ) ;
             goto error ;
          }
          restSize -= writeSize ;
          rc = SDB_OK ;
       }
    }
    else
    {
       SINT64 readSize = 0 ;
       _readOnly = TRUE ;
       while ( restSize > 0 )
       {
          rc = ossRead ( &_file, &pBuffer[bufSize-restSize], restSize, &readSize ) ;
          if ( rc && SDB_INTERRUPT != rc )
          {
             PD_LOG ( PDERROR, "Failed to read from file: %s, rc = %d",
                      _fileName, rc ) ;
             goto error ;
          }
          restSize -= readSize ;
          rc = SDB_OK ;
       }
       if ( ossMemcmp ( unitHead->_eyeCatcher, DMS_REORG_UNIT_EYECATCHER,
                        DMS_REORG_UNIT_EYECATCHER_LEN ) ||
            unitHead->_headerSize != bufSize )
       {
          PD_LOG ( PDERROR, "Invalid reorg file is detected" ) ;
          rc = SDB_DMS_INVALID_REORG_FILE ;
          goto error ;
       }
    }
 done :
    if ( pBuffer )
    {
       SDB_OSS_FREE ( pBuffer ) ;
    }
    PD_TRACE_EXITRC ( SDB__DMSROUNIT__INIT, rc );
    return rc ;
 error :
    goto done ;
 }
Exemple #16
0
INT32 _mongoSession::_reply( MsgOpReply *replyHeader,
                             const CHAR *pBody,
                             const INT32 len )
{
   INT32 rc         = SDB_OK ;
   INT32 offset     = 0 ;
   mongoMsgReply reply ;
   bson::BSONObjBuilder bob ;
   bson::BSONObj bsonBody ;

   reply.header.id = 0 ;
   reply.header.responseTo = replyHeader->header.requestID ;
   reply.header.opCode = dbReply ;
   reply.header._flags = 0 ;
   reply.header._version = 0 ;
   reply.header.reservedFlags = 0 ;
   reply.cursorId = ( -1 == replyHeader->contextID ?
                            0 : replyHeader->contextID ) ;
   reply.startingFrom = replyHeader->startFrom ;
   if ( _converter->getParser().withCmd || _needReply )
   {
      reply.nReturned = replyHeader->numReturned > 0 ? replyHeader->numReturned : 1 ;
   }
   else
   {
      reply.nReturned = replyHeader->numReturned ;
   }

   if ( !_converter->getParser().withCmd && reply.nReturned > 0 )
   {
      while ( offset < len )
      {
         bsonBody.init( pBody + offset ) ;
         _outBuffer.write( bsonBody.objdata(), bsonBody.objsize() ) ;
         offset += ossRoundUpToMultipleX( bsonBody.objsize(), 4 ) ;
      }
      pBody = _outBuffer.data() ;
      reply.header.len = sizeof( mongoMsgReply ) + _outBuffer.size() ;
   }
   else
   {
      if ( pBody )
      {
         bsonBody.init( pBody ) ;
         if ( !bsonBody.hasField( "ok" ))
         {
            bob.append( "ok",
                        0 == replyHeader->flags ? TRUE : replyHeader->flags ) ;
            bob.appendElements( bsonBody ) ;
            pBody = bob.done().objdata() ;
            reply.header.len = sizeof( mongoMsgReply ) + bob.done().objsize() ;
         }
         else
         {
            reply.header.len = sizeof( mongoMsgReply ) + len ;
         }
      }
      else
      {
         bob.append( "ok", 1.0 ) ;
         pBody = bob.done().objdata() ;
         reply.header.len = sizeof( mongoMsgReply ) + bob.done().objsize() ;
      }
   }

   rc = sendData( (CHAR *)&reply, sizeof( mongoMsgReply ) ) ;
   if ( rc )
   {
      PD_LOG( PDERROR, "Session[%s] failed to send response header, rc: %d",
              sessionName(), rc ) ;
      goto error ;
   }

   if ( pBody )
   {
      rc = sendData( pBody, reply.header.len - sizeof( mongoMsgReply ) ) ;
      if ( rc )
      {
         PD_LOG( PDERROR, "Session[%s] failed to send response body, rc: %d",
                          sessionName(), rc ) ;
         goto error ;
      }
   }

   bob.decouple() ;

done:
   return rc ;
error:
   goto done ;
}
   INT32 rtnCoordDelete::_prepareMainCLOp( CoordCataInfoPtr &cataInfo,
                                           CoordGroupSubCLMap &grpSubCl,
                                           rtnSendMsgIn &inMsg,
                                           rtnSendOptions &options,
                                           netMultiRouteAgent *pRouteAgent,
                                           pmdEDUCB *cb,
                                           rtnProcessResult &result,
                                           ossValuePtr &outPtr )
   {
      INT32 rc                = SDB_OK ;
      MsgOpDelete *pDelMsg    = ( MsgOpDelete* )inMsg.msg() ;

      INT32 flag              = 0 ;
      CHAR *pCollectionName   = NULL;
      CHAR *pDeletor          = NULL;
      CHAR *pHint             = NULL;
      BSONObj boDeletor ;
      BSONObj boHint ;
      BSONObj boNew ;

      CHAR *pBuff             = NULL ;
      INT32 buffLen           = 0 ;
      INT32 buffPos           = 0 ;
      vector<CHAR*> *pBlock   = NULL ;

      CoordGroupSubCLMap::iterator it ;

      outPtr                  = (ossValuePtr)0 ;
      inMsg.data()->clear() ;

      rc = msgExtractDelete( (CHAR*)inMsg.msg(), &flag, &pCollectionName,
                             &pDeletor, &pHint ) ;
      PD_RC_CHECK( rc, PDERROR, "Failed to parse delete request, rc: %d",
                   rc ) ;

      boDeletor = BSONObj( pDeletor ) ;
      boHint = BSONObj( pHint ) ;

      pBlock = new vector< CHAR* >( 16 ) ;
      if ( !pBlock )
      {
         PD_LOG( PDERROR, "Alloc vector failed" ) ;
         rc = SDB_OOM ;
         goto error ;
      }

      it = grpSubCl.begin() ;
      while( it != grpSubCl.end() )
      {
         CoordSubCLlist &subCLLst = it->second ;

         netIOVec &iovec = inMsg._datas[ it->first ] ;
         netIOV ioItem ;

         // 1. first vec
         ioItem.iovBase = (CHAR*)inMsg.msg() + sizeof( MsgHeader ) ;
         ioItem.iovLen = ossRoundUpToMultipleX ( offsetof(MsgOpDelete, name) +
                                                 pDelMsg->nameLength + 1, 4 ) -
                         sizeof( MsgHeader ) ;
         iovec.push_back( ioItem ) ;

         // 2. new deletor vec
         boNew = _buildNewDeletor( boDeletor, subCLLst ) ;
         // 2.1 add to buff
         INT32 roundLen = ossRoundUpToMultipleX( boNew.objsize(), 4 ) ;
         if ( buffPos + roundLen > buffLen )
         {
            INT32 alignLen = ossRoundUpToMultipleX( roundLen,
                                                    DMS_PAGE_SIZE4K ) ;
            rc = cb->allocBuff( alignLen, &pBuff, buffLen ) ;
            PD_RC_CHECK( rc, PDERROR, "Alloc buff[%d] failed, rc: %d",
                         alignLen, rc ) ;
            pBlock->push_back( pBuff ) ;
            buffPos = 0 ;
         }
         ossMemcpy( &pBuff[ buffPos ], boNew.objdata(), boNew.objsize() ) ;
         ioItem.iovBase = &pBuff[ buffPos ] ;
         ioItem.iovLen = roundLen ;
         buffPos += roundLen ;
         iovec.push_back( ioItem ) ;

         // 3. hinter vec
         ioItem.iovBase = boHint.objdata() ;
         ioItem.iovLen = boHint.objsize() ;
         iovec.push_back( ioItem ) ;         

         ++it ;
      }

      outPtr = ( ossValuePtr )pBlock ;

   done:
      return rc ;
   error:
      if ( pBlock )
      {
         for ( INT32 i = 0 ; i < (INT32)pBlock->size() ; ++i )
         {
            cb->releaseBuff( (*pBlock)[ i ] ) ;
         }
         delete pBlock ;
         pBlock = NULL ;
      }
      goto done ;
   }
Exemple #18
0
   INT32 rtnCoordInsert::buildInsertMsg( CHAR *pHeadRemain,
                                         INT32 remainLen,
                                         const GroupSubCLMap &groupSubCLMap,
                                         std::vector< bson::BSONObj > &subClInfoLst,
                                         void *pFiller,
                                         GroupInsertMsgMap &groupMsgMap )
   {
      INT32 rc = SDB_OK;
      GroupSubCLMap::const_iterator iterGroup = groupSubCLMap.begin();
      while ( iterGroup != groupSubCLMap.end() )
      {
         UINT32 groupID = iterGroup->first;
         groupMsgMap[groupID].dataList.clear();
         groupMsgMap[groupID].dataLen = 0;
         netIOV ioHead;
         ioHead.iovBase = (void *)pHeadRemain;
         ioHead.iovLen = remainLen;
         groupMsgMap[groupID].dataList.push_back( ioHead );
         SubCLObjsMap::const_iterator iterCL = iterGroup->second.begin();
         while ( iterCL != iterGroup->second.end() )
         {
            UINT32 i = 0;
            SINT32 dataLen = 0;
            SINT32 objNum = 0;
            UINT32 curPos = groupMsgMap[groupID].dataList.size();
            netIOV ioCLInfo;
            ioCLInfo.iovBase = NULL;
            ioCLInfo.iovLen = 0;
            groupMsgMap[groupID].dataList.push_back( ioCLInfo );
            netIOV ioCLInfoFiller;
            ioCLInfoFiller.iovBase = NULL;
            ioCLInfoFiller.iovLen = 0;
            groupMsgMap[groupID].dataList.push_back( ioCLInfoFiller );
            try
            {
               for( ; i < iterCL->second.size(); i++ )
               {
                  BSONObj boInsertor( (iterCL->second)[i] );
                  netIOV ioObj;
                  ioObj.iovBase = (iterCL->second)[i] ;
                  ioObj.iovLen = boInsertor.objsize();
                  groupMsgMap[groupID].dataList.push_back( ioObj );

                  SINT32 usedLen = ossRoundUpToMultipleX( boInsertor.objsize(),
                                                          4 );
                  SINT32 fillLen = usedLen - boInsertor.objsize();
                  if ( fillLen > 0 )
                  {
                     netIOV ioFiller;
                     ioFiller.iovBase = pFiller;
                     ioFiller.iovLen = fillLen;
                     groupMsgMap[groupID].dataList.push_back( ioFiller );
                  }
                  dataLen += usedLen ;
                  ++objNum ;
               }
               BSONObjBuilder subCLInfoBuild;
               subCLInfoBuild.append( FIELD_NAME_SUBOBJSNUM, objNum );
               subCLInfoBuild.append( FIELD_NAME_SUBOBJSSIZE, dataLen );
               subCLInfoBuild.append( FIELD_NAME_SUBCLNAME, iterCL->first );
               UINT32 objPos = subClInfoLst.size();
               subClInfoLst.push_back( subCLInfoBuild.obj() );
               groupMsgMap[groupID].dataList[curPos].iovBase
                                    = subClInfoLst[objPos].objdata();
               groupMsgMap[groupID].dataList[curPos].iovLen
                                    = subClInfoLst[objPos].objsize();
               SINT32 clInfoUsedLen = ossRoundUpToMultipleX(
                  subClInfoLst[objPos].objsize(), 4 ) ;
               SINT32 clInfoFillLen = clInfoUsedLen -
                                      subClInfoLst[objPos].objsize();
               if( clInfoFillLen > 0 )
               {
                  groupMsgMap[groupID].dataList[curPos + 1].iovBase = pFiller;
                  groupMsgMap[groupID].dataList[curPos + 1].iovLen = clInfoFillLen;
               }
               else
               {
                  groupMsgMap[groupID].dataList.erase(
                           groupMsgMap[groupID].dataList.begin() + curPos + 1 ) ;
               }
               groupMsgMap[groupID].dataLen += ( dataLen + clInfoUsedLen ) ;
            }
            catch( std::exception &e )
            {
               PD_CHECK( FALSE, SDB_INVALIDARG, error, PDERROR,
                         "Failed to build insert-message, "
                         "occur unexpected error:%s", e.what() );
            }
            ++iterCL ;
         }
         ++iterGroup ;
      }

   done:
      return rc ;
   error:
      goto done ;
   }