summaryrefslogtreecommitdiff
path: root/jstests/readonly/lib/read_only_test.js
blob: ce177dc0f9db8e550bf10eacbab43cf51c6c1011 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
var StandaloneFixture, ShardedFixture, runReadOnlyTest, zip2, cycleN;

(function() {
"use strict";

StandaloneFixture = function() {};

StandaloneFixture.prototype.runLoadPhase = function runLoadPhase(test) {
    this.mongod = MongoRunner.runMongod({});
    this.dbpath = this.mongod.dbpath;

    test.load(this.mongod.getDB("test")[test.name]);
    assert.commandWorked(this.mongod.getDB("local").dropDatabase());
    MongoRunner.stopMongod(this.mongod);
};

StandaloneFixture.prototype.runExecPhase = function runExecPhase(test) {
    var options = {queryableBackupMode: "", noCleanData: true, dbpath: this.dbpath};
    this.mongod = MongoRunner.runMongod(options);
    assert.neq(this.mongod, null);
    test.exec(this.mongod.getDB("test")[test.name]);
    MongoRunner.stopMongod(this.mongod);
};

ShardedFixture = function() {
    this.nShards = 3;
    this.nConfigs = 3;
};

ShardedFixture.prototype.runLoadPhase = function runLoadPhase(test) {
    this.st =
        new ShardingTest({mongos: 1, config: this.nConfigs, shards: this.nShards, rs: {nodes: 1}});

    // This information will be needed later on to set up the shards on read only mode.
    this.dbPaths = [];
    this.ports = [];
    this.hosts = [];
    for (let i = 0; i < this.nShards; ++i) {
        let primary = this.st["rs" + i].getPrimary();
        this.dbPaths.push(primary.dbpath);
        this.ports.push(primary.port);
        this.hosts.push(primary.host);
    }

    jsTest.log("sharding test collection...");

    // Use a hashed shard key so we actually hit multiple shards.
    this.st.shardColl(test.name, {_id: "hashed"}, false);

    test.load(this.st.getDB("test")[test.name]);
};

ShardedFixture.prototype.runExecPhase = function runExecPhase(test) {
    const docs = [{_id: 1}];
    let shardIdentities = [];
    let readOnlyShards = [];
    let operationTimes = [];

    for (let i = 0; i < this.nShards; ++i) {
        let primary = this.st["rs" + i].getPrimary();
        shardIdentities.push(
            primary.getDB("admin").getCollection("system.version").findOne({_id: "shardIdentity"}));
        assert.neq(null, shardIdentities[i]);
        // Get operation time so the read only shard recover all the info.
        operationTimes.push(
            assert
                .commandWorked(primary.getDB('_temporary_db')
                                   .runCommand({insert: '_temporary_coll' + i, documents: docs}))
                .operationTime);
    }

    this.st.stopAllShards({noCleanData: true, restart: true, skipValidations: true});

    jsTest.log("Restarting shards as read only standalone instances...");
    for (let i = 0; i < this.nShards; ++i) {
        let dbPath = this.dbPaths[i];
        let port = this.ports[i];
        let operationTime = operationTimes[i];

        jsTestLog("Renaming local.system collection on shard " + i);

        let tempMongod =
            MongoRunner.runMongod({port: port, dbpath: dbPath, noReplSet: true, noCleanData: true});
        // Rename the local.system collection to prevent problems with replset configurations.
        tempMongod.getDB('local').getCollection('system').renameCollection('_system');
        MongoRunner.stopMongod(tempMongod, {noCleanData: true, skipValidations: true, wait: true});

        let shardIdentity = shardIdentities[i];
        let host = this.hosts[i];

        // Change the connection string format so the router can create a standalone targeter
        // instead of a replica set targeter.
        this.st.config.shards.update({_id: shardIdentity.shardName}, {$set: {host: host}});

        // Restart the shard as standalone on read only mode.
        let configFileStr = "sharding:\n _overrideShardIdentity: '" +
            tojson(shardIdentity).replace(/\s+/g, ' ') + "'";
        // Use the os-specific path delimiter.
        jsTestLog('Seting up shard ' + i + ' with new shard identity ' + tojson(shardIdentity));
        let delim = _isWindows() ? '\\' : '/';
        let configFilePath = dbPath + delim + "config-for-shard-" + i + ".yml";

        writeFile(configFilePath, configFileStr);

        readOnlyShards.push(MongoRunner.runMongod({
            config: configFilePath,
            dbpath: dbPath,
            port: port,
            queryableBackupMode: "",
            restart: true,
            shardsvr: "",
            setParameter: {recoverToOplogTimestamp: tojson({timestamp: operationTime})}
        }));
    }

    // Restart the config server  and the router so they can reload the shard registry.
    jsTest.log("Restarting the config server...");
    for (let i = 0; i < this.nConfig; ++i) {
        this.st.restartConfigServer(i);
    }

    jsTest.log("Restarting mongos...");
    this.st.restartMongos(0);
    test.exec(this.st.getDB("test")[test.name]);

    for (let i = 0; i < this.nShards; ++i) {
        let dbPath = this.dbPaths[i];
        let port = this.ports[i];

        // Stop the read only shards.
        MongoRunner.stopMongod(readOnlyShards[i],
                               {noCleanData: true, skipValidations: true, wait: true});

        // Run a temporary mongod to rename the local.system collection.
        let tempMongod =
            MongoRunner.runMongod({port: port, dbpath: dbPath, noReplSet: true, noCleanData: true});
        tempMongod.getDB('local').getCollection('_system').renameCollection('system', true);
        MongoRunner.stopMongod(tempMongod, {noCleanData: true, skipValidations: true, wait: true});

        let shardIdentity = shardIdentities[i];
        let host = this.hosts[i];
        this.st.config.shards.update({_id: shardIdentity.shardName},
                                     {$set: {host: shardIdentity.shardName + '/' + host}});

        // Restart the shard to have a well behaved departure.
        this.st.restartShardRS(i);
    }

    // Restart the config server and the router so the shard registry refreshes and enable the RSM
    // again.
    for (let i = 0; i < this.nConfigs; ++i) {
        this.st.restartConfigServer(i);
    }

    this.st.restartMongos(0);

    this.st.stop();
};

runReadOnlyTest = function(test) {
    printjson(test);

    assert.eq(typeof (test.exec), "function");
    assert.eq(typeof (test.load), "function");
    assert.eq(typeof (test.name), "string");

    var fixtureType = TestData.fixture || "standalone";

    var fixture = null;
    if (fixtureType === "standalone") {
        fixture = new StandaloneFixture();
    } else if (fixtureType === "sharded") {
        fixture = new ShardedFixture();
    } else {
        throw new Error("fixtureType must be one of either 'standalone' or 'sharded'");
    }

    jsTest.log("starting load phase for test: " + test.name);
    fixture.runLoadPhase(test);

    jsTest.log("starting execution phase for test: " + test.name);
    fixture.runExecPhase(test);
};

cycleN = function*(arr, N) {
    for (var i = 0; i < N; ++i) {
        yield arr[i % arr.length];
    }
};

zip2 = function*(iter1, iter2) {
    var n1 = iter1.next();
    var n2 = iter2.next();
    while (!n1.done || !n2.done) {
        var res = [];
        if (!n1.done) {
            res.push(n1.value);
            n1 = iter1.next();
        }
        if (!n2.done) {
            res.push(n2.value);
            n2 = iter2.next();
        }

        yield res;
    }
};
}());