summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEliot Horowitz <eliot@10gen.com>2009-02-19 13:26:25 -0500
committerEliot Horowitz <eliot@10gen.com>2009-02-19 13:26:25 -0500
commita55abd3fa2e443d62d4ed473df48468c6ea89e1c (patch)
treed2436cf4053ef7255b3ca54cb62eedf514e928db
parent524e8fd53cbde680c033c7ee3f0dff211224615d (diff)
downloadmongo-a55abd3fa2e443d62d4ed473df48468c6ea89e1c.tar.gz
lots of low level shard infrastructure in place now
-rw-r--r--jstests/sharding/shard1.js2
-rw-r--r--s/config.cpp11
-rw-r--r--s/config.h4
-rw-r--r--s/request.cpp26
-rw-r--r--s/request.h17
-rw-r--r--s/shard.cpp1
-rw-r--r--s/shard.h14
-rw-r--r--s/shardkey.cpp14
-rw-r--r--s/shardkey.h2
-rw-r--r--s/strategy_single.cpp4
10 files changed, 66 insertions, 29 deletions
diff --git a/jstests/sharding/shard1.js b/jstests/sharding/shard1.js
index 974fff6a5a5..893e6a12c47 100644
--- a/jstests/sharding/shard1.js
+++ b/jstests/sharding/shard1.js
@@ -27,7 +27,7 @@ si = s.config.sharding.findOne();
assert( si );
assert.eq( si.ns , "test.foo" );
-//assert.eq( 3 , db.foo.find().length() , "after sharding, no split count failed" );
+assert.eq( 3 , db.foo.find().length() , "after sharding, no split count failed" );
s.stop();
diff --git a/s/config.cpp b/s/config.cpp
index a27032313bb..cf1c15bebd4 100644
--- a/s/config.cpp
+++ b/s/config.cpp
@@ -83,6 +83,17 @@ namespace mongo {
}
+ ShardInfo* DBConfig::getShardInfo( const string& ns ){
+ ShardInfo* info = _shards[ns];
+ if ( info )
+ return info;
+ info = new ShardInfo( this );
+ if ( ! info->loadByName( ns ) )
+ throw UserException( (string)"can't load shard info for: " + ns );
+ _shards[ns] = info;
+ return info;
+ }
+
void DBConfig::serialize(BSONObjBuilder& to){
to.append("name", _name);
to.appendBool("partitioned", _partitioned );
diff --git a/s/config.h b/s/config.h
index f3a72284b68..ec741e97a87 100644
--- a/s/config.h
+++ b/s/config.h
@@ -53,12 +53,14 @@ namespace mongo {
void turnOnPartitioning();
ShardInfo* turnOnSharding( const string& ns , BSONObj fieldsAndOrder );
-
+
/**
* @return whether or not this partition is partitioned
*/
bool sharded( const string& ns );
+ ShardInfo* getShardInfo( const string& ns );
+
/**
* @return the correct for machine for the ns
* if the namespace is partitioned, will return an empty string
diff --git a/s/request.cpp b/s/request.cpp
index bcf80db7ea4..9a3be19ae79 100644
--- a/s/request.cpp
+++ b/s/request.cpp
@@ -27,6 +27,7 @@
#include "request.h"
#include "config.h"
+#include "shard.h"
namespace mongo {
@@ -34,10 +35,29 @@ namespace mongo {
assert( _d.getns() );
_id = _m.data->id;
_config = grid.getDBConfig( getns() );
+
+ if ( _config->sharded( getns() ) ){
+ _shardInfo = _config->getShardInfo( getns() );
+ uassert( (string)"no shard info for: " + getns() , _shardInfo );
+ }
+ else {
+ _shardInfo = 0;
+ }
}
+ string Request::singleServerName(){
+ if ( _shardInfo ){
+ if ( _shardInfo->numShards() > 1 )
+ throw UserException( "can't call singleServerName on a sharded collection" );
+ return _shardInfo->findShard( _shardInfo->getShardKey().globalMin() ).getServer();
+ }
+ string s = _config->getServer( getns() );
+ uassert( "can't call singleServerName on a sharded collection!" , s.size() > 0 );
+ return s;
+ }
+
void Request::process(){
-
+
int op = _m.data->operation();
assert( op > dbMsg );
@@ -67,6 +87,10 @@ namespace mongo {
_d.resetPull();
}
+ if ( _shardInfo && _shardInfo->numShards() > 1 ){
+ throw UserException( "can't do sharding yet silly" );
+ }
+
if ( op == dbQuery ) {
s->queryOp( *this );
diff --git a/s/request.h b/s/request.h
index d18c72a77d3..f251fbf1cad 100644
--- a/s/request.h
+++ b/s/request.h
@@ -13,6 +13,9 @@ namespace mongo {
public:
Request( Message& m, MessagingPort& p );
+ // ---- message info -----
+
+
const char * getns(){
return _d.getns();
}
@@ -30,16 +33,17 @@ namespace mongo {
DBConfig * getConfig(){
return _config;
}
+
+ // ---- remote location info -----
+
+ string singleServerName();
+
const char * primaryName(){
return _config->getPrimary().c_str();
}
- string singleServerName(){
- string s = _config->getServer( getns() );
- uassert( "sharded!" , s.size() > 0 );
- return s;
- }
+ // ---- low level access ----
void reply( Message & response ){
_p.reply( _m , response , _id );
@@ -55,9 +59,10 @@ namespace mongo {
Message& _m;
DbMessage _d;
MessagingPort& _p;
-
+
MSGID _id;
DBConfig * _config;
+ ShardInfo * _shardInfo;
};
class Strategy {
diff --git a/s/shard.cpp b/s/shard.cpp
index 958b421f92e..9682676fd35 100644
--- a/s/shard.cpp
+++ b/s/shard.cpp
@@ -104,6 +104,7 @@ namespace mongo {
}
Shard& ShardInfo::findShard( const BSONObj & obj ){
+
for ( vector<Shard*>::iterator i=_shards.begin(); i != _shards.end(); i++ ){
Shard * s = *i;
if ( s->contains( obj ) )
diff --git a/s/shard.h b/s/shard.h
index 56780c5eddc..0e014c3f5fe 100644
--- a/s/shard.h
+++ b/s/shard.h
@@ -92,20 +92,14 @@ namespace mongo {
return _ns;
}
+ int numShards(){ return _shards.size(); }
Shard& findShard( const BSONObj & obj );
-
- ShardKey& getShardKey(){
- return _key;
- }
-
- virtual const char * getNS() {
- return "config.sharding";
- }
-
+ ShardKey& getShardKey(){ return _key; }
+
+ virtual const char * getNS(){ return "config.sharding"; }
virtual void serialize(BSONObjBuilder& to);
virtual void unserialize(BSONObj& from);
virtual string modelServer();
-
bool loadByName( const string& ns );
string toString() const;
diff --git a/s/shardkey.cpp b/s/shardkey.cpp
index 312d07871cc..e0e14cc8f7a 100644
--- a/s/shardkey.cpp
+++ b/s/shardkey.cpp
@@ -33,12 +33,12 @@ namespace mongo {
_init();
}
else {
- _fieldName = 0;
+ _fieldName = "";
}
}
void ShardKey::init( BSONObj fieldsAndOrder ){
- _fieldsAndOrder = fieldsAndOrder;
+ _fieldsAndOrder = fieldsAndOrder.copy();
_init();
}
@@ -50,18 +50,18 @@ namespace mongo {
void ShardKey::globalMin( BSONObjBuilder& b ){
- uassert( "not valid yet" , _fieldName );
+ uassert( "not valid yet" , _fieldName.size() );
b << _fieldName << (int)(-0xfffffff);
}
void ShardKey::globalMax( BSONObjBuilder& b ){
- uassert( "not valid yet" , _fieldName );
+ uassert( "not valid yet" , _fieldName.size() );
b << _fieldName << (int)(0xfffffff);
}
int ShardKey::compare( const BSONObj& lObject , const BSONObj& rObject ) const {
- uassert( "not valid yet" , _fieldName );
-
+ uassert( "not valid yet" , _fieldName.size() );
+
BSONElement lElement = lObject[ _fieldsAndOrder.firstElement().fieldName() ];
uassert( "left key doesn't have the shard key" , ! lElement.eoo() );
uassert( "left key isn't number" , lElement.isNumber() );
@@ -88,7 +88,7 @@ namespace mongo {
uassert( "right key doesn't have the shard key" , ! rElement.eoo() );
uassert( "right key isn't number" , rElement.isNumber() );
- b.append( _fieldName , ( lElement.number() + rElement.number() ) / 2 );
+ b.append( _fieldName.c_str() , ( lElement.number() + rElement.number() ) / 2 );
}
string ShardKey::toString() const {
diff --git a/s/shardkey.h b/s/shardkey.h
index 3cf2ab508d7..1c5c57268b9 100644
--- a/s/shardkey.h
+++ b/s/shardkey.h
@@ -48,6 +48,6 @@ namespace mongo {
private:
void _init();
BSONObj _fieldsAndOrder;
- const char * _fieldName;
+ string _fieldName;
};
}
diff --git a/s/strategy_single.cpp b/s/strategy_single.cpp
index d300617d022..5a4e0c81b02 100644
--- a/s/strategy_single.cpp
+++ b/s/strategy_single.cpp
@@ -34,7 +34,7 @@ namespace mongo {
DBClientConnection&c = dynamic_cast<DBClientConnection&>(_c);
Message response;
bool ok = c.port().call( r.m(), response);
- uassert("dbgrid: error calling db", ok);
+ uassert("mongos: error calling db", ok);
lateAssert = true;
r.reply( response );
dbcon.done();
@@ -42,7 +42,7 @@ namespace mongo {
catch ( AssertionException& e ) {
assert( !lateAssert );
BSONObjBuilder err;
- err.append("$err", string("dbgrid ") + (e.msg.empty() ? "dbgrid assertion during query" : e.msg));
+ err.append("$err", string("mongos: ") + (e.msg.empty() ? "assertion during query" : e.msg));
BSONObj errObj = err.done();
replyToQuery(QueryResult::ResultFlag_ErrSet, r.p() , r.m() , errObj);
return;