summaryrefslogtreecommitdiff
path: root/jstests/sharding/migration_id_index.js
blob: 3c42a3816f3f365668ba6b154d294b69622587e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// This tests that when a chunk migration occurs, all replica set members of the destination shard
// get the correct _id index version for the collection.
(function() {
"use strict";

load("jstests/libs/index_catalog_helpers.js");

var st = new ShardingTest({shards: 2, rs: {nodes: 2}});
var testDB = st.s.getDB("test");
assert.commandWorked(testDB.adminCommand({enableSharding: testDB.getName()}));
st.ensurePrimaryShard(testDB.getName(), st.shard0.shardName);

// Create a collection with a v:1 _id index.
var coll = testDB.getCollection("migration_id_index");
coll.drop();
assert.commandWorked(
    testDB.createCollection(coll.getName(), {idIndex: {key: {_id: 1}, name: "_id_", v: 1}}));
// We must insert a document into the collection so that subsequent index builds are two-phase.
// If the collection is empty and the build is single-phase, the primary will not wait for the index
// to finish building on the secondary before returning.
assert.commandWorked(testDB.coll.insert({a: 6}));
st.rs0.awaitReplication();
var spec = IndexCatalogHelpers.findByName(
    st.rs0.getPrimary().getDB("test").migration_id_index.getIndexes(), "_id_");
assert.neq(spec, null, "_id index spec not found");
assert.eq(spec.v, 1, tojson(spec));
spec = IndexCatalogHelpers.findByName(
    st.rs0.getSecondary().getDB("test").migration_id_index.getIndexes(), "_id_");
assert.neq(spec, null, "_id index spec not found");
assert.eq(spec.v, 1, tojson(spec));

// Move a chunk to the non-primary shard.
assert.commandWorked(testDB.adminCommand({shardCollection: coll.getFullName(), key: {a: 1}}));
assert.commandWorked(testDB.adminCommand({split: coll.getFullName(), middle: {a: 5}}));
assert.commandWorked(
    testDB.adminCommand({moveChunk: coll.getFullName(), find: {a: 6}, to: st.shard1.shardName}));

// Check that the collection was created with a v:1 _id index on the non-primary shard.
spec = IndexCatalogHelpers.findByName(
    st.rs1.getPrimary().getDB("test").migration_id_index.getIndexes(), "_id_");
assert.neq(spec, null, "_id index spec not found");
assert.eq(spec.v, 1, tojson(spec));
spec = IndexCatalogHelpers.findByName(
    st.rs1.getSecondary().getDB("test").migration_id_index.getIndexes(), "_id_");
assert.neq(spec, null, "_id index spec not found");
assert.eq(spec.v, 1, tojson(spec));

st.stop();
})();