summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/mongos_exhausts_stale_config_retries.js
blob: 575d9a07ccd12d72ce390c4fa586c34d340091aa (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
// Verifies mongos returns StaleConfig if it exhausts its allowed stale version retry attempts,
// using the command read and write modes.
//
// @tags: [
//   requires_sharding,
// ]
(function() {
"use strict";

const dbName = "test";
const collName = "foo";
const ns = dbName + '.' + collName;

const st = new ShardingTest({shards: 2, config: 1});
const testDB = st.s.getDB(dbName);

// Shard a collection with the only chunk on shard0.
assert.commandWorked(st.s.adminCommand({enableSharding: dbName}));
st.ensurePrimaryShard(dbName, st.shard0.shardName);
assert.commandWorked(st.s.adminCommand({shardCollection: ns, key: {_id: 1}}));

const sourcePrimary = st.rs0.getPrimary();
const recipientPrimary = st.rs1.getPrimary();

// Disable the best-effort recipient metadata refresh after migrations and move the chunk
// between shards so the recipient shard, shard1, is stale.
assert.commandWorked(recipientPrimary.adminCommand(
    {configureFailPoint: "migrationRecipientFailPostCommitRefresh", mode: "alwaysOn"}));

assert.commandWorked(st.s.adminCommand({moveChunk: ns, find: {_id: 0}, to: st.shard1.shardName}));

// Disable metadata refreshes on the recipient shard so it will indefinitely return StaleConfig.
assert.commandWorked(recipientPrimary.adminCommand(
    {configureFailPoint: "skipShardFilteringMetadataRefresh", mode: "alwaysOn"}));

// Test various read and write commands that are sent with shard versions and thus can return
// StaleConfig. Batch writes, i.e. insert/update/delete, return batch responses with ok:1 and
// NoProgressMade write errors when retries are exhausted, so they are excluded.
const kCommands = [
    {aggregate: collName, pipeline: [], cursor: {}},
    {count: collName},
    {distinct: collName, query: {}, key: "_id"},
    {find: collName},
    {findAndModify: collName, query: {_id: 0}, update: {$set: {x: 1}}},
];

kCommands.forEach((cmd) => {
    // The recipient shard should return StaleConfig until mongos exhausts its retries and
    // returns the final StaleConfig to the client.
    assert.commandFailedWithCode(testDB.runCommand(cmd),
                                 ErrorCodes.StaleConfig,
                                 "expected to fail with StaleConfig, cmd: " + tojson(cmd));
});

assert.commandWorked(recipientPrimary.adminCommand(
    {configureFailPoint: "migrationRecipientFailPostCommitRefresh", mode: "off"}));

assert.commandWorked(recipientPrimary.adminCommand(
    {configureFailPoint: "skipShardFilteringMetadataRefresh", mode: "off"}));

st.stop();
})();