~evarlast/ubuntu/utopic/mongodb/upstart-workaround-debian-bug-718702

« back to all changes in this revision

Viewing changes to jstests/sharding/read_pref.js

  • Committer: Package Import Robot
  • Author(s): James Page, James Page, Robie Basak
  • Date: 2013-05-29 17:44:42 UTC
  • mfrom: (44.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20130529174442-z0a4qmoww4y0t458
Tags: 1:2.4.3-1ubuntu1
[ James Page ]
* Merge from Debian unstable, remaining changes:
  - Enable SSL support:
    + d/control: Add libssl-dev to BD's.
    + d/rules: Enabled --ssl option.
    + d/mongodb.conf: Add example SSL configuration options.
  - d/mongodb-server.mongodb.upstart: Add upstart configuration.
  - d/rules: Don't strip binaries during scons build for Ubuntu.
  - d/control: Add armhf to target archs.
  - d/p/SConscript.client.patch: fixup install of client libraries.
  - d/p/0010-install-libs-to-usr-lib-not-usr-lib64-Closes-588557.patch:
    Install libraries to lib not lib64.
* Dropped changes:
  - d/p/arm-support.patch: Included in Debian.
  - d/p/double-alignment.patch: Included in Debian.
  - d/rules,control: Debian also builds with avaliable system libraries
    now.
* Fix FTBFS due to gcc and boost upgrades in saucy:
  - d/p/0008-ignore-unused-local-typedefs.patch: Add -Wno-unused-typedefs
    to unbreak building with g++-4.8.
  - d/p/0009-boost-1.53.patch: Fixup signed/unsigned casting issue.

[ Robie Basak ]
* d/p/0011-Use-a-signed-char-to-store-BSONType-enumerations.patch: Fixup
  build failure on ARM due to missing signed'ness of char cast.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
];
13
13
var NODES = SEC_TAGS.length + 1;
14
14
 
15
 
var st = new ShardingTest({ shards: { rs0: { nodes: NODES, oplogSize: 10,
16
 
                useHostName: true }}});
17
 
var replTest = st.rs0;
18
 
var primaryNode = replTest.getMaster();
19
 
 
20
 
