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

178 lines
5.3 KiB
C
Raw Normal View History

2009-03-17 22:25:10 +01:00
// d_logic.h
/*
* 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"
#include "util.h"
2009-03-17 22:25:10 +01:00
namespace mongo {
2010-06-15 00:44:51 +02:00
class ShardingState;
typedef ShardChunkVersion ConfigVersion;
2010-06-15 00:44:51 +02:00
typedef map<string,ConfigVersion> NSVersionMap;
// -----------
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;
ChunkMatcher( ConfigVersion version );
2010-10-12 20:41:48 +02:00
void addRange( const BSONObj& min , const BSONObj& max );
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
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;
};
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 );
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 );
ChunkMatcherPtr getChunkMatcher( const string& ns );
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
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 );
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-09-14 16:05:58 +02:00
void enterForceVersionOkMode(){ _forceVersionOk = true; }
void leaveForceVersionOkMode(){ _forceVersionOk = false; }
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-09-14 16:05:58 +02:00
struct ShardForceVersionOkModeBlock {
ShardForceVersionOkModeBlock(){
info = ShardedConnectionInfo::get( false );
if ( info )
2010-09-14 16:05:58 +02:00
info->enterForceVersionOkMode();
}
2010-09-14 16:05:58 +02:00
~ShardForceVersionOkModeBlock(){
if ( info )
2010-09-14 16:05:58 +02:00
info->leaveForceVersionOkMode();
}
ShardedConnectionInfo * info;
};
2010-06-15 00:44:51 +02:00
2010-06-11 23:16:58 +02:00
// -----------------
// --- core ---
// -----------------
2010-06-11 23:16:58 +02:00
unsigned long long extractVersion( BSONElement e , string& errmsg );
/**
* @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
*/
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
*/
bool handlePossibleShardedMessage( Message &m, DbResponse * dbresponse );
2010-06-11 23:16:58 +02:00
void logOpForSharding( const char * opstr , const char * ns , const BSONObj& obj , BSONObj * patt );
void aboutToDeleteForSharding( const Database* db , const DiskLoc& dl );
2009-03-17 22:25:10 +01:00
}