2009-03-17 22:25:10 +01:00
|
|
|
// d_logic.h
|
2010-02-09 22:48:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 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/>.
|
|
|
|
*/
|
|
|
|
|
2009-03-17 22:25:10 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2010-04-27 21:33:27 +02:00
|
|
|
#include "../pch.h"
|
2010-06-15 00:44:51 +02:00
|
|
|
#include "../db/jsobj.h"
|
2010-07-16 18:30:37 +02:00
|
|
|
#include "util.h"
|
2009-03-17 22:25:10 +01:00
|
|
|
|
|
|
|
namespace mongo {
|
2010-06-15 00:44:51 +02:00
|
|
|
|
2010-06-15 23:42:49 +02:00
|
|
|
class ShardingState;
|
|
|
|
|
2010-07-16 18:30:37 +02:00
|
|
|
typedef ShardChunkVersion ConfigVersion;
|
2010-06-15 00:44:51 +02:00
|
|
|
typedef map<string,ConfigVersion> NSVersionMap;
|
|
|
|
|
2010-06-15 23:42:49 +02:00
|
|
|
// -----------
|
|
|
|
|
|
|
|
class ChunkMatcher {
|
|
|
|
public:
|
|
|
|
bool belongsToMe( const BSONObj& key , const DiskLoc& loc ) const;
|
|
|
|
|
|
|
|
private:
|
2010-10-12 19:57:24 +02:00
|
|
|
// intantiated by ShardingState only
|
|
|
|
friend class ShardingState;
|
2010-06-15 23:42:49 +02:00
|
|
|
ChunkMatcher( ConfigVersion version );
|
|
|
|
|
2010-10-12 20:41:48 +02:00
|
|
|
void addRange( const BSONObj& min , const BSONObj& max );
|
2010-06-15 23:42:49 +02:00
|
|
|
|
2010-10-12 19:57:24 +02:00
|
|
|
// highest ShardChunkVersion for which this ChunkMatcher's information is accurate
|
|
|
|
const ConfigVersion _version;
|
|
|
|
|
2010-10-12 20:41:48 +02:00
|
|
|
// key pattern for chunks under this range
|
2010-07-14 18:30:26 +02:00
|
|
|
BSONObj _key;
|
2010-10-12 19:57:24 +02:00
|
|
|
|
|
|
|
// a map from a min key into a range or continguous chunks
|
|
|
|
typedef map<BSONObj,pair<BSONObj,BSONObj>,BSONObjCmp> RangeMap;
|
|
|
|
RangeMap _map;
|
2010-06-15 23:42:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef shared_ptr<ChunkMatcher> ChunkMatcherPtr;
|
|
|
|
|
2010-06-15 00:44:51 +02:00
|
|
|
// --------------
|
|
|
|
// --- global state ---
|
|
|
|
// --------------
|
|
|
|
|
|
|
|
class ShardingState {
|
|
|
|
public:
|
|
|
|
ShardingState();
|
|
|
|
|
|
|
|
bool enabled() const { return _enabled; }
|
|
|
|
const string& getConfigServer() const { return _configServer; }
|
|
|
|
void enable( const string& server );
|
|
|
|
|
2010-06-15 16:49:47 +02:00
|
|
|
void gotShardName( const string& name );
|
2010-10-11 02:00:09 +02:00
|
|
|
void gotShardHost( string host );
|
2010-06-15 00:44:51 +02:00
|
|
|
|
2010-06-15 16:49:47 +02:00
|
|
|
bool hasVersion( const string& ns );
|
|
|
|
bool hasVersion( const string& ns , ConfigVersion& version );
|
2010-06-15 00:44:51 +02:00
|
|
|
ConfigVersion& getVersion( const string& ns ); // TODO: this is dangeroues
|
|
|
|
void setVersion( const string& ns , const ConfigVersion& version );
|
|
|
|
|
2010-06-15 16:49:47 +02:00
|
|
|
void appendInfo( BSONObjBuilder& b );
|
|
|
|
|
2010-06-16 05:05:45 +02:00
|
|
|
ChunkMatcherPtr getChunkMatcher( const string& ns );
|
2010-06-15 23:42:49 +02:00
|
|
|
|
2010-07-16 05:04:36 +02:00
|
|
|
bool inCriticalMigrateSection();
|
2010-06-15 00:44:51 +02:00
|
|
|
private:
|
|
|
|
|
|
|
|
bool _enabled;
|
2010-06-15 16:49:47 +02:00
|
|
|
|
2010-06-15 00:44:51 +02:00
|
|
|
string _configServer;
|
2010-06-15 16:49:47 +02:00
|
|
|
|
|
|
|
string _shardName;
|
|
|
|
string _shardHost;
|
|
|
|
|
2010-10-12 19:57:24 +02:00
|
|
|
// protects state below
|
2010-06-15 16:49:47 +02:00
|
|
|
mongo::mutex _mutex;
|
2010-10-12 19:57:24 +02:00
|
|
|
|
|
|
|
// map from a namespace into the highest ShardChunkVersion for that collection
|
2010-06-15 00:44:51 +02:00
|
|
|
NSVersionMap _versions;
|
2010-10-12 19:57:24 +02:00
|
|
|
|
2010-10-27 23:37:37 +02:00
|
|
|
// map from a namespace into the ensemble of chunk ranges that are stored in this mongod
|
2010-06-15 23:42:49 +02:00
|
|
|
map<string,ChunkMatcherPtr> _chunks;
|
2010-06-15 00:44:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
extern ShardingState shardingState;
|
2009-03-17 22:25:10 +01:00
|
|
|
|
2010-09-14 16:05:58 +02:00
|
|
|
/**
|
|
|
|
* one per connection from mongos
|
|
|
|
* holds version state for each namesapce
|
|
|
|
*/
|
2010-06-15 00:44:51 +02:00
|
|
|
class ShardedConnectionInfo {
|
|
|
|
public:
|
|
|
|
ShardedConnectionInfo();
|
|
|
|
|
|
|
|
const OID& getID() const { return _id; }
|
|
|
|
bool hasID() const { return _id.isSet(); }
|
|
|
|
void setID( const OID& id );
|
|
|
|
|
|
|
|
ConfigVersion& getVersion( const string& ns ); // TODO: this is dangeroues
|
|
|
|
void setVersion( const string& ns , const ConfigVersion& version );
|
|
|
|
|
|
|
|
static ShardedConnectionInfo* get( bool create );
|
2010-07-25 14:33:03 +02:00
|
|
|
static void reset();
|
2010-06-15 00:44:51 +02:00
|
|
|
|
2010-09-14 16:05:58 +02:00
|
|
|
bool inForceVersionOkMode() const {
|
|
|
|
return _forceVersionOk;
|
2010-07-16 18:30:37 +02:00
|
|
|
}
|
|
|
|
|
2010-09-14 16:05:58 +02:00
|
|
|
void enterForceVersionOkMode(){ _forceVersionOk = true; }
|
|
|
|
void leaveForceVersionOkMode(){ _forceVersionOk = false; }
|
2010-07-16 18:30:37 +02:00
|
|
|
|
2010-06-15 00:44:51 +02:00
|
|
|
private:
|
|
|
|
|
|
|
|
OID _id;
|
|
|
|
NSVersionMap _versions;
|
2010-09-14 16:05:58 +02:00
|
|
|
bool _forceVersionOk; // if this is true, then chunk version #s aren't check, and all ops are allowed
|
2010-06-15 00:44:51 +02:00
|
|
|
|
|
|
|
static boost::thread_specific_ptr<ShardedConnectionInfo> _tl;
|
|
|
|
};
|
2010-07-16 18:30:37 +02:00
|
|
|
|
2010-09-14 16:05:58 +02:00
|
|
|
struct ShardForceVersionOkModeBlock {
|
|
|
|
ShardForceVersionOkModeBlock(){
|
2010-07-16 18:30:37 +02:00
|
|
|
info = ShardedConnectionInfo::get( false );
|
|
|
|
if ( info )
|
2010-09-14 16:05:58 +02:00
|
|
|
info->enterForceVersionOkMode();
|
2010-07-16 18:30:37 +02:00
|
|
|
}
|
2010-09-14 16:05:58 +02:00
|
|
|
~ShardForceVersionOkModeBlock(){
|
2010-07-16 18:30:37 +02:00
|
|
|
if ( info )
|
2010-09-14 16:05:58 +02:00
|
|
|
info->leaveForceVersionOkMode();
|
2010-07-16 18:30:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ShardedConnectionInfo * info;
|
|
|
|
};
|
2010-06-15 00:44:51 +02:00
|
|
|
|
2010-06-11 23:16:58 +02:00
|
|
|
// -----------------
|
|
|
|
// --- core ---
|
|
|
|
// -----------------
|
2010-06-11 21:12:25 +02:00
|
|
|
|
2010-06-11 23:16:58 +02:00
|
|
|
unsigned long long extractVersion( BSONElement e , string& errmsg );
|
|
|
|
|
|
|
|
|
2009-09-10 16:36:11 +02:00
|
|
|
/**
|
|
|
|
* @return true if we have any shard info for the ns
|
|
|
|
*/
|
|
|
|
bool haveLocalShardingInfo( const string& ns );
|
|
|
|
|
2009-03-17 22:25:10 +01:00
|
|
|
/**
|
|
|
|
* @return true if the current threads shard version is ok, or not in sharded version
|
|
|
|
*/
|
2010-08-25 05:59:51 +02:00
|
|
|
bool shardVersionOk( const string& ns , bool write , string& errmsg );
|
2009-03-17 22:25:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return true if we took care of the message and nothing else should be done
|
|
|
|
*/
|
2010-07-23 19:41:21 +02:00
|
|
|
bool handlePossibleShardedMessage( Message &m, DbResponse * dbresponse );
|
2010-06-11 23:16:58 +02:00
|
|
|
|
2010-07-15 23:15:15 +02:00
|
|
|
void logOpForSharding( const char * opstr , const char * ns , const BSONObj& obj , BSONObj * patt );
|
2010-09-22 20:56:10 +02:00
|
|
|
void aboutToDeleteForSharding( const Database* db , const DiskLoc& dl );
|
2010-07-15 23:15:15 +02:00
|
|
|
|
2009-03-17 22:25:10 +01:00
|
|
|
}
|