var setupConf = function(){
21
 
    var replConf = primaryNode.getDB( 'local' ).system.replset.findOne();
22
 
    replConf.version = (replConf.version || 0) + 1;
23
 
 
24
 
    var secIdx = 0;
25
 
    for ( var x = 0; x < NODES; x++ ){
26
 
        var node = replConf.members[x];
27
 
 
28
 
        if ( node.host == primaryNode.name ){
29
 
            node.tags = PRI_TAG;
 
15
var doTest = function(useDollarQuerySyntax) {
 
16
    var st = new ShardingTest({ shards: { rs0: { nodes: NODES, oplogSize: 10,
 
17
                    useHostName: true }}});
 
18
    var replTest = st.rs0;
 
19
    var primaryNode = replTest.getMaster();
 
20
 
 
21
    var setupConf = function(){
 
22
        var replConf = primaryNode.getDB( 'local' ).system.replset.findOne();
 
23
        replConf.version = (replConf.version || 0) + 1;
 
24
 
 
25
        var secIdx = 0;
 
26
        for ( var x = 0; x < NODES; x++ ){
 
27
            var node = replConf.members[x];
 
28
 
 
29
            if ( node.host == primaryNode.name ){
 
30
                node.tags = PRI_TAG;
 
31
            }
 
32
            else {
 
33
                node.tags = SEC_TAGS[secIdx++];
 
34
                node.priority = 0;
 
35
            }
 
36
        }
 
37
 
 
38
        try {
 
39
            primaryNode.getDB( 'admin' ).runCommand({ replSetReconfig: replConf });
 
40
        } catch (x) {
 
41
            jsTest.log('Exception expected because reconfiguring would close all conn, got ' + x);
 
42
        }
 
43
 
 
44
        return replConf;
 
45
    };
 
46
 
 
47
    var checkTag = function( nodeToCheck, tag ){
 
48
        for ( var idx = 0; idx < NODES; idx++ ){
 
49
            var node = replConf.members[idx];
 
50
 
 
51
            if ( node.host == nodeToCheck ){
 
52
                jsTest.log( 'node[' + node.host + '], Tag: ' + tojson( node['tags'] ));
 
53
                jsTest.log( 'tagToCheck: ' + tojson( tag ));
 
54
 
 
55
                var nodeTag = node['tags'];
 
56
 
 
57
                for ( var key in tag ){
 
58
                    assert.eq( tag[key], nodeTag[key] );
 
59
                }
 
60
 
 
61
                return;
 
62
            }
 
63
        }
 
64
 
 
65
        assert( false, 'node ' + nodeToCheck + ' not part of config!' );
 
66
    };
 
67
 
 
68
    var replConf = setupConf();
 
69
 
 
70
    var conn = st.s;
 
71
 
 
72
    // Wait until the ReplicaSetMonitor refreshes its view and see the tags
 
73
    ReplSetTest.awaitRSClientHosts( conn, primaryNode,
 
74
            { ok: true, tags: PRI_TAG }, replTest.name );
 
75
    replTest.awaitReplication();
 
76
 
 
77
    jsTest.log('New rs config: ' + tojson(primaryNode.getDB('local').system.replset.findOne()));
 
78
    jsTest.log( 'connpool: ' + tojson(conn.getDB('admin').runCommand({ connPoolStats: 1 })));
 
79
 
 
80
    var coll = conn.getDB( 'test' ).user;
 
81
 
 
82
    assert.soon(function() {
 
83
        coll.insert({ x: 1 });
 
84
        var err = coll.getDB().getLastError(NODES);
 
85
        if (err == null) {
 
86
            return true;
 
87
        }
 
88
        // Transient transport errors may be expected b/c of the replSetReconfig
 
89
        if (err.indexOf("transport error") == -1) {
 
90
            throw err;
 
91
        }
 
92
        return false;
 
93
    });
 
94
 
 
95
 
 
96
    var getExplain = function(readPrefMode, readPrefTags) {
 
97
        if (useDollarQuerySyntax) {
 
98
            var readPrefObj = {
 
99
                mode: readPrefMode
 
100
            };
 
101
 
 
102
            if (readPrefTags) {
 
103
                readPrefObj.tags = readPrefTags;
 
104
            }
 
105
 
 
106
            return coll.find({ $query: {}, $readPreference: readPrefObj,
 
107
                $explain: true }).limit(-1).next();
30
108
        }
31
109
        else {
32
 
            node.tags = SEC_TAGS[secIdx++];
33
 
        }
34
 
    }
35
 
 
36
 
    try {
37
 
        primaryNode.getDB( 'admin' ).runCommand({ replSetReconfig: replConf });
38
 
    } catch (x) {
39
 
        jsTest.log( 'Exception expected because reconfiguring would close all conn, got ' + x );
40
 
    }
41
 
 
42
 
    return replConf;
43
 
};
44
 
 
45
 
var replConf = setupConf();
46
 
jsTest.log( 'New rs config: ' + tojson( primaryNode.getDB( 'local' ).system.replset.findOne() ));
47
 
 
48
 
var conn = st.s;
49
 
 
50
 
// Wait until the ReplicaSetMonitor refreshes its view and see the tags
51
 
ReplSetTest.awaitRSClientHosts( conn, primaryNode,
52
 
        { ok: true, tags: PRI_TAG }, replTest.name );
53
 
 
54
 
jsTest.log( 'connpool: ' + tojson(conn.getDB('admin').runCommand({ connPoolStats: 1 })));
55
 
 
56
 
var coll = conn.getDB( 'test' ).user;
57
 
 
58
 
coll.insert({ x: 1 });
59
 
assert.eq( null, coll.getDB().getLastError( NODES ));
60
 
 
61
 
// Read pref should work without slaveOk
62
 
var explain = coll.find().readPref( "secondary" ).explain();
63
 
assert.neq( primaryNode.name, explain.server );
64
 
 
65
 
conn.setSlaveOk();
66
 
 
67
 
// It should also work with slaveOk
68
 
explain = coll.find().readPref( "secondary" ).explain();
69
 
assert.neq( primaryNode.name, explain.server );
70
 
 
71
 
// Check that $readPreference does not influence the actual query
72
 
assert.eq( 1, explain.n );
73
 
 
74
 
var checkTag = function( nodeToCheck, tag ){
75
 
    for ( var idx = 0; idx < NODES; idx++ ){
76
 
        var node = replConf.members[idx];
77
 
 
78
 
        if ( node.host == nodeToCheck ){
79
 
            jsTest.log( 'node[' + node.host + '], Tag: ' + tojson( node['tags'] ));
80
 
            jsTest.log( 'tagToCheck: ' + tojson( tag ));
81
 
 
82
 
            var nodeTag = node['tags'];
83
 
 
84
 
            for ( var key in tag ){
85
 
                assert.eq( tag[key], nodeTag[key] );
86
 
            }
87
 
 
88
 
            return;
89
 
        }
90
 
    }
91
 
 
92
 
    assert( false, 'node ' + nodeToCheck + ' not part of config!' );
93
 
};
94
 
 
95
 
explain = coll.find().readPref( "secondaryPreferred", [{ s: "2" }] ).explain();
96
 
checkTag( explain.server, { s: "2" });
97
 
assert.eq( 1, explain.n );
98
 
 
99
 
// Cannot use tags with primaryOnly
100
 
assert.throws( function() {
101
 
    coll.find().readPref( "primaryOnly", [] ).explain();
102
 
});
103
 
 
104
 
// Check that mongos will try the next tag if nothing matches the first
105
 
explain = coll.find().readPref( "secondary", [{ z: "3" }, { dc: "jp" }] ).explain();
106
 
checkTag( explain.server, { dc: "jp" });
107
 
assert.eq( 1, explain.n );
108
 
 
109
 
// Check that mongos will fallback to primary if none of tags given matches
110
 
explain = coll.find().readPref( "secondaryPreferred", [{ z: "3" }, { dc: "ph" }] ).explain();
111
 
assert.eq( primaryNode.name, explain.server );
112
 
assert.eq( 1, explain.n );
113
 
 
114
 
// Kill all members except one
115
 
var stoppedNodes = [];
116
 
for ( var x = 0; x < NODES - 1; x++ ){
117
 
    replTest.stop( x );
118
 
    stoppedNodes.push( replTest.nodes[x] );
119
 
}
120
 
 
121
 
// Wait for ReplicaSetMonitor to realize nodes are down
122
 
ReplSetTest.awaitRSClientHosts( conn, stoppedNodes, { ok: false }, replTest.name );
123
 
 
124
 
// Wait for the last node to be in steady state -> secondary (not recovering)
125
 
var lastNode = replTest.nodes[NODES - 1];
126
 
ReplSetTest.awaitRSClientHosts( conn, lastNode,
127
 
    { ok: true, secondary: true }, replTest.name );
128
 
 
129
 
jsTest.log( 'connpool: ' + tojson(conn.getDB('admin').runCommand({ connPoolStats: 1 })));
130
 
 
131
 
// Test to make sure that connection is ok, in prep for priOnly test
132
 
explain = coll.find().readPref( "nearest" ).explain();
133
 
assert.eq( explain.server, replTest.nodes[NODES - 1].name );
134
 
assert.eq( 1, explain.n );
135
 
 
136
 
// Should assert if request with priOnly but no primary
137
 
assert.throws( function(){
138
 
   coll.find().readPref( "primary" ).explain();
139
 
});
140
 
 
141
 
st.stop();
 
110
            return coll.find().readPref(readPrefMode, readPrefTags).explain();
 
111
        }
 
112
    };
 
113
 
 
114
    // Read pref should work without slaveOk
 
115
    var explain = getExplain("secondary");
 
116
    assert.neq( primaryNode.name, explain.server );
 
117
 
 
118
    conn.setSlaveOk();
 
119
 
 
120
    // It should also work with slaveOk
 
121
    explain = getExplain("secondary");
 
122
    assert.neq( primaryNode.name, explain.server );
 
123
 
 
124
    // Check that $readPreference does not influence the actual query
 
125
    assert.eq( 1, explain.n );
 
126
 
 
127
    explain = getExplain("secondaryPreferred", [{ s: "2" }]);
 
128
    checkTag( explain.server, { s: "2" });
 
129
    assert.eq( 1, explain.n );
 
130
 
 
131
    // Cannot use tags with primaryOnly
 
132
    assert.throws( function() {
 
133
        getExplain("primary", [{ s: "2" }]);
 
134
    });
 
135
 
 
136
    // Ok to use empty tags on primaryOnly
 
137
    explain = coll.find().readPref("primary", [{}]).explain();
 
138
    assert.eq(primaryNode.name, explain.server);
 
139
 
 
140
    explain = coll.find().readPref("primary", []).explain();
 
141
    assert.eq(primaryNode.name, explain.server);
 
142
 
 
143
    // Check that mongos will try the next tag if nothing matches the first
 
144
    explain = getExplain("secondary", [{ z: "3" }, { dc: "jp" }]);
 
145
    checkTag( explain.server, { dc: "jp" });
 
146
    assert.eq( 1, explain.n );
 
147
 
 
148
    // Check that mongos will fallback to primary if none of tags given matches
 
149
    explain = getExplain("secondaryPreferred", [{ z: "3" }, { dc: "ph" }]);
 
150
    // Call getPrimary again since the primary could have changed after the restart.
 
151
    assert.eq(replTest.getPrimary().name, explain.server);
 
152
    assert.eq( 1, explain.n );
 
153
 
 
154
    // Kill all members except one
 
155
    var stoppedNodes = [];
 
156
    for ( var x = 0; x < NODES - 1; x++ ){
 
157
        replTest.stop( x );
 
158
        stoppedNodes.push( replTest.nodes[x] );
 
159
    }
 
160
 
 
161
    // Wait for ReplicaSetMonitor to realize nodes are down
 
162
    ReplSetTest.awaitRSClientHosts( conn, stoppedNodes, { ok: false }, replTest.name );
 
163
 
 
164
    // Wait for the last node to be in steady state -> secondary (not recovering)
 
165
    var lastNode = replTest.nodes[NODES - 1];
 
166
    ReplSetTest.awaitRSClientHosts( conn, lastNode,
 
167
        { ok: true, secondary: true }, replTest.name );
 
168
 
 
169
    jsTest.log( 'connpool: ' + tojson(conn.getDB('admin').runCommand({ connPoolStats: 1 })));
 
170
 
 
171
    // Test to make sure that connection is ok, in prep for priOnly test
 
172
    explain = getExplain("nearest");
 
173
    assert.eq( explain.server, replTest.nodes[NODES - 1].name );
 
174
    assert.eq( 1, explain.n );
 
175
 
 
176
    // Should assert if request with priOnly but no primary
 
177
    assert.throws( function(){
 
178
       getExplain("primary");
 
179
    });
 
180
 
 
181
    st.stop();
 
182
};
 
183
 
 
184
doTest(false);
 
185
doTest(true);
142
186