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

starting to look for actual locations

This commit is contained in:
Eliot Horowitz 2009-02-06 15:44:21 -05:00
parent ee6c8943a5
commit 83174a1a08
7 changed files with 129 additions and 75 deletions

View File

@ -24,6 +24,10 @@ namespace mongo {
ConfigServer::ConfigServer() {
_conn = 0;
_partitioned = false;
_primary = 0;
_name = "grid";
}
ConfigServer::~ConfigServer() {
@ -119,6 +123,7 @@ namespace mongo {
DBClientPaired *dbp = new DBClientPaired();
_conn = dbp;
ok = dbp->connect(left.c_str(),right.c_str());
uassert( "paired config server not supported yet" , 0 );
}
else {
l << left << "...";
@ -127,6 +132,7 @@ namespace mongo {
_conn = dcc;
string errmsg;
ok = dcc->connect(left.c_str(), errmsg);
_primary = Machine::get( left.c_str() );
}
return ok;

View File

@ -25,10 +25,11 @@
#pragma once
#include "../client/dbclient.h"
#include "gridconfig.h"
namespace mongo {
class ConfigServer {
class ConfigServer : public DBConfig {
public:
enum { Port = 27016 }; /* standard port # for a grid db */
@ -39,7 +40,7 @@ namespace mongo {
bool ok(){
return _conn != 0;
}
DBClientWithCommands* conn(){
assert( _conn );
return _conn;

View File

@ -20,9 +20,10 @@
#include "../util/message.h"
#include "../util/unittest.h"
#include "../client/connpool.h"
#include "../client/model.h"
#include "../db/pdfile.h"
#include "gridconfig.h"
#include "../client/model.h"
#include "configserver.h"
namespace mongo {
@ -30,10 +31,51 @@ namespace mongo {
map<string, Machine*> Machine::machines;
/* --- DBConfig --- */
bool DBConfig::partitioned( const NamespaceString& ns ){
if ( ! _partitioned )
return false;
uassert( "don't know what to do!" , 0 );
return 0;
}
Machine * DBConfig::getMachine( const NamespaceString& ns ){
if ( partitioned( ns ) )
return 0;
uassert( "no primary!" , _primary );
return _primary;
}
void DBConfig::serialize(BSONObjBuilder& to){
to.append("name", _name);
to.appendBool("partitioned", _partitioned );
if ( _primary )
to.append("primary", _primary->getName() );
}
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);
}
bool DBConfig::loadByName(const char *nm){
BSONObjBuilder b;
b.append("name", nm);
BSONObj q = b.done();
return load(q);
}
DBClientWithCommands* GridConfigModel::conn(){
return configServer.conn();
}
/* --- Grid --- */
Grid grid;
DBConfig* Grid::getDBConfig( string database ){
{
string::size_type i = database.find( "." );
@ -41,6 +83,9 @@ namespace mongo {
database = database.substr( 0 , i );
}
if ( database == "grid" )
return &configServer;
DBConfig*& cc = _databases[database];
if ( cc == 0 ) {
cc = new DBConfig();
@ -53,18 +98,6 @@ namespace mongo {
return cc;
}
Machine* Grid::owner(const char *ns, BSONObj& objOrKey) {
DBConfig *cc = getDBConfig( nsToClient(ns) );
uassert( string("dbgrid: no config for db for ") + ns , cc );
if ( !cc->partitioned ) {
if ( !cc->primary )
throw UserAssertionException(string("dbgrid: no primary for ")+ns);
return cc->primary;
}
uassert("dbgrid: not implemented 100", false);
return 0;
}
Grid grid;
}

View File

@ -23,20 +23,22 @@
#pragma once
#include "../db/namespace.h"
#include "../client/dbclient.h"
#include "../client/model.h"
#include "configserver.h"
namespace mongo {
/**
abstract class for Models that need to talk to the config db
*/
class GridConfigModel : public Model {
public:
virtual DBClientWithCommands* conn(){
return configServer.conn();
}
virtual DBClientWithCommands* conn();
};
/* Machine is the concept of a host that runs the db process.
/**
Machine is the concept of a host that runs the db process.
*/
class Machine {
static map<string, Machine*> machines;
@ -60,49 +62,44 @@ namespace mongo {
}
};
//typedef map<string,Machine*> ObjLocs;
/* top level grid configuration for an entire database */
/**
top level grid configuration for an entire database
*/
class DBConfig : public GridConfigModel {
public:
string name; // e.g. "alleyinsider"
Machine *primary;
bool partitioned;
DBConfig() : _primary(0), _partitioned(false){ }
DBConfig() : primary(0), partitioned(false) { }
virtual const char * getNS() {
return "grid.db.database";
}
virtual void serialize(BSONObjBuilder& to) {
to.append("name", name);
to.appendBool("partitioned", partitioned);
if ( primary )
to.append("primary", primary->getName());
}
virtual void unserialize(BSONObj& from) {
name = from.getStringField("name");
partitioned = from.getBoolField("partitioned");
string p = from.getStringField("primary");
if ( !p.empty() )
primary = Machine::get(p);
/**
* @return whether or not this partition is partitioned
*/
bool partitioned( const NamespaceString& ns );
/**
* returns the correct for machine for the ns
* if this namespace is partitioned, will return NULL
*/
Machine * getMachine( const NamespaceString& ns );
Machine * getPrimary(){
uassert( "no primary" , _primary );
return _primary;
}
bool loadByName(const char *nm) {
BSONObjBuilder b;
b.append("name", nm);
BSONObj q = b.done();
return load(q);
}
// model stuff
virtual const char * getNS(){ return "grid.db.database"; }
virtual void serialize(BSONObjBuilder& to);
virtual void unserialize(BSONObj& from);
bool loadByName(const char *nm);
protected:
string _name; // e.g. "alleyinsider"
Machine * _primary;
bool _partitioned;
};
class Grid {
public:
/* return which machine "owns" the object in question -- ie which partition
we should go to.
*/
Machine* owner(const char *ns, BSONObj& objOrKey);
/**
gets the config the db.
will return an empty DBConfig if not in db already

View File

@ -43,25 +43,38 @@
#include "request.h"
#include "gridconfig.h"
#include "configserver.h"
namespace mongo {
Request::Request( Message& m, MessagingPort& p ) : _m(m) , _d( m ) , _p(p){
assert( _d.getns() );
_id = _m.data->id;
_config = grid.getDBConfig( getns() );
}
void processRequest(Message& m, MessagingPort& p) {
Request r( m , p );
int op = m.data->operation();
assert( op > dbMsg );
grid.getDBConfig( r.getns() );
if ( op == dbQuery ) {
SINGLE->queryOp( r );
}
else if ( op == dbGetMore ) {
SINGLE->getMore( r );
Strategy * s = 0;
if ( r.getConfig()->partitioned( r.getns() ) ){
uassert( "partitioned not supported" , 0 );
}
else {
SINGLE->writeOp( op, r );
s = SINGLE;
}
if ( op == dbQuery ) {
s->queryOp( r );
}
else if ( op == dbGetMore ) {
s->getMore( r );
}
else {
s->writeOp( op, r );
}
}

View File

@ -5,15 +5,13 @@
#include "../stdafx.h"
#include "../util/message.h"
#include "../db/dbmessage.h"
#include "gridconfig.h"
namespace mongo {
class Request {
public:
Request( Message& m, MessagingPort& p ) : _m(m) , _d( m ) , _p(p){
assert( _d.getns() );
_id = _m.data->id;
}
Request( Message& m, MessagingPort& p );
const char * getns(){
return _d.getns();
@ -23,6 +21,14 @@ namespace mongo {
return _id;
}
DBConfig * getConfig(){
return _config;
}
const char * primaryName(){
return _config->getPrimary()->getName().c_str();
}
void reply( Message & response ){
_p.reply( _m , response , _id );
}
@ -37,6 +43,7 @@ namespace mongo {
MessagingPort& _p;
MSGID _id;
DBConfig * _config;
};
class Strategy {

View File

@ -7,9 +7,6 @@
namespace mongo {
const char *tempHost = "localhost:27018";
class SingleStrategy : public Strategy {
virtual void queryOp( Request& r ){
@ -31,7 +28,7 @@ namespace mongo {
}
}
ScopedDbConnection dbcon(tempHost);
ScopedDbConnection dbcon( r.primaryName() );
DBClientConnection &c = dbcon.conn();
Message response;
bool ok = c.port().call( r.m(), response);
@ -56,7 +53,7 @@ namespace mongo {
log(3) << "getmore: " << ns << endl;
ScopedDbConnection dbcon(tempHost);
ScopedDbConnection dbcon( r.primaryName() );
DBClientConnection &c = dbcon.conn();
Message response;
@ -72,7 +69,7 @@ namespace mongo {
const char *ns = r.getns();
log(3) << "write: " << ns << endl;
ScopedDbConnection dbcon(tempHost);
ScopedDbConnection dbcon( r.primaryName() );
DBClientConnection &c = dbcon.conn();
c.port().say( r.m() );