summaryrefslogtreecommitdiff
path: root/jstests/sharding/convert_to_and_from_sharded.js
blob: 6ef54a2e37a30e909ba8aaca4af089ab61970d90 (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
/**
 * Test that a replica set member can process basic CRUD operations after switching from being
 * a shardsvr and back to non shardsvr.
 * @tags: [requires_persistence]
 */
(function() {
"use strict";
load('jstests/sharding/libs/remove_shard_util.js');

// TODO SERVER-50144 Remove this and allow orphan checking.
// This test calls removeShard which can leave docs in config.rangeDeletions in state "pending",
// therefore preventing orphans from being cleaned up.
TestData.skipCheckOrphans = true;

var NUM_NODES = 3;

/**
 * Checks that basic CRUD operations work as expected. Expects the collection to have a
 * { _id: 'marker' } document.
 */
var checkBasicCRUD = function(coll) {
    var doc = coll.findOne({_id: 'marker', y: {$exists: false}});
    assert.neq(null, doc);

    assert.commandWorked(coll.update({_id: 'marker'}, {$set: {y: 2}}));
    assert.eq(2, coll.findOne({_id: 'marker'}).y);

    assert.commandWorked(coll.remove({_id: 'marker'}));
    assert.eq(null, coll.findOne({_id: 'marker'}));

    assert.commandWorked(coll.insert({_id: 'marker'}, {writeConcern: {w: NUM_NODES}}));
    assert.eq('marker', coll.findOne({_id: 'marker'})._id);
};

const numShards = TestData.configShard ? 1 : 0;
var st = new ShardingTest({shards: numShards});

var replShard = new ReplSetTest({nodes: NUM_NODES});
replShard.startSet({verbose: 1});
replShard.initiate();

var priConn = replShard.getPrimary();

// Starting a brand new replica set without '--shardsvr' will cause the FCV to be written as the
// latest available for that binary. This poses a problem when this test is run in the mixed
// version suite because mongos will be 'last-lts' and if this node is of the latest binary,
// it will report itself as the 'latest' FCV, which would cause mongos to refuse to connect to
// it and shutdown.
//
// In order to work around this, in the mixed version suite, be pessimistic and always set this
// node to the 'last-lts' FCV
if (jsTestOptions().shardMixedBinVersions) {
    assert.commandWorked(priConn.adminCommand({setFeatureCompatibilityVersion: lastLTSFCV}));
    replShard.awaitReplication();
}

assert.commandWorked(priConn.getDB('test').unsharded.insert({_id: 'marker'}));
checkBasicCRUD(priConn.getDB('test').unsharded);

assert.commandWorked(priConn.getDB('test').sharded.insert({_id: 'marker'}));
checkBasicCRUD(priConn.getDB('test').sharded);

for (var x = 0; x < NUM_NODES; x++) {
    replShard.restart(x, {
        shardsvr: '',
    });
}

replShard.awaitNodesAgreeOnPrimary();
assert.commandWorked(st.s.adminCommand({addShard: replShard.getURL()}));

checkBasicCRUD(st.s.getDB('test').unsharded);
checkBasicCRUD(st.s.getDB('test').sharded);

assert.commandWorked(st.s.adminCommand({enableSharding: 'test'}));
assert.commandWorked(st.s.adminCommand({shardCollection: 'test.sharded', key: {_id: 1}}));

checkBasicCRUD(st.s.getDB('test').unsharded);
checkBasicCRUD(st.s.getDB('test').sharded);

for (x = 0; x < 4; x++) {
    assert.commandWorked(st.s.getDB('test').sharded.insert({_id: x}));
    assert.commandWorked(st.s.adminCommand({split: 'test.sharded', middle: {_id: x}}));
}

let newShard =
    new ReplSetTest({name: "toRemoveLater", nodes: NUM_NODES, nodeOptions: {shardsvr: ""}});
newShard.startSet();
newShard.initiate();

assert.commandWorked(st.s.adminCommand({addShard: newShard.getURL(), name: 'toRemoveLater'}));

for (x = 0; x < 2; x++) {
    assert.commandWorked(
        st.s.adminCommand({moveChunk: 'test.sharded', find: {_id: x}, to: 'toRemoveLater'}));
}

checkBasicCRUD(st.s.getDB('test').unsharded);
checkBasicCRUD(st.s.getDB('test').sharded);

// Start the balancer to start draining the chunks.
st.startBalancer();

removeShard(st, 'toRemoveLater');

newShard.stopSet();

checkBasicCRUD(st.s.getDB('test').unsharded);
checkBasicCRUD(st.s.getDB('test').sharded);

st.stop();

jsTest.log('About to restart repl w/o shardsvr');

replShard.nodes.forEach(function(node) {
    delete node.fullOptions.shardsvr;
});

replShard.restart(replShard.nodes);
replShard.awaitNodesAgreeOnPrimary();

priConn = replShard.getPrimary();
checkBasicCRUD(priConn.getDB('test').unsharded);
checkBasicCRUD(priConn.getDB('test').sharded);
replShard.stopSet();
})();