summaryrefslogtreecommitdiff
path: root/jstests/replsets/check_replicated_data_hashes_inconsistent_state.js
blob: e0abb94b7db55675e11c4d4ce61e8095b4ea34c9 (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
/*
 * Tests that checkReplicatedDataHashes will fail when there exists a data inconsistency between the
 * primary and secondary.
 *
 * @tags: [uses_testing_only_commands]
 */

(function() {
"use strict";

// Skip DB hash check in stopSet() since we expect it to fail in this test.
TestData.skipCheckDBHashes = true;

const rst = new ReplSetTest({name: jsTestName(), nodes: 2});
rst.startSet();
rst.initiate();

const primary = rst.getPrimary();
const primaryDB0 = primary.getDB("db0");
const primaryDB1 = primary.getDB("db1");
const primaryDB2 = primary.getDB("db2");
const collName = "testColl";

// The default WC is majority and godinsert command on a secondary is incompatible with wc:majority.
assert.commandWorked(primary.adminCommand(
    {setDefaultRWConcern: 1, defaultWriteConcern: {w: 1}, writeConcern: {w: "majority"}}));

const secondary = rst.getSecondary();
const secondaryDB = secondary.getDB("db0");

// Create a collection on the primary, so that the secondary will have this collection.
assert.commandWorked(primaryDB0.createCollection(collName));
rst.awaitReplication();

// Insert a write on the secondary that will not be on the primary.
assert.commandWorked(secondaryDB.runCommand({godinsert: collName, obj: {_id: 0, a: 0}}));

// Insert writes on various DBs that should be replicated.
assert.commandWorked(primaryDB1.runCommand({insert: collName, documents: [{_id: 1, b: 1}]}));
assert.commandWorked(primaryDB2.runCommand({insert: collName, documents: [{_id: 2, c: 2}]}));

const err = assert.throws(() => rst.checkReplicatedDataHashes());
assert(err.message.includes("dbhash mismatch between primary and secondary"),
       `caught error didn't mention dbhash mismatch: ${tojson(err)}`);

rst.stopSet();
})();