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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
//@file chunktests.cpp : s/chunk.{h,cpp} tests
/**
* Copyright (C) 2012 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the GNU Affero General Public License in all respects
* for all of the code used other than as permitted herein. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you do not
* wish to do so, delete this exception statement from your version. If you
* delete this exception statement from all source files in the program,
* then also delete it in the license file.
*/
#include "mongo/platform/basic.h"
#include "mongo/db/json.h"
#include "mongo/dbtests/dbtests.h"
#include "mongo/s/chunk_manager.h"
namespace mongo {
using std::set;
using std::string;
using std::vector;
class TestableChunkManager : public ChunkManager {
public:
TestableChunkManager(const string& ns, const ShardKeyPattern& keyPattern, bool unique)
: ChunkManager(ns, keyPattern, unique) {
}
void setSingleChunkForShards( const vector<BSONObj> &splitPoints ) {
ChunkMap &chunkMap = const_cast<ChunkMap&>( _chunkMap );
ChunkRangeManager &chunkRanges = const_cast<ChunkRangeManager&>( _chunkRanges );
set<Shard> &shards = const_cast<set<Shard>&>( _shards );
vector<BSONObj> mySplitPoints( splitPoints );
mySplitPoints.insert( mySplitPoints.begin(), _keyPattern.getKeyPattern().globalMin() );
mySplitPoints.push_back( _keyPattern.getKeyPattern().globalMax() );
for( unsigned i = 1; i < mySplitPoints.size(); ++i ) {
string name = str::stream() << (i-1);
Shard shard(name,
name,
0 /* maxSize */,
false /* draining */);
shards.insert( shard );
ChunkPtr chunk( new Chunk( this, mySplitPoints[ i-1 ], mySplitPoints[ i ],
shard ) );
chunkMap[ mySplitPoints[ i ] ] = chunk;
}
chunkRanges.reloadAll( chunkMap );
}
};
} // namespace mongo
namespace ChunkTests {
namespace ChunkManagerTests {
typedef mongo::TestableChunkManager ChunkManager;
class Base {
public:
virtual ~Base() {}
void run() {
ShardKeyPattern shardKeyPattern(shardKey());
ChunkManager chunkManager("", shardKeyPattern, false);
chunkManager.setSingleChunkForShards( splitPointsVector() );
set<Shard> shards;
chunkManager.getShardsForQuery( shards, query() );
BSONArrayBuilder b;
for( set<Shard>::const_iterator i = shards.begin(); i != shards.end(); ++i ) {
b << i->getName();
}
ASSERT_EQUALS( expectedShardNames(), b.arr() );
}
protected:
virtual BSONObj shardKey() const { return BSON( "a" << 1 ); }
virtual BSONArray splitPoints() const { return BSONArray(); }
virtual BSONObj query() const { return BSONObj(); }
virtual BSONArray expectedShardNames() const { return BSON_ARRAY( "0" ); }
virtual vector<BSONObj> splitPointsVector() const {
vector<BSONObj> ret;
BSONArray a = splitPoints();
BSONObjIterator i( a );
while( i.more() ) {
ret.push_back( i.next().Obj().getOwned() );
}
return ret;
}
};
class EmptyQuerySingleShard : public Base {
};
class MultiShardBase : public Base {
virtual BSONArray splitPoints() const {
return BSON_ARRAY( BSON( "a" << "x" ) << BSON( "a" << "y" ) << BSON( "a" << "z" ) );
}
};
class EmptyQueryMultiShard : public MultiShardBase {
virtual BSONArray expectedShardNames() const {
return BSON_ARRAY( "0" << "1" << "2" << "3" );
}
};
class UniversalRangeMultiShard : public EmptyQueryMultiShard {
virtual BSONObj query() const { return BSON( "b" << 1 ); }
};
class EqualityRangeSingleShard : public EmptyQuerySingleShard {
virtual BSONObj query() const { return BSON( "a" << "x" ); }
};
class EqualityRangeMultiShard : public MultiShardBase {
virtual BSONObj query() const { return BSON( "a" << "y" ); }
virtual BSONArray expectedShardNames() const { return BSON_ARRAY( "2" ); }
};
class SetRangeMultiShard : public MultiShardBase {
virtual BSONObj query() const { return fromjson( "{a:{$in:['u','y']}}" ); }
virtual BSONArray expectedShardNames() const { return BSON_ARRAY( "0" << "2" ); }
};
class GTRangeMultiShard : public MultiShardBase {
virtual BSONObj query() const { return BSON( "a" << GT << "x" ); }
virtual BSONArray expectedShardNames() const { return BSON_ARRAY( "1" << "2" << "3" ); }
};
class GTERangeMultiShard : public MultiShardBase {
virtual BSONObj query() const { return BSON( "a" << GTE << "x" ); }
virtual BSONArray expectedShardNames() const { return BSON_ARRAY( "1" << "2" << "3" ); }
};
class LTRangeMultiShard : public MultiShardBase {
virtual BSONObj query() const { return BSON( "a" << LT << "y" ); }
/**
* It isn't actually necessary to return shard 2 because its lowest key is "y", which
* is excluded from the query. SERVER-4791
*/
virtual BSONArray expectedShardNames() const { return BSON_ARRAY( "0" << "1" << "2" ); }
};
class LTERangeMultiShard : public MultiShardBase {
virtual BSONObj query() const { return BSON( "a" << LTE << "y" ); }
virtual BSONArray expectedShardNames() const { return BSON_ARRAY( "0" << "1" << "2" ); }
};
class OrEqualities : public MultiShardBase {
virtual BSONObj query() const { return fromjson( "{$or:[{a:'u'},{a:'y'}]}" ); }
virtual BSONArray expectedShardNames() const { return BSON_ARRAY( "0" << "2" ); }
};
class OrEqualityInequality : public MultiShardBase {
virtual BSONObj query() const { return fromjson( "{$or:[{a:'u'},{a:{$gte:'y'}}]}" ); }
virtual BSONArray expectedShardNames() const { return BSON_ARRAY( "0" << "2" << "3" ); }
};
class OrEqualityInequalityUnhelpful : public MultiShardBase {
virtual BSONObj query() const {
return fromjson( "{$or:[{a:'u'},{a:{$gte:'zz'}},{}]}" );
}
virtual BSONArray expectedShardNames() const {
return BSON_ARRAY( "0" << "1" << "2" << "3" );
}
};
template<class BASE>
class Unsatisfiable : public BASE {
/**
* SERVER-4914 For now the first shard is returned for unsatisfiable queries, as some
* clients of getShardsForQuery() expect at least one shard.
*/
virtual BSONArray expectedShardNames() const {
return BSON_ARRAY( "0" ) /* BSONArray() */;
}
};
class UnsatisfiableRangeSingleShard : public Unsatisfiable<Base> {
virtual BSONObj query() const { return BSON( "a" << GT << "x" << LT << "x" ); }
};
class UnsatisfiableRangeMultiShard : public Unsatisfiable<MultiShardBase> {
virtual BSONObj query() const { return BSON( "a" << GT << "x" << LT << "x" ); }
};
class EqualityThenUnsatisfiable : public Unsatisfiable<Base> {
virtual BSONObj shardKey() const { return BSON( "a" << 1 << "b" << 1 ); }
virtual BSONObj query() const { return BSON( "a" << 1 << "b" << GT << 4 << LT << 4 ); }
};
class InequalityThenUnsatisfiable : public Unsatisfiable<Base> {
virtual BSONObj shardKey() const { return BSON( "a" << 1 << "b" << 1 ); }
virtual BSONObj query() const {
return BSON( "a" << GT << 1 << "b" << GT << 4 << LT << 4 );
}
};
class OrEqualityUnsatisfiableInequality : public MultiShardBase {
virtual BSONObj query() const {
return fromjson( "{$or:[{a:'x'},{a:{$gt:'u',$lt:'u'}},{a:{$gte:'y'}}]}" );
}
virtual BSONArray expectedShardNames() const { return BSON_ARRAY( "1" << "2" << "3" ); }
};
class CompoundKeyBase : public Base {
virtual BSONObj shardKey() const {
return BSON( "a" << 1 << "b" << 1 );
}
virtual BSONArray splitPoints() const {
return BSON_ARRAY( BSON( "a" << 5 << "b" << 10 ) << BSON ( "a" << 5 << "b" << 20 ) );
}
};
class InMultiShard : public CompoundKeyBase {
virtual BSONObj query() const {
return BSON( "a" << BSON( "$in" << BSON_ARRAY( 0 << 5 << 10 ) ) <<
"b" << BSON( "$in" << BSON_ARRAY( 0 << 5 << 25 ) ) );
}
// If we were to send this query to just the shards it actually needed to hit, it would only hit shards 0 and 2
// Because of the optimization from SERVER-4745, however, we'll also hit shard 1.
virtual BSONArray expectedShardNames() const {
return BSON_ARRAY( "0" << "1" << "2" );
}
};
} // namespace ChunkManagerTests
class All : public Suite {
public:
All() : Suite( "chunk" ) {
}
void setupTests() {
add<ChunkManagerTests::EmptyQuerySingleShard>();
add<ChunkManagerTests::EmptyQueryMultiShard>();
add<ChunkManagerTests::UniversalRangeMultiShard>();
add<ChunkManagerTests::EqualityRangeSingleShard>();
add<ChunkManagerTests::EqualityRangeMultiShard>();
add<ChunkManagerTests::SetRangeMultiShard>();
add<ChunkManagerTests::GTRangeMultiShard>();
add<ChunkManagerTests::GTERangeMultiShard>();
add<ChunkManagerTests::LTRangeMultiShard>();
add<ChunkManagerTests::LTERangeMultiShard>();
add<ChunkManagerTests::OrEqualities>();
add<ChunkManagerTests::OrEqualityInequality>();
add<ChunkManagerTests::OrEqualityInequalityUnhelpful>();
add<ChunkManagerTests::UnsatisfiableRangeSingleShard>();
add<ChunkManagerTests::UnsatisfiableRangeMultiShard>();
add<ChunkManagerTests::EqualityThenUnsatisfiable>();
add<ChunkManagerTests::InequalityThenUnsatisfiable>();
add<ChunkManagerTests::OrEqualityUnsatisfiableInequality>();
add<ChunkManagerTests::InMultiShard>();
}
};
SuiteInstance<All> myAll;
} // namespace ChunkTests
|