0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/s/chunk.h

240 lines
6.4 KiB
C
Raw Normal View History

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
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>
#undef assert
#define assert xassert
2008-09-15 15:14:42 +02:00
2009-01-14 23:09:51 +01:00
namespace mongo {
class DBConfig;
class ChunkManager;
class ChunkObjUnitTest;
typedef unsigned long long ShardChunkVersion;
/**
config.chunks
{ ns : "alleyinsider.fs.chunks" , min : {} , max : {} , server : "localhost:30001" }
x is in a shard iff
min <= x < max
*/
class Chunk : public Model , boost::noncopyable {
2009-02-17 20:41:31 +01:00
public:
2009-03-27 21:55:26 +01: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
string getShard() const{
return _shard;
2009-02-17 20:41:31 +01:00
}
void setShard( string shard );
2009-02-20 19:46:57 +01:00
bool contains( const BSONObj& obj ) const;
string toString() const;
2009-02-20 16:48:32 +01:00
operator string() const { return toString(); }
bool operator==(const Chunk& s) const;
2009-03-30 20:33:40 +02:00
bool operator!=(const Chunk& s) const{
return ! ( *this == s );
}
2009-02-20 19:46:57 +01:00
void getFilter( BSONObjBuilder& b ) const;
BSONObj getFilter() const{ BSONObjBuilder b; getFilter( b ); return b.obj(); }
BSONObj pickSplitPoint() const;
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
*/
long getPhysicalSize() const;
long countObjects( const BSONObj& filter = BSONObj() ) const;
2009-04-20 23:42:01 +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
*/
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 );
virtual const char * getNS(){ return "config.chunks"; }
virtual void serialize(BSONObjBuilder& to);
2009-02-27 16:37:13 +01:00
virtual void unserialize(const BSONObj& from);
virtual string modelServer();
2009-03-27 21:55:26 +01:00
virtual void save( bool check=false );
void ensureIndex();
2009-03-27 21:55:26 +01:00
void _markModified();
2009-02-20 19:46:57 +01:00
static long MaxChunkSize;
2009-04-16 04:26:19 +02:00
private:
2009-02-27 18:51:49 +01:00
// main shard info
ChunkManager * _manager;
ShardKeyPattern skey() const;
2009-04-20 23:42:01 +02:00
string _ns;
BSONObj _min;
BSONObj _max;
string _shard;
ShardChunkVersion _lastmod;
2009-02-27 18:51:49 +01:00
bool _modified;
// 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
friend class ChunkManager;
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 } ]
}
*/
class ChunkManager {
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 );
virtual ~ChunkManager();
2009-02-17 20:41:31 +01:00
string getns(){
return _ns;
}
int numChunks(){ return _chunks.size(); }
Chunk* getChunk( int i ){ return _chunks[i]; }
2009-02-20 16:46:42 +01:00
bool hasShardKey( const BSONObj& obj );
Chunk& findChunk( const BSONObj& obj );
Chunk* findChunkOnServer( const string& server ) const;
2009-03-27 21:55:26 +01:00
ShardKeyPattern& getShardKey(){ return _key; }
2009-09-03 22:48:34 +02:00
bool isUnique(){ return _unique; }
/**
* makes sure the shard index is on all servers
*/
void ensureIndex();
2009-03-27 21:55:26 +01:00
/**
* @return number of Chunk added to the vector
*/
int getChunksForQuery( vector<Chunk*>& chunks , const BSONObj& query );
2009-12-02 22:36:46 +01:00
void getAllServers( set<string>& allServers );
void save();
2009-02-19 18:55:01 +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
ShardChunkVersion getVersion( const string& server ) const;
ShardChunkVersion getVersion() const;
2009-03-30 16:50:10 +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;
}
void drop();
2009-03-30 16:50:10 +02:00
2009-02-17 20:41:31 +01:00
private:
DBConfig * _config;
2009-02-17 20:41:31 +01:00
string _ns;
ShardKeyPattern _key;
2009-09-03 22:48:34 +02:00
bool _unique;
vector<Chunk*> _chunks;
map<string,unsigned long long> _maxMarkers;
2009-03-30 16:50:10 +02:00
unsigned long long _sequenceNumber;
friend class Chunk;
2009-03-30 16:50:10 +02:00
static unsigned long long NextSequenceNumber;
};
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