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>
|
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-02-26 18:01:24 +01:00
|
|
|
class ShardManager;
|
|
|
|
class ShardObjUnitTest;
|
|
|
|
|
|
|
|
/**
|
|
|
|
config.shard
|
|
|
|
{ ns : "alleyinsider.fs.chunks" , min : {} , max : {} , server : "localhost:30001" }
|
|
|
|
*/
|
|
|
|
class Shard : public Model , boost::noncopyable {
|
2009-02-17 20:41:31 +01:00
|
|
|
public:
|
|
|
|
|
2009-02-18 05:31:27 +01:00
|
|
|
BSONObj& getMin(){
|
|
|
|
return _min;
|
2009-02-17 20:41:31 +01:00
|
|
|
}
|
2009-02-18 05:31:27 +01:00
|
|
|
BSONObj& getMax(){
|
|
|
|
return _max;
|
2009-02-17 20:41:31 +01:00
|
|
|
}
|
2009-02-20 19:46:57 +01:00
|
|
|
|
2009-02-17 20:41:31 +01:00
|
|
|
string getServer(){
|
2009-02-20 19:46:57 +01:00
|
|
|
return _server;
|
2009-02-17 20:41:31 +01:00
|
|
|
}
|
2009-02-20 19:46:57 +01:00
|
|
|
void setServer( string server );
|
|
|
|
|
2009-02-18 05:31:27 +01:00
|
|
|
bool contains( const BSONObj& obj );
|
|
|
|
|
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
|
|
|
|
|
|
|
bool operator==(const Shard& s);
|
|
|
|
|
|
|
|
bool operator!=(const Shard& s){
|
|
|
|
return ! ( *this == s );
|
|
|
|
}
|
2009-02-20 19:46:57 +01:00
|
|
|
|
|
|
|
void getFilter( BSONObjBuilder& b );
|
2009-02-18 05:31:27 +01:00
|
|
|
|
2009-02-26 18:01:24 +01:00
|
|
|
Shard * split();
|
|
|
|
Shard * split( const BSONObj& middle );
|
|
|
|
|
|
|
|
virtual const char * getNS(){ return "config.shard"; }
|
|
|
|
virtual void serialize(BSONObjBuilder& to);
|
|
|
|
virtual void unserialize(BSONObj& from);
|
|
|
|
virtual string modelServer();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Shard( ShardManager * info );
|
2009-02-20 19:46:57 +01:00
|
|
|
|
2009-02-26 18:01:24 +01:00
|
|
|
private:
|
|
|
|
ShardManager * _manager;
|
2009-02-20 19:46:57 +01:00
|
|
|
|
2009-02-26 18:01:24 +01:00
|
|
|
string _ns;
|
2009-02-18 05:31:27 +01:00
|
|
|
BSONObj _min;
|
|
|
|
BSONObj _max;
|
2009-02-20 19:46:57 +01:00
|
|
|
string _server;
|
|
|
|
|
|
|
|
bool _modified;
|
2009-02-18 05:31:27 +01:00
|
|
|
|
2009-02-17 21:34:52 +01:00
|
|
|
void _split( BSONObj& middle );
|
2009-02-20 19:46:57 +01:00
|
|
|
|
2009-02-26 18:01:24 +01:00
|
|
|
friend class ShardManager;
|
|
|
|
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-02-26 18:01:24 +01:00
|
|
|
class ShardManager {
|
2009-01-15 16:17:11 +01:00
|
|
|
public:
|
2009-02-17 20:41:31 +01:00
|
|
|
|
2009-02-26 18:01:24 +01:00
|
|
|
ShardManager( DBConfig * config , string ns ,ShardKeyPattern pattern );
|
|
|
|
virtual ~ShardManager();
|
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-02-19 19:26:25 +01:00
|
|
|
int numShards(){ return _shards.size(); }
|
2009-02-20 16:46:42 +01:00
|
|
|
bool hasShardKey( const BSONObj& obj );
|
|
|
|
|
|
|
|
Shard& findShard( const BSONObj& obj );
|
2009-02-25 22:46:10 +01:00
|
|
|
ShardKeyPattern& getShardKey(){ return _key; }
|
2009-02-19 19:26:25 +01:00
|
|
|
|
2009-02-22 05:39:41 +01:00
|
|
|
/**
|
|
|
|
* @return number of shards added to the vector
|
|
|
|
*/
|
|
|
|
int getShardsForQuery( vector<Shard*>& shards , const BSONObj& query );
|
|
|
|
|
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-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-02-26 18:01:24 +01:00
|
|
|
|
2009-02-18 05:31:27 +01:00
|
|
|
vector<Shard*> _shards;
|
|
|
|
|
|
|
|
friend class Shard;
|
2009-01-15 16:17:11 +01:00
|
|
|
};
|
2009-01-14 23:09:51 +01:00
|
|
|
|
|
|
|
} // namespace mongo
|