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
|
/**
* Tests the _shardsvrInsertGlobalIndexKey and _shardsvrDeleteGlobalIndexKey commands.
*
* @tags: [
* featureFlagGlobalIndexes,
* requires_fcv_62,
* requires_replication,
* ]
*/
(function() {
"use strict";
const rst = new ReplSetTest({nodes: 2});
rst.startSet();
rst.initiate();
const primary = rst.getPrimary();
const adminDB = primary.getDB("admin");
const globalIndexUUID = UUID();
const otherGlobalIndexUUID = UUID();
assert.commandWorked(adminDB.runCommand({_shardsvrCreateGlobalIndex: globalIndexUUID}));
assert.commandWorked(adminDB.runCommand({_shardsvrCreateGlobalIndex: otherGlobalIndexUUID}));
// _shardsvrInsertGlobalIndexKey must run inside a transaction.
assert.commandFailedWithCode(
adminDB.runCommand(
{_shardsvrInsertGlobalIndexKey: globalIndexUUID, key: {a: 1}, docKey: {sk: 1, _id: 1}}),
6789400);
// _shardsvrDeleteGlobalIndexKey must run inside a transaction.
assert.commandFailedWithCode(
adminDB.runCommand(
{_shardsvrDeleteGlobalIndexKey: globalIndexUUID, key: {a: 1}, docKey: {sk: 1, _id: 1}}),
6924200);
// Cannot insert a key into an unexisting container.
{
const session = primary.startSession();
session.startTransaction();
assert.commandFailedWithCode(
session.getDatabase("system").runCommand(
{_shardsvrInsertGlobalIndexKey: UUID(), key: {a: 1}, docKey: {sk: 1, _id: 1}}),
6789402);
session.endSession();
}
// Cannot delete a key from an unexisting container.
{
const session = primary.startSession();
session.startTransaction();
assert.commandFailedWithCode(
session.getDatabase("system").runCommand(
{_shardsvrDeleteGlobalIndexKey: UUID(), key: {a: 1}, docKey: {sk: 1, _id: 1}}),
6924201);
session.endSession();
}
// _shardsvrInsertGlobalIndexKey and posterior _shardsvrDeleteGlobalIndexKey succeed. Run multiple
// times to ensure that operations are properly serialized on replication and do not cause
// secondaries to crash.
{
const session = primary.startSession();
for (var i = 0; i < 10; i++) {
session.startTransaction();
assert.commandWorked(session.getDatabase("system").runCommand({
"_shardsvrInsertGlobalIndexKey": globalIndexUUID,
key: {a: 1},
docKey: {sk: 1, _id: i}
}));
assert.commandWorked(session.getDatabase("system").runCommand({
"_shardsvrDeleteGlobalIndexKey": globalIndexUUID,
key: {a: 1},
docKey: {sk: 1, _id: i}
}));
session.commitTransaction();
}
session.endSession();
}
// Insert duplicate key.
{
const session = primary.startSession();
session.startTransaction();
assert.commandWorked(session.getDatabase("system").runCommand(
{"_shardsvrInsertGlobalIndexKey": globalIndexUUID, key: {a: 1}, docKey: {sk: 1, _id: 1}}));
assert.commandFailedWithCode(session.getDatabase("system").runCommand({
"_shardsvrInsertGlobalIndexKey": globalIndexUUID,
key: {a: 1},
docKey: {sk: 10, _id: 10}
}),
ErrorCodes.DuplicateKey);
session.endSession();
}
// _shardsvrDeleteGlobalIndexKey and reinsert _shardsvrInsertGlobalIndexKey succeed. Run multiple
// times to ensure that operations are properly serialized on replication and do not cause
// secondaries to crash.
{
const session = primary.startSession();
session.startTransaction();
assert.commandWorked(session.getDatabase("system").runCommand(
{"_shardsvrInsertGlobalIndexKey": globalIndexUUID, key: {a: 1}, docKey: {sk: 1, _id: 0}}));
session.commitTransaction();
for (var i = 0; i < 10; i++) {
session.startTransaction();
assert.commandWorked(session.getDatabase("system").runCommand({
"_shardsvrDeleteGlobalIndexKey": globalIndexUUID,
key: {a: 1},
docKey: {sk: 1, _id: i}
}));
assert.commandWorked(session.getDatabase("system").runCommand({
"_shardsvrInsertGlobalIndexKey": globalIndexUUID,
key: {a: 1},
docKey: {sk: 1, _id: i + 1}
}));
session.commitTransaction();
}
session.endSession();
}
// Delete non existing key in non-empty collection.
{
const session = primary.startSession();
session.startTransaction();
assert.commandFailedWithCode(session.getDatabase("system").runCommand({
"_shardsvrDeleteGlobalIndexKey": globalIndexUUID,
key: {a: 123},
docKey: {sk: 1, _id: 1}
}),
ErrorCodes.KeyNotFound);
session.endSession();
}
// Delete existing docKey with mismatched key.
{
const session = primary.startSession();
session.startTransaction();
assert.commandFailedWithCode(session.getDatabase("system").runCommand({
"_shardsvrDeleteGlobalIndexKey": globalIndexUUID,
key: {a: 123},
docKey: {sk: 1, _id: 10}
}),
ErrorCodes.KeyNotFound);
session.endSession();
}
// Run _shardsvrInsertGlobalIndexKey and _shardsvrDeleteGlobalIndexKey inside a transaction that
// performs reads and writes to other namespaces. Also perform inserts and deletes to two different
// global indexes.
{
primary.getDB("test").getCollection("c").insertOne({foo: 1});
rst.awaitReplication();
const session = primary.startSession();
session.startTransaction({readConcern: {level: "snapshot"}, writeConcern: {w: "majority"}});
assert.commandWorked(session.getDatabase("system").runCommand({
"_shardsvrInsertGlobalIndexKey": otherGlobalIndexUUID,
key: {a: 123},
docKey: {sk: 123, _id: 123}
}));
assert.eq(1, session.getDatabase("test").getCollection("c").find({foo: 1}).itcount());
assert.commandWorked(session.getDatabase("system").runCommand({
"_shardsvrInsertGlobalIndexKey": globalIndexUUID,
key: {a: 123},
docKey: {sk: 123, _id: 123}
}));
assert.commandWorked(session.getDatabase("test").getCollection("c").insertOne({bar: 1}));
assert.commandWorked(session.getDatabase("system").runCommand({
"_shardsvrDeleteGlobalIndexKey": otherGlobalIndexUUID,
key: {a: 123},
docKey: {sk: 123, _id: 123}
}));
assert.commandWorked(session.getDatabase("system").runCommand({
"_shardsvrDeleteGlobalIndexKey": globalIndexUUID,
key: {a: 123},
docKey: {sk: 123, _id: 123}
}));
session.commitTransaction();
session.endSession();
}
rst.stopSet();
})();
|