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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
|
// @file chunk.h
/**
* Copyright (C) 2008 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/>.
*/
#pragma once
#include "../pch.h"
#include "../bson/util/atomic_int.h"
#include "../client/dbclient.h"
#include "../client/distlock.h"
#include "shardkey.h"
#include "shard.h"
#include "util.h"
namespace mongo {
class DBConfig;
class Chunk;
class ChunkRange;
class ChunkManager;
class ChunkRangeMangager;
class ChunkObjUnitTest;
typedef shared_ptr<const Chunk> ChunkPtr;
// key is max for each Chunk or ChunkRange
typedef map<BSONObj,ChunkPtr,BSONObjCmp> ChunkMap;
typedef map<BSONObj,shared_ptr<ChunkRange>,BSONObjCmp> ChunkRangeMap;
typedef shared_ptr<const ChunkManager> ChunkManagerPtr;
/**
config.chunks
{ ns : "alleyinsider.fs.chunks" , min : {} , max : {} , server : "localhost:30001" }
x is in a shard iff
min <= x < max
*/
class Chunk : boost::noncopyable {
public:
Chunk( const ChunkManager * info , BSONObj from);
Chunk( const ChunkManager * info , const BSONObj& min, const BSONObj& max, const Shard& shard);
//
// serialization support
//
void serialize(BSONObjBuilder& to, ShardChunkVersion myLastMod=0);
//
// chunk boundary support
//
const BSONObj& getMin() const { return _min; }
const BSONObj& getMax() const { return _max; }
// if min/max key is pos/neg infinity
bool minIsInf() const;
bool maxIsInf() const;
bool contains( const BSONObj& obj ) const;
string genID() const;
static string genID( const string& ns , const BSONObj& min );
//
// chunk version support
//
void appendShortVersion( const char * name , BSONObjBuilder& b ) const;
ShardChunkVersion getLastmod() const { return _lastmod; }
void setLastmod( ShardChunkVersion v ) { _lastmod = v; }
//
// split support
//
/**
* if the amount of data written nears the max size of a shard
* then we check the real size, and if its too big, we split
* @return if something was split
*/
bool splitIfShould( long dataWritten ) const;
/**
* Splits this chunk at a non-specificed split key to be chosen by the mongod holding this chunk.
*
* @param force if set to true, will split the chunk regardless if the split is really necessary size wise
* if set to false, will only split if the chunk has reached the currently desired maximum size
* @param res the object containing details about the split execution
* @return splitPoint if found a key and split successfully, else empty BSONObj
*/
BSONObj singleSplit( bool force , BSONObj& res ) const;
/**
* Splits this chunk at the given key (or keys)
*
* @param splitPoints the vector of keys that should be used to divide this chunk
* @param res the object containing details about the split execution
* @return if the split was successful
*/
bool multiSplit( const vector<BSONObj>& splitPoints , BSONObj& res ) const;
/**
* Asks the mongod holding this chunk to find a key that approximately divides this chunk in two
*
* @param medianKey the key that divides this chunk, if there is one, or empty
*/
void pickMedianKey( BSONObj& medianKey ) const;
/**
* @param splitPoints vector to be filled in
* @param chunkSize chunk size to target in bytes
* @param maxPoints limits the number of split points that are needed, zero is max (optional)
* @param maxObjs limits the number of objects in each chunk, zero is as max (optional)
*/
void pickSplitVector( vector<BSONObj>& splitPoints , int chunkSize , int maxPoints = 0, int maxObjs = 0) const;
//
// migration support
//
/**
* Issues a migrate request for this chunk
*
* @param to shard to move this chunk to
* @param chunSize maximum number of bytes beyond which the migrate should no go trhough
* @param res the object containing details about the migrate execution
* @return true if move was successful
*/
bool moveAndCommit( const Shard& to , long long chunkSize , BSONObj& res ) const;
/**
* @return size of shard in bytes
* talks to mongod to do this
*/
long getPhysicalSize() const;
//
// public constants
//
static string chunkMetadataNS;
static int MaxChunkSize;
static int MaxObjectPerChunk;
//
// accessors and helpers
//
string toString() const;
friend ostream& operator << (ostream& out, const Chunk& c) { return (out << c.toString()); }
bool operator==(const Chunk& s) const;
bool operator!=(const Chunk& s) const { return ! ( *this == s ); }
string getns() const;
const char * getNS() { return "config.chunks"; }
Shard getShard() const { return _shard; }
const ChunkManager* getManager() const { return _manager; }
private:
// main shard info
const ChunkManager * _manager;
BSONObj _min;
BSONObj _max;
Shard _shard;
ShardChunkVersion _lastmod;
// transient stuff
mutable long _dataWritten;
// methods, etc..
/**
* if sort 1, return lowest key
* if sort -1, return highest key
* will return empty object if have none
*/
BSONObj _getExtremeKey( int sort ) const;
/** initializes _dataWritten with a random value so that a mongos restart wouldn't cause delay in splitting */
static long mkDataWritten();
ShardKeyPattern skey() const;
};
class ChunkRange {
public:
const ChunkManager* getManager() const { return _manager; }
Shard getShard() const { return _shard; }
const BSONObj& getMin() const { return _min; }
const BSONObj& getMax() const { return _max; }
// clones of Chunk methods
bool contains(const BSONObj& obj) const;
ChunkRange(ChunkMap::const_iterator begin, const ChunkMap::const_iterator end)
: _manager(begin->second->getManager())
, _shard(begin->second->getShard())
, _min(begin->second->getMin())
, _max(prior(end)->second->getMax()) {
assert( begin != end );
DEV while (begin != end) {
assert(begin->second->getManager() == _manager);
assert(begin->second->getShard() == _shard);
++begin;
}
}
// Merge min and max (must be adjacent ranges)
ChunkRange(const ChunkRange& min, const ChunkRange& max)
: _manager(min.getManager())
, _shard(min.getShard())
, _min(min.getMin())
, _max(max.getMax()) {
assert(min.getShard() == max.getShard());
assert(min.getManager() == max.getManager());
assert(min.getMax() == max.getMin());
}
friend ostream& operator<<(ostream& out, const ChunkRange& cr) {
return (out << "ChunkRange(min=" << cr._min << ", max=" << cr._max << ", shard=" << cr._shard <<")");
}
private:
const ChunkManager* _manager;
const Shard _shard;
const BSONObj _min;
const BSONObj _max;
};
class ChunkRangeManager {
public:
const ChunkRangeMap& ranges() const { return _ranges; }
void clear() { _ranges.clear(); }
void reloadAll(const ChunkMap& chunks);
// Slow operation -- wrap with DEV
void assertValid() const;
ChunkRangeMap::const_iterator upper_bound(const BSONObj& o) const { return _ranges.upper_bound(o); }
ChunkRangeMap::const_iterator lower_bound(const BSONObj& o) const { return _ranges.lower_bound(o); }
private:
// assumes nothing in this range exists in _ranges
void _insertRange(ChunkMap::const_iterator begin, const ChunkMap::const_iterator end);
ChunkRangeMap _ranges;
};
/* config.sharding
{ ns: 'alleyinsider.fs.chunks' ,
key: { ts : 1 } ,
shards: [ { min: 1, max: 100, server: a } , { min: 101, max: 200 , server : b } ]
}
*/
class ChunkManager {
public:
typedef map<Shard,ShardChunkVersion> ShardVersionMap;
ChunkManager( string ns , ShardKeyPattern pattern , bool unique );
string getns() const { return _ns; }
int numChunks() const { return _chunkMap.size(); }
bool hasShardKey( const BSONObj& obj ) const;
void createFirstChunks( const Shard& shard ) const; // only call from DBConfig::shardCollection
ChunkPtr findChunk( const BSONObj& obj ) const;
ChunkPtr findChunkOnServer( const Shard& shard ) const;
const ShardKeyPattern& getShardKey() const { return _key; }
bool isUnique() const { return _unique; }
void getShardsForQuery( set<Shard>& shards , const BSONObj& query ) const;
void getAllShards( set<Shard>& all ) const;
void getShardsForRange(set<Shard>& shards, const BSONObj& min, const BSONObj& max) const; // [min, max)
string toString() const;
ShardChunkVersion getVersion( const Shard& shard ) const;
ShardChunkVersion getVersion() const;
/**
* this is just an increasing number of how many ChunkManagers we have so we know if something has been updated
*/
unsigned long long getSequenceNumber() const { return _sequenceNumber; }
void getInfo( BSONObjBuilder& b ) const {
b.append( "key" , _key.key() );
b.appendBool( "unique" , _unique );
}
/**
* @param me - so i don't get deleted before i'm done
*/
void drop( ChunkManagerPtr me ) const;
void _printChunks() const;
int getCurrentDesiredChunkSize() const;
private:
ChunkManagerPtr reload(bool force=true) const; // doesn't modify self!
// helpers for constructor
void _load(ChunkMap& chunks, set<Shard>& shards, ShardVersionMap& shardVersions);
static bool _isValid(const ChunkMap& chunks);
// All members should be const for thread-safety
const string _ns;
const ShardKeyPattern _key;
const bool _unique;
const ChunkMap _chunkMap;
const ChunkRangeManager _chunkRanges;
const set<Shard> _shards;
const ShardVersionMap _shardVersions; // max version per shard
ShardChunkVersion _version; // max version of any chunk
mutable mutex _mutex; // only used with _nsLock
mutable DistributedLock _nsLock;
const unsigned long long _sequenceNumber;
mutable TicketHolder _splitTickets; // number of concurrent splitVector we can do from a splitIfShould per collection
friend class Chunk;
friend class ChunkRangeManager; // only needed for CRM::assertValid()
static AtomicUInt NextSequenceNumber;
};
// like BSONObjCmp. for use as an STL comparison functor
// key-order in "order" argument must match key-order in shardkey
class ChunkCmp {
public:
ChunkCmp( const BSONObj &order = BSONObj() ) : _cmp( order ) {}
bool operator()( const Chunk &l, const Chunk &r ) const {
return _cmp(l.getMin(), r.getMin());
}
bool operator()( const ptr<Chunk> l, const ptr<Chunk> r ) const {
return operator()(*l, *r);
}
// Also support ChunkRanges
bool operator()( const ChunkRange &l, const ChunkRange &r ) const {
return _cmp(l.getMin(), r.getMin());
}
bool operator()( const shared_ptr<ChunkRange> l, const shared_ptr<ChunkRange> r ) const {
return operator()(*l, *r);
}
private:
BSONObjCmp _cmp;
};
/*
struct chunk_lock {
chunk_lock( const Chunk* c ){
}
Chunk _c;
};
*/
inline string Chunk::genID() const { return genID(_manager->getns(), _min); }
bool setShardVersion( DBClientBase & conn , const string& ns , ShardChunkVersion version , bool authoritative , BSONObj& result );
} // namespace mongo
|