summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEliot Horowitz <eliot@10gen.com>2009-02-08 14:59:12 -0500
committerEliot Horowitz <eliot@10gen.com>2009-02-08 14:59:12 -0500
commitbd1474e0df0fbcb71d87854e2bacc4b53c1a5a00 (patch)
treecafe2faebfe41b02af12ac3d25efad7a9d72e5ee
parent61e3d021f2f21b09d99d6570f2a6e3539cc052b0 (diff)
parent7892e1771526827ba2d447e111eab181c11da5fc (diff)
downloadmongo-bd1474e0df0fbcb71d87854e2bacc4b53c1a5a00.tar.gz
Merge branch 'master' of git.10gen.com:/data/gitroot/p
-rw-r--r--client/model.cpp7
-rw-r--r--client/model.h8
-rw-r--r--dbgrid/configserver.cpp23
-rw-r--r--dbgrid/configserver.h19
-rw-r--r--dbgrid/dbgrid_commands.cpp18
-rw-r--r--dbgrid/gridconfig.cpp25
-rw-r--r--dbgrid/gridconfig.h48
-rw-r--r--dbgrid/request.h2
-rw-r--r--dbgrid/server.cpp3
-rw-r--r--dbgrid/shard.cpp5
-rw-r--r--dbgrid/shard.h2
11 files changed, 57 insertions, 103 deletions
diff --git a/client/model.cpp b/client/model.cpp
index dd7049e7fa3..f8a5e596f40 100644
--- a/client/model.cpp
+++ b/client/model.cpp
@@ -18,11 +18,16 @@
#include "stdafx.h"
#include "model.h"
+#include "connpool.h"
namespace mongo {
bool Model::load(BSONObj& query) {
- BSONObj b = conn()->findOne(getNS(), query);
+ ScopedDbConnection scoped( modelServer() );
+ DBClientWithCommands& conn = scoped.conn();
+ BSONObj b = conn.findOne(getNS(), query);
+ scoped.done();
+
if ( b.isEmpty() )
return false;
diff --git a/client/model.h b/client/model.h
index 049393a010c..b46deb01855 100644
--- a/client/model.h
+++ b/client/model.h
@@ -43,12 +43,12 @@ namespace mongo {
virtual void unserialize(BSONObj& from) = 0;
/** Define this as you see fit if you are using the default conn() implementation. */
- static DBClientWithCommands *globalConn;
+ static string defaultServer;
/** Override this if you need to do fancier connection management than simply using globalConn. */
- virtual DBClientWithCommands* conn(){
- uassert( "globalConn not set" , globalConn );
- return globalConn;
+ virtual string modelServer(){
+ uassert( "defaultServer not set" , defaultServer.size() );
+ return defaultServer;
}
/** Load a single object.
diff --git a/dbgrid/configserver.cpp b/dbgrid/configserver.cpp
index 728601a039a..dfc93b8dad3 100644
--- a/dbgrid/configserver.cpp
+++ b/dbgrid/configserver.cpp
@@ -23,17 +23,12 @@
namespace mongo {
ConfigServer::ConfigServer() {
- _conn = 0;
-
_partitioned = false;
- _primary = 0;
+ _primary = "";
_name = "grid";
}
ConfigServer::~ConfigServer() {
- if ( _conn )
- delete _conn;
- _conn = 0; // defensive
}
bool ConfigServer::init( vector<string> configHosts , bool infer ){
@@ -114,28 +109,20 @@ namespace mongo {
Nullstream& l = log();
l << "connecting to griddb ";
-
- bool ok;
+
if ( !hostRight.empty() ) {
// connect in paired mode
l << "L:" << left << " R:" << right << "...";
l.flush();
- DBClientPaired *dbp = new DBClientPaired();
- _conn = dbp;
- ok = dbp->connect(left.c_str(),right.c_str());
- uassert( "paired config server not supported yet" , 0 );
+ _primary = left + "," + right;
}
else {
l << left << "...";
l.flush();
- DBClientConnection *dcc = new DBClientConnection(/*autoreconnect=*/true);
- _conn = dcc;
- string errmsg;
- ok = dcc->connect(left.c_str(), errmsg);
- _primary = Machine::get( left.c_str() );
+ _primary = left;
}
- return ok;
+ return true;
}
ConfigServer configServer;
diff --git a/dbgrid/configserver.h b/dbgrid/configserver.h
index 878ac882486..854f3543efc 100644
--- a/dbgrid/configserver.h
+++ b/dbgrid/configserver.h
@@ -38,26 +38,19 @@ namespace mongo {
~ConfigServer();
bool ok(){
- return _conn != 0;
+ // TODO: check can connect
+ return _primary.size() > 0;
}
-
- DBClientWithCommands* conn(){
- assert( _conn );
- return _conn;
+
+ virtual string modelServer(){
+ uassert( "ConfigServer not setup" , _primary.size() );
+ return _primary;
}
/**
call at startup, this will initiate connection to the grid db
*/
bool init( vector<string> configHosts , bool infer );
-
- string toString() {
- return _conn->toString();
- }
-
- private:
- DBClientWithCommands* _conn;
-
};
extern ConfigServer configServer;
diff --git a/dbgrid/dbgrid_commands.cpp b/dbgrid/dbgrid_commands.cpp
index 90055dd8e3b..36755df34d7 100644
--- a/dbgrid/dbgrid_commands.cpp
+++ b/dbgrid/dbgrid_commands.cpp
@@ -64,7 +64,7 @@ namespace mongo {
public:
NetStatCmd() : GridAdminCmd("netstat") { }
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
- result.append("griddb", configServer.toString());
+ result.append("configserver", configServer.getPrimary() );
result.append("isdbgrid", 1);
return true;
}
@@ -102,10 +102,11 @@ namespace mongo {
public:
ListServers() : GridAdminCmd("listservers") { }
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
- DBClientWithCommands * conn = configServer.conn();
+ ScopedDbConnection dbcon( configServer.getPrimary() );
+ DBClientWithCommands& conn = dbcon.conn();
vector<BSONObj> all;
- auto_ptr<DBClientCursor> cursor = conn->query( "config.servers" , emptyObj );
+ auto_ptr<DBClientCursor> cursor = conn.query( "config.servers" , emptyObj );
while ( cursor->more() ){
BSONObj o = cursor->next();
all.push_back( o );
@@ -113,6 +114,8 @@ namespace mongo {
result.append("servers" , all );
result.append("ok" , 1 );
+ dbcon.done();
+
return true;
}
} listServers;
@@ -122,20 +125,23 @@ namespace mongo {
public:
AddServer() : GridAdminCmd("addserver") { }
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
- DBClientWithCommands * conn = configServer.conn();
+ ScopedDbConnection dbcon( configServer.getPrimary() );
+ DBClientWithCommands& conn = dbcon.conn();
BSONObj server = BSON( "host" << cmdObj["addserver"].valuestrsafe() );
- BSONObj old = conn->findOne( "config.servers" , server );
+ BSONObj old = conn.findOne( "config.servers" , server );
if ( ! old.isEmpty() ){
result.append( "ok" , 0.0 );
result.append( "msg" , "already exists" );
+ dbcon.done();
return false;
}
- conn->insert( "config.servers" , server );
+ conn.insert( "config.servers" , server );
result.append( "ok", 1 );
result.append( "added" , server["host"].valuestrsafe() );
+ dbcon.done();
return true;
}
} listMachines;
diff --git a/dbgrid/gridconfig.cpp b/dbgrid/gridconfig.cpp
index dae08f6470e..8c256b213cb 100644
--- a/dbgrid/gridconfig.cpp
+++ b/dbgrid/gridconfig.cpp
@@ -27,12 +27,12 @@
namespace mongo {
- /* --- Machine --- */
-
- map<string, Machine*> Machine::machines;
-
/* --- DBConfig --- */
+ string DBConfig::modelServer() {
+ return configServer.modelServer();
+ }
+
bool DBConfig::partitioned( const NamespaceString& ns ){
if ( ! _partitioned )
return false;
@@ -40,27 +40,24 @@ namespace mongo {
return 0;
}
- Machine * DBConfig::getMachine( const NamespaceString& ns ){
+ string DBConfig::getServer( const NamespaceString& ns ){
if ( partitioned( ns ) )
return 0;
- uassert( "no primary!" , _primary );
+ uassert( "no primary!" , _primary.size() );
return _primary;
}
void DBConfig::serialize(BSONObjBuilder& to){
to.append("name", _name);
to.appendBool("partitioned", _partitioned );
- if ( _primary )
- to.append("primary", _primary->getName() );
+ to.append("primary", _primary );
}
void DBConfig::unserialize(BSONObj& from){
_name = from.getStringField("name");
_partitioned = from.getBoolField("partitioned");
- string p = from.getStringField("primary");
- if ( ! p.empty() )
- _primary = Machine::get(p);
+ _primary = from.getStringField("primary");
}
bool DBConfig::loadByName(const char *nm){
@@ -70,10 +67,6 @@ namespace mongo {
return load(q);
}
- DBClientWithCommands* GridConfigModel::conn(){
- return configServer.conn();
- }
-
/* --- Grid --- */
DBConfig* Grid::getDBConfig( string database ){
@@ -88,7 +81,7 @@ namespace mongo {
DBConfig*& cc = _databases[database];
if ( cc == 0 ) {
- cc = new DBConfig();
+ cc = new DBConfig( database );
if ( !cc->loadByName(database.c_str()) ) {
// note here that cc->primary == 0.
log() << "couldn't find database [" << database << "] in config db" << endl;
diff --git a/dbgrid/gridconfig.h b/dbgrid/gridconfig.h
index e5238098fe0..44092bc625e 100644
--- a/dbgrid/gridconfig.h
+++ b/dbgrid/gridconfig.h
@@ -30,44 +30,11 @@
namespace mongo {
/**
- abstract class for Models that need to talk to the config db
- */
- class GridConfigModel : public Model {
- public:
- virtual DBClientWithCommands* conn();
- };
-
- /**
- Machine is the concept of a host that runs the db process.
- */
- class Machine {
- static map<string, Machine*> machines;
- string name;
- public:
- string getName() const {
- return name;
- }
-
- Machine(string _name) : name(_name) { }
-
- enum {
- Port = 27018 /* default port # for dbs that are downstream of a dbgrid */
- };
-
- static Machine* get(string name) {
- map<string,Machine*>::iterator i = machines.find(name);
- if ( i != machines.end() )
- return i->second;
- return machines[name] = new Machine(name);
- }
- };
-
- /**
top level grid configuration for an entire database
*/
- class DBConfig : public GridConfigModel {
+ class DBConfig : public Model {
public:
- DBConfig() : _primary(0), _partitioned(false){ }
+ DBConfig( string name = "" ) : _name( name ) , _primary("") , _partitioned(false){ }
/**
* @return whether or not this partition is partitioned
@@ -78,13 +45,16 @@ namespace mongo {
* returns the correct for machine for the ns
* if this namespace is partitioned, will return NULL
*/
- Machine * getMachine( const NamespaceString& ns );
+ string getServer( const NamespaceString& ns );
- Machine * getPrimary(){
- uassert( "no primary" , _primary );
+ string getPrimary(){
+ if ( _primary.size() == 0 )
+ throw UserException( (string)"no primary server configured for db: " + _name );
return _primary;
}
+ virtual string modelServer();
+
// model stuff
virtual const char * getNS(){ return "config.db.database"; }
@@ -94,7 +64,7 @@ namespace mongo {
protected:
string _name; // e.g. "alleyinsider"
- Machine * _primary;
+ string _primary; // e.g. localhost , mongo.foo.com:9999
bool _partitioned;
};
diff --git a/dbgrid/request.h b/dbgrid/request.h
index 737f5f6dd9f..a7ea125f355 100644
--- a/dbgrid/request.h
+++ b/dbgrid/request.h
@@ -26,7 +26,7 @@ namespace mongo {
}
const char * primaryName(){
- return _config->getPrimary()->getName().c_str();
+ return _config->getPrimary().c_str();
}
void reply( Message & response ){
diff --git a/dbgrid/server.cpp b/dbgrid/server.cpp
index e6087ca41a0..36d6f4da080 100644
--- a/dbgrid/server.cpp
+++ b/dbgrid/server.cpp
@@ -30,7 +30,7 @@ namespace mongo {
int port = 27017;
const char *curNs = "";
Database *database = 0;
- DBClientWithCommands* Model::globalConn;
+ string Model::defaultServer;
string ourHostname;
string getDbContext() {
@@ -159,7 +159,6 @@ int main(int argc, char* argv[], char *envp[] ) {
}
assert( configServer.ok() );
- Model::globalConn = 0;
start();
dbexit(0);
diff --git a/dbgrid/shard.cpp b/dbgrid/shard.cpp
index e708a2539dc..ad13290a593 100644
--- a/dbgrid/shard.cpp
+++ b/dbgrid/shard.cpp
@@ -22,8 +22,9 @@
namespace mongo {
- DBClientWithCommands* Shard::conn() {
- return configServer.conn();
+ string Shard::modelServer() {
+ // TODO: this could move around?
+ return configServer.modelServer();
}
void Shard::serialize(BSONObjBuilder& to) {
diff --git a/dbgrid/shard.h b/dbgrid/shard.h
index 1cf3a626166..1a1f3312318 100644
--- a/dbgrid/shard.h
+++ b/dbgrid/shard.h
@@ -40,7 +40,7 @@ namespace mongo {
}
virtual void serialize(BSONObjBuilder& to);
virtual void unserialize(BSONObj& from);
- virtual DBClientWithCommands* conn();
+ virtual string modelServer();
};
} // namespace mongo