summaryrefslogtreecommitdiff
path: root/jstests/sharding/oplog_document_key.js
blob: d138457e4f0b9dee8771f317fd9beaf707384d9d (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
/**
 * Verify that shard key appears in oplog records
 *
 * The only records that need to be checked are delete and update records, but updates happen on
 * various paths that must all be checked.
 */

(function() {
"use strict";

var st = new ShardingTest({name: 'test', shards: {rs0: {nodes: 1}}});
var db = st.s.getDB('test');

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

// 'test.un' is left unsharded.
assert.commandWorked(db.adminCommand({shardcollection: 'test.byId', key: {_id: 1}}));
assert.commandWorked(db.adminCommand({shardcollection: 'test.byX', key: {x: 1}}));
assert.commandWorked(db.adminCommand({shardcollection: 'test.byXId', key: {x: 1, _id: 1}}));
assert.commandWorked(db.adminCommand({shardcollection: 'test.byIdX', key: {_id: 1, x: 1}}));

assert.writeOK(db.un.insert({_id: 10, x: 50, y: 60}));
assert.writeOK(db.un.insert({_id: 30, x: 70, y: 80}));

assert.writeOK(db.byId.insert({_id: 11, x: 51, y: 61}));
assert.writeOK(db.byId.insert({_id: 31, x: 71, y: 81}));

assert.writeOK(db.byX.insert({_id: 12, x: 52, y: 62}));
assert.writeOK(db.byX.insert({_id: 32, x: 72, y: 82}));

assert.writeOK(db.byXId.insert({_id: 13, x: 53, y: 63}));
assert.writeOK(db.byXId.insert({_id: 33, x: 73, y: 83}));

assert.writeOK(db.byIdX.insert({_id: 14, x: 54, y: 64}));
assert.writeOK(db.byIdX.insert({_id: 34, x: 74, y: 84}));

var oplog = st.rs0.getPrimary().getDB('local').oplog.rs;

////////////////////////////////////////////////////////////////////////
jsTest.log("Test update command on 'un'");

assert.writeOK(db.un.update({_id: 10, x: 50}, {$set: {y: 70}}));  // in place
assert.writeOK(db.un.update({_id: 30, x: 70}, {y: 75}));          // replacement

// unsharded, only _id appears in o2:

var a = oplog.findOne({ns: 'test.un', op: 'u', 'o2._id': 10});
assert.eq(a.o2, {_id: 10});

var b = oplog.findOne({ns: 'test.un', op: 'u', 'o2._id': 30});
assert.eq(b.o2, {_id: 30});

////////////////////////////////////////////////////////////////////////
jsTest.log("Test update command on 'byId'");

assert.writeOK(db.byId.update({_id: 11}, {$set: {y: 71}}));  // in place
assert.writeOK(db.byId.update({_id: 31}, {x: 71, y: 76}));   // replacement

// sharded by {_id: 1}: only _id appears in o2:

a = oplog.findOne({ns: 'test.byId', op: 'u', 'o2._id': 11});
assert.eq(a.o2, {_id: 11});

b = oplog.findOne({ns: 'test.byId', op: 'u', 'o2._id': 31});
assert.eq(b.o2, {_id: 31});

////////////////////////////////////////////////////////////////////////
jsTest.log("Test update command on 'byX'");

assert.writeOK(db.byX.update({x: 52}, {$set: {y: 72}}));  // in place
assert.writeOK(db.byX.update({x: 72}, {x: 72, y: 77}));   // replacement

// sharded by {x: 1}: x appears in o2, followed by _id:

a = oplog.findOne({ns: 'test.byX', op: 'u', 'o2._id': 12});
assert.eq(a.o2, {x: 52, _id: 12});

b = oplog.findOne({ns: 'test.byX', op: 'u', 'o2._id': 32});
assert.eq(b.o2, {x: 72, _id: 32});

////////////////////////////////////////////////////////////////////////
jsTest.log("Test update command on 'byXId'");

assert.writeOK(db.byXId.update({_id: 13, x: 53}, {$set: {y: 73}}));  // in place
assert.writeOK(db.byXId.update({_id: 33, x: 73}, {x: 73, y: 78}));   // replacement

// sharded by {x: 1, _id: 1}: x appears in o2, followed by _id:

a = oplog.findOne({ns: 'test.byXId', op: 'u', 'o2._id': 13});
assert.eq(a.o2, {x: 53, _id: 13});

b = oplog.findOne({ns: 'test.byXId', op: 'u', 'o2._id': 33});
assert.eq(b.o2, {x: 73, _id: 33});

////////////////////////////////////////////////////////////////////////
jsTest.log("Test update command on 'byIdX'");

assert.writeOK(db.byIdX.update({_id: 14, x: 54}, {$set: {y: 74}}));  // in place
assert.writeOK(db.byIdX.update({_id: 34, x: 74}, {x: 74, y: 79}));   // replacement

// sharded by {_id: 1, x: 1}: _id appears in o2, followed by x:

a = oplog.findOne({ns: 'test.byIdX', op: 'u', 'o2._id': 14});
assert.eq(a.o2, {_id: 14, x: 54});

b = oplog.findOne({ns: 'test.byIdX', op: 'u', 'o2._id': 34});
assert.eq(b.o2, {_id: 34, x: 74});

////////////////////////////////////////////////////////////////////////
jsTest.log("Test remove command: 'un'");

assert.writeOK(db.un.remove({_id: 10}));
assert.writeOK(db.un.remove({_id: 30}));

a = oplog.findOne({ns: 'test.un', op: 'd', 'o._id': 10});
assert.eq(a.o, {_id: 10});
b = oplog.findOne({ns: 'test.un', op: 'd', 'o._id': 30});
assert.eq(b.o, {_id: 30});

////////////////////////////////////////////////////////////////////////
jsTest.log("Test remove command: 'byX'");

assert.writeOK(db.byX.remove({_id: 12}));
assert.writeOK(db.byX.remove({_id: 32}));

a = oplog.findOne({ns: 'test.byX', op: 'd', 'o._id': 12});
assert.eq(a.o, {x: 52, _id: 12});
b = oplog.findOne({ns: 'test.byX', op: 'd', 'o._id': 32});
assert.eq(b.o, {x: 72, _id: 32});

////////////////////////////////////////////////////////////////////////
jsTest.log("Test remove command: 'byXId'");

assert.writeOK(db.byXId.remove({_id: 13}));
assert.writeOK(db.byXId.remove({_id: 33}));

a = oplog.findOne({ns: 'test.byXId', op: 'd', 'o._id': 13});
assert.eq(a.o, {x: 53, _id: 13});
b = oplog.findOne({ns: 'test.byXId', op: 'd', 'o._id': 33});
assert.eq(b.o, {x: 73, _id: 33});

////////////////////////////////////////////////////////////////////////
jsTest.log("Test remove command: 'byIdX'");

assert.writeOK(db.byIdX.remove({_id: 14}));
assert.writeOK(db.byIdX.remove({_id: 34}));

a = oplog.findOne({ns: 'test.byIdX', op: 'd', 'o._id': 14});
assert.eq(a.o, {_id: 14, x: 54});
b = oplog.findOne({ns: 'test.byIdX', op: 'd', 'o._id': 34});
assert.eq(b.o, {_id: 34, x: 74});

st.stop();
})();