示例#1
0
    bool MetadataLoader::initCollection(const string& ns,
                                        const string& shard,
                                        const CollectionManager* oldManager,
                                        CollectionManager* manager,
                                        string* errMsg) {
        //
        // Bring collection entry from the config server.
        //

        BSONObj collObj;
        {
            scoped_ptr<ScopedDbConnection> connPtr;

            try {
                connPtr.reset(
                    ScopedDbConnection::getInternalScopedDbConnection(_configLoc.toString(), 30));
                ScopedDbConnection& conn = *connPtr;

                collObj = conn->findOne(CollectionType::ConfigNS, QUERY(CollectionType::ns()<<ns));
            }
            catch (const DBException& e) {
                *errMsg = str::stream() << "caught exception accessing the config servers "
                                        << causedBy(e);

                // We deliberately do not return connPtr to the pool, since it was involved
                // with the error here.

                return false;
            }

            connPtr->done();
        }

        CollectionType collDoc;
        if (!collDoc.parseBSON(collObj, errMsg) || !collDoc.isValid(errMsg)) {
            return false;
        }

        //
        // Load or generate default chunks for collection config.
        //

        if (!collDoc.getKeyPattern().isEmpty()) {

            manager->_key = collDoc.getKeyPattern();

            if(!initChunks(collDoc, ns, shard, oldManager, manager, errMsg)){
                return false;
            }
        }
        else if(collDoc.getPrimary() == shard) {

            if (shard == "") {
                warning() << "shard not verified, assuming collection "
                          << ns << " is unsharded on this shard" << endl;
            }

            manager->_key = BSONObj();
            manager->_maxShardVersion = ChunkVersion(1, 0, collDoc.getEpoch());
            manager->_maxCollVersion = manager->_maxShardVersion;
        }
        else {
            *errMsg = str::stream() << "collection " << ns << " does not have a shard key "
                                    << "and primary " << collDoc.getPrimary()
                                    << " does not match this shard " << shard;
            return false;
        }

        return true;
    }
示例#2
0
    Status MetadataLoader::initCollection( const string& ns,
                                           const string& shard,
                                           CollectionMetadata* metadata )
    {
        //
        // Bring collection entry from the config server.
        //

        BSONObj collObj;
        {
            try {
                ScopedDbConnection conn( _configLoc.toString(), 30 );
                collObj = conn->findOne( CollectionType::ConfigNS, QUERY(CollectionType::ns()<<ns));
                conn.done();
            }
            catch ( const DBException& e ) {
                string errMsg = str::stream() << "could not query collection metadata"
                                              << causedBy( e );

                // We deliberately do not return conn to the pool, since it was involved
                // with the error here.

                return Status( ErrorCodes::HostUnreachable, errMsg );
            }
        }

        CollectionType collDoc;
        string errMsg;
        if ( !collDoc.parseBSON( collObj, &errMsg ) || !collDoc.isValid( &errMsg ) ) {
            return Status( ErrorCodes::FailedToParse, errMsg );
        }

        //
        // Load or generate default chunks for collection config.
        //

        if ( collDoc.isKeyPatternSet() && !collDoc.getKeyPattern().isEmpty() ) {

            metadata->_keyPattern = collDoc.getKeyPattern();
            metadata->_shardVersion = ChunkVersion( 0, 0, collDoc.getEpoch() );
            metadata->_collVersion = ChunkVersion( 0, 0, collDoc.getEpoch() );

            return Status::OK();
        }
        else if ( collDoc.isPrimarySet() && collDoc.getPrimary() == shard ) {

            if ( shard == "" ) {
                warning() << "shard not verified, assuming collection " << ns
                          << " is unsharded on this shard" << endl;
            }

            metadata->_keyPattern = BSONObj();
            metadata->_shardVersion = ChunkVersion( 1, 0, collDoc.getEpoch() );
            metadata->_collVersion = metadata->_shardVersion;

            return Status::OK();
        }
        else {
            errMsg = str::stream() << "collection " << ns << " does not have a shard key "
                    << "and primary " << ( collDoc.isPrimarySet() ? collDoc.getPrimary() : "" )
                    << " does not match this shard " << shard;
            return Status( ErrorCodes::RemoteChangeDetected, errMsg );
        }
    }
示例#3
0
    Status MetadataLoader::initCollection( const string& ns,
                                           const string& shard,
                                           CollectionMetadata* metadata ) const
    {
        //
        // Bring collection entry from the config server.
        //

        BSONObj collDoc;
        {
            try {
                ScopedDbConnection conn( _configLoc.toString(), 30 );
                collDoc = conn->findOne( CollectionType::ConfigNS, QUERY(CollectionType::ns()<<ns));
                conn.done();
            }
            catch ( const DBException& e ) {
                string errMsg = str::stream() << "could not query collection metadata"
                                              << causedBy( e );

                // We deliberately do not return conn to the pool, since it was involved
                // with the error here.

                return Status( ErrorCodes::HostUnreachable, errMsg );
            }
        }

        string errMsg;
        if ( collDoc.isEmpty() ) {

            errMsg = str::stream() << "could not load metadata, collection " << ns << " not found";
            warning() << errMsg << endl;

            return Status( ErrorCodes::NamespaceNotFound, errMsg );
        }

        CollectionType collInfo;
        if ( !collInfo.parseBSON( collDoc, &errMsg ) || !collInfo.isValid( &errMsg ) ) {

            errMsg = str::stream() << "could not parse metadata for collection " << ns
                                   << causedBy( errMsg );
            warning() << errMsg << endl;

            return Status( ErrorCodes::FailedToParse, errMsg );
        }

        if ( collInfo.isDroppedSet() && collInfo.getDropped() ) {

            errMsg = str::stream() << "could not load metadata, collection " << ns
                                   << " was dropped";
            warning() << errMsg << endl;

            return Status( ErrorCodes::NamespaceNotFound, errMsg );
        }

        if ( collInfo.isKeyPatternSet() && !collInfo.getKeyPattern().isEmpty() ) {

            // Sharded collection, need to load chunks

            metadata->_keyPattern = collInfo.getKeyPattern();
            metadata->_shardVersion = ChunkVersion( 0, 0, collInfo.getEpoch() );
            metadata->_collVersion = ChunkVersion( 0, 0, collInfo.getEpoch() );

            return Status::OK();
        }
        else if ( collInfo.isPrimarySet() && collInfo.getPrimary() == shard ) {

            // A collection with a non-default primary

            // Empty primary field not allowed if set
            dassert( collInfo.getPrimary() != "" );

            metadata->_keyPattern = BSONObj();
            metadata->_shardVersion = ChunkVersion( 1, 0, collInfo.getEpoch() );
            metadata->_collVersion = metadata->_shardVersion;

            return Status::OK();
        }
        else {

            // A collection with a primary that doesn't match this shard or is empty, the primary
            // may have changed before we loaded.

            errMsg = // br
                    str::stream() << "collection " << ns << " does not have a shard key "
                                  << "and primary "
                                  << ( collInfo.isPrimarySet() ? collInfo.getPrimary() : "" )
                                  << " does not match this shard " << shard;

            warning() << errMsg << endl;

            metadata->_collVersion = ChunkVersion( 0, 0, OID() );

            return Status( ErrorCodes::RemoteChangeDetected, errMsg );
        }
    }