2009-02-17 20:41:31 +01:00
|
|
|
// shard.h
|
2008-12-29 02:28:49 +01:00
|
|
|
|
2009-02-17 20:41:31 +01:00
|
|
|
/*
|
2008-11-09 23:49:37 +01:00
|
|
|
A "shard" is a database (replica pair typically) which represents
|
|
|
|
one partition of the overall database.
|
|
|
|
*/
|
2008-09-15 15:14:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright (C) 2008 10gen Inc.
|
2008-12-29 02:28:49 +01:00
|
|
|
*
|
2008-09-15 15:14:42 +02:00
|
|
|
* 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.
|
2008-12-29 02:28:49 +01:00
|
|
|
*
|
2008-09-15 15:14:42 +02:00
|
|
|
* 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.
|
2008-12-29 02:28:49 +01:00
|
|
|
*
|
2008-09-15 15:14:42 +02:00
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-11-09 23:49:37 +01:00
|
|
|
#pragma once
|
2008-10-19 16:02:37 +02:00
|
|
|
|
2009-02-19 23:32:19 +01:00
|
|
|
#include "../stdafx.h"
|
2008-11-09 23:49:37 +01:00
|
|
|
#include "../client/dbclient.h"
|
|
|
|
#include "../client/model.h"
|
2009-02-17 20:41:31 +01:00
|
|
|
#include "shardkey.h"
|
2009-02-19 23:32:19 +01:00
|
|
|
#include <boost/utility.hpp>
|
2009-05-15 23:27:31 +02:00
|
|
|
#undef assert
|
|
|
|
#define assert xassert
|
2008-09-15 15:14:42 +02:00
|
|
|
|
2009-01-14 23:09:51 +01:00
|
|
|
namespace mongo {
|
|
|
|
|
2009-02-18 05:31:27 +01:00
|
|
|
class DBConfig;
|
2009-08-31 22:31:50 +02:00
|
|
|
class ChunkManager;
|
|
|
|
class ChunkObjUnitTest;
|
2009-02-26 18:01:24 +01:00
|
|
|
|
2009-08-31 22:31:50 +02:00
|
|
|
typedef unsigned long long ShardChunkVersion;
|
2009-04-22 21:56:23 +02:00
|
|
|
|
2009-02-26 18:01:24 +01:00
|
|
|
/**
|
2009-08-31 22:31:50 +02:00
|
|
|
config.chunks
|
2009-02-26 18:01:24 +01:00
|
|
|
{ ns : "alleyinsider.fs.chunks" , min : {} , max : {} , server : "localhost:30001" }
|
2009-04-22 21:56:23 +02:00
|
|
|
|
|
|
|
x is in a shard iff
|
|
|
|
min <= x < max
|
2009-02-26 18:01:24 +01:00
|
|
|
*/
|
2009-08-31 22:31:50 +02:00
|
|
|
class Chunk : public Model , boost::noncopyable {
|
2009-02-17 20:41:31 +01:00
|
|
|
public:
|
2009-03-27 21:55:26 +01:00
|
|
|
|
2009-08-31 22:31:50 +02:00
|
|
|
Chunk( ChunkManager * info );
|
2009-02-17 20:41:31 +01:00
|
|
|
|
2009-11-02 21:16:29 +01:00
|
|
|
const BSONObj& getMin() const { return _min; }
|
|
|
|
const BSONObj& getMax() const { return _max; }
|
2009-11-02 21:37:15 +01:00
|
|
|
|
|
|
|
void setMin(const BSONObj& o){
|
|
|
|
_min = o;
|
|
|
|
}
|
|
|
|
void setMax(const BSONObj& o){
|
|
|
|
_max = o;
|
|
|
|
}
|
2009-02-20 19:46:57 +01:00
|
|
|
|
2010-02-09 18:20:10 +01:00
|
|
|
string getShard() const{
|
2009-09-01 17:09:43 +02:00
|
|
|
return _shard;
|
2009-02-17 20:41:31 +01:00
|
|
|
}
|
2009-09-01 17:09:43 +02:00
|
|
|
void setShard( string shard );
|
2009-02-20 19:46:57 +01:00
|
|
|
|
2010-02-09 18:20:10 +01:00
|
|
|
bool contains( const BSONObj& obj ) const;
|
2009-02-18 05:31:27 +01:00
|
|
|
|
2009-02-19 04:17:30 +01:00
|
|
|
string toString() const;
|
2009-02-20 16:48:32 +01:00
|
|
|
operator string() const { return toString(); }
|
2009-02-18 05:31:27 +01:00
|
|
|
|
2010-02-09 18:20:10 +01:00
|
|
|
bool operator==(const Chunk& s) const;
|
2009-03-30 20:33:40 +02:00
|
|
|
|
2010-02-09 18:20:10 +01:00
|
|
|
bool operator!=(const Chunk& s) const{
|
2009-02-18 05:31:27 +01:00
|
|
|
return ! ( *this == s );
|
|
|
|
}
|
2009-02-20 19:46:57 +01:00
|
|
|
|
2010-02-09 18:20:10 +01:00
|
|
|
void getFilter( BSONObjBuilder& b ) const;
|
|
|
|
BSONObj getFilter() const{ BSONObjBuilder b; getFilter( b ); return b.obj(); }
|
2009-04-22 21:56:23 +02:00
|
|
|
|
2009-02-18 05:31:27 +01:00
|
|
|
|
2010-02-09 18:20:10 +01:00
|
|
|
BSONObj pickSplitPoint() const;
|
2009-08-31 22:31:50 +02:00
|
|
|
Chunk * split();
|
|
|
|
Chunk * split( const BSONObj& middle );
|
2009-04-16 04:26:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return size of shard in bytes
|
|
|
|
* talks to mongod to do this
|
|
|
|
*/
|
2010-02-09 18:20:10 +01:00
|
|
|
long getPhysicalSize() const;
|
2009-04-16 16:19:52 +02:00
|
|
|
|
2010-02-09 18:20:10 +01:00
|
|
|
long countObjects( const BSONObj& filter = BSONObj() ) const;
|
2009-04-20 23:42:01 +02:00
|
|
|
|
2009-04-16 16:19:52 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
bool splitIfShould( long dataWritten );
|
|
|
|
|
2009-04-20 23:42:01 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* moves either this shard or newShard if it makes sense too
|
|
|
|
* @return whether or not a shard was moved
|
|
|
|
*/
|
2009-08-31 22:31:50 +02:00
|
|
|
bool moveIfShould( Chunk * newShard = 0 );
|
2009-04-20 23:42:01 +02:00
|
|
|
|
2009-04-03 19:52:06 +02:00
|
|
|
bool moveAndCommit( const string& to , string& errmsg );
|
2009-02-26 18:01:24 +01:00
|
|
|
|
2009-09-01 17:09:43 +02:00
|
|
|
virtual const char * getNS(){ return "config.chunks"; }
|
2009-02-26 18:01:24 +01:00
|
|
|
virtual void serialize(BSONObjBuilder& to);
|
2009-02-27 16:37:13 +01:00
|
|
|
virtual void unserialize(const BSONObj& from);
|
2009-02-26 18:01:24 +01:00
|
|
|
virtual string modelServer();
|
|
|
|
|
2009-03-27 21:55:26 +01:00
|
|
|
virtual void save( bool check=false );
|
2009-03-30 23:13:55 +02:00
|
|
|
|
|
|
|
void ensureIndex();
|
|
|
|
|
2009-03-27 21:55:26 +01:00
|
|
|
void _markModified();
|
2009-02-20 19:46:57 +01:00
|
|
|
|
2010-02-12 21:27:43 +01:00
|
|
|
static int MaxChunkSize;
|
2009-04-16 04:26:19 +02:00
|
|
|
|
2009-02-26 18:01:24 +01:00
|
|
|
private:
|
2009-02-27 18:51:49 +01:00
|
|
|
|
2009-04-16 16:19:52 +02:00
|
|
|
// main shard info
|
|
|
|
|
2009-08-31 22:31:50 +02:00
|
|
|
ChunkManager * _manager;
|
2010-02-09 18:20:10 +01:00
|
|
|
ShardKeyPattern skey() const;
|
2009-04-20 23:42:01 +02:00
|
|
|
|
2009-02-26 18:01:24 +01:00
|
|
|
string _ns;
|
2009-12-10 00:55:59 +01:00
|
|
|
BSONObj _min;
|
|
|
|
BSONObj _max;
|
2009-09-01 17:09:43 +02:00
|
|
|
string _shard;
|
2009-08-31 22:31:50 +02:00
|
|
|
ShardChunkVersion _lastmod;
|
2009-02-18 05:31:27 +01:00
|
|
|
|
2009-02-27 18:51:49 +01:00
|
|
|
bool _modified;
|
|
|
|
|
2009-04-16 16:19:52 +02:00
|
|
|
// transient stuff
|
|
|
|
|
|
|
|
long _dataWritten;
|
|
|
|
|
|
|
|
// methods, etc..
|
|
|
|
|
2009-02-17 21:34:52 +01:00
|
|
|
void _split( BSONObj& middle );
|
2009-02-20 19:46:57 +01:00
|
|
|
|
2009-08-31 22:31:50 +02:00
|
|
|
friend class ChunkManager;
|
2009-02-26 18:01:24 +01:00
|
|
|
friend class ShardObjUnitTest;
|
2009-02-17 20:41:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* config.sharding
|
|
|
|
{ ns: 'alleyinsider.fs.chunks' ,
|
2009-02-17 21:34:52 +01:00
|
|
|
key: { ts : 1 } ,
|
2009-02-17 20:41:31 +01:00
|
|
|
shards: [ { min: 1, max: 100, server: a } , { min: 101, max: 200 , server : b } ]
|
2009-01-15 16:17:11 +01:00
|
|
|
}
|
|
|
|
*/
|
2009-08-31 22:31:50 +02:00
|
|
|
class ChunkManager {
|
2009-01-15 16:17:11 +01:00
|
|
|
public:
|
2009-02-17 20:41:31 +01:00
|
|
|
|
2009-09-03 22:48:34 +02:00
|
|
|
ChunkManager( DBConfig * config , string ns , ShardKeyPattern pattern , bool unique );
|
2009-08-31 22:31:50 +02:00
|
|
|
virtual ~ChunkManager();
|
2009-02-18 05:31:27 +01:00
|
|
|
|
2009-02-17 20:41:31 +01:00
|
|
|
string getns(){
|
|
|
|
return _ns;
|
|
|
|
}
|
2009-02-18 05:31:27 +01:00
|
|
|
|
2009-08-31 22:31:50 +02:00
|
|
|
int numChunks(){ return _chunks.size(); }
|
2009-09-01 22:30:20 +02:00
|
|
|
Chunk* getChunk( int i ){ return _chunks[i]; }
|
2009-02-20 16:46:42 +01:00
|
|
|
bool hasShardKey( const BSONObj& obj );
|
|
|
|
|
2009-08-31 22:31:50 +02:00
|
|
|
Chunk& findChunk( const BSONObj& obj );
|
|
|
|
Chunk* findChunkOnServer( const string& server ) const;
|
2009-03-27 21:55:26 +01:00
|
|
|
|
2009-02-25 22:46:10 +01:00
|
|
|
ShardKeyPattern& getShardKey(){ return _key; }
|
2009-09-03 22:48:34 +02:00
|
|
|
bool isUnique(){ return _unique; }
|
2009-02-19 19:26:25 +01:00
|
|
|
|
2009-03-30 23:13:55 +02:00
|
|
|
/**
|
|
|
|
* makes sure the shard index is on all servers
|
|
|
|
*/
|
|
|
|
void ensureIndex();
|
2009-03-27 21:55:26 +01:00
|
|
|
|
2009-02-22 05:39:41 +01:00
|
|
|
/**
|
2009-08-31 22:31:50 +02:00
|
|
|
* @return number of Chunk added to the vector
|
2009-02-22 05:39:41 +01:00
|
|
|
*/
|
2009-08-31 22:31:50 +02:00
|
|
|
int getChunksForQuery( vector<Chunk*>& chunks , const BSONObj& query );
|
2009-02-22 05:39:41 +01:00
|
|
|
|
2009-12-02 22:36:46 +01:00
|
|
|
void getAllServers( set<string>& allServers );
|
|
|
|
|
2009-02-26 18:01:24 +01:00
|
|
|
void save();
|
2009-02-19 18:55:01 +01:00
|
|
|
|
2009-02-19 04:17:30 +01:00
|
|
|
string toString() const;
|
2009-02-20 16:48:32 +01:00
|
|
|
operator string() const { return toString(); }
|
2009-03-25 22:35:38 +01:00
|
|
|
|
2009-08-31 22:31:50 +02:00
|
|
|
ShardChunkVersion getVersion( const string& server ) const;
|
|
|
|
ShardChunkVersion getVersion() const;
|
2009-03-30 16:50:10 +02:00
|
|
|
|
|
|
|
/**
|
2009-08-31 22:31:50 +02:00
|
|
|
* this is just an increasing number of how many ChunkManagers we have so we know if something has been updated
|
2009-03-30 16:50:10 +02:00
|
|
|
*/
|
|
|
|
unsigned long long getSequenceNumber(){
|
|
|
|
return _sequenceNumber;
|
|
|
|
}
|
2009-11-09 18:42:20 +01:00
|
|
|
|
|
|
|
void drop();
|
2009-03-30 16:50:10 +02:00
|
|
|
|
2009-02-17 20:41:31 +01:00
|
|
|
private:
|
2009-02-18 05:31:27 +01:00
|
|
|
DBConfig * _config;
|
2009-02-17 20:41:31 +01:00
|
|
|
string _ns;
|
2009-02-25 22:46:10 +01:00
|
|
|
ShardKeyPattern _key;
|
2009-09-03 22:48:34 +02:00
|
|
|
bool _unique;
|
2009-02-26 18:01:24 +01:00
|
|
|
|
2009-08-31 22:31:50 +02:00
|
|
|
vector<Chunk*> _chunks;
|
2009-11-06 19:57:01 +01:00
|
|
|
map<string,unsigned long long> _maxMarkers;
|
|
|
|
|
2009-03-30 16:50:10 +02:00
|
|
|
unsigned long long _sequenceNumber;
|
|
|
|
|
2009-08-31 22:31:50 +02:00
|
|
|
friend class Chunk;
|
2009-03-30 16:50:10 +02:00
|
|
|
static unsigned long long NextSequenceNumber;
|
2009-01-15 16:17:11 +01:00
|
|
|
};
|
2009-01-14 23:09:51 +01:00
|
|
|
|
2010-02-09 18:20:56 +01:00
|
|
|
// 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 Chunk *l, const Chunk *r ) const {
|
|
|
|
return operator()(*l, *r);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
BSONObjCmp _cmp;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-01-14 23:09:51 +01:00
|
|
|
} // namespace mongo
|