summaryrefslogtreecommitdiff
path: root/jstests/sharding/operation_time_api.js
blob: cd69450359086c473a4ef00952a3e0b2deec1a7b (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
/**
 * Tests the operationTime API for the following topologies:
 *   - mongos talking to a sharded replica set (sharded and unsharded collections)
 *   - mongod from a sharded replica set
 *   - mongod from a non-sharded replica set
 *   - standalone mongod
 */
(function() {
"use strict";

function responseContainsTimestampOperationTime(res) {
    return res.operationTime !== undefined && isTimestamp(res.operationTime);
}

function isTimestamp(val) {
    return Object.prototype.toString.call(val) === "[object Timestamp]";
}

// A mongos that talks to a non-sharded collection on a sharded replica set returns an
// operationTime that is a Timestamp.
var st = new ShardingTest({name: "operation_time_api", shards: {rs0: {nodes: 1}}});

var testDB = st.s.getDB("test");
var res = assert.commandWorked(testDB.runCommand({insert: "foo", documents: [{x: 1}]}));
assert(responseContainsTimestampOperationTime(res),
       "Expected response from a mongos talking to a non-sharded collection on a sharded " +
           "replica set to contain an operationTime, received: " + tojson(res));

// A mongos that talks to a sharded collection on a sharded replica set returns an operationTime
// that is a Timestamp.
assert.commandWorked(st.s.adminCommand({enableSharding: "test"}));
assert.commandWorked(st.s.adminCommand({shardCollection: "test.bar", key: {x: 1}}));

res = assert.commandWorked(testDB.runCommand({insert: "bar", documents: [{x: 2}]}));
assert(responseContainsTimestampOperationTime(res),
       "Expected response from a mongos inserting to a sharded collection on a sharded " +
           "replica set to contain an operationTime, received: " + tojson(res));

// A mongod in a sharded replica set returns an operationTime that is a Timestamp.
testDB = st.rs0.getPrimary().getDB("test");
res = assert.commandWorked(testDB.runCommand({insert: "foo", documents: [{x: 3}]}));
assert(responseContainsTimestampOperationTime(res),
       "Expected response from a mongod in a sharded replica set to contain an " +
           "operationTime, received: " + tojson(res));

st.stop();

// A mongod from a non-sharded replica set returns an operationTime that is a Timestamp.
var replTest = new ReplSetTest({name: "operation_time_api_non_sharded_replset", nodes: 1});
replTest.startSet();
replTest.initiate();

testDB = replTest.getPrimary().getDB("test");
res = assert.commandWorked(testDB.runCommand({insert: "foo", documents: [{x: 4}]}));
assert(responseContainsTimestampOperationTime(res),
       "Expected response from a non-sharded replica set to contain an operationTime, " +
           "received: " + tojson(res));

replTest.stopSet();

// A standalone mongod does not return an operationTime.
var standalone = MongoRunner.runMongod();

testDB = standalone.getDB("test");
res = assert.commandWorked(testDB.runCommand({insert: "foo", documents: [{x: 5}]}));
assert(!responseContainsTimestampOperationTime(res),
       "Expected response from a standalone mongod to not contain an operationTime, " +
           "received: " + tojson(res));

MongoRunner.stopMongod(standalone);
})();