summaryrefslogtreecommitdiff
path: root/jstests/sharding/read_pref.js
blob: b930678a2b5f07bff41284eb08b6eb400500cecb (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
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
184
185
186
187
/**
 * Integration test for read preference and tagging. The more comprehensive unit test
 * can be found in dbtests/replica_set_monitor_test.cpp.
 */

var PRI_TAG = { dc: 'ny' };
var SEC_TAGS = [
    { dc: 'sf', s: "1" },
    { dc: 'ma', s: "2" },
    { dc: 'eu', s: "3" },
    { dc: 'jp', s: "4" }
];
var NODES = SEC_TAGS.length + 1;

var doTest = function(useDollarQuerySyntax) {
    var st = new ShardingTest({ shards: { rs0: { nodes: NODES, oplogSize: 10, verbose: 2,
                    useHostName: true }}});
    var replTest = st.rs0;
    var primaryNode = replTest.getMaster();

    var setupConf = function(){
        var replConf = primaryNode.getDB( 'local' ).system.replset.findOne();
        replConf.version = (replConf.version || 0) + 1;

        var secIdx = 0;
        for ( var x = 0; x < NODES; x++ ){
            var node = replConf.members[x];

            if ( node.host == primaryNode.name ){
                node.tags = PRI_TAG;
            }
            else {
                node.tags = SEC_TAGS[secIdx++];
                node.priority = 0;
            }
        }

        try {
            primaryNode.getDB( 'admin' ).runCommand({ replSetReconfig: replConf });
        } catch (x) {
            jsTest.log('Exception expected because reconfiguring would close all conn, got ' + x);
        }

        return replConf;
    };

    var checkTag = function( nodeToCheck, tag ){
        for ( var idx = 0; idx < NODES; idx++ ){
            var node = replConf.members[idx];

            if ( node.host == nodeToCheck ){
                jsTest.log( 'node[' + node.host + '], Tag: ' + tojson( node['tags'] ));
                jsTest.log( 'tagToCheck: ' + tojson( tag ));

                var nodeTag = node['tags'];

                for ( var key in tag ){
                    assert.eq( tag[key], nodeTag[key] );
                }

                return;
            }
        }

        assert( false, 'node ' + nodeToCheck + ' not part of config!' );
    };

    var replConf = setupConf();

    var conn = st.s;

    // Wait until the ReplicaSetMonitor refreshes its view and see the tags
    ReplSetTest.awaitRSClientHosts( conn, primaryNode,
            { ok: true, tags: PRI_TAG }, replTest.name );
    replTest.awaitReplication();

    jsTest.log('New rs config: ' + tojson(primaryNode.getDB('local').system.replset.findOne()));
    jsTest.log( 'connpool: ' + tojson(conn.getDB('admin').runCommand({ connPoolStats: 1 })));

    var coll = conn.getDB( 'test' ).user;

    assert.soon(function() {
        var res = coll.insert({ x: 1 }, { writeConcern: { w: NODES }});
        if (!res.hasWriteError()) {
            return true;
        }

        var err = res.getWriteError().errmsg;
        // Transient transport errors may be expected b/c of the replSetReconfig
        if (err.indexOf("transport error") == -1) {
            throw err;
        }
        return false;
    });


    var getExplain = function(readPrefMode, readPrefTags) {
        if (useDollarQuerySyntax) {
            var readPrefObj = {
                mode: readPrefMode
            };

            if (readPrefTags) {
                readPrefObj.tags = readPrefTags;
            }

            return coll.find({ $query: {}, $readPreference: readPrefObj,
                $explain: true }).limit(-1).next();
        }
        else {
            return coll.find().readPref(readPrefMode, readPrefTags).explain();
        }
    };

    // Read pref should work without slaveOk
    var explain = getExplain("secondary");
    assert.neq( primaryNode.name, explain.server );

    conn.setSlaveOk();

    // It should also work with slaveOk
    explain = getExplain("secondary");
    assert.neq( primaryNode.name, explain.server );

    // Check that $readPreference does not influence the actual query
    assert.eq( 1, explain.n );

    explain = getExplain("secondaryPreferred", [{ s: "2" }]);
    checkTag( explain.server, { s: "2" });
    assert.eq( 1, explain.n );

    // Cannot use tags with primaryOnly
    assert.throws( function() {
        getExplain("primary", [{ s: "2" }]);
    });

    // Ok to use empty tags on primaryOnly
    explain = coll.find().readPref("primary", [{}]).explain();
    assert.eq(primaryNode.name, explain.server);

    explain = coll.find().readPref("primary", []).explain();
    assert.eq(primaryNode.name, explain.server);

    // Check that mongos will try the next tag if nothing matches the first
    explain = getExplain("secondary", [{ z: "3" }, { dc: "jp" }]);
    checkTag( explain.server, { dc: "jp" });
    assert.eq( 1, explain.n );

    // Check that mongos will fallback to primary if none of tags given matches
    explain = getExplain("secondaryPreferred", [{ z: "3" }, { dc: "ph" }]);
    // Call getPrimary again since the primary could have changed after the restart.
    assert.eq(replTest.getPrimary().name, explain.server);
    assert.eq( 1, explain.n );

    // Kill all members except one
    var stoppedNodes = [];
    for ( var x = 0; x < NODES - 1; x++ ){
        replTest.stop( x );
        stoppedNodes.push( replTest.nodes[x] );
    }

    // Wait for ReplicaSetMonitor to realize nodes are down
    ReplSetTest.awaitRSClientHosts( conn, stoppedNodes, { ok: false }, replTest.name );

    // Wait for the last node to be in steady state -> secondary (not recovering)
    var lastNode = replTest.nodes[NODES - 1];
    ReplSetTest.awaitRSClientHosts( conn, lastNode,
        { ok: true, secondary: true }, replTest.name );

    jsTest.log( 'connpool: ' + tojson(conn.getDB('admin').runCommand({ connPoolStats: 1 })));

    // Test to make sure that connection is ok, in prep for priOnly test
    explain = getExplain("nearest");
    assert.eq( explain.server, replTest.nodes[NODES - 1].name );
    assert.eq( 1, explain.n );

    // Should assert if request with priOnly but no primary
    assert.throws( function(){
       getExplain("primary");
    });

    st.stop();
};

doTest(false);
doTest(true);