summaryrefslogtreecommitdiff
path: root/jstests/sharding/collstats_returns_orphan_count.js
blob: d3f56be4677c99851fab28fb23a8b16d808b5917 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
 * Test that collstats returns the correct number of orphaned documents.
 *
 * @tags: [
 *  requires_fcv_60,
 * ]
 */

(function() {
'use strict';

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

const rangeDeleterBatchSize = 128;

const st = new ShardingTest({
    shards: 2,
    other: {
        shardOptions: {setParameter: {rangeDeleterBatchSize: rangeDeleterBatchSize}},
    }
});

function assertCollStatsHasCorrectOrphanCount(coll, shardName, numOrphans) {
    const pipeline = [
        {'$collStats': {'storageStats': {}}},
        {'$project': {'shard': true, 'storageStats': {'numOrphanDocs': true}}}
    ];
    const storageStats = coll.aggregate(pipeline).toArray();
    storageStats.forEach((stat) => {
        if (stat['shard'] === shardName) {
            assert.eq(stat.storageStats.numOrphanDocs, numOrphans);
        }
    });
}

// Setup database
const dbName = 'db';
const db = st.getDB(dbName);
assert.commandWorked(
    st.s.adminCommand({enableSharding: dbName, primaryShard: st.shard0.shardName}));

// Test non-existing collection
const noColl = db['unusedColl'];
let res = db.runCommand({'collStats': noColl.getFullName()});
assert.eq(res.shards[st.shard0.shardName].numOrphanDocs, 0);

// Setup collection for test with orphans
const coll = db['test'];
const nss = coll.getFullName();
assert.commandWorked(st.s.adminCommand({shardCollection: nss, key: {_id: 1}}));

// Create two chunks
assert.commandWorked(st.s.adminCommand({split: nss, middle: {_id: 0}}));

// Insert 1000 docs into the chunk we will move.
const numDocs = 1000;
let bulk = coll.initializeUnorderedBulkOp();
for (let i = 0; i < numDocs; i++) {
    bulk.insert({_id: i});
}

// Insert 10 docs into the chunk we will not move.
const numDocsUnmoved = 10;
for (let i = -numDocsUnmoved; i < 0; i++) {
    bulk.insert({_id: i});
}
assert.commandWorked(bulk.execute());

// Check there are no orphans before the chunk is moved
assertCollStatsHasCorrectOrphanCount(coll, st.shard1.shardName, 0);

// Pause before first range deletion task
let beforeDeletionFailpoint = configureFailPoint(st.shard0, "hangBeforeDoingDeletion");
let afterDeletionFailpoint = configureFailPoint(st.shard0, "hangAfterDoingDeletion");
assert.commandWorked(db.adminCommand({moveChunk: nss, find: {_id: 0}, to: st.shard1.shardName}));

// Check the batches are deleted correctly
const numBatches = numDocs / rangeDeleterBatchSize;
for (let i = 0; i < numBatches; i++) {
    // Wait for failpoint and check num orphans
    beforeDeletionFailpoint.wait();
    assertCollStatsHasCorrectOrphanCount(
        coll, st.shard0.shardName, numDocs - rangeDeleterBatchSize * i);
    // Unset and reset failpoint without allowing any batches deleted in the meantime
    afterDeletionFailpoint = configureFailPoint(st.shard0, "hangAfterDoingDeletion");
    beforeDeletionFailpoint.off();
    afterDeletionFailpoint.wait();
    beforeDeletionFailpoint = configureFailPoint(st.shard0, "hangBeforeDoingDeletion");
    afterDeletionFailpoint.off();
}
beforeDeletionFailpoint.off();

st.stop();
})();