1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
// @file shard_version.cpp
/**
* 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/>.
*/
#include "pch.h"
#include "chunk.h"
#include "config.h"
#include "grid.h"
#include "util.h"
#include "shard.h"
#include "writeback_listener.h"
#include "shard_version.h"
namespace mongo {
// when running in sharded mode, use chunk shard version control
static bool isVersionable( DBClientBase * conn );
static bool initShardVersion( DBClientBase & conn, BSONObj& result );
static bool checkShardVersion( DBClientBase & conn , const string& ns , bool authoritative = false , int tryNumber = 1 );
static void resetShardVersion( DBClientBase * conn );
void installChunkShardVersioning() {
//
// Overriding no-op behavior in shardconnection.cpp
//
// TODO: Better encapsulate this mechanism.
//
isVersionableCB = isVersionable;
initShardVersionCB = initShardVersion;
checkShardVersionCB = checkShardVersion;
resetShardVersionCB = resetShardVersion;
}
struct ConnectionShardStatus {
typedef unsigned long long S;
ConnectionShardStatus()
: _mutex( "ConnectionShardStatus" ) {
}
bool isInitialized( DBClientBase * conn ){
scoped_lock lk( _mutex );
return _init.find( conn ) != _init.end();
}
void setInitialized( DBClientBase * conn ){
// At this point, conn may be deleted, *do not access*
scoped_lock lk( _mutex );
_init.insert( conn );
}
S getSequence( DBClientBase * conn , const string& ns ) {
scoped_lock lk( _mutex );
return _map[conn][ns];
}
void setSequence( DBClientBase * conn , const string& ns , const S& s ) {
scoped_lock lk( _mutex );
_map[conn][ns] = s;
}
void reset( DBClientBase * conn ) {
scoped_lock lk( _mutex );
_map.erase( conn );
_init.erase( conn );
}
// protects _maps
mongo::mutex _mutex;
// a map from a connection into ChunkManager's sequence number for each namespace
map<DBClientBase*, map<string,unsigned long long> > _map;
set<DBClientBase*> _init;
} connectionShardStatus;
void resetShardVersion( DBClientBase * conn ) {
connectionShardStatus.reset( conn );
}
bool isVersionable( DBClientBase* conn ){
return conn->type() == ConnectionString::MASTER || conn->type() == ConnectionString::SET;
}
DBClientBase* getVersionable( DBClientBase* conn ){
switch ( conn->type() ) {
case ConnectionString::INVALID:
massert( 15904, str::stream() << "cannot set version on invalid connection " << conn->toString(), false );
return NULL;
case ConnectionString::MASTER:
return conn;
case ConnectionString::PAIR:
massert( 15905, str::stream() << "cannot set version or shard on pair connection " << conn->toString(), false );
return NULL;
case ConnectionString::SYNC:
massert( 15906, str::stream() << "cannot set version or shard on sync connection " << conn->toString(), false );
return NULL;
case ConnectionString::SET:
DBClientReplicaSet* set = (DBClientReplicaSet*) conn;
return &( set->masterConn() );
}
assert( false );
return NULL;
}
extern OID serverID;
bool initShardVersion( DBClientBase& conn_in, BSONObj& result ){
WriteBackListener::init( conn_in );
DBClientBase* conn = getVersionable( &conn_in );
assert( conn ); // errors thrown above
BSONObjBuilder cmdBuilder;
cmdBuilder.append( "setShardVersion" , "" );
cmdBuilder.appendBool( "init", true );
cmdBuilder.append( "configdb" , configServer.modelServer() );
cmdBuilder.appendOID( "serverID" , &serverID );
cmdBuilder.appendBool( "authoritative" , true );
BSONObj cmd = cmdBuilder.obj();
LOG(1) << "initializing shard connection to " << conn->toString() << endl;
LOG(2) << "initial sharding settings : " << cmd << endl;
bool ok = conn->runCommand( "admin" , cmd , result );
// Conn may be deleted here - *do not access again* - css is an exception, since just uses ptr address
connectionShardStatus.setInitialized( conn );
conn = NULL;
// HACK for backwards compatibility with v1.8.x, v2.0.0 and v2.0.1
// Result is false, but will still initialize serverID and configdb
// Not master does not initialize serverID and configdb, but we ignore since if the connection is not master,
// we are not setting the shard version at all
if( ! ok && ! result["errmsg"].eoo() && ( result["errmsg"].String() == "need to specify namespace"/* 2.0.1/2 */ ||
result["errmsg"].String() == "need to speciy namespace" /* 1.8 */ ||
result["errmsg"].String() == "not master" /* both */ ) )
{
ok = true;
}
LOG(3) << "initial sharding result : " << result << endl;
return ok;
}
/**
* @return true if had to do something
*/
bool checkShardVersion( DBClientBase& conn_in , const string& ns , bool authoritative , int tryNumber ) {
// TODO: cache, optimize, etc...
WriteBackListener::init( conn_in );
DBConfigPtr conf = grid.getDBConfig( ns );
if ( ! conf )
return false;
DBClientBase* conn = getVersionable( &conn_in );
assert(conn); // errors thrown above
if( ! connectionShardStatus.isInitialized( conn ) ){
BSONObj result;
uassert( 15918, str::stream() << "cannot initialize version on shard " << conn->getServerAddress() << causedBy( result.toString() ), initShardVersion( *conn, result ) );
}
unsigned long long officialSequenceNumber = 0;
ChunkManagerPtr manager;
const bool isSharded = conf->isSharded( ns );
if ( isSharded ) {
manager = conf->getChunkManagerIfExists( ns , authoritative );
// It's possible the chunk manager was reset since we checked whether sharded was true,
// so must check this here.
if( manager ) officialSequenceNumber = manager->getSequenceNumber();
}
// has the ChunkManager been reloaded since the last time we updated the connection-level version?
// (ie., last time we issued the setShardVersions below)
unsigned long long sequenceNumber = connectionShardStatus.getSequence(conn,ns);
if ( sequenceNumber == officialSequenceNumber ) {
return false;
}
ShardChunkVersion version = 0;
if ( isSharded && manager ) {
version = manager->getVersion( Shard::make( conn->getServerAddress() ) );
}
if( version == 0 ){
LOG(2) << "resetting shard version of " << ns << " on " << conn->getServerAddress() << ", " <<
( ! isSharded ? "no longer sharded" :
( ! manager ? "no chunk manager found" :
"version is zero" ) ) << endl;
}
LOG(2) << " have to set shard version for conn: " << conn << " ns:" << ns
<< " my last seq: " << sequenceNumber << " current: " << officialSequenceNumber
<< " version: " << version << " manager: " << manager.get()
<< endl;
BSONObj result;
// Save the server address, cannot access if fails
string serverAddress = conn->getServerAddress();
if ( setShardVersion( *conn , ns , version , authoritative , result ) ) {
// success!
LOG(1) << " setShardVersion success: " << result << endl;
connectionShardStatus.setSequence( conn , ns , officialSequenceNumber );
return true;
}
// At this point, it is no longer safe to use the pointer to conn, we do not know its state
conn = NULL;
LOG(1) << " setShardVersion failed!\n" << result << endl;
if ( result["need_authoritative"].trueValue() )
massert( 10428 , "need_authoritative set but in authoritative mode already" , ! authoritative );
if ( ! authoritative ) {
checkShardVersion( conn_in , ns , 1 , tryNumber + 1 );
return true;
}
if ( result["reloadConfig"].trueValue() ) {
if( result["version"].timestampTime() == 0 ){
// reload db
conf->reload();
}
else {
// reload config
conf->getChunkManager( ns , true );
}
}
const int maxNumTries = 7;
if ( tryNumber < maxNumTries ) {
LOG( tryNumber < ( maxNumTries / 2 ) ? 1 : 0 )
<< "going to retry checkShardVersion host: " << serverAddress << " " << result << endl;
sleepmillis( 10 * tryNumber );
checkShardVersion( conn_in , ns , true , tryNumber + 1 );
return true;
}
string errmsg = str::stream() << "setShardVersion failed host: " << serverAddress << " " << result;
log() << " " << errmsg << endl;
massert( 10429 , errmsg , 0 );
return true;
}
} // namespace mongo
|