diff options
Diffstat (limited to 's/util.h')
-rw-r--r-- | s/util.h | 120 |
1 files changed, 106 insertions, 14 deletions
@@ -18,35 +18,127 @@ #pragma once -#include "../stdafx.h" +#include "../pch.h" #include "../client/dbclient.h" +#include "../db/jsobj.h" /** some generic sharding utils that can be used in mongod or mongos */ namespace mongo { + + struct ShardChunkVersion { + union { + struct { + int _minor; + int _major; + }; + unsigned long long _combined; + }; + + ShardChunkVersion( int major=0, int minor=0 ) + : _minor(minor),_major(major){ + } + + ShardChunkVersion( unsigned long long ll ) + : _combined( ll ){ + } + + ShardChunkVersion( const BSONElement& e ){ + if ( e.type() == Date || e.type() == Timestamp ){ + _combined = e._numberLong(); + } + else if ( e.eoo() ){ + _combined = 0; + } + else { + log() << "ShardChunkVersion can't handle type (" << (int)(e.type()) << ") " << e << endl; + assert(0); + } + } + + ShardChunkVersion incMajor() const { + return ShardChunkVersion( _major + 1 , 0 ); + } + + void operator++(){ + _minor++; + } + + unsigned long long toLong() const { + return _combined; + } + + bool isSet() const { + return _combined > 0; + } + + string toString() const { + stringstream ss; + ss << _major << "|" << _minor; + return ss.str(); + } + operator unsigned long long() const { return _combined; } + + ShardChunkVersion& operator=( const BSONElement& elem ){ + switch ( elem.type() ){ + case Timestamp: + case NumberLong: + case Date: + _combined = elem._numberLong(); + break; + case EOO: + _combined = 0; + break; + default: + assert(0); + } + return *this; + } + }; + inline ostream& operator<<( ostream &s , const ShardChunkVersion& v){ + s << v._major << "|" << v._minor; + return s; + } + /** - your config info for a given shard/chunk is out of date */ - class StaleConfigException : public std::exception { + * your config info for a given shard/chunk is out of date + */ + class StaleConfigException : public AssertionException { public: - StaleConfigException( const string& ns , const string& msg){ - stringstream s; - s << "StaleConfigException ns: " << ns << " " << msg; - _msg = s.str(); - log(1) << _msg << endl; + StaleConfigException( const string& ns , const string& raw , bool justConnection = false ) + : AssertionException( (string)"ns: " + ns + " " + raw , 9996 ) , + _justConnection(justConnection) , + _ns(ns){ } - + virtual ~StaleConfigException() throw(){} - virtual const char* what() const throw(){ - return _msg.c_str(); + virtual void appendPrefix( stringstream& ss ) const { ss << "StaleConfigException: "; } + + bool justConnection() const { return _justConnection; } + + string getns() const { return _ns; } + + static bool parse( const string& big , string& ns , string& raw ){ + string::size_type start = big.find( '[' ); + if ( start == string::npos ) + return false; + string::size_type end = big.find( ']' ,start ); + if ( end == string::npos ) + return false; + + ns = big.substr( start + 1 , ( end - start ) - 1 ); + raw = big.substr( end + 1 ); + return true; } private: - string _msg; + bool _justConnection; + string _ns; }; - void checkShardVersion( DBClientBase & conn , const string& ns , bool authoritative = false ); - + bool checkShardVersion( DBClientBase & conn , const string& ns , bool authoritative = false , int tryNumber = 1 ); + void resetShardVersion( DBClientBase * conn ); } |