void ShardingTestFixture::expectUpdateCollection(const HostAndPort& expectedHost,
                                                 const CollectionType& coll) {
    onCommand([&](const RemoteCommandRequest& request) {
        ASSERT_EQUALS(expectedHost, request.target);
        ASSERT_EQUALS(BSON(rpc::kReplSetMetadataFieldName << 1), request.metadata);
        ASSERT_EQUALS("config", request.dbname);

        BatchedUpdateRequest actualBatchedUpdate;
        std::string errmsg;
        ASSERT_TRUE(actualBatchedUpdate.parseBSON(request.dbname, request.cmdObj, &errmsg));
        ASSERT_EQUALS(CollectionType::ConfigNS, actualBatchedUpdate.getNS().ns());
        auto updates = actualBatchedUpdate.getUpdates();
        ASSERT_EQUALS(1U, updates.size());
        auto update = updates.front();

        ASSERT_TRUE(update->getUpsert());
        ASSERT_FALSE(update->getMulti());
        ASSERT_EQUALS(update->getQuery(), BSON(CollectionType::fullNs(coll.getNs().toString())));
        ASSERT_EQUALS(update->getUpdateExpr(), coll.toBSON());

        BatchedCommandResponse response;
        response.setOk(true);
        response.setNModified(1);

        return response.toBSON();
    });
}
Exemple #2
0
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());
}
Exemple #3
0
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());
}
    /**
     * Stores ranges for a particular collection and shard starting from some version
     */
    void storeCollectionRanges(const NamespaceString& nss,
                               const string& shardName,
                               const vector<KeyRange>& ranges,
                               const ChunkVersion& startVersion) {
        // Get key pattern from first range
        ASSERT_GREATER_THAN(ranges.size(), 0u);

        CollectionType coll;
        coll.setNs(nss);
        coll.setKeyPattern(ranges.begin()->keyPattern);
        coll.setEpoch(startVersion.epoch());
        coll.setUpdatedAt(Date_t::fromMillisSinceEpoch(1));
        ASSERT_OK(coll.validate());

        DBDirectClient client(&_txn);

        client.update(CollectionType::ConfigNS,
                      BSON(CollectionType::fullNs(coll.getNs().ns())),
                      coll.toBSON(),
                      true,
                      false);

        ChunkVersion nextVersion = startVersion;
        for (vector<KeyRange>::const_iterator it = ranges.begin(); it != ranges.end(); ++it) {
            ChunkType chunk;
            // TODO: We should not rely on the serialized ns, minkey being unique in the future,
            // causes problems since it links string serialization to correctness.
            chunk.setName(Chunk::genID(nss.ns(), it->minKey));
            chunk.setShard(shardName);
            chunk.setNS(nss.ns());
            chunk.setVersion(nextVersion);
            chunk.setMin(it->minKey);
            chunk.setMax(it->maxKey);
            nextVersion.incMajor();

            client.insert(ChunkType::ConfigNS, chunk.toBSON());
        }
    }