2009-11-03 16:35:48 +01:00
|
|
|
// parallel.h
|
|
|
|
|
|
|
|
/* Copyright 2009 10gen Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
tools for wokring in parallel/sharded/clustered environment
|
|
|
|
*/
|
|
|
|
|
2009-11-16 21:55:03 +01:00
|
|
|
#include "../stdafx.h"
|
2009-11-03 16:35:48 +01:00
|
|
|
#include "dbclient.h"
|
|
|
|
#include "../db/dbmessage.h"
|
2010-04-13 21:23:57 +02:00
|
|
|
#include "../db/matcher.h"
|
2009-11-03 16:35:48 +01:00
|
|
|
|
|
|
|
namespace mongo {
|
|
|
|
|
2009-11-16 21:53:40 +01:00
|
|
|
/**
|
|
|
|
* this is a cursor that works over a set of servers
|
|
|
|
* can be used in serial/paralellel as controlled by sub classes
|
|
|
|
*/
|
2009-11-03 16:35:48 +01:00
|
|
|
class ClusteredCursor {
|
|
|
|
public:
|
|
|
|
ClusteredCursor( QueryMessage& q );
|
|
|
|
ClusteredCursor( const string& ns , const BSONObj& q , int options=0 , const BSONObj& fields=BSONObj() );
|
|
|
|
virtual ~ClusteredCursor();
|
|
|
|
|
|
|
|
virtual bool more() = 0;
|
|
|
|
virtual BSONObj next() = 0;
|
|
|
|
|
|
|
|
static BSONObj concatQuery( const BSONObj& query , const BSONObj& extraFilter );
|
|
|
|
|
2010-01-15 07:27:01 +01:00
|
|
|
virtual string type() const = 0;
|
|
|
|
|
2009-11-03 16:35:48 +01:00
|
|
|
protected:
|
|
|
|
auto_ptr<DBClientCursor> query( const string& server , int num = 0 , BSONObj extraFilter = BSONObj() );
|
|
|
|
|
|
|
|
static BSONObj _concatFilter( const BSONObj& filter , const BSONObj& extraFilter );
|
|
|
|
|
|
|
|
string _ns;
|
|
|
|
BSONObj _query;
|
|
|
|
int _options;
|
|
|
|
BSONObj _fields;
|
|
|
|
|
|
|
|
bool _done;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-11-16 21:53:40 +01:00
|
|
|
/**
|
|
|
|
* holder for a server address and a query to run
|
|
|
|
*/
|
2009-11-03 16:35:48 +01:00
|
|
|
class ServerAndQuery {
|
|
|
|
public:
|
|
|
|
ServerAndQuery( const string& server , BSONObj extra = BSONObj() , BSONObj orderObject = BSONObj() ) :
|
|
|
|
_server( server ) , _extra( extra.getOwned() ) , _orderObject( orderObject.getOwned() ){
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<( const ServerAndQuery& other ) const{
|
|
|
|
if ( ! _orderObject.isEmpty() )
|
|
|
|
return _orderObject.woCompare( other._orderObject ) < 0;
|
|
|
|
|
|
|
|
if ( _server < other._server )
|
|
|
|
return true;
|
|
|
|
if ( other._server > _server )
|
|
|
|
return false;
|
|
|
|
return _extra.woCompare( other._extra ) < 0;
|
|
|
|
}
|
|
|
|
|
2010-01-14 16:41:41 +01:00
|
|
|
string toString() const {
|
|
|
|
StringBuilder ss;
|
|
|
|
ss << "server:" << _server << " _extra:" << _extra << " _orderObject:" << _orderObject;
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2010-01-15 07:27:01 +01:00
|
|
|
operator string() const {
|
|
|
|
return toString();
|
|
|
|
}
|
|
|
|
|
2009-11-03 16:35:48 +01:00
|
|
|
string _server;
|
|
|
|
BSONObj _extra;
|
|
|
|
BSONObj _orderObject;
|
|
|
|
};
|
|
|
|
|
2010-04-13 21:23:57 +02:00
|
|
|
class FilteringClientCursor {
|
|
|
|
public:
|
|
|
|
FilteringClientCursor( const BSONObj filter = BSONObj() );
|
|
|
|
FilteringClientCursor( auto_ptr<DBClientCursor> cursor , const BSONObj filter = BSONObj() );
|
|
|
|
~FilteringClientCursor();
|
|
|
|
|
|
|
|
void reset( auto_ptr<DBClientCursor> cursor );
|
|
|
|
|
|
|
|
bool more();
|
|
|
|
BSONObj next();
|
2010-04-14 19:57:51 +02:00
|
|
|
|
|
|
|
BSONObj peek();
|
2010-04-13 21:23:57 +02:00
|
|
|
private:
|
|
|
|
void _advance();
|
|
|
|
|
|
|
|
Matcher _matcher;
|
|
|
|
auto_ptr<DBClientCursor> _cursor;
|
|
|
|
|
|
|
|
BSONObj _next;
|
2010-04-14 19:46:54 +02:00
|
|
|
bool _done;
|
2010-04-13 21:23:57 +02:00
|
|
|
};
|
2009-11-16 21:53:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* runs a query in serial across any number of servers
|
|
|
|
* returns all results from 1 server, then the next, etc...
|
|
|
|
*/
|
2009-11-03 16:35:48 +01:00
|
|
|
class SerialServerClusteredCursor : public ClusteredCursor {
|
|
|
|
public:
|
2010-02-08 10:53:32 +01:00
|
|
|
SerialServerClusteredCursor( const set<ServerAndQuery>& servers , QueryMessage& q , int sortOrder=0);
|
2009-11-03 16:35:48 +01:00
|
|
|
virtual bool more();
|
|
|
|
virtual BSONObj next();
|
2010-01-15 07:27:01 +01:00
|
|
|
virtual string type() const { return "SerialServer"; }
|
2009-11-03 16:35:48 +01:00
|
|
|
private:
|
|
|
|
vector<ServerAndQuery> _servers;
|
|
|
|
unsigned _serverIndex;
|
|
|
|
|
2010-04-13 21:23:57 +02:00
|
|
|
FilteringClientCursor _current;
|
2010-04-14 22:15:57 +02:00
|
|
|
|
|
|
|
int _needToSkip;
|
2009-11-03 16:35:48 +01:00
|
|
|
};
|
2009-11-16 21:53:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* runs a query in parellel across N servers
|
|
|
|
* sots
|
|
|
|
*/
|
2009-11-03 16:35:48 +01:00
|
|
|
class ParallelSortClusteredCursor : public ClusteredCursor {
|
|
|
|
public:
|
2010-02-08 10:53:32 +01:00
|
|
|
ParallelSortClusteredCursor( const set<ServerAndQuery>& servers , QueryMessage& q , const BSONObj& sortKey );
|
|
|
|
ParallelSortClusteredCursor( const set<ServerAndQuery>& servers , const string& ns ,
|
2009-11-03 17:40:00 +01:00
|
|
|
const Query& q , int options=0, const BSONObj& fields=BSONObj() );
|
2009-11-03 16:35:48 +01:00
|
|
|
virtual ~ParallelSortClusteredCursor();
|
|
|
|
virtual bool more();
|
|
|
|
virtual BSONObj next();
|
2010-01-15 07:27:01 +01:00
|
|
|
virtual string type() const { return "ParallelSort"; }
|
2009-11-03 16:35:48 +01:00
|
|
|
private:
|
2009-11-03 17:40:00 +01:00
|
|
|
void _init();
|
|
|
|
|
2009-11-03 16:35:48 +01:00
|
|
|
int _numServers;
|
|
|
|
set<ServerAndQuery> _servers;
|
|
|
|
BSONObj _sortKey;
|
2010-04-13 21:23:57 +02:00
|
|
|
|
|
|
|
FilteringClientCursor * _cursors;
|
2010-04-14 22:15:57 +02:00
|
|
|
int _needToSkip;
|
2009-11-03 16:35:48 +01:00
|
|
|
};
|
|
|
|
|
2009-11-16 21:53:40 +01:00
|
|
|
/**
|
|
|
|
* tools for doing asynchronous operations
|
|
|
|
* right now uses underlying sync network ops and uses another thread
|
|
|
|
* should be changed to use non-blocking io
|
|
|
|
*/
|
2009-11-04 04:53:32 +01:00
|
|
|
class Future {
|
|
|
|
public:
|
|
|
|
class CommandResult {
|
|
|
|
public:
|
|
|
|
|
|
|
|
string getServer() const { return _server; }
|
|
|
|
|
|
|
|
bool isDone() const { return _done; }
|
|
|
|
|
|
|
|
bool ok() const {
|
|
|
|
assert( _done );
|
|
|
|
return _ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
BSONObj result() const {
|
|
|
|
assert( _done );
|
|
|
|
return _res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
blocks until command is done
|
|
|
|
returns ok()
|
|
|
|
*/
|
|
|
|
bool join();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
CommandResult( const string& server , const string& db , const BSONObj& cmd );
|
|
|
|
|
|
|
|
string _server;
|
|
|
|
string _db;
|
|
|
|
BSONObj _cmd;
|
|
|
|
|
|
|
|
boost::thread _thr;
|
|
|
|
|
|
|
|
BSONObj _res;
|
|
|
|
bool _done;
|
|
|
|
bool _ok;
|
|
|
|
|
|
|
|
friend class Future;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void commandThread();
|
|
|
|
|
|
|
|
static shared_ptr<CommandResult> spawnCommand( const string& server , const string& db , const BSONObj& cmd );
|
|
|
|
|
|
|
|
private:
|
|
|
|
static shared_ptr<CommandResult> * _grab;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-11-03 16:35:48 +01:00
|
|
|
}
|