ChunkManager::ChunkManager(const CollectionType& coll) : _ns(coll.getNs().ns()), _keyPattern(coll.getKeyPattern()), _unique(coll.getUnique()), _sequenceNumber(NextSequenceNumber.addAndFetch(1)), _chunkRanges() { _version = ChunkVersion::fromBSON(coll.toBSON()); }
ChunkManager::ChunkManager(const CollectionType& coll) : _ns(coll.getNs().ns()), _keyPattern(coll.getKeyPattern()), _unique(coll.getUnique()), _sequenceNumber(NextSequenceNumber.addAndFetch(1)), _chunkRanges() { // coll does not have correct version. Use same initial version as _load and createFirstChunks. _version = ChunkVersion(0, 0, coll.getEpoch()); }
Status MetadataLoader::_initCollection(CatalogManager* catalogManager, const string& ns, const string& shard, CollectionMetadata* metadata) const { auto coll = catalogManager->getCollection(ns); if (!coll.isOK()) { return coll.getStatus(); } CollectionType collInfo = coll.getValue(); if (collInfo.getDropped()) { return Status(ErrorCodes::NamespaceNotFound, str::stream() << "could not load metadata, collection " << ns << " was dropped"); } metadata->_keyPattern = collInfo.getKeyPattern().toBSON(); metadata->fillKeyPatternFields(); metadata->_shardVersion = ChunkVersion(0, 0, collInfo.getEpoch()); metadata->_collVersion = ChunkVersion(0, 0, collInfo.getEpoch()); return Status::OK(); }
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 ); } }
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; }
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 ); } }