summaryrefslogtreecommitdiff
path: root/jstests/sharding/db_and_coll_timestamps_test.js
blob: 1cc42374552b8a6c7b78474dd3057dca812c3636 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
 * This test checks that the operations that should modify the timestamp of a database or a
 * collection actually do it.
 *
 * For databases, every time a DB is dropped and recreated its timestamp should also be updated
 *
 * For collections, this test verifies that the timestamp is properly updated when sharding a
 * collection, dropping and creating a collection, or refining the sharding key.
 */

// Cannot run the filtering metadata check on tests that run refineCollectionShardKey.
TestData.skipCheckShardFilteringMetadata = true;

(function() {
'use strict';

load("jstests/sharding/libs/find_chunks_util.js");

function checkTimestampConsistencyInPersistentMetadata(
    dbName, nss, dbTimestampInConfig, collTimestampInConfig) {
    // Checking consistency on local shard collection: config.cache.database
    st.shard0.adminCommand({_flushDatabaseCacheUpdates: dbName, syncFromConfig: true});
    let dbTimestampInShard =
        st.shard0.getDB('config').cache.databases.findOne({_id: dbName}).version.timestamp;
    assert.neq(null, dbTimestampInShard);
    assert.eq(timestampCmp(dbTimestampInConfig, dbTimestampInShard), 0);

    // Checking consistency on local shard collection: config.cache.collections
    st.shard0.adminCommand({_flushRoutingTableCacheUpdates: nss, syncFromConfig: true});
    let collTimestampInShard =
        st.shard0.getDB('config').cache.collections.findOne({_id: nss}).timestamp;
    assert.neq(null, collTimestampInShard);
    assert.eq(timestampCmp(collTimestampInConfig, collTimestampInShard), 0);
}

const kDbName = 'testdb';
const kCollName = 'coll';
const kNs = kDbName + '.' + kCollName;

var st = new ShardingTest({shards: 1, mongos: 1});

let configDB = st.s.getDB('config');

assert.commandWorked(st.s.adminCommand({enableSharding: kDbName}));

// Create a sharded collection
assert.commandWorked(st.s.adminCommand({shardCollection: kNs, key: {x: 1}}));

// Check that timestamp is created in ConfigSvr and propagated to the shards.
let db = configDB.databases.findOne({_id: kDbName});
let dbTimestampAfterCreate = db.version.timestamp;
assert.neq(null, dbTimestampAfterCreate);

let coll = configDB.collections.findOne({_id: kNs});
let collTimestampAfterCreate = coll.timestamp;
assert.neq(null, collTimestampAfterCreate);

checkTimestampConsistencyInPersistentMetadata(
    kDbName, kNs, dbTimestampAfterCreate, collTimestampAfterCreate);

// Drop the database and implicitly its collections. Create again both and check their timestamps.
st.s.getDB(kDbName).dropDatabase();
assert.commandWorked(st.s.adminCommand({enableSharding: kDbName}));
assert.commandWorked(st.s.adminCommand({shardCollection: kNs, key: {x: 1}}));

db = configDB.databases.findOne({_id: kDbName});
let dbTimestampAfterDropDB = db.version.timestamp;
assert.neq(null, dbTimestampAfterDropDB);
assert.eq(timestampCmp(dbTimestampAfterDropDB, dbTimestampAfterCreate), 1);

coll = configDB.collections.findOne({_id: kNs});
let collTimestampAfterDropDB = coll.timestamp;
assert.neq(null, collTimestampAfterDropDB);
assert.eq(timestampCmp(collTimestampAfterDropDB, collTimestampAfterCreate), 1);

checkTimestampConsistencyInPersistentMetadata(
    kDbName, kNs, dbTimestampAfterDropDB, collTimestampAfterDropDB);

// Drop the collection and create it again. Collection timestamp should then be updated.
st.s.getDB(kDbName).coll.drop();
assert.commandWorked(st.s.adminCommand({shardCollection: kNs, key: {x: 1}}));

db = configDB.databases.findOne({_id: kDbName});
let dbTimestampAfterDropColl = db.version.timestamp;
assert.neq(null, dbTimestampAfterDropColl);
assert.eq(timestampCmp(dbTimestampAfterDropColl, dbTimestampAfterDropDB), 0);

coll = configDB.collections.findOne({_id: kNs});
let collTimestampAfterDropColl = coll.timestamp;
assert.neq(null, collTimestampAfterDropColl);
assert.eq(timestampCmp(collTimestampAfterDropColl, collTimestampAfterCreate), 1);

checkTimestampConsistencyInPersistentMetadata(
    kDbName, kNs, dbTimestampAfterDropColl, collTimestampAfterDropColl);

// Refine sharding key. Collection timestamp should then be updated.
assert.commandWorked(st.s.getCollection(kNs).createIndex({x: 1, y: 1}));
assert.commandWorked(st.s.adminCommand({refineCollectionShardKey: kNs, key: {x: 1, y: 1}}));

db = configDB.databases.findOne({_id: kDbName});
let dbTimestampAfterRefine = db.version.timestamp;
assert.neq(null, dbTimestampAfterRefine);
assert.eq(timestampCmp(dbTimestampAfterRefine, dbTimestampAfterDropColl), 0);

coll = configDB.collections.findOne({_id: kNs});
let collTimestampAfterRefine = coll.timestamp;
assert.neq(null, collTimestampAfterRefine);
assert.eq(timestampCmp(collTimestampAfterRefine, collTimestampAfterDropColl), 1);

checkTimestampConsistencyInPersistentMetadata(
    kDbName, kNs, dbTimestampAfterRefine, collTimestampAfterRefine);

st.stop();
})();