summaryrefslogtreecommitdiff
path: root/jstests/sharding/features2.js
blob: 4b9843a724cc0f50bd79cda7137454d26610719d (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
188
189
190
191
192
193
194
195
196
// features2.js

s = new ShardingTest( "features2" , 2 , 1 , 1 );

// The counts and the tests for "on-num-shards" only works for previous assumptions in balancer 
// behavior and assumes migrations do not occur during count() commands.
s.stopBalancer()

s.adminCommand( { enablesharding : "test" } );

a = s._connections[0].getDB( "test" );
b = s._connections[1].getDB( "test" );

db = s.getDB( "test" );

// ---- distinct ----

db.foo.save( { x : 1 } );
db.foo.save( { x : 2 } );
db.foo.save( { x : 3 } );
db.foo.ensureIndex( { x : 1 } );

assert.eq( "1,2,3" , db.foo.distinct( "x" ) , "distinct 1" );
assert( a.foo.distinct("x").length == 3 || b.foo.distinct("x").length == 3 , "distinct 2" );
assert( a.foo.distinct("x").length == 0 || b.foo.distinct("x").length == 0 , "distinct 3" );

assert.eq( 1 , s.onNumShards( "foo" ) , "A1" );

s.shardGo( "foo" , { x : 1 } , { x : 2 } , { x : 3 }, null, true /* waitForDelete */ );

assert.eq( 2 , s.onNumShards( "foo" ) , "A2" );

assert.eq( "1,2,3" , db.foo.distinct( "x" ) , "distinct 4" );

// ----- delete ---

assert.eq( 3 , db.foo.count() , "D1" );

db.foo.remove( { x : 3 } );
assert.eq( 2 , db.foo.count() , "D2" );

db.foo.save( { x : 3 } );
assert.eq( 3 , db.foo.count() , "D3" );

db.foo.remove( { x : { $gt : 2 } } );
assert.eq( 2 , db.foo.count() , "D4" );

db.foo.remove( { x : { $gt : -1 } } );
assert.eq( 0 , db.foo.count() , "D5" );

db.foo.save( { x : 1 } );
db.foo.save( { x : 2 } );
db.foo.save( { x : 3 } );
assert.eq( 3 , db.foo.count() , "D6" );
db.foo.remove( {} );
assert.eq( 0 , db.foo.count() , "D7" );

// --- _id key ---

db.foo2.save( { _id : new ObjectId() } );
db.foo2.save( { _id : new ObjectId() } );
db.foo2.save( { _id : new ObjectId() } );

assert.eq( 1 , s.onNumShards( "foo2" ) , "F1" );

printjson( db.system.indexes.find( { ns : "test.foo2" } ).toArray() );
s.adminCommand( { shardcollection : "test.foo2" , key : { _id : 1 } } );

assert.eq( 3 , db.foo2.count() , "F2" )
db.foo2.insert( {} );
assert.eq( 4 , db.foo2.count() , "F3" )

// --- map/reduce

db.mr.save( { x : 1 , tags : [ "a" , "b" ] } );
db.mr.save( { x : 2 , tags : [ "b" , "c" ] } );
db.mr.save( { x : 3 , tags : [ "c" , "a" ] } );
db.mr.save( { x : 4 , tags : [ "b" , "c" ] } );
db.mr.ensureIndex( { x : 1 } );

m = function(){
    this.tags.forEach(
        function(z){
            emit( z , { count : 1 } );
        }
    );
};

r = function( key , values ){
    var total = 0;
    for ( var i=0; i<values.length; i++ ){
        total += values[i].count;
    }
    return { count : total };
};

doMR = function( n ){
    print(n);
    
    // on-disk
    
    var res = db.mr.mapReduce( m , r , "smr1_out" );
    printjson( res );
    assert.eq( 4 , res.counts.input , "MR T0 " + n );

    var x = db[res.result];
    assert.eq( 3 , x.find().count() , "MR T1 " + n );
    
    var z = {};
    x.find().forEach( function(a){ z[a._id] = a.value.count; } );
    assert.eq( 3 , Object.keySet( z ).length , "MR T2 " + n );
    assert.eq( 2 , z.a , "MR T3 " + n );
    assert.eq( 3 , z.b , "MR T4 " + n );
    assert.eq( 3 , z.c , "MR T5 " + n );

    x.drop();

    // inline

    var res = db.mr.mapReduce( m , r , { out : { inline : 1 } } );
    printjson( res );
    assert.eq( 4 , res.counts.input , "MR T6 " + n );

    var z = {};
    res.find().forEach( function(a){ z[a._id] = a.value.count; } );
    printjson( z );
    assert.eq( 3 , Object.keySet( z ).length , "MR T7 " + n ) ;
    assert.eq( 2 , z.a , "MR T8 " + n );
    assert.eq( 3 , z.b , "MR T9 " + n );
    assert.eq( 3 , z.c , "MR TA " + n );

}

doMR( "before" );

assert.eq( 1 , s.onNumShards( "mr" ) , "E1" );
s.shardGo( "mr" , { x : 1 } , { x : 2 } , { x : 3 }, null, true /* waitForDelete */ );
assert.eq( 2 , s.onNumShards( "mr" ) , "E1" );

doMR( "after" );

s.adminCommand({split:'test.mr' , middle:{x:3}} );
s.adminCommand({split:'test.mr' , middle:{x:4}} );
s.adminCommand({movechunk:'test.mr', find:{x:3}, to: s.getServer('test').name } );

doMR( "after extra split" );

cmd = { mapreduce : "mr" , map : "emit( " , reduce : "fooz + " , out : "broken1" };

x = db.runCommand( cmd );
y = s._connections[0].getDB( "test" ).runCommand( cmd );

printjson( x )
printjson( y )

// count

db.countaa.save({"regex" : /foo/i})
db.countaa.save({"regex" : /foo/i})
db.countaa.save({"regex" : /foo/i})
assert.eq( 3 , db.countaa.count() , "counta1" );
assert.eq( 3 , db.countaa.find().itcount() , "counta1" );

x = null; y = null;
try {
    x = db.runCommand( "forceerror" )
}
catch ( e ){
    x = e;
}
try {
    y = s._connections[0].getDB( "test" ).runCommand( "forceerror" );
}
catch ( e ){
    y = e;
}

assert.eq( x.code , y.code , "assert format" )
assert.eq( x.errmsg , y.errmsg , "assert format" )
assert.eq( x.ok , y.ok , "assert format" )

// isMaster and query-wrapped-command
isMaster = db.runCommand({isMaster:1});
assert( isMaster.ismaster );
assert.eq( 'isdbgrid', isMaster.msg );
delete isMaster.localTime;

im2 = db.runCommand({query: {isMaster:1}});
delete im2.localTime;
assert.eq( isMaster, im2 );

im2 = db.runCommand({$query: {isMaster:1}});
delete im2.localTime;
assert.eq( isMaster, im2 );

s.stop();