0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 00:56:44 +01:00
mongodb/s/shard.h

298 lines
7.9 KiB
C
Raw Normal View History

2010-09-12 19:01:05 +02:00
// @file shard.h
2010-04-19 22:55:16 +02:00
/**
* 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
2010-04-27 21:27:52 +02:00
#include "../pch.h"
2010-04-19 22:55:16 +02:00
#include "../client/connpool.h"
namespace mongo {
class ShardConnection;
2010-05-03 17:18:27 +02:00
class ShardStatus;
2010-09-12 19:01:05 +02:00
/*
* A "shard" is a database (replica pair typically) which represents
* one partition of the overall database.
*/
2010-04-19 22:55:16 +02:00
class Shard {
public:
Shard()
2011-01-04 06:40:41 +01:00
: _name("") , _addr("") , _maxSize(0) , _isDraining( false ) {
}
Shard( const string& name , const string& addr, long long maxSize = 0 , bool isDraining = false )
2011-01-04 06:40:41 +01:00
: _name(name) , _addr( addr ) , _maxSize( maxSize ) , _isDraining( isDraining ) {
_setAddr( addr );
2010-04-19 22:55:16 +02:00
}
2011-01-04 06:40:41 +01:00
Shard( const string& ident ) {
2010-05-05 17:02:46 +02:00
reset( ident );
}
2010-04-19 22:55:16 +02:00
Shard( const Shard& other )
: _name( other._name ) , _addr( other._addr ) , _cs( other._cs ) ,
_maxSize( other._maxSize ) , _isDraining( other._isDraining ) , _rs( other._rs ) {
2010-04-19 22:55:16 +02:00
}
Shard( const Shard* other )
: _name( other->_name ) , _addr( other->_addr ), _cs( other->_cs ) ,
_maxSize( other->_maxSize ) , _isDraining( other->_isDraining ) , _rs( other->_rs ) {
}
2011-01-04 06:40:41 +01:00
static Shard make( const string& ident ) {
Shard s;
s.reset( ident );
return s;
}
/**
* @param ident either name or address
*/
void reset( const string& ident );
2011-01-04 06:40:41 +01:00
2011-02-14 20:16:18 +01:00
void setAddress( const ConnectionString& cs );
2011-02-17 07:10:50 +01:00
ConnectionString getAddress() const { return _cs; }
string getName() const {
assert( _name.size() );
return _name;
2010-04-19 22:55:16 +02:00
}
2011-01-04 06:40:41 +01:00
2010-04-19 22:55:16 +02:00
string getConnString() const {
assert( _addr.size() );
return _addr;
}
long long getMaxSize() const {
return _maxSize;
}
bool isDraining() const {
return _isDraining;
}
2010-04-19 22:55:16 +02:00
string toString() const {
return _name + ":" + _addr;
2010-04-19 22:55:16 +02:00
}
2010-04-28 20:32:56 +02:00
friend ostream& operator << (ostream& out, const Shard& s) {
return (out << s.toString());
}
2010-04-19 22:55:16 +02:00
bool operator==( const Shard& s ) const {
bool n = _name == s._name;
bool a = _addr == s._addr;
2011-01-04 06:40:41 +01:00
assert( n == a ); // names and address are 1 to 1
return n;
2010-04-19 22:55:16 +02:00
}
bool operator!=( const Shard& s ) const {
bool n = _name == s._name;
bool a = _addr == s._addr;
return ! ( n && a );
2010-04-19 22:55:16 +02:00
}
bool operator==( const string& s ) const {
return _name == s || _addr == s;
}
2011-01-04 06:40:41 +01:00
bool operator!=( const string& s ) const {
return _name != s && _addr != s;
2010-04-19 22:55:16 +02:00
}
bool operator<(const Shard& o) const {
return _name < o._name;
2010-04-19 22:55:16 +02:00
}
2011-01-04 06:40:41 +01:00
bool ok() const {
return _addr.size() > 0 && _addr.size() > 0;
}
2011-01-04 06:40:41 +01:00
BSONObj runCommand( const string& db , const string& simple ) const {
return runCommand( db , BSON( simple << 1 ) );
}
BSONObj runCommand( const string& db , const BSONObj& cmd ) const ;
2011-01-04 06:40:41 +01:00
ShardStatus getStatus() const ;
/**
* mostly for replica set
* retursn true if node is the shard
* of if the replica set contains node
*/
bool containsNode( const string& node ) const;
2011-01-04 06:40:41 +01:00
2010-05-03 17:18:27 +02:00
static void getAllShards( vector<Shard>& all );
static void printShardInfo( ostream& out );
2010-05-03 17:18:27 +02:00
/**
* @parm current - shard where the chunk/database currently lives in
* @return the currently emptiest shard, if best then current, or EMPTY
2010-05-03 17:18:27 +02:00
*/
static Shard pick( const Shard& current = EMPTY );
2011-01-04 06:40:41 +01:00
2010-05-03 17:18:27 +02:00
static void reloadShardInfo();
static void removeShard( const string& name );
static bool isAShardNode( const string& ident );
2010-04-19 22:55:16 +02:00
static Shard EMPTY;
2010-04-19 22:55:16 +02:00
private:
2011-02-14 20:16:18 +01:00
void _rsInit();
void _setAddr( const string& addr );
string _name;
string _addr;
ConnectionString _cs;
2011-01-04 06:40:41 +01:00
long long _maxSize; // in MBytes, 0 is unlimited
bool _isDraining; // shard is currently being removed
ReplicaSetMonitorPtr _rs;
2010-04-19 22:55:16 +02:00
};
2010-05-03 17:18:27 +02:00
class ShardStatus {
public:
2011-01-04 06:40:41 +01:00
ShardStatus( const Shard& shard , const BSONObj& obj );
friend ostream& operator << (ostream& out, const ShardStatus& s) {
out << s.toString();
return out;
}
string toString() const {
stringstream ss;
2011-01-04 06:40:41 +01:00
ss << "shard: " << _shard << " mapped: " << _mapped << " writeLock: " << _writeLock;
return ss.str();
2010-07-07 00:17:39 +02:00
}
2011-01-04 06:40:41 +01:00
bool operator<( const ShardStatus& other ) const {
return _mapped < other._mapped;
}
2011-01-04 06:40:41 +01:00
Shard shard() const {
return _shard;
}
long long mapped() const {
return _mapped;
}
bool hasOpsQueued() const {
return _hasOpsQueued;
}
2010-05-03 17:18:27 +02:00
private:
Shard _shard;
2010-05-03 17:18:27 +02:00
long long _mapped;
bool _hasOpsQueued; // true if 'writebacks' are pending
2010-05-03 17:18:27 +02:00
double _writeLock;
};
class ShardConnection : public AScopedConnection {
2010-04-19 22:55:16 +02:00
public:
ShardConnection( const Shard * s , const string& ns, bool ignoreDirect = false );
ShardConnection( const Shard& s , const string& ns, bool ignoreDirect = false );
ShardConnection( const string& addr , const string& ns, bool ignoreDirect = false );
2010-04-19 22:55:16 +02:00
~ShardConnection();
void done();
void kill();
2011-01-04 06:40:41 +01:00
DBClientBase& conn() {
_finishInit();
assert( _conn );
return *_conn;
2010-04-19 22:55:16 +02:00
}
2011-01-04 06:40:41 +01:00
DBClientBase* operator->() {
_finishInit();
assert( _conn );
return _conn;
}
2011-01-04 06:40:41 +01:00
DBClientBase* get() {
_finishInit();
assert( _conn );
return _conn;
}
string getHost() const {
return _addr;
2010-04-19 22:55:16 +02:00
}
2010-05-20 19:36:29 +02:00
bool setVersion() {
_finishInit();
return _setVersion;
}
2010-05-20 19:36:29 +02:00
static void sync();
2011-01-04 06:40:41 +01:00
void donotCheckVersion() {
_setVersion = false;
_finishedInit = true;
}
bool ok() const { return _conn > 0; }
/**
this just passes through excpet it checks for stale configs
*/
bool runCommand( const string& db , const BSONObj& cmd , BSONObj& res );
/** checks all of my thread local connections for the version of this ns */
static void checkMyConnectionVersions( const string & ns );
2011-01-04 06:40:41 +01:00
2010-04-19 22:55:16 +02:00
private:
void _init( bool ignoreDirect = false );
void _finishInit();
2011-01-04 06:40:41 +01:00
bool _finishedInit;
string _addr;
2010-05-20 19:36:29 +02:00
string _ns;
DBClientBase* _conn;
bool _setVersion;
2010-04-19 22:55:16 +02:00
};
extern DBConnectionPool shardConnectionPool;
2011-06-22 20:22:09 +02:00
class ShardingConnectionHook : public DBConnectionHook {
public:
ShardingConnectionHook( bool shardedConnections )
: _shardedConnections( shardedConnections ) {
}
virtual void onCreate( DBClientBase * conn );
virtual void onHandedOut( DBClientBase * conn );
virtual void onDestory( DBClientBase * conn );
bool _shardedConnections;
};
2010-04-19 22:55:16 +02:00
}