summaryrefslogtreecommitdiff
path: root/jstests/replsets/rollback_rename_collection_on_sync_source.js
blob: 2084aecfd768f176e51b9cfa03923404536a5caf (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
/**
 * Test to ensure that a 'renameCollection' operation that occurs on the sync source past the
 * rollback common point for a collection that also exists on the rollback node will not cause data
 * corruption on the rollback node.
 */

(function() {
'use strict';

load("jstests/replsets/libs/rollback_test.js");

let dbName = "rollback_rename_collection_on_sync_source";
let otherDbName = "rollback_rename_collection_on_sync_source_other";
let sourceCollName = "sourceColl";
let destCollName = "destColl";
let sourceNs = dbName + '.' + sourceCollName;
let destNs = dbName + '.' + destCollName;
let otherDestNs = otherDbName + '.' + destCollName;

let doc1 = {x: 1};
let doc2 = {x: 2};

let CommonOps = (node) => {
    // Insert a document that will exist on the sync source and rollback node.
    assert.commandWorked(node.getDB(dbName)[sourceCollName].insert(doc1));
};

let RollbackOps = (node) => {
    // Delete the document on rollback node so it will be refetched from sync source.
    assert.commandWorked(node.getDB(dbName)[sourceCollName].remove(doc1));
};

let SyncSourceOps = (node, withinDB) => {
    // Rename the original collection on the sync source.
    let destCollection = (withinDB) ? destNs : otherDestNs;
    assert.commandWorked(
        node.getDB(dbName).adminCommand({renameCollection: sourceNs, to: destCollection}));
    assert.commandWorked(node.getDB(dbName)[destCollection].insert(doc2));
};

// 1. Rename to collection within same database.
// Set up Rollback Test.
let rollbackTest = new RollbackTest();
CommonOps(rollbackTest.getPrimary());

let rollbackNode = rollbackTest.transitionToRollbackOperations();
RollbackOps(rollbackNode);

let syncSourceNode = rollbackTest.transitionToSyncSourceOperationsBeforeRollback();
SyncSourceOps(syncSourceNode, /*withinDB=*/ true);

// Wait for rollback to finish.
rollbackTest.transitionToSyncSourceOperationsDuringRollback();
rollbackTest.transitionToSteadyStateOperations();

// Check the replica set.
rollbackTest.stop();

// 2. Rename to collection in another database.
// Set up Rollback Test.
let rollbackTestAcrossDBs = new RollbackTest();
CommonOps(rollbackTestAcrossDBs.getPrimary());

let rollbackNodeAcrossDBs = rollbackTestAcrossDBs.transitionToRollbackOperations();
RollbackOps(rollbackNodeAcrossDBs);

let syncSourceNodeAcrossDBs =
    rollbackTestAcrossDBs.transitionToSyncSourceOperationsBeforeRollback();
SyncSourceOps(syncSourceNodeAcrossDBs, /*withinDB=*/ false);

// Wait for rollback to finish.
rollbackTestAcrossDBs.transitionToSyncSourceOperationsDuringRollback();
rollbackTestAcrossDBs.transitionToSteadyStateOperations();

// Check the replica set.
rollbackTestAcrossDBs.stop();
}());